Variable renaming and cleanup.

This commit is contained in:
Anna Rose 2025-02-10 01:59:38 -05:00
parent 723b27298b
commit 606442d3a0
2 changed files with 82 additions and 162 deletions

View File

@ -1,3 +1,13 @@
// Airlock logic.
//
// We don't use a Sequencer for Airlocks for a number of reasons. Most notably:
//
// * One door in each cycle needs to be both closed and then opened.
// * The lights (and in the future, displays) need to emit signal information at several stages.
//
// We could add functionality to the Sequencer for these use cases, but right now we'll keep the logic separate,
// even though *for the most part* it follows the same "Enumerator that waits for various bits of state" logic.
using Sandbox.ModAPI.Ingame; using Sandbox.ModAPI.Ingame;
using SpaceEngineers.Game.ModAPI.Ingame; using SpaceEngineers.Game.ModAPI.Ingame;
using System.Collections.Generic; using System.Collections.Generic;
@ -21,7 +31,7 @@ namespace IngameScript
{ {
get get
{ {
return innerDoor.Status == DoorStatus.Closed && outerDoor.Status == DoorStatus.Closed; return _innerDoor.Status == DoorStatus.Closed && _outerDoor.Status == DoorStatus.Closed;
} }
} }
@ -30,21 +40,21 @@ namespace IngameScript
{ {
get get
{ {
return ((airVent.Depressurize && airVent.GetOxygenLevel() <= targetOxygenLevel) || return ((_airVent.Depressurize && _airVent.GetOxygenLevel() <= targetOxygenLevel) ||
(!airVent.Depressurize && airVent.Status == VentStatus.Pressurized)); (!_airVent.Depressurize && _airVent.Status == VentStatus.Pressurized));
} }
} }
public bool OxygenBalanced public bool OxygenBalanced
{ {
get { return oxygenTank.FilledRatio > 0.25; } get { return _oxygenTank.FilledRatio > 0.25; }
} }
public bool Functional public bool Functional
{ {
get get
{ {
return (innerDoor != null && outerDoor != null && airVent != null && oxygenTank != null); return (_innerDoor != null && _outerDoor != null && _airVent != null && _oxygenTank != null);
} }
} }
@ -54,20 +64,20 @@ namespace IngameScript
{ {
get get
{ {
return innerDoor.Status == DoorStatus.Open || outerDoor.Status == DoorStatus.Open; return _innerDoor.Status == DoorStatus.Open || _outerDoor.Status == DoorStatus.Open;
} }
} }
// Returns false if we are in a state where we can't or don't need to balance // Returns false if we are in a state where we can't or don't need to balance
private bool balanceOxygen() private bool balanceOxygen()
{ {
if (innerDoor.Status == DoorStatus.Closed || outerDoor.Status == DoorStatus.Open || OxygenBalanced) { return false; } if (_innerDoor.Status == DoorStatus.Closed || _outerDoor.Status == DoorStatus.Open || OxygenBalanced) { return false; }
_console.Print($"{_name}: Balancing Oxygen Tank"); _console.Print($"{_name}: Balancing Oxygen Tank");
// Configure the vent to suck in Oxygen. // Configure the vent to suck in Oxygen.
airVent.Depressurize = true; _airVent.Depressurize = true;
airVent.Enabled = true; _airVent.Enabled = true;
return true; return true;
} }
@ -76,64 +86,46 @@ namespace IngameScript
private MyGridProgram _p; private MyGridProgram _p;
private float targetOxygenLevel = 0.0F; private float targetOxygenLevel = 0.0F;
private IMyDoor innerDoor; private IMyDoor _innerDoor;
private IMyDoor outerDoor; private IMyDoor _outerDoor;
private List<IMyLightingBlock> lights; private List<IMyLightingBlock> _lights;
private IMyGasTank oxygenTank; private IMyGasTank _oxygenTank;
private IMyAirVent airVent; private IMyAirVent _airVent;
private IMyAirVent airSensor; private IMyAirVent _airSensor;
// private List<IMyTextSurface> _displays;
private const int CooldownTicks = 120; private const int CooldownTicks = 120;
private const int SealTimeoutTicks = 30;
public Airlock(MyGridProgram program, Console console, string name) public Airlock(MyGridProgram program, Console console, string name)
{ {
_p = program; _p = program;
_name = name; _name = name;
_console = console.CreatePrefixedConsole(_name); _console = console.CreatePrefixedConsole(_name);
lights = new List<IMyLightingBlock>(); _lights = new List<IMyLightingBlock>();
// _displays = new List<IMyTextSurface>();
// Find the appropriate blocks given the airlock name
// initDoors();
// initLights();
// initVents();
// initOxygen();
} }
public void AddBlock(IMyTerminalBlock block) public void AddBlock(IMyTerminalBlock block)
{ {
if (block is IMyDoor) if (block is IMyDoor) addDoor(block as IMyDoor);
{ else if (block is IMyLightingBlock) _lights.Add(block as IMyLightingBlock);
addDoor(block as IMyDoor); else if (block is IMyAirVent) addVent(block as IMyAirVent);
return; else if (block is IMyGasTank) addOxygenTank(block as IMyGasTank);
} // else if (block is IMyTextSurfaceProvider) _displays.Add(((IMyTextSurfaceProvider)block).GetSurface(0));
if (block is IMyLightingBlock) else _console.Print($"Tried to add invalid block '{block.CustomName}'");
{
lights.Add(block as IMyLightingBlock);
return;
}
if (block is IMyAirVent)
{
addVent(block as IMyAirVent);
return;
}
if (block is IMyGasTank)
{
addOxygenTank(block as IMyGasTank);
return;
}
_console.Print($"Tried to add invalid block '{block.CustomName}'");
} }
private void addDoor(IMyDoor door) private void addDoor(IMyDoor door)
{ {
if (door.CustomName.Contains("Inner") && innerDoor == null) if (door.CustomName.Contains("Inner") && _innerDoor == null)
{ {
innerDoor = door; _innerDoor = door;
return; return;
} }
if (door.CustomName.Contains("Outer") && outerDoor == null) if (door.CustomName.Contains("Outer") && _outerDoor == null)
{ {
outerDoor = door; _outerDoor = door;
return; return;
} }
_console.Print($"Couldn't add door '{door.CustomName}'"); _console.Print($"Couldn't add door '{door.CustomName}'");
@ -141,14 +133,14 @@ namespace IngameScript
private void addVent(IMyAirVent vent) private void addVent(IMyAirVent vent)
{ {
if (vent.CustomName.Contains("Main") && airVent == null) if (vent.CustomName.Contains("Main") && _airVent == null)
{ {
airVent = vent; _airVent = vent;
return; return;
} }
if (vent.CustomName.Contains("Reference") && airSensor == null) if (vent.CustomName.Contains("Reference") && _airSensor == null)
{ {
airSensor = vent; _airSensor = vent;
return; return;
} }
_console.Print($"Couldn't add air vent '{vent.CustomName}'"); _console.Print($"Couldn't add air vent '{vent.CustomName}'");
@ -156,98 +148,14 @@ namespace IngameScript
private void addOxygenTank(IMyGasTank tank) private void addOxygenTank(IMyGasTank tank)
{ {
if (oxygenTank == null) if (_oxygenTank == null)
{ {
oxygenTank = tank; _oxygenTank = tank;
return; return;
} }
_console.Print($"Couldn't add oxygen tank '{tank.CustomName}'"); _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;
@ -257,7 +165,14 @@ namespace IngameScript
while (!DoorsClosed) { yield return true; } while (!DoorsClosed) { yield return true; }
lockDoors(); lockDoors();
if (!airVent.CanPressurize) int ticks = 0;
while (!_airVent.CanPressurize && ticks < SealTimeoutTicks)
{
ticks++;
yield return true;
}
if (!_airVent.CanPressurize)
{ {
error("Airlock is not airtight."); error("Airlock is not airtight.");
Cycling = false; Cycling = false;
@ -266,7 +181,7 @@ namespace IngameScript
pressurizeDepressurize(); pressurizeDepressurize();
while (!PressureStabilized) { yield return true; } while (!PressureStabilized) { yield return true; }
airVent.Enabled = false; _airVent.Enabled = false;
openDoor(); openDoor();
while (!DoorOpened) { yield return true; } while (!DoorOpened) { yield return true; }
@ -278,7 +193,7 @@ namespace IngameScript
{ {
while (!OxygenBalanced) { yield return true; } while (!OxygenBalanced) { yield return true; }
} }
airVent.Enabled = false; _airVent.Enabled = false;
// Cooldown period // Cooldown period
int cooldown = 0; int cooldown = 0;
@ -298,10 +213,10 @@ namespace IngameScript
_console.Print("Closing Doors"); _console.Print("Closing Doors");
// close the doors // close the doors
innerDoor.Enabled = true; _innerDoor.Enabled = true;
outerDoor.Enabled = true; _outerDoor.Enabled = true;
innerDoor.CloseDoor(); _innerDoor.CloseDoor();
outerDoor.CloseDoor(); _outerDoor.CloseDoor();
} }
private void pressurizeDepressurize() private void pressurizeDepressurize()
@ -309,14 +224,14 @@ namespace IngameScript
_console.Print("Cycling"); _console.Print("Cycling");
// toggle the current state // toggle the current state
airVent.Depressurize = !airVent.Depressurize; _airVent.Depressurize = !_airVent.Depressurize;
airVent.Enabled = true; _airVent.Enabled = true;
// When depressurizing, check the external pressure and only depressurize to that value. // When depressurizing, check the external pressure and only depressurize to that value.
// TODO: test this for floating point errors // TODO: test this for floating point errors
if (airVent.Depressurize && airSensor != null) if (_airVent.Depressurize && _airSensor != null)
{ {
targetOxygenLevel = airSensor.GetOxygenLevel(); targetOxygenLevel = _airSensor.GetOxygenLevel();
_console.Print($"Set depressurization target to {targetOxygenLevel}"); _console.Print($"Set depressurization target to {targetOxygenLevel}");
} }
} }
@ -326,15 +241,15 @@ namespace IngameScript
{ {
_console.Print($"Opening Door"); _console.Print($"Opening Door");
if (airVent.Status == VentStatus.Pressurized) if (_airVent.Status == VentStatus.Pressurized)
{ {
innerDoor.Enabled = true; _innerDoor.Enabled = true;
innerDoor.OpenDoor(); _innerDoor.OpenDoor();
} }
else else
{ {
outerDoor.Enabled = true; _outerDoor.Enabled = true;
outerDoor.OpenDoor(); _outerDoor.OpenDoor();
} }
} }
@ -362,7 +277,7 @@ namespace IngameScript
break; break;
} }
foreach (IMyLightingBlock light in lights) foreach (IMyLightingBlock light in _lights)
{ {
light.Enabled = true; light.Enabled = true;
light.BlinkIntervalSeconds = blinkInterval; light.BlinkIntervalSeconds = blinkInterval;
@ -371,10 +286,16 @@ namespace IngameScript
} }
} }
// private void setDisplays(string text) {
// foreach (IMyTextSurface display in _displays) {
// display.WriteText(text);
// }
// }
private void lockDoors() private void lockDoors()
{ {
innerDoor.Enabled = false; _innerDoor.Enabled = false;
outerDoor.Enabled = false; _outerDoor.Enabled = false;
} }
private void error(string error) private void error(string error)

View File

@ -40,11 +40,12 @@ namespace IngameScript
// keeping around for warnings. // keeping around for warnings.
} }
_console.Print($"Found {_airlocks.Count} airlocks."); _console.Print($"Found {_airlocks.Count} airlocks.");
_console.PrintLower($"Airlock Controller\nTotal Ticks: 0");
} }
public void Main(string argument, UpdateType updateSource) public void Main(string argument, UpdateType updateSource)
{ {
_console.PrintLower($"Total Ticks: {_tickCount++}"); _console.PrintLower($"Airlock Controller\nTotal Ticks: {++_tickCount}");
if (updateSource == UpdateType.Trigger || updateSource == UpdateType.Terminal) if (updateSource == UpdateType.Trigger || updateSource == UpdateType.Terminal)
{ {
@ -53,10 +54,8 @@ namespace IngameScript
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)}");
{ else if (!_airlocks[airlockName].Functional) _console.Print($"Airlock '{airlockName}' is not functional.");
_console.Print($"Invalid airlock ID {_cli.Argument(0)}");
}
else else
{ {
_jobs.Add(_airlocks[airlockName].CycleAirlock()); _jobs.Add(_airlocks[airlockName].CycleAirlock());