Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- VARIABLES --
- bRunning = true -- Whether or not the program is running.
- tConnectedTurtles = {}
- ---------------
- -- Locates and opens any modem that exists on this computer.
- -- Throws an error if no modem exists.
- function openExistantModem()
- local tSides = rs.getSides()
- for nSideIndex, sSide in ipairs(tSides) do
- if peripheral.isPresent(sSide) and peripheral.getType(sSide) == "modem" then
- rednet.open(sSide)
- return
- end
- end
- error("No modem found.")
- end
- -- SETUP:
- -- Each turtle will have a chest behind them and a stack of redstone.
- -- There will be three blocks in front of them, where the obsidian will be.
- -- Opens or closes the pistons, depending on their current state.
- -- CLOSED: Lava will be contained.
- -- OPENED: Lave will be lose, generating obsidian.
- function togglePistons()
- -- If the redstone is activated, then the pistons are closed, open them.
- if rs.getInput("right") then
- rs.setOutput("back", false)
- -- If the redstone is not activated, then the pistons are open, close them.
- else
- rs.setOutput("back", true)
- end
- end
- -- Checks if all of the coneected turtles have the proper materials
- -- to perform another collection cycle.
- function turtlesHaveProperMaterials()
- for nTurtleIndex, nTurtleID in ipairs(tConnectedTurtles) do
- -- Send a message to the turtle asking if it is ready for another cycle.
- rednet.send(nTurtleID, "READY_FOR_CYCLE?")
- -- Wait for a response from the turtle with a timeout of 3 seconds.
- local nSender, sMessage = rednet.receive(3)
- -- If the message received is nil because the timeout triggered, stop the loop and return false.
- if not sMessage then
- return false
- end
- end
- -- If the loop completed without returning, then the turtles are ready to complete another colleciton cycle.
- return true
- end
- -- Sends all of the proper messages to the turtles, telling them
- -- to begin the collection of the obsiian that was just generated.
- function startObsidianCollection_withTurtles()
- -- Before we begin the collection, make sure all of the turtles have
- -- the proper materials for another cycle.
- -- PROPER MATERIALS:
- -- 3 redstone.
- if turtlesHaveProperMaterials() then
- for nTurtleIndex, nTurtleID in ipairs(tConnectedTurtles) do
- rednet.send(nTurtleID, "BEGIN_COLLECTION")
- end
- return true
- else
- bRunning = false
- return false
- end
- end
- -- Sends acknowledgements to all of the turtles so that they will stop sending confirmations,
- -- in the case of a failure during the collection process.
- function sendEmergencyAcknowledgements()
- for nTurtleIndex, nTurtleID in ipairs(tConnectedTurtles) do
- rednet.send(nTurtleID, "COMPLETION_ACKNOWLEDGED")
- end
- end
- -- Waits for the confirmation message from all turtles, then closes the pistons.
- -- ASSUMES THAT THE PISTONS ARE OPEN!
- -- Turtles perform their collection routine, then send confirmation messages
- -- back to this computer. This computer will send an acknowledgement back to
- -- the turtles, then the turtles will await another collection request. TURTLES
- -- WILL CONTINUE SENDING CONFIRMATIONS UNTIL ACKNOWLEDGED!!!
- function waitForCollectionCompletion()
- for nTurtleID, nTurtleID in ipairs(tConnectedTurtles) do
- local nSender, sMessage = rednet.receive(8)
- -- If the message received was not from a turtle, and was about a completed collection, close the pistons.
- if sMessage ~= "COLLECTION_COMPLETE" then
- togglePistons()
- sendEmergencyAcknowledgements() -- Send out acknowledgements just so that the turtles will stop sending confirmations.
- bRunning = false
- return false
- -- If the message was a completion confirmation, acknowledge it so we can listen for other confirmations.
- else
- rednet.send(nSender, "COMPLETION_ACKNOWLEDGED")
- end
- end
- -- Now that all of the turtles have sent a confirmation message, close the pistons.
- togglePistons()
- return true
- end
- -- Incorporates all above methods into one collection cycle.
- function collectionCycle()
- if startObsidianCollection_withTurtles() then
- if waitForCollectionCompletion() then
- return true
- else
- return false
- end
- else
- return false
- end
- end
- -- The main loop of the program.
- print("Cycling started...")
- openExistantModem()
- while bRunning do
- local sInput = read()
- if sInput == "start" then
- if not collectionCycle() then
- print("Cycle failure.")
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement