info = { type = "image", title = "Cone tower", category = "animals", author = "BRRRO", version = "1.0", license = "MIT", desc = "A penguin balances a wobbly ice cream tower", api = 33, } local ltr = ({ sk = { ["Scoops"] = "Gule", ["Melting drip"] = "Topiaca sa kvapka" }, cs = { ["Scoops"] = "Kopečky", ["Melting drip"] = "Tající kapka" }, })[lang] or {} local function LT(s) return ltr[s] or s end settings = { { key = "scoops", label = LT("Scoops"), type = "int", min = 3, max = 7, default = 5 }, { key = "drip", label = LT("Melting drip"), type = "bool", default = true }, } local rect, px, blend, blend_rect = gfx.rect, gfx.px, gfx.blend, gfx.blend_rect local floor, sin = math.floor, math.sin local byte, char, concat, unpack = string.byte, string.char, table.concat, table.unpack local W, H, FPS = gfx.W, gfx.H, gfx.FPS local TAU = 6.28318 local WALL, DOT = { 212, 232, 246 }, { 196, 222, 242 } local FLOOR, FLOOR_HI = { 180, 206, 228 }, { 198, 220, 238 } local SHADOW = { 160, 188, 214 } local FOOT = { 250, 150, 50 } local CHERRY, STEM = { 222, 32, 54 }, { 92, 142, 62 } local function c3(c) return c[1], c[2], c[3] end local FL = { { 246, 150, 172, 220, 112, 140 }, { 150, 226, 190, 108, 194, 154 }, { 158, 104, 78, 122, 76, 54 }, { 252, 238, 200, 226, 204, 156 }, { 172, 156, 238, 134, 118, 210 }, { 252, 196, 88, 228, 158, 58 }, { 184, 212, 120, 148, 180, 90 }, } local HI = {} for i = 1, #FL do local f = FL[i] HI[i] = { floor(f[1] + (255 - f[1]) * 0.55), floor(f[2] + (255 - f[2]) * 0.55), floor(f[3] + (255 - f[3]) * 0.55) } end local PAL = { 38, 50, 84, 70, 88, 128, 250, 252, 255, 210, 222, 238, 24, 24, 40, 250, 150, 50, 212, 108, 34, 252, 170, 180, 236, 180, 104, 204, 142, 72, 224, 162, 90, 186, 122, 58 } local MAP = { ["."] = "\0", K = "\1", k = "\2", W = "\3", w = "\4", E = "\5", O = "\6", o = "\7", P = "\8", C = "\9", c = "\10", T = "\11", t = "\12" } local BODY = { "......KKKKKKKK......", "....KKKKKKKKKKKK....", "...KKkKKKKKKKKKKK...", "..KKkKKWWKKWWKKKKK..", "..KKKKWWWWWWWWKKKK..", ".KKKKWWWWWWWWWWKKKK.", ".KKKWWEWWWWEWWWWKKK.", ".KKKWWEEWWWEEWWWKKK.", ".KKKPPWWWOOWWWPPKKK.", ".KKKKWWWWooWWWWKKKK.", ".KKKKKWWWWWWWWKKKKK.", "KKKKKKKWWWWWWKKKKKKK", "KKKKKKWWWWWWWWKKKKKK", "KKKKKWWWWWWWWWWKKKKK", "KKKKKWWWWWWWWWWKKKKK", "KKKKKWWWWWWWWWWKKKKK", ".KKKKWWWWWWWWWWKKKK.", ".KKKKwWWWWWWWWwKKKK.", "..KKKKwwWWWWwwKKKK..", "...KKKKKwwwwKKKKK...", } local BLINK6 = ".KKKWWWWWWWWWWWWKKK." local CONE = { "CCCCCCCCC", "cCCcCCcCc", ".TtTTtTT.", ".TTtTTtT.", "..TtTTt..", "..tTTtT..", "..TTtTT..", "...TtT...", "...tTT...", "....T....", "....t....", } local function cells(rows) local t = {} for i = 1, #rows do t[i] = (rows[i]:gsub(".", MAP)) end return concat(t) end local PW, PH = #BODY[1], #BODY local CW, CH = #CONE[1], #CONE local BODY_C = { [0] = cells(BODY) } BODY[7] = BLINK6 BODY_C[1] = cells(BODY) local CONE_C = cells(CONE) local KR, KG, KB = 255, 0, 255 local NP = 4 local function shift_x(cs, w, h, f) local out, row = {}, {} local g = 1 - f local br, bg, bb = c3(WALL) for y = 0, h - 1 do local base, k = y * w, 0 for x = 0, w do local a = (x >= 1 and f > 0) and byte(cs, base + x) or 0 local b = (x < w and g > 0) and byte(cs, base + x + 1) or 0 local r1, g1, b1 = KR, KG, KB if a ~= 0 or b ~= 0 then local ar, ag, ab, cr, cg, cb = br, bg, bb, br, bg, bb if a ~= 0 then ar, ag, ab = PAL[3 * a - 2], PAL[3 * a - 1], PAL[3 * a] end if b ~= 0 then cr, cg, cb = PAL[3 * b - 2], PAL[3 * b - 1], PAL[3 * b] end r1 = floor(ar * f + cr * g + 0.5) g1 = floor(ag * f + cg * g + 0.5) b1 = floor(ab * f + cb * g + 0.5) end row[k + 1], row[k + 2], row[k + 3] = r1, g1, b1 k = k + 3 end out[y + 1] = char(unpack(row, 1, k)) end return concat(out) end local CONE_R = {} local BODY_R, built = { [0] = {}, [1] = {} }, false local function span(xl, xr, y, h, r, g, b) local il, ir = floor(xl), floor(xr) if ir - il > 1 then rect(il + 1, y, ir - il - 1, h, r, g, b) end local cl, cr = 1 - (xl - il), xr - ir if il == ir then cl = xr - xl; cr = 0 end if cl > 0.02 then blend_rect(il, y, 1, h, r, g, b, cl) end if cr > 0.02 then blend_rect(ir, y, 1, h, r, g, b, cr) end end local BG = nil local function build_bg() rect(0, 0, W, 57, c3(WALL)) for j = 0, 4 do for i = 0, 7 do px(3 + i * 8 + (j % 2) * 4, 3 + j * 7, c3(DOT)) end end rect(0, 57, W, H - 57, c3(FLOOR)) rect(0, 57, W, 1, c3(FLOOR_HI)) rect(40, 58, 16, 1, c3(SHADOW)); rect(42, 59, 12, 1, c3(SHADOW)) rect(42, 57, 4, 2, c3(FOOT)); rect(50, 57, 4, 2, c3(FOOT)) rect(42, 58, 1, 1, 220, 124, 36); rect(53, 58, 1, 1, 220, 124, 36) BG = gfx.snapshot(0, 0, W, H) end local NS, DRIP = 5, true function on_settings() NS = cfg.scoops or 5 DRIP = cfg.drip ~= false end local XC, CY = 31, 44 local PX0, PY0 = 37, 37 local W0 = TAU / 2.8 local LAG = 0.167 local function base(t) return 1.3 * sin(W0 * t) end local function scoop(i, x, y) local f = FL[i] local r, g, b = f[1], f[2], f[3] span(x - 2.3, x + 2.3, y, 1, r, g, b) span(x - 3.8, x + 3.8, y + 1, 2, r, g, b) span(x - 4.55, x + 4.55, y + 3, 3, r, g, b) span(x - 3.8, x + 3.8, y + 6, 2, f[4], f[5], f[6]) span(x - 2.3, x + 2.3, y + 8, 1, f[4], f[5], f[6]) local h = HI[i] local hx = floor(x - 2 + 0.5) px(hx, y + 2, h[1], h[2], h[3]); px(hx - 1, y + 3, h[1], h[2], h[3]) end local DC = 8 local function draw_drip(t, xc) local c = t % DC if c < 1 then return end local f = FL[1] local r, g, b = f[4], f[5], f[6] local fade = 1 if c >= 5 then fade = 1 - (c - 5) / 3 end local s = (c - 1) / 4 if s > 1 then s = 1 end local yh = 46 + 8 * s for row = 46, floor(yh) - 1 do blend(floor(xc - 3 + 3 * (row - 46) / 8 + 0.5), row, r, g, b, fade) end if c < 5 then local iy = floor(yh) local fy = yh - iy local hx = floor(xc - 3 + 3 * s + 0.5) blend(hx, iy, r, g, b, 1 - fy * 0.5) blend(hx, iy + 1, r, g, b, fy) return end local tx = floor(xc + 0.5) if c < 6 then local k = c - 5 blend(tx, 55, r, g, b, 0.5 + 0.5 * k) if k > 0.6 then blend(tx, 56, r, g, b, (k - 0.6) * 2.5) end return end local tf = c - 6 local rx = floor(XC + base(t - tf) + 0.5) local y = 55 + 36 * tf * tf if y < 61 then rect(rx, floor(y + 0.5), 1, 2, r, g, b) else local age = tf - 0.407 local a = 1 - age / 1.5 if a > 0 then blend_rect(rx - 1, 61, 3, 1, r, g, b, a) if age < 0.2 then blend(rx - 2, 60, r, g, b, a * 0.7); blend(rx + 2, 60, r, g, b, a * 0.7) end end end end function render() built = false if not BG then build_bg() end gfx.blit_rgb(BG, 0, 0, W, H) local t = frame / FPS local xc = XC + base(t) local q = floor((xc - CW / 2) * NP + 0.5) local cp = q % NP local CR = CONE_R[cp] if not CR then CR = shift_x(CONE_C, CW, CH, cp / NP); CONE_R[cp] = CR end gfx.blit_rgb(CR, q // NP, CY, CW + 1, CH, KR, KG, KB) local xs, ys for k = 1, NS do xs = XC + base(t - k * LAG) * (1 + 0.45 * k) ys = 36 - (k - 1) * 5 scoop(k, xs, ys) end span(xs - 1, xs + 1, ys - 3, 1, c3(CHERRY)) span(xs - 1.5, xs + 1.5, ys - 2, 2, c3(CHERRY)) local cx = floor(xs + 0.5) px(cx - 1, ys - 2, 255, 150, 160) px(cx + 1, ys - 4, c3(STEM)); px(cx + 2, ys - 5, c3(STEM)) local px0 = PX0 - 0.7 * sin(W0 * (t - 3 * LAG)) local bl = ((t % 3.5) < 0.133 or (t % 10.5 >= 0.3 and t % 10.5 < 0.4)) and 1 or 0 q = floor(px0 * NP + 0.5) local p = q % NP local R = BODY_R[bl][p] if not R and not built then R = shift_x(BODY_C[bl], PW, PH, p / NP); BODY_R[bl][p] = R; built = true end if not R then R = BODY_R[bl][0] or shift_x(BODY_C[bl], PW, PH, 0) BODY_R[bl][0] = R q = floor(px0 + 0.5) * NP end local pxq = q / NP gfx.blit_rgb(R, q // NP, PY0, PW + 1, PH, KR, KG, KB) span(xc + 3, pxq + 2, 47, 1, PAL[1], PAL[2], PAL[3]) span(xc - 0.5, pxq + 2, 48, 2, PAL[1], PAL[2], PAL[3]) span(xc - 1, xc + 1.5, 50, 1, PAL[4], PAL[5], PAL[6]) if DRIP then draw_drip(t, xc) end end