Skip to content

Commit

Permalink
Fix roughmap blending, make sure height cache is threadsafe.
Browse files Browse the repository at this point in the history
  • Loading branch information
billw2012 committed Apr 5, 2021
1 parent 467a781 commit f4b17e5
Show file tree
Hide file tree
Showing 7 changed files with 307 additions and 179 deletions.
23 changes: 15 additions & 8 deletions BetterContinents.BetterContinentsSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public struct BetterContinentsSettings

public bool OverrideBiomes => this.Biomemap != null;
public bool UseSpawnmap => this.Spawnmap != null;
public bool UseRoughmap => this.Roughmap != null && this.RoughmapBlend > 0;
public bool UseRoughmap => this.Roughmap != null;

public static BetterContinentsSettings Create(long worldUId)
{
Expand Down Expand Up @@ -110,8 +110,6 @@ public BetterContinentsSettings Clean()
return copy;
}

public static string CleanPath(string path) => path?.Replace("\\\"", "").Replace("\"", "").Trim();

private static string GetPath(string projectDir, string projectDirFileName, string defaultFileName)
{
if (!string.IsNullOrEmpty(projectDir))
Expand Down Expand Up @@ -140,9 +138,7 @@ private static string GetPath(string projectDir, string projectDirFileName, stri
private static string BiomemapPath(string defaultFilename, string projectDir) => GetPath(projectDir, BiomemapFilename, defaultFilename);
private static string SpawnmapPath(string defaultFilename, string projectDir) => GetPath(projectDir, SpawnmapFilename, defaultFilename);
private static string RoughmapPath(string defaultFilename, string projectDir) => GetPath(projectDir, RoughmapFilename, defaultFilename);
private static string FlatmapPath(string defaultFilename, string projectDir) => ConfigUseRoughInvertedForFlat.Value
? null
: GetPath(projectDir, FlatmapFilename, defaultFilename);
private static string FlatmapPath(string defaultFilename, string projectDir) => GetPath(projectDir, FlatmapFilename, defaultFilename);
private static string ForestmapPath(string defaultFilename, string projectDir) => GetPath(projectDir, ForestmapFilename, defaultFilename);

private void InitSettings(long worldUId, bool enabled)
Expand Down Expand Up @@ -224,7 +220,7 @@ private void InitSettings(long worldUId, bool enabled)
}
}

var flatmapPath = FlatmapPath(ConfigFlatmapFile.Value, ConfigMapSourceDir.Value);
var flatmapPath = ConfigUseRoughInvertedForFlat.Value ? null : FlatmapPath(ConfigFlatmapFile.Value, ConfigMapSourceDir.Value);
if (ConfigUseRoughInvertedForFlat.Value && Roughmap != null ||
!ConfigUseRoughInvertedForFlat.Value && !string.IsNullOrEmpty(flatmapPath))
{
Expand Down Expand Up @@ -306,6 +302,8 @@ public void SetHeightmapPath(string path, string projectDir = null)
Heightmap = null;
}
}
public void DisableHeightmap() => Heightmap = null;

public void SetBiomemapPath(string path, string projectDir = null)
{
var finalPath = BiomemapPath(path, projectDir);
Expand All @@ -322,6 +320,8 @@ public void SetBiomemapPath(string path, string projectDir = null)
Biomemap = null;
}
}
public void DisableBiomemap() => Biomemap = null;

public void SetSpawnmapPath(string path, string projectDir = null)
{
var finalPath = SpawnmapPath(path, projectDir);
Expand All @@ -338,6 +338,8 @@ public void SetSpawnmapPath(string path, string projectDir = null)
Spawnmap = null;
}
}
public void DisableSpawnmap() => Spawnmap = null;

public void SetRoughmapPath(string path, string projectDir = null)
{
var finalPath = RoughmapPath(path, projectDir);
Expand All @@ -354,6 +356,8 @@ public void SetRoughmapPath(string path, string projectDir = null)
Roughmap = null;
}
}
public void DisableRoughmap() => Roughmap = null;

public void SetFlatmapPath(string path, string projectDir = null)
{
var finalPath = FlatmapPath(path, projectDir);
Expand All @@ -370,6 +374,8 @@ public void SetFlatmapPath(string path, string projectDir = null)
Flatmap = null;
}
}
public void DisableFlatmap() => Flatmap = null;

public void SetForestmapPath(string path, string projectDir = null)
{
var finalPath = ForestmapPath(path, projectDir);
Expand All @@ -386,6 +392,7 @@ public void SetForestmapPath(string path, string projectDir = null)
Forestmap = null;
}
}
public void DisableForestmap() => Forestmap = null;

private static float FeatureScaleCurve(float x) => ScaleRange(Gamma(x, 0.726965071031f), 0.2f, 3f);

Expand Down Expand Up @@ -761,7 +768,7 @@ public float ApplyHeightmap(float x, float y, float height)

