Skip to content

Commit

Permalink
Add Fast FindModulesImplementing (and a const version), add FastRemov…
Browse files Browse the repository at this point in the history
…eAt, add Swap, move Principia bit in KSPUtils to ModUtils
  • Loading branch information
NathanKell committed Dec 4, 2023
1 parent 71348d0 commit 6abbd4f
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 16 deletions.
39 changes: 23 additions & 16 deletions Source/ROUtils/Utils/KSPUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,27 +47,34 @@ public static List<Type> GetAllLoadedTypes<T>(bool instantiableOnly = true)
return list;
}

public static double PrincipiaCorrectInclination(Orbit o)
public static List<T> FixedFindModulesImplementing<T>(this Part part) where T : class
{
if (ModUtils.IsPrincipiaInstalled && o.referenceBody != (FlightGlobals.currentMainBody ?? Planetarium.fetch.Home))
{
Vector3d polarAxis = o.referenceBody.BodyFrame.Z;
var listPM = FixedFindModulesImplementingConst<T>(part);
int c = listPM.Count;
var newList = new List<T>(c);
for (int i = 0; i < c; ++i)
newList.Add(listPM[i] as T);

double hSqrMag = o.h.sqrMagnitude;
if (hSqrMag == 0d)
{
return Math.Acos(Vector3d.Dot(polarAxis, o.pos) / o.pos.magnitude) * (180.0 / Math.PI);
}
else
return newList;
}

public static List<PartModule> FixedFindModulesImplementingConst<T>(this Part part) where T : class
{
Type typeFromHandle = typeof(T);
if (part.cachedModuleLists.TryGetValue(typeFromHandle, out var list))
return list;

list = new List<PartModule>();
for (int i = 0, ic = part.modules.Count; i < ic; ++i)
{
PartModule partModule = part.modules[i];
if (partModule is T)
{
Vector3d orbitZ = o.h / Math.Sqrt(hSqrMag);
return Math.Atan2((orbitZ - polarAxis).magnitude, (orbitZ + polarAxis).magnitude) * (2d * (180.0 / Math.PI));
list.Add(partModule);
}
}
else
{
return o.inclination;
}
part.cachedModuleLists[typeFromHandle] = list;
return list;
}

/// <summary>
Expand Down
14 changes: 14 additions & 0 deletions Source/ROUtils/Utils/MiscUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,20 @@ public static T Pop<T>(this List<T> list)
return val;
}

public static void Swap<T>(ref T a, ref T b)
{
T c = a;
a = b;
b = c;
}

public static void FastRemoveAt<T>(this List<T> list, int i)
{
int c = list.Count - 1;
list[i] = list[c];
list.RemoveAt(c);
}

public static Texture2D GetReadableCopy(this Texture2D texture)
{
Texture2D readable = new Texture2D(texture.width, texture.height);
Expand Down
23 changes: 23 additions & 0 deletions Source/ROUtils/Utils/ModUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,5 +98,28 @@ private static bool FindPrincipia()
_isPrincipiaInstalled = AssemblyLoader.loadedAssemblies.Any(a => a.name.Equals("ksp_plugin_adapter", StringComparison.OrdinalIgnoreCase));
return _isPrincipiaInstalled;
}

public static double PrincipiaCorrectInclination(Orbit o)
{
if (ModUtils.IsPrincipiaInstalled && o.referenceBody != (FlightGlobals.currentMainBody ?? Planetarium.fetch.Home))
{
Vector3d polarAxis = o.referenceBody.BodyFrame.Z;

double hSqrMag = o.h.sqrMagnitude;
if (hSqrMag == 0d)
{
return Math.Acos(Vector3d.Dot(polarAxis, o.pos) / o.pos.magnitude) * (180.0 / Math.PI);
}
else
{
Vector3d orbitZ = o.h / Math.Sqrt(hSqrMag);
return Math.Atan2((orbitZ - polarAxis).magnitude, (orbitZ + polarAxis).magnitude) * (2d * (180.0 / Math.PI));
}
}
else
{
return o.inclination;
}
}
}
}

0 comments on commit 6abbd4f

Please sign in to comment.