-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModuleIgnitionThrusterController.cs
More file actions
233 lines (189 loc) · 9.4 KB
/
ModuleIgnitionThrusterController.cs
File metadata and controls
233 lines (189 loc) · 9.4 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
227
228
229
230
231
232
233
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
namespace Ignition
{
abstract class ModuleIgnitionThrusterController : ModuleIgnitionController
{
[KSPField(isPersistant = true)]
public bool AutoComputeMaxThrust = true;
[KSPField(isPersistant = true)]
public double MaxThrustOriginal = 0;
protected double MaxThrustCurrent = 0;
[KSPField(isPersistant = true)]
public bool AutoComputeIspVacuum = true;
[KSPField(isPersistant = true)]
public double IspVacuumOriginal = 0;
protected double IspVacuumCurrent = 0;
[KSPField(isPersistant = true)]
public bool AutoComputeIspSeaLevel = true;
[KSPField(isPersistant = true)]
public double IspSeaLevelOriginal = 0;
protected double IspSeaLevelCurrent = 0;
protected double MaxFuelFlowCurrent => MaxThrustCurrent / (GetG() * IspVacuumCurrent);
[KSPField(isPersistant = true)]
public string PropellantNodeResourceNames = null;
[KSPField(guiName = "<b>Propellants</b>")]
[UI_Label(scene = UI_Scene.All)]
public string PropellantsString = "";
[KSPField(guiName = "<b>Thrust</b>")]
[UI_Label(scene = UI_Scene.All)]
public string ThrustString = "";
[KSPField(guiName = "<b>Isp</b>")]
[UI_Label(scene = UI_Scene.All)]
public string IspString = "";
protected abstract bool ModuleIsNull();
protected abstract void SetupOriginalData();
protected abstract void ApplyPropellantCombinationToModule();
protected abstract void RecompilePartInfo();
protected abstract double GetG();
protected abstract bool UseIspSeaLevel();
protected abstract double GetScaledMaxThrustOriginal();
protected abstract string GetGuiGroupName();
protected abstract List<Propellant> GetModulePropellants();
protected override bool DisplayGuiStrings()
{
return base.DisplayGuiStrings() && !ModuleIsNull();
}
protected void SetupPropellantNodeResourceNames()
{
var propellants = GetModulePropellants();
if (PropellantNodeResourceNames is null && !(propellants is null))
{
PropellantNodeResourceNames = "";
for (int i = 0; i < propellants.Count; i++)
{
PropellantNodeResourceNames += propellants[i].name;
if (i != propellants.Count - 1) PropellantNodeResourceNames += ";";
}
}
}
protected override void SetInfoStrings()
{
bool isActive = DisplayGuiStrings();
Fields["PropellantsString"].guiActiveEditor = isActive;
Fields["ThrustString"].guiActiveEditor = isActive;
Fields["IspString"].guiActiveEditor = isActive;
Fields["PropellantsString"].guiActive = isActive;
Fields["ThrustString"].guiActive = isActive;
Fields["IspString"].guiActive = isActive;
if (!isActive) return;
string groupName = GetGuiGroupName();
Fields["PropellantsString"].group.name = groupName;
Fields["ThrustString"].group.name = groupName;
Fields["IspString"].group.name = groupName;
Fields["PropellantsString"].group.displayName = groupName;
Fields["ThrustString"].group.displayName = groupName;
Fields["IspString"].group.displayName = groupName;
var configuredPropellantNames = new List<string>();
foreach (var propellant in PropellantConfigCurrent.Propellants) configuredPropellantNames.Add(propellant.name);
PropellantsString = PropellantConfigUtils.GetPropellantRatiosString(GetModulePropellants(), configuredPropellantNames);
if (UseIspSeaLevel())
{
ThrustString = GetValueString("kN", GetScaledMaxThrustOriginal(), MaxThrustCurrent, MaxThrustCurrent * IspSeaLevelCurrent / IspVacuumCurrent);
IspString = GetValueString("s", IspVacuumOriginal, IspVacuumCurrent, IspSeaLevelCurrent);
}
else
{
ThrustString = GetValueString("kN", GetScaledMaxThrustOriginal(), MaxThrustCurrent);
IspString = GetValueString("s", IspVacuumOriginal, IspVacuumCurrent);
}
}
public override void SetupData()
{
SetupOriginalData();
SetupPropellantNodeResourceNames();
}
protected double GetKeyframeValue(Keyframe[] keyframes, double time)
{
foreach (var keyframe in keyframes)
{
if (keyframe.time == time) return keyframe.value;
}
return 0;
}
private void ComputeNewStats()
{
if (PropellantConfigOriginal is null || PropellantConfigCurrent is null) return;
if (PropellantConfigOriginal.Propellants.Count == 0 || PropellantConfigCurrent.Propellants.Count == 0) return;
MaxThrustCurrent = GetScaledMaxThrustOriginal();
IspVacuumCurrent = IspVacuumOriginal;
IspSeaLevelCurrent = IspSeaLevelOriginal;
var thrustMultiplier = Math.Round(100 * PropellantConfigCurrent.ThrustMultiplier / PropellantConfigOriginal.ThrustMultiplier) / 100;
var ispVacuumMultiplier = Math.Round(100 * PropellantConfigCurrent.IspMultiplier / PropellantConfigOriginal.IspMultiplier) / 100;
if (AutoComputeMaxThrust)
{
var maxThrustChange = Math.Round(MaxThrustCurrent * (thrustMultiplier - 1) / 0.1) * 0.1;
if (Math.Abs(maxThrustChange) > 5) maxThrustChange = Math.Round(maxThrustChange);
if (Math.Abs(maxThrustChange) > 20) maxThrustChange = Math.Round(maxThrustChange / 5) * 5;
MaxThrustCurrent += maxThrustChange;
if (MaxThrustCurrent < 0) MaxThrustCurrent = 0;
}
if (AutoComputeIspVacuum)
{
var ispVacuumChange = Math.Round(IspVacuumOriginal * (ispVacuumMultiplier - 1));
if (Math.Abs(ispVacuumChange) > 10) ispVacuumChange = Math.Round(ispVacuumChange / 5) * 5;
IspVacuumCurrent += ispVacuumChange;
if (IspVacuumCurrent < 0) IspVacuumCurrent = 0;
}
if (AutoComputeIspSeaLevel && UseIspSeaLevel())
{
var ispSeaLevelVacuumChange = Math.Round((IspSeaLevelOriginal - IspVacuumOriginal) * ispVacuumMultiplier / thrustMultiplier);
if (Math.Abs(ispSeaLevelVacuumChange) > 10) ispSeaLevelVacuumChange = Math.Round(ispSeaLevelVacuumChange / 5) * 5;
IspSeaLevelCurrent += ispSeaLevelVacuumChange;
if (IspSeaLevelCurrent < 0) IspSeaLevelCurrent = 0;
}
}
public override void ApplyPropellantConfig()
{
ComputeNewStats();
if (ModuleIsNull()) return;
if (PropellantConfigCurrent is null) return;
if (PropellantConfigCurrent.Propellants.Count == 0) return;
if (MaxThrustCurrent == 0) return;
ApplyPropellantCombinationToModule();
}
protected Keyframe[] GetIspKeys()
{
var ispKeys = new List<Keyframe> { new Keyframe(0, (float)IspVacuumCurrent) };
if (UseIspSeaLevel())
{
ispKeys.Add(new Keyframe(1, (float)IspSeaLevelCurrent));
ispKeys.Add(new Keyframe(12, 0.001f));
}
return ispKeys.ToArray();
}
protected List<Propellant> GetAllCurrentPropellants(List<Propellant> allPropellantsPrevious)
{
// Add current configured propellants
var allPropellantsCurrent = new List<Propellant>(PropellantConfigCurrent.Propellants);
// Add propellants corresponding to original propellant nodes, i.e. not created by Ignition
if (!(PropellantNodeResourceNames is null))
{
var currentConfiguredPropellantNames = new List<string>();
foreach (var propellant in PropellantConfigCurrent.Propellants) currentConfiguredPropellantNames.Add(propellant.name);
var externalPropellantNames = PropellantNodeResourceNames.Split(';');
foreach (var propellant in allPropellantsPrevious)
{
if (externalPropellantNames.Contains(propellant.name) && !currentConfiguredPropellantNames.Contains(propellant.name)) allPropellantsCurrent.Add(propellant);
}
}
return allPropellantsCurrent;
}
protected string GetValueString(string unit, double vacuumOriginal, double vacuumCurrent, double seaLevelCurrent = 0)
{
var str = vacuumCurrent.ToString("0.0") + unit;
if (seaLevelCurrent != 0) str = seaLevelCurrent.ToString("0.0") + unit + " — " + str;
if (vacuumCurrent > vacuumOriginal) str += " (<color=#44FF44>+" + Math.Round(100 * (vacuumCurrent / vacuumOriginal - 1)) + "</color>%)";
else if (vacuumCurrent < vacuumOriginal) str += " (<color=#FF8888>-" + Math.Round(100 * (1 - vacuumCurrent / vacuumOriginal)) + "</color>%)";
return str;
}
public override string GetInfo()
{
UpdateAndApply(true);
RecompilePartInfo();
return base.GetInfo();
}
}
}