forked from KSP-RO/TestFlight
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TestFlightFailure_ResourceLeak.cs
220 lines (197 loc) · 7.45 KB
/
TestFlightFailure_ResourceLeak.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
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using TestFlightAPI;
namespace TestFlight
{
public class TestFlightFailure_ResourceLeak : TestFlightFailureBase
{
[KSPField]
public string resourceToLeak = "random";
[KSPField]
public string initialAmount = "10";
[KSPField]
public string perSecondAmount = "0.1";
[KSPField]
public bool calculatePerTick = false;
[KSPField(isPersistant = true)]
public bool isLeaking = false;
private List<ResourceLeak> leaks;
private float _initialAmount, _perSecondAmount;
public class ResourceLeak : IConfigNode
{
public int id = 0;
public double amount;
public double initialAmount;
public void Load(ConfigNode node)
{
id = int.Parse(node.GetValue("id"));
amount = double.Parse(node.GetValue("amount"));
initialAmount = 0d; // if we're loading, the initial leak has occurred.
}
public void Save(ConfigNode node)
{
node.AddValue("id", id);
node.AddValue("amount", amount.ToString("G17"));
}
public ResourceLeak(int newId, double newAmount, double newInit)
{
id = newId;
amount = newAmount;
initialAmount = newInit;
}
public ResourceLeak(ConfigNode node)
{
Load(node);
}
}
public override void OnStart(StartState state)
{
base.OnStart(state);
if (Failed)
DoFailure();
}
/// <summary>
/// Triggers the failure controlled by the failure module
/// </summary>
public override void DoFailure()
{
base.DoFailure();
if (resourceToLeak.ToLower() == "all")
{
foreach (PartResource r in this.part.Resources)
{
int resId = r.info.id;
ParseResourceValues(resId);
leaks.Add(new ResourceLeak(resId, _perSecondAmount, _initialAmount));
}
}
else
{
int resId = 0;
bool found = false;
if (resourceToLeak.ToLower() == "random")
{
if (part.Resources.Count > 0)
{
List<PartResource> allResources = this.part.Resources.ToList();
int randomResource = TestFlightUtil.GetCore(this.part, Configuration).RandomGenerator.Next(0, allResources.Count());
resId = allResources[randomResource].info.id;
found = true;
}
}
else
{
List<PartResource> resources = this.part.Resources.ToList().Where(n => n.resourceName == resourceToLeak).ToList();
if (resources != null && resources.Count > 0)
{
resId = resources[0].info.id;
found = true;
}
}
if (found)
{
ParseResourceValues(resId);
leaks.Add(new ResourceLeak(resId, _perSecondAmount, _initialAmount));
}
}
if (leaks.Count > 0 && !isLeaking)
{
isLeaking = true;
foreach (ResourceLeak leak in leaks)
this.part.RequestResource(leak.id, leak.initialAmount, ResourceFlowMode.NO_FLOW);
}
}
public void FixedUpdate()
{
if(HighLogic.LoadedSceneIsFlight && isLeaking)
{
ResourceLeak leak;
for(int i = leaks.Count - 1; i >= 0; --i)
{
leak = leaks[i];
if (calculatePerTick)
{
leak.amount = ParseValue(perSecondAmount, leak.id);
}
this.part.RequestResource(leak.id, _perSecondAmount * TimeWarp.fixedDeltaTime, ResourceFlowMode.NO_FLOW);
}
}
}
public override float DoRepair()
{
base.DoRepair();
isLeaking = false;
return 0;
}
private float ParseValue(string rawValue, int leakingResourceID)
{
float parsedValue = 0f;
int index = rawValue.IndexOf("%");
string trimmedValue = "";
double calculateFromAmount = 0;
rawValue = rawValue.ToLowerInvariant();
if (index > 0)
{
if (rawValue.EndsWith("%t"))
{
trimmedValue = rawValue.Substring(0, index);
if (!float.TryParse(trimmedValue, System.Globalization.NumberStyles.Any, System.Globalization.CultureInfo.CreateSpecificCulture("en-US"), out parsedValue))
parsedValue = 0f;
// Calculate the % value based on the total capacity of the tank
calculateFromAmount = this.part.Resources.Get(leakingResourceID).maxAmount;
Log(String.Format("Calculating leak amount from maxAmount: {0:F2}", calculateFromAmount));
}
else if (rawValue.EndsWith("%c"))
{
trimmedValue = rawValue.Substring(0, index);
if (!float.TryParse(trimmedValue, System.Globalization.NumberStyles.Any, System.Globalization.CultureInfo.CreateSpecificCulture("en-US"), out parsedValue))
parsedValue = 0f;
// Calculate the % value based on the current resource level of the tank
calculateFromAmount = this.part.Resources.Get(leakingResourceID).amount;
Log(String.Format("Calculating leak amount from current amount: {0:F2}", calculateFromAmount));
}
Log(String.Format("Base value was parsed as: {0:F2}", parsedValue));
parsedValue = parsedValue * (float)calculateFromAmount;
Log(String.Format("Calculated leak: {0:F2}", parsedValue));
}
else
{
if (!float.TryParse(rawValue, out parsedValue))
parsedValue = 0f;
}
return parsedValue;
}
private void ParseResourceValues(int resID)
{
_initialAmount = ParseValue(initialAmount, resID);
_perSecondAmount = ParseValue(perSecondAmount, resID);
}
public override void OnLoad(ConfigNode node)
{
if (node.HasNode("LEAK"))
{
leaks.Clear();
foreach (ConfigNode n in node.GetNodes("LEAK"))
leaks.Add(new ResourceLeak(n));
}
}
public override void OnSave(ConfigNode node)
{
if (leaks.Count > 0)
{
foreach (ResourceLeak leak in leaks)
{
ConfigNode n = node.AddNode("LEAK");
leak.Save(n);
}
}
}
public override void OnAwake()
{
leaks = new List<ResourceLeak>();
}
}
}