info = { type = "image", title = "Cat and mouse", category = "animals", author = "BRRRO", version = "1.0", license = "MIT", desc = "A cat chases a mouse with cheese into a hole", api = 33, } local tr = ({ sk = { ["Chase speed"] = "Rýchlosť naháňania", ["slow"] = "pomaly", ["normal"] = "normálne", ["fast"] = "rýchlo", ["Cat colour"] = "Farba mačky" }, cs = { ["Chase speed"] = "Rychlost honičky", ["slow"] = "pomalu", ["normal"] = "normálně", ["fast"] = "rychle", ["Cat colour"] = "Barva kočky" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "speed", label = T("Chase speed"), type = "int", default = 1, opts = { T("slow"), T("normal"), T("fast") } }, { key = "cat", label = T("Cat colour"), type = "color", default = "#E88A3C" }, } local rect, px = gfx.rect, gfx.px local floor, sqrt = math.floor, math.sqrt local W, H, FPS = gfx.W, gfx.H, gfx.FPS local function cv(w, h) local c = { w = w, h = h }; for i = 1, w * h do c[i] = 0 end; return c end local function cp(c, x, y, v) if x >= 0 and y >= 0 and x < c.w and y < c.h then c[y * c.w + x + 1] = v end end local function cr(c, x, y, w, h, v) for j = y, y + h - 1 do for i = x, x + w - 1 do cp(c, i, j, v) end end end local function ce(c, cx, cy, rx, ry, v) for y = floor(cy - ry), floor(cy + ry) do for x = floor(cx - rx), floor(cx + rx) do local dx, dy = (x + 0.5 - cx) / rx, (y + 0.5 - cy) / ry if dx * dx + dy * dy <= 1 then cp(c, x, y, v) end end end end local function cm(c, x, y, rows, keys) for j = 1, #rows do for i = 1, #rows[j] do local ch = rows[j]:sub(i, i) if ch ~= "." then cp(c, x + i - 1, y + j - 1, keys:find(ch, 1, true) - 1) end end end end local function cs(c, flip) local t = {} for y = 0, c.h - 1 do local row = {} for x = 1, c.w do row[x] = c[y * c.w + (flip and (c.w - x + 1) or x)] end t[#t + 1] = string.char(table.unpack(row)) end return table.concat(t) end local O, D, L, E, N, G, P, MG, MD, MP, CY, CD, WH = 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13 local KEYS = ".odleNgpmMrycw" local CW, CH = 28, 20 local function make_cat(leg, tail) local c = cv(CW, CH) cr(c, 2, 12, 5, 2, O) cr(c, 1 + tail, 5, 2, 8, O) cr(c, 1 + tail, 5, 2, 2, D) local a = leg == 0 and 1 or (leg == 1 and -1 or 0) cr(c, 6 + a, 14, 2, 5, D); cr(c, 10 - a, 14, 2, 5, O) cr(c, 17 + a, 14, 2, 5, D); cr(c, 21 - a, 14, 2, 5, O) cr(c, 6 + a, 18, 2, 1, L); cr(c, 10 - a, 18, 2, 1, L) cr(c, 17 + a, 18, 2, 1, L); cr(c, 21 - a, 18, 2, 1, L) ce(c, 13, 11, 10, 5.5, O) ce(c, 14, 14, 6.5, 2.5, L) cr(c, 6, 6, 1, 3, D); cr(c, 9, 5, 1, 3, D); cr(c, 12, 5, 1, 3, D); cr(c, 15, 5, 1, 3, D) ce(c, 22, 8, 5.5, 4.5, O) cm(c, 17, 1, { "d..", "odo", "ooo" }, KEYS) cm(c, 23, 1, { "..d", "odo", "ooo" }, KEYS) cm(c, 18, 2, { "p" }, KEYS); cm(c, 25, 2, { "p" }, KEYS) cr(c, 24, 11, 4, 2, L) cp(c, 27, 9, N) cr(c, 24, 7, 2, 2, G); cp(c, 25, 7, E); cp(c, 25, 8, E) cr(c, 20, 6, 1, 2, D); cr(c, 22, 4, 1, 2, D) return c end local MW, MH = 19, 11 local function make_mouse(leg) local c = cv(MW, MH) local a = leg == 0 and 1 or -1 cr(c, 0, 7, 4, 1, MP); cp(c, 0, 6, MP) cr(c, 5 + a, 8, 2, 2, MD); cr(c, 12 - a, 8, 2, 2, MD) ce(c, 10, 7, 5.5, 3, MG) ce(c, 15.5, 6.5, 3, 2.5, MG) cm(c, 13, 2, { ".mm", "mpm", ".m." }, KEYS) cp(c, 18, 7, N) cp(c, 16, 5, E) cm(c, 5, 0, { "..yy..", ".yyyyy", "yyycyy", "yyyyyy" }, ".odleNgpmMrycw") cp(c, 7, 2, CD); cp(c, 9, 1, CD); cp(c, 10, 3, CD) return c end local function pal_list(cat) local r, g, b = cat.r, cat.g, cat.b local function dk(k) return { floor(r * k), floor(g * k), floor(b * k) } end local function lt(k) return { r + floor((255 - r) * k), g + floor((255 - g) * k), b + floor((250 - b) * k) } end local l = { { 0, 0, 0 }, { r, g, b }, dk(0.66), lt(0.62), { 24, 20, 30 }, { 236, 110, 130 }, { 90, 210, 120 }, { 255, 170, 180 }, { 156, 160, 190 }, { 100, 104, 140 }, { 255, 150, 170 }, { 255, 214, 60 }, { 214, 150, 24 }, { 255, 255, 255 }, } local t = {} for i = 1, #l do t[#t + 1] = l[i][1]; t[#t + 1] = l[i][2]; t[#t + 1] = l[i][3] end return t end local PAL = pal_list({ r = 232, g = 138, b = 60 }) local CAT, CATF, MOU, MOUF = {}, {}, nil, nil local CATV = { { 0, 0 }, { 1, 0 }, { 2, 0 }, { 2, 1 } } local function build_cat(i) local c = make_cat(CATV[i][1], CATV[i][2]) CAT[i], CATF[i] = { cs(c) }, { cs(c, true) } end local function build_mice() MOU, MOUF = {}, {} for i = 1, 2 do local c = make_mouse(i - 1) MOU[i], MOUF[i] = cs(c), cs(c, true) end end local HOLE_X = 52 local CLIPX = HOLE_X + 5 local SP = 22.5 local RUN, RUN2, SIT, EXIT, LEG = 0, 0, 2.67, 0, 0 local function dur(px_) return px_ / SP end function on_settings() SP = (2 + (cfg.speed or 1)) * 7.5 RUN = dur(CLIPX + MW) RUN2 = dur(HOLE_X + MW + 7) EXIT = dur(40) LEG = RUN2 + SIT + EXIT + 0.27 local c = cfg.cat or { r = 232, g = 138, b = 60 } PAL = pal_list(c) end local BG = nil local function ell(cx, cy, rx, ry, r, g, b) 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, r, g, b) end end end local function hole(x) ell(x + 4.5, 40, 5.5, 5.5, 150, 84, 70) rect(x - 1, 40, 11, 6, 150, 84, 70) ell(x + 4.5, 40.5, 4.5, 4.5, 40, 22, 34) rect(x, 40, 9, 5, 40, 22, 34) end local function build_bg() rect(0, 0, W, 43, 238, 192, 152) for x = 0, W - 1, 8 do rect(x, 0, 4, 43, 226, 172, 136) end rect(0, 43, W, 3, 130, 76, 56) rect(0, 43, W, 1, 168, 108, 80) rect(0, 46, W, 18, 204, 148, 94) rect(0, 53, W, 1, 176, 118, 74); rect(0, 59, W, 1, 176, 118, 74) for x = 6, W - 1, 21 do rect(x, 46, 1, 7, 176, 118, 74) end for x = 16, W - 1, 21 do rect(x, 54, 1, 5, 176, 118, 74) end for x = 2, W - 1, 21 do rect(x, 60, 1, 4, 176, 118, 74) end rect(7, 5, 17, 20, 248, 244, 236) rect(9, 7, 6, 7, 130, 200, 255); rect(16, 7, 6, 7, 130, 200, 255) rect(9, 15, 6, 8, 130, 200, 255); rect(16, 15, 6, 8, 130, 200, 255) rect(10, 10, 4, 2, 255, 255, 255); rect(17, 18, 4, 2, 255, 255, 255) rect(7, 25, 17, 2, 190, 130, 96) rect(35, 6, 16, 14, 150, 84, 70) rect(37, 8, 12, 10, 150, 214, 190) rect(39, 11, 8, 5, 255, 214, 60); rect(41, 10, 4, 1, 255, 214, 60) px(41, 13, 214, 150, 24); px(44, 12, 214, 150, 24); px(45, 14, 214, 150, 24) hole(HOLE_X); hole(W - HOLE_X - 9) BG = gfx.snapshot(0, 0, W, H) end local MOUSE_Y, CAT_Y = 37, 41 function render() if not BG then build_bg() elseif #CAT < 4 then build_cat(#CAT + 1) elseif not MOU then build_mice() end gfx.blit_rgb(BG, 0, 0, W, H) if not MOU then return end local t = (frame / FPS + 2 * LEG - 2) % (2 * LEG) local back = t >= LEG local u = t % LEG local mx, cx, cpose, peek, mvis = 0, 0, 3, false, false if u < RUN2 then local s = floor(u * SP) mx, cx, mvis = s - MW, s - MW - 33, u < RUN cpose = floor(u * 7.5) % 2 elseif u < RUN2 + SIT then local v = u - RUN2 cx = HOLE_X - CW + 2 cpose = 2 + floor(v * 2.5) % 2 peek = v > SIT - 1.47 and v < SIT - 0.13 else local e = u - RUN2 - SIT cx = HOLE_X - CW + 2 + floor(e * SP) cpose = floor(e * 7.5) % 2 end if mvis then local lg = floor(u * 7.5) % 2 local bob = (lg == 0) and 0 or -1 local spr = back and MOUF[1 + lg] or MOU[1 + lg] local x = back and (W - mx - MW) or mx local cl = back and (W - CLIPX) or CLIPX if back then gfx.clip(cl, 0, W - cl, H) else gfx.clip(0, 0, cl, H) end gfx.blit_indexed(spr, PAL, x, MOUSE_Y + bob, MW, MH, MW, MH, true) gfx.clip() end if peek then local blink = floor(u * 10) % 9 == 0 local ex = back and (W - HOLE_X - 9) or HOLE_X local y = blink and 42 or 41 local hh = blink and 1 or 2 rect(ex + 1, y, 2, hh, 255, 255, 255); rect(ex + 5, y, 2, hh, 255, 255, 255) px(ex + (back and 1 or 2), y, 24, 20, 30); px(ex + (back and 5 or 6), y, 24, 20, 30) end local csp = (back and CATF or CAT)[cpose + 1][1] local x = back and (W - cx - CW) or cx gfx.blit_indexed(csp, PAL, x, CAT_Y + (cpose < 2 and floor(u * 7.5) % 2 or 0), CW, CH, CW, CH, true) end