Tools: Change GetAtt to use the KSP function.
[VOID.git] / Tools.cs
blob:a/Tools.cs -> blob:b/Tools.cs
--- a/Tools.cs
+++ b/Tools.cs
@@ -149,83 +149,60 @@
 			return string.Format("{0}° {1}", Math.Abs(v_lat).ToString(format), dir_lat);
 		}
 
-		///////////////////////////////////////////////////////////////////////////////
-
-		//For MuMech_get_heading()
-		public class MuMech_MovingAverage
-		{
-			private double[] store;
-			private int storeSize;
-			private int nextIndex = 0;
-
-			public double value
-			{
-				get
-				{
-					double tmp = 0;
-					foreach (double i in store)
-					{
-						tmp += i;
-					}
-					return tmp / storeSize;
-				}
-				set
-				{
-					store[nextIndex] = value;
-					nextIndex = (nextIndex + 1) % storeSize;
-				}
-			}
-
-			public MuMech_MovingAverage(int size = 10, double startingValue = 0)
-			{
-				storeSize = size;
-				store = new double[size];
-				force(startingValue);
-			}
-
-			public void force(double newValue)
-			{
-				for (int i = 0; i < storeSize; i++)
-				{
-					store[i] = newValue;
-				}
-			}
-
-			public static implicit operator double(MuMech_MovingAverage v)
-			{
-				return v.value;
-			}
-
-			public override string ToString()
-			{
-				return value.ToString();
-			}
-
-			public string ToString(string format)
-			{
-				return value.ToString(format);
-			}
-		}
-		//From http://svn.mumech.com/KSP/trunk/MuMechLib/VOID.vesselState.cs
-		public static double MuMech_get_heading(Vessel vessel)
-		{
-			Vector3d CoM = vessel.findWorldCenterOfMass();
-			Vector3d up = (CoM - vessel.mainBody.position).normalized;
-			Vector3d north = Vector3d.Exclude(
-				                 up,
-				                 (vessel.mainBody.position +
-				                 vessel.mainBody.transform.up * (float)vessel.mainBody.Radius
-				                 ) - CoM).normalized;
-
-			Quaternion rotationSurface = Quaternion.LookRotation(north, up);
-			Quaternion rotationvesselSurface = Quaternion.Inverse(
-				                                   Quaternion.Euler(90, 0, 0) *
-				                                   Quaternion.Inverse(vessel.transform.rotation) *
-				                                   rotationSurface);
-
-			return rotationvesselSurface.eulerAngles.y;
-		}
-		//From http://svn.mumech.com/KSP/trunk/MuMechLib/MuUtils.cs
+		/*
+		* MuMechLib Methods
+		* The methods below are adapted from MuMechLib, © 2013-2014 r4m0n
+		* The following methods are a derivative work of the code from MuMechLib in the MechJeb project.
+		* Used under license.
+		* */
+
+		// Derived from MechJeb2/VesselState.cs
+		public static Quaternion getSurfaceRotation(this Vessel vessel)
+		{
+			Vector3 CoM;
+
+			try
+			{
+				CoM = vessel.findWorldCenterOfMass();
+			}
+			catch
+			{
+				return new Quaternion();
+			}
+
+			Vector3 bodyPosition = vessel.mainBody.position;
+			Vector3 bodyUp = vessel.mainBody.transform.up;
+
+			Vector3 surfaceUp = (CoM - vessel.mainBody.position).normalized;
+			Vector3 surfaceNorth = Vector3.Exclude(
+				surfaceUp,
+				(bodyPosition + bodyUp * (float)vessel.mainBody.Radius) - CoM
+			).normalized;
+
+			Quaternion surfaceRotation = Quaternion.LookRotation(surfaceNorth, surfaceUp);
+
+			return Quaternion.Inverse(
+				Quaternion.Euler(90, 0, 0) * Quaternion.Inverse(vessel.GetTransform().rotation) * surfaceRotation
+			);
+		}
+
+		// Derived from MechJeb2/VesselState.cs
+		public static double getSurfaceHeading(this Vessel vessel)
+		{
+			return vessel.getSurfaceRotation().eulerAngles.y;
+		}
+
+		// Derived from MechJeb2/VesselState.cs
+		public static double getSurfacePitch(this Vessel vessel)
+		{
+			Quaternion vesselSurfaceRotation = vessel.getSurfaceRotation();
+
+			return (vesselSurfaceRotation.eulerAngles.x > 180f) ?
+				(360f - vesselSurfaceRotation.eulerAngles.x) :
+				-vesselSurfaceRotation.eulerAngles.x;
+		}
+
+		// Derived from MechJeb2/MuUtils.cs
 		public static string MuMech_ToSI(
 			double d, int digits = 3, int MinMagnitude = 0, int MaxMagnitude = int.MaxValue
 		)
@@ -315,6 +292,10 @@
 			}
 		}
 
+		/*
+		 * END MuMecLib METHODS
+		 * */
+
 		public static string ConvertInterval(double seconds)
 		{
 			string format_1 = "{0:D1}y {1:D1}d {2:D2}h {3:D2}m {4:D2}.{5:D1}s";
@@ -959,6 +940,83 @@
 			return icon;
 		}
 
+		public static ExperimentSituations GetExperimentSituation(this Vessel vessel)
+		{
+			Vessel.Situations situation = vessel.situation;
+
+			switch (situation)
+			{
+				case Vessel.Situations.PRELAUNCH:
+				case Vessel.Situations.LANDED:
+					return ExperimentSituations.SrfLanded;
+				case Vessel.Situations.SPLASHED:
+					return ExperimentSituations.SrfSplashed;
+				case Vessel.Situations.FLYING:
+					if (vessel.altitude < (double)vessel.mainBody.scienceValues.flyingAltitudeThreshold)
+					{
+						return ExperimentSituations.FlyingLow;
+					}
+					else
+					{
+						return ExperimentSituations.FlyingHigh;
+					}
+			}
+
+			if (vessel.altitude < (double)vessel.mainBody.scienceValues.spaceAltitudeThreshold)
+			{
+				return ExperimentSituations.InSpaceLow;
+			}
+			else
+			{
+				return ExperimentSituations.InSpaceHigh;
+			}
+		}
+
+		public static double Radius(this Vessel vessel)
+		{
+			double radius;
+
+			radius = vessel.altitude;
+
+			if (vessel.mainBody != null)
+			{
+				radius += vessel.mainBody.Radius;
+			}
+
+			return radius;
+		}
+
+		public static double TryGetLastMass(this Engineer.VesselSimulator.SimManager simManager)
+		{
+			if (simManager.Stages == null || simManager.Stages.Length <= Staging.lastStage)
+			{
+				return double.NaN;
+			}
+
+			return simManager.Stages[Staging.lastStage].totalMass;
+		}
+
+		public static string HumanString(this ExperimentSituations situation)
+		{
+			switch (situation)
+			{
+				case ExperimentSituations.FlyingHigh:
+					return "Upper Atmosphere";
+				case ExperimentSituations.FlyingLow:
+					return "Flying";
+				case ExperimentSituations.SrfLanded:
+					return "Surface";
+				case ExperimentSituations.InSpaceLow:
+					return "Near in Space";
+				case ExperimentSituations.InSpaceHigh:
+					return "High in Space";
+				case ExperimentSituations.SrfSplashed:
+					return "Splashed Down";
+				default:
+					return "Unknown";
+			}
+		}
+
 		private static ScreenMessage debugmsg = new ScreenMessage("", 2f, ScreenMessageStyle.UPPER_RIGHT);
 
 		[System.Diagnostics.Conditional("DEBUG")]