-- FF_STORY_COMPUTER_GUARD
local function ff_story_computer_allowed()
  local label = os.getComputerLabel and os.getComputerLabel() or nil
  return fs.exists(".ff_story_computer")
    or fs.exists(".ff_titan_computer")
    or fs.exists(".ff_titan_program_id")
    or fs.exists(".ff_titan_allow_autoinstall")
    or fs.exists(".ff_titan_allow_autorun")
    or (label ~= nil and string.sub(string.lower(label), 1, 9) == "ff_titan:")
end

if not ff_story_computer_allowed() then
  return
end
-- TITAN DESCENT CONTROL
-- Surface elevator activation puzzle
-- ComputerCraft / CC:Tweaked
--
-- Save as: startup
--
-- Import:
-- pastebin get PASTE_CODE startup
-- reboot

local OUTPUT_SIDE = "back"
local STATE_FILE = ".titan_descent_online"

local answers = {
  motor = "gearbox",
  brake = "wet",
  route = "green",
  albedo = "awake"
}

local function c(col)
  if term.isColor and term.isColor() then
    term.setTextColor(col)
  end
end

local function cls()
  term.setBackgroundColor(colors.black)
  term.clear()
  term.setCursorPos(1, 1)
end

local function printC(text, col)
  c(col or colors.white)
  print(text)
end

local function header(title)
  cls()
  c(colors.lightBlue)
  print("[ TITAN DESCENT CONTROL ]")
  c(colors.gray)
  print("SURFACE ELEVATOR NODE")
  print("---------------------------------------")
  if title then
    c(colors.white)
    print(title)
    c(colors.gray)
    print("---------------------------------------")
  end
  c(colors.white)
end

local function pause()
  c(colors.gray)
  print("")
  print("[key] back")
  os.pullEvent("key")
end

local function ask(label)
  c(colors.gray)
  print("")
  write(label .. " > ")
  c(colors.white)
  return string.lower(read() or "")
end

local function isOnline()
  return fs.exists(STATE_FILE)
end

local function saveOnline()
  local f = fs.open(STATE_FILE, "w")
  f.write("ONLINE")
  f.close()
end

local function setOut(on)
  redstone.setOutput(OUTPUT_SIDE, on)
end

local function boot()
  setOut(isOnline())
end

local function status()
  header("STATUS")

  if isOnline() then
    printC("DESCENT DRIVE: ONLINE", colors.lime)
    print("")
    print("Output is active.")
    print("")
    print("If wired correctly, the gearbox")
    print("at the drive socket is now placed")
    print("or pushed into position.")
    print("")
    printC("OUTPUT SIDE:", colors.gray)
    printC(OUTPUT_SIDE, colors.cyan)
  else
    printC("DESCENT DRIVE: OFFLINE", colors.orange)
    print("")
    print("Motor line has no coupling.")
    print("Surface brake is still locked.")
    print("")
    print("Run REAUTHORIZE when ready.")
  end

  pause()
end

local function motorFile()
  header("FILE: MOTOR LINE")

  print("The elevator pulley responds.")
  print("The motor spins into dead air.")
  print("")
  print("Socket at drive junction is empty.")
  print("")
  print("Maintenance left a label on it:")
  print("")
  printC("TOKEN:", colors.gray)
  printC("GEARBOX", colors.lime)
  print("")
  print("Someone circled the word twice.")
  print("Albedo added a third circle.")
  print("")
  printC("ALBEDO:", colors.cyan)
  print("\"Subtlety was attempted.\"")

  pause()
end

local function brakeFile()
  header("FILE: INCIDENT G-12")

  print("Cage returned at 02:17.")
  print("")
  print("Floor was dry.")
  print("Ceiling was not.")
  print("")
  print("No passenger logged.")
  print("No call signal found.")
  print("")
  print("Brake phrase changed after review.")
  print("")
  printC("TOKEN:", colors.gray)
  printC("WET", colors.lime)
  print("")
  print("The old phrase was deleted.")
  print("Good.")

  pause()
end

