/
MechJebModuleWaypointWindow.cs
1543 lines (1328 loc) · 66.2 KB
/
MechJebModuleWaypointWindow.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
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using JetBrains.Annotations;
using KSP.IO;
using KSP.Localization;
using UnityEngine;
using static MechJebLib.Utils.Statics;
namespace MuMech
{
public class MechJebWaypoint
{
public const float defaultRadius = 5;
public double Latitude;
public double Longitude;
public double Altitude;
public Vector3d Position;
public float Radius;
public string Name;
public Vessel Target;
public float MinSpeed;
public float MaxSpeed;
public bool Quicksave;
public CelestialBody Body => Target != null ? Target.mainBody : FlightGlobals.ActiveVessel.mainBody;
public MechJebWaypoint(double Latitude, double Longitude, float Radius = defaultRadius, string Name = "", float MinSpeed = 0,
float MaxSpeed = 0)
{
this.Latitude = Latitude;
this.Longitude = Longitude;
this.Radius = Radius;
this.Name = Name == null ? "" : Name;
this.MinSpeed = MinSpeed;
this.MaxSpeed = MaxSpeed;
Update();
}
public MechJebWaypoint(Vector3d Position, float Radius = defaultRadius, string Name = "", float MinSpeed = 0, float MaxSpeed = 0)
{
Latitude = Body.GetLatitude(Position);
Longitude = Body.GetLongitude(Position);
this.Radius = Radius;
this.Name = Name == null ? "" : Name;
this.MinSpeed = MinSpeed;
this.MaxSpeed = MaxSpeed;
Update();
}
public MechJebWaypoint(Vessel Target, float Radius = defaultRadius, string Name = "", float MinSpeed = 0, float MaxSpeed = 0)
{
this.Target = Target;
this.Radius = Radius;
this.Name = Name == null ? "" : Name;
this.MinSpeed = MinSpeed;
this.MaxSpeed = MaxSpeed;
Update();
}
public MechJebWaypoint(ConfigNode Node)
{
if (Node.HasValue("Latitude")) { double.TryParse(Node.GetValue("Latitude"), out Latitude); }
if (Node.HasValue("Longitude")) { double.TryParse(Node.GetValue("Longitude"), out Longitude); }
Target = Node.HasValue("Target") ? FlightGlobals.Vessels.Find(v => v.id.ToString() == Node.GetValue("Target")) : null;
if (Node.HasValue("Radius")) { float.TryParse(Node.GetValue("Radius"), out Radius); }
else { Radius = defaultRadius; }
Name = Node.HasValue("Name") ? Node.GetValue("Name") : "";
if (Node.HasValue("MinSpeed")) { float.TryParse(Node.GetValue("MinSpeed"), out MinSpeed); }
if (Node.HasValue("MaxSpeed")) { float.TryParse(Node.GetValue("MaxSpeed"), out MaxSpeed); }
if (Node.HasValue("Quicksave")) { bool.TryParse(Node.GetValue("Quicksave"), out Quicksave); }
Update();
}
public ConfigNode ToConfigNode()
{
var cn = new ConfigNode("Waypoint");
if (Target != null)
{
cn.AddValue("Target", Target.id);
}
if (Name != "") { cn.AddValue("Name", Name); }
cn.AddValue("Latitude", Latitude);
cn.AddValue("Longitude", Longitude);
cn.AddValue("Radius", Radius);
cn.AddValue("MinSpeed", MinSpeed);
cn.AddValue("MaxSpeed", MaxSpeed);
cn.AddValue("Quicksave", Quicksave);
return cn;
}
public string GetNameWithCoords()
{
return (Name != "" ? Name : Target != null ? Target.vesselName : "Waypoint") + " - " + Coordinates.ToStringDMS(Latitude, Longitude);
// ((Latitude >= 0 ? "N " : "S ") + Math.Abs(Math.Round(Latitude, 3)) + ", " + (Longitude >= 0 ? "E " : "W ") + Math.Abs(Math.Round(Longitude, 3)));
}
public void Update()
{
if (Target != null)
{
Position = Target.CoM;
Latitude = Body.GetLatitude(Position);
Longitude = Body.GetLongitude(Position);
}
else
{
Position = Body.GetWorldSurfacePosition(Latitude, Longitude, Body.TerrainAltitude(Latitude, Longitude));
if (Vector3d.Distance(Position, FlightGlobals.ActiveVessel.CoM) < 200)
{
Vector3d dir = (Position - Body.position).normalized;
Vector3d rayPos = Body.position + dir * (Body.Radius + 50000);
dir = (Body.position - rayPos).normalized;
RaycastHit hit;
bool raycast = Physics.Raycast(rayPos, dir, out hit, (float)Body.Radius, 1 << 15, QueryTriggerInteraction.Ignore);
if (raycast)
{
dir = hit.point - Body.position;
Position = Body.position + dir.normalized * (dir.magnitude + 0.5);
// Latitude = Body.GetLatitude(Position);
// Longitude = Body.GetLongitude(Position);
}
}
}
if (MinSpeed > 0 && MaxSpeed > 0 && MinSpeed > MaxSpeed) { MinSpeed = MaxSpeed; }
else if (MinSpeed > 0 && MaxSpeed > 0 && MaxSpeed < MinSpeed) { MaxSpeed = MinSpeed; }
}
}
public class MechJebWaypointRoute : List<MechJebWaypoint>
{
public string Name;
public CelestialBody Body { get; }
public string Mode { get; }
private string stats;
public string Stats
{
get
{
updateStats(); // recalculation of the stats all the time are fine according to Majiir and Fractal_UK :3
return stats;
}
}
public ConfigNode ToConfigNode()
{
var cn = new ConfigNode("Waypoints");
cn.AddValue("Name", Name);
cn.AddValue("Body", Body.bodyName);
cn.AddValue("Mode", Mode);
ForEach(wp => cn.AddNode(wp.ToConfigNode()));
return cn;
}
private void updateStats()
{
float distance = 0;
if (Count > 1)
{
for (int i = 1; i < Count; i++)
{
distance += Vector3.Distance(this[i - 1].Position, this[i].Position);
}
}
stats = string.Format("{0} waypoints over {1}m", Count, distance.ToSI(-1));
}
public MechJebWaypointRoute(string Name = "", CelestialBody Body = null, string Mode = "Rover")
{
this.Name = Name;
this.Body = Body != null ? Body : FlightGlobals.currentMainBody;
this.Mode = Mode;
}
public MechJebWaypointRoute(ConfigNode Node)
{
// feed this "Waypoints" nodes, just not the local ones of a ship
if (Node == null) { return; }
Name = Node.HasValue("Name") ? Node.GetValue("Name") : "";
Body = Node.HasValue("Body") ? FlightGlobals.Bodies.Find(b => b.bodyName == Node.GetValue("Body")) : null;
Mode = Node.HasValue("Mode") ? Node.GetValue("Mode") : "Rover";
if (Node.HasNode("Waypoint"))
{
foreach (ConfigNode cn in Node.GetNodes("Waypoint"))
{
Add(new MechJebWaypoint(cn));
}
}
}
}
[UsedImplicitly]
public class MechJebModuleWaypointWindow : DisplayModule
{
public enum WaypointMode
{
Rover,
Plane
}
public WaypointMode Mode = WaypointMode.Rover;
public MechJebModuleRoverController ap;
public static List<MechJebWaypointRoute> Routes = new List<MechJebWaypointRoute>();
[EditableInfoItem("#MechJeb_MohoMapdist", InfoItem.Category.Rover)]
[Persistent(pass = (int)Pass.Global)] // Moho Mapdist
public EditableDouble MohoMapdist = 5000;
[EditableInfoItem("#MechJeb_EveMapdist", InfoItem.Category.Rover)]
[Persistent(pass = (int)Pass.Global)] // Eve Mapdist
public EditableDouble EveMapdist = 5000;
[EditableInfoItem("#MechJeb_GillyMapdist", InfoItem.Category.Rover)]
[Persistent(pass = (int)Pass.Global)] // Gilly Mapdist
public EditableDouble GillyMapdist = -500;
[EditableInfoItem("#MechJeb_KerbinMapdist", InfoItem.Category.Rover)]
[Persistent(pass = (int)Pass.Global)] // Kerbin Mapdist
public EditableDouble KerbinMapdist = 500;
[EditableInfoItem("#MechJeb_MunMapdist", InfoItem.Category.Rover)]
[Persistent(pass = (int)Pass.Global)] // Mun Mapdist
public EditableDouble MunMapdist = 4000;
[EditableInfoItem("#MechJeb_MinmusMapdist", InfoItem.Category.Rover)]
[Persistent(pass = (int)Pass.Global)] // Minmus Mapdist
public EditableDouble MinmusMapdist = 3500;
[EditableInfoItem("#MechJeb_DunaMapdist", InfoItem.Category.Rover)]
[Persistent(pass = (int)Pass.Global)] // Duna Mapdist
public EditableDouble DunaMapdist = 5000;
[EditableInfoItem("#MechJeb_IkeMapdist", InfoItem.Category.Rover)]
[Persistent(pass = (int)Pass.Global)] // Ike Mapdist
public EditableDouble IkeMapdist = 4000;
[EditableInfoItem("#MechJeb_DresMapdist", InfoItem.Category.Rover)]
[Persistent(pass = (int)Pass.Global)] // Dres Mapdist
public EditableDouble DresMapdist = 1500;
[EditableInfoItem("#MechJeb_EelooMapdist", InfoItem.Category.Rover)]
[Persistent(pass = (int)Pass.Global)] // Eeloo Mapdist
public EditableDouble EelooMapdist = 2000;
[EditableInfoItem("#MechJeb_JoolMapdist", InfoItem.Category.Rover)]
[Persistent(pass = (int)Pass.Global)] // Jool Mapdist
public EditableDouble JoolMapdist = 30000;
[EditableInfoItem("#MechJeb_TyloMapdist", InfoItem.Category.Rover)]
[Persistent(pass = (int)Pass.Global)] // Tylo Mapdist
public EditableDouble TyloMapdist = 5000;
[EditableInfoItem("#MechJeb_LaytheMapdist", InfoItem.Category.Rover)]
[Persistent(pass = (int)Pass.Global)] // Laythe Mapdist
public EditableDouble LaytheMapdist = 1000;
[EditableInfoItem("#MechJeb_PolMapdist", InfoItem.Category.Rover)]
[Persistent(pass = (int)Pass.Global)] // Pol Mapdist
public EditableDouble PolMapdist = 500;
[EditableInfoItem("#MechJeb_BopMapdist", InfoItem.Category.Rover)]
[Persistent(pass = (int)Pass.Global)] // Bop Mapdist
public EditableDouble BopMapdist = 1000;
[EditableInfoItem("#MechJeb_VallMapdist", InfoItem.Category.Rover)]
[Persistent(pass = (int)Pass.Global)] // Vall Mapdist
public EditableDouble VallMapdist = 5000;
internal int selIndex = -1;
internal int saveIndex = -1;
internal string tmpRadius = "";
internal string tmpMinSpeed = "";
internal string tmpMaxSpeed = "";
internal string tmpLat = "";
internal string tmpLon = "";
internal const string coordRegEx =
@"^([nsew])?\s*(-?\d+(?:\.\d+)?)(?:[°:\s]+(-?\d+(?:\.\d+)?))?(?:[':\s]+(-?\d+(?:\.\d+)?))?(?:[^nsew]*([nsew])?)?$";
private Vector2 scroll;
private GUIStyle styleActive;
private GUIStyle styleInactive;
private GUIStyle styleQuicksave;
private string titleAdd = "";
private string saveName = "";
private bool waitingForPick;
private pages showPage = pages.waypoints;
private enum pages { waypoints, settings, routes }
private static MechJebRouteRenderer renderer;
private Rect[] waypointRects = new Rect[0];
private int lastIndex = -1;
private int settingPageIndex;
private readonly string[] settingPages = { "Rover", "Waypoints" };
// private static LineRenderer redLine;
// private static LineRenderer greenLine;
public MechJebModuleWaypointWindow(MechJebCore core) : base(core) { }
public override void OnStart(PartModule.StartState state)
{
hidden = true;
ap = core.GetComputerModule<MechJebModuleRoverController>();
if (HighLogic.LoadedSceneIsFlight && vessel.isActiveVessel)
{
renderer = MechJebRouteRenderer.AttachToMapView(core);
renderer.enabled = enabled;
}
// GameObject obj = new GameObject("LineRenderer");
// redLine = obj.AddComponent<LineRenderer>();
// redLine.useWorldSpace = true;
// redLine.material = renderer.material;
// redLine.SetWidth(10.0f, 10.0f);
// redLine.SetColors(Color.red, Color.red);
// redLine.SetVertexCount(2);
// GameObject obj2 = new GameObject("LineRenderer");
// greenLine = obj2.AddComponent<LineRenderer>();
// greenLine.useWorldSpace = true;
// greenLine.material = renderer.material;
// greenLine.SetWidth(10.0f, 10.0f);
// greenLine.SetColors(Color.green, Color.green);
// greenLine.SetVertexCount(2);
// MechJebRouteRenderer.NewLineRenderer(ref greenLine);
base.OnStart(state);
}
public override void OnModuleEnabled()
{
if (renderer != null) { renderer.enabled = true; }
base.OnModuleEnabled();
}
public override void OnModuleDisabled()
{
if (renderer != null) { renderer.enabled = false; }
base.OnModuleDisabled();
}
public override void OnLoad(ConfigNode local, ConfigNode type, ConfigNode global)
{
base.OnLoad(local, type, global);
if (!FlightGlobals.ready) return; // bodies not loaded yet
var wps = new ConfigNode("Routes");
if (File.Exists<MechJebCore>("mechjeb_routes.cfg"))
{
try
{
wps = ConfigNode.Load(MuUtils.GetCfgPath("mechjeb_routes.cfg"));
}
catch (Exception e)
{
Debug.LogError("MechJebModuleWaypointWindow.OnLoad caught an exception trying to load mechjeb_routes.cfg: " + e);
}
}
if (wps.HasNode("Waypoints"))
{
Routes.Clear();
foreach (ConfigNode cn in wps.GetNodes("Waypoints"))
{
Routes.Add(new MechJebWaypointRoute(cn));
}
Routes.Sort(SortRoutes);
}
}
public void SaveRoutes()
{
var cn = new ConfigNode("Routes");
if (Routes.Count > 0)
{
Routes.Sort(SortRoutes);
foreach (MechJebWaypointRoute r in Routes)
{
cn.AddNode(r.ToConfigNode());
}
}
cn.Save(MuUtils.GetCfgPath("mechjeb_routes.cfg"));
}
public override string GetName()
{
return Mode + " Waypoints" + (titleAdd != "" ? " - " + titleAdd : "");
}
public static Coordinates GetMouseFlightCoordinates()
{
CelestialBody body = FlightGlobals.currentMainBody;
Ray mouseRay = FlightCamera.fetch.mainCamera.ScreenPointToRay(Input.mousePosition);
RaycastHit raycast;
Vector3d relOrigin = mouseRay.origin - body.position;
Vector3d relSurfacePosition;
if (Physics.Raycast(mouseRay, out raycast, (float)body.Radius * 4f, 1 << 15, QueryTriggerInteraction.Ignore))
{
return new Coordinates(body.GetLatitude(raycast.point), MuUtils.ClampDegrees180(body.GetLongitude(raycast.point)));
}
double curRadius = body.pqsController.radiusMax;
double lastRadius = 0;
double error = 0;
int loops = 0;
float st = Time.time;
while (loops < 50)
{
if (PQS.LineSphereIntersection(relOrigin, mouseRay.direction, curRadius, out relSurfacePosition))
{
Vector3d surfacePoint = body.position + relSurfacePosition;
double alt = body.pqsController.GetSurfaceHeight(QuaternionD.AngleAxis(body.GetLongitude(surfacePoint), Vector3d.down) *
QuaternionD.AngleAxis(body.GetLatitude(surfacePoint), Vector3d.forward) *
Vector3d.right);
error = Math.Abs(curRadius - alt);
if (error < (body.pqsController.radiusMax - body.pqsController.radiusMin) / 100)
{
return new Coordinates(body.GetLatitude(surfacePoint), MuUtils.ClampDegrees180(body.GetLongitude(surfacePoint)));
}
lastRadius = curRadius;
curRadius = alt;
loops++;
}
else
{
if (loops == 0)
{
break;
} // Went too low, needs to try higher
curRadius = (lastRadius * 9 + curRadius) / 10;
loops++;
}
}
return null;
// var cam = FlightCamera.fetch.mainCamera;
// Ray ray = cam.ScreenPointToRay(Input.mousePosition);
//// greenLine.SetPosition(0, ray.origin);
//// greenLine.SetPosition(1, (Vector3d)ray.direction * body.Radius / 2);
//// if (Physics.Raycast(ray, out raycast, (float)body.Radius * 4f, ~(1 << 1))) {
// Vector3d hit;
// //body.pqsController.RayIntersection(ray.origin, ray.direction, out hit);
// PQS.LineSphereIntersection(ray.origin - body.position, ray.direction, body.Radius, out hit);
// if (hit != Vector3d.zero) {
// hit = body.position + hit;
// Vector3d start = ray.origin;
// Vector3d end = hit;
// Vector3d point = Vector3d.zero;
// for (int i = 0; i < 16; i++) {
// point = (start + end) / 2;
// //var lat = body.GetLatitude(point);
// //var lon = body.GetLongitude(point);
// //var surf = body.GetWorldSurfacePosition(lat, lon, body.TerrainAltitude(lat, lon));
// var alt = body.GetAltitude(point) - body.TerrainAltitude(point);
// //Debug.Log(alt);
// if (alt > 0) {
// start = point;
// }
// else if (alt < 0) {
// end = point;
// }
// else {
// break;
// }
// }
// hit = point;
//// redLine.SetPosition(0, ray.origin);
//// redLine.SetPosition(1, hit);
// return new Coordinates(body.GetLatitude(hit), MuUtils.ClampDegrees180(body.GetLongitude(hit)));
// }
// else {
// return null;
// }
}
public static string LatToString(double Lat)
{
while (Lat > 90) { Lat -= 180; }
while (Lat < -90) { Lat += 180; }
string ns = Lat >= 0 ? "N" : "S";
Lat = Math.Abs(Lat);
int h = (int)Lat;
Lat -= h;
Lat *= 60;
int m = (int)Lat;
Lat -= m;
Lat *= 60;
float s = (float)Lat;
return string.Format("{0} {1}° {2}' {3:F3}\"", ns, h, m, s);
}
public static string LonToString(double Lon)
{
while (Lon > 180) { Lon -= 360; }
while (Lon < -180) { Lon += 360; }
string ew = Lon >= 0 ? "E" : "W";
Lon = Math.Abs(Lon);
int h = (int)Lon;
Lon -= h;
Lon *= 60;
int m = (int)Lon;
Lon -= m;
Lon *= 60;
float s = (float)Lon;
return string.Format("{0} {1}° {2}' {3:F3}\"", ew, h, m, s);
}
public static double ParseCoord(string LatLon, bool IsLongitute = false)
{
Match match = new Regex(coordRegEx, RegexOptions.IgnoreCase).Match(LatLon);
int range = IsLongitute ? 180 : 90;
float nsew = 1;
if (match.Groups[5] != null)
{
if (match.Groups[5].Value.ToUpper() == "N" || match.Groups[5].Value.ToUpper() == "E") { nsew = 1; }
else if (match.Groups[5].Value.ToUpper() == "S" || match.Groups[5].Value.ToUpper() == "W") { nsew = -1; }
else if (match.Groups[1] != null)
{
if (match.Groups[1].Value.ToUpper() == "N" || match.Groups[1].Value.ToUpper() == "E") { nsew = 1; }
else if (match.Groups[1].Value.ToUpper() == "S" || match.Groups[1].Value.ToUpper() == "W") { nsew = -1; }
}
}
float h = 0;
if (match.Groups[2] != null) { float.TryParse(match.Groups[2].Value, out h); }
if (h < 0)
{
nsew *= -1;
h *= -1;
}
float m = 0;
if (match.Groups[3] != null) { float.TryParse(match.Groups[3].Value, out m); }
float s = 0;
if (match.Groups[4] != null) { float.TryParse(match.Groups[4].Value, out s); }
h = (h + m / 60 + s / 3600) * nsew;
while (h > range) { h -= range * 2; }
while (h < -range) { h += range * 2; }
return h;
}
private int SortRoutes(MechJebWaypointRoute a, MechJebWaypointRoute b)
{
int bn = string.Compare(a.Body.bodyName, b.Body.bodyName, true);
if (bn != 0)
{
return bn;
}
return string.Compare(a.Name, b.Name, true);
}
public MechJebWaypoint SelectedWaypoint()
{
return selIndex > -1 ? ap.Waypoints[selIndex] : null;
}
public int SelectedWaypointIndex
{
get => selIndex;
set => selIndex = value;
}
public override GUILayoutOption[] WindowOptions()
{
return new[] { GUILayout.Width(500), GUILayout.Height(400) };
}
public void DrawPageWaypoints()
{
bool alt = GameSettings.MODIFIER_KEY.GetKey();
scroll = GUILayout.BeginScrollView(scroll);
if (ap.Waypoints.Count > 0)
{
waypointRects = new Rect[ap.Waypoints.Count];
GUILayout.BeginVertical();
double eta = 0;
double dist = 0;
for (int i = 0; i < ap.Waypoints.Count; i++)
{
MechJebWaypoint wp = ap.Waypoints[i];
double maxSpeed = wp.MaxSpeed > 0 ? wp.MaxSpeed : ap.speed.val;
float minSpeed = wp.MinSpeed > 0 ? wp.MinSpeed : 0;
if (MapView.MapIsEnabled && i == selIndex)
{
GLUtils.DrawGroundMarker(mainBody, wp.Latitude, wp.Longitude, Color.red, true,
(DateTime.Now.Second + DateTime.Now.Millisecond / 1000f) * 6, mainBody.Radius / 100);
}
if (i >= ap.WaypointIndex)
{
if (ap.WaypointIndex > -1)
{
eta += GuiUtils.FromToETA(i == ap.WaypointIndex ? vessel.CoM : (Vector3)ap.Waypoints[i - 1].Position, wp.Position,
ap.etaSpeed > 0.1 && ap.etaSpeed < maxSpeed ? (float)Math.Round(ap.etaSpeed, 1) : maxSpeed);
}
dist += Vector3.Distance(
i == ap.WaypointIndex || (ap.WaypointIndex == -1 && i == 0) ? vessel.CoM : (Vector3)ap.Waypoints[i - 1].Position,
wp.Position);
}
string str = string.Format("[{0}] - {1} - R: {2:F1} m\n S: {3:F0} ~ {4:F0} - D: {5}m - ETA: {6}", i + 1,
wp.GetNameWithCoords(), wp.Radius,
minSpeed, maxSpeed, dist.ToSI(-1), GuiUtils.TimeToDHMS(eta));
GUI.backgroundColor = i == ap.WaypointIndex ? new Color(0.5f, 1f, 0.5f) : Color.white;
if (GUILayout.Button(str, i == selIndex ? styleActive : wp.Quicksave ? styleQuicksave : styleInactive))
{
if (alt)
{
ap.WaypointIndex = ap.WaypointIndex == i ? -1 : i;
// set current waypoint or unset it if it's already the current one
}
else
{
if (selIndex == i)
{
selIndex = -1;
}
else
{
selIndex = i;
tmpRadius = wp.Radius.ToString();
tmpMinSpeed = wp.MinSpeed.ToString();
tmpMaxSpeed = wp.MaxSpeed.ToString();
tmpLat = LatToString(wp.Latitude);
tmpLon = LonToString(wp.Longitude);
}
}
}
if (Event.current.type == EventType.Repaint)
{
waypointRects[i] = GUILayoutUtility.GetLastRect();
//if (i == ap.WaypointIndex) { Debug.Log(Event.current.type.ToString() + " - " + waypointRects[i].ToString() + " - " + scroll.ToString()); }
}
GUI.backgroundColor = Color.white;
if (selIndex > -1 && selIndex == i)
{
GUILayout.BeginHorizontal();
GUILayout.Label(" Radius: ", GUILayout.ExpandWidth(false));
tmpRadius = GUILayout.TextField(tmpRadius, GUILayout.Width(50));
float.TryParse(tmpRadius, out wp.Radius);
if (GUILayout.Button("A", GUILayout.ExpandWidth(false)))
{
ap.Waypoints.GetRange(i, ap.Waypoints.Count - i).ForEach(fewp => fewp.Radius = wp.Radius);
}
GUILayout.Label("- Speed: ", GUILayout.ExpandWidth(false));
tmpMinSpeed = GUILayout.TextField(tmpMinSpeed, GUILayout.Width(40));
float.TryParse(tmpMinSpeed, out wp.MinSpeed);
if (GUILayout.Button("A", GUILayout.ExpandWidth(false)))
{
ap.Waypoints.GetRange(i, ap.Waypoints.Count - i).ForEach(fewp => fewp.MinSpeed = wp.MinSpeed);
}
GUILayout.Label(" - ", GUILayout.ExpandWidth(false));
tmpMaxSpeed = GUILayout.TextField(tmpMaxSpeed, GUILayout.Width(40));
float.TryParse(tmpMaxSpeed, out wp.MaxSpeed);
if (GUILayout.Button("A", GUILayout.ExpandWidth(false)))
{
ap.Waypoints.GetRange(i, ap.Waypoints.Count - i).ForEach(fewp => fewp.MaxSpeed = wp.MaxSpeed);
}
GUILayout.FlexibleSpace();
if (GUILayout.Button("QS", wp.Quicksave ? styleQuicksave : styleInactive, GUILayout.ExpandWidth(false)))
{
if (alt)
{
ap.Waypoints.GetRange(i, ap.Waypoints.Count - i).ForEach(fewp => fewp.Quicksave = !fewp.Quicksave);
}
else
{
wp.Quicksave = !wp.Quicksave;
}
}
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal();
GUILayout.Label("Lat ", GUILayout.ExpandWidth(false));
tmpLat = GUILayout.TextField(tmpLat, GUILayout.Width(125));
wp.Latitude = ParseCoord(tmpLat);
GUILayout.Label(" - Lon ", GUILayout.ExpandWidth(false));
tmpLon = GUILayout.TextField(tmpLon, GUILayout.Width(125));
wp.Longitude = ParseCoord(tmpLon, true);
GUILayout.EndHorizontal();
}
}
titleAdd = "Distance: " + dist.ToSI(-1) + "m - ETA: " + GuiUtils.TimeToDHMS(eta);
GUILayout.EndVertical();
}
else
{
titleAdd = "";
}
GUILayout.EndScrollView();
GUILayout.BeginHorizontal();
if (GUILayout.Button(alt ? "Reverse" : !waitingForPick ? "Add Waypoint" : "Abort Adding", GUILayout.Width(110)))
{
if (alt)
{
ap.Waypoints.Reverse();
if (ap.WaypointIndex > -1) { ap.WaypointIndex = ap.Waypoints.Count - 1 - ap.WaypointIndex; }
if (selIndex > -1) { selIndex = ap.Waypoints.Count - 1 - selIndex; }
}
else
{
if (!waitingForPick)
{
waitingForPick = true;
if (MapView.MapIsEnabled)
{
core.Target.Unset();
core.Target.PickPositionTargetOnMap();
}
}
else
{
waitingForPick = false;
}
}
}
if (GUILayout.Button(alt ? "Clear" : "Remove", GUILayout.Width(65)) && ap.Waypoints.Count > 0)
{
if (alt)
{
ap.WaypointIndex = -1;
ap.Waypoints.Clear();
}
else if (selIndex >= 0)
{
ap.Waypoints.RemoveAt(selIndex);
if (ap.WaypointIndex > selIndex) { ap.WaypointIndex--; }
if (ap.WaypointIndex >= ap.Waypoints.Count) { ap.WaypointIndex = ap.Waypoints.Count - 1; }
}
selIndex = -1;
}
if (GUILayout.Button(alt ? "Top" : "Up", GUILayout.Width(57)) && selIndex > 0 && selIndex < ap.Waypoints.Count && ap.Waypoints.Count >= 2)
{
if (alt)
{
MechJebWaypoint t = ap.Waypoints[selIndex];
ap.Waypoints.RemoveAt(selIndex);
ap.Waypoints.Insert(0, t);
selIndex = 0;
}
else
{
ap.Waypoints.Swap(selIndex, --selIndex);
}
}
if (GUILayout.Button(alt ? "Bottom" : "Down", GUILayout.Width(57)) && selIndex >= 0 && selIndex < ap.Waypoints.Count - 1 &&
ap.Waypoints.Count >= 2)
{
if (alt)
{
MechJebWaypoint t = ap.Waypoints[selIndex];
ap.Waypoints.RemoveAt(selIndex);
ap.Waypoints.Add(t);
selIndex = ap.Waypoints.Count - 1;
}
else
{
ap.Waypoints.Swap(selIndex, ++selIndex);
}
}
if (GUILayout.Button("Routes"))
{
showPage = pages.routes;
scroll = Vector2.zero;
}
if (GUILayout.Button("Settings"))
{
showPage = pages.settings;
scroll = Vector2.zero;
}
GUILayout.EndHorizontal();
if (selIndex >= ap.Waypoints.Count) { selIndex = -1; }
if (selIndex == -1 && ap.WaypointIndex > -1 && lastIndex != ap.WaypointIndex && waypointRects.Length > 0)
{
scroll.y = waypointRects[ap.WaypointIndex].y - 160;
}
lastIndex = ap.WaypointIndex;
}
public void DrawPageSettings()
{
bool alt = GameSettings.MODIFIER_KEY.GetKey();
titleAdd = "Settings";
MechJebModuleCustomWindowEditor ed = core.GetComputerModule<MechJebModuleCustomWindowEditor>();
if (!ap.enabled) { ap.CalculateTraction(); } // keep calculating traction just for displaying it
scroll = GUILayout.BeginScrollView(scroll);
settingPageIndex = GUILayout.SelectionGrid(settingPageIndex, settingPages, settingPages.Length);
switch (settingPageIndex)
{
case 0:
GUILayout.BeginHorizontal();
GUILayout.BeginVertical();
ed.registry.Find(i => i.id == "Editable:RoverController.hPIDp").DrawItem();
ed.registry.Find(i => i.id == "Editable:RoverController.hPIDi").DrawItem();
ed.registry.Find(i => i.id == "Editable:RoverController.hPIDd").DrawItem();
ed.registry.Find(i => i.id == "Editable:RoverController.terrainLookAhead").DrawItem();
// ed.registry.Find(i => i.id == "Value:RoverController.speedIntAcc").DrawItem();
ed.registry.Find(i => i.id == "Editable:RoverController.tractionLimit").DrawItem();
ed.registry.Find(i => i.id == "Toggle:RoverController.LimitAcceleration").DrawItem();
GUILayout.EndVertical();
GUILayout.BeginVertical();
ed.registry.Find(i => i.id == "Editable:RoverController.sPIDp").DrawItem();
ed.registry.Find(i => i.id == "Editable:RoverController.sPIDi").DrawItem();
ed.registry.Find(i => i.id == "Editable:RoverController.sPIDd").DrawItem();
ed.registry.Find(i => i.id == "Editable:RoverController.turnSpeed").DrawItem();
ed.registry.Find(i => i.id == "Value:RoverController.traction").DrawItem();
GUILayout.EndVertical();
GUILayout.EndHorizontal();
break;
case 1:
GUILayout.BeginHorizontal();
GUILayout.BeginVertical();
ed.registry.Find(i => i.id == "Editable:WaypointWindow.MohoMapdist").DrawItem();
ed.registry.Find(i => i.id == "Editable:WaypointWindow.EveMapdist").DrawItem();
ed.registry.Find(i => i.id == "Editable:WaypointWindow.GillyMapdist").DrawItem();
ed.registry.Find(i => i.id == "Editable:WaypointWindow.KerbinMapdist").DrawItem();
ed.registry.Find(i => i.id == "Editable:WaypointWindow.MunMapdist").DrawItem();
ed.registry.Find(i => i.id == "Editable:WaypointWindow.MinmusMapdist").DrawItem();
ed.registry.Find(i => i.id == "Editable:WaypointWindow.DunaMapdist").DrawItem();
ed.registry.Find(i => i.id == "Editable:WaypointWindow.IkeMapdist").DrawItem();
GUILayout.EndVertical();
GUILayout.BeginVertical();
ed.registry.Find(i => i.id == "Editable:WaypointWindow.DresMapdist").DrawItem();
ed.registry.Find(i => i.id == "Editable:WaypointWindow.JoolMapdist").DrawItem();
ed.registry.Find(i => i.id == "Editable:WaypointWindow.LaytheMapdist").DrawItem();
ed.registry.Find(i => i.id == "Editable:WaypointWindow.VallMapdist").DrawItem();
ed.registry.Find(i => i.id == "Editable:WaypointWindow.TyloMapdist").DrawItem();
ed.registry.Find(i => i.id == "Editable:WaypointWindow.BopMapdist").DrawItem();
ed.registry.Find(i => i.id == "Editable:WaypointWindow.PolMapdist").DrawItem();
ed.registry.Find(i => i.id == "Editable:WaypointWindow.EelooMapdist").DrawItem();
GUILayout.EndVertical();
GUILayout.EndHorizontal();
break;
}
GUILayout.EndScrollView();
GUILayout.BeginHorizontal();
if (GUILayout.Button("Waypoints"))
{
showPage = pages.waypoints;
scroll = Vector2.zero;
lastIndex = -1;
}
if (GUILayout.Button("Routes"))
{
showPage = pages.routes;
scroll = Vector2.zero;
}
if (GUILayout.Button("Help"))
{
core.GetComputerModule<MechJebModuleWaypointHelpWindow>().enabled =
!core.GetComputerModule<MechJebModuleWaypointHelpWindow>().enabled;
}
GUILayout.EndHorizontal();
}
public void DrawPageRoutes()
{
bool alt = GameSettings.MODIFIER_KEY.GetKey();
titleAdd = "Routes for " + vessel.mainBody.bodyName;
scroll = GUILayout.BeginScrollView(scroll);
List<MechJebWaypointRoute> bodyWPs = Routes.FindAll(r => r.Body == vessel.mainBody && r.Mode == Mode.ToString());
for (int i = 0; i < bodyWPs.Count; i++)
{
GUILayout.BeginHorizontal();
string str = bodyWPs[i].Name + " - " + bodyWPs[i].Stats;
if (GUILayout.Button(str, i == saveIndex ? styleActive : styleInactive))
{
saveIndex = saveIndex == i ? -1 : i;
}
if (i == saveIndex)
{
if (GUILayout.Button("Delete", GUILayout.Width(70)))
{
Routes.RemoveAll(r => r.Name == bodyWPs[i].Name && r.Body == vessel.mainBody && r.Mode == Mode.ToString());
SaveRoutes();
saveIndex = -1;
}
}
GUILayout.EndHorizontal();
}
GUILayout.EndScrollView();
GUILayout.BeginHorizontal();
saveName = GUILayout.TextField(saveName, GUILayout.Width(150));
if (GUILayout.Button("Save", GUILayout.Width(50)))
{
if (saveName != "" && ap.Waypoints.Count > 0)
{
MechJebWaypointRoute old = Routes.Find(r => r.Name == saveName && r.Body == vessel.mainBody && r.Mode == Mode.ToString());
var wps = new MechJebWaypointRoute(saveName, vessel.mainBody);
ap.Waypoints.ForEach(wp => wps.Add(wp));
if (old == null)
{
Routes.Add(wps);
}
else
{
Routes[Routes.IndexOf(old)] = wps;
}
Routes.Sort(SortRoutes);
SaveRoutes();
}
}
if (GUILayout.Button(alt ? "Add" : "Load", GUILayout.Width(50)))
{
if (saveIndex > -1)