Made the pretty lines slightly prettier.
Made the pretty lines slightly prettier.

--- a/ARConfiguration.cs
+++ b/ARConfiguration.cs
@@ -37,6 +37,12 @@
 			private set;
 		}
 
+		public static bool PrettyLines
+		{
+			get;
+			private set;
+		}
+
 		private bool showConfigWindow;
 		private Rect configWindowPos;
 
@@ -80,6 +86,8 @@
 				this.LoadConfigValue("requireConnectionForControl", false);
 
 			ARConfiguration.FixedPowerCost = this.LoadConfigValue("fixedPowerCost", false);
+
+			ARConfiguration.PrettyLines = this.LoadConfigValue("drawPrettyLines", true);
 
 			GameEvents.onGameSceneLoadRequested.Add(this.onSceneChangeRequested);
 
@@ -184,6 +192,17 @@
 
 			GUILayout.EndHorizontal();
 
+			GUILayout.BeginHorizontal();
+
+			bool prettyLines = GUITools.Toggle(ARConfiguration.PrettyLines, "Draw Pretty Lines");
+			if (prettyLines != ARConfiguration.PrettyLines)
+			{
+				ARConfiguration.PrettyLines = prettyLines;
+				this.SaveConfigValue("drawPrettyLines", prettyLines);
+			}
+
+			GUILayout.EndHorizontal();
+
 			if (requireLineOfSight)
 			{
 				GUILayout.BeginHorizontal();

--- a/ARMapRenderer.cs
+++ b/ARMapRenderer.cs
@@ -38,7 +38,6 @@
 	{
 		#region Fields
 		private Dictionary<Guid, LineRenderer> vesselLineRenderers;
-		private Dictionary<Guid, bool> vesselFrameCache;
 		#endregion
 
 		#region Properties
@@ -59,7 +58,7 @@
 					LineRenderer lr = obj.AddComponent<LineRenderer>();
 
 					lr.SetColors(Color.green, Color.green);
-					lr.material = new Material(Shader.Find("Particles/Additive"));
+					lr.material = MapView.OrbitLinesMaterial;
 					lr.SetVertexCount(2);
 
 					this.vesselLineRenderers[idx] = lr;
@@ -73,13 +72,15 @@
 		#region MonoBehaviour Lifecycle
 		private void Awake()
 		{
-			this.vesselLineRenderers = new Dictionary<Guid, LineRenderer>();
-			this.vesselFrameCache = new Dictionary<Guid, bool>();
+			if (ARConfiguration.PrettyLines)
+			{
+				this.vesselLineRenderers = new Dictionary<Guid, LineRenderer>();
+			}
 		}
 
 		private void OnPreCull()
 		{
-			if (!HighLogic.LoadedSceneIsFlight || !MapView.MapIsEnabled)
+			if (!HighLogic.LoadedSceneIsFlight || !MapView.MapIsEnabled || !ARConfiguration.PrettyLines)
 			{
 				this.Cleanup();
 
@@ -99,8 +100,6 @@
 					MapView.MapCamera.camera.fieldOfView,
 					MapView.MapCamera.Distance
 				);
-
-				this.vesselFrameCache.Clear();
 
 				log.AppendLine("vesselFrameCache cleared.");
 
@@ -167,10 +166,16 @@
 					}
 				}
 			}
+			catch (Exception)
+			{
+				this.Cleanup();
+			}
+			#if DEBUG
 			finally
 			{
 				log.Print();
 			}
+			#endif
 		}
 
 		private void OnDestroy()
