using System; using System.Collections; using System.Collections.Generic; using System.Collections.Immutable; using System.Linq; using System.Text; using Sandbox.Game.EntityComponents; using Sandbox.ModAPI.Ingame; using Sandbox.ModAPI.Interfaces; using SpaceEngineers.Game.ModAPI.Ingame; using VRage; using VRage.Collections; using VRage.Game; using VRage.Game.Components; using VRage.Game.GUI.TextPanel; using VRage.Game.ModAPI.Ingame; using VRage.Game.ModAPI.Ingame.Utilities; using VRage.Game.ObjectBuilders.Definitions; using VRageMath; namespace IngameScript { partial class Program : MyGridProgram { MyIni _configIni = new MyIni(); bool _doPolling; string _connectorPrefix; bool _manageGyroscopes; bool _rechargeBatteries; bool _stockpileGasTanks; bool _manageThrusters; bool _enableAllThrustersOnUndock; bool _manageManueveringThrusters; string _maneuveringThrusterPrefix; MyIni _storageIni = new MyIni(); bool _isDocked; public Program() { Echo("Loading storage..."); // Parse storage configuration MyIniParseResult storageResult; if (!_storageIni.TryParse(Storage, out storageResult)) { throw new Exception( $"Encountered error attempting to parse storage: {storageResult}" ); } _isDocked = _storageIni.Get("AutomaticConnectorActions", "Is_Docked").ToBoolean(); Echo("Loading configuration..."); // Parse CustomData configuration MyIniParseResult configResult; if (!_configIni.TryParse(Me.CustomData, out configResult)) { throw new Exception( $"Encountered error attempting to parse CustomData: {configResult}" ); } // Check if the CustomData configuration contains the AutomaticConnectorActions section if (!_configIni.ContainsSection("AutomaticConnectorActions")) { Echo( "CustomData does not contain the AutomaticConnectorActions section. Adding..." ); // Write configuration to CustomData _configIni.AddSection("AutomaticConnectorActions"); _configIni.Set("AutomaticConnectorActions", "Polling", true); _configIni.Set("AutomaticConnectorActions", "Connector_Prefix", ""); _configIni.Set("AutomaticConnectorActions", "Manage_Gyroscopes", true); _configIni.Set("AutomaticConnectorActions", "Recharge_Batteries", true); _configIni.Set("AutomaticConnectorActions", "Stockpile_Gas_Tanks", true); _configIni.Set("AutomaticConnectorActions", "Manage_Thrusters", true); _configIni.Set( "AutomaticConnectorActions", "Enable_All_Thrusters_On_Undock", false ); _configIni.Set("AutomaticConnectorActions", "Manage_Maneuvering_Thrusters", true); _configIni.Set( "AutomaticConnectorActions", "Maneuvering_Thruster_Prefix", "[MANEUVERING]" ); _configIni.SetComment("AutomaticConnectorActions", "Polling", "Enable polling"); _configIni.SetComment( "AutomaticConnectorActions", "Connector_Prefix", "Connector prefix" ); _configIni.SetComment( "AutomaticConnectorActions", "Manage_Gyroscopes", "Manage gyroscopes" ); _configIni.SetComment( "AutomaticConnectorActions", "Recharge_Batteries", "Recharge batteries" ); _configIni.SetComment( "AutomaticConnectorActions", "Stockpile_Gas_Tanks", "Stockpile gas tanks" ); _configIni.SetComment( "AutomaticConnectorActions", "Manage_Thrusters", "Manage thrusters" ); _configIni.SetComment( "AutomaticConnectorActions", "Enable_All_Thrusters_On_Undock", "Enable all thrusters on undock" ); _configIni.SetComment( "AutomaticConnectorActions", "Manage_Maneuvering_Thrusters", "Manage maneuvering thrusters" ); _configIni.SetComment( "AutomaticConnectorActions", "Maneuvering_Thruster_Prefix", "Prefix for maneuvering thrusters" ); Me.CustomData = _configIni.ToString(); } // Set up the script's configuration _doPolling = _configIni.Get("AutomaticConnectorActions", "Polling").ToBoolean(); _connectorPrefix = _configIni .Get("AutomaticConnectorActions", "Connector_Prefix") .ToString(); _manageGyroscopes = _configIni .Get("AutomaticConnectorActions", "Manage_Gyroscopes") .ToBoolean(); _rechargeBatteries = _configIni .Get("AutomaticConnectorActions", "Recharge_Batteries") .ToBoolean(); _stockpileGasTanks = _configIni .Get("AutomaticConnectorActions", "Stockpile_Gas_Tanks") .ToBoolean(); _manageThrusters = _configIni .Get("AutomaticConnectorActions", "Manage_Thrusters") .ToBoolean(); _enableAllThrustersOnUndock = _configIni .Get("AutomaticConnectorActions", "Enable_All_Thrusters_On_Undock") .ToBoolean(); _manageManueveringThrusters = _configIni .Get("AutomaticConnectorActions", "Manage_Maneuvering_Thrusters") .ToBoolean(); _maneuveringThrusterPrefix = _configIni .Get("AutomaticConnectorActions", "Maneuvering_Thruster_Prefix") .ToString(); Echo("Configuration loaded!"); if (_doPolling == true) { Echo("Polling enabled. Script will run every 10 ticks."); Runtime.UpdateFrequency = UpdateFrequency.Update10; } else { Echo("Polling disabled. Script will only run manually."); Runtime.UpdateFrequency = UpdateFrequency.None; } } public void Save(bool isDocked) { _isDocked = isDocked; _storageIni.Clear(); _storageIni.Set("AutomaticConnectorActions", "Is_Docked", isDocked); Storage = _storageIni.ToString(); } private void Dock() { if (_manageThrusters == true) { Echo("Turning off grid thrusters..."); List thrusters = new List(); // Get all thrusters on the grid GridTerminalSystem.GetBlocksOfType( thrusters, thruster => thruster.IsSameConstructAs(Me) ); // Disable all thrusters on the grid foreach (IMyThrust thruster in thrusters) { thruster.Enabled = false; } Echo("Turned off grid thrusters."); } if (_manageGyroscopes == true) { Echo("Turning off grid gyroscopes..."); List gyroscopes = new List(); // Get all gyroscopes on the grid GridTerminalSystem.GetBlocksOfType( gyroscopes, gyroscope => gyroscope.IsSameConstructAs(Me) ); // Disable all gyroscopes on the grid foreach (IMyGyro gyro in gyroscopes) { gyro.Enabled = false; } Echo("Turned off grid gyroscopes."); } if (_stockpileGasTanks == true) { Echo("Setting grid gas tanks to stockpile..."); List gasTanks = new List(); // Get all gas tanks on the grid GridTerminalSystem.GetBlocksOfType( gasTanks, gasTank => gasTank.IsSameConstructAs(Me) ); // Stockpile all gas tanks on the grid foreach (IMyGasTank gasTank in gasTanks) { gasTank.Stockpile = true; } Echo("Set grid gas tanks to stockpile."); } if (_rechargeBatteries == true) { Echo("Setting grid batteries to recharge..."); List batteries = new List(); // Get all batteries on the grid GridTerminalSystem.GetBlocksOfType( batteries, battery => battery.IsSameConstructAs(Me) ); foreach (IMyBatteryBlock battery in batteries) { battery.ChargeMode = ChargeMode.Recharge; } Echo("Set grid batteries to recharge."); } Save(true); } private void Undock() { if (_rechargeBatteries == true) { Echo("Setting grid batteries to auto..."); List batteries = new List(); // Get all batteries on the grid GridTerminalSystem.GetBlocksOfType( batteries, battery => battery.IsSameConstructAs(Me) ); // Set all batteries on the grid to auto foreach (IMyBatteryBlock battery in batteries) { battery.ChargeMode = ChargeMode.Auto; } Echo("Set grid batteries to auto."); } 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, gasTank => gasTank.IsSameConstructAs(Me) ); // Disable stockpiling on all gas tanks on the grid foreach (IMyGasTank gasTank in gasTanks) { gasTank.Stockpile = false; } Echo("Set grid gas tanks to not stockpile."); } if (_manageGyroscopes == true) { Echo("Turning on grid gyroscopes..."); List gyroscopes = new List(); // Get all gyroscopes on the grid GridTerminalSystem.GetBlocksOfType( gyroscopes, gyroscope => gyroscope.IsSameConstructAs(Me) ); // Enable all gyroscopes on the grid foreach (IMyGyro gyro in gyroscopes) { gyro.Enabled = true; } Echo("Turned on grid gyroscopes."); } 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, maneuveringThruster => maneuveringThruster.IsSameConstructAs(Me) && maneuveringThruster.CustomName.Contains(_maneuveringThrusterPrefix) ); // Enable all maneuvering thrusters on the grid foreach (IMyThrust maneuveringThruster in maneuveringThrusters) { maneuveringThruster.Enabled = true; } Echo("Turned on grid maneuvering thrusters."); } if (_enableAllThrustersOnUndock == true) { Echo("Turning on grid thrusters..."); List thrusters = new List(); // Get all thrusters on the grid GridTerminalSystem.GetBlocksOfType( thrusters, thruster => thruster.IsSameConstructAs(Me) ); // Enable all thrusters on the grid foreach (IMyThrust thruster in thrusters) { thruster.Enabled = true; } Echo("Turned on grid thrusters."); } Save(false); } public void Main(string argument, UpdateType updateSource) { if (argument == "dock") { Echo("Script ran manually. Docking..."); Dock(); Echo("Docking complete!"); } else if (argument == "undock") { Echo("Script ran manually. Undocking..."); Undock(); Echo("Undocking complete!"); } else if (string.IsNullOrEmpty(argument)) { Echo("Polling..."); List connectors = new List(); // Get all connectors on the grid GridTerminalSystem.GetBlocksOfType( connectors, connector => connector.IsSameConstructAs(Me) && connector.CustomName.Contains(_connectorPrefix) ); if (_isDocked == true) { foreach (IMyShipConnector connector in connectors) { if (connector.IsConnected == _isDocked) { return; } } Undock(); } } } } }