Network resolution by total connection cost is working now.
[AntennaRange.git] / ModuleLimitedDataTransmitter.cs
blob:a/ModuleLimitedDataTransmitter.cs -> blob:b/ModuleLimitedDataTransmitter.cs
--- a/ModuleLimitedDataTransmitter.cs
+++ b/ModuleLimitedDataTransmitter.cs
@@ -52,9 +52,6 @@
 		: ModuleDataTransmitter, IScienceDataTransmitter, IAntennaRelay, IModuleInfo
 	{
 		private const string tooltipSkinName = "PartTooltipSkin";
-		private static GUISkin partTooltipSkin;
-		private static GUIStyle partTooltipBodyStyle;
-		private static GUIStyle partTooltipHeaderStyle;
 
 		// Every antenna is a relay.
 		private AntennaRelay relay;
@@ -62,8 +59,17 @@
 		// Sometimes we will need to communicate errors; this is how we do it.
 		private ScreenMessage ErrorMsg;
 
-		// Used in module info panes for part tooltips in the editor and R&D
-		private GUIContent moduleInfoContent;
+		/// <summary>
+		/// The base size of a transmission packet, in MiT.
+		/// </summary>
+		[KSPField(isPersistant = false)]
+		public float basePacketSize;
+
+		/// <summary>
+		/// The base resource cost of a transmission packet, presumably in EC.
+		/// </summary>
+		[KSPField(isPersistant = false)]
+		public float basePacketResourceCost;
 
 		/// <summary>
 		/// When additive ranges are enabled, the distance from Kerbin at which the antenna will perform exactly as
@@ -186,53 +192,31 @@
 			}
 		}
 
-		/// <summary>
-		/// Gets or sets the data capacity of a packet, in MiT/packet
-		/// </summary>
-		/// <value>The data capacity of a packet, in MiT/packet</value>
-		public float PacketSize
-		{
-			get
-			{
-				return this.packetSize;
+		private RelayDataCost _currentLinkCost = new RelayDataCost();
+		/// <summary>
+		/// Gets the current link resource rate in EC/MiT.
+		/// </summary>
+		/// <value>The current link resource rate in EC/MiT.</value>
+		public RelayDataCost CurrentLinkCost
+		{
+			get
+			{
+				_currentLinkCost.PacketResourceCost = this.packetResourceCost;
+				_currentLinkCost.PacketSize = this.packetSize;
+				return _currentLinkCost;
 			}
 			set
 			{
-				this.packetSize = value;
-			}
-		}
-
-		/// <summary>
-		/// Gets the base data capacity of a packet, in MiT/packet
-		/// </summary>
-		/// <value>The base data capacity of a packet, in MiT/packet</value>
-		public float BasePacketSize
-		{
-			get;
-			private set;
-		}
-
-		/// <summary>
-		/// Gets or sets the resource cost of a packet, in EC/packet
-		/// </summary>
-		/// <value>The resource cost of a packet, in EC/packet</value>
-		public float PacketResourceCost
-		{
-			get
-			{
-				return this.packetResourceCost;
-			}
-			set
-			{
-				this.packetResourceCost = value;
-			}
-		}
-
-		/// <summary>
-		/// Gets the base resource cost of a packet, in EC/packet
-		/// </summary>
-		/// <value>The base resource cost of a packet, in EC/packet</value>
-		public float BasePacketResourceCost
+				this.packetResourceCost = value.PacketResourceCost;
+				this.packetSize = value.PacketSize;
+			}
+		}
+
+		/// <summary>
+		/// Gets the base link resource rate in EC/MiT.
+		/// </summary>
+		/// <value>The base link resource rate in EC/MiT.</value>
+		public RelayDataCost BaseLinkCost
 		{
 			get;
 			private set;
@@ -431,12 +415,14 @@
 		{
 			get
 			{
-				if (this.relay == null)
-				{
-					return float.PositiveInfinity;
-				}
-
-				return this.relay.DataRate;
+				if (this.CanTransmit())
+				{
+					return this.packetSize;
+				}
+				else
+				{
+					return float.Epsilon;
+				}
 			}
 		}
 
@@ -448,12 +434,14 @@
 		{
 			get
 			{
-				if (this.relay == null)
-				{
-					return double.PositiveInfinity;
-				}
-
-				return this.relay.DataResourceCost;
+				if (this.CanTransmit())
+				{
+					return this.packetResourceCost;
+				}
+				else
+				{
+					return float.PositiveInfinity;
+				}
 			}
 		}
 
@@ -500,16 +488,13 @@
 			this.packetThrottle = 100f;
 		}
 
