info = { type = "image", title = "Burrow bunny", category = "animals", author = "BRRRO", version = "1.0", license = "MIT", desc = "A bunny pops out of three autumn burrows", api = 33, } local ltr = ({ sk = { ["Speed"] = "Rýchlosť", ["Leaves"] = "Lístie", ["Leaps"] = "Skoky" }, cs = { ["Speed"] = "Rychlost", ["Leaves"] = "Listí", ["Leaps"] = "Skoky" }, })[lang] or {} local function LT(s) return ltr[s] or s end settings = { { key = "speed", label = LT("Speed"), type = "int", min = 1, max = 3, default = 2 }, { key = "leaves", label = LT("Leaves"), type = "int", min = 0, max = 10, default = 6 }, { key = "hops", label = LT("Leaps"), type = "bool", default = true }, } local rect, px, blend, blend_rect = gfx.rect, gfx.px, gfx.blend, gfx.blend_rect local blit_rgb, snapshot = gfx.blit_rgb, gfx.snapshot local floor, sin, sqrt, min, max = math.floor, math.sin, math.sqrt, math.min, math.max local hash32, umod = math.hash32, math.umod local W, H = gfx.W, gfx.H local function rnd(v) return floor(v + 0.5) end local BUR = { { x = 13, y = 41 }, { x = 47, y = 44 }, { x = 29, y = 57 } } local function disc(cx, cy, r, cr, cg, cb) for dy = -r, r do local hw = rnd(sqrt(r * r + 0.5 - dy * dy)) rect(cx - hw, cy + dy, 2 * hw + 1, 1, cr, cg, cb) end end local function mound(b) for dy = -6, 5 do local hw = rnd(11 * sqrt(1 - ((dy + 0.5) / 6.5) ^ 2)) local lit = dy < -3 and 1 or (dy < 2 and 2 or 3) if lit == 1 then rect(b.x - hw, b.y + dy, 2 * hw, 1, 206, 132, 60) elseif lit == 2 then rect(b.x - hw, b.y + dy, 2 * hw, 1, 172, 104, 46) else rect(b.x - hw, b.y + dy, 2 * hw, 1, 140, 82, 38) end end local HW = { 4, 6, 6, 6, 4 } for i = 1, 5 do rect(b.x - HW[i], b.y + i - 3, 2 * HW[i], 1, 44, 22, 12) end rect(b.x - 6, b.y + 3, 12, 1, 120, 70, 34) end local BG = nil local function bake_bg() for y = 0, 31 do local t = y / 31 rect(0, y, W, 1, rnd(232 + 23 * t), rnd(142 + 72 * t), rnd(92 + 48 * t)) end disc(47, 17, 6, 255, 234, 172) for x = 0, W - 1 do local t1 = 28 + rnd(3 * sin(x * 0.1) + 1.5 * sin(x * 0.27)) rect(x, t1, 1, 12, 196, 104, 64) local t2 = 33 + rnd(2 * sin(x * 0.12 + 2)) rect(x, t2, 1, 8, 214, 148, 60) end local CROWN = { { 228, 118, 40 }, { 190, 48, 36 }, { 244, 184, 52 } } for i = 0, 8 do local c = CROWN[i % 3 + 1] local cx = 4 + i * 7 + umod(hash32(i * 977), 3) disc(cx, 27 + umod(hash32(i * 131), 3), 3, c[1], c[2], c[3]) rect(cx, 30 + umod(hash32(i * 131), 3), 1, 2, 96, 52, 30) end for y = 36, H - 1 do local t = (y - 36) / 27 rect(0, y, W, 1, rnd(212 - 42 * t), rnd(150 - 50 * t), rnd(54 - 20 * t)) end for i = 1, 90 do local h = hash32(i * 0x9E3779B1) local x, y = umod(h, W), 38 + umod(h >> 8, 25) local light = (h >> 20) & 1 == 1 local r, g, b if light then r, g, b = 238, 184, 78 else r, g, b = 146, 86, 28 end px(x, y, r, g, b); px(x - 1, y - 1, r, g, b); px(x + 1, y - 1, r, g, b) end for dy = -4, 4 do local hw = rnd(6 * sqrt(1 - (dy / 4.6) ^ 2)) rect(56 - hw, 58 + dy, 2 * hw, 1, 238, 110, 24) end rect(53, 54, 1, 9, 190, 70, 16); rect(59, 54, 1, 9, 190, 70, 16) rect(56, 53, 1, 2, 70, 104, 34) for i = 1, 3 do mound(BUR[i]) end BG = snapshot(0, 0, W, H) end local FUR, SHADE, PINK, PAW = { 255, 250, 242 }, { 232, 204, 190 }, { 244, 150, 150 }, { 255, 212, 200 } local function bunny(bx, yt, wig, twitch, blink, look) local function ear(ex, w) rect(ex, yt + 4, 3, 4, FUR[1], FUR[2], FUR[3]) rect(ex + w, yt + 1, 3, 3, FUR[1], FUR[2], FUR[3]) rect(ex + w + 1, yt, 1, 1, FUR[1], FUR[2], FUR[3]) rect(ex + 1 + w, yt + 1, 1, 3, PINK[1], PINK[2], PINK[3]) rect(ex + 1, yt + 4, 1, 3, PINK[1], PINK[2], PINK[3]) end ear(bx - 5, wig) ear(bx + 2, -wig) rect(bx - 4, yt + 19, 2, 4, FUR[1], FUR[2], FUR[3]); rect(bx + 2, yt + 19, 2, 4, FUR[1], FUR[2], FUR[3]) rect(bx - 5, yt + 22, 3, 1, SHADE[1], SHADE[2], SHADE[3]); rect(bx + 2, yt + 22, 3, 1, SHADE[1], SHADE[2], SHADE[3]) rect(bx - 4, yt + 15, 8, 5, FUR[1], FUR[2], FUR[3]) rect(bx - 4, yt + 19, 8, 1, SHADE[1], SHADE[2], SHADE[3]) rect(bx - 3, yt + 16, 2, 1, PAW[1], PAW[2], PAW[3]); rect(bx + 1, yt + 16, 2, 1, PAW[1], PAW[2], PAW[3]) rect(bx - 3, yt + 8, 6, 1, FUR[1], FUR[2], FUR[3]) rect(bx - 4, yt + 9, 8, 1, FUR[1], FUR[2], FUR[3]) rect(bx - 5, yt + 10, 10, 4, FUR[1], FUR[2], FUR[3]) rect(bx - 4, yt + 14, 8, 1, SHADE[1], SHADE[2], SHADE[3]) rect(bx + 4, yt + 10, 1, 4, SHADE[1], SHADE[2], SHADE[3]) px(bx - 4, yt + 12, 255, 188, 172); px(bx + 3, yt + 12, 255, 188, 172) local eh = blink and 1 or 2 rect(bx - 3 + look, yt + 11, 1, eh, 36, 22, 26) rect(bx + 2 + look, yt + 11, 1, eh, 36, 22, 26) if twitch then rect(bx - 1, yt + 11, 2, 1, 232, 112, 124) px(bx - 7, yt + 12, 240, 226, 212); px(bx + 6, yt + 12, 240, 226, 212) else rect(bx - 1, yt + 12, 2, 1, 232, 112, 124) end px(bx - 2, yt + 13, 150, 84, 90); px(bx + 1, yt + 13, 150, 84, 90) end local CYC = 560 local SF = { 0.7, 1, 1.4 } local PK_HOP = { { 1, 0, 112, true, true }, { 3, 130, 232, true, true }, { 2, 250, 330, true, false }, { 3, 348, 420, false, true }, { 1, 440, 542, true, true } } local PK_STAY = { { 1, 0, 112, true, true }, { 3, 130, 232, true, true }, { 2, 250, 340, true, true }, { 3, 356, 420, true, true }, { 1, 440, 542, true, true } } local RISE, DUCK, TOP = 14, 12, 15 local function ease(e) return e * e * (3 - 2 * e) end local function draw_bunny(t, hops) local pk = hops and PK_HOP or PK_STAY for i = 1, #pk do local p = pk[i] if t >= p[2] and t < p[3] then local b = BUR[p[1]] local u, dur = t - p[2], p[3] - p[2] local yt = b.y - TOP if p[4] and u < RISE then yt = b.y + 2 - (TOP + 2) * ease(u / RISE) elseif p[5] and u > dur - DUCK then yt = b.y - TOP + (TOP + 2) * ease((u - dur + DUCK) / DUCK) end local a = u - RISE local wig = (a > 24 and a < 62) and floor(sin(a * 0.7) * 1.2) or 0 local twitch = a > 4 and (a // 4) % 2 == 1 and a % 44 < 28 local blink = a % 58 >= 55 local look = (a % 100 < 30) and -1 or ((a % 100 >= 60 and a % 100 < 80) and 1 or 0) gfx.clip(0, 0, W, b.y + 2) bunny(b.x, rnd(yt), wig, twitch, blink, look) gfx.clip() return end end if hops and t >= 330 and t < 348 then local a, b = BUR[2], BUR[3] local s = (t - 330) / 18 local yb = a.y + 7 + (b.y - a.y) * s - 72 * s * (1 - s) local x = rnd(a.x + (b.x - a.x) * s) if s < 0.2 then gfx.clip(0, 0, W, a.y + 2) elseif s > 0.8 then gfx.clip(0, 0, W, b.y + 2) end bunny(x, rnd(yb) - 22, s < 0.5 and 1 or -1, false, false, s < 0.5 and -1 or 1) gfx.clip() end end local LC = { { 196, 48, 28 }, { 230, 120, 24 }, { 238, 176, 40 }, { 150, 60, 24 } } local LV = {} for i = 1, 10 do local h = hash32(i * 0x85EBCA6B) LV[i] = { x0 = umod(h, W + 8), y0 = umod(h >> 8, H + 8), vx = 0.12 + umod(h >> 16, 14) / 100, vy = 0.1 + umod(h >> 20, 10) / 100, ph = umod(h >> 24, 628) / 100, c = LC[i % 4 + 1], } end local function leaves(t) for i = 1, min(cfg.leaves or 6, 10) do local l = LV[i] local x = (l.x0 + l.vx * t + 3 * sin(t * 0.04 + l.ph)) % (W + 8) - 4 local y = (l.y0 + l.vy * t) % (H + 8) - 4 local ix, iy = floor(x), floor(y) local fx, fy = x - ix, y - iy local r, g, b = l.c[1], l.c[2], l.c[3] for k = 0, 1 do local ay = k == 0 and 1 - fy or fy blend(ix, iy + k, r, g, b, (1 - fx) * ay) blend(ix + 1, iy + k, r, g, b, ay) blend(ix + 2, iy + k, r, g, b, fx * ay) end end end function render() if not BG then bake_bg() end blit_rgb(BG, 0, 0, W, H) draw_bunny(floor(frame * SF[cfg.speed or 2]) % CYC, cfg.hops ~= false) leaves(frame) end