Skip to content

Instantly share code, notes, and snippets.

@artwhaley
Last active May 22, 2017 09:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save artwhaley/b5d1770fd6a0389920620a6a17bd2166 to your computer and use it in GitHub Desktop.
Save artwhaley/b5d1770fd6a0389920620a6a17bd2166 to your computer and use it in GitHub Desktop.
using System;
using UnityEngine;
using KRPC.Service.Attributes;
using KRPC.Service;
using KRPC.SpaceCenter.Services;
using System.Reflection;
namespace mjtest
{
public class addon //little class that invokes the initializer in the wrapper.
{
[KSPAddon(KSPAddon.Startup.Flight, false)]
public sealed class Addon : MonoBehaviour
{
public void Start()
{
MJWrapper.Initialize();
}
}
}
public class MJWrapper
{
public static System.Type CoreType; //holds the mechjeb core type
public static System.Object core; //holds the core itself
public static bool Initialized = false; //keeps from reinitializing
public static Vessel vessel { get { return FlightGlobals.ActiveVessel; } } //shortcut to grab active vessel
public static bool Initialize() //Startup function to grab the core type and core
{
if (Initialized)
return true;
AssemblyLoader.loadedAssemblies.TypeOperation(t =>{if (t.FullName == "MuMech.MechJebCore"){CoreType = t;}});
if (CoreType == null) {return false;}
if (!GetCore()){ return false;}
Initialized = true;
return true;
}
public static bool GetCore()
{
foreach (Part p in vessel.parts)
{
foreach (PartModule module in p.Modules)
{
if (module.GetType() == CoreType)
{
core = module;
return true;
}
}
}
return false;
}
public static System.Type FindMechJebModule(string module) // Used to load mechjeb modules.
{
Type type = null;
AssemblyLoader.loadedAssemblies.TypeOperation(t =>
{
if (t.FullName == module)
{
type = t;
}
});
return type;
}
#region Public Functions
public static Vector3d Circularize(double UT) //This part works great- grabbing a static method.
{
System.Type OrbitalManeuverCalculatorType = FindMechJebModule("MuMech.OrbitalManeuverCalculator");
MethodInfo MPMethod = OrbitalManeuverCalculatorType.GetMethod("DeltaVToCircularize", BindingFlags.Public | BindingFlags.Static);
return (Vector3d)MPMethod.Invoke(null, new object[] { vessel.orbit, UT });
}
public static void ExecuteOne()
{
}
public static bool ExecStatus
{
get {return true; } //Placeholder to make this compile! Obviously doesn't work!
}
#endregion
}
[KRPCService(GameScene = GameScene.All)]
public static class MJTest //This is the static method that actually contains the KRPC service. I'm good on this part.
{
[KRPCProcedure]
public static Node mpCircAtUT(double UT)
{
Vessel v = KRPC.SpaceCenter.Services.SpaceCenter.ActiveVessel.InternalVessel;
Vector3d deltav = MJWrapper.Circularize(UT);
Vector3d nodeDV = v.orbit.DeltaVToManeuverNodeCoordinates(UT, deltav);
ManeuverNode n = v.patchedConicSolver.AddManeuverNode(UT);
n.DeltaV = nodeDV;
v.patchedConicSolver.UpdateFlightPlan();
return new Node(v, n);
}
[KRPCProperty]
public static bool ExecutorStatus
{
get
{
return MJWrapper.ExecStatus;
}
}
[KRPCProcedure]
public static void ExecuteNextNode()
{
MJWrapper.ExecuteOne();
}
}
public static class Extensions // A few borrowed Mechjeb vessel and orbit extensions
{
public static Vector3d DeltaVToManeuverNodeCoordinates(this Orbit o, double UT, Vector3d dV)
{
return new Vector3d(Vector3d.Dot(o.RadialPlus(UT), dV),
Vector3d.Dot(-o.NormalPlus(UT), dV),
Vector3d.Dot(o.Prograde(UT), dV));
}
//normalized vector pointing radially outward and perpendicular to prograde
public static Vector3d RadialPlus(this Orbit o, double UT)
{
return Vector3d.Exclude(o.Prograde(UT), o.Up(UT)).normalized;
}
//another name for the orbit normal; this form makes it look like the other directions
public static Vector3d NormalPlus(this Orbit o, double UT)
{
return o.SwappedOrbitNormal();
}
public static Vector3d Prograde(this Orbit o, double UT)
{
return o.SwappedOrbitalVelocityAtUT(UT).normalized;
}
public static Vector3d SwappedOrbitalVelocityAtUT(this Orbit o, double UT)
{
return SwapYZ(o.getOrbitalVelocityAtUT(UT));
}
public static Vector3d SwapYZ(Vector3d v)
{
Vector3d r = new Vector3d();
r.x = v.x;
r.y = v.z;
r.z = v.y;
return r;
}
//normalized vector pointing radially outward from the planet
public static Vector3d Up(this Orbit o, double UT)
{
return o.SwappedRelativePositionAtUT(UT).normalized;
}
//position relative to the primary
public static Vector3d SwappedRelativePositionAtUT(this Orbit o, double UT)
{
return SwapYZ(o.getRelativePositionAtUT(UT));
}
public static Vector3d SwappedOrbitNormal(this Orbit o)
{
return -SwapYZ(o.GetOrbitNormal()).normalized;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment