info = { type = "image", title = "Bell jar cat", category = "animals", author = "BRRRO", version = "1.0", license = "MIT", desc = "A cat sits under a glass bell jar", api = 33, } local tr = ({ sk = { ["Cat colour"] = "Farba mačky", ["Sparkles"] = "Iskričky", ["Glass colour"] = "Farba skla" }, cs = { ["Cat colour"] = "Barva kočky", ["Sparkles"] = "Jiskřičky", ["Glass colour"] = "Barva skla" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "cat", label = T("Cat colour"), type = "color", default = "#8C8CAA" }, { key = "sparks", label = T("Sparkles"), type = "int", min = 0, max = 10, default = 7 }, { key = "glass", label = T("Glass colour"), type = "color", default = "#D2F4FA" }, } local rect, px, blend_rect = gfx.rect, gfx.px, gfx.blend_rect local floor, sin, cos, sqrt, abs = math.floor, math.sin, math.cos, math.sqrt, math.abs local W, H, FPS = gfx.W, gfx.H, gfx.FPS local PI = 3.14159265 local WALL, WALL_L = { 190, 102, 94 }, { 204, 118, 106 } local WOOD, WOOD_L, WOOD_D = { 216, 152, 96 }, { 230, 172, 116 }, { 132, 82, 52 } local EDGE, FRONT = { 112, 66, 44 }, { 150, 96, 60 } local BRASS, BRASS_L, BRASS_D = { 192, 130, 62 }, { 238, 196, 112 }, { 122, 78, 42 } local INSIDE = { 226, 164, 146 } local SKY, SKY_L = { 142, 204, 240 }, { 246, 252, 255 } local MAT, HEART = { 238, 208, 130 }, { 222, 70, 100 } local EYE, PUPIL = { 120, 224, 110 }, { 24, 18, 30 } local PINK = { 244, 150, 170 } local TAILC = { 140, 140, 170 } local CX = 32 local TOP, FLOOR_Y = 8, 44 local HWMAX = 17 local HW = {} for y = TOP, FLOOR_Y do local d = (22 - (y + 0.5)) / 14.5 HW[y] = d > 0 and floor(HWMAX * sqrt(1 - d * d) + 0.5) or HWMAX end local function c3(c) return c[1], c[2], c[3] end local function mix(c, k) return { c[1] + (255 - c[1]) * k // 100, c[2] + (255 - c[2]) * k // 100, c[3] + (255 - c[3]) * k // 100 } end local function darken(c, k) return { c[1] * k // 100, c[2] * k // 100, c[3] * k // 100 } end local BG, BGKEY = nil, nil local CAT = { body = TAILC, dark = TAILC, light = TAILC } local GLASS = { 210, 244, 250 } local function disc(cx, cy, r, c) for y = cy - r, cy + r - 1 do local dy = y + 0.5 - cy local q = r * r - dy * dy if q > 0 then local hw = sqrt(q) local xa, xb = math.ceil(cx - hw - 0.5), floor(cx + hw - 0.5) if xb >= xa then rect(xa, y, xb - xa + 1, 1, c3(c)) end end end end local function build_room() rect(0, 0, W, 45, c3(WALL)) for x = 6, W - 1, 12 do rect(x, 0, 3, 45, c3(WALL_L)) end rect(1, 3, 12, 17, c3(WOOD_D)) rect(3, 5, 3, 6, c3(SKY)); rect(7, 5, 3, 6, c3(SKY)); rect(3, 12, 3, 6, c3(SKY)); rect(7, 12, 3, 6, c3(SKY)) rect(4, 6, 1, 1, c3(SKY_L)); rect(8, 13, 1, 1, c3(SKY_L)); rect(0, 20, 14, 1, c3(EDGE)) rect(50, 5, 12, 14, c3(WOOD_D)); rect(52, 7, 8, 10, c3(MAT)) rect(53, 9, 2, 2, c3(HEART)); rect(57, 9, 2, 2, c3(HEART)); rect(53, 10, 6, 2, c3(HEART)) rect(54, 12, 4, 1, c3(HEART)); rect(55, 13, 2, 1, c3(HEART)) rect(0, 45, W, 1, c3(EDGE)) rect(0, 46, W, 9, c3(WOOD)) rect(0, 49, W, 1, c3(WOOD_L)); rect(0, 53, W, 1, c3(WOOD_L)) rect(0, 55, W, 2, c3(FRONT)); rect(0, 57, W, 7, c3(WOOD_D)) rect(10, 60, 44, 1, c3(FRONT)) end local function build_jar() rect(12, 45, 40, 1, c3(BRASS_L)); rect(11, 46, 42, 3, c3(BRASS)); rect(12, 49, 40, 1, c3(BRASS_D)) rect(13, 50, 38, 1, c3(WOOD_D)) for y = TOP, FLOOR_Y do rect(CX - HW[y], y, 2 * HW[y], 1, c3(INSIDE)) end local gr, gg, gb = GLASS[1], GLASS[2], GLASS[3] for y = TOP, FLOOR_Y do local xl, xr = CX - HW[y] - 1, CX + HW[y] local pl = y > TOP and (CX - HW[y - 1] - 1) or xl local pr = y > TOP and (CX + HW[y - 1]) or xr rect(xl, y, pl - xl + 1, 1, gr, gg, gb) rect(pr, y, xr - pr + 1, 1, gr, gg, gb) end rect(CX - HW[TOP], TOP - 1, 2 * HW[TOP], 1, gr, gg, gb) disc(CX, 5, 3, GLASS); disc(CX, 5, 2, INSIDE) rect(CX - 1, 3, 1, 1, 255, 255, 255) for y = 14, 26 do rect(CX - HW[y] + 2, y, 1, 1, 250, 224, 212) end rect(CX - HW[29] + 2, 29, 1, 2, 250, 224, 212) for y = 16, 19 do rect(CX + HW[y] - 3, y, 1, 1, 250, 224, 212) end end local function build_cat() local b, d, l = CAT.body, CAT.dark, CAT.light local BW = { 4, 5, 6, 7, 7, 8, 8, 8, 9, 9, 9, 9, 9, 9 } for i = 1, #BW do rect(CX - BW[i], 30 + i, 2 * BW[i], 1, c3(b)) end for i = 6, #BW do rect(CX - 4, 30 + i, 8, 1, c3(l)) end rect(CX - 9, 43, 7, 2, c3(l)); rect(CX + 2, 43, 7, 2, c3(l)) rect(CX - 1, 41, 2, 4, c3(b)) rect(CX - 9, 44, 18, 1, c3(l)) rect(CX - 9, 36, 3, 1, c3(d)); rect(CX - 9, 39, 3, 1, c3(d)) rect(CX + 6, 36, 3, 1, c3(d)); rect(CX + 6, 39, 3, 1, c3(d)) disc(CX, 26, 7, b) rect(CX - 1, 19, 1, 3, c3(d)); rect(CX + 1, 19, 1, 3, c3(d)); rect(CX - 3, 20, 1, 2, c3(d)); rect(CX + 3, 20, 1, 2, c3(d)) rect(CX - 1, 28, 2, 1, c3(PINK)) rect(CX - 1, 29, 1, 1, c3(d)); rect(CX, 29, 1, 1, c3(d)) rect(CX - 3, 30, 2, 1, c3(l)); rect(CX + 1, 30, 2, 1, c3(l)) local wr, wg, wb = 248, 238, 232 rect(CX - 11, 27, 3, 1, wr, wg, wb); rect(CX - 11, 29, 3, 1, wr, wg, wb) rect(CX + 8, 27, 3, 1, wr, wg, wb); rect(CX + 8, 29, 3, 1, wr, wg, wb) end local function rebuild() local cc = cfg.cat or { r = 140, g = 140, b = 170 } CAT.body = { cc.r, cc.g, cc.b } CAT.dark = { cc.r * 62 // 100, cc.g * 62 // 100, cc.b * 62 // 100 } CAT.light = mix(CAT.body, 62) TAILC = CAT.body local gc = cfg.glass or { r = 210, g = 244, b = 250 } GLASS = { gc.r, gc.g, gc.b } build_room(); build_jar(); build_cat() BG = gfx.snapshot(0, 0, W, H) end local function draw_tail(t) local d, b = CAT.dark, CAT.body local end_a = -0.9 + 0.55 * sin(t * 2.1) local N = 9 for i = 0, N do local a = PI / 2 + (end_a - PI / 2) * i / N local x = CX + 9 + 5.2 * cos(a) + 0.6 * sin(t * 2.1 + i * 0.5) * i / N local y = 38 + 5.2 * sin(a) + 0.5 * i / N local xi, yi = floor(x + 0.5), floor(y + 0.5) if i >= N - 1 then rect(xi, yi, 2, 2, c3(d)) else rect(xi, yi, 2, 2, c3(b)) end end end local function draw_head(t) local b, d = CAT.body, CAT.dark local tw = (t % 5.0) < 0.15 and 1 or 0 local function ear(o, dir, dy) for r = 0, 4 do local w = r < 4 and r + 1 or 3 local x0 = dir < 0 and (CX + o) or (CX + o - w + 1) rect(x0, 16 + r + dy, w, 1, c3(b)) end px(CX + o - dir, 18 + dy, c3(PINK)) end ear(-6, -1, 0) ear(5, 1, tw) local blink = (t % 3.7) < 4 / FPS local look = floor(((t * 0.5) % 2)) == 0 and 0 or 1 local ey = 24 if blink then rect(CX - 4, ey + 1, 3, 1, c3(d)); rect(CX + 1, ey + 1, 3, 1, c3(d)) else rect(CX - 4, ey, 3, 2, c3(EYE)); rect(CX + 1, ey, 3, 2, c3(EYE)) rect(CX - 4 + look + 0, ey, 1, 2, c3(PUPIL)); rect(CX + 1 + look, ey, 1, 2, c3(PUPIL)) end end local function draw_glint(t) local u = (t % 4.5) / 0.9 if u >= 1 then return end local g = CX - 20 + 40 * u for y = TOP + 2, FLOOR_Y - 1 do local x = floor(g - (y - TOP) * 0.55 + 0.5) if x > CX - HW[y] and x < CX + HW[y] - 1 then blend_rect(x, y, 2, 1, 255, 255, 255, 0.50) if y < TOP + 8 then blend_rect(x + 2, y, 1, 1, 255, 255, 255, 0.30) end end end end local SPX = { 8, 56, 20, 46, 5, 58, 12, 52, 28, 38 } local SPY = { 30, 28, 3, 4, 40, 40, 10, 36, 2, 2 } local SPP = { 0, 11, 23, 5, 17, 29, 8, 20, 14, 26 } local SPL = 1.33 local function draw_sparks(n, t) for i = 1, n do local k = (t + SPP[i] * 0.1) % SPL local s = 0 if k < 0.133 then s = 0 elseif k < 0.233 then s = 1 elseif k < 0.367 then s = 2 elseif k < 0.467 then s = 1 elseif k < 0.567 then s = 0 else s = -1 end if s >= 0 then local x, y = SPX[i], SPY[i] px(x, y, 255, 252, 232) for a = 1, s do local ar, ag, ab = 255, 238, 170 px(x - a, y, ar, ag, ab); px(x + a, y, ar, ag, ab); px(x, y - a, ar, ag, ab); px(x, y + a, ar, ag, ab) end end end end function render() local cc = cfg.cat or { r = 140, g = 140, b = 170 } local gc = cfg.glass or { r = 210, g = 244, b = 250 } local key = (cc.r * 256 + cc.g) * 256 + cc.b + gc.r * 7919 + gc.g * 104729 + gc.b * 15485863 if key ~= BGKEY then BGKEY = key; rebuild() end gfx.blit_rgb(BG, 0, 0, W, H) local t = frame / FPS draw_tail(t) draw_head(t) draw_glint(t) draw_sparks(cfg.sparks or 7, t) end