info = { type = "image", title = "Barrel fire and cat", category = "city", author = "BRRRO", version = "1.0", license = "MIT", desc = "A stray cat warms up by a burning barrel", api = 44, } local ltr = ({ sk = { ["Cat"] = "Mačka", ["Flames"] = "Plamene", ["Smoke"] = "Dym" }, cs = { ["Cat"] = "Kočka", ["Flames"] = "Plameny", ["Smoke"] = "Kouř" }, })[lang] or {} local function LT(s) return ltr[s] or s end settings = { { key = "cat", label = LT("Cat"), type = "bool", default = true }, { key = "flame", label = LT("Flames"), type = "int", min = 1, max = 5, default = 3 }, { key = "smoke", label = LT("Smoke"), type = "int", min = 0, max = 5, default = 3 }, } local rect, px, blend_rect, blend = gfx.rect, gfx.px, gfx.blend_rect, gfx.blend local blit_rgb, blit_indexed, blend_mask, snapshot = gfx.blit_rgb, gfx.blit_indexed, gfx.blend_mask, 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 char, unpack, concat = string.char, table.unpack, table.concat local W, H, FPS = gfx.W, gfx.H, gfx.FPS local GY = 58 local BX0, BY0, BW, BH = 6, 38, 16, 21 local FX, FYC = 14, 36 local GW, GH, GOY = 48, 47, 12 local function h15(a, b) return hash32(a * 7919 + b * 104729) & 0x7FFF end local PART, BG, GLOW = nil, nil, nil local function wall() for y = 0, 11 do rect(0, y, W, 1, 10 + y, 14 + y * 1.3 // 1, 40 + y * 3) end for i = 1, 14 do local h = hash32(i * 0x9E3779B1) px(umod(h, W), umod(h >> 8, 8), 150 + umod(h >> 16, 80), 160 + umod(h >> 16, 70), 230) end rect(52, 3, 4, 1, 214, 220, 238); rect(51, 4, 2, 2, 214, 220, 238); rect(52, 6, 4, 1, 214, 220, 238) rect(54, 4, 2, 2, 20, 28, 66) rect(0, 10, W, 2, 14, 16, 34) rect(44, 5, 5, 6, 14, 16, 34) end local function bricks() rect(0, 12, W, 35, 42, 46, 84) for r = 0, 8 do local y = 12 + r * 4 rect(0, y + 3, W, 1, 42, 46, 84) for i = -1, 8 do local x = i * 8 + (r % 2) * 4 local k = h15(r, i) % 17 - 8 rect(x, y, 7, 3, 70 + k, 76 + k, 122 + k) end end end local function details() rect(36, 16, 13, 16, 30, 34, 62) rect(38, 18, 9, 11, 14, 20, 44) rect(42, 18, 1, 11, 30, 34, 62); rect(38, 23, 9, 1, 30, 34, 62) px(39, 19, 52, 70, 118); px(40, 20, 52, 70, 118); px(39, 20, 52, 70, 118) rect(35, 32, 15, 2, 56, 62, 98) rect(26, 21, 7, 9, 112, 110, 142) rect(27, 23, 5, 1, 70, 66, 96); rect(27, 25, 5, 1, 70, 66, 96); rect(27, 27, 3, 1, 70, 66, 96) rect(57, 12, 2, 35, 46, 52, 86); rect(57, 12, 1, 35, 62, 70, 108) rect(56, 22, 4, 1, 30, 34, 60); rect(56, 38, 4, 1, 30, 34, 60) end local function ground() rect(0, 47, W, H - 47, 34, 38, 66) rect(0, 47, W, 1, 76, 82, 122); rect(0, 48, W, 1, 50, 56, 92) rect(0, 54, W, 1, 28, 32, 58); rect(0, 61, W, 1, 28, 32, 58) for i = 0, 9 do px(3 + i * 7 + (i % 2) * 3, 50 + (i % 3) * 3, 52, 58, 96) end rect(34, 60, 18, 2, 44, 56, 100); rect(37, 60, 8, 1, 70, 86, 140) end local GROWS = {} local function glow_rows(ya, yb) for y = ya, yb do local c = {} local v = ((y + GOY - FYC) / 21) ^ 2 for x = 0, GW - 1 do local d = sqrt(((x - FX) / 25) ^ 2 + v) c[x + 1] = char(d >= 1 and 0 or floor((1 - d) * 255 + 0.5)) end GROWS[y + 1] = concat(c) end if yb == GH - 1 then GLOW = concat(GROWS) end end local barrel local STAGES = { wall, bricks, details, ground, function() glow_rows(0, 9) end, function() glow_rows(10, 19) end, function() glow_rows(20, 29) end, function() glow_rows(30, 39) end, function() glow_rows(40, GH - 1) end, function() barrel() end, } local STAGE = 1 local BPAL = { 0, 0, 0 } do local S0, S1 = { 28, 48, 62 }, { 92, 126, 140 } for k = 0, 5 do local t = k / 5 for j = 1, 3 do BPAL[#BPAL + 1] = floor(S0[j] + (S1[j] - S0[j]) * t + 0.5) end end for k = 0, 5 do local t = k / 5 for j = 1, 3 do BPAL[#BPAL + 1] = floor((S0[j] + (S1[j] - S0[j]) * t) * 0.6 + 0.5) end end for _, c in ipairs({ 100, 56, 38, 214, 150, 96, 120, 84, 70, 14, 22, 32 }) do BPAL[#BPAL + 1] = c end end local BARREL barrel = function() local rows = {} for y = 0, BH - 1 do local c = {} for x = 0, BW - 1 do local v if y == 0 then v = 14 elseif y == 1 then v = 15 elseif x == 0 or x == BW - 1 then v = 16 else local g = floor(5 * (1 - (math.abs(x - 5.5) / 8.5) ^ 1.5) + 0.5) g = max(0, min(5, g)) + 1 v = g if y == 6 or y == 7 or y == 16 or y == 17 then v = g + 6 end if h15(x, y) % 23 == 0 and y > 9 then v = 13 end end c[x + 1] = char(v) end rows[y + 1] = concat(c) end BARREL = concat(rows) end local function grid(w, h) local g = { w = w, h = h } for y = 1, h do local r = {} for x = 1, w do r[x] = 0 end g[y] = r end return g end local function box(g, x, y, w, h, v) for j = y + 1, y + h do local r = g[j] if r then for i = x + 1, x + w do if r[i] then r[i] = v end end end end end local function dot(g, x, y, v) box(g, x, y, 1, 1, v) end local POSES = { { -1, 1, 1, -1, 0, 1, 0, 1 }, { 0, 0, 0, 0, 0, 0, 0, 0 }, { 1, -1, -1, 1, 1, 0, 1, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0 }, } local function cat_walk(pose, dy) local g = grid(18, 12) local p = POSES[pose] local lh = 4 - dy local function leg(x, off, lift, v) box(g, x + off, 8 + dy, 1, lh - lift, v) end leg(7, p[2], p[6], 2); leg(10, p[4], p[8], 2) box(g, 4, 3 + dy, 10, 5, 1) dot(g, 13, 3 + dy, 0); dot(g, 13, 7 + dy, 0); dot(g, 4, 7 + dy, 0) box(g, 0, 2 + dy, 5, 4, 1) box(g, 0, dy, 1, 2, 1); box(g, 3, dy, 1, 2, 1) dot(g, 1, 3 + dy, 4); dot(g, 0, 4 + dy, 5) box(g, 14, 5 + dy, 1, 1, 1); box(g, 15, 4 + dy, 1, 1, 1); box(g, 16, 2 + dy, 1, 2, 1); box(g, 17, 1 + dy, 1, 1, 1) leg(5, p[1], p[5], 1); leg(12, p[3], p[7], 1) return g end local SPANS = { { 7, 3, 7 }, { 8, 3, 9 }, { 9, 3, 11 }, { 10, 3, 12 }, { 11, 3, 13 }, { 12, 3, 13 }, { 13, 3, 13 } } local function cat_sit(tail, eyes, paw) local g = grid(16, 15) for _, s in ipairs(SPANS) do box(g, s[2], s[1], s[3] - s[2] + 1, 1, 1) end box(g, 1, 2, 6, 5, 1) box(g, 1, 0, 1, 2, 1); box(g, 5, 0, 1, 2, 1) if paw then box(g, 2, 9, 2, 3, 1); box(g, 0, 9, 3, 2, 1) else box(g, 2, 9, 2, 6, 1) end box(g, 5, 14, 10, 1, 2) if tail then dot(g, 4, 14, 2); dot(g, 4, 13, 2); dot(g, 4, 12, 2) else dot(g, 4, 14, 2) end dot(g, 3, 4, eyes and 4 or 2); dot(g, 1, 5, 5) return g end local function mirror(g) for y = 1, g.h do local r, n = g[y], {} for x = 1, g.w do n[x] = r[g.w + 1 - x] end g[y] = n end return g end local function finish(g) local at = function(x, y) local r = g[y]; return r and r[x] or 0 end local out, edge = {}, {} for y = 1, g.h do local c, e = {}, {} for x = 1, g.w do local v = g[y][x] local o = v if v == 1 or v == 2 then if at(x - 1, y) == 0 then o = v == 1 and 3 or 7 elseif v == 1 and at(x, y - 1) == 0 then o = 7 end end c[x] = char(o) e[x] = (v ~= 0 and at(x + 1, y) == 0) and "\255" or "\0" end out[y], edge[y] = concat(c), concat(e) end return { s = concat(out), edge = concat(edge), w = g.w, h = g.h } end local CACHE = {} local function sprite(key, make) local s = CACHE[key] if not s then s = finish(make()); CACHE[key] = s end return s end local PALC = { 0, 0, 0, 24, 20, 36, 12, 10, 22, 0, 0, 0, 232, 188, 70, 164, 96, 120, 0, 0, 0, 0, 0, 0 } local BODYC = { 24, 20, 36 } local function put(sp, fx, y) local ix = floor(fx) local ax = fx - ix if ax > 0.06 then blend_mask(sp.edge, BODYC[1], BODYC[2], BODYC[3], ix + 1, y, sp.w, sp.h, sp.w, sp.h, ax) end blit_indexed(sp.s, PALC, ix, y, sp.w, sp.h, sp.w, sp.h, true) end local NC = 13 local FL = {} for i = 0, NC - 1 do local h = hash32(i * 0x85EBCA6B) FL[i] = { x = 8 + i, base = 15 - 8 * (math.abs(i - 6) / 6) ^ 1.3, a = 5 + umod(h, 60) / 10, p = umod(h >> 6, 628) / 100, b = 8 + umod(h >> 12, 60) / 10, q = umod(h >> 18, 628) / 100 } end local function col(x, top, bot, r, g, b) local it = floor(top) local f = top - it if bot > it + 1 then rect(x, it + 1, 1, bot - it - 1, r, g, b) end if f < 0.95 then blend(x, it, r, g, b, 1 - f) end end local NS = 9 local SPK = {} for i = 1, NS do local h = hash32(i * 0x27D4EB2F) SPK[i] = { L = 1.4 + umod(h, 14) / 10, x0 = 9 + umod(h >> 4, 11), vy = 14 + umod(h >> 9, 16), vx = umod(h >> 14, 11) - 4, ph = umod(h >> 19, 100) / 100, w = 2 + umod(h >> 22, 20) / 10 } end local NP = 5 local PUF = {} for i = 1, NP do local h = hash32(i * 0xC2B2AE35) PUF[i] = { L = 3.6 + umod(h, 16) / 10, x0 = 11 + umod(h >> 5, 7), v = 7 + umod(h >> 9, 30) / 10, vx = 3 + umod(h >> 14, 20) / 10, ph = umod(h >> 18, 100) / 100 } end local SOFT do local rows = {} for y = 0, 15 do local c = {} for x = 0, 15 do local d = sqrt((x - 7.5) ^ 2 + (y - 7.5) ^ 2) / 8 c[x + 1] = char(d >= 1 and 0 or floor((1 - d) * 255 + 0.5)) end rows[y + 1] = concat(c) end SOFT = concat(rows) end local LOOP = 900 local T_WALK_IN, T_STAND, T_CROUCH, T_SIT, T_UP, T_IDLE, T_OUT, T_GONE = 100, 206, 230, 242, 662, 674, 698, 774 local STOP = 28 local function cat(f, lit) if f < T_WALK_IN or f >= T_GONE then return end PALC[10], PALC[11], PALC[12] = 236 * lit, 124 * lit, 44 * lit PALC[22], PALC[23], PALC[24] = 150 * lit, 76 * lit, 34 * lit PALC[10], PALC[11], PALC[12] = floor(PALC[10]), floor(PALC[11]), floor(PALC[12]) PALC[22], PALC[23], PALC[24] = floor(PALC[22]), floor(PALC[23]), floor(PALC[24]) local sp, fx, y if f < T_STAND then local d = (f - T_WALK_IN) * 0.36 sp, fx = sprite("w" .. (floor(d / 2.4) % 4 + 1), function() return cat_walk(floor(d / 2.4) % 4 + 1, 0) end), 66 - d y = GY - 11 elseif f < T_CROUCH then sp, fx, y = sprite("w2", function() return cat_walk(2, 0) end), STOP, GY - 11 elseif f < T_SIT then sp, fx, y = sprite("c", function() return cat_walk(2, 2) end), STOP, GY - 11 elseif f < T_UP then local s = f - T_SIT local tail = s % 110 < 24 local eyes = not ((s % 240 >= 100 and s % 240 < 170) or s % 130 < 4) local paw = (s >= 70 and s < 130) or (s >= 250 and s < 320) local k = "s" .. (tail and 1 or 0) .. (eyes and 1 or 0) .. (paw and 1 or 0) sp, fx, y = sprite(k, function() return cat_sit(tail, eyes, paw) end), STOP - 1, GY - 14 elseif f < T_IDLE then sp, fx, y = sprite("c", function() return cat_walk(2, 2) end), STOP, GY - 11 elseif f < T_OUT then sp, fx, y = sprite("w2", function() return cat_walk(2, 0) end), STOP, GY - 11 else local d = (f - T_OUT) * 0.5 local pi = floor(d / 2.8) % 4 + 1 sp, fx, y = sprite("m" .. pi, function() return mirror(cat_walk(pi, 0)) end), STOP + d, GY - 11 end blend_rect(floor(fx) + 3, GY + 1, sp.w + 4, 1, 6, 8, 20, 0.55) blend_rect(floor(fx) + 5, GY + 2, sp.w, 1, 6, 8, 20, 0.3) put(sp, fx, y) end function render() if STAGE <= #STAGES then if BG then blit_rgb(BG, 0, 0, W, H) elseif STAGE > 1 then blit_rgb(PART, 0, 0, W, H) end STAGES[STAGE]() if STAGE <= 3 then PART = snapshot(0, 0, W, H) end if STAGE == 4 then BG = snapshot(0, 0, W, H) end STAGE = STAGE + 1 gfx.ready(false) return end local t = frame / FPS local flick = 0.82 + 0.1 * sin(t * 9.1) + 0.08 * sin(t * 5.3 + 1.1) + 0.05 * sin(t * 17.3) blit_rgb(BG, 0, 0, W, H) blend_mask(GLOW, 255, 126, 34, 0, GOY, GW, GH, GW, GH, 0.62 * flick) blend_rect(8, 60, 12, 1, 255, 126, 34, 0.16 * flick) blend_rect(7, 61, 14, 1, 255, 126, 34, 0.12 * flick) blend_rect(9, 62, 10, 1, 255, 126, 34, 0.08 * flick) blit_indexed(BARREL, BPAL, BX0, BY0, BW, BH, BW, BH) blend_rect(BX0 + 1, BY0 + 2, BW - 2, 9, 255, 126, 34, 0.26 * flick) if cfg.cat ~= false then local lit = max(0.3, 1.05 - (STOP - 8) / 46) * flick cat(frame % LOOP, lit) end local k = 0.6 + 0.2 * (cfg.flame or 3) for i = 0, NC - 1 do local c = FL[i] local s = 0.5 + 0.5 * (0.6 * sin(t * c.a + c.p) + 0.4 * sin(t * c.b + c.q)) local h = c.base * k * (0.5 + 0.5 * s) local x = c.x col(x, BY0 - h, BY0, 216, 44, 28) col(x, BY0 - h * 0.72, BY0, 255, 136, 30) col(x, BY0 - h * 0.46, BY0, 255, 214, 72) if i >= 3 and i <= 9 then rect(x, BY0 - floor(h * 0.2 + 0.5), 1, floor(h * 0.2 + 0.5), 255, 244, 180) end end for i = 1, NS do local s = SPK[i] local tau = ((t / s.L + s.ph) % 1) * s.L local p = tau / s.L local yf = BY0 - 8 - s.vy * tau + 6 * tau * tau local iy = floor(yf) local f = yf - iy local x = floor(s.x0 + s.vx * tau + 1.2 * sin(tau * s.w) + 0.5) local a = p > 0.7 and (1 - p) / 0.3 or 1 local r, g, b if p < 0.35 then r, g, b = 255, 220, 100 elseif p < 0.7 then r, g, b = 255, 140, 40 else r, g, b = 200, 56, 24 end blend(x, iy, r, g, b, (1 - f) * a) blend(x, iy + 1, r, g, b, f * a) end for i = 1, cfg.smoke or 3 do local s = PUF[i] local tau = ((t / s.L + s.ph) % 1) * s.L local p = tau / s.L local r = floor(2 + 5 * p + 0.5) local x = floor(s.x0 + s.vx * tau + 0.5) local y = floor(BY0 - 15 - s.v * tau + 0.5) local a = 0.3 * sin(3.14159 * p) ^ 0.8 local w = 1 - p blend_mask(SOFT, floor(120 * w + 64 * p), floor(96 * w + 70 * p), floor(92 * w + 100 * p), x - r, y - r, 2 * r + 1, 2 * r + 1, 16, 16, a) end end