@@ -183,75 +188,71 @@
 
 		private void SetRelayVertices(IAntennaRelay relay)
 		{
-			do
-			{
-				if (this.vesselFrameCache.ContainsKey(relay.vessel.id))
-				{
-					break;
-				}
-
-				LineRenderer renderer = this[relay.vessel.id];
-
-				Vector3d start;
-				Vector3d end;
-
-				renderer.enabled = true;
-
-				if (!relay.CanTransmit())
-				{
-					renderer.SetColors(Color.red, Color.red);
+			if (relay == null)
+			{
+				return;
+			}
+
+			LineRenderer renderer = this[relay.vessel.id];
+
+			Vector3d start;
+			Vector3d end;
+
+			renderer.enabled = true;
+
+			if (!relay.CanTransmit())
+			{
+				renderer.SetColors(Color.red, Color.red);
+			}
+			else
+			{
+				if (relay.transmitDistance < relay.nominalTransmitDistance)
+				{
+					renderer.SetColors(Color.green, Color.green);
 				}
 				else
 				{
-					if (relay.transmitDistance < relay.nominalTransmitDistance)
-					{
-						renderer.SetColors(Color.green, Color.green);
-					}
-					else
-					{
-						renderer.SetColors(Color.yellow, Color.yellow);
-					}
-				}
-
-				start = ScaledSpace.LocalToScaledSpace(relay.vessel.GetWorldPos3D());
-
-				if (relay.KerbinDirect)
-				{
-					end = ScaledSpace.LocalToScaledSpace(AntennaRelay.Kerbin.position);
-				}
-				else
-				{
-					end = ScaledSpace.LocalToScaledSpace(relay.targetRelay.vessel.GetWorldPos3D());
-				}
-
-				float lineWidth;
-
-				if (MapView.Draw3DLines)
-				{
-					lineWidth = 0.004f * MapView.MapCamera.Distance;
-				}
-				else
-				{
-					lineWidth = 1f;
-
-					start = MapView.MapCamera.camera.WorldToScreenPoint(start);
-					end = MapView.MapCamera.camera.WorldToScreenPoint(end);
-
-					float d = Screen.height / 2f + 0.01f;
-					start.z = start.z >= 0f ? d : -d;
-					end.z = end.z >= 0f ? d : -d;
-				}
-
-				renderer.SetWidth(lineWidth, lineWidth);
-
-				renderer.SetPosition(0, start);
-				renderer.SetPosition(1, end);
-
-				this.vesselFrameCache[relay.vessel.id] = true;
-
-				relay = relay.nearestRelay;
-			}
-			while (relay != null);
+					renderer.SetColors(Color.yellow, Color.yellow);
+				}
+			}
+
+			start = ScaledSpace.LocalToScaledSpace(relay.vessel.GetWorldPos3D());
+
+			if (relay.KerbinDirect)
+			{
+				end = ScaledSpace.LocalToScaledSpace(AntennaRelay.Kerbin.position);
+			}
+			else
+			{
+				if (relay.targetRelay == null)
+				{
+					return;
+				}
+				end = ScaledSpace.LocalToScaledSpace(relay.targetRelay.vessel.GetWorldPos3D());
+			}
+
+			float lineWidth;
+
+			if (MapView.Draw3DLines)
+			{
+				lineWidth = 0.005859375f * MapView.MapCamera.Distance;
+			}
+			else
+			{
+				lineWidth = 2f;
+
+				start = MapView.MapCamera.camera.WorldToScreenPoint(start);
+				end = MapView.MapCamera.camera.WorldToScreenPoint(end);
+
+				float d = Screen.height / 2f + 0.01f;
+				start.z = start.z >= 0f ? d : -d;
+				end.z = end.z >= 0f ? d : -d;
+			}
+
+			renderer.SetWidth(lineWidth, lineWidth);
+
+			renderer.SetPosition(0, start);
+			renderer.SetPosition(1, end);
 		}
 
 		public void Cleanup()
@@ -265,11 +266,6 @@
 				}
 				this.vesselLineRenderers.Clear();
 			}
-
-			if (this.vesselFrameCache != null && this.vesselFrameCache.Count > 0)
-			{
-				this.vesselFrameCache.Clear();
-			}
 		}
 	}
 }

--- a/AntennaRelay.cs
+++ b/AntennaRelay.cs
@@ -194,7 +194,7 @@
 				Tools.PostLogMessage(string.Format(
 					"{0}: finding nearest relay for {1}",
 					this.GetType().Name,
-					this.moduleRef.ToString()
+					this.ToString()
 				));
 			}
 
