AntennaRelay, ModuleLimitedDataTransmitter: Integrated getLineOfSight methods to detect when LOS is almost lost. ARdev-LOS
[AntennaRange.git] / AntennaRelay.cs
blob:a/AntennaRelay.cs -> blob:b/AntennaRelay.cs
--- 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()