This commit is contained in:
Anavrins
2020-09-20 23:08:18 -04:00
parent 88cacb4823
commit c7a347f723

View File

@@ -17,158 +17,156 @@
-- 13-16: Furnace Outputs for 2 to 4 furni.lua arrays -- 13-16: Furnace Outputs for 2 to 4 furni.lua arrays
-- Only user option, slot 4, what to use it for? -- Only user option, slot 4, what to use it for?
local useSlot4=nil
local useSlot4 = nil
-- Begin main program code -- Begin main program code
-- Find modem with -only- turtles attached (these should be our furni/furniController arrays) -- Find modem with -only- turtles attached (these should be our furni/furniController arrays)
local devModem="" local devModem = ""
for nDev,modem in ipairs({peripheral.find("modem")}) do for _, modem in ipairs({peripheral.find("modem")}) do
if #modem.getNamesRemote() > 1 then -- We're only looking for 2 or more devices on a network segment, ignore anything less if #modem.getNamesRemote() > 1 then -- We're only looking for 2 or more devices on a network segment, ignore anything less
nTurtles=0 local nTurtles = 0
for nDev,dev in ipairs(modem.getNamesRemote()) do for _, dev in ipairs(modem.getNamesRemote()) do
if modem.getTypeRemote(dev) == "turtle" then nTurtles=nTurtles+1 end if modem.getTypeRemote(dev) == "turtle" then nTurtles = nTurtles + 1 end
end end
if nTurtles==#modem.getNamesRemote() then if nTurtles == #modem.getNamesRemote() then
devModem=modem devModem = modem
break break
end end
end end
end end
if devModem ~= "" then if devModem ~= "" then
print("Found turtle-only modem, detected",#devModem.getNamesRemote(),"turtles",(#devModem.getNamesRemote() > 4 and "(Max 4 will be mapped)" or "(All will be mapped)")) print("Found turtle-only modem, detected", #devModem.getNamesRemote(), "turtles", (#devModem.getNamesRemote() > 4 and "(Max 4 will be mapped)" or "(All will be mapped)"))
else else
print("Did not find a modem with only turtles connected. Please check your networking cables/modems and devices, then try again.") print("Did not find a modem with only turtles connected. Please check your networking cables/modems and devices, then try again.")
return -- We don't have a modem, halt return -- We don't have a modem, halt
end end
local devTurtles={}
for nDev,dev in ipairs(devModem.getNamesRemote()) do local devTurtles = {}
for _, dev in ipairs(devModem.getNamesRemote()) do
if #devTurtles >= 4 then break end if #devTurtles >= 4 then break end
devTurtles[#devTurtles+1]=peripheral.wrap(dev) devTurtles[#devTurtles+1] = peripheral.wrap(dev)
print("Mapping",devTurtles[#devTurtles].getLabel(),"("..dev..")","as device",#devTurtles) print("Mapping", devTurtles[#devTurtles].getLabel(), "("..dev..")", "as device", #devTurtles)
devTurtles[#devTurtles].inputSlot=#devTurtles+4 -- Set the input for this device on second row devTurtles[#devTurtles].inputSlot = #devTurtles + 4 -- Set the input for this device on second row
devTurtles[#devTurtles].fuelSlot=#devTurtles+8 -- Set the fuel for this device on third row devTurtles[#devTurtles].fuelSlot = #devTurtles + 8 -- Set the fuel for this device on third row
devTurtles[#devTurtles].outputSlot=#devTurtles+12 -- Set the output for this device on fourth row devTurtles[#devTurtles].outputSlot = #devTurtles + 12 -- Set the output for this device on fourth row
if devTurtles[#devTurtles].isOn() then if devTurtles[#devTurtles].isOn() then
print("Resynchronizing",devTurtles[#devTurtles].getLabel()) print("Resynchronizing", devTurtles[#devTurtles].getLabel())
devTurtles[#devTurtles].reboot() devTurtles[#devTurtles].reboot()
else else
print("Powering",devTurtles[#devTurtles].getLabel(),"on") print("Powering", devTurtles[#devTurtles].getLabel(), "on")
devTurtles[#devTurtles].turnOn() devTurtles[#devTurtles].turnOn()
end end
end end
local turtle.list = function () -- Supplement a local .list()-like function local function listInventory() -- Supplement a local .list()-like function
local tList={} local tList = {}
for i=1,16 do for i = 1, 16 do
local slotData=turtle.getItemDetail(i) -- DWGFJTLR local slotData = turtle.getItemDetail(i) -- DWGFJTLR
if slotData ~= nil then if slotData ~= nil then
slotData.maxStack=turtle.getItemCount(i) + turtle.getItemSpace(i) slotData.maxStack=turtle.getItemCount(i) + turtle.getItemSpace(i)
tList[i]=slotData tList[i] = slotData
end end
end end
return tList return tList
end end
function manageLocalResources() local function manageLocalResources()
--print("Input found") --print("Input found")
local itemCount=turtle.getItemCount(1)/#devTurtles local itemCount = turtle.getItemCount(1) / #devTurtles
if itemCount < 1 then itemCount = 1 end -- Patch for single items fed into master slot if itemCount < 1 then itemCount = 1 end -- Patch for single items fed into master slot
if turtle.getItemCount(1) > 0 then -- Input if turtle.getItemCount(1) > 0 then -- Input
for nDev,dev in ipairs(devTurtles) do for _, dev in ipairs(devTurtles) do
local devList=dev.list()
if turtle.select(1) and ((turtle.compareTo(dev.inputSlot) and turtle.getItemSpace(dev.inputSlot) > 0) or turtle.getItemCount(dev.inputSlot) == 0) then -- Move Input if turtle.select(1) and ((turtle.compareTo(dev.inputSlot) and turtle.getItemSpace(dev.inputSlot) > 0) or turtle.getItemCount(dev.inputSlot) == 0) then -- Move Input
turtle.transferTo(dev.inputSlot,itemCount) turtle.transferTo(dev.inputSlot, itemCount)
end end
if useSlot4 == "input" and turtle.select(4) and ((turtle.compareTo(dev.inputSlot) and turtle.getItemSpace(dev.inputSlot) > 0) or turtle.getItemCount(dev.inputSlot) == 0) then -- Move Input if useSlot4 == "input" and turtle.select(4) and ((turtle.compareTo(dev.inputSlot) and turtle.getItemSpace(dev.inputSlot) > 0) or turtle.getItemCount(dev.inputSlot) == 0) then -- Move Input
turtle.transferTo(dev.inputSlot,itemCount) turtle.transferTo(dev.inputSlot, itemCount)
end --Slot 4 input end --Slot 4 input
end end
end end
if turtle.select(2) and turtle.getSelectedSlot() == 2 then -- Fuel if turtle.select(2) and turtle.getSelectedSlot() == 2 then -- Fuel
for nDev,dev in ipairs(devTurtles) do for _, dev in ipairs(devTurtles) do
local devList=dev.list()
if turtle.compareTo(dev.fuelSlot) or turtle.getItemCount(dev.fuelSlot) == 0 then turtle.transferTo(dev.fuelSlot,64) end -- Move fuel into slot if turtle.compareTo(dev.fuelSlot) or turtle.getItemCount(dev.fuelSlot) == 0 then turtle.transferTo(dev.fuelSlot,64) end -- Move fuel into slot
end end
end end
if useSlot4 == "fuel" and turtle.select(4) and turtle.getSelectedSlot() == 4 then -- Fuel if useSlot4 == "fuel" and turtle.select(4) and turtle.getSelectedSlot() == 4 then -- Fuel
for nDev,dev in ipairs(devTurtles) do for _, dev in ipairs(devTurtles) do
local devList=dev.list()
if turtle.compareTo(dev.fuelSlot) or turtle.getItemCount(dev.fuelSlot) == 0 then turtle.transferTo(dev.fuelSlot,64) end -- Move fuel into slot if turtle.compareTo(dev.fuelSlot) or turtle.getItemCount(dev.fuelSlot) == 0 then turtle.transferTo(dev.fuelSlot,64) end -- Move fuel into slot
end end
end -- Slot 4 fuel end -- Slot 4 fuel
for nDev,dev in ipairs(devTurtles) do for _, dev in ipairs(devTurtles) do
local devList=dev.list()
if turtle.getItemCount(3) == 0 and turtle.getItemCount(dev.outputSlot) > 0 then if turtle.getItemCount(3) == 0 and turtle.getItemCount(dev.outputSlot) > 0 then
turtle.select(dev.outputSlot) turtle.select(dev.outputSlot)
turtle.transferTo(3,64) turtle.transferTo(3, 64)
elseif turtle.getItemCount(dev.outputSlot) > 0 and turtle.select(3) and turtle.compareTo(dev.outputSlot) then -- Move output elseif turtle.getItemCount(dev.outputSlot) > 0 and turtle.select(3) and turtle.compareTo(dev.outputSlot) then -- Move output
--turtle.setStatus("moving") --turtle.setStatus("moving")
turtle.select(dev.outputSlot) turtle.select(dev.outputSlot)
turtle.transferTo(3,64) turtle.transferTo(3, 64)
end end
if useSlot4 == "output" then -- Slot 4 output if useSlot4 == "output" then -- Slot 4 output
if turtle.getItemCount(4) == 0 and turtle.getItemCount(dev.outputSlot) > 0 then if turtle.getItemCount(4) == 0 and turtle.getItemCount(dev.outputSlot) > 0 then
turtle.select(dev.outputSlot) turtle.select(dev.outputSlot)
turtle.transferTo(4,64) turtle.transferTo(4, 64)
elseif turtle.getItemCount(dev.outputSlot) > 0 and turtle.select(4) and turtle.compareTo(dev.outputSlot) then elseif turtle.getItemCount(dev.outputSlot) > 0 and turtle.select(4) and turtle.compareTo(dev.outputSlot) then
turtle.select(dev.outputSlot) turtle.select(dev.outputSlot)
turtle.transferTo(4,64) turtle.transferTo(4, 64)
end end
end end
end end
end end
function manageRemoteResources() local function manageRemoteResources()
local localList=turtle.list() local localList = listInventory()
for nDev,dev in ipairs(devTurtles) do for _, dev in ipairs(devTurtles) do
local devList=dev.list() local devList = dev.list()
--print(textutils.serialize(devList)) --print(textutils.serialize(devList))
if devList[2] == nil then if devList[2] == nil then
if localList[dev.fuelSlot] ~= nil then if localList[dev.fuelSlot] ~= nil then
turtle.setStatus("fueling") turtle.setStatus("fueling")
dev.pullItems(devModem.getNameLocal(),dev.fuelSlot,64,2) dev.pullItems(devModem.getNameLocal(), dev.fuelSlot, 64, 2)
end end
else else
if localList[dev.fuelSlot] ~= nil and localList[dev.fuelSlot].name == devList[2].name then if localList[dev.fuelSlot] ~= nil and localList[dev.fuelSlot].name == devList[2].name then
turtle.setStatus("fueling") turtle.setStatus("fueling")
dev.pullItems(devModem.getNameLocal(),dev.fuelSlot,64,2) dev.pullItems(devModem.getNameLocal(), dev.fuelSlot, 64, 2)
end end
end end
if devList[1] == nil then if devList[1] == nil then
if localList[dev.inputSlot] ~= nil then if localList[dev.inputSlot] ~= nil then
turtle.setStatus("pushing") turtle.setStatus("pushing")
dev.pullItems(devModem.getNameLocal(),dev.inputSlot,64,1) dev.pullItems(devModem.getNameLocal(), dev.inputSlot, 64, 1)
end end
else else
if localList[dev.inputSlot] ~= nil and localList[dev.inputSlot].name == devList[1].name then if localList[dev.inputSlot] ~= nil and localList[dev.inputSlot].name == devList[1].name then
turtle.setStatus("pushing") turtle.setStatus("pushing")
dev.pullItems(devModem.getNameLocal(),dev.inputSlot,64,1) dev.pullItems(devModem.getNameLocal(), dev.inputSlot, 64, 1)
end end
end -- Move input end -- Move input
if devList[3] ~= nil then if devList[3] ~= nil then
if localList[dev.outputSlot] == nil then if localList[dev.outputSlot] == nil then
turtle.setStatus("pulling") turtle.setStatus("pulling")
dev.pushItems(devModem.getNameLocal(),3,64,dev.outputSlot) dev.pushItems(devModem.getNameLocal(), 3, 64, dev.outputSlot)
end end
else else
if localList[dev.outputSlot] ~= nil then if localList[dev.outputSlot] ~= nil then
if devList[3] ~= nil and localList[dev.outputSlot].name == devList[3].name then if devList[3] ~= nil and localList[dev.outputSlot].name == devList[3].name then
turtle.setStatus("pulling") turtle.setStatus("pulling")
dev.pushItems(devModem.getNameLocal(),3,64,dev.outputSlot) dev.pushItems(devModem.getNameLocal(), 3, 64, dev.outputSlot)
end end
end end
end end
end end
end end
local lastList={}
while true do -- Main Loop while true do -- Main Loop
local turtleList=turtle.list() local turtleList = listInventory()
if turtleList[1]~=nil or turtleList[13]~=nil or turtleList[14]~=nil or turtleList[15]~=nil or turtleList[16]~=nil or ((useSlot4=="input" or useSlot4=="fuel") and turtleList[4]~=nil) then if turtleList[1] ~= nil or turtleList[13] ~= nil or turtleList[14] ~= nil or turtleList[15] ~= nil or turtleList[16] ~= nil or ((useSlot4 == "input" or useSlot4 == "fuel") and turtleList[4] ~= nil) then
manageLocalResources() manageLocalResources()
end end
manageRemoteResources() manageRemoteResources()
turtle.setStatus("sleeping") turtle.setStatus("sleeping")
os.sleep(1) sleep(1)
end end