Advertisement
mycosis

Computercraft - Reactor management

May 28th, 2015
291
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.92 KB | None | 0 0
  1. rednet.close("bottom")
  2. rednet.open("bottom")
  3. mon = peripheral.wrap("top")
  4. monHeader = " Reactor and Turbine - No. ##HEADERNUM##"
  5. rednetReactor = "BigReactors-Reactor_##MACHINENUM##"
  6. rednetTurbine = "BigReactors-Turbine_##MACHINENUM##"
  7.  
  8. function alarm(seconds)
  9.     rednet.send(##ALARMID##,"alarmOn")
  10.     sleep(seconds)
  11.     rednet.send(##ALARMID##,"alarmOff")
  12. end
  13.  
  14.  
  15. -- Initial reactor and turbine status
  16. reactor = peripheral.wrap(rednetReactor)
  17. turbine = peripheral.wrap(rednetTurbine)
  18. if reactor.getActive() == true or turbine.getActive() == true then
  19.     reactorBtnText = "Deactivate"
  20. else
  21.     reactorBtnText = " Activate "
  22. end
  23.  
  24. -- Write monitor text colors
  25. function writeRed(text)
  26.     mon.setTextColor(colors.red)
  27.     mon.write(text)
  28.     mon.setTextColor(colors.white)
  29. end
  30. function writeGreen(text)
  31.     mon.setTextColor(colors.green)
  32.     mon.write(text)
  33.     mon.setTextColor(colors.white)
  34. end
  35. function writeOrange(text)
  36.     mon.setTextColor(colors.orange)
  37.     mon.write(text)
  38.     mon.setTextColor(colors.white)
  39. end
  40.  
  41. function resetScreen()
  42.     mon.setBackgroundColor(colors.black)
  43.     mon.setTextColor(colors.white)
  44.     mon.clear()
  45.     mon.setCursorPos(1,2)
  46.     mon.write(monHeader)
  47.     mon.setCursorPos(2,4)
  48. end
  49.  
  50. function nextWriteLine()
  51.     local cX,cY = mon.getCursorPos()
  52.     mon.setCursorPos(2,cY+1)
  53. end
  54.  
  55. function reactorOn()
  56.     -- Reset screen
  57.     resetScreen()
  58.    
  59.     -- Activate reactor
  60.     mon.write("Reactor... ")
  61.     if reactor.getActive() == false then
  62.         alarm(4)
  63.         reactor.setActive(true)
  64.         writeGreen("Activated")
  65.     else
  66.         writeOrange("Already active")
  67.     end
  68.     nextWriteLine()
  69.    
  70.     sleep(2) -- For dramatic effect
  71.    
  72.     -- Activate turbine
  73.     mon.write("Turbine... ")
  74.     if turbine.getActive() == false then
  75.         alarm(2)
  76.         turbine.setActive(true)
  77.         writeGreen("Activated")
  78.     else
  79.         writeOrange("Already active")
  80.     end
  81.     nextWriteLine()
  82.    
  83.     sleep(2) -- For dramatic effect
  84.  
  85.     -- Disengage induction coils
  86.     if turbine.getInductorEngaged() == true then
  87.         mon.write("Ind. Coils... ")
  88.         turbine.setInductorEngaged(false)
  89.         writeGreen("Disengaged")
  90.         nextWriteLine()
  91.         sleep(2) -- For dramatic effect
  92.     end
  93.  
  94.     -- Activate induction coils at 1780 RPM
  95.     nextWriteLine()
  96.     mon.write("Waiting for 1780 RPM")
  97.     nextWriteLine()
  98.     mon.write("Ind. coils... ")
  99.     while turbine.getRotorSpeed() < 1780 do
  100.         sleep(5)
  101.     end
  102.     turbine.setInductorEngaged(true)
  103.     writeGreen("Engaged")
  104.    
  105.     sleep(15) -- For dramatic effect
  106. end
  107.  
  108. function reactorOff()
  109.     -- Reset screen
  110.     resetScreen()
  111.    
  112.     -- Deactivate reactor
  113.     mon.write("Reactor... ")
  114.     if reactor.getActive() == true then
  115.         alarm(4)
  116.         reactor.setActive(false)
  117.         writeGreen("Deactivated")
  118.     else
  119.         writeOrange("Already off")
  120.     end
  121.     nextWriteLine()
  122.     nextWriteLine()
  123.    
  124.     sleep(2) -- For dramatic effect
  125.    
  126.     -- Spin down turbine
  127.     mon.write("Spinning down. Please wait")
  128.     if turbine.getInductorEngaged() == false then
  129.         turbine.setInductorEngaged(true)
  130.     end
  131.     nextWriteLine()
  132.     mon.write("Ind. Coils... ")
  133.     while turbine.getRotorSpeed() > 100 do
  134.         sleep(10)
  135.     end
  136.  
  137.     -- Disengage induction coils
  138.     if turbine.getInductorEngaged() == true then
  139.         turbine.setInductorEngaged(false)
  140.         writeGreen("Disengaged")
  141.         nextWriteLine()
  142.         sleep(2) -- For dramatic effect
  143.     end
  144.  
  145.     -- Deactivate turbine
  146.     mon.write("Turbine... ")
  147.     if turbine.getActive() == true then
  148.         sleep(8) -- For dramatic effect
  149.         alarm(2)
  150.         turbine.setActive(false)
  151.         writeGreen("Deactivated")
  152.     else
  153.         writeOrange("Already off")
  154.     end
  155.     nextWriteLine()
  156.     nextWriteLine()
  157.    
  158.     sleep(2) -- For dramatic effect
  159.    
  160.     mon.write("System powered down.")
  161.     sleep(15) -- For dramatic effect
  162. end
  163.  
  164. local button = { --Our main button table. Contains everything from button draw functions to the button information.
  165.     button_defaults = { --This is the metatable which we give set new buttons to. It provides color/size defaults.
  166.         __index = {
  167.             color_bg = colors.orange;
  168.             color_cl = colors.blue;
  169.             color_txt = colors.black;
  170.            
  171.             height = 3;
  172.             padding = 2;
  173.             isClicked = false;
  174.         };
  175.     };
  176.     mt = { --This is the main metatable of the button table. It changes the behavior of the table to allow for calling and adding new indexes.
  177.         __call = function(self) --This allows us to call the table as if it were a function.
  178.             for index, btn in pairs(self.buttons) do
  179.                 local color = btn.isClicked and btn.color_cl or btn.color_bg
  180.                 mon.setBackgroundColor(color)
  181.                 mon.setTextColor(btn.color_txt)
  182.                 for yPos = btn.y, btn.bounds.y2 do
  183.                     mon.setCursorPos(btn.x, yPos)
  184.                     mon.write(string.rep(" ", btn.width))
  185.                 end
  186.                 local text = btn.isClicked and btn.clickText or btn.text
  187.                 mon.setCursorPos(btn.x + (btn.width/2 - #text/2), btn.y + (btn.height/2))
  188.                 mon.write(text)
  189.             end
  190.         end;
  191.        
  192.         __newindex = function(t, key, value) --This changes the behavior of the table upon adding a new button
  193.             assert(type(value)=="table", "Requires a table") --assert will check that a condition is true; if it is not, it will error with the provided text
  194.             assert(value.x, "Requires initial x")
  195.             assert(value.y, "Requires initial y")
  196.             assert(value.text, "Requires text value")
  197.             setmetatable(value, t.button_defaults) --Give our new button its defaults with the __index metamethod
  198.             value.width = #value.text + (value.padding * 2)
  199.             value.bounds = {
  200.                 x1 = value.x; --I don't use the x1 or y1 vars from the bounds table due to the fact that it's shorter to simply type btn.x than btn.bounds.x, but they are equal to the same thing (obviously)
  201.                 y1 = value.y;
  202.                 x2 = value.x + value.width - 1; --In order to draw and detect clicks correctly, you need to subtract 1 from the width and height.
  203.                 y2 = value.y + value.height - 1;
  204.             }
  205.             t.buttons[key]=value --In the video, I am aware that I used rawset. However, it is actually not necessary because we are not changing the button table directly, but rather the button.buttons table (which has no newindex metamethod)
  206.         end;
  207.     };
  208.  
  209.     checkClick = function(self, x,y) --This checks whether you have actually clicked on a table
  210.         for index, btn in pairs(self.buttons) do
  211.             if x>=btn.x and x<=btn.bounds.x2 and y>=btn.y and y<=btn.bounds.y2 then
  212.                 btn.isClicked = true --If we have actually clicked the button then set its click value to true
  213.                 if btn.onClick then --And check if it has an onClick function
  214.                     btn:onClick() --If so, then execute it and pass the button's table/info into it by using the colon operator
  215.                 end
  216.                 return index --Return the index of the button so we can unhighlight it
  217.             end
  218.         end
  219.     end;
  220.    
  221.     buttons = {}; --This is the table that we'll keep all the buttons in
  222.    
  223. }
  224. setmetatable(button, button.mt) --Set the metatable of button to button.mt
  225.  
  226. button[1] = {
  227.     x = 9;
  228.     y = 6;
  229.     text = reactorBtnText;
  230.     onClick = function(self)
  231.         if reactor.getActive() == true or turbine.getActive() == true then
  232.             -- Deactivate reactor chain
  233.             reactorOff()
  234.             self.text = " Activate "
  235.         else
  236.             -- Activate reactor chain
  237.             reactorOn()
  238.             self.text = "Deactivate"
  239.         end
  240.     end
  241. }
  242.  
  243. local timer = { --This will keep track of clicked buttons/the timers associated with them
  244.     index = false;
  245.     timer = false;
  246. }
  247.  
  248. term.setCursorPos(1,1)
  249. term.clear()
  250. term.write("Running management")
  251.  
  252. while true do
  253.     resetScreen()
  254.     mon.write()
  255.    
  256.     button() --Always draw the button first
  257.     local e = {os.pullEvent()} --Then pull our events
  258.     if e[1] == "monitor_touch" then
  259.         local index = button:checkClick(e[3], e[4]) --Check the click: make sure to pass the button table into the checkClick function
  260.         if index then
  261.             timer.index =   index--The index of the button that is clicked
  262.             timer.timer = os.startTimer(1)
  263.         end
  264.     elseif e[1] == "timer" and e[2] == timer.timer then --If we get a timer event and the ID is equal to the timer.timer var then
  265.         button.buttons[timer.index].isClicked = false --Deselect the button
  266.         timer = {} --This is actually fairly memory inneficient, but for such a small program it doesn't really matter. Rather than do this though, you should probably just manually set the values of timer.index/timer.timer to false
  267.     end
  268. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement