Skip to content

Commit

Permalink
Added heightmap override all and heightmap mask
Browse files Browse the repository at this point in the history
  • Loading branch information
billw2012 committed Apr 8, 2021
1 parent ec3d4a4 commit 2513bdf
Show file tree
Hide file tree
Showing 24 changed files with 2,685 additions and 527 deletions.
58 changes: 47 additions & 11 deletions BetterContinents.BetterContinentsSettings.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using System.IO;
using HarmonyLib;
using UnityEngine;

namespace BetterContinents
Expand All @@ -12,7 +11,7 @@ public partial class BetterContinents
public struct BetterContinentsSettings
{
// Add new properties at the end, and comment where new versions start
public const int LatestVersion = 5;
public const int LatestVersion = 6;

// Version 1
public int Version;
Expand Down Expand Up @@ -64,6 +63,10 @@ public struct BetterContinentsSettings
public bool MountainsAllowedAtCenter;

public bool ForestFactorOverrideAllTrees;

// Version 6
public bool HeightmapOverrideAll;
public float HeightmapMask;

// Non-serialized
private ImageMapFloat Heightmap;
Expand All @@ -79,10 +82,8 @@ public struct BetterContinentsSettings
public bool HasRoughmap => this.Roughmap != null;
public bool HasFlatmap => this.Flatmap != null;
public bool HasForestmap => this.Forestmap != null;

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

public bool ShouldHeightmapOverrideAll => this.HasHeightmap && this.HeightmapOverrideAll;

public static BetterContinentsSettings Create(long worldUId)
{
Expand Down Expand Up @@ -168,6 +169,8 @@ private void InitSettings(long worldUId, bool enabled)
SetHeightmapAmount(ConfigHeightmapAmount.Value);
SetHeightmapBlend(ConfigHeightmapBlend.Value);
SetHeightmapAdd(ConfigHeightmapAdd.Value);
SetHeightmapMask(ConfigHeightmapMask.Value);
SetHeightmapOverrideAll(ConfigHeightmapOverrideAll.Value);

Heightmap = new ImageMapFloat(heightmapPath);
if (!Heightmap.LoadSourceImage() || !Heightmap.CreateMap())
Expand Down Expand Up @@ -265,6 +268,8 @@ private void InitSettings(long worldUId, bool enabled)
public void SetHeightmapAmount(float heightmapAmount) => HeightmapAmount = heightmapAmount;
public void SetHeightmapBlend(float heightmapBlend) => HeightmapBlend = heightmapBlend;
public void SetHeightmapAdd(float heightmapAdd) => HeightmapAdd = heightmapAdd;
public void SetHeightmapMask(float heightmapMask) => HeightmapMask = heightmapMask;
public void SetHeightmapOverrideAll(bool heightmapOverrideAll) => HeightmapOverrideAll = heightmapOverrideAll;

public void SetRoughmapBlend(float roughmapBlend) => RoughmapBlend = roughmapBlend;

Expand Down Expand Up @@ -419,7 +424,11 @@ public void Dump(Action<string> output = null)
if (Heightmap != null)
{
output($"Heightmap file {Heightmap.FilePath}");
output($"Heightmap size {Heightmap.Size}x{Heightmap.Size}, amount {HeightmapAmount}, blend {HeightmapBlend}, add {HeightmapAdd}");
output($"Heightmap size {Heightmap.Size}x{Heightmap.Size}, amount {HeightmapAmount}, blend {HeightmapBlend}, add {HeightmapAdd}, mask {HeightmapMask}");
if (HeightmapOverrideAll)
{
output($"Heightmap overrides ALL");
}
}
else
{
Expand Down Expand Up @@ -578,6 +587,9 @@ public void Serialize(ZPackage pkg)
}

// Version 4
// (nothing)

// Version 5
if (Version >= 5)
{
pkg.Write(Roughmap?.FilePath ?? string.Empty);
Expand Down Expand Up @@ -610,6 +622,13 @@ public void Serialize(ZPackage pkg)
pkg.Write(MountainsAllowedAtCenter);
pkg.Write(ForestFactorOverrideAllTrees);
}

// Version 6
if (Version >= 6)
{
pkg.Write(HeightmapOverrideAll);
pkg.Write(HeightmapMask);
}
}
}

Expand Down Expand Up @@ -704,7 +723,7 @@ private void Deserialize(ZPackage pkg)
}

// Version 4
// Nothing...
// (nothing)

