Mostly refactoring, and removing the onVesselDestroy event subscription, which wasn't being very useful.
[AntennaRange.git] / ProtoAntennaRelay.cs
blob:a/ProtoAntennaRelay.cs -> blob:b/ProtoAntennaRelay.cs
// AntennaRange © 2014 toadicus // AntennaRange
// //
// AntennaRange provides incentive and requirements for the use of the various antenna parts. // ProtoAntennaRelay.cs
// Nominally, the breakdown is as follows:  
// //
// Communotron 16 - Suitable up to Kerbalsynchronous Orbit // Copyright © 2014-2015, toadicus
// Comms DTS-M1 - Suitable throughout the Kerbin subsystem // All rights reserved.
// Communotron 88-88 - Suitable throughout the Kerbol system.  
// //
// This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License. To view a // Redistribution and use in source and binary forms, with or without modification,
// copy of this license, visit http://creativecommons.org/licenses/by-nc-sa/3.0/ // are permitted provided that the following conditions are met:
// //
// This software uses the ModuleManager library © 2013 ialdabaoth, used under a Creative Commons Attribution-ShareAlike // 1. Redistributions of source code must retain the above copyright notice,
// 3.0 Uported License. // this list of conditions and the following disclaimer.
// //
// This software uses code from the MuMechLib library, © 2013 r4m0n, used under the GNU GPL version 3. // 2. Redistributions in binary form must reproduce the above copyright notice,
  // this list of conditions and the following disclaimer in the documentation and/or other
  // materials provided with the distribution.
  //
  // 3. Neither the name of the copyright holder nor the names of its contributors may be used
  // to endorse or promote products derived from this software without specific prior written permission.
  //
  // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  // INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
   
  using KSP;
using System; using System;
using System.Linq; using ToadicusTools;
   
