FakoTheGreat

Monitor Free Reactor Code

Oct 26th, 2014
267
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.65 KB | None | 0 0
  1. -- Use sides or modem names to connect the devices
  2. local reactorName = "back"
  3. local redstoneListener = "top"
  4. local redstoneOutput = "right"
  5.  
  6. -- How long to wait for various parts of the program. Do note that
  7. -- any changes to sleepDelay will affect updateDelay.
  8. local connectDelay = 1
  9. local updateDelay = 2
  10. local sleepDelay = 1
  11.  
  12. -- Whether or not to clear display between printouts
  13. local clearDisplay = true
  14.  
  15. -- Used internally. Do not adjust.
  16. local reactor = nil
  17. local isOnline = true
  18. local hasFuel = true
  19. local rodLevels = 100
  20. local updateCountdown = 0
  21. local redPower = 0
  22.  
  23. function getConnected()
  24.   while reactor == nil do
  25.     reactor=peripheral.wrap(reactorName)
  26.     if reactor == nil then
  27.       print("Waiting for reactor peripheral")
  28.       os.sleep(reactorDelay)
  29.     end
  30.   end
  31.  
  32.   print("Found reactor.")
  33.  
  34.   while not reactor.getConnected() do
  35.     print("Waiting for reactor to connect.")
  36.     os.sleep(reactorDelay)
  37.   end
  38.  
  39.   print("Program Initialized.")
  40. end
  41.  
  42. function adjustLevels()
  43.   if reactor.getEnergyStored()>= 1 then
  44.     rodLevels = math.floor(reactor.getEnergyStored()/100000)
  45.   else
  46.     rodLevels = 0 --Full Draw
  47.   end
  48.   reactor.setAllControlRodLevels(rodLevels)
  49. end
  50.  
  51. function checkIssues()  
  52.   if reactor.getFuelAmount()<=100 and isOnline then
  53.       rodLevels = 100
  54.       reactor.setAllControlRodLevels(100)
  55.       reactor.setActive(false)
  56.       hasFuel = false
  57.   else
  58.       hasFuel = true
  59.   end
  60.  
  61.   if rs.getInput(redstoneListener)==false and hasFuel then
  62.       reactor.setActive(true)
  63.       isOnline = true
  64.   end
  65.  
  66.   if rs.getInput(redstoneListener)==true and hasFuel then
  67.       rodLevels = 100
  68.       reactor.setActive(false)
  69.       reactor.setAllControlRodLevels(100)
  70.       isOnline = false
  71.   end
  72. end
  73.  
  74. function displayUpdate()
  75.   if clearDisplay then
  76.     term.clear()
  77.     term.setCursorPos(1,1)
  78.   end
  79.   if not hasFuel then
  80.     print("Reactor Offline - No Fuel")
  81.   elseif not isOnline then
  82.     print("Reactor Offline - Detected Redstone")
  83.   else
  84.     print("Reactor Online - "..(100 - rodLevels).."% Production.")
  85.   end
  86. end
  87.  
  88. function determineOutput()
  89.   if not (redstoneListener == redstoneOutput) then
  90.     if rodLevels < 100 then
  91.       redPower = math.floor((100 - rodLevels) / 15) + 1
  92.     else
  93.       redPower = 0
  94.     end
  95.     rs.setAnalogOutput(redstoneOutput, redPower)
  96.   end
  97. end
  98.  
  99. getConnected()
  100.  
  101. while true do
  102.   checkIssues()
  103.  
  104.   if hasFuel and isOnline then
  105.     adjustLevels()
  106.   end
  107.  
  108.   determineOutput()
  109.  
  110.   if updateCountdown > 0 then
  111.     updateCountdown = updateCountdown - 1
  112.   else
  113.     updateCountdown = updateDelay
  114.     displayUpdate()
  115.   end
  116.  
  117.   os.sleep(sleepDelay)
  118. end
Add Comment
Please, Sign In to add comment