info = { type = "image", title = "Spook town", category = "holidays", author = "BRRRO", version = "1.0", license = "MIT", desc = "A ghost drifts over a haunted town at night", api = 44, } local tr = ({ sk = { ["Ghost colour"] = "Farba ducha", ["Green"] = "Zelená", ["Cyan"] = "Tyrkysová", ["Orange"] = "Oranžová", ["Window lights"] = "Svetlá v oknách", ["Bat"] = "Netopier" }, cs = { ["Ghost colour"] = "Barva ducha", ["Green"] = "Zelená", ["Cyan"] = "Tyrkysová", ["Orange"] = "Oranžová", ["Window lights"] = "Světla v oknech", ["Bat"] = "Netopýr" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "tone", label = T("Ghost colour"), type = "int", default = 0, opts = { T("Green"), T("Cyan"), T("Orange") } }, { key = "lights", label = T("Window lights"), type = "bool", default = true }, { key = "bat", label = T("Bat"), type = "bool", default = true }, } local rect, px, blend, blit_rgb, blend_mask = gfx.rect, gfx.px, gfx.blend, gfx.blit_rgb, gfx.blend_mask local floor, sin, cos, sqrt, abs = math.floor, math.sin, math.cos, math.sqrt, math.abs local hash32 = math.hash32 local W, H = gfx.W, gfx.H local TONES = { { { 70, 255, 70 }, { 170, 255, 180 }, { 210, 255, 60 } }, { { 60, 240, 255 }, { 170, 230, 255 }, { 255, 255, 120 } }, { { 255, 150, 40 }, { 255, 220, 170 }, { 255, 255, 90 } }, } local TN = TONES[1] local FAR, FAR_L = { 74, 44, 104 }, { 96, 60, 130 } local NEAR, NEAR_L = { 30, 20, 50 }, { 52, 36, 82 } local YEL = { 255, 224, 60 } local KEY = { 255, 0, 255 } local TOWERS = { { x = 0, apex = 17 }, { x = 25, apex = 12 }, { x = 50, apex = 19 } } local WIN = {} local function lerp(a, b, t) return a + (b - a) * t end local function sky(y) if y < 24 then local t = y / 24 return floor(lerp(34, 84, t)), floor(lerp(14, 26, t)), floor(lerp(108, 160, t)) end local t = (y - 24) / 22 return floor(lerp(84, 176, t)), floor(lerp(26, 56, t)), floor(lerp(160, 168, t)) end local function roof(x, w, apex, base, c, cl) for y = apex, base - 1 do local rw = 2 + floor((y - apex + 1) / (base - apex) * (w - 2)) local l = x + floor((w - rw) / 2) rect(l, y, rw, 1, c[1], c[2], c[3]) if cl then px(l, y, cl[1], cl[2], cl[3]) end end end local function far_house(x, w, top, rh) rect(x, top + rh, w, 64 - top - rh, FAR[1], FAR[2], FAR[3]) roof(x - 1, w + 2, top, top + rh, FAR, FAR_L) if w >= 7 then WIN[#WIN + 1] = { x + 1, top + rh + 3, 2, 2, false } WIN[#WIN + 1] = { x + w - 3, top + rh + 7, 2, 2, false } else WIN[#WIN + 1] = { x + 2, top + rh + 4, 2, 2, false } end end local BG, FG, FG_FRAME = nil, nil, nil local function build_sky() for y = 0, 45 do rect(0, y, W, 1, sky(y)) end rect(0, 46, W, H - 46, 176, 56, 168) far_house(0, 8, 40, 5); far_house(8, 7, 36, 4); far_house(15, 9, 42, 4) far_house(22, 6, 38, 4); far_house(31, 8, 33, 5); far_house(39, 7, 41, 4) far_house(45, 8, 37, 4); far_house(55, 9, 39, 5) rect(0, 54, W, 10, 40, 26, 62) BG = gfx.snapshot(0, 0, W, H) end local function build_towers() gfx.clear(KEY[1], KEY[2], KEY[3]) for i = 1, 3 do local t = TOWERS[i] rect(t.x, 30, 14, 34, NEAR[1], NEAR[2], NEAR[3]) rect(t.x, 30, 1, 34, NEAR_L[1], NEAR_L[2], NEAR_L[3]) roof(t.x - 1, 16, t.apex, 30, NEAR, NEAR_L) for _, wy in ipairs({ 35, 44, 53 }) do WIN[#WIN + 1] = { t.x + 3, wy, 2, 3, true } WIN[#WIN + 1] = { t.x + 9, wy, 2, 3, true } end end FG = gfx.snapshot(0, 0, W, H) FG_FRAME = frame end local GW, GH = 14, 16 local function inside(u, v, m) if v < m then return false end local r = 6 - m if v <= 6 then return u * u + (v - 6) * (v - 6) <= r * r end if abs(u) > r - (v - 6) * 0.1 then return false end return v < 12 + 1.5 * cos(u * 1.5708) - m end local OUTL, BODY, EYES = {}, {}, {} local function bake_body(k) local qx, qy = k % 4 / 4, k // 4 / 4 local o, b = {}, {} local n = 0 for y = 0, GH - 1 do for x = 0, GW - 1 do local co, ci = 0, 0 for sy = 0.25, 0.75, 0.5 do for sx = 0.25, 0.75, 0.5 do local u, v = x + sx - 6.5 - qx, y + sy - qy if inside(u, v, 0) then co = co + 1 if inside(u, v, 1) then ci = ci + 1 end end end end n = n + 1 o[n], b[n] = (co - ci) * 63, ci * 63 end end OUTL[k] = string.char(table.unpack(o)) BODY[k] = string.char(table.unpack(b)) end local function bake_eyes(pose, k) local qx, qy = k % 4 / 4, k // 4 / 4 local e = (pose - 1) * 0.9 local t, n = {}, 0 for y = 0, GH - 1 do for x = 0, GW - 1 do local c = 0 if y >= 2 and y <= 9 then for sy = 0.25, 0.75, 0.5 do for sx = 0.25, 0.75, 0.5 do local u, v = x + sx - 6.5 - qx - e, y + sy - qy - 5.2 local a, d = abs(u) - 2.2, v / 1.5 if a * a + d * d <= 1 then c = c + 1 end end end end n = n + 1 t[n] = c * 63 end end EYES[pose * 16 + k] = string.char(table.unpack(t)) end local LEGS = { { { 7, 41 }, { 7, 41 }, { 12, 24 }, { 30, 10 }, { 50, 16 }, { 58, 31 }, { 57, 41 }, { 57, 41 } }, { { 32, 41 }, { 32, 41 }, { 20, 26 }, { 6, 12 }, { 22, 6 }, { 42, 14 }, { 20, 30 }, { 7, 41 }, { 7, 41 } }, { { 57, 41 }, { 57, 41 }, { 46, 22 }, { 36, 8 }, { 20, 16 }, { 18, 30 }, { 32, 41 }, { 32, 41 } }, } local LEG_F = 330 local function cr(a, b, c, d, t) return 0.5 * (2 * b + (c - a) * t + (2 * a - 5 * b + 4 * c - d) * t * t + (3 * b - a - 3 * c + d) * t * t * t) end local function path(f) local leg = LEGS[f // LEG_F % 3 + 1] local n = #leg - 1 local s = (f % LEG_F) / LEG_F * n local i = floor(s) local t = s - i local p0, p1, p2, p3 = leg[math.max(i, 1)], leg[i + 1], leg[i + 2], leg[math.min(i + 3, n + 1)] return cr(p0[1], p1[1], p2[1], p3[1], t), cr(p0[2], p1[2], p2[2], p3[2], t) end local pose, LAST_EYES = 1, nil local function draw_ghost() local x, y = path(frame) local x2 = path(frame + 2) local vx = x2 - x if vx > 0.05 then pose = 2 elseif vx < -0.05 then pose = 0 else pose = 1 end y = y + 1.3 * sin(frame * 0.07) local X, Y = x - 6.5, y - 7 local ix, iy = floor(X), floor(Y) local k = floor((Y - iy) * 4) * 4 + floor((X - ix) * 4) local ek = pose * 16 + k local baked = false if not BODY[k] then bake_body(k); baked = true end if not EYES[ek] and not baked then bake_eyes(pose, k) end local b, o, e = TN[2], TN[1], TN[3] blend_mask(BODY[k], b[1], b[2], b[3], ix, iy, GW, GH, GW, GH, 0.5) blend_mask(OUTL[k], o[1], o[2], o[3], ix, iy, GW, GH, GW, GH, 0.95) LAST_EYES = EYES[ek] or LAST_EYES if LAST_EYES then blend_mask(LAST_EYES, e[1], e[2], e[3], ix, iy, GW, GH, GW, GH, 1) end end local function lit(i, slot) return (hash32(i * 0x85EBCA6B + slot * 0x27D4EB2F) & 7) < 5 and 1 or 0 end local function windows(near) for i = 1, #WIN do local w = WIN[i] if w[5] == near then local t = frame + (hash32(i * 0x9E3779B1) & 0x7F) * 7 local slot = t // 160 local fr = t - slot * 160 local cur = lit(i, slot) local l = cur if fr < 8 then l = lit(i, slot - 1) + (cur - lit(i, slot - 1)) * fr / 8 end if cfg.lights == false then l = 0.55 end if l > 0.04 then local base = near and NEAR or FAR rect(w[1], w[2], w[3], w[4], floor(lerp(base[1], YEL[1], l)), floor(lerp(base[2], YEL[2], l)), floor(lerp(base[3], YEL[3], l))) end end end end local STARS = {} for i = 1, 14 do local h = hash32(i * 0x9E3779B1) STARS[i] = { (h & 0x7FFF) % W, ((h >> 8) & 0x7FFF) % 26, ((h >> 16) & 0x3F) / 10 } end local BAT = { { "X.....X", ".XX.XX.", "..XXX..", "...X..." }, { "...X...", ".XXXXX.", "XX.X.XX", "X.....X" }, } local function draw_bat() local c = frame % 495 if c >= 100 then return end local dir = (frame // 495) % 2 == 0 and -1 or 1 local x = dir < 0 and 70 - c * 0.8 or -6 + c * 0.8 local y = 20 + 4 * sin(c * 0.13) local rows = BAT[frame // 4 % 2 + 1] local ix, iy = floor(x), floor(y) for r = 1, 4 do for q = 1, 7 do if rows[r]:byte(q) == 88 then px(ix + q - 4, iy + r - 2, 20, 10, 34) end end end end function on_settings() TN = TONES[(cfg.tone or 0) + 1] or TONES[1] end function render() if not BG then build_sky() elseif not FG then build_towers() end if not FG then gfx.ready(false) end blit_rgb(BG, 0, 0, W, H) windows(false) for i = 1, #STARS do local s = STARS[i] blend(s[1], s[2], 255, 250, 235, 0.45 + 0.4 * sin(frame * 0.05 + s[3] * 6)) end if cfg.bat ~= false then draw_bat() end if FG then if FG_FRAME ~= frame then draw_ghost() end blit_rgb(FG, 0, 0, W, H, KEY[1], KEY[2], KEY[3]) windows(true) end end