Revert "A bunch of stuff trying to use prefabs and protos instead of relying on"
[AntennaRange.git] / Extensions.cs
blob:a/Extensions.cs -> blob:b/Extensions.cs
// AntennaRange © 2014 toadicus // AntennaRange © 2014 toadicus
// //
// AntennaRange provides incentive and requirements for the use of the various antenna parts. // AntennaRange provides incentive and requirements for the use of the various antenna parts.
// Nominally, the breakdown is as follows: // Nominally, the breakdown is as follows:
// //
// Communotron 16 - Suitable up to Kerbalsynchronous Orbit // Communotron 16 - Suitable up to Kerbalsynchronous Orbit
// Comms DTS-M1 - Suitable throughout the Kerbin subsystem // Comms DTS-M1 - Suitable throughout the Kerbin subsystem
// Communotron 88-88 - Suitable throughout the Kerbol system. // 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 // This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License. To view a
// copy of this license, visit http://creativecommons.org/licenses/by-nc-sa/3.0/ // copy of this license, visit http://creativecommons.org/licenses/by-nc-sa/3.0/
// //
// This software uses the ModuleManager library © 2013 ialdabaoth, used under a Creative Commons Attribution-ShareAlike // This software uses the ModuleManager library © 2013 ialdabaoth, used under a Creative Commons Attribution-ShareAlike
// 3.0 Uported License. // 3.0 Uported License.
// //
// This software uses code from the MuMechLib library, © 2013 r4m0n, used under the GNU GPL version 3. // This software uses code from the MuMechLib library, © 2013 r4m0n, used under the GNU GPL version 3.
   
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 AntennaRelay 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 AntennaRelay 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 AntennaRelay 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( return RelayDatabase.Instance[vessel].Values.ToList();
"{0}: Getting antenna relays from vessel {1}.", }
"IAntennaRelay",  
vessel.name  
));  
   
List<IAntennaRelay> Transmitters;  
   
// If the vessel is loaded, we can fetch modules implementing IAntennaRelay directly.  
if (vessel.loaded) {  
Tools.PostDebugMessage(string.Format(  
"{0}: vessel {1} is loaded.",  
"IAntennaRelay",  
vessel.name  
));  
   
// Gets a list of PartModules implementing IAntennaRelay  
Transmitters = vessel.Parts  
.SelectMany (p => p.Modules.OfType<IAntennaRelay> ())  
.ToList();  
}  
// If the vessel is not loaded, we need to find ProtoPartModuleSnapshots with a true IsAntenna field.  
else  
{  
Tools.PostDebugMessage(string.Format(  
"{0}: vessel {1} is not loaded.",  
"IAntennaRelay",  
vessel.name  
));  
   
Transmitters = new List<IAntennaRelay>();  
   
// Loop through the ProtoPartModuleSnapshots in this Vessel  
foreach (ProtoPartSnapshot pps in vessel.protoVessel.protoPartSnapshots)  
{  
Transmitters.AddRange(  
PartLoader.getPartInfoByName(pps.partName)  
.partPrefab  
.Modules  
.OfType<IAntennaRelay>()  
);  
}  
}  
   
Tools.PostDebugMessage(string.Format(  
"{0}: vessel {1} has {2} transmitters.",  
"IAntennaRelay",  
vessel.name,  
Transmitters.Count  
));  
   
// Return the list of IAntennaRelays  
return Transmitters;  
}  
} }
} }