Merge branch 'master' into PrettyLinesDev2
Merge branch 'master' into PrettyLinesDev2

Conflicts:
ARFlightController.cs

--- a/ARFlightController.cs
+++ b/ARFlightController.cs
@@ -137,6 +137,8 @@
 
 		protected void FixedUpdate()
 		{
+			Tools.DebugLogger log = Tools.DebugLogger.New(this);
+
 			// If we are requiring a connection for control, the vessel does not have any adequately staffed pods,
 			// and the vessel does not have any connected relays...
 			if (
@@ -163,20 +165,39 @@
 
 			if (HighLogic.LoadedSceneIsFlight && this.toolbarButton != null && FlightGlobals.ActiveVessel != null)
 			{
+				log.Append("Checking vessel relay status.\n");
+
 				List<ModuleLimitedDataTransmitter> relays =
 					FlightGlobals.ActiveVessel.getModulesOfType<ModuleLimitedDataTransmitter>();
 
+				log.AppendFormat("\t...found {0} relays\n", relays.Count);
+
 				bool vesselCanTransmit = false;
 				bool vesselHasOptimalRelay = false;
 
 				foreach (ModuleLimitedDataTransmitter relay in relays)
 				{
-					if (!vesselCanTransmit && relay.CanTransmit())
+					log.AppendFormat("\tvesselCanTransmit: {0}, vesselHasOptimalRelay: {1}\n",
+						vesselCanTransmit, vesselHasOptimalRelay);
+
+					log.AppendFormat("\tChecking relay {0}\n" +
+						"\t\tCanTransmit: {1}, transmitDistance: {2}, nominalRange: {3}\n",
+						relay,
+						relay.CanTransmit(),
+						relay.transmitDistance,
+						relay.nominalRange
+					);
+
+					bool relayCanTransmit = relay.CanTransmit();
+
+					if (!vesselCanTransmit && relayCanTransmit)
 					{
 						vesselCanTransmit = true;
 					}
 
-					if (!vesselHasOptimalRelay && relay.transmitDistance <= (double)relay.nominalRange)
+					if (!vesselHasOptimalRelay &&
+						relayCanTransmit &&
+						relay.transmitDistance <= (double)relay.nominalRange)
 					{
 						vesselHasOptimalRelay = true;
 					}
@@ -187,6 +208,9 @@
 					}
 				}
 
+				log.AppendFormat("Done checking.  vesselCanTransmit: {0}, vesselHasOptimalRelay: {1}\n",
+					vesselCanTransmit, vesselHasOptimalRelay);
+
 				if (vesselHasOptimalRelay)
 				{
 					this.currentConnectionStatus = ConnectionStatus.Optimal;
@@ -200,11 +224,16 @@
 					this.currentConnectionStatus = ConnectionStatus.None;
 				}
 
+				log.AppendFormat("currentConnectionStatus: {0}, setting texture to {1}",
+					this.currentConnectionStatus, this.currentConnectionTexture);
+
 				this.toolbarButton.TexturePath = this.currentConnectionTexture;
 			}
-		}
-
-		protected void Destroy()
+
+			log.Print();
+		}
+
+		protected void OnDestroy()
 		{
 			InputLockManager.RemoveControlLock(this.lockID);
 
@@ -220,6 +249,8 @@
 
 			GameEvents.onGameSceneLoadRequested.Remove(this.onSceneChangeRequested);
 			GameEvents.onVesselChange.Remove(this.onVesselChange);
+
+			print("ARFlightController: Destroyed.");
 		}
 		#endregion
 
@@ -228,7 +259,8 @@
 		{
 			if (scene != GameScenes.FLIGHT)
 			{
-				MonoBehaviour.DestroyImmediate(this, true);
+				print("ARFlightController: Requesting Destruction.");
+				MonoBehaviour.Destroy(this);
 			}
 		}
 

--- a/ARMapRenderer.cs
+++ b/ARMapRenderer.cs
@@ -73,6 +73,7 @@
 		#region MonoBehaviour Lifecycle
 		private void Awake()
 		{
+			this.vesselLineRenderers = new Dictionary<Guid, LineRenderer>();
 			this.vesselFrameCache = new Dictionary<Guid, bool>();
 		}
 
