dlord

/airlock/maincontrol

Nov 1st, 2012
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.61 KB | None | 0 0
  1. --   Copyright 2012 John Paul Alcala
  2. --
  3. --   Licensed under the Apache License, Version 2.0 (the "License");
  4. --   you may not use this file except in compliance with the License.
  5. --   You may obtain a copy of the License at
  6. --
  7. --       http://www.apache.org/licenses/LICENSE-2.0
  8. --
  9. --   Unless required by applicable law or agreed to in writing, software
  10. --   distributed under the License is distributed on an "AS IS" BASIS,
  11. --   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. --   See the License for the specific language governing permissions and
  13. --   limitations under the License.
  14.  
  15. local doorControlScript = "/cc/doorcontrol"
  16. local pumpControlScript = "/cc/pumpcontrol"
  17.  
  18. local entranceHatchOpen = function()
  19.   return redstone.testBundledInput("top", colors.red) ~= true
  20. end
  21.  
  22. local exitHatchOpen = function()
  23.   return redstone.testBundledInput("top", colors.yellow) ~= true
  24. end
  25.  
  26. local openAirlockCommand = function()
  27.   return redstone.testBundledInput("top", colors.cyan)
  28. end
  29.  
  30. local closeAirlockCommand = function()
  31.   return redstone.testBundledInput("top", colors.lime)
  32. end
  33.  
  34. local openAirlock = function()
  35.   if exitHatchOpen() then
  36.     shell.run(doorControlScript, "close", "exit")
  37.     shell.run(pumpControlScript, "drain")
  38.     shell.run(doorControlScript, "open", "entrance")
  39.   end
  40. end
  41.  
  42. local closeAirlock = function()
  43.   if entranceHatchOpen() then
  44.     shell.run(doorControlScript, "close", "entrance")
  45.     shell.run(pumpControlScript, "fill")
  46.     shell.run(doorControlScript, "open", "exit")
  47.   end
  48. end
  49.  
  50. local sendOkSignal = function()
  51.   redstone.setBundledOutput("top", colors.cyan+colors.lime)
  52.   sleep(0.5)
  53.   redstone.setBundledOutput("top", 0)
  54. end
  55.  
  56. local testAirlockState = function()
  57.   if entranceHatchOpen() == exitHatchOpen() then
  58.     print("WARNING! Airlock state has been compromised.")
  59.     print("Correcting state...")
  60.     print("")
  61.    
  62.     shell.run(doorControlScript, "close", "entrance")
  63.     shell.run(doorControlScript, "close", "exit")
  64.     shell.run(pumpControlScript, "drain")
  65.     shell.run(doorControlScript, "open", "entrance")
  66.  
  67.     print("Finished correcting Airlock state.")
  68.   end
  69. end
  70.  
  71. -- Run the airlock server
  72. term.clear()
  73. term.setCursorPos(1,1)
  74.  
  75. print("Starting Airlock Control...")
  76. sleep(2)
  77. print("Checking Airlock state...")
  78. sleep(2)
  79. testAirlockState()
  80. print("Airlock Control is now running.")
  81.  
  82. while true do
  83.   testAirlockState()
  84.   local event = os.pullEvent("redstone")
  85.   if openAirlockCommand() then
  86.     openAirlock()
  87.     sendOkSignal()
  88.   elseif closeAirlockCommand() then
  89.     closeAirlock()
  90.     sendOkSignal()
  91.   end
  92. end
Advertisement
Add Comment
Please, Sign In to add comment