public float ApplyRoughmap(float x, float y, float smoothHeight, float roughHeight)
{
if (this.Roughmap == null || this.RoughmapBlend == 0)
if (this.Roughmap == null)
{
return roughHeight;
}
Expand Down
28 changes: 16 additions & 12 deletions BetterContinents.WorldGeneratorPatch.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using HarmonyLib;
using UnityEngine;
Expand All @@ -12,8 +13,8 @@ public partial class BetterContinents
public class WorldGeneratorPatch
{
// The base map x, y coordinates in 0..1 range
private static Dictionary<Vector2, float> cachedHeights;
private static bool cacheEnabled = true;
private static ConcurrentDictionary<Vector2, float> cachedHeights;
private static int cacheEnabled = 1;

private static readonly string[] TreePrefixes =
{
Expand All @@ -35,7 +36,7 @@ public class WorldGeneratorPatch
[HarmonyPrefix, HarmonyPatch(nameof(WorldGenerator.Initialize))]
private static void InitializePrefix(World world)
{
cachedHeights = new Dictionary<Vector2, float>(100000);
cachedHeights = new ConcurrentDictionary<Vector2, float>(1, 100100);

if(Settings.EnabledForThisWorld && !world.m_menu && Settings.ForestFactorOverrideAllTrees && ZoneSystem.instance != null)
{
Expand All @@ -53,11 +54,14 @@ private static void InitializePrefix(World world)
}
}

public static void DisableCache() => cacheEnabled = false;
public static void DisableCache() => cacheEnabled--;
public static void EnableCache()
{
cacheEnabled = true;
cachedHeights.Clear();
cacheEnabled++;
if (cacheEnabled == 1)
{
cachedHeights.Clear();
}
}

// wx, wy are [-10500, 10500]
Expand All @@ -70,7 +74,7 @@ private static bool GetBaseHeightPrefix(ref float wx, ref float wy, bool menuTer
return true;
}

if (cacheEnabled && cachedHeights.TryGetValue(new Vector2(wx, wy), out __result))
if (cacheEnabled == 1 && cachedHeights.TryGetValue(new Vector2(wx, wy), out __result))
{
return false;
}
Expand Down Expand Up @@ -99,16 +103,16 @@ private static void GetBaseHeightPostfix(float wx, float wy, float ___m_offset0,
return;
}

if (cacheEnabled)
if (cacheEnabled == 1)
{
if (cachedHeights.Count >= 100000)
{
// Can't easily do LRU, so we will just clear it entirely
cachedHeights.Clear();
}

// Do this AFTER clearing so we have it available below
cachedHeights[new Vector2(wx, wy)] = __result;
// Do this AFTER clearing so we have the latest value available (hopefully, concurrency means its still not guaranteed)
cachedHeights.TryAdd(new Vector2(wx, wy), __result);
}
}

Expand All @@ -125,7 +129,7 @@ private static void GetBiomeHeightPostfix(WorldGenerator __instance, float wx, f
}

float smoothHeight = GetBaseHeightMethod(__instance, wx, wy, false) * 200f;
__result = Settings.ApplyRoughmap(WorldX(wx), WorldY(wy), smoothHeight, __result);
__result = Settings.ApplyRoughmap(NormalizedX(wx), NormalizedY(wy), smoothHeight, __result);
}

private static float GetBaseHeightV1(float wx, float wy, float ___m_offset0, float ___m_offset1, float ___m_minMountainDistance)
Expand Down
2 changes: 2 additions & 0 deletions BetterContinents.ZNetPatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -495,5 +495,7 @@ byte[] ArraySlice(byte[] source, int offset, int length)
RPC_PeerInfo(instance, rpc, pkg);
}
}

public static string CleanPath(string path) => path?.Replace("\\\"", "").Replace("\"", "").Trim();
}
}
4 changes: 4 additions & 0 deletions BetterContinents.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,13 @@ public partial class BetterContinents : BaseUnityPlugin
Settings.EnabledForThisWorld && ConfigDebugModeEnabled.Value;

public static BetterContinentsSettings Settings;

public static BetterContinents instance;

private void Awake()
{
instance = this;

// Cos why...
Application.SetStackTraceLogType(LogType.Log, StackTraceLogType.None);
// Application.SetStackTraceLogType(LogType.Warning, StackTraceLogType.None);
Expand Down
11 changes: 8 additions & 3 deletions BetterContinents.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,16 @@
<Private>False</Private>
</Reference>
<Reference Include="assembly_guiutils, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null">
<HintPath>..\..\..\..\..\Program Files (x86)\Steam\steamapps\common\Valheim\valheim_Data\Managed\assembly_guiutils.dll</HintPath>
<HintPath>..\..\..\..\..\Program Files (x86)\Steam\steamapps\common\Valheim-debug\valheim_Data\Managed\publicized_assemblies\assembly_guiutils_publicized.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="assembly_utils, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null">
<HintPath>..\..\..\..\..\Program Files (x86)\Steam\steamapps\common\Valheim\valheim_Data\Managed\assembly_utils.dll</HintPath>
<HintPath>..\..\..\..\..\Program Files (x86)\Steam\steamapps\common\Valheim-debug\valheim_Data\Managed\publicized_assemblies\assembly_utils_publicized.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="assembly_valheim, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null">
<HintPath>..\..\..\..\..\Program Files (x86)\Steam\steamapps\common\Valheim\valheim_Data\Managed\publicized_assemblies\assembly_valheim_publicized.dll</HintPath>
<HintPath>..\..\..\..\..\Program Files (x86)\Steam\steamapps\common\Valheim-debug\valheim_Data\Managed\publicized_assemblies\assembly_valheim_publicized.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="BepInEx, Version=5.4.8.0, Culture=neutral, PublicKeyToken=null">
<HintPath>lib\BepInEx\core\BepInEx.dll</HintPath>
Expand Down Expand Up @@ -218,6 +219,10 @@
<HintPath>..\..\..\..\..\Program Files (x86)\Steam\steamapps\common\Valheim\unstripped_managed\UnityEngine.UI.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="UnityEngine.UIModule, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null">
<HintPath>..\..\..\..\..\Program Files (x86)\Steam\steamapps\common\Valheim\unstripped_managed\UnityEngine.UIModule.dll</HintPath>
<Private>False</Private>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="BetterContinents.BetterContinentsSettings.cs" />
Expand Down
Loading

0 comments on commit f4b17e5

Please sign in to comment.