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 _connectorString; bool _manageGyroscopes; bool _rechargeBatteries; bool _stockpileGasTanks; bool _manageThrusters; bool _enableAllThrustersOnUndock; bool _manageManueveringThrusters; string _maneuveringThrusterString; bool _naniteControlFacilityIntegration; string _naniteBeaconString; string _naniteControlFacilityString; 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..."); // 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_String", ""); _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_String", "[MANEUVERING]" ); _configIni.Set( "AutomaticConnectorActions", "Nanite_Control_Facility_Integration", true ); _configIni.Set( "AutomaticConnectorActions", "Nanite_Beacon_String", "Nanite Beacon" ); _configIni.Set( "AutomaticConnectorActions", "Nanite_Control_Facility_String", "Nanite Control Facility" ); _configIni.SetComment( "AutomaticConnectorActions", "Polling", "If Polling is enabled, the script will check for docked/undocked connectors every 10 ticks. Otherwise, the script will only run manually." ); _configIni.SetComment( "AutomaticConnectorActions", "Connector_String", "If the connector's name contains this string, the script will manage it." ); _configIni.SetComment( "AutomaticConnectorActions", "Manage_Gyroscopes", "If this is enabled, the script will disable and enable gyroscopes when docking and undocking, respectively." ); _configIni.SetComment( "AutomaticConnectorActions", "Recharge_Batteries", "If this is enabled, the script will set all batteries to recharge mode when docking and set them to auto mode when undocking." ); _configIni.SetComment( "AutomaticConnectorActions", "Stockpile_Gas_Tanks", "If this is enabled, the script will set all gas tanks to stockpile when docking and set them to not stockpile when undocking." ); _configIni.SetComment( "AutomaticConnectorActions", "Manage_Thrusters", "If this is enabled, the script will disable all thrusters when docking." ); _configIni.SetComment( "AutomaticConnectorActions", "Enable_All_Thrusters_On_Undock", "If this is enabled, the script will enable all thrusters when undocking." ); _configIni.SetComment( "AutomaticConnectorActions", "Manage_Maneuvering_Thrusters", "If this is enabled, the script will disable and enable maneuvering thrusters when docking and undocking, respectively." ); _configIni.SetComment( "AutomaticConnectorActions", "Maneuvering_Thruster_String", "If the maneuvering thruster's name contains this string, the script will manage it." ); _configIni.SetComment( "AutomaticConnectorActions", "Nanite_Control_Facility_Integration", "If this is enabled, the script will disable and enable Nanite Beacons from the Nanite Control Facility mod when docking and undocking, respectively. This setting exists to cut down on NCF scanning times." ); _configIni.SetComment( "AutomaticConnectorActions", "Nanite_Beacon_String", "If the Nanite Beacon's name contains this string, the script will manage it." ); _configIni.SetComment( "AutomaticConnectorActions", "Nanite_Control_Facility_String", "If the Nanite Control Facility's name contains this string, the script will manage it." ); Me.CustomData = _configIni.ToString(); } // Set up the script's configuration _doPolling = _configIni.Get("AutomaticConnectorActions", "Polling").ToBoolean(); _connectorString = _configIni .Get("AutomaticConnectorActions", "Connector_String") .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(); _maneuveringThrusterString = _configIni .Get("AutomaticConnectorActions", "Maneuvering_Thruster_String") .ToString(); _naniteControlFacilityIntegration = _configIni .Get("AutomaticConnectorActions", "Nanite_Control_Facility_Integration") .ToBoolean(); _naniteBeaconString = _configIni .Get("AutomaticConnectorActions", "Nanite_Beacon_String") .ToString(); _naniteControlFacilityString = _configIni .Get("AutomaticConnectorActions", "Nanite_Control_Facility_String") .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() { _storageIni.Clear(); _storageIni.Set("AutomaticConnectorActions", "Is_Docked", _isDocked); Storage = _storageIni.ToString(); } private bool DoesGridHaveNaniteControlFacility() { // Get all functional blocks on the grid that are Nanite Control Facilities // Nanite Control Facilities are considered IMyShipWelder GridTerminalSystem.GetBlocksOfType( _naniteControlFacilities, naniteControlFacility => naniteControlFacility.CustomName.Contains(_naniteControlFacilityString) ); return _naniteControlFacilities.Count > 0; } private void Dock() { if (_isDocked == true) { Echo("Already docked."); return; } if (_manageThrusters == true) { Echo("Turning off grid thrusters..."); // 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..."); // 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..."); // 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..."); ; // 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."); } if (_naniteControlFacilityIntegration && DoesGridHaveNaniteControlFacility()) { Echo("Turning off grid Nanite Beacons..."); // Get all nanite beacons on the grid GridTerminalSystem.GetBlocksOfType( _naniteBeacons, naniteBeacon => naniteBeacon.IsSameConstructAs(Me) && naniteBeacon.CustomName.Contains(_naniteBeaconString) ); // Disable all nanite beacons on the grid foreach (IMyFunctionalBlock naniteBeacon in _naniteBeacons) { naniteBeacon.Enabled = false; } Echo("Turned off grid Nanite Beacons."); } _isDocked = true; Save(); } private void Undock() { if (_isDocked == false) { Echo("Already undocked."); return; } if (_rechargeBatteries == true) { Echo("Setting grid batteries to auto..."); // 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..."); // 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..."); // 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..."); // Get all thrusters on the grid that are considered maneuvering thrusters GridTerminalSystem.GetBlocksOfType( _thrusters, maneuveringThruster => maneuveringThruster.IsSameConstructAs(Me) && maneuveringThruster.CustomName.Contains(_maneuveringThrusterString) ); // Enable all maneuvering thrusters on the grid foreach (IMyThrust maneuveringThruster in _thrusters) { maneuveringThruster.Enabled = true; } Echo("Turned on grid maneuvering thrusters."); } if (_enableAllThrustersOnUndock == true) { Echo("Turning on grid thrusters..."); // 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."); } if (_naniteControlFacilityIntegration && !DoesGridHaveNaniteControlFacility()) { Echo("Turning on grid Nanite Beacons..."); // Get all nanite beacons on the grid GridTerminalSystem.GetBlocksOfType( _naniteBeacons, naniteBeacon => naniteBeacon.CustomName.Contains(_naniteBeaconString) ); // Enable all nanite beacons on the grid foreach (IMyFunctionalBlock naniteBeacon in _naniteBeacons) { naniteBeacon.Enabled = true; } Echo("Turned on grid Nanite Beacons."); } _isDocked = false; Save(); } 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..."); ; // Get all connectors on the grid that are managed by the script Echo($"Retrieving connectors, filtering by \"{_connectorString}\"..."); GridTerminalSystem.GetBlocksOfType( _connectors, connector => connector.IsSameConstructAs(Me) && connector.CustomName.Contains(_connectorString) ); if (_isDocked == true) { foreach (IMyShipConnector connector in _connectors) { if (connector.IsConnected == true) { Echo($"Connector '{connector.CustomName}' is connected. Exiting."); return; } else { Echo( $"Connector '{connector.CustomName}' is not connected. Continuing." ); } } Echo("No connectors are connected. Undocking..."); Undock(); } else if (_isDocked == false) { foreach (IMyShipConnector connector in _connectors) { if (connector.IsConnected == true) { Echo($"Connector '{connector.CustomName}' is connected. Docking..."); Dock(); break; } else { Echo( $"Connector '{connector.CustomName}' is not connected. Continuing." ); } } } } else { Echo("Invalid argument."); } } } }