AntennaRelay, ModuleLimitedDataTransmitter: Integrated getLineOfSight methods to detect when LOS is almost lost. ARdev-LOS
AntennaRelay, ModuleLimitedDataTransmitter: Integrated getLineOfSight methods to detect when LOS is almost lost.

--- a/ARConfiguration.cs
+++ b/ARConfiguration.cs
@@ -67,19 +67,22 @@
 		public void OnGUI()
 		{
 			// Only runs once, if the Toolbar is available.
-			if (this.toolbarButton == null && ToolbarManager.ToolbarAvailable)
-			{
-				Tools.PostDebugMessage(this, "Toolbar available; initializing toolbar button.");
-
-				this.toolbarButton = ToolbarManager.Instance.add("AntennaRange", "ARConfiguration");
-				this.toolbarButton.Visibility = new GameScenesVisibility(GameScenes.SPACECENTER);
-				this.toolbarButton.Text = "AR";
-				this.toolbarButton.TexturePath = "AntennaRange/Textures/toolbarIcon";
-				this.toolbarButton.TextColor = (Color)XKCDColors.Amethyst;
-				this.toolbarButton.OnClick += delegate(ClickEvent e)
-				{
-					this.toggleConfigWindow();
-				};
+			if (ToolbarManager.ToolbarAvailable)
+			{
+				if (this.toolbarButton == null)
+				{
+					Tools.PostDebugMessage(this, "Toolbar available; initializing toolbar button.");
+
+					this.toolbarButton = ToolbarManager.Instance.add("AntennaRange", "ARConfiguration");
+					this.toolbarButton.Visibility = new GameScenesVisibility(GameScenes.SPACECENTER);
+					this.toolbarButton.Text = "AR";
+					this.toolbarButton.TexturePath = "AntennaRange/Textures/toolbarIcon";
+					this.toolbarButton.TextColor = (Color)XKCDColors.Amethyst;
+					this.toolbarButton.OnClick += delegate(ClickEvent e)
+					{
+						this.toggleConfigWindow();
+					};
+				}
 			}
 			else if (this.appLauncherButton == null && ApplicationLauncher.Ready)
 			{
@@ -90,7 +93,7 @@
 					this.toggleConfigWindow,
 					ApplicationLauncher.AppScenes.SPACECENTER,
 					GameDatabase.Instance.GetTexture(
-						"AntennaRange/Textures/toolbarIcon",
+						"AntennaRange/Textures/appLauncherIcon",
 						false
 					)
 				);

--- a/ARFlightController.cs
+++ b/ARFlightController.cs
@@ -43,6 +43,7 @@
 
 		#region Fields
 		protected Dictionary<ConnectionStatus, string> connectionTextures;
+		protected Dictionary<ConnectionStatus, Texture> appLauncherTextures;
 
 		protected IButton toolbarButton;
 
@@ -61,6 +62,14 @@
 			get
 			{
 				return this.connectionTextures[this.currentConnectionStatus];
+			}
+		}
+
+		protected Texture currentAppLauncherTexture
+		{
+			get
+			{
+				return this.appLauncherTextures[this.currentConnectionStatus];
 			}
 		}
 
@@ -115,6 +124,15 @@
 			this.connectionTextures[ConnectionStatus.None] = "AntennaRange/Textures/toolbarIconNoConnection";
 			this.connectionTextures[ConnectionStatus.Suboptimal] = "AntennaRange/Textures/toolbarIconSubOptimal";
 			this.connectionTextures[ConnectionStatus.Optimal] = "AntennaRange/Textures/toolbarIcon";
+
+			this.appLauncherTextures = new Dictionary<ConnectionStatus, Texture>();
+
+			this.appLauncherTextures[ConnectionStatus.None] =
+				GameDatabase.Instance.GetTexture("AntennaRange/Textures/appLauncherIconNoConnection", false);
+			this.appLauncherTextures[ConnectionStatus.Suboptimal] =
+				GameDatabase.Instance.GetTexture("AntennaRange/Textures/appLauncherIconSubOptimal", false);
+			this.appLauncherTextures[ConnectionStatus.Optimal] =
+				GameDatabase.Instance.GetTexture("AntennaRange/Textures/appLauncherIcon", false);
 
 			if (ToolbarManager.ToolbarAvailable)
 			{
@@ -136,11 +154,32 @@
 			{
 				this.appLauncherButton = ApplicationLauncher.Instance.AddModApplication(
 					ApplicationLauncher.AppScenes.FLIGHT,
-					GameDatabase.Instance.GetTexture("AntennaRange/Textures/toolbarIconNoConnection", false)
+					this.appLauncherTextures[ConnectionStatus.None]
 				);
 			}
 
 			Tools.DebugLogger log = Tools.DebugLogger.New(this);
+
+			VesselCommand availableCommand;
+
+			if (requireConnectionForControl)
+			{
+				availableCommand = this.vessel.CurrentCommand();
+			}
+			else
+			{
+				availableCommand = VesselCommand.Crew;
+			}
+
+			log.AppendFormat("availableCommand: {0}\n\t" +
+				"(availableCommand & VesselCommand.Crew) == VesselCommand.Crew: {1}\n\t" +
+				"(availableCommand & VesselCommand.Probe) == VesselCommand.Probe: {2}\n\t" +
+				"vessel.HasConnectedRelay(): {3}",
+				(int)availableCommand,
+				(availableCommand & VesselCommand.Crew) == VesselCommand.Crew,
+				(availableCommand & VesselCommand.Probe) == VesselCommand.Probe,
+				vessel.HasConnectedRelay()
+			);
 
 			// 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...
@@ -149,8 +188,10 @@
 				requireConnectionForControl &&
 				this.vessel != null &&
 				this.vessel.vesselType != VesselType.EVA &&
-				!this.vessel.hasCrewCommand() &&
-				!this.vessel.HasConnectedRelay())
+				!(
+				    (availableCommand & VesselCommand.Crew) == VesselCommand.Crew ||
+				    (availableCommand & VesselCommand.Probe) == VesselCommand.Probe && vessel.HasConnectedRelay()
+				))
 			{
 				// ...and if the controls are not currently locked...
 				if (currentControlLock == ControlTypes.None)
@@ -240,8 +281,7 @@
 				}
 				if (this.appLauncherButton != null)
 				{
-					this.appLauncherButton.SetTexture(
-						GameDatabase.Instance.GetTexture(this.currentConnectionTexture, false));
+					this.appLauncherButton.SetTexture(this.currentAppLauncherTexture);
 				}
 			}
 
