Skip to content

Commit

Permalink
Added fix for MapSO co-ordinate wrapping (#83)
Browse files Browse the repository at this point in the history
  • Loading branch information
RCrockford authored and gotmachine committed Jan 30, 2023
1 parent 8154120 commit d55f655
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 0 deletions.
4 changes: 4 additions & 0 deletions GameData/KSPCommunityFixes/Settings.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,10 @@ KSP_COMMUNITY_FIXES
// For this reason this defaults to off, since otherwise it would change
// stock gameplay.
StrategyDuration = false
// Fix biome and heightmap texture wrapping from pole to pole
// Avoids big spikes on the poles and incorrect biomes where the oles have disparate biomes.
MapSOCorrectWrapping = true
// Fix Admin Building not using HeadImage if that is defined for a Department but a kerbal prefab is not
DepartmentHeadImage = true
Expand Down
73 changes: 73 additions & 0 deletions KSPCommunityFixes/BugFixes/MapSOWrapping.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
using System;
using System.Collections.Generic;
using HarmonyLib;
using UnityEngine;

namespace KSPCommunityFixes
{
public class MapSOCorrectWrapping : BasePatch
{
protected override Version VersionMin => new Version(1, 10, 0);

protected override void ApplyPatches(List<PatchInfo> patches)
{
patches.Add(new PatchInfo(
PatchMethodType.Prefix,
AccessTools.Method(typeof(MapSO), nameof(MapSO.ConstructBilinearCoords), new Type[] { typeof(float), typeof(float) }),
this,
"MapSO_ConstructBilinearCoords_Float"));

patches.Add(new PatchInfo(
PatchMethodType.Prefix,
AccessTools.Method(typeof(MapSO), nameof(MapSO.ConstructBilinearCoords), new Type[] { typeof(double), typeof(double) }),
this,
"MapSO_ConstructBilinearCoords_Double"));
}

static bool MapSO_ConstructBilinearCoords_Float(MapSO __instance, float x, float y)
{
// X wraps around as it is longitude.
x = Mathf.Abs(x - Mathf.Floor(x));
__instance.centerX = x * __instance._width;
__instance.minX = Mathf.FloorToInt(__instance.centerX);
__instance.maxX = Mathf.CeilToInt(__instance.centerX);
__instance.midX = __instance.centerX - __instance.minX;
if (__instance.maxX == __instance._width)
__instance.maxX = 0;

// Y clamps as it is latitude and the poles don't wrap to each other.
y = Mathf.Clamp(y, 0, 0.99999f);
__instance.centerY = y * __instance._height;
__instance.minY = Mathf.FloorToInt(__instance.centerY);
__instance.maxY = Mathf.CeilToInt(__instance.centerY);
__instance.midY = __instance.centerY - __instance.minY;
if (__instance.maxY >= __instance._height)
__instance.maxY = __instance._height - 1;

return false;
}

static bool MapSO_ConstructBilinearCoords_Double(MapSO __instance, double x, double y)
{
// X wraps around as it is longitude.
x = Math.Abs(x - Math.Floor(x));
__instance.centerXD = x * __instance._width;
__instance.minX = (int)Math.Floor(__instance.centerXD);
__instance.maxX = (int)Math.Ceiling(__instance.centerXD);
__instance.midX = (float)__instance.centerXD - __instance.minX;
if (__instance.maxX == __instance._width)
__instance.maxX = 0;

// Y clamps as it is latitude and the poles don't wrap to each other.
y = Math.Min(Math.Max(y, 0), 0.99999);
__instance.centerYD = y * __instance._height;
__instance.minY = (int)Math.Floor(__instance.centerYD);
__instance.maxY = (int)Math.Ceiling(__instance.centerYD);
__instance.midY = (float)__instance.centerYD - __instance.minY;
if (__instance.maxY >= __instance._height)
__instance.maxY = __instance._height - 1;

return false;
}
}
}
1 change: 1 addition & 0 deletions KSPCommunityFixes/KSPCommunityFixes.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@
<ItemGroup>
<Compile Include="BasePatch.cs" />
<Compile Include="BugFixes\AsteroidInfiniteMining.cs" />
<Compile Include="BugFixes\MapSOWrapping.cs" />
<Compile Include="BugFixes\UpgradeBugs.cs" />
<Compile Include="BugFixes\PartTooltipUpgradesApplyToSubstituteParts.cs" />
<Compile Include="BugFixes\UpgradesApplyToPrefabs.cs" />
Expand Down

0 comments on commit d55f655

Please sign in to comment.