/
SimVesselUpdater.cs
244 lines (205 loc) · 9.96 KB
/
SimVesselUpdater.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
#nullable enable
using System.Collections.Generic;
using System.Reflection;
using System.Runtime.CompilerServices;
using KSP.UI.Screens;
using MechJebLib.Simulations.PartModules;
using MuMech;
using UnityEngine;
using static MechJebLib.Statics;
using static System.Math;
namespace MechJebLib.Simulations
{
public partial class SimVesselManager
{
public class SimVesselUpdater
{
private readonly SimVesselManager _manager;
private Dictionary<Part, SimPart> _partMapping => _manager._partMapping;
private Dictionary<SimPart, Part> _inversePartMapping => _manager._inversePartMapping;
private Dictionary<SimPartModule, PartModule> _inversePartModuleMapping => _manager._inversePartModuleMapping;
private SimVessel _vessel => _manager._vessel;
private static readonly FieldInfo? _pfDecoupled;
private static readonly FieldInfo? _rfPredictedMaximumResiduals;
static SimVesselUpdater()
{
if (ReflectionUtils.IsAssemblyLoaded("ProceduralFairings"))
{
_pfDecoupled = ReflectionUtils.GetFieldByReflection("ProceduralFairings", "ProceduralFairings.ProceduralFairingDecoupler",
"decoupled");
if (_pfDecoupled == null)
Debug.Log("MechJeb BUG: ProceduralFairings loaded, but ProceduralFairings.ProceduralFairingDecoupler has no decoupled field");
}
if (ReflectionUtils.IsAssemblyLoaded("RealFuels"))
{
_rfPredictedMaximumResiduals =
ReflectionUtils.GetFieldByReflection("RealFuels", "RealFuels.ModuleEnginesRF", "predictedMaximumResiduals");
if (_rfPredictedMaximumResiduals == null)
Debug.Log(
"MechJeb BUG: RealFuels loaded, but RealFuels.ModuleEnginesRF has no predictedMaximumResiduals field, disabling residuals");
}
}
public SimVesselUpdater(SimVesselManager manager)
{
_manager = manager;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal void Update() => UpdateParts();
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void UpdateParts()
{
_vessel.CurrentStage = StageManager.CurrentStage;
// FIXME: could track only parts that matter to the sim (engines+tanks) and only loop over them here
foreach (SimPart part in _vessel.Parts)
{
UpdatePart(part);
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void UpdatePart(SimPart part)
{
Part kspPart = _inversePartMapping[part];
part.EngineResiduals = 0;
UpdateResources(part, kspPart);
UpdateModules(part);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static void UpdateResources(SimPart part, Part kspPart)
{
part.Resources.Clear();
part.DisabledResourcesMass = 0;
for (int i = 0; i < kspPart.Resources.Count; i++)
{
PartResource kspResource = kspPart.Resources[i];
if (!kspResource.flowState)
{
// disabled resources are dead weight and cannot be enabled by staging
part.DisabledResourcesMass += kspResource.amount * kspResource.info.density;
continue;
}
var resource = new SimResource
{
Amount = kspResource.amount,
MaxAmount = kspResource.maxAmount,
Id = kspResource.info.id,
Free = kspResource.info.density == 0,
Density = kspResource.info.density,
Residual = 0
};
part.Resources[resource.Id] = resource;
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void UpdateModules(SimPart part)
{
foreach (SimPartModule module in part.Modules)
{
UpdateModule(part, module);
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void UpdateModule(SimPart part, SimPartModule m)
{
PartModule kspModule = _inversePartModuleMapping[m];
m.ModuleIsEnabled = kspModule.moduleIsEnabled;
m.StagingEnabled = kspModule.stagingEnabled;
switch (m)
{
case SimModuleEngines engine:
UpdateModuleEngines(part, engine, kspModule as ModuleEngines);
break;
case SimLaunchClamp _:
// intentionally left blank
break;
case SimModuleDecouple decoupler:
UpdateModuleDecouplerBase(decoupler, kspModule as ModuleDecouplerBase);
break;
case SimModuleDockingNode decoupler:
UpdateDockingNode(decoupler, kspModule as ModuleDockingNode);
break;
case SimModuleRCS rcs:
UpdateModuleRCS(rcs, kspModule as ModuleRCS);
break;
case SimProceduralFairingDecoupler decoupler:
UpdateProceduralFairingDecoupler(decoupler, kspModule);
break;
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static void UpdateModuleEngines(SimPart part, SimModuleEngines engine, ModuleEngines? kspEngine)
{
if (kspEngine is null)
{
Print("Internal FuelFlowSimluation Error: SimModuleEngines mapped to wrong type");
return;
}
engine.IsEnabled = kspEngine.isEnabled;
engine.IsOperational = kspEngine.isOperational;
engine.ThrustPercentage = kspEngine.thrustPercentage;
engine.MultIsp = kspEngine.multIsp;
engine.NoPropellants = kspEngine is { flameout: true, statusL2: "No propellants" };
engine.ModuleResiduals = 0;
if (engine.isModuleEnginesRF && _rfPredictedMaximumResiduals!.GetValue(kspEngine) is double doubleVal)
engine.ModuleResiduals = doubleVal;
part.EngineResiduals = Max(part.EngineResiduals, engine.ModuleResiduals);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void UpdateDockingNode(SimModuleDockingNode decoupler, ModuleDockingNode? kspModuleDockingNode)
{
if (kspModuleDockingNode is null)
{
Print("Internal FuelFlowSimluation Error: SimModuleDockingNode mapped to wrong type");
return;
}
decoupler.Staged = kspModuleDockingNode.staged;
if (!(kspModuleDockingNode.referenceNode.attachedPart is null))
decoupler.AttachedPart = _partMapping[kspModuleDockingNode.referenceNode.attachedPart];
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void UpdateModuleDecouplerBase(SimModuleDecouple decoupler, ModuleDecouplerBase? kspModuleDecouple)
{
if (kspModuleDecouple is null)
{
Print("Internal FuelFlowSimluation Error: SimModuleDecouple mapped to wrong type");
return;
}
decoupler.IsDecoupled = kspModuleDecouple.isDecoupled;
decoupler.Staged = kspModuleDecouple.staged;
Part kspPart = kspModuleDecouple.part;
AttachNode attach;
if (HighLogic.LoadedSceneIsEditor)
{
attach = kspModuleDecouple.explosiveNodeID != "srf"
? kspPart.FindAttachNode(kspModuleDecouple.explosiveNodeID)
: kspPart.srfAttachNode;
}
else
{
attach = kspModuleDecouple.ExplosiveNode;
}
if (!(attach.attachedPart is null))
decoupler.AttachedPart = _partMapping[attach.attachedPart];
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static void UpdateModuleRCS(SimModuleRCS rcs, ModuleRCS? kspModuleRCS)
{
if (kspModuleRCS is null)
{
Print("Internal FuelFlowSimluation Error: SimModuleRCS mapped to wrong type");
return;
}
rcs.IsEnabled = kspModuleRCS.isEnabled;
rcs.Isp = kspModuleRCS.atmosphereCurve.Evaluate(0) * kspModuleRCS.ispMult;
rcs.Thrust = kspModuleRCS.flowMult * kspModuleRCS.maxFuelFlow * rcs.Isp * rcs.G;
rcs.RcsEnabled = kspModuleRCS.rcsEnabled;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static void UpdateProceduralFairingDecoupler(SimProceduralFairingDecoupler decoupler, PartModule kspPartModule)
{
if (_pfDecoupled == null) return;
if (_pfDecoupled.GetValue(kspPartModule) is bool boolVal)
decoupler.IsDecoupled = boolVal;
}
}
}
}