Polished up edge cases so doors close and lock properly.

This commit is contained in:
Anna Rose 2025-02-07 12:08:25 -05:00
parent 643dd04d6b
commit 696b502ae8

View File

@ -23,6 +23,7 @@
MyCommandLine cli;
List<IEnumerator<bool>> jobs;
Dictionary<string, Door> doors;
int tickCount;
public class DoorHinge {
public DoorHinge(Program p, IMyMotorStator hinge) {
@ -47,9 +48,10 @@ public class DoorHinge {
// Process the hinge's movement.
// It will return true when the panel has finished moving.
public bool Actuate() {
if (Math.Abs(Hinge.Angle - TargetAngle) < 0.001) {
if (Math.Abs(Hinge.Angle - TargetAngle) < 0.1 && Hinge.Angle == LastAngle) {
Hinge.RotorLock = true;
}
LastAngle = Hinge.Angle;
return Locked();
}
@ -60,6 +62,7 @@ public class DoorHinge {
private Program P; // for access to Echo, etc.
private IMyMotorStator Hinge { get; set; }
private float TargetAngle { get; set; }
private float LastAngle {get; set; }
private float OpenAngle { get; set; } = 90F;
private float ClosedAngle { get; set; } = 0F;
private float Velocity { get; set; } = 5F;
@ -117,7 +120,7 @@ public class Door {
public bool Actuate() {
bool done = true;
foreach (DoorHinge hinge in Hinges) {
if (hinge.Actuate()) done = false;
if (!hinge.Actuate()) done = false;
}
return done;
}
@ -134,6 +137,7 @@ public class Door {
}
public Program() {
tickCount = 0;
cli = new MyCommandLine();
jobs = new List<IEnumerator<bool>>();
@ -143,7 +147,7 @@ public Program() {
GridTerminalSystem.GetBlocksOfType(allHinges);
foreach(IMyMotorStator hinge in allHinges) {
if (hinge.CustomName.StartsWith("Door")) {
string doorName = hinge.CustomName;
string doorName = hinge.CustomName.Split(' ')[0];
if (!doors.ContainsKey(doorName)) {
doors[doorName] = new Door(this);
}
@ -155,22 +159,34 @@ public Program() {
}
public void Main(string argument, UpdateType updateSource) {
Echo($"index: {tickCount++}");
if (updateSource == UpdateType.Trigger || updateSource == UpdateType.Terminal) {
// Create a new job
cli.TryParse(argument);
List<Door> doorsToControl = new List<Door>();
if (cli.ArgumentCount == 0) {
Echo("No arguments passed. Controlling all doors.");
foreach (Door door in doors.Values) {
if (!door.Locked()) {
continue;
}
doorsToControl.Add(door);
}
}
for (int i=0; i < cli.ArgumentCount; i++) {
string arg = cli.Argument(i);
if (!doors.ContainsKey($"Door{arg}")) {
Echo($"Door with identifier {arg} not found. Skipping.");
string key = "Door" + cli.Argument(i);
if (!doors.ContainsKey(key)) {
Echo($"Door with identifier {key} not found. Skipping.");
continue;
}
if (!doors[arg].Locked()) {
Echo($"Door {arg} already moving. Skipping.");
if (!doors[key].Locked()) {
Echo($"Door {key} already moving. Skipping.");
continue;
}
doorsToControl.Add(doors[arg]);
doorsToControl.Add(doors[key]);
}
if (doorsToControl.Count == 0) {
@ -189,6 +205,7 @@ public void Main(string argument, UpdateType updateSource) {
jobs[i].Dispose();
jobs.Remove(jobs[i]);
i--;
Echo("Operation Complete.");
}
}