-
Notifications
You must be signed in to change notification settings - Fork 0
/
AutoTaxi.cs
1330 lines (1237 loc) · 35.9 KB
/
AutoTaxi.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
const string Program_Name = "AutoTaxi Drone AI"; //Name me!
Color DEFAULT_TEXT_COLOR=new Color(197,137,255,255);
Color DEFAULT_BACKGROUND_COLOR=new Color(44,0,88,255);
class Prog{
public static MyGridProgram P;
}
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 Dock{
public Vector3D Position;
public Vector3D Orientation;
public Vector3D Return;
public bool DoUp;
public Vector3D Up;
public Dock(Vector3D p,Vector3D o,Vector3D r){
Position=p;
Orientation=o;
Orientation.Normalize();
Return=r;
Up=new Vector3D(0,0,1);
DoUp=false;
}
public Dock(Vector3D p,Vector3D o,Vector3D r,Vector3D u):this(p,o,r){
Up=u;
DoUp=true;
}
public override string ToString(){
if(DoUp)
return '('+Position.ToString()+';'+Orientation.ToString()+';'+Return.ToString()+';'+Up.ToString()+')';
else
return '('+Position.ToString()+';'+Orientation.ToString()+';'+Return.ToString()+')';
}
public static bool TryParse(string Parse,out Dock output){
output=null;
if(Parse[0]!='('||Parse[Parse.Length-1]!=')')
return false;
Parse=Parse.Substring(1,Parse.Length-2);
string[] args=Parse.Split(';');
if(args.Length!=3&&args.Length!=4)
return false;
Vector3D p;
if(!Vector3D.TryParse(args[0],out p))
return false;
Vector3D o;
if((!Vector3D.TryParse(args[1],out o))||o.Length()==0)
return false;
Vector3D r;
if(!Vector3D.TryParse(args[2],out r))
return false;
output=new Dock(p,o,r);
Vector3D u;
if(args.Length==4&&Vector3D.TryParse(args[3],out u)){
output.Up=u;
output.DoUp=true;
}
return true;
}
}
enum DroneTask{
None=0,
Docked=1,
Docking=2,
Traveling=3
}
TimeSpan FromSeconds(double seconds){
return (new TimeSpan(0,0,0,(int)seconds,(int)(seconds*1000)%1000));
}
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";
}
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 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);
}
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);
}
string GetRemovedString(string big_string, string small_string){
string output=big_string;
if(big_string.Contains(small_string)){
output=big_string.Substring(0, big_string.IndexOf(small_string))+big_string.Substring(big_string.IndexOf(small_string)+small_string.Length);
}
return output;
}
TimeSpan Time_Since_Start=new TimeSpan(0);
long cycle=0;
char loading_char='|';
double seconds_since_last_update=0;
TimeSpan Current_Time;
double Cycle_Time{
get{
return Current_Time.TotalSeconds%600;
}
}
Stack<DroneTask> Tasks;
Queue<Dock> Docks;
Dock MyDock{
get{
if(Docks.Count>0)
return Docks.Peek();
return null;
}
}
IMyRemoteControl Controller;
IMyGyro Gyroscope;
IMyRadioAntenna Antenna;
IMyShipConnector Connector;
List<IMyBatteryBlock> Batteries;
List<IMyTextPanel> LCDs;
List<IMyDoor> Doors;
IMyBeacon Beacon;
IMySoundBlock Sound;
bool Running=false;
float Charge{
get{
float current=0,max=0;
foreach(IMyBatteryBlock B in Batteries){
current+=B.CurrentStoredPower;
max+=B.MaxStoredPower;
}
return current/max;
}
}
double ShipMass{
get{
return Controller.CalculateShipMass().TotalMass;
}
}
bool Match_Direction=false;
bool Slow_Down=true;
Vector3D Target_Direction=new Vector3D(0,1,0);
bool Match_Position=false;
Vector3D Pseudo_Target=new Vector3D(0,0,0);
Vector3D Relative_Pseudo_Target{
get{
return GlobalToLocalPosition(Pseudo_Target,Controller);
}
}
Vector3D Target_Position=new Vector3D(0,0,0);
double Target_Distance{
get{
return (Target_Position-Controller.GetPosition()).Length();
}
}
Vector3D RestingVelocity=new Vector3D(0,0,0);
Vector3D Relative_RestingVelocity{
get{
return GlobalToLocal(RestingVelocity,Controller);
}
}
Vector3D LinearVelocity;
Vector3D Relative_LinearVelocity{
get{
Vector3D output=Vector3D.Transform(LinearVelocity+Controller.GetPosition(),MatrixD.Invert(Controller.WorldMatrix));
output.Normalize();
output*=LinearVelocity.Length();
return output;
}
}
double Speed_Deviation{
get{
return (LinearVelocity-RestingVelocity).Length();
}
}
double Acceleration{
get{
return (Forward_Thrust+Backward_Thrust)/(2*ShipMass);
}
}
double Time_To_Resting{
get{
return (RestingVelocity-LinearVelocity).Length()/Acceleration;
}
}
double Distance_To_Resting{
get{
return Acceleration*Math.Pow(Time_To_Resting,2)/2;
}
}
double Distance_To_Base{
get{
return (Controller.GetPosition()-MyDock.Return).Length();
}
}
double Speed_Limit=100;
Vector3D AngularVelocity;
Vector3D Relative_AngularVelocity{
get{
return GlobalToLocal(AngularVelocity,Controller);
}
}
List<IMyThrust>[] All_Thrusters=new List<IMyThrust>[6];
List<IMyThrust> Forward_Thrusters{
set{
All_Thrusters[0]=value;
}
get{
return All_Thrusters[0];
}
}
List<IMyThrust> Backward_Thrusters{
set{
All_Thrusters[1]=value;
}
get{
return All_Thrusters[1];
}
}
List<IMyThrust> Up_Thrusters{
set{
All_Thrusters[2]=value;
}
get{
return All_Thrusters[2];
}
}
List<IMyThrust> Down_Thrusters{
set{
All_Thrusters[3]=value;
}
get{
return All_Thrusters[3];
}
}
List<IMyThrust> Left_Thrusters{
set{
All_Thrusters[4]=value;
}
get{
return All_Thrusters[4];
}
}
List<IMyThrust> Right_Thrusters{
set{
All_Thrusters[5]=value;
}
get{
return All_Thrusters[5];
}
}
Base6Directions.Direction Forward;
Base6Directions.Direction Backward{
get{
return Base6Directions.GetOppositeDirection(Forward);
}
}
Base6Directions.Direction Up;
Base6Directions.Direction Down{
get{
return Base6Directions.GetOppositeDirection(Up);
}
}
Base6Directions.Direction Left;
Base6Directions.Direction Right{
get{
return Base6Directions.GetOppositeDirection(Left);
}
}
float GetThrust(int i){
float total=0;
foreach(IMyThrust T in All_Thrusters[i])
total+=T.MaxEffectiveThrust;
return Math.Max(total,1);
}
float Forward_Thrust{
get{
return GetThrust(0);
}
}
float Backward_Thrust{
get{
return GetThrust(1);
}
}
float Up_Thrust{
get{
return GetThrust(2);
}
}
float Down_Thrust{
get{
return GetThrust(3);
}
}
float Left_Thrust{
get{
return GetThrust(4);
}
}
float Right_Thrust{
get{
return GetThrust(5);
}
}
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;
}
}
string GetThrustTypeName(IMyThrust Thruster){
string block_type=Thruster.BlockDefinition.SubtypeName;
if(block_type.Contains("LargeBlock"))
block_type=GetRemovedString(block_type,"LargeBlock");
else if(block_type.Contains("SmallBlock"))
block_type=GetRemovedString(block_type,"SmallBlock");
if(block_type.Contains("Thrust"))
block_type=GetRemovedString(block_type,"Thrust");
string size="";
if(block_type.Contains("Small")){
size="Small";
block_type=GetRemovedString(block_type,size);
}
else if(block_type.Contains("Large")){
size="Large";
block_type=GetRemovedString(block_type,size);
}
if((!block_type.ToLower().Contains("atmospheric"))||(!block_type.ToLower().Contains("hydrogen")))
block_type+="Ion";
return (size+" "+block_type).Trim();
}
struct NameTuple{
public string Name;
public int Count;
public NameTuple(string n,int c=0){
Name=n;
Count=c;
}
}
void SetThrusterList(List<IMyThrust> Thrusters,string Direction){
List<NameTuple> Thruster_Types=new List<NameTuple>();
foreach(IMyThrust Thruster in Thrusters){
if(!HasBlockData(Thruster,"DefaultOverride"))
SetBlockData(Thruster,"DefaultOverride",Thruster.ThrustOverridePercentage.ToString());
SetBlockData(Thruster,"Owner",Me.CubeGrid.EntityId.ToString());
SetBlockData(Thruster,"DefaultName",Thruster.CustomName);
string name=GetThrustTypeName(Thruster);
bool found=false;
for(int i=0;i<Thruster_Types.Count;i++){
if(name.Equals(Thruster_Types[i].Name)){
found=true;
Thruster_Types[i]=new NameTuple(name,Thruster_Types[i].Count+1);
break;
}
}
if(!found)
Thruster_Types.Add(new NameTuple(name,1));
}
foreach(IMyThrust Thruster in Thrusters){
string name=GetThrustTypeName(Thruster);
for(int i=0;i<Thruster_Types.Count;i++){
if(name.Equals(Thruster_Types[i].Name)){
Thruster.CustomName=(Direction+" "+name+" Thruster "+(Thruster_Types[i].Count).ToString()).Trim();
Thruster_Types[i]=new NameTuple(name,Thruster_Types[i].Count-1);
break;
}
}
}
}
void ResetThruster(IMyThrust Thruster){
if(HasBlockData(Thruster,"DefaultOverride")){
float ThrustOverride=0.0f;
if(float.TryParse(GetBlockData(Thruster,"DefaultOverride"),out ThrustOverride))
Thruster.ThrustOverridePercentage=ThrustOverride;
else
Thruster.ThrustOverridePercentage=0.0f;
}
if(HasBlockData(Thruster,"DefaultName")){
Thruster.CustomName=GetBlockData(Thruster,"DefaultName");
}
SetBlockData(Thruster,"Owner","0");
}
public Program(){
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");
Docks=new Queue<Dock>();
Tasks=new Stack<DroneTask>();
string[] args=this.Storage.Split('•');
foreach(string arg in args){
if(arg.IndexOf("Tas:")==0){
int t;
if(Int32.TryParse(arg.Substring(4),out t))
Tasks.Push((DroneTask)t);
}
else if(arg.IndexOf("Doc:")==0){
Dock D;
if(!arg.Substring(4).Equals("null")){
Dock.TryParse(arg.Substring(4),out D);
Docks.Enqueue(D);
}
}
else if(arg.IndexOf("Run:")==0){
bool.TryParse(arg.Substring(4),out Running);
}
}
Controller=GenericMethods<IMyRemoteControl>.GetConstruct("Taxi Remote Control");
Gyroscope=GenericMethods<IMyGyro>.GetConstruct("Control Gyroscope");
Connector=GenericMethods<IMyShipConnector>.GetConstruct("Taxi Connector");
if(Controller==null||Gyroscope==null||Connector==null)
return;
Forward=Controller.Orientation.Forward;
Up=Controller.Orientation.Up;
Left=Controller.Orientation.Left;
List<IMyThrust> MyThrusters=GenericMethods<IMyThrust>.GetAllConstruct("");
for(int i=0;i<6;i++)
All_Thrusters[i]=new List<IMyThrust>();
for(int i=0;i<2;i++){
bool retry=!Me.CubeGrid.IsStatic;
foreach(IMyThrust Thruster in MyThrusters){
if(Thruster.CubeGrid!=Controller.CubeGrid)
continue;
retry=false;
Base6Directions.Direction ThrustDirection=Thruster.Orientation.Forward;
if(ThrustDirection==Backward)
Forward_Thrusters.Add(Thruster);
else if(ThrustDirection==Forward)
Backward_Thrusters.Add(Thruster);
else if(ThrustDirection==Down)
Up_Thrusters.Add(Thruster);
else if(ThrustDirection==Up)
Down_Thrusters.Add(Thruster);
else if(ThrustDirection==Right)
Left_Thrusters.Add(Thruster);
else if(ThrustDirection==Left)
Right_Thrusters.Add(Thruster);
}
if(!retry)
break;
}
SetThrusterList(Forward_Thrusters,"Forward");
SetThrusterList(Backward_Thrusters,"Backward");
SetThrusterList(Up_Thrusters,"Up");
SetThrusterList(Down_Thrusters,"Down");
SetThrusterList(Left_Thrusters,"Left");
SetThrusterList(Right_Thrusters,"Right");
Sound=GenericMethods<IMySoundBlock>.GetConstruct("Taxi Sound Block");
Beacon=GenericMethods<IMyBeacon>.GetConstruct("Taxi Beacon");
Doors=GenericMethods<IMyDoor>.GetAllConstruct("Taxi Door");
Antenna=GenericMethods<IMyRadioAntenna>.GetConstruct("Taxi Antenna");
Batteries=GenericMethods<IMyBatteryBlock>.GetAllConstruct("Taxi");
LCDs=GenericMethods<IMyTextPanel>.GetAllConstruct("Taxi LCD");
foreach(IMyTextPanel Panel in LCDs){
Panel.FontColor=DEFAULT_TEXT_COLOR;
Panel.BackgroundColor=DEFAULT_BACKGROUND_COLOR;
Panel.Alignment=TextAlignment.CENTER;
Panel.ContentType=ContentType.TEXT_AND_IMAGE;
Panel.WriteText("Taxi Service",false);
}
if(Batteries.Count==0)
return;
Runtime.UpdateFrequency=UpdateFrequency.Update1;
}
public void Save(){
this.Storage="•Run:"+Running.ToString();
foreach(Dock dock in Docks)
this.Storage+="•Doc:"+dock.ToString();
Stack<DroneTask> temp=new Stack<DroneTask>();
foreach(DroneTask T in Tasks)
temp.Push(T);
foreach(DroneTask T in temp)
this.Storage+="•Tas:"+((int)T).ToString();
}
void EndTask(bool do_pop=true){
DroneTask Last=DroneTask.None;
if(Tasks.Count>0){
Last=Tasks.Peek();
if(do_pop)
Tasks.Pop();
}
if(Tasks.Count==0)
Tasks.Push(DroneTask.None);
switch(Last){
case DroneTask.Docked:
foreach(IMyBatteryBlock B in Batteries)
B.ChargeMode=ChargeMode.Auto;
Connector.Disconnect();
foreach(IMyDoor Door in Doors)
Door.CloseDoor();
break;
case DroneTask.Docking:
Match_Position=false;
Match_Direction=false;
break;
case DroneTask.Traveling:
Match_Position=false;
Match_Direction=false;
Controller.ClearWaypoints();
Controller.SetAutoPilotEnabled(false);
Sound.Play();
break;
}
}
void Docked(){
if(MyDock==null){
EndTask();
return;
}
foreach(IMyBatteryBlock B in Batteries)
B.ChargeMode=ChargeMode.Recharge;
foreach(IMyDoor Door in Doors)
Door.OpenDoor();
Runtime.UpdateFrequency=UpdateFrequency.Update10;
}
void Docking(){
if(MyDock==null){
EndTask();
return;
}
Target_Direction=MyDock.Orientation;
Match_Direction=true;
Target_Position=MyDock.Position+10*MyDock.Orientation;
Match_Position=true;
Speed_Limit=2.5;
Vector3D angle=Controller.GetPosition()-MyDock.Position;
angle.Normalize();
if((Controller.GetPosition()-MyDock.Position).Length()<12&&GetAngle(MyDock.Orientation,angle)<5){
Target_Position=Controller.GetPosition()-Connector.GetPosition();
Target_Position=MyDock.Orientation*1.5+MyDock.Position+Target_Position;
Speed_Limit=1;
}
if(Connector.Status!=MyShipConnectorStatus.Unconnected)
Connector.Connect();
if(Connector.Status==MyShipConnectorStatus.Connected){
EndTask();
Tasks.Push(DroneTask.Docked);
}
Runtime.UpdateFrequency=UpdateFrequency.Update1;
}
void Traveling(){
Match_Position=false;
if(MyDock==null){
EndTask();
return;
}
MyWaypointInfo Destination=new MyWaypointInfo("Return to Base",MyDock.Return);
if((!Controller.CurrentWaypoint.Equals(Destination))||!Controller.IsAutoPilotEnabled){
Controller.ClearWaypoints();
Controller.AddWaypoint(Destination);
Controller.SetCollisionAvoidance(true);
Speed_Limit=Math.Max(5,Math.Min(100,Distance_To_Base/15));
Controller.SpeedLimit=(float)Speed_Limit;
Controller.SetAutoPilotEnabled(true);
}
if((Controller.GetPosition()-MyDock.Return).Length()<2.5){
EndTask();
Tasks.Push(DroneTask.Docking);
}
if(Match_Position)
Runtime.UpdateFrequency=UpdateFrequency.Update1;
else
Runtime.UpdateFrequency=UpdateFrequency.Update10;
}
void SetGyroscopes(){
if((!Match_Direction)||Controller.IsUnderControl||Controller.IsAutoPilotEnabled){
Gyroscope.GyroOverride=false;
Write("Gyroscope Controls:Off");
return;
}
Write("Gyroscope Controls:On");
if(Match_Direction)
Write("Match_Direction:"+Math.Round(GetAngle(Target_Direction,Forward_Vector),1).ToString()+'°');
Gyroscope.GyroOverride=(AngularVelocity.Length()<3);
float current_pitch=(float) Relative_AngularVelocity.X;
float current_yaw=(float) Relative_AngularVelocity.Y;
float current_roll=(float) Relative_AngularVelocity.Z;
float gyro_count = 0;
List<IMyGyro> AllGyros=new List<IMyGyro>();
GridTerminalSystem.GetBlocksOfType<IMyGyro>(AllGyros);
foreach(IMyGyro Gyro in AllGyros){
if(Gyro.IsWorking)
gyro_count+=Gyro.GyroPower/100.0f;
}
float gyro_multx=(float)Math.Max(0.1f,Math.Min(1,1.5f/(ShipMass/gyro_count/1000000)));
float input_pitch=current_pitch*0.99f;
double difference=GetAngle(Down_Vector,Target_Direction)-GetAngle(Up_Vector,Target_Direction);
if(Math.Abs(difference)>.1)
input_pitch-=(float)Math.Min(Math.Max(difference/5,-4),4)*gyro_multx;
float input_yaw=current_yaw*0.99f;
difference=GetAngle(Left_Vector,Target_Direction)-GetAngle(Right_Vector,Target_Direction);
if(Math.Abs(difference)>.1)
input_yaw+=(float)Math.Min(Math.Max(difference/5,-4),4)*gyro_multx;
float input_roll=current_roll*0.99f;
if(MyDock!=null&&MyDock.DoUp&&Tasks.Peek()==DroneTask.Docking){
difference=GetAngle(Left_Vector,MyDock.Up)-GetAngle(Right_Vector,MyDock.Up);
if(Math.Abs(difference)>.1)
input_roll+=(float)Math.Min(Math.Max(difference/5,-4),1)*gyro_multx;
}
Vector3D input=new Vector3D(input_pitch,input_yaw,input_roll);
Vector3D global=Vector3D.TransformNormal(input,Controller.WorldMatrix);
Vector3D output=Vector3D.TransformNormal(global,MatrixD.Invert(Gyroscope.WorldMatrix));
output.Normalize();
output*=input.Length();
Gyroscope.Pitch=(float)output.X;
Gyroscope.Yaw=(float)output.Y;
Gyroscope.Roll=(float)output.Z;
}