Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- mission_control_server.lua
- local component = require("component")
- local event = require("event")
- local sides = require("sides")
- local serialization = require("serialization")
- -- Config
- local PORT = 1000
- local UPDATE_INTERVAL = 1
- -- Components
- local modem = component.modem
- -- State Management
- local state = {
- fuelProduction = 0,
- fuelStored = 0,
- maxFuelCapacity = 0,
- rocketDocked = false,
- rocketFuel = 0,
- refueling = false,
- lastUpdate = 0
- }
- -- Component Discovery
- local adapters = {}
- local fuelTanks = {}
- local function discoverComponents()
- print("=== Discovering Components ===")
- for address in component.list("adapter") do
- table.insert(adapters, component.proxy(address))
- print("Found Adapter: " .. address:sub(1, 8))
- end
- print("Total Adapters: " .. #adapters)
- print("")
- for i, adapter in ipairs(adapters) do
- print("Analyzing Adapter " .. i .. ":")
- for side = 0, 5 do
- local sideName = sides[side]
- local success, methods = pcall(function()
- return adapter.methods(side)
- end)
- if success and methods then
- print(" Side " .. sideName .. ": " .. table.concat(methods, ", "))
- local hasGetFluid = false
- local hasGetFuel = false
- for _, method in ipairs(methods) do
- if method:lower():find("fluid") or method:lower():find("tank") then
- hasGetFluid = true
- end
- if method:lower():find("fuel") then
- hasGetFuel = true
- end
- end
- if hasGetFluid or hasGetFuel then
- table.insert(fuelTanks, {adapter = adapter, side = side, methods = methods})
- print(" -> Registered as Fuel Tank/Loader")
- end
- end
- end
- print("")
- end
- print("Registered Fuel Components: " .. #fuelTanks)
- print("===========================")
- print("")
- end
- local function updateFuelData()
- state.fuelStored = 0
- state.maxFuelCapacity = 0
- state.fuelProduction = 0
- for i, component_info in ipairs(fuelTanks) do
- local adapter = component_info.adapter
- local side = component_info.side
- local methods = component_info.methods
- local tryMethods = {
- "getFluidAmount",
- "getFluidInTank",
- "getTankInfo",
- "getStored",
- "getFuelAmount",
- "getFuel"
- }
- for _, methodName in ipairs(tryMethods) do
- local hasMethod = false
- for _, m in ipairs(methods) do
- if m == methodName then
- hasMethod = true
- break
- end
- end
- if hasMethod then
- local success, result = pcall(function()
- return adapter.invoke(methodName, side)
- end)
- if success and result then
- if type(result) == "table" then
- if result.amount then
- state.fuelStored = state.fuelStored + result.amount
- end
- if result.capacity then
- state.maxFuelCapacity = state.maxFuelCapacity + result.capacity
- end
- elseif type(result) == "number" then
- state.fuelStored = state.fuelStored + result
- end
- end
- end
- end
- end
- state.lastUpdate = os.time()
- end
- local function updateRocketStatus()
- state.rocketDocked = false
- state.rocketFuel = 0
- for i, component_info in ipairs(fuelTanks) do
- local adapter = component_info.adapter
- local side = component_info.side
- local methods = component_info.methods
- for _, method in ipairs(methods) do
- if method:lower():find("rocket") then
- state.rocketDocked = true
- local success, result = pcall(function()
- return adapter.invoke(method, side)
- end)
- if success and type(result) == "number" then
- state.rocketFuel = result
- end
- break
- end
- end
- end
- end
- local function startRefueling()
- print("[ACTION] Starting refueling sequence...")
- state.refueling = true
- for i, component_info in ipairs(fuelTanks) do
- local adapter = component_info.adapter
- local side = component_info.side
- local methods = component_info.methods
- for _, method in ipairs(methods) do
- if method:lower():find("start") or method:lower():find("enable") or method == "setEnabled" then
- pcall(function()
- adapter.invoke(method, side, true)
- end)
- end
- end
- end
- print("[ACTION] Refueling started")
- end
- local function stopRefueling()
- print("[ACTION] Stopping refueling sequence...")
- state.refueling = false
- for i, component_info in ipairs(fuelTanks) do
- local adapter = component_info.adapter
- local side = component_info.side
- local methods = component_info.methods
- for _, method in ipairs(methods) do
- if method:lower():find("stop") or method:lower():find("disable") or method == "setEnabled" then
- pcall(function()
- adapter.invoke(method, side, false)
- end)
- end
- end
- end
- print("[ACTION] Refueling stopped")
- end
- local function handleMessage(sender, port, distance, command, data)
- print("[REQUEST] " .. command .. " from " .. sender:sub(1, 8))
- if command == "GET_STATUS" then
- local serialized = serialization.serialize(state)
- modem.send(sender, PORT, "STATUS", serialized)
- elseif command == "START_REFUEL" then
- startRefueling()
- modem.send(sender, PORT, "ACK")
- elseif command == "STOP_REFUEL" then
- stopRefueling()
- modem.send(sender, PORT, "ACK")
- elseif command == "PING" then
- modem.send(sender, PORT, "PONG")
- else
- modem.send(sender, PORT, "ERROR")
- end
- end
- local function displayStatus()
- print("╔═══════════════════════════════════════╗")
- print("║ MISSION CONTROL SERVER STATUS ║")
- print("╚═══════════════════════════════════════╝")
- print("")
- print("Server Address: " .. modem.address)
- print("Port: " .. PORT)
- print("Components: " .. #fuelTanks .. " fuel systems")
- print("")
- print("Fuel Stored: " .. state.fuelStored .. " / " .. state.maxFuelCapacity .. " mB")
- print("Production: " .. state.fuelProduction .. " mB/t")
- print("Rocket: " .. (state.rocketDocked and "DOCKED" or "NOT DETECTED"))
- if state.rocketDocked then
- print("Rocket Fuel: " .. state.rocketFuel .. "%")
- end
- print("Refueling: " .. (state.refueling and "ACTIVE" or "IDLE"))
- print("")
- print("Press Ctrl+C to stop server")
- print("───────────────────────────────────────")
- end
- local function main()
- print("=== MISSION CONTROL SERVER STARTING ===")
- print("")
- modem.open(PORT)
- print("✓ Modem listening on port " .. PORT)
- print("✓ Server Address: " .. modem.address)
- print("")
- discoverComponents()
- if #fuelTanks == 0 then
- print("⚠ WARNING: No GalacticCraft components found!")
- print("Make sure adapters are placed next to:")
- print(" - Fuel Loaders")
- print(" - Fuel Tanks")
- print(" - Refineries")
- print("")
- end
- print("Server is running...")
- print("")
- local lastStatusDisplay = 0
- local lastDataUpdate = 0
- while true do
- local currentTime = os.time()
- if currentTime - lastDataUpdate >= UPDATE_INTERVAL then
- updateFuelData()
- updateRocketStatus()
- lastDataUpdate = currentTime
- end
- if currentTime - lastStatusDisplay >= 5 then
- displayStatus()
- lastStatusDisplay = currentTime
- end
- local _, _, sender, port, distance, command, data = event.pull(0.5, "modem_message")
- if command then
- handleMessage(sender, port, distance, command, data)
- end
- end
- end
- local function cleanup()
- print("")
- print("=== SERVER SHUTTING DOWN ===")
- modem.close(PORT)
- print("✓ Port closed")
- print("Server stopped.")
- end
- local success, err = xpcall(main, debug.traceback)
- if not success then
- print("")
- print("ERROR: " .. tostring(err))
- end
- cleanup()
Advertisement
Add Comment
Please, Sign In to add comment