info = { type = "image", title = "Piglet munching", category = "animals", author = "BRRRO", version = "1.0", license = "MIT", desc = "A piglet chews a carrot down and giggles", api = 33, } local tr = ({ sk = { ["Skin colour"] = "Farba pokožky", ["Chewing"] = "Žuvanie", ["slow"] = "pomaly", ["normal"] = "normálne", ["fast"] = "rýchlo" }, cs = { ["Skin colour"] = "Barva pokožky", ["Chewing"] = "Žvýkání", ["slow"] = "pomalu", ["normal"] = "normálně", ["fast"] = "rychle" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "skin", label = T("Skin colour"), type = "color", default = "#FFB0C4" }, { key = "chew", label = T("Chewing"), type = "int", default = 1, opts = { T("slow"), T("normal"), T("fast") } }, } 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 function quad(x1, y1, x2, y2, x3, y3, x4, y4, c) local xs, ys = { x1, x2, x3, x4 }, { y1, y2, y3, y4 } local lo, hi = y1, y1 for i = 2, 4 do if ys[i] < lo then lo = ys[i] end; if ys[i] > hi then hi = ys[i] end end for y = floor(lo), floor(hi) do local yy = y + 0.5 local a, b = 1e9, -1e9 for i = 1, 4 do local j = i % 4 + 1 local ya, yb = ys[i], ys[j] if (ya <= yy and yy < yb) or (yb <= yy and yy < ya) then local x = xs[i] + (yy - ya) * (xs[j] - xs[i]) / (yb - ya) if x < a then a = x end if x > b then b = x end end end if b > a then local x0 = floor(a + 0.5) rect(x0, y, floor(b + 0.5) - x0, 1, c[1], c[2], c[3]) end end end local SKY, GRASS, GRASS2 = { 160, 206, 255 }, { 126, 206, 120 }, { 84, 170, 90 } local SKIN, EAR_IN, SNOUT = { 255, 176, 196 }, { 255, 130, 164 }, { 255, 214, 224 } local OUT = { 214, 110, 146 } local EYE, NOSTRIL, MOUTH, TONGUE = { 44, 36, 60 }, { 210, 100, 134 }, { 120, 36, 70 }, { 255, 120, 150 } local HEART = { 255, 120, 164 } local CAR, CAR_D, LEAF = { 255, 136, 36 }, { 214, 92, 20 }, { 70, 196, 90 } local BOW = { 190, 150, 255 } local BG, HB = nil, nil function on_settings() local c = cfg.skin or { r = 255, g = 176, b = 196 } SKIN = { c.r, c.g, c.b } EAR_IN = { c.r, c.g * 74 // 100, c.b * 82 // 100 } SNOUT = { c.r, c.g + (255 - c.g) * 4 // 10, c.b + (255 - c.b) * 4 // 10 } OUT = { c.r * 84 // 100, c.g * 62 // 100, c.b * 74 // 100 } BG = nil end local function build_bg() rect(0, 0, W, 40, SKY[1], SKY[2], SKY[3]) rect(0, 40, W, 12, GRASS[1], GRASS[2], GRASS[3]); rect(0, 52, W, 12, GRASS2[1], GRASS2[2], GRASS2[3]) for x = 0, W - 1, 2 do px(x, 39, GRASS[1], GRASS[2], GRASS[3]); px(x + 1, 51, GRASS2[1], GRASS2[2], GRASS2[3]) end for i = 1, 10 do local h = hash32(i * 0x9E3779B1) local x, y = umod(h, W), 42 + umod(h >> 8, 20) px(x, y, 255, 236, 80); px(x + 1, y, 255, 236, 80); px(x, y + 1, 255, 236, 80); px(x + 1, y + 1, 255, 190, 40) end for i = 1, 6 do local h = hash32(i * 0x85EBCA6B) local x, y = umod(h, W), umod(h >> 8, 10) rect(x, y, 6, 1, 255, 255, 255); rect(x + 1, y - 1, 4, 1, 255, 255, 255) end BG = gfx.snapshot(0, 0, W, H) rect(-1, -1, W + 2, H + 2, 255, 0, 255) local cx, cy = 32, 31 ell(cx, cy, 23.5, 19.5, OUT) ell(cx, cy, 22.5, 18.5, SKIN) rect(cx - 5, cy - 18, 3, 1, EAR_IN[1], EAR_IN[2], EAR_IN[3]) local hx = { cx - 20, cx + 14 } for i = 1, 2 do local x, y = hx[i], cy + 2 rect(x, y, 2, 2, HEART[1], HEART[2], HEART[3]); rect(x + 4, y, 2, 2, HEART[1], HEART[2], HEART[3]) rect(x, y + 1, 6, 2, HEART[1], HEART[2], HEART[3]); rect(x + 1, y + 3, 4, 1, HEART[1], HEART[2], HEART[3]) rect(x + 2, y + 4, 2, 1, HEART[1], HEART[2], HEART[3]) end HB = gfx.snapshot(0, 0, W, H) end 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 LMAX = 22 local function carrot_len(u) if u < 20 then return (LMAX * u) // 20 end local i = (u - 20) // 40 if i >= 4 then return 0 end local r = (u - 20) % 40 local to = LMAX - (i + 1) * 5 local from = LMAX - i * 5 if r < 5 then return from - (r * (from - to)) // 5 end return to 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 % 240 local ch = ({ 8, 6, 4 })[(cfg.chew or 1) + 1] local L = carrot_len(u) local eating = u >= 20 and u < 180 local happy = u >= 182 and u < 226 local r = (u - 20) % 40 local open = false if u < 20 then open = true elseif eating then open = r < 6 or (r // ch) % 2 == 0 elseif happy then open = (u // 6) % 2 == 0 and u < 214 end local wide = eating and r < 6 or (u < 20) local bump = (eating and not open) and 1 or 0 local cx, cy = 32, 31 + bump local flap = 0 if eating then flap = (((u // ch) % 2 == 0) and 0.12 or -0.1) elseif happy then flap = ((u // 3) % 2 == 0) and 0.18 or -0.18 end local lean = 0.35 + 0.1 * sin(f * 0.07) rell(cx - 22, cy - 13, 12, 7, PI + 0.62 + lean + flap, OUT) rell(cx + 22, cy - 13, 12, 7, -0.62 - lean - flap, OUT) rell(cx - 22, cy - 13, 10.6, 5.8, PI + 0.62 + lean + flap, SKIN) rell(cx + 22, cy - 13, 10.6, 5.8, -0.62 - lean - flap, SKIN) rell(cx - 22, cy - 12, 6, 3, PI + 0.62 + lean + flap, EAR_IN) rell(cx + 22, cy - 12, 6, 3, -0.62 - lean - flap, EAR_IN) gfx.blit_rgb(HB, 0, bump, W, H, 255, 0, 255) local sy = open and 0 or 1 local sw = (open or (u // 4) % 2 == 0) and 9 or 8.2 ell(cx, cy + 3 - sy, sw + 0.8, 6.6, OUT) ell(cx, cy + 3 - sy, sw, 5.8, SNOUT) local nw = open and 2 or 1 rect(cx - 4, cy + 1 - sy, nw, 3, NOSTRIL[1], NOSTRIL[2], NOSTRIL[3]) rect(cx + 3 - (nw - 1), cy + 1 - sy, nw, 3, NOSTRIL[1], NOSTRIL[2], NOSTRIL[3]) local blink = (f % 117) < 3 local ey = cy - 8 if happy then for _, x in ipairs({ cx - 13, cx + 10 }) do rect(x, ey + 2, 3, 1, EYE[1], EYE[2], EYE[3]); rect(x + 1, ey + 1, 3, 1, EYE[1], EYE[2], EYE[3]); rect(x + 3, ey + 2, 2, 1, EYE[1], EYE[2], EYE[3]) end elseif blink then rect(cx - 14, ey + 3, 7, 1, EYE[1], EYE[2], EYE[3]); rect(cx + 8, ey + 3, 7, 1, EYE[1], EYE[2], EYE[3]) else ell(cx - 11, ey + 2, 3.6, 5.2, EYE); ell(cx + 12, ey + 2, 3.6, 5.2, EYE) rect(cx - 12, ey - 1, 2, 2, 255, 255, 255); rect(cx + 11, ey - 1, 2, 2, 255, 255, 255) px(cx - 10, ey + 4, 190, 190, 220); px(cx + 13, ey + 4, 190, 190, 220) px(cx - 15, ey - 3, EYE[1], EYE[2], EYE[3]); px(cx - 14, ey - 4, EYE[1], EYE[2], EYE[3]) px(cx + 16, ey - 3, EYE[1], EYE[2], EYE[3]); px(cx + 15, ey - 4, EYE[1], EYE[2], EYE[3]) end ell(cx - 18, cy - 20, 3.2, 2.3, BOW); ell(cx - 12, cy - 21, 3, 2.3, BOW); rect(cx - 16, cy - 21, 3, 2, 150, 110, 230) local my = cy + 13 if open then local oh = wide and 3.4 or 2.2 ell(cx, my + 1, 4.4, oh, MOUTH) if wide then rect(cx - 2, my + 2, 4, 1, TONGUE[1], TONGUE[2], TONGUE[3]) end else rect(cx - 3, my, 6, 1, MOUTH[1], MOUTH[2], MOUTH[3]) px(cx - 4, my - 1, MOUTH[1], MOUTH[2], MOUTH[3]); px(cx + 3, my - 1, MOUTH[1], MOUTH[2], MOUTH[3]) end if L > 0 then local ang = 0.95 local dx, dy = cos(ang), sin(ang) local mx, mmy = cx + 1.5, my + 1 local fx, fy = mx + L * dx, mmy + L * dy local nx, ny = -dy, dx local w = 3.4 local slide = u < 20 and (20 - u) * 0.6 or 0 fx, fy = fx + slide, fy + slide quad(mx + nx * 0.9, mmy + ny * 0.9, mx - nx * 0.9, mmy - ny * 0.9, fx - nx * w, fy - ny * w, fx + nx * w, fy + ny * w, CAR) quad(mx + nx * 0.3, mmy + ny * 0.3, mx - nx * 0.9, mmy - ny * 0.9, fx - nx * w, fy - ny * w, fx - nx * 0.9, fy - ny * 0.9, CAR_D) rell(fx + dx * 3.2, fy + dy * 3.2, 4.6, 1.8, ang - 0.7, LEAF) rell(fx + dx * 3.6, fy + dy * 3.6, 5, 1.8, ang, LEAF) rell(fx + dx * 3.2, fy + dy * 3.2, 4.6, 1.8, ang + 0.7, LEAF) end if eating and r < 14 then local x0, y0 = cx + 1, my + 3 for k = 1, 3 do local x = x0 + (k - 2) * 3 + (k - 2) * r // 4 local y = y0 + r + k px(x, y, CAR[1], CAR[2], CAR[3]) end end end