namespace AntennaRange namespace AntennaRange
{ {
/* /// <summary>
* Wrapper class for ProtoPartModuleSnapshot extending AntennaRelay and implementing IAntennaRelay. /// Wrapper class for ProtoPartModuleSnapshot extending AntennaRelay and implementing IAntennaRelay.
* This is used for finding relays in unloaded Vessels. /// This is used for finding relays in unloaded Vessels.
* */ /// </summary>
public class ProtoAntennaRelay : AntennaRelay, IAntennaRelay public class ProtoAntennaRelay : AntennaRelay, IAntennaRelay
{ {
// Stores the relay prefab  
protected IAntennaRelay relayPrefab;  
   
// Stores the prototype part so we can make sure we haven't exploded or so. // Stores the prototype part so we can make sure we haven't exploded or so.
protected ProtoPartSnapshot protoPart; private ProtoPartSnapshot protoPart;
   
/// <summary> /// <summary>
/// The maximum distance at which this transmitter can operate. /// Gets the parent Vessel.
/// </summary> /// </summary>
/// <value>The max transmit distance.</value> public override Vessel vessel
public override float maxTransmitDistance  
{ {
get get
{ {
return relayPrefab.maxTransmitDistance; if (
  this.protoPart != null &&
  this.protoPart.pVesselRef != null &&
  this.protoPart.pVesselRef.vesselRef != null
  )
  {
  return this.protoPart.pVesselRef.vesselRef;
  }
  else
  {
  Tools.PostErrorMessage("{0}: Could not fetch vessel! {1}{2}{3}",
  this.ToString(),
  this.protoPart == null ? "\n\tprotoPart=null" : string.Empty,
  this.protoPart != null && this.protoPart.pVesselRef == null ?
  "\n\tthis.protoPart.pVesselRef=null" : string.Empty,
  this.protoPart != null && this.protoPart.pVesselRef != null &&
  this.protoPart.pVesselRef.vesselRef == null ?
  "\n\tthis.protoPart.pVesselRef.vesselRef=null" : string.Empty
  );
  return null;
  }
} }
} }
   
/// <summary> /// <summary>
/// Gets a value indicating whether this <see cref="AntennaRange.ProtoDataTransmitter"/> has been checked during /// Gets the nominal transmit distance at which the Antenna behaves just as prescribed by Squad's config.
/// the current relay attempt.  
/// </summary> /// </summary>
/// <value><c>true</c> if relay checked; otherwise, <c>false</c>.</value> public override double nominalTransmitDistance
public override bool relayChecked  
{ {
get; get
protected set; {
  return this.moduleRef.nominalTransmitDistance;
  }
  }
   
  /// <summary>
  /// The maximum distance at which this relay can operate.
  /// </summary>
  public override double maxTransmitDistance
  {
  get
  {
  return moduleRef.maxTransmitDistance;
  }
} }
   
/// <summary> /// <summary>
/// Gets the underlying part's title. /// Gets the underlying part's title.
/// </summary> /// </summary>
/// <value>The title.</value> /// <value>The title.</value>
public string title public string Title
{ {
get get
{ {
return this.protoPart.partInfo.title; if (this.protoPart != null && this.protoPart.partInfo != null)
  {
  return this.protoPart.partInfo.title;
  }
   
  return string.Empty;
} }
} }
   
  /// <summary>
  /// Determines whether this instance can transmit.
  /// <c>true</c> if this instance can transmit; otherwise, <c>false</c>.
  /// </summary>
public override bool CanTransmit() public override bool CanTransmit()
{ {
PartStates partState = (PartStates)this.protoPart.state; PartStates partState = (PartStates)this.protoPart.state;
if (partState == PartStates.DEAD || partState == PartStates.DEACTIVATED) if (partState == PartStates.DEAD || partState == PartStates.DEACTIVATED)
{ {
Tools.PostDebugMessage(string.Format( Tools.PostDebugMessage(string.Format(
"{0}: {1} on {2} cannot transmit: {3}", "{0}: {1} on {2} cannot transmit: {3}",
this.GetType().Name, this.GetType().Name,
this.title, this.Title,
this.vessel.name, this.vessel.vesselName,
Enum.GetName(typeof(PartStates), partState) Enum.GetName(typeof(PartStates), partState)
)); ));
return false; return false;
} }
return base.CanTransmit(); return base.CanTransmit();
} }
   
  /// <summary>
  /// Returns a <see cref="System.String"/> that represents the current <see cref="AntennaRange.ProtoAntennaRelay"/>.
  /// </summary>
  /// <returns>A <see cref="System.String"/> that represents the current <see cref="AntennaRange.ProtoAntennaRelay"/>.</returns>
public override string ToString() public override string ToString()
{ {
return string.Format( System.Text.StringBuilder sb = Tools.GetStringBuilder();
"{0} on {1}.",  
this.title, sb.Append(this.Title);
this.protoPart.pVesselRef.vesselName  
); if (this.protoPart != null && this.protoPart.pVesselRef != null)
  {
  sb.AppendFormat(" on {0}", this.protoPart.pVesselRef.vesselName);
  }
   
  Tools.PutStringBuilder(sb);
   
  return sb.ToString();
} }
   
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="AntennaRange.ProtoAntennaRelay"/> class. /// Initializes a new instance of the <see cref="AntennaRange.AntennaRelay"/> class.
/// </summary> /// </summary>
/// <param name="ms">The ProtoPartModuleSnapshot to wrap</param> /// <param name="prefabRelay">The module reference underlying this AntennaRelay,
/// <param name="vessel">The parent Vessel</param> /// as an <see cref="AntennaRange.IAntennaRelay"/></param>
public ProtoAntennaRelay(IAntennaRelay prefabRelay, ProtoPartSnapshot pps) : base(pps.pVesselRef.vesselRef) /// <param name="pps">The prototype partreference on which the module resides.</param>
  public ProtoAntennaRelay(IAntennaRelay prefabRelay, ProtoPartSnapshot pps) : base(prefabRelay)
{ {
this.relayPrefab = prefabRelay;  
this.protoPart = pps; this.protoPart = pps;
this.vessel = pps.pVesselRef.vesselRef;  
  Tools.PostLogMessage("{0}: constructed {1}", this.GetType().Name, this.ToString());
} }
} }
} }