More changes to make bad relay detection work. Something's still fighting something.
More changes to make bad relay detection work. Something's still fighting something.

--- a/ARMapRenderer.cs
+++ b/ARMapRenderer.cs
@@ -131,9 +131,9 @@
 
 						log.Append("\tChecking connection status...\n");
 
-						if (vessel.HasConnectedRelay())
-						{
-							log.AppendLine("\tHas a connection, checking for the best relay to use for the line.");
+						/*if (vessel.HasConnectedRelay())
+						{
+							log.AppendLine("\tHas a connection, checking for the best relay to use for the line.");*/
 
 							IAntennaRelay vesselRelay = null;
 							float bestScore = float.PositiveInfinity;
@@ -158,12 +158,12 @@
 
 								this.SetRelayVertices(vesselRelay);
 							}
-						}
+						/*}
 						else if (this.vesselLineRenderers.ContainsKey(vessel.id))
 						{
 							log.AppendLine("\tDisabling line because vessel has no connection.");
 							this[vessel.id].enabled = false;
-						}
+						}*/
 					}
 				}
 			}
@@ -197,31 +197,38 @@
 
 				renderer.enabled = true;
 
-				if (relay.transmitDistance < relay.nominalTransmitDistance)
-				{
-					renderer.SetColors(Color.green, Color.green);
+				if (!relay.CanTransmit())
+				{
+					renderer.SetColors(Color.red, Color.red);
 				}
 				else
 				{
-					renderer.SetColors(Color.yellow, Color.yellow);
+					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
+				{
 					if (relay.firstOccludingBody != null && relay.bestOccludedRelay != null)
 					{
 						end = ScaledSpace.LocalToScaledSpace(relay.bestOccludedRelay.vessel.GetWorldPos3D());
 					}
 					else
 					{
-						end = ScaledSpace.LocalToScaledSpace(AntennaRelay.Kerbin.position);
-					}
-				}
-				else
-				{
-					end = ScaledSpace.LocalToScaledSpace(relay.nearestRelay.vessel.GetWorldPos3D());
+						end = ScaledSpace.LocalToScaledSpace(relay.nearestRelay.vessel.GetWorldPos3D());
+					}
 				}
 
 				float lineWidth;

--- a/AntennaRelay.cs
+++ b/AntennaRelay.cs
@@ -136,8 +136,13 @@
 					}
 				}
 
+				if (this.firstOccludingBody != null && this.bestOccludedRelay != null)
+				{
+					this.KerbinDirect = false;
+					return this.DistanceTo(this.bestOccludedRelay);
+				}
+
 				this.KerbinDirect = true;
-				
 
 				// .. return the distance to Kerbin
 				return kerbinDistance;
@@ -188,6 +193,12 @@
 			// @DONE TODO: Remove nearestRelay == null
 			// Because we're correctly falling back to Kerbin in transmitDistance the first test should always fail
 			// when we're out of range of anything, and the second will fail when LOS is blocked (and enforced).
+
+			Tools.PostDebugMessage(this.moduleRef, "Checking if CanTransmit with maxTransmitDistance={0}" +
+				"\n\ttransmitDistance={1}; KerbinDirect={2}, nearestRelay={3}, bestOccludedRelay={4}",
+				this.maxTransmitDistance, this.transmitDistance, this.KerbinDirect,
+				this.nearestRelay, this.bestOccludedRelay
+			);
 
 			// If our transmit distance is greater than our maximum range, we can't transmit and it doesn't matter why.
 			if (this.transmitDistance > this.maxTransmitDistance)
@@ -296,24 +307,47 @@
 				)
 				{
 					this.firstOccludingBody = fob;
+
 					Tools.PostDebugMessage(
 						this,
-						"Vessel {0} discarded because we do not have line of sight.",
-						potentialVessel.vesselName
+						"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)
 					);
 
 					if (
-						potentialSqrDistance < bestOccludedSqrDistance &&
-						potentialSqrDistance < this.maxTransmitDistance
+						(potentialSqrDistance < bestOccludedSqrDistance) &&
+						(potentialSqrDistance < (this.maxTransmitDistance * this.maxTransmitDistance))
 					)
 					{
+						Tools.PostDebugMessage(
+							this,
+							"Checking {0} relays on {1}.",
+							potentialVessel.GetAntennaRelays().Count(),
+							potentialVessel
+						);
+
 						foreach (IAntennaRelay occludedRelay in potentialVessel.GetAntennaRelays())
 						{
+							Tools.PostDebugMessage(this, "Checking candidate for bestOccludedRelay: {0}" +
+								"\n\tCanTransmit: {1}", occludedRelay, occludedRelay.CanTransmit());
 							if (occludedRelay.CanTransmit())
 							{
 								this.bestOccludedRelay = occludedRelay;
 								this.firstOccludingBody = fob;
 								bestOccludedSqrDistance = potentialSqrDistance;
+								Tools.PostDebugMessage(this, "Found new bestOccludedRelay: {0}" +
+									"\nfirstOccludingBodoy: {1}" +
+									"\nbestOccludedSqrDistance: {2}",
+									occludedRelay,
+									fob,
+									potentialSqrDistance
+								);
 								break;
 							}
 						}
@@ -369,7 +403,7 @@
 			this.moduleRef = module;
 
 			this.searchTimer = new System.Diagnostics.Stopwatch();
-			this.millisecondsBetweenSearches = 5000;
+			this.millisecondsBetweenSearches = 1250;
 		}
 	}
 }

