info = { type = "image", title = "Hedgehog and apple", category = "animals", author = "BRRRO", version = "1.0", license = "MIT", desc = "A hedgehog spears a fallen apple and leaves", api = 33, } local tr = ({ sk = { ["Season"] = "Ročné obdobie", ["Autumn"] = "Jeseň", ["Summer"] = "Leto", ["Speed"] = "Rýchlosť" }, cs = { ["Season"] = "Roční období", ["Autumn"] = "Podzim", ["Summer"] = "Léto", ["Speed"] = "Rychlost" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "season", label = T("Season"), type = "int", default = "autumn", opts = { keys = { "autumn", "summer" }, T("Autumn"), T("Summer") } }, { key = "speed", label = T("Speed"), type = "int", min = 1, max = 3, default = 2 }, } local rect, px, blend_rect, blit_rgb, blit = gfx.rect, gfx.px, gfx.blend_rect, gfx.blit_rgb, gfx.blit_indexed local floor, sin, sqrt = math.floor, math.sin, math.sqrt local hash32, umod = math.hash32, math.umod local W, H, FPS = gfx.W, gfx.H, gfx.FPS local GY = 57 local SW, SHH = 26, 16 local CYC = 8.5 local XS = 16 local AX = 43 local V = 30 local SPR = { 0, 0, 0, 62, 38, 24, 108, 70, 40, 178, 132, 80, 234, 208, 168, 186, 148, 108, 172, 128, 92, 118, 84, 60, 24, 16, 16, 210, 160, 124, } local THEME = { autumn = { haze0 = { 252, 214, 136 }, haze1 = { 206, 134, 70 }, far = { 170, 104, 62 }, near = { 82, 50, 34 }, nearl = { 122, 78, 48 }, canopy = { { 232, 130, 36 }, { 200, 70, 34 }, { 244, 184, 62 }, { 150, 60, 36 } }, gr0 = { 160, 114, 64 }, gr1 = { 112, 76, 44 }, edge = { 190, 140, 78 }, litter = { { 210, 90, 28 }, { 230, 160, 40 }, { 170, 48, 30 }, { 120, 70, 30 } }, moss = { 70, 104, 44 }, mossl = { 98, 138, 56 }, leaf = { { 214, 96, 30 }, { 232, 168, 40 }, { 176, 52, 32 } }, }, summer = { haze0 = { 214, 236, 170 }, haze1 = { 150, 198, 104 }, far = { 120, 150, 110 }, near = { 66, 56, 40 }, nearl = { 100, 84, 56 }, canopy = { { 70, 160, 64 }, { 110, 196, 72 }, { 40, 118, 56 }, { 160, 210, 90 } }, gr0 = { 92, 142, 62 }, gr1 = { 58, 98, 44 }, edge = { 120, 176, 76 }, litter = { { 250, 250, 240 }, { 250, 220, 70 }, { 130, 190, 80 }, { 90, 150, 60 } }, moss = { 74, 124, 50 }, mossl = { 110, 160, 66 }, leaf = { { 140, 200, 80 }, { 200, 230, 120 }, { 100, 170, 70 } }, }, } local TH = THEME.autumn local BG = nil function on_settings() TH = THEME[cfg.season] or THEME.autumn BG = nil end local function mix(a, b, t) return floor(a[1] + (b[1] - a[1]) * t + 0.5), floor(a[2] + (b[2] - a[2]) * t + 0.5), floor(a[3] + (b[3] - a[3]) * t + 0.5) end local function disc(cx, cy, r, c) for dy = -r, r do local hw = floor(sqrt(r * r - dy * dy) + 0.5) rect(cx - hw, cy + dy, 2 * hw + 1, 1, c[1], c[2], c[3]) end end local function build_bg() for y = 0, 43 do rect(0, y, W, 1, mix(TH.haze0, TH.haze1, y / 43)) end for _, t in ipairs({ { 9, 2 }, { 27, 2 }, { 40, 3 }, { 53, 2 } }) do rect(t[1], 8, t[2], 36, TH.far[1], TH.far[2], TH.far[3]) end for i = 1, 11 do local h = hash32(i * 0x9E3779B1) local cx = umod(h, W) local c = TH.canopy[1 + umod(h >> 8, 4)] disc(cx, 1 + umod(h >> 12, 9), 5 + umod(h >> 16, 4), c) local d = TH.canopy[1 + umod(h >> 20, 4)] disc(cx - 1, 0 + umod(h >> 12, 9), 2, d) end for i = 1, 5 do disc(umod(hash32(i * 0x85EBCA6B), 14), 4 + 6 * i, 5, TH.canopy[4]) end for _, t in ipairs({ { 1, 4 }, { 59, 4 } }) do rect(t[1], 0, t[2], 47, TH.near[1], TH.near[2], TH.near[3]) rect(t[1], 0, 1, 47, TH.nearl[1], TH.nearl[2], TH.nearl[3]) end for y = 44, 63 do rect(0, y, W, 1, mix(TH.gr0, TH.gr1, (y - 44) / 19)) end rect(0, 44, W, 1, TH.edge[1], TH.edge[2], TH.edge[3]) for i = 1, 70 do local h = hash32(i * 0xC2B2AE35) local c = TH.litter[1 + umod(h >> 16, 4)] local x, y = umod(h, W), 46 + umod(h >> 8, 17) rect(x, y, 1 + (h >> 24) % 2, 1, c[1], c[2], c[3]) end for _, m in ipairs({ { 0, 45, 9 }, { 54, 45, 8 }, { 20, 45, 5 } }) do rect(m[1], m[2], m[3], 2, TH.moss[1], TH.moss[2], TH.moss[3]) rect(m[1] + 1, m[2], m[3] - 3, 1, TH.mossl[1], TH.mossl[2], TH.mossl[3]) end rect(9, 52, 1, 3, 236, 224, 204); rect(7, 50, 5, 2, 208, 44, 40); px(8, 50, 250, 240, 230); px(10, 51, 250, 240, 230) rect(52, 51, 1, 3, 236, 224, 204); rect(50, 49, 5, 2, 208, 44, 40); px(51, 51, 250, 240, 230); px(53, 49, 250, 240, 230) rect(29, 61, 1, 3, 226, 214, 190); rect(28, 59, 3, 2, 150, 96, 54) rect(34, 62, 1, 2, 226, 214, 190); rect(33, 60, 3, 2, 170, 110, 60) BG = gfx.snapshot(0, 0, W, H) end local G = {} local queue, qn = {}, 1 for p = 1, 6 do for v = 0, 1 do queue[#queue + 1] = { p, v } end end local function bake(pose, fr) local g = {} for i = 1, SW * SHH do g[i] = 0 end local function put(x, y, v) if x >= 0 and x < SW and y >= 0 and y < SHH then g[y * SW + x + 1] = v end end local function ell(cx, cy, rx, ry, fn, spiky) for y = 0, SHH - 1 do for x = 0, SW - 1 do local dx, dy = (x + 0.5 - fr - cx) / rx, (y + 0.5 - cy) / ry local thr = 1 if spiky and (x * 7 + y * 3) % 5 == 0 then thr = 1 + spiky end if dx * dx + dy * dy <= thr then local v = fn(dx, dy, x, y) if v then put(x, y, v) end end end end end local function quill(dx, dy, x, y) local p = (x * 5 + y * 3) % 7 local v = p == 0 and 3 or p <= 2 and 2 or 1 if -0.5 * dx - 0.6 * dy > 0.3 and v < 3 then v = v + 1 end if dy > 0.55 then v = 1 end return v end local function leg(x, y0, y1, c) for y = y0, y1 do put(x, y, c); put(x + 1, y, c) end end if pose <= 4 then local ph = (pose - 1) * 1.5708 local bob = (pose == 2 or pose == 4) and -1 or 0 local function lg(cx, ph2, c) local s = sin(ph2) leg(floor(cx + 2 * s + fr), 13, math.cos(ph2) > 0.3 and 14 or 15, c) end lg(15.5, ph + 3.1416, 7); lg(8, ph, 7) ell(11, 12 + bob, 10.5, 8, function(dx, dy, x, y) if y <= 13 + bob then return quill(dx, dy, x, y) end end, 0.2) lg(17.5, ph, 6); lg(6, ph + 3.1416, 6) ell(20.5, 11.6 + bob, 4.2, 3.2, function(dx, dy) return dy > 0.35 and 5 or 4 end) ell(24.8, 12.6 + bob, 1.8, 1.3, function() return 4 end) put(25, 12 + bob, 8); put(21, 10 + bob, 8) put(18, 8 + bob, 9) elseif pose == 5 then leg(15, 13, 15, 7); leg(8, 13, 15, 7) ell(11, 12, 10.5, 8, function(dx, dy, x, y) if y <= 13 then return quill(dx, dy, x, y) end end, 0.2) leg(17, 13, 15, 6); leg(6, 13, 15, 6) ell(21.5, 13.4, 4, 2.8, function(dx, dy) return dy > 0.35 and 5 or 4 end) ell(24.9, 14.5, 1.8, 1.2, function() return 4 end) put(26, 14, 8); put(22, 12, 8); put(19, 10, 9) else ell(12, 8.5, 7.6, 7.8, function(dx, dy, x, y) return quill(dx, dy, x, y) end, 0.3) put(8, 15, 6); put(9, 15, 6); put(15, 15, 6); put(16, 15, 6) end local rows = {} for y = 0, SHH - 1 do rows[y + 1] = string.char(table.unpack(g, y * SW + 1, y * SW + SW)) end return table.concat(rows) end local function build_step() local j = queue[qn] if j then G[j[1] * 2 + j[2]] = bake(j[1], j[2] * 0.5); qn = qn + 1 end end local LEAF = {} for i = 1, 7 do local h = hash32(i * 0x27D4EB2F) LEAF[i] = { umod(h, 56) + 4, umod(h >> 8, 80), 5 + umod(h >> 12, 6), umod(h >> 16, 628) / 100, 1 + umod(h >> 20, 3) } end local function apple(x, y) rect(x + 1, y, 3, 1, 200, 34, 32); rect(x, y + 1, 5, 3, 200, 34, 32); rect(x + 1, y + 4, 3, 1, 140, 20, 24) rect(x + 4, y + 2, 1, 2, 140, 20, 24); px(x + 1, y + 1, 246, 120, 100) px(x + 2, y - 1, 90, 60, 30); px(x + 3, y - 1, 70, 140, 50) end local function smooth(u) if u <= 0 then return 0 elseif u >= 1 then return 1 end return u * u * (3 - 2 * u) end function render() if not BG then build_bg() end build_step() blit_rgb(BG, 0, 0, W, H) local ts = frame * (cfg.speed or 2) / 2 / FPS + 1 local t = ts % CYC local x, pose = nil, 1 if t >= 0.8 and t < 2.4 then local tau = t - 0.8 if tau < 1.2 then x = -26 + V * tau else local d = tau - 1.2; x = 10 + V * d - V * d * d / 0.8 end pose = (t < 2.4) and (floor(x / 2) % 4 + 1) or 1 elseif t >= 2.4 and t < 3.6 then x, pose = XS, 5 elseif t >= 3.6 and t < 4.0 then x, pose = XS, 6 elseif t >= 4.0 then local tau = t - 4.0 x = XS + (tau < 0.4 and V * tau * tau / 0.8 or 6 + V * (tau - 0.4)) pose = floor(x / 2) % 4 + 1 if x > 66 then x = nil end end local ay, ax = nil, AX if t < 0.7 then ay = -8 + 61 * (t / 0.7) ^ 2 elseif t < 1.06 then local u = (t - 0.7) / 0.36; ay = 53 - 24 * u * (1 - u) elseif t < 1.26 then local u = (t - 1.06) / 0.2; ay = 53 - 8 * u * (1 - u) else ay = 53 end if t >= 3.7 and x then local u = smooth((t - 3.7) / 0.3) local tx, ty = floor(x + 0.5) + 9, GY - 15 + 1 ax, ay = AX + (tx - AX) * u, 53 + (ty - 53) * u - 12 * u * (1 - u) * 2 if t >= 4.0 then ax, ay = tx, ty end elseif t >= 3.7 then ay = nil end for i = 1, 7 do local l = LEAF[i] local yy = (ts * l[3] + l[2]) % 76 - 8 local lx = floor(l[1] + 6 * sin(ts * 0.8 + l[4]) + 0.5) local yi = floor(yy) local f = yy - yi local c = TH.leaf[l[5]] blend_rect(lx, yi, 2, 1, c[1], c[2], c[3], 1 - f) blend_rect(lx, yi + 1, 2, 1, c[1], c[2], c[3], f) end if ay then blend_rect(floor(ax), 57, 5, 1, 20, 14, 8, 0.25) if t < 3.7 or not x then apple(floor(ax), floor(ay)) end end if x then local q = floor(x * 2 + 0.5) / 2 local xi = floor(q) local v = floor((q - xi) * 2 + 0.5) blend_rect(xi + 3, GY, 21, 1, 20, 14, 8, 0.3); blend_rect(xi + 5, GY + 1, 17, 1, 20, 14, 8, 0.2) local g = G[pose * 2 + v] if g then blit(g, SPR, xi, GY - SHH + 1, SW, SHH, SW, SHH, true) end if t >= 3.7 and ay then apple(floor(ax + 0.5), floor(ay + 0.5)) if t >= 3.95 then local bx, by = xi + 9, GY - 14 + 4 + (pose == 6 and 0 or 2) px(bx, by + 1, 178, 132, 80); px(bx + 2, by + 2, 108, 70, 40); px(bx + 4, by + 1, 178, 132, 80) end end end end