VOID_Tools: New GetWindowHandler method to wrap window functions in some exception handling code, to avoid baking it in to every window.
[VOID.git] / VOID_Tools.cs
blob:a/VOID_Tools.cs -> blob:b/VOID_Tools.cs
--- a/VOID_Tools.cs
+++ b/VOID_Tools.cs
@@ -29,12 +29,71 @@
 using KSP;
 using System;
 using System.Collections.Generic;
+using ToadicusTools;
 using UnityEngine;
 
 namespace VOID
 {
 	public static partial class VOID_Tools
 	{
+		#region CelestialBody Utilities
+		public static bool hasAncestor(this CelestialBody bodyA, CelestialBody bodyB)
+		{
+			if (bodyA == null || bodyB == null)
+			{
+				return false;
+			}
+
+			while (bodyA.orbitDriver != null)
+			{
+				if (bodyA.orbit.referenceBody == bodyB)
+				{
+					return true;
+				}
+
+				bodyA = bodyA.orbit.referenceBody;
+			} 
+
+			return false;
+		}
+
+		public static bool NearestRelatedParents(ref CelestialBody bodyA, ref CelestialBody bodyB)
+		{
+			if (bodyA == null || bodyB == null || bodyA.orbitDriver == null || bodyB.orbitDriver == null)
+			{
+				throw new ArgumentException(string.Concat(
+					"CelestialBody::FindRelatedParents: ",
+					"Neither body may be null, and both bodies must have orbits."
+				));
+			}
+
+			CelestialBody a, b;
+
+			a = bodyA;
+
+			while (bodyA.orbitDriver != null)
+			{
+				b = bodyB;
+
+				while (b.orbitDriver != null)
+				{
+					if (a.orbit.referenceBody == b.orbit.referenceBody)
+					{
+						bodyA = a;
+						bodyB = b;
+						return true;
+					}
+
+					b = b.orbit.referenceBody;
+				}
+
+				a = a.orbit.referenceBody;
+			}
+
+			return false;
+		}
+		#endregion
+
 		#region VESSEL_EXTENSIONS_SCIENCE
 		public static CBAttributeMap.MapAttribute GetBiome(this Vessel vessel)
 		{
@@ -275,14 +334,18 @@
 					{
 						func(id);
 					}
+					#if DEBUG
 					catch (ArgumentException ex)
+					#else
+					catch (ArgumentException)
+					#endif
 					{
 						Debug.LogWarning(
 							string.Format("[{0}]: ArgumentException caught during window call.", func.Target.GetType().Name)
 						);
-						#if DEBUG
+						/*#if DEBUG
 						Debug.LogException(ex);
-						#endif
+						#endif*/
 					}
 				};
 			}
@@ -805,6 +868,83 @@
 				return "";
 		}
 	}
+
+	public class CBListComparer : IComparer<CelestialBody>
+	{
+		public int Compare(CelestialBody bodyA, CelestialBody bodyB)
+		{
+			Tools.PostDebugMessage(this, "got bodyA: {0} & bodyB: {1}", bodyA, bodyB);
+
+			if (bodyA == null && bodyB == null)
+			{
+				Tools.PostDebugMessage(this, "both bodies are null, returning 0");
+				return 0;
+			}
+			if (bodyA == null)
+			{
+				Tools.PostDebugMessage(this, "bodyA is null, returning -1");
+				return -1;
+			}
+			if (bodyB == null)
+			{
+				Tools.PostDebugMessage(this, "bodyB is null, returning 1");
+				return 1;
+			}
+
+			Tools.PostDebugMessage(this, "bodies are not null, carrying on");
+
+			if (object.ReferenceEquals(bodyA, bodyB))
+			{
+				Tools.PostDebugMessage(this, "bodies are equal, returning 0");
+				return 0;
+			}
+
+			Tools.PostDebugMessage(this, "bodies are not equal, carrying on");
+
+			if (bodyA.orbitDriver == null)
+			{
+				Tools.PostDebugMessage(this, "bodyA.orbit is null (bodyA is the sun, returning 1");
+				return 1;
+			}
+			if (bodyB.orbitDriver == null)
+			{
+				Tools.PostDebugMessage(this, "bodyB.orbit is null (bodyB is the sun, returning -1");
+				return -1;
+			}
+
+			Tools.PostDebugMessage(this, "orbits are not null, carrying on");
+
+			if (bodyA.orbit.referenceBody == bodyB.orbit.referenceBody)
+			{
+				Tools.PostDebugMessage(this, "bodies share a parent, comparing SMAs");
+				return -bodyA.orbit.semiMajorAxis.CompareTo(bodyB.orbit.semiMajorAxis);
+			}
+
+			Tools.PostDebugMessage(this, "orbits do not share a parent, carrying on");
+
+			if (bodyA.hasAncestor(bodyB))
+			{
+				Tools.PostDebugMessage(this, "bodyA is a moon or sub-moon of bodyB, returning -1");
+				return -1;
+			}
+			if (bodyB.hasAncestor(bodyA))
+			{
+				Tools.PostDebugMessage(this, "bodyA is a moon or sub-moon of bodyB, returning 1");
+				return 1;
+			}
+
+			Tools.PostDebugMessage(this, "bodies do not have an obvious relationship, searching for one");
+
+			if (VOID_Tools.NearestRelatedParents(ref bodyA, ref bodyB))
+			{
+				Tools.PostDebugMessage(this, "good relation {0} and {1}, comparing", bodyA.bodyName, bodyB.bodyName);
+				return this.Compare(bodyA, bodyB);
+			}
+
+			Tools.PostDebugMessage(this, "bad relation {0} and {1}, giving up", bodyA.bodyName, bodyB.bodyName);
+
+			return 0;
+		}
+	}
 }
 
-