Advertisement
TwoThe

CCBig Reactor Control

Dec 28th, 2015
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.00 KB | None | 0 0
  1. local REACTOR_AT = "back"
  2. local SHUTDOWN_AT = 90.0
  3. local ACTIVATE_AT = 10.0
  4. local RF_MAX = 10000000     -- maximum power the reactor can store
  5.  
  6. print "Activating reactor control..."
  7. local reactor = peripheral.wrap(REACTOR_AT)
  8.  
  9. if (not reactor) then
  10.   print("No reactor found at ", REACTOR_AT, "!")
  11.   return
  12. end
  13.  
  14. local getStoragePercent = function()
  15.   return reactor.getEnergyStored() / RF_MAX * 100.0
  16. end
  17.  
  18. local printReactorState = function()
  19.   print(string.format("Reactor %s at %3.2f%% storage", (reactor.getActive() and "enabled") or "disabled", getStoragePercent()))
  20. end
  21.  
  22. local setActive = function(active)
  23.   if (reactor.getActive() ~= active) then
  24.     reactor.setActive(active)
  25.     printReactorState()
  26.   end
  27. end
  28.  
  29. printReactorState()
  30. local storage
  31. while true do
  32.   if (reactor.getConnected()) then
  33.     storage = getStoragePercent()
  34.     if (storage <= ACTIVATE_AT) then
  35.       setActive(true)
  36.     elseif (storage >= SHUTDOWN_AT) then
  37.       setActive(false)
  38.     end
  39.   end
  40.  
  41.   os.sleep(1)
  42. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement