better bulk handling
This commit is contained in:
@@ -60,111 +60,125 @@ if not fs.exists(STARTUP_FILE) then
|
|||||||
shell.openForegroundTab('packages/milo/apps/furni')]])
|
shell.openForegroundTab('packages/milo/apps/furni')]])
|
||||||
end
|
end
|
||||||
|
|
||||||
local furni
|
local furnaces
|
||||||
local localName
|
local localName
|
||||||
|
|
||||||
print('detecting wired modem connected to furnaces...')
|
print('detecting wired modem connected to furnaces...')
|
||||||
for _, dev in pairs(device) do
|
for _, dev in pairs(device) do
|
||||||
if dev.type == 'wired_modem' and dev.getNameLocal then
|
if dev.type == 'wired_modem' and dev.getNameLocal then
|
||||||
local list = dev.getNamesRemote()
|
local list = dev.getNamesRemote()
|
||||||
furni = { }
|
furnaces = { }
|
||||||
localName = dev.getNameLocal()
|
localName = dev.getNameLocal()
|
||||||
for _, name in pairs(list) do
|
for _, name in pairs(list) do
|
||||||
if device[name].type ~= 'minecraft:furnace' then
|
if device[name].type ~= 'minecraft:furnace' then
|
||||||
furni = nil
|
furnaces = nil
|
||||||
break
|
break
|
||||||
end
|
end
|
||||||
table.insert(furni, device[name])
|
table.insert(furnaces, {
|
||||||
|
dev = device[name],
|
||||||
|
list = device[name].list(),
|
||||||
|
})
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
if furni then
|
if furnaces then
|
||||||
print('Using wired modem: ' .. dev.name)
|
print('Using wired modem: ' .. dev.name)
|
||||||
print('Furnaces: ' .. #furni)
|
print('Furnaces: ' .. #furnaces)
|
||||||
break
|
break
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
if not furni then
|
if not furnaces then
|
||||||
error('Turtle must be connected to a second wired_modem connected to furnaces only')
|
error('Turtle must be connected to a second wired_modem connected to furnaces only')
|
||||||
end
|
end
|
||||||
|
|
||||||
_G.printError([[Program must be restarted if new furnaces are added.]])
|
_G.printError([[Program must be restarted if new furnaces are added.]])
|
||||||
|
|
||||||
-- slot 1: item to cook
|
local function getSlot(furnace, slotNo)
|
||||||
-- slot 2: fuel
|
if not furnace.list[slotNo] then
|
||||||
-- slot 3: return
|
furnace.list[slotNo] = {
|
||||||
|
count = 0
|
||||||
local active = false
|
}
|
||||||
|
end
|
||||||
|
return furnace.list[slotNo]
|
||||||
|
end
|
||||||
|
|
||||||
local function process(list)
|
local function process(list)
|
||||||
active = false
|
local inItem = list[INPUT_SLOT]
|
||||||
|
local inFuel = list[FUEL_SLOT]
|
||||||
|
local inReturn = list[OUTPUT_SLOT] or { count = 0 }
|
||||||
|
|
||||||
for _, furnace in ipairs(Util.shallowCopy(furni)) do
|
for _, furnace in ipairs(Util.shallowCopy(furnaces)) do
|
||||||
pcall(function()
|
local s, m = pcall(function()
|
||||||
local f = furnace.list()
|
if furnace.list[INPUT_SLOT] and furnace.list[INPUT_SLOT].count > 0 then
|
||||||
|
furnace.list = furnace.dev.list()
|
||||||
-- items to cook
|
print('listing ' .. furnace.dev.name)
|
||||||
local item = list[INPUT_SLOT]
|
|
||||||
local cooking = f[INPUT_SLOT]
|
|
||||||
|
|
||||||
if cooking or item then
|
|
||||||
active = true
|
|
||||||
end
|
end
|
||||||
|
|
||||||
if item and item.count > 0 then
|
-- items to cook
|
||||||
if not cooking or cooking.name == item.name then
|
local cooking = getSlot(furnace, INPUT_SLOT)
|
||||||
local count = cooking and cooking.count or 0
|
if cooking.count < 64 and inItem and inItem.count > 0 then
|
||||||
if count < 64 then
|
if cooking.count == 0 or cooking.name == inItem.name then
|
||||||
print('cooking : ' .. furnace.name)
|
print('cooking : ' .. furnace.dev.name)
|
||||||
count = furnace.pullItems(localName, INPUT_SLOT, SMELT_AMOUNT, INPUT_SLOT)
|
local count = furnace.dev.pullItems(localName, INPUT_SLOT, SMELT_AMOUNT, INPUT_SLOT)
|
||||||
item.count = item.count - count
|
|
||||||
Util.removeByValue(furni, furnace)
|
if count > 0 then
|
||||||
table.insert(furni, furnace)
|
inItem.count = inItem.count - count
|
||||||
|
|
||||||
|
cooking.name = inItem.name
|
||||||
|
cooking.count = cooking.count + count
|
||||||
|
|
||||||
|
-- push to end of queue
|
||||||
|
Util.removeByValue(furnaces, furnace)
|
||||||
|
table.insert(furnaces, furnace)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
-- fuel
|
-- fuel
|
||||||
local fuel = f[FUEL_SLOT] or { count = 0 }
|
local fuel = getSlot(furnace, FUEL_SLOT)
|
||||||
if fuel.count < 8 and list[2] and list[2].count > 0 then
|
if fuel.count < 8 and inFuel and inFuel.count > 0 then
|
||||||
if fuel.count == 0 or fuel.name == list[2].name then
|
if fuel.count == 0 or fuel.name == inFuel.name then
|
||||||
print('fueling ' ..furnace.name)
|
print('fueling ' .. furnace.dev.name)
|
||||||
list[2].count = list[2].count - furnace.pullItems(localName, FUEL_SLOT, 8 - fuel.count, FUEL_SLOT)
|
local count = furnace.dev.pullItems(localName, FUEL_SLOT, 8 - fuel.count, FUEL_SLOT)
|
||||||
|
if count > 0 then
|
||||||
|
inFuel.count = inFuel.count - count
|
||||||
|
|
||||||
|
fuel.name = inFuel.name
|
||||||
|
fuel.count = fuel.count - count
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local result = f[OUTPUT_SLOT]
|
local result = getSlot(furnace, OUTPUT_SLOT)
|
||||||
if result then
|
if result.count > 0 then
|
||||||
if not list[OUTPUT_SLOT] or result.name == list[OUTPUT_SLOT].name then
|
if inReturn.count == 0 or result.name == inReturn.name then
|
||||||
print('pulling from : ' .. furnace.name)
|
print('pulling from : ' .. furnace.dev.name)
|
||||||
furnace.pushItems(localName, OUTPUT_SLOT, result.count, OUTPUT_SLOT)
|
local count = furnace.dev.pushItems(localName, OUTPUT_SLOT, result.count, OUTPUT_SLOT)
|
||||||
list[OUTPUT_SLOT] = result
|
|
||||||
|
if count > 0 then
|
||||||
|
result.count = result.count - count
|
||||||
|
if result.count == 0 then
|
||||||
|
furnace.list[OUTPUT_SLOT] = nil
|
||||||
|
end
|
||||||
|
|
||||||
|
inReturn.name = result.name
|
||||||
|
inReturn.count = inReturn.count + count
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
end
|
|
||||||
|
|
||||||
return active
|
|
||||||
end
|
|
||||||
|
|
||||||
Event.on('turtle_inventory', function()
|
|
||||||
print('processing')
|
|
||||||
while true do
|
|
||||||
-- furnace block updates can cause errors
|
|
||||||
local s, m = pcall(process, inv.list())
|
|
||||||
if s and not active then
|
|
||||||
break
|
|
||||||
end
|
|
||||||
if not s and m then
|
if not s and m then
|
||||||
_G.printError(m)
|
_G.printError(m)
|
||||||
end
|
end
|
||||||
os.sleep(3)
|
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
Event.on('turtle_inventory', function()
|
||||||
|
process(inv.list())
|
||||||
print('idle')
|
print('idle')
|
||||||
end)
|
end)
|
||||||
|
|
||||||
Event.onInterval(5, function()
|
Event.onInterval(3, function()
|
||||||
-- for some reason, it keeps stalling ...
|
|
||||||
os.queueEvent('turtle_inventory')
|
os.queueEvent('turtle_inventory')
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user