From 13aba05745978fb2ddcb49a580069f2d8da360f4 Mon Sep 17 00:00:00 2001 From: MayaTheShy Date: Sun, 15 Mar 2026 16:33:55 -0400 Subject: [PATCH] Refactor buildCatalogue and monitor drawing functions for improved performance and reduced flickering --- inventoryManager.lua | 78 ++++++++++++++++++++++++++++++-------------- 1 file changed, 54 insertions(+), 24 deletions(-) diff --git a/inventoryManager.lua b/inventoryManager.lua index 099dc34..8d8a245 100644 --- a/inventoryManager.lua +++ b/inventoryManager.lua @@ -38,15 +38,33 @@ local function scanInventory(deviceName) end -- Build a full catalogue: itemName -> { {chest=name, total=N}, ... } +-- Scans all chests in parallel for speed local function buildCatalogue() - local catalogue = {} -- itemName -> list of {chest, total} - for _, chest in ipairs(getChests()) do - local contents = scanInventory(chest) - for itemName, info in pairs(contents) do + local chests = getChests() + local results = {} -- index -> { chest=name, contents={} } + + -- Build parallel scan tasks + local tasks = {} + for i, chest in ipairs(chests) do + results[i] = { chest = chest, contents = {} } + tasks[i] = function() + results[i].contents = scanInventory(chest) + end + end + + -- Run all scans simultaneously + if #tasks > 0 then + parallel.waitForAll(table.unpack(tasks)) + end + + -- Merge results into catalogue + local catalogue = {} + for _, result in ipairs(results) do + for itemName, info in pairs(result.contents) do if not catalogue[itemName] then catalogue[itemName] = {} end - table.insert(catalogue[itemName], { chest = chest, total = info.total }) + table.insert(catalogue[itemName], { chest = result.chest, total = info.total }) end end return catalogue @@ -57,6 +75,7 @@ end ------------------------------------------------- local mon = nil +local buf = nil -- offscreen buffer to prevent flickering local function setupMonitor() mon = peripheral.wrap(MONITOR_SIDE) @@ -66,45 +85,52 @@ local function setupMonitor() return true end --- Helpers +-- Drawing target (buf or mon, set in drawDashboard) +local draw = nil + +-- Helpers (draw to buffer) local function monWrite(x, y, text, fg, bg) - mon.setCursorPos(x, y) - if fg then mon.setTextColor(fg) end - if bg then mon.setBackgroundColor(bg) end - mon.write(text) + draw.setCursorPos(x, y) + if fg then draw.setTextColor(fg) end + if bg then draw.setBackgroundColor(bg) end + draw.write(text) end local function monFill(y, color) - local w, _ = mon.getSize() - mon.setCursorPos(1, y) - mon.setBackgroundColor(color) - mon.write(string.rep(" ", w)) + local w, _ = draw.getSize() + draw.setCursorPos(1, y) + draw.setBackgroundColor(color) + draw.write(string.rep(" ", w)) end local function monCenter(y, text, fg, bg) - local w, _ = mon.getSize() + local w, _ = draw.getSize() local x = math.floor((w - #text) / 2) + 1 monWrite(x, y, text, fg, bg) end local function monBar(x, y, width, ratio, barColor, bgColor) local filled = math.floor(ratio * width) - mon.setCursorPos(x, y) - mon.setBackgroundColor(barColor) - mon.write(string.rep(" ", filled)) - mon.setBackgroundColor(bgColor) - mon.write(string.rep(" ", width - filled)) + draw.setCursorPos(x, y) + draw.setBackgroundColor(barColor) + draw.write(string.rep(" ", filled)) + draw.setBackgroundColor(bgColor) + draw.write(string.rep(" ", width - filled)) end --- Draw the full dashboard +-- Draw the full dashboard (double-buffered) local function drawDashboard() if not mon then return end local w, h = mon.getSize() - -- Clear - mon.setBackgroundColor(colors.black) - mon.clear() + -- Create offscreen buffer + buf = window.create(mon, 1, 1, w, h, false) + draw = buf + + -- Clear buffer + draw.setBackgroundColor(colors.black) + draw.clear() -- ===== Title bar ===== monFill(1, colors.blue) @@ -221,6 +247,10 @@ local function drawDashboard() -- Bottom accent monFill(h, colors.blue) monCenter(h, " \4 Auto-Sort Active \4 ", colors.lightBlue, colors.blue) + + -- Flush buffer to monitor in one go (no flicker) + buf.setVisible(true) + buf.setVisible(false) end -------------------------------------------------