Skip to content

Commit

Permalink
ScatterDistribution : Fix incorrect terrain scatter distribution when…
Browse files Browse the repository at this point in the history
… a partial longitude range is defined in the PQSLandControl definition, see issue #41
  • Loading branch information
gotmachine committed Jan 30, 2023
1 parent 87d7e9f commit daecd85
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 3 deletions.
2 changes: 1 addition & 1 deletion GameData/KSPCommunityFixes/KSPCommunityFixes.version
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
4 changes: 4 additions & 0 deletions GameData/KSPCommunityFixes/Settings.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
58 changes: 58 additions & 0 deletions KSPCommunityFixes/BugFixes/ScatterDistribution.cs
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;
}
}
}
1 change: 1 addition & 0 deletions KSPCommunityFixes/KSPCommunityFixes.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@
<Compile Include="BugFixes\PAWItemOrder.cs" />
<Compile Include="BugFixes\RoboticsDrift.cs" />
<Compile Include="BugFixes\ROCValidationOOR.cs" />
<Compile Include="BugFixes\ScatterDistribution.cs" />
<Compile Include="BugFixes\StockAlarmDescPreserveLineBreak.cs" />
<Compile Include="Internal\EditorPhysics.cs" />
<Compile Include="Modding\BaseFieldListUseFieldHost.cs" />
Expand Down
4 changes: 2 additions & 2 deletions KSPCommunityFixes/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ User options are available from the "ESC" in-game settings menu :<br/><img src="
- **[DeltaVHideWhenDisabled](https://github.com/KSPModdingLibs/KSPCommunityFixes/pull/31)** [KSP 1.12.0 - 1.12.3]<br/>Hide the stock stage delta-v UI elements and navball extended burn info when `DELTAV_CALCULATIONS_ENABLED` and `DELTAV_APP_ENABLED` are disabled by another mod or in the KSP `settings.cfg` file.
- **AsteroidSpawnerUniqueFlightId** [KSP 1.8.0 - 1.12.3]<br/>Fix the asteroid/comet spawner generating non-unique `Part.flightId` identifiers. This has a few minor side effects in stock (mainly incorrect science bonuses), but this field is heavily relied upon by various mods and this can cause major issues for them.
- **PartListTooltipIconSpin** [KSP 1.8.0 - 1.12.3]<br/> Fix editor tooltip part icons not spinning anymore after hovering on a greyed out surface attachable only part while the editor is empty.
- **[ScatterDistribution](https://github.com/KSPModdingLibs/KSPCommunityFixes/issues/41)** [KSP 1.8.0 - 1.12.3]<br/>Fix incorrect terrain scatter distribution when a partial longitude range is defined in the PQSLandControl definition.

#### Quality of Life tweaks

Expand Down Expand Up @@ -123,6 +124,9 @@ If doing so in the `Debug` configuration and if your KSP install is modified to

### Changelog

##### 1.15.0
- New KSP bugfix : [ScatterDistribution](https://github.com/KSPModdingLibs/KSPCommunityFixes/issues/41) (credit to @R-T-B)

##### 1.14.1
- Fix KSPCF [issue #39](https://github.com/KSPModdingLibs/KSPCommunityFixes/issues/39) : AltimeterHorizontalPosition patch causes state inconsistencies with vessel filters.
- Fix KSPCF [issue #40](https://github.com/KSPModdingLibs/KSPCommunityFixes/issues/40) : UIFloatEditNumericInput patch breaking IR custom FloatEdit control. Don't alter the control prefab list, instead return our custom prefab on the fly in the KSP methods searching the prefab list.
Expand Down

0 comments on commit daecd85

Please sign in to comment.