ARFlightController: Optimized a bit, and only search for ConnectionStatus every now and then.
ARFlightController: Optimized a bit, and only search for ConnectionStatus every now and then.

--- 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/ARFlightController.cs
+++ b/ARFlightController.cs
@@ -46,6 +46,9 @@
 		protected IButton toolbarButton;
 
 		protected ApplicationLauncherButton appLauncherButton;
+		protected Tools.DebugLogger log;
+
+		protected System.Diagnostics.Stopwatch updateTimer;
 		#endregion
 
 		#region Properties
@@ -116,6 +119,10 @@
 		protected void Awake()
 		{
 			this.lockID = "ARConnectionRequired";
+
+			this.log = Tools.DebugLogger.New(this);
+
+			this.updateTimer = new System.Diagnostics.Stopwatch();
 
 			this.connectionTextures = new Dictionary<ConnectionStatus, string>();
 
@@ -161,7 +168,7 @@
 				);
 			}
 
-			Tools.DebugLogger log = Tools.DebugLogger.New(this);
+			this.log.Clear();
 
 			VesselCommand availableCommand;
 
@@ -209,7 +216,23 @@
 				// ...unlock the controls.
 				InputLockManager.RemoveControlLock(this.lockID);
 			}
-				
+
+			log.Print();
+		}
+
+		protected void Update()
+		{
+			if (!this.updateTimer.IsRunning || this.updateTimer.ElapsedMilliseconds > 125L)
+			{
+				this.updateTimer.Reset();
+			}
+			else
+			{
+				return;
+			}
+
+			this.log.Clear();
+
 			if (
 				(this.toolbarButton != null || this.appLauncherButton != null) &&
 				HighLogic.LoadedSceneIsFlight &&
@@ -218,62 +241,7 @@
 			{
 				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)
-				{
-					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 &&
-						relayCanTransmit &&
-						relay.transmitDistance <= (double)relay.nominalRange)
-					{
-						vesselHasOptimalRelay = true;
-					}
-
-					if (vesselCanTransmit && vesselHasOptimalRelay)
-					{
-						break;
-					}
-				}
-
-				log.AppendFormat("Done checking.  vesselCanTransmit: {0}, vesselHasOptimalRelay: {1}\n",
-					vesselCanTransmit, vesselHasOptimalRelay);
-
-				if (vesselHasOptimalRelay)
-				{
-					this.currentConnectionStatus = ConnectionStatus.Optimal;
-				}
-				else if (vesselCanTransmit)
-				{
-					this.currentConnectionStatus = ConnectionStatus.Suboptimal;
-				}
-				else
-				{
-					this.currentConnectionStatus = ConnectionStatus.None;
-				}
+				this.currentConnectionStatus = FlightGlobals.ActiveVessel.GetConnectionStatus();
 
 				log.AppendFormat("currentConnectionStatus: {0}, setting texture to {1}",
 					this.currentConnectionStatus, this.currentConnectionTexture);
@@ -339,13 +307,6 @@
 			InputLockManager.RemoveControlLock(this.lockID);
 		}
 		#endregion
-
-		public enum ConnectionStatus
-		{
-			None,
-			Suboptimal,
-			Optimal
-		}
 	}
 }
 

--- 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()
@@ -230,11 +235,11 @@
 
 			if (MapView.Draw3DLines)
 			{
-				lineWidth = 0.004f * MapView.MapCamera.Distance;
+				lineWidth = 0.005859375f * MapView.MapCamera.Distance;
 			}
 			else
 			{
-				lineWidth = 1f;
+				lineWidth = 2f;
 
 				start = MapView.MapCamera.camera.WorldToScreenPoint(start);
 				end = MapView.MapCamera.camera.WorldToScreenPoint(end);
@@ -261,11 +266,6 @@
 				}
 				this.vesselLineRenderers.Clear();
 			}
-
-			if (this.vesselFrameCache != null && this.vesselFrameCache.Count > 0)
-			{
-				this.vesselFrameCache.Clear();
-			}
 		}
 	}
 }

--- a/AntennaRelay.cs
+++ b/AntennaRelay.cs
@@ -522,7 +522,7 @@
 			this.moduleRef = module;
 
 			this.searchTimer = new System.Diagnostics.Stopwatch();
-			this.millisecondsBetweenSearches = 66;
+			this.millisecondsBetweenSearches = 125L;
 		}
 	}
 }

--- a/RelayExtensions.cs
+++ b/RelayExtensions.cs
@@ -109,6 +109,39 @@
 
 			return false;
 		}
+
+		public static ConnectionStatus GetConnectionStatus(this Vessel vessel)
+		{
+			bool canTransmit = false;
+
+			foreach (IAntennaRelay relay in RelayDatabase.Instance[vessel].Values)
+			{
+				if (relay.CanTransmit())
+				{
+					canTransmit = true;
+					if (relay.transmitDistance <= relay.nominalTransmitDistance)
+					{
+						return ConnectionStatus.Optimal;
+					}
+				}
+			}
+
+			if (canTransmit)
+			{
+				return ConnectionStatus.Suboptimal;
+			}
+			else
+			{
+				return ConnectionStatus.None;
+			}
+		}
+	}
+
+	public enum ConnectionStatus
+	{
+		None,
+		Suboptimal,
+		Optimal
 	}
 }