-
Notifications
You must be signed in to change notification settings - Fork 0
/
Flare Printer.cs
498 lines (451 loc) · 14.3 KB
/
Flare Printer.cs
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
const string Program_Name = "Flare Printer"; //Name me!
private Color DEFAULT_TEXT_COLOR=new Color(197,137,255,255);
private Color DEFAULT_BACKGROUND_COLOR=new Color(44,0,88,255);
class GenericMethods<T> where T : class, IMyTerminalBlock{
private IMyGridTerminalSystem TerminalSystem;
private IMyTerminalBlock Prog;
private MyGridProgram Program;
public GenericMethods(MyGridProgram Program){
this.Program = Program;
TerminalSystem = Program.GridTerminalSystem;
Prog = Program.Me;
}
public T GetFull(string name, double max_distance, Vector3D Reference){
List<T> AllBlocks = new List<T>();
List<T> MyBlocks = new List<T>();
TerminalSystem.GetBlocksOfType<T>(AllBlocks);
double min_distance = max_distance;
foreach(T Block in AllBlocks){
if(Block.CustomName.Equals(name)){
double distance = (Reference - Block.GetPosition()).Length();
min_distance = Math.Min(min_distance, distance);
MyBlocks.Add(Block);
}
}
foreach(T Block in MyBlocks){
double distance = (Reference - Block.GetPosition()).Length();
if(distance <= min_distance + 0.1){
return Block;
}
}
return null;
}
public T GetFull(string name, double max_distance, IMyTerminalBlock Reference){
return GetFull(name, max_distance, Reference.GetPosition());
}
public T GetFull(string name, double max_distance){
return GetFull(name, max_distance, Prog);
}
public T GetFull(string name){
return GetFull(name, double.MaxValue);
}
public T GetContaining(string name, Vector3D Reference, double max_distance){
List<T> AllBlocks = new List<T>();
List<T> MyBlocks = new List<T>();
TerminalSystem.GetBlocksOfType<T>(AllBlocks);
double min_distance = max_distance;
foreach(T Block in AllBlocks){
if(Block.CustomName.Contains(name)){
double distance = (Reference - Block.GetPosition()).Length();
min_distance = Math.Min(min_distance, distance);
MyBlocks.Add(Block);
}
}
foreach(T Block in MyBlocks){
double distance = (Reference - Block.GetPosition()).Length();
if(distance <= min_distance + 0.1){
return Block;
}
}
return null;
}
public T GetContaining(string name, IMyTerminalBlock Reference, double max_distance){
return GetContaining(name, Reference.GetPosition(), max_distance);
}
public T GetContaining(string name, double max_distance){
return GetContaining(name, Prog, max_distance);
}
public T GetContaining(string name){
return GetContaining(name, double.MaxValue);
}
public List<T> GetAllContaining(string name, double max_distance, Vector3D Reference){
List<T> AllBlocks = new List<T>();
List<List<T>> MyLists = new List<List<T>>();
List<T> MyBlocks = new List<T>();
TerminalSystem.GetBlocksOfType<T>(AllBlocks);
foreach(T Block in AllBlocks){
if(Block.CustomName.Contains(name)){
bool has_with_name = false;
for(int i=0; i<MyLists.Count && !has_with_name; i++){
if(Block.CustomName.Equals(MyLists[i][0].CustomName)){
MyLists[i].Add(Block);
has_with_name = true;
break;
}
}
if(!has_with_name){
List<T> new_list = new List<T>();
new_list.Add(Block);
MyLists.Add(new_list);
}
}
}
foreach(List<T> list in MyLists){
if(list.Count == 1){
MyBlocks.Add(list[0]);
continue;
}
double min_distance = max_distance;
foreach(T Block in list){
double distance = (Reference - Block.GetPosition()).Length();
min_distance = Math.Min(min_distance, distance);
}
foreach(T Block in list){
double distance = (Reference - Block.GetPosition()).Length();
if(distance <= min_distance + 0.1){
MyBlocks.Add(Block);
break;
}
}
}
return MyBlocks;
}
public List<T> GetAllIncluding(string name, Vector3D Reference, double max_distance = double.MaxValue){
List<T> AllBlocks = new List<T>();
List<T> MyBlocks = new List<T>();
TerminalSystem.GetBlocksOfType<T>(AllBlocks);
foreach(T Block in AllBlocks){
double distance = (Reference - Block.GetPosition()).Length();
if(Block.CustomName.Contains(name) && distance <= max_distance){
MyBlocks.Add(Block);
}
}
return MyBlocks;
}
public List<T> GetAllIncluding(string name, IMyTerminalBlock Reference, double max_distance = double.MaxValue){
return GetAllIncluding(name, Reference.GetPosition(), max_distance);
}
public List<T> GetAllIncluding(string name, double max_distance = double.MaxValue){
return GetAllIncluding(name, Prog, max_distance);
}
public List<T> GetAllContaining(string name, double max_distance, IMyTerminalBlock Reference){
return GetAllContaining(name, max_distance, Reference.GetPosition());
}
public List<T> GetAllContaining(string name, double max_distance){
return GetAllContaining(name, max_distance, Prog);
}
public List<T> GetAllContaining(string name){
return GetAllContaining(name, double.MaxValue);
}
public List<T> GetAllFunc(Func<T, bool> f){
List<T> AllBlocks = new List<T>();
List<T> MyBlocks = new List<T>();
TerminalSystem.GetBlocksOfType<T>(AllBlocks);
foreach(T Block in AllBlocks){
if(f(Block)){
MyBlocks.Add(Block);
}
}
return MyBlocks;
}
public T GetClosestFunc(Func<T, bool> f, double max_distance, Vector3D Reference){
List<T> MyBlocks = GetAllFunc(f);
double min_distance = max_distance;
foreach(T Block in MyBlocks){
double distance = (Reference - Block.GetPosition()).Length();
min_distance = Math.Min(min_distance, distance);
}
foreach(T Block in MyBlocks){
double distance = (Reference - Block.GetPosition()).Length();
if(distance <= min_distance + 0.1){
return Block;
}
}
return null;
}
public T GetClosestFunc(Func<T, bool> f, double max_distance, IMyTerminalBlock Reference){
return GetClosestFunc(f, max_distance, Reference.GetPosition());
}
public T GetClosestFunc(Func<T, bool> f, double max_distance){
return GetClosestFunc(f, max_distance, Program.Me);
}
public T GetClosestFunc(Func<T, bool> f){
return GetClosestFunc(f, double.MaxValue);
}
public static List<T> SortByDistance(List<T> unsorted, Vector3D Reference){
List<T> output = new List<T>();
while(unsorted.Count > 0){
double min_distance = double.MaxValue;
foreach(T Block in unsorted){
double distance = (Reference - Block.GetPosition()).Length();
min_distance = Math.Min(min_distance, distance);
}
for(int i=0; i<unsorted.Count; i++){
double distance = (Reference - unsorted[i].GetPosition()).Length();
if(distance <= min_distance + 0.1){
output.Add(unsorted[i]);
unsorted.RemoveAt(i);
break;
}
}
}
return output;
}
public static List<T> SortByDistance(List<T> unsorted, IMyTerminalBlock Reference){
return SortByDistance(unsorted, Reference.GetPosition());
}
public List<T> SortByDistance(List<T> unsorted){
return SortByDistance(unsorted, Prog);
}
public static double GetAngle(Vector3D v1, Vector3D v2){
v1.Normalize();
v2.Normalize();
return Math.Round(Math.Acos(v1.X*v2.X + v1.Y*v2.Y + v1.Z*v2.Z) * 57.295755, 5);
}
}
bool HasBlockData(IMyTerminalBlock Block, string Name){
if(Name.Contains(':'))
return false;
string[] args=Block.CustomData.Split('•');
foreach(string argument in args){
if(argument.IndexOf(Name+':')==0){
return true;
}
}
return false;
}
string GetBlockData(IMyTerminalBlock Block, string Name){
if(Name.Contains(':'))
return "";
string[] args=Block.CustomData.Split('•');
foreach(string argument in args){
if(argument.IndexOf(Name+':')==0){
return argument.Substring((Name+':').Length);
}
}
return "";
}
bool SetBlockData(IMyTerminalBlock Block, string Name, string Data){
if(Name.Contains(':'))
return false;
string[] args=Block.CustomData.Split('•');
for(int i=0; i<args.Count(); i++){
if(args[i].IndexOf(Name+':')==0){
Block.CustomData=Name+':'+Data;
for(int j=0; j<args.Count(); j++){
if(j!=i){
Block.CustomData+='•'+args[j];
}
}
return true;
}
}
Block.CustomData+='•'+Name+':'+Data;
return true;
}
Vector3D GlobalToLocal(Vector3D Global, IMyCubeBlock Reference){
Vector3D Local=Vector3D.Transform(Global+Reference.GetPosition(), MatrixD.Invert(Reference.WorldMatrix));
Local.Normalize();
return Local*Global.Length();
}
Vector3D GlobalToLocalPosition(Vector3D Global, IMyCubeBlock Reference){
Vector3D Local=Vector3D.Transform(Global, MatrixD.Invert(Reference.WorldMatrix));
Local.Normalize();
return Local*(Global-Reference.GetPosition()).Length();
}
Vector3D LocalToGlobal(Vector3D Local, IMyCubeBlock Reference){
Vector3D Global=Vector3D.Transform(Local, Reference.WorldMatrix)-Reference.GetPosition();
Global.Normalize();
return Global*Local.Length();
}
Vector3D LocalToGlobalPosition(Vector3D Local, IMyCubeBlock Reference){
return Vector3D.Transform(Local,Reference.WorldMatrix);
}
double GetAngle(Vector3D v1, Vector3D v2){
return GenericMethods<IMyTerminalBlock>.GetAngle(v1,v2);
}
void Write(string text, bool new_line=true, bool append=true){
Echo(text);
if(new_line)
Me.GetSurface(0).WriteText(text+'\n', append);
else
Me.GetSurface(0).WriteText(text, append);
}
enum PrinterStatus{
Returning=0,
Printing=1,
Ready=2,
Deploying=3
}
class FlarePrinter{
public IMyShipWelder Welder;
public IMyPistonBase Piston;
public IMyProjector Projector;
public bool HasLaunched=true;
private FlarePrinter(IMyShipWelder welder,IMyPistonBase piston,IMyProjector projector){
Welder=welder;
Piston=piston;
Projector=projector;
}
public static bool TryGet(MyGridProgram P,IMyShipWelder welder,out FlarePrinter output){
output=null;
if(!welder.CustomName.Equals("Flare Welder"))
return false;
IMyPistonBase piston=(new GenericMethods<IMyPistonBase>(P)).GetFull("Flare Construction Piston",5,welder);
if(piston==null)
return false;
IMyProjector projector=(new GenericMethods<IMyProjector>(P)).GetFull("Flare Projector",5,welder);
if(projector==null)
return false;
output=new FlarePrinter(welder,piston,projector);
return true;
}
public PrinterStatus Status{
get{
if(Piston.Status==PistonStatus.Extending)
return PrinterStatus.Deploying;
if(Piston.Status!=PistonStatus.Retracted)
return PrinterStatus.Returning;
if(Projector.RemainingBlocks==0)
return PrinterStatus.Ready;
return PrinterStatus.Printing;
}
}
}
long cycle_long = 1;
long cycle = 0;
char loading_char = '|';
double seconds_since_last_update = 0;
List<FlarePrinter> Printers = new List<FlarePrinter>();
public Program()
{
Me.CustomName=(Program_Name+" Programmable block").Trim();
for(int i=0;i<Me.SurfaceCount;i++){
Me.GetSurface(i).FontColor=DEFAULT_TEXT_COLOR;
Me.GetSurface(i).BackgroundColor=DEFAULT_BACKGROUND_COLOR;
Me.GetSurface(i).Alignment=TextAlignment.CENTER;
Me.GetSurface(i).ContentType=ContentType.TEXT_AND_IMAGE;
}
Me.GetSurface(1).FontSize=2.2f;
Me.GetSurface(1).TextPadding=40.0f;
Echo("Beginning initialization");
List<IMyShipWelder> Welders=(new GenericMethods<IMyShipWelder>(this)).GetAllIncluding("Flare Welder");
Printers=new List<FlarePrinter>();
foreach(IMyShipWelder Welder in Welders){
FlarePrinter printer;
if(FlarePrinter.TryGet(this,Welder,out printer))
Printers.Add(printer);
}
Runtime.UpdateFrequency=UpdateFrequency.Update100;
}
public void Save()
{
// Called when the program needs to save its state. Use
// this method to save your state to the Storage field
// or some other means.
//
// This method is optional and can be removed if not
// needed.
}
void UpdateProgramInfo(){
cycle_long += ((++cycle)/long.MaxValue)%long.MaxValue;
cycle = cycle % long.MaxValue;
switch(loading_char){
case '|':
loading_char='\\';
break;
case '\\':
loading_char='-';
break;
case '-':
loading_char='/';
break;
case '/':
loading_char='|';
break;
}
Write("",false,false);
Echo(Program_Name + " OS " + cycle_long.ToString() + '-' + cycle.ToString() + " (" + loading_char + ")");
Me.GetSurface(1).WriteText(Program_Name + " OS " + cycle_long.ToString() + '-' + cycle.ToString() + " (" + loading_char + ")",false);
seconds_since_last_update = Runtime.TimeSinceLastRun.TotalSeconds + (Runtime.LastRunTimeMs / 1000);
if(seconds_since_last_update<1){
Echo(Math.Round(seconds_since_last_update*1000, 0).ToString() + " milliseconds\n");
}
else if(seconds_since_last_update<60){
Echo(Math.Round(seconds_since_last_update, 3).ToString() + " seconds\n");
}
else if(seconds_since_last_update/60<60){
Echo(Math.Round(seconds_since_last_update/60, 2).ToString() + " minutes\n");
}
else if(seconds_since_last_update/60/60<24){
Echo(Math.Round(seconds_since_last_update/60/60, 2).ToString() + " hours\n");
}
else {
Echo(Math.Round(seconds_since_last_update/60/60/24, 2).ToString() + " days\n");
}
}
public void Main(string argument, UpdateType updateSource)
{
UpdateProgramInfo();
int returning_count=0;
int printing_count=0;
int ready_count=0;
int deploying_count=0;
Write("Send \"Deploy\" to Deploy Flares");
if(Printers.Count==1)
Write(Printers.Count.ToString() + " Flare Printer");
else
Write(Printers.Count.ToString() + " Flare Printers");
if(argument.ToLower().Equals("deploy")){
foreach(FlarePrinter Printer in Printers){
Printer.HasLaunched=false;
if(Printer.Status==PrinterStatus.Ready)
Printer.Piston.Extend();
}
}
foreach(FlarePrinter Printer in Printers){
switch(Printer.Status){
case PrinterStatus.Returning:
returning_count++;
Printer.Piston.Retract();
break;
case PrinterStatus.Printing:
printing_count++;
Printer.Welder.Enabled=true;
break;
case PrinterStatus.Ready:
Printer.Welder.Enabled=false;
if(!Printer.HasLaunched){
Printer.Piston.Extend();
deploying_count++;
}
else
ready_count++;
break;
case PrinterStatus.Deploying:
deploying_count++;
if(Printer.Piston.CurrentPosition>=1){
IMyTimerBlock FlareTimer=(new GenericMethods<IMyTimerBlock>(this)).GetFull("Flare Timer 1",5,Printer.Welder);
if(FlareTimer!=null){
FlareTimer.Trigger();
Printer.HasLaunched=true;
}
}
break;
}
}
if(returning_count>0)
Write("Returning:"+returning_count);
if(printing_count>0)
Write("Printing:"+printing_count);
if(ready_count>0)
Write("Ready:"+ready_count);
if(deploying_count>0)
Write("Deploying:"+deploying_count);
if(deploying_count>0)
Runtime.UpdateFrequency=UpdateFrequency.Update10;
else if(ready_count<Printers.Count)
Runtime.UpdateFrequency=UpdateFrequency.Update100;
else
Runtime.UpdateFrequency=UpdateFrequency.None;
}