AntennaRelay: Added optional line of sight checking.
[AntennaRange.git] / AntennaRelay.cs
blob:a/AntennaRelay.cs -> blob:b/AntennaRelay.cs
--- a/AntennaRelay.cs
+++ b/AntennaRelay.cs
@@ -44,8 +44,6 @@
 		protected System.Diagnostics.Stopwatch searchTimer;
 		protected long millisecondsBetweenSearches;
 
-		protected bool requireLineOfSight;
-
 		/// <summary>
 		/// Gets the parent Vessel.
 		/// </summary>
@@ -82,6 +80,15 @@
 		}
 
 		/// <summary>
+		/// Gets the first <see cref="CelestialBody"/> found to be blocking line of sight.
+		/// </summary>
+		public virtual CelestialBody firstOccludingBody
+		{
+			get;
+			protected set;
+		}
+
+		/// <summary>
 		/// Gets the transmit distance.
 		/// </summary>
 		/// <value>The transmit distance.</value>
@@ -132,15 +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 ||
-				(this.requireLineOfSight && this.nearestRelay == null && !this.vessel.hasLineOfSightTo(this.Kerbin))
+				(
+					ARConfiguration.RequireLineOfSight &&
+					this.nearestRelay == null &&
+					!this.vessel.hasLineOfSightTo(this.Kerbin, out fob, ARConfiguration.RadiusRatio)
+				)
 			)
 			{
+				this.firstOccludingBody = fob;
 				return false;
 			}
 			else
 			{
+				this.firstOccludingBody = null;
 				return true;
 			}
 		}
@@ -170,6 +185,8 @@
 				this,
 				this.vessel.id
 			));
+
+			this.firstOccludingBody = null;
 
 			// Set this vessel as checked, so that we don't check it again.
 			RelayDatabase.Instance.CheckedVesselsTable[vessel.id] = true;
@@ -188,12 +205,6 @@
 				// Skip vessels that have already been checked for a nearest relay this pass.
 				if (RelayDatabase.Instance.CheckedVesselsTable.ContainsKey(potentialVessel.id))
 				{
-					Tools.PostDebugMessage(
-						this,
-						"Vessel {0} discarded because it has already been checked this pass.",
-						potentialVessel.vesselName,
-						potentialVessel.vesselType
-					);
 					continue;
 				}
 
@@ -205,12 +216,6 @@
 					case VesselType.EVA:
 					case VesselType.SpaceObject:
 					case VesselType.Unknown:
-						Tools.PostDebugMessage(
-							this,
-							"Vessel {0} discarded because it is of invalid type {1}.",
-							potentialVessel.vesselName,
-							potentialVessel.vesselType
-						);
 						continue;
 					default:
 						break;
@@ -223,8 +228,12 @@
 				}
 
 				// Skip vessels to which we do not have line of sight.
-				if (this.requireLineOfSight && !this.vessel.hasLineOfSightTo(potentialVessel))
-				{
+				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.",
@@ -232,6 +241,8 @@
 					);
 					continue;
 				}
+
+				this.firstOccludingBody = null;
 
 				// Find the distance from here to the vessel...
 				double potentialSqrDistance = (potentialVessel.GetWorldPos3D() - vessel.GetWorldPos3D()).sqrMagnitude;
@@ -295,14 +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");
-
-			var config = KSP.IO.PluginConfiguration.CreateForType<AntennaRelay>();
-
-			config.load();
-
-			this.requireLineOfSight = config.GetValue<bool>("requireLineOfSight", false);
-
-			config.save();
 		}
 	}
 }