/
SimVesselBuilder.cs
338 lines (275 loc) · 14.2 KB
/
SimVesselBuilder.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
#nullable enable
using System;
using System.Collections.Generic;
using System.Reflection;
using KSP.UI;
using KSP.UI.Screens;
using MechJebLib.Simulations.PartModules;
using MuMech;
using UnityEngine;
using static MechJebLib.Utils.Statics;
namespace MechJebLib.Simulations
{
public partial class SimVesselManager
{
public class SimVesselBuilder
{
private Dictionary<Part, SimPart> _partMapping => _manager._partMapping;
private Dictionary<SimPart, Part> _inversePartMapping => _manager._inversePartMapping;
private Dictionary<SimPartModule, PartModule> _inversePartModuleMapping => _manager._inversePartModuleMapping;
private static readonly FieldInfo? _rfSpoolUpTime;
private delegate double CrewMass(ProtoCrewMember crew);
private static readonly CrewMass _crewMassDelegate;
private SimVessel _vessel
{
get => _manager._vessel;
set => _manager._vessel = value;
}
private IShipconstruct _kspVessel
{
get => _manager._kspVessel;
set => _manager._kspVessel = value;
}
private readonly SimVesselManager _manager;
private static readonly Type? _rfType;
static SimVesselBuilder()
{
_crewMassDelegate = Versioning.version_major == 1 && Versioning.version_minor < 11 ? (CrewMass)CrewMassOld : CrewMassNew;
if (ReflectionUtils.isAssemblyLoaded("RealFuels"))
{
_rfSpoolUpTime = ReflectionUtils.getFieldByReflection("RealFuels", "RealFuels.ModuleEnginesRF", "effectiveSpoolUpTime");
if (_rfSpoolUpTime == null)
Debug.Log(
"MechJeb BUG: RealFuels loaded, but RealFuels.ModuleEnginesRF has no effectiveSpoolUpTime field, disabling spoolup");
_rfType = Type.GetType("RealFuels.ModuleEnginesRF, RealFuels");
}
}
private static double CrewMassOld(ProtoCrewMember crew) => PhysicsGlobals.KerbalCrewMass;
private static double CrewMassNew(ProtoCrewMember crew) => PhysicsGlobals.KerbalCrewMass + crew.ResourceMass() + crew.InventoryMass();
public SimVesselBuilder(SimVesselManager manager)
{
_manager = manager;
}
internal void UpdateEngineSet()
{
foreach (SimPart part in _vessel.Parts)
{
foreach (SimPartModule m in part.Modules)
{
if (m is SimModuleEngines e)
_vessel.EnginesDroppedInStage[part.DecoupledInStage].Add(e);
if (m is SimModuleRCS r)
_vessel.RCSDroppedInStage[part.DecoupledInStage].Add(r);
}
}
}
internal void UpdateLinks()
{
foreach (Part kspPart in _kspVessel.Parts)
{
SimPart p = _partMapping[kspPart];
if (!(kspPart.parent is null))
p.Links.Add(_partMapping[kspPart.parent]);
foreach (Part child in kspPart.children)
p.Links.Add(_partMapping[child]);
}
}
internal void UpdateCrossFeedSet()
{
foreach (Part kspPart in _kspVessel.Parts)
{
SimPart p = _partMapping[kspPart];
foreach (Part pset in kspPart.crossfeedPartSet.GetParts())
{
p.CrossFeedPartSet.Add(_partMapping[pset]);
}
}
}
internal void BuildVessel(IShipconstruct kspVessel)
{
_vessel.Dispose();
_vessel = SimVessel.Borrow();
_kspVessel = kspVessel;
}
internal void BuildParts()
{
_vessel.CurrentStage = StageManager.CurrentStage;
foreach (Part kspPart in _kspVessel.Parts)
{
SimPart part = BuildPart(kspPart);
_vessel.Parts.Add(part);
_partMapping.Add(kspPart, part);
_inversePartMapping.Add(part, kspPart);
}
}
private SimPart BuildPart(Part kspPart)
{
var part = SimPart.Borrow(_vessel, kspPart.partName);
part.InverseStage = kspPart.inverseStage;
part.ActivatesEvenIfDisconnected = kspPart.ActivatesEvenIfDisconnected;
part.StagingOn = kspPart.stagingOn;
part.ResourcePriority = kspPart.GetResourcePriority();
part.ResourceRequestRemainingThreshold = kspPart.resourceRequestRemainingThreshold;
part.Mass = kspPart.mass;
part.Name = kspPart.name;
part.DryMass = kspPart.prefabMass;
part.ModulesStagedMass = GetModuleMass(kspPart, kspPart.prefabMass, ModifierStagingSituation.STAGED);
part.ModulesUnstagedMass = GetModuleMass(kspPart, kspPart.prefabMass, ModifierStagingSituation.UNSTAGED);
part.DecoupledInStage = int.MinValue;
HandleCrewMass(part, kspPart);
BuildModules(part, kspPart);
return part;
}
private void BuildModules(SimPart part, Part kspPart)
{
foreach (PartModule kspModule in kspPart.Modules)
{
if (!TryBuildModule(part, kspModule, out SimPartModule? m))
continue;
part.Modules.Add(m!);
_inversePartModuleMapping.Add(m!, kspModule);
}
}
// FIXME: needs [NotNullWhen(returnValue: true)]
private bool TryBuildModule(SimPart part, PartModule kspModule, out SimPartModule? m)
{
m = kspModule switch
{
ModuleEngines kspEngine => BuildModuleEngines(part, kspEngine),
LaunchClamp _ => BuildLaunchClamp(part),
ModuleDecouplerBase kspModuleDecouple => BuildModuleDecouple(part, kspModuleDecouple),
ModuleDockingNode _ => BuildDockingNode(part),
ModuleRCS kspModuleRCS => BuildModuleRCS(part, kspModuleRCS),
_ => null
} ?? kspModule.moduleName switch
{
"ProceduralFairingDecoupler" => BuildProceduralFairingDecoupler(part),
_ => null
};
return m != null;
}
private SimPartModule BuildDockingNode(SimPart part)
{
var decoupler = SimModuleDockingNode.Borrow(part);
return decoupler;
}
private SimPartModule BuildModuleDecouple(SimPart part, ModuleDecouplerBase kspModuleDecouple)
{
var decoupler = SimModuleDecouple.Borrow(part);
decoupler.IsOmniDecoupler = kspModuleDecouple.isOmniDecoupler;
return decoupler;
}
private SimLaunchClamp BuildLaunchClamp(SimPart part)
{
part.IsLaunchClamp = true;
return SimLaunchClamp.Borrow(part);
}
private SimModuleEngines BuildModuleEngines(SimPart part, ModuleEngines kspEngine)
{
var engine = SimModuleEngines.Borrow(part);
engine.ThrottleLocked = kspEngine.throttleLocked;
engine.MaxFuelFlow = kspEngine.maxFuelFlow;
engine.MinFuelFlow = kspEngine.minFuelFlow;
engine.FlowMultiplier = kspEngine.flowMultiplier;
engine.G = kspEngine.g;
engine.MaxThrust = kspEngine.maxThrust;
engine.MinThrust = kspEngine.minThrust;
engine.Clamp = kspEngine.CLAMP;
engine.FlowMultCap = kspEngine.flowMultCap;
engine.FlowMultCapSharpness = kspEngine.flowMultCapSharpness;
engine.AtmChangeFlow = kspEngine.atmChangeFlow;
engine.UseAtmCurve = kspEngine.useAtmCurve;
engine.UseAtmCurveIsp = kspEngine.useAtmCurveIsp;
engine.UseThrottleIspCurve = kspEngine.useThrottleIspCurve;
engine.UseThrustCurve = kspEngine.useThrustCurve;
engine.UseVelCurve = kspEngine.useVelCurve;
engine.UseVelCurveIsp = kspEngine.useVelCurveIsp;
engine.ThrustCurve.LoadH1(kspEngine.thrustCurve);
engine.ThrottleIspCurve.LoadH1(kspEngine.throttleIspCurve);
engine.ThrottleIspCurveAtmStrength.LoadH1(kspEngine.throttleIspCurveAtmStrength);
engine.VelCurve.LoadH1(kspEngine.velCurve);
engine.VelCurveIsp.LoadH1(kspEngine.velCurveIsp);
engine.ATMCurve.LoadH1(kspEngine.atmCurve);
engine.ATMCurveIsp.LoadH1(kspEngine.atmCurveIsp);
engine.AtmosphereCurve.LoadH1(kspEngine.atmosphereCurve);
engine.ThrustTransformMultipliers.Clear();
foreach (double multiplier in kspEngine.thrustTransformMultipliers)
engine.ThrustTransformMultipliers.Add(multiplier);
engine.ThrustDirectionVectors.Clear();
foreach (Transform transform in kspEngine.thrustTransforms)
// thrust transforms point at the flamey end and we want to point at the pointy end
engine.ThrustDirectionVectors.Add(MathExtensions.WorldToV3Rotated(-transform.forward));
engine.Propellants.Clear();
foreach (Propellant p in kspEngine.propellants)
engine.Propellants.Add(new SimPropellant(p.id, p.ignoreForIsp, p.ratio, (SimFlowMode)p.GetFlowMode(),
PartResourceLibrary.Instance.GetDefinition(p.id).density));
_vessel.EnginesActivatedInStage[kspEngine.part.inverseStage].Add(engine);
part.IsThrottleLocked = kspEngine.throttleLocked;
part.IsEngine = true;
engine.ModuleSpoolupTime = 0;
engine.isModuleEnginesRF = false;
if (ReflectionUtils.isAssemblyLoaded("RealFuels"))
{
engine.isModuleEnginesRF = _rfType!.IsInstanceOfType(kspEngine);
if (engine.isModuleEnginesRF && _rfSpoolUpTime!.GetValue(kspEngine) is float floatVal)
engine.ModuleSpoolupTime = floatVal;
}
return engine;
}
private SimPartModule BuildModuleRCS(SimPart part, ModuleRCS kspModuleRCS)
{
var rcs = SimModuleRCS.Borrow(part);
rcs.G = kspModuleRCS.G;
rcs.ISPMult = kspModuleRCS.ispMult;
rcs.ThrustPercentage = kspModuleRCS.thrustPercentage;
rcs.MaxFuelFlow = kspModuleRCS.maxFuelFlow;
rcs.AtmosphereCurve.LoadH1(kspModuleRCS.atmosphereCurve);
rcs.Propellants.Clear();
foreach (Propellant p in kspModuleRCS.propellants)
rcs.Propellants.Add(new SimPropellant(p.id, p.ignoreForIsp, p.ratio, (SimFlowMode)p.GetFlowMode(),
PartResourceLibrary.Instance.GetDefinition(p.id).density));
_vessel.RCSActivatedInStage[kspModuleRCS.part.inverseStage].Add(rcs);
return rcs;
}
private SimProceduralFairingDecoupler BuildProceduralFairingDecoupler(SimPart part) => SimProceduralFairingDecoupler.Borrow(part);
private static double GetModuleMass(Part kspPart, float defaultMass, ModifierStagingSituation sit)
{
double mass = 0;
for (int i = 0; i < kspPart.Modules.Count; i++)
if (kspPart.Modules[i] is IPartMassModifier m)
mass += m.GetModuleMass(defaultMass, sit);
return mass;
}
private void HandleCrewMass(SimPart part, Part kspPart)
{
part.CrewMass = 0;
if (HighLogic.LoadedSceneIsFlight && kspPart.protoModuleCrew != null)
{
for (int i = 0; i < kspPart.protoModuleCrew.Count; i++)
{
ProtoCrewMember crewMember = kspPart.protoModuleCrew[i];
part.CrewMass += _crewMassDelegate(crewMember);
}
}
else if (HighLogic.LoadedSceneIsEditor)
{
if (!(CrewAssignmentDialog.Instance is null) && CrewAssignmentDialog.Instance.CurrentManifestUnsafe != null)
{
PartCrewManifest partCrewManifest = CrewAssignmentDialog.Instance.CurrentManifestUnsafe.GetPartCrewManifest(kspPart.craftID);
if (partCrewManifest != null)
{
ProtoCrewMember?[]? partCrew = null;
partCrewManifest.GetPartCrew(ref partCrew);
for (int i = 0; i < partCrew.Length; i++)
{
ProtoCrewMember? crewMember = partCrew[i];
if (crewMember == null) continue;
part.CrewMass += _crewMassDelegate(crewMember);
}
}
}
}
}
}
}
}