-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathStockFinder.cs
98 lines (92 loc) · 2.76 KB
/
StockFinder.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
using System.Collections.Generic;
namespace DVPathTracer
{
public static class StockFinder
{
// <Order ID, car> To preserve order and location within the reports
private static Dictionary<int, StockReporter> trackedStock = new Dictionary<int, StockReporter>();
public static Dictionary<int, StockReporter> TrackedStock
{
get
{
return trackedStock;
}
}
public static int numStock = 0;
/**
* Inserts the given TrainCar into the earliest available space
*/
public static void Add(TrainCar car)
{
int i = 0;
while (trackedStock.ContainsKey(i) && trackedStock[i] != null)
{
i++;
}
trackedStock[i] = new StockReporter(car);
numStock++;
}
/**
* Returns true if the car is being tracked, false otherwise
*/
public static bool IsTracking(TrainCar car)
{
foreach (int index in trackedStock.Keys)
{
if (trackedStock[index] != null && trackedStock[index].Target == car)
{
return true;
}
}
return false;
}
/**
* Removes the given car, freeing that space for a new one
*/
public static void Remove(StockReporter car)
{
foreach (int index in trackedStock.Keys)
{
if (trackedStock[index] == car)
{
Remove(index);
return;
}
}
}
public static void Remove(TrainCar car)
{
foreach (int index in trackedStock.Keys)
{
if (trackedStock[index].Target == car)
{
Remove(index);
return;
}
}
}
public static void Remove(int carIndex)
{
trackedStock[carIndex] = null;
numStock--;
}
/**
* Updates its dictionary of all new avaliable rolling stock to be tracked
* TODO: Optimise somehow because this is definitely not an efficient task
*/
public static void UpdateTrackedStock()
{
TrainCar[] allCars = UnityEngine.Object.FindObjectsOfType<TrainCar>();
foreach (TrainCar car in allCars)
{
if (!IsTracking(car))
{
if (Main.settings.trackFreight || car.IsLoco || car.carType == TrainCarType.CabooseRed)
{
Add(car);
}
}
}
}
}
}