info = { type = "image", title = "Snow leap", category = "animals", author = "BRRRO", version = "1.0", license = "MIT", desc = "A cat in a Santa hat leaps across the snow", api = 33, } local tr = ({ sk = { ["Snow"] = "Sneh", ["None"] = "Žiadny", ["Light"] = "Slabý", ["Heavy"] = "Hustý", ["Hat"] = "Čiapka", ["Coat"] = "Srsť", ["White"] = "Biela", ["Ginger"] = "Ryšavá", ["Grey"] = "Sivá" }, cs = { ["Snow"] = "Sníh", ["None"] = "Žádný", ["Light"] = "Slabý", ["Heavy"] = "Hustý", ["Hat"] = "Čepice", ["Coat"] = "Srst", ["White"] = "Bílá", ["Ginger"] = "Zrzavá", ["Grey"] = "Šedá" }, })[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 = "hat", label = T("Hat"), type = "bool", default = true }, { key = "coat", label = T("Coat"), type = "int", default = 0, opts = { T("White"), T("Ginger"), T("Grey") } }, } local rect, px, blend, blend_rect, blit_rgb, blit_idx = gfx.rect, gfx.px, gfx.blend, gfx.blend_rect, gfx.blit_rgb, gfx.blit_indexed local floor, sin, abs, min, max = math.floor, math.sin, math.abs, math.min, math.max local hash32 = math.hash32 local W, H = gfx.W, gfx.H local GY = 55 local CW, CH = 22, 18 local BG local function lerp(a, b, t) return a + (b - a) * t end local function build_bg() for y = 0, 39 do local t = y / 39 rect(0, y, W, 1, floor(lerp(120, 172, t)), floor(lerp(130, 180, t)), floor(lerp(152, 196, t))) end for y = 40, H - 1 do local t = (y - 40) / (H - 41) rect(0, y, W, 1, floor(lerp(180, 214, t)), floor(lerp(186, 218, t)), floor(lerp(200, 230, t))) end for x = 0, W - 1 do local h = floor(3 + 2 * math.sin(x * 0.13) + 1.5 * math.sin(x * 0.31 + 1)) rect(x, 40 - h, 1, h, 178, 186, 202) end local function fir(x, top, h) for k = 0, h - 1 do local w = 1 + floor(k * 3.2 / h * 2) rect(x - w // 2, top + k, w, 1, 52, 78, 84) end rect(x, top + h, 1, 2, 70, 54, 48) px(x, top, 232, 238, 246) end fir(7, 28, 12); fir(12, 32, 8); fir(57, 29, 11) BG = gfx.snapshot(0, 0, W, H) end local POSES = { run1 = { body = { 8, 8, 6, 3 }, head = { 15, 6 }, tail = { { 3, 7 }, { 2, 6 }, { 1, 5 }, { 1, 4 } }, legs = { { 12, 10, 17, 13 }, { 10, 10, 14, 14 }, { 5, 10, 1, 13 }, { 7, 10, 3, 14 } } }, run2 = { body = { 9, 8, 6, 3 }, head = { 15, 7 }, tail = { { 3, 6 }, { 2, 5 }, { 2, 4 }, { 3, 3 } }, legs = { { 12, 10, 13, 14 }, { 13, 10, 16, 13 }, { 6, 10, 5, 14 }, { 4, 10, 6, 13 } } }, crouch = { body = { 9, 10, 6.5, 2.5 }, head = { 15, 9 }, tail = { { 3, 10 }, { 2, 9 }, { 1, 8 } }, legs = { { 12, 11, 14, 14 }, { 5, 11, 4, 14 } } }, up = { body = { 9, 8, 6, 3 }, head = { 15, 4 }, tail = { { 3, 8 }, { 2, 9 }, { 1, 10 } }, legs = { { 13, 6, 18, 5 }, { 12, 8, 17, 8 }, { 5, 10, 1, 13 }, { 6, 10, 2, 14 } } }, peak = { body = { 9, 7, 5.5, 3 }, head = { 15, 6 }, tail = { { 3, 6 }, { 2, 5 }, { 1, 4 } }, legs = { { 12, 9, 15, 11 }, { 5, 9, 3, 12 } } }, down = { body = { 9, 8, 5.5, 3 }, head = { 15, 9 }, tail = { { 3, 6 }, { 2, 5 }, { 2, 3 } }, legs = { { 13, 10, 17, 14 }, { 11, 10, 15, 14 }, { 5, 10, 2, 12 }, { 7, 10, 4, 13 } } }, land = { body = { 9, 10, 7, 2.2 }, head = { 15, 10 }, tail = { { 2, 10 }, { 1, 9 } }, legs = { { 12, 11, 16, 14 }, { 5, 11, 2, 14 } } }, } local function gen(p) local fill = {} local function set(x, y) x, y = x + 1, y + 1 if x >= 0 and x < CW and y >= 0 and y < CH then fill[y * CW + x + 1] = 1 end end local function ellipse(cx, cy, rx, ry) for y = 0, CH - 1 do for x = 0, CW - 1 do local a, b = (x + 0.5 - cx) / rx, (y + 0.5 - cy) / ry if a * a + b * b <= 1 then set(x, y) end end end end local function line(x0, y0, x1, y1) local n = max(abs(x1 - x0), abs(y1 - y0)) * 2 for i = 0, n do local x, y = lerp(x0, x1, i / n), lerp(y0, y1, i / n) set(floor(x), floor(y)); set(floor(x) + 1, floor(y)); set(floor(x), floor(y) + 1); set(floor(x) + 1, floor(y) + 1) end end local b, hd = p.body, { p.head[1] + 1, p.head[2] - 1 } ellipse(b[1], b[2], b[3], b[4]) ellipse(hd[1], hd[2], 3.2, 3.2) for _, l in ipairs(p.legs) do line(l[1], l[2], l[3], l[4]) end for _, t in ipairs(p.tail) do set(t[1], t[2]); set(t[1] + 1, t[2]) end local hx, hy = floor(hd[1]), floor(hd[2]) for _, e in ipairs({ { -3, -3 }, { -3, -4 }, { -2, -3 }, { 2, -3 }, { 2, -4 }, { 1, -3 } }) do set(hx + e[1], hy + e[2]) end local out = {} for y = 0, CH - 1 do for x = 0, CW - 1 do local i = y * CW + x + 1 local v = 0 if fill[i] then v = 2 if not fill[i + 2 * CW] and y > b[2] then v = 3 end elseif (x > 0 and fill[i - 1]) or (x < CW - 1 and fill[i + 1]) or fill[i - CW] or fill[i + CW] then v = 1 end out[i] = v end end hx, hy = hx + 1, hy + 1 out[hy * CW + hx - 1 + 1] = 4 out[hy * CW + hx + 1 + 1] = 4 out[(hy + 1) * CW + hx + 3 + 1] = 5 return out, hx, hy end local HAT = { "8....", "66...", "6666.", "77777" } local function paint_hat(out, hx, hy) for r = 1, 4 do for c = 1, 5 do local ch = HAT[r]:sub(c, c) if ch ~= "." then out[(hy - 7 + r) * CW + hx - 3 + c] = tonumber(ch) + 0 end end end end local function pack(t, mirror) local rows = {} for y = 0, CH - 1 do local r = {} for x = 0, CW - 1 do r[x + 1] = t[y * CW + (mirror and CW - 1 - x or x) + 1] or 0 end rows[y + 1] = string.char(table.unpack(r)) end return table.concat(rows) end local S = {} local function sprite(name, hat, m) local k = name .. (hat and "h" or "n") .. (m and "m" or "r") local s = S[k] if not s then local t, hx, hy = gen(POSES[name]) if hat then paint_hat(t, hx, hy) end s = pack(t, m) S[k] = s end return s end local PAL = {} local COATS = { { 255, 255, 255, 212, 218, 232 }, { 244, 160, 72, 214, 120, 50 }, { 168, 170, 184, 132, 134, 150 } } function on_settings() local c = COATS[(cfg.coat or 0) + 1] or COATS[1] PAL = { 0, 0, 0, 58, 62, 88, c[1], c[2], c[3], c[4], c[5], c[6], 28, 28, 44, 255, 140, 160, 226, 28, 40, 250, 240, 210, 255, 255, 255 } S = {} end local HALF = 140 local RUN_V = 1.2 local PRINTS_IN, PRINTS_OUT = {}, {} for p = 2, 22, 7 do PRINTS_IN[#PRINTS_IN + 1] = { p, (p - 14 + 22) / RUN_V } end for p = 48, 62, 7 do PRINTS_OUT[#PRINTS_OUT + 1] = { p, 70 + (p - 6 - 40) / RUN_V } end local function cat_at(h) if h < 24 then return -22 + RUN_V * h, 0, (h // 3) % 2 == 0 and "run1" or "run2" end if h < 32 then return -22 + RUN_V * 24, 0, "crouch" end if h < 62 then local s = (h - 32) / 30 return 9 + 31 * s, 22 * 4 * s * (1 - s), s < 0.22 and "up" or (s < 0.7 and "peak" or "down") end if h < 70 then return 40, 0, h < 66 and "land" or "crouch" end local x = 40 + RUN_V * (h - 70) if x > W then return nil end return x, 0, (h // 3) % 2 == 0 and "run1" or "run2" end local function draw_prints(base, mirror, t) for _, set in ipairs({ PRINTS_IN, PRINTS_OUT }) do for i = 1, #set do local a = 0.34 * min(1, max(0, 1 - (t - base - set[i][2]) / 230)) if t >= base + set[i][2] and a > 0.02 then local x = mirror and W - 2 - set[i][1] or set[i][1] blend_rect(x, GY, 2, 1, 96, 104, 126, a) end end end end local PUFF = { { -1, -0.7 }, { 1, -0.9 }, { -0.5, -1.3 }, { 0.6, -1.1 }, { -1.5, -0.3 }, { 1.4, -0.4 } } local FL = {} for i = 1, 56 do local h = hash32(i * 0x9E3779B1) FL[i] = { (h & 0x7FFF) % 640 / 10, ((h >> 8) & 0x7FFF) % 700 / 10, ((h >> 16) & 0xFF) / 40, near = i % 5 < 2 } end local function flake(x, y, a) local ix, iy = floor(x), floor(y) local fx, fy = x - ix, y - iy blend(ix, iy, 255, 255, 255, a * (1 - fx) * (1 - fy)) blend(ix + 1, iy, 255, 255, 255, a * fx * (1 - fy)) blend(ix, iy + 1, 255, 255, 255, a * (1 - fx) * fy) blend(ix + 1, iy + 1, 255, 255, 255, a * fx * fy) end local function draw_snow() local n = ({ 0, 24, 56 })[(cfg.snow or 1) + 1] for i = 1, n do local f = FL[i] local v = f.near and 0.42 or 0.2 local x = (f[1] + sin(frame * 0.021 + f[3]) * 3 + frame * 0.05) % W local y = (f[2] + v * frame) % 70 - 3 flake(x, y, f.near and 0.95 or 0.6) end end function render() if not BG then build_bg() end blit_rgb(BG, 0, 0, W, H) local half = frame // HALF local h = frame % HALF local hat = cfg.hat ~= false local base = half * HALF draw_prints(base - HALF, half % 2 == 0, frame) draw_prints(base, half % 2 == 1, frame) local mirror = half % 2 == 1 local x, lift, pose = cat_at(h) if x then if mirror then x = W - CW - x end local ix = floor(x + 0.5) local sw = 14 - lift * 0.25 blend_rect(ix + 10 - floor(sw / 2), GY + 1, floor(sw), 1, 70, 76, 96, 0.28) blend_rect(ix + 12 - floor(sw / 2), GY + 2, floor(sw) - 4, 1, 70, 76, 96, 0.2) blit_idx(sprite(pose, hat, mirror), PAL, ix, GY - 17 - floor(lift + 0.5), CW, CH, CW, CH, true) end if h >= 62 and h < 80 then local lx = 40 + 10 if mirror then lx = W - 1 - lx end local t = h - 62 for i = 1, #PUFF do local p = PUFF[i] local dir = mirror and -1 or 1 blend(floor(lx + dir * p[1] * t * 0.9 + 0.5), floor(GY - 1 + p[2] * t * 0.7 + 0.02 * t * t * 3 + 0.5), 255, 255, 255, 0.8 * (1 - t / 18)) end end draw_snow() end