Skip to content

Commit

Permalink
Fully patch Vessel.SetRotation, patch VesselPrecalculate.SetLandedPos…
Browse files Browse the repository at this point in the history
…Rot for more perf.
  • Loading branch information
NathanKell committed Nov 22, 2023
1 parent af831b8 commit 3842fca
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 3 deletions.
22 changes: 19 additions & 3 deletions Source/Harmony/Vessel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,26 @@ internal static bool Prefix_SetRotation(Vessel __instance, Quaternion rotation,
return true;

vmr.StoreRotAndTime(rotation);
if (!setPos)
return true;
if (setPos)
{
vmr.SetPosRot(rotation, __instance.transform.position);
return false;
}

if (!__instance.loaded)
{
__instance.vesselTransform.rotation = rotation;
return false;
}

vmr.SetPosRot(rotation, __instance.transform.position);
var parts = __instance.parts;
// Have to iterate forwards to preserve hierarchy
int c = parts.Count;
for (int i = 0; i < c; ++i)
{
Part part = parts[i];
part.partTransform.rotation = rotation * part.orgRot;
}

return false;
}
Expand Down
33 changes: 33 additions & 0 deletions Source/Harmony/VesselPrecalculate.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using HarmonyLib;
using UnityEngine;

namespace RealismOverhaul.Harmony
{
[HarmonyPatch(typeof(VesselPrecalculate))]
internal class PatchVesselPrecalculate
{
[HarmonyPrefix]
[HarmonyPatch(nameof(VesselPrecalculate.SetLandedPosRot))]
internal static bool Prefix_SetLandedPosRot(VesselPrecalculate __instance)
{
Vessel vessel = __instance.vessel;
if (vessel.LandedOrSplashed && vessel.packed && __instance.railsSetPosRot)
{
__instance.worldSurfaceRot = vessel.mainBody.bodyTransform.rotation * vessel.srfRelRotation;
__instance.worldSurfacePos = vessel.mainBody.GetWorldSurfacePosition(vessel.latitude, vessel.longitude, vessel.altitude);
VesselModuleRotationRO vmr = VesselModuleRotationRO.GetModule(vessel);
if (vmr == null)
{
vessel.SetRotation(__instance.worldSurfaceRot, false);
vessel.SetPosition(__instance.worldSurfacePos, true);
}
else
{
vmr.StoreRotAndTime(__instance.worldSurfaceRot);
vmr.SetPosRot(__instance.worldSurfaceRot, __instance.worldSurfacePos);
}
}
return false;
}
}
}
1 change: 1 addition & 0 deletions Source/RealismOverhaul.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
<Compile Include="Harmony\ModuleEngines.cs" />
<Compile Include="Harmony\KSPUtil.cs" />
<Compile Include="Harmony\KSPWheel.cs" />
<Compile Include="Harmony\VesselPrecalculate.cs" />
<Compile Include="Harmony\Vessel.cs" />
<Compile Include="Harmony\PartLoader.cs" />
<Compile Include="Harmony\OrbitDriver.cs" />
Expand Down

0 comments on commit 3842fca

Please sign in to comment.