info = { type = "image", title = "Peeking cat", category = "animals", author = "BRRRO", version = "1.0", license = "MIT", desc = "A ginger cat peeks over a brick wall", api = 33, } local tr = ({ sk = { ["Fur colour"] = "Farba srsti", ["Peek pace"] = "Tempo vykúkania", ["Ivy"] = "Brečtan", ["Butterfly"] = "Motýľ" }, cs = { ["Fur colour"] = "Barva srsti", ["Peek pace"] = "Tempo vykukování", ["Ivy"] = "Břečťan", ["Butterfly"] = "Motýl" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "fur", label = T("Fur colour"), type = "color", default = "#E8913C" }, { key = "pace", label = T("Peek pace"), type = "int", min = 1, max = 5, default = 3 }, { key = "ivy", label = T("Ivy"), type = "bool", default = true }, { key = "fly", label = T("Butterfly"), type = "bool", default = true }, } local rect, px, blend_rect = gfx.rect, gfx.px, gfx.blend_rect local floor, sin, sqrt = math.floor, math.sin, math.sqrt local hash32, umod = math.hash32, math.umod local W, H, FPS = gfx.W, gfx.H, gfx.FPS local STEP_HZ = 30 local TAU = 6.28318 local WALL = 36 local SKY_T, SKY_B = { 118, 186, 236 }, { 206, 232, 244 } local CLOUD, CLOUD_D = { 246, 250, 252 }, { 214, 230, 242 } local COP, COP_L, COP_D = { 196, 188, 178 }, { 228, 222, 212 }, { 164, 156, 148 } local MORTAR = { 214, 198, 172 } local BRICKS = { { 176, 82, 58 }, { 160, 72, 52 }, { 190, 98, 68 } } local IVY, IVY_L, VINE = { 60, 128, 58 }, { 100, 164, 70 }, { 96, 86, 54 } local WING, WING_D, BODYC = { 250, 170, 60 }, { 210, 110, 40 }, { 50, 36, 30 } local function c3(c) return c[1], c[2], c[3] end local function E(cx, cy, a, b) local S = {} for y = math.ceil(cy - b), floor(cy + b) do local d = (y - cy) / b local hw = a * sqrt(math.max(0, 1 - d * d)) S[y] = { math.ceil(cx - hw - 1e-6), floor(cx + hw + 1e-6) } end return function(x, y) local s = S[y] return s ~= nil and x >= s[1] and x <= s[2] end end local E2 = E(11.5, 11, 10.5, 7.5) local E3 = E(11.5, 13.6, 3.8, 2.4) local E4 = E(6, 18.6, 2.6, 1.7) local E5 = E(17, 18.6, 2.6, 1.7) local CW, CH = 24, 22 local function ear(x, y, ax, ay, a, b) local i = y - ay if i < 0 or y > 7 then return 0 end local x0, x1 = ax - floor(i * a), ax + floor(i * b) if x < x0 or x > x1 then return 0 end return (i >= 2 and y <= 5 and x > x0 and x < x1) and 5 or 2 end local function cat_cls(tw) return function(x, y) local inh = E2(x, y) if (x >= 6 and x <= 8) or (x >= 15 and x <= 17) then if y >= 9 and y <= 11 then if x == 7 or x == 16 then return 7 end if (x == 8 or x == 17) and y == 9 then return 8 end return 6 end end if inh then if (x == 11 or x == 12) and y == 12 then return 5 end if ((x == 11 or x == 12) and y == 13) or ((x == 10 or x == 13) and y == 14) then return 1 end if E3(x, y) then return 4 end if (x == 9 or x == 14) and y >= 5 and y <= 6 then return 3 end if (x == 11 or x == 12) and y >= 5 and y <= 7 then return 3 end if (x <= 2 or x >= 21) and (y == 10 or y == 12) then return 3 end return 2 end if E4(x, y) or E5(x, y) then if y == 20 and (x == 5 or x == 7 or x == 16 or x == 18) then return 1 end return 4 end local e if tw == 1 then e = ear(x, y, 3, 2, 0.1, 1.1) else e = ear(x, y, 5, 0, 0.45, 0.8) end if e > 0 then return e end if tw == 2 then e = ear(x, y, 20, 2, 1.1, 0.1) else e = ear(x, y, 18, 0, 0.8, 0.45) end return e end end local function grid(cls, y0, y1) local C = {} for y = y0 - 1, y1 do for x = -1, CW do C[(y - y0 + 1) * 64 + x + 1] = cls(x, y) end end local rows, row = {}, {} for y = y0, y1 - 1 do for x = 0, CW - 1 do local k = (y - y0 + 1) * 64 + x + 1 local c = C[k] if c == 0 and (C[k - 1] > 0 or C[k + 1] > 0 or C[k - 64] > 0 or C[k + 64] > 0) then c = 1 end row[x + 1] = c end rows[#rows + 1] = string.char(table.unpack(row, 1, CW)) end return table.concat(rows) end local CAT = {} local function cat_tw(tw) if not CAT[tw] then CAT[tw] = grid(cat_cls(tw - 1), 0, 9) .. CAT[1]:sub(9 * CW + 1) end return CAT[tw] end local RUNS = {} local HALF = nil local function build_cat() if not HALF then HALF = grid(cat_cls(0), 0, 11); return end CAT[1] = HALF .. grid(cat_cls(0), 11, CH) HALF = nil local g = CAT[1] for y = 0, CH - 1 do local x = 0 while x < CW do local c = g:byte(y * CW + x + 1) local x1 = x while x1 + 1 < CW and g:byte(y * CW + x1 + 2) == c do x1 = x1 + 1 end if c > 0 then RUNS[#RUNS + 1] = { x, y, x1 - x + 1, c } end x = x1 + 1 end end end local PAL, COL = {}, {} local BG = nil function on_settings() local f = cfg.fur or { r = 232, g = 145, b = 60 } local fur = { f.r, f.g, f.b } local dk = { floor(f.r * 0.72), floor(f.g * 0.62), floor(f.b * 0.58) } local lt = { floor(f.r + (255 - f.r) * 0.55), floor(f.g + (246 - f.g) * 0.55), floor(f.b + (232 - f.b) * 0.55) } local out = { floor(f.r * 0.34), floor(f.g * 0.28), floor(f.b * 0.26) } COL = { out, fur, dk, lt, { 240, 150, 160 }, { 196, 210, 90 }, { 28, 24, 20 }, { 255, 255, 255 } } PAL = { 0, 0, 0 } for i = 1, #COL do PAL[#PAL + 1] = COL[i][1]; PAL[#PAL + 1] = COL[i][2]; PAL[#PAL + 1] = COL[i][3] end BG = nil end local function cloud(x, y, w) rect(x + 3, y, w - 6, 1, c3(CLOUD)) rect(x + 1, y + 1, w - 2, 1, c3(CLOUD)) rect(x, y + 2, w, 2, c3(CLOUD)) rect(x + 1, y + 4, w - 2, 1, c3(CLOUD_D)) end local function build_bg() for y = 0, WALL - 1 do local t = y / (WALL - 1) rect(0, y, W, 1, floor(SKY_T[1] + (SKY_B[1] - SKY_T[1]) * t + 0.5), floor(SKY_T[2] + (SKY_B[2] - SKY_T[2]) * t + 0.5), floor(SKY_T[3] + (SKY_B[3] - SKY_T[3]) * t + 0.5)) end cloud(4, 5, 16); cloud(44, 11, 14) rect(0, WALL, W, 2, c3(COP)) rect(0, WALL, W, 1, c3(COP_L)) for x = 7, W, 16 do px(x, WALL + 1, c3(COP_D)) end rect(0, WALL + 2, W, H - WALL - 2, c3(MORTAR)) local k = 0 for y = WALL + 2, H - 1, 5 do local off = (k % 2) * 5 - 5 for x = off, W - 1, 10 do local h = hash32(k * 0x9E3779B1 + x * 0x85EBCA6B) rect(x, y, 9, 4, c3(BRICKS[1 + umod(h, 3)])) end k = k + 1 end if cfg.ivy ~= false then local x = 6 for y = H - 1, WALL - 2, -1 do x = x + ((y % 7 == 0) and 1 or ((y % 11 == 0) and -1 or 0)) px(x, y, c3(VINE)) if y % 3 == 0 then local s = (y % 6 == 0) and -2 or 1 rect(x + s, y - 1, 2, 2, c3(IVY)) px(x + s + (s < 0 and 0 or 1), y - 1, c3(IVY_L)) end end rect(8, WALL - 3, 2, 2, c3(IVY)); rect(11, WALL - 2, 2, 2, c3(IVY_L)); rect(3, WALL - 2, 2, 2, c3(IVY)) rect(12, WALL + 4, 2, 2, c3(IVY)); rect(1, WALL + 9, 2, 2, c3(IVY_L)) end BG = gfx.snapshot(0, 0, W, H) end local function soft(x, y, w, h, c) local ix, iy = floor(x), floor(y) local fx, fy = x - ix, y - iy blend_rect(ix, iy, w, h, c[1], c[2], c[3], (1 - fx) * (1 - fy)) blend_rect(ix + 1, iy, w, h, c[1], c[2], c[3], fx * (1 - fy)) blend_rect(ix, iy + 1, w, h, c[1], c[2], c[3], (1 - fx) * fy) blend_rect(ix + 1, iy + 1, w, h, c[1], c[2], c[3], fx * fy) end local function fly_pos(t) return 32 + 22 * sin(t * 0.013) + 4 * sin(t * 0.07), 14 + 7 * sin(t * 0.021 + 1) + 2 * sin(t * 0.11) end local function butterfly(t) local x, y = fly_pos(t) if (t // 3) % 2 == 0 then soft(x - 2, y - 1, 2, 2, WING); soft(x + 1, y - 1, 2, 2, WING) soft(x - 2, y + 1, 1, 1, WING_D); soft(x + 2, y + 1, 1, 1, WING_D) else soft(x - 1, y - 1, 1, 2, WING); soft(x + 1, y - 1, 1, 2, WING) end soft(x, y - 1, 1, 3, BODYC) end local function tail(t, bx, show, dir) local w = TAU / (2.2 * STEP_HZ) local x, y, a = bx, WALL + 2, 0.15 local PX, PY = {}, {} local n = floor(9 * show) for j = 1, n do a = a + 0.05 * j / 3 + 0.12 * sin(w * t - j * 0.45) x = x + dir * sin(a) * 1.6 y = y - math.cos(a) * 1.6 PX[j], PY[j] = floor(x + 0.5), floor(y + 0.5) end local out, fur = COL[1], COL[2] for j = 1, n do rect(PX[j] - 1, PY[j] - 1, 4, 4, c3(out)) end for j = 1, n do rect(PX[j], PY[j], 2, 2, c3(j >= n - 1 and COL[3] or fur)) end end local HID, PEEK, UP = 37, 24, 16 local function smooth(a, b, k) k = k * k * (3 - 2 * k); return a + (b - a) * k end function render() if not BG then build_bg(); return end gfx.blit_rgb(BG, 0, 0, W, H) if not CAT[1] then build_cat(); return end local t = frame * STEP_HZ // FPS local pace = cfg.pace or 3 local upl = floor((8 - pace) * 0.8 * STEP_HZ) local hid = floor((6 - pace) * 0.6 * STEP_HZ) + 20 local K = { 0, HID, 22, PEEK, 52, PEEK, 64, UP, 64 + upl, UP, 76 + upl, HID, 76 + upl + hid, HID } local cyc = K[#K - 1] local tt = t + 40 local n = tt // cyc local u = tt % cyc local oy = HID for i = 1, #K - 3, 2 do if u >= K[i] and u < K[i + 2] then oy = smooth(K[i + 1], K[i + 3], (u - K[i]) / (K[i + 2] - K[i])) break end end local left = 12 + umod(hash32(n * 0x9E3779B1 + 7), 27) local fly = cfg.fly ~= false local bx, by = fly_pos(t) if oy < HID - 0.01 then gfx.clip(0, 0, W, WALL) local show = math.max(0, math.min(1, (HID - oy) / (HID - UP))) if show > 0.6 and left > 25 then tail(t, left - 5, (show - 0.6) / 0.4, -1) elseif show > 0.6 then tail(t, left + 27, (show - 0.6) / 0.4, 1) end local iy = floor(oy) local f = oy - iy if f > 0.03 then gfx.blit_indexed(CAT[1], PAL, left, iy + 1, CW, CH, CW, CH, true) local a = 1 - f for i = 1, #RUNS do local r = RUNS[i] local c = COL[r[4]] blend_rect(left + r[1], iy + r[2], r[3], 1, c[1], c[2], c[3], a) end else local still = u >= K[3] and u < K[5] or u >= K[7] and u < K[9] local tw = 1 if still then local e = (u + n * 37) % 70 if e < 4 or (e >= 7 and e < 10) then tw = 2 + umod(n + u // 70, 2) end end gfx.blit_indexed(cat_tw(tw), PAL, left, iy, CW, CH, CW, CH, true) local look if fly then look = (bx - (left + 11.5)) / 9 else look = ((u // 40) % 4 == 1) and -1 or (((u // 40) % 4 == 3) and 1 or 0) end local dx = look < -0.5 and -1 or (look > 0.5 and 1 or 0) if (u + n * 13) % 95 < 3 then rect(left + 6, iy + 9, 3, 3, c3(COL[2])); rect(left + 15, iy + 9, 3, 3, c3(COL[2])) rect(left + 6, iy + 10, 3, 1, c3(COL[1])); rect(left + 15, iy + 10, 3, 1, c3(COL[1])) elseif dx ~= 0 then for _, ex in ipairs({ left + 6, left + 15 }) do rect(ex, iy + 9, 3, 3, c3(COL[6])) rect(ex + 1 + dx, iy + 9, 1, 3, c3(COL[7])) px(ex + 1 - dx, iy + 9, c3(COL[8])) end end end gfx.clip() if oy <= UP + 0.01 then local lt, out = COL[4], COL[1] for _, x in ipairs({ left + 4, left + 15 }) do rect(x - 1, WALL, 7, 2, c3(out)) rect(x, WALL, 5, 1, c3(lt)) px(x + 1, WALL, c3(out)); px(x + 3, WALL, c3(out)) end end end if fly then butterfly(t) end end