Refactor autoSmelt and autoCompost functions to use snapshots for catalogue iteration and improve command return values
This commit is contained in:
@@ -1816,7 +1816,7 @@ local function autoSmelt()
|
||||
if smeltingPaused then return false end
|
||||
|
||||
local furnaces = getFurnaces()
|
||||
if #furnaces == 0 then return end
|
||||
if #furnaces == 0 then return false end
|
||||
|
||||
local chests = getChests()
|
||||
local catalogue = cache.catalogue
|
||||
@@ -1984,7 +1984,10 @@ local function autoSmelt()
|
||||
local remaining = toLoad
|
||||
local loaded = false
|
||||
|
||||
for _, source in ipairs(catalogue[itemName]) do
|
||||
-- Snapshot: adjustCache may remove entries during iteration
|
||||
local srcSnapshot = { table.unpack(catalogue[itemName]) }
|
||||
for _, source in ipairs(srcSnapshot) do
|
||||
if source.total > 0 then
|
||||
local chest = wrapCached(source.chest)
|
||||
if chest then
|
||||
for slot, slotItem in pairs(chest.list()) do
|
||||
@@ -2006,6 +2009,7 @@ local function autoSmelt()
|
||||
end
|
||||
end
|
||||
end
|
||||
end -- source.total > 0
|
||||
if loaded then break end
|
||||
end
|
||||
|
||||
@@ -2039,7 +2043,6 @@ local function defragInventory()
|
||||
local inv = wrapCached(chestName)
|
||||
if inv then
|
||||
local contents = inv.list()
|
||||
local detail_cache = {}
|
||||
for slot, item in pairs(contents) do
|
||||
if not itemSlots[item.name] then
|
||||
itemSlots[item.name] = {}
|
||||
@@ -2090,6 +2093,11 @@ local function defragInventory()
|
||||
donor.count = donor.count - n
|
||||
recv.count = recv.count + n
|
||||
totalMerged = totalMerged + n
|
||||
-- Update catalogue for cross-chest moves
|
||||
if donor.chest ~= recv.chest then
|
||||
adjustCache(itemName, donor.chest, -n)
|
||||
adjustCache(itemName, recv.chest, n)
|
||||
end
|
||||
end
|
||||
end
|
||||
if donor.count <= 0 then i = i + 1 end
|
||||
@@ -2149,7 +2157,6 @@ local function autoCompost()
|
||||
end
|
||||
end
|
||||
local dropperSize = dropper.size()
|
||||
local dropperFreeSlots = dropperSize - dropperUsedSlots
|
||||
local dropperFreeItems = (dropperSize * 64) - dropperUsedItems
|
||||
|
||||
if dropperFreeItems <= 0 then return didWork end
|
||||
@@ -2168,7 +2175,10 @@ local function autoCompost()
|
||||
if available > 0 then
|
||||
local toFeed = math.min(available, dropperFreeItems)
|
||||
local fed = 0
|
||||
for _, source in ipairs(catalogue[itemName]) do
|
||||
-- Snapshot: adjustCache may remove entries during iteration
|
||||
local srcSnapshot = { table.unpack(catalogue[itemName]) }
|
||||
for _, source in ipairs(srcSnapshot) do
|
||||
if source.total > 0 then
|
||||
local chest = wrapCached(source.chest)
|
||||
if chest then
|
||||
for slot, slotItem in pairs(chest.list()) do
|
||||
@@ -2186,6 +2196,7 @@ local function autoCompost()
|
||||
end
|
||||
end
|
||||
end
|
||||
end -- source.total > 0
|
||||
if fed >= toFeed then break end
|
||||
end
|
||||
dropperFreeItems = dropperFreeItems - fed
|
||||
@@ -2257,7 +2268,10 @@ local function orderItem(itemName, amount, dropperOverride)
|
||||
end
|
||||
|
||||
local remaining = amount
|
||||
for _, entry in ipairs(catalogue[itemName]) do
|
||||
-- Snapshot: adjustCache may remove entries during iteration
|
||||
local srcSnapshot = { table.unpack(catalogue[itemName]) }
|
||||
for _, entry in ipairs(srcSnapshot) do
|
||||
if entry.total > 0 then
|
||||
local chest = wrapCached(entry.chest)
|
||||
if chest then
|
||||
for slot, slotItem in pairs(chest.list()) do
|
||||
@@ -2275,6 +2289,7 @@ local function orderItem(itemName, amount, dropperOverride)
|
||||
end
|
||||
end
|
||||
end
|
||||
end -- entry.total > 0
|
||||
if remaining <= 0 then break end
|
||||
end
|
||||
|
||||
@@ -2851,6 +2866,7 @@ local function main()
|
||||
statusMessage = "Order error"
|
||||
statusColor = colors.red
|
||||
statusTimer = 5
|
||||
needsRedraw = true
|
||||
end
|
||||
pcall(function()
|
||||
networkModem.transmit(replyChannel, ORDER_CHANNEL, {
|
||||
@@ -2937,6 +2953,7 @@ local function main()
|
||||
end
|
||||
end
|
||||
cache.droppers = merged
|
||||
bumpStateVersion()
|
||||
pcall(broadcastState)
|
||||
elseif message.type == "craft" and message.recipeIdx then
|
||||
log.info("NET", "Craft request: recipe #%d", message.recipeIdx)
|
||||
|
||||
Reference in New Issue
Block a user