Major refactor and cleanup as we look towards reuse.
This commit is contained in:
@ -21,11 +21,11 @@ namespace IngameScript
|
||||
|
||||
private const float TriggerLevel = 0.75F;
|
||||
|
||||
public AirZone(string zoneName, MyIni ini, IConsole console)
|
||||
public AirZone(string zoneName, IConsoleProgram program)
|
||||
{
|
||||
Name = zoneName;
|
||||
_ini = ini;
|
||||
_console = new PrefixedConsole(console, zoneName);
|
||||
_ini = program.Ini;
|
||||
_console = new PrefixedConsole(program.Console, zoneName);
|
||||
_sequencer = new Sequencer(zoneName, _console);
|
||||
Vents = new List<IMyAirVent>();
|
||||
_doors = new List<IMyDoor>();
|
||||
|
@ -1,32 +1,18 @@
|
||||
using Sandbox.Game.EntityComponents;
|
||||
using Sandbox.ModAPI.Ingame;
|
||||
using Sandbox.ModAPI.Interfaces;
|
||||
using Sandbox.ModAPI.Ingame;
|
||||
using SpaceEngineers.Game.ModAPI.Ingame;
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Immutable;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using VRage;
|
||||
using VRage.Collections;
|
||||
using VRage.Game;
|
||||
using VRage.Game.Components;
|
||||
using VRage.Game.GUI.TextPanel;
|
||||
using VRage.Game.ModAPI.Ingame;
|
||||
using VRage.Game.ModAPI.Ingame.Utilities;
|
||||
using VRage.Game.ObjectBuilders.Definitions;
|
||||
using VRageMath;
|
||||
|
||||
namespace IngameScript
|
||||
{
|
||||
public partial class Program : MyGridProgram
|
||||
public partial class Program : MyGridProgram, IConsoleProgram
|
||||
{
|
||||
private MyIni _ini;
|
||||
private Console _console;
|
||||
private MyCommandLine _cli;
|
||||
private int _totalTicks = 0;
|
||||
public MyIni Ini { get; private set; }
|
||||
public IConsole Console { get; private set; }
|
||||
|
||||
private MyCommandLine _cli;
|
||||
private Dictionary<string, AirZone> _zones;
|
||||
private List<IEnumerator<bool>> _jobs;
|
||||
private List<IMyTextSurface> _displays;
|
||||
@ -35,9 +21,10 @@ namespace IngameScript
|
||||
|
||||
public Program()
|
||||
{
|
||||
Ini = new MyIni();
|
||||
Console = new MainConsole(this, "Air Pressure Monitor");
|
||||
|
||||
_cli = new MyCommandLine();
|
||||
_ini = new MyIni();
|
||||
_console = new Console(this, _ini);
|
||||
_zones = new Dictionary<string, AirZone>();
|
||||
_jobs = new List<IEnumerator<bool>>();
|
||||
_displays = new List<IMyTextSurface>();
|
||||
@ -49,7 +36,7 @@ namespace IngameScript
|
||||
GridTerminalSystem.GetBlocksOfType(blocks, block => MyIni.HasSection(block.CustomData, "airMonitor"));
|
||||
foreach (IMyTerminalBlock block in blocks)
|
||||
{
|
||||
_ini.TryParse(block.CustomData);
|
||||
Ini.TryParse(block.CustomData);
|
||||
string[] zones = new string[] { };
|
||||
|
||||
// TODO: how do we display text on e.g. decorative console blocks?
|
||||
@ -58,16 +45,16 @@ namespace IngameScript
|
||||
// It'd probably be safe to just check SurfaceCount... experiment with this later
|
||||
if (block is IMyTextSurface)
|
||||
{
|
||||
_console.Print($"Adding monitoring display '{block.CustomName}'");
|
||||
Console.Print($"Adding monitoring display '{block.CustomName}'");
|
||||
_displays.Add(block as IMyTextSurface);
|
||||
}
|
||||
if (_ini.Get("airMonitor", "display").ToString() != "")
|
||||
if (Ini.Get("airMonitor", "display").ToString() != "")
|
||||
{
|
||||
int displayIndex = _ini.Get("airMonitor", "display").ToInt32();
|
||||
int displayIndex = Ini.Get("airMonitor", "display").ToInt32();
|
||||
IMyTextSurfaceProvider provider = block as IMyTextSurfaceProvider;
|
||||
if (provider.SurfaceCount <= displayIndex)
|
||||
{
|
||||
_console.Print($"Invalid display index '{displayIndex}' in block '{block.CustomName}'");
|
||||
Console.Print($"Invalid display index '{displayIndex}' in block '{block.CustomName}'");
|
||||
}
|
||||
else
|
||||
{
|
||||
@ -79,30 +66,30 @@ namespace IngameScript
|
||||
_oxygenTanks.Add(block as IMyGasTank);
|
||||
}
|
||||
|
||||
if (_ini.Get("airMonitor", "zones").ToString() != "")
|
||||
if (Ini.Get("airMonitor", "zones").ToString() != "")
|
||||
{
|
||||
zones = _ini.Get("airMonitor", "zones").ToString().Split(',');
|
||||
zones = Ini.Get("airMonitor", "zones").ToString().Split(',');
|
||||
}
|
||||
else if (_ini.Get("airMonitor", "zone").ToString() != "")
|
||||
else if (Ini.Get("airMonitor", "zone").ToString() != "")
|
||||
{
|
||||
zones = new string[] { _ini.Get("airMonitor", "zone").ToString() };
|
||||
zones = new string[] { Ini.Get("airMonitor", "zone").ToString() };
|
||||
}
|
||||
|
||||
foreach (string zone in zones)
|
||||
{
|
||||
if (!_zones.ContainsKey(zone))
|
||||
{
|
||||
_zones[zone] = new AirZone(zone, _ini, _console);
|
||||
_zones[zone] = new AirZone(zone, this);
|
||||
}
|
||||
_zones[zone].AddBlock(block);
|
||||
}
|
||||
}
|
||||
|
||||
_console.Print($"Found {_zones.Count} zones:");
|
||||
Console.Print($"Found {_zones.Count} zones:");
|
||||
foreach (KeyValuePair<string, AirZone> kvp in _zones)
|
||||
{
|
||||
AirZone zone = kvp.Value;
|
||||
_console.Print(kvp.Key);
|
||||
Console.Print(kvp.Key);
|
||||
_jobs.Add(zone.Monitor());
|
||||
}
|
||||
Runtime.UpdateFrequency |= UpdateFrequency.Update100;
|
||||
@ -110,7 +97,8 @@ namespace IngameScript
|
||||
|
||||
public void Main(string argument, UpdateType updateSource)
|
||||
{
|
||||
_console.PrintLower($"Air Monitor\nTotal Ticks: {++_totalTicks}");
|
||||
Console.UpdateTickCount();
|
||||
|
||||
if (argument != "")
|
||||
{
|
||||
_cli.TryParse(argument);
|
||||
@ -119,7 +107,7 @@ namespace IngameScript
|
||||
for (int i = 0; i < _cli.ArgumentCount; i++)
|
||||
{
|
||||
string zone = _cli.Argument(i);
|
||||
_console.Print($"Resetting {zone}.");
|
||||
Console.Print($"Resetting {zone}.");
|
||||
if (_zones.ContainsKey(zone))
|
||||
{
|
||||
_zones[zone].Reset();
|
||||
@ -128,41 +116,43 @@ namespace IngameScript
|
||||
}
|
||||
}
|
||||
|
||||
// write diagnostics
|
||||
if (_displays.Count != 0)
|
||||
{
|
||||
_displayBuffer.Clear();
|
||||
_displayBuffer.Append("AIR PRESSURE REPORT\n\n");
|
||||
foreach (AirZone zone in _zones.Values)
|
||||
{
|
||||
_displayBuffer.Append(zone.Name);
|
||||
_displayBuffer.Append(": ");
|
||||
_displayBuffer.Append(zone.Triggered ? "ALARM TRIPPED " : "NOMINAL ");
|
||||
foreach (IMyAirVent vent in zone.Vents)
|
||||
{
|
||||
_displayBuffer.Append((int)(vent.GetOxygenLevel() * 100F));
|
||||
_displayBuffer.Append("% ");
|
||||
}
|
||||
|
||||
_displayBuffer.Append("\n");
|
||||
}
|
||||
|
||||
_displayBuffer.Append("\n");
|
||||
_displayBuffer.Append("OXYGEN TANK LEVELS\n");
|
||||
foreach (IMyGasTank tank in _oxygenTanks)
|
||||
{
|
||||
_displayBuffer.Append((int)(tank.FilledRatio * 100));
|
||||
_displayBuffer.Append("% ");
|
||||
}
|
||||
|
||||
foreach (IMyTextSurface display in _displays) display.WriteText(_displayBuffer.ToString());
|
||||
}
|
||||
|
||||
foreach (IEnumerator job in _jobs)
|
||||
{
|
||||
if (job.MoveNext()) continue;
|
||||
_console.Print("WARNING: Monitoring job exited. Zone no longer being monitored.");
|
||||
Console.Print("WARNING: Monitoring job exited. Zone no longer being monitored.");
|
||||
}
|
||||
}
|
||||
|
||||
// write diagnostics to any configured display screens
|
||||
private void _updateDisplays()
|
||||
{
|
||||
if (_displays.Count == 0) return;
|
||||
|
||||
_displayBuffer.Clear();
|
||||
_displayBuffer.Append("AIR PRESSURE REPORT\n\n");
|
||||
foreach (AirZone zone in _zones.Values)
|
||||
{
|
||||
_displayBuffer.Append(zone.Name);
|
||||
_displayBuffer.Append(": ");
|
||||
_displayBuffer.Append(zone.Triggered ? "ALARM TRIPPED " : "NOMINAL ");
|
||||
foreach (IMyAirVent vent in zone.Vents)
|
||||
{
|
||||
_displayBuffer.Append((int)(vent.GetOxygenLevel() * 100F));
|
||||
_displayBuffer.Append("% ");
|
||||
}
|
||||
|
||||
_displayBuffer.Append("\n");
|
||||
}
|
||||
|
||||
_displayBuffer.Append("\n");
|
||||
_displayBuffer.Append("OXYGEN TANK LEVELS\n");
|
||||
foreach (IMyGasTank tank in _oxygenTanks)
|
||||
{
|
||||
_displayBuffer.Append((int)(tank.FilledRatio * 100));
|
||||
_displayBuffer.Append("% ");
|
||||
}
|
||||
|
||||
foreach (IMyTextSurface display in _displays) display.WriteText(_displayBuffer.ToString());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user