From 30927142121c04ca483a5ffc83d67b1ae695681e Mon Sep 17 00:00:00 2001 From: annabunches Date: Wed, 19 Feb 2025 19:51:03 -0500 Subject: [PATCH] Add checks to avoid running docking commands in invalid state. --- DockingController/Program.cs | 57 +++++++++++++++++++++++------------- 1 file changed, 36 insertions(+), 21 deletions(-) diff --git a/DockingController/Program.cs b/DockingController/Program.cs index c990a9c..a36e601 100644 --- a/DockingController/Program.cs +++ b/DockingController/Program.cs @@ -12,40 +12,37 @@ namespace IngameScript private IEnumerator _job = null; private MyCommandLine _cli = new MyCommandLine(); - private List dockingPorts = new List(); - private List thrusters = new List(); - private List gasTanks = new List(); - - private ActionGroup actionGroup; + private List _dockingPorts = new List(); + private ActionGroup _actionGroup; public Program() { Console = new MainConsole(this, "Docking Controller"); - actionGroup = new ActionGroup(Console, "docking"); + _actionGroup = new ActionGroup(Console, "docking"); // We store a list of the dockingPorts because we will only dock if one is ready to lock. - GridTerminalSystem.GetBlocksOfType(dockingPorts, blockFilter); + GridTerminalSystem.GetBlocksOfType(_dockingPorts, blockFilter); - foreach (IMyShipConnector dockingPort in dockingPorts) + foreach (IMyShipConnector dockingPort in _dockingPorts) { - actionGroup.AddActionBlock("dock", 0, new BlockActionConnector(dockingPort, ConnectorAction.Connect)); - actionGroup.AddActionBlock("undock", 1, new BlockActionConnector(dockingPort, ConnectorAction.Disconnect)); + _actionGroup.AddActionBlock("dock", 0, new BlockActionConnector(dockingPort, ConnectorAction.Connect)); + _actionGroup.AddActionBlock("undock", 1, new BlockActionConnector(dockingPort, ConnectorAction.Disconnect)); } List thrusters = new List(); GridTerminalSystem.GetBlocksOfType(thrusters, blockFilter); foreach (IMyThrust thruster in thrusters) { - actionGroup.AddActionBlock("dock", 1, new BlockActionThruster(thruster, ThrusterAction.Disable)); - actionGroup.AddActionBlock("undock", 0, new BlockActionThruster(thruster, ThrusterAction.Enable)); + _actionGroup.AddActionBlock("dock", 1, new BlockActionThruster(thruster, ThrusterAction.Disable)); + _actionGroup.AddActionBlock("undock", 0, new BlockActionThruster(thruster, ThrusterAction.Enable)); } List gasTanks = new List(); GridTerminalSystem.GetBlocksOfType(gasTanks, blockFilter); foreach (IMyGasTank gasTank in gasTanks) { - actionGroup.AddActionBlock("dock", 1, new BlockActionGasTank(gasTank, GasTankAction.Stockpile)); - actionGroup.AddActionBlock("undock", 0, new BlockActionGasTank(gasTank, GasTankAction.Dispense)); + _actionGroup.AddActionBlock("dock", 1, new BlockActionGasTank(gasTank, GasTankAction.Stockpile)); + _actionGroup.AddActionBlock("undock", 0, new BlockActionGasTank(gasTank, GasTankAction.Dispense)); } } @@ -53,18 +50,27 @@ namespace IngameScript { if (argument != "") { - _cli.TryParse(argument); - if (_cli.ArgumentCount == 0) - { - Console.Print("Error: Must pass an argument, 'dock' or 'undock'."); - } - else if (_job != null) + if (_job != null) { Console.Print("Already performing a docking action."); } else { - _job = actionGroup.RunAction(_cli.Argument(0).ToLower()); + _cli.TryParse(argument); + if (_cli.ArgumentCount == 0) + { + Console.Print("Error: Must pass an argument, 'dock' or 'undock'."); + return; + } + string action = _cli.Argument(0).ToLower(); + + if (action == "dock" && !readyToDock()) + { + Console.Print("No docking port in a dockable state."); + return; + } + + _job = _actionGroup.RunAction(_cli.Argument(0).ToLower()); Runtime.UpdateFrequency = UpdateFrequency.Update10; } } @@ -87,5 +93,14 @@ namespace IngameScript } return true; } + + private bool readyToDock() + { + foreach (IMyShipConnector dockingPort in _dockingPorts) + { + if (dockingPort.Status == MyShipConnectorStatus.Connectable) return true; + } + return false; + } } }