-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ScatterDistribution : Fix incorrect terrain scatter distribution when…
… a partial longitude range is defined in the PQSLandControl definition, see issue #41
- Loading branch information
1 parent
87d7e9f
commit daecd85
Showing
6 changed files
with
70 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Reflection; | ||
using System.Reflection.Emit; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using HarmonyLib; | ||
using UnityEngine; | ||
|
||
namespace KSPCommunityFixes.BugFixes | ||
{ | ||
class ScatterDistribution : BasePatch | ||
{ | ||
protected override Version VersionMin => new Version(1, 8, 0); | ||
|
||
protected override void ApplyPatches(ref List<PatchInfo> patches) | ||
{ | ||
patches.Add(new PatchInfo( | ||
PatchMethodType.Transpiler, | ||
AccessTools.Method(typeof(PQSLandControl), nameof(PQSLandControl.OnVertexBuildHeight)), | ||
this)); | ||
} | ||
|
||
static IEnumerable<CodeInstruction> PQSLandControl_OnVertexBuildHeight_Transpiler(IEnumerable<CodeInstruction> instructions) | ||
{ | ||
FieldInfo PQSMod_sphere_field = AccessTools.Field(typeof(PQSMod), nameof(PQSMod.sphere)); | ||
FieldInfo PQS_sx_field = AccessTools.Field(typeof(PQS), nameof(PQS.sx)); | ||
MethodInfo GetLongitudeFromSX_method = AccessTools.Method(typeof(ScatterDistribution), nameof(ScatterDistribution.GetLongitudeFromSX)); | ||
|
||
List<CodeInstruction> code = new List<CodeInstruction>(instructions); | ||
|
||
for (int i = 0; i < code.Count - 1; i++) | ||
{ | ||
if (code[i].opcode == OpCodes.Ldfld && code[i].operand == PQSMod_sphere_field | ||
&& code[i + 1].opcode == OpCodes.Ldfld && code[i + 1].operand == PQS_sx_field) | ||
{ | ||
code[i + 1].opcode = OpCodes.Call; | ||
code[i + 1].operand = GetLongitudeFromSX_method; | ||
} | ||
} | ||
|
||
return code; | ||
} | ||
|
||
/// <summary> | ||
/// Transform the from the sx [-0.25, 0.75] longitude range convention where [-0.25, 0] maps to [270°, 360°] | ||
/// and [0, 0.75] maps to [0°, 270°] into a linear [0,1] longitude range. | ||
/// </summary> | ||
static double GetLongitudeFromSX(PQS sphere) | ||
{ | ||
if (sphere.sx < 0.0) | ||
return sphere.sx + 1.0; | ||
|
||
return sphere.sx; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters