-
Notifications
You must be signed in to change notification settings - Fork 2
/
FreightTrackSegment.cs
66 lines (61 loc) · 3.37 KB
/
FreightTrackSegment.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
using System.Collections.Generic;
using UnityEngine;
public class FreightTrackSegment
{
public List<FreightTrackJunction> ConnectedJunctions = new List<FreightTrackJunction>();
public int Length; // Actual pathfinding length! Can include penalties!
public FreightTrackNetwork TrackNetwork;
public List<FreightCartStation> Stations = new List<FreightCartStation>();
public FreightTrackSegment(FreightTrackJunction junction1, FreightTrackJunction junction2, int length)
{
this.ConnectedJunctions.Add(junction1);
this.ConnectedJunctions.Add(junction2);
this.Length = length;
this.MergeNetworks(junction1, junction2);
}
public void MergeNetworks(FreightTrackJunction junction1, FreightTrackJunction junction2)
{
//Debug.LogWarning("FTSeg junction1 ID: " + junction1.JunctionID.ToString() + " junction2 ID: " + junction2.JunctionID.ToString());
if (junction1.TrackNetwork != junction2.TrackNetwork)
{
//Merge in smaller network into the larger one for efficiency
FreightTrackNetwork oldnetwork = junction1.TrackNetwork.TrackJunctions.Count > junction2.TrackNetwork.TrackJunctions.Count ? junction2.TrackNetwork : junction1.TrackNetwork;
FreightTrackNetwork newnetwork = junction1.TrackNetwork.TrackJunctions.Count > junction2.TrackNetwork.TrackJunctions.Count ? junction1.TrackNetwork : junction2.TrackNetwork;
this.TransferOwnership(oldnetwork, newnetwork);
}
this.TrackNetwork = junction1.TrackNetwork;
this.TrackNetwork.TrackSegments.Add(this);
}
/// <summary>
/// For migrating junctions and segments to a new network on merging
/// </summary>
/// <param name="oldnetwork"></param>
/// <param name="newnetwork"></param>
public void TransferOwnership(FreightTrackNetwork oldnetwork, FreightTrackNetwork newnetwork)
{
if (oldnetwork == null || newnetwork == null)
return;
//Debug.LogWarning("FTSeg oldnetwork: " + oldnetwork.NetworkID.ToString() + " junction count: " + oldnetwork.TrackJunctions.Count.ToString() + " newnetwork ID: " + newnetwork.NetworkID.ToString() + " junction count: " + newnetwork.TrackJunctions.Count.ToString());
for (int n = 0; n < oldnetwork.TrackJunctions.Count; n++)
{
oldnetwork.TrackJunctions[n].TrackNetwork = newnetwork;
if (!newnetwork.TrackJunctions.Contains(oldnetwork.TrackJunctions[n]))
newnetwork.TrackJunctions.Add(oldnetwork.TrackJunctions[n]);
}
for (int n = 0; n < oldnetwork.TrackSegments.Count; n++)
{
oldnetwork.TrackSegments[n].TrackNetwork = newnetwork;
//if (!newnetwork.TrackSegments.Contains(oldnetwork.TrackSegments[n]))
newnetwork.TrackSegments.Add(oldnetwork.TrackSegments[n]);
}
newnetwork.ResetJunctionIndices();
newnetwork.ReassignTourCartStations(oldnetwork);
oldnetwork.TrackJunctions.Clear();
oldnetwork.TrackSegments.Clear();
if (FreightCartManager.instance != null)
FreightCartManager.instance.GlobalTrackNetworks.Remove(oldnetwork);
else
Debug.LogWarning("FreightCartManager is null! Old network couldn't be registered anyway so no network to remove...");
oldnetwork = null;
}
}