-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathModuleResourceScannerIndicator.cs
More file actions
226 lines (197 loc) · 8.14 KB
/
Copy pathModuleResourceScannerIndicator.cs
File metadata and controls
226 lines (197 loc) · 8.14 KB
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
using UnityEngine;
namespace IndicatorLights
{
/// <summary>
/// Indicates the relative degree of resource concentration found by the scanner:
/// none, low, medium, high.
/// </summary>
class ModuleResourceScannerIndicator : ModuleSourceIndicator<ModuleResourceScanner>, IToggle, IScalar
{
private static readonly Animations.Blink LOCKED_BLINK = Animations.Blink.of(300, 900, 0);
private IColorSource inactiveSource;
private IColorSource unavailableSource;
private IColorSource lowSource;
private IColorSource mediumSource;
private IColorSource highSource;
/// <summary>
/// The color to display when the scanner is inactive (i.e. can't scan right now).
/// </summary>
[KSPField]
[ColorSourceIDField]
public string inactiveColor = Colors.ToString(DefaultColor.Off);
/// <summary>
/// The color to display when the resource content is zero.
/// </summary>
[KSPField]
[ColorSourceIDField]
public string unavailableColor = "inactiveColor";
/// <summary>
/// The color to display when the resource content is "low".
/// </summary>
[KSPField]
[ColorSourceIDField]
public string lowResourceColor = Colors.ToString(DefaultColor.LowResource);
/// <summary>
/// The color to display when the resource content is "medium".
/// </summary>
[KSPField]
[ColorSourceIDField]
public string mediumResourceColor = Colors.ToString(DefaultColor.MediumResource);
/// <summary>
/// The color to display when the resource content is "high".
/// </summary>
[KSPField]
[ColorSourceIDField]
public string highResourceColor = Colors.ToString(DefaultColor.HighResource);
/// <summary>
/// The resource abundance below which we treat it as having zero availability.
/// </summary>
[KSPField]
[StaticField]
public double unavailableResourceThreshold = 1E-03;
/// <summary>
/// The resource abundance threshold between "low" and "medium". The default value of
/// 0.025 is chosen specifically to align with the minimum mining threshold of the mini-drill.
/// </summary>
[KSPField]
[StaticField]
public double lowResourceThreshold = 0.025;
/// <summary>
/// The resource abundance threshold between "medium" and "high".
/// </summary>
[KSPField]
[StaticField]
public double highResourceThreshold = 0.05;
public override void ParseIDs()
{
base.ParseIDs();
inactiveSource = FindColorSource(inactiveColor);
unavailableSource = FindColorSource(unavailableColor);
lowSource = FindColorSource(lowResourceColor);
mediumSource = FindColorSource(mediumResourceColor);
highSource = FindColorSource(highResourceColor);
}
public override bool HasColor
{
get
{
return CurrentSource.HasColor;
}
}
public override Color OutputColor
{
get
{
return CurrentSource.OutputColor;
}
}
private IColorSource CurrentSource
{
get
{
// No scanning in the editor!
if (!HighLogic.LoadedSceneIsFlight) return inactiveSource;
// Can we scan, based on where we're currently located?
if (SourceModule == null) return inactiveSource;
bool canScan;
HarvestTypes scannerType = (HarvestTypes)SourceModule.ScannerType;
switch (scannerType)
{
case HarvestTypes.Planetary:
canScan = vessel.Landed || (ResourceUtilities.GetAltitude(vessel) <= SourceModule.MaxAbundanceAltitude);
break;
case HarvestTypes.Oceanic:
canScan = vessel.Splashed;
break;
case HarvestTypes.Atmospheric:
canScan = vessel.mainBody.atmosphere;
break;
case HarvestTypes.Exospheric:
canScan = true;
break;
default:
// Unknown scanner type! This should never happen; basically the only way is
// if Squad has updated the HarvestTypes enum since this code was written,
// to include new scanner types (in which case this code needs to be updated
// to take the new types into account).
return ColorSources.ERROR;
}
if (!canScan) return inactiveSource;
// Scanning is available. Have we unlocked the current biome?
bool isUnlocked = true;
if (scannerType == HarvestTypes.Planetary)
{
// It's a planetary scanner, so we have to care about biome.
string biomeName = vessel.GetCurrentBiome() ?? ResourceMap.GetDefaultSituation(scannerType);
isUnlocked = ResourceMap.Instance.IsBiomeUnlocked(vessel.mainBody.flightGlobalsIndex, biomeName);
}
// Now pick a source based on resource abundance.
IColorSource abundanceSource;
if (Abundance < unavailableResourceThreshold)
{
abundanceSource = unavailableSource;
}
else if (Abundance < lowResourceThreshold)
{
abundanceSource = lowSource;
}
else if (Abundance < highResourceThreshold)
{
abundanceSource = mediumSource;
}
else
{
abundanceSource = highSource;
}
return isUnlocked ? abundanceSource : (LOCKED_BLINK.State ? abundanceSource : ColorSources.BLACK);
}
}
/// <summary>
/// IToggle implementation.
/// </summary>
public bool ToggleStatus
{
get
{
// Toggle status is considered "active" if it can scan and there's anything available.
if (!HighLogic.LoadedSceneIsFlight) return false;
if (SourceModule == null) return false;
HarvestTypes scannerType = (HarvestTypes)SourceModule.ScannerType;
bool canScan = false;
switch (scannerType)
{
case HarvestTypes.Planetary:
canScan = vessel.Landed || (ResourceUtilities.GetAltitude(vessel) <= SourceModule.MaxAbundanceAltitude);
break;
case HarvestTypes.Oceanic:
canScan = vessel.Splashed;
break;
case HarvestTypes.Atmospheric:
canScan = vessel.mainBody.atmosphere;
break;
case HarvestTypes.Exospheric:
canScan = true;
break;
default:
// Unknown scanner type! This should never happen; basically the only way is
// if Squad has updated the HarvestTypes enum since this code was written,
// to include new scanner types (in which case this code needs to be updated
// to take the new types into account).
return false;
}
return canScan && (Abundance >= unavailableResourceThreshold);
}
}
private double Abundance
{
get { return (SourceModule == null) ? 0.0 : SourceModule.abundance; }
}
public double ScalarValue
{
get
{
return ToggleStatus ? Abundance : 0;
}
}
}
}