Advertisement
Guest User

reactor.lua

a guest
Feb 15th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.16 KB | None | 0 0
  1. os.sleep(5)
  2. --------------------------INIT BEGIN-----------------------------------------------------
  3. --init libraries
  4. local component = require("component")
  5. local term = require("term")
  6. local computer = require("computer")
  7. local thread = require("thread")
  8. local event = require("event")
  9.  
  10. --Set GPU's and screens
  11. local gpu = component.proxy(component.get("40a"))
  12. local termgpu = component.proxy(component.get("92a"))
  13. local screen_address_main = component.get("1fe")
  14. local screen_address_term = component.get("e8b")
  15.  
  16. --Prepare main screen and sets up terminal bind
  17. gpu.bind(screen_address_main)
  18. gpu.setResolution(90,25)
  19. local w,h = gpu.getResolution()
  20. gpu.fill(1,1,w,h," ")
  21.  
  22. termgpu.bind(screen_address_term)
  23. term.bind(termgpu)
  24.  
  25.  
  26. --Init reactors and static variables
  27. local r1 = component.proxy("0c60c163-e924-47d3-ac83-6c686e854f04")
  28. local r2 = component.proxy("0ef58c7f-2984-4f8f-88b7-3474767dbb7a")
  29. local r3 = component.proxy("255aa1b0-b6dd-4e8c-8339-c04d548eea57")
  30. local r1gen, r2gen, r3gen = 31680, 304128, 916692
  31. local close = false
  32. local matrix = component.induction_matrix
  33. local maxEnergy = matrix.getMaxEnergy() / 2.5
  34.  
  35. --------------------------INIT END-------------------------------------------------------
  36.  
  37. ---------------------------FUNCTION BEGIN------------------------------------------------
  38. function r1a()
  39.   r1.activate()
  40. end
  41.  
  42. function r1d()
  43.   r1.deactivate()
  44. end
  45.  
  46. function r2a()
  47.   r2.activate()
  48. end
  49.  
  50. function r2d()
  51.   r2.deactivate()
  52. end
  53.  
  54. function r3a()
  55.   r3.activate()
  56. end
  57.  
  58. function r3d()
  59.   r3.deactivate()
  60. end
  61.  
  62. function activate_all()
  63.   r1.activate()
  64.   r2.activate()
  65.   r3.activate()
  66. end
  67.  
  68. function deactivate_all()
  69.   r1.deactivate()
  70.   r2.deactivate()
  71.   r3.deactivate()
  72. end
  73.  
  74. function write(x,y,z,colour)
  75.   gpu.setBackground(0x000000)
  76.   gpu.setForeground(0xFFFFFF)
  77.   gpu.set(x,y,"                              ")
  78.   gpu.setForeground(colour)
  79.   gpu.set(x,y,z)
  80.   gpu.setForeground(0xFFFFFF)
  81. end
  82.  
  83. function exitprogram(_,_,_,z,_)
  84.   if z == 207 then
  85.     event.ignore("key_down", exitprogram)
  86.     io.write("Program \n")
  87.     close = true  
  88.   end
  89. end
  90. ------------------------------FUNCTION END-----------------------------------------------
  91.  
  92. --Events register
  93. event.listen("key_down", exitprogram)
  94.  
  95. ----------------------CONTROL THREAD BEGIN-----------------------------------------------
  96. --init thread
  97. local control_thread = thread.create(function()
  98.   while true do
  99.     local output = matrix.getOutput() / 2.5
  100.     local current = matrix.getEnergy() / 2.5
  101.  
  102.   --Check if energy cell capacity exceeds 80%
  103.    
  104.     if current/maxEnergy > 0.8 then
  105.  
  106.       --Turn off all reactors if they aren't already
  107.  
  108.       if r1.isProcessing() or r2.isProcessing() or r3.isProcessing() == true then
  109.       deactivate_all()
  110.       end
  111.     end
  112.    
  113.     if current/maxEnergy < 0.8 then
  114.       --Check if energy cell capacity is less than 80% and then checks current output compared to reactor rates and current reactor status
  115.      
  116.       local r1flagproc = r1.isProcessing()
  117.       local r2flagproc = r2.isProcessing()
  118.       local r3flagproc = r3.isProcessing()
  119.       local r1flag,r2flag,r3flag = false,false,false
  120.      
  121.       --Set flags to determine which reactors need to be turned on
  122.      
  123.       if output > r1gen + r2gen then
  124.         r1flag,r2flag,r3flag = true,true,true      
  125.       elseif output > r1gen then
  126.         r1flag,r2flag = true,true
  127.       else
  128.         r1flag = true
  129.       end
  130.      
  131.       --Checks set flags and adjusts the reactors status if need be. No unneccessary on/off commands are sent
  132.       if r1flag == true and r1flagproc == false then
  133.         r1a()
  134.       elseif r1flag == false and r1flagproc == true then
  135.         r1d()
  136.       else
  137.       end
  138.      
  139.       if r2flag == true and r2flagproc == false then
  140.         r2a()
  141.       elseif r2flag == false and r2flagproc == true then
  142.         r2d()
  143.       else
  144.       end
  145.      
  146.       if r3flag == true and r3flagproc == false then
  147.         r3a()
  148.       elseif r3flag == false and r3flagproc == true then
  149.         r3d()
  150.       else
  151.       end  
  152.     end
  153.     os.sleep(30)
  154.   end
  155. end)
  156. -----------------------------CONTROL THREAD END------------------------------------------
  157.  
  158. ----------------------------SCREEN THREAD BEGIN------------------------------------------
  159.  
  160.  
  161.  
  162.  
  163.  
  164.  
  165.  
  166.  
  167.  
  168. -----------------------------SCREEN THREAD END-------------------------------------------
  169.  
  170. ------------------------------IO THREAD BEGIN--------------------------------------------
  171. local io_thread = thread.create(function()
  172.   while true do
  173.    --terminal thread to allow commands to be executed
  174.    term.clear()
  175.    term.setCursor(1,1)
  176.    term.write("Reactor Control active \n")
  177.    term.write("Awaiting Input")
  178.    term.setCursor(1,5)
  179.    term.write("Press END to exit program")
  180.    term.setCursor(1,3)
  181.    local input = term.read()
  182.    load(input)()
  183.   end
  184. end)
  185. -----------------------------IO THREAD END-----------------------------------------------
  186.  
  187. ----------------------------KILL THREAD BEGIN--------------------------------------------
  188. local kill_thread = thread.create(function()
  189.   while true do
  190.     if close == true then
  191.       break
  192.     end
  193.     os.sleep(1)
  194.   end
  195. end)
  196. ---------------------------KILL THREAD END-----------------------------------------------
  197.  
  198. thread.waitForAny({control_thread,io_thread,kill_thread})
  199. io.write("Program closing \n")
  200. term.clear()
  201. os.exit(0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement