Untitled

 avatar
unknown
plain_text
10 months ago
5.3 kB
12
Indexable
-- ===================================================================
--[[ │ CONFIGURATION │ ]]
-- ===================================================================
local CONFIG = {
    monitor_side = "top",
    fuel_tank_name = "bottom",
    weapon_output_side = "right",
    update_rate = 0.5
}

-- ===================================================================
--[[ │ INITIALIZATION │ ]]
-- ===================================================================

-- Check for the ship API.
if ship == nil or type(ship) ~= "table" then
  error("FATAL: Global 'ship' API not found!", 0)
end

-- Set up the monitor completely.
local monitor = peripheral.wrap(CONFIG.monitor_side)
if not monitor then error("FATAL: Monitor not found on side: "..CONFIG.monitor_side, 0) end

-- CHANGE #1: Save the original terminal before redirecting.
local originalTerm = term.current() 
term.redirect(monitor)
monitor.setTextScale(0.5)

-- Load the Basalt library.
if not fs.exists("basalt.lua") then
  error("FATAL: Basalt GUI library not found.", 0)
end
local basalt = require("basalt")

-- Wrap optional peripherals.
local fuelTank = peripheral.wrap(CONFIG.fuel_tank_name)

-- ===================================================================
--[[ │ PROTECTED GUI SETUP │ ]]
-- ===================================================================
local guiSuccess, guiOrError = pcall(function()
  basalt.setTheme({ background = colors.black, foreground = colors.green })

  local screenW, screenH = monitor.getSize()
  local main = basalt.getMainFrame():setSize(screenW, screenH):setTitle("FIGHTER FLIGHT CONTROL"):show()

  local tabs = {"FLIGHT", "RADAR", "SYSTEMS"}
  local panes = {}
  local activeTab = 1

  for i, name in ipairs(tabs) do
    main:addButton():setText(name):setPosition(2 + (i-1)*17, 2):setSize(16, 1):onClick(function()
      activeTab = i; for j, p in ipairs(panes) do p:setVisible(j == i) end
    end)
    panes[i] = main:addFrame():setPosition(1, 4):setSize(screenW, screenH - 3):setVisible(i == 1)
  end

  -- FLIGHT INFO TAB
  local flightPane = panes[1]
  flightPane:addLabel():setText("TELEMETRY"):setPosition(2, 2)
  local speedLabel = flightPane:addLabel():setText("SPD: --- m/s"):setPosition(2, 4)
  local altLabel = flightPane:addLabel():setText("ALT: --- m"):setPosition(2, 5)
  local massLabel = flightPane:addLabel():setText("MASS: --- kg"):setPosition(2, 6)
  flightPane:addLabel():setText("ATTITUDE: (Quaternion)"):setPosition(2, 9)

  -- RADAR TAB
  local radarPane = panes[2]
  radarPane:addLabel():setText("RADAR SYSTEM OFFLINE"):setPosition(2, 2)

  -- SYSTEMS TAB
  local systemsPane = panes[3]
  systemsPane:addLabel():setText("WEAPON CONTROL"):setPosition(2, 2)
  systemsPane:addButton():setText("FIRE PRIMARY"):setPosition(2, 4):setSize(screenW - 2, 2):onClick(function() rs.pulse(CONFIG.weapon_output_side, colors.red) end)
  systemsPane:addButton():setText("FIRE SECONDARY"):setPosition(2, 7):setSize(screenW - 2, 2):onClick(function() rs.pulse(CONFIG.weapon_output_side, colors.orange) end)
  systemsPane:addLabel():setText("FUEL STATUS"):setPosition(2, 11)
  local fuelBar = systemsPane:addProgressBar():setPosition(2, 13):setSize(screenW - 2, 1):setValue(0)
  local fuelLabel = systemsPane:addLabel():setText("--- / --- mB"):setPosition(2, 12)

  -- UPDATE LOOP
  local function updateFlightInfo()
    local pos = ship.getWorldspacePosition()
    local vel = ship.getVelocity()
    local mass = ship.getMass()
    local speed = math.sqrt(vel.x^2 + vel.y^2 + vel.z^2)
    altLabel:setText(string.format("ALT: %.1f m", pos.y))
    speedLabel:setText(string.format("SPD: %.1f m/s", speed))
    massLabel:setText(string.format("MASS: %d kg", mass))
  end

  local function updateSystems()
    if not fuelTank then return end
    local ok, fluid = pcall(fuelTank.getFluid);
    if not ok or not fluid or fluid.amount == 0 then
      fuelLabel:setText("TANK EMPTY"); fuelBar:setValue(0); return
    end
    local capacity = fuelTank.getCapacity()
    if capacity and capacity > 0 then
      fuelBar:setValue((fluid.amount / capacity) * 100)
      fuelLabel:setText(string.format("%d / %d mB", fluid.amount, capacity))
    end
  end

  basalt.createTimer(CONFIG.update_rate, function()
    updateFlightInfo()
    if activeTab == 3 then updateSystems() end
  end)

  return true
end)

-- ===================================================================
--[[ │ FINAL EXECUTION │ ]]
-- ===================================================================
if guiSuccess then
  -- If pcall succeeded, start the GUI event loop.
  basalt.autoUpdate()
else
  -- If pcall failed, print a manual error report.
  -- CHANGE #2: Redirect back to the original terminal object we saved.
  term.redirect(originalTerm)
  monitor.clear()
  monitor.setCursorPos(1, 1)
  monitor.setTextScale(0.5)
  monitor.write("--- BASALT INITIALIZATION FAILED ---\n\n")
  monitor.write("This appears to be a bug in this version of the Basalt library.\n\n")
  monitor.write("Error Details:\n")
  monitor.write(tostring(guiOrError).."\n\n")
  local w, h = monitor.getSize()
  monitor.write("Detected Monitor Size: " .. w .. "x" .. h .. "\n")
end
Editor is loading...
Leave a Comment