1
using Sandbox
.Game
.EntityComponents
;
2 using Sandbox
.ModAPI
.Ingame
;
3 using Sandbox
.ModAPI
.Interfaces
;
5 using SpaceEngineers
.Game
.ModAPI
.Ingame
;
8 using System
.Collections
;
9 using System
.Collections
.Generic
;
10 using System
.Collections
.Immutable
;
15 using VRage
.Collections
;
17 using VRage
.Game
.Components
;
18 using VRage
.Game
.GUI
.TextPanel
;
19 using VRage
.Game
.ModAPI
.Ingame
;
20 using VRage
.Game
.ModAPI
.Ingame
.Utilities
;
21 using VRage
.Game
.ObjectBuilders
.Definitions
;
25 namespace IngameScript
27 partial class Program
: MyGridProgram
36 const float EPSILON
= 0.05f;
37 const float EPSILON_RAD
= (float)(Math
.PI
/ 64);
39 State currentState
= State
.DEFAULT
;
41 IMyMotorAdvancedStator rotor01
;
42 IMyMotorAdvancedStator rotor02
;
43 IMyMotorAdvancedStator hinge01
;
44 IMyMotorAdvancedStator hinge02
;
45 IMyMotorAdvancedStator hinge03
;
46 IMyExtendedPistonBase piston01
;
48 List
<IMyExtendedPistonBase
> pistons
= new List
<IMyExtendedPistonBase
>();
49 List
<string> pistonsNames
= new List
<string> { "Piston 02", "Piston 03", "Piston 04", "Piston 05", "Piston 06", "Piston 07", };
51 IMyLandingGear magneticPlateArm
;
52 IMyLandingGear lockPlate
;
53 IMyExtendedPistonBase lockPiston
;
55 IMyTextSurface output
;
57 public void Print(string text
)
59 this.output
.WriteText((this.output
.GetText() != "" ? Environment
.NewLine
: "") + text
, true);
62 public void PrintError(string text
)
64 this.Print($"Error: {text}");
69 this.output
= this.Me
.GetSurface(0);
70 this.output
.ContentType
= ContentType
.TEXT_AND_IMAGE
;
71 this.output
.WriteText("");
73 this.Me
.CustomData
= "Packing";
75 this.Print("Mining rover system starting...");
77 var stators
= new List
<IMyMotorAdvancedStator
>();
78 GridTerminalSystem
.GetBlocksOfType(stators
);
80 foreach (var s
in stators
)
82 if (s
.DisplayNameText
.Contains("Rotor 01"))
84 else if (s
.DisplayNameText
.Contains("Rotor 02"))
86 else if (s
.DisplayNameText
.Contains("Hinge 01"))
88 else if (s
.DisplayNameText
.Contains("Hinge 02"))
90 else if (s
.DisplayNameText
.Contains("Hinge 03"))
94 var pistons
= new List
<IMyExtendedPistonBase
>();
95 GridTerminalSystem
.GetBlocksOfType(pistons
);
97 foreach (var p
in pistons
)
99 if (p
.DisplayNameText
.Contains("Piston 01"))
103 else if (p
.DisplayNameText
.Contains("Lock Piston"))
109 foreach (var name
in pistonsNames
)
111 if (p
.DisplayNameText
.Contains(name
))
120 var magneticPlates
= new List
<IMyLandingGear
>();
121 GridTerminalSystem
.GetBlocksOfType(magneticPlates
);
122 foreach (var magneticPlate
in magneticPlates
)
124 if (magneticPlate
.DisplayNameText
.Contains("Magnetic Plate arm"))
126 this.magneticPlateArm
= magneticPlate
;
128 else if (magneticPlate
.DisplayNameText
.Contains("Lock Plate"))
130 this.lockPlate
= magneticPlate
;
134 if (this.rotor01
== null)
136 this.PrintError("Can't find 'Rotor 01'");
140 if (this.rotor02
== null)
142 this.PrintError("Can't find 'Rotor 02'");
146 if (this.hinge01
== null)
148 this.PrintError("Can't find 'Hinge 01'");
152 if (this.hinge02
== null)
154 this.PrintError("Can't find 'Hinge 02'");
158 if (this.magneticPlateArm
== null)
160 this.PrintError("Can't find 'Magnetic Plate arm'");
164 if (this.lockPlate
== null)
166 this.PrintError("Can't find 'Lock Plate'");
170 if (this.piston01
== null)
172 this.PrintError("Can't find 'Piston 01'");
176 if (this.lockPiston
== null)
178 this.PrintError("Can't find 'Lock Piston'");
183 this.Runtime
.UpdateFrequency
= UpdateFrequency
.Update100
;
185 this.Print("Mining rover system has started");
193 /// Always returns an angle between 0 and pi/2.
195 /// <param name="a1"></param>
196 /// <param name="a2"></param>
197 /// <returns></returns>
198 float SumAngles(float a1
, float a2
)
200 var r
= (float)((a1
+ a2
) % (2 * Math
.PI
));
203 return (float)(r
+ 2 * Math
.PI
);
208 // Return 'true' if the rotor is in place.
209 bool SetRotorVelocity(float targetAngle
, IMyMotorAdvancedStator rotor
, float tolerance
= (float)(Math
.PI
/ 64))
217 if (this.currentState
== State
.UNPACKING
)
219 if (!this.piston01
.Enabled
)
221 this.piston01
.Enabled
= true;
222 foreach (var p
in this.pistons
)
226 bool finished
= true;
227 //bool roverLocked = false;
229 if (this.magneticPlateArm
.IsLocked
)
230 this.magneticPlateArm
.Unlock();
232 this.rotor01
.TargetVelocityRPM
= 0.0f;
234 if (this.lockPiston
.CurrentPosition
<= EPSILON
)
237 this.lockPiston
.Velocity
= 0.5f;
239 else if (this.lockPiston
.CurrentPosition
>= this.lockPiston
.MaxLimit
- EPSILON
)
241 //roverLocked = true;
242 if (!this.lockPlate
.IsLocked
)
243 this.lockPlate
.Lock();
244 this.lockPiston
.Velocity
= 0.0f;
247 if (this.hinge01
.Angle
<= -Math
.PI
/ 8)
249 this.hinge01
.TargetVelocityRPM
= 0.0f;
251 else // if (roverLocked)
253 this.hinge01
.TargetVelocityRPM
= -1.5f;
257 if (this.hinge02
.Angle
<= -Math
.PI
/ 3)
259 this.hinge02
.TargetVelocityRPM
= 0.0f;
261 else // if (roverLocked)
263 this.hinge02
.TargetVelocityRPM
= -1.5f;
267 if (this.piston01
.CurrentPosition
<= this.piston01
.MaxLimit
- EPSILON
/*&& roverLocked*/)
269 this.piston01
.Velocity
= 1.0f;
274 this.piston01
.Velocity
= 0.0f;
279 this.rotor01
.RotorLock
= false;
280 this.currentState
= State
.DEFAULT
;
281 this.Print("Unpacking terminated");
284 else if (this.currentState
== State
.PACKING
)
286 bool finished
= true;
288 var rotor01InPlace
= this.rotor01
.Angle
>= 2 * Math
.PI
- EPSILON_RAD
|| this.rotor01
.Angle
<= EPSILON_RAD
;
291 this.rotor01
.RotorLock
= true;
292 this.rotor01
.TargetVelocityRPM
= 0.0f;
294 else if (this.rotor01
.Angle
>= Math
.PI
)
296 this.rotor01
.TargetVelocityRPM
= 1.0f;
299 else // if (this.rotor01.Angle <= Math.PI)
301 this.rotor01
.TargetVelocityRPM
= -1.0f;
305 //this.Print($"Hinge 01 angle: {this.hinge01.Angle}");
306 if (this.hinge01
.Angle
<= Math
.PI
/ 4.0 || rotor01InPlace
&& this.hinge01
.Angle
<= Math
.PI
/ 2.0 - Math
.PI
/ 32.0)
308 this.hinge01
.TargetVelocityRPM
= 1.5f;
313 this.hinge01
.TargetVelocityRPM
= 0f;
316 if (this.hinge02
.Angle
<= -Math
.PI
/ 32.0)
318 this.hinge02
.TargetVelocityRPM
= 1.5f;
323 this.hinge02
.TargetVelocityRPM
= 0f;
326 if (this.piston01
.CurrentPosition
>= EPSILON
)
328 this.piston01
.Velocity
= -1.0f;
333 this.piston01
.Velocity
= 0.0f;
336 //var rotor2TargetAngle = 2 * Math.PI - Math.PI / 4; // 315°.
340 if (!this.magneticPlateArm
.IsLocked
)
341 this.magneticPlateArm
.Lock();
343 if (this.lockPlate
.IsLocked
)
345 this.lockPlate
.Unlock();
346 this.lockPiston
.Velocity
= -0.5f;
349 this.piston01
.Enabled
= false;
350 foreach (var p
in this.pistons
)
353 this.currentState
= State
.DEFAULT
;
354 this.Print("Packing terminated");
366 enum ElevationDirection
372 const double SPEED_ROTATION = 1.0;
373 const double SPEED_HINGE = 1.0;
374 const double SPEED_EXTEND_1 = 1.0;
375 const double SPEED_EXTEND_2 = 1.0;
377 void Rotate(RotateDirection direction)
382 void ChangeElevation(ElevationDirection direction)
392 public void Main(string argument
, UpdateType updateSource
)
394 if ((updateSource
& UpdateType
.Update100
) != 0)
398 else if ((updateSource
& (UpdateType
.Terminal
| UpdateType
.Trigger
)) != 0)
403 this.Print("Unpacking...");
404 this.currentState
= State
.UNPACKING
;
408 this.Print("Packing...");
409 this.currentState
= State
.PACKING
;
413 this.currentState
= State
.DEFAULT
;
417 case "LOCK OR UNLOCK":
422 ChangeElevation(ElevationDirection.DOWN);
426 ChangeElevation(ElevationDirection.UP);
430 Rotate(RotateDirection.LEFT);
434 Rotate(RotateDirection.RIGHT);
438 Echo($"Uknown command: {argument}");