forked from Nivekk/KOS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVesselUtils.cs
More file actions
198 lines (164 loc) · 5.93 KB
/
VesselUtils.cs
File metadata and controls
198 lines (164 loc) · 5.93 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
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 float GetResource(Vessel vessel, string resourceName)
{
float total = 0;
resourceName = resourceName.ToUpper();
foreach (Part part in vessel.parts)
{
foreach (PartResource resource in part.Resources)
{
if (resource.resourceName.ToUpper() == resourceName)
{
total += (float)resource.amount;
}
}
}
return total;
}
public static float 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 += e.maxThrust;
}
}
}
return (float)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 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)
{
FlightGlobals.fetch.SetVesselTarget(val);
}
public static float GetCommRange(Vessel vessel)
{
float 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 == "commDish")
{
String status = ((ModuleAnimateGeneric)part.Modules["ModuleAnimateGeneric"]).status;
if (status == "Fixed" || status == "Locked")
{
range *= 200;
}
}
}
return range;
}
public static float GetDistanceToKerbinSurface(Vessel vessel)
{
foreach (var body in FlightGlobals.fetch.bodies)
{
if (body.name.ToUpper() == "KERBIN") return (float)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);
}
}
}