info = { type = "image", title = "Cat on the wall", category = "characters", author = "BRRRO", version = "1.0", license = "MIT", desc = "A black cat waves, blinks and watches a firefly", api = 33, } local tr = ({ sk = { ["Scarf colour"] = "Farba šálu", ["Waving paw"] = "Mávajúca labka", ["off"] = "vypnuté", ["rare"] = "zriedka", ["often"] = "často", ["Firefly"] = "Svetluška", ["Wind"] = "Vietor" }, cs = { ["Scarf colour"] = "Barva šály", ["Waving paw"] = "Mávající tlapka", ["off"] = "vypnuto", ["rare"] = "zřídka", ["often"] = "často", ["Firefly"] = "Světluška", ["Wind"] = "Vítr" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "scarf", label = T("Scarf colour"), type = "color", default = "#D2323E" }, { key = "wave", label = T("Waving paw"), type = "int", default = 2, opts = { T("off"), T("rare"), T("often") } }, { key = "fly", label = T("Firefly"), type = "bool", default = true }, { key = "wind", label = T("Wind"), type = "int", min = 1, max = 5, default = 3 }, } local rect, px, blend, brect = gfx.rect, gfx.px, gfx.blend, gfx.blend_rect local floor, sin, cos, sqrt, abs = math.floor, math.sin, math.cos, math.sqrt, math.abs local W, H, FPS = gfx.W, gfx.H, gfx.FPS local STEP_HZ = 30 local TAU = 6.28318 local CAT = { 18, 14, 26 } local EAR_IN, NOSE = { 176, 84, 120 }, { 240, 120, 150 } local AMBER, PUPIL = { 255, 190, 40 }, { 20, 10, 10 } local WHISK = { 255, 238, 224 } local PAD = { 236, 124, 156 } local function c3(c) return c[1], c[2], c[3] end local function lerp(a, b, t) return a + (b - a) * t end local function span(xa, xb, y, r, g, b) if xb <= xa then return end local ia, ib = floor(xa), floor(xb) if ia == ib then blend(ia, y, r, g, b, xb - xa) return end local s0 = ia if xa > ia then blend(ia, y, r, g, b, ia + 1 - xa); s0 = ia + 1 end if ib > s0 then rect(s0, y, ib - s0, 1, r, g, b) end if xb > ib then blend(ib, y, r, g, b, xb - ib) end end local function vspan(x, ya, yb, r, g, b) if yb <= ya then return end local ia, ib = floor(ya), floor(yb) if ia == ib then blend(x, ia, r, g, b, yb - ya) return end local s0 = ia if ya > ia then blend(x, ia, r, g, b, ia + 1 - ya); s0 = ia + 1 end if ib > s0 then rect(x, s0, 1, ib - s0, r, g, b) end if yb > ib then blend(x, ib, r, g, b, yb - ib) end end local function line(cx, cy, dx, dy, len, r, g, b) if abs(dy) >= abs(dx) then local n = floor(abs(dy) * len + 0.5) local st = dy > 0 and 1 or -1 local sx = dx / dy for k = 0, n - 1 do local Y = cy + st * (k + 0.5) local X = cx + sx * (Y - cy) - 0.5 local ix = floor(X) local f = X - ix local yi = floor(Y) if f < 0.97 then blend(ix, yi, r, g, b, 1 - f) end if f > 0.03 then blend(ix + 1, yi, r, g, b, f) end end else local n = floor(abs(dx) * len + 0.5) local st = dx > 0 and 1 or -1 local sy = dy / dx for k = 0, n - 1 do local X = cx + st * (k + 0.5) local Y = cy + sy * (X - cx) - 0.5 local iy = floor(Y) local f = Y - iy local xi = floor(X) if f < 0.97 then blend(xi, iy, r, g, b, 1 - f) end if f > 0.03 then blend(xi, iy + 1, r, g, b, f) end end end end local function dotf(X, Y, r, g, b, a) local fx, fy = X - 0.5, Y - 0.5 local ix, iy = floor(fx), floor(fy) fx, fy = fx - ix, fy - iy local w = (1 - fx) * (1 - fy) * a; if w > 0.04 then blend(ix, iy, r, g, b, w) end w = fx * (1 - fy) * a; if w > 0.04 then blend(ix + 1, iy, r, g, b, w) end w = (1 - fx) * fy * a; if w > 0.04 then blend(ix, iy + 1, r, g, b, w) end w = fx * fy * a; if w > 0.04 then blend(ix + 1, iy + 1, r, g, b, w) end end local SC, SCL, SCD = { 210, 50, 62 }, { 240, 104, 108 }, { 150, 28, 44 } local WAVE_P, FLY_ON, WINDF = 240, true, 0.6 local BG = nil function on_settings() local c = cfg.scarf or { r = 210, g = 50, b = 62 } SC = { c.r, c.g, c.b } SCL = { floor(c.r + (255 - c.r) * 0.35), floor(c.g + (255 - c.g) * 0.35), floor(c.b + (255 - c.b) * 0.35) } SCD = { floor(c.r * 0.68), floor(c.g * 0.55), floor(c.b * 0.7) } local k = cfg.wave or 2 WAVE_P = k == 0 and 0 or (k == 1 and 480 or 240) FLY_ON = cfg.fly ~= false WINDF = (cfg.wind or 3) / 5 BG = nil end local BODY = { 8.5, 9.8, 10.8, 11.6, 12.2, 12.6, 13, 13.3, 13.5, 13.6, 13.6, 13.5, 13.4, 13.2, 13, 12.8, 12.6, 12.4, 12.3, 12.2 } local function sky(t) if t < 0.55 then local u = t / 0.55 return floor(lerp(64, 170, u) + 0.5), floor(lerp(56, 90, u) + 0.5), floor(lerp(140, 150, u) + 0.5) end local u = (t - 0.55) / 0.45 return floor(lerp(170, 255, u) + 0.5), floor(lerp(90, 168, u) + 0.5), floor(lerp(150, 120, u) + 0.5) end local STX = { 6, 14, 48, 58, 9, 52, 3, 60 } local STY = { 4, 9, 3, 16, 17, 22, 12, 6 } local function build_bg() for y = 0, 45 do rect(0, y, W, 1, sky(y / 45)) end for y = 5, 15 do for x = 49, 59 do local d1 = sqrt((x + 0.5 - 54) ^ 2 + (y + 0.5 - 10) ^ 2) local d2 = sqrt((x + 0.5 - 55.8) ^ 2 + (y + 0.5 - 9) ^ 2) if d1 <= 4.3 and d2 > 3.8 then px(x, y, 252, 240, 196) end end end rect(0, 46, W, 18, 126, 80, 78) for r = 0, 4 do local y = 46 + r * 4 rect(0, y + 3, W, 1, 100, 60, 62) for x = (r % 2) * 4, W - 1, 8 do rect(x, y, 1, 3, 100, 60, 62) end end rect(0, 46, W, 1, 214, 140, 112) brect(14, 46, 36, 2, 0, 0, 0, 0.3) for y = 11, 26 do local dy = y + 0.5 - 19 local q = 1 - (dy / 8) * (dy / 8) local hw = 10 * sqrt(q > 0 and q or 0) span(32 - hw, 32 + hw, y, c3(CAT)) px(floor(32 + hw), y, 120, 60, 88) end for y = 26, 45 do local hw = BODY[y - 25] span(32 - hw, 32 + hw, y, c3(CAT)) px(floor(32 + hw), y, 120, 60, 88) end for y = 3, 11 do local w = 0.8 + (y - 3) * 0.55 local cl = 22.5 + (y - 3) * 0.55 span(cl - w, cl + w, y, c3(CAT)) span(64 - cl - w, 64 - cl + w, y, c3(CAT)) if y >= 6 and y <= 10 then span(cl - w + 1.3, cl + w - 0.6, y, c3(EAR_IN)) span(64 - cl - w + 0.6, 64 - cl + w - 1.3, y, c3(EAR_IN)) end end rect(31, 22, 2, 1, c3(NOSE)) px(31, 23, 90, 70, 104); px(32, 23, 90, 70, 104) for k = -1, 1 do local y = 22 + k * 2 px(22, y, c3(WHISK)); px(21, y, c3(WHISK)); px(20, y + k, c3(WHISK)); px(19, y + k, c3(WHISK)) px(41, y, c3(WHISK)); px(42, y, c3(WHISK)); px(43, y + k, c3(WHISK)); px(44, y + k, c3(WHISK)) end for x = 23, 40 do local u = ((x + 0.5 - 32) / 9) u = 1 - u * u local top = 26 local h = 3 + floor(2 * u + 0.5) rect(x, top, 1, h, c3(SC)) px(x, top, c3(SCL)) px(x, top + h - 1, c3(SCD)) if x % 4 == 1 then px(x, top + 1 + (h > 4 and 1 or 0), 255, 238, 238) end end rect(37, 28, 4, 4, c3(SCD)); rect(38, 28, 3, 1, c3(SC)); px(38, 29, c3(SCL)) BG = gfx.snapshot(0, 0, W, H) end local function draw_tail(f) local x, y = 19, 44 local wag = 0.12 + 0.3 * WINDF for j = 1, 15 do local u = j / 15 local phi = -2.9 + 1.5 * u + wag * sin(f * TAU / 70 - 0.5 * j) * u x = x + cos(phi) * 1.5 y = y + sin(phi) * 1.5 local yi = floor(y - 1.7) for yy = yi, yi + 3 do local d = yy + 0.5 - y local q = 2.89 - d * d if q > 0 then local hw = sqrt(q) span(x - hw, x + hw, yy, c3(CAT)) end end end end local function draw_scarf_tails(f) local a = 0.7 + 2.4 * WINDF for x = 41, 54 do local u = (x - 41) / 13 local yc = 30 + 0.3 * (x - 41) + a * u * sin(f * TAU / 50 - 0.55 * (x - 41)) vspan(x, yc - 1.6, yc + 1.6, c3(SC)) if x % 4 == 1 then px(x, floor(yc + 0.5), 255, 238, 238) end end for x = 41, 50 do local u = (x - 41) / 9 local yc = 32.5 + 0.55 * (x - 41) + a * 0.8 * u * sin(f * TAU / 50 - 0.55 * (x - 41) + 1.2) vspan(x, yc - 1.4, yc + 1.4, c3(SCD)) end end local function draw_eyes(f, happy, dirc) local shut = f % 150 < 3 for e = 0, 1 do local ex = e == 0 and 26 or 35 if happy then px(ex + 1, 17, c3(AMBER)); px(ex + 2, 17, c3(AMBER)); px(ex, 18, c3(AMBER)); px(ex + 3, 18, c3(AMBER)) elseif shut then rect(ex, 18, 4, 1, 90, 70, 104) else rect(ex + 1, 16, 2, 1, c3(AMBER)); rect(ex, 17, 4, 3, c3(AMBER)); rect(ex + 1, 20, 2, 1, c3(AMBER)) rect(ex + dirc, 17, 2, 3, c3(PUPIL)) px(ex + 3, 16, 255, 250, 220) end end end local SHX, SHY, ARML = 42, 31, 11 local function smooth(u) if u <= 0 then return 0 elseif u >= 1 then return 1 end return u * u * (3 - 2 * u) end local function arm_angle(p) if p < 0 or p >= 104 then return nil end if p < 20 then return lerp(0.15, 2.4, smooth(p / 20)) end if p < 80 then return 2.4 + 0.38 * sin(TAU * (p - 20) / 15) end return lerp(2.4, 0.15, smooth((p - 80) / 24)) end local function draw_arm(th) local dx, dy = sin(th), cos(th) local nx, ny = -dy, dx for o = -1.8, 1.8, 0.9 do line(SHX + nx * o, SHY + ny * o, dx, dy, ARML, c3(CAT)) end local tx, ty = SHX + dx * ARML, SHY + dy * ARML for yy = floor(ty - 2.7), floor(ty + 2.7) do local d = yy + 0.5 - ty local q = 7.3 - d * d if q > 0 then local hw = sqrt(q) span(tx - hw, tx + hw, yy, c3(CAT)) end end dotf(tx + dx * 1.2, ty + dy * 1.2, PAD[1], PAD[2], PAD[3], 0.95) dotf(tx + dx * 1.2 + nx * 1.1, ty + dy * 1.2 + ny * 1.1, PAD[1], PAD[2], PAD[3], 0.8) dotf(tx + dx * 1.2 - nx * 1.1, ty + dy * 1.2 - ny * 1.1, PAD[1], PAD[2], PAD[3], 0.8) end local function firefly(f) local x = 32 + 25 * sin(f * 0.019) local y = 9 + 5 * sin(f * 0.031 + 1) + 3 * sin(f * 0.07) return x, y end local function draw_firefly(x, y, f) local p = 0.65 + 0.35 * sin(f * 0.2) dotf(x, y, 255, 244, 120, p) dotf(x - 1, y, 255, 230, 100, p * 0.55); dotf(x + 1, y, 255, 230, 100, p * 0.55) dotf(x, y - 1, 255, 230, 100, p * 0.55); dotf(x, y + 1, 255, 230, 100, p * 0.55) end local function draw_stars(f) for k = 1, #STX do local a = 0.45 + 0.55 * sin(f * 0.07 + k * 2.1) blend(STX[k], STY[k], 255, 250, 235, a) 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 draw_stars(f) draw_tail(f) draw_scarf_tails(f) local p = WAVE_P > 0 and f >= 10 and (f - 10) % WAVE_P or -1 local th = arm_angle(p) local fx, fy local dirc = 1 if FLY_ON then fx, fy = firefly(f) dirc = fx < 23 and 0 or (fx > 41 and 2 or 1) end draw_eyes(f, th ~= nil and p >= 8 and p < 92, dirc) if th then draw_arm(th) end if FLY_ON then draw_firefly(fx, fy, f) end end