Skip to content

Commit

Permalink
Merge pull request #70 from NathanKell/StrategyDurations
Browse files Browse the repository at this point in the history
Fix Strategy Duration handling
  • Loading branch information
gotmachine committed Aug 5, 2022
2 parents 4b8c51e + 3b49080 commit 9edc6d3
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 0 deletions.
6 changes: 6 additions & 0 deletions GameData/KSPCommunityFixes/Settings.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,12 @@ KSP_COMMUNITY_FIXES
// Fix mass of comets not actually reducing when mining them, despite the PAW saying so.
CometMiningNotRemovingMass = true
// Fix Strategies not using Duration settings
// Note that many stock strategies do have these set, but the code was broken.
// For this reason this defaults to off, since otherwise it would change
// stock gameplay.
StrategyDuration = false
// ##########################
// Obsolete bugfixes
// ##########################
Expand Down
60 changes: 60 additions & 0 deletions KSPCommunityFixes/BugFixes/StrategyDuration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using System;
using System.Collections.Generic;
using HarmonyLib;
using Strategies;
using System.Reflection.Emit;

namespace KSPCommunityFixes.BugFixes
{
class StrategyDuration : BasePatch
{
protected override Version VersionMin => new Version(1, 8, 0);

protected override void ApplyPatches(List<PatchInfo> patches)
{
patches.Add(new PatchInfo(
PatchMethodType.Prefix,
AccessTools.PropertyGetter(typeof(Strategy), nameof(Strategy.LongestDuration)),
this, nameof(Strategy_LongestDuration)));

patches.Add(new PatchInfo(
PatchMethodType.Prefix,
AccessTools.PropertyGetter(typeof(Strategy), nameof(Strategy.LeastDuration)),
this, nameof(Strategy_LeastDuration)));

patches.Add(new PatchInfo(
PatchMethodType.Transpiler,
AccessTools.Method(typeof(Strategy), nameof(Strategy.CanBeDeactivated)),
this));
}

static bool Strategy_LongestDuration(Strategy __instance, ref double __result)
{
__result = __instance.FactorLerp(__instance.MinLongestDuration, __instance.MaxLongestDuration);
return false;
}

static bool Strategy_LeastDuration(Strategy __instance, ref double __result)
{
__result = __instance.FactorLerp(__instance.MinLeastDuration, __instance.MaxLeastDuration);
return false;
}

internal static IEnumerable<CodeInstruction> Strategy_CanBeActivated_Transpiler(IEnumerable<CodeInstruction> instructions)
{
List<CodeInstruction> code = new List<CodeInstruction>(instructions);
for (int i = 9; i < code.Count; ++i)
{
// We need to fix the inequality check for if ( dateActivated + LeastDuration < Planetarium.fetch.time )
// because that should be a > check.
if (code[i].opcode == OpCodes.Bge_Un_S)
{
code[i].opcode = OpCodes.Ble_Un_S;
break;
}
}

return code;
}
}
}
1 change: 1 addition & 0 deletions KSPCommunityFixes/KSPCommunityFixes.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@
<Compile Include="BugFixes\AsteroidInfiniteMining.cs" />
<Compile Include="BugFixes\CometMiningNotRemovingMass.cs" />
<Compile Include="BugFixes\EnginePlateAirstreamShieldedTopPart.cs" />
<Compile Include="BugFixes\StrategyDuration.cs" />
<Compile Include="Performance\AsteroidAndCometDrillCache.cs" />
<Compile Include="Performance\CommNetThrottling.cs" />
<Compile Include="Performance\DisableMapUpdateInFlight.cs" />
Expand Down

0 comments on commit 9edc6d3

Please sign in to comment.