robocyclone

Extreme Reactors Nuke Controller

Feb 5th, 2025 (edited)
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.50 KB | None | 0 0
  1. local MONITOR = peripheral.find("monitor")
  2. term.redirect(MONITOR)
  3. MONITOR.setTextScale(1)
  4. term.setCursorBlink(false)
  5. local WINDOW_WIDTH, WINDOW_HEIGHT = term.getSize()
  6. local BACKGROUND_COLOR = colors.gray
  7. local TEXT_COLOR = colors.cyan
  8. term.setBackgroundColor(BACKGROUND_COLOR)
  9. term.setTextColor(TEXT_COLOR)
  10. term.clear()
  11. local NUKE_INPUT_SIDE = "left"
  12. local NUKE_OUTPUT_SIDE = "right"
  13. local NUKE_STATE = false
  14. local NUKE_STATE_TRANS = {[true] = "RUNNING.", [false] = "OFF."}
  15. local NUKE_BATTERY_STATE = false --FALSE if battery NOT FULL. TRUE if battery FULL
  16. local NUKE_BATTERY_TRANS ={[false] = "FILLING.", [true] = "FULL."}
  17. local REDSTONE_OVERRIDE = false
  18. rs.setOutput(NUKE_OUTPUT_SIDE, NUKE_STATE)
  19. local BUTTONS = {} --[[Table containing all BUTTON objects. BUTTON objects are tables containing the bounds of a button, structured as so:
  20. {
  21. xPos (left border) 
  22. yPos (top border)
  23. xPosD (right border)
  24. yPosD (bottom border)
  25. func (button function that will fire when clicked or touched)
  26. }
  27. ]]--
  28.  
  29. function drawButton(xPos, yPos, text, f, textC, backC) --draws a button at xPos and yPos, that displays string text, with text color textC, and background color backC, and executes function f
  30.     term.setCursorPos(xPos, yPos)
  31.     term.setTextColor(textC)
  32.     term.setBackgroundColor(backC)
  33.     term.write(text)
  34.     term.setTextColor(TEXT_COLOR)
  35.     term.setBackgroundColor(BACKGROUND_COLOR)
  36.     local xPosD, yPosD = term.getCursorPos()
  37.     local buttonBounds = {xPos, yPos, xPosD, yPosD, f}
  38.     table.insert(BUTTONS, buttonBounds)
  39. end
  40.  
  41. function drawLabel(xPos, yPos, text, textC, backC) --draws a label at xPos and yPos, that displays string text, with text color textC, with background color backC
  42.     term.setCursorPos(xPos, yPos)
  43.     term.setTextColor(textC)
  44.     term.setBackgroundColor(backC)
  45.     term.write(text)
  46.     term.setTextColor(TEXT_COLOR)
  47.     term.setBackgroundColor(BACKGROUND_COLOR)
  48. end
  49.  
  50. function checkTouchPos(touchX, touchY) --checks if coordinates are within bounds of a button in table BUTTONS and runs function f if true. returns false if no button found
  51.     for _, v in pairs(BUTTONS) do
  52.         if touchX >= v[1] and touchX <= v[3] and touchY >= v[4] and touchY <= v[2] then
  53.             v[5]()
  54.         end
  55.     end
  56.     return false
  57. end
  58.  
  59. function setNukeState(newState) --Will set nuke to new state, returns true if swap was successful or false if swap was unnecessary
  60.     local isBool = {[true] = true, [false] = true}
  61.     if not isBool[newState] then
  62.         error("Input to setNukeState must be type BOOLEAN")
  63.     end
  64.     if NUKE_STATE == newState then
  65.         return false
  66.     else
  67.         NUKE_STATE = newState
  68.         rs.setOutput(NUKE_OUTPUT_SIDE, NUKE_STATE)
  69.     end
  70. end
  71.  
  72. function eNuke() --Defined to pass to drawButton and prevent multiple anonymous functions entering the heap
  73.     setNukeState(true)
  74. end
  75.  
  76. function dNuke() --See eNuke
  77.     setNukeState(false)
  78. end
  79.  
  80. function drawScreen() --Updates render screen
  81.     local RunningCoords = {x = 1, y = WINDOW_HEIGHT * .2}
  82.     local BatteryCoords = {x = 1, y = WINDOW_HEIGHT * .4}
  83.     local ToggleCoords = {x = 1, y = WINDOW_HEIGHT * .8}
  84.     local ManualCoords = {x = 1, y = WINDOW_HEIGHT * .6}
  85.     term.clear()
  86.     drawLabel(RunningCoords.x, RunningCoords.y, "Nuke is " .. NUKE_STATE_TRANS[NUKE_STATE], TEXT_COLOR, BACKGROUND_COLOR) --Draw nuke  operational status
  87.     drawLabel(BatteryCoords.x, BatteryCoords.y, "Nuke battery is " .. NUKE_BATTERY_TRANS[NUKE_BATTERY_STATE], TEXT_COLOR, BACKGROUND_COLOR) --Draw nuke battery status
  88.     drawLabel(ManualCoords.x, ManualCoords.y, "Redstone override is" .. tostring(REDSTONE_OVERRIDE), TEXT_COLOR, BACKGROUND_COLOR)
  89.     if not NUKE_STATE and not NUKE_BATTERY_STATE then --Draw nuke enable button if nuke disabled, and battery not full
  90.         drawButton(ToggleCoords.x, ToggleCoords.y, "DISABLE NUKE", eNuke, colors.red, colors.green)
  91.     elseif not NUKE_STATE and NUKE_BATTERY_STATE then --Draw battery full indicator
  92.         drawLabel(ToggleCoords.x, ToggleCoords.y, "BATTERY FULL; NUKE DISABLED", TEXT_COLOR, BACKGROUND_COLOR)
  93.     elseif NUKE_STATE then --Draw nuke disable button if nuke enabled
  94.         drawButton(ToggleCoords.x, ToggleCoords.y, "SCRAM", dNuke, colors.green, colors.red)
  95.     end
  96. end
  97.  
  98. while true do
  99.     drawScreen()
  100.     local eventData = {os.pullEvent()}
  101.     local event = eventData[1]
  102.     if event == "monitor_touch" then
  103.         touchX = eventData[3]
  104.         touchY = eventData[4]
  105.         checkTouchPos(touchX, touchY)
  106.         REDSTONE_OVERRIDE = not REDSTONE_OVERRIDE
  107.     elseif event == "redstone" then
  108.         NUKE_BATTERY_STATE = rs.getInput(NUKE_INPUT_SIDE)
  109.         if not NUKE_BATTERY_STATE and not REDSTONE_OVERRIDE then
  110.             setNukeState(not NUKE_BATTERY_STATE)
  111.         end
  112.     end
  113. end
Tags: ATM10
Advertisement
Add Comment
Please, Sign In to add comment