@@ -88,30 +89,13 @@
 			try
 			{
 				log.AppendFormat("OnPreCull.\n");
-
-				double sma;
-
-				switch (MapView.MapCamera.target.type)
-				{
-					case MapObject.MapObjectType.CELESTIALBODY:
-						sma = MapView.MapCamera.target.celestialBody.orbit.semiMajorAxis;
-						break;
-					case MapObject.MapObjectType.VESSEL:
-						sma = MapView.MapCamera.target.vessel.orbit.semiMajorAxis;
-						break;
-					default:
-						sma = ScaledSpace.ScaleFactor;
-						break;
-				}
 
 				log.AppendFormat("\tMapView: Draw3DLines: {0}\n" +
 					"\tMapView.MapCamera.camera.fieldOfView: {1}\n" +
-					"\tMapView.MapCamera.Distance: {2}\n" +
-					"\tDistanceVSSMAFactor: {3}\n",
+					"\tMapView.MapCamera.Distance: {2}\n",
 					MapView.Draw3DLines,
 					MapView.MapCamera.camera.fieldOfView,
-					MapView.MapCamera.Distance,
-					(float)((double)MapView.MapCamera.Distance / Math.Abs(sma) * ScaledSpace.InverseScaleFactor)
+					MapView.MapCamera.Distance
 				);
 
 				this.vesselFrameCache.Clear();
@@ -143,6 +127,8 @@
 								continue;
 						}
 
+						log.Append("\tChecking connection status...\n");
+
 						if (vessel.HasConnectedRelay())
 						{
 							log.AppendLine("\tHas a connection, checking for the best relay to use for the line.");
@@ -185,10 +171,11 @@
 			}
 		}
 
-		private void Destroy()
+		private void OnDestroy()
 		{
 			this.vesselLineRenderers.Clear();
 			this.vesselLineRenderers = null;
+			print("ARMapRenderer: Destroyed.");
 		}
 		#endregion
 
@@ -210,6 +197,15 @@
 
 					renderer.enabled = true;
 
+					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.nearestRelay == null)
@@ -235,8 +231,10 @@
 						end = MapView.MapCamera.camera.WorldToScreenPoint(end);
 
 						float d = Screen.height / 2f + 0.01f;
-						start.z = start.z >= 0.15f ? d : -d;
-						end.z = end.z >= 0.15f ? d : -d;
+						start.z = start.z >= 0f ? d : -d;
+						end.z = end.z >= 0f ? d : -d;
+
+						Debug.Log(string.Format("start.z: {0}", start.z));
 					}
 
 					renderer.SetWidth(lineWidth, lineWidth);

--- a/AntennaRelay.cs
+++ b/AntennaRelay.cs
@@ -117,6 +117,12 @@
 					return this.DistanceTo(nearestRelay);
 				}
 			}
+		}
+
+		public virtual double nominalTransmitDistance
+		{
+			get;
+			set;
 		}
 
 		/// <summary>

--- a/IAntennaRelay.cs
+++ b/IAntennaRelay.cs
@@ -50,6 +50,8 @@
 		/// <value>The distance to the nearest relay or Kerbin, whichever is closer.</value>
 		double transmitDistance { get; }
 
+		double nominalTransmitDistance { get; }
+
 		/// <summary>
 		/// The maximum distance at which this relay can operate.
 		/// </summary>

--- a/ModuleLimitedDataTransmitter.cs
+++ b/ModuleLimitedDataTransmitter.cs
@@ -145,6 +145,14 @@
 			get
 			{
 				return this.relay.transmitDistance;
+			}
+		}
+
+		public double nominalTransmitDistance
+		{
+			get
+			{
+				return this.nominalRange;
 			}
 		}
 
@@ -249,6 +257,7 @@
 			{
 				this.relay = new AntennaRelay(this);
 				this.relay.maxTransmitDistance = this.maxTransmitDistance;
+				this.relay.nominalTransmitDistance = this.nominalRange;
 
 				this.UImaxTransmitDistance = Tools.MuMech_ToSI(this.maxTransmitDistance) + "m";
 
@@ -354,6 +363,11 @@
 		// Override ModuleDataTransmitter.CanTransmit to return false when transmission is not possible.
 		public new bool CanTransmit()
 		{
+			if (this.relay == null)
+			{
+				return false;
+			}
+
 			PartStates partState = this.part.State;
 			if (partState == PartStates.DEAD || partState == PartStates.DEACTIVATED)
 			{

--- a/ProtoAntennaRelay.cs
+++ b/ProtoAntennaRelay.cs
@@ -47,6 +47,14 @@
 			get
 			{
 				return this.protoPart.pVesselRef.vesselRef;
+			}
+		}
+
+		public override double nominalTransmitDistance
+		{
+			get
+			{
+				return this.moduleRef.nominalTransmitDistance;
 			}
 		}