+		#if DEBUG
 		/// <summary>
 		/// PartModule OnAwake override; runs at Unity Awake.
 		/// </summary>
 		public override void OnAwake()
 		{
 			base.OnAwake();
-
-			this.BasePacketSize = base.packetSize;
-			this.BasePacketResourceCost = base.packetResourceCost;
-			this.moduleInfoContent = new GUIContent();
 
 			this.LogDebug("{0} loaded:\n" +
 				"packetSize: {1}\n" +
@@ -519,12 +504,13 @@
 				"maxDataFactor: {5}\n",
 				this,
 				base.packetSize,
-				this.BasePacketResourceCost,
+				this.packetResourceCost,
 				this.nominalTransmitDistance,
 				this.maxPowerFactor,
 				this.maxDataFactor
 			);
 		}
+		#endif
 
 		/// <summary>
 		/// PartModule OnStart override; runs at Unity Start.
@@ -532,9 +518,10 @@
 		/// <param name="state">State.</param>
 		public override void OnStart (StartState state)
 		{
+			this.BaseLinkCost = new RelayDataCost(this.basePacketResourceCost, this.basePacketSize);
+			this.RecalculateMaxRange();
+
 			base.OnStart (state);
-
-			this.RecalculateMaxRange();
 
 			if (state >= StartState.PreLaunch)
 			{
@@ -562,6 +549,7 @@
 
 			base.OnLoad (node);
 
+			this.BaseLinkCost = new RelayDataCost(this.basePacketResourceCost, this.basePacketSize);
 			this.RecalculateMaxRange();
 		}
 
@@ -585,6 +573,9 @@
 		// HACK: Currently hacks around Squad's extraneous layout box, see KSPModders issue #5118
 		private void drawTooltipWidget(Rect rect)
 		{
+			/*
+			 * Removed all this because Squad doesn't even call it anymore.
+			 *
 			this.moduleInfoContent.text = this.GetInfo();
 
 			if (partTooltipSkin == null)
@@ -626,7 +617,7 @@
 			GUILayout.Space(height - orgHeight
 				- partTooltipBodyStyle.padding.bottom - partTooltipBodyStyle.padding.top
 				- 2f * (partTooltipBodyStyle.margin.bottom + partTooltipBodyStyle.margin.top)
-			);
+			);*/
 		}
 
 		/// <summary>
@@ -644,16 +635,12 @@
 		{
 			using (PooledStringBuilder sb = PooledStringBuilder.Get())
 			{
-				string text;
-
-				sb.Append(base.GetInfo());
-
 				if (ARConfiguration.UseAdditiveRanges)
 				{
-					sb.AppendFormat("Nominal Range to Kerbin: {0:S3}m\n",
+					sb.AppendFormat("<b>Nominal Range to Kerbin: </b>{0:S3}m\n",
 						Math.Sqrt(this.nominalTransmitDistance * ARConfiguration.KerbinNominalRange)
 					);
-					sb.AppendFormat("Maximum Range to Kerbin: {0:S3}m",
+					sb.AppendFormat("<b>Maximum Range to Kerbin: </b>{0:S3}m\n",
 						Math.Sqrt(
 							this.nominalTransmitDistance * Math.Sqrt(this.maxPowerFactor) *
 							ARConfiguration.KerbinRelayRange
@@ -662,13 +649,69 @@
 				}
 				else
 				{
-					sb.AppendFormat("Nominal Range: {0:S3}m\n", this.nominalTransmitDistance);
-					sb.AppendFormat("Maximum Range: {0:S3}m", this.maxTransmitDistance);
-				}
-
-				text = sb.ToString();
-
-				return text;
+					sb.AppendFormat("<b>Nominal Range: </b>{0:S3}m\n", this.nominalTransmitDistance);
+					sb.AppendFormat("<b>Maximum Range: </b>{0:S3}m\n", this.maxTransmitDistance);
+				}
+
+				sb.AppendLine();
+
+				sb.AppendFormat("<b>Nominal Packet Size: </b>{0:S2}iT\n", this.BaseLinkCost.PacketSize * 1000000f);
+				sb.AppendFormat(
+					"<b>Nominal Data Rate: </b>{0:S2}iT/sec\n",
+					this.BaseLinkCost.PacketSize / this.packetInterval * 1000000f
+				);
+
+				sb.AppendLine();
+
+				sb.AppendFormat("<b>Within Nominal Range...\n...Maximum Speedup:</b> {0:P0}\n", this.maxDataFactor);
+
+				if (ARConfiguration.FixedPowerCost)
+				{
+					sb.AppendLine();
+
+					sb.AppendFormat(
+						"<b>Outside Nominal Range...\n...Maximum Slowdown:</b> {0:P1}\n",
+						1f / this.maxPowerFactor
+					);
+
+					sb.AppendLine();
+
+					sb.AppendFormat(
+						"<b>Packet Cost:</b> {0:0.0#} {1}\n",
+						this.BaseLinkCost.PacketResourceCost,
+						this.requiredResource == "ElectricCharge" ? "EC" : this.requiredResource
+					);
+					sb.AppendFormat(
+						"<b>Power Drain:</b> {0:0.0#} {1}/s\n",
+						this.BaseLinkCost.PacketResourceCost / this.packetInterval,
+						this.requiredResource == "ElectricCharge" ? "EC" : this.requiredResource
+					);
+				}
+				else
+				{
+					sb.AppendLine();
+
+					sb.AppendFormat(
+						"<b>Nominal Packet Cost:</b> {0:0.0#} {1}\n",
+						this.BaseLinkCost.PacketResourceCost,
+						this.requiredResource == "ElectricCharge" ? "EC" : this.requiredResource
+					);
+					sb.AppendFormat(
+						"<b>Nominal Power Drain:</b> {0:0.0#} {1}/s\n",
+						this.BaseLinkCost.PacketResourceCost / this.packetInterval,
+						this.requiredResource == "ElectricCharge" ? "EC" : this.requiredResource
+					);
+
+					sb.AppendLine();
+
+					sb.AppendFormat(
+						"<b>Outside Nominal Range...\n...Maximum Power Drain:</b> {0:0.0#} {1}/s\n",
+						this.BaseLinkCost.PacketResourceCost / this.packetInterval * this.maxPowerFactor,
+						this.requiredResource == "ElectricCharge" ? "EC" : this.requiredResource
+					);
+				}
+
+				return sb.ToString();
 			}
 		}
 
@@ -710,7 +753,14 @@
 			if (this.relay != null)
 			{
 				this.relay.RecalculateTransmissionRates();
-			}
+				this.LogDebug("Recalculated transmission rates in MLDT, cost is {0}", this.CurrentLinkCost);
+			}
+			#if DEBUG
+			else
+			{
+				this.LogDebug("Skipping recalculation; relay is null.");
+			}
+			#endif
 		}
 
 		/// <summary>
@@ -942,6 +992,10 @@
 				if (this.part != null && this.part.partInfo != null)
 				{
 					sb.Append(this.part.partInfo.title);
+					#if DEBUG
+					sb.Append('#');
+					sb.Append(this.part.flightID);
+					#endif
 				}
 				else
 				{