info = { type = "image", title = "Sneezing snowman", category = "holidays", author = "BRRRO", version = "1.0", license = "MIT", desc = "Snowman waves, a flake lands on his nose", api = 33, } local tr = ({ sk = { ["Scarf and mittens"] = "Šál a rukavice", ["Snowfall"] = "Sneženie", ["Sneezes"] = "Kýchanie" }, cs = { ["Scarf and mittens"] = "Šála a rukavice", ["Snowfall"] = "Sněžení", ["Sneezes"] = "Kýchání" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "scarf", label = T("Scarf and mittens"), type = "color", default = "#E0483C" }, { key = "snow", label = T("Snowfall"), type = "int", min = 1, max = 10, default = 5 }, { key = "sneeze", label = T("Sneezes"), type = "bool", default = true }, } local rect, px, blend_rect = gfx.rect, gfx.px, gfx.blend_rect local floor, sin, cos, sqrt = math.floor, math.sin, math.cos, math.sqrt local hash32, umod = math.hash32, math.umod local W, H, FPS = gfx.W, gfx.H, gfx.FPS local STEP_HZ = 30 local RAD = 0.0174533 local SKY0, SKY1 = { 34, 50, 104 }, { 84, 114, 176 } local GROUND, GSH = { 226, 234, 248 }, { 156, 178, 214 } local WHITE, SHADE = { 238, 244, 253 }, { 172, 192, 224 } local COAL, CARROT, CARROT_D = { 24, 26, 40 }, { 244, 128, 36 }, { 196, 88, 24 } local HAT, HAT_L = { 18, 18, 30 }, { 76, 80, 112 } local STICK = { 104, 70, 44 } local CREAM = { 244, 234, 204 } local PINE, PINE_S = { 20, 58, 66 }, { 206, 224, 246 } local MOON, MOON_D = { 252, 246, 214 }, { 214, 206, 160 } local FLAKE = { 230, 240, 255 } local CX, CY = 30, 20 local ARM_L = 8 local function c3(c) return c[1], c[2], c[3] end local function half(r, dy) return floor(sqrt((r + 0.5) * (r + 0.5) - dy * dy)) end local function ball(cx, cy, r, light) for dy = -r, r do local h = half(r, dy) rect(cx - h, cy + dy, 2 * h + 1, 1, c3(WHITE)) rect(cx + h - 1, cy + dy, 2, 1, c3(SHADE)) end end local function pine(x, top, layers) for l = 0, layers - 1 do local y0 = top + l * 7 for i = 0, 8 do local h = 1 + (i * (3 + l)) // 7 rect(x - h, y0 + i, 2 * h + 1, 1, c3(PINE)) end px(x, y0 - 1, c3(PINE)) px(x - 2, y0 + 5, c3(PINE_S)); px(x + 3, y0 + 6, c3(PINE_S)); px(x, y0 + 2, c3(PINE_S)) end rect(x - 1, top + layers * 7 + 2, 3, 3, c3(STICK)) end local BG = nil local function build_bg() local G0 = 52 for y = 0, G0 - 1 do local t = y / (G0 - 1) rect(0, y, W, 1, floor(SKY0[1] + (SKY1[1] - SKY0[1]) * t + 0.5), floor(SKY0[2] + (SKY1[2] - SKY0[2]) * t + 0.5), floor(SKY0[3] + (SKY1[3] - SKY0[3]) * t + 0.5)) end for dy = -4, 4 do local h = half(4, dy) rect(11 - h, 9 + dy, 2 * h + 1, 1, c3(MOON)) end px(9, 8, c3(MOON_D)); px(10, 8, c3(MOON_D)); px(12, 11, c3(MOON_D)) rect(0, G0, W, H - G0, c3(GROUND)) rect(0, G0 - 1, 22, 1, c3(GROUND)) rect(0, G0 - 2, 10, 1, c3(GROUND)) rect(0, G0 + 4, 14, 1, c3(GSH)); rect(22, G0 + 9, 20, 1, c3(GSH)); rect(46, G0 + 6, 12, 1, c3(GSH)) pine(8, 31, 2) pine(54, 24, 3) rect(CX - 9, 56, 24, 1, c3(GSH)); rect(CX - 5, 57, 22, 1, c3(GSH)); rect(CX - 1, 58, 17, 1, c3(GSH)) ball(CX, 46, 10) ball(CX, 33, 7) rect(CX, 31, 1, 2, c3(COAL)); rect(CX, 35, 1, 2, c3(COAL)); rect(CX, 42, 1, 2, c3(COAL)); rect(CX, 46, 1, 2, c3(COAL)) BG = gfx.snapshot(0, 0, W, H) end local NF = 60 local FX, FV, FP, FA = {}, {}, {}, {} for i = 1, NF do local h = hash32(i * 0x9E3779B1) FX[i] = umod(h, W) FV[i] = 0.22 + umod(h >> 6, 30) / 100.0 FP[i] = umod(h >> 12, 628) / 100.0 FA[i] = 0.55 + FV[i] * 0.9 end local FY0 = {} for i = 1, NF do FY0[i] = umod(hash32(i * 0x85EBCA6B), H + 4) end local NP = 9 local PVX, PVY = {}, {} for i = 1, NP do local h = hash32(i * 0x27D4EB2F) PVX[i] = 0.55 + umod(h, 80) / 100.0 PVY[i] = -0.9 + umod(h >> 8, 90) / 50.0 end local function flake_at(x, y, a) local iy = floor(y) local f = y - iy blend_rect(x, iy, 1, 1, FLAKE[1], FLAKE[2], FLAKE[3], a * (1 - f)) blend_rect(x, iy + 1, 1, 1, FLAKE[1], FLAKE[2], FLAKE[3], a * f) end local function soft(x, y, w, h, r, g, b) local ix, iy = floor(x), floor(y) local fx, fy = x - ix, y - iy rect(ix + 1, iy + 1, w - 1, h - 1, r, g, b) blend_rect(ix, iy + 1, 1, h - 1, r, g, b, 1 - fx) blend_rect(ix + w, iy + 1, 1, h - 1, r, g, b, fx) blend_rect(ix + 1, iy, w - 1, 1, r, g, b, 1 - fy) blend_rect(ix + 1, iy + h, w - 1, 1, r, g, b, fy) blend_rect(ix, iy, 1, 1, r, g, b, (1 - fx) * (1 - fy)) blend_rect(ix + w, iy, 1, 1, r, g, b, fx * (1 - fy)) blend_rect(ix, iy + h, 1, 1, r, g, b, (1 - fx) * fy) blend_rect(ix + w, iy + h, 1, 1, r, g, b, fx * fy) end local function ramp(p, a, b) if p <= a then return 0 elseif p >= b then return 1 end local t = (p - a) / (b - a) return t * t * (3 - 2 * t) end local function arm(sx, sy, ang, r, g, b) local dx, dy = cos(ang), -sin(ang) for k = 1, ARM_L do px(floor(sx + dx * k + 0.5), floor(sy + dy * k + 0.5), c3(STICK)) end soft(sx + dx * (ARM_L + 1) - 1, sy + dy * (ARM_L + 1) - 1, 3, 3, r, g, b) end local function draw_head(ox, oy, sc, shut, open) local cx, cy = CX + ox, CY + oy for dy = -6, 6 do local h = half(6, dy) rect(cx - h, cy + dy, 2 * h + 1, 1, c3(WHITE)) rect(cx + h - 1, cy + dy, 2, 1, c3(SHADE)) end rect(cx - 8, cy - 8, 17, 2, c3(HAT)) rect(cx - 5, cy - 17, 11, 9, c3(HAT)) rect(cx - 5, cy - 17, 1, 9, c3(HAT_L)) rect(cx - 5, cy - 11, 11, 2, sc.r, sc.g, sc.b) if shut then rect(cx - 4, cy - 1, 3, 1, c3(COAL)); rect(cx + 2, cy - 1, 3, 1, c3(COAL)) else rect(cx - 3, cy - 2, 1, 2, c3(COAL)); rect(cx + 3, cy - 2, 1, 2, c3(COAL)) end rect(cx, cy + 1, 3, 2, c3(CARROT)) rect(cx + 3, cy + 1, 2, 1, c3(CARROT_D)) if open then rect(cx - 1, cy + 3, 4, 3, c3(COAL)) else px(cx - 2, cy + 3, c3(COAL)); px(cx + 4, cy + 3, c3(COAL)) rect(cx - 1, cy + 4, 5, 1, c3(COAL)) end end local function draw_scarf(sc, t) local r, g, b = sc.r, sc.g, sc.b rect(CX - 5, 25, 11, 1, r, g, b) rect(CX - 6, 26, 13, 2, r, g, b) rect(CX - 6, 26, 13, 1, CREAM[1], CREAM[2], CREAM[3]) for i = 0, 11 do local k = i < 4 and i / 4 or 1 local yy = 27 + i * 0.22 + 1.5 * k * sin(i * 0.6 - t * 0.22) local iy = floor(yy) local f = yy - iy local x = CX + 5 + i local a, bb, cc = r, g, b if (i // 3) % 2 == 1 then a, bb, cc = CREAM[1], CREAM[2], CREAM[3] end rect(x, iy + 1, 1, 2, a, bb, cc) blend_rect(x, iy, 1, 1, a, bb, cc, 1 - f) blend_rect(x, iy + 3, 1, 1, a, bb, cc, f) end end function render() if not BG then build_bg() end gfx.blit_rgb(BG, 0, 0, W, H) local t = frame * STEP_HZ // FPS local sc = cfg.scarf or { r = 224, g = 72, b = 60 } local sneeze = cfg.sneeze ~= false local p = t % 360 local n = (cfg.snow or 5) * 6 for i = 1, n do local y = (FY0[i] + FV[i] * t) % (H + 4) - 2 local x = floor(FX[i] + 1.8 * sin(t * 0.045 + FP[i])) % W flake_at(x, y, FA[i]) end local ox, oy, shut, open = 0, 0, false, false local e = 1 if sneeze then e = 1 - ramp(p, 150, 170) + ramp(p, 270, 290) if p >= 192 and p < 214 then oy = -1 end if p >= 214 and p < 222 then ox, oy = 1, 2; open = true elseif p >= 222 and p < 238 then oy = 1 end if p >= 198 and p < 238 then shut = true end end draw_head(ox, oy, sc, shut, open) draw_scarf(sc, t) if sneeze and p >= 110 and p < 214 then local lx, ly = CX + ox + 4, CY + oy + 1 if p < 170 then local u = (p - 110) / 60 flake_at(floor(lx + 9 * (1 - u) + 0.5), -2 + (ly + 2) * u * u * 0.6 + (ly + 2) * u * 0.4, 1) else px(lx, ly, c3(FLAKE)); px(lx - 1, ly - 1, c3(FLAKE)) end end arm(CX - 7, 30, (180 + 28) * RAD + 0.05 * sin(t * 0.05), sc.r, sc.g, sc.b) local up, rest = 62 * RAD, -34 * RAD local a = rest + e * (up - rest) + e * 22 * RAD * sin(t * 0.17) arm(CX + 7, 30, a, sc.r, sc.g, sc.b) if sneeze and p >= 214 and p < 262 then local nn = p - 214 local S = (1 - 0.9 ^ nn) / 0.1 local al = nn < 28 and 1 or 1 - (nn - 28) / 20 for i = 1, NP do flake_at(floor(CX + 5 + PVX[i] * S + 0.5), CY + 2 + PVY[i] * S + 0.28 * nn, al) end end end