@@ -255,16 +255,15 @@
 
 					if (FlightGlobals.ActiveVessel != null && FlightGlobals.ActiveVessel.id == this.vessel.id)
 					{
-						Tools.PostLogMessage(
-							this.moduleRef.ToString() + 
-							"Vessel {0} discarded because we do not have line of sight." +
+						Tools.PostLogMessage("{6}: Vessel {0} discarded because we do not have line of sight." +
 							"\npotentialSqrDistance: {1}, bestOccludedSqrDistance: {2}, maxTransmitSqrDistance: {3}" +
 							"\npotentialSqrDistance < bestOccludedSqrDistance: {4}" +
 							"\npotentialSqrDistance < (this.maxTransmitDistance * this.maxTransmitDistance): {5}",
 							potentialVessel.vesselName,
 							potentialSqrDistance, bestOccludedSqrDistance, this.maxTransmitDistance * this.maxTransmitDistance,
 							potentialSqrDistance < bestOccludedSqrDistance,
-							potentialSqrDistance < (this.maxTransmitDistance * this.maxTransmitDistance)
+							potentialSqrDistance < (this.maxTransmitDistance * this.maxTransmitDistance),
+							this.ToString()
 						);
 					}
 
@@ -275,9 +274,8 @@
 					{
 						if (FlightGlobals.ActiveVessel != null && FlightGlobals.ActiveVessel.id == this.vessel.id)
 						{
-							Tools.PostLogMessage(
-								this.moduleRef.ToString() +
-								"Checking {0} relays on {1}.",
+							Tools.PostLogMessage("{0}: Checking {1} relays on {2}.",
+								this.ToString(),
 								potentialVessel.GetAntennaRelays().Count(),
 								potentialVessel
 							);
@@ -287,7 +285,7 @@
 						{
 							if (FlightGlobals.ActiveVessel != null && FlightGlobals.ActiveVessel.id == this.vessel.id)
 							{
-								Tools.PostLogMessage(this.moduleRef.ToString() +  " Checking candidate for bestOccludedRelay: {0}" +
+								Tools.PostLogMessage(this.ToString() +  " Checking candidate for bestOccludedRelay: {0}" +
 									"\n\tCanTransmit: {1}", occludedRelay, occludedRelay.CanTransmit());
 							}
 
@@ -299,7 +297,7 @@
 
 								if (FlightGlobals.ActiveVessel != null && FlightGlobals.ActiveVessel.id == this.vessel.id)
 								{
-									Tools.PostLogMessage(this.moduleRef.ToString() + " Found new bestOccludedRelay: {0}" +
+									Tools.PostLogMessage(this.ToString() + " Found new bestOccludedRelay: {0}" +
 										"\nfirstOccludingBody: {1}" +
 										"\nbestOccludedSqrDistance: {2}",
 										occludedRelay,
@@ -322,9 +320,8 @@
 				{
 					if (FlightGlobals.ActiveVessel != null && FlightGlobals.ActiveVessel.id == this.vessel.id)
 					{
-						Tools.PostLogMessage(
-							this.moduleRef.ToString() +
-							" Vessel {0} discarded because it is out of range, or farther than another relay.",
+						Tools.PostLogMessage("{0}: Vessel {1} discarded because it is out of range, or farther than another relay.",
+							this.ToString(),
 							potentialVessel.vesselName
 						);
 					}
@@ -335,14 +332,14 @@
 
 				foreach (IAntennaRelay potentialRelay in potentialVessel.GetAntennaRelays())
 				{
-					if (potentialRelay.CanTransmit())
+					if (potentialRelay.CanTransmit() && potentialRelay.targetRelay != this)
 					{
 						this.nearestRelay = potentialRelay;
 
 						if (FlightGlobals.ActiveVessel != null && FlightGlobals.ActiveVessel.id == this.vessel.id)
 						{
 							Tools.PostLogMessage(string.Format("{0}: found new best relay {1} ({2})",
-								this.moduleRef.ToString(),
+								this.ToString(),
 								this.nearestRelay.ToString(),
 								this.nearestRelay.vessel.id
 							));
@@ -352,17 +349,14 @@
 				}
 			}
 
-			// Now that we're done with our recursive CanTransmit checks, flag this relay as not checked so it can be
-			// used next time.
-			RelayDatabase.Instance.CheckedVesselsTable.Remove(vessel.id);
-
 			CelestialBody bodyOccludingKerbin = null;
 
-			double kerbinSqrDistance = this.vessel.sqrDistanceTo(Kerbin);
+			double kerbinSqrDistance = this.vessel.DistanceTo(Kerbin) - Kerbin.Radius;
+			kerbinSqrDistance *= kerbinSqrDistance;
 
 			System.Text.StringBuilder log = new System.Text.StringBuilder();
 
-			log.AppendFormat("{0}: Search done, figuring status.", this.moduleRef.ToString());
+			log.AppendFormat("{0} ({1}): Search done, figuring status.", this.ToString(), this.GetType().Name);
 
 			// If we don't have LOS to Kerbin, focus on relays
 			if (!this.vessel.hasLineOfSightTo(Kerbin, out bodyOccludingKerbin, ARConfiguration.RadiusRatio))
@@ -430,6 +424,10 @@
 					// If the nearestRelay is closer than Kerbin, use it.
 					if (nearestRelaySqrDistance < kerbinSqrDistance)
 					{
+						log.AppendFormat("\n\tPicking relay {0} over Kerbin ({1} < {2}).",
+							this.nearestRelay == null ? "null" : this.nearestRelay.ToString(),
+							nearestRelaySqrDistance, kerbinSqrDistance);
+
 						this.KerbinDirect = false;
 						this.targetRelay = this.nearestRelay;
 					}
@@ -497,9 +495,22 @@
 				}
 			}
 
-			log.AppendFormat("{0}: Status determination complete.", this.moduleRef.ToString());
+			log.AppendFormat("\n{0}: Status determination complete.", this.ToString());
 
 			Tools.PostLogMessage(log.ToString());
+
+			// Now that we're done with our recursive CanTransmit checks, flag this relay as not checked so it can be
+			// used next time.
+			RelayDatabase.Instance.CheckedVesselsTable.Remove(vessel.id);
+		}
+
+		public override string ToString()
+		{
+			if (this is ProtoAntennaRelay)
+			{
+				return (this as ProtoAntennaRelay).ToString();
+			}
+			return this.moduleRef.ToString();
 		}
 
 		/// <summary>
@@ -511,7 +522,7 @@
 			this.moduleRef = module;
 
 			this.searchTimer = new System.Diagnostics.Stopwatch();
-			this.millisecondsBetweenSearches = 1250;
+			this.millisecondsBetweenSearches = 125;
 		}
 	}
 }

--- a/IAntennaRelay.cs
+++ b/IAntennaRelay.cs
@@ -86,6 +86,10 @@
 		/// </summary>
 		/// <returns><c>true</c> if this instance can transmit; otherwise, <c>false</c>.</returns>
 		bool CanTransmit();
+
+		string ToString();
+
+		string Title { get; }
 	}
 }
 

--- a/ModuleLimitedDataTransmitter.cs
+++ b/ModuleLimitedDataTransmitter.cs
@@ -121,7 +121,19 @@
 		{
 			get
 			{
-				return base.vessel;
+				if (base.vessel != null)
+				{
+					return base.vessel;
+				}
+				else if (this.part != null)
+				{
+					return this.part.vessel;
+				}
+
+				else
+				{
+					return null;
+				}
 			}
 		}
 
@@ -296,6 +308,19 @@
 			}
 		}
 
+		public string Title
+		{
+			get
+			{
+				if (this.part != null && this.part.partInfo != null)
+				{
+					return this.part.partInfo.title;
+				}
+
+				return string.Empty;
+			}
+		}
+
 		/*
 		 * Methods
 		 * */
@@ -686,6 +711,16 @@
 			{
 				msg.Append(" on ");
 				msg.Append(vessel.vesselName);
+			}
+			else if (
+				this.part != null &&
+				this.part.protoPartSnapshot != null &&
+				this.part.protoPartSnapshot != null &&
+				this.part.protoPartSnapshot.pVesselRef != null
+			)
+			{
+				msg.Append(" on ");
+				msg.Append(this.part.protoPartSnapshot.pVesselRef.vesselName);
 			}
 
 			return msg.ToString();

--- a/ProtoAntennaRelay.cs
+++ b/ProtoAntennaRelay.cs
@@ -85,11 +85,16 @@
 		/// Gets the underlying part's title.
 		/// </summary>
 		/// <value>The title.</value>
-		public string title
+		public string Title
 		{
 			get
 			{
-				return this.protoPart.partInfo.title;
+				if (this.protoPart != null && this.protoPart.partInfo != null)
+				{
+					return this.protoPart.partInfo.title;
+				}
+
+				return string.Empty;
 			}
 		}
 
@@ -101,7 +106,7 @@
 				Tools.PostDebugMessage(string.Format(
 					"{0}: {1} on {2} cannot transmit: {3}",
 					this.GetType().Name,
-					this.title,
+					this.Title,
 					this.vessel.vesselName,
 					Enum.GetName(typeof(PartStates), partState)
 				));
@@ -114,7 +119,7 @@
 		{
 			return string.Format(
 				"{0} on {1}",
-				this.title,
+				this.Title,
 				this.protoPart.pVesselRef.vesselName
 			);
 		}

--- a/RelayExtensions.cs
+++ b/RelayExtensions.cs
@@ -89,7 +89,7 @@
 		/// <param name="vessel">This <see cref="Vessel"/></param>
 		public static IEnumerable<IAntennaRelay> GetAntennaRelays (this Vessel vessel)
 		{
-			return RelayDatabase.Instance[vessel].Values.ToList();
+			return RelayDatabase.Instance[vessel].Values.ToList().AsReadOnly();
 		}
 
 		/// <summary>