@@ -259,7 +299,8 @@
 
 			if (this.appLauncherButton != null)
 			{
-				ApplicationLauncher.Instance.RemoveApplication(this.appLauncherButton);
+				ApplicationLauncher.Instance.RemoveModApplication(this.appLauncherButton);
+				this.appLauncherButton = null;
 			}
 
 			GameEvents.onGameSceneLoadRequested.Remove(this.onSceneChangeRequested);
@@ -272,11 +313,8 @@
 		#region Event Handlers
 		protected void onSceneChangeRequested(GameScenes scene)
 		{
-			if (scene != GameScenes.FLIGHT)
-			{
-				print("ARFlightController: Requesting Destruction.");
-				MonoBehaviour.Destroy(this);
-			}
+			print("ARFlightController: Requesting Destruction.");
+			MonoBehaviour.Destroy(this);
 		}
 
 		protected void onVesselChange(Vessel vessel)

--- a/AntennaRange.cfg
+++ b/AntennaRange.cfg
@@ -46,6 +46,16 @@
 		maxPowerFactor = 8
 		maxDataFactor = 4
 	}
+
+	MODULE
+	{
+		name = ModuleScienceContainer
+
+		dataIsCollectable = true
+		dataIsStorable = false
+
+		storageRange = 2
+	}
 }
 
 @PART[mediumDishAntenna]:FOR[AntennaRange]:NEEDS[!RemoteTech2]
@@ -56,6 +66,16 @@
 		nominalRange = 30000000
 		maxPowerFactor = 8
 		maxDataFactor = 4
