-
Notifications
You must be signed in to change notification settings - Fork 49
/
FSfuelSwitch.cs
356 lines (325 loc) · 13.6 KB
/
FSfuelSwitch.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
using System.Collections.Generic;
using System.Text;
using UnityEngine;
namespace Firespitter.customization
{
public class FSfuelSwitch : PartModule, IPartCostModifier
{
[KSPField]
public string resourceNames = "ElectricCharge;LiquidFuel,Oxidizer;MonoPropellant";
[KSPField]
public string resourceAmounts = "100;75,25;200";
[KSPField]
public string initialResourceAmounts = "";
[KSPField]
public float basePartMass = 0.25f;
[KSPField]
public string tankMass = "0;0;0;0";
[KSPField]
public string tankCost = "0; 0; 0; 0";
[KSPField]
public bool displayCurrentTankCost = false;
[KSPField]
public bool hasGUI = true;
[KSPField]
public bool availableInFlight = false;
[KSPField]
public bool availableInEditor = true;
[KSPField(isPersistant = true)]
public int selectedTankSetup = -1;
[KSPField(isPersistant = true)]
public bool hasLaunched = false;
[KSPField]
public bool showInfo = true; // if false, does not feed info to the part list pop up info menu
[KSPField(guiActive = false, guiActiveEditor = false, guiName = "Added cost")]
public float addedCost = 0f;
[KSPField(guiActive = false, guiActiveEditor = true, guiName = "Dry mass")]
public float dryMassInfo = 0f;
private List<FSmodularTank> tankList;
private List<double> weightList;
private List<double> tankCostList;
private bool initialized = false;
[KSPField (isPersistant = true)]
public bool configLoaded = false;
UIPartActionWindow tweakableUI;
public override void OnStart(PartModule.StartState state)
{
initializeData();
if (selectedTankSetup == -1)
{
selectedTankSetup = 0;
assignResourcesToPart(false);
}
}
public override void OnAwake()
{
//Debug.Log("FS AWAKE "+initialized+" "+configLoaded+" "+resourceAmounts);
if (configLoaded)
{
initializeData();
}
//Debug.Log("FS AWAKE DONE " + (configLoaded ? tankList.Count.ToString() : "NO CONFIG"));
}
public override void OnLoad(ConfigNode node)
{
base.OnLoad(node);
//Debug.Log("FS LOAD " + initialized + " " + resourceAmounts+configLoaded);
if (!configLoaded)
{
initializeData();
}
if (basePartMass != part.mass)
{
Debug.LogError("Error: FSFuelSwitch Mass Discrepancy detected in part '" + part.name + "'.", part);
}
configLoaded = true;
//Debug.Log("FS LOAD DONE " + tankList.Count);
}
private void initializeData()
{
if (!initialized)
{
setupTankList(false);
weightList = Tools.parseDoubles(tankMass);
tankCostList = Tools.parseDoubles(tankCost);
if (HighLogic.LoadedSceneIsFlight) hasLaunched = true;
if (hasGUI)
{
Events["nextTankSetupEvent"].guiActive = availableInFlight;
Events["nextTankSetupEvent"].guiActiveEditor = availableInEditor;
Events["previousTankSetupEvent"].guiActive = availableInFlight;
Events["previousTankSetupEvent"].guiActiveEditor = availableInEditor;
}
else
{
Events["nextTankSetupEvent"].guiActive = false;
Events["nextTankSetupEvent"].guiActiveEditor = false;
Events["previousTankSetupEvent"].guiActive = false;
Events["previousTankSetupEvent"].guiActiveEditor = false;
}
if (HighLogic.CurrentGame == null || HighLogic.CurrentGame.Mode == Game.Modes.CAREER)
{
Fields["addedCost"].guiActiveEditor = displayCurrentTankCost;
}
initialized = true;
}
}
[KSPEvent(guiActive = true, guiActiveEditor = true, guiName = "Next tank setup")]
public void nextTankSetupEvent()
{
selectedTankSetup++;
if (selectedTankSetup >= tankList.Count)
{
selectedTankSetup = 0;
}
assignResourcesToPart(true);
}
[KSPEvent(guiActive = true, guiActiveEditor = true, guiName = "Previous tank setup")]
public void previousTankSetupEvent()
{
selectedTankSetup--;
if (selectedTankSetup < 0)
{
selectedTankSetup = tankList.Count-1;
}
assignResourcesToPart(true);
}
public void selectTankSetup(int i, bool calledByPlayer)
{
initializeData();
if (selectedTankSetup != i)
{
selectedTankSetup = i;
assignResourcesToPart(calledByPlayer);
}
}
private void assignResourcesToPart(bool calledByPlayer)
{
// destroying a resource messes up the gui in editor, but not in flight.
setupTankInPart(part, calledByPlayer);
if (HighLogic.LoadedSceneIsEditor)
{
for (int s = 0; s < part.symmetryCounterparts.Count; s++)
{
setupTankInPart(part.symmetryCounterparts[s], calledByPlayer);
FSfuelSwitch symSwitch = part.symmetryCounterparts[s].GetComponent<FSfuelSwitch>();
if (symSwitch != null)
{
symSwitch.selectedTankSetup = selectedTankSetup;
}
}
}
//Debug.Log("refreshing UI");
if (tweakableUI == null)
{
tweakableUI = Tools.FindActionWindow(part);
}
if (tweakableUI != null)
{
tweakableUI.displayDirty = true;
}
else
{
Debug.Log("no UI to refresh");
}
}
private void setupTankInPart(Part currentPart, bool calledByPlayer)
{
currentPart.Resources.list.Clear();
PartResource[] partResources = currentPart.GetComponents<PartResource>();
for (int i = 0; i < partResources.Length; i++)
{
DestroyImmediate(partResources[i]);
}
for (int tankCount = 0; tankCount < tankList.Count; tankCount++)
{
if (selectedTankSetup == tankCount)
{
for (int resourceCount = 0; resourceCount < tankList[tankCount].resources.Count; resourceCount++)
{
if (tankList[tankCount].resources[resourceCount].name != "Structural")
{
//Debug.Log("new node: " + tankList[i].resources[j].name);
ConfigNode newResourceNode = new ConfigNode("RESOURCE");
newResourceNode.AddValue("name", tankList[tankCount].resources[resourceCount].name);
newResourceNode.AddValue("maxAmount", tankList[tankCount].resources[resourceCount].maxAmount);
if (calledByPlayer && !HighLogic.LoadedSceneIsEditor)
{
newResourceNode.AddValue("amount", 0.0f);
}
else
{
newResourceNode.AddValue("amount", tankList[tankCount].resources[resourceCount].amount);
}
//Debug.Log("add node to part");
currentPart.AddResource(newResourceNode);
}
else
{
//Debug.Log("Skipping structural fuel type");
}
}
}
}
currentPart.Resources.UpdateList();
updateWeight(currentPart, selectedTankSetup);
updateCost();
}
private float updateCost()
{
if (selectedTankSetup >= 0 && selectedTankSetup < tankCostList.Count)
{
addedCost = (float)tankCostList[selectedTankSetup];
}
else
{
addedCost = 0f;
}
//GameEvents.onEditorShipModified.Fire(EditorLogic.fetch.ship); //crashes game
return addedCost;
}
private void updateWeight(Part currentPart, int newTankSetup)
{
if (newTankSetup < weightList.Count)
{
currentPart.mass = (float)(basePartMass + weightList[newTankSetup]);
}
}
public override void OnUpdate()
{
}
public void Update()
{
if (HighLogic.LoadedSceneIsEditor)
{
dryMassInfo = part.mass;
}
}
private void setupTankList(bool calledByPlayer)
{
tankList = new List<FSmodularTank>();
weightList = new List<double>();
tankCostList = new List<double>();
// First find the amounts each tank type is filled with
List<List<double>> resourceList = new List<List<double>>();
List<List<double>> initialResourceList = new List<List<double>>();
string[] resourceTankArray = resourceAmounts.Split(';');
string[] initialResourceTankArray = initialResourceAmounts.Split(';');
if (initialResourceAmounts.Equals("") ||
initialResourceTankArray.Length != resourceTankArray.Length)
{
initialResourceTankArray = resourceTankArray;
}
//Debug.Log("FSDEBUGRES: " + resourceTankArray.Length+" "+resourceAmounts);
for (int tankCount = 0; tankCount < resourceTankArray.Length; tankCount++)
{
resourceList.Add(new List<double>());
initialResourceList.Add(new List<double>());
string[] resourceAmountArray = resourceTankArray[tankCount].Trim().Split(',');
string[] initialResourceAmountArray = initialResourceTankArray[tankCount].Trim().Split(',');
if (initialResourceAmounts.Equals("") ||
initialResourceAmountArray.Length != resourceAmountArray.Length)
{
initialResourceAmountArray = resourceAmountArray;
}
for (int amountCount = 0; amountCount < resourceAmountArray.Length; amountCount++)
{
try
{
resourceList[tankCount].Add(double.Parse(resourceAmountArray[amountCount].Trim()));
initialResourceList[tankCount].Add(double.Parse(initialResourceAmountArray[amountCount].Trim()));
}
catch
{
Debug.Log("FSfuelSwitch: error parsing resource amount " + tankCount + "/" + amountCount + ": '" + resourceTankArray[amountCount] + "': '" + resourceAmountArray[amountCount].Trim()+"'");
}
}
}
// Then find the kinds of resources each tank holds, and fill them with the amounts found previously, or the amount hey held last (values kept in save persistence/craft)
string[] tankArray = resourceNames.Split(';');
for (int tankCount = 0; tankCount < tankArray.Length; tankCount++)
{
FSmodularTank newTank = new FSmodularTank();
tankList.Add(newTank);
string[] resourceNameArray = tankArray[tankCount].Split(',');
for (int nameCount = 0; nameCount < resourceNameArray.Length; nameCount++)
{
engine.FSresource newResource = new engine.FSresource(resourceNameArray[nameCount].Trim(' '));
if (resourceList[tankCount] != null)
{
if (nameCount < resourceList[tankCount].Count)
{
newResource.maxAmount = resourceList[tankCount][nameCount];
newResource.amount = initialResourceList[tankCount][nameCount];
}
}
newTank.resources.Add(newResource);
}
}
}
public override string GetInfo()
{
if (showInfo)
{
List<string> resourceList = Tools.parseNames(resourceNames);
StringBuilder info = new StringBuilder();
info.AppendLine("Fuel tank setups available:");
for (int i = 0; i < resourceList.Count; i++)
{
info.AppendLine(resourceList[i].Replace(",", ", "));
}
return info.ToString();
}
else
return string.Empty;
}
public float GetModuleCost(float defaultCost, ModifierStagingSituation sit)
{
return updateCost();
}
public ModifierChangeWhen GetModuleCostChangeWhen()
{
return ModifierChangeWhen.CONSTANTLY;
}
}
}