Last active
August 17, 2019 18:41
-
-
Save ekovac/1a054d49395fd75db6776e82973aefc7 to your computer and use it in GitHub Desktop.
Airlock script for Space Engineers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public Program() | |
{ | |
// The constructor, called only once every session and | |
// always before any other method is called. Use it to | |
// initialize your script. | |
// | |
// The constructor is optional and can be removed if not | |
// needed. | |
// | |
// It's recommended to set RuntimeInfo.UpdateFrequency | |
// here, which will allow your script to run itself without a | |
// timer block. | |
Runtime.UpdateFrequency = UpdateFrequency.Update10; | |
} | |
public void Main(string argument, UpdateType updateSource) | |
{ | |
string doorNamePattern = @"(?<name>\w+)\s+\[Airlock (?<side>Inner|Outer)\]"; | |
var doors = new List<IMyTerminalBlock>(); | |
var doorSets = new Dictionary<string, List<IMyDoor>>(); | |
var doorNameExpression = new System.Text.RegularExpressions.Regex(doorNamePattern); | |
GridTerminalSystem.SearchBlocksOfName("[Airlock", doors); | |
foreach (IMyTerminalBlock door in doors) { | |
System.Text.RegularExpressions.Match match = doorNameExpression.Match(door.CustomName); | |
if (match.Success) { | |
System.Text.RegularExpressions.GroupCollection groups = match.Groups; | |
string name = groups["name"].Value; | |
string side = groups["side"].Value; | |
List<IMyDoor> doorSet; | |
if (doorSets.ContainsKey(name)) { | |
doorSet = doorSets[name]; | |
} else { | |
doorSet = new List<IMyDoor>(); | |
} | |
doorSet.Add(door as IMyDoor); | |
doorSets[name] = doorSet; | |
} else { | |
Echo("Failed to parse door name: " + door.CustomName); | |
} | |
} | |
foreach (var doorSet in doorSets.Values) { | |
IMyDoor openDoor = null; | |
bool tooManyOpen = false; | |
foreach (var door in doorSet) { | |
if (door.Open && openDoor == null) { | |
openDoor = door; | |
} else if (door.Open && openDoor != null) { | |
tooManyOpen = true; | |
} | |
} | |
// Slam everything shut if there are too many doors open. | |
if (tooManyOpen) { | |
foreach (var door in doorSet) { | |
door.Enabled = true; | |
var action = door.GetActionWithName("Open_Off"); | |
action.Apply(door); | |
} | |
continue; | |
} | |
if (openDoor != null) { | |
foreach (var door in doorSet) { | |
if (door != openDoor) { | |
door.Enabled = false; | |
} else { | |
door.Enabled = true; | |
} | |
} | |
// Update timer on open door | |
try | |
{ | |
var doorTimer = Int32.Parse(openDoor.CustomData); | |
if (doorTimer < 1) { | |
var action = openDoor.GetActionWithName("Open_Off"); | |
action.Apply(openDoor); | |
} else { | |
doorTimer -= 1; | |
openDoor.CustomData = doorTimer.ToString(); | |
} | |
} | |
catch (FormatException) { | |
openDoor.CustomData = "15"; | |
} | |
} else { | |
foreach (var door in doorSet) { | |
door.CustomData = ""; | |
door.Enabled = true; | |
} | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment