Add missile launcher and missile controller projects
[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 system starting...");
62
63 this.InitMiningSystem();
64
65 this.minerConnector = this.GridTerminalSystem.GetBlockWithName("[Base] Connector Miner") as IMyShipConnector;
66 if (this.minerConnector == null)
67 this.output.Print($"Error: miner connector not found");
68
69 this.Runtime.UpdateFrequency = UpdateFrequency.Update100;
70
71 this.output.Print("Base system has started");
72 }
73
74 void InitMiningSystem()
75 {
76 this.output.Print("Mining system initializing...");
77
78 this.GridTerminalSystem.GetBlocksOfType(
79 this.drills,
80 (IMyShipDrill drill) => drill.CustomName.StartsWith(GRID_PREFIX)
81 );
82
83 this.output.Print($"Nb of drills: {this.drills.Count}");
84
85 this.GridTerminalSystem.GetBlocksOfType(
86 this.pistons,
87 (IMyExtendedPistonBase piston) => piston.CustomName.StartsWith(GRID_PREFIX) && piston.CustomName.Contains("Miner")
88 );
89
90 this.output.Print($"Nb of pistons: {this.pistons.Count}");
91
92 this.magneticPlate = this.GridTerminalSystem.GetBlockWithName("[Base] Magnetic Plate Miner") as IMyLandingGear;
93
94 this.soundAlert = this.GridTerminalSystem.GetBlockWithName("[Base] Sound Block Miner") as IMySoundBlock;
95 if (this.soundAlert == null)
96 this.output.Print($"Error: sound alert system not found");
97
98 this.GridTerminalSystem.GetBlocksOfType(
99 this.rotatingLights,
100 (IMyLightingBlock light) => light.CustomName.StartsWith(GRID_PREFIX) && light.CustomName.Contains("Miner")
101 );
102
103 this.output.Print($"Nb of rotating lights: {this.rotatingLights.Count}");
104
105 this.output.Print("Mining system initialized");
106 }
107
108 public void Save()
109 {
110 }
111
112 void UpdateState()
113 {
114 if (this.currentState == State.STOPPING_MINING)
115 {
116 bool finished = true;
117
118 foreach (var d in this.drills)
119 {
120 d.Enabled = false;
121 }
122
123 foreach (var p in this.pistons)
124 {
125 var distanceDocked = 0.0;
126
127 if (p.CustomName == "[Base] Piston Miner 02")
128 distanceDocked = 0.1;
129
130 if (p.CurrentPosition > distanceDocked + EPSILON)
131 {
132 finished = false;
133 p.Velocity = 5 * -PISTON_SPEED;
134 }
135 else
136 {
137 p.Velocity = 0.0f;
138 }
139 }
140
141 if (finished)
142 {
143 foreach (var p in this.pistons)
144 p.Enabled = false;
145
146 foreach (var l in this.rotatingLights)
147 l.Enabled = false;
148
149 this.soundAlert.Stop();
150
151 this.output.Print("Drills parked, operation terminated");
152 this.currentState = State.NORMAL;
153 }
154 }
155 else if (this.currentState == State.MINING)
156 {
157 this.magneticPlate.Unlock();
158
159 foreach (var p in this.pistons)
160 {
161 p.Enabled = true;
162 p.Velocity = PISTON_SPEED;
163 }
164
165 foreach (var d in this.drills)
166 {
167 d.Enabled = true;
168 }
169 }
170
171 // Send miner connector position.
172 this.IGC.SendBroadcastMessage("POSITION_CONNECTOR_MINER", this.minerConnector.WorldMatrix);
173 }
174
175 public void Main(string argument, UpdateType updateSource)
176 {
177 if ((updateSource & UpdateType.Update100) != 0)
178 {
179 this.UpdateState();
180 }
181 else if ((updateSource & (UpdateType.Terminal | UpdateType.Trigger)) != 0)
182 {
183 switch (argument)
184 {
185 case "STOP_MINING":
186 this.output.Print("Stopping mining...");
187 this.currentState = State.STOPPING_MINING;
188 break;
189
190 case "START_MINING":
191 this.soundAlert.Play();
192 foreach (var l in this.rotatingLights)
193 l.Enabled = true;
194
195 this.output.Print("Mining in progress...");
196 this.currentState = State.MINING;
197 break;
198
199 default:
200 this.output.Print($"Uknown command: {argument}");
201 break;
202 }
203 }
204 }
205 }
206 }
207