Skip to content

Commit 9fb6190

Browse files
committed
Preserve dynamic-biome geology and stagger geome boundaries
1 parent 5036ecf commit 9fb6190

7 files changed

Lines changed: 268 additions & 27 deletions

File tree

‎src/main/java/zone/moddev/mc/orespawn/worldgen/BakedGeomeConfig.java‎

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public final class BakedGeomeConfig {
4848

4949
BakedGeomeConfig(GeomeDefinition[] geomes, double geomeScale, double biomeInfluence,
5050
double regionalNoiseInfluence, double boundaryNoiseInfluence, Map<Biome, double[]> biomeWeights,
51-
RockEntry[] rocks, FormationSettings formations) {
51+
Map<ResourceLocation, double[]> biomeWeightsById, RockEntry[] rocks, FormationSettings formations) {
5252
this.geomes = geomes;
5353
this.geomeScale = geomeScale;
5454
this.biomeInfluence = biomeInfluence;
@@ -57,7 +57,7 @@ public final class BakedGeomeConfig {
5757
this.formations = formations;
5858
this.familyDiversitySlots = formations.familyDiversitySlots();
5959
this.biomeWeights = new IdentityHashMap<>(biomeWeights);
60-
this.biomeWeightsById = new HashMap<>();
60+
this.biomeWeightsById = new HashMap<>(biomeWeightsById);
6161
for (Map.Entry<Biome, double[]> entry : biomeWeights.entrySet()) {
6262
ResourceLocation biomeId = ForgeRegistries.BIOMES.getKey(entry.getKey());
6363
if (biomeId != null) {
@@ -109,6 +109,23 @@ int pickGeome(Biome biome, ResourceLocation biomeId, double[] regionalNoise, dou
109109
return bestIndex;
110110
}
111111

112+
int scoreGeomes(Biome biome, ResourceLocation biomeId, double[] regionalNoiseAndScores, double boundaryNoise) {
113+
double[] weights = biomeWeightsFor(biome, biomeId);
114+
double bestScore = Double.NEGATIVE_INFINITY;
115+
int bestIndex = 0;
116+
for (int i = 0; i < geomes.length; i++) {
117+
double boundary = ((i & 1) == 0 ? boundaryNoise : -boundaryNoise) * boundaryNoiseInfluence;
118+
double score = geomes[i].baseWeight + (weights[i] * biomeInfluence)
119+
+ (regionalNoiseAndScores[i] * regionalNoiseInfluence) + boundary;
120+
regionalNoiseAndScores[i] = score;
121+
if (score > bestScore) {
122+
bestScore = score;
123+
bestIndex = i;
124+
}
125+
}
126+
return bestIndex;
127+
}
128+
112129
public RockFamily pickFamily(int geomeIndex, int y, int formationValue) {
113130
return pickFamily(geomeIndex, y, formationValue, 0);
114131
}

‎src/main/java/zone/moddev/mc/orespawn/worldgen/FluidDepositFeature.java‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,9 @@ public boolean place(FeaturePlaceContext<NoneFeatureConfiguration> context) {
111111
for (BakedDeposit deposit : deposits) {
112112
if (!deposit.acceptsBiome(biome)) continue;
113113
if (!geomeClassified && deposit.usesGeomeWeights && config != null) {
114+
ResourceLocation biomeId = biome.unwrapKey().map(ResourceKey::location).orElse(null);
114115
geome = classifier(dimension, world.getSeed(), config).classifyColumn(
115-
biome.value(), centerX, centerZ, scratch.geomeValues(config.geomeCount()));
116+
biome.value(), biomeId, centerX, centerZ, scratch.geomeValues(config.geomeCount()));
116117
geomeClassified = true;
117118
}
118119
double frequency = geome < 0 ? deposit.frequency : deposit.frequency * deposit.geomeWeights[geome];

‎src/main/java/zone/moddev/mc/orespawn/worldgen/GeomeConfig.java‎

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import java.nio.file.Paths;
1111
import java.nio.file.StandardCopyOption;
1212
import java.util.ArrayList;
13+
import java.util.Arrays;
1314
import java.util.HashMap;
1415
import java.util.IdentityHashMap;
1516
import java.util.LinkedHashMap;
@@ -287,11 +288,14 @@ private static BakedGeomeConfig bake(JsonObject root, ResourceLocation dimension
287288
return null;
288289
}
289290
Map<Biome, double[]> biomeWeights = bakeBiomeWeights(geomeIndexes, biomeRules, dictionaryRules);
291+
Map<ResourceLocation, double[]> biomeWeightsById = bakeBiomeIdentifierWeights(geomeIndexes, biomeRules);
290292

291-
LOGGER.info("Baked OreSpawn geome config for '{}' with {} geomes, {} rock entries, {} biome profiles, and {} formations",
292-
dimension, geomes.length, rocks.length, biomeWeights.size(), formations.algorithm.configName);
293+
LOGGER.info("Baked OreSpawn geome config for '{}' with {} geomes, {} rock entries, "
294+
+ "{} resolved biome profiles, {} identifier profiles, and {} formations",
295+
dimension, geomes.length, rocks.length, biomeWeights.size(), biomeWeightsById.size(),
296+
formations.algorithm.configName);
293297
return new BakedGeomeConfig(geomes, geomeScale, biomeInfluence, regionalNoiseInfluence,
294-
boundaryNoiseInfluence, biomeWeights, rocks, formations);
298+
boundaryNoiseInfluence, biomeWeights, biomeWeightsById, rocks, formations);
295299
}
296300

297301
private static JsonObject applyFreshWorldTemplate(JsonObject root) {
@@ -1034,11 +1038,33 @@ private static Map<Biome, double[]> bakeBiomeWeights(Map<String, Integer> geomeI
10341038
return result;
10351039
}
10361040

1041+
static Map<ResourceLocation, double[]> bakeBiomeIdentifierWeights(Map<String, Integer> geomeIndexes,
1042+
Map<String, double[]> biomeRules) {
1043+
Map<ResourceLocation, double[]> result = new LinkedHashMap<>();
1044+
for (Entry<String, double[]> entry : biomeRules.entrySet()) {
1045+
try {
1046+
ResourceLocation biomeId = new ResourceLocation(entry.getKey());
1047+
double[] weights = new double[geomeIndexes.size()];
1048+
Arrays.fill(weights, 1.0D);
1049+
merge(weights, entry.getValue());
1050+
applyBiomeHeuristic(weights, geomeIndexes, biomeId, Float.NaN, Float.NaN);
1051+
result.put(biomeId, weights);
1052+
} catch (RuntimeException e) {
1053+
LOGGER.warn("Ignoring invalid OreSpawn biome rule ID '{}'", entry.getKey());
1054+
}
1055+
}
1056+
return result;
1057+
}
1058+
10371059
private static void applyBiomeHeuristic(double[] weights, Map<String, Integer> geomeIndexes,
10381060
ResourceLocation biomeId, Biome biome) {
1061+
applyBiomeHeuristic(weights, geomeIndexes, biomeId,
1062+
biome.getBaseTemperature(), biome.getModifiedClimateSettings().downfall());
1063+
}
1064+
1065+
private static void applyBiomeHeuristic(double[] weights, Map<String, Integer> geomeIndexes,
1066+
ResourceLocation biomeId, float temperature, float downfall) {
10391067
String biomeName = biomeId == null ? "" : biomeId.getPath();
1040-
float temperature = biome.getBaseTemperature();
1041-
float downfall = biome.getModifiedClimateSettings().downfall();
10421068

10431069
if (biomeName.contains("ocean") || biomeName.contains("river") || biomeName.contains("beach")
10441070
|| biomeName.contains("shore") || biomeName.contains("coast")

‎src/main/java/zone/moddev/mc/orespawn/worldgen/GeomeDistributionSampler.java‎

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -132,15 +132,13 @@ public static String sampleTerrain(long seed, Path path) throws IOException {
132132
}
133133

134134
Biome[] biomePalette = new Biome[paletteSize];
135+
ResourceLocation[] biomeIds = new ResourceLocation[paletteSize];
135136
for (int index = 0; index < paletteSize; index++) {
136137
int length = input.readUnsignedShort();
137138
byte[] encoded = new byte[length];
138139
input.readFully(encoded);
139-
ResourceLocation biomeId = new ResourceLocation(new String(encoded, StandardCharsets.UTF_8));
140-
biomePalette[index] = ForgeRegistries.BIOMES.getValue(biomeId);
141-
if (biomePalette[index] == null) {
142-
throw new IOException("Unknown biome " + biomeId + " in " + path);
143-
}
140+
biomeIds[index] = new ResourceLocation(new String(encoded, StandardCharsets.UTF_8));
141+
biomePalette[index] = ForgeRegistries.BIOMES.getValue(biomeIds[index]);
144142
}
145143

146144
int height = maxY - minY + 1;
@@ -153,17 +151,18 @@ public static String sampleTerrain(long seed, Path path) throws IOException {
153151
}
154152
input.readFully(rockMask);
155153
Biome biome = biomePalette[biomeIndex];
154+
ResourceLocation biomeId = biomeIds[biomeIndex];
156155
int x = minX + xOffset;
157156
int z = minZ + zOffset;
158-
int geomeIndex = geology.classifyColumn(biome, x, z, regionalValues);
157+
int geomeIndex = geology.classifyColumn(biome, biomeId, x, z, regionalValues);
159158
int stratumOffset = geology.stratumOffsetAt(x, z);
160159
long formationRegion = geology.formationRegionAt(x, z);
161160
add(geomeCounts, config.geomeName(geomeIndex));
162161
for (int yIndex = 0; yIndex < height; yIndex++) {
163162
if ((rockMask[yIndex >>> 3] & (1 << (yIndex & 7))) == 0) {
164163
continue;
165164
}
166-
Block block = geology.getStoneAt(geomeIndex, stratumOffset, formationRegion,
165+
Block block = geology.getStoneAt(geomeIndex, regionalValues, stratumOffset, formationRegion,
167166
x, minY + yIndex, z);
168167
ResourceLocation id = ForgeRegistries.BLOCKS.getKey(block);
169168
String rockId = id == null ? "<unregistered>" : id.toString();

‎src/main/java/zone/moddev/mc/orespawn/worldgen/GeomeGeology.java‎

Lines changed: 84 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import net.minecraft.world.level.block.state.BlockState;
1919

2020
public final class GeomeGeology {
21+
private static final double GEOME_TRANSITION_SCORE_WIDTH = 0.125D;
2122
private static final int[] LITHOLOGY_PHASES = {
2223
0, 2, 1, 3,
2324
3, 1, 2, 0,
@@ -34,6 +35,7 @@ public final class GeomeGeology {
3435
private final short[] whiteNoiseArray;
3536
private final boolean[] globallyContinuousLayers;
3637
private final boolean[] regionallyVariedRocks;
38+
private final int geomeTransitionPhase;
3739
private final int layerThickness;
3840
private final int formationRegionScale;
3941
private final int familyDiversitySlots;
@@ -67,6 +69,7 @@ public GeomeGeology(long seed, BakedGeomeConfig config) {
6769
}
6870

6971
Random random = new Random(seed ^ 0x5EEDBEEFL);
72+
geomeTransitionPhase = new Random(seed ^ 0x47454F4D4554524EL).nextInt(256);
7073
whiteNoiseArray = new short[256];
7174
for (int i = 0; i < whiteNoiseArray.length; i++) {
7275
whiteNoiseArray[i] = (short) random.nextInt(0x7FFF);
@@ -109,7 +112,9 @@ public void replaceStoneInChunk(LevelAccessor world, ChunkAccess chunk, BakedTer
109112
long formationRegion = formationRegionAt(x, z);
110113

111114
if (stableLayers) {
112-
changed |= replaceStableColumn(chunk, cursor, geomeIndex, baseRockValue,
115+
int secondGeome = runnerUpGeome(regionalValues, geomeIndex);
116+
changed |= replaceStableColumn(chunk, cursor, geomeIndex, secondGeome,
117+
regionalValues, baseRockValue,
113118
formationRegion, x, z, surfaceY, terrain);
114119
} else {
115120
for (int y = surfaceY; y >= chunk.getMinBuildHeight(); y--) {
@@ -130,11 +135,14 @@ public void replaceStoneInChunk(LevelAccessor world, ChunkAccess chunk, BakedTer
130135
}
131136

132137
private boolean replaceStableColumn(ChunkAccess chunk, BlockPos.MutableBlockPos cursor, int geomeIndex,
133-
int baseRockValue, long formationRegion, int x, int z, int surfaceY,
138+
int secondGeome, double[] geomeScores, int baseRockValue,
139+
long formationRegion, int x, int z, int surfaceY,
134140
BakedTerrainDimension terrain) {
135141
int layerIndex = Math.floorDiv(baseRockValue + surfaceY, layerThickness);
136142
int layerStart = layerIndex * layerThickness;
137-
BlockState replacement = pickStableReplacement(geomeIndex, formationRegion, layerIndex);
143+
int layerGeome = pickStableLayerGeome(geomeScores, geomeIndex, secondGeome,
144+
layerIndex, geomeTransitionPhase);
145+
BlockState replacement = pickStableReplacement(layerGeome, formationRegion, layerIndex);
138146
boolean changed = false;
139147
cursor.set(x, surfaceY, z);
140148

@@ -143,7 +151,9 @@ private boolean replaceStableColumn(ChunkAccess chunk, BlockPos.MutableBlockPos
143151
if (stratum < layerStart) {
144152
layerIndex--;
145153
layerStart -= layerThickness;
146-
replacement = pickStableReplacement(geomeIndex, formationRegion, layerIndex);
154+
layerGeome = pickStableLayerGeome(geomeScores, geomeIndex, secondGeome,
155+
layerIndex, geomeTransitionPhase);
156+
replacement = pickStableReplacement(layerGeome, formationRegion, layerIndex);
147157
}
148158
cursor.setY(y);
149159
if (terrain.isReplaceable(chunk.getBlockState(cursor))) {
@@ -157,7 +167,14 @@ private boolean replaceStableColumn(ChunkAccess chunk, BlockPos.MutableBlockPos
157167
public Block getStoneAt(Biome biome, int x, int y, int z, int surfaceY) {
158168
double[] regionalValues = new double[config.geomeCount()];
159169
int geomeIndex = classifyColumn(biome, x, z, regionalValues);
160-
return pickReplacement(geomeIndex, stratumOffsetAt(x, z), formationRegionAt(x, z), x, y, z).getBlock();
170+
int stratumOffset = stratumOffsetAt(x, z);
171+
long formationRegion = formationRegionAt(x, z);
172+
if (stableLayers) {
173+
int layerIndex = Math.floorDiv(stratumOffset + y, layerThickness);
174+
geomeIndex = pickStableLayerGeome(regionalValues, geomeIndex,
175+
runnerUpGeome(regionalValues, geomeIndex), layerIndex, geomeTransitionPhase);
176+
}
177+
return pickReplacement(geomeIndex, stratumOffset, formationRegion, x, y, z).getBlock();
161178
}
162179

163180
public String getGeomeName(Biome biome, int x, int z) {
@@ -176,18 +193,24 @@ int classifyColumn(Biome biome, int x, int z, double[] regionalValues) {
176193
public ColumnSample sampleColumn(Biome biome, ResourceLocation biomeId, int x, int z) {
177194
double[] regionalValues = new double[config.geomeCount()];
178195
int geomeIndex = classifyColumn(biome, biomeId, x, z, regionalValues);
179-
return new ColumnSample(geomeIndex, stratumOffsetAt(x, z), formationRegionAt(x, z), x, z);
196+
return new ColumnSample(geomeIndex, runnerUpGeome(regionalValues, geomeIndex),
197+
regionalValues, stratumOffsetAt(x, z), formationRegionAt(x, z), x, z);
180198
}
181199

182200
public final class ColumnSample {
183201
private final int geomeIndex;
202+
private final int secondGeome;
203+
private final double[] geomeScores;
184204
private final int stratumOffset;
185205
private final long formationRegion;
186206
private final int x;
187207
private final int z;
188208

189-
private ColumnSample(int geomeIndex, int stratumOffset, long formationRegion, int x, int z) {
209+
private ColumnSample(int geomeIndex, int secondGeome, double[] geomeScores,
210+
int stratumOffset, long formationRegion, int x, int z) {
190211
this.geomeIndex = geomeIndex;
212+
this.secondGeome = secondGeome;
213+
this.geomeScores = geomeScores;
191214
this.stratumOffset = stratumOffset;
192215
this.formationRegion = formationRegion;
193216
this.x = x;
@@ -199,21 +222,27 @@ public String geomeName() {
199222
}
200223

201224
public BlockState rockAt(int y) {
202-
return pickReplacement(geomeIndex, stratumOffset, formationRegion, x, y, z);
225+
int selectedGeome = geomeIndex;
226+
if (stableLayers) {
227+
int layerIndex = Math.floorDiv(stratumOffset + y, layerThickness);
228+
selectedGeome = pickStableLayerGeome(geomeScores, geomeIndex, secondGeome,
229+
layerIndex, geomeTransitionPhase);
230+
}
231+
return pickReplacement(selectedGeome, stratumOffset, formationRegion, x, y, z);
203232
}
204233

205234
public RockFamily familyAt(int y) {
206235
return config.familyOf(rockAt(y));
207236
}
208237
}
209238

210-
private int classifyColumn(Biome biome, ResourceLocation biomeId, int x, int z, double[] regionalValues) {
239+
int classifyColumn(Biome biome, ResourceLocation biomeId, int x, int z, double[] regionalValues) {
211240
for (int i = 0; i < regionalValues.length; i++) {
212241
regionalValues[i] = regionalNoise.valueAt(x + config.noiseOffsetX[i], z + config.noiseOffsetZ[i]);
213242
}
214243

215244
double boundary = boundaryNoise.valueAt(x, z);
216-
return config.pickGeome(biome, biomeId, regionalValues, boundary);
245+
return config.scoreGeomes(biome, biomeId, regionalValues, boundary);
217246
}
218247

219248
private net.minecraft.world.level.block.state.BlockState pickReplacement(int geomeIndex, int baseRockValue,
@@ -269,7 +298,13 @@ int stratumLayerAt(int x, int y, int z) {
269298
return Math.floorDiv(stratumOffsetAt(x, z) + y, layerThickness);
270299
}
271300

272-
Block getStoneAt(int geomeIndex, int stratumOffset, long formationRegion, int x, int y, int z) {
301+
Block getStoneAt(int geomeIndex, double[] geomeScores, int stratumOffset,
302+
long formationRegion, int x, int y, int z) {
303+
if (stableLayers) {
304+
int layerIndex = Math.floorDiv(stratumOffset + y, layerThickness);
305+
geomeIndex = pickStableLayerGeome(geomeScores, geomeIndex,
306+
runnerUpGeome(geomeScores, geomeIndex), layerIndex, geomeTransitionPhase);
307+
}
273308
return pickReplacement(geomeIndex, stratumOffset, formationRegion, x, y, z).getBlock();
274309
}
275310

@@ -298,6 +333,44 @@ private static int mixRegion(int cellX, int cellZ, int contour) {
298333
return hash ^ (hash >>> 16);
299334
}
300335

336+
static int pickStableLayerGeome(double[] geomeScores, int firstGeome, int secondGeome,
337+
int layerIndex, int phase) {
338+
if (firstGeome == secondGeome) {
339+
return firstGeome;
340+
}
341+
int lowerGeome = Math.min(firstGeome, secondGeome);
342+
int higherGeome = Math.max(firstGeome, secondGeome);
343+
double higherFraction = 0.5D + ((geomeScores[higherGeome] - geomeScores[lowerGeome])
344+
/ (2.0D * GEOME_TRANSITION_SCORE_WIDTH));
345+
if (higherFraction <= 0.0D) {
346+
return lowerGeome;
347+
}
348+
if (higherFraction >= 1.0D) {
349+
return higherGeome;
350+
}
351+
352+
// Bit reversal supplies an allocation-free low-discrepancy sequence. Nearby
353+
// layers therefore cross a close geome boundary at different horizontal
354+
// positions instead of moving as one full-height wall.
355+
int pairPhase = (lowerGeome * 53) + (higherGeome * 97);
356+
int layerBucket = (layerIndex + phase + pairPhase) & 0xFF;
357+
int threshold = Integer.reverse(layerBucket) >>> 24;
358+
return ((threshold + 0.5D) / 256.0D) < higherFraction ? higherGeome : lowerGeome;
359+
}
360+
361+
private static int runnerUpGeome(double[] geomeScores, int bestGeome) {
362+
if (geomeScores.length < 2) {
363+
return bestGeome;
364+
}
365+
int second = bestGeome == 0 ? 1 : 0;
366+
for (int i = 0; i < geomeScores.length; i++) {
367+
if (i != bestGeome && geomeScores[i] > geomeScores[second]) {
368+
second = i;
369+
}
370+
}
371+
return second;
372+
}
373+
301374
private static double faciesFraction(double regionScale) {
302375
if (regionScale <= 100.0D) {
303376
return Math.max(0.0D, (regionScale - 50.0D) / 350.0D);

‎src/main/java/zone/moddev/mc/orespawn/worldgen/OreSpawnOreGeneration.java‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,8 @@ private static boolean generateChunk(WorldGenLevel world, ChunkAccess chunk, Hol
140140
int centerZ = chunkPos.getMinBlockZ() + 8;
141141
int geome = -1;
142142
if (Level.OVERWORLD.equals(dimension)) {
143-
geome = classifier(worldSeed).classifyColumn(biome.value(), centerX, centerZ,
143+
ResourceLocation biomeId = biome.unwrapKey().map(ResourceKey::location).orElse(null);
144+
geome = classifier(worldSeed).classifyColumn(biome.value(), biomeId, centerX, centerZ,
144145
scratch.geomeValues(geomeConfig.geomeCount()));
145146
}
146147

0 commit comments

Comments
 (0)