A little refactoring and a new message to indicate if you are using a relay. Also fixed using distant relays instead of nearby Kerbin.
[AntennaRange.git] / Extensions.cs
blob:a/Extensions.cs -> blob:b/Extensions.cs
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
   
namespace AntennaRange namespace AntennaRange
{ {
/* /*
* A class of utility extensions for Vessels and Relays to help find a relay path back to Kerbin. * A class of utility extensions for Vessels and Relays to help find a relay path back to Kerbin.
* */ * */
public static class Extensions public static class Extensions
{ {
/// <summary> /// <summary>
/// Returns the distance between this Vessel and another Vessel. /// Returns the distance between this Vessel and another Vessel.
/// </summary> /// </summary>
/// <param name="vesselOne">This <see cref="Vessel"/><see ></param> /// <param name="vesselOne">This <see cref="Vessel"/><see ></param>
/// <param name="vesselTwo">Another <see cref="Vessel"/></param> /// <param name="vesselTwo">Another <see cref="Vessel"/></param>
public static double DistanceTo(this Vessel vesselOne, Vessel vesselTwo) public static double DistanceTo(this Vessel vesselOne, Vessel vesselTwo)
{ {
return (vesselOne.GetWorldPos3D() - vesselTwo.GetWorldPos3D()).magnitude; return (vesselOne.GetWorldPos3D() - vesselTwo.GetWorldPos3D()).magnitude;
} }
   
/// <summary> /// <summary>
/// Returns the distance between this Vessel and a CelestialBody /// Returns the distance between this Vessel and a CelestialBody
/// </summary> /// </summary>
/// <param name="vessel">This Vessel</param> /// <param name="vessel">This Vessel</param>
/// <param name="body">A <see cref="CelestialBody"/></param> /// <param name="body">A <see cref="CelestialBody"/></param>
public static double DistanceTo(this Vessel vessel, CelestialBody body) public static double DistanceTo(this Vessel vessel, CelestialBody body)
{ {
return (vessel.GetWorldPos3D() - body.position).magnitude; return (vessel.GetWorldPos3D() - body.position).magnitude;
} }
   
/// <summary> /// <summary>
/// Returns the distance between this IAntennaRelay and a Vessel /// Returns the distance between this IAntennaRelay and a Vessel
/// </summary> /// </summary>
/// <param name="relay">This <see cref="IAntennaRelay"/></param> /// <param name="relay">This <see cref="IAntennaRelay"/></param>
/// <param name="Vessel">A <see cref="Vessel"/></param> /// <param name="Vessel">A <see cref="Vessel"/></param>
public static double DistanceTo(this IAntennaRelay relay, Vessel Vessel) public static double DistanceTo(this AntennaRelay relay, Vessel Vessel)
{ {
return relay.vessel.DistanceTo(Vessel); return relay.vessel.DistanceTo(Vessel);
} }
   
/// <summary> /// <summary>
/// Returns the distance between this IAntennaRelay and a CelestialBody /// Returns the distance between this IAntennaRelay and a CelestialBody
/// </summary> /// </summary>
/// <param name="relay">This <see cref="IAntennaRelay"/></param> /// <param name="relay">This <see cref="IAntennaRelay"/></param>
/// <param name="body">A <see cref="CelestialBody"/></param> /// <param name="body">A <see cref="CelestialBody"/></param>
public static double DistanceTo(this IAntennaRelay relay, CelestialBody body) public static double DistanceTo(this AntennaRelay relay, CelestialBody body)
{ {
return relay.vessel.DistanceTo(body); return relay.vessel.DistanceTo(body);
} }
   
/// <summary> /// <summary>
/// Returns the distance between this IAntennaRelay and another IAntennaRelay /// Returns the distance between this IAntennaRelay and another IAntennaRelay
/// </summary> /// </summary>
/// <param name="relayOne">This <see cref="IAntennaRelay"/></param> /// <param name="relayOne">This <see cref="IAntennaRelay"/></param>
/// <param name="relayTwo">Another <see cref="IAntennaRelay"/></param> /// <param name="relayTwo">Another <see cref="IAntennaRelay"/></param>
public static double DistanceTo(this IAntennaRelay relayOne, IAntennaRelay relayTwo) public static double DistanceTo(this AntennaRelay relayOne, IAntennaRelay relayTwo)
{ {
return relayOne.DistanceTo(relayTwo.vessel); return relayOne.DistanceTo(relayTwo.vessel);
} }
   
/// <summary> /// <summary>
/// Returns all of the PartModules or ProtoPartModuleSnapshots implementing IAntennaRelay in this Vessel. /// Returns all of the PartModules or ProtoPartModuleSnapshots implementing IAntennaRelay in this Vessel.
/// </summary> /// </summary>
/// <param name="vessel">This <see cref="Vessel"/></param> /// <param name="vessel">This <see cref="Vessel"/></param>
public static IEnumerable<IAntennaRelay> GetAntennaRelays (this Vessel vessel) public static IEnumerable<IAntennaRelay> GetAntennaRelays (this Vessel vessel)
{ {
Tools.PostDebugMessage(string.Format( Tools.PostDebugMessage(string.Format(
"{0}: Getting antenna relays from vessel {1}.", "{0}: Getting antenna relays from vessel {1}.",
"IAntennaRelay", "IAntennaRelay",
vessel.name vessel.name
)); ));
   
List<IAntennaRelay> Transmitters; List<IAntennaRelay> Transmitters;
   
// If the vessel is loaded, we can fetch modules implementing IAntennaRelay directly. // If the vessel is loaded, we can fetch modules implementing IAntennaRelay directly.
if (vessel.loaded) { if (vessel.loaded) {
Tools.PostDebugMessage(string.Format( Tools.PostDebugMessage(string.Format(
"{0}: vessel {1} is loaded.", "{0}: vessel {1} is loaded.",
"IAntennaRelay", "IAntennaRelay",
vessel.name vessel.name
)); ));
   
// Gets a list of PartModules implementing IAntennaRelay // Gets a list of PartModules implementing IAntennaRelay
Transmitters = vessel.Parts Transmitters = vessel.Parts
.SelectMany (p => p.Modules.OfType<IAntennaRelay> ()) .SelectMany (p => p.Modules.OfType<IAntennaRelay> ())
.ToList(); .ToList();
} }
// If the vessel is not loaded, we need to find ProtoPartModuleSnapshots with a true IsAntenna field. // If the vessel is not loaded, we need to find ProtoPartModuleSnapshots with a true IsAntenna field.
else else
{ {
Tools.PostDebugMessage(string.Format( Tools.PostDebugMessage(string.Format(
"{0}: vessel {1} is not loaded.", "{0}: vessel {1} is not loaded.",
"IAntennaRelay", "IAntennaRelay",
vessel.name vessel.name
)); ));
   
Transmitters = new List<IAntennaRelay>(); Transmitters = new List<IAntennaRelay>();
   
// Loop through the ProtoPartModuleSnapshots in this Vessel // Loop through the ProtoPartModuleSnapshots in this Vessel
foreach (ProtoPartModuleSnapshot ms in vessel.protoVessel.protoPartSnapshots.SelectMany(ps => ps.modules)) foreach (ProtoPartModuleSnapshot ms in vessel.protoVessel.protoPartSnapshots.SelectMany(ps => ps.modules))
{ {
// If they are antennas... // If they are antennas...
if (ms.IsAntenna()) if (ms.IsAntenna())
{ {
// ...add a new ProtoAntennaRelay wrapper to the list. // ...add a new ProtoAntennaRelay wrapper to the list.
Transmitters.Add(new ProtoAntennaRelay(ms, vessel)); Transmitters.Add(new ProtoAntennaRelay(ms, vessel));
} }
} }
} }
   
Tools.PostDebugMessage(string.Format( Tools.PostDebugMessage(string.Format(
"{0}: vessel {1} has {2} transmitters.", "{0}: vessel {1} has {2} transmitters.",
"IAntennaRelay", "IAntennaRelay",
vessel.name, vessel.name,
Transmitters.Count Transmitters.Count
)); ));
   
// Return the list of IAntennaRelays // Return the list of IAntennaRelays
return Transmitters; return Transmitters;
} }
   
// Returns true if this PartModule contains a True IsAntenna field, false otherwise. // Returns true if this PartModule contains a True IsAntenna field, false otherwise.
public static bool IsAntenna (this PartModule module) public static bool IsAntenna (this PartModule module)
{ {
return module.Fields.GetValue<bool> ("IsAntenna"); return module.Fields.GetValue<bool> ("IsAntenna");
} }
   
// Returns true if this ProtoPartModuleSnapshot contains a persistent True IsAntenna field, false otherwise // Returns true if this ProtoPartModuleSnapshot contains a persistent True IsAntenna field, false otherwise
public static bool IsAntenna(this ProtoPartModuleSnapshot protomodule) public static bool IsAntenna(this ProtoPartModuleSnapshot protomodule)
{ {
bool result; bool result;
   
return Boolean.TryParse (protomodule.moduleValues.GetValue ("IsAntenna") ?? "False", out result) return Boolean.TryParse (protomodule.moduleValues.GetValue ("IsAntenna") ?? "False", out result)
? result : false; ? result : false;
} }
} }
} }