info = { type = "image", title = "Attic flashlight", category = "city", author = "BRRRO", version = "1.0", license = "MIT", desc = "A flashlight beam explores a dusty dark attic", api = 44, } local tr = ({ sk = { ["Light"] = "Svetlo", ["Warm"] = "Teplé", ["Cold"] = "Studené" }, cs = { ["Light"] = "Světlo", ["Warm"] = "Teplé", ["Cold"] = "Studené" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "light", label = T("Light"), type = "int", default = "warm", opts = { keys = { "warm", "cold" }, T("Warm"), T("Cold") } }, } local rect, px, blend, blend_rect = gfx.rect, gfx.px, gfx.blend, gfx.blend_rect local floor, sin, cos, sqrt, min, max = math.floor, math.sin, math.cos, math.sqrt, math.min, math.max local W, H = gfx.W, gfx.H local AMB = { 6, 8, 22 } local AMB_A = 0.74 local S, HS = 31, 15 local LIT, MASKD, MASKG local STAGE = 0 local TINT = { 255, 214, 150 } function on_settings() TINT = cfg.light == "cold" and { 190, 220, 255 } or { 255, 214, 150 } end local function R(x, y, w, h, c) rect(x, y, w, h, c[1], c[2], c[3]) end local function line(x0, y0, x1, y1, c) local dx, dy = math.abs(x1 - x0), -math.abs(y1 - y0) local sx, sy = x0 < x1 and 1 or -1, y0 < y1 and 1 or -1 local e = dx + dy while true do px(x0, y0, c[1], c[2], c[3]) if x0 == x1 and y0 == y1 then break end local e2 = 2 * e if e2 >= dy then e = e + dy; x0 = x0 + sx end if e2 <= dx then e = e + dx; y0 = y0 + sy end end end local function circle(cx, cy, r, c) for k = 0, 39 do local a = k / 40 * 6.2831853 px(floor(cx + r * cos(a) + 0.5), floor(cy + r * sin(a) + 0.5), c[1], c[2], c[3]) end end local function build1() local P1, P2, SEAM = { 118, 84, 56 }, { 126, 90, 60 }, { 86, 60, 40 } for i = 0, 6 do R(i * 9, 0, 9, 46, i % 2 == 0 and P1 or P2); R(i * 9, 0, 1, 46, SEAM) end R(0, 46, W, 18, { 140, 100, 64 }); R(0, 45, W, 2, { 80, 56, 36 }) R(0, 51, W, 1, { 104, 72, 46 }); R(0, 57, W, 1, { 104, 72, 46 }) for x = 6, W, 17 do R(x, 46, 1, 5, { 104, 72, 46 }) end for x = 15, W, 17 do R(x, 52, 1, 5, { 104, 72, 46 }) end for d = 0, 2 do line(0, 18 + d, 22, d, { 74, 50, 34 }); line(63, 18 + d, 41, d, { 74, 50, 34 }) end R(2, 40, 15, 12, { 128, 82, 40 }); R(2, 45, 15, 1, { 100, 64, 30 }) R(1, 37, 17, 3, { 150, 100, 50 }); R(3, 36, 13, 1, { 150, 100, 50 }) R(5, 36, 2, 16, { 190, 160, 64 }); R(12, 36, 2, 16, { 190, 160, 64 }) R(8, 40, 3, 4, { 232, 204, 96 }); px(9, 42, 60, 40, 24) R(2, 52, 15, 1, { 70, 46, 28 }) local RED, STEEL = { 176, 62, 48 }, { 150, 156, 164 } circle(23, 48, 7, STEEL); circle(38, 48, 7, STEEL) line(23, 48, 28, 43, { 110, 116, 124 }); line(23, 48, 18, 53, { 110, 116, 124 }) line(38, 48, 43, 43, { 110, 116, 124 }); line(38, 48, 33, 53, { 110, 116, 124 }) line(23, 48, 30, 48, RED); line(30, 48, 28, 38, RED); line(28, 38, 23, 48, RED) line(28, 38, 36, 39, RED); line(30, 48, 36, 39, RED); line(36, 39, 38, 48, RED) line(35, 36, 39, 35, { 70, 70, 80 }); line(36, 39, 35, 36, { 70, 70, 80 }) R(24, 36, 6, 2, { 70, 44, 30 }); R(22, 47, 2, 2, { 60, 62, 70 }); R(37, 47, 2, 2, { 60, 62, 70 }) end local function build2() local HB, HD = { 176, 128, 78 }, { 96, 60, 32 } for _, x in ipairs({ 48, 51, 55, 58 }) do R(x, 48, 2, 8, HD) end for x = 46, 60 do R(x, 57 - floor(3 * ((x - 53) / 7) ^ 2 + 0.5), 1, 2, HD) end R(47, 42, 13, 7, HB); R(47, 47, 13, 2, { 150, 104, 60 }) R(54, 40, 5, 3, { 172, 44, 44 }) R(55, 35, 4, 8, HB); R(56, 31, 6, 5, HB); R(60, 34, 3, 2, { 190, 140, 90 }) px(56, 30, 150, 104, 60); px(58, 30, 150, 104, 60); R(54, 32, 1, 11, HD); px(58, 33, 30, 20, 14) R(45, 43, 2, 1, HD); R(44, 44, 1, 4, HD) R(34, 25, 29, 2, { 96, 66, 40 }); R(37, 27, 2, 4, { 80, 54, 34 }); R(58, 27, 2, 4, { 80, 54, 34 }) R(38, 19, 4, 6, { 110, 150, 128 }); R(38, 18, 4, 1, { 200, 180, 80 }) R(44, 21, 6, 4, { 140, 60, 56 }); R(44, 21, 6, 1, { 200, 190, 160 }) local CAT, STRIPE = { 112, 108, 104 }, { 80, 76, 74 } R(53, 16, 7, 1, CAT); R(52, 17, 9, 8, CAT); R(53, 11, 7, 5, CAT); R(52, 12, 9, 3, CAT) R(53, 9, 2, 2, CAT); R(58, 9, 2, 2, CAT); px(53, 8, 112, 108, 104); px(59, 8, 112, 108, 104) px(54, 10, 170, 120, 120); px(58, 10, 170, 120, 120) R(54, 18, 1, 3, STRIPE); R(57, 18, 1, 3, STRIPE); R(61, 19, 2, 8, CAT); R(61, 22, 2, 1, STRIPE) end local function build3() local WEB = { 176, 180, 194 } for _, a in ipairs({ 0, 0.39, 0.78, 1.18, 1.5708 }) do line(0, 0, floor(20 * cos(a) + 0.5), floor(20 * sin(a) + 0.5), WEB) end for _, r in ipairs({ 6, 11, 16 }) do for k = 0, floor(r * 1.6) do local a = k / floor(r * 1.6) * 1.5708 px(floor(r * cos(a) + 0.5), floor(r * sin(a) + 0.5), WEB[1], WEB[2], WEB[3]) end end line(18, 14, 18, 19, { 120, 120, 134 }); R(17, 20, 2, 2, { 50, 40, 40 }) px(16, 19, 50, 40, 40); px(19, 19, 50, 40, 40); px(16, 22, 50, 40, 40); px(19, 22, 50, 40, 40) LIT = gfx.snapshot(0, 0, W, H) end local function build4() local d, g, rd, rg = {}, {}, {}, {} for y = 0, S - 1 do for x = 0, S - 1 do local dist = sqrt((x - HS) ^ 2 + (y - HS) ^ 2) / (HS + 0.5) local m = min(1, max(0, (dist - 0.42) / 0.58)) rd[x + 1], rg[x + 1] = floor(m * 255 + 0.5), floor((1 - m) * 255 + 0.5) end d[y + 1], g[y + 1] = string.char(table.unpack(rd, 1, S)), string.char(table.unpack(rg, 1, S)) end MASKD, MASKG = table.concat(d), table.concat(g) end local WP = { { 32, 52 }, { 13, 46 }, { 9, 12 }, { 30, 26 }, { 36, 44 }, { 54, 50 }, { 52, 18 }, { 32, 34 } } local DW = { 20, 40, 40, 20, 40, 40, 150, 10 } local CAT_I = 7 local EYE_X, EYE_Y = 56, 14 local T0, T1, T2, TOTAL = {}, {}, {}, 0 for i = 1, #WP do local nx = WP[i % #WP + 1] local d = sqrt((nx[1] - WP[i][1]) ^ 2 + (nx[2] - WP[i][2]) ^ 2) T0[i], T1[i] = TOTAL, TOTAL + DW[i] TOTAL = T1[i] + max(45, floor(d * 30 / 14)) T2[i] = TOTAL end local function smooth(u) return u * u * (3 - 2 * u) end local MOTE = {} for i = 1, 14 do local h = math.hash32(i * 0x9E3779B1) MOTE[i] = { math.umod(h, W), math.umod(h >> 8, H), (math.umod(h >> 16, 9) - 4) / 60, (math.umod(h >> 20, 7) - 5) / 70 } end local function dark_band(x, y, w, h) if w > 0 and h > 0 then blend_rect(x, y, w, h, AMB[1], AMB[2], AMB[3], AMB_A) end end function render() if STAGE < 4 then STAGE = STAGE + 1 ;({ build1, build2, build3, build4 })[STAGE]() gfx.ready(false) return end gfx.blit_rgb(LIT, 0, 0, W, H) local t = frame % TOTAL local i = 1 while t >= T2[i] do i = i + 1 end local a, bx = WP[i], WP[i % #WP + 1] local f = t < T1[i] and 0 or smooth((t - T1[i]) / (T2[i] - T1[i])) local cx = floor(a[1] + (bx[1] - a[1]) * f + sin(frame * 0.07) * 0.8 + 0.5) local cy = floor(a[2] + (bx[2] - a[2]) * f + sin(frame * 0.053 + 1) * 0.6 + 0.5) local x0, y0 = cx - HS, cy - HS local ya, yb = max(y0, 0), min(y0 + S, H) dark_band(0, 0, W, y0) dark_band(0, y0 + S, W, H - (y0 + S)) dark_band(0, ya, x0, yb - ya) dark_band(x0 + S, ya, W - (x0 + S), yb - ya) gfx.blend_mask(MASKD, AMB[1], AMB[2], AMB[3], x0, y0, S, S, S, S, AMB_A) gfx.blend_mask(MASKG, TINT[1], TINT[2], TINT[3], x0, y0, S, S, S, S, 0.16 + 0.01 * sin(frame * 0.4)) for k = 1, #MOTE do local m = MOTE[k] local mx, my = floor((m[1] + m[3] * frame) % W), floor((m[2] + m[4] * frame) % H) local d = sqrt((mx - cx) ^ 2 + (my - cy) ^ 2) if d < 13 then blend(mx, my, 255, 240, 200, 0.5 * (1 - d / 13) * (0.6 + 0.4 * sin(frame * 0.2 + k))) end end local ed = sqrt((cx - EYE_X) ^ 2 + (cy - EYE_Y) ^ 2) local ea = min(1, max(0, (30 - ed) / 14)) if ea > 0 then local k = (i == CAT_I and t < T1[i]) and t - T0[i] or -1 local shut = (k >= 40 and k < 46) or (k >= 86 and k < 92) local squint = (k >= 38 and k < 48) or (k >= 84 and k < 94) if not shut then local hh = squint and 1 or 2 for _, ex in ipairs({ 53, 58 }) do blend_rect(ex - 1, 12, 4, 4, 160, 230, 70, 0.22 * ea) blend_rect(ex, 13 + (2 - hh), 2, hh, 190, 255, 90, ea) end end end end