info = { type = "image", title = "Box stacker", category = "other", author = "BRRRO", version = "1.0", license = "MIT", desc = "Robot arm stacks boxes on a pallet", api = 33, } local tr = ({ sk = { ["Speed"] = "Rýchlosť", ["Slow"] = "Pomaly", ["Normal"] = "Normálne", ["Fast"] = "Rýchlo", ["Boxes"] = "Krabice", ["Cardboard"] = "Kartón", ["Blue"] = "Modré", ["Green"] = "Zelené", ["Pallet load"] = "Náklad palety" }, cs = { ["Speed"] = "Rychlost", ["Slow"] = "Pomalu", ["Normal"] = "Normálně", ["Fast"] = "Rychle", ["Boxes"] = "Krabice", ["Cardboard"] = "Karton", ["Blue"] = "Modré", ["Green"] = "Zelené", ["Pallet load"] = "Náklad palety" }, })[lang] or {} local function T(s) return tr[s] or s end settings = { { key = "speed", label = T("Speed"), type = "int", default = 1, opts = { T("Slow"), T("Normal"), T("Fast") } }, { key = "crate", label = T("Boxes"), type = "int", default = 0, opts = { T("Cardboard"), T("Blue"), T("Green") } }, { key = "n", label = T("Pallet load"), type = "int", min = 3, max = 6, default = 6 }, } local rect, px, blit_rgb = gfx.rect, gfx.px, gfx.blit_rgb local floor, sqrt, abs, max = math.floor, math.sqrt, math.abs, math.max local W, H, FPS = gfx.W, gfx.H, gfx.FPS local SX, SY, LK = 32, 10, 22 local PAL_X = 4 local PICK = 47 local GAP = 14 local TB, CP = 110, 268 local SLOT = { { 5, 46 }, { 13, 46 }, { 21, 46 }, { 9, 40 }, { 17, 40 }, { 13, 34 } } local PWX, PHY, PDY = 50, 29, 36 local WALL, WALL_L, SEAM = { 66, 78, 100 }, { 52, 62, 82 }, { 46, 54, 72 } local STEEL, STEEL_D = { 120, 128, 146 }, { 70, 78, 96 } local ARM, ARM_D = { 240, 190, 20 }, { 96, 66, 10 } local JOINT, JOINT_D = { 168, 176, 194 }, { 52, 60, 78 } local WOOD, WOOD_L, WOOD_D = { 150, 106, 58 }, { 188, 140, 82 }, { 108, 74, 42 } local BELT, BELT_L = { 38, 42, 52 }, { 88, 96, 112 } local CRATES = { [0] = { { 172, 124, 68 }, { 208, 162, 96 }, { 124, 86, 46 }, { 232, 208, 160 } }, [1] = { { 58, 108, 190 }, { 100, 152, 232 }, { 34, 68, 140 }, { 204, 222, 250 } }, [2] = { { 56, 148, 88 }, { 100, 202, 132 }, { 32, 98, 58 }, { 222, 242, 202 } }, } local C = CRATES[0] local N, SPD, CYC = 6, 1, 6 * TB + CP local function c3(c) return c[1], c[2], c[3] end function on_settings() C = CRATES[cfg.crate] or CRATES[0] N = cfg.n or 6 SPD = ({ [0] = 0.7, 1, 1.4 })[cfg.speed] or 1 CYC = N * TB + CP end local BG local function build_bg() rect(0, 0, W, 57, c3(WALL)) rect(0, 47, W, 10, c3(WALL_L)) for x = 15, 63, 16 do rect(x, 3, 1, 54, c3(SEAM)) end rect(0, 0, W, 3, 36, 42, 56) rect(0, 3, W, 1, 96, 104, 120) rect(31, 4, 3, 4, c3(STEEL)) rect(54, 20, 8, 8, c3(SEAM)) for y = 21, 27, 2 do rect(55, y, 6, 1, c3(WALL)) end rect(4, 8, 20, 10, 30, 36, 48) rect(5, 9, 18, 8, 14, 18, 26) rect(0, 57, W, 7, 84, 88, 98) rect(0, 57, W, 1, 124, 128, 138) for x = 0, 60, 8 do rect(x, 62, 4, 2, 228, 182, 24) rect(x + 4, 62, 4, 2, 34, 36, 42) end rect(46, 50, 2, 7, c3(STEEL)); rect(60, 50, 2, 7, c3(STEEL)) rect(42, 49, 22, 1, c3(STEEL_D)) rect(41, 47, 1, 2, c3(STEEL)) BG = gfx.snapshot(0, 0, W, H) end local function ease(t) if t <= 0 then return 0 elseif t >= 1 then return 1 end return t * t * (3 - 2 * t) end local function mv(x0, y0, x1, y1, t) local e = ease(t) return x0 + (x1 - x0) * e, y0 + (y1 - y0) * e end local function pose(i, u) local s = SLOT[i] if u < 28 then if i == 1 then return PWX, PHY, 2 end local p = SLOT[i - 1] if u < 8 then local x, y = mv(p[1] + 3, p[2] - 4, p[1] + 3, p[2] - 12, u / 8); return x, y, 2 end local x, y = mv(p[1] + 3, p[2] - 12, PWX, PHY, (u - 8) / 20) return x, y, 2 elseif i > N then return PWX, PHY, 2 elseif u < 40 then local x, y = mv(PWX, PHY, PWX, PDY, (u - 28) / 12); return x, y, 2 elseif u < 48 then return PWX, PDY, 2 - 2 * ease((u - 40) / 8) elseif u < 60 then local x, y = mv(PWX, PDY, PWX, PHY, (u - 48) / 12); return x, y, 0 elseif u < 88 then local x, y = mv(PWX, PHY, s[1] + 3, s[2] - 12, (u - 60) / 28); return x, y, 0 elseif u < 100 then local x, y = mv(s[1] + 3, s[2] - 12, s[1] + 3, s[2] - 4, (u - 88) / 12); return x, y, 0 end return s[1] + 3, s[2] - 4, 2 * ease((u - 100) / 6) end local function box(lx, ty) rect(lx, ty, 7, 6, c3(C[1])) rect(lx, ty, 7, 1, c3(C[2])) rect(lx, ty + 5, 7, 1, c3(C[3])) rect(lx + 3, ty + 1, 1, 4, c3(C[4])) end local function link(x0, y0, x1, y1) local n = floor(max(abs(x1 - x0), abs(y1 - y0)) / 1.5) + 1 for k = 0, n do local t = k / n rect(floor(x0 + (x1 - x0) * t + 0.5) - 1, floor(y0 + (y1 - y0) * t + 0.5) - 1, 3, 3, c3(ARM_D)) end for k = 0, n do local t = k / n rect(floor(x0 + (x1 - x0) * t + 0.5) - 1, floor(y0 + (y1 - y0) * t + 0.5) - 1, 2, 2, c3(ARM)) end end local function joint(x, y) x, y = floor(x + 0.5) - 2, floor(y + 0.5) - 2 rect(x + 1, y, 3, 5, c3(JOINT_D)) rect(x, y + 1, 5, 3, c3(JOINT_D)) rect(x + 1, y + 1, 3, 3, c3(JOINT)) end local function agv(c, tick) rect(c - 9, 55, 19, 2, 226, 180, 24) rect(c - 9, 55, 19, 1, 250, 214, 90) rect(c - 8, 57, 17, 1, 40, 44, 56) rect(c - 7, 57, 3, 2, 24, 26, 34); rect(c + 5, 57, 3, 2, 24, 26, 34) if tick % 10 < 5 then px(c + 8, 54, 255, 150, 30) end end local function pallet(ox, dy, placed) rect(PAL_X + ox, 52 + dy, 25, 2, c3(WOOD)) rect(PAL_X + ox, 52 + dy, 25, 1, c3(WOOD_L)) for _, x in ipairs({ 4, 14, 25 }) do rect(x + ox, 54 + dy, 4, 3, c3(WOOD_D)) end for k = 1, placed do box(SLOT[k][1] + ox, SLOT[k][2] + dy) end end function render() if not BG then build_bg() end blit_rgb(BG, 0, 0, W, H) local c = (frame + 120) * SPD % CYC local i = floor(c / TB) + 1 local u = c - (i - 1) * TB if i > N then i, u = N + 1, c - N * TB end local wx, wy, op = pose(i, u) local picked = i > N or u >= 48 local carry = i <= N and u >= 48 and u < 100 local placed = i > N and N or (i - 1 + (u >= 100 and 1 or 0)) local e = (i <= N and u < 24) and ease(u / 24) or 1 rect(42, 46, 22, 3, c3(BELT)) local sh = (e < 1) and e * GAP % 7 or 0 for x = 42 + 7 - sh, 63, 7 do rect(floor(x + 0.5), 46, 1, 3, c3(BELT_L)) end local bx = PICK + GAP * (1 - e) if not picked then box(floor(bx + 0.5), 40) end box(floor(bx + GAP + 0.5), 40) local tn = u - 28 if i <= N or tn < 0 then pallet(0, 0, placed) elseif tn < 100 then local ca = 16 - 32 * ease((tn - 50) / 50) if tn < 50 then ca = -16 + 32 * ease(tn / 40) end local dy = tn >= 45 and -1 or 0 pallet(floor(ca + 0.5) - 16, dy, N) agv(floor(ca + 0.5), frame) placed = N elseif tn >= 110 then local cb = -16 + 32 * ease((tn - 110) / 50) if tn >= 170 then cb = 16 - 32 * ease((tn - 170) / 40) end local ox = tn < 165 and floor(cb + 0.5) - 16 or 0 pallet(ox, tn < 165 and -1 or 0, 0) agv(floor(cb + 0.5), frame) placed = 0 else placed = 0 end for k = 1, 6 do if k <= N then rect(3 + k * 3, 10, 2, 6, k <= placed and 240 or 40, k <= placed and 190 or 46, k <= placed and 20 or 60) end end local dx, dy = wx - SX, wy - SY local d = sqrt(dx * dx + dy * dy) local dd = math.min(d, 2 * LK - 0.3) local h = sqrt(LK * LK - dd * dd / 4) local ex, ey = SX + dx / 2 + dy / d * h, SY + dy / 2 - dx / d * h link(SX, SY, ex, ey) link(ex, ey, wx, wy) joint(SX, SY); joint(ex, ey) local fx, fy = floor(wx + 0.5), floor(wy + 0.5) if carry then box(fx - 3, fy + 4) end local o = floor(op + 0.5) rect(fx - 4 - o, fy + 1, 9 + 2 * o, 2, c3(JOINT_D)) rect(fx - 4 - o, fy + 3, 1, 4, c3(JOINT)); rect(fx + 4 + o, fy + 3, 1, 4, c3(JOINT)) rect(fx - 1, fy - 1, 3, 3, c3(JOINT)) px(SX, SY, 80, 230, 120) end