-
Notifications
You must be signed in to change notification settings - Fork 18
/
SetStationCustomizations.cs
1241 lines (1184 loc) · 53 KB
/
SetStationCustomizations.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 MetroOverhaul.NEXT.Extensions;
using System;
using System.Collections.Generic;
using System.Linq;
using MetroOverhaul.Extensions;
using UnityEngine;
using static MetroOverhaul.StationTrackCustomizations;
namespace MetroOverhaul
{
public static class SetStationCustomizations
{
public static float maxHorizontal = 10;
public static float minHorizontal = -10;
public static float defHorizontal = 0;
public static float maxVertical = 10;
public static float minVertical = -10;
public static float defVertical = 0;
public const float MAX_DEPTH = 36;
public const float MIN_DEPTH = 12;
public const float DEF_DEPTH = 15;
public const float MAX_ROTATION = 180;
public const float MIN_ROTATION = -180;
public const float DEF_ROTATION = 0;
public const float MAX_LENGTH = 184;
public const float MIN_LENGTH = 144;
public const float DEF_LENGTH = 144;
public const float MAX_CURVE = 90;
public const float MIN_CURVE = -90;
public const float DEF_CURVE = 0;
public static int m_PremierPath = -1;
public static float m_PrevAngle = 0;
private static List<BuildingInfo.PathInfo> m_StationPaths;
private static Dictionary<BuildingInfo.PathInfo, StationTrackCustomizations> m_PathCustomizationDict = new Dictionary<BuildingInfo.PathInfo, StationTrackCustomizations>();
public static Dictionary<BuildingInfo, List<BuildingInfo.PathInfo>> StationBuildingCustomizations;
private static StationTrackCustomizations m_PathCustomization;
private static Stack<BuildingInfo> m_BuildingStack = new Stack<BuildingInfo>();
private static Stack<ParentStationMetaData> m_ParentMetaDataStack = new Stack<ParentStationMetaData>();
private static BuildingInfo m_Info;
private static ParentStationMetaData m_ParentMetaData;
private static bool m_ResetMainRotation = false;
private static float StairCoeff { get { return 0.2f; } }// (11f / 64f); } }
private static List<Vector3> m_connectorNode = null;
private static bool IsAlterOfType(MetroStationTrackAlterType type)
{
return (PathCustomization.AlterType & type) != MetroStationTrackAlterType.None;
}
public static void ResetPathCustomizationDict()
{
m_PathCustomization = null;
m_PathCustomizationDict.Clear();
m_PathCustomizationDict = null;
}
public static Dictionary<BuildingInfo.PathInfo, StationTrackCustomizations> PathCustomizationDict {
get
{
if (m_PathCustomizationDict == null)
{
m_PathCustomizationDict = new Dictionary<BuildingInfo.PathInfo, StationTrackCustomizations>();
}
return m_PathCustomizationDict;
}
private set
{
m_PathCustomizationDict = value;
}
}
public static StationTrackCustomizations PathCustomization {
get
{
if (m_PathCustomization == null)
{
m_PathCustomization = new StationTrackCustomizations();
}
return m_PathCustomization;
}
private set
{
m_PathCustomization = value;
}
}
private static Vector3 AdjForOffset(Vector3 node, Vector3 offset, bool subToSuper = true)
{
var multiplier = subToSuper ? 1 : -1;
return new Vector3(node.x + (multiplier * offset.x), node.y, node.z - (multiplier * offset.z));
}
private static void FillInPathCustomizations()
{
foreach (var path in m_StationPaths)
{
if (PathCustomizationDict.ContainsKey(path))
{
PathCustomizationDict[path] = PathCustomization;
}
else
{
PathCustomizationDict.Add(path, PathCustomization);
}
}
if (PathCustomizationDict.ContainsKey(new BuildingInfo.PathInfo()))
{
PathCustomizationDict[new BuildingInfo.PathInfo()] = PathCustomization;
}
else
{
PathCustomizationDict.Add(new BuildingInfo.PathInfo(), PathCustomization);
}
}
public static List<Vector3> m_AnchorPoints = null;
public static List<Vector3> AnchorPoints {
get
{
if (m_AnchorPoints == null)
{
m_AnchorPoints = new List<Vector3>();
Debug.Log("Setting up AnchorPoints for " + m_Info);
if (m_ParentMetaData != null)
{
Debug.Log("SubBuilding " + m_Info.name + " is being checked for AnchorPoints");
var anchorPoints = m_Info.m_paths.Where(p => p.IsPedestrianPath() && p.m_nodes.Any(n => n.y < 0) && p.m_nodes.Any(n => n.y >= 0)).SelectMany(p => p.m_nodes).Where(n => n.y < 0);
if (anchorPoints != null && anchorPoints.Count() > 0)
{
m_AnchorPoints.AddRange(anchorPoints);
}
else
{
anchorPoints = m_ParentMetaData.Info.m_paths.Where(p => p.IsPedestrianPath() && p.m_nodes.Any(n => n.y == 0)).SelectMany(p => p.m_nodes).Where(n => n.y == 0).ToList();
m_AnchorPoints.AddRange(anchorPoints.Select(n => AdjustNodeFromParentToChild(n)));
}
}
else
{
Debug.Log("Building " + m_Info.name + " is being checked for AnchorPoints");
var pedPaths = m_Info.m_paths.Where(p => p.IsPedestrianPath());
var anchorPaths = pedPaths.Where(p => p.m_nodes.Any(n => n.y < 0) && p.m_nodes.Any(n => n.y >= 0)).ToList();
if (anchorPaths == null || anchorPaths.Count() == 0)
{
anchorPaths = pedPaths.OrderBy(p => p.m_nodes.Min(n => n.z)).ToList();
}
for (int i = 0; i < anchorPaths.Count(); i++)
{
var anchorNode = anchorPaths[i].m_nodes.OrderBy(p => p.y).FirstOrDefault();
m_AnchorPoints.Add(anchorNode);
if (anchorNode == default)
{
Debug.Log("No suitable anchorNodes found on building.");
foreach (var node in anchorPaths[i].m_nodes)
{
Debug.Log(node.ToString() + ", parent: " + AdjustNodeFromChildToParent(node).ToString());
}
}
Debug.Log("Anchorpoint " + anchorNode.ToString() + " found on building" + m_Info.name);
}
}
}
return m_AnchorPoints;
}
}
private static double m_AngleDelta = 0;
public static void ModifyStation(BuildingInfo info, StationTrackCustomizations pathCustomization, ParentStationMetaData parentMetaData = null)
{
Debug.Log("XXXXX STARTING " + info.name + " XXXXXXX");
m_BuildingStack.Push(info);
m_Info = info;
m_ParentMetaDataStack.Push(parentMetaData);
m_ParentMetaData = parentMetaData;
m_StationPaths = info.UndergroundStationPaths(false);
if (pathCustomization.Path.m_nodes == null && m_ResetMainRotation)
{
m_ResetMainRotation = false;
m_PrevAngle = 0;
}
var hasPaths = m_StationPaths.Count() > 0;
PathCustomization = pathCustomization;
ClearAnchorPoints();
if (PathCustomization.AlterType == MetroStationTrackAlterType.None)
PathCustomization.AlterType = MetroStationTrackAlterType.All;
if (PathCustomization.Depth <= 0 || PathCustomization.Length <= 0 || (!info.HasUndergroundMetroStationTracks() && (info?.m_subBuildings == null || info?.m_subBuildings.Count() == 0)))
{
return;
}
Debug.Log("PathIndex = " + m_StationPaths.IndexOf(PathCustomization.Path));
Debug.Log("PathCount = " + m_StationPaths.Count());
Debug.Log("Has a path = " + (PathCustomization.Path != null));
Debug.Log("Path has nodes = " + (PathCustomization.Path?.m_nodes != null));
if (PathCustomization.Path?.m_nodes != null)
Debug.Log("Path has positive nodes = " + PathCustomization.Path?.m_nodes.Count());
else
Debug.Log("Path has no positive nodes");
Debug.Log("Horizontal = " + PathCustomization.Horizontal);
Debug.Log("Vertical = " + PathCustomization.Vertical);
Debug.Log("Length = " + PathCustomization.Length);
Debug.Log("Depth = " + PathCustomization.Depth);
Debug.Log("Rotation = " + PathCustomization.Rotation);
Debug.Log("Curve = " + PathCustomization.Curve);
Debug.Log("AlterType " + (int)PathCustomization.AlterType);
CleanUpPaths();
HandleSubBuildings();
if (hasPaths)
{
ClearAnchorPoints();
Debug.Log("info " + m_Info.name + " has " + m_StationPaths.Count() + " station paths");
if (m_Info.HasUndergroundMetroStationTracks(false))
//RemovePedPaths();
if (IsAlterOfType(MetroStationTrackAlterType.Length))
ResizeUndergroundStationTracks();
ChangeStationDepthAndRotation();
if (IsAlterOfType(MetroStationTrackAlterType.Vertical | MetroStationTrackAlterType.Horizontal) && !IsAlterOfType(MetroStationTrackAlterType.Rotation))
ChangeStationPlanarPosition();
ReconfigureStationAccess();
ConnectStations();
CurveStationTrack();
RecalculateSpawnPoints();
info.StoreBuildingDefault();
}
var originalInfo = m_BuildingStack.Pop();
if (originalInfo != null)
{
m_Info = originalInfo;
m_StationPaths = originalInfo.UndergroundStationPaths(false);
}
m_ParentMetaData = m_ParentMetaDataStack.Pop();
}
private static void ClearAnchorPoints()
{
if (m_AnchorPoints != null)
{
m_AnchorPoints.Clear();
m_AnchorPoints = null;
}
}
private static NetInfo m_SpecialNetInfo = null;
private static NetInfo SpecialNetInfo {
get
{
if (m_SpecialNetInfo == null)
{
m_SpecialNetInfo = FindNetInfo("Pedestrian Connection Inside").ShallowClone();
m_SpecialNetInfo.m_maxSlope = 100;
m_SpecialNetInfo.m_maxTurnAngle = 180;
m_SpecialNetInfo.m_maxTurnAngleCos = -1;
var lanes = m_SpecialNetInfo.m_lanes.ToList();
lanes.First().m_speedLimit = 10;
m_SpecialNetInfo.m_lanes = lanes.ToArray();
}
return m_SpecialNetInfo;
}
}
private static BuildingInfo.PathInfo TrackPath { get; set; }
private static BuildingInfo.PathInfo NewPath { get; set; }
private static Vector3 CurveVector { get; set; }
private static Vector3 Crossing { get; set; }
private static float xCoeff { get; set; }
private static float zCoeff { get; set; }
private static float StairsLengthX { get; set; }
private static float StairsLengthZ { get; set; }
private static List<Vector3> m_NodeList = new List<Vector3>();
private static List<Vector3> NodeList {
get
{
if (m_NodeList == null)
{
m_NodeList = new List<Vector3>();
}
return m_NodeList;
}
}
private static void ClearNodeList()
{
if (m_NodeList != null)
{
m_NodeList.Clear();
m_NodeList = null;
}
}
private static List<BuildingInfo.PathInfo> SetStationPaths(params float[] connectorHalfWidths)
{
var retval = new List<BuildingInfo.PathInfo>();
if (connectorHalfWidths != null)
{
var newNodes = new List<Vector3>();
var connectorHalfWidthList = new List<float>();
connectorHalfWidthList.AddRange(connectorHalfWidths);
connectorHalfWidthList.Sort();
var offset = connectorHalfWidthList[0];
var forbiddenList = new List<bool>();
var midPoint = Vector3.zero;
for (int i = 0; i < connectorHalfWidthList.Count(); i++)
{
if (i == 0)
{
newNodes.Add(new Vector3()
{
x = Crossing.x + (connectorHalfWidthList[0] * zCoeff) - (3 * xCoeff),
y = TrackPath.m_nodes.Last().y + 8,
z = Crossing.z + (connectorHalfWidthList[0] * xCoeff) + (3 * zCoeff)
});
newNodes[0] = new Vector3(newNodes[0].x - CurveVector.x, newNodes[0].y, newNodes[0].z + CurveVector.z);
}
else
{
offset -= connectorHalfWidthList[i];
newNodes.Add(new Vector3()
{
x = newNodes[i - 1].x + (Math.Abs(offset) * zCoeff),
y = TrackPath.m_nodes.Last().y + 8,
z = newNodes[i - 1].z + (Math.Abs(offset) * xCoeff)
});
offset = connectorHalfWidthList[i];
}
}
var stairNodes = new List<Vector3>();
for (int i = 0; i < newNodes.Count(); i++)
{
stairNodes.Add(new Vector3()
{
x = newNodes[i].x + StairsLengthX,
y = TrackPath.m_nodes.Last().y,
z = newNodes[i].z + StairsLengthZ,
});
}
if (!StairDict.ContainsKey(TrackPath))
StairDict.Add(TrackPath, new List<BuildingInfo.PathInfo>());
if (connectorHalfWidths.Count() > 1)
{
var newNodesCount = newNodes.Count() - 1;
for (int i = 0; i < newNodesCount; i++)
{
if (Vector3.Distance(newNodes[i], newNodes[i + 1]) <= 4)
{
var averageNode = AverageNode(newNodes[i], newNodes[i + 1]);
newNodes[i] = averageNode;
newNodes[i + 1] = averageNode;
}
}
}
for (var i = 0; i < stairNodes.Count(); i++)
{
var branchPathStair = CreatePath(NewPath, newNodes[i], stairNodes[i]);
branchPathStair.m_forbidLaneConnection = new[] { true, false };
branchPathStair.AssignNetInfo(m_SpecialNetInfo);
StairDict[TrackPath].Add(branchPathStair);
retval.Add(branchPathStair);
}
NodeList.AddRange(newNodes);
}
return retval;
}
private static Dictionary<BuildingInfo.PathInfo, List<BuildingInfo.PathInfo>> m_StairDict = null;
private static Dictionary<BuildingInfo.PathInfo, List<BuildingInfo.PathInfo>> StairDict {
get
{
if (m_StairDict == null)
m_StairDict = new Dictionary<BuildingInfo.PathInfo, List<BuildingInfo.PathInfo>>();
return m_StairDict;
}
set
{
m_StairDict = value;
}
}
private static bool IsATargetPath(BuildingInfo.PathInfo path)
{
return (m_PathCustomization.Path?.m_nodes == null || m_PathCustomization.Path == path);
}
private static void ReconfigureStationAccess()
{
var pathList = m_Info.m_paths.ToList();
var directionList = new List<string>();
var aPath = pathList.FirstOrDefault(p => p.IsPedestrianPath());
if (aPath == null)
{
aPath = new BuildingInfo.PathInfo();
}
Debug.Log(m_Info.name + " has " + pathList.Count() + " paths with " + pathList.Where(p => p?.m_netInfo != null && p.m_netInfo.IsUndergroundDualIslandPlatformMetroStationTrack()));
if (m_StairDict != null)
{
m_StairDict.Clear();
m_StairDict = null;
}
ClearNodeList();
for (int i = 0; i < pathList.Count; i++)
{
if (pathList[i]?.m_netInfo != null && pathList[i].m_netInfo.IsUndergroundMetroStationTrack())
{
Debug.Log("On " + m_Info.name + " Track on index" + i);
TrackPath = pathList[i];
if (PathCustomizationDict.ContainsKey(TrackPath))
{
var curve = PathCustomizationDict[TrackPath].Curve;
var middleCurve = TrackPath.GetMiddle(curve);
var middle = TrackPath.GetMiddle();
CurveVector = middleCurve - middle;
NewPath = aPath.ShallowClone();
NewPath.AssignNetInfo(SpecialNetInfo);
NewPath.MarkPathGenerated();
xCoeff = -(TrackPath.m_nodes[0].x - TrackPath.m_nodes.Last().x) / Vector3.Distance(TrackPath.m_nodes[0], TrackPath.m_nodes.Last());
zCoeff = (TrackPath.m_nodes[0].z - TrackPath.m_nodes.Last().z) / Vector3.Distance(TrackPath.m_nodes[0], TrackPath.m_nodes.Last());
var stationLength = Vector3.Distance(TrackPath.m_nodes[0], TrackPath.m_nodes.Last());
StairsLengthX = ((0.12f * curve) + 1) * (stationLength * StairCoeff) * -xCoeff;
StairsLengthZ = ((0.12f * curve) + 1) * (stationLength * StairCoeff) * zCoeff;
var interpolant = 1;
Crossing = Vector3.Lerp(TrackPath.m_nodes.First(), TrackPath.m_nodes.Last(), interpolant);
if (TrackPath.m_netInfo.IsUndergroundIslandPlatformStationTrack())
{
pathList.AddRange(SetStationPaths(0));
}
//else if (TrackPath.m_netInfo.IsUndergroundSideIslandPlatformMetroStationTrack())
//{
// pathList.AddRange(SetStationPaths(-14, 0, 14));
//}
else if (TrackPath.m_netInfo.IsUndergroundSmallStationTrack())
{
pathList.AddRange(SetStationPaths(5));
}
else if (TrackPath.m_netInfo.IsUndergroundPlatformLargeMetroStationTrack())
{
pathList.AddRange(SetStationPaths(-11, 11));
}
else if (TrackPath.m_netInfo.IsUndergroundDualIslandPlatformMetroStationTrack())
{
pathList.AddRange(SetStationPaths(-8.8f, -5.5f, 5.5f, 8.8f));
}
else if (TrackPath.m_netInfo.IsUndergroundSidePlatformMetroStationTrack())
{
pathList.AddRange(SetStationPaths(-7, 7));
}
}
else
{
Debug.Log("PathCustomizationDict does not contain this trackpath...");
}
}
}
if (/*m_SuperInfo == null &&*/ m_Info.m_paths.Count() >= 2)
{
CheckPedestrianConnections();
}
m_Info.m_paths = pathList.ToArray();
CleanPaths();
}
private static List<BuildingInfo.PathInfo> m_TrackPool;
private static List<BuildingInfo.PathInfo> TrackPool {
get
{
if (m_TrackPool == null)
{
m_TrackPool = new List<BuildingInfo.PathInfo>();
m_TrackPool.AddRange(m_StationPaths);
}
return m_TrackPool;
}
}
private static void ClearTrackPool()
{
if (m_TrackPool != null)
{
m_TrackPool.Clear();
m_TrackPool = null;
}
}
private static Vector2 ConvertToPlanar(Vector3 vector)
{
return new Vector2(vector.x, vector.z);
}
private static float GetPlanarDistance(Vector3 a, Vector3 b)
{
return Vector2.Distance(ConvertToPlanar(a), ConvertToPlanar(b));
}
private static void ConnectStations()
{
if (m_StationPaths.Count > 0)
{
var pathList = m_Info.m_paths.ToList();
var nodePool = new List<Vector3>();
var pathNodeList = new List<Vector3>();
var connectorPath = NewPath.ShallowClone();
var averageAnchorNode = Vector3.zero;
foreach (var node in AnchorPoints)
{
averageAnchorNode += node;
}
averageAnchorNode /= AnchorPoints.Count();
m_NodeList = m_NodeList.OrderByDescending(n => Vector3.Distance(n, averageAnchorNode)).ToList();
nodePool.AddRange(m_NodeList);
nodePool.AddRange(AnchorPoints);
for (int i = 0; i < nodePool.Count(); i++)
{
Debug.Log("NodePool contains " + nodePool[i].ToString());
}
Debug.Log("NodeList count is " + NodeList.Count());
for (int i = 0; i < NodeList.Count(); i++)
{
var node = NodeList[i];
nodePool.Remove(node);
Debug.Log("Inspecting NodeList[" + i + "] " + node.ToString());
Debug.Log("NodePool has the following remaining");
for (int j = 0; j < nodePool.Count(); j++)
{
Debug.Log(nodePool[j].ToString());
}
var closestNode = nodePool.Where(n => GetPlanarDistance(n, node) >= 4).OrderBy(n => Vector3.Distance(n, node)).FirstOrDefault();
Debug.Log("Starting Node " + node.ToString());
if (!pathNodeList.Contains(node))
pathNodeList.Add(node);
if (!pathNodeList.Contains(closestNode))
pathNodeList.Add(closestNode);
Debug.Log("Node " + closestNode.ToString() + " added");
if (AnchorPoints.Contains(closestNode))
{
Debug.Log("Path done");
connectorPath.m_nodes = pathNodeList.ToArray();
var forbidLaneConnectionList = new List<bool>();
for (int j = 0; j < pathNodeList.Count() - 1; j++)
{
forbidLaneConnectionList.Add(true);
}
forbidLaneConnectionList.Add(false);
connectorPath.m_forbidLaneConnection = forbidLaneConnectionList.ToArray();
connectorPath.MarkPathGenerated();
connectorPath.m_curveTargets = null;
pathList.Add(connectorPath);
pathNodeList.Clear();
connectorPath = connectorPath.ShallowClone();
}
}
m_Info.m_paths = pathList.ToArray();
//CleanPaths();
}
}
public static List<Vector3> AdjustNodeFromParentToChild(Vector3[] nodes, ParentStationMetaData parentmetadata = null)
{
List<Vector3> retList = new List<Vector3>();
foreach (var node in nodes)
{
retList.Add(AdjustNodeFromParentToChild(node, parentmetadata));
}
return retList;
}
public static Vector3 AdjustNodeFromParentToChild(Vector3 node, ParentStationMetaData parentMetaData = null)
{
if (parentMetaData == null)
parentMetaData = m_ParentMetaData;
var retNode = node;
if (m_ParentMetaData != null)
{
var angle = (parentMetaData.Angle * Math.PI) / 180;
retNode = new Vector3
{
x = (float)(((node.x - parentMetaData.Position.x) * Math.Cos(angle)) - ((node.z + parentMetaData.Position.z) * Math.Sin(angle))),
y = node.y,
z = (float)(((node.x - parentMetaData.Position.x) * Math.Sin(angle)) + ((node.z + parentMetaData.Position.z) * Math.Cos(angle)))
};
}
return retNode;
}
public static Vector3 AdjustNodeFromChildToParent(Vector3 node)
{
var retNode = node;
if (m_ParentMetaData != null)
{
var angle = (m_ParentMetaData.Angle * Math.PI) / 180;
retNode = new Vector3
{
x = (float)(node.x * Math.Cos(angle) + node.z * Math.Sin(angle) + m_ParentMetaData.Position.x),
y = node.y,
z = (float)(node.z * Math.Cos(angle) - node.x * Math.Sin(angle) - m_ParentMetaData.Position.z)
};
}
return retNode;
}
private static Vector3 AverageNode(params Vector3[] nodes)
{
var center = Vector3.zero;
foreach (var node in nodes)
{
center += node;
}
return center / nodes.Count();
}
private static Dictionary<Vector3, Vector3> m_MergePathDict = null;
private static Dictionary<Vector3, Vector3> MergePathDict {
get
{
if (m_MergePathDict == null)
{
m_MergePathDict = new Dictionary<Vector3, Vector3>();
}
return m_MergePathDict;
}
}
private static void ClearMergePathDict()
{
if (m_MergePathDict != null)
{
m_MergePathDict.Clear();
m_MergePathDict = null;
}
}
private static void CleanPaths()
{
var pathList = m_Info.m_paths;
List<BuildingInfo.PathInfo> parentBuildingInfo = null;
ClearMergePathDict();
for (int i = 0; i < pathList.Count(); i++)
{
var path = pathList[i];
if (path.IsPathGenerated())
{
for (int j = 0; j < path.m_nodes.Count() - 1; j++)
{
var node = path.m_nodes[j];
var nextNode = path.m_nodes[j + 1];
if (!MergePathDict.ContainsKey(node) && Vector3.Distance(node, nextNode) <= 4)
{
var average = AverageNode(node, nextNode);
MergePathDict.Add(node, average);
}
}
}
}
ConsolidatePaths(false);
if (m_ParentMetaData != null)
ConsolidatePaths(true);
}
private static void ConsolidatePaths(bool consolidateParentNodes)
{
var pathList = consolidateParentNodes ? m_ParentMetaData.Info.m_paths : m_Info.m_paths;
for (int i = 0; i < pathList.Count(); i++)
{
var path = pathList[i];
List<Vector3> nodeList = path.m_nodes.Any(n => MergePathDict.ContainsKey(n)) ? new List<Vector3>() : null;
if (nodeList != null)
{
for (int j = 0; j < pathList[i].m_nodes.Count(); j++)
{
var node = path.m_nodes[j];
for (int k = 0; k < MergePathDict.Count(); k++)
{
var kvp = MergePathDict.ElementAt(k);
var nodeToMove = consolidateParentNodes ? AdjustNodeFromChildToParent(kvp.Key) : kvp.Key;
var moveNodeTo = consolidateParentNodes ? AdjustNodeFromChildToParent(kvp.Value) : kvp.Value;
if (node == nodeToMove)
if (!nodeList.Contains(moveNodeTo))
nodeList.Add(moveNodeTo);
else
if (!nodeList.Contains(node))
nodeList.Add(node);
}
}
pathList[i].m_nodes = nodeList.ToArray();
pathList[i].SetCurveTargets();
}
}
}
private static NetInfo FindNetInfo(string prefabName)
{
return PrefabCollection<NetInfo>.FindLoaded(prefabName);
}
private static BuildingInfo.PathInfo ChainPath(BuildingInfo.PathInfo startPath, Vector3 endNode, int startNodeIndex = -1, NetInfo info = null)
{
var newNodes = new List<Vector3>();
var newPath = startPath.ShallowClone();
if (startNodeIndex == -1 || startNodeIndex >= startPath.m_nodes.Count())
{
startNodeIndex = startPath.m_nodes.Count() - 1;
}
if (info != null)
{
newPath.AssignNetInfo(info);
}
var startNode = startPath.m_nodes[startNodeIndex];
newNodes.Add(startNode);
newNodes.Add(endNode);
newPath.m_nodes = newNodes.ToArray();
newPath.MarkPathGenerated();
return newPath;
}
private static BuildingInfo.PathInfo CreatePath(BuildingInfo.PathInfo templatePath, Vector3 vectorA, Vector3 vectorB)
{
var newPath = templatePath.ShallowClone();
var nodeList = new List<Vector3>();
nodeList.Add(vectorA);
nodeList.Add(vectorB);
newPath.m_nodes = nodeList.ToArray();
newPath.MarkPathGenerated();
newPath.AssignNetInfo(SpecialNetInfo);
newPath.SetCurveTargets();
return newPath;
}
private static void CurveStationTrack()
{
for (var i = 0; i < m_StationPaths.Count(); i++)
{
var stationPath = m_StationPaths[i];
if (PathCustomizationDict.ContainsKey(stationPath))
{
var curve = PathCustomizationDict[stationPath].Curve;
CurveStationTrack(stationPath, curve);
if (StairDict.ContainsKey(stationPath))
{
var stairPaths = m_Info.m_paths.Where(p => StairDict[stationPath].Contains(p)).ToList();
for (var j = 0; j < stairPaths.Count(); j++)
{
CurveStationTrack(stairPaths[j], curve, -StairCoeff);
}
}
}
}
}
private static void CurveStationTrack(BuildingInfo.PathInfo targetPath, float curve, float curveCoefficient = 1)
{
var aCurveStrength = curve * curveCoefficient;
var middle = targetPath.GetMiddle();
var newX = (targetPath.m_nodes.First().z - targetPath.m_nodes.Last().z) / 2;
var newY = (targetPath.m_nodes.First().y + targetPath.m_nodes.Last().y) / 2;
var newZ = -(targetPath.m_nodes.First().x - targetPath.m_nodes.Last().x) / 2;
var newCurve = middle + (aCurveStrength * new Vector3(newX, newY, newZ));
var curveTargetsList = new List<Vector3>();
if (targetPath.m_curveTargets != null)
{
curveTargetsList.AddRange(targetPath.m_curveTargets);
}
if (curveTargetsList.Count() > 0)
{
curveTargetsList[0] = newCurve;
}
else
{
curveTargetsList.Add(newCurve);
}
targetPath.m_curveTargets = curveTargetsList.ToArray();
}
private static void CleanUpPaths(BuildingInfo info)
{
info.m_paths = info.m_paths.Where(p => !p.IsPathGenerated()).ToArray();
}
private static void CleanUpPaths()
{
if (m_ParentMetaData == null && m_Info.m_paths.SelectMany(p => p.m_nodes).All(n => n.y < 0))
{
m_Info.m_paths = m_Info.m_paths.Where(p => !p.IsPathGenerated()).ToArray();
}
else
{
var pathsToKeep = m_Info.m_paths.Where(p => !p.IsPathGenerated() && (!p.IsPedestrianPath() || p.m_nodes.Any(n => n.y >= 0)));
if (pathsToKeep == null || pathsToKeep.Count() == 0)
pathsToKeep = m_Info.m_paths.OrderBy(p => p.m_nodes.Min(n => n.z)).Take(1);
m_Info.m_paths = pathsToKeep.ToArray();
}
//else
//m_Info.m_paths = m_Info.m_paths.Where(p => !p.IsPathGenerated()).ToArray();
}
private static void RecalculateSpawnPoints()
{
var buildingAI = m_Info?.GetComponent<DepotAI>();
var paths = m_Info?.m_paths;
if (buildingAI == null || paths == null)
{
return;
}
var spawnPoints = (from path in paths
where IsVehicleStop(path) && PathCustomizationDict.ContainsKey(path)
select path.GetMiddle(PathCustomizationDict[path].Curve)).Distinct().ToArray();
switch (spawnPoints.Length)
{
case 0:
buildingAI.m_spawnPosition = Vector3.zero;
buildingAI.m_spawnTarget = Vector3.zero;
buildingAI.m_spawnPoints = new DepotAI.SpawnPoint[] { };
break;
case 1:
buildingAI.m_spawnPosition = spawnPoints[0];
buildingAI.m_spawnTarget = spawnPoints[0];
buildingAI.m_spawnPoints = new[]
{
new DepotAI.SpawnPoint
{
m_position = spawnPoints[0],
m_target = spawnPoints[0]
}
};
break;
default:
buildingAI.m_spawnPosition = Vector3.zero;
buildingAI.m_spawnTarget = Vector3.zero;
var spawnPointList = new List<DepotAI.SpawnPoint>();
foreach (var msp in buildingAI.m_spawnPoints)
{
spawnPointList.Add(msp);
}
foreach (var sp in spawnPoints)
{
spawnPointList.Add(new DepotAI.SpawnPoint() { m_position = sp, m_target = sp });
}
buildingAI.m_spawnPoints = spawnPointList.ToArray();
break;
}
}
public static void HandleSubBuildings()
{
if (m_Info?.m_subBuildings != null && m_Info.m_subBuildings.Count() > 0)
{
for (int i = 0; i < m_Info.m_subBuildings.Count(); i++)
{
var subBuilding = m_Info.m_subBuildings[i];
if (subBuilding?.m_buildingInfo?.m_paths != null && subBuilding.m_buildingInfo.HasUndergroundMetroStationTracks())
{
CleanUpPaths(subBuilding.m_buildingInfo);
var metaData = new ParentStationMetaData()
{
Info = m_Info,
AnchorPoints = AnchorPoints,
Angle = subBuilding.m_angle,
Position = subBuilding.m_position
};
var modifiedPathCustomization = PathCustomization;
ModifyStation(subBuilding.m_buildingInfo, modifiedPathCustomization, metaData);
}
}
}
}
private static bool IsVehicleStop(BuildingInfo.PathInfo path)
{
return (path?.m_nodes?.Length ?? 0) > 1 && (path?.m_netInfo?.IsUndergroundMetroStationTrack() ?? false);
}
private static void RemovePedPaths()
{
var pathList = new List<BuildingInfo.PathInfo>();
foreach (var path in m_Info.m_paths)
{
if (path.m_netInfo.IsPedestrianNetwork() && !path.IsPathGenerated() && path.m_nodes.All(n => n.y <= -8))
continue;
pathList.Add(path);
}
m_Info.m_paths = pathList.ToArray();
}
private static void ChangeStationPlanarPosition()
{
var center = Vector3.zero;
if (PathCustomization.Path?.m_nodes == null)
{
center = m_Info.FindAverageNode(true);
}
else
{
center = Vector3.Lerp(PathCustomization.Path.m_nodes.First(), PathCustomization.Path.m_nodes.Last(), 0.5f);
}
var diff = new Vector3(PathCustomization.Horizontal, center.y, PathCustomization.Vertical) - center;
if (PathCustomization.Path?.m_nodes == null)
{
foreach (var path in m_StationPaths)
{
var nodeList = new List<Vector3>();
for (int i = 0; i < path.m_nodes.Count(); i++)
{
nodeList.Add(path.m_nodes[i] + diff);
}
path.m_nodes = nodeList.ToArray();
}
}
else
{
var nodeList = new List<Vector3>();
for (int i = 0; i < PathCustomization.Path.m_nodes.Count(); i++)
{
nodeList.Add(PathCustomization.Path.m_nodes[i] + diff);
}
PathCustomization.Path.m_nodes = nodeList.ToArray();
}
}
private static void ResizeUndergroundStationTracks()
{
var linkedStationTracks = GetInterlinkedStationTracks();
var processedConnectedPaths = new List<int>();
for (var index = 0; index < m_StationPaths.Count(); index++)
{
var path = m_StationPaths[index];
if (!path.m_netInfo.IsUndergroundMetroStationTrack())
continue;
if (PathCustomization.Path?.m_nodes == null || PathCustomization.Path == path)
{
//if (!linkedStationTracks.Contains(path))
ChangeStationTrackLength(index, processedConnectedPaths);
}
else
{
}
}
}
private static void ChangeStationDepthAndRotation()
{
var pathList = new List<BuildingInfo.PathInfo>();
var highestStation = float.MinValue;
var totalNode = Vector3.zero;
if (PathCustomization.Path?.m_nodes != null)
{
highestStation = PathCustomization.Path.m_nodes.OrderByDescending(n => n.y).FirstOrDefault().y;
}
else
{
foreach (var path in m_Info.m_paths)
{
if (path.m_netInfo?.m_netAI == null || !path.m_netInfo.IsUndergroundMetroStationTrack())
continue;
var highest = path.m_nodes.OrderByDescending(n => n.y).FirstOrDefault().y;
highestStation = Math.Max(highest, highestStation);
}
}
var offsetDepthDist = PathCustomization.Depth + highestStation;
if (AnchorPoints.Count > 0)
{
m_AngleDelta = CalculateAngleDelta();
for (var i = 0; i < m_Info.m_paths.Count(); i++)
{
var path = m_Info.m_paths[i];
if (AllNodesUnderGround(path))
{
if (path.IsPedestrianPath())
{
pathList.Add(path);
continue;
}
if (IsAlterOfType(MetroStationTrackAlterType.Rotation))
ChangePathRotation(path, m_AngleDelta);
if (IsAlterOfType(MetroStationTrackAlterType.Depth) && IsATargetPath(path))
DipPath(path, offsetDepthDist);
}
pathList.Add(path);
}
m_Info.m_paths = pathList.ToArray();
}
}
private static void DipPath(BuildingInfo.PathInfo path, float depthOffsetDist)
{
ShiftPath(path, new Vector3(0, -depthOffsetDist, 0));
}
private static void ShiftPath(BuildingInfo.PathInfo path, Vector3 offset)
{
for (var i = 0; i < path.m_nodes.Length; i++)
{
path.m_nodes[i] = path.m_nodes[i] + offset;
}
for (var i = 0; i < path.m_curveTargets.Length; i++)
{
path.m_curveTargets[i] = path.m_curveTargets[i] + offset;
}
}
private static void ChangeStationTrackLength(int pathIndex, ICollection<int> processedConnectedPaths)
{
var path = m_StationPaths[pathIndex];