-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScript.cs
More file actions
151 lines (131 loc) · 5.5 KB
/
Script.cs
File metadata and controls
151 lines (131 loc) · 5.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
Space Engineers ship emergency stop script by drNovikov.
v0.51
Saves your ships if you accidentally get out of your cockpit or if you get disconnected from a server.
Does not rely on sensors.
How to use: have a looping timer run a programmable block with this script and also restart itself
every second. You can also run the script with the word "manual" as an argument if you want to stop
your ship by a quick key, button panel, sensor, timer, etc.
Additionally, you can place a timer, name it "TriggerOnShipControlLost", and use this timer to set
your own actions that need to be performed when ship control is lost.
Special note: scripts need at least one working cockpit, control station or remote control on a ship
in order to enable inertia dampeners. It's a good idea to have a remote control block or another
control station hidden away from your main cockpit in case the main one gets destroyed mid-flight.
If there are several blocks named "TriggerOnShipControlLost", the script will attempt to trigger
only the first one.
What does it do: as soon as the script detects there is no one in control of the ship or any of its
turrets, it enables inertia dampeners, turns on gyroscopes and thrusters, cancels their overrides.
Also, turns off gravity generators and artificial masses to prevent them from counteracting
the inertia dampener.
Thanks to Mustardman24 for the idea of enabling inertia dampeners by sensors and his script that
I initially used for my ships in conjunction with sensors, and later wrote my own smaller version with
a reliable way of detecting loss of control.
*/
void Main(string argument)
{
if ((!ShipIsControlled() && !TurretsAreControlled()) || argument == "manual")
{
EnableInertiaDampeners();
DisableThrusterOverrides();
DisableGyroscopeOverrides();
DisableGravityDrives(); // Comment this out if you don't have any gravity drives.
TriggerOnShipControlLost(); // Comment this out if you don't want to trigger the TriggerOnShipControlLost timer.
}
}
// drNovikov's famous pilot detector: checks is a ship is piloted. For example, if a pilot accidentally exits a ship, this could be used to engage inertia dampeners, turn on beacons, etc.
bool ShipIsControlled()
{
List<IMyTerminalBlock> shipControllers = new List<IMyTerminalBlock>();
GridTerminalSystem.GetBlocksOfType<IMyShipController>(shipControllers);
for (int i = 0; i < shipControllers.Count; i++)
{
if ((shipControllers[i] as IMyShipController).IsUnderControl)
{
return true;
}
}
return false;
}
bool TurretsAreControlled()
{
List<IMyTerminalBlock> turrets = new List<IMyTerminalBlock>();
GridTerminalSystem.GetBlocksOfType<IMyLargeTurretBase>(turrets);
for (int i = 0; i < turrets.Count; i++)
{
if ((turrets[i] as IMyLargeTurretBase).IsUnderControl)
{
return true;
}
}
return false;
}
// Triggering a timer named "TriggerOnShipControlLost"
void TriggerOnShipControlLost()
{
IMyTimerBlock triggerOnShipControlLost = (IMyTimerBlock)GridTerminalSystem.GetBlockWithName("TriggerOnShipControlLost");
if (triggerOnShipControlLost is IMyTimerBlock)
{
triggerOnShipControlLost.GetActionWithName("TriggerNow").Apply(triggerOnShipControlLost);
}
}
// drNovikov's gravity drive disabler for large ships with gravity drives
void DisableGravityDrives()
{
DisableGravityGenerators();
DisableArtificialMasses();
}
void DisableGravityGenerators()
{
List<IMyTerminalBlock> gravgens = new List<IMyTerminalBlock>();
GridTerminalSystem.GetBlocksOfType<IMyGravityGenerator>(gravgens);
for (int i = 0; i < gravgens.Count; i++)
{
gravgens[i].GetActionWithName("OnOff_Off").Apply(gravgens[i]);
}
}
void DisableArtificialMasses()
{
List<IMyTerminalBlock> artmasses = new List<IMyTerminalBlock>();
GridTerminalSystem.GetBlocksOfType<IMyVirtualMass>(artmasses);
for (int i = 0; i < artmasses.Count; i++)
{
artmasses[i].GetActionWithName("OnOff_Off").Apply(artmasses[i]);
}
}
// Inertia dampeners enabler, inspired by Mustardman24's script
void EnableInertiaDampeners()
{
List<IMyTerminalBlock> shipControllers = new List<IMyTerminalBlock>();
GridTerminalSystem.GetBlocksOfType<IMyShipController>(shipControllers);
for (int i = 0; i < shipControllers.Count; i++)
{
if ((shipControllers[i] as IMyShipController).DampenersOverride == false)
{
shipControllers[i].GetActionWithName("DampenersOverride").Apply(shipControllers[i]);
}
}
}
void DisableThrusterOverrides()
{
List<IMyTerminalBlock> thrusters = new List<IMyTerminalBlock>();
GridTerminalSystem.GetBlocksOfType<IMyThrust>(thrusters);
for (int i = 0; i < thrusters.Count; i++)
{
thrusters[i].GetActionWithName("OnOff_On").Apply(thrusters[i]);
thrusters[i].SetValueFloat("Override", 0);
}
}
void DisableGyroscopeOverrides()
{
List<IMyTerminalBlock> gyroscopes = new List<IMyTerminalBlock>();
GridTerminalSystem.GetBlocksOfType<IMyGyro>(gyroscopes);
for (int i = 0; i < gyroscopes.Count; i++)
{
gyroscopes[i].GetActionWithName("OnOff_On").Apply(gyroscopes[i]);
gyroscopes[i].SetValueFloat("Power", 1);
if ((gyroscopes[i] as IMyGyro).GyroOverride)
{
gyroscopes[i].GetActionWithName("Override").Apply(gyroscopes[i]);
}
}
}