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
@@ -35,14 +35,9 @@
 {
 	public class AntennaRelay
 	{
-		public static bool requireLineOfSight;
-		public static double radiusRatio;
-
 		// We don't have a Bard, so we'll hide Kerbin here.
 		protected CelestialBody Kerbin;
 
-		protected CelestialBody _firstOccludingBody;
-
 		protected IAntennaRelay _nearestRelayCache;
 		protected IAntennaRelay moduleRef;
 
@@ -85,21 +80,9 @@
 		}
 
 		/// <summary>
-		/// Gets the first occluding body.
-		/// </summary>
-		/// <value>The first occluding body.</value>
-		public CelestialBody firstOccludingBody
-		{
-			get
-			{
-				return this._firstOccludingBody;
-			}
-		}
-
-		/// <summary>
-		/// Gets the <see cref="ToadicusTools.LineOfSightStatus"/> of this relay.
-		/// </summary>
-		public LineOfSightStatus losStatus
+		/// Gets the first <see cref="CelestialBody"/> found to be blocking line of sight.
+		/// </summary>
+		public virtual CelestialBody firstOccludingBody
 		{
 			get;
 			protected set;
@@ -156,20 +139,23 @@
 		/// <returns><c>true</c> if this instance can transmit; otherwise, <c>false</c>.</returns>
 		public virtual bool CanTransmit()
 		{
+			CelestialBody fob = null;
+
 			if (
 				this.transmitDistance > this.maxTransmitDistance ||
 				(
-					requireLineOfSight &&
-					this.nearestRelay == null
+					ARConfiguration.RequireLineOfSight &&
+					this.nearestRelay == null &&
+					!this.vessel.hasLineOfSightTo(this.Kerbin, out fob, ARConfiguration.RadiusRatio)
 				)
 			)
 			{
-				this.losStatus = this.vessel.getLineOfSightTo(this.Kerbin, out this._firstOccludingBody, radiusRatio);
-
-				return this.losStatus != LineOfSightStatus.Blocked;
+				this.firstOccludingBody = fob;
+				return false;
 			}
 			else
 			{
+				this.firstOccludingBody = null;
 				return true;
 			}
 		}
@@ -200,7 +186,7 @@
 				this.vessel.id
 			));
 
-			this._firstOccludingBody = null;
+			this.firstOccludingBody = null;
 
 			// Set this vessel as checked, so that we don't check it again.
 			RelayDatabase.Instance.CheckedVesselsTable[vessel.id] = true;
@@ -242,20 +228,21 @@
 				}
 
 				// Skip vessels to which we do not have line of sight.
-				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;
-					}
-				}
+				CelestialBody fob = null;
+
+				if (ARConfiguration.RequireLineOfSight &&
+					!this.vessel.hasLineOfSightTo(potentialVessel, out fob, ARConfiguration.RadiusRatio))
+				{
+					this.firstOccludingBody = fob;
+					Tools.PostDebugMessage(
+						this,
+						"Vessel {0} discarded because we do not have line of sight.",
+						potentialVessel.vesselName
+					);
+					continue;
+				}
+
+				this.firstOccludingBody = null;
 
 				// Find the distance from here to the vessel...
 				double potentialSqrDistance = (potentialVessel.GetWorldPos3D() - vessel.GetWorldPos3D()).sqrMagnitude;
@@ -319,19 +306,6 @@
 			// 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()
-		{
-			var config = KSP.IO.PluginConfiguration.CreateForType<AntennaRelay>();
-
-			config.load();
-
-			AntennaRelay.requireLineOfSight = config.GetValue<bool>("requireLineOfSight", false);
-
-			config.save();
 		}
 	}
 }