Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Space Engineers Valve Management Script
- // Manages O2 and H2 valves and displays status on LCD/Console
- string lcdName = "Console Valve Management"; // Change this to your screen name
- // Valve connector names - adjust these to match your setup
- string o2ValveConnector1 = "O2 Valve 1";
- string o2ValveConnector2 = "O2 Valve 2";
- string o2BaseValveConnector1 = "O2 Base Valve 1";
- string o2BaseValveConnector2 = "O2 Base Valve 2";
- string h2BaseValveConnector1 = "H2 Base Valve 1";
- string h2BaseValveConnector2 = "H2 Base Valve 2";
- // Tank and engine names
- string o2TankName = "Base O2 Tank";
- string h2TankName = "Base H2 Tank";
- string engineName = "Hydrogen Engine";
- const double BIT_SPACING = 255.0 / 7.0;
- public static char ColorToChar(byte r, byte g, byte b) {
- return (char)(0xe100 + ((int)Math.Round(r / BIT_SPACING) << 6) + ((int)Math.Round(g / BIT_SPACING) << 3) + (int)Math.Round(b / BIT_SPACING));
- }
- public Program()
- {
- Runtime.UpdateFrequency = UpdateFrequency.Update100; // Update every 100 ticks
- }
- public void Main(string argument, UpdateType updateSource)
- {
- // Get the display
- IMyTextSurfaceProvider console = GridTerminalSystem.GetBlockWithName(lcdName) as IMyTextSurfaceProvider;
- if (console == null)
- {
- Echo($"Display '{lcdName}' not found!");
- return;
- }
- IMyTextSurface surface = console.GetSurface(0);
- // Check valve statuses
- bool o2ValveConnected = IsValveConnected(o2ValveConnector1, o2ValveConnector2);
- bool o2BaseValveConnected = IsValveConnected(o2BaseValveConnector1, o2BaseValveConnector2);
- bool h2BaseValveConnected = IsValveConnected(h2BaseValveConnector1, h2BaseValveConnector2);
- // Generate display text
- string displayText = GenerateDisplay(o2ValveConnected, o2BaseValveConnected, h2BaseValveConnected);
- // Configure and display
- surface.ContentType = ContentType.TEXT_AND_IMAGE;
- surface.FontSize = 1.0f;
- surface.FontColor = Color.White;
- surface.BackgroundColor = Color.Black;
- surface.WriteText(displayText);
- // Debug info
- Echo($"O2 Valve: {(o2ValveConnected ? "Connected" : "Disconnected")}");
- Echo($"O2 Base Valve: {(o2BaseValveConnected ? "Connected" : "Disconnected")}");
- Echo($"H2 Base Valve: {(h2BaseValveConnected ? "Connected" : "Disconnected")}");
- }
- bool IsValveConnected(string connector1Name, string connector2Name)
- {
- IMyShipConnector conn1 = GridTerminalSystem.GetBlockWithName(connector1Name) as IMyShipConnector;
- IMyShipConnector conn2 = GridTerminalSystem.GetBlockWithName(connector2Name) as IMyShipConnector;
- if (conn1 == null || conn2 == null)
- {
- Echo($"Connector not found: {connector1Name} or {connector2Name}");
- return false;
- }
- // Both connectors need to be locked for the valve to be connected
- return conn1.Status == MyShipConnectorStatus.Connected &&
- conn2.Status == MyShipConnectorStatus.Connected;
- }
- string GenerateDisplay(bool o2Valve, bool o2BaseValve, bool h2BaseValve)
- {
- char greenColor = ColorToChar(0, 255, 0);
- char redColor = ColorToChar(255, 0, 0);
- char whiteColor = ColorToChar(255, 255, 255);
- // Get tank and engine status
- string o2TankStatus = GetTankStatus(o2TankName);
- string h2TankStatus = GetTankStatus(h2TankName);
- string engineStatus = GetEngineStatus();
- // Create the display with valve network and status below
- return $@"VALVE NETWORK
- =============
- Industry ──{(o2Valve ? greenColor : redColor)}●{whiteColor}── O2 Tank ──{(o2BaseValve ? greenColor : redColor)}●{whiteColor}──┐
- │ │
- │ │
- └──────{(h2BaseValve ? greenColor : redColor)}●{whiteColor}── H2 Tank ────────┘
- │
- ▼
- Base Network
- Legend: {greenColor}●{whiteColor} = Connected {redColor}●{whiteColor} = Disconnected
- O2: {o2TankStatus} | H2: {h2TankStatus}
- Engine: {engineStatus}";
- }
- string GetTankStatus(string tankName)
- {
- IMyGasTank tank = GridTerminalSystem.GetBlockWithName(tankName) as IMyGasTank;
- if (tank == null)
- {
- return $"{tankName} not found";
- }
- double current = tank.FilledRatio * tank.Capacity;
- double total = tank.Capacity;
- double percentage = tank.FilledRatio * 100;
- // Format numbers for display
- string currentStr = FormatVolume(current);
- string totalStr = FormatVolume(total);
- return $"{percentage:F1}%";
- }
- string GetEngineStatus()
- {
- // Look for the specific hydrogen engine
- IMyPowerProducer engine = GridTerminalSystem.GetBlockWithName(engineName) as IMyPowerProducer;
- if (engine == null)
- {
- return $"Engine '{engineName}' not found";
- }
- char greenColor = ColorToChar(0, 255, 0);
- char redColor = ColorToChar(255, 0, 0);
- char whiteColor = ColorToChar(255, 255, 255);
- if (engine.Enabled && engine.CurrentOutput > 0)
- {
- return $"{greenColor}ON{greenColor} ({engine.CurrentOutput:F1} MW)";
- }
- else if (engine.Enabled)
- {
- return $"{whiteColor}IDLE{whiteColor} (0 MW)";
- }
- else
- {
- return $"{redColor}OFF{redColor} (Disabled)";
- }
- }
- string FormatVolume(double volume)
- {
- if (volume >= 1000000)
- {
- return $"{volume / 1000000:F1}M L";
- }
- else if (volume >= 1000)
- {
- return $"{volume / 1000:F1}K L";
- }
- else
- {
- return $"{volume:F0} L";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement