diff --git a/Program.cs b/Program.cs index 9179fc8..3d9bf6b 100644 --- a/Program.cs +++ b/Program.cs @@ -39,6 +39,14 @@ namespace IngameScript MyIni _storageIni = new MyIni(); bool _isDocked; + List _connectors = new List(); + List _naniteControlFacilities = new List(); + List _naniteBeacons = new List(); + List _gyroscopes = new List(); + List _thrusters = new List(); + List _gasTanks = new List(); + List _batteries = new List(); + public Program() { Echo("Loading storage..."); @@ -219,37 +227,38 @@ namespace IngameScript _storageIni.Clear(); _storageIni.Set("AutomaticConnectorActions", "Is_Docked", _isDocked); Storage = _storageIni.ToString(); - Echo($"Docking status set to {_isDocked} and saved to internal storage."); } private bool DoesGridHaveNaniteControlFacility() { - List naniteControlFacilities = new List(); - // Get all functional blocks on the grid that are Nanite Control Facilities + // Nanite Control Facilities are considered IMyShipWelder GridTerminalSystem.GetBlocksOfType( - naniteControlFacilities, + _naniteControlFacilities, naniteControlFacility => naniteControlFacility.CustomName.Contains(_naniteControlFacilityString) ); - return naniteControlFacilities.Count > 0; + return _naniteControlFacilities.Count > 0; } private void Dock() { + if (_isDocked == true) + { + Echo("Already docked."); + return; + } if (_manageThrusters == true) { Echo("Turning off grid thrusters..."); - List thrusters = new List(); - // Get all thrusters on the grid GridTerminalSystem.GetBlocksOfType( - thrusters, + _thrusters, thruster => thruster.IsSameConstructAs(Me) ); // Disable all thrusters on the grid - foreach (IMyThrust thruster in thrusters) + foreach (IMyThrust thruster in _thrusters) { thruster.Enabled = false; } @@ -260,16 +269,14 @@ namespace IngameScript if (_manageGyroscopes == true) { Echo("Turning off grid gyroscopes..."); - List gyroscopes = new List(); - // Get all gyroscopes on the grid GridTerminalSystem.GetBlocksOfType( - gyroscopes, + _gyroscopes, gyroscope => gyroscope.IsSameConstructAs(Me) ); // Disable all gyroscopes on the grid - foreach (IMyGyro gyro in gyroscopes) + foreach (IMyGyro gyro in _gyroscopes) { gyro.Enabled = false; } @@ -280,16 +287,14 @@ namespace IngameScript if (_stockpileGasTanks == true) { Echo("Setting grid gas tanks to stockpile..."); - List gasTanks = new List(); - // Get all gas tanks on the grid GridTerminalSystem.GetBlocksOfType( - gasTanks, + _gasTanks, gasTank => gasTank.IsSameConstructAs(Me) ); // Stockpile all gas tanks on the grid - foreach (IMyGasTank gasTank in gasTanks) + foreach (IMyGasTank gasTank in _gasTanks) { gasTank.Stockpile = true; } @@ -300,15 +305,14 @@ namespace IngameScript if (_rechargeBatteries == true) { Echo("Setting grid batteries to recharge..."); - List batteries = new List(); - + ; // Get all batteries on the grid GridTerminalSystem.GetBlocksOfType( - batteries, + _batteries, battery => battery.IsSameConstructAs(Me) ); - foreach (IMyBatteryBlock battery in batteries) + foreach (IMyBatteryBlock battery in _batteries) { battery.ChargeMode = ChargeMode.Recharge; } @@ -318,18 +322,16 @@ namespace IngameScript if (_naniteControlFacilityIntegration && DoesGridHaveNaniteControlFacility()) { Echo("Turning off grid Nanite Beacons..."); - List naniteBeacons = new List(); - // Get all nanite beacons on the grid GridTerminalSystem.GetBlocksOfType( - naniteBeacons, + _naniteBeacons, naniteBeacon => naniteBeacon.IsSameConstructAs(Me) && naniteBeacon.CustomName.Contains(_naniteBeaconString) ); // Disable all nanite beacons on the grid - foreach (IMyFunctionalBlock naniteBeacon in naniteBeacons) + foreach (IMyFunctionalBlock naniteBeacon in _naniteBeacons) { naniteBeacon.Enabled = false; } @@ -343,19 +345,22 @@ namespace IngameScript private void Undock() { + if (_isDocked == false) + { + Echo("Already undocked."); + return; + } if (_rechargeBatteries == true) { Echo("Setting grid batteries to auto..."); - List batteries = new List(); - // Get all batteries on the grid GridTerminalSystem.GetBlocksOfType( - batteries, + _batteries, battery => battery.IsSameConstructAs(Me) ); // Set all batteries on the grid to auto - foreach (IMyBatteryBlock battery in batteries) + foreach (IMyBatteryBlock battery in _batteries) { battery.ChargeMode = ChargeMode.Auto; } @@ -366,16 +371,14 @@ namespace IngameScript if (_stockpileGasTanks == true) { Echo("Setting grid gas tanks to not stockpile..."); - List gasTanks = new List(); - // Get all gas tanks on the grid GridTerminalSystem.GetBlocksOfType( - gasTanks, + _gasTanks, gasTank => gasTank.IsSameConstructAs(Me) ); // Disable stockpiling on all gas tanks on the grid - foreach (IMyGasTank gasTank in gasTanks) + foreach (IMyGasTank gasTank in _gasTanks) { gasTank.Stockpile = false; } @@ -386,16 +389,14 @@ namespace IngameScript if (_manageGyroscopes == true) { Echo("Turning on grid gyroscopes..."); - List gyroscopes = new List(); - // Get all gyroscopes on the grid GridTerminalSystem.GetBlocksOfType( - gyroscopes, + _gyroscopes, gyroscope => gyroscope.IsSameConstructAs(Me) ); // Enable all gyroscopes on the grid - foreach (IMyGyro gyro in gyroscopes) + foreach (IMyGyro gyro in _gyroscopes) { gyro.Enabled = true; } @@ -406,18 +407,16 @@ namespace IngameScript if (_manageManueveringThrusters == true) { Echo("Turning on grid maneuvering thrusters..."); - List maneuveringThrusters = new List(); - // Get all thrusters on the grid that are considered maneuvering thrusters GridTerminalSystem.GetBlocksOfType( - maneuveringThrusters, + _thrusters, maneuveringThruster => maneuveringThruster.IsSameConstructAs(Me) && maneuveringThruster.CustomName.Contains(_maneuveringThrusterString) ); // Enable all maneuvering thrusters on the grid - foreach (IMyThrust maneuveringThruster in maneuveringThrusters) + foreach (IMyThrust maneuveringThruster in _thrusters) { maneuveringThruster.Enabled = true; } @@ -428,16 +427,14 @@ namespace IngameScript if (_enableAllThrustersOnUndock == true) { Echo("Turning on grid thrusters..."); - List thrusters = new List(); - // Get all thrusters on the grid GridTerminalSystem.GetBlocksOfType( - thrusters, + _thrusters, thruster => thruster.IsSameConstructAs(Me) ); // Enable all thrusters on the grid - foreach (IMyThrust thruster in thrusters) + foreach (IMyThrust thruster in _thrusters) { thruster.Enabled = true; } @@ -448,16 +445,14 @@ namespace IngameScript if (_naniteControlFacilityIntegration && !DoesGridHaveNaniteControlFacility()) { Echo("Turning on grid Nanite Beacons..."); - List naniteBeacons = new List(); - // Get all nanite beacons on the grid GridTerminalSystem.GetBlocksOfType( - naniteBeacons, + _naniteBeacons, naniteBeacon => naniteBeacon.CustomName.Contains(_naniteBeaconString) ); // Enable all nanite beacons on the grid - foreach (IMyFunctionalBlock naniteBeacon in naniteBeacons) + foreach (IMyFunctionalBlock naniteBeacon in _naniteBeacons) { naniteBeacon.Enabled = true; } @@ -486,12 +481,11 @@ namespace IngameScript else if (string.IsNullOrEmpty(argument)) { Echo("Polling..."); - List connectors = new List(); - + ; // Get all connectors on the grid that are managed by the script Echo($"Retrieving connectors, filtering by \"{_connectorString}\"..."); GridTerminalSystem.GetBlocksOfType( - connectors, + _connectors, connector => connector.IsSameConstructAs(Me) && connector.CustomName.Contains(_connectorString) @@ -499,7 +493,7 @@ namespace IngameScript if (_isDocked == true) { - foreach (IMyShipConnector connector in connectors) + foreach (IMyShipConnector connector in _connectors) { if (connector.IsConnected == true) { @@ -518,7 +512,7 @@ namespace IngameScript } else if (_isDocked == false) { - foreach (IMyShipConnector connector in connectors) + foreach (IMyShipConnector connector in _connectors) { if (connector.IsConnected == true) {