info = { type = "image", title = "Snowman builder", category = "holidays", author = "BRRRO", version = "1.0", license = "MIT", desc = "A little robot builds a snowman in the snow", api = 33, } local tr = ({ sk = { ["Snow"] = "Sneženie", ["None"] = "Žiadne", ["Light"] = "Slabé", ["Heavy"] = "Silné", ["Robot"] = "Robot", ["Top hat"] = "Cylinder" }, cs = { ["Snow"] = "Sněžení", ["None"] = "Žádné", ["Light"] = "Slabé", ["Heavy"] = "Silné", ["Robot"] = "Robot", ["Top hat"] = "Cylindr" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "snow", label = T("Snow"), type = "int", default = 1, opts = { T("None"), T("Light"), T("Heavy") } }, { key = "tone", label = T("Robot"), type = "color", default = "#3C82DC" }, { key = "hat", label = T("Top hat"), type = "bool", default = true }, } local rect, px, blend, blit = gfx.rect, gfx.px, gfx.blend, gfx.blit_indexed local blend_rect, blit_rgb = gfx.blend_rect, gfx.blit_rgb local floor, sin, sqrt, min, max = math.floor, math.sin, math.sqrt, math.min, math.max local hash32, umod = math.hash32, math.umod local W, H, FPS = gfx.W, gfx.H, gfx.FPS local GY = 53 local TOT, OFS = 440, 50 local SXL = 37 local TRAIL = { 198, 212, 238 } local function rgb(r, g, b) return floor(r + 0.5), floor(g + 0.5), floor(b + 0.5) end local function mix(a, b, t) return a[1] + (b[1] - a[1]) * t, a[2] + (b[2] - a[2]) * t, a[3] + (b[3] - a[3]) * t end local function ease(t) if t <= 0 then return 0 elseif t >= 1 then return 1 end return t * t * (3 - 2 * t) end local BG, PART, STAGE = nil, nil, 1 local BALL = {} local BALL_PAL = { 0, 0, 0, 252, 254, 255, 222, 232, 250, 176, 196, 236, 140, 164, 214 } local function pine(x, base, h) for r = 0, h - 1 do local hw = max(0, floor(r * 0.42) - (r % 3 == 2 and 1 or 0)) rect(x - hw, base - h + r, 2 * hw + 1, 1, 46, 82, 100) if r % 3 == 0 then px(x - hw, base - h + r, 200, 216, 238) end end rect(x, base, 1, 2, 70, 56, 48) end local function scene() local top, hor = { 122, 164, 224 }, { 218, 232, 250 } for y = 0, 42 do rect(0, y, W, 1, rgb(mix(top, hor, (y / 42) ^ 1.2))) end rect(0, 43, W, 3, 196, 212, 240) for x = 0, W - 1 do local y = 40 + floor(2.2 * sin(x * 0.17) + 1.6 * sin(x * 0.43 + 1) + 0.5) rect(x, y, 1, 4, 204, 218, 242) end for _, p in ipairs({ { 4, 46, 13 }, { 11, 46, 9 }, { 17, 45, 7 }, { 57, 46, 12 }, { 62, 46, 8 }, { 53, 45, 7 } }) do pine(p[1], p[2], p[3]) end local g0, g1 = { 238, 244, 255 }, { 198, 214, 244 } for y = 46, H - 1 do rect(0, y, W, 1, rgb(mix(g0, g1, ((y - 46) / 17) ^ 1.1))) end blend_rect(0, 49, 30, 2, 255, 255, 255, 0.45) blend_rect(34, 58, 30, 2, 255, 255, 255, 0.35) end local function discs() for D = 5, 15, 2 do local c = (D - 1) / 2 local t = {} for j = 0, D - 1 do for i = 0, D - 1 do local dx, dy = i - c, j - c local d = sqrt(dx * dx + dy * dy) local v = 0 if d <= D / 2 - 0.05 then local l = -(dx * 0.5 + dy * 0.7) / (D / 2) v = l > 0.35 and 1 or (l > -0.15 and 2 or 3) if d > D / 2 - 0.95 and l < 0 then v = 4 end end t[#t + 1] = v end end BALL[D] = string.char(table.unpack(t)) end end local STAGES = { scene, discs } local function build() if STAGE > 1 then blit_rgb(PART, 0, 0, W, H) end STAGES[STAGE]() if STAGE == #STAGES then BG = PART or BG else PART = gfx.snapshot(0, 0, W, H) end STAGE = STAGE + 1 end local NF, HAT = 10, true local C_BODY, C_SH, C_DK, C_HEAD, C_HSH = {}, {}, {}, {}, {} local FLAKES = {} for i = 1, 24 do local h = hash32(i * 0x9E3779B1) FLAKES[i] = { x = umod(h, W), y = umod(h >> 6, 70), v = 0.35 + umod(h >> 12, 50) / 100, amp = 1 + umod(h >> 18, 20) / 10, w = 0.03 + umod(h >> 22, 4) / 100, ph = umod(h >> 8, 628) / 100 } end function on_settings() NF = ({ [0] = 0, 10, 24 })[cfg.snow] or 10 HAT = cfg.hat ~= false local c = cfg.tone or { r = 60, g = 130, b = 220 } C_BODY = { c.r, c.g, c.b } C_SH = { c.r * 7 // 10, c.g * 7 // 10, c.b * 7 // 10 } C_DK = { c.r * 45 // 100, c.g * 45 // 100, c.b * 45 // 100 } C_HEAD = { c.r + (255 - c.r) * 45 // 100, c.g + (255 - c.g) * 45 // 100, c.b + (255 - c.b) * 45 // 100 } C_HSH = { c.r + (255 - c.r) * 22 // 100, c.g + (255 - c.g) * 22 // 100, c.b + (255 - c.b) * 22 // 100 } end local VISOR, EYE, RED = { 20, 28, 56 }, { 150, 250, 255 }, { 255, 90, 80 } local COAL, ORANGE, TWIG = { 24, 30, 48 }, { 255, 142, 32 }, { 100, 68, 38 } local function robot(rx, dy, face, arms, leg, eyes) local ry = GY + dy local function R(dx, y, w, h, c) rect(face == 1 and rx + dx or rx + 8 - dx - w + 1, ry + y, w, h, c[1], c[2], c[3]) end R(4, -15, 1, 1, RED); R(4, -14, 1, 1, C_DK) R(2, -13, 5, 1, C_HEAD); R(1, -12, 7, 4, C_HEAD); R(2, -8, 5, 1, C_HEAD) R(7, -12, 1, 4, C_HSH) R(2, -11, 5, 2, VISOR) if eyes == 1 then R(2, -11, 2, 2, { 255, 255, 255 }); R(5, -11, 2, 2, { 255, 255, 255 }); R(3, -10, 1, 1, VISOR); R(6, -10, 1, 1, VISOR) else R(3, -11, 1, 2, EYE); R(5, -11, 1, 2, EYE) end R(3, -7, 3, 1, C_DK) R(2, -6, 5, 4, C_BODY); R(4, -5, 1, 1, C_HEAD); R(2, -3, 5, 1, C_DK) if leg == 0 then R(2, -2, 2, 2, C_DK); R(5, -2, 2, 1, C_DK) else R(2, -2, 2, 1, C_DK); R(5, -2, 2, 2, C_DK) end if arms == 0 then R(1, -6, 1, 3, C_SH); R(7, -6, 1, 3, C_SH) elseif arms == 1 then R(7, -6, 4, 1, C_SH); R(1, -6, 1, 3, C_SH) elseif arms == 2 then R(1, -10, 1, 4, C_SH); R(7, -10, 1, 4, C_SH) elseif arms == 3 then R(-2, -9, 4, 1, C_SH); R(7, -6, 1, 3, C_SH) else R(7, -9, 4, 1, C_SH); R(1, -6, 1, 3, C_SH) end return ry end local function ball_at(D, left, top) blit(BALL[D], BALL_PAL, left, top, D, D, D, D, true) end local function toss(s, l0, l1, t0, t1, up) local e = ease(s) return floor(l0 + (l1 - l0) * e + 0.5), floor(t0 + (t1 - t0) * e - up * 4 * s * (1 - s) + 0.5) end local function foreground(t) local rx, face, arms, leg, dy, eyes = -14, 1, 0, (t // 4) % 2, 0, 0 local carrot = false if t < 15 then elseif t < 55 then rx, arms = -14 + (t - 15), 1 elseif t < 70 then rx, arms = 26, (t // 3) % 2 == 0 and 1 or 0 elseif t < 90 then rx, face = 26 - (t - 70) * 2, -1 elseif t < 119 then rx, arms = -14 + (t - 90), 1 elseif t < 141 then rx, arms = 15, 2 elseif t < 153 then rx, arms, dy = 15, 2, (t // 4) % 2 == 0 and -1 or 0 elseif t < 168 then rx, face = 15 - (t - 153) * 2, -1 elseif t < 199 then rx, arms = -14 + (t - 168), 1 elseif t < 221 then rx, arms = 17, 2 elseif t < 233 then rx, arms, dy = 17, 2, (t // 4) % 2 == 0 and -1 or 0 elseif t < 249 then rx, face = 17 - (t - 233) * 2, -1 elseif t < 283 then rx, arms, carrot = -14 + (t - 249), 0, true elseif t < 293 then rx, arms, carrot = 20, 3, true elseif t < 297 then rx, arms, carrot = 20, 4, true elseif t < 311 then rx, arms = 20, 4 elseif t < 326 then rx, arms, dy = 20, 2, (t // 4) % 2 == 0 and -1 or 0 elseif t < 334 then local s = (t - 326) / 8 rx, arms, eyes, dy = 20 - floor(6 * s + 0.5), 2, 1, -floor(5 * 4 * s * (1 - s) + 0.5) elseif t < 362 then rx, arms, eyes = 14 + ((t // 2) % 2), 2, 1 elseif t < 376 then rx, face = 14 - (t - 362) * 2, -1 end local tx = 0 if t >= 15 then local p, D, l if t < 55 then p = (t - 15) / 40 D = 5 + 2 * min(5, floor(p * 6)); l = rx + 11 else D, l = 15, SXL end ball_at(D, l, GY - D) tx = l + D // 2 end if t >= 90 then local D, l, top if t < 119 then D = 5 + 2 * min(3, floor((t - 90) / 29 * 4)); l, top = rx + 11, GY - D elseif t < 141 then D = 11; l, top = toss((t - 119) / 22, 26, 39, GY - 11, 29, 12) else D, l, top = 11, 39, 29 end ball_at(D, l, top) end if t >= 168 then local D, l, top if t < 199 then D = 5 + 2 * min(2, floor((t - 168) / 31 * 3)); l, top = rx + 11, GY - D elseif t < 221 then D = 9; l, top = toss((t - 199) / 22, 28, 40, GY - 9, 22, 12) else D, l, top = 9, 40, 22 end ball_at(D, l, top) end if tx > 0 then rect(0, GY, tx, 2, TRAIL[1], TRAIL[2], TRAIL[3]) end if t >= 221 then if t >= 322 and t < 326 or t >= 390 and t < 394 then rect(41, 25, 2, 1, COAL[1], COAL[2], COAL[3]); rect(46, 25, 2, 1, COAL[1], COAL[2], COAL[3]) else rect(41, 24, 2, 2, COAL[1], COAL[2], COAL[3]); rect(46, 24, 2, 2, COAL[1], COAL[2], COAL[3]) end for _, x in ipairs({ 41, 47 }) do px(x, 28, COAL[1], COAL[2], COAL[3]) end for _, x in ipairs({ 42, 44, 46 }) do px(x, 29, COAL[1], COAL[2], COAL[3]) end rect(43, 32, 2, 2, COAL[1], COAL[2], COAL[3]); rect(43, 36, 2, 2, COAL[1], COAL[2], COAL[3]) if HAT then rect(40, 21, 9, 1, 28, 30, 44); rect(42, 16, 5, 5, 28, 30, 44); rect(42, 19, 5, 1, RED[1], RED[2], RED[3]) end end if t >= 311 then rect(44, 26, 2, 2, ORANGE[1], ORANGE[2], ORANGE[3]); rect(46, 27, 3, 1, ORANGE[1], ORANGE[2], ORANGE[3]) for _, p in ipairs({ { 38, 33 }, { 37, 32 }, { 36, 31 }, { 35, 30 }, { 34, 30 }, { 35, 29 } }) do px(p[1], p[2], TWIG[1], TWIG[2], TWIG[3]); px(88 - p[1], p[2], TWIG[1], TWIG[2], TWIG[3]) end elseif t >= 297 then local x, y = toss((t - 297) / 14, 30, 44, 44, 27, 12) rect(x, y, 4, 1, ORANGE[1], ORANGE[2], ORANGE[3]) end local ry = robot(rx, dy, face, arms, leg, eyes) if carrot then rect(rx + 9, ry - 9, 4, 1, ORANGE[1], ORANGE[2], ORANGE[3]) end if t >= 326 and t < 362 then local ex = rx + 4 rect(ex, ry - 22, 1, 3, RED[1], RED[2], RED[3]); px(ex, ry - 18, RED[1], RED[2], RED[3]) end end local function flakes() for i = 1, NF do local f = FLAKES[i] local y = (f.y + f.v * frame) % (H + 4) - 2 local x = floor(f.x + f.amp * sin(frame * f.w + f.ph) + 0.5) local iy = floor(y) local k = y - iy blend(x, iy, 255, 255, 255, 0.9 * (1 - k)) blend(x, iy + 1, 255, 255, 255, 0.9 * k) end end function render() if not BG then build(); return end blit_rgb(BG, 0, 0, W, H) local t = (frame + OFS) % TOT if t >= 410 then local xf = floor((t - 410) / 26 * 76) if xf < W then gfx.clip(xf, 0, W - xf, H) foreground(t) gfx.clip() end for i = 1, 8 do local h = hash32(i * 0x85EBCA6B) blend_rect(xf - 2 + umod(h, 7), 18 + umod(h >> 8, 36), 5 + umod(h >> 16, 5), 1, 255, 255, 255, 0.85) end else foreground(t) end flakes() end