local function routeFile()
  header("FILE: ROUTE TABLE")

  print("Surface terminal may authorize")
  print("one destination.")
  print("")
  print("Lower route is not available here.")
  print("")
  print("The allowed route is printed")
  print("on the old destination card.")
  print("")
  printC("TOKEN:", colors.gray)
  printC("GREEN", colors.lime)
  print("")
  printC("ALBEDO:", colors.cyan)
  print("\"You get the polite floor first.\"")

  pause()
end

local function albedoFile()
  header("FILE: LOCAL OVERRIDE")

  print("Old guesses failed.")
  print("")
  print("TITAN failed.")
  print("NEMESIS failed.")
  print("CROWNWATCH failed.")
  print("")
  print("One staff note remained:")
  print("")
  print("she is not the tower")
  print("she is not the platform")
  print("she is still")
  print("")
  printC("TOKEN:", colors.gray)
  printC("AWAKE", colors.lime)

  pause()
end

local function reauthorize()
  header("REAUTHORIZE")

  if isOnline() then
    printC("Already online.", colors.lime)
    pause()
    return
  end

  print("Four tokens required.")
  print("")
  print("Use local files.")
  print("Each answer is one word.")
  print("")
  print("No punctuation.")
  print("No staff jokes.")
  print("")
  printC("ALBEDO:", colors.cyan)
  print("\"That second rule is for me.\"")

  c(colors.gray)
  print("")
  print("[key] begin")
  os.pullEvent("key")

  header("CHECK 1")
  print("Missing drive component.")
  local a1 = ask("TOKEN")
  if a1 ~= answers.motor then
    printC("Rejected.", colors.red)
    sleep(1)
    return
  end

  header("CHECK 2")
  print("Incident G-12 brake phrase.")
  local a2 = ask("TOKEN")
  if a2 ~= answers.brake then
    printC("Rejected.", colors.red)
    sleep(1)
    return
  end

  header("CHECK 3")
  print("Allowed surface destination.")
  local a3 = ask("TOKEN")
  if a3 ~= answers.route then
    printC("Rejected.", colors.red)
    sleep(1)
    return
  end

  header("CHECK 4")
  print("Local override state.")
  local a4 = ask("TOKEN")
  if a4 ~= answers.albedo then
    printC("Rejected.", colors.red)
    sleep(1)
    return
  end

  header("COUPLING")

  printC("MOTOR LINE ACCEPTED", colors.lime)
  printC("BRAKE RELEASE ACCEPTED", colors.lime)
  printC("GREEN ROUTE ACCEPTED", colors.lime)
  printC("LOCAL OVERRIDE ACCEPTED", colors.lime)
  print("")
  print("Signal held on:")
  printC(OUTPUT_SIDE, colors.cyan)
  print("")
  printC("ALBEDO:", colors.cyan)
  print("\"There. The elevator may now")
  print("make its terrible little trip.\"")

  saveOnline()
  setOut(true)

  pause()
end

local function debugMenu()
  header("DEBUG")

  print("1. force output on")
  print("2. force output off")
  print("3. clear saved state")
  print("")
  print("Q. back")

  local input = ask("")

  if input == "1" then
    setOut(true)
  elseif input == "2" then
    setOut(false)
  elseif input == "3" then
    if fs.exists(STATE_FILE) then
      fs.delete(STATE_FILE)
    end
    setOut(false)
  end
end

boot()

while true do
  header()

  if isOnline() then
    printC("DESCENT DRIVE: ONLINE", colors.lime)
  else
    printC("DESCENT DRIVE: OFFLINE", colors.orange)
  end

  print("")
  print("1. Status")
  print("2. Motor line file")
  print("3. Incident G-12 file")
  print("4. Route table file")
  print("5. Override file")
  print("6. Reauthori
::contentReference[oaicite:2]{index=2}
ze")
  print("7. Debug")
  print("")
  print("Q. close")

  local input = ask("")

  if input == "q" or input == "quit" or input == "exit" then
    cls()
    printC("SESSION CLOSED", colors.gray)
    return
  elseif input == "1" then
    status()
  elseif input == "2" then
    motorFile()
  elseif input == "3" then
    brakeFile()
  elseif input == "4" then
    routeFile()
  elseif input == "5" then
    albedoFile()
  elseif input == "6" then
    reauthorize()
  elseif input == "7" then
    debugMenu()
  else
    printC("Invalid.", colors.red)
    sleep(0.6)
  end
end