info = { type = "image", title = "Sky village", category = "city", author = "BRRRO", version = "1.0", license = "MIT", desc = "Tiny villagers climb a tower on a floating isle", api = 33, } local tr = ({ sk = { ["Villagers"] = "Obyvatelia", ["Smoke"] = "Dym" }, cs = { ["Villagers"] = "Obyvatelé", ["Smoke"] = "Kouř" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "folk", label = T("Villagers"), type = "int", min = 1, max = 5, default = 4 }, { key = "smoke", label = T("Smoke"), type = "bool", default = true }, } local rect, blend, blend_rect, px = gfx.rect, gfx.blend, gfx.blend_rect, gfx.px local floor, sin, sqrt = math.floor, math.sin, math.sqrt local W, H = gfx.W, gfx.H local function rn(a, b) return math.hash32(a * 7919 + b * 104729) & 0x7FFF end local BG local WALL = { { 86, 96, 132 }, { 110, 100, 126 }, { 132, 112, 112 }, { 96, 112, 124 }, { 74, 84, 116 } } local ROOF = { { 150, 64, 72 }, { 70, 80, 140 }, { 122, 70, 110 }, { 60, 100, 110 } } local OFFWIN, ONWIN = { 30, 34, 62 }, { 255, 208, 110 } local PLANK, POST, LADDER = { 190, 176, 150 }, { 72, 56, 48 }, { 214, 150, 120 } local HOUSES = { { 9, 52, 6, 5, 1, 1 }, { 21, 52, 7, 5, 2, 2, true }, { 31, 52, 6, 5, 3, 3 }, { 40, 52, 7, 5, 4, 4 }, { 21, 43, 7, 5, 5, 2 }, { 33, 43, 6, 5, 1, 3, true }, { 19, 34, 5, 4, 3, 4 }, { 31, 34, 7, 5, 2, 1 }, { 30, 25, 7, 4, 4, 2, true }, { 38, 25, 5, 4, 1, 3 }, } local WINS, CHIM = {}, {} local function ladder(x, y0, y1) rect(x, y0, 1, y1 - y0 + 1, LADDER[1], LADDER[2], LADDER[3]) rect(x + 2, y0, 1, y1 - y0 + 1, LADDER[1], LADDER[2], LADDER[3]) for y = y0 + 1, y1, 2 do rect(x + 1, y, 1, 1, LADDER[1], LADDER[2], LADDER[3]) end end local function build_bg() rect(0, 0, W, 32, 14, 18, 44) for y = 32, H - 1 do local t = (y - 32) / 31 rect(0, y, W, 1, floor(14 + 9 * t), floor(18 + 14 * t), floor(44 + 32 * t)) end for i = 1, 26 do local x, y = rn(i, 1) % W, rn(i, 2) % 46 px(x, y, 120, 128, 186) end for dy = -5, 5 do for dx = -5, 5 do local d1, d2 = dx * dx + dy * dy, (dx - 3) * (dx - 3) + (dy + 1) * (dy + 1) if d1 <= 22 and d2 > 22 then px(51 + dx, 9 + dy, 244, 236, 196) end end end for dy = 0, 12 do local hw = floor(26 * sqrt(1 - (dy / 13) ^ 2) + 0.5) rect(32 - hw, 53 + dy, 2 * hw, 1, 118 - dy * 4, 78 - dy * 3, 64 - dy * 2) end for i = 1, 36 do local x, y = 8 + rn(i, 3) % 48, 54 + rn(i, 4) % 9 local dx, dy = (x - 32) / 25, (y - 53) / 12 if dx * dx + dy * dy < 0.85 then px(x, y, 64, 42, 46) end end rect(7, 52, 50, 1, 42, 86, 70) rect(14, 43, 38, 1, PLANK[1], PLANK[2], PLANK[3]) rect(18, 34, 31, 1, PLANK[1], PLANK[2], PLANK[3]) rect(22, 25, 23, 1, PLANK[1], PLANK[2], PLANK[3]) for _, p in ipairs({ { 14, 44, 51 }, { 51, 44, 51 }, { 18, 35, 42 }, { 48, 35, 42 }, { 22, 26, 33 }, { 44, 26, 33 } }) do rect(p[1], p[2], 1, p[3] - p[2] + 1, POST[1], POST[2], POST[3]) end ladder(16, 43, 52); ladder(44, 34, 43); ladder(24, 25, 34) for _, h in ipairs(HOUSES) do local x, yb, w, wh = h[1], h[2], h[3], h[4] local wl, rf = WALL[h[5]], ROOF[h[6]] rect(x, yb - wh, w, wh, wl[1], wl[2], wl[3]) rect(x + w - 1, yb - wh, 1, wh, wl[1] + 24, wl[2] + 24, wl[3] + 28) rect(x - 1, yb - wh - 1, w + 2, 1, rf[1], rf[2], rf[3]) rect(x, yb - wh - 2, w, 1, rf[1], rf[2], rf[3]) rect(x + 1, yb - wh - 3, w - 2, 1, rf[1] + 20, rf[2] + 14, rf[3] + 14) rect(x + w // 2, yb - 2, 1, 2, 40, 30, 36) WINS[#WINS + 1] = { x + 1, yb - wh + 1 } rect(x + 1, yb - wh + 1, 2, 2, OFFWIN[1], OFFWIN[2], OFFWIN[3]) if w >= 7 then WINS[#WINS + 1] = { x + w - 3, yb - wh + 1 } rect(x + w - 3, yb - wh + 1, 2, 2, OFFWIN[1], OFFWIN[2], OFFWIN[3]) end if h[7] then rect(x + 1, yb - wh - 4, 1, 3, 84, 56, 60) CHIM[#CHIM + 1] = { x + 1, yb - wh - 5 } end end BG = gfx.snapshot(0, 0, W, H) end local PATHS = { { 12, 52, 17, 52, 17, 43, 45, 43, 45, 34, 25, 34, 25, 25, 41, 25 }, { 50, 52, 17, 52, 17, 43, 29, 43 }, { 30, 43, 45, 43, 45, 34, 30, 34 }, { 23, 34, 25, 34, 25, 25, 36, 25 }, { 10, 52, 54, 52 }, } local SPEED = { 0.14, 0.12, 0.13, 0.11, 0.15 } local COATS = { { 226, 96, 96 }, { 96, 150, 236 }, { 236, 206, 96 }, { 110, 200, 140 }, { 214, 130, 220 } } local SKIN = { 240, 200, 170 } local LEN = {} for i, p in ipairs(PATHS) do local n = 0 for k = 3, #p - 1, 2 do n = n + math.abs(p[k] - p[k - 2]) + math.abs(p[k + 1] - p[k - 1]) end LEN[i] = n end local function dot(x, y, c, a) local ix, iy = floor(x), floor(y) local fx, fy = x - ix, y - iy local r, g, b = c[1], c[2], c[3] blend(ix, iy, r, g, b, (1 - fx) * (1 - fy) * a) if fx > 0.02 then blend(ix + 1, iy, r, g, b, fx * (1 - fy) * a) end if fy > 0.02 then blend(ix, iy + 1, r, g, b, (1 - fx) * fy * a) end if fx > 0.02 and fy > 0.02 then blend(ix + 1, iy + 1, r, g, b, fx * fy * a) end end local function villager(i) local p, L = PATHS[i], LEN[i] local d = (frame * SPEED[i] + rn(i, 5) % 200) % (2 * L) if d > L then d = 2 * L - d end local x, y, climbing = p[#p - 1], p[#p], false for k = 3, #p - 1, 2 do local dx, dy = p[k] - p[k - 2], p[k + 1] - p[k - 1] local sl = math.abs(dx) + math.abs(dy) if d <= sl then local f = d / sl x, y, climbing = p[k - 2] + dx * f, p[k - 1] + dy * f, dx == 0 break end d = d - sl end local c = COATS[i] dot(x, y - 2, SKIN, 1) dot(x, y - 1, c, 1) dot(x, y, c, 1) if climbing then local ph = (frame // 10) % 2 dot(x + (ph == 0 and -1 or 1), y - 2 + ph, SKIN, 0.9) end end local function windows() for i = 1, #WINS do local P = 240 + rn(i, 6) % 360 local u = (frame + rn(i, 7)) % P local on = P * (0.35 + (rn(i, 8) % 30) / 100) local w = WINS[i] if u < on then local c = u < 4 and 0.5 or (u > on - 4 and 0.5 or 1) rect(w[1], w[2], 2, 2, floor(30 + (ONWIN[1] - 30) * c), floor(34 + (ONWIN[2] - 34) * c), floor(62 + (ONWIN[3] - 62) * c)) end end end local SMK = { 150, 160, 190 } local function smoke() for ci = 1, #CHIM do local c = CHIM[ci] for k = 0, 3 do local a = ((frame + k * 45 + ci * 17) % 180) / 180 local x = c[1] + 7 * a + 1.5 * sin(a * 6 + ci + k) local y = c[2] - 15 * a local al = 0.55 * (1 - a) dot(x, y, SMK, al) if a > 0.4 then dot(x + 1, y, SMK, al * 0.7) end end end end local function stars() for i = 1, 8 do local x, y = rn(i, 20) % W, rn(i, 21) % 40 local s = sin(frame * 0.05 + rn(i, 22)) * 0.5 + 0.5 local v = floor(120 + 110 * s) px(x, y, v, v + 4, 200 + floor(50 * s)) end local u = frame % 330 if u < 16 then local n = frame // 330 local x0, y0 = 6 + rn(n, 1) % 30, 2 + rn(n, 2) % 12 for k = 0, 4 do local t = u - k if t >= 0 then blend(floor(x0 + 3.2 * t), floor(y0 + 1.4 * t), 240, 244, 255, 0.9 - k * 0.18) end end end end local function wisp(x, y, w, a) local ix = floor(x) local f = x - ix blend_rect(ix, y, 1, 2, 96, 106, 170, a * (1 - f)) blend_rect(ix + 1, y, w - 1, 2, 96, 106, 170, a) blend_rect(ix + w, y, 1, 2, 96, 106, 170, a * f) end function render() if not BG then build_bg() end gfx.blit_rgb(BG, 0, 0, W, H) stars() windows() for i = 1, cfg.folk or 4 do villager(i) end if cfg.smoke ~= false then smoke() end wisp((frame * 0.12) % 90 - 24, 56, 20, 0.28) wisp(64 - (frame * 0.09 + 30) % 90, 61, 16, 0.25) end