-
Notifications
You must be signed in to change notification settings - Fork 0
/
COS Vigilance.cs
2072 lines (1940 loc) · 59.3 KB
/
COS Vigilance.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
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Cosmic Engineering Vigilance Targeting System
* Built by mlq1616
* https://github.com/mlq1819
*/
//Name me!
const string Program_Name="Vigilance AI";
//Sets the maximum firing distance
const double FIRING_DISTANCE=20000; //Recommended between 5k and 20k; works best within range
//The distance the scanners will default to during AutoScan; the lower the faster it scans
const double AUTOSCAN_DISTANCE=20000;//Recommended between 2k and 10k
//Sets whether the AI starts out scanning or whether it has to wait to be told to autoscan
const bool DEFAULT_AUTOSCAN=false;
//Sets whether the AI starts out automatically shooting enemies or whether it has to wait to be told to autofire
const bool DEFAULT_AUTOFIRE=false;
//Set to the maximum time you expect the cannon to take to aim and print
const double AIM_TIME=15;
Color DEFAULT_TEXT_COLOR=new Color(197,137,255,255);
Color DEFAULT_BACKGROUND_COLOR=new Color(44,0,88,255);
Color Green_Text=new Color(137,255,137,255);
Color Green_Back=new Color(0,151,0,255);
Color Blue_Text=new Color(137,239,255,255);
Color Blue_Back=new Color(0,88,151,255);
Color Yellow_Text=new Color(255,239,137,255);
Color Yellow_Back=new Color(66,66,0,255);
Color Orange_Text=new Color(255,197,0,255);
Color Orange_Back=new Color(88,44,0,255);
Color Red_Text=new Color(255,137,137,255);
Color Red_Back=new Color(151,0,0,255);
class Prog{
public static MyGridProgram P;
public static TimeSpan FromSeconds(double seconds){
return (new TimeSpan(0,0,0,(int)seconds,(int)(seconds*1000)%1000));
}
public static TimeSpan UpdateTimeSpan(TimeSpan old,double seconds){
return old+FromSeconds(seconds);
}
}
class GenericMethods<T> where T : class, IMyTerminalBlock{
static IMyGridTerminalSystem TerminalSystem{
get{
return P.GridTerminalSystem;
}
}
public static MyGridProgram P{
get{
return Prog.P;
}
}
public static T GetFull(string name,Vector3D Ref,double mx_d=double.MaxValue){
List<T> AllBlocks=new List<T>();
List<T> MyBlocks=new List<T>();
TerminalSystem.GetBlocksOfType<T>(AllBlocks);
double min_distance=mx_d;
foreach(T Block in AllBlocks){
if(Block.CustomName.Equals(name)){
double distance=(Ref-Block.GetPosition()).Length();
min_distance=Math.Min(min_distance, distance);
MyBlocks.Add(Block);
}
}
foreach(T Block in MyBlocks){
double distance=(Ref-Block.GetPosition()).Length();
if(distance<=min_distance+0.1)
return Block;
}
return null;
}
public static T GetFull(string name,IMyTerminalBlock Ref,double mx_d=double.MaxValue){
return GetFull(name,Ref.GetPosition(),mx_d);
}
public static T GetFull(string name,double mx_d=double.MaxValue){
return GetFull(name,P.Me,mx_d);
}
public static T GetConstruct(string name,IMyTerminalBlock Ref,double mx_d=double.MaxValue){
List<T> input=GetAllConstruct(name,Ref,mx_d);
if(input.Count>0)
return input[0];
return null;
}
public static T GetConstruct(string name,double mx_d=double.MaxValue){
return GetConstruct(name,P.Me,mx_d);
}
public static List<T> GetAllConstruct(string name,IMyTerminalBlock Ref,double mx_d=double.MaxValue){
List<T> input=GetAllContaining(name,Ref,mx_d);
List<T> output=new List<T>();
foreach(T Block in input){
if(Ref.IsSameConstructAs(Block))
output.Add(Block);
}
return output;
}
public static List<T> GetAllConstruct(string name){
return GetAllConstruct(name,P.Me);
}
public static T GetContaining(string name,Vector3D Ref,double mx_d){
List<T> AllBlocks=new List<T>();
List<T> MyBlocks=new List<T>();
TerminalSystem.GetBlocksOfType<T>(AllBlocks);
double min_distance=mx_d;
foreach(T Block in AllBlocks){
if(Block.CustomName.Contains(name)){
double distance=(Ref-Block.GetPosition()).Length();
min_distance=Math.Min(min_distance,distance);
MyBlocks.Add(Block);
}
}
foreach(T Block in MyBlocks){
double distance=(Ref-Block.GetPosition()).Length();
if(distance<=min_distance+0.1)
return Block;
}
return null;
}
public static T GetContaining(string name,IMyTerminalBlock Ref,double mx_d){
return GetContaining(name,Ref.GetPosition(),mx_d);
}
public static T GetContaining(string name,double mx_d=double.MaxValue){
return GetContaining(name,P.Me,mx_d);
}
public static List<T> GetAllContaining(string name,Vector3D Ref,double mx_d){
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=mx_d;
foreach(T Block in list){
double distance=(Ref-Block.GetPosition()).Length();
min_distance=Math.Min(min_distance, distance);
}
foreach(T Block in list){
double distance=(Ref-Block.GetPosition()).Length();
if(distance<=min_distance+0.1){
MyBlocks.Add(Block);
break;
}
}
}
return MyBlocks;
}
public static List<T> GetAllIncluding(string name,Vector3D Ref,double mx_d=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=(Ref-Block.GetPosition()).Length();
if(Block.CustomName.Contains(name)&&distance<=mx_d)
MyBlocks.Add(Block);
}
return MyBlocks;
}
public static List<T> GetAllIncluding(string name,IMyTerminalBlock Ref,double mx_d=double.MaxValue){
return GetAllIncluding(name,Ref.GetPosition(),mx_d);
}
public static List<T> GetAllIncluding(string name,double mx_d=double.MaxValue){
return GetAllIncluding(name,P.Me,mx_d);
}
public static List<T> GetAllContaining(string name,IMyTerminalBlock Ref,double mx_d=double.MaxValue){
return GetAllContaining(name,Ref.GetPosition(),mx_d);
}
public static List<T> GetAllContaining(string name,double mx_d=double.MaxValue){
return GetAllContaining(name,P.Me,mx_d);
}
public static 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 static T GetClosestFunc(Func<T,bool> f,Vector3D Ref,double mx_d=double.MaxValue){
List<T> MyBlocks=GetAllFunc(f);
double min_distance=mx_d;
foreach(T Block in MyBlocks){
double distance=(Ref-Block.GetPosition()).Length();
min_distance=Math.Min(min_distance,distance);
}
foreach(T Block in MyBlocks){
double distance=(Ref-Block.GetPosition()).Length();
if(distance<=min_distance+0.1)
return Block;
}
return null;
}
public static T GetClosestFunc(Func<T,bool> f,IMyTerminalBlock Ref,double mx_d=double.MaxValue){
return GetClosestFunc(f,Ref.GetPosition(),mx_d);
}
public static T GetClosestFunc(Func<T,bool> f,double mx_d=double.MaxValue){
return GetClosestFunc(f,P.Me,mx_d);
}
public static T GetGrid(string name,IMyCubeGrid Grid,IMyTerminalBlock Ref,double mx_d=double.MaxValue){
List<T> input=GetAllGrid(name,Grid,Ref,mx_d);
if(input.Count>0)
return input[0];
return null;
}
public static T GetGrid(string name,IMyCubeGrid Grid,double mx_d=double.MaxValue){
return GetGrid(name,Grid,P.Me,mx_d);
}
public static List<T> GetAllGrid(string name,IMyCubeGrid Grid,IMyTerminalBlock Ref,double mx_d){
List<T> output=new List<T>();
List<T> input=GetAllContaining(name,Ref,mx_d);
foreach(T Block in input){
if(Block.CubeGrid==Grid)
output.Add(Block);
}
return output;
}
public static List<T> GetAllGrid(string name,IMyCubeGrid Grid,double mx_d=double.MaxValue){
return GetAllGrid(name,Grid,P.Me,mx_d);
}
public static List<T> SortByDistance(List<T> unsorted,Vector3D Ref){
List<T> output=new List<T>();
while(unsorted.Count>0){
double min_distance=double.MaxValue;
foreach(T Block in unsorted){
double distance=(Ref-Block.GetPosition()).Length();
min_distance=Math.Min(min_distance,distance);
}
for(int i=0; i<unsorted.Count; i++){
double distance=(Ref-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 Ref){
return SortByDistance(unsorted, Ref.GetPosition());
}
public static List<T> SortByDistance(List<T> unsorted){
return SortByDistance(unsorted,P.Me);
}
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);
}
}
class EntityInfo{
public long ID;
public string Name;
public MyDetectedEntityType Type;
Vector3D? _hitposition;
public Vector3D? HitPosition{
get{
return _hitposition;
}
set{
_hitposition=value;
if(_hitposition!=null){
Size=Math.Max(Size, (Position-((Vector3D)_hitposition)).Length());
}
}
}
Vector3D _velocity;
public Vector3D Velocity{
get{
return _velocity;
}
set{
_velocity=value;
Age=TimeSpan.Zero;
}
}
public MyRelationsBetweenPlayerAndBlock Relationship;
public Vector3D Position;
public double Size=0;
public TimeSpan Age=TimeSpan.Zero;
public Vector3D TargetPosition{
get{
if(HitPosition!=null)
return (Vector3D)HitPosition;
return Position;
}
}
public EntityInfo(long id, string name, MyDetectedEntityType type, Vector3D? hitposition, Vector3D velocity, MyRelationsBetweenPlayerAndBlock relationship, Vector3D position){
ID=id;
Name=name;
Type=type;
HitPosition=hitposition;
Velocity=velocity;
Relationship=relationship;
Position=position;
Age=TimeSpan.Zero;
}
public EntityInfo(long id, string name, MyDetectedEntityType type, Vector3D? hitposition, Vector3D velocity, MyRelationsBetweenPlayerAndBlock relationship, Vector3D position, double size) : this(id, name, type, hitposition, velocity, relationship, position){
this.Size=size;
}
public EntityInfo(EntityInfo o){
ID=o.ID;
Name=o.Name;
Type=o.Type;
Position=o.Position;
HitPosition=o.HitPosition;
Velocity=o.Velocity;
Relationship=o.Relationship;
Size=o.Size;
Age=o.Age;
}
public EntityInfo(MyDetectedEntityInfo entity_info){
ID=entity_info.EntityId;
Name=entity_info.Name;
Type=entity_info.Type;
Position=entity_info.Position;
if(entity_info.HitPosition!=null){
HitPosition=entity_info.HitPosition;
}
else {
HitPosition=(Vector3D?) null;
}
Velocity=entity_info.Velocity;
Relationship=entity_info.Relationship;
Age=TimeSpan.Zero;
}
public EntityInfo(Vector3D position,Vector3D velocity,long id=-1):this(id,"unknown",MyDetectedEntityType.Unknown,null,velocity,MyRelationsBetweenPlayerAndBlock.Enemies,position){
;
}
public static bool TryParse(string Parse, out EntityInfo Entity){
Entity=new EntityInfo(-1,"bad", MyDetectedEntityType.None, null, new Vector3D(0,0,0), MyRelationsBetweenPlayerAndBlock.NoOwnership, new Vector3D(0,0,0));
try{
string[] args=Parse.Split('\n');
long id;
if(!Int64.TryParse(args[0], out id)){
return false;
}
string name=args[1];
MyDetectedEntityType type=(MyDetectedEntityType) Int32.Parse(args[2]);
Vector3D? hitposition;
if(args[3].Equals("null")){
hitposition=(Vector3D?) null;
}
else {
Vector3D temp;
if(!Vector3D.TryParse(args[3], out temp)){
return false;
}
else {
hitposition=(Vector3D?) temp;
}
}
Vector3D velocity;
if(!Vector3D.TryParse(args[4], out velocity)){
return false;
}
MyRelationsBetweenPlayerAndBlock relationship=(MyRelationsBetweenPlayerAndBlock) Int32.Parse(args[5]);
Vector3D position;
if(!Vector3D.TryParse(args[6], out position)){
return false;
}
double size=0;
if(!double.TryParse(args[7], out size)){
size=0;
}
TimeSpan age;
if(!TimeSpan.TryParse(args[8], out age)){
return false;
}
Entity=new EntityInfo(id, name, type, hitposition, velocity, relationship, position, size);
Entity.Age=age;
return true;
}
catch(Exception){
return false;
}
}
public override string ToString(){
string entity_info="";
entity_info+=ID.ToString()+'\n';
entity_info+=Name.ToString()+'\n';
entity_info+=((int)Type).ToString()+'\n';
if(HitPosition!=null){
entity_info+=((Vector3D) HitPosition).ToString()+'\n';
}
else {
entity_info+="null"+'\n';
}
entity_info+=Velocity.ToString()+'\n';
entity_info+=((int)Relationship).ToString()+'\n';
entity_info+=Position.ToString()+'\n';
entity_info+=Size.ToString()+'\n';
entity_info+=Age.ToString()+'\n';
return entity_info;
}
public string NiceString(){
string entity_info="";
entity_info+="ID: "+ID.ToString()+'\n';
entity_info+="Name: "+Name.ToString()+'\n';
entity_info+="Type: "+Type.ToString()+'\n';
if(HitPosition!=null){
entity_info+="HitPosition: "+NeatVector((Vector3D) HitPosition)+'\n';
}
else {
entity_info+="HitPosition: "+"null"+'\n';
}
entity_info+="Velocity: "+NeatVector(Velocity)+'\n';
entity_info+="Relationship: "+Relationship.ToString()+'\n';
entity_info+="Position: "+NeatVector(Position)+'\n';
entity_info+="Size: "+((int)Size).ToString()+'\n';
entity_info+="Data Age: "+Age.ToString()+'\n';
return entity_info;
}
public static string NeatVector(Vector3D vector){
return "X:"+Math.Round(vector.X,1).ToString()+" Y:"+Math.Round(vector.Y,1).ToString()+" Z:"+Math.Round(vector.Z,1).ToString();
}
public void Update(double seconds){
Age=Prog.UpdateTimeSpan(Age,seconds);
}
public double GetDistance(Vector3D Reference){
return (TargetPosition-Reference).Length();
}
}
class EntityList:IEnumerable<EntityInfo>{
List<EntityInfo> E_List;
public IEnumerator<EntityInfo> GetEnumerator(){
return E_List.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator(){
return GetEnumerator();
}
public int Count{
get{
return E_List.Count;
}
}
public EntityInfo this[int key]{
get{
return E_List[key];
}
set{
E_List[key]=value;
}
}
public EntityList(){
E_List=new List<EntityInfo>();
}
public void UpdatePositions(double seconds){
foreach(EntityInfo entity in E_List)
entity.Update(seconds);
}
public bool UpdateEntry(EntityInfo Entity){
for(int i=0; i<E_List.Count; i++){
if(E_List[i].ID==Entity.ID || (Entity.GetDistance(E_List[i].Position)<=0.5f&&Entity.Type==E_List[i].Type)){
if(E_List[i].Age >= Entity.Age){
E_List[i]=Entity;
return true;
}
return false;
}
}
E_List.Add(Entity);
return true;
}
public bool RemoveEntry(EntityInfo Entity){
for(int i=0; i<E_List.Count; i++){
if(E_List[i].ID==Entity.ID || (Entity.GetDistance(E_List[i].Position)<=0.5f&&Entity.Type==E_List[i].Type)){
E_List.RemoveAt(i);
return true;
}
}
return false;
}
public void RemoveAt(int index){
E_List.RemoveAt(index);
}
public EntityInfo Get(long ID){
foreach(EntityInfo entity in E_List){
if(entity.ID==ID)
return entity;
}
return null;
}
public double ClosestDistance(MyRelationsBetweenPlayerAndBlock Relationship, double min_size=0){
double min_distance=double.MaxValue;
foreach(EntityInfo entity in E_List){
if(entity.Size >= min_size && entity.Relationship==Relationship)
min_distance=Math.Min(min_distance, (Prog.P.Me.GetPosition()-entity.Position).Length()-entity.Size);
}
return min_distance;
}
public double ClosestDistance(double min_size=0){
double min_distance=double.MaxValue;
foreach(EntityInfo entity in E_List){
if(entity.Size >= min_size)
min_distance=Math.Min(min_distance, (Prog.P.Me.GetPosition()-entity.Position).Length()-entity.Size);
}
return min_distance;
}
public void Clear(){
E_List.Clear();
}
public void Sort(Vector3D Reference){
Queue<EntityInfo> Unsorted=new Queue<EntityInfo>();
double last_distance=0;
for(int i=0;i<E_List.Count;i++){
double distance=E_List[i].GetDistance(Reference);
if(distance<last_distance){
Unsorted.Enqueue(E_List[i]);
E_List.RemoveAt(i);
i--;
continue;
}
last_distance=distance;
}
while(Unsorted.Count>0){
double distance=Unsorted.Peek().GetDistance(Reference);
int upper=E_List.Count;
int lower=0;
int index;
double down;
double up;
do{
index=(upper-lower)/2+lower;
down=0;
if(index>0)
down=E_List[index-1].GetDistance(Reference);
up=E_List[index].GetDistance(Reference);
if(down>distance)
upper=index-1;
else
lower=index-1;
if(up<distance)
lower=index;
else
upper=index;
}
while((down>distance||up<distance)&&upper!=lower);
E_List.Insert(index,Unsorted.Dequeue());
}
if(E_List.Count>128)
E_List.RemoveRange(128,E_List.Count-128);
}
}
TimeSpan FromSeconds(double seconds){
return Prog.FromSeconds(seconds);
}
TimeSpan UpdateTimeSpan(TimeSpan old,double seconds){
return old+FromSeconds(seconds);
}
string ToString(TimeSpan ts){
if(ts.TotalDays>=1)
return Math.Round(ts.TotalDays,2).ToString()+" days";
else if(ts.TotalHours>=1)
return Math.Round(ts.TotalHours,2).ToString()+" hours";
else if(ts.TotalMinutes>=1)
return Math.Round(ts.TotalMinutes,2).ToString()+" minutes";
else if(ts.TotalSeconds>=1)
return Math.Round(ts.TotalSeconds,3).ToString()+" seconds";
else
return Math.Round(ts.TotalMilliseconds,0).ToString()+" milliseconds";
}
void Write(string text, bool new_line=true, bool append=true){
Echo(text);
if(new_line){
Me.GetSurface(0).WriteText(text+'\n', append);
foreach(IMyTextPanel Panel in StatusPanels)
Panel.WriteText(text+'\n', append);
}
else{
Me.GetSurface(0).WriteText(text, append);
foreach(IMyTextPanel Panel in StatusPanels)
Panel.WriteText(text, append);
}
}
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;
}
enum CannonTask{
None=0,
Scan=1,
Reset=2,
Search=3,
Fire=4,
Manual=5
}
enum FireStatus{
Idle=0,
Printing=1,
Aiming=2,
WaitingClear=3,
WaitingTarget=4,
Firing=5
}
enum PrintStatus{
StartingPrint=0,
Printing=1,
WaitingResources=2,
EndingPrint=3,
Ready=4
}
TimeSpan Time_Since_Start=new TimeSpan(0);
long cycle=0;
char loading_char='|';
double seconds_since_last_update=0;
Random Rnd;
IMyRemoteControl Controller;
IMyProjector Projector;
IMyMotorStator YawRotor;
IMyMotorStator PitchRotor;
IMyMotorStator ShellRotor;
IMyShipWelder Welder;
IMySensorBlock Sensor;
IMyShipMergeBlock Merge;
List<IMyCameraBlock> Cameras;
List<IMyGravityGenerator> Generators;
IMyCockpit Cockpit;
List<IMyInteriorLight> FiringLights;
List<List<IMyInteriorLight>> StatusLights;
List<IMyTextPanel> StatusPanels;
EntityList Targets=new EntityList();
bool AutoFire=DEFAULT_AUTOFIRE;
bool AutoScan=DEFAULT_AUTOSCAN;
EntityInfo Target{
get{
if(Targets.Count>0)
return Targets[0];
return new EntityInfo(0, "invalid", MyDetectedEntityType.None, null, new Vector3D(0,0,0), MyRelationsBetweenPlayerAndBlock.Neutral, new Vector3D(0,0,0), 0);
}
}
Vector3D Target_Position{
get{
if(Target.HitPosition!=null)
return(Vector3D)Target.HitPosition;
return Target.Position;
}
}
Vector3D Aim_Position=new Vector3D(0,0,0);
Vector3D Aim_Direction{
get{
Vector3D output=Aim_Position-Controller.GetPosition();
output.Normalize();
return output;
}
}
double Aim_Distance{
get{
return (Aim_Position-Controller.GetPosition()).Length();
}
}
Vector3D Suspect_Position=new Vector3D(0,0,0);
Vector3D Suspect_Velocity=new Vector3D(0,0,0);
Vector3D Suspect_Direction{
get{
Vector3D output=Suspect_Position-Controller.GetPosition();
output.Normalize();
return output;
}
}
double Suspect_Distance{
get{
return (Suspect_Position-Controller.GetPosition()).Length();
}
}
double Time_To_Hit{
get{
double distance=20;
return (Aim_Distance-distance+distance*1.5657)/104.38;
}
}
double Time_To_Position{
get{
return (Target_Position-Aim_Position).Length()/Target.Velocity.Length();
}
}
double Precision{
get{
return Math.Min(1,50/Aim_Distance);
}
}
int Fire_Count=0;
Vector3D Forward_Vector;
Vector3D Backward_Vector{
get{
return -1*Forward_Vector;
}
}
Vector3D Up_Vector;
Vector3D Down_Vector{
get{
return -1*Up_Vector;
}
}
Vector3D Left_Vector;
Vector3D Right_Vector{
get{
return -1*Left_Vector;
}
}
Queue<CannonTask> TaskQueue=new Queue<CannonTask>();
CannonTask CurrentTask{
get{
if(Controller.IsUnderControl)
return CannonTask.Manual;
if(TaskQueue.Count==0)
return CannonTask.None;
return TaskQueue.Peek();
}
}
PrintStatus CurrentPrintStatus;
FireStatus CurrentFireStatus;
Vector3D GlobalToLocal(Vector3D Global,IMyCubeBlock Ref){
Vector3D Local=Vector3D.Transform(Global+Ref.GetPosition(), MatrixD.Invert(Ref.WorldMatrix));
Local.Normalize();
return Local*Global.Length();
}
Vector3D GlobalToLocalPosition(Vector3D Global,IMyCubeBlock Ref){
Vector3D Local=Vector3D.Transform(Global, MatrixD.Invert(Ref.WorldMatrix));
Local.Normalize();
return Local*(Global-Ref.GetPosition()).Length();
}
Vector3D LocalToGlobal(Vector3D Local,IMyCubeBlock Ref){
Vector3D Global=Vector3D.Transform(Local, Ref.WorldMatrix)-Ref.GetPosition();
Global.Normalize();
return Global*Local.Length();
}
Vector3D LocalToGlobalPosition(Vector3D Local,IMyCubeBlock Ref){
return Vector3D.Transform(Local,Ref.WorldMatrix);
}
double GetAngle(Vector3D v1,Vector3D v2){
return GenericMethods<IMyTerminalBlock>.GetAngle(v1,v2);
}
bool Operational=false;
public Program(){
Rnd=new Random();
Prog.P=this;
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");
Me.Enabled=true;
StatusPanels=GenericMethods<IMyTextPanel>.GetAllContaining("Cannon Status Panel ");
foreach(IMyTextPanel Panel in StatusPanels){
Panel.FontColor=DEFAULT_TEXT_COLOR;
Panel.BackgroundColor=DEFAULT_BACKGROUND_COLOR;
Panel.Alignment=TextAlignment.CENTER;
Panel.ContentType=ContentType.TEXT_AND_IMAGE;
Panel.FontSize=1.0f;
Panel.TextPadding=10.0f;
}
Operational=false;
Cockpit=GenericMethods<IMyCockpit>.GetFull("Vigilance Control Cockpit");
if(Cockpit!=null){
for(int i=0;i<Cockpit.SurfaceCount;i++){
if(i<3)
Cockpit.GetSurface(i).ContentType=ContentType.TEXT_AND_IMAGE;
Cockpit.GetSurface(i).FontSize=2.2f;
Cockpit.GetSurface(i).TextPadding=20;
Cockpit.GetSurface(i).FontColor=DEFAULT_TEXT_COLOR;
Cockpit.GetSurface(i).BackgroundColor=DEFAULT_BACKGROUND_COLOR;
Cockpit.GetSurface(i).Alignment=TextAlignment.CENTER;
Cockpit.GetSurface(i).ScriptForegroundColor=DEFAULT_TEXT_COLOR;
Cockpit.GetSurface(i).ScriptBackgroundColor=DEFAULT_BACKGROUND_COLOR;
}
}
Controller=GenericMethods<IMyRemoteControl>.GetFull("Driver Controller");
if(Controller==null)
return;
Projector=GenericMethods<IMyProjector>.GetFull("Shell Projector");
if(Projector==null)
return;
YawRotor=GenericMethods<IMyMotorStator>.GetFull("Yaw Rotor");
if(YawRotor==null)
return;
PitchRotor=GenericMethods<IMyMotorStator>.GetFull("Pitch Rotor");
if(PitchRotor==null)
return;
ShellRotor=GenericMethods<IMyMotorStator>.GetFull("Shell Rotor");
if(ShellRotor==null)
return;
Sensor=GenericMethods<IMySensorBlock>.GetFull("Driver Sensor");
if(Sensor==null)
return;
Welder=GenericMethods<IMyShipWelder>.GetFull("Driver Shell Welder");
if(Welder==null)
return;
Merge=GenericMethods<IMyShipMergeBlock>.GetFull("Shell Printer Merge Block");
if(Merge==null)
return;
Generators=GenericMethods<IMyGravityGenerator>.GetAllContaining("Driver Generator ");
if(Generators.Count==0)
return;
Cameras=GenericMethods<IMyCameraBlock>.GetAllContaining("Driver Camera ");
if(Cameras.Count==0)
return;
foreach(IMyCameraBlock Camera in Cameras)
Camera.EnableRaycast=true;
FiringLights=GenericMethods<IMyInteriorLight>.GetAllContaining("Driver Firing Light ");
StatusLights=new List<List<IMyInteriorLight>>();
for(int i=1;i<=3;i++){
StatusLights.Add(GenericMethods<IMyInteriorLight>.GetAllContaining("Driver Status Light "+i.ToString()));
}
TaskQueue=new Queue<CannonTask>();
string[] args=this.Storage.Split('•');
foreach(string arg in args){
try{
if(arg.IndexOf("Task:")==0){
string word=arg.Substring("Task:".Length);
int output=0;
if(Int32.TryParse(word,out output))
TaskQueue.Enqueue((CannonTask)output);
}
else if(arg.IndexOf("Target:")==0){
EntityInfo output;
if(EntityInfo.TryParse(arg.Substring("Target:".Length),out output))
Targets.UpdateEntry(output);
}
else if(arg.IndexOf("FireStatus:")==0){
string word=arg.Substring("Status:".Length);
int output=0;
if(Int32.TryParse(word,out output))
CurrentFireStatus=(FireStatus)output;
}
else if(arg.IndexOf("PrintStatus:")==0){
string word=arg.Substring("Status:".Length);
int output=0;
if(Int32.TryParse(word,out output))
CurrentPrintStatus=(PrintStatus)output;
}
else if(arg.IndexOf("AutoScan:")==0){
string word=arg.Substring("AutoScan:".Length);
bool.TryParse(word,out AutoScan);
}
else if(arg.IndexOf("AutoFire:")==0){
string word=arg.Substring("AutoFire:".Length);
bool.TryParse(word,out AutoFire);
}
}
catch(Exception){
continue;
}
}
if(TaskQueue.Count==0&&AutoScan)
TaskQueue.Enqueue(CannonTask.Scan);
Fire_Count=3;
Operational=true;
IGC.RegisterBroadcastListener("Vigilance AI");
IGC.RegisterBroadcastListener("Entity Report");
IGC.RegisterBroadcastListener(Me.CubeGrid.CustomName);