Add missile launcher and missile controller projects
[SEScripts.git] / MissileLauncher / Program.cs
1 using Sandbox.Game.Entities.Cube;
2 using Sandbox.Game.EntityComponents;
3 using Sandbox.Game.GameSystems;
4 using Sandbox.ModAPI.Ingame;
5 using Sandbox.ModAPI.Interfaces;
6
7 using SpaceEngineers.Game.ModAPI.Ingame;
8
9 using System;
10 using System.Collections;
11 using System.Collections.Generic;
12 using System.Collections.Immutable;
13 using System.Linq;
14 using System.Text;
15
16 using VRage;
17 using VRage.Collections;
18 using VRage.Game;
19 using VRage.Game.Components;
20 using VRage.Game.GUI.TextPanel;
21 using VRage.Game.ModAPI.Ingame;
22 using VRage.Game.ModAPI.Ingame.Utilities;
23 using VRage.Game.ObjectBuilders.Definitions;
24
25 using VRageMath;
26
27 using static VRageRender.Utils.MyWingedEdgeMesh;
28
29 namespace IngameScript
30 {
31 enum State
32 {
33 NORMAL,
34 STARTING_SEQUENCE,
35 FILLING_TANK,
36 LAUNCHING,
37 }
38
39 class Missile
40 {
41 string prefix;
42 readonly string tankName = "Hydrogen Tank";
43 readonly string connectorName = "Connector";
44 readonly string mergeBlockName = "Merge Block";
45 readonly string programmableBlockName = "Programmable Block";
46
47 State currentState = State.NORMAL;
48
49 IMyGridTerminalSystem gridTerminalSystem;
50
51 IMyGasTank tank = null;
52 IMyShipConnector connector = null;
53 IMyShipMergeBlock mergeBlock = null;
54 IMyProgrammableBlock programmableBlock = null;
55
56 // bool loop = false; // Not used for the moment.
57 public Missile(string prefix, IMyGridTerminalSystem gridTerminalSystem)
58 {
59 this.prefix = prefix;
60 this.gridTerminalSystem = gridTerminalSystem;
61 }
62 }
63
64 partial class Program : MyGridProgram
65 {
66 const string GRID_PREFIX = "[PML]";
67 const string MISSILE_GRID_PREFIX = "[PM]";
68 const double HYDRO_TANK_FILLED_PERCENT = 20;
69
70 const float EPSILON = 0.05f;
71
72 readonly Output output;
73
74 public Program()
75 {
76 var output = this.Me.GetSurface(0);
77 this.output = new Output(output);
78 this.output.Print("Missile launcher system starting...");
79
80 this.Runtime.UpdateFrequency = UpdateFrequency.Update100;
81
82 this.output.Print("Missile launcher system started");
83 }
84
85 void UpdateState()
86 {
87 switch (this.currentState)
88 {
89 case State.STARTING_SEQUENCE:
90 this.tank = this.GridTerminalSystem.GetBlockWithName("[PM] Hydrogen Tank") as IMyGasTank;
91 if (this.tank == null)
92 {
93 this.output.Print("Cannot find the missile hydrogen tank");
94 break;
95 }
96
97 this.connector = this.GridTerminalSystem.GetBlockWithName("[PM] Connector") as IMyShipConnector;
98 if (this.connector == null)
99 {
100 this.output.Print("Cannot find the missile connector");
101 break;
102 }
103
104 this.mergeBlock = this.GridTerminalSystem.GetBlockWithName("[PM] Merge Block") as IMyShipMergeBlock;
105 if (this.mergeBlock == null)
106 {
107 this.output.Print("Cannot find the missile merge block");
108 break;
109 }
110
111 this.programmableBlock = this.GridTerminalSystem.GetBlockWithName("[PM] Programmable Block") as IMyProgrammableBlock;
112 if (this.programmableBlock == null)
113 {
114 this.output.Print("Cannot find the missile programmable block");
115 break;
116 }
117
118 this.tank.Stockpile = true;
119 this.connector.Connect();
120
121 this.currentState = State.FILLING_TANK;
122 break; ;
123
124 case State.FILLING_TANK:
125 this.output.Print("Waiting missile tank filled...");
126 if (this.tank.FilledRatio >= HYDRO_TANK_FILLED_PERCENT / 100)
127 {
128 this.tank.Stockpile = false;
129 this.currentState = State.LAUNCHING;
130 }
131 break;
132
133 case State.LAUNCHING:
134 this.output.Print("Launching missile...");
135
136 if (this.programmableBlock.TryRun("START"))
137 this.output.Print("Missile launched!");
138 else
139 this.output.Print("ERROR: Can't send START command to missile");
140
141 this.mergeBlock.Enabled = false;
142 this.connector.Disconnect();
143
144 if (this.loop)
145 this.currentState = State.STARTING_SEQUENCE;
146 else
147 this.currentState = State.NORMAL;
148
149 break;
150
151 case State.NORMAL:
152 break; // Nothing;
153 }
154
155 }
156
157 public void Save()
158 {
159 }
160
161 public void Main(string argument, UpdateType updateSource)
162 {
163 if ((updateSource & UpdateType.Update100) != 0)
164 {
165 this.UpdateState();
166 }
167 else if ((updateSource & (UpdateType.Terminal | UpdateType.Trigger)) != 0)
168 {
169 switch (argument)
170 {
171 case "LAUNCH ONE":
172 this.loop = false;
173 this.currentState = State.STARTING_SEQUENCE;
174 break;
175
176 case "LAUNCH SOME":
177 this.loop = true;
178 this.currentState = State.STARTING_SEQUENCE;
179 break;
180
181 case "STOP":
182 this.loop = false;
183 this.currentState = State.NORMAL;
184 break;
185
186 default:
187 this.output.Print($"Uknown command: {argument}");
188 break;
189 }
190 }
191 }
192 }
193 }