--- a/IAntennaRelay.cs
+++ b/IAntennaRelay.cs
@@ -44,6 +44,8 @@
 
 		IAntennaRelay nearestRelay { get; }
 
+		IAntennaRelay bestOccludedRelay { get; }
+
 		/// <summary>
 		/// Gets the distance to the nearest relay or Kerbin, whichever is closer.
 		/// </summary>

--- a/ModuleLimitedDataTransmitter.cs
+++ b/ModuleLimitedDataTransmitter.cs
@@ -138,11 +138,29 @@
 			}
 		}
 
+		public IAntennaRelay bestOccludedRelay
+		{
+			get
+			{
+				if (this.relay == null)
+				{
+					return null;
+				}
+
+				return this.relay.bestOccludedRelay;
+			}
+		}
+
 		// Returns the distance to the nearest relay or Kerbin, whichever is closer.
 		public double transmitDistance
 		{
 			get
 			{
+				if (this.relay == null)
+				{
+					return double.PositiveInfinity;
+				}
+
 				return this.relay.transmitDistance;
 			}
 		}
@@ -160,7 +178,8 @@
 		{
 			get
 			{
-				return Mathf.Sqrt (this.maxPowerFactor) * this.nominalRange;
+				// TODO: Cache this in a way that doesn't break everything.
+				return Mathf.Sqrt(this.maxPowerFactor) * this.nominalRange;
 			}
 		}
 
@@ -667,7 +686,7 @@
 		}
 
 		// When debugging, it's nice to have a button that just tells you everything.
-		#if DEBUG
+
 		[KSPEvent (guiName = "Show Debug Info", active = true, guiActive = true)]
 		public void DebugInfo()
 		{
@@ -688,7 +707,9 @@
 				"DataResourceCost: {10}\n" +
 				"TransmitterScore: {11}\n" +
 				"NearestRelay: {12}\n" +
-				"Vessel ID: {13}",
+				"BestOccludedRelay: {13}\n" +
+				"KerbinDirect: {14}\n" +
+				"Vessel ID: {15}",
 				this.name,
 				this._basepacketSize,
 				base.packetSize,
@@ -701,10 +722,13 @@
 				this.DataRate,
 				this.DataResourceCost,
 				ScienceUtil.GetTransmitterScore(this),
-				this.relay.FindNearestRelay(),
+				this.relay.nearestRelay == null ? "null" : this.relay.nearestRelay.ToString(),
+				this.relay.bestOccludedRelay == null ? "null" : this.relay.bestOccludedRelay.ToString(),
+				this.KerbinDirect,
 				this.vessel.id
 				);
-			Tools.PostDebugMessage(msg);
+
+			Tools.PostLogMessage(msg);
 		}
 
 		[KSPEvent (guiName = "Dump Vessels", active = true, guiActive = true)]
@@ -722,11 +746,10 @@
 			Tools.PostDebugMessage(sb.ToString());
 		}
 
-		[KSPEvent (guiName = "Dump RelayDB", active = true, guiActive = true)]
+		/*[KSPEvent (guiName = "Dump RelayDB", active = true, guiActive = true)]
 		public void DumpRelayDB()
 		{
 			RelayDatabase.Instance.Dump();
-		}
-		#endif
+		}*/
 	}
 }

--- a/RelayExtensions.cs
+++ b/RelayExtensions.cs
@@ -78,7 +78,7 @@
 			return relay.vessel.sqrDistanceTo(body);
 		}
 
-		public static double sqrDistanceTo(this AntennaRelay relayOne, AntennaRelay relayTwo)
+		public static double sqrDistanceTo(this AntennaRelay relayOne, IAntennaRelay relayTwo)
 		{
 			return relayOne.vessel.sqrDistanceTo(relayTwo.vessel);
 		}