// Version 5
if (Version >= 5)
Expand Down Expand Up @@ -751,19 +770,31 @@ private void Deserialize(ZPackage pkg)
MountainsAllowedAtCenter = pkg.ReadBool();
ForestFactorOverrideAllTrees = pkg.ReadBool();
}

// Version 6
if (Version >= 6)
{
HeightmapOverrideAll = pkg.ReadBool();
HeightmapMask = pkg.ReadSingle();
}
else
{
HeightmapOverrideAll = false;
HeightmapMask = 0;
}
}
}

public float ApplyHeightmap(float x, float y, float height)
{
if (this.Heightmap == null || (this.HeightmapBlend == 0 && this.HeightmapAdd == 0))
if (this.Heightmap == null || (this.HeightmapBlend == 0 && this.HeightmapAdd == 0 && this.HeightmapMask == 0))
{
return height;
}

float h = this.Heightmap.GetValue(x, y);

return Mathf.Lerp(height, h * HeightmapAmount, this.HeightmapBlend) + h * this.HeightmapAdd;
float blendedHeight = Mathf.Lerp(height, h * HeightmapAmount, this.HeightmapBlend);
return Mathf.Lerp(blendedHeight, blendedHeight * h, HeightmapMask) + h * this.HeightmapAdd;
}

