83 lines
3.1 KiB
C#
83 lines
3.1 KiB
C#
using Sandbox.ModAPI.Ingame;
|
|
using System.Collections.Generic;
|
|
using VRage.Game.ModAPI.Ingame.Utilities;
|
|
|
|
namespace IngameScript
|
|
{
|
|
public partial class Program : MyGridProgram
|
|
{
|
|
private Dictionary<string, Airlock> _airlocks;
|
|
private List<IEnumerator<bool>> _jobs;
|
|
|
|
private int _tickCount = 0;
|
|
private MyCommandLine _cli;
|
|
private Console _console;
|
|
private MyIni _ini;
|
|
|
|
public Program()
|
|
{
|
|
_ini = new MyIni();
|
|
_console = new Console(this, _ini);
|
|
_cli = new MyCommandLine();
|
|
_jobs = new List<IEnumerator<bool>>();
|
|
_airlocks = new Dictionary<string, Airlock>();
|
|
|
|
List<IMyTerminalBlock> airlockBlocks = new List<IMyTerminalBlock>();
|
|
GridTerminalSystem.GetBlocksOfType(airlockBlocks, block => block.CustomName.Contains("!Airlock"));
|
|
foreach (IMyTerminalBlock block in airlockBlocks)
|
|
{
|
|
{
|
|
string airlockName = Utils.ExtractTag(block, "!Airlock");
|
|
if (!_airlocks.ContainsKey(airlockName))
|
|
{
|
|
_airlocks[airlockName] = new Airlock(this, _console, airlockName);
|
|
}
|
|
|
|
_airlocks[airlockName].AddBlock(block);
|
|
}
|
|
|
|
// TODO: it would be most convenient to just delete non-functional Airlocks... but maybe they're worth
|
|
// keeping around for warnings.
|
|
}
|
|
_console.Print($"Found {_airlocks.Count} airlocks.");
|
|
_console.PrintLower($"Airlock Controller\nTotal Ticks: 0");
|
|
}
|
|
|
|
public void Main(string argument, UpdateType updateSource)
|
|
{
|
|
_console.PrintLower($"Airlock Controller\nTotal Ticks: {++_tickCount}");
|
|
|
|
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 = $"!Airlock{_cli.Argument(0)}";
|
|
if (!_airlocks.ContainsKey(airlockName)) _console.Print($"Invalid airlock ID {_cli.Argument(0)}");
|
|
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())
|
|
{
|
|
job.Dispose();
|
|
_jobs.Remove(job);
|
|
i--;
|
|
_console.Print("Job Removed From Queue.");
|
|
}
|
|
}
|
|
|
|
if (_jobs.Count == 0) Runtime.UpdateFrequency = UpdateFrequency.None;
|
|
}
|
|
}
|
|
}
|