-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathVesselUtils.cs
More file actions
411 lines (343 loc) · 11.1 KB
/
VesselUtils.cs
File metadata and controls
411 lines (343 loc) · 11.1 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
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
namespace kOS
{
public class VesselUtils
{
public static List<Part> GetListOfActivatedEngines(Vessel vessel)
{
var retList = new List<Part>();
foreach (Part part in vessel.Parts)
{
foreach (PartModule module in part.Modules)
{
if (module is ModuleEngines)
{
var engineMod = (ModuleEngines)module;
if (engineMod.getIgnitionState)
{
retList.Add(part);
}
}
}
}
return retList;
}
public static bool TryGetResource(Vessel vessel, string resourceName, out double total)
{
bool resourceIsFound = false;
total = 0;
PartResourceDefinition resourceDefinition = PartResourceLibrary.Instance.resourceDefinitions.FirstOrDefault(rd => rd.name.Equals(resourceName, StringComparison.OrdinalIgnoreCase));
// Ensure the built-in resource types never produce an error, even if the particular vessel is incapable of carrying them
if (resourceDefinition == null)
return resourceIsFound;
resourceName = resourceName.ToUpper();
foreach (Part part in vessel.parts)
{
foreach (PartResource resource in part.Resources)
{
if (resource.resourceName.ToUpper() == resourceName)
{
resourceIsFound = true;
total += resource.amount;
}
}
}
return resourceIsFound;
}
public static double GetResource(Vessel vessel, string resourceName)
{
double total = 0;
resourceName = resourceName.ToUpper();
foreach (Part part in vessel.parts)
{
foreach (PartResource resource in part.Resources)
{
if (resource.resourceName.ToUpper() == resourceName)
{
total += resource.amount;
}
}
}
return total;
}
public static double GetMaxThrust(Vessel vessel)
{
var thrust = 0.0;
ModuleEngines e;
foreach (Part p in vessel.parts)
{
foreach (PartModule pm in p.Modules)
{
if (!pm.isEnabled) continue;
if (pm is ModuleEngines)
{
e = (pm as ModuleEngines);
if (!e.EngineIgnited) continue;
thrust += (double)e.maxThrust;
}
}
}
return thrust;
}
public static Vessel TryGetVesselByName(String name, Vessel origin)
{
foreach (Vessel v in FlightGlobals.Vessels)
{
if (v != origin && v.vesselName.ToUpper() == name.ToUpper())
{
return v;
}
}
return null;
}
public static CelestialBody GetBodyByName(String name)
{
foreach (var body in FlightGlobals.fetch.bodies)
{
if (name.ToUpper() == body.name.ToUpper())
{
return body;
}
}
return null;
}
public static Vessel GetVesselByName(String name, Vessel origin)
{
Vessel vessel = TryGetVesselByName(name, origin);
if (vessel == null)
{
throw new kOSException("Vessel '" + name + "' not found");
}
else
{
return vessel;
}
}
public static void SetTarget(ITargetable val)
{
//if (val is Vessel)
//{
FlightGlobals.fetch.SetVesselTarget(val);
//}
//else if (val is CelestialBody)
//{/
// }
}
public static double GetCommRange(Vessel vessel)
{
double range = 100000;
foreach (Part part in vessel.parts)
{
if (part.partInfo.name == "longAntenna")
{
String status = ((ModuleAnimateGeneric)part.Modules["ModuleAnimateGeneric"]).status;
if (status == "Fixed" || status == "Locked")
{
range += 1000000;
}
}
}
foreach (Part part in vessel.parts)
{
if (part.partInfo.name == "mediumDishAntenna")
{
String status = ((ModuleAnimateGeneric)part.Modules["ModuleAnimateGeneric"]).status;
if (status == "Fixed" || status == "Locked")
{
range *= 100;
}
}
}
foreach (Part part in vessel.parts)
{
if (part.partInfo.name == "commDish")
{
String status = ((ModuleAnimateGeneric)part.Modules["ModuleAnimateGeneric"]).status;
if (status == "Fixed" || status == "Locked")
{
range *= 200;
}
}
}
return range;
}
public static double GetDistanceToKerbinSurface(Vessel vessel)
{
foreach (var body in FlightGlobals.fetch.bodies)
{
if (body.name.ToUpper() == "KERBIN") return Vector3d.Distance(body.position, vessel.GetWorldPos3D()) - 600000; // Kerbin radius = 600,000
}
throw new kOSException("Planet Kerbin not found!");
}
public static float AngleDelta(float a, float b)
{
var delta = b - a;
while (delta > 180) delta -= 360;
while (delta < -180) delta += 360;
return delta;
}
public static float GetHeading(Vessel vessel)
{
var up = vessel.upAxis;
var north = GetNorthVector(vessel);
var headingQ = Quaternion.Inverse(Quaternion.Euler(90, 0, 0) * Quaternion.Inverse(vessel.GetTransform().rotation) * Quaternion.LookRotation(north, up));
return headingQ.eulerAngles.y;
}
public static float GetVelocityHeading(Vessel vessel)
{
var up = vessel.upAxis;
var north = GetNorthVector(vessel);
var headingQ = Quaternion.Inverse(Quaternion.Inverse(Quaternion.LookRotation(vessel.srf_velocity, up)) * Quaternion.LookRotation(north, up));
return headingQ.eulerAngles.y;
}
public static float GetTargetBearing(Vessel vessel, Vessel target)
{
return AngleDelta(GetHeading(vessel), GetTargetHeading(vessel, target));
}
public static float GetTargetHeading(Vessel vessel, Vessel target)
{
var up = vessel.upAxis;
var north = GetNorthVector(vessel);
var vector = Vector3d.Exclude(vessel.upAxis, target.GetWorldPos3D() - vessel.GetWorldPos3D()).normalized;
var headingQ = Quaternion.Inverse(Quaternion.Euler(90, 0, 0) * Quaternion.Inverse(Quaternion.LookRotation(vector, up)) * Quaternion.LookRotation(north, up));
return headingQ.eulerAngles.y;
}
public static Vector3d GetNorthVector(Vessel vessel)
{
return Vector3d.Exclude(vessel.upAxis, vessel.mainBody.transform.up);
}
public static object TryGetEncounter(Vessel vessel)
{
foreach (Orbit patch in vessel.patchedConicSolver.flightPlan)
{
if (patch.patchStartTransition == Orbit.PatchTransitionType.ENCOUNTER)
{
return new OrbitInfo(patch);
}
}
return "None";
}
public static void LandingLegsCtrl(Vessel vessel, bool state)
{
// This appears to work on all legs in 0.22
vessel.rootPart.SendEvent(state ? "LowerLeg" : "RaiseLeg");
}
internal static object GetLandingLegStatus(Vessel vessel)
{
bool atLeastOneLeg = false; // No legs at all? Always return false
foreach (Part p in vessel.parts)
{
if (p.Modules.OfType<ModuleLandingLeg>().Count() > 0)
{
atLeastOneLeg = true;
foreach (ModuleLandingLeg l in p.FindModulesImplementing<ModuleLandingLeg>())
{
if (l.savedLegState != (int)(ModuleLandingLeg.LegStates.DEPLOYED))
{
// If just one leg is retracted, still moving, or broken return false.
return false;
}
}
}
}
return atLeastOneLeg;
}
public static object GetChuteStatus(Vessel vessel)
{
bool atLeastOneChute = false; // No chutes at all? Always return false
foreach (Part p in vessel.parts)
{
foreach (ModuleParachute c in p.FindModulesImplementing<ModuleParachute>())
{
atLeastOneChute = true;
if (c.deploymentState == ModuleParachute.deploymentStates.STOWED)
{
// If just one chute is not deployed return false
return false;
}
}
}
return atLeastOneChute;
}
public static void DeployParachutes(Vessel vessel, bool state)
{
if (vessel.mainBody.atmosphere && state)
{
foreach (Part p in vessel.parts)
{
if (p.Modules.OfType<ModuleParachute>().Count() > 0 && state)
{
foreach (ModuleParachute c in p.FindModulesImplementing<ModuleParachute>())
{
if (c.deploymentState == ModuleParachute.deploymentStates.STOWED) //&& c.deployAltitude * 3 > vessel.heightFromTerrain)
{
c.DeployAction(null);
}
}
}
}
}
}
public static object GetSolarPanelStatus(Vessel vessel)
{
bool atLeastOneSolarPanel = false; // No panels at all? Always return false
foreach (Part p in vessel.parts)
{
foreach (ModuleDeployableSolarPanel c in p.FindModulesImplementing<ModuleDeployableSolarPanel>())
{
atLeastOneSolarPanel = true;
if (c.panelState == ModuleDeployableSolarPanel.panelStates.RETRACTED)
{
// If just one panel is not deployed return false
return false;
}
}
}
return atLeastOneSolarPanel;
}
public static void SolarPanelCtrl(Vessel vessel, bool state)
{
vessel.rootPart.SendEvent(state ? "Extend" : "Retract");
}
public static double GetMassDrag(Vessel vessel)
{
double massDrag = 0;
foreach (Part p in vessel.parts)
{
massDrag += (p.mass + p.GetResourceMass()) * p.maximum_drag;
}
return massDrag;
}
/*public static double RealMaxAtmosphereAltitude(CelestialBody body)
{
// This comes from MechJeb CelestialBodyExtensions.cs
if (!body.atmosphere) return 0;
//Atmosphere actually cuts out when exp(-altitude / scale height) = 1e-6
return -body.atmosphereScaleHeight * 1000 * Math.Log(1e-6);
}
public static double GetTerminalVelocity(Vessel vessel)
{
if(vessel.mainBody.GetAltitude(vessel.findWorldCenterOfMass()) > RealMaxAtmosphereAltitude(vessel.mainBody)) return double.PositiveInfinity;
double DensityOfAir = FlightGlobals.getAtmDensity(FlightGlobals.getStaticPressure(vessel.findWorldCenterOfMass(), vessel.mainBody));
return Math.Sqrt(2 * (double)FlightGlobals.getGeeForceAtPosition(vessel.findWorldCenterOfMass()).magnitude * vessel.GetTotalMass() / ( GetMassDrag(vessel) * FlightGlobals.DragMultiplier * DensityOfAir ));
}*/
public static float GetVesselLattitude(Vessel vessel)
{
float retVal = (float)vessel.latitude;
if (retVal > 90) return 90;
if (retVal < -90) return -90;
return retVal;
}
public static float GetVesselLongitude(Vessel vessel)
{
float retVal = (float)vessel.longitude;
while (retVal > 180) retVal -= 360;
while (retVal < -180) retVal += 360;
return retVal;
}
}
}