clean up a bunch of stuff

This commit is contained in:
Seaswimmer 2024-11-11 12:54:46 -05:00
parent b25f88cee0
commit 0f7875d7cd
Signed by: cswimr
GPG key ID: D68E1EAD0F97196D

View file

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