info = { type = "image", title = "Squishy peek-a-boo", category = "animals", author = "BRRRO", version = "1.0", license = "MIT", desc = "A soft cat hides its eyes, peeks and says boo", api = 33, } local tr = ({ sk = { ["Body colour"] = "Farba tela", ["Hearts"] = "Srdiečka" }, cs = { ["Body colour"] = "Barva těla", ["Hearts"] = "Srdíčka" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "body", label = T("Body colour"), type = "color", default = "#84DEB4" }, { key = "hearts", label = T("Hearts"), type = "int", min = 0, max = 6, default = 5 }, } local rect, px = gfx.rect, gfx.px local floor, sin, cos, sqrt = math.floor, math.sin, math.cos, math.sqrt local hash32, umod = math.hash32, math.umod local W, H, FPS = gfx.W, gfx.H, gfx.FPS local STEP_HZ = 30 local PI = 3.14159 local function ell(cx, cy, rx, ry, c) for y = floor(cy - ry + 0.5), floor(cy + ry - 0.5) do local dy = (y + 0.5 - cy) / ry local k = 1 - dy * dy if k > 0 then local hw = rx * sqrt(k) local x0 = floor(cx - hw + 0.5) rect(x0, y, floor(cx + hw + 0.5) - x0, 1, c[1], c[2], c[3]) end end end local function rell(cx, cy, a, b, phi, c) local cs, sn = cos(phi), sin(phi) local a2, b2 = a * a, b * b local A = cs * cs / a2 + sn * sn / b2 local k1 = 2 * (cs * sn / a2 - sn * cs / b2) local k2 = sn * sn / a2 + cs * cs / b2 local hh = sqrt(a2 * sn * sn + b2 * cs * cs) for y = floor(cy - hh), floor(cy + hh) do local dy = y + 0.5 - cy local B, C = k1 * dy, k2 * dy * dy - 1 local disc = B * B - 4 * A * C if disc > 0 then local sq = sqrt(disc) local x0 = floor(cx + (-B - sq) / (2 * A) + 0.5) local x1 = floor(cx + (-B + sq) / (2 * A) + 0.5) if x1 > x0 then rect(x0, y, x1 - x0, 1, c[1], c[2], c[3]) end end end end local BG1, BG2, BG3 = { 255, 232, 214 }, { 255, 218, 208 }, { 255, 204, 204 } local BODY, OUT, PAW, BELLY = { 132, 222, 180 }, { 52, 150, 112 }, { 98, 196, 150 }, { 190, 246, 220 } local EYE, CHEEK, PINK = { 60, 40, 84 }, { 255, 150, 170 }, { 255, 118, 160 } local INNER = { 255, 176, 190 } local BG, HB = nil, nil function on_settings() local c = cfg.body or { r = 132, g = 222, b = 180 } BODY = { c.r, c.g, c.b } OUT = { c.r * 4 // 10, c.g * 68 // 100, c.b * 6 // 10 } PAW = { c.r * 75 // 100, c.g * 9 // 10, c.b * 83 // 100 } BELLY = { c.r + (255 - c.r) // 2, c.g + (255 - c.g) // 2, c.b + (255 - c.b) // 2 } BG = nil end local MOUTH = { 120, 70, 96 } local function build_bg() rect(0, 0, W, 22, BG1[1], BG1[2], BG1[3]); rect(0, 22, W, 24, BG2[1], BG2[2], BG2[3]) rect(0, 46, W, 18, BG3[1], BG3[2], BG3[3]) for x = 0, W - 1, 2 do px(x, 21, BG2[1], BG2[2], BG2[3]); px(x + 1, 45, BG3[1], BG3[2], BG3[3]) end for i = 1, 9 do local h = hash32(i * 0x9E3779B1) local x, y = umod(h, W), umod(h >> 8, 14) px(x, y, 255, 255, 255); px(x - 1, y, 255, 244, 236); px(x + 1, y, 255, 244, 236) px(x, y - 1, 255, 244, 236); px(x, y + 1, 255, 244, 236) end BG = gfx.snapshot(0, 0, W, H) rect(-1, -1, W + 2, H + 2, 255, 0, 255) local cx, cy = 32, 36 ell(cx + 1, 56, 20, 11, OUT); ell(cx + 1, 57, 19, 10, BODY) ell(cx, cy, 26, 21, OUT) ell(cx, cy, 25, 20, BODY) ell(cx, cy + 9, 17, 8, BELLY) ell(cx - 17, 45, 4.4, 2.6, CHEEK); ell(cx + 17, 45, 4.4, 2.6, CHEEK) rect(cx - 1, 41, 3, 2, MOUTH[1], MOUTH[2], MOUTH[3]) px(cx - 3, 44, MOUTH[1], MOUTH[2], MOUTH[3]); px(cx - 2, 45, MOUTH[1], MOUTH[2], MOUTH[3]); px(cx - 1, 45, MOUTH[1], MOUTH[2], MOUTH[3]); px(cx, 44, MOUTH[1], MOUTH[2], MOUTH[3]) px(cx + 1, 45, MOUTH[1], MOUTH[2], MOUTH[3]); px(cx + 2, 45, MOUTH[1], MOUTH[2], MOUTH[3]); px(cx + 3, 44, MOUTH[1], MOUTH[2], MOUTH[3]) HB = gfx.snapshot(0, 0, W, H) end local HEART_ROWS = { ".xx.xx.", "xhxxxxx", "xxxxxxx", ".xxxxx.", "..xxx..", "...x..." } local HEART = {} for i = 1, #HEART_ROWS do HEART[i] = HEART_ROWS[i]:gsub(".", { ["."] = "\0", x = "\1", h = "\2" }) end HEART = table.concat(HEART) local HPAL = { 0, 0, 0, 255, 104, 156, 255, 200, 216 } local HPAL2 = { 0, 0, 0, 196, 130, 255, 234, 208, 255 } local LP = { { 9, 62 }, { 22, 38 }, { 16, 33 }, { 7, 30 } } local RP = { { 55, 62 }, { 42, 38 }, { 42, 38 }, { 57, 30 } } local function smooth(k) if k < 0 then k = 0 elseif k > 1 then k = 1 end return k * k * (3 - 2 * k) end local function plan(u) local la, lb, lk, ra, rb, rk, mood = 1, 1, 0, 1, 1, 0, 0 if u < 55 then elseif u < 75 then la, lb, lk = 1, 2, smooth((u - 55) / 20); ra, rb, rk = 1, 2, lk elseif u < 135 then la, lb, lk = 2, 2, 0; ra, rb, rk = 2, 2, 0; mood = 1 elseif u < 145 then la, lb, lk = 2, 3, smooth((u - 135) / 10); ra, rb = 2, 2; mood = 1 elseif u < 175 then la, lb, lk = 3, 3, 0; ra, rb = 2, 2; mood = 2 elseif u < 183 then la, lb, lk = 3, 4, smooth((u - 175) / 8); ra, rb, rk = 2, 4, lk; mood = 2 elseif u < 235 then la, lb = 4, 4; ra, rb = 4, 4; mood = 3 elseif u < 260 then la, lb, lk = 4, 1, smooth((u - 235) / 25); ra, rb, rk = 4, 1, lk; mood = 3 end return la, lb, lk, ra, rb, rk, mood end local function lerp(a, b, k) return a + (b - a) * k end local function paw(x, y, base, palm) local bx, by = base[1], 66 local mx, my = lerp(x, bx, 0.45), lerp(y, by, 0.45) ell(mx, my, 4.6, 5.4, OUT); ell(x, y, 8.6, 7.2, OUT) ell(mx, my, 3.6, 4.4, PAW); ell(x, y, 7.6, 6.2, PAW) if palm then ell(x, y + 1.2, 3.2, 2.6, INNER) px(floor(x) - 4, floor(y) - 3, INNER[1], INNER[2], INNER[3]); px(floor(x) - 1, floor(y) - 4, INNER[1], INNER[2], INNER[3]) px(floor(x) + 2, floor(y) - 4, INNER[1], INNER[2], INNER[3]); px(floor(x) + 4, floor(y) - 3, INNER[1], INNER[2], INNER[3]) end end local function eye(x, y, mood, blink) if mood == 3 then rect(x - 3, y, 2, 1, EYE[1], EYE[2], EYE[3]); rect(x - 1, y - 1, 3, 1, EYE[1], EYE[2], EYE[3]) rect(x + 2, y, 2, 1, EYE[1], EYE[2], EYE[3]) elseif blink then rect(x - 3, y + 1, 7, 1, EYE[1], EYE[2], EYE[3]) else ell(x + 0.5, y + 1, 3.2, 4, EYE) px(x - 1, y - 1, 255, 255, 255); px(x, y - 1, 255, 255, 255); px(x + 1, y + 2, 230, 220, 255) end end function render() if not BG then build_bg() end gfx.blit_rgb(BG, 0, 0, W, H) local f = frame * STEP_HZ // FPS local u = f % 300 local la, lb, lk, ra, rb, rk, mood = plan(u) local lx = lerp(LP[la][1], LP[lb][1], lk) local ly = lerp(LP[la][2], LP[lb][2], lk) local rx = lerp(RP[ra][1], RP[rb][1], rk) local ry = lerp(RP[ra][2], RP[rb][2], rk) local by = (sin(f * 0.1) > 0) and 0 or 1 local tilt = (mood == 1) and floor(sin(f * 0.09) * 1.4 + 0.5) or 0 local cx, cy = 32 + tilt, 36 + by local lean = 0.55 if mood == 1 then lean = 1.0 elseif mood == 2 then lean = 0.7 elseif mood == 3 then lean = 0.3 end local tw = (f % 83) < 8 and 0.4 * sin(f * 1.3) or 0 local tw2 = ((f + 40) % 97) < 7 and 0.4 * sin(f * 1.4) or 0 local ey0 = 19 + by rell(cx - 17, ey0, 9.5, 6.2, -PI / 2 - lean - tw, OUT); rell(cx + 17, ey0, 9.5, 6.2, -PI / 2 + lean + tw2, OUT) rell(cx - 17, ey0, 8.2, 5, -PI / 2 - lean - tw, BODY); rell(cx + 17, ey0, 8.2, 5, -PI / 2 + lean + tw2, BODY) rell(cx - 17, ey0 + 1, 5, 2.6, -PI / 2 - lean - tw, INNER); rell(cx + 17, ey0 + 1, 5, 2.6, -PI / 2 + lean + tw2, INNER) gfx.blit_rgb(HB, tilt, by, W, H, 255, 0, 255) if mood == 3 then rect(cx - 4, 44, 9, 2, BELLY[1], BELLY[2], BELLY[3]) ell(cx, 47, 3.6, 3, { 110, 50, 80 }); rect(cx - 2, 48, 4, 2, 255, 130, 150) end local blink = (f % 110) >= 52 and (f % 110) < 55 if mood == 0 or mood == 3 then eye(cx - 10, 36 + by, mood, blink); eye(cx + 10, 36 + by, mood, blink) elseif mood == 2 then eye(cx - 10, 36 + by, 0, false) px(cx - 12, 33 + by, 255, 255, 255) if (f // 6) % 2 == 0 then px(cx - 16, 31, 255, 255, 255); px(cx - 16, 30, 255, 255, 255); px(cx - 17, 31, 255, 255, 255); px(cx - 15, 31, 255, 255, 255) end end paw(lx, ly, LP[1], mood == 3 and u > 187) paw(rx, ry, RP[1], mood == 3 and u > 187) local n = cfg.hearts or 5 for k = 1, n do local t0 = 183 + (k - 1) * 14 local age = (u - t0) % 300 if age < 76 then local h = hash32(k * 0x85EBCA6B) local x0 = 12 + umod(h, 40) local y = 22 - age // 2 - (k % 3) * 2 local x = x0 + floor(1.6 * sin(age * 0.13 + k) + 0.5) gfx.blit_indexed(HEART, (k % 2 == 0) and HPAL or HPAL2, x, y, 7, 6, 7, 6, true) end end end