-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathVariableIspEngine.cs
310 lines (240 loc) · 9.77 KB
/
VariableIspEngine.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
/// VariableIspEngine
/// ---------------------------------------------------
/// A module that allows the Isp and thrust of an engine to be varied via a GUI
///
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
namespace NearFuture
{
class VariableISPEngine:PartModule
{
// Use the direct throttle method
[KSPField(isPersistant = false)]
public bool UseDirectThrottle = false;
// Link all engines
[KSPField(isPersistant = true)]
public bool LinkAllEngines = false;
// Maximum thrust
[KSPField(isPersistant = false)]
public float MaxThrust;
// Isp at maximum thrust
[KSPField(isPersistant = false)]
public float MaxThrustIsp;
// Minimum thrust
[KSPField(isPersistant = false)]
public float MinThrust;
// Isp at minimum thrust
[KSPField(isPersistant = false)]
public float MinThrustIsp;
// Name of the fuel
[KSPField(isPersistant = false)]
public string FuelName;
// Ec to use
[KSPField(isPersistant = false)]
public float EnergyUsage = 100f;
// Current thrust setting
[KSPField(isPersistant = true, guiActive = true, guiActiveEditor = true, guiName = "Variable Thrust Level"), UI_FloatRange(minValue = 0f, maxValue = 100f, stepIncrement = 0.1f)]
public float CurThrustSetting = 0f;
[KSPEvent(guiActive = true, guiName = "Link All Variable Engines", active = true)]
public void LinkEngines()
{
LinkAllEngines = true;
}
// Retract all radiators attached to this reactor
[KSPEvent(guiActive = true, guiName = "Unlink All Variable Engines", active = false)]
public void UnlinkEngines()
{
LinkAllEngines = false;
}
// Actions
[KSPAction("Link Engines")]
public void LinkEnginesAction(KSPActionParam param)
{
LinkEngines();
}
[KSPAction("Unlink Engines")]
public void UnlinkEnginesAction(KSPActionParam param)
{
UnlinkEngines();
}
[KSPAction("Toggle Link Engines")]
public void ToggleLinkEnginesAction(KSPActionParam param)
{
LinkAllEngines = !LinkAllEngines;
}
public override string GetInfo()
{
return String.Format("Maximum Thrust: {0:F1} kN", MaxThrust) + "\n" +
String.Format("Isp at Maximum Thrust: {0:F0} s", MaxThrustIsp) + "\n";
}
private float minThrust= 0f;
private ModuleEnginesFX engine;
private Propellant ecPropellant;
private Propellant fuelPropellant;
private FloatCurve ThrustCurve;
private FloatCurve AtmoCurve;
public override void OnLoad(ConfigNode node)
{
base.OnLoad(node);
this.moduleName = "Variable ISP Engine";
}
public void ChangeIspAndThrust(float level)
{
engine.atmosphereCurve = new FloatCurve();
engine.atmosphereCurve.Add(0f, Mathf.Lerp(MinThrustIsp ,MaxThrustIsp,level));
engine.maxThrust = Mathf.Lerp(MinThrust, MaxThrust, level);
RecalculateRatios(engine.maxThrust, Mathf.Lerp(MinThrustIsp, MaxThrustIsp, level));
}
private void RecalculateRatios(float desiredthrust, float desiredisp)
{
double fuelDensity = PartResourceLibrary.Instance.GetDefinition(fuelPropellant.name).density;
double fuelRate = ((desiredthrust * 1000f) / (desiredisp * 9.82d)) / (fuelDensity*1000f);
float ecRate = EnergyUsage / (float)fuelRate;
fuelPropellant.ratio = 0.1f;
ecPropellant.ratio = fuelPropellant.ratio * ecRate;
CalculateCurves();
}
public override void OnStart(PartModule.StartState state)
{
if (state != StartState.Editor)
started = true;
else
started = false;
// Get moduleEngines
PartModuleList pml = this.part.Modules;
for (int i = 0; i < pml.Count; i++)
{
PartModule curModule = pml.GetModule(i);
engine = curModule.GetComponent<ModuleEnginesFX>();
}
if (engine != null)
Debug.Log("NFPP: Engine Check Passed");
foreach (Propellant prop in engine.propellants)
{
if (prop.name == FuelName)
fuelPropellant = prop;
if (prop.name == "ElectricCharge")
ecPropellant = prop;
}
if (UseDirectThrottle)
ChangeIspAndThrust(engine.requestedThrottle);
else
ChangeIspAndThrust(CurThrustSetting / 100f);
if (state != StartState.Editor)
SetupVariableEngines();
CalculateCurves();
Debug.Log("NFPP: Variable ISP engine setup complete");
if (UseDirectThrottle)
{
Debug.Log("NFPP: Using direct throttle method");
}
}
private void CalculateCurves()
{
ThrustCurve = new FloatCurve();
ThrustCurve.Add(0f, engine.maxThrust);
ThrustCurve.Add(1f, 0f);
AtmoCurve = new FloatCurve();
AtmoCurve.Add(0f, engine.atmosphereCurve.Evaluate(0f));
float rate = FindFlowRate(engine.maxThrust, engine.atmosphereCurve.Evaluate(0f), fuelPropellant);
AtmoCurve.Add(1f, FindIsp(minThrust, rate, fuelPropellant));
}
// finds the flow rate given thrust, isp and the propellant
private float FindFlowRate(float thrust, float isp, Propellant fuelPropellant)
{
double fuelDensity = PartResourceLibrary.Instance.GetDefinition(fuelPropellant.name).density;
double fuelRate = ((thrust * 1000f) / (isp * 9.82d)) / (fuelDensity * 1000f);
return (float)fuelRate;
}
private float FindIsp(float thrust, float flowRate, Propellant fuelPropellant)
{
double fuelDensity = PartResourceLibrary.Instance.GetDefinition(fuelPropellant.name).density;
double isp = (((minThrust * 1000f) / (9.82d)) / flowRate) / (fuelDensity * 1000f);
return (float)isp;
}
private void SetupVariableEngines()
{
allVariableEngines = new List<VariableISPEngine>();
List<Part> allParts = this.vessel.parts;
foreach (Part pt in allParts)
{
PartModuleList pml = pt.Modules;
for (int i = 0; i < pml.Count; i++)
{
PartModule curModule = pml.GetModule(i);
VariableISPEngine candidate = curModule.GetComponent<VariableISPEngine>();
if (candidate != null && candidate != this && !allVariableEngines.Contains(candidate ) )
allVariableEngines.Add(candidate);
}
}
}
int frameCounter = 0;
float lastThrottle = -1f;
float lastThrustSetting = -1f;
List<VariableISPEngine> allVariableEngines;
public void ChangeIspAndThrustLinked(float level)
{
//ChangeIspAndThrust(level);
ResetFrameCount();
LinkAllEngines = true;
CurThrustSetting = level * 100f;
}
public void ResetFrameCount()
{
// frameCounter = 0;
}
public override void OnUpdate()
{
if ((LinkAllEngines && Events["LinkEngines"].active) || (!LinkAllEngines && Events["UnlinkEngines"].active))
{
Events["LinkEngines"].active = !LinkAllEngines;
Events["UnlinkEngines"].active = LinkAllEngines;
}
}
bool started = false;
public void FixedUpdate()
{
if (engine != null && started)
{
if (UseDirectThrottle)
{
float throttleAmt = engine.requestedThrottle;
Debug.Log(throttleAmt);
if (throttleAmt != lastThrottle)
{
ChangeIspAndThrust(throttleAmt);
lastThrottle = throttleAmt;
}
CurThrustSetting = engine.requestedThrottle * 100f;
}
else
{
if (LinkAllEngines)
{
//Debug.Log("NFPP: VariableEngineCount is " + allVariableEngines.Count.ToString());
foreach (VariableISPEngine variableEngine in allVariableEngines)
{
variableEngine.ChangeIspAndThrustLinked(CurThrustSetting / 100f);
}
}
frameCounter++;
if (frameCounter >= 10)
{
if (CurThrustSetting != lastThrustSetting)
{
ChangeIspAndThrust(CurThrustSetting / 100f);
lastThrustSetting = CurThrustSetting;
}
frameCounter = 0;
}
}
engine.maxThrust = ThrustCurve.Evaluate((float)FlightGlobals.getStaticPressure(vessel.transform.position));
engine.atmosphereCurve = new FloatCurve();
engine.atmosphereCurve.Add(0f, AtmoCurve.Evaluate((float)FlightGlobals.getStaticPressure(vessel.transform.position)));
}
}
}
}