+	}
+
+	MODULE
+	{
+		name = ModuleScienceContainer
+
+		dataIsCollectable = true
+		dataIsStorable = false
+
+		storageRange = 2
 	}
 }
 
@@ -68,5 +88,15 @@
 		maxPowerFactor = 8
 		maxDataFactor = 4
 	}
+
+	MODULE
+	{
+		name = ModuleScienceContainer
+
+		dataIsCollectable = true
+		dataIsStorable = false
+
+		storageRange = 2
+	}
 }
 

--- a/AntennaRelay.cs
+++ b/AntennaRelay.cs
@@ -97,6 +97,15 @@
 		}
 
 		/// <summary>
+		/// Gets the <see cref="ToadicusTools.LineOfSightStatus"/> of this relay.
+		/// </summary>
+		public LineOfSightStatus losStatus
+		{
+			get;
+			protected set;
+		}
+
+		/// <summary>
 		/// Gets the transmit distance.
 		/// </summary>
 		/// <value>The transmit distance.</value>
@@ -151,12 +160,13 @@
 				this.transmitDistance > this.maxTransmitDistance ||
 				(
 					requireLineOfSight &&
-					this.nearestRelay == null &&
-					!this.vessel.hasLineOfSightTo(this.Kerbin, out this._firstOccludingBody, radiusRatio)
+					this.nearestRelay == null
 				)
 			)
 			{
-				return false;
+				this.losStatus = this.vessel.getLineOfSightTo(this.Kerbin, out this._firstOccludingBody, radiusRatio);
+
+				return this.losStatus != LineOfSightStatus.Blocked;
 			}
 			else
 			{
@@ -232,15 +242,19 @@
 				}
 
 				// Skip vessels to which we do not have line of sight.
-				if (requireLineOfSight &&
-					!this.vessel.hasLineOfSightTo(potentialVessel, out this._firstOccludingBody, radiusRatio))
-				{
-					Tools.PostDebugMessage(
-						this,
-						"Vessel {0} discarded because we do not have line of sight.",
-						potentialVessel.vesselName
-					);
-					continue;
+				if (requireLineOfSight)
+				{
+					this.losStatus = this.vessel.getLineOfSightTo(potentialVessel, out this._firstOccludingBody, radiusRatio);
+
+					if (this.losStatus == LineOfSightStatus.Blocked)
+					{
+						Tools.PostDebugMessage(
+							this,
+							"Vessel {0} discarded because we do not have line of sight.",
+							potentialVessel.vesselName
+						);
+						continue;
+					}
 				}
 
 				// Find the distance from here to the vessel...
@@ -305,6 +319,8 @@
 			// HACK: This might not be safe in all circumstances, but since AntennaRelays are not built until Start,
 			// we hope it is safe enough.
 			this.Kerbin = FlightGlobals.Bodies.FirstOrDefault(b => b.name == "Kerbin");
+
+			this.losStatus = LineOfSightStatus.Clear;
 		}
 
 		static AntennaRelay()

