VOID_DataLogger: Add downrange distance to CSV output.
VOID_DataLogger: Add downrange distance to CSV output.

--- a/VOID_DataLogger.cs
+++ b/VOID_DataLogger.cs
@@ -229,6 +229,7 @@
 					"Temperature (°C);" +
 					"Gravity (m/s²);" +
 					"Atmosphere Density (g/m³);" +
+					"Downrange Distance  (m);" +
 					"\n"
 				);
 			}
@@ -289,6 +290,10 @@
 			line.Append((vessel.atmDensity * 1000).ToString("F3"));
 			line.Append(';');
 
+			// Downrange Distance
+			line.Append((VOID_Data.downrangeDistance.Value.ToString("G3")));
+			line.Append(';');
+
 			line.Append('\n');
 
 			csvList.Add(line.ToString());

--- a/VOID_HUD.cs
+++ b/VOID_HUD.cs
@@ -189,6 +189,19 @@
 					VOID_Data.vesselHeading.ValueUnitString(),
 					VOID_Data.vesselPitch.ToSIString(2)
 				);
+
+				if (
+					this.core.vessel.mainBody == this.core.Kerbin &&
+					(
+						this.core.vessel.situation == Vessel.Situations.FLYING ||
+						this.core.vessel.situation == Vessel.Situations.SUB_ORBITAL ||
+						this.core.vessel.situation == Vessel.Situations.LANDED ||
+						this.core.vessel.situation == Vessel.Situations.SPLASHED
+					)
+				)
+				{
+					rightHUD.AppendFormat("\nRange to KSC: {0}", VOID_Data.downrangeDistance.ValueUnitString(2));
+				}
 			}
 			else
 			{
@@ -226,7 +239,7 @@
 			this.leftHUDPos.value = GUI.Window(
 				this.core.windowID,
 				this.leftHUDPos,
-				this.leftHUDWindow,
+				VOID_Tools.GetWindowHandler(this.leftHUDWindow),
 				GUIContent.none,
 				GUIStyle.none
 			);
@@ -234,7 +247,7 @@
 			this.rightHUDPos.value = GUI.Window(
 				this.core.windowID,
 				this.rightHUDPos,
-				this.rightHUDWindow,
+				VOID_Tools.GetWindowHandler(this.rightHUDWindow),
 				GUIContent.none,
 				GUIStyle.none
 			);
@@ -271,6 +284,44 @@
 			() => core.vessel.getSurfacePitch(),
 			"°"
 		);
+
+		public static readonly VOID_DoubleValue downrangeDistance = new VOID_DoubleValue(
+			"Downrange Distance",
+			delegate() {
+
+			if (core.vessel == null ||
+				Planetarium.fetch == null ||
+				core.vessel.mainBody != Planetarium.fetch.Home
+			)
+			{
+				return double.NaN;
+			}
+
+			double vesselLongitude = core.vessel.longitude * Math.PI / 180d;
+			double vesselLatitude = core.vessel.latitude * Math.PI / 180d;
+
+			const double kscLongitude = 285.442323427289 * Math.PI / 180d;
+			const double kscLatitude = -0.0972112860655246 * Math.PI / 180d;
+
+			double diffLon = vesselLongitude - kscLongitude;
+			double diffLat = vesselLatitude - kscLatitude;
+
+			double sinHalfDiffLat = Math.Sin(diffLat / 2d);
+			double sinHalfDiffLon = Math.Sin(diffLon / 2d);
+
+			double cosVesselLon = Math.Cos(vesselLongitude);
+			double cosKSCLon = Math.Cos(kscLongitude);
+
+			double haversine =
+				sinHalfDiffLat * sinHalfDiffLat +
+				cosVesselLon * cosKSCLon * sinHalfDiffLon * sinHalfDiffLon;
+
+			double arc = 2d * Math.Atan2(Math.Sqrt(haversine), Math.Sqrt(1d - haversine));
+
+			return core.vessel.mainBody.Radius * arc;
+		},
+			"m"
+		);
 	}
 }
 

--- a/VOID_SurfAtmo.cs
+++ b/VOID_SurfAtmo.cs
@@ -65,6 +65,9 @@
 			this.precisionValues [idx]= (ushort)VOID_Data.terrainElevation.DoGUIHorizontal (this.precisionValues [idx]);
 			idx++;
 
+			this.precisionValues[idx] = (ushort)VOID_Data.downrangeDistance.DoGUIHorizontal(this.precisionValues[idx]);
+			idx++;
+
 			this.precisionValues [idx]= (ushort)VOID_Data.surfVelocity.DoGUIHorizontal (this.precisionValues [idx]);
 			idx++;
 
@@ -76,7 +79,8 @@
 
 			VOID_Data.temperature.DoGUIHorizontal ("F2");
 
-			VOID_Data.atmDensity.DoGUIHorizontal (3);
+			this.precisionValues [idx]= (ushort)VOID_Data.atmDensity.DoGUIHorizontal (this.precisionValues [idx]);
+			idx++;
 
 			VOID_Data.atmPressure.DoGUIHorizontal ("F2");
 

--- a/VOID_Tools.cs
+++ b/VOID_Tools.cs
@@ -341,11 +341,23 @@
 					#endif
 					{
 						Debug.LogWarning(
-							string.Format("[{0}]: ArgumentException caught during window call.", func.Target.GetType().Name)
-						);
+							string.Format("[{0}]: ArgumentException caught during window call.  This is not a bug.",
+								func.Target.GetType().Name
+							));
+
 						/*#if DEBUG
 						Debug.LogException(ex);
 						#endif*/
+					}
+					catch (Exception ex)
+					{
+						Debug.LogError(
+							string.Format("[{0}]: {1} caught during window call.\nMessage:\n{2}\nStackTrace:\n{3}",
+								func.Target.GetType().Name,
+								ex.GetType().Name,
+								ex.Message,
+								ex.StackTrace
+							));
 					}
 				};
 			}