Advertisement
Yorinar

BigReactorController

Apr 11th, 2014
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.34 KB | None | 0 0
  1. local reactor = peripheral.wrap("back")
  2. local maxEnergy = 10000000.0
  3.  
  4. local lowerThreshold = 25.0
  5. local upperThreshold = 90.0
  6.  
  7. local temperatureThreshold = 700
  8.  
  9. function computePercent()
  10.   return 100.0 * reactor.getEnergyStored() / maxEnergy
  11. end
  12.  
  13. function powerState()
  14.   if computePercent() < lowerThreshold then
  15.     return -1
  16.   elseif computePercent() < upperThreshold then
  17.     return 0
  18.   else
  19.     return 1
  20.   end
  21. end
  22.  
  23. function activate()
  24.   print("Powering up reactor [" .. computePercent() .. "%] (" .. reactor.getTemperature() .. " C)")
  25.   reactor.setActive(true)
  26. end
  27.  
  28. function deactivate()
  29.   print("Powering down reactor [" .. computePercent() .. "%] (" .. reactor.getTemperature() .. " C)")
  30.   reactor.setActive(false)
  31. end
  32.  
  33. print("Starting energy: " .. computePercent() .. "%")
  34.  
  35. if powerState() < 0 then
  36.   activate()
  37. elseif powerState() > 0 then
  38.   deactivate()
  39. end
  40.  
  41. while true do
  42.   if powerState() < 0 then
  43.     while powerState() < 1 do
  44.       if reactor.getTemperature() > temperatureThreshold and reactor.getActive() then
  45.         deactivate()
  46.         sleep(5)
  47.       else
  48.         if not reactor.getActive() then
  49.           activate()
  50.         end
  51.         sleep(1)
  52.       end
  53.     end
  54.   elseif powerState() > 0 then
  55.     if reactor.getActive() then
  56.       deactivate()
  57.     end
  58.     sleep(20)
  59.   else
  60.     sleep(10)
  61.   end
  62. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement