info = { type = "image", title = "Stork nest", category = "animals", author = "BRRRO", version = "1.0", license = "MIT", desc = "Stork lands on a chimney nest at dusk", api = 33, } local tr = ({ sk = { ["Time of day"] = "Denná doba", ["Day"] = "Deň", ["Dusk"] = "Súmrak" }, cs = { ["Time of day"] = "Denní doba", ["Day"] = "Den", ["Dusk"] = "Soumrak" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "tod", label = T("Time of day"), type = "int", default = "dusk", opts = { keys = { "day", "dusk" }, T("Day"), T("Dusk") } }, } local rect, px, blend_rect = gfx.rect, gfx.px, gfx.blend_rect local floor, sin, sqrt = math.floor, math.sin, math.sqrt local hash32 = math.hash32 local W, H = gfx.W, gfx.H local HZ = 46 local CYC, OFF = 900, 60 local KEY = { 255, 0, 255 } local DAY = { sky = { { 0, 78, 156, 226 }, { 1, 186, 222, 242 } }, hill = { 128, 170, 160 }, wall = { 236, 220, 196 }, roof = { 178, 74, 54 }, win = { 120, 150, 180 }, rf1 = { 176, 78, 58 }, rf2 = { 146, 58, 46 }, rf3 = { 196, 98, 70 }, br1 = { 190, 104, 78 }, br2 = { 138, 70, 54 }, mortar = { 214, 180, 160 }, ground = { 86, 130, 76 }, cloud = { 252, 252, 255 }, smoke = { 200, 204, 210 }, white = { 248, 248, 244 }, shade = { 206, 210, 216 }, tint = 1, } local DUSK = { sky = { { 0, 30, 32, 88 }, { 0.5, 108, 70, 128 }, { 0.82, 238, 138, 92 }, { 1, 252, 194, 112 } }, hill = { 74, 56, 104 }, wall = { 70, 56, 90 }, roof = { 46, 36, 66 }, win = { 255, 204, 98 }, rf1 = { 96, 52, 66 }, rf2 = { 66, 38, 58 }, rf3 = { 128, 70, 76 }, br1 = { 120, 64, 66 }, br2 = { 82, 44, 56 }, mortar = { 96, 66, 72 }, ground = { 44, 40, 66 }, cloud = { 250, 170, 150 }, smoke = { 150, 124, 140 }, white = { 250, 228, 208 }, shade = { 206, 176, 168 }, tint = 0.9, } local P, dusk = DUSK, true local RED, BLK = { 214, 52, 40 }, { 30, 30, 36 } local NEST = { { 70, 48, 30 }, { 112, 80, 48 }, { 156, 116, 70 } } local function cl(v) v = floor(v + 0.5); return v < 0 and 0 or v > 255 and 255 or v end local function grad(st, t) for i = 2, #st do local a, b = st[i - 1], st[i] if t <= b[1] then local k = (t - a[1]) / (b[1] - a[1]) return cl(a[2] + (b[2] - a[2]) * k), cl(a[3] + (b[3] - a[3]) * k), cl(a[4] + (b[4] - a[4]) * k) end end local l = st[#st] return l[2], l[3], l[4] end local function c3(c) return c[1], c[2], c[3] end local function fdisc(cx, cy, r, c, a, ymax) for y = floor(cy - r + 0.5), floor(cy + r - 0.5) do if not ymax or y < ymax then local dy = y + 0.5 - cy local hw = sqrt(r * r - dy * dy) local x0, x1 = floor(cx - hw + 0.5), floor(cx + hw + 0.5) if a then blend_rect(x0, y, x1 - x0, 1, c[1], c[2], c[3], a) else rect(x0, y, x1 - x0, 1, c[1], c[2], c[3]) end end end end local function house(x, base, w, h, rh, ch) rect(x, base - h, w, h, c3(P.wall)) for i = 0, rh - 1 do local half = (w + 2) * (i + 1) // (2 * rh) rect(x + w // 2 - half, base - h - rh + i, 2 * half + (w % 2), 1, c3(P.roof)) end if ch then rect(x + ch, base - h - rh - 1, 2, rh, c3(P.roof)) end rect(x + 2, base - h + 2, 2, 2, c3(P.win)) if w > 9 then rect(x + w - 4, base - h + 2, 2, 2, c3(P.win)) end end local NX, NY, NRX, NRY = 44, 27, 12, 4.5 local BG, FRONT local stage = 0 local function nest_rows(y0, y1) for y = y0, y1 do local dy = y + 0.5 - NY local hw = NRX * sqrt(1 - (dy / NRY) ^ 2) local x = floor(NX - hw + 0.5) local xe = floor(NX + hw + 0.5) local k = 0 while x < xe do k = k + 1 local h = hash32(y * 7919 + k * 104729) & 0x7FFF local len = 2 + h % 4 if x + len > xe then len = xe - x end local c = NEST[1 + (h >> 5) % 3] if y == y0 and y0 == 22 then c = NEST[3] end rect(x, y, len, 1, c3(c)) x = x + len end end end local function build_scene() for y = 0, HZ - 1 do rect(0, y, W, 1, grad(P.sky, y / (HZ - 1))) end if dusk then fdisc(12, HZ - 1, 11, { 244, 150, 100 }, 0.2, HZ) fdisc(12, HZ - 1, 6.5, { 255, 226, 150 }, nil, HZ) for i = 1, 12 do local h = hash32(i * 0x9E3779B1) px((h & 0x7FFF) % W, ((h >> 8) & 0x7FFF) % 20, 190, 190, 230) end else fdisc(11, 12, 7, { 214, 232, 248 }, 0.4) fdisc(11, 12, 4.5, { 255, 248, 214 }) end for x = 0, W - 1 do local t = HZ - 8 + floor(2.5 * sin(x * 0.08) + 1.5 * sin(x * 0.21 + 1) + 0.5) rect(x, t, 1, HZ - t, c3(P.hill)) end rect(0, HZ, W, H - HZ, c3(P.ground)) rect(8, 31, 4, 17, c3(P.wall)); rect(7, 28, 6, 3, c3(P.roof)); rect(9, 25, 2, 3, c3(P.roof)) house(1, 52, 12, 7, 5, 2) house(15, 53, 11, 6, 4) house(51, 51, 13, 8, 5, 9) for y = 42, H - 1 do local half = y - 42 + 2 local x0, x1 = 44 - half, 44 + half local r = ((y - 42) // 2) % 2 local l, d = r == 0 and P.rf1 or P.rf2, r == 0 and P.rf2 or P.rf1 rect(x0, y, 44 - x0, 1, c3(l)) rect(44, y, x1 - 44, 1, c3(d)) if (y - 42) % 4 == 0 then rect(x0, y, x1 - x0, 1, c3(P.rf3)) end end rect(39, 31, 11, 19, c3(P.br1)); rect(46, 31, 4, 19, c3(P.br2)) for y = 34, 49, 3 do rect(39, y, 11, 1, c3(P.mortar)) end rect(38, 29, 13, 3, c3(P.br2)); rect(38, 29, 13, 1, c3(P.br1)) nest_rows(23, 31) BG = gfx.snapshot(0, 0, W, H) end local function build_front() rect(30, 27, 28, 6, KEY[1], KEY[2], KEY[3]) nest_rows(28, 31) FRONT = gfx.snapshot(30, 27, 28, 6) gfx.blit_rgb(BG, 0, 0, W, H) end function on_settings() dusk = cfg.tod ~= "day" P = dusk and DUSK or DAY stage = 0 end local OX, OY, DR = 0, 0, 1 local function R(x, y, w, h, c) local l = DR > 0 and OX + x or OX - x - w local ix = floor(l) local f = l - ix y = OY + y local r, g, b = c[1], c[2], c[3] if f < 0.05 then rect(ix, y, w, h, r, g, b) else if w > 1 then rect(ix + 1, y, w - 1, h, r, g, b) end blend_rect(ix, y, 1, h, r, g, b, 1 - f) blend_rect(ix + w, y, 1, h, r, g, b, f) end end local WH, SH local GRY = { 120, 120, 128 } local function fly(x, y, dy, legs) OX, OY, DR = x, floor(y + 0.5), 1 R(-10, 2, 10, 1, RED) if legs then R(2, 3, 1, 9, RED); R(5, 3, 1, 9, RED) end local n = dy < 0 and -dy or dy for pass = 1, 2 do local ox = pass == 1 and 3 or 0 local m = pass == 1 and n - 2 or n for i = 1, m do local s = i / n local yy = dy < 0 and -i or 2 + i local w = floor(7 - 4 * s) local col = pass == 1 and (s > 0.55 and GRY or SH) or (s > 0.55 and BLK or WH) R(ox + 1 - floor(s * 5), yy, w, 1, col) end end R(-2, 1, 3, 2, WH); R(0, 0, 9, 3, WH); R(1, 3, 7, 1, SH) R(9, 0, 4, 2, WH); R(13, 0, 3, 2, WH); R(16, 1, 7, 1, RED); px(floor(OX + 14), OY, 20, 20, 24) end local function stand(fx, fy, pose, open, d) OX, OY, DR = fx, fy, d R(1, -9, 1, 9, RED); R(3, -9, 1, 9, RED) R(-2, -15, 9, 1, WH); R(-3, -14, 11, 5, WH); R(-2, -9, 9, 1, SH) R(-1, -12, 7, 2, BLK); R(-3, -13, 2, 2, BLK) if pose == 0 then R(7, -21, 2, 8, WH); R(7, -23, 3, 2, WH); R(10, -22, 7, 1, RED); px(OX + (d > 0 and 8 or -9), OY - 23, 20, 20, 24) elseif pose == 1 then R(7, -19, 2, 6, WH); R(5, -22, 3, 3, WH) for i = 0, 3 do R(4 - i, -23 - i, 1, 1, RED) end if open then for i = 0, 3 do R(6 - i, -23 - i, 1, 1, RED) end end px(OX + (d > 0 and 6 or -7), OY - 22, 20, 20, 24) else R(7, -16, 3, 4, WH); R(9, -14, 6, 1, RED) end end local function sitter(pose, open) OX, OY, DR = 38, 0, 1 R(0, 21, 11, 5, WH); R(1, 20, 9, 1, WH); R(1, 26, 9, 1, SH); R(0, 23, 6, 2, BLK) if pose == 0 then R(9, 12, 2, 10, WH); R(8, 10, 3, 2, WH); R(11, 11, 7, 1, RED); R(10, 11, 1, 1, BLK) elseif pose == 1 then R(9, 20, 3, 3, WH); R(12, 21, 6, 1, RED) else R(9, 13, 2, 9, WH); R(8, 11, 3, 2, WH) R(11, 10, 6, 1, RED); R(11, open and 13 or 12, 6, 1, RED) end end local function bez(a, b, c, d, t) local s = 1 - t return s * s * s * a + 3 * s * s * t * b + 3 * s * t * t * c + t * t * t * d end local FLAP = { -11, -8, -3, 2, 6, 7, 3, -4 } local SX, SY = 31, 13 local function draw_visitor(u) if u < 200 then local t = u / 200 t = t * (2 - t) * 0.5 + t * 0.5 local x = bez(-24, 12, 24, SX, t) local y = bez(4, -2, 8, SY, t) local dy if u < 130 then dy = FLAP[(u // 2) % 8 + 1] elseif u < 160 then dy = -3 else dy = ({ -11, -9, -6, -9, -11 })[(u // 3) % 5 + 1] end fly(x, y, dy, u >= 150) elseif u < 620 then local clat = u >= 250 and u < 430 stand(SX + 3, 26, clat and 1 or 0, clat and (u // 4) % 2 == 0, 1) elseif u < 800 then local t = (u - 620) / 180 local x = bez(SX, 40, 70, 90, t) local y = bez(SY, -10, -12, -18, t) fly(x, y, FLAP[(u // 2) % 8 + 1] - (u < 680 and 2 or 0), u < 640) end end local CLOUDS = { { 38, 9, 15, 0.018 }, { 4, 20, 10, 0.012 } } local function draw_clouds(f) local c = P.cloud local a = dusk and 0.5 or 0.8 for _, k in ipairs(CLOUDS) do local w = k[3] local x = floor((k[1] + f * k[4]) % (W + w + 8) - w) blend_rect(x + 2, k[2], w - 4, 1, c[1], c[2], c[3], a * 0.8) blend_rect(x, k[2] + 1, w, 2, c[1], c[2], c[3], a) blend_rect(x + 3, k[2] + 3, w - 6, 1, c[1], c[2], c[3], a * 0.6) end end local CHIM = { { 3, 41 }, { 60, 38 } } local function draw_smoke(f) local c = P.smoke for _, ch in ipairs(CHIM) do for k = 0, 5 do local e = f // 24 - k local age = f - e * 24 if age < 140 then local x = ch[1] + 1 + age * 0.05 + 2 * sin(e * 1.3 + age * 0.03) local y = ch[2] - age * 0.09 local r = 1 + age // 50 blend_rect(floor(x), floor(y), r + 1, r + 1, c[1], c[2], c[3], 0.5 * (1 - age / 140)) end end end end function render() if stage < 2 then stage = stage + 1 if stage == 1 then build_scene() else build_front() end gfx.blit_rgb(BG, 0, 0, W, H) return end local u = (frame + OFF) % CYC gfx.blit_rgb(BG, 0, 0, W, H) draw_clouds(frame) draw_smoke(frame) WH, SH = P.white, P.shade local clat2 = u >= 280 and u < 400 local pose = clat2 and 2 or ((u // 90) % 5 == 3 and 1 or 0) sitter(pose, clat2 and (u // 4) % 2 == 1) draw_visitor(u) gfx.blit_rgb(FRONT, 30, 27, 28, 6, KEY[1], KEY[2], KEY[3]) if dusk and (frame // 45) % 7 ~= 0 then px(55, 44, 255, 214, 110) end end