96 lines
3.7 KiB
C#
96 lines
3.7 KiB
C#
using Sandbox.ModAPI.Ingame;
|
|
using SpaceEngineers.Game.ModAPI.Ingame;
|
|
using System.Collections.Generic;
|
|
using VRage.Game.ModAPI.Ingame.Utilities;
|
|
|
|
namespace IngameScript
|
|
{
|
|
public partial class Program : MyGridProgram, IConsoleProgram
|
|
{
|
|
public IConsole Console { get; private set; }
|
|
public MyIni Ini { get; private set; }
|
|
|
|
private Dictionary<string, Airlock> _airlocks;
|
|
private List<IEnumerator<bool>> _jobs;
|
|
private MyCommandLine _cli;
|
|
|
|
public Program()
|
|
{
|
|
Ini = new MyIni();
|
|
Console = new MainConsole(this, "Airlock Controller");
|
|
_cli = new MyCommandLine();
|
|
_jobs = new List<IEnumerator<bool>>();
|
|
_airlocks = new Dictionary<string, Airlock>();
|
|
|
|
List<IMyTerminalBlock> airlockBlocks = new List<IMyTerminalBlock>();
|
|
GridTerminalSystem.GetBlocksOfType(airlockBlocks, block => MyIni.HasSection(block.CustomData, "airlock"));
|
|
IMyAirVent referenceVent = null;
|
|
foreach (IMyTerminalBlock block in airlockBlocks)
|
|
{
|
|
Ini.TryParse(block.CustomData, "airlock");
|
|
|
|
// TODO: redundant reference vents would be awesome. Everyone loves redundancy
|
|
if (block is IMyAirVent && Ini.Get("airlock", "reference").ToBoolean())
|
|
{
|
|
if (referenceVent != null)
|
|
{
|
|
Console.Print("Found multiple reference vents. Only the first one will be used.");
|
|
continue;
|
|
}
|
|
|
|
referenceVent = block as IMyAirVent;
|
|
Console.Print($"Found reference vent {block.CustomName}.");
|
|
continue;
|
|
}
|
|
|
|
string airlockName = Ini.Get("airlock", "id").ToString();
|
|
if (!_airlocks.ContainsKey(airlockName))
|
|
{
|
|
_airlocks[airlockName] = new Airlock(airlockName, this);
|
|
}
|
|
|
|
_airlocks[airlockName].AddBlock(block);
|
|
}
|
|
|
|
if (referenceVent != null) foreach (Airlock airlock in _airlocks.Values) { airlock.ReferenceVent = referenceVent; }
|
|
|
|
Console.Print($"Found {_airlocks.Count} airlocks.");
|
|
Console.PrintLower($"Airlock Controller\nTotal Ticks: 0");
|
|
}
|
|
|
|
public void Main(string argument, UpdateType updateSource)
|
|
{
|
|
Console.UpdateTickCount();
|
|
|
|
if (updateSource == UpdateType.Trigger || updateSource == UpdateType.Terminal)
|
|
{
|
|
_cli.TryParse(argument);
|
|
if (_cli.ArgumentCount == 0) { Console.Print("You must provide an airlock ID."); }
|
|
else
|
|
{
|
|
string airlockName = _cli.Argument(0);
|
|
if (!_airlocks.ContainsKey(airlockName)) Console.Print($"Airlock ID '{airlockName}' not found.");
|
|
else if (!_airlocks[airlockName].Functional) Console.Print($"Airlock '{airlockName}' is not functional.");
|
|
else
|
|
{
|
|
_jobs.Add(_airlocks[airlockName].CycleAirlock());
|
|
Runtime.UpdateFrequency |= UpdateFrequency.Update1;
|
|
}
|
|
}
|
|
}
|
|
|
|
for (int i = 0; i < _jobs.Count; i++)
|
|
{
|
|
IEnumerator<bool> job = _jobs[i];
|
|
if (job.MoveNext()) continue;
|
|
job.Dispose();
|
|
_jobs.Remove(job);
|
|
i--;
|
|
Console.Print("Job Removed From Queue.");
|
|
}
|
|
|
|
if (_jobs.Count == 0) Runtime.UpdateFrequency = UpdateFrequency.None;
|
|
}
|
|
}
|
|
}
|