--- a/ModuleLimitedDataTransmitter.cs
+++ b/ModuleLimitedDataTransmitter.cs
@@ -341,6 +341,11 @@
 		// Override ModuleDataTransmitter.CanTransmit to return false when transmission is not possible.
 		public new bool CanTransmit()
 		{
+			if (this.part == null || this.relay == null)
+			{
+				return false;
+			}
+
 			PartStates partState = this.part.State;
 			if (partState == PartStates.DEAD || partState == PartStates.DEACTIVATED)
 			{
@@ -389,6 +394,75 @@
 			}
 			else
 			{
+				Tools.PostDebugMessage(this, "{0} unable to transmit during TransmitData.", this.part.partInfo.title);
+
+				var logger = Tools.DebugLogger.New(this);
+
+				foreach (ModuleScienceContainer	scienceContainer in this.vessel.getModulesOfType<ModuleScienceContainer>())
+				{
+					logger.AppendFormat("Checking ModuleScienceContainer in {0}\n",
+						scienceContainer.part.partInfo.title);
+
+					if (
+						scienceContainer.capacity != 0 &&
+						scienceContainer.GetScienceCount() >= scienceContainer.capacity
+					)
+					{
+						logger.Append("\tInsufficient capacity, skipping.\n");
+						continue;
+					}
+
+					List<ScienceData> dataStored = new List<ScienceData>();
+
+					foreach (ScienceData data in dataQueue)
+					{
+						if (!scienceContainer.allowRepeatedSubjects && scienceContainer.HasData(data))
+						{
+							logger.Append("\tAlready contains subject and repeated subjects not allowed, skipping.\n");
+							continue;
+						}
+
+						logger.AppendFormat("\tAcceptable, adding data on subject {0}... ", data.subjectID);
+						if (scienceContainer.AddData(data))
+						{
+							logger.Append("done, removing from queue.\n");
+
+							dataStored.Add(data);
+						}
+						#if DEBUG
+						else
+						{
+							logger.Append("failed.\n");
+						}
+						#endif
+					}
+
+					dataQueue.RemoveAll(i => dataStored.Contains(i));
+
+					logger.AppendFormat("\t{0} data left in queue.", dataQueue.Count);
+				}
+
+				logger.Print();
+
+				if (dataQueue.Count > 0)
+				{
+					StringBuilder msg = new StringBuilder();
+
+					msg.Append('[');
+					msg.Append(this.part.partInfo.title);
+					msg.AppendFormat("]: {0} data items could not be saved: no space available in data containers.\n");
+					msg.Append("Data to be discarded:\n");
+
+					foreach (ScienceData data in dataQueue)
+					{
+						msg.AppendFormat("\n{0}\n", data.title);
+					}
+
+					ScreenMessages.PostScreenMessage(msg.ToString(), 4f, ScreenMessageStyle.UPPER_LEFT);
+
+					Tools.PostDebugMessage(msg.ToString());
+				}
+
 				this.PostCannotTransmitError ();
 			}
 
@@ -448,7 +522,7 @@
 			{
 				if (this.CanTransmit())
 				{
-					this.UIrelayStatus = string.Intern("Connected");
+					this.UIrelayStatus = string.Format("Connected via {0}", this.relay);
 					this.UItransmitDistance = Tools.MuMech_ToSI(this.transmitDistance) + "m";
 					this.UIpacketSize = Tools.MuMech_ToSI(this.DataRate) + "MiT";
 					this.UIpacketCost = Tools.MuMech_ToSI(this.DataResourceCost) + "E";
@@ -461,7 +535,16 @@
 					}
 					else
 					{
-						this.UIrelayStatus = string.Format("Blocked by {0}", this.relay.firstOccludingBody.bodyName);
+						if (this.relay.losStatus == LineOfSightStatus.Blocked)
+						{
+							this.UIrelayStatus =
+								string.Format("Blocked by {0}", this.relay.firstOccludingBody.bodyName);
+						}
+						else if (this.relay.losStatus == LineOfSightStatus.Marginal)
+						{
+							this.UIrelayStatus =
+								string.Format("Almost blocked by {0}", this.relay.firstOccludingBody.bodyName);
+						}
 					}
 					this.UImaxTransmitDistance = "N/A";
 					this.UIpacketSize = "N/A";

--- a/Properties/AssemblyInfo.cs
+++ b/Properties/AssemblyInfo.cs
@@ -39,7 +39,7 @@
 // The assembly version has the format "{Major}.{Minor}.{Build}.{Revision}".
 // The form "{Major}.{Minor}.*" will automatically update the build and revision,
 // and "{Major}.{Minor}.{Build}.*" will update just the revision.
-[assembly: AssemblyVersion("1.4.*")]
+[assembly: AssemblyVersion("1.5.*")]
 // The following attributes are used to specify the signing key for the assembly,
 // if desired. See the Mono documentation for more information about signing.
 //[assembly: AssemblyDelaySign(false)]

 Binary files a/toolbarIcon_24x24.xcf and b/toolbarIcon_24x24.xcf differ
 Binary files a/toolbarIcon_38x38.xcf and b/toolbarIcon_38x38.xcf differ