Improve algo for sorting items away

This commit is contained in:
Salt 2024-02-26 02:25:39 -06:00
parent bcbb419179
commit 53bfc85164

View File

@ -6,9 +6,6 @@
-- Startup diagnostics -- Startup diagnostics
print("Salt's CC Storage Net") print("Salt's CC Storage Net")
-- Required libraries
local basalt = require("basalt") -- wget run https://basalt.madefor.cc/install.lua release latest.lua
-- Global scope locals -- Global scope locals
local output = peripheral.wrap("top") or error("Put a chest on top of this terminal for output items", 0) local output = peripheral.wrap("top") or error("Put a chest on top of this terminal for output items", 0)
@ -36,10 +33,27 @@ function pushDepositsToChests()
for k,hopper in ipairs(hoppers) do for k,hopper in ipairs(hoppers) do
-- For each item in that hopper's inventory... -- For each item in that hopper's inventory...
for hslot,hitem in pairs(hopper.list()) do for hslot,hitem in pairs(hopper.list()) do
local remaining = hitem["count"]
-- For each connected "chest"... -- For each connected "chest"...
for k,chest in ipairs(chests) do for k,chest in ipairs(chests) do
-- Attempt to push our items in -- First, make an attempt to find slots that we can shove the item into
hopper.pushItems(peripheral.getName(chest),hslot) for cslot,citem in pairs(chest.list()) do
if
citem["name"] == hitem["name"] and -- We have the same item
citem["count"] < chest.getItemLimit(cslot) and -- There's space in this slot
remaining > 0 -- We still have things to sort
then
difference = chest.getItemLimit(cslot) - citem["count"]
hopper.pushItems(peripheral.getName(chest),hslot,difference,cslot)
remaining = remaining - difference
end
end
end
-- We've fallen through trying to fill up existing stacks. Fragmentation is not a concern, put it wherever
if remaining > 0 then
for k,chest in ipairs(chests) do
hopper.pushItems(peripheral.getName(chest),hslot)
end
end end
end end
end end