space_engineers/docking.cs
2025-02-06 19:02:13 -05:00

88 lines
2.6 KiB
C#

// Docking controller script; handles docking and undocking.
// Specifically, when you activate the script it will:
// * Connect any Connectors whose names start with "Docking Port" that are ready to connect.
// * Turn off all engines.
// * Set all O2 and Hydrogen tanks to Stockpile.
// * Set all batteries to Recharge.
//
// When you activate the script with "-undock" it will reverse all of these actions.
//
// TODO: None of the below switches work yet.
// You can selectively disable some functionality with switches.
// * "-no-refuel" to disable refueling
// * "-no-recharge" to disable battery recharge
// (batteries will be left in whatever state they are already in)
MyCommandLine cli;
List<IMyShipConnector> dockingPorts;
List<IMyThrust> thrusters;
List<IMyBatteryBlock> batteries;
List<IMyGasTank> tanks;
public void Program() {
cli = new MyCommandLine();
dockingPorts = new List<IMyShipConnector>();
List<IMyShipConnector> allConnectors = new List<IMyShipConnector>();
foreach(IMyShipConnector connector in allConnectors) {
if (connector.CustomName.StartsWith("Docking Port")) {
dockingPorts.Add(connector);
}
}
thrusters = new List<IMyThrust>();
GridTerminalSystem.GetBlocksOfType(thrusters);
batteries = new List<IMyBatteryBlock>();
GridTerminalSystem.GetBlocksOfType(batteries);
tanks = new List<IMyGasTank>();
GridTerminalSystem.GetBlocksOfType(tanks);
}
public void Main(string argument, UpdateType updateSource) {
cli.TryParse(argument);
if (cli.Switch("undock")) Undock();
else Dock();
}
private void Dock() {
foreach (IMyShipConnector dockingPort in dockingPorts) {
if (dockingPort.Status == MyShipConnectorStatus.Connectable) {
dockingPort.Connect();
}
}
foreach (IMyThrust thruster in thrusters) {
thruster.Enabled = false;
}
foreach (IMyBatteryBlock battery in batteries) {
battery.ChargeMode = ChargeMode.Recharge;
}
foreach (IMyGasTank tank in tanks) {
tank.Stockpile = true;
}
}
private void Undock() {
foreach (IMyBatteryBlock battery in batteries) {
battery.ChargeMode = ChargeMode.Recharge;
}
foreach (IMyGasTank tank in tanks) {
tank.Stockpile = true;
}
foreach (IMyThrust thruster in thrusters) {
thruster.Enabled = true;
}
foreach (IMyShipConnector dockingPort in dockingPorts) {
if (dockingPort.Status == MyShipConnectorStatus.Connected) {
dockingPort.Disconnect();
}
}
}