info = { type = "image", title = "Parcel night", category = "holidays", author = "BRRRO", version = "1.0", license = "MIT", desc = "A postman delivers a parcel to a wreathed door", api = 33, } local tr = ({ sk = { ["Window light"] = "Svetlo okien", ["Snow"] = "Sneh", ["Speed"] = "Rýchlosť" }, cs = { ["Window light"] = "Světlo oken", ["Snow"] = "Sníh", ["Speed"] = "Rychlost" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "glow", label = T("Window light"), type = "color", default = "#FFCC6C" }, { key = "snow", label = T("Snow"), type = "int", min = 0, max = 16, default = 10 }, { key = "speed", label = T("Speed"), type = "int", min = 1, max = 5, default = 3 }, } local rect, px, blend_rect, blend = gfx.rect, gfx.px, gfx.blend_rect, gfx.blend local blit_rgb, snapshot = gfx.blit_rgb, gfx.snapshot local floor, sin, cos, sqrt, min, max = math.floor, math.sin, math.cos, math.sqrt, math.min, math.max local hash32, umod = math.hash32, math.umod local W, H = gfx.W, gfx.H local WALL_A, WALL_B, WALL_C = { 70, 88, 136 }, { 62, 78, 124 }, { 54, 68, 112 } local TRIM, TRIM_D = { 196, 208, 236 }, { 150, 164, 204 } local OFF = { 30, 38, 76 } local SNOW_A, SNOW_B = { 214, 226, 248 }, { 172, 190, 230 } local function mix(a, b, t) return floor(a[1] + (b[1] - a[1]) * t + 0.5), floor(a[2] + (b[2] - a[2]) * t + 0.5), floor(a[3] + (b[3] - a[3]) * t + 0.5) end local function c3(c) return c[1], c[2], c[3] end local function ease(x) x = max(0, min(1, x)); return x * x * (3 - 2 * x) end local function runs(rows, keys) local out = {} for y, row in ipairs(rows) do if #row == 11 then row = "." .. row .. "." end local x, last = 1, -1 while x <= #row do local c = row:sub(x, x) local e = x while row:sub(e + 1, e + 1) == c do e = e + 1 end local k = keys:find(c, 1, true) if k > 1 then out[#out + 1] = { x - 1, y - 1, e - x + 1, k - 1, last == x - 1 } end last, x = k > 1 and e or -1, e + 1 end end return out end local function draw(sp, pal, X, Y, al) local x0 = floor(X) local f = X - x0 if f > 0.88 then x0, f = x0 + 1, 0 end for i = #sp, 1, -1 do local s = sp[i] local r, g, b = c3(pal[s[4]]) local x, y, n = x0 + s[1], Y + s[2], s[3] if al then blend_rect(x, y, n, 1, r, g, b, al) elseif f < 0.12 then rect(x, y, n, 1, r, g, b) elseif s[5] then rect(x, y, n, 1, r, g, b) blend_rect(x + n, y, 1, 1, r, g, b, f) else blend_rect(x, y, 1, 1, r, g, b, 1 - f) if n > 1 then rect(x + 1, y, n - 1, 1, r, g, b) end blend_rect(x + n, y, 1, 1, r, g, b, f) end end end local HEAD = { "..ccccc....", ".cccccccc..", "..ssssss...", "..sssse....", "..ssssss..." } local TORSO = { "..jjjjjj...", "..djjjjaa..", "..djjjjaa..", "..djjjj....", "..djjjj....", "..kkkkk...." } local FREE = { "..jjjjjj...", "..djjjjj...", "..djjjjja..", "..djjjjja..", "..djjjj....", "..kkkkk...." } local STAND = { "..kk.kk....", "..kk.kk....", "..kk.kk....", "..kk.kk....", ".bbb.bbb..." } local STRIDE = { "..kk.kkk...", ".kk...kk...", ".kk....kk..", ".kk....kk..", "bbb....bbb." } local RING = { "..ccccc....", ".cccccccc..", "..ssssss...", "..sssse..aa", "..ssssss.a.", "..jjjjjja..", "..djjjjj...", "..djjjj....", "..djjjj....", "..djjjj....", "..kkkkk....", } local BEND = { ".............", ".............", "....ccccc....", "...ccccccc...", "....ssssss...", "....sssse....", "....sssss....", "..djjjjjj....", "..djjjjjaa...", "..djjjj..aa..", "..kkkkk..aa..", } local KEYS = ".csejdakb" local PAL = { { 44, 54, 118 }, { 255, 214, 186 }, { 40, 36, 60 }, { 246, 196, 52 }, { 206, 150, 28 }, { 226, 172, 36 }, { 46, 54, 106 }, { 30, 28, 40 } } local PK_KEYS = ".lkTwd" local PK_PAL = { { 232, 190, 128 }, { 212, 164, 104 }, { 246, 230, 190 }, { 255, 250, 240 }, { 170, 124, 76 } } local PARCEL = runs({ "lllTlll", "kkkTkkk", "kkkTwwk", "kkkTwwk", "kkkTkkk", "ddddddd" }, PK_KEYS) local POSES = {} local function man(name) if not POSES[name] then local rows = {} local function add(src) for _, r in ipairs(src) do rows[#rows + 1] = r end end if name == "ring" then add(RING); add(STAND) elseif name == "bend" then add(BEND); add(STAND) else add(HEAD) add(name:sub(-1) == "f" and FREE or TORSO) add(name:sub(1, 6) == "stride" and STRIDE or STAND) end POSES[name] = runs(rows, KEYS) end return POSES[name] end local BG local WIN = { { 6, 17, 12, 14 }, { 55, 21, 6, 10 } } local DX, DY, DW = 35, 20, 13 local WR_X, WR_Y = 43, 30 local LIGHTS = {} local function lump(cx, cy, rx, ry) for y = cy - ry, cy do local dy = (y + 0.5 - cy) / ry local hw = rx * sqrt(max(0, 1 - dy * dy)) local x0, w = floor(cx - hw + 0.5), floor(2 * hw + 0.5) local snow = y < cy - ry + 3 rect(x0, y, w, 1, snow and 226 or 40, snow and 236 or 86, snow and 252 or 70) end end local function bake() rect(0, 0, W, 6, 190, 206, 238) rect(0, 5, W, 1, 140, 158, 206) rect(0, 6, W, 4, 58, 52, 88) for x = 1, W - 1, 3 do px(x, 10, 190, 206, 238) end for y = 10, 51 do rect(0, y, W, 1, c3(y % 3 == 0 and WALL_C or (y % 6 == 1 and WALL_A or WALL_B))) end rect(0, 10, 1, 42, c3(TRIM_D)); rect(W - 1, 10, 1, 42, c3(TRIM_D)) rect(0, 50, W, 2, 84, 96, 138) for i, w in ipairs(WIN) do rect(w[1] - 2, w[2] - 2, w[3] + 4, w[4] + 4, c3(TRIM)) rect(w[1] - 2, w[2] + w[4] + 2, w[3] + 4, 1, c3(TRIM_D)) end rect(4, 35, 18, 2, c3(TRIM)); rect(5, 34, 16, 1, 226, 236, 252) rect(33, 17, 17, 35, c3(TRIM)) rect(DX, DY, DW, 30, 150, 84, 58) rect(37, 24, 9, 10, 122, 66, 46); rect(37, 24, 9, 1, 176, 106, 74) rect(37, 36, 9, 13, 122, 66, 46); rect(37, 36, 9, 1, 176, 106, 74) rect(44, 43, 2, 2, 255, 214, 120) rect(39, 11, 5, 4, 40, 44, 70); rect(40, 12, 3, 2, 255, 214, 130) for y = -6, 6 do for x = -6, 6 do local d = sqrt(x * x + y * y) if d > 3.2 and d < 5.2 then local h = hash32((x + 9) * 31 + y + 17) & 3 px(WR_X + x, WR_Y + y, h == 0 and 70 or 38, h == 0 and 150 or 112, h == 0 and 86 or 62) end end end rect(WR_X - 2, WR_Y + 5, 5, 2, 196, 40, 52) rect(WR_X - 2, WR_Y + 7, 1, 3, 196, 40, 52); rect(WR_X + 2, WR_Y + 7, 1, 3, 196, 40, 52) for i = 1, 6 do local a = i * 1.047 local x, y = floor(WR_X + 4.2 * cos(a) + 0.5), floor(WR_Y + 4.2 * sin(a) + 0.5) if i % 2 == 0 then px(x, y, 210, 50, 60) else LIGHTS[#LIGHTS + 1] = { x, y, i } end end rect(29, 47, 3, 4, 190, 170, 120) for y = 52, H - 1 do rect(0, y, W, 1, mix(SNOW_A, SNOW_B, (y - 52) / 11)) end rect(31, 52, 21, 2, 232, 240, 255) rect(29, 54, 25, 2, 186, 204, 238) lump(8, 51, 8, 9); lump(57, 51, 7, 8) BG = snapshot(0, 0, W, H) end local LOOP, VX = 1080, 0.4 local SPD = { 0.5, 0.75, 1, 1.3, 1.7 } local PK_X, PK_Y, MY = 38, 50, 47 local START = 150 local FL = {} for i = 1, 16 do local h = hash32(i * 0x9E3779B1) FL[i] = { x = umod(h, W), y0 = umod(h >> 6, 70), v = 0.15 + umod(h >> 12, 8) * 0.02, sw = 1.5 + umod(h >> 16, 3), ph = umod(h >> 20, 628) / 100 } end local function windows(t, glow) local fade = 1 - ease((t - 960) / 40) for i, w in ipairs(WIN) do local L = ease((t - 90 - i * 20) / 40) * fade local x, y, ww, hh = w[1], w[2], w[3], w[4] if L > 0 then blend_rect(x - 5, y - 4, ww + 10, hh + 8, glow.r, glow.g, glow.b, 0.1 * L) end local tr_, tg, tb = mix(OFF, { glow.r, glow.g, glow.b }, L) rect(x, y, ww, hh // 2, tr_, tg, tb) rect(x, y + hh // 2, ww, hh - hh // 2, mix(OFF, { glow.r * 0.86, glow.g * 0.8, glow.b * 0.7 }, L)) if L > 0 then blend_rect(x, y, 2, hh, 200, 90, 80, 0.55 * L); blend_rect(x + ww - 2, y, 2, hh, 200, 90, 80, 0.55 * L) end rect(x + ww // 2 - 1, y, 2, hh, c3(TRIM)) rect(x, y + hh // 2 - 1, ww, 1, c3(TRIM)) end end local function door(t) local o = t < 540 and 0 or t < 580 and (t - 540) / 40 * 3 or t < 640 and 3 or t < 680 and 3 - (t - 640) / 40 * 3 or 0 o = floor(o + 0.5) if o > 0 then rect(DX, DY, o, 15, 252, 206, 120) rect(DX, DY + 15, o, 15, 232, 160, 90) rect(DX + o, DY, 1, 30, 90, 50, 36) blend_rect(33, 52, 17, 4, 255, 210, 130, 0.07 * o) blend_rect(31, 56, 21, 4, 255, 210, 130, 0.04 * o) end end function render() if not BG then bake() end blit_rgb(BG, 0, 0, W, H) local Tm = frame * SPD[cfg.speed or 3] + START local t = Tm % LOOP local glow = cfg.glow or { r = 255, g = 204, b = 108 } windows(t, glow) door(t) for _, l in ipairs(LIGHTS) do if sin(Tm * 0.11 + l[3] * 2) > 0 then px(l[1], l[2], 255, 232, 150) end end local ring = (t >= 293 and t < 303) or (t >= 323 and t < 333) px(30, 49, ring and 255 or 120, ring and 220 or 100, ring and 90 or 60) if ring then blend_rect(28, 45, 5, 1, 255, 230, 120, 0.9) px(27, 47, 255, 230, 120); px(33, 47, 255, 230, 120) end local X, name if t >= 180 and t < 263 then X, name = -16.2 + VX * (t - 180), "stride" elseif t >= 263 and t < 343 then X, name = 17, "ring" elseif t >= 343 and t < 373 then X, name = 17 + VX * (t - 343), "stride" elseif t >= 373 and t < 413 then X, name = 29, "bend" elseif t >= 413 and t < 510 then X, name = 29 + VX * (t - 413), "stride" end local pk_x, pk_y, al if t >= 180 and t < 373 then if name == "ring" then pk_x, pk_y = X - 4, MY + 10 else pk_x, pk_y = X + 8, MY + 6 end elseif t >= 373 and t < 596 then local k = ease((t - 373) / 17) pk_x, pk_y = 37 + (PK_X - 37) * k, (MY + 6) + (PK_Y - MY - 6) * k elseif t >= 596 and t < 636 then local k = (t - 596) / 40 pk_x, pk_y, al = PK_X, PK_Y - floor(3 * sin(k * 3.14159) + 0.5), 1 - k end local placed = t >= 390 if pk_x and (placed or name == "ring") then draw(PARCEL, PK_PAL, pk_x, pk_y, al) end if X then local nm = name if name == "stride" then nm = (floor(t / 12) % 2 == 0 and "stride" or "stand") .. (t >= 413 and "f" or "") end blend_rect(floor(X) + 1, 63, 11, 1, 40, 56, 110, 0.3) draw(man(nm), PAL, X, MY) end if pk_x and not placed and name ~= "ring" then draw(PARCEL, PK_PAL, pk_x, pk_y) end for i = 1, cfg.snow or 10 do local f = FL[i] local y = (f.y0 + Tm * f.v) % 70 - 3 local x = f.x + f.sw * sin(Tm * 0.021 + f.ph) local ix, iy = floor(x), floor(y) local fx, fy = x - ix, y - iy blend(ix, iy, 255, 255, 255, (1 - fx) * (1 - fy)) blend(ix + 1, iy, 255, 255, 255, fx * (1 - fy)) blend(ix, iy + 1, 255, 255, 255, (1 - fx) * fy) blend(ix + 1, iy + 1, 255, 255, 255, fx * fy) end end