forked from asdfCYBER/Ferram-Aerospace-Research
/
FARAPI.cs
518 lines (447 loc) · 18.1 KB
/
FARAPI.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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
/*
Ferram Aerospace Research v0.15.10.1 "Lundgren"
=========================
Aerodynamics model for Kerbal Space Program
Copyright 2019, Michael Ferrara, aka Ferram4
This file is part of Ferram Aerospace Research.
Ferram Aerospace Research is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Ferram Aerospace Research is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Ferram Aerospace Research. If not, see <http://www.gnu.org/licenses/>.
Serious thanks: a.g., for tons of bugfixes and code-refactorings
stupid_chris, for the RealChuteLite implementation
Taverius, for correcting a ton of incorrect values
Tetryds, for finding lots of bugs and issues and not letting me get away with them, and work on example crafts
sarbian, for refactoring code for working with MechJeb, and the Module Manager updates
ialdabaoth (who is awesome), who originally created Module Manager
Regex, for adding RPM support
DaMichel, for some ferramGraph updates and some control surface-related features
Duxwing, for copy editing the readme
CompatibilityChecker by Majiir, BSD 2-clause http://opensource.org/licenses/BSD-2-Clause
Part.cfg changes powered by sarbian & ialdabaoth's ModuleManager plugin; used with permission
http://forum.kerbalspaceprogram.com/threads/55219
ModularFLightIntegrator by Sarbian, Starwaster and Ferram4, MIT: http://opensource.org/licenses/MIT
http://forum.kerbalspaceprogram.com/threads/118088
Toolbar integration powered by blizzy78's Toolbar plugin; used with permission
http://forum.kerbalspaceprogram.com/threads/60863
*/
using System;
using UnityEngine;
using FerramAerospaceResearch.FARGUI.FARFlightGUI;
using FerramAerospaceResearch.FARAeroComponents;
using FerramAerospaceResearch.FARUtils;
namespace FerramAerospaceResearch
{
public partial class FARAPI
{
private static FARAPI instance;
private static FARAPI Instance
{
get
{
if (instance == null)
instance = new FARAPI();
return instance;
}
}
private static FARVersion version;
public static FARVersion Version
{
get
{
if (version == null)
version = new FARVersion();
return version;
}
}
#region CurrentFlightInfo
private FlightGUI GetFlightInfo(Vessel v)
{
FlightGUI gui = null;
FlightGUI.vesselFlightGUI.TryGetValue(v, out gui);
return gui;
}
/// <summary>
/// Toggle or enable/disable FAR speed display.
/// </summary>
/// <param name="enabled">Enable/disable the speed display, null value toggles the speed display</param>
/// <param name="v">Vessel to toggle or enable/disable speed display for, null to apply <paramref name="enabled"/> globally</param>
/// <returns>Success/failure of toggling or enabling/disabling the speed display</returns>
public static bool ToggleAirspeedDisplay(bool? enabled = null, Vessel v = null)
{
if (v == null)
{
if (enabled == null)
{
AirspeedSettingsGUI.allEnabled = !AirspeedSettingsGUI.allEnabled;
}
else
{
AirspeedSettingsGUI.allEnabled = (bool) enabled;
}
return true;
}
FlightGUI gui = VesselFlightInfo(v);
if (gui != null)
{
AirspeedSettingsGUI airspeedSettingsGUI = gui.airSpeedGUI;
if (airspeedSettingsGUI != null)
{
if (enabled == null)
{
airspeedSettingsGUI.enabled = !airspeedSettingsGUI.enabled;
}
else
{
airspeedSettingsGUI.enabled = (bool) enabled;
}
return true;
}
}
return false;
}
public static FlightGUI VesselFlightInfo(Vessel v)
{
return Instance.GetFlightInfo(v);
}
public static double ActiveVesselIAS()
{
return VesselIAS(FlightGlobals.ActiveVessel);
}
public static double VesselIAS(Vessel vessel)
{
return VesselFlightInfo(vessel)?.airSpeedGUI?.CalculateIAS() ?? 0;
}
public static double ActiveVesselEAS()
{
return VesselEAS(FlightGlobals.ActiveVessel);
}
public static double VesselEAS(Vessel vessel)
{
return VesselFlightInfo(vessel)?.airSpeedGUI?.CalculateEAS() ?? 0;
}
public static double ActiveVesselDynPres()
{
return VesselDynPres(FlightGlobals.ActiveVessel);
}
public static double VesselDynPres(Vessel v)
{
FlightGUI gui = VesselFlightInfo(v);
if(gui == null)
return 0;
else
return gui.InfoParameters.dynPres;
}
public static double ActiveVesselLiftCoeff()
{
return VesselLiftCoeff(FlightGlobals.ActiveVessel);
}
public static double VesselLiftCoeff(Vessel v)
{
FlightGUI gui = VesselFlightInfo(v);
if(gui == null)
return 0;
else
return gui.InfoParameters.liftCoeff;
}
public static double ActiveVesselDragCoeff()
{
return VesselDragCoeff(FlightGlobals.ActiveVessel);
}
public static double VesselDragCoeff(Vessel v)
{
FlightGUI gui = VesselFlightInfo(v);
if(gui == null)
return 0;
else
return gui.InfoParameters.dragCoeff;
}
public static double ActiveVesselRefArea()
{
return VesselRefArea(FlightGlobals.ActiveVessel);
}
public static double VesselRefArea(Vessel v)
{
FlightGUI gui = VesselFlightInfo(v);
if(gui == null)
return 0;
else
return gui.InfoParameters.refArea;
}
public static double ActiveVesselTermVelEst()
{
return VesselTermVelEst(FlightGlobals.ActiveVessel);
}
public static double VesselTermVelEst(Vessel v)
{
FlightGUI gui = VesselFlightInfo(v);
if(gui == null)
return 0;
else
return gui.InfoParameters.termVelEst;
}
public static double ActiveVesselBallisticCoeff()
{
return VesselBallisticCoeff(FlightGlobals.ActiveVessel);
}
public static double VesselBallisticCoeff(Vessel v)
{
FlightGUI gui = VesselFlightInfo(v);
if(gui == null)
return 0;
else
return gui.InfoParameters.ballisticCoeff;
}
public static double ActiveVesselAoA()
{
return VesselAoA(FlightGlobals.ActiveVessel);
}
public static double VesselAoA(Vessel v)
{
FlightGUI gui = VesselFlightInfo(v);
if(gui == null)
return 0;
else
return gui.InfoParameters.aoA;
}
public static double ActiveVesselSideslip()
{
return VesselSideslip(FlightGlobals.ActiveVessel);
}
public static double VesselSideslip(Vessel v)
{
FlightGUI gui = VesselFlightInfo(v);
if(gui == null)
return 0;
else
return gui.InfoParameters.sideslipAngle;
}
public static double ActiveVesselTSFC()
{
return VesselTSFC(FlightGlobals.ActiveVessel);
}
public static double VesselTSFC(Vessel v)
{
FlightGUI gui = VesselFlightInfo(v);
if(gui == null)
return 0;
else
return gui.InfoParameters.tSFC;
}
public static double ActiveVesselStallFrac()
{
return VesselStallFrac(FlightGlobals.ActiveVessel);
}
public static double VesselStallFrac(Vessel v)
{
FlightGUI gui = VesselFlightInfo(v);
if(gui == null)
return 0;
else
return gui.InfoParameters.stallFraction;
}
/// <summary>
/// Increases flap deflection level for all control surfaces on this vessel, up to max setting of 3
/// </summary>
public static void VesselIncreaseFlapDeflection(Vessel v)
{
for(int i = 0; i < v.parts.Count; i++)
{
Part p = v.parts[i];
if(p.Modules.Contains<ferram4.FARControllableSurface>())
{
ferram4.FARControllableSurface surface = p.Modules.GetModule<ferram4.FARControllableSurface>();
surface.SetDeflection(surface.flapDeflectionLevel + 1);
}
}
}
/// <summary>
/// Decreases flap deflection level for all control surfaces on this vessel, down to min setting of 0
/// </summary>
public static void VesselDecreaseFlapDeflection(Vessel v)
{
for(int i = 0; i < v.parts.Count; i++)
{
Part p = v.parts[i];
if (p.Modules.Contains<ferram4.FARControllableSurface>())
{
ferram4.FARControllableSurface surface = p.Modules.GetModule<ferram4.FARControllableSurface>();
surface.SetDeflection(surface.flapDeflectionLevel - 1);
}
}
}
/// <summary>
/// Returns flap setting for this vessel
/// </summary>
/// <param name="v"></param>
/// <returns>Flap setting; 0 - 3 indicates no to full flap deflections; -1 indicates lack of any control surface parts</returns>
public static int VesselFlapSetting(Vessel v)
{
for (int i = 0; i < v.parts.Count; i++)
{
Part p = v.parts[i];
if (p.Modules.Contains<ferram4.FARControllableSurface>())
{
ferram4.FARControllableSurface surface = p.Modules.GetModule<ferram4.FARControllableSurface>();
if (surface.isFlap)
return surface.flapDeflectionLevel;
}
}
return -1;
}
/// <summary>
/// Sets spoilers to a certain value on this vessel
/// </summary>
public static void VesselSetSpoilers(Vessel v, bool spoilerActive)
{
for(int i = 0; i < v.parts.Count; i++)
{
Part p = v.parts[i];
if (p.Modules.Contains<ferram4.FARControllableSurface>())
{
ferram4.FARControllableSurface surface = p.Modules.GetModule<ferram4.FARControllableSurface>();
surface.brake = spoilerActive;
}
}
}
/// <summary>
/// Returns spoiler setting for this vessel
/// </summary>
/// <param name="v"></param>
/// <returns>Spoiler setting; true indicates active spoilers, false indicates inactive or no spoilers in existence</returns>
public static bool VesselSpoilerSetting(Vessel v)
{
for (int i = 0; i < v.parts.Count; i++)
{
Part p = v.parts[i];
if (p.Modules.Contains<ferram4.FARControllableSurface>())
{
ferram4.FARControllableSurface surface = p.Modules.GetModule<ferram4.FARControllableSurface>();
if (surface.isSpoiler)
return surface.brake;
}
}
return false;
}
/// <summary>
/// Returns the current aerodynamic force being experienced by the vehicle in world space
/// </summary>
/// <param name="v">The vessel that force is being queried</param>
/// <returns>The force on the vessel in world space</returns>
public static Vector3 VesselAerodynamicForce(Vessel v)
{
return VesselFlightInfo(v)?.InfoParameters.aerodynamicForce ?? Vector3.zero;
}
/// <summary>
/// Returns the current aerodynamic torque being experienced by the vehicle in world space
/// </summary>
/// <param name="v">The vessel that force is being queried</param>
/// <returns>The torque on the vessel in world space</returns>
public static Vector3 VesselAerodynamicTorque(Vessel v)
{
return VesselFlightInfo(v)?.InfoParameters.aerodynamicTorque ?? Vector3.zero;
}
/// <summary>
/// Returns the current aerodynamic force being experienced by the active vehicle in world space
/// </summary>
/// <returns>The force on the vessel in world space</returns>
public static Vector3 ActiveVesselAerodynamicForce()
{
return VesselAerodynamicForce(FlightGlobals.ActiveVessel);
}
/// <summary>
/// Returns the current aerodynamic torque being experienced by the active vehicle in world space
/// </summary>
/// <returns>The torque on the vessel in world space</returns>
public static Vector3 ActiveVesselAerodynamicTorque()
{
return VesselAerodynamicTorque(FlightGlobals.ActiveVessel);
}
#endregion
#region AeroPredictions
private void InstanceCalcVesselAeroForces(Vessel vessel, out Vector3 aeroForce, out Vector3 aeroTorque, Vector3 velocityWorldVector, double altitude)
{
aeroForce = aeroTorque = Vector3.zero;
if (vessel == null)
{
FARLogger.Error("API Error: attempted to simulate aerodynamics of null vessel");
return;
}
FARVesselAero vesselAero = vessel.GetComponent<FARVesselAero>();
if (vesselAero == null)
{
FARLogger.Error("API Error: vessel does not have FARVesselAero aerocomponent for simulation");
return;
}
vesselAero.SimulateAeroProperties(out aeroForce, out aeroTorque, velocityWorldVector, altitude);
}
/// <summary>
/// Calculates the forces and torque on a vessel at a given condition at the CoM
/// </summary>
/// <param name="vessel">Vessel in question</param>
/// <param name="aeroForce">Total aerodynamic force at CoM, in kN</param>
/// <param name="aeroTorque">Total aerodynamic torque at CoM, in kN * m</param>
/// <param name="velocityWorldVector">Velocity vector in worldspace relative to the atmosphere for CURRENT vessel orientation, m/s</param>
/// <param name="density">Atm density at that location, kg/m^3</param>
/// <param name="machNumber">Mach number at that location</param>
public static void CalculateVesselAeroForces(Vessel vessel, out Vector3 aeroForce, out Vector3 aeroTorque, Vector3 velocityWorldVector, double altitude)
{
Instance.InstanceCalcVesselAeroForces(vessel, out aeroForce, out aeroTorque, velocityWorldVector, altitude);
}
#endregion
#region Misc State
/// <summary>
/// Method to determine if the given vessel has been successfully voxelized at any time after being loaded
/// </summary>
/// <param name="vessel"></param>
/// <returns>True if vessel has ever been successfully voxelized, returns false if not or if the vessel is null and/or destroyed</returns>
public static bool VesselVoxelizationCompletedEver(Vessel vessel)
{
if (vessel == null)
return false;
FARVesselAero vesselAeroModule = null;
for(int i =0; i < vessel.vesselModules.Count; ++i)
{
VesselModule vM = vessel.vesselModules[i];
if(vM is FARVesselAero)
{
vesselAeroModule = (FARVesselAero)vM;
break;
}
}
if ((object)vesselAeroModule == null) //if this is true, then the vessel has not been initialized yet and so must be false
return false;
return vesselAeroModule.HasEverValidVoxelization();
}
/// <summary>
/// Method to determine if the given vessel has been successfully voxelized nd currently has valid voxelization
/// </summary>
/// <param name="vessel"></param>
/// <returns>True if vessel has valid voxelization currently, returns false if not or if the vessel is null and/or destroyed</returns>
public static bool VesselVoxelizationCompletedAndValid(Vessel vessel)
{
if (vessel == null)
return false;
FARVesselAero vesselAeroModule = null;
for(int i =0; i < vessel.vesselModules.Count; ++i)
{
VesselModule vM = vessel.vesselModules[i];
if(vM is FARVesselAero)
{
vesselAeroModule = (FARVesselAero)vM;
break;
}
}
if ((object)vesselAeroModule == null) //if this is true, then the vessel has not been initialized yet and so must be false
return false;
return vesselAeroModule.HasValidVoxelizationCurrently();
}
#endregion
}
}