Advertisement
Guest User

reactor.lua

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