public float ApplyRoughmap(float x, float y, float smoothHeight, float roughHeight)
Expand All @@ -780,6 +811,11 @@ public float ApplyRoughmap(float x, float y, float smoothHeight, float roughHeig

public float ApplyFlatmap(float x, float y, float flatHeight, float height)
{
if (Settings.ShouldHeightmapOverrideAll)
{
return flatHeight;
}

if ((this.UseRoughInvertedAsFlat && this.Roughmap == null) || (!this.UseRoughInvertedAsFlat && this.Flatmap == null) || this.RoughmapBlend == 0)
{
return height;
Expand Down
7 changes: 1 addition & 6 deletions BetterContinents.ConsolePatch.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using HarmonyLib;
using UnityEngine;
using UnityEngine.UI;
using HarmonyLib;

namespace BetterContinents
{
Expand Down
21 changes: 15 additions & 6 deletions BetterContinents.WorldGeneratorPatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,20 @@ private static readonly GetBaseHeightDelegate GetBaseHeightMethod
[HarmonyPostfix, HarmonyPatch("GetBiomeHeight")]
private static void GetBiomeHeightPostfix(WorldGenerator __instance, float wx, float wy, ref float __result, World ___m_world)
{
if (!Settings.EnabledForThisWorld || ___m_world.m_menu || !Settings.UseRoughmap)
if (!Settings.EnabledForThisWorld || ___m_world.m_menu)
{
return;
}

if (!Settings.ShouldHeightmapOverrideAll && !Settings.HasRoughmap)
{
return;
}

float smoothHeight = GetBaseHeightMethod(__instance, wx, wy, false) * 200f;
__result = Settings.ApplyRoughmap(NormalizedX(wx), NormalizedY(wy), smoothHeight, __result);
__result = Settings.ShouldHeightmapOverrideAll
? smoothHeight
: 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 Expand Up @@ -112,7 +119,9 @@ private static float GetBaseHeightV1(float wx, float wy, float ___m_offset0, flo

// https://www.desmos.com/calculator/uq8wmu6dy7
float SigmoidActivation(float x, float a, float b) => 1 / (1 + Mathf.Exp(a + b * x));
float lerp = Mathf.Clamp01(SigmoidActivation(Mathf.PerlinNoise(wx * 0.005f - 10000, wy * 0.005f - 5000) - Settings.RidgeBlendSigmoidXOffset, 0, Settings.RidgeBlendSigmoidB));
float lerp = Settings.ShouldHeightmapOverrideAll
? 0
: Mathf.Clamp01(SigmoidActivation(Mathf.PerlinNoise(wx * 0.005f - 10000, wy * 0.005f - 5000) - Settings.RidgeBlendSigmoidXOffset, 0, Settings.RidgeBlendSigmoidB));

float finalHeight = 0f;

Expand All @@ -130,7 +139,7 @@ private static float GetBaseHeightV1(float wx, float wy, float ___m_offset0, flo

finalHeight += Settings.SeaLevelAdjustment;

if (Settings.OceanChannelsEnabled)
if (Settings.OceanChannelsEnabled && !Settings.ShouldHeightmapOverrideAll)
{
float v = Mathf.Abs(
Mathf.PerlinNoise(wx * 0.002f * 0.25f + 0.123f, wy * 0.002f * 0.25f + 0.15123f) -
Expand All @@ -150,7 +159,7 @@ private static float GetBaseHeightV1(float wx, float wy, float ___m_offset0, flo
finalHeight = Mathf.Lerp(finalHeight, -2f, t2);
}
}
if (distance < ___m_minMountainDistance && finalHeight > 0.28f)
if (distance < ___m_minMountainDistance && finalHeight > 0.28f && !Settings.ShouldHeightmapOverrideAll)
{
float t3 = Mathf.Clamp01((finalHeight - 0.28f) / 0.099999994f);
finalHeight = Mathf.Lerp(Mathf.Lerp(0.28f, 0.38f, t3), finalHeight, Utils.LerpStep(___m_minMountainDistance - 400f, ___m_minMountainDistance, distance));
Expand Down Expand Up @@ -238,7 +247,7 @@ private static float GetBaseHeightV2(float wx, float wy, float ___m_offset0, flo
[HarmonyPrefix, HarmonyPatch(nameof(WorldGenerator.GetBiome), typeof(float), typeof(float)), HarmonyAfter("org.github.spacedrive.worldgen")]
private static bool GetBiomePrefix(float wx, float wy, ref Heightmap.Biome __result, World ___m_world)
{
if (!Settings.EnabledForThisWorld || ___m_world.m_menu || !Settings.OverrideBiomes)
if (!Settings.EnabledForThisWorld || ___m_world.m_menu || !Settings.HasBiomemap)
{
return true;
}
Expand Down
1 change: 0 additions & 1 deletion BetterContinents.ZNetPatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
using System.Threading.Tasks;
using HarmonyLib;
using UnityEngine;
using UnityEngine.UI;

namespace BetterContinents
{
Expand Down
5 changes: 2 additions & 3 deletions BetterContinents.ZoneSystemPatch.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Collections.Generic;
using HarmonyLib;
using HarmonyLib;
using UnityEngine;

namespace BetterContinents
Expand All @@ -24,7 +23,7 @@ private static bool GenerateLocationsPrefix(ZoneSystem __instance, ZoneSystem.Zo
Log($"Generating location of group {groupName}, required {location.m_quantity}, unique {location.m_unique}, name {location.m_prefabName}");
if (Settings.EnabledForThisWorld)
{
if (Settings.UseSpawnmap)
if (Settings.HasSpawnmap)
{
// Place all locations specified by the spawn map, ignoring counts specified in the prefab
int placed = 0;
Expand Down
15 changes: 7 additions & 8 deletions BetterContinents.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,7 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Reflection;
using System.Threading.Tasks;
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
using UnityEngine;
using UnityEngine.Assertions;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
using Debug = UnityEngine.Debug;

Expand All @@ -19,6 +11,7 @@ namespace BetterContinents
public partial class BetterContinents : BaseUnityPlugin
{
// See the Awake function for the config descriptions
public static ConfigEntry<int> NexusID;
public static ConfigEntry<bool> ConfigEnabled;

public static ConfigEntry<float> ConfigContinentSize;
Expand All @@ -35,6 +28,8 @@ public partial class BetterContinents : BaseUnityPlugin
public static ConfigEntry<float> ConfigHeightmapAmount;
public static ConfigEntry<float> ConfigHeightmapBlend;
public static ConfigEntry<float> ConfigHeightmapAdd;
public static ConfigEntry<float> ConfigHeightmapMask;
public static ConfigEntry<bool> ConfigHeightmapOverrideAll;

public static ConfigEntry<string> ConfigBiomemapFile;

Expand Down Expand Up @@ -114,6 +109,8 @@ private void Awake()
.Value("Heightmap Amount").Description("Multiplier of the height value from the heightmap file (more than 1 leads to higher max height than vanilla, good results are not guaranteed)").Default(1f).Range(0f, 5f).Bind(out ConfigHeightmapAmount)
.Value("Heightmap Blend").Description("How strongly to blend the heightmap file into the final result").Default(1f).Range(0f, 1f).Bind(out ConfigHeightmapBlend)
.Value("Heightmap Add").Description("How strongly to add the heightmap file to the final result (usually you want to blend it instead)").Default(0f).Range(-1f, 1f).Bind(out ConfigHeightmapAdd)
.Value("Heightmap Mask").Description("How strongly to apply the heightmap as a mask on normal height generation (i.e. it limits maximum height to the height of the mask)").Default(0f).Range(0f, 1f).Bind(out ConfigHeightmapMask)
.Value("Heightmap Override All").Description("All other aspects of the height calculation will be disabled, so the world will perfectly conform to your heightmap").Default(true).Bind(out ConfigHeightmapOverrideAll)
.Group("BetterContinents.Roughmap")
.Value("Roughmap File").Description("Path to a roughmap file to use. See the description on Nexusmods.com for the specifications (it will fail if they are not met)").Bind(out ConfigRoughmapFile)
.Value("Roughmap Blend").Description("How strongly to apply the roughmap file").Default(1f).Range(0f, 1f).Bind(out ConfigRoughmapBlend)
Expand Down Expand Up @@ -144,6 +141,8 @@ private void Awake()
.Group("BetterContinents.Debug")
.Value("Debug Mode").Description("Automatically reveals the full map on respawn, enables cheat mode, and debug mode, for debugging purposes").Bind(out ConfigDebugModeEnabled)
.Value("Skip Default Location Placement").Description("Skips default location placement during world gen (spawn temple and spawnmap are still placed), for quickly testing the heightmap itself").Bind(out ConfigDebugSkipDefaultLocationPlacement)
.Group("BetterContinents.Misc")
.Value("NexusID").Default(446).Description("For Nexus Update Check compatibility").Bind(out NexusID);
;

new Harmony("BetterContinents.Harmony").PatchAll();
Expand Down
1 change: 1 addition & 0 deletions BetterContinents.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@
<Compile Include="ConfigHelpers.cs" />
<Compile Include="ConfigurationManagerAttributes.cs" />
<Compile Include="DebugUtils.cs" />
<Compile Include="FastNoiseLite\FastNoiseLite.cs" />
<Compile Include="GameUtils.cs" />
<Compile Include="ImageMapBase.cs" />
<Compile Include="ImageMapBiome.cs" />
Expand Down
1 change: 0 additions & 1 deletion ConfigHelpers.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using BepInEx.Configuration;
using UnityEngine;
using UnityEngine.Assertions;

namespace BetterContinents
Expand Down
9 changes: 5 additions & 4 deletions DebugUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ public class DebugUtils

private static readonly string[] Bosses =
{
"StartTemple", "Eikthymir", "GDKing", "GoblinKing", "Bonemass", "Dragonqueen",
"Vendor"
"StartTemple", "Eikthyrnir", "GDKing", "GoblinKing", "Bonemass", "Dragonqueen",
"Vendor_BlackForest"
};
static DebugUtils()
{
Expand Down Expand Up @@ -89,8 +89,7 @@ static DebugUtils()
});
AddCommand("bosses", "show pins for bosses, start temple and trader", _ =>
{
GameUtils.ShowOnMap("StartTemple", "Eikthymir", "GDKing", "GoblinKing", "Bonemass", "Dragonqueen",
"Vendor");
GameUtils.ShowOnMap(Bosses);
});
// AddCommand("show", "pin all locations on the map", _ =>
// {
Expand Down Expand Up @@ -195,9 +194,11 @@ void AddHeightmapSubcommand(Command command, string cmd, string desc, string arg
else
BetterContinents.Settings.SetHeightmapPath(args);
});
AddHeightmapSubcommand(subcmd, "ov", "heightmap override all", "(0 to disable, 1 to enable)", args => BetterContinents.Settings.SetHeightmapOverrideAll(int.Parse(args) != 0));
AddHeightmapSubcommand(subcmd, "am", "heightmap amount", "(between 0 and 5)", args => BetterContinents.Settings.SetHeightmapAmount(float.Parse(args)));
AddHeightmapSubcommand(subcmd, "bl", "heightmap blend", "(between 0 and 1)", args => BetterContinents.Settings.SetHeightmapBlend(float.Parse(args)));
AddHeightmapSubcommand(subcmd, "ad", "heightmap add", "(between -1 and 1)", args => BetterContinents.Settings.SetHeightmapAdd(float.Parse(args)));
AddHeightmapSubcommand(subcmd, "ma", "heightmap mask", "(between 0 and 1)", args => BetterContinents.Settings.SetHeightmapMask(float.Parse(args)));
});
command.AddSubcommand("r", "roughmap settings, get more info with 'bc param r help'", subcmdConfig: subcmd =>
{
Expand Down
Loading

0 comments on commit 2513bdf

Please sign in to comment.