Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- rednet.close("bottom")
- rednet.open("bottom")
- mon = peripheral.wrap("top")
- monHeader = " Reactor and Turbine - No. ##HEADERNUM##"
- rednetReactor = "BigReactors-Reactor_##MACHINENUM##"
- rednetTurbine = "BigReactors-Turbine_##MACHINENUM##"
- function alarm(seconds)
- rednet.send(##ALARMID##,"alarmOn")
- sleep(seconds)
- rednet.send(##ALARMID##,"alarmOff")
- end
- -- Initial reactor and turbine status
- reactor = peripheral.wrap(rednetReactor)
- turbine = peripheral.wrap(rednetTurbine)
- if reactor.getActive() == true or turbine.getActive() == true then
- reactorBtnText = "Deactivate"
- else
- reactorBtnText = " Activate "
- end
- -- Write monitor text colors
- function writeRed(text)
- mon.setTextColor(colors.red)
- mon.write(text)
- mon.setTextColor(colors.white)
- end
- function writeGreen(text)
- mon.setTextColor(colors.green)
- mon.write(text)
- mon.setTextColor(colors.white)
- end
- function writeOrange(text)
- mon.setTextColor(colors.orange)
- mon.write(text)
- mon.setTextColor(colors.white)
- end
- function resetScreen()
- mon.setBackgroundColor(colors.black)
- mon.setTextColor(colors.white)
- mon.clear()
- mon.setCursorPos(1,2)
- mon.write(monHeader)
- mon.setCursorPos(2,4)
- end
- function nextWriteLine()
- local cX,cY = mon.getCursorPos()
- mon.setCursorPos(2,cY+1)
- end
- function reactorOn()
- -- Reset screen
- resetScreen()
- -- Activate reactor
- mon.write("Reactor... ")
- if reactor.getActive() == false then
- alarm(4)
- reactor.setActive(true)
- writeGreen("Activated")
- else
- writeOrange("Already active")
- end
- nextWriteLine()
- sleep(2) -- For dramatic effect
- -- Activate turbine
- mon.write("Turbine... ")
- if turbine.getActive() == false then
- alarm(2)
- turbine.setActive(true)
- writeGreen("Activated")
- else
- writeOrange("Already active")
- end
- nextWriteLine()
- sleep(2) -- For dramatic effect
- -- Disengage induction coils
- if turbine.getInductorEngaged() == true then
- mon.write("Ind. Coils... ")
- turbine.setInductorEngaged(false)
- writeGreen("Disengaged")
- nextWriteLine()
- sleep(2) -- For dramatic effect
- end
- -- Activate induction coils at 1780 RPM
- nextWriteLine()
- mon.write("Waiting for 1780 RPM")
- nextWriteLine()
- mon.write("Ind. coils... ")
- while turbine.getRotorSpeed() < 1780 do
- sleep(5)
- end
- turbine.setInductorEngaged(true)
- writeGreen("Engaged")
- sleep(15) -- For dramatic effect
- end
- function reactorOff()
- -- Reset screen
- resetScreen()
- -- Deactivate reactor
- mon.write("Reactor... ")
- if reactor.getActive() == true then
- alarm(4)
- reactor.setActive(false)
- writeGreen("Deactivated")
- else
- writeOrange("Already off")
- end
- nextWriteLine()
- nextWriteLine()
- sleep(2) -- For dramatic effect
- -- Spin down turbine
- mon.write("Spinning down. Please wait")
- if turbine.getInductorEngaged() == false then
- turbine.setInductorEngaged(true)
- end
- nextWriteLine()
- mon.write("Ind. Coils... ")
- while turbine.getRotorSpeed() > 100 do
- sleep(10)
- end
- -- Disengage induction coils
- if turbine.getInductorEngaged() == true then
- turbine.setInductorEngaged(false)
- writeGreen("Disengaged")
- nextWriteLine()
- sleep(2) -- For dramatic effect
- end
- -- Deactivate turbine
- mon.write("Turbine... ")
- if turbine.getActive() == true then
- sleep(8) -- For dramatic effect
- alarm(2)
- turbine.setActive(false)
- writeGreen("Deactivated")
- else
- writeOrange("Already off")
- end
- nextWriteLine()
- nextWriteLine()
- sleep(2) -- For dramatic effect
- mon.write("System powered down.")
- sleep(15) -- For dramatic effect
- end
- local button = { --Our main button table. Contains everything from button draw functions to the button information.
- button_defaults = { --This is the metatable which we give set new buttons to. It provides color/size defaults.
- __index = {
- color_bg = colors.orange;
- color_cl = colors.blue;
- color_txt = colors.black;
- height = 3;
- padding = 2;
- isClicked = false;
- };
- };
- 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.
- __call = function(self) --This allows us to call the table as if it were a function.
- for index, btn in pairs(self.buttons) do
- local color = btn.isClicked and btn.color_cl or btn.color_bg
- mon.setBackgroundColor(color)
- mon.setTextColor(btn.color_txt)
- for yPos = btn.y, btn.bounds.y2 do
- mon.setCursorPos(btn.x, yPos)
- mon.write(string.rep(" ", btn.width))
- end
- local text = btn.isClicked and btn.clickText or btn.text
- mon.setCursorPos(btn.x + (btn.width/2 - #text/2), btn.y + (btn.height/2))
- mon.write(text)
- end
- end;
- __newindex = function(t, key, value) --This changes the behavior of the table upon adding a new button
- 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
- assert(value.x, "Requires initial x")
- assert(value.y, "Requires initial y")
- assert(value.text, "Requires text value")
- setmetatable(value, t.button_defaults) --Give our new button its defaults with the __index metamethod
- value.width = #value.text + (value.padding * 2)
- value.bounds = {
- 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)
- y1 = value.y;
- x2 = value.x + value.width - 1; --In order to draw and detect clicks correctly, you need to subtract 1 from the width and height.
- y2 = value.y + value.height - 1;
- }
- 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)
- end;
- };
- checkClick = function(self, x,y) --This checks whether you have actually clicked on a table
- for index, btn in pairs(self.buttons) do
- if x>=btn.x and x<=btn.bounds.x2 and y>=btn.y and y<=btn.bounds.y2 then
- btn.isClicked = true --If we have actually clicked the button then set its click value to true
- if btn.onClick then --And check if it has an onClick function
- btn:onClick() --If so, then execute it and pass the button's table/info into it by using the colon operator
- end
- return index --Return the index of the button so we can unhighlight it
- end
- end
- end;
- buttons = {}; --This is the table that we'll keep all the buttons in
- }
- setmetatable(button, button.mt) --Set the metatable of button to button.mt
- button[1] = {
- x = 9;
- y = 6;
- text = reactorBtnText;
- onClick = function(self)
- if reactor.getActive() == true or turbine.getActive() == true then
- -- Deactivate reactor chain
- reactorOff()
- self.text = " Activate "
- else
- -- Activate reactor chain
- reactorOn()
- self.text = "Deactivate"
- end
- end
- }
- local timer = { --This will keep track of clicked buttons/the timers associated with them
- index = false;
- timer = false;
- }
- term.setCursorPos(1,1)
- term.clear()
- term.write("Running management")
- while true do
- resetScreen()
- mon.write()
- button() --Always draw the button first
- local e = {os.pullEvent()} --Then pull our events
- if e[1] == "monitor_touch" then
- local index = button:checkClick(e[3], e[4]) --Check the click: make sure to pass the button table into the checkClick function
- if index then
- timer.index = index--The index of the button that is clicked
- timer.timer = os.startTimer(1)
- end
- 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
- button.buttons[timer.index].isClicked = false --Deselect the button
- 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
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement