mirror of
https://github.com/VolmitSoftware/Iris.git
synced 2026-11-27 16:25:36 +00:00
Roll seeded noise styles from the world seed, read them flat
IrisGeneratorStyle gets the seed field the IrisApi style has. A seeded
style is built from splitMix(worldSeed ^ splitMix(seed)) instead of the
caller's random, so every style with the same seed and shape shares one
noise per world. Material palettes and child biomes read a seeded style
in plain blocks (no palette zoom, no childShrinkFactor).
Tests: the factory ignores the asked seed and follows the world seed;
the decorator chance gate and palette layers read seeded styles flat
(module code, tested here as the other module tests are).
Ported from v3e a1bd24186 and 1be1795e6. IrisApi 2914ed1.
This commit is contained in:
1 parent
ee5f3d90eb
commit
3b9012535f
7 files changed
+154
-5
No files matched your search
@@ -109,6 +109,7 @@ public final class IrisValues {
|
||||
iris.setMultiplier(style.multiplier());
|
||||
iris.setFracture(style.fracture().map(fracture -> generatorStyle(fracture, resources)).orElse(null));
|
||||
iris.setExponent(style.exponent());
|
||||
style.seed().ifPresent(iris::setSeed);
|
||||
return iris;
|
||||
}
|
||||
|
||||
|
||||
@@ -85,10 +85,15 @@ public class IrisMaterialPalette {
|
||||
return localBlockData.get(0);
|
||||
}
|
||||
|
||||
CNG generator = getLayerGenerator(rng, rdata);
|
||||
if (style.isSeeded()) {
|
||||
return generator.fit(localBlockData, x, z);
|
||||
}
|
||||
|
||||
double scaledX = x / zoom;
|
||||
double scaledY = y / zoom;
|
||||
double scaledZ = z / zoom;
|
||||
return getLayerGenerator(rng, rdata).fit(localBlockData, scaledX, scaledY, scaledZ);
|
||||
return generator.fit(localBlockData, scaledX, scaledY, scaledZ);
|
||||
}
|
||||
|
||||
public Optional<TileData> getTile(RNG rng, double x, double y, double z, IrisData rdata) {
|
||||
|
||||
@@ -88,6 +88,8 @@ public class IrisGeneratorStyle {
|
||||
@MaxNumber(8192)
|
||||
@Desc("If the cache size is set above 0, this generator will be cached")
|
||||
private int cacheSize = 0;
|
||||
@Desc("Rolls this noise from the world seed and this number, so every style with the same seed shares one noise. Child biomes, palettes and decorator placement read a seeded style flat in blocks: zoom is its only scale, so a layer's zoom, childShrinkFactor and the decorator's extra zoom don't apply.")
|
||||
private Long seed = null;
|
||||
|
||||
public IrisGeneratorStyle(NoiseStyle s) {
|
||||
this.style = s;
|
||||
@@ -116,7 +118,29 @@ public class IrisGeneratorStyle {
|
||||
}
|
||||
|
||||
private int hash() {
|
||||
return Objects.hash(expression, imageMapHash(), multiplier, fracture != null ? fracture.hash() : 0, exponent, cacheSize, zoom, cellularZoom, cellularFrequency, style);
|
||||
return Objects.hash(expression, imageMapHash(), multiplier, fracture != null ? fracture.hash() : 0, exponent, cacheSize, zoom, cellularZoom, cellularFrequency, style, seed);
|
||||
}
|
||||
|
||||
public boolean isSeeded() {
|
||||
return seed != null;
|
||||
}
|
||||
|
||||
/** The random a style is built from: the caller's, or for a seeded style one rolled from its seed and the world seed. */
|
||||
private RNG rollFor(RNG callerRandom, Engine engine) {
|
||||
if (!isSeeded()) {
|
||||
return callerRandom;
|
||||
}
|
||||
|
||||
long worldSeed = engine == null ? 0L : engine.getSeedManager().getSeed();
|
||||
return new RNG(splitMix(worldSeed ^ splitMix(seed)));
|
||||
}
|
||||
|
||||
// Spreads nearby seed numbers far apart; Iris's own seed forking only adds.
|
||||
private static long splitMix(long value) {
|
||||
long mixed = value + 0x9E3779B97F4A7C15L;
|
||||
mixed = (mixed ^ (mixed >>> 30)) * 0xBF58476D1CE4E5B9L;
|
||||
mixed = (mixed ^ (mixed >>> 27)) * 0x94D049BB133111EBL;
|
||||
return mixed ^ (mixed >>> 31);
|
||||
}
|
||||
|
||||
public int prebakeSignature() {
|
||||
@@ -163,8 +187,9 @@ public class IrisGeneratorStyle {
|
||||
return createNoCache(rng, data, actuallyCached, 0, false, resolveEngine(data));
|
||||
}
|
||||
|
||||
private CNG createNoCache(RNG rng, IrisData data, boolean actuallyCached, int fallbackCacheSize,
|
||||
private CNG createNoCache(RNG callerRandom, IrisData data, boolean actuallyCached, int fallbackCacheSize,
|
||||
boolean quietCacheLog, Engine engine) {
|
||||
RNG rng = rollFor(callerRandom, engine);
|
||||
CNG cng = null;
|
||||
long sourceStamp = 0L;
|
||||
if (getExpression() != null) {
|
||||
@@ -214,7 +239,8 @@ public class IrisGeneratorStyle {
|
||||
return create(rng, data, resolveEngine(data));
|
||||
}
|
||||
|
||||
public CNG create(RNG rng, IrisData data, Engine engine) {
|
||||
public CNG create(RNG callerRandom, IrisData data, Engine engine) {
|
||||
RNG rng = rollFor(callerRandom, engine);
|
||||
GeneratorCacheKey key = new GeneratorCacheKey(data, engine, rng.getSeed());
|
||||
return generatorCache.computeIfAbsent(key,
|
||||
ignored -> createNoCache(rng, data, true, 0, false, engine));
|
||||
|
||||
@@ -295,7 +295,8 @@ public class IrisBiome extends IrisRegistrant implements IRare {
|
||||
}
|
||||
|
||||
public CNG getChildrenGenerator(RNG random, int sig, double scale) {
|
||||
return childrenCell.aquire(() -> getChildStyle().create(random.nextParallelRNG(sig * 2137), getLoader()).bake().scale(scale).bake());
|
||||
double childScale = getChildStyle().isSeeded() ? 1 : scale;
|
||||
return childrenCell.aquire(() -> getChildStyle().create(random.nextParallelRNG(sig * 2137), getLoader()).bake().scale(childScale).bake());
|
||||
}
|
||||
|
||||
public boolean isLand() {
|
||||
|
||||
@@ -3,6 +3,7 @@ package art.arcane.iris.core.modules;
|
||||
import art.arcane.iris.core.loader.IrisData;
|
||||
import art.arcane.iris.core.loader.ResourceLoader;
|
||||
import art.arcane.iris.engine.UsableBlockPlatform;
|
||||
import art.arcane.iris.engine.framework.Engine;
|
||||
import art.arcane.iris.engine.object.material.IrisBlockData;
|
||||
import art.arcane.iris.engine.object.material.IrisMaterialPalette;
|
||||
import art.arcane.iris.engine.object.noise.IrisGeneratorStyle;
|
||||
@@ -41,6 +42,7 @@ import static org.junit.Assert.assertThrows;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.anyBoolean;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@@ -129,6 +131,28 @@ public class IrisValuesTest {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void aSeededStyleIgnoresTheSeedItIsAskedForAndFollowsTheWorldSeed() {
|
||||
NoiseStyle seeded = NoiseStyle.of("SIMPLEX").withZoom(40).withSeed(7);
|
||||
IrisData world = packOfWorldSeed(1337L);
|
||||
IrisData otherWorld = packOfWorldSeed(4242L);
|
||||
|
||||
NoiseSampler asked = noiseOf(world).sampler(seeded, 1);
|
||||
NoiseSampler askedOtherwise = noiseOf(world).sampler(seeded, 987_654_321L);
|
||||
NoiseSampler inOtherWorld = noiseOf(otherWorld).sampler(seeded, 1);
|
||||
NoiseSampler otherStyleSeed = noiseOf(world).sampler(seeded.withSeed(8), 1);
|
||||
|
||||
int differsByWorld = 0;
|
||||
int differsByStyleSeed = 0;
|
||||
for (double[] column : COLUMNS) {
|
||||
assertEquals(asked.sample(column[0], column[1]), askedOtherwise.sample(column[0], column[1]), 0);
|
||||
differsByWorld += asked.sample(column[0], column[1]) != inOtherWorld.sample(column[0], column[1]) ? 1 : 0;
|
||||
differsByStyleSeed += asked.sample(column[0], column[1]) != otherStyleSeed.sample(column[0], column[1]) ? 1 : 0;
|
||||
}
|
||||
assertEquals(COLUMNS.length, differsByWorld);
|
||||
assertEquals(COLUMNS.length, differsByStyleSeed);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void signedNoiseIsTheSignedFastReadOfTheIrisGeneratorsNoise() {
|
||||
String json = "{\"style\": \"SIMPLEX\", \"zoom\": 4, \"exponent\": 1.5, \"fracture\": {\"style\": \"PERLIN\", \"zoom\": 2, \"multiplier\": 3}}";
|
||||
@@ -250,6 +274,14 @@ public class IrisValuesTest {
|
||||
return data;
|
||||
}
|
||||
|
||||
private static IrisData packOfWorldSeed(long worldSeed) {
|
||||
IrisData data = packWithoutCustomBlocks();
|
||||
Engine engine = mock(Engine.class, RETURNS_DEEP_STUBS);
|
||||
when(engine.getSeedManager().getSeed()).thenReturn(worldSeed);
|
||||
when(data.getEngine()).thenReturn(engine);
|
||||
return data;
|
||||
}
|
||||
|
||||
private static IrisBlockRegistry blocksOf(IrisData data) {
|
||||
return new IrisBlockRegistry(data, new IrisPackResources(data, List::of));
|
||||
}
|
||||
|
||||
@@ -86,6 +86,30 @@ public class ResolvedDecoratorTest {
|
||||
assertEquals(List.of("CLOVER@" + (SEED + 3 + 7 + 2), "SIMPLEX@" + (SEED + 3), "PERLIN@" + (SEED + 3)), built);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void theChanceGateReadsASeededStyleInPlainBlocks() {
|
||||
List<String> reads = new ArrayList<>();
|
||||
NoiseFactory recording = (style, seed) -> new NoiseSampler() {
|
||||
@Override
|
||||
public double sample(double worldX, double worldZ) {
|
||||
reads.add(worldX + "," + worldZ);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public double sample(double worldX, double y, double worldZ) {
|
||||
return 0;
|
||||
}
|
||||
};
|
||||
Decorator zoomed = new Gson().fromJson("{\"style\": {\"style\": \"SIMPLEX\", \"zoom\": 4}}", Decorator.class);
|
||||
Decorator seeded = new Gson().fromJson("{\"style\": {\"style\": \"SIMPLEX\", \"zoom\": 4, \"seed\": 3}}", Decorator.class);
|
||||
|
||||
new ResolvedDecorator(zoomed, SEED, recording, state("a"), List.of(), null, List.of(), List.of(), ModuleLogs.discarding(), "test").passesChanceGate(40, -8);
|
||||
new ResolvedDecorator(seeded, SEED, recording, state("a"), List.of(), null, List.of(), List.of(), ModuleLogs.discarding(), "test").passesChanceGate(40, -8);
|
||||
|
||||
assertEquals(List.of("10.0,-2.0", "40.0,-8.0"), reads);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void noiseIsOnlyBuiltForWhatIsAskedFor() {
|
||||
NoiseFactory refusing = (style, seed) -> {
|
||||
|
||||
@@ -0,0 +1,60 @@
|
||||
package art.arcane.iris.engine.terrain;
|
||||
|
||||
import art.arcane.iris.builtin.terrain.PaletteLayer;
|
||||
import art.arcane.iris.modules.terrain.LayerBlocks;
|
||||
import art.arcane.volmlib.util.math.RNG;
|
||||
import com.google.gson.Gson;
|
||||
import com.volmit.block.BlockRegistry;
|
||||
import com.volmit.block.BlockSpec;
|
||||
import com.volmit.block.BlockState;
|
||||
import com.volmit.noise.NoiseFactory;
|
||||
import com.volmit.noise.NoiseSampler;
|
||||
import com.volmit.resource.PackResources;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
public class LayerBlocksSeededStyleTest {
|
||||
private static final String ZOOMED = "{\"zoom\": 4, \"style\": {\"style\": \"SIMPLEX\"}, \"palette\": [{\"block\": \"stone\"}, {\"block\": \"dirt\"}]}";
|
||||
private static final String SEEDED = "{\"zoom\": 4, \"style\": {\"style\": \"SIMPLEX\", \"seed\": 9}, \"palette\": [{\"block\": \"stone\"}, {\"block\": \"dirt\"}]}";
|
||||
|
||||
@Test
|
||||
public void aSeededLayerStyleIsReadFlatInPlainBlocks() {
|
||||
List<String> reads = new ArrayList<>();
|
||||
LayerBlocks blocks = TestLayerBlocks.of(resourcesRecording(reads)).get();
|
||||
Gson gson = new Gson();
|
||||
|
||||
blocks.pick(gson.fromJson(ZOOMED, PaletteLayer.class), new RNG(1), 0, 40, 3, -8);
|
||||
blocks.pick(gson.fromJson(SEEDED, PaletteLayer.class), new RNG(1), 0, 40, 3, -8);
|
||||
|
||||
assertEquals(List.of("10.0,0.75,-2.0", "40.0,-8.0"), reads);
|
||||
}
|
||||
|
||||
private static PackResources resourcesRecording(List<String> reads) {
|
||||
BlockRegistry registry = mock(BlockRegistry.class);
|
||||
when(registry.resolve(any(BlockSpec.class))).thenReturn(BlockState.parse("minecraft:stone"));
|
||||
NoiseFactory recording = (style, seed) -> new NoiseSampler() {
|
||||
@Override
|
||||
public double sample(double worldX, double worldZ) {
|
||||
reads.add(worldX + "," + worldZ);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public double sample(double worldX, double y, double worldZ) {
|
||||
reads.add(worldX + "," + y + "," + worldZ);
|
||||
return 0;
|
||||
}
|
||||
};
|
||||
PackResources resources = mock(PackResources.class);
|
||||
when(resources.blocks()).thenReturn(registry);
|
||||
when(resources.noise()).thenReturn(recording);
|
||||
return resources;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user