bb89dc56ef807e372e04abf2a8fe2d47b46a4748
[SEScripts.git] / BaseMiner / Program.cs
1 using Sandbox.Game.EntityComponents;
2 using Sandbox.ModAPI.Ingame;
3 using Sandbox.ModAPI.Interfaces;
4
5 using SpaceEngineers.Game.ModAPI.Ingame;
6
7 using System;
8 using System.Collections;
9 using System.Collections.Generic;
10 using System.Collections.Immutable;
11 using System.Linq;
12 using System.Text;
13
14 using VRage;
15 using VRage.Collections;
16 using VRage.Game;
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;
22
23 using VRageMath;
24
25 namespace IngameScript
26 {
27 partial class Program : MyGridProgram
28 {
29 const int CONSOLE_NB_LINES = 7;
30 const string GRID_PREFIX = "[Base]";
31
32 const float PISTON_SPEED = 0.05f;
33 enum State
34 {
35 NORMAL,
36 MINING,
37 STOPPING_MINING,
38 }
39
40 const float EPSILON = 0.05f;
41
42 State currentState = State.NORMAL;
43
44 readonly Output output;
45
46 List<IMyExtendedPistonBase> pistons = new List<IMyExtendedPistonBase>();
47
48 IMyLandingGear magneticPlate;
49 List<IMyShipDrill> drills = new List<IMyShipDrill>();
50
51 IMySoundBlock soundAlert;
52 List<IMyLightingBlock> rotatingLights = new List<IMyLightingBlock>();
53
54 IMyShipConnector minerConnector;
55
56 public Program()
57 {
58 var output = this.Me.GetSurface(0);
59 this.output = new Output(output, CONSOLE_NB_LINES);
60
61 this.output.Print("Base mining system starting...");
62
63 this.GridTerminalSystem.GetBlocksOfType(
64 this.drills,
65 (IMyShipDrill drill) => drill.CustomName.StartsWith(GRID_PREFIX)
66 );
67
68 this.output.Print($"Nb of drills: {this.drills.Count}");
69
70 this.GridTerminalSystem.GetBlocksOfType(
71 this.pistons,
72 (IMyExtendedPistonBase piston) => piston.CustomName.StartsWith(GRID_PREFIX) && piston.CustomName.Contains("Miner")
73 );
74
75 this.output.Print($"Nb of pistons: {this.pistons.Count}");
76
77 this.magneticPlate = this.GridTerminalSystem.GetBlockWithName("[Base] Magnetic Plate Miner") as IMyLandingGear;
78
79 this.soundAlert = this.GridTerminalSystem.GetBlockWithName("[Base] Sound Block Miner") as IMySoundBlock;
80 if (this.soundAlert == null)
81 this.output.Print($"Error: sound alert system not found");
82
83 this.GridTerminalSystem.GetBlocksOfType(
84 this.rotatingLights,
85 (IMyLightingBlock light) => light.CustomName.StartsWith(GRID_PREFIX) && light.CustomName.Contains("Miner")
86 );
87
88 this.output.Print($"Nb of rotating lights: {this.rotatingLights.Count}");
89
90 this.minerConnector = this.GridTerminalSystem.GetBlockWithName("[Base] Connector Miner") as IMyShipConnector;
91 if (this.minerConnector == null)
92 this.output.Print($"Error: miner connector not found");
93
94 this.Runtime.UpdateFrequency = UpdateFrequency.Update100;
95
96 this.output.Print("base Mining system has started");
97 }
98
99 public void Save()
100 {
101 }
102
103 void UpdateState()
104 {
105 if (this.currentState == State.STOPPING_MINING)
106 {
107 bool finished = true;
108
109 foreach (var d in this.drills)
110 {
111 d.Enabled = false;
112 }
113
114 foreach (var p in this.pistons)
115 {
116 var distanceDocked = 0.0;
117
118 if (p.CustomName == "[Base] Piston Miner 02")
119 distanceDocked = 0.1;
120
121 if (p.CurrentPosition > distanceDocked + EPSILON)
122 {
123 finished = false;
124 p.Velocity = 5 * -PISTON_SPEED;
125 }
126 else
127 {
128 p.Velocity = 0.0f;
129 }
130 }
131
132 if (finished)
133 {
134 foreach (var p in this.pistons)
135 p.Enabled = false;
136
137 foreach (var l in this.rotatingLights)
138 l.Enabled = false;
139
140 this.soundAlert.Stop();
141
142 this.output.Print("Drills parked, operation terminated");
143 this.currentState = State.NORMAL;
144 }
145 }
146 else if (this.currentState == State.MINING)
147 {
148 this.magneticPlate.Unlock();
149
150 foreach (var p in this.pistons)
151 {
152 p.Enabled = true;
153 p.Velocity = PISTON_SPEED;
154 }
155
156 foreach (var d in this.drills)
157 {
158 d.Enabled = true;
159 }
160 }
161
162 // Send miner connector position.
163 this.IGC.SendBroadcastMessage("POSITION_CONNECTOR_MINER", this.minerConnector.WorldMatrix);
164 }
165
166 public void Main(string argument, UpdateType updateSource)
167 {
168 if ((updateSource & UpdateType.Update100) != 0)
169 {
170 this.UpdateState();
171 }
172 else if ((updateSource & (UpdateType.Terminal | UpdateType.Trigger)) != 0)
173 {
174 switch (argument)
175 {
176 case "STOP_MINING":
177 this.output.Print("Stopping mining...");
178 this.currentState = State.STOPPING_MINING;
179 break;
180
181 case "START_MINING":
182 this.soundAlert.Play();
183 foreach (var l in this.rotatingLights)
184 l.Enabled = true;
185
186 this.output.Print("Mining in progress...");
187 this.currentState = State.MINING;
188 break;
189
190 default:
191 this.output.Print($"Uknown command: {argument}");
192 break;
193 }
194 }
195 }
196 }
197 }
198