using Sandbox.ModAPI.Ingame; using SpaceEngineers.Game.ModAPI.Ingame; using System.Collections.Generic; using VRage.Game.ModAPI.Ingame.Utilities; namespace IngameScript { partial class Program { public class AirZone { public bool Triggered { get; private set; } = false; public string Name { get; private set; } public List Vents { get; private set; } private MyIni _ini; private List _doors; private List _displays; private PrefixedConsole _console; private Sequencer _sequencer; private const float TriggerLevel = 0.75F; public AirZone(string zoneName, MyIni ini, IConsole console) { Name = zoneName; _ini = ini; _console = new PrefixedConsole(console, zoneName); _sequencer = new Sequencer(zoneName, _console); Vents = new List(); _doors = new List(); _displays = new List(); } public void AddBlock(IMyTerminalBlock block) { if (block is IMyAirVent) { Vents.Add(block as IMyAirVent); } else if (block is IMyDoor) { _doors.Add(block as IMyDoor); SequenceableDoor wrapped = SequenceableFactory.MakeSequenceable(block as IMyDoor, _ini, "airMonitor") as SequenceableDoor; wrapped.Step = 0; wrapped.DeployOpen = false; wrapped.LockOpen = false; wrapped.LockClosed = true; _sequencer.AddBlock(wrapped); } else { _console.Print("Tried to add incompatible block '{block.CustomName}'"); } // TODO: support for arbitrary (sub-)sequences here could be powerful } public void Reset() { _console.Print("Resetting sensor."); Triggered = false; } // This enumerator should run forever. public IEnumerator Monitor() { while (true) { foreach (IMyAirVent vent in Vents) { if (vent.GetOxygenLevel() < TriggerLevel) Triggered = true; } if (Triggered == true) { _console.Print($"Low pressure alarm triggered."); // close the doors IEnumerator job = _sequencer.RunSequence(true); while (job.MoveNext()) yield return true; while (Triggered) { // if any of the doors become re-enabled, reset and unlock all of them; // think of it as a player manually overriding the safety protocols. // Note that this means resets propagate across zones that share doors, // which will likely be all of them. Which is a *little* bit of a // bug, but it's better than never re-triggering foreach (IMyDoor door in _doors) { if (door.Enabled) { Reset(); break; } } yield return true; } // unlock the doors, but we'll leave them closed. foreach (IMyDoor door in _doors) { door.Enabled = true; } } yield return true; } } } } }