AutomaticConnectorActions/Program.cs
2024-11-07 19:54:14 -05:00

419 lines
15 KiB
C#

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",
"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_Prefix",
"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_Prefix",
"If the maneuvering thruster'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();
_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()
{
_storageIni.Clear();
_storageIni.Set("AutomaticConnectorActions", "Is_Docked", _isDocked);
Storage = _storageIni.ToString();
}
private void Dock()
{
if (_manageThrusters == true)
{
Echo("Turning off grid thrusters...");
List<IMyThrust> thrusters = new List<IMyThrust>();
// 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<IMyGyro> gyroscopes = new List<IMyGyro>();
// 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<IMyGasTank> gasTanks = new List<IMyGasTank>();
// 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<IMyBatteryBlock> batteries = new List<IMyBatteryBlock>();
// 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.");
}
_isDocked = true;
Save();
}
private void Undock()
{
if (_rechargeBatteries == true)
{
Echo("Setting grid batteries to auto...");
List<IMyBatteryBlock> batteries = new List<IMyBatteryBlock>();
// 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<IMyGasTank> gasTanks = new List<IMyGasTank>();
// 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<IMyGyro> gyroscopes = new List<IMyGyro>();
// 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<IMyThrust> maneuveringThrusters = new List<IMyThrust>();
// 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<IMyThrust> thrusters = new List<IMyThrust>();
// 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.");
}
_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...");
List<IMyShipConnector> connectors = new List<IMyShipConnector>();
// 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();
}
}
else
{
Echo("Invalid argument.");
}
}
}
}