info = { type = "image", title = "Squirrel snack", category = "animals", author = "BRRRO", version = "1.0", license = "MIT", desc = "A squirrel nibbles acorns on an autumn branch", api = 44, } local tr = ({ sk = { ["Fur colour"] = "Farba srsti", ["Drops acorns"] = "Pustí žalude", ["Falling leaves"] = "Padajúce lístie" }, cs = { ["Fur colour"] = "Barva srsti", ["Drops acorns"] = "Pustí žaludy", ["Falling leaves"] = "Padající listí" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "fur", label = T("Fur colour"), type = "color", default = "#D0662C" }, { key = "drop", label = T("Drops acorns"), type = "bool", default = true }, { key = "leaves", label = T("Falling leaves"), type = "int", min = 0, max = 6, default = 3 }, } local rect, px, blend_rect = gfx.rect, gfx.px, gfx.blend_rect 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 STEP_HZ = 30 local TAU = 6.28318 local GROUND = 57 local BARK, BARK_D, BARK_L = { 122, 80, 52 }, { 92, 60, 40 }, { 156, 108, 72 } local SOIL = { 150, 104, 64 } local FAR, FAR_L = { 178, 168, 170 }, { 196, 188, 186 } local HAZE1, HAZE2 = { 236, 188, 128 }, { 224, 164, 106 } local LEAF = { { 226, 116, 42 }, { 240, 172, 58 }, { 196, 72, 40 }, { 146, 156, 60 } } local NUT, NUT_D, CAP, NUT_L = { 198, 138, 64 }, { 160, 104, 48 }, { 108, 72, 44 }, { 240, 200, 124 } local function c3(c) return c[1], c[2], c[3] end local function E(cx, cy, a, b) local S = {} for y = math.ceil(cy - b), floor(cy + b) do local d = (y - cy) / b local hw = a * sqrt(math.max(0, 1 - d * d)) S[y] = { math.ceil(cx - hw - 1e-6), floor(cx + hw + 1e-6) } end return function(x, y) local s = S[y] return s ~= nil and x >= s[1] and x <= s[2] end end local E2 = E(32, 34, 5.5, 6.5) local E3 = E(30, 37, 5, 3.5) local E4 = E(35, 35, 2.2, 4.2) local E5 = E(30, 37, 3.8, 2.4) local E6 = E(37, 24, 4.5, 4) local E7 = E(41, 25.5, 2.6, 2) local E8 = E(39.5, 26.8, 2.6, 1.3) local function grid(x0, y0, w, h, cls) local C = {} for y = y0 - 1, y0 + h do for x = x0 - 1, x0 + w do C[(y - y0 + 1) * 64 + (x - x0 + 1)] = cls(x, y) end end local rows, row = {}, {} for y = 0, h - 1 do for x = 0, w - 1 do local k = (y + 1) * 64 + (x + 1) local c = C[k] if c == 0 and (C[k - 1] > 0 or C[k + 1] > 0 or C[k - 64] > 0 or C[k + 64] > 0) then c = 1 end row[x + 1] = c end rows[y + 1] = string.char(table.unpack(row, 1, w)) end return table.concat(rows) end local BODY_X, BODY_Y, BODY_W, BODY_H = 23, 26, 18, 16 local BODY, HEAD = nil, nil local function body_cls(x, y) local inb, inh = E2(x, y), E3(x, y) if y >= 39 and y <= 40 and x >= 33 and x <= 38 then return 3 end if inb or inh then if E4(x, y) then return 4 end if inh and not E5(x, y) and y < 40 and x > 26 then return 3 end return 2 end return 0 end local HEAD_X, HEAD_Y, HEAD_W, HEAD_H = 32, 16, 13, 14 local function head_cls(x, y) if (x == 38 or x == 39) and (y == 22 or y == 23) then return (x == 38 and y == 22) and 6 or 5 end if x == 43 and y == 25 then return 5 end if E6(x, y) or E7(x, y) then if E8(x, y) then return 4 end return 2 end local i = y - 17 if i >= 0 and i <= 3 and x >= 35 - (i // 2) and x <= 35 + floor(i * 0.7) then return i <= 1 and 3 or 2 end return 0 end local T_TOP, T_BOT = 10, 38 local TCX, THW, TS = {}, {}, {} for y = T_TOP, T_BOT do local s = (T_BOT - y) / (T_BOT - T_TOP) TS[y] = s TCX[y] = 25 - 6 * sin(3.14159 * 0.9 * s) + 7 * s * s * s THW[y] = floor(1.8 + 4.5 * sin(3.14159 * 0.92 * s ^ 0.8) + 0.5) end local PAL, FUR, FUR_D, FUR_L, OUT = {}, {}, {}, {}, {} local CREAM, EYE = { 240, 222, 184 }, { 32, 22, 20 } function on_settings() local f = cfg.fur or { r = 208, g = 102, b = 44 } FUR = { f.r, f.g, f.b } FUR_D = { floor(f.r * 0.76), floor(f.g * 0.72), floor(f.b * 0.72) } FUR_L = { floor(f.r + (255 - f.r) * 0.30), floor(f.g + (230 - f.g) * 0.30), floor(f.b + (200 - f.b) * 0.30) } OUT = { floor(f.r * 0.40), floor(f.g * 0.34), floor(f.b * 0.34) } PAL = { 0, 0, 0, OUT[1], OUT[2], OUT[3], FUR[1], FUR[2], FUR[3], FUR_D[1], FUR_D[2], FUR_D[3], CREAM[1], CREAM[2], CREAM[3], EYE[1], EYE[2], EYE[3], 255, 255, 255 } end local function acorn(x, y, lying, a) x, y = floor(x + 0.5), floor(y + 0.5) local R = a >= 0.99 and rect or nil local function r(xx, yy, w, h, c) if R then R(xx, yy, w, h, c[1], c[2], c[3]) else blend_rect(xx, yy, w, h, c[1], c[2], c[3], a) end end if lying then r(x - 1, y + 1, 1, 1, CAP) r(x, y, 2, 3, CAP) r(x + 2, y, 2, 3, NUT) r(x + 4, y + 1, 1, 1, NUT_D) r(x + 2, y + 2, 2, 1, NUT_D) r(x + 2, y, 1, 1, NUT_L) else r(x + 2, y - 1, 1, 1, CAP) r(x + 1, y, 2, 1, CAP) r(x, y + 1, 4, 1, CAP) r(x, y + 2, 4, 2, NUT) r(x + 1, y + 4, 2, 1, NUT_D) r(x, y + 2, 1, 1, NUT_L) end end local AX, AY, LAND = 40, 27, GROUND - 3 local G = 0.07 local FALL = sqrt(2 * (LAND - AY) / G) local V1 = G * FALL * 0.3 local T2 = 2 * V1 / G local function apos(n, s) local vx = 0.13 + umod(hash32(n * 0x27D4EB2F), 18) / 100 if s < FALL then return AX + vx * s, AY + 0.5 * G * s * s, (s // 5) % 2 == 1 end local s2 = s - FALL local xl = AX + vx * FALL if s2 < T2 then return xl + vx * s2, LAND - (V1 * s2 - 0.5 * G * s2 * s2), true end local s3 = math.min(s2 - T2, 20) return xl + vx * T2 + vx * (s3 - s3 * s3 / 40), LAND, true end local BG = nil local function blob(cx, cy, r, ca, cb, seed) for y = cy - r, cy + r do for x = cx - r, cx + r do local dx, dy = x - cx, y - cy if dx * dx + dy * dy <= r * r + r and x >= 0 and x < W and y >= 0 then local h = umod(hash32((x // 2) * 0x9E3779B1 + (y // 2) * 0x85EBCA6B + seed), 3) local c = h == 0 and cb or ca px(x, y, c[1], c[2], c[3]) end end end end local function build_bg() local A, B = { 140, 192, 228 }, { 224, 226, 204 } for y = 0, GROUND - 1 do local t = math.min(1, y / 50) rect(0, y, W, 1, 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 rect(50, 0, 3, GROUND, c3(FAR)); rect(52, 0, 1, GROUND, c3(FAR_L)) rect(59, 0, 2, GROUND, c3(FAR)) rect(21, 0, 2, GROUND, c3(FAR)) blob(55, 4, 9, HAZE1, HAZE2, 7); blob(63, 12, 6, HAZE2, HAZE1, 9); blob(42, 1, 6, HAZE1, HAZE2, 11) rect(0, GROUND, W, H - GROUND, c3(SOIL)) for i = 1, 40 do local h = hash32(i * 0x9E3779B1) local x, y = umod(h, W), GROUND + umod(h >> 8, H - GROUND) local c = LEAF[1 + umod(h >> 16, 4)] px(x, y, c3(c)); if umod(h >> 20, 2) == 0 then px(x + 1, y, c3(c)) end end for x = 3, W - 1, 7 do local c = LEAF[4] px(x, GROUND - 1, c3(c)); px(x + 1, GROUND - 2, c3(c)); px(x + 2, GROUND - 1, c3(c)) end rect(0, 0, 9, GROUND, c3(BARK)) rect(8, 0, 1, GROUND, c3(BARK_L)) for i = 1, 9 do local h = hash32(i * 0x85EBCA6B) rect(1 + umod(h, 6), umod(h >> 8, 50), 1, 3 + umod(h >> 16, 6), c3(BARK_D)) end rect(3, 46, 3, 4, c3(BARK_D)); rect(4, 47, 1, 2, 60, 40, 28) local seg = { { 9, 12, 5 }, { 21, 12, 4 }, { 33, 9, 3 } } for i = 1, #seg do local x, w, th = seg[i][1], seg[i][2], seg[i][3] rect(x, 41, w, th, c3(BARK)) rect(x, 41, w, 1, c3(BARK_L)) rect(x, 41 + th - 1, w, 1, c3(BARK_D)) end px(41, 42, c3(BARK_D)) px(15, 40, c3(BARK_D)); px(16, 39, c3(BARK_D)); px(16, 38, c3(BARK_D)); px(17, 37, c3(BARK_D)) rect(13, 37, 2, 2, c3(LEAF[1])); rect(17, 35, 2, 2, c3(LEAF[2])); rect(18, 38, 2, 2, c3(LEAF[3])) blob(5, 1, 10, LEAF[1], LEAF[2], 3); blob(16, -2, 7, LEAF[3], LEAF[1], 5) acorn(37, 38, true, 1) BG = gfx.snapshot(0, 0, W, H) end local function tail(t) local w = TAU / (1.8 * STEP_HZ) for y = T_TOP, T_BOT do local s = TS[y] local x = TCX[y] + 1.8 * s * s * sin(w * t - 2.4 * s) local ix = floor(x) local f = x - ix local hw = THW[y] local fc = s > 0.82 and FUR_L or FUR if y == T_TOP then rect(ix - hw, y - 1, 2 * hw + 1, 1, c3(OUT)) end rect(ix - hw - 1, y, 2 * hw + 3, 1, c3(OUT)) rect(ix - hw, y, 2 * hw + 1, 1, c3(fc)) if f > 0.04 then blend_rect(ix - hw, y, 1, 1, OUT[1], OUT[2], OUT[3], f) blend_rect(ix + hw + 1, y, 1, 1, fc[1], fc[2], fc[3], f) blend_rect(ix + hw + 2, y, 1, 1, OUT[1], OUT[2], OUT[3], f) end end end local function leaves(t, n) for i = 1, n do local h = hash32(i * 0x9E3779B1 + 17) local P = (6 + umod(h, 5)) * STEP_HZ local s = (t + umod(h >> 4, P)) % P local x0 = umod(h >> 8, 56) + 2 local y = -2 + (GROUND + 1) * s / P local x = x0 + 3 * sin(s * 0.07 + i) + 0.02 * s local c = LEAF[1 + umod(h >> 16, 3)] local ix, iy = floor(x), floor(y) local fx, fy = x - ix, y - iy blend_rect(ix, iy, 1, 1, c[1], c[2], c[3], (1 - fx) * (1 - fy)) blend_rect(ix + 1, iy, 1, 1, c[1], c[2], c[3], fx * (1 - fy)) blend_rect(ix, iy + 1, 1, 1, c[1], c[2], c[3], (1 - fx) * fy) blend_rect(ix + 1, iy + 1, 1, 1, c[1], c[2], c[3], fx * fy) end end local CYC, D = 270, 180 local PICK0, PICK1 = D + 28, D + 64 function render() if not BG then build_bg(); gfx.ready(false) return end gfx.blit_rgb(BG, 0, 0, W, H) if not BODY then BODY = grid(BODY_X, BODY_Y, BODY_W, BODY_H, body_cls); gfx.ready(false) return end if not HEAD then HEAD = grid(HEAD_X, HEAD_Y, HEAD_W, HEAD_H, head_cls); gfx.ready(false) return end local t = frame * STEP_HZ // FPS local drop = cfg.drop ~= false local n = t // CYC local u = drop and t % CYC or 0 if drop then for m = n - 2, n - 1 do local x, y = apos(m, 999) local a = 1 if m == n - 2 and u >= D + 30 then a = math.max(0, 1 - (u - D - 30) / 30) end if a > 0.02 then acorn(x, y, true, a) end end end tail(t) gfx.blit_indexed(BODY, PAL, BODY_X, BODY_Y, BODY_W, BODY_H, BODY_W, BODY_H, true) local chew = u < D or u >= PICK1 local dy, pdy, hold = 0, 0, true if chew then dy = (t // 4) % 2 else hold = u >= PICK0 + 18 if u >= PICK0 and u < PICK1 then local k = (u - PICK0) / (PICK1 - PICK0) k = k < 0.5 and k * 2 or (1 - k) * 2 dy = floor(2.4 * k + 0.5) pdy = floor(9.5 * k + 0.5) end end gfx.blit_indexed(HEAD, PAL, HEAD_X, HEAD_Y + dy, HEAD_W, HEAD_H, HEAD_W, HEAD_H, true) local s = u - D if drop and s >= 0 and s < 40 then rect(38, 22 + dy, 2, 2, c3(EYE)); px(39, 23 + dy, 255, 255, 255) elseif (t + 37) % 115 < 3 then rect(38, 22 + dy, 2, 2, c3(FUR)); rect(38, 23 + dy, 2, 1, c3(OUT)) end local py = 29 + dy + pdy rect(36, 29 + dy, 3, 2 + pdy, c3(FUR)) if hold then acorn(40, 27 + dy + pdy, chew and (t // 12) % 4 == 3, 1) end rect(39, py, 1, 2, c3(FUR_D)); rect(44, py, 1, 2, c3(FUR_D)) if drop and s >= 0 then local x, y, lying = apos(n, s) acorn(x, y, lying, 1) end leaves(t, cfg.leaves or 3) end