CCCPaste

reactor_gui

-- === Reactor Monitor & Control Script ===
-- CC:Tweaked - Extreme Reactors (ATM10)
-- Monitor GUI + Auto Control + Manual Button
-- Ethan Edition

local REACTOR_NAME = "BigReactors-Reactor_0"  -- Adjust if different
local MONITOR_NAME = "monitor_0"             -- Adjust if different

local autoControl = true
local reactor = peripheral.wrap(REACTOR_NAME)
local monitor = peripheral.wrap(MONITOR_NAME)

local function centerText(line, text)
  local w, _ = monitor.getSize()
  monitor.setCursorPos(math.floor((w - #text) / 2) + 1, line)
  monitor.write(text)
end

local function drawButton(label, active)
  local w, h = monitor.getSize()
  monitor.setCursorPos(2, h - 1)
  if active then
    monitor.setBackgroundColor(colors.red)
    monitor.setTextColor(colors.white)
  else
    monitor.setBackgroundColor(colors.lime)
    monitor.setTextColor(colors.black)
  end
  monitor.write(" [ " .. label .. " ] ")
  monitor.setBackgroundColor(colors.black)
end

local function drawGUI()
  monitor.clear()
  monitor.setTextColor(colors.cyan)

  centerText(1, "=== Reactor Control ===")

  monitor.setTextColor(colors.white)
  monitor.setCursorPos(1, 3)
  monitor.write("Status: ")
  monitor.setTextColor(reactor.getActive() and colors.lime or colors.red)
  monitor.write(reactor.getActive() and "ONLINE" or "OFFLINE")

  monitor.setTextColor(colors.white)
  monitor.setCursorPos(1, 4)
  monitor.write(string.format("RF/t:      %.0f", reactor.getEnergyProducedLastTick()))

  monitor.setCursorPos(1, 5)
  monitor.write(string.format("Fuel:      %.2f%%", reactor.getFuelAmount() / reactor.getFuelAmountMax() * 100))

  monitor.setCursorPos(1, 6)
  monitor.write(string.format("Temp:      %.1f C", reactor.getFuelTemperature()))

  monitor.setCursorPos(1, 7)
  monitor.write(string.format("Energy:    %.0f / %.0f RF",
    reactor.getEnergyStored(), reactor.getEnergyCapacity()))

  monitor.setCursorPos(1, 8)
  monitor.write("Mode: " .. (autoControl and "AUTO" or "MANUAL"))

  drawButton(reactor.getActive() and "TURN OFF" or "TURN ON", not autoControl)
end

local function toggleReactor()
  reactor.setActive(not reactor.getActive())
end

local function toggleControlMode()
  autoControl = not autoControl
end

local function handleTouch(x, y)
  local _, h = monitor.getSize()
  if y == h - 1 then
    if autoControl then
      toggleControlMode()
    else
      toggleReactor()
    end
  end
end

local function autoControlLogic()
  local stored = reactor.getEnergyStored()
  local max = reactor.getEnergyCapacity()

  if stored / max >= 0.95 then
    reactor.setActive(false)
  elseif stored / max <= 0.20 then
    reactor.setActive(true)
  end
end

-- === Startup ===
monitor.setTextScale(1)
monitor.setBackgroundColor(colors.black)
monitor.clear()
monitor.setCursorBlink(false)

-- === Event Loop ===
while true do
  drawGUI()

  if autoControl then
    autoControlLogic()
  end

  local event, side, x, y = os.pullEvent()

  if event == "monitor_touch" then
    handleTouch(x, y)
  end
end