Advertisement
PaymentOption

Obsidian Computer

Oct 7th, 2012
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.34 KB | None | 0 0
  1. -- VARIABLES --
  2. bRunning = true -- Whether or not the program is running.
  3. tConnectedTurtles = {}
  4. ---------------
  5.  
  6. -- Locates and opens any modem that exists on this computer.
  7. -- Throws an error if no modem exists.
  8. function openExistantModem()
  9.     local tSides = rs.getSides()
  10.  
  11.     for nSideIndex, sSide in ipairs(tSides) do
  12.         if peripheral.isPresent(sSide) and peripheral.getType(sSide) == "modem" then
  13.             rednet.open(sSide)
  14.             return
  15.         end
  16.     end
  17.  
  18.     error("No modem found.")
  19. end
  20.  
  21. -- SETUP:
  22. --  Each turtle will have a chest behind them and a stack of redstone.
  23. --  There will be three blocks in front of them, where the obsidian will be.
  24.  
  25. -- Opens or closes the pistons, depending on their current state.
  26. -- CLOSED: Lava will be contained.
  27. -- OPENED: Lave will be lose, generating obsidian.
  28. function togglePistons()
  29.     -- If the redstone is activated, then the pistons are closed, open them.
  30.     if rs.getInput("right") then
  31.         rs.setOutput("back", false)
  32.     -- If the redstone is not activated, then the pistons are open, close them.
  33.     else
  34.         rs.setOutput("back", true)
  35.     end
  36. end
  37.  
  38.  
  39. -- Checks if all of the coneected turtles have the proper materials
  40. -- to perform another collection cycle.
  41. function turtlesHaveProperMaterials()
  42.     for nTurtleIndex, nTurtleID in ipairs(tConnectedTurtles) do
  43.         -- Send a message to the turtle asking if it is ready for another cycle.
  44.         rednet.send(nTurtleID, "READY_FOR_CYCLE?")
  45.         -- Wait for a response from the turtle with a timeout of 3 seconds.
  46.         local nSender, sMessage = rednet.receive(3)
  47.         -- If the message received is nil because the timeout triggered, stop the loop and return false.
  48.         if not sMessage then
  49.             return false
  50.         end
  51.     end
  52.  
  53.     -- If the loop completed without returning, then the turtles are ready to complete another colleciton cycle.
  54.     return true
  55. end
  56.  
  57. -- Sends all of the proper messages to the turtles, telling them
  58. -- to begin the collection of the obsiian that was just generated.
  59. function startObsidianCollection_withTurtles()
  60.     -- Before we begin the collection, make sure all of the turtles have
  61.     -- the proper materials for another cycle.
  62.     -- PROPER MATERIALS:
  63.     -- 3 redstone.
  64.     if turtlesHaveProperMaterials() then
  65.         for nTurtleIndex, nTurtleID in ipairs(tConnectedTurtles) do
  66.             rednet.send(nTurtleID, "BEGIN_COLLECTION")
  67.         end
  68.  
  69.         return true
  70.     else
  71.         bRunning = false
  72.         return false
  73.     end
  74. end
  75.  
  76. -- Sends acknowledgements to all of the turtles so that they will stop sending confirmations,
  77. -- in the case of a failure during the collection process.
  78. function sendEmergencyAcknowledgements()
  79.     for nTurtleIndex, nTurtleID in ipairs(tConnectedTurtles) do
  80.         rednet.send(nTurtleID, "COMPLETION_ACKNOWLEDGED")
  81.     end
  82. end
  83.  
  84. -- Waits for the confirmation message from all turtles, then closes the pistons.
  85. -- ASSUMES THAT THE PISTONS ARE OPEN!
  86. -- Turtles perform their collection routine, then send confirmation messages
  87. -- back to this computer. This computer will send an acknowledgement back to
  88. -- the turtles, then the turtles will await another collection request. TURTLES
  89. -- WILL CONTINUE SENDING CONFIRMATIONS UNTIL ACKNOWLEDGED!!!
  90. function waitForCollectionCompletion()
  91.     for nTurtleID, nTurtleID in ipairs(tConnectedTurtles) do
  92.         local nSender, sMessage = rednet.receive(8)
  93.  
  94.  
  95.         -- If the message received was not from a turtle, and was about a completed collection, close the pistons.
  96.         if sMessage ~= "COLLECTION_COMPLETE" then
  97.             togglePistons()
  98.             sendEmergencyAcknowledgements() -- Send out acknowledgements just so that the turtles will stop sending confirmations.
  99.  
  100.             bRunning = false
  101.             return false
  102.         -- If the message was a completion confirmation, acknowledge it so we can listen for other confirmations.
  103.         else
  104.             rednet.send(nSender, "COMPLETION_ACKNOWLEDGED")
  105.         end
  106.     end
  107.  
  108.     -- Now that all of the turtles have sent a confirmation message, close the pistons.
  109.     togglePistons()
  110.     return true
  111. end
  112.  
  113. -- Incorporates all above methods into one collection cycle.
  114. function collectionCycle()
  115.     if startObsidianCollection_withTurtles() then
  116.         if waitForCollectionCompletion() then
  117.             return true
  118.         else
  119.             return false
  120.         end
  121.     else
  122.         return false
  123.     end
  124. end
  125.  
  126. -- The main loop of the program.
  127. print("Cycling started...")
  128. openExistantModem()
  129. while bRunning do
  130.     local sInput = read()
  131.  
  132.     if sInput == "start" then
  133.         if not collectionCycle() then
  134.             print("Cycle failure.")
  135.         end
  136.     end
  137. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement