info = { type = "image", title = "Rainy window cat", category = "animals", author = "BRRRO", version = "1.0", license = "MIT", desc = "A sleepy cat on a sill while rain falls", api = 44, } local tr = ({ sk = { ["Rain"] = "Dážď", ["Tail sway"] = "Kývanie chvosta", ["Fur"] = "Srsť", ["Violet"] = "Fialová", ["Plum"] = "Slivková", ["Indigo"] = "Indigová" }, cs = { ["Rain"] = "Déšť", ["Tail sway"] = "Houpání ocasu", ["Fur"] = "Srst", ["Violet"] = "Fialová", ["Plum"] = "Švestková", ["Indigo"] = "Indigová" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "rain", label = T("Rain"), type = "int", min = 0, max = 10, default = 6 }, { key = "tail", label = T("Tail sway"), type = "int", min = 0, max = 5, default = 3 }, { key = "fur", label = T("Fur"), type = "int", default = "violet", opts = { keys = { "violet", "plum", "indigo" }, T("Violet"), T("Plum"), T("Indigo") } }, } local rect, px, blend, blend_rect = gfx.rect, gfx.px, gfx.blend, gfx.blend_rect local blit_indexed, blit_rgb = gfx.blit_indexed, gfx.blit_rgb local floor, sin, cos, sqrt, abs, max, min = math.floor, math.sin, math.cos, math.sqrt, math.abs, math.max, math.min local hash32, umod = math.hash32, math.umod local char, unpack, concat = string.char, table.unpack, table.concat local W, H, FPS = gfx.W, gfx.H, gfx.FPS local PI, TAU = 3.14159265, 6.28318 local FURS = { violet = { { 0, 26, 14, 52 }, { 0.35, 70, 38, 118 }, { 0.65, 128, 84, 184 }, { 0.9, 186, 146, 230 }, { 1, 214, 184, 250 } }, plum = { { 0, 34, 12, 44 }, { 0.35, 92, 34, 98 }, { 0.65, 152, 74, 150 }, { 0.9, 208, 132, 194 }, { 1, 236, 172, 220 } }, indigo = { { 0, 14, 16, 56 }, { 0.35, 36, 44, 124 }, { 0.65, 80, 96, 184 }, { 0.9, 140, 160, 232 }, { 1, 184, 204, 252 } }, } local PAL local function ramp(keys, t) for i = 1, #keys - 1 do local a, b = keys[i], keys[i + 1] if t <= b[1] then local u = (t - a[1]) / (b[1] - a[1]) return a[2] + (b[2] - a[2]) * u, a[3] + (b[3] - a[3]) * u, a[4] + (b[4] - a[4]) * u end end local l = keys[#keys] return l[2], l[3], l[4] end local function add(r, g, b) PAL[#PAL + 1], PAL[#PAL + 2], PAL[#PAL + 3] = floor(r + 0.5), floor(g + 0.5), floor(b + 0.5) end local function make_pal() local keys = FURS[cfg.fur] or FURS.violet PAL = { 0, 0, 0 } for k = 0, 23 do add(ramp(keys, k / 23)) end for k = 0, 7 do add(96 + 84 * k / 7, 92 + 112 * k / 7, 176 + 79 * k / 7) end for k = 0, 11 do add(118 + 137 * k / 11, 138 + 117 * k / 11, 200 + 55 * k / 11) end for _, c in ipairs({ { 44, 60, 128 }, { 244, 150, 170 }, { 30, 16, 40 }, { 196, 90, 120 }, { 84, 104, 172 }, { 250, 130, 150 } }) do add(c[1], c[2], c[3]) end end local LX, LY, LZ = -0.5, -0.45, 0.74 local function shade(nx, ny, nz, x, y) local d = (nx * LX + ny * LY + nz * LZ + 0.25) / 1.25 if d < 0 then d = 0 end local t = 0.16 + 0.64 * d if ny > 0.4 then t = t + 0.14 * (ny - 0.4) end t = t + ((hash32(x * 73 + y * 151) & 7) - 3) * 0.012 if ny < -0.55 and nz < 0.62 then return 25 + floor(min(1, (-ny - 0.55) / 0.35) * 7 + 0.5) end if t > 1 then t = 1 elseif t < 0 then t = 0 end return 1 + floor(t * 23 + 0.5) end local function grid(w, h) local g = {} for i = 1, w * h do g[i] = 0 end return g end local function ell(g, w, h, cx, cy, rx, ry, ang, cap, occ, ymax) local c, s = cos(ang), sin(ang) local ex, ey = sqrt((rx * c) ^ 2 + (ry * s) ^ 2), sqrt((rx * s) ^ 2 + (ry * c) ^ 2) for y = max(0, floor(cy - ey)), min(ymax or h - 1, h - 1, floor(cy + ey) + 1) do for x = max(0, floor(cx - ex)), min(w - 1, floor(cx + ex) + 1) do local dx, dy = x + 0.5 - cx, y + 0.5 - cy local u, v = (dx * c + dy * s) / rx, (dy * c - dx * s) / ry local r2 = u * u + v * v if r2 <= 1 then local nz = sqrt(1 - r2) local nx, ny = u * c - v * s, u * s + v * c if cap then local d = max(0, (nx * LX + ny * LY + nz * LZ + 0.2) / 1.2) g[y * w + x + 1] = 33 + floor(min(1, 0.12 + 0.9 * d) * 11 + 0.5) else local k = shade(nx, ny, nz, x, y) if occ and k <= 24 then k = max(1, k - occ(x, y)) end g[y * w + x + 1] = k end end end end end local function flat_ell(g, w, cx, cy, rx, ry, idx) for y = floor(cy - ry), floor(cy + ry) + 1 do for x = floor(cx - rx), floor(cx + rx) + 1 do local u, v = (x + 0.5 - cx) / rx, (y + 0.5 - cy) / ry if u * u + v * v <= 1 and y >= 0 then g[y * w + x + 1] = idx end end end end local function rows_of(g, w, h) local rows = {} for y = 0, h - 1 do rows[y + 1] = char(unpack(g, y * w + 1, y * w + w)) end return concat(rows) end local BODY = {} local function bake_body(q) local w, h = 34, 18 local g = grid(w, h) ell(g, w, h, 16, 10.3 - q * 0.25, 15.2, 8.3, 0, false, function(x, y) return max(0, floor((y - 12) * 1.6)) end, 16) BODY[q] = rows_of(g, w, h) end local HEAD local function bake_head() local w, h = 20, 17 local g = grid(w, h) ell(g, w, h, 4.4, 4, 2.2, 3.3, -0.45) ell(g, w, h, 15.6, 4, 2.2, 3.3, 0.45) flat_ell(g, w, 4.7, 4.6, 0.9, 1.7, 46) flat_ell(g, w, 15.3, 4.6, 0.9, 1.7, 46) ell(g, w, h, 10, 10, 8, 6.5, 0, false, function(x, y) return max(0, floor((y - 12) * 1.4)) end) ell(g, w, h, 10, 6.2, 6.6, 4.4, 0, true, nil, 7) for y = 6, 7 do for x = 0, w - 1 do local v = g[y * w + x + 1] if v >= 33 and v <= 44 then g[y * w + x + 1] = 45 end end end flat_ell(g, w, 10, 8.4, 7.2, 1.2, 49) for _, ex in ipairs({ 6, 12 }) do g[9 * w + ex + 1], g[10 * w + ex + 2], g[9 * w + ex + 3] = 47, 47, 47 end g[12 * w + 10] = 50; g[12 * w + 11] = 50 g[13 * w + 10] = 47; g[13 * w + 11] = 47 HEAD = rows_of(g, w, h) end local PAWS local function bake_paws() local w, h = 18, 4 local g = grid(w, h) ell(g, w, h, 5.5, 2.8, 3, 1.8, 0, false, nil, 3) ell(g, w, h, 13.5, 2.8, 3, 1.8, 0, false, nil, 3) PAWS = rows_of(g, w, h) end local TAILS = {} local TL = 16 local function tail_pose(k) local s = TAILS[k] if s then return s end local th = (k - 7) * 2 * PI / 180 local ct, st = cos(th), sin(th) local pts = {} for i = 0, 16 do local sl = i * TL / 16 local lx = 2.6 * (sl / TL) ^ 2 pts[i + 1] = { 9 + lx * ct + sl * st, 1.5 - lx * st + sl * ct, 3.5 - 1.5 * sl / TL } end local w, h = 18, 22 local g = grid(w, h) for y = 0, h - 1 do for x = 0, w - 1 do local bx, by = x + 0.5, y + 0.5 local bd, bi = 1e9, 1 for i = 1, 17 do local p = pts[i] local d = (bx - p[1]) ^ 2 + (by - p[2]) ^ 2 if d < bd then bd, bi = d, i end end local p = pts[bi] local r2 = bd / (p[3] * p[3]) if r2 <= 1 then local nz = sqrt(1 - r2) local nx, ny = (bx - p[1]) / p[3], (by - p[2]) / p[3] local k = shade(nx, ny, nz, x, y) if bi <= 2 and k <= 24 then k = max(1, k - 3) end g[y * w + x + 1] = k end end end s = rows_of(g, w, h) TAILS[k] = s return s end local OX0, OY0, OX1, OY1 = 7, 5, 56, 43 local function disc(cx, cy, r, c, a) for y = -r, r do local hw = floor(sqrt(r * r + 0.5 - y * y)) blend_rect(cx - hw, cy + y, 2 * hw + 1, 1, c[1], c[2], c[3], a) end end local BG local function build_bg() rect(0, 0, W, H, 62, 46, 58) for y = 52, H - 1 do rect(0, y, W, 1, 62 - (y - 52) // 4, 46 - (y - 52) // 5, 58 - (y - 52) // 5) end for y = OY0, OY1 do local t = (y - OY0) / (OY1 - OY0) rect(OX0, y, OX1 - OX0 + 1, 1, floor(26 + 36 * t), floor(34 + 44 * t), floor(58 + 40 * t)) end for _, b in ipairs({ { 8, 30, 9 }, { 17, 34, 8 }, { 24, 28, 10 }, { 35, 32, 7 }, { 42, 29, 9 }, { 50, 33, 7 } }) do rect(b[1], b[2], b[3], OY1 - b[2] + 1, 28, 34, 56) px(b[1] + 2, b[2] + 3, 150, 120, 70); px(b[1] + 5, b[2] + 6, 120, 100, 64) end local AMB, CYA = { 255, 190, 100 }, { 110, 200, 215 } for _, l in ipairs({ { 14, 36, AMB }, { 26, 38, CYA }, { 40, 35, AMB }, { 51, 37, AMB }, { 33, 28, CYA } }) do disc(l[1], l[2], 4, l[3], 0.16) disc(l[1], l[2], 2, l[3], 0.32) end rect(4, 2, 56, 3, 98, 68, 50); rect(4, 2, 56, 1, 132, 94, 66) rect(4, 2, 3, 42, 98, 68, 50); rect(4, 2, 1, 42, 132, 94, 66) rect(57, 2, 3, 42, 82, 56, 44) rect(30, 3, 3, 41, 90, 62, 48); rect(30, 3, 1, 41, 120, 86, 62) rect(7, 43, 50, 1, 82, 56, 44) rect(2, 44, 60, 5, 150, 108, 76) for x = 2, 31 do blend_rect(x, 44, 1, 5, 255, 190, 120, 0.12 * (1 - (x - 2) / 30)) end rect(2, 44, 60, 1, 184, 140, 100) rect(2, 49, 60, 3, 100, 70, 52) rect(2, 49, 60, 1, 124, 88, 64) rect(2, 52, 60, 2, 40, 28, 34) blend_rect(0, 54, W, 3, 30, 20, 28, 0.3) rect(9, 40, 7, 5, 172, 92, 62); rect(8, 39, 9, 2, 196, 112, 76); rect(9, 40, 1, 5, 208, 132, 96) rect(11, 33, 3, 6, 54, 132, 82); rect(9, 35, 2, 4, 66, 154, 94); rect(14, 34, 3, 5, 44, 112, 72) blend_rect(29, 47, 32, 1, 20, 8, 30, 0.45) blend_rect(31, 48, 29, 1, 20, 8, 30, 0.25) BG = gfx.snapshot(0, 0, W, H) end local BAKE = { build_bg, make_pal } for q = 0, 3 do BAKE[#BAKE + 1] = function() bake_body(q) end end BAKE[#BAKE + 1] = bake_head BAKE[#BAKE + 1] = bake_paws local STAGE = 1 function on_settings() PAL = nil; BODY = {}; HEAD = nil; PAWS = nil; TAILS = {}; STAGE = BG and 2 or 1 end local DR = {} for i = 1, 26 do local h = hash32(i * 0x9E3779B1) DR[i] = { x = umod(h, OX1 - OX0 + 8), y = umod(h >> 8, 60), near = i > 18, v = umod(h >> 16, 12) } end local GLASS = {} for i = 1, 5 do local h = hash32(i * 0x85EBCA6B) GLASS[i] = { x = OX0 + 3 + umod(h, 44), y = umod(h >> 8, 36), v = 3 + umod(h >> 16, 5) } end local function tail_angle(t) local u = t % 10 local env = u < 1.2 and (sin(u / 1.2 * PI / 2)) ^ 2 or 2.718 ^ (-(u - 1.2) / 3.0) return 2 + (cfg.tail or 3) * 2.6 * env * sin(t * TAU / 3.4) end function render() if STAGE <= #BAKE then BAKE[STAGE](); STAGE = STAGE + 1 if STAGE <= #BAKE then gfx.ready(false) end end if not BG then return end blit_rgb(BG, 0, 0, W, H) local t = frame gfx.clip(OX0, OY0, OX1 - OX0 + 1, OY1 - OY0 + 1) for i = 1, (cfg.rain or 6) * 26 // 10 do local d = DR[i] local sp = d.near and 3 or 2 local y = floor((d.y + t * (sp + d.v * 0.05)) % 62) - 8 local x = OX0 + d.x - floor(y / 5) - 4 if d.near then blend_rect(x, y, 1, 4, 196, 214, 240, 0.55) blend_rect(x, y - 2, 1, 2, 196, 214, 240, 0.25) else blend_rect(x, y, 1, 3, 150, 170, 205, 0.4) end end for i = 1, #GLASS do local g = GLASS[i] local y = OY0 + (g.y + t * g.v // FPS) % 36 blend(g.x, y, 226, 236, 250, 0.7) blend(g.x, y + 1, 90, 104, 140, 0.5) end gfx.clip() if not (PAL and PAWS) then return end local ts = t / FPS local k = floor(max(-7, min(7, tail_angle(ts) / 2)) + 0.5) + 7 blit_indexed(tail_pose(k), PAL, 48, 41, 18, 22, 18, 22, true) local q = min(3, floor((0.5 + 0.5 * sin(ts * TAU / 3.2)) * 0.8 * 4 + 0.5)) blit_indexed(BODY[q], PAL, 28, 30, 34, 18, 34, 18, true) blit_indexed(HEAD, PAL, 20, 31, 20, 17, 20, 17, true) blit_indexed(PAWS, PAL, 21, 43, 18, 4, 18, 4, true) end