Advertisement
PeterMaltzoff

SpaceEngineers Valve console

Jul 19th, 2025
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.61 KB | Gaming | 0 0
  1. // Space Engineers Valve Management Script
  2. // Manages O2 and H2 valves and displays status on LCD/Console
  3.  
  4. string lcdName = "Console Valve Management"; // Change this to your screen name
  5.  
  6. // Valve connector names - adjust these to match your setup
  7. string o2ValveConnector1 = "O2 Valve 1";
  8. string o2ValveConnector2 = "O2 Valve 2";
  9. string o2BaseValveConnector1 = "O2 Base Valve 1";
  10. string o2BaseValveConnector2 = "O2 Base Valve 2";
  11. string h2BaseValveConnector1 = "H2 Base Valve 1";
  12. string h2BaseValveConnector2 = "H2 Base Valve 2";
  13.  
  14. // Tank and engine names
  15. string o2TankName = "Base O2 Tank";
  16. string h2TankName = "Base H2 Tank";
  17. string engineName = "Hydrogen Engine";
  18.  
  19. const double BIT_SPACING = 255.0 / 7.0;
  20.  
  21. public static char ColorToChar(byte r, byte g, byte b) {
  22.     return (char)(0xe100 + ((int)Math.Round(r / BIT_SPACING) << 6) + ((int)Math.Round(g / BIT_SPACING) << 3) + (int)Math.Round(b / BIT_SPACING));
  23. }
  24.  
  25. public Program()
  26. {
  27.     Runtime.UpdateFrequency = UpdateFrequency.Update100; // Update every 100 ticks
  28. }
  29.  
  30. public void Main(string argument, UpdateType updateSource)
  31. {
  32.     // Get the display
  33.     IMyTextSurfaceProvider console = GridTerminalSystem.GetBlockWithName(lcdName) as IMyTextSurfaceProvider;
  34.     if (console == null)
  35.     {
  36.         Echo($"Display '{lcdName}' not found!");
  37.         return;
  38.     }
  39.    
  40.     IMyTextSurface surface = console.GetSurface(0);
  41.    
  42.     // Check valve statuses
  43.     bool o2ValveConnected = IsValveConnected(o2ValveConnector1, o2ValveConnector2);
  44.     bool o2BaseValveConnected = IsValveConnected(o2BaseValveConnector1, o2BaseValveConnector2);
  45.     bool h2BaseValveConnected = IsValveConnected(h2BaseValveConnector1, h2BaseValveConnector2);
  46.    
  47.     // Generate display text
  48.     string displayText = GenerateDisplay(o2ValveConnected, o2BaseValveConnected, h2BaseValveConnected);
  49.    
  50.     // Configure and display
  51.     surface.ContentType = ContentType.TEXT_AND_IMAGE;
  52.     surface.FontSize = 1.0f;
  53.     surface.FontColor = Color.White;
  54.     surface.BackgroundColor = Color.Black;
  55.     surface.WriteText(displayText);
  56.    
  57.     // Debug info
  58.     Echo($"O2 Valve: {(o2ValveConnected ? "Connected" : "Disconnected")}");
  59.     Echo($"O2 Base Valve: {(o2BaseValveConnected ? "Connected" : "Disconnected")}");
  60.     Echo($"H2 Base Valve: {(h2BaseValveConnected ? "Connected" : "Disconnected")}");
  61. }
  62.  
  63. bool IsValveConnected(string connector1Name, string connector2Name)
  64. {
  65.     IMyShipConnector conn1 = GridTerminalSystem.GetBlockWithName(connector1Name) as IMyShipConnector;
  66.     IMyShipConnector conn2 = GridTerminalSystem.GetBlockWithName(connector2Name) as IMyShipConnector;
  67.    
  68.     if (conn1 == null || conn2 == null)
  69.     {
  70.         Echo($"Connector not found: {connector1Name} or {connector2Name}");
  71.         return false;
  72.     }
  73.    
  74.     // Both connectors need to be locked for the valve to be connected
  75.     return conn1.Status == MyShipConnectorStatus.Connected &&
  76.            conn2.Status == MyShipConnectorStatus.Connected;
  77. }
  78.  
  79. string GenerateDisplay(bool o2Valve, bool o2BaseValve, bool h2BaseValve)
  80. {
  81.     char greenColor = ColorToChar(0, 255, 0);
  82.     char redColor = ColorToChar(255, 0, 0);
  83.     char whiteColor = ColorToChar(255, 255, 255);
  84.    
  85.     // Get tank and engine status
  86.     string o2TankStatus = GetTankStatus(o2TankName);
  87.     string h2TankStatus = GetTankStatus(h2TankName);
  88.     string engineStatus = GetEngineStatus();
  89.    
  90.     // Create the display with valve network and status below
  91.     return $@"VALVE NETWORK
  92. =============
  93. Industry ──{(o2Valve ? greenColor : redColor)}●{whiteColor}── O2 Tank ──{(o2BaseValve ? greenColor : redColor)}●{whiteColor}──┐
  94.    │                             │
  95.    │                             │
  96.    └──────{(h2BaseValve ? greenColor : redColor)}●{whiteColor}── H2 Tank ────────┘
  97.                            │
  98.                            ▼
  99.                       Base Network
  100.  
  101. Legend: {greenColor}●{whiteColor} = Connected    {redColor}●{whiteColor} = Disconnected
  102.  
  103. O2: {o2TankStatus} | H2: {h2TankStatus}
  104. Engine: {engineStatus}";
  105. }
  106.  
  107. string GetTankStatus(string tankName)
  108. {
  109.     IMyGasTank tank = GridTerminalSystem.GetBlockWithName(tankName) as IMyGasTank;
  110.     if (tank == null)
  111.     {
  112.         return $"{tankName} not found";
  113.     }
  114.    
  115.     double current = tank.FilledRatio * tank.Capacity;
  116.     double total = tank.Capacity;
  117.     double percentage = tank.FilledRatio * 100;
  118.    
  119.     // Format numbers for display
  120.     string currentStr = FormatVolume(current);
  121.     string totalStr = FormatVolume(total);
  122.    
  123.     return $"{percentage:F1}%";
  124. }
  125.  
  126. string GetEngineStatus()
  127. {
  128.     // Look for the specific hydrogen engine
  129.     IMyPowerProducer engine = GridTerminalSystem.GetBlockWithName(engineName) as IMyPowerProducer;
  130.    
  131.     if (engine == null)
  132.     {
  133.         return $"Engine '{engineName}' not found";
  134.     }
  135.    
  136.     char greenColor = ColorToChar(0, 255, 0);
  137.     char redColor = ColorToChar(255, 0, 0);
  138.     char whiteColor = ColorToChar(255, 255, 255);
  139.    
  140.     if (engine.Enabled && engine.CurrentOutput > 0)
  141.     {
  142.         return $"{greenColor}ON{greenColor} ({engine.CurrentOutput:F1} MW)";
  143.     }
  144.     else if (engine.Enabled)
  145.     {
  146.         return $"{whiteColor}IDLE{whiteColor} (0 MW)";
  147.     }
  148.     else
  149.     {
  150.         return $"{redColor}OFF{redColor} (Disabled)";
  151.     }
  152. }
  153.  
  154. string FormatVolume(double volume)
  155. {
  156.     if (volume >= 1000000)
  157.     {
  158.         return $"{volume / 1000000:F1}M L";
  159.     }
  160.     else if (volume >= 1000)
  161.     {
  162.         return $"{volume / 1000:F1}K L";
  163.     }
  164.     else
  165.     {
  166.         return $"{volume:F0} L";
  167.     }
  168. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement