Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local mon = peripheral.wrap("left")
- local monwidth, monheight = mon.getSize()
- local failsafeDefault = 20
- local failsafe = failsafeDefault
- local screenDebug = 0
- buttonTable = {}
- mon.setBackgroundColor(colors.black)
- mon.clear()
- if screenDebug ~= 0 then
- --This will make the background a checkerboard. For debugging button positions.
- for a = 1, monheight do
- for b = 1, monwidth do
- if a%2 == 0 then
- if b%2 ~= 0 then
- mon.setBackgroundColor(colors.black)
- else
- mon.setBackgroundColor(colors.white)
- end
- else
- if b%2 ~= 0 then
- mon.setBackgroundColor(colors.white)
- else
- mon.setBackgroundColor(colors.black)
- end
- end
- mon.setCursorPos(b,a)
- mon.write(" ")
- end
- end
- end
- function newButton(name, xmin, xmax, ymin, ymax, offfunct, onfunct, offcolor, oncolor, offlabel, onlabel, center, hidden, overwrite)
- buttonTable[name] = {}
- buttonTable[name]["xmin"] = xmin
- buttonTable[name]["xmax"] = xmax
- buttonTable[name]["ymin"] = ymin
- buttonTable[name]["ymax"] = ymax
- buttonTable[name]["offfunct"] = offfunct
- buttonTable[name]["onfunct"] = onfunct
- buttonTable[name]["offcolor"] = offcolor
- buttonTable[name]["oncolor"] = oncolor
- buttonTable[name]["offlabel"] = offlabel
- buttonTable[name]["onlabel"] = onlabel
- buttonTable[name]["center"] = center
- buttonTable[name]["hidden"] = hidden
- buttonTable[name]["overwrite"] = overwrite
- buttonTable[name]["width"] = (xmax - xmin)
- buttonTable[name]["height"] = (ymax - ymin)
- buttonTable[name]["state"] = false
- buttonTable[name]["monmargin"] = (math.floor((monwidth - buttonTable[name]["width"]) / 2) - 1)
- buttonTable[name]["offlength"] = string.len(offlabel)
- buttonTable[name]["offmargin"] = math.floor((buttonTable[name]["width"] - buttonTable[name]["offlength"]) / 2)
- buttonTable[name]["onlength"] = string.len(onlabel)
- buttonTable[name]["onmargin"] = math.floor((buttonTable[name]["width"] - buttonTable[name]["onlength"]) / 2)
- drawButton(name, buttonTable[name]["state"], false)
- end
- function drawButton(name, state, overwrite)
- --Clear button location
- if (buttonTable[name]["overwrite"] == true) or (overwrite == true) then
- for y = 0,(buttonTable[name]["height"]) do
- for x = 0,(buttonTable[name]["width"]) do
- if buttonTable[name]["center"] == true then
- mon.setCursorPos((buttonTable[name]["xmin"] + x + buttonTable[name]["monmargin"]),(buttonTable[name]["ymin"] + y))
- mon.write(" ")
- else
- mon.setCursorPos((buttonTable[name]["xmin"] + x),(buttonTable[name]["ymin"] + y))
- mon.write(" ")
- end
- end
- end
- end
- if buttonTable[name]["hidden"] == false then
- --Set button background
- if state == false then
- mon.setBackgroundColor(buttonTable[name]["offcolor"])
- else
- mon.setBackgroundColor(buttonTable[name]["oncolor"])
- end
- --Set currentLabel
- if state == true then
- currentLabel = buttonTable[name]["onlabel"]
- else
- currentLabel = buttonTable[name]["offlabel"]
- end
- labelLen = string.len(currentLabel)
- textVertMargin = math.ceil(buttonTable[name]["height"] / 2)
- textHoriMargin = math.ceil((buttonTable[name]["width"] - labelLen) / 2)
- --Draw button
- for y = 0,(buttonTable[name]["height"]) do
- for x = 0,(buttonTable[name]["width"]) do
- if buttonTable[name]["center"] == true then
- mon.setCursorPos((buttonTable[name]["xmin"] + x + buttonTable[name]["monmargin"]),(buttonTable[name]["ymin"] + y))
- mon.write(" ")
- else
- mon.setCursorPos((buttonTable[name]["xmin"] + x),(buttonTable[name]["ymin"] + y))
- mon.write(" ")
- end
- end
- end
- --Draw button text
- for y = 0,(buttonTable[name]["height"]) do
- for x = 0,(buttonTable[name]["width"]) do
- if buttonTable[name]["center"] == true then
- mon.setCursorPos((buttonTable[name]["xmin"] + x + buttonTable[name]["monmargin"]),(buttonTable[name]["ymin"] + y))
- if (y == textVertMargin) and (x == textHoriMargin) then
- mon.write(currentLabel)
- end
- else
- mon.setCursorPos((buttonTable[name]["xmin"] + x),(buttonTable[name]["ymin"] + y))
- if (y == textVertMargin) and (x == textHoriMargin) then
- mon.write(currentLabel)
- end
- end
- end
- end
- --Reset background color
- mon.setBackgroundColor(colors.black)
- else
- print("Skipped hidden button: " .. name)
- end
- end
- function toggleButton(name)
- if buttonTable[name]["state"] == false then
- buttonTable[name]["state"] = true
- drawButton(name, true, false)
- else
- buttonTable[name]["state"] = false
- drawButton(name, false, false)
- end
- end
- function toggleHidden(name)
- if buttonTable[name]["hidden"] == false then
- buttonTable[name]["hidden"] = true
- drawButton(name, false, true)
- else
- buttonTable[name]["hidden"] = false
- drawButton(name, false, true)
- end
- end
- --User functions:
- function enableReactor(name)
- if rs.testBundledInput("bottom",colors.brown) == false then
- print("Toggling memory cell to on")
- rs.setBundledOutput("bottom",colors.lime)
- sleep(0.1)
- rs.setBundledOutput("bottom",0)
- end
- rs.setBundledOutput("bottom",colors.white)
- end
- function disableReactor(name)
- if rs.testBundledInput("bottom",colors.brown) == true then
- print("Toggling memory cell to off")
- rs.setBundledOutput("bottom",colors.lime)
- sleep(0.1)
- rs.setBundledOutput("bottom",0)
- end
- rs.setBundledOutput("bottom", 0)
- end
- function failsafeReset()
- rs.setBundledOutput("bottom",0)
- toggleHidden("reset")
- toggleHidden("main")
- end
- --User functions end.
- --Startup code:
- --newButton(str name, int xmin, int xmax, int ymin, int ymax, function or "none", function or "none",
- -- str offcolor, str oncolor, str offlabel, str onlabel, bool centered, bool hidden, bool overwrite)
- newButton("main", 2, 18, 2, 4, disableReactor, enableReactor, 16384, 8192, "Offline", "Online", true, false, true)
- newButton("reset", 2, 18, 2, 4, "none", failsafeReset, 8, 16384, "Reset", "Reset", true, true, false)
- newButton("hold", 2, 18, 6, 8, "none", "none", 16, 16, "Hold", "Hold", true, true, false)
- newButton("cool", 2, 18, 6, 8, "none", "none", 8, 16384, "Cooldown", "Cooldown", true, true, false)
- print("Reactor control online!")
- if rs.testBundledInput("bottom",colors.brown) == true then
- print("Memory cell states reactor should be online! Turning on reactor!")
- rs.setBundledOutput("bottom",colors.white)
- toggleButton("main")
- else
- print("Memory cell states reactor should be offline! Ensuring reactor is offline!")
- rs.setBundledOutput("bottom",0)
- end
- --Startup code end.
- while true do
- if failsafe ~= 0 then
- failsafe = failsafe - 1
- term.setCursorPos(1,17)
- term.clearLine()
- print(failsafe)
- os.startTimer(5)
- local event, p1, p2, p3, p4 = os.pullEvent()
- if event == "monitor_touch" then
- print("Touch event triggered")
- for name,value in pairs(buttonTable) do
- if buttonTable[name]["hidden"] == false then
- if buttonTable[name]["center"] == false then
- if p2 >= buttonTable[name]["xmin"] then
- if p2 <= buttonTable[name]["xmax"] then
- if p3 >= buttonTable[name]["ymin"] then
- if p3 <= buttonTable[name]["ymax"] then
- if buttonTable[name]["state"] == true then
- if buttonTable[name]["offfunct"] ~= "none" then
- buttonTable[name]["offfunct"](name)
- end
- elseif buttonTable[name]["state"] == false then
- if buttonTable[name]["onfunct"] ~= "none" then
- buttonTable[name]["onfunct"](name)
- end
- end
- toggleButton(name)
- end
- end
- end
- end
- else
- if p2 >= (buttonTable[name]["xmin"] + buttonTable[name]["monmargin"]) then
- if p2 <= (buttonTable[name]["xmax"] + buttonTable[name]["monmargin"]) then
- if p3 >= buttonTable[name]["ymin"] then
- if p3 <= buttonTable[name]["ymax"] then
- if buttonTable[name]["state"] == true then
- if buttonTable[name]["offfunct"] ~= "none" then
- buttonTable[name]["offfunct"](name)
- end
- elseif buttonTable[name]["state"] == false then
- if buttonTable[name]["onfunct"] ~= "none" then
- buttonTable[name]["onfunct"](name)
- end
- end
- toggleButton(name)
- end
- end
- end
- end
- end
- end
- end
- elseif event == "redstone" then
- if rs.testBundledInput("bottom", colors.blue) == true then
- print("Energy storage full! Halting reactor!")
- rs.setBundledOutput("bottom",0)
- toggleHidden("hold")
- while rs.testBundledInput("bottom", colors.blue) == true do
- sleep(5)
- end
- toggleHidden("hold")
- rs.setBundledOutput("bottom",colors.white)
- elseif rs.getInput("top") == true then
- if rs.testBundledInput("bottom", colors.brown) then
- print("Failsafe triggered! 5 Minute shutdown")
- disableReactor()
- toggleHidden("cool")
- term.scroll(1)
- for count = 1, 500 do
- term.setCursorPos(1,18)
- term.clearLine()
- print(500 - count)
- sleep(1)
- end
- print("Resuming reactor run!")
- enableReactor()
- toggleHidden("cool")
- end
- end
- elseif event == "lan_message" then
- if p4 == "reset" then
- failsafe = failsafeDefault
- end
- end
- else
- print("Failsafe triggered due to contact with reactor monitor lost!")
- print("Toggling memory cell to off")
- rs.setBundledOutput("bottom",colors.lime)
- sleep(0.1)
- print("Shutting down reactor!")
- rs.setBundledOutput("bottom",colors.yellow)
- error("Failsafe!")
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement