-
Notifications
You must be signed in to change notification settings - Fork 2
/
TrackJunction.cs
786 lines (724 loc) · 33.5 KB
/
TrackJunction.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
using UnityEngine;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System;
public class FreightTrackJunction : MachineEntity
{
public static int GlobalJunctions;
private bool mbLinkedToGO = false;
public bool LinkStatusDirty = false;
public FreightTrackNetwork TrackNetwork;
public FreightTrackJunction[] ConnectedJunctions = new FreightTrackJunction[4];
public FreightTrackSegment[] ConnectedSegments = new FreightTrackSegment[4];
public int JunctionIndex = 0;
public int JunctionID;
public int[] SegmentDistances = new int[4];
//public List<KeyValuePair<int,FreightTrackJunction>> ConnectedJunctions = new List<KeyValuePair<int, FreightTrackJunction>>(4);
//public List<KeyValuePair<int, FreightTrackSegment>> ConnectedSegments = new List<KeyValuePair<int, FreightTrackSegment>>(4);
public static ushort TRACKTYPE = 538;
public static ushort TRACKSTRAIGHT = 0;
public static ushort TRACKCORNER = 1;
public static ushort TRACKSLOPE = 2;
public static ushort TRACKBUFFER = 3;
public static ushort TRACKFULL = 4;
public static ushort TRACKEMPTY = 5;
public static ushort CONTROLTYPE = 539;
public static ushort CONTROLTURBO = 2;
public static ushort CONTROLUNLOAD = 3;
public static ushort CONTROLLOAD = 4;
public static ushort FREIGHTSTATIONTYPE = ModManager.mModMappings.CubesByKey["steveman0.FreightCartStation"].CubeType;
public static ushort JUNCTIONTYPE = ModManager.mModMappings.CubesByKey["steveman0.TrackJunction"].CubeType;
public static ushort TOURSTATIONTYPE = ModManager.mModMappings.CubesByKey["steveman0.TourCartStation"].CubeType;
public static ushort ScrapJunctionVal = ModManager.mModMappings.CubesByKey["steveman0.TrackJunction"].ValuesByKey["steveman0.ScrapTrackJunction"].Value;
public static ushort ScrapTrackType = ModManager.mModMappings.CubesByKey["steveman0.ScrapTrack"].CubeType;
public static ushort ScrapStraightVal = ModManager.mModMappings.CubesByKey["steveman0.ScrapTrack"].ValuesByKey["steveman0.ScrapTrackStraight"].Value;
public static ushort ScrapCornerVal = ModManager.mModMappings.CubesByKey["steveman0.ScrapTrack"].ValuesByKey["steveman0.ScrapTrackCorner"].Value;
public static ushort ScrapSlopeVal = ModManager.mModMappings.CubesByKey["steveman0.ScrapTrack"].ValuesByKey["steveman0.ScrapTrackSlope"].Value;
private Segment mPrevGetSeg;
private Segment mUnderSegment;
private int Updates = 0;
private int[] DelayCheck = new int[4];
//Rendering stuff
public int instanceID = -1;
public int instanceID2 = -1;
public static Mesh TrackMesh;
public static Mesh TrackMesh2;
public static Material TrackMaterial;
public static Material ScrapTrackMat; // Move to the scrap track entity...
private JunctionRenderer TrackRenderer;
private GameObject CrossTrack;
public FreightTrackJunction(ModCreateSegmentEntityParameters parameters)
: base(parameters)
{
this.mbNeedsUnityUpdate = true;
this.mbNeedsLowFrequencyUpdate = true;
if (WorldScript.mbIsServer)
{
this.JunctionID = GlobalJunctions;
this.RequestImmediateNetworkUpdate(); //Is this necessary?
}
else
this.JunctionID = -1;
GlobalJunctions++;
this.TrackNetwork = new FreightTrackNetwork(this);
}
public override string GetPopupText()
{
string str1;
string str2;
string str3;
string str4 = "";
string str5 = "";
str1 = "Track Junction (ID: " + this.JunctionID + ")\n";
int count = 0;
string str = "";
for (int n = 0; n < 4; n++)
{
if (this.ConnectedJunctions[n] != null)
{
count++;
str += SegmentDistances[n].ToString() + ", ";
}
}
if (count > 0)
{
str2 = count.ToString() + " valid track connections\n";
str3 = "Track lengths: " + str.Substring(0, str.Length - 2) + "\nPress Q to reset the junction\n";
}
else
{
str2 = "No valid track connections detected\n";
str3 = "";
}
if (this.TrackNetwork != null)
{
str4 = "Track Network ID: " + this.TrackNetwork.NetworkID.ToString() + "\nJunction Count: " + this.TrackNetwork.TrackJunctions.Count.ToString() + "\n";
//for (int n = 0; n < this.TrackNetwork.TrackJunctions.Count; n++)
// str5 += this.TrackNetwork.TrackJunctions[n].JunctionID.ToString() + ", ";
//if (str5.Length > 0)
// str5 = str5.Substring(0, str5.Length - 2) + "\n";
}
if (Input.GetButtonDown("Extract"))
TrackJunctionWindow.ResetJunction(this);
return str1 + str2 + str3 + str4 + str5;
}
public override void LowFrequencyUpdate()
{
Updates++;
int direction = Updates % 4;
if (this.DelayCheck[direction] > 0)
{
this.DelayCheck[direction]--;
return;
}
if (this.ConnectedJunctions[direction] == null && !this.TrackFollow(direction))
this.DelayCheck[direction] = 10; //Delay checking this direction again 10 times or about 8 seconds
}
/// <summary>
/// Follows a track segment to find all containing stations until it reaches another junction or determines track is invalid
/// </summary>
/// <param name="direction">0 - 3 representing the four directions out of the junction</param>
/// <returns>True if it found complete segment</returns>
public bool TrackFollow(int direction)
{
//Initialize the check from the junction
long nextX = this.mnX;
long nextY = this.mnY;
long nextZ = this.mnZ;
Vector3 dirvec = new Vector3();
//Store the initial junction direction for later recording which direction the connected junction is associated with
int initialdirection = direction;
//List of freight cart stations found on this segment -> to be written to the final constructed FreightTrackSegment
List<FreightCartStation> SegmentStations = new List<FreightCartStation>();
//Store visited track pieces for catching when the segment enters a closed loop
List<TrackPiece> VisitedTracks = new List<TrackPiece>();
//Add a penalty for pathfinding in certain directions to avoid stations and other undesirable routes
int PathfindPenalty = 0;
//Begin loop here. Direction can be set and used to check the next location each time through the loop
//Allow segments only up to 2048 long due to cost of loop checking - may revise after testing
for (int n = 0; n < 2048; n++)
{
switch (direction)
{
case 0:
nextX++;
dirvec = Vector3.right;
break;
case 1:
nextZ++;
dirvec = Vector3.forward;
break;
case 2:
nextX--;
dirvec = Vector3.left;
break;
case 3:
nextZ--;
dirvec = Vector3.back;
break;
default:
nextX++;
break;
}
ushort lValue1 = 0;
byte lFlags1 = 0;
ushort type = this.GetCube(nextX, nextY, nextZ, out lValue1, out lFlags1);
this.mUnderSegment = this.mPrevGetSeg;
//Debug.LogWarning("GetCube type: " + type.ToString() + " value: " + lValue1);
bool foundslope = false;
//Found air and need to check for a downward slope under it
if (type == 1)
{
ushort lValue2 = 0;
byte lFlags2 = 0;
ushort cube = this.GetCube(nextX, nextY - 1L, nextZ, out lValue2, out lFlags2);
Segment segment = this.mPrevGetSeg;
type = cube;
lFlags1 = lFlags2;
lValue1 = lValue2;
if ((type == 538 && lValue1 == 2) || (type == ScrapTrackType && lValue1 == ScrapSlopeVal))
{
foundslope = true;
nextY--; //decrement Y level for next loop through!
}
else
{
if (type == 0)
Debug.LogError("Error, track follower has null under segment!");
if (this.mPrevGetSeg == null)
Debug.LogError("Error, prevseg was null!");
if (segment == null)
Debug.LogError("Error, old was null!");
if (this.mPrevGetSeg != segment)
Debug.LogWarning(("Track follower is looking for a slope, and has had to check across segment boundaries for this![Old/New" + segment.GetName() + " -> " + this.mPrevGetSeg.GetName()));
return false;
}
}
Vector3 trackvec = SegmentCustomRenderer.GetRotationQuaternion(lFlags1) * Vector3.forward;
bool oneway = false;
trackvec.Normalize();
trackvec.x = trackvec.x >= -0.5 ? (trackvec.x <= 0.5 ? 0.0f : 1f) : -1f;
trackvec.y = trackvec.y >= -0.5 ? (trackvec.y <= 0.5 ? 0.0f : 1f) : -1f;
trackvec.z = trackvec.z >= -0.5 ? (trackvec.z <= 0.5 ? 0.0f : 1f) : -1f;
//Begin checking track type
if (type == TRACKTYPE || type == ScrapTrackType)
{
if ((type == TRACKTYPE && (lValue1 == TRACKSTRAIGHT || lValue1 == TRACKEMPTY || lValue1 == TRACKFULL)) || (type == ScrapTrackType && lValue1 == ScrapStraightVal))
{
if (trackvec.y > 0.5 || trackvec.y < -0.5)
return false;
else if (!(trackvec == dirvec) && !(trackvec == -dirvec))
{
dirvec = new Vector3(trackvec.x, 0f, trackvec.z);
oneway = true; // Can't set return path as the same -> they're different!
}
}
if ((type == TRACKTYPE && lValue1 == TRACKCORNER) || (type == ScrapTrackType && lValue1 == ScrapCornerVal))
{
if (dirvec == new Vector3(-trackvec.z, 0.0f, trackvec.x))
dirvec = new Vector3(dirvec.z, 0.0f, -dirvec.x);
else if (trackvec == -dirvec)
dirvec = new Vector3(-dirvec.z, 0.0f, dirvec.x);
else
return false;
}
if ((type == TRACKTYPE && lValue1 == TRACKSLOPE) || (type == ScrapTrackType && lValue1 == ScrapSlopeVal))
{
Vector3 vector3_2 = trackvec;
dirvec.y = 0.0f;
dirvec.Normalize();
if (dirvec == trackvec)
{
if (foundslope)
return false;
else
nextY++;
}
else if (dirvec == -trackvec)
;
}
if (type == TRACKTYPE && lValue1 == TRACKBUFFER)
{
dirvec = new Vector3(-dirvec.x, 0f, -dirvec.z);
}
}
//Begin checking special types
else if (type == CONTROLTYPE)
{
if (lValue1 == CONTROLLOAD || lValue1 == CONTROLUNLOAD || lValue1 == CONTROLTURBO)
{
if ((trackvec == dirvec) || (trackvec == -dirvec))
{
//Do nothing... direction doesn't change
//Except turbo... reduce the penalty for this path!
if (lValue1 == CONTROLTURBO)
PathfindPenalty--;
}
else
return false;
}
}
//Check for freight stations
else if (type == FREIGHTSTATIONTYPE)
{
if ((trackvec == dirvec) || (trackvec == -dirvec))
{
Segment segment = this.AttemptGetSegment(nextX, nextY, nextZ);
if (segment == null)
{
segment = WorldScript.instance.GetSegment(nextX, nextY, nextZ);
if (segment == null)
{
Debug.Log((object)"Track junction track follower did not find segment");
return false;
}
}
FreightCartStation fcs = segment.FetchEntity(eSegmentEntity.Mod, nextX, nextY, nextZ) as FreightCartStation;
if (fcs == null)
{
Debug.LogWarning("Track Junction Track Follower tried to get a freight cart station but got other mod machine instead?");
return false;
}
if (!SegmentStations.Contains(fcs))
SegmentStations.Add(fcs);
fcs.ClosestJunction = this;
fcs.JunctionDirection = initialdirection;
// Penalize this route for multidirection pathfinding due to the station
PathfindPenalty += 5;
}
else
return false;
}
//Is it a junction?
else if (type == JUNCTIONTYPE)
{
//Debug.LogWarning("Track follower success! Found another junction!");
Segment segment = this.AttemptGetSegment(nextX, nextY, nextZ);
if (segment == null)
{
segment = WorldScript.instance.GetSegment(nextX, nextY, nextZ);
if (segment == null)
{
Debug.Log((object)"Track junction track follower did not find segment");
return false;
}
}
FreightTrackJunction junction = segment.FetchEntity(eSegmentEntity.Mod, nextX, nextY, nextZ) as FreightTrackJunction;
if (junction == null)
{
Debug.LogWarning("Track Junction Track Follower tried to get a track junction but got other mod machine instead?");
return false;
}
this.ConnectedJunctions[initialdirection] = junction;
//Don't let segment distance be negative just to be safe! This should rarely happen...
if (PathfindPenalty < 0 && Math.Abs(PathfindPenalty) > n)
PathfindPenalty = -n;
FreightTrackSegment tracksegment = new FreightTrackSegment(this, junction, n + 1 + PathfindPenalty);
tracksegment.Stations = SegmentStations;
//Debug.LogWarning("trackseg station count: " + tracksegment.Stations.Count);
this.ConnectedSegments[initialdirection] = tracksegment;
this.SegmentDistances[initialdirection] = n + 1;
this.LinkStatusDirty = true;
//handle the connection for the other junction so we don't need to double the work - only if return path is valid!
//Mirror the direction to reflect the correct side of the connecting junction
if (!oneway)
{
int mirroreddir = direction += 2;
if (mirroreddir > 3)
mirroreddir -= 4;
junction.ConnectedJunctions[mirroreddir] = this;
junction.ConnectedSegments[mirroreddir] = tracksegment;
junction.SegmentDistances[mirroreddir] = n + 1;
junction.LinkStatusDirty = true;
}
return true;
}
else if (type == TOURSTATIONTYPE)
{
if (trackvec != -dirvec)
return false;
Segment segment = this.AttemptGetSegment(nextX, nextY, nextZ);
if (segment == null)
{
segment = WorldScript.instance.GetSegment(nextX, nextY, nextZ);
if (segment == null)
{
Debug.Log((object)"Track junction track follower did not find segment");
return false;
}
}
TourCartStation station = segment.FetchEntity(eSegmentEntity.Mod, nextX, nextY, nextZ) as TourCartStation;
station.TrackNetwork = this.TrackNetwork;
station.ClosestJunction = this;
station.JunctionDirection = initialdirection;
this.ConnectedJunctions[initialdirection] = this;
FreightTrackSegment tracksegment = new FreightTrackSegment(this, this, 2*n + 1);
this.SegmentDistances[initialdirection] = 2 * n + 1;
this.ConnectedSegments[initialdirection] = tracksegment;
this.LinkStatusDirty = true;
if (!string.IsNullOrEmpty(station.StationName) && !this.TrackNetwork.TourCartStations.ContainsKey(station.StationName))
this.TrackNetwork.TourCartStations.Add(station.StationName, station);
return true;
}
else
return false; //Not a track type
//Update the direction int based on the changed direction vector
if (dirvec == Vector3.right)
direction = 0;
else if (dirvec == Vector3.forward)
direction = 1;
else if (dirvec == Vector3.left)
direction = 2;
else if (dirvec == Vector3.back)
direction = 3;
TrackPiece visitedpiece = new TrackPiece(new Vector3(nextX - this.mnX, nextY - this.mnY, nextZ - this.mnZ), direction);
//Debug.LogWarning("Visited track piece: " + new Vector4(nextX - this.mnX, nextY - mnY, nextZ - mnZ, direction).ToString());
//Store every track piece and check every 10th for monitoring for closed, endless loops of track
if (n % 10 == 0)
{
int count = VisitedTracks.Count;
for (int m = 0; m < count; m++)
{
TrackPiece piece = VisitedTracks[m];
if (piece.Position == visitedpiece.Position && piece.Direction == visitedpiece.Direction)
{
//Debug.LogWarning("piece position: " + piece.Position.ToString() + " visited: " + visitedpiece.Position.ToString());
Debug.LogWarning("TrackJunction followed track route and found a closed loop. Ending search.");
return false;
}
}
}
VisitedTracks.Add(visitedpiece);
if (n == 2047)
Debug.LogWarning("Track Junction Found track length > 2048m -> ending search.");
}
return false;
}
public override void SpawnGameObject()
{
return;
}
public override void UnityUpdate()
{
if (!mbLinkedToGO)
{
// Update the instanced version
//this.instanceID = FreightCartMod.TrackInstances.TryAdd();
//this.instanceID2 = FreightCartMod.TrackInstances.TryAdd();
//this.UpdateInstancedBase();
//if (instanceID != -1 && instanceID2 != -1)
//{
// this.mbLinkedToGO = true;
// this.LinkStatusDirty = true;
//}
if (this.CrossTrack != null || FreightTrackJunction.TrackMesh == null || FreightTrackJunction.TrackMesh2 == null || FreightTrackJunction.TrackMaterial == null)
return;
Quaternion rot = SegmentCustomRenderer.GetRotationQuaternion(this.mFlags);
Quaternion rot2 = rot * Quaternion.Euler(Vector3.up * 90);
MaterialPropertyBlock materialPropertyBlock = new MaterialPropertyBlock();
materialPropertyBlock.SetColor("_GlowColor", Color.red);
materialPropertyBlock.SetFloat("_GlowMult", 5f);
this.CrossTrack = new GameObject();
JunctionRenderer ren = this.CrossTrack.AddComponent<JunctionRenderer>();
Vector3 lUnityPos = WorldScript.instance.mPlayerFrustrum.GetCoordsToUnity(this.mnX, this.mnY, this.mnZ);
lUnityPos.x += 0.5f;
lUnityPos.y += 0.5f;
lUnityPos.z += 0.5f;
ren.position = lUnityPos;
ren.rotation = rot;
ren.rotation2 = rot2;
ren.mpb = materialPropertyBlock;
ren.scrap = this.mValue == FreightTrackJunction.ScrapJunctionVal;
ren.enabled = true;
ren.gameObject.SetActive(true);
this.CrossTrack.SetActive(true);
this.TrackRenderer = ren;
this.mbLinkedToGO = true;
//if (mWrapper == null || mWrapper.mGameObjectList == null || SpawnableObjectManagerScript.instance == null || SpawnableObjectManagerScript.instance.maSpawnableObjects == null || SpawnableObjectManagerScript.instance.maSpawnableObjects[(int)SpawnableObjectEnum.Minecart_Track_Straight] == null)
//{
// return;
//}
//else
//{
// //this.CrossTrack = (GameObject)GameObject.Instantiate(SpawnableObjectManagerScript.instance.maSpawnableObjects[(int)SpawnableObjectEnum.Minecart_Track_Straight]);
// //this.CrossTrack.transform.parent = mWrapper.mGameObjectList[0].gameObject.transform;
// //this.CrossTrack.transform.localPosition = new Vector3(0, 0f, 0);//put in the correct relative position
// //mWrapper.mGameObjectList[0].gameObject.transform.eulerAngles = new Vector3(0, 0, 0);
// //this.CrossTrack.transform.eulerAngles = new Vector3(0, 90f, 0);
// //this.CrossTrack.transform.localScale = new Vector3(0.99f, 0.99f, 0.99f);
// //this.CrossTrack.SetActive(true);
//}
}
if (this.mbLinkedToGO && this.LinkStatusDirty)
{
Color value = Color.red;
int links = 0;
for (int n = 0; n < 4; n++)
{
if (this.ConnectedJunctions[n] != null)
links++;
}
switch (links)
{
case 1:
value = new Color(1f, 0.3f, 0, 1f);
break;
case 2:
value = Color.yellow;
break;
case 3:
value = Color.green;
break;
case 4:
value = Color.blue;
break;
}
//Renderer[] componentsInChildren = this.CrossTrack.GetComponentsInChildren<Renderer>();
//Renderer[] comp2 = this.mWrapper.mGameObjectList[0].GetComponentsInChildren<Renderer>();
//MaterialPropertyBlock materialPropertyBlock = new MaterialPropertyBlock();
//materialPropertyBlock.SetColor("_GlowColor", value);
//materialPropertyBlock.SetFloat("_GlowMult", 5f);
//for (int i = 0; i < componentsInChildren.Length; i++)
// componentsInChildren[i].SetPropertyBlock(materialPropertyBlock);
//for (int i = 0; i < comp2.Length; i++)
// comp2[i].SetPropertyBlock(materialPropertyBlock);
if (this.TrackRenderer != null)
this.TrackRenderer.mpb.SetColor("_GlowColor", value);
if (this.instanceID != -1 && this.instanceID2 != -1)
{
//Debug.Log("Setting Track instance color as value: " + value.ToString());
FreightCartMod.TrackInstances.SetCol(this.instanceID, value);
FreightCartMod.TrackInstances.SetParamVal(this.instanceID, 1f);
FreightCartMod.TrackInstances.SetCol(this.instanceID2, value);
FreightCartMod.TrackInstances.SetParamVal(this.instanceID2, 1f);
}
this.LinkStatusDirty = false;
}
}
void UpdateInstancedBase()
{
if (instanceID == -1 || instanceID2 == -1) return;
Vector3 lUnityPos = WorldScript.instance.mPlayerFrustrum.GetCoordsToUnity(this.mnX, this.mnY, this.mnZ);
lUnityPos.x += 0.5f;
lUnityPos.y += 0.5f;
lUnityPos.z += 0.5f;
//lUnityPos += mUp * -0.2174988f;
//Vector3 rot = SegmentCustomRenderer.GetRotationQuaternion(mFlags).eulerAngles;
Vector3 rot = Vector3.zero;
Vector3 rot2 = new Vector3(0, 90f, 0);
Vector3 sizeadjust = new Vector3(0.99f, 0.99f, 0.99f); // to prevent render flickering
// Create my own instancer and just call that one instead!
FreightCartMod.TrackInstances.SetMatrix(instanceID, lUnityPos, rot, Vector3.one);
FreightCartMod.TrackInstances.SetMatrix(instanceID2, lUnityPos, rot2, sizeadjust);
}
public override void UnitySuspended()
{
GameObject.Destroy(this.CrossTrack);
if (this.instanceID != -1)
FreightCartMod.TrackInstances.Remove(this.instanceID);
if (this.instanceID2 != -1)
FreightCartMod.TrackInstances.Remove(this.instanceID2);
this.CrossTrack = null;
this.mbLinkedToGO = false;
base.UnitySuspended();
}
/// <summary>
/// Returns the direction corresponding with the route out of this junction to get to the destination junction
/// Ensures shortest route by checking for duplicate paths to the same junction and takes the shortest.
/// </summary>
/// <param name="junction">Destination junction</param>
/// <returns>Direction integer</returns>
public int GetConnectedDirection(FreightTrackJunction junction)
{
int shortestroute = -1;
for (int n = 0; n < 4; n++)
{
if (this.ConnectedJunctions[n] == junction)
{
if (shortestroute == -1)
shortestroute = n;
else
{
if (this.ConnectedSegments[n].Length < this.ConnectedSegments[shortestroute].Length)
shortestroute = n;
}
}
}
return shortestroute;
}
public ushort GetCube(long lTestX, long lTestY, long lTestZ, out ushort lValue, out byte lFlags)
{
if (lTestX < 100000L)
Debug.LogError((object)("Error, either you travelled 500 light years, or the mob is lost! X is " + (object)lTestX));
if (lTestY < 100000L)
Debug.LogError((object)("Error, either you travelled 500 light years, or the mob is lost! Y is " + (object)lTestY));
if (lTestZ < 100000L)
Debug.LogError((object)("Error, either you travelled 500 light years, or the mob is lost! Z is " + (object)lTestZ));
Segment segment;
if (this.mSegment.ContainsCoordinate(lTestX, lTestY, lTestZ))
{
segment = this.mSegment;
}
else
{
long segX;
long segY;
long segZ;
WorldHelper.GetSegmentCoords(lTestX, lTestY, lTestZ, out segX, out segY, out segZ);
segment = WorldScript.instance.GetSegment(segX, segY, segZ);
if (segment == null)
this.AttemptGetSegment(segX, segY, segZ);
}
if (segment == null || !segment.mbInitialGenerationComplete || segment.mbDestroyed)
{
//CentralPowerHub.mnMinecartX = lTestX;
//CentralPowerHub.mnMinecartY = lTestY;
//CentralPowerHub.mnMinecartZ = lTestZ;
lFlags = (byte)0;
lValue = (ushort)0;
this.mPrevGetSeg = (Segment)null;
return 0;
}
lValue = segment.GetCubeData(lTestX, lTestY, lTestZ).mValue;
lFlags = segment.GetCubeData(lTestX, lTestY, lTestZ).meFlags;
this.mPrevGetSeg = segment;
return segment.GetCube(lTestX, lTestY, lTestZ);
}
public override void OnUpdateRotation(byte newFlags)
{
int x = (int)(this.mnX - mSegment.baseX);
int y = (int)(this.mnY - mSegment.baseY);
int z = (int)(this.mnZ - mSegment.baseZ);
// nodeworker automatically sets the new flags in the cubedata that is actually used by the diskserializer, so we have to restore them!
mSegment.maCubeData[(y << 8) + (z << 4) + x].meFlags = this.mFlags;
}
public void ResetJunction()
{
this.ClearConnections();
Array.Clear(this.ConnectedJunctions, 0, 4);
Array.Clear(this.ConnectedSegments, 0, 4);
Array.Clear(this.SegmentDistances, 0, 4);
this.TrackNetwork = new FreightTrackNetwork(this);
}
private void ClearConnections()
{
for (int n = 0; n < 4; n++)
{
//Remove this from connected junctions/segments from all connected neighbors
FreightTrackJunction junc = this.ConnectedJunctions[n];
if (junc == null)
continue;
for (int m = 0; m < 4; m++)
{
FreightTrackJunction junc2 = junc.ConnectedJunctions[m];
if (junc2 == null)
continue;
if (junc2 == this)
{
junc.ConnectedJunctions[m] = null;
junc.LinkStatusDirty = true;
}
}
for (int m = 0; m < 4; m++)
{
FreightTrackSegment seg = junc.ConnectedSegments[m];
if (seg == null)
continue;
if (seg.ConnectedJunctions.Contains(this))
junc.ConnectedSegments[m] = null;
}
}
this.TrackNetwork.TrackJunctions.Remove(this);
this.TrackNetwork.NetworkIntegrityCheck(this.ConnectedJunctions.ToList());
}
public override void OnDelete()
{
if (this.instanceID != -1)
FreightCartMod.TrackInstances.Remove(this.instanceID);
if (this.instanceID2 != -1)
FreightCartMod.TrackInstances.Remove(this.instanceID2);
GameObject.Destroy(this.CrossTrack);
this.ClearConnections();
}
public void InvalidConnection(FreightTrackJunction missingtarget)
{
for (int n = 0; n < 4; n++)
{
//Remove this from connected junctions/segments from all connected neighbors
FreightTrackJunction junc = this.ConnectedJunctions[n];
if (junc == null || junc != missingtarget)
continue;
for (int m = 0; m < 4; m++)
{
FreightTrackJunction junc2 = missingtarget.ConnectedJunctions[m];
if (junc2 == null)
continue;
if (junc2 == this)
{
junc.ConnectedJunctions[m] = null;
junc.LinkStatusDirty = true;
}
}
for (int m = 0; m < 4; m++)
{
FreightTrackSegment seg = missingtarget.ConnectedSegments[m];
if (seg == null)
continue;
if (seg.ConnectedJunctions.Contains(this))
junc.ConnectedSegments[m] = null;
}
this.ConnectedJunctions[n] = null;
this.ConnectedSegments[n] = null;
}
}
public override bool ShouldSave()
{
return true;
}
public override int GetVersion()
{
return 2;
}
public override bool ShouldNetworkUpdate()
{
return true;
}
public override void WriteNetworkUpdate(BinaryWriter writer)
{
writer.Write(this.JunctionID);
}
public override void ReadNetworkUpdate(BinaryReader reader)
{
this.JunctionID = reader.ReadInt32();
}
public override void Write(BinaryWriter writer)
{
//Maybe don't do this to start? See if the cost is noticeable
//Write coordinates for connected junctions so that every load it doesn't need to follow every track segment again
//Will need to write segment length as well for segment recreation on load
//... nah :D
writer.Write(this.JunctionID);
base.Write(writer);
}
public override void Read(BinaryReader reader, int entityVersion)
{
if (entityVersion > 1)
{
this.JunctionID = reader.ReadInt32();
if (this.JunctionID >= GlobalJunctions)
GlobalJunctions = this.JunctionID + 1;
}
base.Read(reader, entityVersion);
}
}
public class TrackPiece
{
public Vector3 Position;
public int Direction;
public TrackPiece(Vector3 position, int direction)
{
this.Position = position;
this.Direction = direction;
}
}