diff --git a/core/src/main/java/art/arcane/iris/generation/decoration/IrisOreBands.java b/core/src/main/java/art/arcane/iris/generation/decoration/IrisOreBands.java
index 5a3b2c788..5f91c6a83 100644
--- a/core/src/main/java/art/arcane/iris/generation/decoration/IrisOreBands.java
+++ b/core/src/main/java/art/arcane/iris/generation/decoration/IrisOreBands.java
@@ -1,6 +1,5 @@
package art.arcane.iris.generation.decoration;
-import art.arcane.iris.generation.runtime.Engine;
import art.arcane.iris.generation.runtime.PackRuntime;
import art.arcane.iris.pack.value.IrisRange;
import art.arcane.volmlib.nativelib.terrain.NativeBlockState;
@@ -11,20 +10,24 @@ import java.util.Arrays;
/**
* One ore list (a biome's, region's or dimension's surface or underground ores) indexed by Y. Each Y
- * holds the generators whose range contains it, in list order, so resolving a block tries exactly
- * the generators that could place there and still returns the first one that does.
+ * holds the indices of the generators whose range contains it, in list order, so resolving a block tries exactly
+ * the generators that could place there and still returns the first one that does. The seeded chance fields live in
+ * an {@link OreBandsRuntime}, aligned with {@link #generators()}.
*/
public final class IrisOreBands {
public static final IrisOreBands EMPTY = new IrisOreBands(new IrisOreGenerator[0], 0, -1, null);
private static final int MAXIMUM_INDEXED_HEIGHT = 8192;
private final IrisOreGenerator[] generators;
+ private final int[] everyGenerator;
private final int minY;
private final int maxY;
- private final IrisOreGenerator[][] byY;
+ private final int[][] byY;
- private IrisOreBands(IrisOreGenerator[] generators, int minY, int maxY, IrisOreGenerator[][] byY) {
+ private IrisOreBands(IrisOreGenerator[] generators, int minY, int maxY, int[][] byY) {
this.generators = generators;
+ this.everyGenerator = new int[generators.length];
+ Arrays.setAll(everyGenerator, index -> index);
this.minY = minY;
this.maxY = maxY;
this.byY = byY;
@@ -57,19 +60,19 @@ public final class IrisOreBands {
height > MAXIMUM_INDEXED_HEIGHT ? null : index(generators, minY, (int) height));
}
- private static IrisOreGenerator[][] index(IrisOreGenerator[] generators, int minY, int height) {
- IrisOreGenerator[][] byY = new IrisOreGenerator[height][];
- IrisOreGenerator[] scratch = new IrisOreGenerator[generators.length];
- IrisOreGenerator[] previous = null;
+ private static int[][] index(IrisOreGenerator[] generators, int minY, int height) {
+ int[][] byY = new int[height][];
+ int[] scratch = new int[generators.length];
+ int[] previous = null;
for (int offset = 0; offset < height; offset++) {
int y = minY + offset;
int count = 0;
- for (IrisOreGenerator generator : generators) {
- if (generator.getRange().contains(y)) {
- scratch[count++] = generator;
+ for (int index = 0; index < generators.length; index++) {
+ if (generators[index].getRange().contains(y)) {
+ scratch[count++] = index;
}
}
- IrisOreGenerator[] candidates = Arrays.copyOf(scratch, count);
+ int[] candidates = Arrays.copyOf(scratch, count);
byY[offset] = previous != null && Arrays.equals(previous, candidates) ? previous : candidates;
previous = byY[offset];
}
@@ -92,13 +95,18 @@ public final class IrisOreBands {
return y >= minY && y <= maxY;
}
+ /** The banded generators in list order; chance fields built for them must keep this order. */
+ public IrisOreGenerator[] generators() {
+ return generators.clone();
+ }
+
/**
* The ore placed at a Y inside {@link #contains(int)}, or null when no generator places one.
*/
- public NativeBlockState generate(int x, int y, int z, RNG rng, PackRuntime runtime) {
- IrisOreGenerator[] candidates = byY == null ? generators : byY[y - minY];
- for (IrisOreGenerator candidate : candidates) {
- NativeBlockState ore = candidate.generate(x, y, z, rng, runtime);
+ NativeBlockState generate(int x, int y, int z, RNG rng, IrisOreGenerator.OreChance[] chances, PackRuntime runtime) {
+ int[] candidates = byY == null ? everyGenerator : byY[y - minY];
+ for (int index : candidates) {
+ NativeBlockState ore = generators[index].generate(x, y, z, rng, chances[index], runtime);
if (ore != null) {
return ore;
}
diff --git a/core/src/main/java/art/arcane/iris/generation/decoration/IrisOreGenerator.java b/core/src/main/java/art/arcane/iris/generation/decoration/IrisOreGenerator.java
index ba720ec14..f0c5b1ce6 100644
--- a/core/src/main/java/art/arcane/iris/generation/decoration/IrisOreGenerator.java
+++ b/core/src/main/java/art/arcane/iris/generation/decoration/IrisOreGenerator.java
@@ -62,18 +62,28 @@ public class IrisOreGenerator {
}
public NativeBlockState generate(int x, int y, int z, RNG rng, PackRuntime runtime) {
- if (threshold <= 0 || !range.contains(y)) {
+ if (!canPlaceAt(y)) {
return null;
}
+ return placeByChance(x, y, z, rng, runtime.oreChance(this, rng.getSeed()), runtime);
+ }
- if (palette.getPalette().isEmpty()) {
+ /** Places with a chance field already resolved for {@code rng}'s seed, as {@link OreBandsRuntime} holds them. */
+ public NativeBlockState generate(int x, int y, int z, RNG rng, OreChance chance, PackRuntime runtime) {
+ if (!canPlaceAt(y)) {
return null;
}
+ return placeByChance(x, y, z, rng, chance, runtime);
+ }
- if (runtime.oreChance(this, rng.getSeed()).sample(x, y, z) > threshold) {
+ private boolean canPlaceAt(int y) {
+ return threshold > 0 && range.contains(y) && !palette.getPalette().isEmpty();
+ }
+
+ private NativeBlockState placeByChance(int x, int y, int z, RNG rng, OreChance chance, PackRuntime runtime) {
+ if (chance.sample(x, y, z) > threshold) {
return null;
}
-
return palette.get(rng, x, y, z, runtime);
}
diff --git a/core/src/main/java/art/arcane/iris/generation/decoration/OreBandsRuntime.java b/core/src/main/java/art/arcane/iris/generation/decoration/OreBandsRuntime.java
new file mode 100644
index 000000000..31fa33e14
--- /dev/null
+++ b/core/src/main/java/art/arcane/iris/generation/decoration/OreBandsRuntime.java
@@ -0,0 +1,68 @@
+/*
+ * Iris is a World Generator for Minecraft Bukkit Servers
+ * Copyright (c) 2022 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.generation.decoration;
+
+import art.arcane.iris.generation.runtime.PackRuntime;
+import art.arcane.volmlib.nativelib.terrain.NativeBlockState;
+import art.arcane.volmlib.util.math.RNG;
+
+/**
+ * What one engine derives from an {@link IrisOreBands} for one seed: the chance field of every banded generator,
+ * resolved once so placing ores block by block reads plain fields instead of looking each generator up.
+ */
+public final class OreBandsRuntime {
+ private final IrisOreBands bands;
+ private final PackRuntime runtime;
+ private final IrisOreGenerator.OreChance[] chances;
+
+ public OreBandsRuntime(IrisOreBands bands, long seed, PackRuntime runtime) {
+ this.bands = bands;
+ this.runtime = runtime;
+ IrisOreGenerator[] generators = bands.generators();
+ this.chances = new IrisOreGenerator.OreChance[generators.length];
+ for (int index = 0; index < generators.length; index++) {
+ chances[index] = runtime.oreChance(generators[index], seed);
+ }
+ }
+
+ public IrisOreBands bands() {
+ return bands;
+ }
+
+ public boolean hasOres() {
+ return bands.hasOres();
+ }
+
+ public int lowestY() {
+ return bands.lowestY();
+ }
+
+ public int highestY() {
+ return bands.highestY();
+ }
+
+ public boolean contains(int y) {
+ return bands.contains(y);
+ }
+
+ /** The ore placed at a Y inside {@link #contains(int)}, or null when no generator places one. */
+ public NativeBlockState generate(int x, int y, int z, RNG rng) {
+ return bands.generate(x, y, z, rng, chances, runtime);
+ }
+}
diff --git a/core/src/main/java/art/arcane/iris/generation/runtime/PackRuntime.java b/core/src/main/java/art/arcane/iris/generation/runtime/PackRuntime.java
index 714697178..10a6a168a 100644
--- a/core/src/main/java/art/arcane/iris/generation/runtime/PackRuntime.java
+++ b/core/src/main/java/art/arcane/iris/generation/runtime/PackRuntime.java
@@ -29,7 +29,9 @@ import art.arcane.iris.generation.decoration.DecoratorRuntime;
import art.arcane.iris.generation.decoration.DepositRuntime;
import art.arcane.iris.generation.decoration.IrisDecorator;
import art.arcane.iris.generation.decoration.IrisDepositGenerator;
+import art.arcane.iris.generation.decoration.IrisOreBands;
import art.arcane.iris.generation.decoration.IrisOreGenerator;
+import art.arcane.iris.generation.decoration.OreBandsRuntime;
import art.arcane.iris.generation.noise.ExpressionRuntime;
import art.arcane.iris.generation.noise.GeneratorRuntime;
import art.arcane.iris.generation.noise.IrisExpression;
@@ -62,6 +64,7 @@ public final class PackRuntime {
private static final int MATERIAL_PALETTE_SEED_CAPACITY = 32;
private static final int CARVING_CHILD_SEED_CAPACITY = 8;
private static final int ORE_CHANCE_SEED_CAPACITY = 32;
+ private static final int ORE_BANDS_SEED_CAPACITY = 8;
private static final int SURFACE_WARP_SEED_CAPACITY = 8;
private final Engine engine;
@@ -77,6 +80,8 @@ public final class PackRuntime {
entry -> new SeedTable<>(CARVING_CHILD_SEED_CAPACITY, seed -> entry.createChildrenGenerator(seed, this)));
private final RuntimeTable> oreChances = new RuntimeTable<>(
ore -> new SeedTable<>(ORE_CHANCE_SEED_CAPACITY, seed -> ore.createChance(seed, this)));
+ private final RuntimeTable> oreBands = new RuntimeTable<>(
+ bands -> new SeedTable<>(ORE_BANDS_SEED_CAPACITY, seed -> new OreBandsRuntime(bands, seed, this)));
private final RuntimeTable> surfaceWarps = new RuntimeTable<>(
placement -> new SeedTable<>(SURFACE_WARP_SEED_CAPACITY, seed -> placement.createSurfaceWarp(seed, this)));
private final ConcurrentMap structureGraphs = new ConcurrentHashMap<>();
@@ -182,6 +187,11 @@ public final class PackRuntime {
return oreChances.get(ore).get(seed);
}
+ /** An ore list's chance fields for one seed, resolved together so painting a column holds them directly. */
+ public OreBandsRuntime oreBands(IrisOreBands bands, long seed) {
+ return oreBands.get(bands).get(seed);
+ }
+
/** Object placements warp by the engine's component seed, so a placement keeps one warp generator per seed. */
public CNG surfaceWarp(IrisObjectPlacement placement, long seed) {
return surfaceWarps.get(placement).get(seed);
diff --git a/core/src/main/java/art/arcane/iris/generation/stage/IrisTerrainNormalActuator.java b/core/src/main/java/art/arcane/iris/generation/stage/IrisTerrainNormalActuator.java
index a42b20fed..542d92e36 100644
--- a/core/src/main/java/art/arcane/iris/generation/stage/IrisTerrainNormalActuator.java
+++ b/core/src/main/java/art/arcane/iris/generation/stage/IrisTerrainNormalActuator.java
@@ -31,6 +31,7 @@ import art.arcane.iris.generation.hydrology.config.IrisHydrologyMaterials;
import art.arcane.iris.generation.hydrology.raster.WaterRole;
import art.arcane.iris.generation.terrain.IrisDimension;
import art.arcane.iris.generation.decoration.IrisOreBands;
+import art.arcane.iris.generation.decoration.OreBandsRuntime;
import art.arcane.iris.generation.terrain.IrisMaterialPalette;
import art.arcane.iris.generation.terrain.IrisRegion;
import art.arcane.iris.generation.terrain.Terrain3DColumn;
@@ -156,14 +157,14 @@ public class IrisTerrainNormalActuator extends EngineAssignedActuator rockCache;
private final ChunkedDataCache fluidCache;
private final UpperDimensionContext upperContext;
- private final IrisOreBands dimensionSurfaceOres;
- private final IrisOreBands dimensionUndergroundOres;
+ private final OreBandsRuntime dimensionSurfaceOres;
+ private final OreBandsRuntime dimensionUndergroundOres;
private final NativeBlockState air;
private final NativeBlockState bedrock;
- private IrisOreBands biomeSurfaceOres;
- private IrisOreBands regionSurfaceOres;
- private IrisOreBands biomeUndergroundOres;
- private IrisOreBands regionUndergroundOres;
+ private OreBandsRuntime biomeSurfaceOres;
+ private OreBandsRuntime regionSurfaceOres;
+ private OreBandsRuntime biomeUndergroundOres;
+ private OreBandsRuntime regionUndergroundOres;
private int surfaceOreTop;
private int surfaceOreFloor;
private int undergroundOreTop;
@@ -194,8 +195,8 @@ public class IrisTerrainNormalActuator extends EngineAssignedActuator layers;
private final NativeBlockState mapped;
private final NativeBlockState rock;
- private final IrisOreBands biomeSurface;
- private final IrisOreBands regionSurface;
- private final IrisOreBands dimensionSurface;
- private final IrisOreBands biomeUnderground;
- private final IrisOreBands regionUnderground;
- private final IrisOreBands dimensionUnderground;
+ private final OreBandsRuntime biomeSurface;
+ private final OreBandsRuntime regionSurface;
+ private final OreBandsRuntime dimensionSurface;
+ private final OreBandsRuntime biomeUnderground;
+ private final OreBandsRuntime regionUnderground;
+ private final OreBandsRuntime dimensionUnderground;
private StrataColumn(int x, int z, int surface, boolean submerged, KList layers,
NativeBlockState mapped, NativeBlockState rock, IrisBiome biome, IrisRegion region) {
@@ -88,12 +89,12 @@ public final class IrisStructureStrata implements StructureStrata {
this.layers = layers;
this.mapped = mapped;
this.rock = rock;
- biomeSurface = hideOres ? IrisOreBands.EMPTY : biome.getSurfaceOreBands();
- regionSurface = hideOres ? IrisOreBands.EMPTY : region.getSurfaceOreBands();
- dimensionSurface = hideOres ? IrisOreBands.EMPTY : dimension.getSurfaceOreBands();
- biomeUnderground = hideOres ? IrisOreBands.EMPTY : biome.getUndergroundOreBands();
- regionUnderground = hideOres ? IrisOreBands.EMPTY : region.getUndergroundOreBands();
- dimensionUnderground = hideOres ? IrisOreBands.EMPTY : dimension.getUndergroundOreBands();
+ biomeSurface = ores(biome.getSurfaceOreBands());
+ regionSurface = ores(region.getSurfaceOreBands());
+ dimensionSurface = ores(dimension.getSurfaceOreBands());
+ biomeUnderground = ores(biome.getUndergroundOreBands());
+ regionUnderground = ores(region.getUndergroundOreBands());
+ dimensionUnderground = ores(dimension.getUndergroundOreBands());
}
@Override
@@ -120,16 +121,20 @@ public final class IrisStructureStrata implements StructureStrata {
return layers.size();
}
- private NativeBlockState ore(IrisOreBands first, IrisOreBands second, IrisOreBands third, int internalY) {
+ private OreBandsRuntime ores(IrisOreBands bands) {
+ return runtime.oreBands(hideOres ? IrisOreBands.EMPTY : bands, rng.getSeed());
+ }
+
+ private NativeBlockState ore(OreBandsRuntime first, OreBandsRuntime second, OreBandsRuntime third, int internalY) {
NativeBlockState ore = null;
if (first.contains(internalY)) {
- ore = first.generate(x, internalY, z, rng, runtime);
+ ore = first.generate(x, internalY, z, rng);
}
if (ore == null && second.contains(internalY)) {
- ore = second.generate(x, internalY, z, rng, runtime);
+ ore = second.generate(x, internalY, z, rng);
}
if (ore == null && third.contains(internalY)) {
- ore = third.generate(x, internalY, z, rng, runtime);
+ ore = third.generate(x, internalY, z, rng);
}
return ore;
}
diff --git a/core/src/test/java/art/arcane/iris/generation/decoration/IrisOreBandsTest.java b/core/src/test/java/art/arcane/iris/generation/decoration/IrisOreBandsTest.java
index 464936c83..389e34805 100644
--- a/core/src/test/java/art/arcane/iris/generation/decoration/IrisOreBandsTest.java
+++ b/core/src/test/java/art/arcane/iris/generation/decoration/IrisOreBandsTest.java
@@ -48,7 +48,7 @@ public class IrisOreBandsTest {
}
for (int x = -3; x <= 3; x++) {
assertSame("trial " + trial + " y " + y + " x " + x,
- scan(ores, x, y, 5 - x), bands.generate(x, y, 5 - x, RNG_SOURCE, PackRuntime.standalone(null)));
+ scan(ores, x, y, 5 - x), generate(bands, x, y, 5 - x));
}
}
}
@@ -84,6 +84,10 @@ public class IrisOreBandsTest {
}
}
+ private static NativeBlockState generate(IrisOreBands bands, int x, int y, int z) {
+ return PackRuntime.standalone(null).oreBands(bands, RNG_SOURCE.getSeed()).generate(x, y, z, RNG_SOURCE);
+ }
+
private static NativeBlockState scan(KList ores, int x, int y, int z) {
for (IrisOreGenerator ore : ores) {
NativeBlockState state = ore.generate(x, y, z, RNG_SOURCE, PackRuntime.standalone(null));
@@ -110,10 +114,10 @@ public class IrisOreBandsTest {
ores.add(generator(1D, 57D, 1D, 2L));
ores.add(generator(58D, 58D, 1D, 3L));
IrisOreBands bands = IrisOreBands.of(ores);
- assertSame(scan(ores, 0, 0, 0), bands.generate(0, 0, 0, RNG_SOURCE, PackRuntime.standalone(null)));
- assertSame(scan(ores, 0, 30, 0), bands.generate(0, 30, 0, RNG_SOURCE, PackRuntime.standalone(null)));
- assertSame(scan(ores, 0, 58, 0), bands.generate(0, 58, 0, RNG_SOURCE, PackRuntime.standalone(null)));
- assertNull(bands.contains(59) ? bands.generate(0, 59, 0, RNG_SOURCE, PackRuntime.standalone(null)) : null);
+ assertSame(scan(ores, 0, 0, 0), generate(bands, 0, 0, 0));
+ assertSame(scan(ores, 0, 30, 0), generate(bands, 0, 30, 0));
+ assertSame(scan(ores, 0, 58, 0), generate(bands, 0, 58, 0));
+ assertNull(bands.contains(59) ? generate(bands, 0, 59, 0) : null);
}
private static final class FixedPalette extends IrisMaterialPalette {
private final NativeBlockState state;
diff --git a/core/src/test/java/art/arcane/iris/structure/placement/IrisStructureStrataTest.java b/core/src/test/java/art/arcane/iris/structure/placement/IrisStructureStrataTest.java
index e52b2b75c..61b05d244 100644
--- a/core/src/test/java/art/arcane/iris/structure/placement/IrisStructureStrataTest.java
+++ b/core/src/test/java/art/arcane/iris/structure/placement/IrisStructureStrataTest.java
@@ -3,7 +3,11 @@ package art.arcane.iris.structure.placement;
import art.arcane.iris.generation.runtime.PackRuntime;
import art.arcane.iris.testsupport.MockPackData;
import art.arcane.iris.generation.biome.IrisBiome;
+import art.arcane.iris.generation.block.IrisBlockData;
import art.arcane.iris.generation.decoration.IrisOreBands;
+import art.arcane.iris.generation.decoration.IrisOreGenerator;
+import art.arcane.iris.generation.terrain.IrisMaterialPalette;
+import art.arcane.iris.pack.value.IrisRange;
import art.arcane.iris.generation.image.IrisImageMapRuntime;
import art.arcane.iris.generation.runtime.Engine;
import art.arcane.iris.generation.runtime.IrisComplex;
@@ -110,11 +114,12 @@ public class IrisStructureStrataTest {
public void oreBandsAreSampledAtEachHeightSoAFillNeverStretchesOneOreBlock() {
dimension.setHideOresForHiddenOre(false);
NativeBlockState coal = state("minecraft:coal_ore");
- IrisOreBands bands = mock(IrisOreBands.class);
+ IrisOreGenerator coalSeam = new IrisOreGenerator()
+ .setPalette(new FixedPalette(coal))
+ .setThreshold(1D)
+ .setRange(new IrisRange(40 - MIN_HEIGHT, 40 - MIN_HEIGHT));
when(biome.getSurfaceOreBands()).thenReturn(IrisOreBands.EMPTY);
- when(biome.getUndergroundOreBands()).thenReturn(bands);
- when(bands.contains(anyInt())).thenReturn(true);
- when(bands.generate(eq(3), eq(40 - MIN_HEIGHT), eq(5), any(RNG.class), MockPackData.runtimeOf(data))).thenReturn(coal);
+ when(biome.getUndergroundOreBands()).thenReturn(IrisOreBands.of(new KList<>(List.of(coalSeam))));
StructureStrata.Column column = IrisStructureStrata.world(engine).column(3, 5, 90, false);
@@ -122,6 +127,20 @@ public class IrisStructureStrataTest {
assertSame(rock, column.block(41));
}
+ private static final class FixedPalette extends IrisMaterialPalette {
+ private final NativeBlockState state;
+
+ private FixedPalette(NativeBlockState state) {
+ this.state = state;
+ setPalette(new KList<>(new IrisBlockData()));
+ }
+
+ @Override
+ public NativeBlockState get(RNG rng, double x, double y, double z, PackRuntime runtime) {
+ return state;
+ }
+ }
+
private static NativeBlockState state(String key) {
NativeBlockState state = mock(NativeBlockState.class);
when(state.key()).thenReturn(key);