info = { type = "image", title = "Fishbowl and cat", category = "animals", author = "BRRRO", version = "1.0", license = "MIT", desc = "A cat watches a goldfish and taps the bowl", api = 44, } local ltr = ({ sk = { ["Cat"] = "Mačka", ["Fur"] = "Srsť", ["Tabby"] = "Sivá", ["Ginger"] = "Ryšavá", ["Dark"] = "Tmavá", ["Fish speed"] = "Rýchlosť rybky" }, cs = { ["Cat"] = "Kočka", ["Fur"] = "Srst", ["Tabby"] = "Šedá", ["Ginger"] = "Zrzavá", ["Dark"] = "Tmavá", ["Fish speed"] = "Rychlost rybky" }, })[lang] or {} local function LT(s) return ltr[s] or s end settings = { { key = "cat", label = LT("Cat"), type = "bool", default = true }, { key = "fur", label = LT("Fur"), type = "int", default = "tabby", opts = { keys = { "tabby", "ginger", "dark" }, LT("Tabby"), LT("Ginger"), LT("Dark") } }, { key = "speed", label = LT("Fish speed"), type = "int", min = 1, max = 5, default = 3 }, } local rect, px, blend_rect, blend = gfx.rect, gfx.px, gfx.blend_rect, gfx.blend local blit_rgb, blit_indexed, blend_mask, snapshot = gfx.blit_rgb, gfx.blit_indexed, gfx.blend_mask, gfx.snapshot local floor, sin, cos, sqrt, abs, min, max = math.floor, math.sin, math.cos, math.sqrt, math.abs, math.min, math.max local hash32, umod = math.hash32, math.umod local char, concat = string.char, table.concat local W, H, FPS = gfx.W, gfx.H, gfx.FPS local TAU = 6.2831853 local BCX, BCY, BR = 28, 36, 15 local WLV = 26 local TABLE_Y = 46 local LOOP = 1080 local function mix(a, b, t) return a[1] + (b[1] - a[1]) * t, a[2] + (b[2] - a[2]) * t, a[3] + (b[3] - a[3]) * t end local function rgb(r, g, b) return floor(r + 0.5), floor(g + 0.5), floor(b + 0.5) end local function h15(a, b) return hash32(a * 7919 + b * 104729) & 0x7FFF end local PART, BG = nil, nil local function room() for y = 0, TABLE_Y - 1 do local t = y / (TABLE_Y - 1) rect(0, y, W, 1, rgb(54 + 22 * t, 46 + 18 * t, 98 + 20 * t)) end for x = 4, W - 1, 8 do blend_rect(x, 0, 2, TABLE_Y, 150, 130, 210, 0.07) end rect(2, 3, 14, 22, 176, 168, 206) rect(4, 5, 10, 18, 20, 28, 78) rect(8, 5, 2, 18, 176, 168, 206); rect(4, 13, 10, 2, 176, 168, 206) rect(10, 7, 3, 3, 236, 232, 190); rect(11, 7, 2, 2, 20, 28, 78) px(5, 7, 220, 224, 255); px(6, 17, 180, 190, 240); px(12, 18, 220, 224, 255); px(5, 20, 150, 160, 220) rect(1, 25, 16, 2, 206, 198, 228) end local function table_top() for y = TABLE_Y, H - 1 do local t = (y - TABLE_Y) / (H - 1 - TABLE_Y) rect(0, y, W, 1, rgb(mix({ 164, 106, 72 }, { 118, 72, 52 }, t))) end rect(0, TABLE_Y, W, 1, 214, 156, 108) rect(0, TABLE_Y + 1, W, 1, 190, 130, 90) rect(0, 52, W, 1, 96, 58, 42); rect(0, 58, W, 1, 90, 54, 40) for i = 1, 16 do local h = hash32(i * 0x9E3779B1) blend_rect(umod(h, W), TABLE_Y + 2 + umod(h >> 7, 16), 3 + umod(h >> 12, 8), 1, 80, 44, 30, 0.2) end for k = 0, 3 do local hw = 15 - k * 2.5 blend_rect(floor(BCX + 2 - hw), 50 + k, floor(hw * 2), 1, 40, 20, 40, 0.16) end for k = 0, 3 do local hw = 7 - k * 1.5 blend_rect(floor(49 - hw), 54 + k - (k > 1 and 1 or 0), floor(hw * 2), 1, 150, 226, 236, 0.2) end end local GRAVEL = { { 214, 190, 150 }, { 190, 160, 130 }, { 240, 226, 196 }, { 150, 170, 176 }, { 226, 174, 150 } } local function gravel() for x = BCX - 13, BCX + 13 do local top = 47 - floor(1.6 * sin(x * 0.55) + 0.5) - (abs(x - BCX) < 6 and 1 or 0) for y = top, 51 do local dx, dy = x + 0.5 - BCX, y + 0.5 - BCY if dx * dx + dy * dy < 13.6 * 13.6 then local c = GRAVEL[h15(x, y) % 5 + 1] px(x, y, c[1], c[2], c[3]) end end end end local MX, MY, MN = BCX - 16, BCY - 16, 32 local M_LIGHT, M_DEEP, M_AIR, M_HI, M_EDGE = {}, {}, {}, {}, {} local MASKS = {} local function mask_rows(ya, yb) local unpack = table.unpack for j = ya, yb do local l, d, a, hi, e = {}, {}, {}, {}, {} local y = MY + j for i = 0, MN - 1 do local dx, dy = MX + i + 0.5 - BCX, y + 0.5 - BCY local r2 = dx * dx + dy * dy local vl, vd, va, vh, ve = 0, 0, 0, 0, 0 if r2 <= 237 then local r = sqrt(r2) if r <= BR - 0.8 then if y >= WLV then local dep = (y - WLV) / 25 vl = floor(127 * (1 - dep) * (y == WLV and 1.6 or 1)) vd = floor(255 * (0.22 + 0.55 * dep)) + (r > BR - 2 and 60 or 0) else va = 36 end end if r >= BR - 1.6 then ve = floor(255 * (1 - abs(r - (BR - 0.6)) / 1.2)) end if r > 11.4 and r < 13.8 then if dx < 0 and dy < 0 then local ang = math.atan(dy, dx) if ang > -2.65 and ang < -1.75 then vh = floor(255 * max(0, 1 - abs(r - 12.6) / 1.1) * (1 - abs(ang + 2.2) / 0.5)) end elseif dx > 0 and dy > 0 then local ang = math.atan(dy, dx) if ang > 0.35 and ang < 1.0 then vh = floor(110 * max(0, 1 - abs(r - 12.6) / 1.0)) end end end local ex, ey = dx + 7.5, dy + 8.5 if ex * ex + ey * ey < 1.6 then vh = 255 end end l[i + 1], d[i + 1], a[i + 1], hi[i + 1], e[i + 1] = min(255, max(0, vl)), min(255, max(0, vd)), va, min(255, max(0, vh)), min(255, max(0, ve)) end M_LIGHT[j + 1], M_DEEP[j + 1], M_AIR[j + 1], M_HI[j + 1], M_EDGE[j + 1] = char(unpack(l)), char(unpack(d)), char(unpack(a)), char(unpack(hi)), char(unpack(e)) end if yb == MN - 1 then MASKS.light, MASKS.deep, MASKS.air, MASKS.hi, MASKS.edge = concat(M_LIGHT), concat(M_DEEP), concat(M_AIR), concat(M_HI), concat(M_EDGE) end end local STAGE = 1 local STAGES local function build() if STAGE > 1 and STAGE <= 3 then blit_rgb(PART, 0, 0, W, H) end STAGES[STAGE]() if STAGE <= 2 then PART = snapshot(0, 0, W, H) end if STAGE == 3 then BG = snapshot(0, 0, W, H) end STAGE = STAGE + 1 end local function tri(x, y, ax, ay, bx, by, cx, cy) local d1 = (x - bx) * (ay - by) - (ax - bx) * (y - by) local d2 = (x - cx) * (by - cy) - (bx - cx) * (y - cy) local d3 = (x - ax) * (cy - ay) - (cx - ax) * (y - ay) return not ((d1 < 0 or d2 < 0 or d3 < 0) and (d1 > 0 or d2 > 0 or d3 > 0)) end local PEND = {} local function pack(w, h, f, key, ya, yb) local g = key and PEND[key] or {} if key then PEND[key] = g end for y = ya or 0, yb or h - 1 do local r = {} for x = 0, w - 1 do r[x] = f(x, y) end g[y] = r end if yb and yb < h - 1 then return nil end if key then PEND[key] = nil end local rows, e = {}, {} for y = 0, h - 1 do local c, m = {}, {} for x = 0, w - 1 do c[x + 1] = char(g[y][x]) m[x + 1] = (g[y][x] ~= 0 and (x == w - 1 or g[y][x + 1] == 0)) and "\255" or "\0" end rows[y + 1], e[y + 1] = concat(c), concat(m) end return { s = concat(rows), edge = concat(e), w = w, h = h } end local LXD, LYD = -0.55, -0.7 local function shade(nx, ny) local t = nx * LXD + ny * LYD if t > 0.45 then return 2 elseif t > -0.25 then return 1 end return 3 end local FURS = { tabby = { { 128, 128, 152 }, { 168, 168, 190 }, { 92, 92, 120 }, { 62, 62, 90 }, { 226, 156, 176 } }, ginger = { { 220, 138, 64 }, { 250, 186, 110 }, { 168, 94, 44 }, { 124, 62, 32 }, { 246, 170, 160 } }, dark = { { 70, 68, 92 }, { 108, 106, 136 }, { 44, 42, 64 }, { 30, 28, 46 }, { 200, 130, 150 } }, } local CPAL = { 0, 0, 0 } local function set_fur(k) local f = FURS[k] or FURS.tabby CPAL = { 0, 0, 0 } local function add(c) CPAL[#CPAL + 1], CPAL[#CPAL + 2], CPAL[#CPAL + 3] = c[1], c[2], c[3] end for i = 1, 5 do add(f[i]) end add({ 214, 232, 96 }); add({ 18, 16, 28 }); add({ 244, 240, 236 }) end local HEAD, BODY, PAW = {}, nil, nil local RINGS = {} local function ring(r) local m = RINGS[r] if m then return m end local rows = {} for y = 0, 2 * r do local c = {} for x = 0, 2 * r do c[x + 1] = char(max(0, floor((1 - abs(sqrt((x - r) ^ 2 + (y - r) ^ 2) - r) / 0.75) * 255))) end rows[y + 1] = concat(c) end m = concat(rows) RINGS[r] = m return m end local function head(turn, ya, yb) local sp = HEAD[turn] if sp then return sp end sp = pack(18, 15, function(x, y) local fx, fy = x + 0.5, y + 0.5 if y <= 5 then if tri(fx, fy, 2.6, 5.4, 3.0, 0.0, 7.6, 3.6) and ((fx - 9) / 7.6) ^ 2 + ((fy - 9.2) / 5.6) ^ 2 > 1 then if tri(fx, fy, 3.4, 4.4, 3.6, 1.6, 5.8, 3.6) then return 5 end return shade(-0.4, -0.6) end if tri(fx, fy, 15.4, 5.4, 15.0, 0.0, 10.4, 3.6) and ((fx - 9) / 7.6) ^ 2 + ((fy - 9.2) / 5.6) ^ 2 > 1 then if tri(fx, fy, 14.6, 4.4, 14.4, 1.6, 12.2, 3.6) then return 5 end return shade(0.4, -0.6) end end if y >= 10 then if x == 8 + turn and y == 10 then return 5 end if x == 8 + turn and y == 11 then return 7 end if y == 10 and ((x >= 1 + turn and x <= 3 + turn) or (x >= 13 + turn and x <= 15 + turn)) and (x + y) % 2 == 0 then return 8 end end local hu, hv = (fx - 9) / 7.6, (fy - 9.2) / 5.6 if hu * hu + hv * hv <= 1 then if (y == 4 or y == 5) and (x == 6 or x == 9 or x == 12) then return 4 end if y >= 10 and y <= 12 and abs(fx - (9 + turn)) < 2.6 then return 8 end return shade(hu, hv) end return 0 end, "h" .. turn, ya, yb) HEAD[turn] = sp return sp end local function body() if BODY then return BODY end BODY = pack(24, 14, function(x, y) local fx, fy = x + 0.5, y + 0.5 local u, v = (fx - 12) / 10.8, (fy - 9) / 8 if u * u + v * v <= 1 then if ((fx - 12) / 3.4) ^ 2 + ((fy - 5.5) / 3.2) ^ 2 <= 1 then return 8 end return shade(u, v) end return 0 end) return BODY end local function paw() if PAW then return PAW end PAW = pack(6, 5, function(x, y) local u, v = (x + 0.5 - 3) / 3, (y + 0.5 - 2.7) / 2.4 if u * u + v * v <= 1 then if y == 3 and (x == 2 or x == 3) then return 4 end return shade(u, v) end return 0 end) return PAW end local FPAL = { 0, 0, 0, 255, 140, 36, 255, 198, 112, 255, 96, 28, 40, 20, 20 } local FISH = {} local function fish_sprite(dir, wag) local key = dir * 2 + wag local s = FISH[key] if s then return s end s = pack(10, 6, function(x0, y) local x = dir > 0 and x0 or 9 - x0 local fx, fy = x + 0.5, y + 0.5 if abs(fx - 8) < 0.8 and abs(fy - 2.5) < 0.8 then return 4 end local u, v = (fx - 6) / 3.6, (fy - 3) / 2.4 if u * u + v * v <= 1 then return fy > 3.4 and 2 or 1 end if fx < 3.4 and abs(fy - 3 - (wag == 1 and 0.9 or -0.9)) < 0.7 + (3.4 - fx) * 0.5 then return 3 end if tri(fx, fy, 4.6, 1.4, 6.8, 1.4, 6.0, 0.1) then return 3 end return 0 end) FISH[key] = s return s end local PLANT = {} local function plant(pose, ya, yb) local s = PLANT[pose] if s then return s end local sway = ({ -0.06, 0, 0.06 })[pose] local L = {} for k = 0, 8 do local a = -1.3 + k * 0.325 + sway L[#L + 1] = { sin(a), -cos(a), 8 + (k % 3) * 2.2, 1.5 + (k % 2) * 0.5 } end s = pack(16, 16, function(x, y) local fx, fy = x + 0.5 - 8, y + 0.5 - 15.5 local best for _, l in ipairs(L) do local t = max(0, min(l[3], fx * l[1] + fy * l[2])) local dx, dy = fx - l[1] * t, fy - l[2] * t if dx * dx + dy * dy <= l[4] * l[4] then local k = t / l[3] local c = k > 0.7 and 3 or (k > 0.3 and 2 or 1) if not best or c > best then best = c end end end return best or 0 end, "p" .. pose, ya, yb) PLANT[pose] = s return s end local PLPAL = { 0, 0, 0, 14, 66, 74, 24, 106, 86, 62, 160, 104 } STAGES = { room, table_top, gravel, function() mask_rows(0, 3) end, function() mask_rows(4, 7) end, function() mask_rows(8, 11) end, function() mask_rows(12, 15) end, function() mask_rows(16, 19) end, function() mask_rows(20, 23) end, function() mask_rows(24, 27) end, function() mask_rows(28, MN - 1) end, function() body(); paw() end, function() head(-2, 0, 7) end, function() head(-2, 8, 14) end, function() head(-1, 0, 7) end, function() head(-1, 8, 14) end, function() head(0, 0, 7) end, function() head(0, 8, 14) end, function() fish_sprite(1, 0); fish_sprite(1, 1) end, function() fish_sprite(-1, 0); fish_sprite(-1, 1) end, function() plant(1, 0, 7) end, function() plant(1, 8, 15) end, function() plant(2, 0, 7) end, function() plant(2, 8, 15) end, function() plant(3, 0, 7) end, function() plant(3, 8, 15) end, } function on_settings() set_fur(cfg.fur) end local T_UP0, T_UP1, T_PAW0, T_PAW1, T_TAP1, T_TAP2, T_PAW2, T_PAW3, T_DN0, T_DN1 = 180, 270, 510, 550, 560, 624, 650, 690, 900, 990 local T_HIDE0, T_HIDE1, T_BACK0, T_BACK1 = 566, 596, 980, 1025 local HX, HY = 21, 44 local PLX, PLY = 14, 35 local function smooth(p) p = max(0, min(1, p)); return p * p * (3 - 2 * p) end local function fish_xy(f, k) local a = f / FPS * TAU / 7 * k local x, y = 28 + 8.5 * cos(a), 38 + 3.4 * sin(a) local w if f < T_HIDE0 or f >= T_BACK1 then w = 0 elseif f < T_BACK0 then w = smooth((f - T_HIDE0) / (T_HIDE1 - T_HIDE0)) else w = 1 - smooth((f - T_BACK0) / (T_BACK1 - T_BACK0)) end if w > 0 then x, y = x + (HX - x) * w, y + (HY - y) * w + 0.5 * w * sin(f * 0.07) end return x, y end local function cat_yoff(f) if f < T_UP0 or f >= T_DN1 then return 26 end if f < T_UP1 then return 26 * (1 - smooth((f - T_UP0) / (T_UP1 - T_UP0))) end if f < T_DN0 then return 0 end return 26 * smooth((f - T_DN0) / (T_DN1 - T_DN0)) end local function paw_state(f) if f < T_PAW0 or f >= T_PAW3 then return nil end local px_, py_ = 47, 38 if f < T_PAW1 then local p = smooth((f - T_PAW0) / (T_PAW1 - T_PAW0)) return 47, 52 - 14 * p end if f >= T_PAW2 then local p = smooth((f - T_PAW2) / (T_PAW3 - T_PAW2)) return 47, 38 + 14 * p end for _, t0 in ipairs({ T_TAP1, T_TAP2 }) do local d = f - t0 if d >= 0 and d < 16 then local p = d < 5 and d / 5 or (d < 8 and 1 or 1 - (d - 8) / 8) return 47 - 5 * p, py_ end end return px_, py_ end local function put(sp, pal, fx, y) local ix = floor(fx) local ax = fx - ix if ax > 0.06 then blend_mask(sp.edge, pal[4], pal[5], pal[6], ix + 1, y, sp.w, sp.h, sp.w, sp.h, ax) end blit_indexed(sp.s, pal, ix, y, sp.w, sp.h, sp.w, sp.h, true) end local NBUB = 3 local function draw_cat(f, fxx, fyy) local yo = cat_yoff(f) if yo >= 24 then return end gfx.clip(0, 0, W, TABLE_Y) local iy = floor(yo + 0.5) local dx, dy = fxx - 44, fyy - 30 local turn = dx < -19 and -2 or (dx < -13 and -1 or 0) local look = dy > 9 and 1 or 0 local lf = f - T_PAW3 if lf >= 0 and f < T_DN0 then local s = lf // 70 % 3 turn = s == 0 and -2 or (s == 1 and -1 or 0) look = 0 end local blink = f % 170 < 5 local b = body() blit_indexed(b.s, CPAL, 32, 34 + iy, b.w, b.h, b.w, b.h, true) local hd = HEAD[turn] blit_indexed(hd.s, CPAL, 35, 21 + iy, hd.w, hd.h, hd.w, hd.h, true) for _, ex in ipairs({ 4 + turn, 11 + turn }) do local x, y = 35 + ex, 28 + iy if blink then rect(x, y, 2, 1, CPAL[4], CPAL[5], CPAL[6]); rect(x, y + 1, 2, 1, CPAL[22], CPAL[23], CPAL[24]) else rect(x, y, 2, 2, CPAL[19], CPAL[20], CPAL[21]) px(x + (turn <= -1 and 0 or 1), y + look, CPAL[22], CPAL[23], CPAL[24]) end end local pxp, pyp = paw_state(f) if pxp then local ax, ay = 38, 41 for i = 0, 5 do local t = i / 5 rect(floor(ax + (pxp + 2 - ax) * t), floor(ay + (pyp + 1 - ay) * t), 3, 3, CPAL[4], CPAL[5], CPAL[6]) end local pw = paw() blit_indexed(pw.s, CPAL, floor(pxp), floor(pyp), pw.w, pw.h, pw.w, pw.h, true) end gfx.clip() end function render() if STAGE <= #STAGES then if BG then blit_rgb(BG, 0, 0, W, H) end build() gfx.ready(false) return end local f = (frame + 240) % LOOP local t = frame / FPS local k = 0.55 + 0.15 * (cfg.speed or 3) blit_rgb(BG, 0, 0, W, H) local fx, fy = fish_xy(f, k) if cfg.cat ~= false then draw_cat(f, fx, fy) end blend_mask(MASKS.air, 200, 230, 240, MX, MY, MN, MN, MN, MN, 1) blend_mask(MASKS.light, 156, 226, 238, MX, MY, MN, MN, MN, MN, 0.75) blend_mask(MASKS.deep, 24, 106, 186, MX, MY, MN, MN, MN, MN, 0.75) gfx.clip(14, WLV, 29, 24) for _, t0 in ipairs({ T_TAP1 + 5, T_TAP2 + 5 }) do local u = (f - t0) / FPS if u >= 0 and u < 1.3 then local rf = 1 + u * 9 local r0 = floor(rf) local fr = rf - r0 local a = 0.8 * (1 - u / 1.3) for j = 0, 1 do local r = r0 + j local aa = a * (j == 0 and 1 - fr or fr) if aa > 0.03 then blend_mask(ring(r), 236, 250, 255, 41 - r, 38 - r, 2 * r + 1, 2 * r + 1, 2 * r + 1, 2 * r + 1, aa) end end end end gfx.clip() local dxv = fish_xy(f + 2, k) - fish_xy(f - 2, k) local dir = dxv > 0.01 and 1 or -1 local sp = fish_sprite(dir, (f // 6) % 2) put(sp, FPAL, fx - 5, floor(fy - 3 + 0.5)) local pl = plant((f // 75) % 4 == 1 and 1 or ((f // 75) % 4 == 3 and 3 or 2)) blit_indexed(pl.s, PLPAL, PLX, PLY, pl.w, pl.h, pl.w, pl.h, true) for i = 1, NBUB do local h = hash32(i * 0x27D4EB2F) local P = 3.2 + umod(h, 20) / 10 local p = (t / P + umod(h >> 6, 100) / 100) % 1 local yf = 47 - 21 * p local iy = floor(yf) local fr = yf - iy local x = 33 + umod(h >> 12, 6) + floor(1.2 * sin(t * 2 + i) + 0.5) blend(x, iy, 236, 250, 255, 0.8 * (1 - fr)) blend(x, iy + 1, 236, 250, 255, 0.8 * fr) end blend_mask(MASKS.edge, 10, 70, 110, MX, MY, MN, MN, MN, MN, 0.7) blend_mask(MASKS.hi, 255, 255, 255, MX, MY, MN, MN, MN, MN, 0.85) local gx = 20 + floor((t * 3) % 16) blend_rect(gx, WLV, 3, 1, 255, 255, 255, 0.5) end