AutomaticConnectorActions/Program.cs

539 lines
21 KiB
C#
Raw Permalink Normal View History

2024-11-07 17:55:36 -05:00
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;
2024-11-09 22:34:31 -05:00
string _connectorString;
2024-11-07 17:55:36 -05:00
bool _manageGyroscopes;
bool _rechargeBatteries;
bool _stockpileGasTanks;
bool _manageThrusters;
bool _enableAllThrustersOnUndock;
bool _manageManueveringThrusters;
2024-11-09 22:34:31 -05:00
string _maneuveringThrusterString;
bool _naniteControlFacilityIntegration;
string _naniteBeaconString;
string _naniteControlFacilityString;
2024-11-07 17:55:36 -05:00
MyIni _storageIni = new MyIni();
bool _isDocked;
2024-11-11 12:54:46 -05:00
List<IMyShipConnector> _connectors = new List<IMyShipConnector>();
List<IMyShipWelder> _naniteControlFacilities = new List<IMyShipWelder>();
List<IMyUpgradeModule> _naniteBeacons = new List<IMyUpgradeModule>();
List<IMyGyro> _gyroscopes = new List<IMyGyro>();
List<IMyThrust> _thrusters = new List<IMyThrust>();
List<IMyGasTank> _gasTanks = new List<IMyGasTank>();
List<IMyBatteryBlock> _batteries = new List<IMyBatteryBlock>();
2024-11-07 17:55:36 -05:00
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);
2024-11-09 22:34:31 -05:00
_configIni.Set("AutomaticConnectorActions", "Connector_String", "");
2024-11-07 17:55:36 -05:00
_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",
2024-11-09 22:34:31 -05:00
"Maneuvering_Thruster_String",
2024-11-07 17:55:36 -05:00
"[MANEUVERING]"
);
2024-11-09 22:34:31 -05:00
_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."
);
2024-11-07 17:55:36 -05:00
_configIni.SetComment(
"AutomaticConnectorActions",
2024-11-09 22:34:31 -05:00
"Connector_String",
"If the connector's name contains this string, the script will manage it."
2024-11-07 17:55:36 -05:00
);
_configIni.SetComment(
"AutomaticConnectorActions",
"Manage_Gyroscopes",
"If this is enabled, the script will disable and enable gyroscopes when docking and undocking, respectively."
2024-11-07 17:55:36 -05:00
);
_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."
2024-11-07 17:55:36 -05:00
);
_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."
2024-11-07 17:55:36 -05:00
);
_configIni.SetComment(
"AutomaticConnectorActions",
"Manage_Thrusters",
"If this is enabled, the script will disable all thrusters when docking."
2024-11-07 17:55:36 -05:00
);
_configIni.SetComment(
"AutomaticConnectorActions",
"Enable_All_Thrusters_On_Undock",
"If this is enabled, the script will enable all thrusters when undocking."
2024-11-07 17:55:36 -05:00
);
_configIni.SetComment(
"AutomaticConnectorActions",
"Manage_Maneuvering_Thrusters",
"If this is enabled, the script will disable and enable maneuvering thrusters when docking and undocking, respectively."
2024-11-07 17:55:36 -05:00
);
_configIni.SetComment(
"AutomaticConnectorActions",
2024-11-09 22:34:31 -05:00
"Maneuvering_Thruster_String",
"If the maneuvering thruster's name contains this string, the script will manage it."
2024-11-07 17:55:36 -05:00
);
2024-11-09 22:34:31 -05:00
_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."
);
2024-11-07 17:55:36 -05:00
Me.CustomData = _configIni.ToString();
}
// Set up the script's configuration
_doPolling = _configIni.Get("AutomaticConnectorActions", "Polling").ToBoolean();
2024-11-09 22:34:31 -05:00
_connectorString = _configIni
.Get("AutomaticConnectorActions", "Connector_String")
2024-11-07 17:55:36 -05:00
.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();
2024-11-09 22:34:31 -05:00
_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")
2024-11-07 17:55:36 -05:00
.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;
}
}
2024-11-07 19:54:14 -05:00
public void Save()
2024-11-07 17:55:36 -05:00
{
_storageIni.Clear();
2024-11-07 19:54:14 -05:00
_storageIni.Set("AutomaticConnectorActions", "Is_Docked", _isDocked);
2024-11-07 17:55:36 -05:00
Storage = _storageIni.ToString();
}
2024-11-09 22:34:31 -05:00
private bool DoesGridHaveNaniteControlFacility()
{
// Get all functional blocks on the grid that are Nanite Control Facilities
2024-11-11 12:54:46 -05:00
// Nanite Control Facilities are considered IMyShipWelder
2024-11-09 22:34:31 -05:00
GridTerminalSystem.GetBlocksOfType(
2024-11-11 12:54:46 -05:00
_naniteControlFacilities,
2024-11-09 22:34:31 -05:00
naniteControlFacility =>
naniteControlFacility.CustomName.Contains(_naniteControlFacilityString)
);
2024-11-11 12:54:46 -05:00
return _naniteControlFacilities.Count > 0;
2024-11-09 22:34:31 -05:00
}
2024-11-07 17:55:36 -05:00
private void Dock()
{
2024-11-11 12:54:46 -05:00
if (_isDocked == true)
{
Echo("Already docked.");
return;
}
2024-11-07 17:55:36 -05:00
if (_manageThrusters == true)
{
Echo("Turning off grid thrusters...");
// Get all thrusters on the grid
GridTerminalSystem.GetBlocksOfType(
2024-11-11 12:54:46 -05:00
_thrusters,
2024-11-07 17:55:36 -05:00
thruster => thruster.IsSameConstructAs(Me)
);
// Disable all thrusters on the grid
2024-11-11 12:54:46 -05:00
foreach (IMyThrust thruster in _thrusters)
2024-11-07 17:55:36 -05:00
{
thruster.Enabled = false;
}
Echo("Turned off grid thrusters.");
}
if (_manageGyroscopes == true)
{
Echo("Turning off grid gyroscopes...");
// Get all gyroscopes on the grid
GridTerminalSystem.GetBlocksOfType(
2024-11-11 12:54:46 -05:00
_gyroscopes,
2024-11-07 17:55:36 -05:00
gyroscope => gyroscope.IsSameConstructAs(Me)
);
// Disable all gyroscopes on the grid
2024-11-11 12:54:46 -05:00
foreach (IMyGyro gyro in _gyroscopes)
2024-11-07 17:55:36 -05:00
{
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(
2024-11-11 12:54:46 -05:00
_gasTanks,
2024-11-07 17:55:36 -05:00
gasTank => gasTank.IsSameConstructAs(Me)
);
// Stockpile all gas tanks on the grid
2024-11-11 12:54:46 -05:00
foreach (IMyGasTank gasTank in _gasTanks)
2024-11-07 17:55:36 -05:00
{
gasTank.Stockpile = true;
}
Echo("Set grid gas tanks to stockpile.");
}
if (_rechargeBatteries == true)
{
Echo("Setting grid batteries to recharge...");
2024-11-11 12:54:46 -05:00
;
2024-11-07 17:55:36 -05:00
// Get all batteries on the grid
GridTerminalSystem.GetBlocksOfType(
2024-11-11 12:54:46 -05:00
_batteries,
2024-11-07 17:55:36 -05:00
battery => battery.IsSameConstructAs(Me)
);
2024-11-11 12:54:46 -05:00
foreach (IMyBatteryBlock battery in _batteries)
2024-11-07 17:55:36 -05:00
{
battery.ChargeMode = ChargeMode.Recharge;
}
Echo("Set grid batteries to recharge.");
}
2024-11-09 22:34:31 -05:00
if (_naniteControlFacilityIntegration && DoesGridHaveNaniteControlFacility())
{
Echo("Turning off grid Nanite Beacons...");
// Get all nanite beacons on the grid
GridTerminalSystem.GetBlocksOfType(
2024-11-11 12:54:46 -05:00
_naniteBeacons,
2024-11-09 22:34:31 -05:00
naniteBeacon =>
naniteBeacon.IsSameConstructAs(Me)
&& naniteBeacon.CustomName.Contains(_naniteBeaconString)
);
// Disable all nanite beacons on the grid
2024-11-11 12:54:46 -05:00
foreach (IMyFunctionalBlock naniteBeacon in _naniteBeacons)
2024-11-09 22:34:31 -05:00
{
naniteBeacon.Enabled = false;
}
Echo("Turned off grid Nanite Beacons.");
}
2024-11-07 19:54:14 -05:00
_isDocked = true;
Save();
2024-11-07 17:55:36 -05:00
}
private void Undock()
{
2024-11-11 12:54:46 -05:00
if (_isDocked == false)
{
Echo("Already undocked.");
return;
}
2024-11-07 17:55:36 -05:00
if (_rechargeBatteries == true)
{
Echo("Setting grid batteries to auto...");
// Get all batteries on the grid
GridTerminalSystem.GetBlocksOfType(
2024-11-11 12:54:46 -05:00
_batteries,
2024-11-07 17:55:36 -05:00
battery => battery.IsSameConstructAs(Me)
);
// Set all batteries on the grid to auto
2024-11-11 12:54:46 -05:00
foreach (IMyBatteryBlock battery in _batteries)
2024-11-07 17:55:36 -05:00
{
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(
2024-11-11 12:54:46 -05:00
_gasTanks,
2024-11-07 17:55:36 -05:00
gasTank => gasTank.IsSameConstructAs(Me)
);
// Disable stockpiling on all gas tanks on the grid
2024-11-11 12:54:46 -05:00
foreach (IMyGasTank gasTank in _gasTanks)
2024-11-07 17:55:36 -05:00
{
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(
2024-11-11 12:54:46 -05:00
_gyroscopes,
2024-11-07 17:55:36 -05:00
gyroscope => gyroscope.IsSameConstructAs(Me)
);
// Enable all gyroscopes on the grid
2024-11-11 12:54:46 -05:00
foreach (IMyGyro gyro in _gyroscopes)
2024-11-07 17:55:36 -05:00
{
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(
2024-11-11 12:54:46 -05:00
_thrusters,
2024-11-07 17:55:36 -05:00
maneuveringThruster =>
maneuveringThruster.IsSameConstructAs(Me)
2024-11-09 22:34:31 -05:00
&& maneuveringThruster.CustomName.Contains(_maneuveringThrusterString)
2024-11-07 17:55:36 -05:00
);
// Enable all maneuvering thrusters on the grid
2024-11-11 12:54:46 -05:00
foreach (IMyThrust maneuveringThruster in _thrusters)
2024-11-07 17:55:36 -05:00
{
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(
2024-11-11 12:54:46 -05:00
_thrusters,
2024-11-07 17:55:36 -05:00
thruster => thruster.IsSameConstructAs(Me)
);
// Enable all thrusters on the grid
2024-11-11 12:54:46 -05:00
foreach (IMyThrust thruster in _thrusters)
2024-11-07 17:55:36 -05:00
{
thruster.Enabled = true;
}
Echo("Turned on grid thrusters.");
}
2024-11-09 22:34:31 -05:00
if (_naniteControlFacilityIntegration && !DoesGridHaveNaniteControlFacility())
{
Echo("Turning on grid Nanite Beacons...");
// Get all nanite beacons on the grid
GridTerminalSystem.GetBlocksOfType(
2024-11-11 12:54:46 -05:00
_naniteBeacons,
2024-11-09 22:34:31 -05:00
naniteBeacon => naniteBeacon.CustomName.Contains(_naniteBeaconString)
);
// Enable all nanite beacons on the grid
2024-11-11 12:54:46 -05:00
foreach (IMyFunctionalBlock naniteBeacon in _naniteBeacons)
2024-11-09 22:34:31 -05:00
{
naniteBeacon.Enabled = true;
}
Echo("Turned on grid Nanite Beacons.");
}
2024-11-07 19:54:14 -05:00
_isDocked = false;
Save();
2024-11-07 17:55:36 -05:00
}
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...");
2024-11-11 12:54:46 -05:00
;
// Get all connectors on the grid that are managed by the script
2024-11-09 22:34:31 -05:00
Echo($"Retrieving connectors, filtering by \"{_connectorString}\"...");
2024-11-07 17:55:36 -05:00
GridTerminalSystem.GetBlocksOfType(
2024-11-11 12:54:46 -05:00
_connectors,
2024-11-07 17:55:36 -05:00
connector =>
connector.IsSameConstructAs(Me)
2024-11-09 22:34:31 -05:00
&& connector.CustomName.Contains(_connectorString)
2024-11-07 17:55:36 -05:00
);
if (_isDocked == true)
{
2024-11-11 12:54:46 -05:00
foreach (IMyShipConnector connector in _connectors)
2024-11-07 17:55:36 -05:00
{
if (connector.IsConnected == true)
2024-11-07 17:55:36 -05:00
{
Echo($"Connector '{connector.CustomName}' is connected. Exiting.");
2024-11-07 17:55:36 -05:00
return;
}
else
{
Echo(
$"Connector '{connector.CustomName}' is not connected. Continuing."
);
}
2024-11-07 17:55:36 -05:00
}
Echo("No connectors are connected. Undocking...");
2024-11-07 17:55:36 -05:00
Undock();
}
else if (_isDocked == false)
{
2024-11-11 12:54:46 -05:00
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."
);
}
}
}
2024-11-07 17:55:36 -05:00
}
else
{
Echo("Invalid argument.");
}
2024-11-07 17:55:36 -05:00
}
}
}