MomoFloFlo

Untitled

Nov 7th, 2025
269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.27 KB | None | 0 0
  1.  
  2. -- mission_control_server.lua
  3. local component = require("component")
  4. local event = require("event")
  5. local sides = require("sides")
  6. local serialization = require("serialization")
  7.  
  8. -- Config
  9. local PORT = 1000
  10. local UPDATE_INTERVAL = 1
  11.  
  12. -- Components
  13. local modem = component.modem
  14.  
  15. -- State Management
  16. local state = {
  17.   fuelProduction = 0,
  18.   fuelStored = 0,
  19.   maxFuelCapacity = 0,
  20.   rocketDocked = false,
  21.   rocketFuel = 0,
  22.   refueling = false,
  23.   lastUpdate = 0
  24. }
  25.  
  26. -- Component Discovery
  27. local adapters = {}
  28. local fuelTanks = {}
  29.  
  30. local function discoverComponents()
  31.   print("=== Discovering Components ===")
  32.  
  33.   for address in component.list("adapter") do
  34.     table.insert(adapters, component.proxy(address))
  35.     print("Found Adapter: " .. address:sub(1, 8))
  36.   end
  37.  
  38.   print("Total Adapters: " .. #adapters)
  39.   print("")
  40.  
  41.   for i, adapter in ipairs(adapters) do
  42.     print("Analyzing Adapter " .. i .. ":")
  43.    
  44.     for side = 0, 5 do
  45.       local sideName = sides[side]
  46.      
  47.       local success, methods = pcall(function()
  48.         return adapter.methods(side)
  49.       end)
  50.      
  51.       if success and methods then
  52.         print("  Side " .. sideName .. ": " .. table.concat(methods, ", "))
  53.        
  54.         local hasGetFluid = false
  55.         local hasGetFuel = false
  56.        
  57.         for _, method in ipairs(methods) do
  58.           if method:lower():find("fluid") or method:lower():find("tank") then
  59.             hasGetFluid = true
  60.           end
  61.           if method:lower():find("fuel") then
  62.             hasGetFuel = true
  63.           end
  64.         end
  65.        
  66.         if hasGetFluid or hasGetFuel then
  67.           table.insert(fuelTanks, {adapter = adapter, side = side, methods = methods})
  68.           print("    -> Registered as Fuel Tank/Loader")
  69.         end
  70.       end
  71.     end
  72.     print("")
  73.   end
  74.  
  75.   print("Registered Fuel Components: " .. #fuelTanks)
  76.   print("===========================")
  77.   print("")
  78. end
  79.  
  80. local function updateFuelData()
  81.   state.fuelStored = 0
  82.   state.maxFuelCapacity = 0
  83.   state.fuelProduction = 0
  84.  
  85.   for i, component_info in ipairs(fuelTanks) do
  86.     local adapter = component_info.adapter
  87.     local side = component_info.side
  88.     local methods = component_info.methods
  89.    
  90.     local tryMethods = {
  91.       "getFluidAmount",
  92.       "getFluidInTank",
  93.       "getTankInfo",
  94.       "getStored",
  95.       "getFuelAmount",
  96.       "getFuel"
  97.     }
  98.    
  99.     for _, methodName in ipairs(tryMethods) do
  100.       local hasMethod = false
  101.       for _, m in ipairs(methods) do
  102.         if m == methodName then
  103.           hasMethod = true
  104.           break
  105.         end
  106.       end
  107.      
  108.       if hasMethod then
  109.         local success, result = pcall(function()
  110.           return adapter.invoke(methodName, side)
  111.         end)
  112.        
  113.         if success and result then
  114.           if type(result) == "table" then
  115.             if result.amount then
  116.               state.fuelStored = state.fuelStored + result.amount
  117.             end
  118.             if result.capacity then
  119.               state.maxFuelCapacity = state.maxFuelCapacity + result.capacity
  120.             end
  121.           elseif type(result) == "number" then
  122.             state.fuelStored = state.fuelStored + result
  123.           end
  124.         end
  125.       end
  126.     end
  127.   end
  128.  
  129.   state.lastUpdate = os.time()
  130. end
  131.  
  132. local function updateRocketStatus()
  133.   state.rocketDocked = false
  134.   state.rocketFuel = 0
  135.  
  136.   for i, component_info in ipairs(fuelTanks) do
  137.     local adapter = component_info.adapter
  138.     local side = component_info.side
  139.     local methods = component_info.methods
  140.    
  141.     for _, method in ipairs(methods) do
  142.       if method:lower():find("rocket") then
  143.         state.rocketDocked = true
  144.        
  145.         local success, result = pcall(function()
  146.           return adapter.invoke(method, side)
  147.         end)
  148.        
  149.         if success and type(result) == "number" then
  150.           state.rocketFuel = result
  151.         end
  152.        
  153.         break
  154.       end
  155.     end
  156.   end
  157. end
  158.  
  159. local function startRefueling()
  160.   print("[ACTION] Starting refueling sequence...")
  161.   state.refueling = true
  162.  
  163.   for i, component_info in ipairs(fuelTanks) do
  164.     local adapter = component_info.adapter
  165.     local side = component_info.side
  166.     local methods = component_info.methods
  167.    
  168.     for _, method in ipairs(methods) do
  169.       if method:lower():find("start") or method:lower():find("enable") or method == "setEnabled" then
  170.         pcall(function()
  171.           adapter.invoke(method, side, true)
  172.         end)
  173.       end
  174.     end
  175.   end
  176.  
  177.   print("[ACTION] Refueling started")
  178. end
  179.  
  180. local function stopRefueling()
  181.   print("[ACTION] Stopping refueling sequence...")
  182.   state.refueling = false
  183.  
  184.   for i, component_info in ipairs(fuelTanks) do
  185.     local adapter = component_info.adapter
  186.     local side = component_info.side
  187.     local methods = component_info.methods
  188.    
  189.     for _, method in ipairs(methods) do
  190.       if method:lower():find("stop") or method:lower():find("disable") or method == "setEnabled" then
  191.         pcall(function()
  192.           adapter.invoke(method, side, false)
  193.         end)
  194.       end
  195.     end
  196.   end
  197.  
  198.   print("[ACTION] Refueling stopped")
  199. end
  200.  
  201. local function handleMessage(sender, port, distance, command, data)
  202.   print("[REQUEST] " .. command .. " from " .. sender:sub(1, 8))
  203.  
  204.   if command == "GET_STATUS" then
  205.     local serialized = serialization.serialize(state)
  206.     modem.send(sender, PORT, "STATUS", serialized)
  207.    
  208.   elseif command == "START_REFUEL" then
  209.     startRefueling()
  210.     modem.send(sender, PORT, "ACK")
  211.    
  212.   elseif command == "STOP_REFUEL" then
  213.     stopRefueling()
  214.     modem.send(sender, PORT, "ACK")
  215.    
  216.   elseif command == "PING" then
  217.     modem.send(sender, PORT, "PONG")
  218.    
  219.   else
  220.     modem.send(sender, PORT, "ERROR")
  221.   end
  222. end
  223.  
  224. local function displayStatus()
  225.   print("╔═══════════════════════════════════════╗")
  226.   print("║   MISSION CONTROL SERVER STATUS       ║")
  227.   print("╚═══════════════════════════════════════╝")
  228.   print("")
  229.   print("Server Address: " .. modem.address)
  230.   print("Port: " .. PORT)
  231.   print("Components: " .. #fuelTanks .. " fuel systems")
  232.   print("")
  233.   print("Fuel Stored: " .. state.fuelStored .. " / " .. state.maxFuelCapacity .. " mB")
  234.   print("Production: " .. state.fuelProduction .. " mB/t")
  235.   print("Rocket: " .. (state.rocketDocked and "DOCKED" or "NOT DETECTED"))
  236.   if state.rocketDocked then
  237.     print("Rocket Fuel: " .. state.rocketFuel .. "%")
  238.   end
  239.   print("Refueling: " .. (state.refueling and "ACTIVE" or "IDLE"))
  240.   print("")
  241.   print("Press Ctrl+C to stop server")
  242.   print("───────────────────────────────────────")
  243. end
  244.  
  245. local function main()
  246.   print("=== MISSION CONTROL SERVER STARTING ===")
  247.   print("")
  248.  
  249.   modem.open(PORT)
  250.   print("✓ Modem listening on port " .. PORT)
  251.   print("✓ Server Address: " .. modem.address)
  252.   print("")
  253.  
  254.   discoverComponents()
  255.  
  256.   if #fuelTanks == 0 then
  257.     print("⚠ WARNING: No GalacticCraft components found!")
  258.     print("Make sure adapters are placed next to:")
  259.     print("  - Fuel Loaders")
  260.     print("  - Fuel Tanks")
  261.     print("  - Refineries")
  262.     print("")
  263.   end
  264.  
  265.   print("Server is running...")
  266.   print("")
  267.  
  268.   local lastStatusDisplay = 0
  269.   local lastDataUpdate = 0
  270.  
  271.   while true do
  272.     local currentTime = os.time()
  273.    
  274.     if currentTime - lastDataUpdate >= UPDATE_INTERVAL then
  275.       updateFuelData()
  276.       updateRocketStatus()
  277.       lastDataUpdate = currentTime
  278.     end
  279.    
  280.     if currentTime - lastStatusDisplay >= 5 then
  281.       displayStatus()
  282.       lastStatusDisplay = currentTime
  283.     end
  284.    
  285.     local _, _, sender, port, distance, command, data = event.pull(0.5, "modem_message")
  286.    
  287.     if command then
  288.       handleMessage(sender, port, distance, command, data)
  289.     end
  290.   end
  291. end
  292.  
  293. local function cleanup()
  294.   print("")
  295.   print("=== SERVER SHUTTING DOWN ===")
  296.   modem.close(PORT)
  297.   print("✓ Port closed")
  298.   print("Server stopped.")
  299. end
  300.  
  301. local success, err = xpcall(main, debug.traceback)
  302.  
  303. if not success then
  304.   print("")
  305.   print("ERROR: " .. tostring(err))
  306. end
  307.  
  308. cleanup()
  309.  
Advertisement
Add Comment
Please, Sign In to add comment