info = { type = "image", title = "Fox snow dive", category = "animals", author = "BRRRO", version = "1.0", license = "MIT", desc = "A fox listens, then dives into a snowdrift", api = 33, } local tr = ({ sk = { ["Fox"] = "Líška", ["Red"] = "Červená", ["Silver"] = "Strieborná", ["Golden"] = "Zlatá", ["Snowfall"] = "Sneženie", ["Light"] = "Mierne", ["Normal"] = "Bežné", ["Heavy"] = "Husté" }, cs = { ["Fox"] = "Liška", ["Red"] = "Červená", ["Silver"] = "Stříbrná", ["Golden"] = "Zlatá", ["Snowfall"] = "Sněžení", ["Light"] = "Mírné", ["Normal"] = "Běžné", ["Heavy"] = "Husté" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "fur", label = T("Fox"), type = "int", default = 0, opts = { T("Red"), T("Silver"), T("Golden") } }, { key = "snow", label = T("Snowfall"), type = "int", default = 1, opts = { T("Light"), T("Normal"), T("Heavy") } }, } local rect, px, blend, blend_rect, blit = gfx.rect, gfx.px, gfx.blend, gfx.blend_rect, gfx.blit_indexed local floor, ceil, sin, cos, sqrt, max, min, abs = math.floor, math.ceil, math.sin, math.cos, math.sqrt, math.max, math.min, math.abs local hash32, umod = math.hash32, math.umod local W, H, FPS = gfx.W, gfx.H, gfx.FPS local PERIOD = 20 * FPS local GY = 53 local X0, X1 = 22, 46 local FW, FH = 28, 24 local GR = {} local function grid() for i = 1, FW * FH do GR[i] = 0 end; return GR end local function put(g, x, y, c) x, y = floor(x), floor(y) if x >= 0 and x < FW and y >= 0 and y < FH then g[y * FW + x + 1] = c end end local function box(g, x, y, w, h, c) for yy = y, y + h - 1 do for xx = x, x + w - 1 do put(g, xx, yy, c) end end end local function ell(g, cx, cy, rx, ry, c, dk, lt) for y = max(0, floor(cy - ry)), min(FH - 1, ceil(cy + ry)) do local dy = (y + 0.5 - cy) / ry for x = max(0, floor(cx - rx)), min(FW - 1, ceil(cx + rx)) do local dx = (x + 0.5 - cx) / rx if dx * dx + dy * dy <= 1 then g[y * FW + x + 1] = (dk and dy > 0.45) and dk or ((lt and dx + dy < -0.8) and lt or c) end end end end local function stroke(g, x0, y0, x1, y1, r, c, shade) local n = max(ceil(sqrt((x1 - x0) ^ 2 + (y1 - y0) ^ 2)), 1) local function pass(oy, rr, col, only) for i = 0, n do local cx, cy = x0 + (x1 - x0) * i / n, y0 + (y1 - y0) * i / n + oy for y = floor(cy - rr), ceil(cy + rr) do for x = floor(cx - rr), ceil(cx + rr) do if (x + 0.5 - cx) ^ 2 + (y + 0.5 - cy) ^ 2 <= rr * rr and x >= 0 and x < FW and y >= 0 and y < FH then if not only or g[y * FW + x + 1] == only then g[y * FW + x + 1] = col end end end end end end pass(0, r, c) if shade then pass(r * 0.55, r * 0.5, 2, c); pass(-r * 0.5, r * 0.4, 3, c) end end local function ear(g, cx, top, lean, tipc) for i = 0, 4 do local wd = ({ 1, 2, 3, 3, 4 })[i + 1] local sx = cx - wd // 2 + floor((4 - i) * lean / 4 + 0.5) for x = sx, sx + wd - 1 do put(g, x, top + i, i < 2 and 6 or 1) end if i >= 2 and wd >= 3 then put(g, sx + 1, top + i, 7) end end if tipc then put(g, cx + floor(lean / 2 + 0.5), top, tipc) end end local function head(g, hx, hy, o) local el, er = o.el or 0, o.er or 0 local sn = o.snow or 0 ear(g, hx - 3, hy - 8 + (el < 0 and 1 or 0), el, sn > 0 and 4 or nil) ear(g, hx + 1, hy - 8 + (er > 0 and 1 or 0), er, sn > 0 and 4 or nil) ell(g, hx, hy, 4.4, 3.8, 1, 2, 3) if o.down then ell(g, hx + 2.2, hy + 3.6, 2, 2.9, 1) put(g, hx + 2, hy + 6, 6) else ell(g, hx + 4, hy + 1, 3, 1.8, 1) ell(g, hx + 3.5, hy + 2.8, 2.8, 1.1, 4) put(g, hx + 6.5, hy + 0.5, 6); put(g, hx + 6.5, hy + 1.5, 6) end ell(g, hx - 0.5, hy + 3, 2.4, 1.2, 4) put(g, hx + 1.5, hy - 0.8, 6) if sn > 0 then ell(g, hx - 0.5, hy - 3.2, sn > 1 and 3.6 or 2.6, sn > 1 and 1.7 or 1.2, 4, 5) if sn > 1 then ell(g, hx + 4, hy + 0.3, 2.4, 1.2, 4) ell(g, hx - 6, hy + 5, 3, 1.5, 4, 5) end end end local POSE = { sit = function(g, o) ell(g, 4, 20.5, 5, 3, 1, 2, 3); ell(g, 0.8, 21.5, 2.2, 1.8, 4, 5) ell(g, 9, 19, 6, 4.6, 1, 2, 3) ell(g, 13, 15, 4.4, 7, 1, 2, 3) ell(g, 15.8, 16.5, 2, 4.4, 4, 5) box(g, 14, 19, 2, 5, 6); box(g, 17, 21, 2, 3, 6) ell(g, 11, 22.7, 3, 1.3, 1); box(g, 12, 22, 3, 2, 6) local t = o.tilt or 0 head(g, 17 + t * 0.5, 8.5 + (o.ears == 3 and 0.5 or 0), o) if (o.snow or 0) > 1 then ell(g, 11, 12, 3.6, 1.5, 4, 5) end end, front = function(g) ell(g, 21, 20, 4, 2.8, 1, 2, 3); ell(g, 24.5, 20.5, 2, 1.8, 4, 5) ell(g, 14, 17.5, 5.5, 6, 1, 2, 3) ell(g, 14, 18.5, 3, 5, 4, 5) box(g, 11, 21, 2, 3, 6); box(g, 16, 21, 2, 3, 6) ear(g, 10, 0, 0); ear(g, 18, 0, 0) ell(g, 14, 8, 5, 4.2, 1, 2, 3) ell(g, 10.5, 9.8, 2.2, 1.8, 4); ell(g, 17.5, 9.8, 2.2, 1.8, 4) ell(g, 14, 10.6, 2.8, 2, 4) box(g, 13, 9, 2, 1, 6); put(g, 11, 7, 6); put(g, 17, 7, 6) end, crouch = function(g, k) ell(g, 3, 17 - k, 3, 2.6, 1, 2, 3) stroke(g, 3, 17 - k, 0, 12, 2, 1); ell(g, 0.8, 11, 2, 1.8, 4, 5) stroke(g, 5, 18 - k, 15, 19, 3.4, 1, true) stroke(g, 15, 20, 22, 23, 0.9, 6) ell(g, 6, 21.5, 3, 1.8, 1, 2); box(g, 3, 22, 4, 2, 6) head(g, 19, 16, { el = 1, er = 1 }) end, leap1 = function(g) stroke(g, 4, 19, 0, 15, 2, 1); ell(g, 1, 14.5, 1.8, 1.6, 4, 5) stroke(g, 5, 20, 0, 22, 1, 6) stroke(g, 4, 19, 17, 12, 3.3, 1, true) stroke(g, 16, 15, 25, 16, 0.9, 6) head(g, 21, 9, { el = -1, er = -1 }) end, leap2 = function(g) stroke(g, 4, 13, 0, 9, 2, 1); ell(g, 1, 8.5, 1.8, 1.6, 4, 5) stroke(g, 5, 16, 0, 17, 1, 6) stroke(g, 4, 13, 18, 14, 3.3, 1, true) stroke(g, 17, 16, 25, 19, 0.9, 6) head(g, 22, 15, { el = 1, er = 1 }) end, dive = function(g) stroke(g, 5, 6, 2, 1, 2, 1); ell(g, 1.5, 0.8, 1.8, 1.6, 4, 5) stroke(g, 6, 7, 2, 3, 1, 6) stroke(g, 5, 6, 14, 13, 3.3, 1, true) stroke(g, 14, 14, 20, 20, 0.9, 6) head(g, 18, 16, { down = true, el = 2, er = 2 }) end, buried = function(g, k) local tx = k == 0 and 13 or 17 stroke(g, 10, 17, tx, 9, 2, 1); ell(g, tx + 0.5, 8, 1.9, 1.7, 4, 5) stroke(g, 9, 22, 11, 12, 3.2, 1, true) stroke(g, 10, 14, 7, 6, 1.1, 6) ell(g, 12, 23.2, 11, 3, 4, 5) ell(g, 5, 22.5, 4, 1.8, 4); ell(g, 19, 22.8, 5, 2, 4) end, trot = function(g, k) stroke(g, 7, 15, 1, 14, 2.4, 1); ell(g, 0.6, 14, 1.8, 1.8, 4, 5) local a, b = k == 0 and 1 or -1, 0 stroke(g, 11, 17, 12 + 2 * a, 23, 0.9, 2); stroke(g, 15, 17, 14 - 2 * a, 23, 0.9, 2) stroke(g, 7, 15, 18, 14, 3.3, 1, true) stroke(g, 9, 17, 7 - 3 * a, 23, 0.9, 6); stroke(g, 17, 17, 18 + 3 * a, 23, 0.9, 6) head(g, 22, 11.5, { el = 0, er = 0 }) end, } local CACHE = {} local function pose(name, arg) local key = name .. (arg and (type(arg) == "table" and (arg.ears or 0) .. "_" .. (arg.snow or 0) .. "_" .. (arg.tilt or 0) or arg) or "") local p = CACHE[key] if not p then local g = grid() POSE[name](g, arg) local t, m = {}, {} for y = 0, FH - 1 do local r = string.char(table.unpack(g, y * FW + 1, y * FW + FW)) t[y + 1], m[y + 1] = r, r:reverse() end p = { table.concat(t), table.concat(m) } CACHE[key] = p end return p end local FURS = { [0] = { 230, 112, 40, 172, 68, 30, 250, 160, 72 }, [1] = { 150, 152, 172, 104, 106, 128, 196, 198, 214 }, [2] = { 236, 170, 62, 180, 118, 40, 252, 214, 112 }, } local PAL = {} function on_settings() local f = FURS[cfg.fur or 0] or FURS[0] PAL = { 0, 0, 0, f[1], f[2], f[3], f[4], f[5], f[6], f[7], f[8], f[9], 252, 248, 244, 200, 212, 236, 40, 30, 36, 240, 150, 150 } end local BG local function build_bg() for y = 0, 44 do local t = y / 44 rect(0, y, W, 1, floor(104 + 136 * t ^ 1.3), floor(140 + 56 * t ^ 1.3), floor(214 - 8 * t)) end for x = 0, W - 1 do local top = floor(40 + 2 * sin(x * 0.1) + 1.2 * sin(x * 0.27 + 1)) rect(x, top, 1, 8, 214, 224, 246) end for _, s in ipairs({ { 18, 40, 9 }, { 25, 41, 7 }, { 33, 40, 10 }, { 41, 41, 7 }, { 50, 40, 9 }, { 58, 41, 8 }, { 62, 40, 6 } }) do for i = 0, s[3] - 1 do local w = 1 + 2 * (i // 2) rect(s[1] - w // 2, s[2] - s[3] + i, w, 1, 52, 84, 112) if i % 3 == 2 then blend_rect(s[1] - w // 2, s[2] - s[3] + i, w, 1, 236, 244, 255, 0.45) end end end for y = 45, H - 1 do local t = (y - 45) / 18 rect(0, y, W, 1, floor(246 - 26 * t), floor(250 - 26 * t), 255 - floor(8 * t)) end for i = 1, 14 do local h = hash32(i * 0x9E3779B1) local x, y, w = umod(h, 70) - 6, 47 + umod(h >> 8, 16), 8 + umod(h >> 16, 12) blend_rect(x, y, w, 1, 176, 196, 232, 0.55) blend_rect(x + 2, y + 1, w - 4, 1, 255, 255, 255, 0.6) end rect(0, 0, 9, 56, 78, 56, 64); rect(0, 0, 2, 56, 104, 76, 84); rect(7, 0, 2, 56, 56, 40, 52) for y = 6, 52, 9 do rect(2, y, 2, 3, 62, 44, 56); rect(5, y + 4, 1, 3, 62, 44, 56) end rect(0, 14, 15, 3, 78, 56, 64); rect(0, 14, 15, 1, 104, 76, 84) rect(1, 12, 13, 2, 246, 250, 255); rect(4, 11, 8, 1, 246, 250, 255) rect(-2, 50, 14, 5, 236, 242, 255); rect(0, 49, 11, 1, 246, 250, 255) BG = gfx.snapshot(0, 0, W, H) end local FAR, NEAR = {}, {} for i = 1, 22 do local h = hash32(i * 0x85EBCA6B) FAR[i] = { x = umod(h, 70), v = 5 + umod(h >> 8, 4), ph = umod(h >> 12, 628) / 100, off = umod(h >> 20, 1000) / 10, a = 5 + umod(h >> 4, 3) } end for i = 1, 11 do local h = hash32(i * 0xC2B2AE35) NEAR[i] = { x = umod(h, 70), v = 12 + umod(h >> 8, 8), ph = umod(h >> 12, 628) / 100, off = umod(h >> 20, 1000) / 10, a = 5 + umod(h >> 4, 4) } end local COUNTS = { { 8, 4 }, { 14, 7 }, { 22, 11 } } local function flake(f, t, a) local y = (f.off + f.v * t) % (H + 8) - 4 local x = f.x + f.a * sin(t * 0.8 + f.ph) - 3 local ix, iy = floor(x), floor(y) local fx, fy = x - ix, y - iy blend(ix, iy, 255, 255, 255, a * (1 - fx) * (1 - fy)) blend(ix + 1, iy, 255, 255, 255, a * fx * (1 - fy)) blend(ix, iy + 1, 255, 255, 255, a * (1 - fx) * fy) blend(ix + 1, iy + 1, 255, 255, 255, a * fx * fy) end local function bigflake(f, t) local y = (f.off + f.v * t) % (H + 8) - 4 local x = f.x + f.a * sin(t * 0.7 + f.ph) - 3 local ix, iy = floor(x), floor(y) local fx, fy = x - ix, y - iy for r = 0, 2 do local a = (r == 0 and 1 - fy) or (r == 1 and 1) or fy blend_rect(ix + 1, iy + r, 1, 1, 255, 255, 255, a) blend_rect(ix, iy + r, 1, 1, 255, 255, 255, a * (1 - fx)) blend_rect(ix + 2, iy + r, 1, 1, 255, 255, 255, a * fx) end end local function puff(x, y, t0, u, n, spread) local d = u - t0 if d < 0 or d > 1 then return end for i = 1, n do local h = hash32(i * 0x9E3779B1 + floor(t0 * 7)) local vx = (umod(h, 100) - 50) / 50 * spread local vy = 10 + umod(h >> 8, 16) local px_, py_ = x + vx * d, y - vy * d + 38 * d * d blend(floor(px_), floor(py_), 255, 255, 255, 1 - d) if i % 3 == 0 then blend(floor(px_) + 1, floor(py_), 255, 255, 255, (1 - d) * 0.7) end end end function render() if not BG then build_bg() end gfx.blit_rgb(BG, 0, 0, W, H) local f = frame % PERIOD local u = f / FPS local t = frame / FPS local cnt = COUNTS[(cfg.snow or 1) + 1] or COUNTS[2] for i = 1, cnt[1] do flake(FAR[i], t, 0.55) end local name, arg, mir, x, dy = "sit", { ears = 0 }, false, X0, 0 if u < 7 or u >= 15.8 then local w = u % 20 if (w > 1.0 and w < 1.25) or (w > 17.0 and w < 17.25) then arg = { ears = 1, el = -2 } elseif (w > 2.4 and w < 2.65) or (w > 18.6 and w < 18.85) then arg = { ears = 2, er = 2 } elseif w > 4.2 and w < 5.6 then arg = { ears = 3, el = -2, er = 2, tilt = 1 } end elseif u < 7.7 then name, arg = "crouch", (f // 4) % 2 elseif u < 8.4 then local k = (u - 7.7) / 0.7 name, arg, x, dy = k < 0.3 and "leap1" or (k < 0.62 and "leap2" or "dive"), nil, X0 + (X1 - X0) * k, -floor(13 * 4 * k * (1 - k) + 0.5) elseif u < 10.4 then name, arg, x = "buried", (f // 8) % 2, X1 elseif u < 13 then x = X1 if u < 11.1 then arg = { snow = 2, tilt = 1 } elseif u < 11.7 then arg = { snow = 2, tilt = (f // 4) % 2 * 2 - 1 } elseif u < 12.3 then arg = { snow = 1, tilt = (f // 4) % 2 * 2 - 1 } else arg = { ears = 1, el = -2 } end elseif u < 13.9 then name, arg, mir = "trot", (f // 4) % 2, true x = X1 - (X1 - X0) * (u - 13) / 0.9 elseif u < 14.8 then arg, mir, x = { ears = 0 }, true, X0 elseif u < 15.8 then name, arg = "front", nil end x = floor(x + 0.5) local sy = GY + 1 if name ~= "buried" then local k = dy < 0 and -dy // 4 or 0 blend_rect(x - 8 + k, sy, 16 - 2 * k, 2, 150, 172, 220, 0.45) end blit(pose(name, arg)[mir and 2 or 1], PAL, x - 14, GY - 23 + dy, FW, FH, FW, FH, true) puff(X1 + 4, GY - 1, 8.4, u, 14, 22) puff(X1 + 3, GY - 12, 11.1, u, 10, 14) puff(X1 + 3, GY - 12, 11.7, u, 8, 12) for i = 1, cnt[2] do bigflake(NEAR[i], t) end end