-
Notifications
You must be signed in to change notification settings - Fork 0
/
Vigilance Targeting.cs
1777 lines (1659 loc) · 51.2 KB
/
Vigilance Targeting.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
/*
* Vigilance Targeting System
* Built by mlq1616
* https://github.com/mlq1819
*/
//Name me!
private const string Program_Name="Vigilance AI";
//Sets the maximum firing distance
private const double FIRING_DISTANCE=10000; //Recommended between 5k and 20k; works best within range
//The distance the scanners will default to during AutoScan; the lower the faster it scans
private const double AUTOSCAN_DISTANCE=5000;//Recommended between 2k and 10k
//Sets whether the AI starts out scanning or whether it has to wait to be told to autoscan
private 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
private const bool DEFAULT_AUTOFIRE=true;
//Set to the maximum time you expect the cannon to take to aim and print
private const double AIM_TIME=15;
private Color DEFAULT_TEXT_COLOR=new Color(197,137,255,255);
private Color DEFAULT_BACKGROUND_COLOR=new Color(44,0,88,255);
public 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);
}
}
public class EntityInfo{
public long ID;
public string Name;
public MyDetectedEntityType Type;
private Vector3D? _hitposition;
public Vector3D? HitPosition{
get{
return _hitposition;
}
set{
_hitposition=value;
if(_hitposition!=null){
Size=Math.Max(Size, (Position-((Vector3D)_hitposition)).Length());
}
}
}
private 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 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){
TimeSpan time=new TimeSpan((int)(seconds/60/60/24), ((int)(seconds/60/60))%24, ((int)(seconds/60))%60, ((int)(seconds))%60, ((int)(seconds*1000))%1000);
Age.Add(time);
Position+=seconds*Velocity;
if(HitPosition!=null){
HitPosition=(Vector3D?) (((Vector3D)HitPosition)+seconds*Velocity);
}
}
public double GetDistance(Vector3D Reference){
return (Position-Reference).Length();
}
}
public class EntityList : IEnumerable<EntityInfo>{
private 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){
if(E_List[i].Age >= Entity.Age){
E_List[i]=Entity;
return true;
}
return false;
}
}
E_List.Add(Entity);
return true;
}
public EntityInfo Get(long ID){
foreach(EntityInfo entity in E_List){
if(entity.ID==ID)
return entity;
}
return null;
}
public double ClosestDistance(MyGridProgram P, 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, (P.Me.GetPosition()-entity.Position).Length()-entity.Size);
}
}
return min_distance;
}
public double ClosestDistance(MyGridProgram P, 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, (P.Me.GetPosition()-entity.Position).Length()-entity.Size);
}
}
return min_distance;
}
public void Clear(){
E_List.Clear();
}
public void RemoveAt(int index){
E_List.RemoveAt(index);
}
public void Sort(Vector3D Reference){
List<EntityInfo> Sorted=new List<EntityInfo>();
List<EntityInfo> Unsorted=new List<EntityInfo>();
foreach(EntityInfo Entity in E_List){
double distance=Entity.GetDistance(Reference);
double last_distance=0;
if(Sorted.Count>0)
last_distance=Sorted[Sorted.Count-1].GetDistance(Reference);
if(distance>=last_distance)
Sorted.Add(Entity);
else
Unsorted.Add(Entity);
}
while(Unsorted.Count>0){
double distance=Unsorted[0].GetDistance(Reference);
if(distance>=Sorted[Sorted.Count-1].GetDistance(Reference)){
Sorted.Add(Unsorted[0]);
Unsorted.RemoveAt(0);
continue;
}
for(int i=0;i<Sorted.Count;i++){
if(distance<=Sorted[i].GetDistance(Reference)){
Sorted.Insert(i,Unsorted[0]);
Unsorted.RemoveAt(0);
break;
}
}
}
E_List=Sorted;
}
}
public 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);
}
}
}
private 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;
}
private 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 "";
}
private 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;
}
private enum CannonTask{
None=0,
Scan=1,
Reset=2,
Search=3,
Fire=4
}
private enum FireStatus{
Idle=0,
Printing=1,
Aiming=2,
WaitingClear=3,
WaitingTarget=4,
Firing=5
}
private enum PrintStatus{
StartingPrint=0,
Printing=1,
WaitingResources=2,
EndingPrint=3,
Ready=4
}
private long cycle_long = 1;
private long cycle = 0;
private char loading_char = '|';
private double seconds_since_last_update = 0;
private Random Rnd;
private IMyRemoteControl Controller;
private IMyProjector Projector;
private IMyMotorStator YawRotor;
private IMyMotorStator PitchRotor;
private IMyMotorStator ShellRotor;
private IMyShipWelder Welder;
private IMySensorBlock Sensor;
private IMyShipMergeBlock Merge;
private List<IMyCameraBlock> Cameras;
private List<IMyGravityGenerator> Generators;
private List<IMyInteriorLight> FiringLights;
private List<List<IMyInteriorLight>> StatusLights;
private List<IMyTextPanel> StatusPanels;
private EntityList Targets=new EntityList();
private bool AutoFire=DEFAULT_AUTOFIRE;
private bool AutoScan=DEFAULT_AUTOSCAN;
private 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);
}
}
private Vector3D Target_Position{
get{
if(Target.HitPosition!=null)
return(Vector3D)Target.HitPosition;
return Target.Position;
}
}
private Vector3D Aim_Position=new Vector3D(0,0,0);
private Vector3D Aim_Direction{
get{
Vector3D output=Aim_Position-Controller.GetPosition();
output.Normalize();
return output;
}
}
private double Aim_Distance{
get{
return (Aim_Position-Controller.GetPosition()).Length();
}
}
private Vector3D Suspect_Position=new Vector3D(0,0,0);
private Vector3D Suspect_Velocity=new Vector3D(0,0,0);
private Vector3D Suspect_Direction{
get{
Vector3D output=Suspect_Position-Controller.GetPosition();
output.Normalize();
return output;
}
}
private double Suspect_Distance{
get{
return (Suspect_Position-Controller.GetPosition()).Length();
}
}
private double Time_To_Hit{
get{
double distance=20;
return (Aim_Distance-distance+distance*1.5657)/104.38;
}
}
private double Time_To_Position{
get{
return (Target_Position-Aim_Position).Length()/Target.Velocity.Length();
}
}
private double Precision{
get{
return Math.Min(1,50/Aim_Distance);
}
}
private int Fire_Count=0;
private Vector3D Forward_Vector;
private Vector3D Backward_Vector{
get{
return -1*Forward_Vector;
}
}
private Vector3D Up_Vector;
private Vector3D Down_Vector{
get{
return -1*Up_Vector;
}
}
private Vector3D Left_Vector;
private Vector3D Right_Vector{
get{
return -1*Left_Vector;
}
}
private Queue<CannonTask> TaskQueue=new Queue<CannonTask>();
private CannonTask CurrentTask{
get{
if(TaskQueue.Count==0)
return CannonTask.None;
return TaskQueue.Peek();
}
}
private PrintStatus CurrentPrintStatus;
private FireStatus CurrentFireStatus;
public Vector3D GlobalToLocal(Vector3D Global){
Vector3D Local=Vector3D.Transform(Global+Controller.GetPosition(), MatrixD.Invert(Controller.WorldMatrix));
Local.Normalize();
return Local*Global.Length();
}
public Vector3D GlobalToLocalPosition(Vector3D Global){
Vector3D Local=Vector3D.Transform(Global, MatrixD.Invert(Controller.WorldMatrix));
Local.Normalize();
return Local*(Global-Controller.GetPosition()).Length();
}
public Vector3D LocalToGlobal(Vector3D Local){
Vector3D Global=Vector3D.Transform(Local, Controller.WorldMatrix)-Controller.GetPosition();
Global.Normalize();
return Global*Local.Length();
}
public Vector3D LocalToGlobalPosition(Vector3D Local){
return Vector3D.Transform(Local,Controller.WorldMatrix);
}
private double GetAngle(Vector3D v1, Vector3D v2){
return GenericMethods<IMyTerminalBlock>.GetAngle(v1,v2);
}
private bool Operational=false;
public Program(){
Rnd=new Random();
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=(new GenericMethods<IMyTextPanel>(this)).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;
Controller=(new GenericMethods<IMyRemoteControl>(this)).GetFull("Driver Controller");
if(Controller==null)
return;
Projector=(new GenericMethods<IMyProjector>(this)).GetFull("Shell Projector");
if(Projector==null)
return;
YawRotor=(new GenericMethods<IMyMotorStator>(this)).GetFull("Yaw Rotor");
if(YawRotor==null)
return;
PitchRotor=(new GenericMethods<IMyMotorStator>(this)).GetFull("Pitch Rotor");
if(PitchRotor==null)
return;
ShellRotor=(new GenericMethods<IMyMotorStator>(this)).GetFull("Shell Rotor");
if(ShellRotor==null)
return;
Sensor=(new GenericMethods<IMySensorBlock>(this)).GetFull("Driver Sensor");
if(Sensor==null)
return;
Welder=(new GenericMethods<IMyShipWelder>(this)).GetFull("Driver Shell Welder");
if(Welder==null)
return;
Merge=(new GenericMethods<IMyShipMergeBlock>(this)).GetFull("Shell Printer Merge Block");
if(Merge==null)
return;
Generators=(new GenericMethods<IMyGravityGenerator>(this)).GetAllContaining("Driver Generator ");
if(Generators.Count==0)
return;
Cameras=(new GenericMethods<IMyCameraBlock>(this)).GetAllContaining("Driver Camera ");
if(Cameras.Count==0)
return;
foreach(IMyCameraBlock Camera in Cameras)
Camera.EnableRaycast=true;
FiringLights=(new GenericMethods<IMyInteriorLight>(this)).GetAllContaining("Driver Firing Light ");
StatusLights=new List<List<IMyInteriorLight>>();
for(int i=1;i<=3;i++){
StatusLights.Add((new GenericMethods<IMyInteriorLight>(this)).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);
if(((int)CurrentTask)>=((int)CannonTask.Fire))
Runtime.UpdateFrequency=UpdateFrequency.Update10;
else
Runtime.UpdateFrequency=UpdateFrequency.Update100;
}
public void Save(){
this.Storage="FireStatus:"+((int)CurrentFireStatus).ToString();
this.Storage+="•PrintStatus:"+((int)CurrentPrintStatus).ToString();
this.Storage+="•AutoScan:"+AutoScan.ToString();
this.Storage+="•AutoFire:"+AutoFire.ToString();
while(TaskQueue.Count>0){
this.Storage+="•Task:"+((int)CurrentTask).ToString();
TaskQueue.Dequeue();
}
foreach(EntityInfo Entity in Targets){
this.Storage+="•Target:"+Entity.ToString();
}
Me.CustomData=this.Storage;
}
private void AddTask(CannonTask Task){
if(Task==CannonTask.None||TaskQueue.Contains(Task))
return;
if(((int)Task)>((int)CurrentTask)){
Queue<CannonTask> old=new Queue<CannonTask>();
while(TaskQueue.Count>0)
old.Enqueue(TaskQueue.Dequeue());
TaskQueue.Enqueue(Task);
while(old.Count>0)
TaskQueue.Enqueue(old.Dequeue());
}
else
TaskQueue.Enqueue(Task);
if(Task==CannonTask.Fire){
if(CurrentPrintStatus==PrintStatus.Ready)
CurrentFireStatus=FireStatus.Aiming;
else
CurrentFireStatus=FireStatus.Printing;
Fire_Count=3;
}
}
private void NextTask(){
bool remove=true;
CannonTask last_task=CurrentTask;
CurrentFireStatus=FireStatus.Idle;
if(CurrentTask==CannonTask.Fire){
Called_Next_Fire=true;
if(Fire_Count>1){
Fire_Count--;
remove=false;
}
else if(Targets.Count>0){
Targets.RemoveAt(0);
if(Targets.Count>0){
Fire_Count=3;
remove=false;
}
}
}
if(remove){
if(TaskQueue.Count>0)
TaskQueue.Dequeue();
if(TaskQueue.Count==0){
if(AutoScan)
TaskQueue.Enqueue(CannonTask.Scan);
else if(last_task!=CannonTask.Reset)
TaskQueue.Enqueue(CannonTask.Reset);
}
}
if(TaskQueue.Count==0){
PitchRotor.RotorLock=true;
YawRotor.RotorLock=true;
}
}
private 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;
}
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);
Me.GetSurface(0).WriteText("", 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");