From daecd85baaa4f98090668897b0774776e2792e50 Mon Sep 17 00:00:00 2001 From: gotmachine <24925209+gotmachine@users.noreply.github.com> Date: Wed, 1 Jun 2022 13:16:48 +0200 Subject: [PATCH] ScatterDistribution : Fix incorrect terrain scatter distribution when a partial longitude range is defined in the PQSLandControl definition, see issue #41 --- .../KSPCommunityFixes.version | 2 +- GameData/KSPCommunityFixes/Settings.cfg | 4 ++ .../BugFixes/ScatterDistribution.cs | 58 +++++++++++++++++++ KSPCommunityFixes/KSPCommunityFixes.csproj | 1 + KSPCommunityFixes/Properties/AssemblyInfo.cs | 4 +- README.md | 4 ++ 6 files changed, 70 insertions(+), 3 deletions(-) create mode 100644 KSPCommunityFixes/BugFixes/ScatterDistribution.cs diff --git a/GameData/KSPCommunityFixes/KSPCommunityFixes.version b/GameData/KSPCommunityFixes/KSPCommunityFixes.version index 7462bbf..bc5214b 100644 --- a/GameData/KSPCommunityFixes/KSPCommunityFixes.version +++ b/GameData/KSPCommunityFixes/KSPCommunityFixes.version @@ -2,7 +2,7 @@ "NAME": "KSPCommunityFixes", "URL": "https://raw.githubusercontent.com/KSPModdingLibs/KSPCommunityFixes/master/GameData/KSPCommunityFixes/KSPCommunityFixes.version", "DOWNLOAD": "https://github.com/KSPModdingLibs/KSPCommunityFixes/releases", - "VERSION": {"MAJOR": 1, "MINOR": 14, "PATCH": 1, "BUILD": 0}, + "VERSION": {"MAJOR": 1, "MINOR": 15, "PATCH": 0, "BUILD": 0}, "KSP_VERSION": {"MAJOR": 1, "MINOR": 12, "PATCH": 3}, "KSP_VERSION_MIN": {"MAJOR": 1, "MINOR": 8, "PATCH": 0}, "KSP_VERSION_MAX": {"MAJOR": 1, "MINOR": 12, "PATCH": 3} diff --git a/GameData/KSPCommunityFixes/Settings.cfg b/GameData/KSPCommunityFixes/Settings.cfg index 63b88d7..05a273e 100644 --- a/GameData/KSPCommunityFixes/Settings.cfg +++ b/GameData/KSPCommunityFixes/Settings.cfg @@ -89,6 +89,10 @@ KSP_COMMUNITY_FIXES // attachable only part while the editor is empty. PartListTooltipIconSpin = true + // Fix incorrect terrain scatter distribution when a partial longitude range is defined in the + // PQSLandControl definition. + ScatterDistribution = true + GetPotentialTorqueFixes = true BetterSAS = true diff --git a/KSPCommunityFixes/BugFixes/ScatterDistribution.cs b/KSPCommunityFixes/BugFixes/ScatterDistribution.cs new file mode 100644 index 0000000..69f4bc4 --- /dev/null +++ b/KSPCommunityFixes/BugFixes/ScatterDistribution.cs @@ -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 patches) + { + patches.Add(new PatchInfo( + PatchMethodType.Transpiler, + AccessTools.Method(typeof(PQSLandControl), nameof(PQSLandControl.OnVertexBuildHeight)), + this)); + } + + static IEnumerable PQSLandControl_OnVertexBuildHeight_Transpiler(IEnumerable 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 code = new List(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; + } + + /// + /// 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. + /// + static double GetLongitudeFromSX(PQS sphere) + { + if (sphere.sx < 0.0) + return sphere.sx + 1.0; + + return sphere.sx; + } + } +} diff --git a/KSPCommunityFixes/KSPCommunityFixes.csproj b/KSPCommunityFixes/KSPCommunityFixes.csproj index 2d88ecd..931e1f8 100644 --- a/KSPCommunityFixes/KSPCommunityFixes.csproj +++ b/KSPCommunityFixes/KSPCommunityFixes.csproj @@ -104,6 +104,7 @@ + diff --git a/KSPCommunityFixes/Properties/AssemblyInfo.cs b/KSPCommunityFixes/Properties/AssemblyInfo.cs index bd0b607..23ac303 100644 --- a/KSPCommunityFixes/Properties/AssemblyInfo.cs +++ b/KSPCommunityFixes/Properties/AssemblyInfo.cs @@ -29,5 +29,5 @@ // Build Number // Revision // -[assembly: AssemblyVersion("1.14.1.0")] -[assembly: AssemblyFileVersion("1.14.1.0")] +[assembly: AssemblyVersion("1.15.0.0")] +[assembly: AssemblyFileVersion("1.15.0.0")] diff --git a/README.md b/README.md index ad4bc08..9609e73 100644 --- a/README.md +++ b/README.md @@ -57,6 +57,7 @@ User options are available from the "ESC" in-game settings menu :