info = { type = "image", title = "Printed cat", category = "animals", author = "BRRRO", version = "1.0", license = "MIT", desc = "A 3D printer builds a cat layer by layer", api = 33, } local tr = ({ sk = { ["Filament"] = "Filament", ["Speed"] = "Rýchlosť" }, cs = { ["Filament"] = "Filament", ["Speed"] = "Rychlost" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "fil", label = T("Filament"), type = "color", default = "#F7931E" }, { key = "speed", label = T("Speed"), type = "int", min = 1, max = 4, default = 2 }, } local rect, px, blend_rect, blit_rgb = gfx.rect, gfx.px, gfx.blend_rect, gfx.blit_rgb local floor, sqrt, min, max = math.floor, math.sqrt, math.min, math.max local W, H = gfx.W, gfx.H local SW, SH = 18, 24 local OX, OY = 23, 22 local BED_Y = OY + SH local CH_L, CH_R = 11, 52 local SLIDE = 53 local HOLD, EJECT = 45, 40 local LAYER_FRAMES = { 22, 14, 10, 7 } local CELLS, ROW_L, ROW_R = nil, {}, {} local function build_cat() local g = {} for i = 1, SW * SH do g[i] = 0 end local function R(x, y, w, h, v) for yy = y, y + h - 1 do for xx = x, x + w - 1 do g[yy * SW + xx + 1] = v end end end R(4, 0, 1, 1, 1); R(4, 1, 2, 1, 1); R(4, 2, 3, 1, 1); R(5, 2, 1, 1, 5) R(15, 0, 1, 1, 1); R(14, 1, 2, 1, 1); R(13, 2, 3, 1, 1); R(14, 2, 1, 1, 5) R(4, 3, 12, 7, 1); R(5, 10, 10, 1, 1) R(8, 3, 1, 2, 2); R(11, 3, 1, 2, 2); R(4, 6, 2, 1, 2); R(14, 6, 2, 1, 2) R(6, 6, 2, 2, 4); R(12, 6, 2, 2, 4) R(8, 8, 4, 2, 3); R(9, 8, 2, 1, 5) R(5, 11, 10, 6, 1); R(4, 17, 12, 7, 1); R(8, 11, 4, 7, 3) R(5, 13, 2, 1, 2); R(13, 13, 2, 1, 2); R(4, 19, 2, 1, 2); R(14, 19, 2, 1, 2) R(7, 20, 2, 4, 3); R(11, 20, 2, 4, 3); R(9, 21, 2, 3, 2) R(0, 22, 4, 2, 1); R(0, 16, 2, 7, 1); R(0, 15, 2, 2, 2) local rows = {} for y = 0, SH - 1 do local lo, hi = nil, nil for x = 0, SW - 1 do if g[y * SW + x + 1] ~= 0 then lo = lo or x; hi = x end end ROW_L[y], ROW_R[y] = lo, hi rows[y + 1] = string.char(table.unpack(g, y * SW + 1, y * SW + SW)) end CELLS = table.concat(rows) end local PAL, PAL_HOT, FIL = nil, nil, nil local function lit(v, k, a) return min(255, floor(v * k + a)) end local function make_pal(c, k, a) return { 0, 0, 0, lit(c.r, k, a), lit(c.g, k, a), lit(c.b, k, a), lit(c.r, 0.62 * k, a * 0.5), lit(c.g, 0.62 * k, a * 0.5), lit(c.b, 0.62 * k, a * 0.5), lit(c.r, 0.5 * k, 150 * k * 0.5 + a), lit(c.g, 0.5 * k, 150 * k * 0.5 + a), lit(c.b, 0.5 * k, 150 * k * 0.5 + a), 44, 28, 22, 236, 120, 140 } end local BG, BG_KEY = nil, nil local function disc(cx, cy, r, cr, cg, cb) for dy = -r, r do local dx = floor(sqrt(r * r + 0.5 - dy * dy) + 0.5) rect(cx - dx, cy + dy, 2 * dx + 1, 1, cr, cg, cb) end end local function build_bg(c) gfx.clear(30, 32, 38) rect(CH_L, 7, CH_R - CH_L + 1, 42, 58, 68, 82) rect(CH_L, 7, CH_R - CH_L + 1, 1, 46, 54, 66) rect(6, 3, 52, 4, 38, 40, 50) rect(6, 3, 52, 1, 64, 68, 80) rect(6, 7, 5, 43, 38, 40, 50); rect(53, 7, 5, 43, 38, 40, 50) rect(10, 7, 1, 43, 70, 74, 88); rect(53, 7, 1, 43, 70, 74, 88) rect(6, 50, 52, 11, 30, 32, 38) rect(6, 50, 52, 1, 56, 60, 72) rect(24, 54, 16, 5, 16, 18, 24) rect(25, 55, 14, 3, 20, 70, 78) rect(10, 54, 3, 3, 62, 92, 170); rect(51, 54, 3, 3, 62, 92, 170) rect(7, 59, 50, 1, 22, 24, 30) disc(46, 14, 6, 52, 54, 62) disc(46, 14, 5, c.r, c.g, c.b) disc(46, 14, 2, 52, 54, 62) disc(46, 14, 1, 20, 22, 28) rect(13, 8, 30, 1, 52, 60, 72) BG = gfx.snapshot(0, 0, W, H) end local function ease(e) return e * e * (3 - 2 * e) end function render() if not CELLS then build_cat() end local fc = cfg.fil or { r = 247, g = 147, b = 30 } local key = fc.r * 65536 + fc.g * 256 + fc.b if key ~= BG_KEY then BG_KEY = key FIL = fc PAL = make_pal(fc, 1, 0) PAL_HOT = make_pal(fc, 1.0, 70) build_bg(fc) end local D = LAYER_FRAMES[cfg.speed or 2] or 14 local PRINT = SH * D local t = frame % (PRINT + HOLD + EJECT) blit_rgb(BG, 0, 0, W, H) local s, bedx2 = 0, nil local ty, nx = nil, nil local row, nxi, left_to_right = nil, nil, true local end_x = OX + ROW_L[0] - 1 local start_x = OX + ROW_L[SH - 1] local prog = 1 if t < PRINT then local r = t // D local q = (t % D + 1) / D row = SH - 1 - r left_to_right = (r % 2 == 0) local xa, xb = OX + ROW_L[row] - 1, OX + ROW_R[row] + 1 if not left_to_right then xa, xb = xb, xa end nx = floor(xa + (xb - xa) * q + 0.5) nxi = nx ty = OY + row - 1 prog = t / PRINT elseif t < PRINT + HOLD then local e = ease((t - PRINT) / HOLD) ty = floor(OY - 1 + (12 - (OY - 1)) * e + 0.5) nx = end_x else local e = ease((t - PRINT - HOLD) / EJECT) s = floor(SLIDE * e + 0.5) ty = floor(12 + ((BED_Y - 2) - 12) * e + 0.5) nx = floor(end_x + (start_x - end_x) * e + 0.5) bedx2 = CH_L + s - SLIDE end local function bed(x0) rect(x0, BED_Y, 42, 1, 104, 110, 124) rect(x0, BED_Y + 1, 42, 3, 66, 70, 82) rect(x0, BED_Y + 3, 42, 1, 44, 48, 58) end bed(CH_L + s) if bedx2 then bed(bedx2) end local blit_indexed, clip = gfx.blit_indexed, gfx.clip if row then if row < SH - 1 then clip(OX, OY + row + 1, SW, SH - row - 1) blit_indexed(CELLS, PAL, OX, OY, SW, SH, SW, SH, true) end if left_to_right then clip(OX, OY + row, nxi - OX + 1, 1) else clip(nxi, OY + row, OX + SW - nxi, 1) end blit_indexed(CELLS, PAL_HOT, OX, OY, SW, SH, SW, SH, true) clip() elseif s < SLIDE then blit_indexed(CELLS, PAL, OX + s, OY, SW, SH, SW, SH, true) end local yb = ty - 5 rect(CH_L + 2, yb, CH_R - CH_L - 3, 2, 22, 24, 30) rect(CH_L + 2, yb, CH_R - CH_L - 3, 1, 50, 54, 64) rect(nx - 3, yb - 1, 7, 5, 82, 88, 102) rect(nx - 3, yb - 1, 7, 1, 130, 138, 154) rect(nx - 1, ty - 1, 3, 1, 170, 164, 150) px(nx, ty, 255, 150, 60) if row then blend_rect(nx - 1, ty, 3, 2, 255, 140, 50, 0.35) end rect(26, 56, floor(12 * prog), 1, 90, 210, 220) if t < PRINT then if (frame // 8) % 2 == 0 then px(11, 55, 255, 150, 40) end else px(11, 55, 80, 230, 120) end end