info = { type = "image", title = "Puppy chase", category = "animals", author = "BRRRO", version = "1.0", license = "MIT", desc = "Puppy chases its tail, then a butterfly", api = 33, } local tr = ({ sk = { ["Puppy"] = "Šteňa", ["Cream"] = "Krémové", ["Brown"] = "Hnedé", ["Spotted"] = "Škvrnité", ["Butterfly"] = "Motýľ", ["Orange"] = "Oranžový", ["Blue"] = "Modrý", ["Yellow"] = "Žltý" }, cs = { ["Puppy"] = "Štěně", ["Cream"] = "Krémové", ["Brown"] = "Hnědé", ["Spotted"] = "Skvrnité", ["Butterfly"] = "Motýl", ["Orange"] = "Oranžový", ["Blue"] = "Modrý", ["Yellow"] = "Žlutý" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "coat", label = T("Puppy"), type = "int", default = 0, opts = { T("Cream"), T("Brown"), T("Spotted") } }, { key = "wing", label = T("Butterfly"), type = "int", default = 0, opts = { T("Orange"), T("Blue"), T("Yellow") } }, } local rect, px, blend, blend_rect, blit = gfx.rect, gfx.px, gfx.blend, gfx.blend_rect, gfx.blit_indexed local floor, ceil, sin, sqrt, max, abs = math.floor, math.ceil, math.sin, math.sqrt, math.max, math.abs local hash32, umod = math.hash32, math.umod local W, H, FPS = gfx.W, gfx.H, gfx.FPS local PERIOD = 22 * FPS local DW, DH = 24, 20 local GR = {} local function grid() for i = 1, DW * DH do GR[i] = 0 end; return GR end local function put(g, x, y, c) x, y = floor(x), floor(y) if x >= 0 and x < DW and y >= 0 and y < DH then g[y * DW + x + 1] = c end end local function box(g, x, y, w, h, c) for yy = y, y + h - 1 do for xx = x, x + w - 1 do put(g, xx, yy, c) end end end local function ell(g, cx, cy, rx, ry, c, dk, lt) for y = max(0, floor(cy - ry)), math.min(DH - 1, ceil(cy + ry)) do local dy = (y + 0.5 - cy) / ry for x = max(0, floor(cx - rx)), math.min(DW - 1, ceil(cx + rx)) do local dx = (x + 0.5 - cx) / rx if dx * dx + dy * dy <= 1 then g[y * DW + x + 1] = (dk and dy > 0.45) and dk or ((lt and dx + dy < -0.8) and lt or c) end end end end local function line(g, x0, y0, x1, y1, c) local n = max(abs(x1 - x0), abs(y1 - y0), 1) for i = 0, n do local x, y = x0 + (x1 - x0) * i / n, y0 + (y1 - y0) * i / n put(g, x, y, c); put(g, x + 1, y, c) end end local function head(g, hx, hy, d, up, pant) local function X(v) return hx + d * v end ell(g, X(0), hy, 3.9, 3.6, 1, 2, 7) ell(g, X(3.2), hy + 1.6 - up * 2.2, 2.3, 1.7, 7) put(g, X(5) - (d < 0 and 1 or 0), hy + 0.8 - up * 2.2, 5); put(g, X(5) - (d < 0 and 1 or 0), hy + 1.8 - up * 2.2, 5) ell(g, X(-2.2), hy - 0.3, 1.7, 3.3, 3, 4) put(g, X(1.5) - (d < 0 and 1 or 0), hy - 1, 5) if pant then put(g, X(3.5) - (d < 0 and 1 or 0), hy + 3.5 - up * 2.2, 6); put(g, X(3.5) - (d < 0 and 1 or 0), hy + 4.5 - up * 2.2, 6) end end local function four(g, o) for _, l in ipairs(o.legs) do if l[4] == 2 then line(g, l[1], 13, l[2], l[3], 2) end end line(g, o.tail[1], o.tail[2], o.tail[3], o.tail[4], 1); put(g, o.tail[3], o.tail[4] - 1, 3) ell(g, 10.5, o.by or 11, 7, 4.4, 1, 2, 7) ell(g, 9, (o.by or 11) - 2, 4, 2.3, 3, 4) for _, l in ipairs(o.legs) do if l[4] == 1 then line(g, l[1], 13, l[2], l[3], 1) end end for _, l in ipairs(o.legs) do if l[3] >= 15 then put(g, l[2], l[3], 7); put(g, l[2] + 1, l[3], 7) end end box(g, 14, (o.by or 11) - 2, 2, 3, 8) head(g, 18, o.hy or 6.5, 1, o.up or 0, o.pant) end local POSE = { stand = function(g, pant) four(g, { tail = { 4, 9, 1, 4 }, pant = pant, legs = { { 8, 8, 19, 2 }, { 13, 13, 19, 2 }, { 6, 5, 19, 1 }, { 15, 16, 19, 1 } } }) end, walkA = function(g) four(g, { tail = { 4, 9, 1, 5 }, legs = { { 9, 11, 19, 2 }, { 12, 11, 19, 2 }, { 6, 3, 19, 1 }, { 15, 17, 19, 1 } } }) end, walkB = function(g) four(g, { tail = { 4, 9, 1, 5 }, legs = { { 9, 6, 19, 2 }, { 12, 15, 19, 2 }, { 6, 8, 19, 1 }, { 15, 13, 19, 1 } } }) end, runA = function(g) four(g, { by = 10, hy = 5.5, tail = { 4, 8, 0, 6 }, legs = { { 8, 3, 16, 2 }, { 13, 19, 16, 2 }, { 6, 0, 15, 1 }, { 15, 21, 15, 1 } } }) end, runB = function(g) four(g, { by = 11, hy = 6.5, tail = { 4, 9, 0, 7 }, legs = { { 8, 9, 19, 2 }, { 13, 14, 19, 2 }, { 6, 7, 19, 1 }, { 15, 12, 19, 1 } } }) end, look = function(g) four(g, { hy = 4.5, up = 1, tail = { 4, 9, 1, 4 }, legs = { { 8, 8, 19, 2 }, { 13, 13, 19, 2 }, { 6, 5, 19, 1 }, { 15, 16, 19, 1 } } }) end, sit = function(g, wag) if not wag then line(g, 3, 18, 0, 17, 1) else line(g, 3, 17, 1, 13, 1) end ell(g, 8.5, 14, 5.2, 4.6, 1, 2, 7) ell(g, 12.5, 9.5, 3.6, 6.5, 1, 2, 7) ell(g, 9, 11, 2.2, 2.6, 3, 4) ell(g, 11, 18.5, 3.2, 1.3, 7) line(g, 12, 13, 12, 19, 2); line(g, 14, 13, 14, 19, 1) put(g, 12, 19, 7); put(g, 13, 19, 7); put(g, 14, 19, 7); put(g, 15, 19, 7) box(g, 11, 7, 4, 2, 8) head(g, 14.5, 4, 1, 0.3, true) end, spin = function(g) line(g, 7, 11, 1, 9, 1); put(g, 1, 8, 3) ell(g, 13, 11.5, 6.5, 4.4, 1, 2, 7) ell(g, 13, 9.5, 4, 2.3, 3, 4) line(g, 9, 13, 8, 19, 2); line(g, 15, 13, 16, 19, 2) line(g, 11, 13, 11, 19, 1); line(g, 17, 13, 18, 19, 1) for _, x in ipairs({ 8, 11, 16, 18 }) do put(g, x, 19, 7); put(g, x + 1, 19, 7) end head(g, 6.5, 6.5, -1, 0, false) end, front = function(g) ell(g, 12, 12.5, 5.5, 5, 1, 2, 7) ell(g, 12, 14, 3, 3, 7) box(g, 8, 18, 3, 2, 7); box(g, 13, 18, 3, 2, 7) box(g, 9, 11, 6, 1, 8) ell(g, 12, 6.5, 5, 4.4, 1, 2, 7) ell(g, 6.8, 7, 1.8, 3.6, 3, 4); ell(g, 17.2, 7, 1.8, 3.6, 3, 4) ell(g, 12, 8.8, 2.6, 1.9, 7) box(g, 11, 7, 2, 1, 5); put(g, 9, 5, 5); put(g, 14, 5, 5) put(g, 12, 10, 6) end, back = function(g) ell(g, 12, 12.5, 5.5, 5, 1, 2, 7) ell(g, 12, 12, 3.8, 3.6, 3, 4) box(g, 8, 18, 3, 2, 2); box(g, 13, 18, 3, 2, 2) ell(g, 12, 5.5, 4.5, 3.8, 1, 2, 7) ell(g, 7.3, 6, 1.8, 3.2, 3, 4); ell(g, 16.7, 6, 1.8, 3.2, 3, 4) box(g, 9, 9, 6, 1, 8) line(g, 12, 9, 14, 5, 1); put(g, 14, 4, 3) end, } local CACHE = {} local function pose(name, arg) local k = name .. (arg and 1 or 0) local p = CACHE[k] if not p then local g = grid() POSE[name](g, arg) local t, m = {}, {} for y = 0, DH - 1 do local r = string.char(table.unpack(g, y * DW + 1, y * DW + DW)) t[y + 1], m[y + 1] = r, r:reverse() end p = { table.concat(t), table.concat(m) } CACHE[k] = p end return p end local COATS = { [0] = { { 240, 220, 186 }, { 204, 178, 144 }, { 176, 104, 54 }, { 128, 70, 38 } }, [1] = { { 178, 114, 64 }, { 132, 82, 46 }, { 96, 56, 34 }, { 62, 36, 24 } }, [2] = { { 248, 244, 236 }, { 204, 202, 198 }, { 52, 46, 48 }, { 30, 26, 28 } }, } local WINGS = { [0] = { 255, 148, 40 }, [1] = { 90, 168, 255 }, [2] = { 255, 228, 70 } } local PAL = {} function on_settings() local c = COATS[cfg.coat or 0] or COATS[0] PAL = { 0, 0, 0 } for i = 1, 4 do PAL[#PAL + 1], PAL[#PAL + 2], PAL[#PAL + 3] = c[i][1], c[i][2], c[i][3] end for _, v in ipairs({ 22, 20, 22, 255, 160, 170, 255, 252, 244, 214, 44, 52 }) do PAL[#PAL + 1] = v end end local BG local function fbox(x, y, w, h, r, g, b) local ix = floor(x) local f = x - ix rect(ix + 1, y, w - 1, h, r, g, b) blend_rect(ix, y, 1, h, r, g, b, 1 - f) blend_rect(ix + w, y, 1, h, r, g, b, f) end local function fb(x, y, w, h, r, g, b, a) local ix = floor(x) local f = x - ix blend_rect(ix + 1, y, w - 1, h, r, g, b, a) blend_rect(ix, y, 1, h, r, g, b, a * (1 - f)) blend_rect(ix + w, y, 1, h, r, g, b, a * f) end local function build_bg() for y = 0, 41 do local t = y / 41 rect(0, y, W, 1, floor(112 + 78 * t), floor(186 + 42 * t), floor(255 - 4 * t)) end for x = 0, W - 1 do local top = floor(35 + 2.5 * sin(x * 0.12) + 1.5 * sin(x * 0.33 + 1)) rect(x, top, 1, 20, 128, 190, 140) end rect(8, 28, 4, 17, 112, 78, 52); rect(8, 28, 1, 17, 134, 96, 64) for _, d in ipairs({ { 10, 18, 9 }, { 4, 22, 6 }, { 16, 21, 6 }, { 10, 12, 6 } }) do for y = d[2] - d[3], d[2] + d[3] do local hw = sqrt(max(0, d[3] * d[3] - (y + 0.5 - d[2]) ^ 2)) rect(floor(d[1] - hw + 0.5), y, floor(2 * hw + 0.5), 1, 64, 150, 78) end end for y = 12, 24 do rect(6 + (y % 5), y, 3, 1, 96, 178, 92) end rect(0, 40, W, 2, 214, 204, 184); rect(0, 46, W, 1, 214, 204, 184) for x = 1, W - 1, 5 do rect(x, 38, 3, 9, 242, 234, 216); rect(x + 2, 38, 1, 9, 206, 196, 176) rect(x, 37, 3, 1, 242, 234, 216) end for y = 47, H - 1 do local t = (y - 47) / 16 rect(0, y, W, 1, floor(100 - 34 * t), floor(180 - 40 * t), floor(76 - 18 * t)) end for x = 0, W - 1, 16 do rect(x, 47, 8, 17, 108, 190, 82); end for i = 1, 40 do local h = hash32(i * 0x9E3779B1) local x, y = umod(h, W), 49 + umod(h >> 8, 14) px(x, y, 52, 120, 48); px(x, y - 1, 52, 120, 48) end for i = 1, 8 do local h = hash32(i * 0x85EBCA6B) local x, y = umod(h, 60) + 2, 50 + umod(h >> 8, 12) px(x, y, 255, 252, 240); px(x - 1, y, 255, 252, 240); px(x + 1, y, 255, 252, 240); px(x, y - 1, 255, 252, 240); px(x, y + 1, 255, 252, 240) px(x, y, 255, 214, 60) end BG = gfx.snapshot(0, 0, W, H) end local FLY = { { 6.0, 72, 20 }, { 8.6, 47, 23 }, { 9.4, 48, 21 }, { 10.0, 53, 15 }, { 10.8, 56, 10 }, { 12.4, 76, 2 }, } local function butterfly(u, f) if u < FLY[1][1] or u > FLY[#FLY][1] then return end local i = 1 while FLY[i + 1][1] < u do i = i + 1 end local a, b = FLY[i], FLY[i + 1] local t = (u - a[1]) / (b[1] - a[1]) local x = a[2] + (b[2] - a[2]) * t + 0.9 * sin(u * 9) local y = a[3] + (b[3] - a[3]) * t + 1.5 * sin(u * 13) local c = WINGS[cfg.wing or 0] or WINGS[0] local iy = floor(y + 0.5) if (f // 3) % 2 == 0 then fbox(x - 2, iy - 1, 2, 2, c[1], c[2], c[3]); fbox(x + 1, iy - 1, 2, 2, c[1], c[2], c[3]) fbox(x - 1, iy + 1, 1, 1, c[1], c[2], c[3]); fbox(x + 1, iy + 1, 1, 1, c[1], c[2], c[3]) else fbox(x - 1, iy - 1, 1, 3, c[1], c[2], c[3]); fbox(x + 1, iy - 1, 1, 3, c[1], c[2], c[3]) end fbox(x, iy - 1, 1, 3, 40, 26, 26) end local GY = 56 function render() if not BG then build_bg() end gfx.blit_rgb(BG, 0, 0, W, H) local f = frame % PERIOD local u = f / FPS for i = 0, 1 do local cx = (frame * (0.05 + 0.02 * i) / 1 + i * 40) % 90 - 18 fb(cx + 3, 5 + i * 9, 10, 1, 255, 255, 255, 0.8) fb(cx + 2, 6 + i * 9, 13, 2, 255, 255, 255, 0.8) end local name, arg, mir, x, dy = "stand", false, false, 32, 0 if u < 5 then local k = (f // 5) % 4 name, mir = ({ "spin", "front", "spin", "back" })[k + 1], k == 2 elseif u < 6 then name, arg = "stand", true elseif u < 9 then name = "look" elseif u < 9.7 then name = (f // 4) % 2 == 0 and "runA" or "runB" x = 32 + 20 * (u - 9) / 0.7 elseif u < 10.3 then local t = (u - 9.7) / 0.6 name, x, dy = "runA", 52 + 4 * t, -floor(7 * 4 * t * (1 - t) + 0.5) elseif u < 12.5 then name, x = "look", 56 elseif u < 13.4 then name, mir = (f // 4) % 2 == 0 and "walkA" or "walkB", true x = 56 - 24 * (u - 12.5) / 0.9 elseif u < 14 then mir = true elseif u < 20 then name, arg, mir = "sit", (f // 5) % 2 == 1, true else name, arg, mir = "stand", true, true end x = floor(x + 0.5) local sh = 1 + (dy < 0 and -dy // 3 or 0) blend_rect(x - 8 + sh, GY + 1, 16 - 2 * sh, 2, 28, 70, 30, 0.32) local p = pose(name, arg) blit(p[mir and 2 or 1], PAL, x - 12, GY - 19 + dy, DW, DH, DW, DH, true) butterfly(u, f) end