forked from JereKuusela/BetterContinents
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BetterContinents.WorldGeneratorPatch.cs
352 lines (295 loc) · 15.9 KB
/
BetterContinents.WorldGeneratorPatch.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection.Emit;
using System.Threading;
using System.Threading.Tasks;
using HarmonyLib;
using UnityEngine;
namespace BetterContinents;
public partial class BetterContinents
{
// Changes to height, biome, forests, rivers etc. (this is the functional part of the mod)
[HarmonyPatch(typeof(WorldGenerator))]
public class WorldGeneratorPatch
{
private static readonly string[] TreePrefixes =
[
"FirTree",
"Pinetree_01",
"SwampTree2_darkland",
"SwampTree1",
"SwampTree2",
"FirTree_small",
"FirTree_small_dead",
"HugeRoot1",
"SwampTree2_log",
"FirTree_oldLog",
"vertical_web",
"horizontal_web",
"tunnel_web",
];
private static int currentSeed;
// Hardcoded gaps don't work well with the mod when often the whole world layout is changed.
public static bool DisableGap(ref double __result)
{
__result = 1d;
return false;
}
//private static Noise
[HarmonyPrefix, HarmonyPatch(nameof(WorldGenerator.Initialize))]
private static void InitializePrefix(World world)
{
if (Settings.EnabledForThisWorld && !world.m_menu && Settings.ForestFactorOverrideAllTrees && ZoneSystem.instance != null)
{
foreach (var v in ZoneSystem.instance.m_vegetation)
{
if (TreePrefixes.Contains(v.m_prefab.name))
{
v.m_inForest = true;
v.m_forestTresholdMin = 0f;
v.m_forestTresholdMax = 1.15f;
}
}
}
if (Settings.EnabledForThisWorld)
{
currentSeed = world.m_seed;
ApplyNoiseSettings();
}
}
public static NoiseStack? BaseHeightNoise;
public static void ApplyNoiseSettings()
{
BaseHeightNoise = new NoiseStack(currentSeed, Settings.BaseHeightNoise);
}
// wx, wy are [-10500, 10500]
// __result should be [0, 1]
public static bool GetBaseHeightPrefixV3(ref float wx, ref float wy, ref float __result, float ___m_minMountainDistance)
{
__result = GetBaseHeightV3(wx, wy, ___m_minMountainDistance);
return false;
}
public static bool GetBaseHeightPrefixV2(ref float wx, ref float wy, ref float __result, float ___m_offset0, float ___m_offset1, float ___m_minMountainDistance)
{
__result = GetBaseHeightV2(wx, wy, ___m_offset0, ___m_offset1, ___m_minMountainDistance);
return false;
}
public static bool GetBaseHeightPrefixV1(ref float wx, ref float wy, ref float __result, float ___m_offset0, float ___m_offset1, float ___m_minMountainDistance)
{
__result = GetBaseHeightV1(wx, wy, ___m_offset0, ___m_offset1, ___m_minMountainDistance);
return false;
}
#pragma warning disable IDE0060
public static float GetBiomeHeightWithHeightPaint(float result, WorldGenerator __instance, Heightmap.Biome biome, ref Color mask, float wx, float wy)
{
Settings.ApplyPaintMap(wx, wy, biome, ref mask);
return __instance.GetBaseHeight(wx, wy, false) * 200f;
}
public static float GetBiomeHeightWithHeight(float result, WorldGenerator __instance, float wx, float wy)
{
return __instance.GetBaseHeight(wx, wy, false) * 200f;
}
#pragma warning restore IDE0060
public static float GetBiomeHeightWithRoughPaint(float result, WorldGenerator __instance, Heightmap.Biome biome, ref Color mask, float wx, float wy)
{
var smoothHeight = __instance.GetBaseHeight(wx, wy, false) * 200f;
Settings.ApplyPaintMap(wx, wy, biome, ref mask);
return Settings.ApplyRoughmap(NormalizedX(wx), NormalizedY(wy), smoothHeight, result);
}
public static float GetBiomeHeightWithRough(float result, WorldGenerator __instance, ref Color mask, float wx, float wy)
{
var smoothHeight = __instance.GetBaseHeight(wx, wy, false) * 200f;
return Settings.ApplyRoughmap(NormalizedX(wx), NormalizedY(wy), smoothHeight, result);
}
public static void GetBiomeHeightWithPaint(ref Color mask, Heightmap.Biome biome, float wx, float wy)
{
Settings.ApplyPaintMap(wx, wy, biome, ref mask);
}
private static float GetBaseHeightV1(float wx, float wy, float ___m_offset0, float ___m_offset1, float ___m_minMountainDistance)
{
float distance = Utils.Length(wx, wy);
// The base map x, y coordinates in 0..1 range
float mapX = NormalizedX(wx);
float mapY = NormalizedY(wy);
wx *= Settings.GlobalScale;
wy *= Settings.GlobalScale;
float WarpScale = 0.001f * Settings.RidgeScale;
float warpX = (Mathf.PerlinNoise(wx * WarpScale, wy * WarpScale) - 0.5f) * WorldSize;
float warpY = (Mathf.PerlinNoise(wx * WarpScale + 2f, wy * WarpScale + 3f) - 0.5f) * WorldSize;
wx += 100000f + ___m_offset0;
wy += 100000f + ___m_offset1;
float bigFeatureNoiseHeight = Mathf.PerlinNoise(wx * 0.002f * 0.5f, wy * 0.002f * 0.5f) * Mathf.PerlinNoise(wx * 0.003f * 0.5f, wy * 0.003f * 0.5f) * 1f;
float bigFeatureHeight = Settings.ApplyHeightmap(mapX, mapY, bigFeatureNoiseHeight);
float ridgeHeight = Mathf.PerlinNoise(warpX * 0.002f * 0.5f, warpY * 0.002f * 0.5f) * Mathf.PerlinNoise(warpX * 0.003f * 0.5f, warpY * 0.003f * 0.5f) * Settings.MaxRidgeHeight;
// https://www.desmos.com/calculator/uq8wmu6dy7
float SigmoidActivation(float x, float a, float b) => 1 / (1 + Mathf.Exp(a + b * x));
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;
float bigFeature = Mathf.Clamp01(Mathf.Lerp(bigFeatureHeight, ridgeHeight, lerp));
const float SeaLevel = 0.05f;
float ApplyMountains(float x, float n) => x * (1 - Mathf.Pow(1 - x, 1.2f + n * 0.8f)) + x * (1 - x);
finalHeight += ApplyMountains(bigFeature - SeaLevel, Settings.MountainsAmount) + SeaLevel;
finalHeight += Mathf.PerlinNoise(wx * 0.002f * 1f, wy * 0.002f * 1f) * Mathf.PerlinNoise(wx * 0.003f * 1f, wy * 0.003f * 1f) * finalHeight * 0.9f;
finalHeight += Mathf.PerlinNoise(wx * 0.005f * 1f, wy * 0.005f * 1f) * Mathf.PerlinNoise(wx * 0.01f * 1f, wy * 0.01f * 1f) * 0.5f * finalHeight;
finalHeight -= 0.07f;
finalHeight += Settings.SeaLevelAdjustment;
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) -
Mathf.PerlinNoise(wx * 0.002f * 0.25f + 0.321f, wy * 0.002f * 0.25f + 0.231f));
finalHeight *= 1f - (1f - Utils.LerpStep(0.02f, 0.12f, v)) *
Utils.SmoothStep(744f, 1000f, distance);
}
// Edge of the world
var size = WorldSize - EdgeSize;
if (distance > size)
{
float t = Utils.LerpStep(size, WorldSize, distance);
finalHeight = Mathf.Lerp(finalHeight, -0.2f, t);
var edge = WorldSize - 10;
if (distance > edge)
{
float t2 = Utils.LerpStep(edge, WorldSize, distance);
finalHeight = Mathf.Lerp(finalHeight, -2f, t2);
}
}
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));
}
return finalHeight;
}
private static float GetBaseHeightV2(float wx, float wy, float ___m_offset0, float ___m_offset1, float ___m_minMountainDistance)
{
float distance = Utils.Length(wx, wy);
// The base map x, y coordinates in 0..1 range
float mapX = NormalizedX(wx);
float mapY = NormalizedY(wy);
wx *= Settings.GlobalScale;
wy *= Settings.GlobalScale;
float WarpScale = 0.001f * Settings.RidgeScale;
float warpX = (Mathf.PerlinNoise(wx * WarpScale, wy * WarpScale) - 0.5f) * WorldSize;
float warpY = (Mathf.PerlinNoise(wx * WarpScale + 2f, wy * WarpScale + 3f) - 0.5f) * WorldSize;
wx += 100000f + ___m_offset0;
wy += 100000f + ___m_offset1;
float bigFeatureNoiseHeight = Mathf.PerlinNoise(wx * 0.002f * 0.5f, wy * 0.002f * 0.5f) * Mathf.PerlinNoise(wx * 0.003f * 0.5f, wy * 0.003f * 0.5f) * 1f;
float bigFeatureHeight = Settings.ApplyHeightmap(mapX, mapY, bigFeatureNoiseHeight);
float ridgeHeight = (Mathf.PerlinNoise(warpX * 0.002f * 0.5f, warpY * 0.002f * 0.5f) * Mathf.PerlinNoise(warpX * 0.003f * 0.5f, warpY * 0.003f * 0.5f)) * Settings.MaxRidgeHeight;
// 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 bigFeature = Mathf.Clamp01(bigFeatureHeight + ridgeHeight * lerp);
const float SeaLevel = 0.05f;
float ApplyMountains(float x, float n) => x * (1 - Mathf.Pow(1 - x, 1.2f + n * 0.8f)) + x * (1 - x);
float detailedFinalHeight = ApplyMountains(bigFeature - SeaLevel, Settings.MountainsAmount) + SeaLevel;
// Finer height variation
detailedFinalHeight += Mathf.PerlinNoise(wx * 0.002f * 1f, wy * 0.002f * 1f) * Mathf.PerlinNoise(wx * 0.003f * 1f, wy * 0.003f * 1f) * detailedFinalHeight * 0.9f;
detailedFinalHeight += Mathf.PerlinNoise(wx * 0.005f * 1f, wy * 0.005f * 1f) * Mathf.PerlinNoise(wx * 0.01f * 1f, wy * 0.01f * 1f) * 0.5f * detailedFinalHeight;
float finalHeight = Settings.ApplyFlatmap(mapX, mapY, bigFeatureHeight, detailedFinalHeight);
finalHeight -= 0.07f;
finalHeight += Settings.SeaLevelAdjustment;
if (Settings.OceanChannelsEnabled)
{
float v = Mathf.Abs(
Mathf.PerlinNoise(wx * 0.002f * 0.25f + 0.123f, wy * 0.002f * 0.25f + 0.15123f) -
Mathf.PerlinNoise(wx * 0.002f * 0.25f + 0.321f, wy * 0.002f * 0.25f + 0.231f));
finalHeight *= 1f - (1f - Utils.LerpStep(0.02f, 0.12f, v)) *
Utils.SmoothStep(744f, 1000f, distance);
}
// Edge of the world
var size = WorldSize - EdgeSize;
if (!Settings.DisableMapEdgeDropoff && distance > size)
{
float t = Utils.LerpStep(size, WorldSize, distance);
finalHeight = Mathf.Lerp(finalHeight, -0.2f, t);
var edge = WorldSize - 10;
if (distance > edge)
{
float t2 = Utils.LerpStep(edge, WorldSize, distance);
finalHeight = Mathf.Lerp(finalHeight, -2f, t2);
}
}
// Avoid mountains in the center
if (!Settings.MountainsAllowedAtCenter && distance < ___m_minMountainDistance && finalHeight > 0.28f)
{
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));
}
return finalHeight;
}
private static float GetBaseHeightV3(float wx, float wy, float ___m_minMountainDistance)
{
float distance = Utils.Length(wx, wy);
// The base map x, y coordinates in 0..1 range
float mapX = NormalizedX(wx);
float mapY = NormalizedY(wy);
float baseHeight = Settings.ApplyHeightmap(mapX, mapY, 0f);
float finalHeight = BaseHeightNoise?.Apply(wx, wy, baseHeight) ?? 0;
finalHeight -= 0.15f; // Resulting in about 30% water coverage by default
finalHeight += Settings.SeaLevelAdjustment;
// Edge of the world
var size = WorldSize - EdgeSize;
if (!Settings.DisableMapEdgeDropoff && distance > size)
{
float t = Utils.LerpStep(size, WorldSize, distance);
finalHeight = Mathf.Lerp(finalHeight, -0.2f, t);
var edge = WorldSize - 10;
if (distance > edge)
{
float t2 = Utils.LerpStep(edge, WorldSize, distance);
finalHeight = Mathf.Lerp(finalHeight, -2f, t2);
}
}
// Avoid mountains in the center
if (!Settings.MountainsAllowedAtCenter && distance < ___m_minMountainDistance && finalHeight > 0.28f)
{
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));
}
return finalHeight;
}
public static IEnumerable<CodeInstruction> GetBiomeHeightTranspiler(IEnumerable<CodeInstruction> instructions)
{
var matcher = new CodeMatcher(instructions);
matcher = matcher.MatchForward(false, new CodeMatch(OpCodes.Ldc_R4, -2f)).Advance(-5).
SetOpcodeAndAdvance(OpCodes.Nop).SetOpcodeAndAdvance(OpCodes.Nop).SetOpcodeAndAdvance(OpCodes.Nop).
SetOpcodeAndAdvance(OpCodes.Nop).SetOpcodeAndAdvance(OpCodes.Nop).SetOpcodeAndAdvance(OpCodes.Nop).
SetOpcodeAndAdvance(OpCodes.Nop).SetOpcodeAndAdvance(OpCodes.Nop).SetOpcodeAndAdvance(OpCodes.Nop);
return matcher.Instructions();
}
public static bool GetBiomePrefix(float wx, float wy, ref Heightmap.Biome __result, World ___m_world)
{
var normalized = WorldToNormalized(wx, wy);
var result = Settings.GetBiomeOverride(normalized.x, normalized.y);
if (result != Heightmap.Biome.None)
__result = result;
return false;
}
public static bool AddRiversPrefix(ref float __result, float h)
{
__result = h;
return false;
}
public static void GetForestFactorPrefix(ref Vector3 pos)
{
pos *= Settings.ForestScale;
}
// Range: 0.145071 1.850145
public static void GetForestFactorPostfix(Vector3 pos, ref float __result)
{
if (Settings.ForestScale != 1f)
pos /= Settings.ForestScale;
__result = Settings.ApplyForest(NormalizedX(pos.x), NormalizedY(pos.z), __result);
}
public static bool GetAshlandsOceanGradientPrefix(float x, float y, ref float __result)
{
__result = Settings.ApplyHeatmap(NormalizedX(x), NormalizedY(y));
return false;
}
}
}