info = { type = "image", title = "Melting snowman", category = "holidays", author = "BRRRO", version = "1.0", license = "MIT", desc = "Snowman melts in the sun, snow rebuilds him", api = 33, } local tr = ({ sk = { ["Speed"] = "Rýchlosť", ["Snowfall"] = "Sneženie", ["Hat colour"] = "Farba klobúka", slow = "pomaly", normal = "normálne", fast = "rýchlo" }, cs = { ["Speed"] = "Rychlost", ["Snowfall"] = "Sněžení", ["Hat colour"] = "Barva klobouku", slow = "pomalu", normal = "normálně", fast = "rychle" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "speed", label = T("Speed"), type = "int", default = 1, opts = { T("slow"), T("normal"), T("fast") } }, { key = "snow", label = T("Snowfall"), type = "bool", default = true }, { key = "hat", label = T("Hat colour"), type = "color", default = "#2B2D42" }, } 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 hash32, umod = math.hash32, math.umod local W, H = gfx.W, gfx.H local CX, GY, HORIZON = 22, 57, 44 local CYC = 1260 local T_MELT, STAGE = 60, 54 local T_FALL, T_BUILD, T_HAT, T_END = 700, 820, 1100, 1180 local SNOW, SHADE, OUT = { 240, 246, 252 }, { 176, 198, 224 }, { 140, 170, 202 } local COAL, CARROT, TWIG = { 36, 36, 44 }, { 244, 128, 28 }, { 112, 68, 42 } local HAT, BAND = { 43, 45, 66 }, { 190, 48, 52 } local function c3(c) return c[1], c[2], c[3] end local SPEED, SNOWING = 1, true function on_settings() SPEED = ({ 0.6, 1, 1.7 })[(cfg.speed or 1) + 1] SNOWING = cfg.snow ~= false local c = cfg.hat or { r = 43, g = 45, b = 66 } HAT = { c.r, c.g, c.b } BAND = { 190, 48, 52 } end local BG = nil local function build_bg() for y = 0, HORIZON - 1 do local t = y / HORIZON rect(0, y, W, 1, floor(122 + 86 * t), floor(180 + 52 * t), floor(228 + 20 * t)) end for x = 0, W - 1 do local top = floor(HORIZON - 3 + 1.5 * sin(x * 0.13) + 1.1 * sin(x * 0.37 + 1) + 0.5) rect(x, top, 1, HORIZON - top, 196, 216, 228) end for y = HORIZON, H - 1 do local t = (y - HORIZON) / (H - HORIZON) rect(0, y, W, 1, floor(240 - 22 * t), floor(246 - 16 * t), floor(252 - 10 * t)) end rect(0, HORIZON, W, 1, 214, 226, 238) BG = gfx.snapshot(0, 0, W, H) end local function ell(cx, cy, rx, ry, c, a) for dy = -ry, ry do local h = floor(rx * sqrt(max(0, 1 - (dy * dy) / (ry * ry + 0.5))) + 0.5) if a then blend_rect(cx - h, cy + dy, 2 * h + 1, 1, c[1], c[2], c[3], a) else rect(cx - h, cy + dy, 2 * h + 1, 1, c3(c)) end end end local function ramp(t, a, b) return min(1, max(0, (t - a) / (b - a))) end local function melt(t) if t < T_FALL then return ramp(t, 120, 640) end if t < 760 then return 1 end return 1 - ramp(t, 760, 1000) end local function stage(t) if t < T_MELT then return 0 end if t < T_FALL then return min(9, (t - T_MELT) // STAGE) end if t < T_BUILD then return 9 end if t < T_HAT then return max(0, 9 - (t - T_BUILD) // 31) end return 0 end local PATCH = { { 6, 50, 5 }, { 14, 60, 6 }, { 38, 53, 6 }, { 48, 59, 7 }, { 55, 48, 5 }, { 30, 62, 6 }, { 60, 56, 4 } } local function ground(m) if m <= 0.02 then return end for _, p in ipairs(PATCH) do local rx, ry = floor(p[3] * m + 0.5), floor(p[3] * 0.35 * m + 0.5) if rx >= 1 then ell(p[1], p[2], rx, ry, { 98, 152, 72 }) if ry >= 1 then rect(p[1] - rx + 1, p[2] - ry, rx, 1, 126, 176, 88) end if m > 0.7 and rx >= 4 then px(p[1] + 1, p[2] - ry - 1, 255, 238, 120) end end end end local function dims(k) local ry1 = max(3, floor(11 - k * 0.9 + 0.5)) local rx1 = floor(11 - k * 0.3 + 0.5) local r2 = max(0, floor(8 * (1 - k / 7.5) + 0.5)) local r3 = max(0, floor(6 * (1 - k / 5.5) + 0.5)) local y1 = GY - ry1 local y2 = r2 > 0 and (y1 - ry1 - r2 + 3) or y1 local y3 = r3 > 0 and (y2 - (r2 > 0 and r2 or ry1) - r3 + 2) or y2 return rx1, ry1, r2, r3, y1, y2, y3 end local function hat(x, y, tilt) local s = tilt and 2 or 0 rect(x - 5 + s, y - 1 + (tilt and 1 or 0), 11, 1, c3(HAT)) rect(x - 3 + s, y - 8 + (tilt and 1 or 0), 7, 7, c3(HAT)) rect(x - 3 + s, y - 3 + (tilt and 1 or 0), 7, 1, c3(BAND)) end local function carrot(x, y, len) rect(x, y, len, 1, c3(CARROT)); px(x + len - 1, y, 200, 90, 20) end local function twig(x, y, d) for s = 0, 7 do px(x + d * s, y - floor(s * 0.6), c3(TWIG)) end px(x + d * 5, y - 5, c3(TWIG)); px(x + d * 6, y - 6, c3(TWIG)) end local function flakes(t, a) for i = 0, 25 do if i < 26 * a then local h = hash32(i * 0x9E3779B1) local x = umod(h, W) + 2 * sin(t * 0.05 + i) local y = (umod(h >> 8, 70) + t * (0.35 + umod(h >> 16, 8) * 0.06)) % 70 - 3 local ix, iy = floor(x - 0.5), floor(y - 0.5) local fx, fy = x - 0.5 - ix, y - 0.5 - 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 end function render() if not BG then build_bg() end gfx.blit_rgb(BG, 0, 0, W, H) local t = floor(frame * SPEED) % CYC local m = melt(t) local env = ramp(t, T_FALL, 760) * (1 - ramp(t, T_HAT + 30, T_END)) if not SNOWING then env = 0 end local sx, sy = 54, 9 local sa = t * 0.004 for i = 0, 7 do local a = sa + i * 0.7854 local len = 8 + (i % 2) * 2 for s = 6, len do blend(floor(sx + 0.5 + s * cos(a)), floor(sy + 0.5 + s * sin(a)), 255, 226, 120, 1 - env) end end ell(sx, sy, 5, 5, { 255, 232, 130 }) ell(sx, sy, 3, 3, { 255, 246, 190 }) if env > 0 then blend_rect(0, 0, W, HORIZON, 120, 130, 150, 0.4 * env) for _, c in ipairs({ { 52, 8, 13, 5 }, { 44, 5, 9, 4 }, { 60, 11, 8, 4 } }) do ell(c[1], c[2], c[3], c[4], { 168, 178, 196 }, 0.95 * env) end end ground(m) local k = stage(t) local rx1, ry1, r2, r3, y1, y2, y3 = dims(k) if m > 0.04 then local prx, pry = floor(2 + 12 * m + 0.5), floor(1 + 2 * m + 0.5) ell(CX + 1, GY - 1, prx, pry, { 92, 148, 214 }) rect(CX + 1 - prx // 2 + floor(1.5 * sin(t * 0.08)), GY - pry, prx, 1, 190, 220, 244) end local lay = ramp(m, 0.1, 0.4) local carrot_down = (t >= T_MELT + 2 * STAGE + 14 and t < T_HAT + 35) local hat_down = (t >= T_MELT + 4 * STAGE + 16 and t < T_HAT) local twigs_down = (t >= T_MELT + 6 * STAGE and t < T_BUILD + 4 * 31) if lay > 0 then if carrot_down then carrot(CX + 8, GY + 1, 4) end if hat_down then rect(CX + 9, GY - 1, 7, 4, c3(HAT)); rect(CX + 16, GY - 2, 1, 6, c3(HAT)) end if twigs_down then rect(CX - 16, GY + 1, 7, 1, c3(TWIG)); rect(CX + 12, GY + 3, 6, 1, c3(TWIG)) end end local balls = { { CX, y1, rx1, ry1 } } if r2 > 0 then balls[#balls + 1] = { CX, y2, r2, r2 } end if r3 > 0 then balls[#balls + 1] = { CX, y3, r3, r3 } end for _, b in ipairs(balls) do ell(b[1], b[2], b[3] + 1, b[4] + 1, OUT) end for _, b in ipairs(balls) do ell(b[1], b[2], b[3], b[4], SNOW) end for _, b in ipairs(balls) do local rx, ry = b[3], b[4] if rx >= 3 then for dy = -ry + 1, ry do local h = floor(rx * sqrt(max(0, 1 - (dy * dy) / (ry * ry + 0.5))) + 0.5) local w = dy > ry - 3 and h or max(1, h // 4) rect(b[1] + h - w + 1, b[2] + dy, w, 1, c3(SHADE)) end px(b[1] - rx // 2, b[2] - ry // 2, 255, 255, 255) end end if r3 >= 3 then local ex = r3 >= 5 and 2 or 1 px(CX - ex, y3 - 1, c3(COAL)); px(CX + ex, y3 - 1, c3(COAL)) if r3 >= 5 then for dx = -2, 2 do px(CX + dx, y3 + 2 + (dx * dx > 1 and 0 or 1), c3(COAL)) end end end if r2 >= 4 then px(CX, y2 - 1, c3(COAL)); px(CX, y2 + 2, c3(COAL)) end local on_face = t < T_MELT + 2 * STAGE or t >= T_HAT + 35 local nlen = max(2, r3 - 2) if r3 >= 3 and on_face then if t >= T_MELT + STAGE and t < T_MELT + 2 * STAGE then carrot(CX + 1, y3 + 1, nlen - 1); px(CX + nlen, y3 + 2, 200, 90, 20) else carrot(CX + 1, y3 + 1, nlen) end elseif t >= T_MELT + 2 * STAGE and t < T_MELT + 2 * STAGE + 14 then local u = (t - T_MELT - 2 * STAGE) / 14 carrot(floor(CX + 3 + 5 * u + 0.5), floor((y3 + 1) + (GY - y3) * u * u + 0.5), 4) end if r2 >= 3 then twig(CX - r2, y2 - 1, -1); twig(CX + r2, y2 - 1, 1) end if r3 >= 3 then local top = y3 - r3 + 1 if t < T_MELT + 3 * STAGE or (t >= T_HAT + 30) then hat(CX, top, false) elseif t < T_MELT + 4 * STAGE then hat(CX, top, true) elseif t >= T_HAT and t < T_HAT + 30 then local u = (t - T_HAT) / 30 hat(CX, floor(-8 + (top + 8) * u * u + 0.5), false) end elseif t >= T_MELT + 4 * STAGE and t < T_MELT + 4 * STAGE + 16 then local u = (t - T_MELT - 4 * STAGE) / 16 hat(floor(CX + 2 + 7 * u + 0.5), floor((y3 - 1) + (GY + 1 - y3) * u * u + 0.5), true) end if env > 0 then flakes(t, env) end end