CCCPaste

NC reactor advanced

-- === Reactor Control Program ===
-- ComputerCraft: Tweaked
-- Touchscreen GUI with auto-scaling buttons

-- === CONFIGURATION ===
local reactorPowerSide = "back"   -- redstone side for reactor on/off
local sequenceSide1 = "left"      -- first side of open/close sequence
local sequenceSide2 = "right"     -- second side of open/close sequence
local sequenceDelay = 2           -- seconds between signals (adjust as needed)

-- === STATE ===
local reactorOn = false
local reactorOpen = false

-- === SETUP ===
local mon = peripheral.find("monitor")
if not mon then
  print("No monitor found!")
  return
end

mon.setTextScale(1)
mon.setBackgroundColor(colors.black)
mon.setTextColor(colors.white)
mon.clear()

local monW, monH = mon.getSize()

-- === FUNCTIONS ===
local function drawButton(x, y, w, h, label, active)
  mon.setBackgroundColor(active and colors.green or colors.red)
  for i = y, y + h - 1 do
    mon.setCursorPos(x, i)
    mon.write(string.rep(" ", w))
  end
  mon.setTextColor(colors.white)
  mon.setCursorPos(x + math.floor((w - #label) / 2), y + math.floor(h / 2))
  mon.write(label)
  mon.setBackgroundColor(colors.black)
end

local function drawUI()
  mon.clear()
  local buttonW = math.floor(monW * 0.6)
  local buttonH = math.floor(monH * 0.3)
  local startX = math.floor((monW - buttonW) / 2)
  
  -- Top button (reactor)
  drawButton(startX, 2, buttonW, buttonH, "Reactor: " .. (reactorOn and "ON" or "OFF"), reactorOn)
  
  -- Bottom button (open/close)
  local bottomY = monH - buttonH - 1
  drawButton(startX, bottomY, buttonW, buttonH, reactorOpen and "Close Reactor" or "Open Reactor", reactorOpen)
end

local function toggleReactor()
  reactorOn = not reactorOn
  redstone.setOutput(reactorPowerSide, reactorOn)
end

local function toggleReactorOpen()
  if reactorOpen then
    -- Close sequence (reverse)
    redstone.setOutput(sequenceSide2, true)
    sleep(sequenceDelay)
    redstone.setOutput(sequenceSide2, false)
    redstone.setOutput(sequenceSide1, true)
    sleep(sequenceDelay)
    redstone.setOutput(sequenceSide1, false)
  else
    -- Open sequence (forward)
    redstone.setOutput(sequenceSide1, true)
    sleep(sequenceDelay)
    redstone.setOutput(sequenceSide1, false)
    redstone.setOutput(sequenceSide2, true)
    sleep(sequenceDelay)
    redstone.setOutput(sequenceSide2, false)
  end
  reactorOpen = not reactorOpen
end

-- === MAIN LOOP ===
drawUI()

while true do
  local event, side, x, y = os.pullEvent("monitor_touch")

  local buttonW = math.floor(monW * 0.6)
  local buttonH = math.floor(monH * 0.3)
  local startX = math.floor((monW - buttonW) / 2)
  local bottomY = monH - buttonH - 1

  -- Reactor toggle area
  if x >= startX and x <= startX + buttonW - 1 and y >= 2 and y <= 2 + buttonH - 1 then
    toggleReactor()
    drawUI()
  end

  -- Open/Close area
  if x >= startX and x <= startX + buttonW - 1 and y >= bottomY and y <= bottomY + buttonH - 1 then
    toggleReactorOpen()
    drawUI()
  end
end