info = { type = "image", title = "Hamster and berry", category = "animals", author = "BRRRO", version = "1.0", license = "MIT", desc = "Hamster stuffs a berry into its cheek", api = 33, } local tr = ({ sk = { ["Fur"] = "Srsť", ["Berry"] = "Bobuľa", ["Golden"] = "Zlatá", ["Grey"] = "Sivá", ["Cocoa"] = "Kakaová", ["Red"] = "Červená", ["Blue"] = "Modrá", ["Purple"] = "Fialová" }, cs = { ["Fur"] = "Srst", ["Berry"] = "Bobule", ["Golden"] = "Zlatá", ["Grey"] = "Šedá", ["Cocoa"] = "Kakaová", ["Red"] = "Červená", ["Blue"] = "Modrá", ["Purple"] = "Fialová" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "fur", label = T("Fur"), type = "int", default = "golden", opts = { keys = { "golden", "grey", "cocoa" }, T("Golden"), T("Grey"), T("Cocoa") } }, { key = "berry", label = T("Berry"), type = "int", default = "red", opts = { keys = { "red", "blue", "purple" }, T("Red"), T("Blue"), T("Purple") } }, } local rect, px, blend_rect = gfx.rect, gfx.px, gfx.blend_rect local blit_rgb, blit_indexed, snapshot = gfx.blit_rgb, gfx.blit_indexed, gfx.snapshot local floor, ceil, sin, cos, sqrt, max, min = math.floor, math.ceil, math.sin, math.cos, math.sqrt, math.max, math.min local hash32, umod = math.hash32, math.umod local char, unpack, concat = string.char, table.unpack, table.concat local W, H, FPS = gfx.W, gfx.H, gfx.FPS local STEP_HZ = 30 local FURS = { golden = { { 0, 112, 60, 24 }, { 0.5, 196, 130, 60 }, { 1, 244, 202, 138 } }, grey = { { 0, 66, 66, 80 }, { 0.5, 140, 138, 152 }, { 1, 216, 214, 226 } }, cocoa = { { 0, 54, 28, 20 }, { 0.5, 128, 78, 52 }, { 1, 204, 154, 120 } }, } local BERRIES = { red = { { 0, 70, 4, 14 }, { 0.55, 206, 24, 44 }, { 1, 255, 150, 150 } }, blue = { { 0, 14, 20, 74 }, { 0.55, 52, 80, 196 }, { 1, 170, 196, 255 } }, purple = { { 0, 44, 8, 60 }, { 0.55, 128, 36, 168 }, { 1, 226, 170, 255 } }, } local CREAM = { { 0, 150, 120, 110 }, { 0.5, 238, 218, 198 }, { 1, 255, 252, 244 } } local PINK = { { 0, 170, 80, 90 }, { 0.5, 240, 150, 160 }, { 1, 255, 208, 212 } } local PAL = nil local function ramp3(keys, t) local a, b = keys[1], keys[2] if t > b[1] then a, b = b, keys[3] end local u = (t - a[1]) / (b[1] - a[1]) return floor(a[2] + (b[2] - a[2]) * u + 0.5), floor(a[3] + (b[3] - a[3]) * u + 0.5), floor(a[4] + (b[4] - a[4]) * u + 0.5) end function on_settings() PAL = { 0, 0, 0 } for _, keys in ipairs({ FURS[cfg.fur] or FURS.golden, CREAM, PINK, BERRIES[cfg.berry] or BERRIES.red }) do for k = 0, 15 do local r, g, b = ramp3(keys, k / 15) PAL[#PAL + 1], PAL[#PAL + 2], PAL[#PAL + 3] = r, g, b end end PAL[#PAL + 1], PAL[#PAL + 2], PAL[#PAL + 3] = 52, 36, 20 end local LX, LY, LZ = -0.46, -0.58, 0.67 local HX, HY, HZ = -0.252, -0.317, 0.914 local function shade(nx, ny, nz, spec) local d = (nx * LX + ny * LY + nz * LZ + 0.3) / 1.3 if d < 0 then d = 0 end local t = 0.3 + 0.5 * d if ny > 0.35 then t = t + 0.2 * (ny - 0.35) * (1 - d) end local s = nx * HX + ny * HY + nz * HZ if s > 0.8 then local s2 = s * s; s2 = s2 * s2; s2 = s2 * s2; s2 = s2 * s2 t = t + spec * 0.6 * s2 end t = t * (0.76 + 0.24 * nz) if t > 1 then t = 1 elseif t < 0 then t = 0 end return floor(t * 15 + 0.5) end local function grid(w, h) local g = {} for i = 1, w * h do g[i] = 0 end return g end local function ell(g, w, h, cx, cy, rx, ry, ang, bi, spec, pred, occ, ya, yb) local c, s = cos(ang), sin(ang) local ex, ey = sqrt((rx * c) ^ 2 + (ry * s) ^ 2), sqrt((rx * s) ^ 2 + (ry * c) ^ 2) for y = max(ya or 0, floor(cy - ey)), min(yb or h - 1, ceil(cy + ey)) do for x = max(0, floor(cx - ex)), min(w - 1, ceil(cx + ex)) do local dx, dy = x + 0.5 - cx, y + 0.5 - cy local u, v = (dx * c + dy * s) / rx, (dy * c - dx * s) / ry local r2 = u * u + v * v if r2 <= 1 and (not pred or pred(x + 0.5, y + 0.5)) then local k = shade(u * c - v * s, u * s + v * c, sqrt(1 - r2), spec) if occ then k = max(0, k - floor(occ(x + 0.5, y + 0.5) * 15 + 0.5)) end g[y * w + x + 1] = bi + k end end end end local function rows_of(g, w, h) local rows = {} for y = 0, h - 1 do rows[y + 1] = char(unpack(g, y * w + 1, y * w + w)) end return concat(rows) end local FUR, CRM, PNK, BRY = 1, 17, 33, 49 local HW, HH = 40, 28 local HEAD0, HEAD1, BODY, ARM, PAW = nil, nil, nil, nil, nil local BERRY = {} local HG local function head_ears(g) ell(g, HW, HH, 8.5, 5, 4.6, 4.6, 0, FUR, 0.3) ell(g, HW, HH, 31.5, 5, 4.6, 4.6, 0, FUR, 0.3) ell(g, HW, HH, 8.5, 5.6, 2.6, 2.6, 0, PNK, 0.3) ell(g, HW, HH, 31.5, 5.6, 2.6, 2.6, 0, PNK, 0.3) end local function head_top(g, ya, yb) ell(g, HW, HH, 20, 14.5, 11.5, 9, 0, FUR, 0.5, nil, function(x, y) return y < 9 and 0.2 or 0 end, ya, yb) end local function head_face(g) ell(g, HW, HH, 20, 14.5, 11.5, 9, 0, CRM, 0.4, function(x, y) return ((x - 20) / 2.8) ^ 2 + ((y - 9) / 6.5) ^ 2 < 1 end) ell(g, HW, HH, 20, 19.5, 6, 4, 0, CRM, 0.6) end local HG0 local function head0a() HG = grid(HW, HH); head_ears(HG); head_top(HG, 0, 13) end local function head0b() head_top(HG, 14, HH - 1) end local function head0c() HG0 = {} for i = 1, #HG do HG0[i] = HG[i] end head_face(HG) HEAD0 = rows_of(HG, HW, HH) end local function head1a() HG = HG0 ell(HG, HW, HH, 9.5, 18, 6.8, 6.2, 0, FUR, 0.4) ell(HG, HW, HH, 30.5, 18, 6.8, 6.2, 0, FUR, 0.4) end local function head1b() head_face(HG) HEAD1 = rows_of(HG, HW, HH) HG, HG0 = nil, nil end local BW, BH = 30, 24 local BG_GRID local function body_a() BG_GRID = grid(BW, BH) ell(BG_GRID, BW, BH, 15, 12, 13.5, 10.5, 0, FUR, 0.3, nil, nil, 0, 11) end local function body_b() ell(BG_GRID, BW, BH, 15, 12, 13.5, 10.5, 0, FUR, 0.3, nil, nil, 12, BH - 1) end local function body_c() local g = BG_GRID ell(g, BW, BH, 15, 12, 13.5, 10.5, 0, CRM, 0.3, function(x, y) return ((x - 15) / 8.5) ^ 2 + ((y - 14) / 9) ^ 2 < 1 end) ell(g, BW, BH, 8.5, 22, 3.4, 1.8, 0, PNK, 0.3) ell(g, BW, BH, 21.5, 22, 3.4, 1.8, 0, PNK, 0.3) BODY = rows_of(g, BW, BH) BG_GRID = nil end local function small() local g = grid(14, 9) ell(g, 14, 9, 6.5, 4.5, 6.4, 2.4, 0.5, FUR, 0.3) ell(g, 14, 9, 10.8, 6.4, 2.4, 1.8, 0, PNK, 0.3) ARM = rows_of(g, 14, 9) g = grid(6, 5) ell(g, 6, 5, 3, 2.5, 2.8, 2.2, 0, PNK, 0.3) PAW = rows_of(g, 6, 5) for i, r in ipairs({ 4.5, 3.5, 2.5 }) do local n = floor(r * 2 + 1) g = grid(n, n) ell(g, n, n, n / 2, n / 2, r - 0.1, r - 0.1, 0, BRY, 1.0) g[floor(n / 2) + 1] = 65 BERRY[i] = { s = rows_of(g, n, n), n = n } end end local BG, FG = nil, nil local KEY = { 255, 0, 255 } local function floor_col(y) local t = (y - 54) / 10 return floor(214 - 40 * t + 0.5), floor(172 - 44 * t + 0.5), floor(116 - 36 * t + 0.5) end local function mound_y(x) return floor(53 + 1.4 * sin(x * 0.37) + 1.3 * sin(x * 0.13 + 1) + (x > 30 and (x - 30) * 0.18 or 0) + 0.5) end local function disc(cx, cy, r, cr, cg, cb, a) for y = -r, r, 2 do local hw = floor(sqrt(r * r - y * y)) blend_rect(cx - hw, cy + y, 2 * hw, 2, cr, cg, cb, a) end end local function bg_a() for y = 0, 53 do local t = y / 53 rect(0, y, W, 1, floor(252 - 12 * t + 0.5), floor(224 - 54 * t + 0.5), floor(176 - 38 * t + 0.5)) end for y = 54, H - 1 do rect(0, y, W, 1, floor_col(y)) end BG = snapshot(0, 0, W, H) end local function bg_b() for i = 1, 7 do local h = hash32(i * 0x9E3779B1 + 9) disc(30 + umod(h, 32), 4 + umod(h >> 8, 36), 4 + umod(h >> 16, 6), 255, 236 - (i % 3) * 14, 176 + (i % 3) * 10, 0.13) end disc(14, 30, 22, 255, 240, 190, 0.07) disc(14, 30, 15, 255, 240, 190, 0.07) BG = snapshot(0, 0, W, H) end local function bg_c() for i = 1, 70 do local h = hash32(i * 0x7FEB352D + 3) local x, y = umod(h, W), 54 + umod(h >> 8, 10) local a = umod(h >> 16, 2) == 0 rect(x, y, 1 + umod(h >> 18, 2), 1, a and 238 or 150, a and 208 or 110, a and 154 or 66) end BG = snapshot(0, 0, W, H) end local function fg_a() rect(0, 46, 50, 18, KEY[1], KEY[2], KEY[3]) for x = 0, 47 do local y = mound_y(x) rect(x, y, 1, H - y, 222, 184, 128) rect(x, y, 1, 1, 242, 212, 158) rect(x, H - 3, 1, 3, 188, 148, 98) end FG = snapshot(0, 46, 50, 18) blit_rgb(BG, 0, 0, W, H) end local function fg_b() blit_rgb(FG, 0, 46, 50, 18) for i = 1, 90 do local h = hash32(i * 0x2545F491 + 7) local x = umod(h, 47) local y = mound_y(x) + 1 + umod(h >> 8, H - mound_y(x) - 1) local k = umod(h >> 16, 3) rect(x, y, 1 + (k > 0 and 1 or 0), 1, k == 0 and 150 or 244, k == 0 and 110 or 214, k == 0 and 66 or 160) end for i = 1, 14 do local h = hash32(i * 0x9E3779B1 + 17) local x = umod(h, 46) local y = mound_y(x) rect(x, y - 1, 2, 1, 236, 204, 148); px(x + 2, y - 2, 236, 204, 148) end FG = snapshot(0, 46, 50, 18) blit_rgb(BG, 0, 0, W, H) end local STAGES = { bg_a, bg_b, bg_c, fg_a, fg_b, head0a, head0b, head0c, head1a, head1b, body_a, body_b, body_c, small } local STAGE = 1 local TCYC = 420 local BX, BY = 3, 35 local HX0, HY0 = -2, 18 local GY = 51 local function smooth(u) return u * u * (3 - 2 * u) end local function draw_berry(cx, cy, i) local b = BERRY[i] if b then blit_indexed(b.s, PAL, floor(cx - b.n / 2 + 0.5), floor(cy - b.n / 2 + 0.5), b.n, b.n, b.n, b.n, true) end end function render() if not PAL then on_settings() end if STAGE <= #STAGES then if BG then blit_rgb(BG, 0, 0, W, H) end STAGES[STAGE]() STAGE = STAGE + 1 return end blit_rgb(BG, 0, 0, W, H) local t = frame * STEP_HZ // FPS % TCYC local bx, by, bs = nil, nil, 1 if t < 18 then local u = t / 18 bx, by = 36 - 0.5 * u, -4 + 57 * u * u elseif t < 28 then local u = (t - 18) / 10 bx, by = 35.5 - 1.5 * u, GY - 7 * 4 * u * (1 - u) elseif t < 34 then local u = (t - 28) / 6 bx, by = 34 - 0.5 * u, GY - 2 * 4 * u * (1 - u) elseif t < 86 then bx, by = 33.5, GY elseif t < 104 then local u = smooth((t - 86) / 18) bx, by = 33.5 - 15.5 * u, GY - 11 * u - 4 * sin(u * 3.14159) elseif t < 130 then bx, by = 18, GY - 11 bs = t < 112 and 1 or (t < 120 and 2 or 3) end local hx, hy = 0, 0 local full = (t >= 130 and t < 380) if t >= 60 and t < 72 then hx, hy = 1, 1 elseif t >= 72 and t < 86 then hx, hy = 2, 3 elseif t >= 86 and t < 104 then hx, hy = 1, 2 elseif t >= 104 and t < 130 then hx, hy = 0, 1 + (t // 4) % 2 elseif (t >= 130 and t < 170) or (t >= 340 and t < 380) then hy = (t // 4) % 2 end local chew = (t >= 104 and t < 170) or (t >= 340 and t < 380) local sniff = (t >= 34 and t < 60) or (t >= 230 and t < 290) local ground_a = 0.18 if bx and bs == 1 and by >= GY - 1 then blend_rect(floor(bx - 3), GY + 4, 7, 1, 90, 50, 20, ground_a) end blit_indexed(BODY, PAL, BX, BY, BW, BH, BW, BH, true) local reach = t >= 72 and t < 90 if reach then blit_indexed(ARM, PAL, 24, 44, 14, 9, 14, 9, true) end local hxw, hyw = HX0 + hx, HY0 + hy blit_indexed(full and HEAD1 or HEAD0, PAL, hxw, hyw, HW, HH, HW, HH, true) local ex = reach and 1 or 0 local shut = chew or (t % 150 >= 22 and t % 150 < 26) local wink = t >= 296 and t < 308 local function eye(x, closed) if closed then rect(hxw + x - 1, hyw + 14, 3, 1, 40, 22, 14) else rect(hxw + x + ex, hyw + 12, 2, 3, 30, 16, 12) px(hxw + x + ex, hyw + 12, 255, 255, 255) end end eye(13, shut) eye(25, shut or wink) local nd = (sniff and (t // 3) % 2 == 1) and 1 or 0 rect(hxw + 19, hyw + 17 + nd, 2, 2, 232, 112, 134) px(hxw + 19, hyw + 17 + nd, 255, 196, 204) px(hxw + 18, hyw + 20, 140, 60, 70); px(hxw + 21, hyw + 20, 140, 60, 70) px(hxw + 19, hyw + 21 - (chew and (t // 2) % 2 or 0), 140, 60, 70); px(hxw + 20, hyw + 21 - (chew and (t // 2) % 2 or 0), 140, 60, 70) local wy = hyw + 18 + nd local wk = full and 0.5 or 1 blend_rect(hxw + 6, wy - 1, 8, 1, 255, 248, 236, 0.45 * wk); blend_rect(hxw + 5, wy + 1, 9, 1, 255, 248, 236, 0.4 * wk) blend_rect(hxw + 7, wy + 3, 7, 1, 255, 248, 236, 0.32 * wk) blend_rect(hxw + 26, wy - 1, 8, 1, 255, 248, 236, 0.45 * wk); blend_rect(hxw + 26, wy + 1, 9, 1, 255, 248, 236, 0.4 * wk) blend_rect(hxw + 26, wy + 3, 7, 1, 255, 248, 236, 0.32 * wk) if not (t >= 90 and t < 130) then blit_indexed(PAW, PAL, 11, 44, 6, 5, 6, 5, true) if not reach then blit_indexed(PAW, PAL, 19, 44, 6, 5, 6, 5, true) end end blit_rgb(FG, 0, 46, 50, 18, KEY[1], KEY[2], KEY[3]) if bx then draw_berry(bx, by, bs) end if t >= 90 and t < 130 then local py0 = floor(by) - 1 blit_indexed(PAW, PAL, 11, py0, 6, 5, 6, 5, true) blit_indexed(PAW, PAL, 19, py0, 6, 5, 6, 5, true) end end