info = { type = "image", title = "Sleepy koala", category = "animals", author = "BRRRO", version = "1.0", license = "MIT", desc = "A koala naps on a eucalyptus, wakes for a leaf", api = 33, } local tr = ({ sk = { ["Baby"] = "Mláďa" }, cs = { ["Baby"] = "Mládě" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "baby", label = T("Baby"), type = "bool", default = true }, } local rect, blend, blend_rect = gfx.rect, gfx.blend, gfx.blend_rect local floor, sin, sqrt, abs, hash32, umod = math.floor, math.sin, math.sqrt, math.abs, math.hash32, math.umod local W, H = gfx.W, gfx.H local CYC = 720 local FUR_D, FUR, FUR_L = { 92, 94, 114 }, { 150, 152, 170 }, { 198, 200, 214 } local BELLY, EAR_IN = { 224, 226, 234 }, { 228, 170, 184 } local NOSE, EYE, PAW = { 30, 26, 34 }, { 28, 24, 36 }, { 78, 80, 100 } local LEAF, LEAF_D = { 150, 184, 156 }, { 96, 134, 116 } local function E(cx, cy, rx, ry, c, a) a = a or 1 for y = floor(cy - ry - 0.5), floor(cy + ry + 0.5) do local dy = y + 0.5 - cy local va = ry + 0.5 - abs(dy) if va > 0 then if va > 1 then va = 1 end local d = dy / ry if d > 1 then d = 1 elseif d < -1 then d = -1 end local hw = rx * sqrt(1 - d * d) if hw < 0.5 then hw = 0.5 end local l, r = cx - hw, cx + hw local xl, xr = floor(l), floor(r) va = va * a if xl == xr then blend(xl, y, c[1], c[2], c[3], (r - l) * va) else blend(xl, y, c[1], c[2], c[3], (xl + 1 - l) * va) blend(xr, y, c[1], c[2], c[3], (r - xr) * va) if xr - xl > 1 then if va > 0.98 then rect(xl + 1, y, xr - xl - 1, 1, c[1], c[2], c[3]) else blend_rect(xl + 1, y, xr - xl - 1, 1, c[1], c[2], c[3], va) end end end end end end local function limb(x1, y1, x2, y2, r, c) local n = floor(sqrt((x2 - x1) ^ 2 + (y2 - y1) ^ 2) / 1.4) + 1 for i = 0, n do local t = i / n E(x1 + (x2 - x1) * t, y1 + (y2 - y1) * t, r, r, c) end end local function line(x1, y1, x2, y2, c, a) local n = floor(sqrt((x2 - x1) ^ 2 + (y2 - y1) ^ 2)) + 1 for i = 0, n do local t = i / n local x, y = x1 + (x2 - x1) * t, y1 + (y2 - y1) * t local ix, iy = floor(x), floor(y) local fx, fy = x - ix, y - iy blend(ix, iy, c[1], c[2], c[3], a * (1 - fx) * (1 - fy)) blend(ix + 1, iy, c[1], c[2], c[3], a * fx * (1 - fy)) blend(ix, iy + 1, c[1], c[2], c[3], a * (1 - fx) * fy) blend(ix + 1, iy + 1, c[1], c[2], c[3], a * fx * fy) end end local BG local TRUNK = { { 214, 208, 192 }, { 188, 182, 166 }, { 162, 154, 140 }, { 136, 128, 118 }, { 108, 102, 98 } } local function build_bg() for y = 0, H - 1 do local t = y / 56 if t > 1 then t = 1 end rect(0, y, W, 1, floor(62 + 76 * t), floor(74 + 64 * t), floor(126 + 52 * t)) end for _, m in ipairs({ { 57, 9, 3 }, { 57, 9, 8 }, { 57, 9, 6 }, { 57, 9, 4 } }) do for dy = -m[3], m[3] do local hw = floor(sqrt(m[3] * m[3] - dy * dy) + 0.5) if m[3] == 3 then rect(m[1] - hw, m[2] + dy, 2 * hw + 1, 1, 250, 248, 226) else blend_rect(m[1] - hw, m[2] + dy, 2 * hw + 1, 1, 230, 232, 250, 0.1) end end end for x = 0, W - 1 do rect(x, 51 - floor(2 * sin(x * 0.12 + 1) + 2 * sin(x * 0.29)), 1, 20, 84, 98, 146) rect(x, 57 - floor(2.5 * sin(x * 0.09 + 2) + sin(x * 0.4)), 1, 20, 46, 66, 98) end for y = 0, H - 1 do local w = 12 - (y < 20 and (20 - y) // 5 or 0) local x0 = 40 + (12 - w) // 2 for k = 0, w - 1 do local c = TRUNK[1 + k * 5 // w] rect(x0 + k, y, 1, 1, c[1], c[2], c[3]) end end for i = 1, 26 do local h = hash32(i * 0x9E3779B1) local x, y, l = 41 + umod(h, 10), umod(h >> 6, 60), 3 + umod(h >> 12, 6) if umod(h >> 18, 3) == 0 then rect(x, y, 2, l, 224, 220, 204) else rect(x, y, 1, l, 118, 110, 100) end end for _, b in ipairs({ { 12, 44, 40, 49 }, { 14, 5, 41, 9 } }) do local x0, y0, x1, y1 = b[1], b[2], b[3], b[4] for x = x0, x1 do local t = (x - x0) / (x1 - x0) local top = floor(y0 + (y1 - 3 - y0) * t + 0.5) local th = 2 + floor(3 * t + 0.5) for k = 0, th - 1 do local c = TRUNK[2 + (k * 3) // th] rect(x, top + k, 1, 1, c[1], c[2], c[3]) end end end BG = gfx.snapshot(0, 0, W, H) end local BUNCH = {} do local anchors = { { 18, 9 }, { 23, 9 }, { 28, 10 }, { 33, 10 }, { 37, 11 }, { 14, 8 } } for i = 1, #anchors do BUNCH[i] = {} for k = 1, 3 do local h = hash32((i * 7 + k) * 0x85EBCA6B) BUNCH[i][k] = { x = anchors[i][1] + (k - 2) * 1.2, y = anchors[i][2], dx = (umod(h, 5) - 2) * 0.8, len = 5 + umod(h >> 8, 3), ph = umod(h >> 12, 628) / 100, } end end BUNCH[4][2].pick = true end local function leaf(l, f, scale) local len = floor(l.len * scale + 0.5) if len < 2 then return end local tx = l.x + l.dx + 1.2 * sin(f * 0.04 + l.ph) rect(floor(l.x + l.dx * 0.3 + 0.5), l.y, 1, len // 2, LEAF_D[1], LEAF_D[2], LEAF_D[3]) local ix = floor(tx) local fx = tx - ix for y = l.y + len // 2, l.y + len - 1 do blend(ix, y, LEAF[1], LEAF[2], LEAF[3], 1 - fx) blend(ix + 1, y, LEAF[1], LEAF[2], LEAF[3], fx) end end local CX, CY, CW, CH = 14, 12, 30, 38 local NPH = 4 local BREATH = { -0.5, -0.17, 0.17, 0.5 } local COMP = { {}, {} } local stage = 0 local TMP local function bake(state, ph, step) local br = BREATH[ph] local hx, hy = 28, 22 + br if state == 1 then hx, hy = hx + 1.3, hy + 1.6 end gfx.blit_rgb(BG, 0, 0, W, H) if step > 1 then gfx.blit_rgb(TMP, CX, CY, CW, CH) end local by = br * 0.6 if step == 1 then if cfg.baby then local bb = 0.4 * br E(19.5, 34 + bb, 3.4, 4.6, FUR_D); E(19.9, 33.5 + bb, 2.8, 4.0, FUR) E(19, 29.8 + bb, 3.2, 2.9, FUR_D); E(19.3, 29.5 + bb, 2.7, 2.5, FUR_L) E(16.4, 27.6 + bb, 1.8, 1.8, FUR_D); E(21.8, 27.6 + bb, 1.8, 1.8, FUR_D) E(16.5, 27.8 + bb, 0.9, 0.9, EAR_IN); E(21.7, 27.8 + bb, 0.9, 0.9, EAR_IN) rect(17, 30, 2, 1, EYE[1], EYE[2], EYE[3]); rect(20, 30, 2, 1, EYE[1], EYE[2], EYE[3]) E(19.8, 31.4 + bb, 1.0, 0.8, NOSE) end elseif step == 2 then E(29, 37 + by, 9.2, 10.5, FUR_D) E(29.8, 36.4 + by, 8.2, 9.6, FUR) E(30.4, 38 + by, 5.2, 7, BELLY) E(24, 46.2, 3.2, 1.8, PAW); E(34, 46.2, 3.2, 1.8, PAW) elseif step == 3 then if not cfg.baby then limb(23, 31 + by, 25, 39 + by, 2.4, FUR_L) end limb(35, 36 + by, 42, 38.5 + by, 2.5, FUR) rect(42, 37 + floor(by + 0.5), 2, 1, 230, 226, 220); rect(42, 40 + floor(by + 0.5), 2, 1, 230, 226, 220) else E(hx - 6.8, hy - 5, 4.4, 4.4, FUR_D); E(hx + 6.8, hy - 5, 4.4, 4.4, FUR_D) E(hx - 6.6, hy - 4.6, 2.5, 2.5, EAR_IN); E(hx + 6.6, hy - 4.6, 2.5, 2.5, EAR_IN) E(hx, hy, 8.4, 6.8, FUR_D) E(hx + 0.5, hy - 0.5, 7.6, 6.1, FUR) E(hx + 1.2, hy - 1.5, 5, 3.4, FUR_L, 0.7) local ey = floor(hy - 0.3 + 0.5) for s = -1, 1, 2 do local ex = floor(hx + s * 3.6 + 0.5) if state == 2 then rect(ex - 1, ey, 2, 2, EYE[1], EYE[2], EYE[3]); blend(ex, ey, 255, 255, 255, 0.8) else rect(ex - 1, ey + 1, 3, 1, EYE[1], EYE[2], EYE[3]) end end E(hx, hy + 1.6, 2.4, 3.2, NOSE); blend(floor(hx - 0.5), floor(hy + 0.3), 255, 255, 255, 0.35) end local snap = gfx.snapshot(CX, CY, CW, CH) if step == 4 then COMP[state][ph] = snap else TMP = snap end end local function ease(t) if t < 0 then return 0 elseif t > 1 then return 1 end return t * t * (3 - 2 * t) end local function koala(f, t) local awake = t >= 330 and t < 590 local state = awake and 2 or 1 local ph = floor((sin(f * 2 * math.pi / 150) + 1) * 0.5 * NPH) if ph >= NPH then ph = NPH - 1 end ph = ph + 1 local c = COMP[state][ph] if c then gfx.blit_rgb(c, CX, CY, CW, CH) end local br = BREATH[ph] local hx, hy = 28, 22 + br if state == 1 then hx, hy = hx + 1.3, hy + 1.6 end if awake and t > 450 and t < 570 and (t // 8) % 2 == 0 then E(hx, hy + 5.6, 1.8, 1.1, { 196, 110, 124 }) end local by = br * 0.6 local ax, ay = 41.5, 33 + by if t >= 380 and t < 420 then local u = ease((t - 380) / 40) ax, ay = 41.5 + (35.4 - 41.5) * u, 33 + (16.5 - 33) * u elseif t >= 420 and t < 450 then local u = ease((t - 420) / 30) ax, ay = 35.4 + (hx + 3 - 35.4) * u, 16.5 + (hy + 4.5 - 16.5) * u elseif t >= 450 and t < 575 then ax, ay = hx + 3, hy + 4.5 elseif t >= 575 and t < 610 then local u = ease((t - 575) / 35) ax, ay = hx + 3 + (41.5 - hx - 3) * u, hy + 4.5 + (33 - hy - 4.5) * u end limb(35, 30.5 + by, ax, ay, 2.5, FUR_L) E(ax, ay, 2.1, 2.1, PAW) if t < 380 or t >= 610 then rect(floor(ax) + 1, floor(ay) - 1, 2, 1, 230, 226, 220); rect(floor(ax) + 1, floor(ay) + 2, 2, 1, 230, 226, 220) end if t >= 420 and t < 575 then local L = t > 450 and 7 * (1 - (t - 450) / 125) or 7 if L > 0.6 then local lx, ly = ax + 0.5, ay - 0.5 line(lx, ly, lx + L * 0.7, ly - L * 0.7, LEAF, 0.95) end end end function on_settings() COMP = { {}, {} } stage = 0 end function render() local fresh = not BG if fresh then build_bg() end if not fresh and stage < 8 * NPH then stage = stage + 1 local k = (stage - 1) // 4 bake(k // NPH + 1, k % NPH + 1, (stage - 1) % 4 + 1) end gfx.blit_rgb(BG, 0, 0, W, H) local f = frame local t = f % CYC for i = 1, 6 do local h = hash32(i * 0xC2B2AE35) local a = 0.5 + 0.5 * sin(f * 0.05 + i * 1.9) blend(umod(h, 36), umod(h >> 8, 10), 255, 255, 240, 0.25 + 0.5 * a) end koala(f, t) local regrow = ease((t - 610) / 100) for i = 1, #BUNCH do for k = 1, 3 do local l = BUNCH[i][k] if l.pick then if t < 420 then leaf(l, f, 1) elseif t >= 610 then leaf(l, f, regrow) end else leaf(l, f, 1) end end end end