Skip to content

Commit 78a62a9

Browse files
authored
Fix #251: Fix stock code that fails to limit timewarp when approaching a SOI transition (#259)
- Fix warp rate not being always limited correctly when approaching SOI transitions
1 parent 206ede2 commit 78a62a9

File tree

3 files changed

+52
-0
lines changed

3 files changed

+52
-0
lines changed

GameData/KSPCommunityFixes/Settings.cfg

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,11 @@ KSP_COMMUNITY_FIXES
225225
// Fix loading of drag cubes without a name failing with an IndexOutOfRangeException
226226
DragCubeLoadException = true
227227
228+
// Fix the stock code that tries to reduce timewarp when approaching a SOI transition.
229+
// Jumping to high warp levels with a SOI change on your trajectory could often warp through
230+
// the encounter entirely, and even through bodies.
231+
TimeWarpBodyCollision = true
232+
228233
// ##########################
229234
// Obsolete bugfixes
230235
// ##########################
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
using HarmonyLib;
2+
using System;
3+
using System.Collections.Generic;
4+
using UnityEngine;
5+
using static TimeWarp;
6+
7+
namespace KSPCommunityFixes.BugFixes
8+
{
9+
class TimeWarpBodyCollision : BasePatch
10+
{
11+
protected override Version VersionMin => new Version(1, 12, 0);
12+
13+
protected override void ApplyPatches(List<PatchInfo> patches)
14+
{
15+
patches.Add(new PatchInfo(
16+
PatchMethodType.Prefix,
17+
AccessTools.Method(typeof(TimeWarp), nameof(TimeWarp.ClampRateToOrbitTransitions)),
18+
this));
19+
}
20+
21+
static bool TimeWarp_ClampRateToOrbitTransitions_Prefix(TimeWarp __instance, int rate, Orbit obt, int maxAllowedSOITransitionRate, int secondsBeforeSOItransition, out int __result)
22+
{
23+
__result = rate;
24+
25+
// the stock version of this method is designed to aid in ramping down timewarp when you approach a SOI change.
26+
// it fails to limit the warp rate if you suddenly jump to a very high warp rate while you have an SOI change in your trajectory
27+
// - doing this will often warp you all the way through the SOI and sometimes even through the body.
28+
// Instead, we treat the SOI transition like a "warp to here" point and use the existing logic for limiting the warp rate to get to that point
29+
if (obt.patchEndTransition != Orbit.PatchTransitionType.FINAL && rate > maxAllowedSOITransitionRate)
30+
{
31+
double warpDeltaTime = obt.EndUT - Planetarium.GetUniversalTime() - secondsBeforeSOItransition;
32+
__instance.getMaxWarpRateForTravel(warpDeltaTime, 1, 4, out var rateIdx);
33+
if (rate < rateIdx)
34+
{
35+
__result = rate;
36+
}
37+
else
38+
{
39+
__result = Math.Max(rateIdx, maxAllowedSOITransitionRate);
40+
}
41+
}
42+
43+
return false;
44+
}
45+
}
46+
}

KSPCommunityFixes/KSPCommunityFixes.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@
139139
<Compile Include="BugFixes\ReRootPreserveSurfaceAttach.cs" />
140140
<Compile Include="BugFixes\RespawnDeadKerbals.cs" />
141141
<Compile Include="BugFixes\ThumbnailSpotlight.cs" />
142+
<Compile Include="BugFixes\TimeWarpBodyCollision.cs" />
142143
<Compile Include="BugFixes\TimeWarpOrbitShift.cs" />
143144
<Compile Include="BugFixes\UpgradeBugs.cs" />
144145
<Compile Include="BugFixes\PartTooltipUpgradesApplyToSubstituteParts.cs" />

0 commit comments

Comments
 (0)