diff --git a/core/src/main/java/art/arcane/iris/core/studio/noise/ExpressionSeeds.java b/core/src/main/java/art/arcane/iris/core/studio/noise/ExpressionSeeds.java new file mode 100644 index 000000000..167bbadc0 --- /dev/null +++ b/core/src/main/java/art/arcane/iris/core/studio/noise/ExpressionSeeds.java @@ -0,0 +1,70 @@ +/* + * Iris is a World Generator for Minecraft Servers + * Copyright (c) 2026 Arcane Arts (Volmit Software) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package art.arcane.iris.core.studio.noise; + +import art.arcane.iris.core.loader.IrisData; +import art.arcane.iris.engine.IrisComplex; +import art.arcane.iris.engine.framework.Engine; +import art.arcane.iris.engine.object.noise.IrisGeneratorStyle; +import art.arcane.iris.engine.object.noise.expression.IrisExpression; +import art.arcane.volmlib.util.math.RNG; + +import java.util.HashMap; +import java.util.Map; + +/** + * The randoms the world evaluates the pack's expressions with, found by following them down from the dimension's noise + * styles. An expression previewed with its random lines up with the terrain it shapes. + */ +final class ExpressionSeeds { + private final Engine engine; + private final IrisData data; + private final Map randomsByExpression = new HashMap<>(); + + private ExpressionSeeds(Engine engine) { + this.engine = engine; + this.data = engine.getData(); + } + + static Map of(Engine engine) { + ExpressionSeeds seeds = new ExpressionSeeds(engine); + for (IrisComplex.SeededStyle root : engine.getComplex().seededNoiseStyles(engine.getDimension())) { + seeds.follow(root.style(), root.rng()); + } + return Map.copyOf(seeds.randomsByExpression); + } + + // Variables and functions sample their styles with their expression's random, so it passes down unchanged. + private void follow(IrisGeneratorStyle style, RNG callerRandom) { + if (style == null || style.getExpression() == null) { + return; + } + String key = style.getExpression(); + RNG expressionRandom = style.rollFor(callerRandom, engine); + if (randomsByExpression.putIfAbsent(key, expressionRandom) != null) { + return; + } + IrisExpression expression = data.getExpressionLoader().load(key); + if (expression == null) { + return; + } + expression.getVariables().forEach(variable -> follow(variable.getStyleValue(), expressionRandom)); + expression.getFunctions().forEach(function -> follow(function.getStyleValue(), expressionRandom)); + } +} diff --git a/core/src/main/java/art/arcane/iris/core/studio/noise/NoiseSources.java b/core/src/main/java/art/arcane/iris/core/studio/noise/NoiseSources.java index 26339b626..c3a417e90 100644 --- a/core/src/main/java/art/arcane/iris/core/studio/noise/NoiseSources.java +++ b/core/src/main/java/art/arcane/iris/core/studio/noise/NoiseSources.java @@ -33,6 +33,7 @@ import java.util.Arrays; import java.util.Comparator; import java.util.List; import java.util.Locale; +import java.util.Map; import java.util.NoSuchElementException; /** @@ -54,7 +55,7 @@ public final class NoiseSources { List entries = new ArrayList<>(); addOverlayNoise(engine, entries); addPackGenerators(engine.getData(), entries); - addPackExpressions(engine.getData(), entries); + addPackExpressions(engine, entries); addStyles(entries); return new NoiseSources(entries); } @@ -106,15 +107,19 @@ public final class NoiseSources { } } - private static void addPackExpressions(IrisData data, List entries) { + // An expression the world uses keeps the random the world gives it, so it lines up with the height map; any other takes the view's seed. + private static void addPackExpressions(Engine engine, List entries) { + IrisData data = engine.getData(); + Map worldRandoms = ExpressionSeeds.of(engine); for (String key : sortedKeys(data.getExpressionLoader().getPossibleKeys())) { - Source source = new Source("expression:" + key, displayName(key), NoiseCategory.PACK_EXPRESSIONS.label(), true, null); + RNG worldRandom = worldRandoms.get(key); + Source source = new Source("expression:" + key, displayName(key), NoiseCategory.PACK_EXPRESSIONS.label(), worldRandom == null, null); entries.add(new Entry(source, NoiseCategory.PACK_EXPRESSIONS, seed -> { IrisExpression expression = data.getExpressionLoader().load(key); if (expression == null) { throw new NoSuchElementException("No pack expression " + key); } - RNG rng = new RNG(seed); + RNG rng = worldRandom == null ? new RNG(seed) : worldRandom; requireParses(expression, rng); return (x, z) -> expression.evaluate(rng, x, z); })); @@ -156,7 +161,7 @@ public final class NoiseSources { /** * One noise the view lists. * - * @param usesSeed whether the view's seed changes it; the overlay keeps the world's + * @param usesSeed whether the view's seed changes it; the overlay and the expressions the world uses keep the world's * @param blockRange where the noise is measured in blocks above the fluid, what it spans; null for noise of 0 to 1 */ public record Source(String id, String name, String category, boolean usesSeed, BlockRange blockRange) { diff --git a/core/src/main/java/art/arcane/iris/engine/IrisComplex.java b/core/src/main/java/art/arcane/iris/engine/IrisComplex.java index 6962355af..7a047eebc 100644 --- a/core/src/main/java/art/arcane/iris/engine/IrisComplex.java +++ b/core/src/main/java/art/arcane/iris/engine/IrisComplex.java @@ -28,6 +28,7 @@ import art.arcane.iris.engine.object.world.biome.IrisBiome; import art.arcane.iris.engine.object.world.biome.IrisBiomeGeneratorLink; import art.arcane.iris.engine.object.world.dimension.IrisDimension; import art.arcane.iris.engine.object.terrain.IrisGenerator; +import art.arcane.iris.engine.object.noise.IrisGeneratorStyle; import art.arcane.iris.engine.object.noise.IrisInterpolator; import art.arcane.iris.engine.object.world.region.IrisRegion; import art.arcane.iris.spi.IrisPlatforms; @@ -49,6 +50,7 @@ import lombok.Getter; import lombok.ToString; import java.io.File; +import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; @@ -80,6 +82,7 @@ public class IrisComplex implements DataProvider { Comparator.nullsFirst(Comparator.naturalOrder()) ); static final int OVERLAY_GENERATOR_SEED = 571093; + private static final int REGION_STYLE_SEED = 883; private static final int DEEPEST_CHILD_LEVEL = 3; /** How many levels of children a biome can be replaced through, parent first. */ public static final int CHILD_PICK_LEVELS = DEEPEST_CHILD_LEVEL + 1; @@ -213,7 +216,7 @@ public class IrisComplex implements DataProvider { return value; }); - regionStyleStream = engine.getDimension().getRegionStyle().create(rng.nextParallelRNG(883), getData()).stream() + regionStyleStream = engine.getDimension().getRegionStyle().create(rng.nextParallelRNG(REGION_STYLE_SEED), getData()).stream() .zoom(engine.getDimension().getRegionZoom()); regionIdentityStream = regionStyleStream.fit(Integer.MIN_VALUE, Integer.MAX_VALUE); ProceduralStream proceduralRegionStream = focusRegion != null ? @@ -559,25 +562,45 @@ public class IrisComplex implements DataProvider { return preparedInferredBiomeStream(inferredBiomeStreams, region, inferredType); } + private RNG biomeStyleRandom(InferredType type) { + return rng.nextParallelRNG(type.ordinal()); + } + + /** + * The dimension's noise styles the world builds from its seed, each with the random it samples with: the region + * style and the land, sea, shore and cave biome styles. Expressions under a style share its random. + */ + public List seededNoiseStyles(IrisDimension dimension) { + List styles = new ArrayList<>(); + styles.add(new SeededStyle(dimension.getRegionStyle(), rng.nextParallelRNG(REGION_STYLE_SEED))); + for (InferredType type : INFERRED_BIOME_PREPARATION_ORDER) { + styles.add(new SeededStyle(dimension.getBiomeStyle(type), biomeStyleRandom(type))); + } + return styles; + } + + public record SeededStyle(IrisGeneratorStyle style, RNG rng) { + } + private ProceduralStream compileInferredPickNoise(Engine engine, IrisRegion region, InferredType inferredType) { IrisDimension dimension = engine.getDimension(); return switch (inferredType) { case CAVE -> dimension.getCaveBiomeStyle() - .create(rng.nextParallelRNG(InferredType.CAVE.ordinal()), getData()).stream() + .create(biomeStyleRandom(InferredType.CAVE), getData()).stream() .zoom(dimension.getBiomeZoom()) .zoom(region.getCaveBiomeZoom()); case LAND -> dimension.getLandBiomeStyle() - .create(rng.nextParallelRNG(InferredType.LAND.ordinal()), getData()).stream() + .create(biomeStyleRandom(InferredType.LAND), getData()).stream() .zoom(dimension.getBiomeZoom()) .zoom(dimension.getLandZoom()) .zoom(region.getLandBiomeZoom()); case SEA -> dimension.getSeaBiomeStyle() - .create(rng.nextParallelRNG(InferredType.SEA.ordinal()), getData()).stream() + .create(biomeStyleRandom(InferredType.SEA), getData()).stream() .zoom(dimension.getBiomeZoom()) .zoom(dimension.getSeaZoom()) .zoom(region.getSeaBiomeZoom()); case SHORE -> dimension.getShoreBiomeStyle() - .create(rng.nextParallelRNG(InferredType.SHORE.ordinal()), getData()).stream() + .create(biomeStyleRandom(InferredType.SHORE), getData()).stream() .zoom(dimension.getBiomeZoom()) .zoom(region.getShoreBiomeZoom()); }; diff --git a/core/src/main/java/art/arcane/iris/engine/object/noise/IrisGeneratorStyle.java b/core/src/main/java/art/arcane/iris/engine/object/noise/IrisGeneratorStyle.java index 30007b60c..5e98eaf8b 100644 --- a/core/src/main/java/art/arcane/iris/engine/object/noise/IrisGeneratorStyle.java +++ b/core/src/main/java/art/arcane/iris/engine/object/noise/IrisGeneratorStyle.java @@ -130,7 +130,7 @@ public class IrisGeneratorStyle { } /** 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) { + public RNG rollFor(RNG callerRandom, Engine engine) { if (!isSeeded()) { return callerRandom; } diff --git a/core/src/test/java/art/arcane/iris/core/studio/noise/ExpressionSeedsTest.java b/core/src/test/java/art/arcane/iris/core/studio/noise/ExpressionSeedsTest.java new file mode 100644 index 000000000..b208b7548 --- /dev/null +++ b/core/src/test/java/art/arcane/iris/core/studio/noise/ExpressionSeedsTest.java @@ -0,0 +1,137 @@ +package art.arcane.iris.core.studio.noise; + +import art.arcane.iris.core.loader.IrisData; +import art.arcane.iris.core.loader.ResourceLoader; +import art.arcane.iris.engine.IrisComplex; +import art.arcane.iris.engine.framework.Engine; +import art.arcane.iris.engine.object.noise.IrisGeneratorStyle; +import art.arcane.iris.engine.object.noise.NoiseStyle; +import art.arcane.iris.engine.object.noise.expression.IrisExpression; +import art.arcane.iris.engine.object.noise.expression.IrisExpressionLoad; +import art.arcane.iris.engine.object.world.dimension.IrisDimension; +import art.arcane.volmlib.util.collection.KList; +import art.arcane.volmlib.util.math.RNG; +import org.junit.Before; +import org.junit.Test; + +import java.util.List; +import java.util.Map; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertSame; +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +public class ExpressionSeedsTest { + private static final long VIEW_SEED = 777L; + private static final long WORLD_SEED = 424242L; + private static final int REGION_STYLE_STREAM = 883; + private static final double EXACT = 0D; + private static final double[][] SPOTS = {{0, 0}, {37.5, -12.25}, {-901, 4444}, {123456, -654321}}; + + private IrisData data; + private Engine engine; + private IrisComplex complex; + private IrisDimension dimension; + private RNG worldRandom; + + @Before + public void world() { + data = mock(IrisData.class); + engine = mock(Engine.class); + complex = mock(IrisComplex.class); + dimension = mock(IrisDimension.class); + worldRandom = new RNG(WORLD_SEED).nextParallelRNG(REGION_STYLE_STREAM); + when(engine.getData()).thenReturn(data); + when(engine.getComplex()).thenReturn(complex); + when(engine.getDimension()).thenReturn(dimension); + } + + @Test + public void anExpressionUnderTheRegionStylePreviewsLikeTheEngineStream() { + IrisExpression ridges = expression("ridges", "x * 0.01 + z * 0.02"); + packHas(ridges); + worldSamples(styleOf("ridges")); + + NoiseSources sources = NoiseSources.of(engine); + + assertFalse(source(sources, "ridges").usesSeed()); + IrisGeneratorStyle engineStyle = styleOf("ridges"); + var engineNoise = engineStyle.createNoCache(worldRandom, data); + for (double[] spot : SPOTS) { + double preview = sources.sampler("expression:ridges", VIEW_SEED).noise(spot[0], spot[1]); + assertEquals(engineNoise.noise(spot[0], spot[1]), preview, EXACT); + } + } + + @Test + public void anExpressionBehindAVariableSharesTheRandomOfTheExpressionThatReadsIt() { + IrisExpression warp = expression("warp", "x * 0.01 + z * 0.02"); + IrisExpression ridges = expression("ridges", "x + warp * 100"); + ridges.getVariables().add(new IrisExpressionLoad().setName("warp").setStyleValue(styleOf("warp"))); + packHas(ridges, warp); + worldSamples(styleOf("ridges")); + + Map randoms = ExpressionSeeds.of(engine); + + assertSame(worldRandom, randoms.get("ridges")); + assertSame(worldRandom, randoms.get("warp")); + } + + @Test + public void anExpressionTheWorldDoesNotReachKeepsTheViewSeed() { + IrisExpression ridges = expression("ridges", "x * 0.01"); + IrisExpression unused = expression("unused", "x * 0.02"); + packHas(ridges, unused); + worldSamples(styleOf("ridges")); + + NoiseSources sources = NoiseSources.of(engine); + + assertTrue(source(sources, "unused").usesSeed()); + assertEquals(unused.evaluate(new RNG(VIEW_SEED), 5, 9), sources.sampler("expression:unused", VIEW_SEED).noise(5, 9), EXACT); + } + + @Test + public void aStyleWithoutAnExpressionReachesNothing() { + packHas(); + worldSamples(new IrisGeneratorStyle(NoiseStyle.SIMPLEX)); + + assertTrue(ExpressionSeeds.of(engine).isEmpty()); + } + + private NoiseSources.Source source(NoiseSources sources, String key) { + return sources.list().stream().filter(source -> source.id().equals("expression:" + key)).findFirst().orElseThrow(); + } + + private IrisGeneratorStyle styleOf(String expressionKey) { + return new IrisGeneratorStyle().setExpression(expressionKey); + } + + private IrisExpression expression(String key, String formula) { + IrisExpression expression = new IrisExpression(); + expression.setExpression(formula); + expression.setLoadKey(key); + expression.setLoader(data); + return expression; + } + + @SuppressWarnings("unchecked") + private void packHas(IrisExpression... expressions) { + ResourceLoader loader = mock(ResourceLoader.class); + String[] keys = new String[expressions.length]; + for (int index = 0; index < expressions.length; index++) { + keys[index] = expressions[index].getLoadKey(); + when(loader.load(keys[index])).thenReturn(expressions[index]); + } + when(loader.getPossibleKeys()).thenReturn(keys); + when(data.getExpressionLoader()).thenReturn(loader); + when(data.getGeneratorLoader()).thenReturn(mock(ResourceLoader.class)); + when(dimension.getOverlayNoise()).thenReturn(new KList<>()); + } + + private void worldSamples(IrisGeneratorStyle regionStyle) { + when(complex.seededNoiseStyles(dimension)).thenReturn(List.of(new IrisComplex.SeededStyle(regionStyle, worldRandom))); + } +}