-
Notifications
You must be signed in to change notification settings - Fork 32
/
Misc.cs
219 lines (189 loc) · 6.93 KB
/
Misc.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
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using UnityEngine;
namespace Kethane
{
internal static class Misc
{
/// <summary>
/// Get true altitude above terrain (from MuMech lib)
/// Also from: http://kerbalspaceprogram.com/forum/index.php?topic=10324.msg161923#msg161923
/// </summary>
public static double GetTrueAltitude(Vessel vessel)
{
Vector3 CoM = vessel.findWorldCenterOfMass();
Vector3 up = (CoM - vessel.mainBody.position).normalized;
double altitudeASL = vessel.mainBody.GetAltitude(CoM);
double altitudeTrue = 0.0;
RaycastHit sfc;
if (Physics.Raycast(CoM, -up, out sfc, (float)altitudeASL + 10000.0F, 1 << 15))
altitudeTrue = sfc.distance;
else if (vessel.mainBody.pqsController != null)
altitudeTrue = vessel.mainBody.GetAltitude(CoM) - (vessel.mainBody.pqsController.GetSurfaceHeight(QuaternionD.AngleAxis(vessel.mainBody.GetLongitude(CoM), Vector3d.down) * QuaternionD.AngleAxis(vessel.mainBody.GetLatitude(CoM), Vector3d.forward) * Vector3d.right) - vessel.mainBody.pqsController.radius);
else
altitudeTrue = vessel.mainBody.GetAltitude(CoM);
return altitudeTrue;
}
public static double NormalizeAngle(double a)
{
a = a % 360;
if (a < 0)
a += 360;
return a;
}
public static Vector2 CartesianToPolar(Vector3 point)
{
Vector2 polar = new Vector2();
polar.y = Mathf.Atan2(point.x, point.z);
float xzLen = new Vector2(point.x, point.z).magnitude;
polar.x = Mathf.Atan2(-point.y, xzLen);
polar *= Mathf.Rad2Deg;
return polar;
}
public static List<PartResource> GetConnectedResources(Part part, String resourceName)
{
var resourceDef = PartResourceLibrary.Instance.GetDefinition(resourceName);
var resources = new List<PartResource>();
part.GetConnectedResources(resourceDef.id, resourceDef.resourceFlowMode, resources);
return resources;
}
public static float Dot(this Vector3 lhs, Vector3 rhs) { return Vector3.Dot(lhs, rhs); }
public static AnimationState[] SetUpAnimation(string animationName, Part part)
{
var states = new List<AnimationState>();
foreach (var animation in part.FindModelAnimators(animationName))
{
var animationState = animation[animationName];
animationState.speed = 0;
animationState.enabled = true;
animationState.wrapMode = WrapMode.ClampForever;
animation.Blend(animationName);
states.Add(animationState);
}
return states.ToArray();
}
public static float Range(this System.Random random, float min, float max)
{
return (float)random.Range((double)min, max);
}
public static double Range(this System.Random random, double min, double max)
{
return random.NextDouble() * (max - min) + min;
}
public static void Add<T>(this List<T> list, params T[] values)
{
list.AddRange(values);
}
#region Parsing utility methods
public static float Parse(string s, float defaultValue)
{
float value;
if (!float.TryParse(s, out value))
{
value = defaultValue;
}
return value;
}
public static double Parse(string s, double defaultValue)
{
double value;
if (!double.TryParse(s, out value))
{
value = defaultValue;
}
return value;
}
public static int Parse(string s, int defaultValue)
{
int value;
if (!int.TryParse(s, out value))
{
value = defaultValue;
}
return value;
}
public static bool Parse(string s, bool defaultValue)
{
bool value;
if (!bool.TryParse(s, out value))
{
value = defaultValue;
}
return value;
}
public static Vector3 Parse(string s, Vector3 defaultValue)
{
try
{
return ConfigNode.ParseVector3(s);
}
catch
{
return defaultValue;
}
}
public static Color32 Parse(string s, Color32 defaultValue)
{
if (s == null) { return defaultValue; }
return ConfigNode.ParseColor32(s);
}
public static ParticleRenderMode Parse(string s, ParticleRenderMode defaultValue)
{
try
{
return (ParticleRenderMode)Enum.Parse(typeof(ParticleRenderMode), s);
}
catch
{
return defaultValue;
}
}
#endregion
#region BitArray extensions
public static IEnumerable<bool> GetTypedEnumerator(this System.Collections.BitArray bits)
{
for (int i = 0; i < bits.Length; i++)
{
yield return bits[i];
}
}
public static byte[] ToByteArray(this System.Collections.BitArray bits)
{
byte[] ret = new byte[(bits.Length - 1) / 8 + 1];
bits.CopyTo(ret, 0);
return ret;
}
#endregion
public static string GetInformationalVersion(this System.Reflection.Assembly assembly)
{
return System.Diagnostics.FileVersionInfo.GetVersionInfo(assembly.Location).ProductVersion;
}
#region Stream extensions
public static byte[] ReadFully(this Stream input)
{
byte[] buffer = new byte[16 * 1024];
using (MemoryStream ms = new MemoryStream())
{
int read;
while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
{
ms.Write(buffer, 0, read);
}
return ms.ToArray();
}
}
#endregion
#region Encoding
public static byte[] FromBase64String(string encoded)
{
return Convert.FromBase64String(encoded.Replace('.', '/').Replace('%', '='));
}
public static string ToBase64String(byte[] data)
{
return Convert.ToBase64String(data).Replace('/', '.').Replace('=', '%');
}
#endregion
}
}