Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- To use connect 4 chests and any number of furnaces to the computer.
- To reset the peripherals list use the argument "new" when running the program.
- --]]
- local tArgs = {...}
- --Reset peripheral list
- if #tArgs == 1 then
- if tArgs[1] == "new" then
- fs.delete("./peripherals")
- end
- end
- local chests = {}
- local furnaces = {}
- --clears the screen
- local function clear()
- term.clear()
- term.setCursorPos(1,1)
- end
- --writes all the needed peripherals names to a file
- local function getPeripherals()
- clear()
- local file = fs.open("./peripherals","w")
- local chests = {}
- for k, v in pairs(peripheral.getNames()) do
- if string.find(v,"chest") then
- table.insert(chests,v)
- end
- end
- if #chests < 4 then
- error("Not enough chests connected.")
- end
- local lines = {
- "input",
- "fuel",
- "output",
- "buffer"
- }
- local chestList = {}
- for i=1, 4 do
- print("Choose the "..lines[i].." chest:")
- for k, v in pairs(chests) do
- print(k..": "..v)
- end
- local correct = false
- while not correct do
- local input = tonumber(read())
- if chests[input] then
- file.writeLine(chests[input])
- table.insert(chestList,chests[input])
- table.remove(chests,input)
- correct = true
- else
- print("Input the index of the chest you choose:")
- end
- end
- clear()
- end
- local furnaces = {}
- for k, v in pairs(peripheral.getNames()) do
- if string.find(v,"furnace") then
- table.insert(furnaces, v)
- file.writeLine(v)
- end
- end
- file.close()
- return chestList, furnaces
- end
- --Code
- --If the file does not exist in creates a new one.
- --If it does then it reads from it.
- if not fs.exists("./peripherals") then
- chests, furnaces = getPeripherals()
- else
- local file = fs.open("./peripherals", "r")
- for i=1, 4 do
- table.insert(chests,file.readLine())
- end
- local done = false
- while not done do
- local line = file.readLine()
- if line then
- table.insert(furnaces, line)
- else
- done = true
- end
- end
- file.close()
- end
- --program loop
- local finish = false
- while not finish do
- --input
- local buffer = peripheral.wrap(chests[4])
- local items = 0
- for i = 1, 54 do
- items = buffer.pullItems(chests[1],i)
- if items ~= 0 then break end
- end
- if buffer.list()[1] then
- items = buffer.list()[1].count
- else items = 0 end
- local n = math.floor(items/#furnaces)
- if n <= 0 then n = 1 end
- for k, v in pairs(furnaces) do
- buffer.pushItems(v,1,n)
- if items == 0 then break end
- end
- --fuel
- local items = 0
- for i = 1, 54 do
- items = buffer.pullItems(chests[2],i,64,2)
- if items ~= 0 then break end
- end
- if buffer.list()[2] then
- items = buffer.list()[2].count
- else items = 0 end
- local n = math.floor(items/#furnaces)
- if n <= 0 then n = 1 end
- for k, v in pairs(furnaces) do
- buffer.pushItems(v,2,n,2)
- if items == 0 then break end
- end
- sleep(.05)
- --output
- local output = peripheral.wrap(chests[3])
- for k, v in pairs(furnaces) do
- output.pullItems(v,3)
- end
- --exit loop
- if rs.getInput("top") then
- finish = true
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement