Refactor airlock for fewer lookups and in preparation for using a sequencer.

This commit is contained in:
Anna Rose 2025-02-09 21:51:47 -05:00
parent e82303ff00
commit 723b27298b
3 changed files with 363 additions and 284 deletions

View File

@ -5,6 +5,8 @@ using VRageMath;
namespace IngameScript namespace IngameScript
{ {
partial class Program
{
public class Airlock public class Airlock
{ {
enum AirlockLightState enum AirlockLightState
@ -38,7 +40,13 @@ namespace IngameScript
get { return oxygenTank.FilledRatio > 0.25; } get { return oxygenTank.FilledRatio > 0.25; }
} }
public bool Functional { get; private set; } = true; public bool Functional
{
get
{
return (innerDoor != null && outerDoor != null && airVent != null && oxygenTank != null);
}
}
public bool Cycling { get; private set; } = false; public bool Cycling { get; private set; } = false;
@ -81,98 +89,165 @@ namespace IngameScript
_p = program; _p = program;
_name = name; _name = name;
_console = console.CreatePrefixedConsole(_name); _console = console.CreatePrefixedConsole(_name);
lights = new List<IMyLightingBlock>();
// Find the appropriate blocks given the airlock name // Find the appropriate blocks given the airlock name
initDoors(); // initDoors();
initLights(); // initLights();
initVents(); // initVents();
initOxygen(); // initOxygen();
} }
private void initDoors() public void AddBlock(IMyTerminalBlock block)
{ {
List<IMyDoor> onlyDoors = new List<IMyDoor>(); if (block is IMyDoor)
_p.GridTerminalSystem.GetBlocksOfType(onlyDoors);
foreach (IMyDoor door in onlyDoors)
{
if (!door.CustomName.StartsWith(_name)) continue;
if (door.CustomName.Contains("Inner"))
{
innerDoor = door;
}
else if (door.CustomName.Contains("Outer"))
{
outerDoor = door;
}
if (innerDoor != null && outerDoor != null)
{ {
addDoor(block as IMyDoor);
return; return;
} }
if (block is IMyLightingBlock)
{
lights.Add(block as IMyLightingBlock);
return;
}
if (block is IMyAirVent)
{
addVent(block as IMyAirVent);
return;
}
if (block is IMyGasTank)
{
addOxygenTank(block as IMyGasTank);
return;
} }
Functional = false; _console.Print($"Tried to add invalid block '{block.CustomName}'");
} }
private void initVents() private void addDoor(IMyDoor door)
{ {
List<IMyAirVent> onlyFans = new List<IMyAirVent>(); if (door.CustomName.Contains("Inner") && innerDoor == null)
_p.GridTerminalSystem.GetBlocksOfType(onlyFans);
foreach (IMyAirVent vent in onlyFans)
{ {
if (!vent.CustomName.StartsWith(_name)) continue; innerDoor = door;
return;
}
if (door.CustomName.Contains("Outer") && outerDoor == null)
{
outerDoor = door;
return;
}
_console.Print($"Couldn't add door '{door.CustomName}'");
}
if (vent.CustomName.StartsWith(_name)) private void addVent(IMyAirVent vent)
{ {
if (vent.CustomName.Contains("Main")) if (vent.CustomName.Contains("Main") && airVent == null)
{ {
airVent = vent; airVent = vent;
continue; return;
} }
else if (vent.CustomName.Contains("Reference")) if (vent.CustomName.Contains("Reference") && airSensor == null)
{ {
airSensor = vent; airSensor = vent;
continue; return;
} }
_console.Print($"Couldn't add air vent '{vent.CustomName}'");
} }
// A global reference vent will be used if we don't have one specific to our airlock. private void addOxygenTank(IMyGasTank tank)
// A specific vent found later will overwrite this assignment.
if (vent.CustomName.StartsWith("Airlock Reference") && airSensor == null)
{ {
airSensor = vent; if (oxygenTank == null)
}
}
if (airVent == null) Functional = false;
}
private void initLights()
{ {
lights = new List<IMyLightingBlock>();
List<IMyLightingBlock> allLights = new List<IMyLightingBlock>();
_p.GridTerminalSystem.GetBlocksOfType(allLights);
foreach (IMyLightingBlock light in allLights)
{
if (!light.CustomName.StartsWith(_name)) continue;
lights.Add(light);
}
}
private void initOxygen()
{
List<IMyGasTank> allTanks = new List<IMyGasTank>();
_p.GridTerminalSystem.GetBlocksOfType(allTanks);
foreach (IMyGasTank tank in allTanks)
{
if (!tank.CustomName.StartsWith(_name)) continue;
oxygenTank = tank; oxygenTank = tank;
return; return;
} }
Functional = false; _console.Print($"Couldn't add oxygen tank '{tank.CustomName}'");
} }
// private void initDoors()
// {
// List<IMyDoor> onlyDoors = new List<IMyDoor>();
// _p.GridTerminalSystem.GetBlocksOfType(onlyDoors);
// foreach (IMyDoor door in onlyDoors)
// {
// if (!door.CustomName.StartsWith(_name)) continue;
// if (door.CustomName.Contains("Inner"))
// {
// innerDoor = door;
// }
// else if (door.CustomName.Contains("Outer"))
// {
// outerDoor = door;
// }
// if (innerDoor != null && outerDoor != null)
// {
// return;
// }
// }
// Functional = false;
// }
// private void initVents()
// {
// List<IMyAirVent> onlyFans = new List<IMyAirVent>();
// _p.GridTerminalSystem.GetBlocksOfType(onlyFans);
// foreach (IMyAirVent vent in onlyFans)
// {
// if (!vent.CustomName.StartsWith(_name)) continue;
// if (vent.CustomName.StartsWith(_name))
// {
// if (vent.CustomName.Contains("Main"))
// {
// airVent = vent;
// continue;
// }
// else if (vent.CustomName.Contains("Reference"))
// {
// airSensor = vent;
// continue;
// }
// }
// // A global reference vent will be used if we don't have one specific to our airlock.
// // A specific vent found later will overwrite this assignment.
// if (vent.CustomName.StartsWith("Airlock Reference") && airSensor == null)
// {
// airSensor = vent;
// }
// }
// if (airVent == null) Functional = false;
// }
// private void initLights()
// {
// lights = new List<IMyLightingBlock>();
// List<IMyLightingBlock> allLights = new List<IMyLightingBlock>();
// _p.GridTerminalSystem.GetBlocksOfType(allLights);
// foreach (IMyLightingBlock light in allLights)
// {
// if (!light.CustomName.StartsWith(_name)) continue;
// lights.Add(light);
// }
// }
// private void initOxygen()
// {
// List<IMyGasTank> allTanks = new List<IMyGasTank>();
// _p.GridTerminalSystem.GetBlocksOfType(allTanks);
// foreach (IMyGasTank tank in allTanks)
// {
// if (!tank.CustomName.StartsWith(_name)) continue;
// oxygenTank = tank;
// return;
// }
// Functional = false;
// }
public IEnumerator<bool> CycleAirlock() public IEnumerator<bool> CycleAirlock()
{ {
Cycling = true; Cycling = true;
@ -308,4 +383,5 @@ namespace IngameScript
setLights(AirlockLightState.Error); setLights(AirlockLightState.Error);
} }
} }
}
} }

View File

@ -24,5 +24,5 @@
<AdditionalFiles Include="thumb.png" /> <AdditionalFiles Include="thumb.png" />
</ItemGroup> </ItemGroup>
<Import Project="..\Mixins\Console\Console.projitems" Label="shared" /> <Import Project="..\Mixins\Console\Console.projitems" Label="shared" />
<Import Project="..\Mixins\ConfigParser\ConfigParser.projitems" Label="shared" /> <Import Project="..\Mixins\Utils\Utils.projitems" Label="shared" />
</Project> </Project>

View File

@ -12,30 +12,33 @@ namespace IngameScript
private int _tickCount = 0; private int _tickCount = 0;
private MyCommandLine _cli; private MyCommandLine _cli;
private Console _console; private Console _console;
private MyIni _ini;
public Program() public Program()
{ {
_console = new Console(this); _ini = new MyIni();
_console = new Console(this, _ini);
_cli = new MyCommandLine(); _cli = new MyCommandLine();
_jobs = new List<IEnumerator<bool>>(); _jobs = new List<IEnumerator<bool>>();
_airlocks = new Dictionary<string, Airlock>(); _airlocks = new Dictionary<string, Airlock>();
List<IMyDoor> doors = new List<IMyDoor>(); List<IMyTerminalBlock> airlockBlocks = new List<IMyTerminalBlock>();
GridTerminalSystem.GetBlocksOfType(doors); GridTerminalSystem.GetBlocksOfType(airlockBlocks, block => block.CustomName.Contains("!Airlock"));
foreach (IMyDoor door in doors) foreach (IMyTerminalBlock block in airlockBlocks)
{ {
if (!door.CustomName.StartsWith("Airlock")) continue;
string airlockName = door.CustomName.Split(' ')[0];
if (_airlocks.ContainsKey(airlockName)) continue;
Airlock newAirlock = new Airlock(this, _console, airlockName);
if (!newAirlock.Functional)
{ {
_console.Print($"{airlockName} is missing one or more required blocks."); string airlockName = Utils.ExtractTag(block, "!Airlock");
continue; if (!_airlocks.ContainsKey(airlockName))
} {
_airlocks[airlockName] = newAirlock; _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.Print($"Found {_airlocks.Count} airlocks.");
} }
@ -49,7 +52,7 @@ namespace IngameScript
if (_cli.ArgumentCount == 0) { _console.Print("You must provide an airlock ID."); } if (_cli.ArgumentCount == 0) { _console.Print("You must provide an airlock ID."); }
else else
{ {
string airlockName = $"Airlock{_cli.Argument(0)}"; string airlockName = $"!Airlock{_cli.Argument(0)}";
if (!_airlocks.ContainsKey(airlockName)) if (!_airlocks.ContainsKey(airlockName))
{ {
_console.Print($"Invalid airlock ID {_cli.Argument(0)}"); _console.Print($"Invalid airlock ID {_cli.Argument(0)}");