Chunk-scoped cave lookups

- Subterrain cells for carve and decoration resolve through a per-chunk sampler with a column height hull instead of per-block engine dispatch
- IrisEngine subterrain lookups read the thread frame and runtime once and memoize the dimension gate, removing the per-call disabled-component list scan
- Unrouted workers resolve cave biomes through the captured carving snapshot and column cache with subterrain biomes checked first
- Captured cave biome lookups bypass the per-voxel hash cache
- Internal carve walls come from word-level column masks instead of hashed neighbor puts, painted after cheap solidity and surface checks
- Floor and ceiling layers are memoized per column when the biome layers do not depend on the zone height
- Zone scanning walks carved runs, the decoration scan memoizes block solidity and reuses the previous block, and waterlogging normalization skips snapshots without hydrology
- Boundary walls read own cells straight from the snapshot; the unused snapshot face builder and face accessors are removed
This commit is contained in:
Brian Neumann-Fopiano committed 2026-10-22 06:42:35 -04:00
1 parent af90765a92
commit 0bdb022f56
16 files changed
+790 -197

No files matched your search

@@ -144,20 +144,6 @@ public final class CaveTerrainSnapshot {
}
}
public MatterCavern[] composedFace(TerrainMatterView.Face face, int height) {
Objects.requireNonNull(face, "Terrain face");
if (height < 0) {
throw new IllegalArgumentException("Terrain face height must be nonnegative");
}
MatterCavern[] result = new MatterCavern[Math.multiplyExact(height, 16)];
for (int y = 0; y < height; y++) {
for (int offset = 0; offset < 16; offset++) {
result[y * 16 + offset] = composedCavern(face.x(offset), y, face.z(offset));
}
}
return result;
}
private void captureSection(int section, Matter matter) {
caverns[section] = copy(matter.getSlice(MatterCavern.class), MatterCavern[]::new);
hydrology[section] = copy(matter.getSlice(HydrologyCaveCell.class), HydrologyCaveCell[]::new);
@@ -136,12 +136,8 @@ public final class TerrainMatterView {
SOUTH(false, 15);
private final int[] positions = new int[256];
private final boolean fixedX;
private final int coordinate;
Face(boolean fixedX, int coordinate) {
this.fixedX = fixedX;
this.coordinate = coordinate;
for (int y = 0; y < 16; y++) {
for (int offset = 0; offset < 16; offset++) {
int x = fixedX ? coordinate : offset;
@@ -150,14 +146,6 @@ public final class TerrainMatterView {
}
}
}
int x(int offset) {
return fixedX ? coordinate : offset;
}
int z(int offset) {
return fixedX ? offset : coordinate;
}
}
public static <T> void iterate(MantleChunk<Matter> chunk, Class<T> type,
@@ -342,12 +342,8 @@ public interface Engine extends DataProvider, Fallible, BlockUpdater, Renderer,
@BlockCoordinates
default SubterrainCell getSubterrainCell(int x, int y, int z) {
if (getComplex() == null || getDimension() == null
|| !getDimension().allowsSubterrainFeatures()
|| getDimension().getSubterrainFeatures() == null || getDimension().getSubterrainFeatures().isEmpty()) {
return SubterrainCell.OUTSIDE;
}
SubterrainPlanner planner = getComplex().getSubterrainPlanner();
IrisComplex complex = getComplex();
SubterrainPlanner planner = complex == null ? null : SubterrainChunkCells.activePlanner(complex, getDimension());
return planner == null ? SubterrainCell.OUTSIDE : planner.sample(x, y + getWorld().minHeight(), z);
}
@@ -19,6 +19,7 @@
package art.arcane.iris.generation.runtime;
import art.arcane.iris.generation.subterrain.SubterrainCell;
import art.arcane.iris.generation.subterrain.SubterrainPlanner;
import art.arcane.iris.generation.runtime.EngineBackgroundTasks.BackgroundTaskDrain;
import art.arcane.iris.generation.runtime.EngineRuntimeBuilder.RuntimeAssembly;
import art.arcane.iris.pack.loading.IrisData;
@@ -178,6 +179,7 @@ public class IrisEngine implements Engine {
@Getter(AccessLevel.NONE)
@Setter(AccessLevel.NONE)
private volatile boolean generationHistoryRoutingRequired;
private volatile SubterrainGate subterrainGate;
@Getter(AccessLevel.NONE)
@Setter(AccessLevel.NONE)
volatile EngineTarget publishedTarget;
@@ -613,14 +615,55 @@ public class IrisEngine implements Engine {
@BlockCoordinates
@Override
public SubterrainCell getSubterrainCell(int x, int y, int z) {
if (!hasGenerationRuntimeScope() && generationHistoryRuntimeRouter != null) {
Optional<SubterrainCell> saved = generationHistoryRuntimeRouter.biomes()
.subterrainCell(x, y + getWorld().minHeight(), z);
EngineThreadState.Frame frame = threadState.current();
GenerationHistoryRuntimeRouter router = generationHistoryRuntimeRouter;
if ((frame == null || frame.binding == null) && router != null) {
Optional<SubterrainCell> saved = router.biomes()
.subterrainCell(x, y + targetIn(frame).getWorld().minHeight(), z);
if (saved.isPresent()) {
return saved.get();
}
}
return Engine.super.getSubterrainCell(x, y, z);
if (frame == null || frame.assembly == null && (frame.environment == null || frame.binding != null)) {
GenerationRuntime current = selectedGenerationRuntime(frame);
if (current == null) {
return SubterrainCell.OUTSIDE;
}
SubterrainPlanner planner = admittedSubterrainPlanner(current.complex(), current.dimension());
return planner == null ? SubterrainCell.OUTSIDE
: planner.sample(x, y + current.target().getWorld().minHeight(), z);
}
SubterrainPlanner planner = subterrainPlannerIn(frame);
return planner == null ? SubterrainCell.OUTSIDE
: planner.sample(x, y + targetIn(frame).getWorld().minHeight(), z);
}
@ChunkCoordinates
public SubterrainChunkCells subterrainChunkCells(int chunkX, int chunkZ) {
EngineThreadState.Frame frame = threadState.current();
if ((frame == null || frame.binding == null) && generationHistoryRuntimeRouter != null) {
return SubterrainChunkCells.delegated(this);
}
SubterrainPlanner planner = subterrainPlannerIn(frame);
return SubterrainChunkCells.planned(this, planner, chunkX, chunkZ,
planner == null ? 0 : targetIn(frame).getWorld().minHeight());
}
private SubterrainPlanner subterrainPlannerIn(EngineThreadState.Frame frame) {
IrisComplex complex = complexIn(frame);
return complex == null ? null : admittedSubterrainPlanner(complex, dimensionIn(frame));
}
private SubterrainPlanner admittedSubterrainPlanner(IrisComplex complex, IrisDimension dimension) {
if (complex == null || dimension == null) {
return null;
}
SubterrainGate gate = subterrainGate;
if (gate == null || gate.dimension() != dimension) {
gate = new SubterrainGate(dimension, SubterrainChunkCells.admits(dimension));
subterrainGate = gate;
}
return gate.admitted() ? complex.getSubterrainPlanner() : null;
}
@BlockCoordinates
@@ -1036,6 +1079,10 @@ public class IrisEngine implements Engine {
return threadState.binding() != null;
}
public boolean hasGenerationHistoryRouting() {
return generationHistoryRuntimeRouter != null || generationHistoryRoutingRequired;
}
private boolean usesScopedNaturalTerrain() {
return hasGenerationRuntimeScope() && getComplex().isNaturalTerrainContext();
}
@@ -1333,7 +1380,10 @@ public class IrisEngine implements Engine {
@Override
public IrisComplex getComplex() {
EngineThreadState.Frame frame = threadState.current();
return complexIn(threadState.current());
}
private IrisComplex complexIn(EngineThreadState.Frame frame) {
RuntimeAssembly assembly = frame == null ? null : frame.assembly;
if (assembly != null && assembly.complex != null) {
return assembly.complex;
@@ -1344,7 +1394,10 @@ public class IrisEngine implements Engine {
@Override
public EngineTarget getTarget() {
EngineThreadState.Frame frame = threadState.current();
return targetIn(threadState.current());
}
private EngineTarget targetIn(EngineThreadState.Frame frame) {
RuntimeAssembly assembly = frame == null ? null : frame.assembly;
if (assembly != null) {
return assembly.target;
@@ -1372,7 +1425,10 @@ public class IrisEngine implements Engine {
@Override
public IrisDimension getDimension() {
EngineThreadState.Frame frame = threadState.current();
return dimensionIn(threadState.current());
}
private IrisDimension dimensionIn(EngineThreadState.Frame frame) {
if (frame != null) {
BiomeEnvironmentBinding environment = frame.environment;
if (environment != null && frame.binding == null) {
@@ -1892,4 +1948,6 @@ public class IrisEngine implements Engine {
}
}
private record SubterrainGate(IrisDimension dimension, boolean admitted) {
}
}
@@ -0,0 +1,67 @@
package art.arcane.iris.generation.runtime;
import art.arcane.iris.generation.biome.IrisBiome;
import art.arcane.iris.generation.subterrain.SubterrainCell;
import art.arcane.iris.generation.subterrain.SubterrainPlanner;
import art.arcane.iris.generation.terrain.IrisDimension;
import java.util.Objects;
public final class SubterrainChunkCells {
private final Engine engine;
private final boolean delegated;
private final SubterrainPlanner.ChunkSampler sampler;
private final int minimumY;
private SubterrainChunkCells(Engine engine, boolean delegated, SubterrainPlanner.ChunkSampler sampler, int minimumY) {
this.engine = Objects.requireNonNull(engine);
this.delegated = delegated;
this.sampler = sampler;
this.minimumY = minimumY;
}
public static SubterrainChunkCells of(Engine engine, int chunkX, int chunkZ) {
return engine instanceof IrisEngine irisEngine
? irisEngine.subterrainChunkCells(chunkX, chunkZ)
: delegated(engine);
}
public static SubterrainChunkCells delegated(Engine engine) {
return new SubterrainChunkCells(engine, true, null, 0);
}
static SubterrainChunkCells planned(Engine engine, SubterrainPlanner planner, int chunkX, int chunkZ, int minimumY) {
return new SubterrainChunkCells(engine, false, planner == null ? null : planner.chunkSampler(chunkX, chunkZ), minimumY);
}
static SubterrainPlanner activePlanner(IrisComplex complex, IrisDimension dimension) {
if (complex == null || dimension == null || !admits(dimension)) {
return null;
}
return complex.getSubterrainPlanner();
}
static boolean admits(IrisDimension dimension) {
return dimension.allowsSubterrainFeatures()
&& dimension.getSubterrainFeatures() != null && !dimension.getSubterrainFeatures().isEmpty();
}
public SubterrainCell cell(int x, int y, int z) {
if (delegated) {
SubterrainCell cell = engine.getSubterrainCell(x, y, z);
return cell == null ? SubterrainCell.OUTSIDE : cell;
}
return sampler == null ? SubterrainCell.OUTSIDE : sampler.sample(x, y + minimumY, z);
}
public IrisBiome biome(int x, int y, int z) {
if (delegated) {
return engine.getSubterrainBiome(x, y, z);
}
SubterrainCell cell = cell(x, y, z);
if (!cell.occupied() || cell.room().biome().isEmpty()) {
return null;
}
return engine.getSubterrainBiome(x, y, z);
}
}
@@ -20,9 +20,7 @@ package art.arcane.iris.generation.stage;
import art.arcane.iris.pack.loading.IrisData;
import art.arcane.iris.generation.subterrain.SubterrainCell;
import art.arcane.iris.generation.subterrain.SubterrainPlan;
import art.arcane.iris.generation.subterrain.SubterrainPlanner;
import java.util.List;
import art.arcane.iris.generation.subterrain.SubterrainRasterizer;
import art.arcane.iris.generation.subterrain.SubterrainSurfaceMaterials;
import art.arcane.iris.generation.runtime.DimensionStackContext;
@@ -31,6 +29,7 @@ import art.arcane.iris.generation.runtime.UpperDimensionContext;
import art.arcane.iris.generation.terrain.Terrain3DColumn;
import art.arcane.iris.generation.runtime.Engine;
import art.arcane.iris.generation.runtime.IrisEngine;
import art.arcane.iris.generation.runtime.SubterrainChunkCells;
import art.arcane.iris.generation.runtime.IrisComplex;
import art.arcane.iris.generation.terrain.IrisDimension;
import art.arcane.volmlib.util.stream.ProceduralStream;
@@ -39,6 +38,7 @@ import art.arcane.iris.generation.mantle.CaveTerrainSnapshot;
import art.arcane.iris.generation.mantle.TerrainMatterView;
import art.arcane.iris.generation.terrain.InferredType;
import art.arcane.iris.generation.biome.IrisBiome;
import art.arcane.iris.generation.biome.IrisBiomePaletteLayer;
import art.arcane.iris.generation.decoration.IrisDecorationPart;
import art.arcane.iris.generation.decoration.IrisDecorator;
import art.arcane.iris.generation.terrain.IrisDimensionCarvingResolver;
@@ -101,7 +101,7 @@ public class IrisCarveModifier extends EngineAssignedModifier<NativeBlockState>
public void onModify(int x, int z, Hunk<NativeBlockState> output, boolean multicore, ChunkContext context) {
PrecisionStopwatch caveStopwatch = PrecisionStopwatch.start();
Mantle<Matter> mantle = getEngine().getMantle().getMantle();
CaveInputs resolverState = new CaveInputs(getEngine());
CaveInputs resolverState = CaveInputs.forTerrainChunk(getEngine(), x, z);
IrisCarveScratch scratch = SCRATCH.get();
scratch.reset();
Long2ObjectOpenHashMap<IrisBiome> caveBiomeCache = scratch.caveBiomeCache;
@@ -144,6 +144,7 @@ public class IrisCarveModifier extends EngineAssignedModifier<NativeBlockState>
}
int worldHeightSpan = getEngine().getWorld().maxHeight() - getEngine().getWorld().minHeight();
int caveLavaHeight = getEngine().getDimension().getCaveLavaHeight();
SubterrainPlanner subterrainPlanner = resolverState.complex.getSubterrainPlanner();
CarveResolutionContext resolutionContext = new CarveResolutionContext(
output,
context,
@@ -156,44 +157,28 @@ public class IrisCarveModifier extends EngineAssignedModifier<NativeBlockState>
caveLavaHeight,
chunkBlockX,
chunkBlockZ,
getComplex().getSubterrainPlanner(),
getComplex().getSubterrainPlanner() == null ? List.of() : getComplex().getSubterrainPlanner().plansForBounds(chunkBlockX - 1, chunkBlockZ - 1, chunkBlockX + 16, chunkBlockZ + 16)
getEngine().getWorld().minHeight(),
resolverState.complex,
subterrainPlanner == null ? null : subterrainPlanner.chunkSampler(x, z)
);
CarveResolver carveResolver = new CarveResolver(resolutionContext);
terrain.forEachCell(carveResolver::apply);
if (scratch.customCaveBiomePresent) {
addInternalWallsFromMantle(terrain, walls, columnMasks);
} else {
addInternalWallsFromMasks(walls, columnMasks);
collectInternalWalls(scratch.wallMasks, columnMasks);
}
addCrossChunkBoundaryWalls(mantle, terrain, walls, boundaryMasks, x, z, surfaceHeights);
getEngine().getMetrics().getCarveResolve().put(resolveStopwatch.getMilliseconds());
PrecisionStopwatch applyStopwatch = PrecisionStopwatch.start();
try {
IrisData wallData = getData();
walls.forEach((rx, yy, rz, cavern) -> {
HydrologyCaveCell hydrology = terrain.hydrology(rx, yy, rz);
if (hydrology != null && hydrology.protectsPlacement()
|| subterrainCell(rx + chunkBlockX, yy, rz + chunkBlockZ).owned()) {
return;
}
int worldX = rx + chunkBlockX;
int worldZ = rz + chunkBlockZ;
String customBiome = cavern.getCustomBiome();
IrisBiome biome = customBiome.isEmpty()
? resolveCaveBiome(caveBiomeCache, worldX, yy, worldZ, resolverState)
: resolveCustomBiome(customBiomeCache, customBiome);
if (biome != null) {
NativeBlockState data = biome.getWall().get(rng, worldX, yy, worldZ, wallData);
int columnIndex = PowerOfTwoCoordinates.packLocal16(rx, rz);
if (data != null && B.isSolid(output.getRaw(rx, yy, rz)) && yy < surfaceHeights[columnIndex]) {
output.setRaw(rx, yy, rz, data);
}
}
});
WallPainter painter = new WallPainter(output, terrain, resolverState, caveBiomeCache, customBiomeCache,
surfaceHeights, getData(), chunkBlockX, chunkBlockZ);
if (!scratch.customCaveBiomePresent) {
paintInternalWalls(painter, scratch.wallMasks, boundaryMasks);
}
walls.forEach(painter);
for (int columnIndex = 0; columnIndex < 256; columnIndex++) {
processColumnFromMask(
@@ -205,7 +190,8 @@ public class IrisCarveModifier extends EngineAssignedModifier<NativeBlockState>
z,
resolverState,
caveBiomeCache,
customBiomeCache
customBiomeCache,
scratch.layers
);
}
@@ -223,7 +209,8 @@ public class IrisCarveModifier extends EngineAssignedModifier<NativeBlockState>
z,
resolverState,
caveBiomeCache,
customBiomeCache
customBiomeCache,
scratch.layers
);
}
@@ -257,11 +244,6 @@ public class IrisCarveModifier extends EngineAssignedModifier<NativeBlockState>
}
}
private SubterrainCell subterrainCell(int x, int y, int z) {
SubterrainCell cell = getEngine().getSubterrainCell(x, y, z);
return cell == null ? SubterrainCell.OUTSIDE : cell;
}
public void preserveSubterrain(int blockX, int blockZ, Hunk<NativeBlockState> output) {
SubterrainPlanner planner = getComplex().getSubterrainPlanner();
if (planner == null || planner.isEmpty()) {
@@ -437,8 +419,9 @@ public class IrisCarveModifier extends EngineAssignedModifier<NativeBlockState>
int localX = x & 15;
int localZ = z & 15;
int columnIndex = PowerOfTwoCoordinates.packLocal16(localX, localZ);
SubterrainCell feature = context.subterrainPlanner() == null ? SubterrainCell.OUTSIDE : context.subterrainPlanner().sample(context.subterrainPlans(),
context.chunkBlockX() + localX, y + getEngine().getWorld().minHeight(), context.chunkBlockZ() + localZ);
SubterrainCell feature = context.subterrainSampler() == null ? SubterrainCell.OUTSIDE
: context.subterrainSampler().sampleCandidates(context.chunkBlockX() + localX, y + context.minimumY(),
context.chunkBlockZ() + localZ);
if (feature.owned() && (!feature.solid() || feature.room().reservedSolid())) {
context.output().setRaw(localX, y, localZ, SubterrainRasterizer.state(feature));
if (feature.carve()) {
@@ -492,7 +475,7 @@ public class IrisCarveModifier extends EngineAssignedModifier<NativeBlockState>
if (isFluidIntent(cavern)) {
fluid = hydrology == null
? context.chunkContext().getFluid().get(localX, localZ)
: getComplex().resolveHydrologyFluid(
: context.complex().resolveHydrologyFluid(
hydrology.fluidProfileKey(),
context.chunkBlockX() + localX,
context.chunkBlockZ() + localZ
@@ -526,35 +509,87 @@ public class IrisCarveModifier extends EngineAssignedModifier<NativeBlockState>
int caveLavaHeight,
int chunkBlockX,
int chunkBlockZ,
SubterrainPlanner subterrainPlanner,
List<SubterrainPlan> subterrainPlans
int minimumY,
IrisComplex complex,
SubterrainPlanner.ChunkSampler subterrainSampler
) {
}
private void addInternalWallsFromMasks(CarveWallBuffer walls, CarveColumnMask[] columnMasks) {
static void collectInternalWalls(CarveColumnMask[] wallMasks, CarveColumnMask[] columnMasks) {
for (int columnIndex = 0; columnIndex < 256; columnIndex++) {
CarveColumnMask columnMask = columnMasks[columnIndex];
if (columnMask.isEmpty()) {
continue;
}
int rx = columnIndex >> 4;
int rz = columnIndex & 15;
int yy = columnMask.nextSetBit(0);
while (yy >= 0) {
if (rz < 15 && !columnMasks[columnIndex + 1].contains(yy)) {
walls.put(rx, yy, rz + 1, BASIC_CAVERN);
wallMasks[columnIndex].collectWalls(
columnMasks[columnIndex],
rz > 0 ? columnMasks[columnIndex - 1] : null,
rz < 15 ? columnMasks[columnIndex + 1] : null,
rx > 0 ? columnMasks[columnIndex - 16] : null,
rx < 15 ? columnMasks[columnIndex + 16] : null
);
}
}
private static void paintInternalWalls(WallPainter painter, CarveColumnMask[] wallMasks, CarveColumnMask[] boundaryMasks) {
for (int columnIndex = 0; columnIndex < 256; columnIndex++) {
CarveColumnMask wallMask = wallMasks[columnIndex];
if (wallMask.isEmpty()) {
continue;
}
int rx = columnIndex >> 4;
int rz = columnIndex & 15;
CarveColumnMask boundaryMask = boundaryMasks[columnIndex];
for (int yy = wallMask.nextSetBit(0); yy >= 0; yy = wallMask.nextSetBit(yy + 1)) {
if (!boundaryMask.contains(yy)) {
painter.accept(rx, yy, rz, BASIC_CAVERN);
}
if (rx < 15 && !columnMasks[columnIndex + 16].contains(yy)) {
walls.put(rx + 1, yy, rz, BASIC_CAVERN);
}
if (rz > 0 && !columnMasks[columnIndex - 1].contains(yy)) {
walls.put(rx, yy, rz - 1, BASIC_CAVERN);
}
if (rx > 0 && !columnMasks[columnIndex - 16].contains(yy)) {
walls.put(rx - 1, yy, rz, BASIC_CAVERN);
}
yy = columnMask.nextSetBit(yy + 1);
}
}
}
private final class WallPainter implements CarveWallBuffer.Consumer {
private final Hunk<NativeBlockState> output;
private final CaveTerrainSnapshot terrain;
private final CaveInputs inputs;
private final Long2ObjectOpenHashMap<IrisBiome> caveBiomeCache;
private final Map<String, IrisBiome> customBiomeCache;
private final int[] surfaceHeights;
private final IrisData data;
private final int chunkBlockX;
private final int chunkBlockZ;
private WallPainter(Hunk<NativeBlockState> output, CaveTerrainSnapshot terrain, CaveInputs inputs,
Long2ObjectOpenHashMap<IrisBiome> caveBiomeCache, Map<String, IrisBiome> customBiomeCache,
int[] surfaceHeights, IrisData data, int chunkBlockX, int chunkBlockZ) {
this.output = output;
this.terrain = terrain;
this.inputs = inputs;
this.caveBiomeCache = caveBiomeCache;
this.customBiomeCache = customBiomeCache;
this.surfaceHeights = surfaceHeights;
this.data = data;
this.chunkBlockX = chunkBlockX;
this.chunkBlockZ = chunkBlockZ;
}
@Override
public void accept(int rx, int yy, int rz, MatterCavern cavern) {
HydrologyCaveCell hydrology = terrain.hydrology(rx, yy, rz);
int worldX = rx + chunkBlockX;
int worldZ = rz + chunkBlockZ;
if (hydrology != null && hydrology.protectsPlacement() || inputs.cells.cell(worldX, yy, worldZ).owned()
|| yy >= surfaceHeights[PowerOfTwoCoordinates.packLocal16(rx, rz)] || !B.isSolid(output.getRaw(rx, yy, rz))) {
return;
}
String customBiome = cavern.getCustomBiome();
IrisBiome biome = customBiome.isEmpty()
? resolveCaveBiome(caveBiomeCache, worldX, yy, worldZ, inputs)
: resolveCustomBiome(customBiomeCache, customBiome);
if (biome == null) {
return;
}
NativeBlockState wall = biome.getWall().get(rng, worldX, yy, worldZ, data);
if (wall != null) {
output.setRaw(rx, yy, rz, wall);
}
}
}
@@ -621,10 +656,6 @@ public class IrisCarveModifier extends EngineAssignedModifier<NativeBlockState>
}
int height = maxY + 1;
MatterCavern[] ownWest = west == null ? null : terrain.composedFace(TerrainMatterView.Face.WEST, height);
MatterCavern[] ownEast = east == null ? null : terrain.composedFace(TerrainMatterView.Face.EAST, height);
MatterCavern[] ownNorth = north == null ? null : terrain.composedFace(TerrainMatterView.Face.NORTH, height);
MatterCavern[] ownSouth = south == null ? null : terrain.composedFace(TerrainMatterView.Face.SOUTH, height);
MatterCavern[] fromWest = west == null ? null : TerrainMatterView.getComposedFace(west, TerrainMatterView.Face.EAST, height);
MatterCavern[] fromEast = east == null ? null : TerrainMatterView.getComposedFace(east, TerrainMatterView.Face.WEST, height);
MatterCavern[] fromNorth = north == null ? null : TerrainMatterView.getComposedFace(north, TerrainMatterView.Face.SOUTH, height);
@@ -633,16 +664,16 @@ public class IrisCarveModifier extends EngineAssignedModifier<NativeBlockState>
for (int offset = 0; offset < 16; offset++) {
int index = yy * 16 + offset;
if (west != null) {
tryAddBoundaryWall(ownWest[index], fromWest[index], walls, boundaryMasks, 0, yy, offset);
tryAddBoundaryWall(terrain.composedCavern(0, yy, offset), fromWest[index], walls, boundaryMasks, 0, yy, offset);
}
if (east != null) {
tryAddBoundaryWall(ownEast[index], fromEast[index], walls, boundaryMasks, 15, yy, offset);
tryAddBoundaryWall(terrain.composedCavern(15, yy, offset), fromEast[index], walls, boundaryMasks, 15, yy, offset);
}
if (north != null) {
tryAddBoundaryWall(ownNorth[index], fromNorth[index], walls, boundaryMasks, offset, yy, 0);
tryAddBoundaryWall(terrain.composedCavern(offset, yy, 0), fromNorth[index], walls, boundaryMasks, offset, yy, 0);
}
if (south != null) {
tryAddBoundaryWall(ownSouth[index], fromSouth[index], walls, boundaryMasks, offset, yy, 15);
tryAddBoundaryWall(terrain.composedCavern(offset, yy, 15), fromSouth[index], walls, boundaryMasks, offset, yy, 15);
}
}
}
@@ -682,7 +713,8 @@ public class IrisCarveModifier extends EngineAssignedModifier<NativeBlockState>
int chunkZ,
CaveInputs resolverState,
Long2ObjectOpenHashMap<IrisBiome> caveBiomeCache,
Map<String, IrisBiome> customBiomeCache
Map<String, IrisBiome> customBiomeCache,
CaveLayerMemo layers
) {
if (columnMask == null || columnMask.isEmpty()) {
return;
@@ -697,33 +729,19 @@ public class IrisCarveModifier extends EngineAssignedModifier<NativeBlockState>
int rz = columnIndex & 15;
int worldX = rx + PowerOfTwoCoordinates.chunkToBlock(chunkX);
int worldZ = rz + PowerOfTwoCoordinates.chunkToBlock(chunkZ);
CaveZone zone = new CaveZone();
zone.setFloor(firstHeight);
int buf = firstHeight - 1;
int y = firstHeight;
while (y >= 0) {
if (y <= resolverState.height) {
if (y == buf + 1) {
buf = y;
zone.ceiling = buf;
} else {
if (zone.isValid(resolverState.height)) {
processZone(output, terrain, zone, rx, rz, worldX, worldZ, resolverState,
caveBiomeCache, customBiomeCache);
}
zone = new CaveZone();
zone.setFloor(y);
buf = y;
}
int height = resolverState.height;
layers.reset();
int floor = firstHeight;
while (floor >= 0 && floor <= height) {
int end = columnMask.nextClearBit(floor) - 1;
CaveZone zone = new CaveZone();
zone.setFloor(floor);
zone.setCeiling(Math.min(end, height));
if (zone.isValid(height)) {
processZone(output, terrain, zone, rx, rz, worldX, worldZ, resolverState,
caveBiomeCache, customBiomeCache, layers);
}
y = columnMask.nextSetBit(y + 1);
}
if (zone.isValid(resolverState.height)) {
processZone(output, terrain, zone, rx, rz, worldX, worldZ, resolverState,
caveBiomeCache, customBiomeCache);
floor = columnMask.nextSetBit(end + 1);
}
}
@@ -737,7 +755,8 @@ public class IrisCarveModifier extends EngineAssignedModifier<NativeBlockState>
int chunkZ,
CaveInputs resolverState,
Long2ObjectOpenHashMap<IrisBiome> caveBiomeCache,
Map<String, IrisBiome> customBiomeCache
Map<String, IrisBiome> customBiomeCache,
CaveLayerMemo layers
) {
int firstHeight = boundaryMask.nextSetBit(0);
if (firstHeight < 0) {
@@ -748,6 +767,7 @@ public class IrisCarveModifier extends EngineAssignedModifier<NativeBlockState>
int rz = columnIndex & 15;
int worldX = rx + PowerOfTwoCoordinates.chunkToBlock(chunkX);
int worldZ = rz + PowerOfTwoCoordinates.chunkToBlock(chunkZ);
layers.reset();
int zoneFloor = firstHeight;
int zoneCeiling = firstHeight;
int y = boundaryMask.nextSetBit(firstHeight + 1);
@@ -757,7 +777,7 @@ public class IrisCarveModifier extends EngineAssignedModifier<NativeBlockState>
zoneCeiling = y;
} else {
paintBoundaryZone(output, terrain, walls, rx, rz, worldX, worldZ, zoneFloor, zoneCeiling,
resolverState, caveBiomeCache, customBiomeCache);
resolverState, caveBiomeCache, customBiomeCache, layers);
zoneFloor = y;
zoneCeiling = y;
}
@@ -765,7 +785,7 @@ public class IrisCarveModifier extends EngineAssignedModifier<NativeBlockState>
}
paintBoundaryZone(output, terrain, walls, rx, rz, worldX, worldZ, zoneFloor, zoneCeiling,
resolverState, caveBiomeCache, customBiomeCache);
resolverState, caveBiomeCache, customBiomeCache, layers);
}
private void paintBoundaryZone(
@@ -780,7 +800,8 @@ public class IrisCarveModifier extends EngineAssignedModifier<NativeBlockState>
int zoneCeiling,
CaveInputs resolverState,
Long2ObjectOpenHashMap<IrisBiome> caveBiomeCache,
Map<String, IrisBiome> customBiomeCache
Map<String, IrisBiome> customBiomeCache,
CaveLayerMemo layers
) {
IrisBiome floorBiome = resolveCaveBoundaryBiome(
walls.get(rx, zoneFloor, rz), worldX, zoneFloor, worldZ,
@@ -794,9 +815,8 @@ public class IrisCarveModifier extends EngineAssignedModifier<NativeBlockState>
if (floorBiome != null) {
HydrologyCaveCell floorHydrology = terrain.hydrology(rx, zoneFloor, rz);
IrisRiverMaterialConfig bedMaterial = undergroundBedMaterial();
KList<NativeBlockState> floorLayers = floorBiome.generateLayers(
resolverState.dimension, worldX, worldZ, rng, 3, zoneFloor, resolverState.data, resolverState.complex);
IrisRiverMaterialConfig bedMaterial = undergroundBedMaterial(resolverState.dimension);
KList<NativeBlockState> floorLayers = floorLayers(layers, floorBiome, worldX, worldZ, zoneFloor, resolverState);
for (int i = 0; i < zoneFloor - 1; i++) {
if (!floorLayers.hasIndex(i)) {
break;
@@ -833,8 +853,7 @@ public class IrisCarveModifier extends EngineAssignedModifier<NativeBlockState>
if (ceilingBiome != null) {
int worldMaxY = getEngine().getWorld().maxHeight() - getEngine().getWorld().minHeight();
KList<NativeBlockState> ceilingLayers = ceilingBiome.generateCeilingLayers(
resolverState.dimension, worldX, worldZ, rng, 3, zoneCeiling, resolverState.data, resolverState.complex);
KList<NativeBlockState> ceilingLayers = ceilingLayers(layers, ceilingBiome, worldX, worldZ, zoneCeiling, resolverState);
for (int i = 0; i < ceilingLayers.size(); i++) {
int ceilingY = zoneCeiling + i + 1;
if (ceilingY >= worldMaxY) {
@@ -879,7 +898,8 @@ public class IrisCarveModifier extends EngineAssignedModifier<NativeBlockState>
CaveZone zone, int rx, int rz, int xx, int zz,
CaveInputs resolverState,
Long2ObjectOpenHashMap<IrisBiome> caveBiomeCache,
Map<String, IrisBiome> customBiomeCache) {
Map<String, IrisBiome> customBiomeCache,
CaveLayerMemo layers) {
int maxY = output.getHeight();
if (zone.ceiling + 1 < maxY && B.isDecorant(output.getRaw(rx, zone.ceiling + 1, rz))) {
@@ -899,8 +919,8 @@ public class IrisCarveModifier extends EngineAssignedModifier<NativeBlockState>
if (floorBiome != null) {
HydrologyCaveCell floorHydrology = terrain.hydrology(rx, zone.floor, rz);
IrisRiverMaterialConfig bedMaterial = undergroundBedMaterial();
KList<NativeBlockState> floorBlocks = floorBiome.generateLayers(resolverState.dimension, xx, zz, rng, 3, zone.floor, resolverState.data, resolverState.complex);
IrisRiverMaterialConfig bedMaterial = undergroundBedMaterial(resolverState.dimension);
KList<NativeBlockState> floorBlocks = floorLayers(layers, floorBiome, xx, zz, zone.floor, resolverState);
for (int i = 0; i < zone.floor - 1; i++) {
if (!floorBlocks.hasIndex(i)) {
break;
@@ -933,7 +953,7 @@ public class IrisCarveModifier extends EngineAssignedModifier<NativeBlockState>
}
if (ceilingBiome != null) {
KList<NativeBlockState> ceilingBlocks = ceilingBiome.generateCeilingLayers(resolverState.dimension, xx, zz, rng, 3, zone.ceiling, resolverState.data, resolverState.complex);
KList<NativeBlockState> ceilingBlocks = ceilingLayers(layers, ceilingBiome, xx, zz, zone.ceiling, resolverState);
for (int i = 0; i < ceilingBlocks.size(); i++) {
int cy = zone.ceiling + i + 1;
if (cy >= maxY) {
@@ -961,6 +981,44 @@ public class IrisCarveModifier extends EngineAssignedModifier<NativeBlockState>
normalizeCaveZoneWaterlogging(output, terrain, zone, rx, rz, xx, zz);
}
private KList<NativeBlockState> floorLayers(CaveLayerMemo layers, IrisBiome biome, int worldX, int worldZ, int floorY,
CaveInputs inputs) {
if (!hasHeightFreeFloorLayers(biome)) {
return biome.generateLayers(inputs.dimension, worldX, worldZ, rng, 3, floorY, inputs.data, inputs.complex);
}
KList<NativeBlockState> remembered = layers.floor(biome);
if (remembered != null) {
return remembered;
}
KList<NativeBlockState> blocks = biome.generateLayers(inputs.dimension, worldX, worldZ, rng, 3, floorY, inputs.data, inputs.complex);
layers.rememberFloor(biome, blocks);
return blocks;
}
private KList<NativeBlockState> ceilingLayers(CaveLayerMemo layers, IrisBiome biome, int worldX, int worldZ, int ceilingY,
CaveInputs inputs) {
KList<NativeBlockState> remembered = layers.ceiling(biome);
if (remembered != null) {
return remembered;
}
KList<NativeBlockState> blocks = biome.generateCeilingLayers(inputs.dimension, worldX, worldZ, rng, 3, ceilingY, inputs.data, inputs.complex);
layers.rememberCeiling(biome, blocks);
return blocks;
}
static boolean hasHeightFreeFloorLayers(IrisBiome biome) {
if (biome.isLockLayers()) {
return false;
}
KList<IrisBiomePaletteLayer> layers = biome.getLayers();
for (int index = 0; index < layers.size(); index++) {
if (!layers.get(index).getSlopeCondition().isDefault()) {
return false;
}
}
return true;
}
public void decorateNaturalCaves(int blockX, int blockZ, Hunk<NativeBlockState> output, ChunkContext context) {
Mantle<Matter> mantle = getEngine().getMantle().getMantle();
CaveTerrainSnapshot terrain = context == null ? null : context.getCaveTerrain();
@@ -983,26 +1041,30 @@ public class IrisCarveModifier extends EngineAssignedModifier<NativeBlockState>
int depth = output.getDepth();
int height = output.getHeight();
ColumnExtent extent = output instanceof ColumnExtent columnExtent ? columnExtent : null;
SolidMemo solids = new SolidMemo();
for (int localX = 0; localX < width; localX++) {
for (int localZ = 0; localZ < depth; localZ++) {
int worldX = blockX + localX;
int worldZ = blockZ + localZ;
int floor = -1;
int top = extent == null ? height - 1 : extent.highestStoredY(localX, localZ);
boolean previousKnown = false;
boolean previousSolid = false;
for (int y = 1; y < height; y++) {
if (y > top) {
break;
}
NativeBlockState state = output.getRaw(localX, y, localZ);
if (B.isSolid(state)) {
boolean solid = solids.test(output.getRaw(localX, y, localZ));
boolean decorated = false;
if (solid) {
if (floor >= 0) {
CaveZone zone = new CaveZone();
zone.setFloor(floor);
zone.setCeiling(y - 1);
SubterrainCell floorCell = zone.isValid(resolver.height)
? subterrainCell(worldX, floor, worldZ) : null;
? resolver.cells.cell(worldX, floor, worldZ) : null;
if (floorCell != null && (floorCell.occupied()
|| !isNaturalTerrainOpening(getComplex(), worldX, floor, worldZ, context))) {
|| !isNaturalTerrainOpening(resolver.complex, worldX, floor, worldZ, context))) {
if (caveFloorMarkers && markerRoll(worldX, zone.floor, worldZ, 0xC2B2AE3D27D4EB4FL)) {
mantle.set(worldX, zone.floor, worldZ, MarkerMatter.CAVE_FLOOR);
}
@@ -1012,15 +1074,19 @@ public class IrisCarveModifier extends EngineAssignedModifier<NativeBlockState>
worldX, worldZ, resolver, caveBiomes, customBiomes);
decorateZone(output, zone, localX, localZ, worldX, worldZ,
floorBiome, ceilingBiome, floorCell);
decorated = true;
if (extent != null) {
top = extent.highestStoredY(localX, localZ);
}
}
floor = -1;
}
} else if (floor < 0 && B.isSolid(output.getRaw(localX, y - 1, localZ))) {
} else if (floor < 0 && (previousKnown ? previousSolid
: solids.test(output.getRaw(localX, y - 1, localZ)))) {
floor = y;
}
previousSolid = solid;
previousKnown = !decorated;
}
}
}
@@ -1057,6 +1123,9 @@ public class IrisCarveModifier extends EngineAssignedModifier<NativeBlockState>
int worldX,
int worldZ
) {
if (!terrain.hasHydrology()) {
return;
}
int minimumY = Math.max(0, zone.floor - 1);
int maximumY = Math.min(output.getHeight() - 1, zone.ceiling + 1);
for (int y = minimumY; y <= maximumY; y++) {
@@ -1090,7 +1159,7 @@ public class IrisCarveModifier extends EngineAssignedModifier<NativeBlockState>
}
IrisBiome resolveCaveBoundaryBiome(MatterCavern cavern, int worldX, int y, int worldZ, CaveInputs resolverState, Long2ObjectOpenHashMap<IrisBiome> caveBiomeCache, Map<String, IrisBiome> customBiomeCache) {
IrisBiome featureBiome = getEngine().getSubterrainBiome(worldX, y, worldZ);
IrisBiome featureBiome = resolverState.cells.biome(worldX, y, worldZ);
if (featureBiome != null) {
return featureBiome;
}
@@ -1127,8 +1196,8 @@ public class IrisCarveModifier extends EngineAssignedModifier<NativeBlockState>
}
/** The dimension's underground river bed palette, or null when nothing paints. */
private IrisRiverMaterialConfig undergroundBedMaterial() {
IrisHydrology hydrology = getDimension().getHydrology();
private static IrisRiverMaterialConfig undergroundBedMaterial(IrisDimension dimension) {
IrisHydrology hydrology = dimension.getHydrology();
if (hydrology == null) {
return null;
}
@@ -1256,6 +1325,9 @@ public class IrisCarveModifier extends EngineAssignedModifier<NativeBlockState>
}
private IrisBiome sampleCaveBiome(Long2ObjectOpenHashMap<IrisBiome> caveBiomeCache, int x, int y, int z, CaveInputs resolverState) {
if (resolverState.captures(x, z)) {
return resolverState.resolve(x, y, z);
}
long key = BlockPosition.toLong(x, y, z);
IrisBiome cachedBiome = caveBiomeCache.get(key);
if (cachedBiome != null) {
@@ -1287,6 +1359,8 @@ public class IrisCarveModifier extends EngineAssignedModifier<NativeBlockState>
private final int height;
private final IrisDimensionCarvingResolver.State state = new IrisDimensionCarvingResolver.State();
private final IrisDimensionCarvingResolver.Snapshot snapshot;
private final SubterrainChunkCells cells;
private final boolean subterrainFirst;
private final int minimumY;
private final DimensionStackContext stack;
private final ProceduralStream<IrisBiome> surfaceBiomes;
@@ -1301,6 +1375,10 @@ public class IrisCarveModifier extends EngineAssignedModifier<NativeBlockState>
this(engine, false, 0, 0);
}
static CaveInputs forTerrainChunk(Engine engine, int chunkX, int chunkZ) {
return new CaveInputs(engine, false, chunkX, chunkZ);
}
/**
* Inputs for the content stage of one chunk. A coordinate inside that chunk resolves under the chunk's
* own generation route, exactly the binding the natural terrain stage samples with, so it takes the
@@ -1316,14 +1394,19 @@ public class IrisCarveModifier extends EngineAssignedModifier<NativeBlockState>
dimension = engine.getDimension();
data = engine.getData();
height = engine.getHeight();
cells = SubterrainChunkCells.of(engine, chunkX, chunkZ);
IrisContext context = IrisContext.get();
boolean generating = engine instanceof IrisEngine irisEngine
&& irisEngine.hasGenerationRuntimeScope() && !engine.getPlatformHooks().isMainThread()
IrisEngine irisEngine = engine instanceof IrisEngine candidate ? candidate : null;
boolean bound = irisEngine != null && irisEngine.hasGenerationRuntimeScope();
boolean worker = irisEngine != null && !engine.getPlatformHooks().isMainThread();
boolean generating = bound && worker
&& context != null && context.getChunkContext() != null
&& context.getChunkContext().getComplex() == complex;
boolean natural = generating && context.getChunkContext().isNaturalTerrain();
boolean scoped = natural || generating && contentChunk;
this.chunkLimited = !natural;
boolean unrouted = !bound && worker && !irisEngine.hasGenerationHistoryRouting();
boolean scoped = natural || generating && contentChunk || unrouted;
this.subterrainFirst = unrouted;
this.chunkLimited = !natural && !unrouted;
this.chunkX = chunkX;
this.chunkZ = chunkZ;
snapshot = scoped ? IrisDimensionCarvingResolver.snapshot(engine) : null;
@@ -1335,10 +1418,20 @@ public class IrisCarveModifier extends EngineAssignedModifier<NativeBlockState>
columns = scoped ? new CaveColumn[1024] : null;
}
boolean captures(int x, int z) {
return snapshot != null && (!chunkLimited || (x >> 4) == chunkX && (z >> 4) == chunkZ);
}
IrisBiome resolve(int x, int y, int z) {
if (snapshot == null || chunkLimited && ((x >> 4) != chunkX || (z >> 4) != chunkZ)) {
if (!captures(x, z)) {
return engine.getCaveBiome(x, y, z, state);
}
if (subterrainFirst) {
IrisBiome feature = cells.biome(x, y, z);
if (feature != null) {
return feature;
}
}
IrisBiome configured = snapshot.resolveBiome(x, y + minimumY, z);
if (configured != null) {
return configured;
@@ -1372,6 +1465,19 @@ public class IrisCarveModifier extends EngineAssignedModifier<NativeBlockState>
private record CaveColumn(long key, IrisBiome surface, IrisBiome cave, int surfaceY, int minimumDepth) {
}
private static final class SolidMemo {
private NativeBlockState state;
private boolean solid;
private boolean test(NativeBlockState candidate) {
if (candidate != state) {
state = candidate;
solid = B.isSolid(candidate);
}
return solid;
}
}
@Data
public static class CaveZone {
private int ceiling = -1;
@@ -19,6 +19,8 @@
package art.arcane.iris.generation.stage;
import art.arcane.iris.generation.biome.IrisBiome;
import art.arcane.volmlib.nativelib.terrain.NativeBlockState;
import art.arcane.volmlib.util.collection.KList;
import art.arcane.volmlib.util.matter.MatterCavern;
import art.arcane.volmlib.util.math.PowerOfTwoCoordinates;
import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap;
@@ -30,10 +32,12 @@ import java.util.Map;
final class IrisCarveScratch {
final CarveColumnMask[] columnMasks = new CarveColumnMask[256];
final CarveColumnMask[] boundaryMasks = new CarveColumnMask[256];
final CarveColumnMask[] wallMasks = new CarveColumnMask[256];
final int[] surfaceHeights = new int[256];
final CarveWallBuffer walls = new CarveWallBuffer(512);
final Map<String, IrisBiome> customBiomeCache = new HashMap<>();
final Long2ObjectOpenHashMap<IrisBiome> caveBiomeCache = new Long2ObjectOpenHashMap<>(4096);
final CaveLayerMemo layers = new CaveLayerMemo();
int[] upperSurfaceHeights;
boolean customCaveBiomePresent;
@@ -41,6 +45,7 @@ final class IrisCarveScratch {
for (int index = 0; index < columnMasks.length; index++) {
columnMasks[index] = new CarveColumnMask();
boundaryMasks[index] = new CarveColumnMask();
wallMasks[index] = new CarveColumnMask();
}
}
@@ -55,6 +60,7 @@ final class IrisCarveScratch {
for (int index = 0; index < columnMasks.length; index++) {
columnMasks[index].clear();
boundaryMasks[index].clear();
wallMasks[index].clear();
}
walls.clear();
customBiomeCache.clear();
@@ -63,6 +69,52 @@ final class IrisCarveScratch {
}
}
final class CaveLayerMemo {
private static final int SLOTS = 4;
private final Layers[] floor = new Layers[SLOTS];
private final Layers[] ceiling = new Layers[SLOTS];
private int floorCount;
private int ceilingCount;
void reset() {
floorCount = 0;
ceilingCount = 0;
}
KList<NativeBlockState> floor(IrisBiome biome) {
return find(floor, floorCount, biome);
}
KList<NativeBlockState> ceiling(IrisBiome biome) {
return find(ceiling, ceilingCount, biome);
}
void rememberFloor(IrisBiome biome, KList<NativeBlockState> blocks) {
if (floorCount < SLOTS) {
floor[floorCount++] = new Layers(biome, blocks);
}
}
void rememberCeiling(IrisBiome biome, KList<NativeBlockState> blocks) {
if (ceilingCount < SLOTS) {
ceiling[ceilingCount++] = new Layers(biome, blocks);
}
}
private static KList<NativeBlockState> find(Layers[] entries, int count, IrisBiome biome) {
for (int index = 0; index < count; index++) {
if (entries[index].biome() == biome) {
return entries[index].blocks();
}
}
return null;
}
private record Layers(IrisBiome biome, KList<NativeBlockState> blocks) {
}
}
final class CarveColumnMask {
private long[] words = new long[8];
private int maxWord = -1;
@@ -108,10 +160,59 @@ final class CarveColumnMask {
}
}
int nextClearBit(int fromBit) {
int startBit = Math.max(0, fromBit);
int wordIndex = startBit >> 6;
if (wordIndex > maxWord) {
return startBit;
}
long word = ~words[wordIndex] & (-1L << (startBit & 63));
while (true) {
if (word != 0L) {
return (wordIndex << 6) + Long.numberOfTrailingZeros(word);
}
wordIndex++;
if (wordIndex > maxWord) {
return wordIndex << 6;
}
word = ~words[wordIndex];
}
}
boolean isEmpty() {
return maxWord < 0;
}
void collectWalls(CarveColumnMask own, CarveColumnMask first, CarveColumnMask second,
CarveColumnMask third, CarveColumnMask fourth) {
int top = Math.max(Math.max(topWord(first), topWord(second)), Math.max(topWord(third), topWord(fourth)));
clear();
if (top < 0) {
return;
}
if (top >= words.length) {
words = Arrays.copyOf(words, top + 1);
}
for (int index = 0; index <= top; index++) {
long walls = (word(first, index) | word(second, index) | word(third, index) | word(fourth, index))
& ~word(own, index);
words[index] = walls;
if (walls != 0L) {
maxWord = index;
}
}
}
private static int topWord(CarveColumnMask mask) {
return mask == null ? -1 : mask.maxWord;
}
private static long word(CarveColumnMask mask, int index) {
return mask == null || index > mask.maxWord ? 0L : mask.words[index];
}
boolean contains(int y) {
if (y < 0) {
return false;
@@ -1,6 +1,7 @@
package art.arcane.iris.generation.subterrain;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
@@ -113,6 +114,10 @@ public final class SubterrainPlanner {
return sample(candidates(Math.floorDiv(x, 16), Math.floorDiv(z, 16)), x, y, z);
}
public ChunkSampler chunkSampler(int chunkX, int chunkZ) {
return new ChunkSampler(this, chunkX, chunkZ, isEmpty() ? List.of() : candidates(chunkX, chunkZ));
}
public SubterrainCell sample(List<SubterrainPlan> candidates, int x, int y, int z) {
SubterrainCell selected = raw(candidates, x, y, z);
if (!selected.fluid()) {
@@ -238,6 +243,79 @@ public final class SubterrainPlanner {
}
}
public static final class ChunkSampler {
private final SubterrainPlanner planner;
private final int chunkX;
private final int chunkZ;
private final List<SubterrainPlan> plans;
private final int[] columnMinY;
private final int[] columnMaxY;
private ChunkSampler(SubterrainPlanner planner, int chunkX, int chunkZ, List<SubterrainPlan> plans) {
this.planner = planner;
this.chunkX = chunkX;
this.chunkZ = chunkZ;
this.plans = plans;
if (plans.isEmpty()) {
columnMinY = null;
columnMaxY = null;
return;
}
columnMinY = new int[256];
columnMaxY = new int[256];
Arrays.fill(columnMinY, Integer.MAX_VALUE);
Arrays.fill(columnMaxY, Integer.MIN_VALUE);
int baseX = chunkX << 4;
int baseZ = chunkZ << 4;
for (int index = 0; index < plans.size(); index++) {
SubterrainBounds bounds = plans.get(index).bounds();
int lowX = Math.max(bounds.minX(), baseX) - baseX;
int highX = Math.min(bounds.maxX(), baseX + 15) - baseX;
int lowZ = Math.max(bounds.minZ(), baseZ) - baseZ;
int highZ = Math.min(bounds.maxZ(), baseZ + 15) - baseZ;
for (int localX = lowX; localX <= highX; localX++) {
for (int localZ = lowZ; localZ <= highZ; localZ++) {
int column = (localX << 4) | localZ;
columnMinY[column] = Math.min(columnMinY[column], bounds.minY());
columnMaxY[column] = Math.max(columnMaxY[column], bounds.maxY());
}
}
}
}
public boolean isEmpty() {
return plans.isEmpty();
}
public SubterrainCell sample(int x, int y, int z) {
if ((x >> 4) != chunkX || (z >> 4) != chunkZ) {
return planner.sample(x, y, z);
}
if (plans.isEmpty() || y < planner.minY || y >= planner.maxYExclusive) {
return SubterrainCell.OUTSIDE;
}
return sampleColumn(x, y, z);
}
public SubterrainCell sampleCandidates(int x, int y, int z) {
if (plans.isEmpty()) {
return SubterrainCell.OUTSIDE;
}
if ((x >> 4) != chunkX || (z >> 4) != chunkZ) {
return planner.sample(plans, x, y, z);
}
return sampleColumn(x, y, z);
}
private SubterrainCell sampleColumn(int x, int y, int z) {
int column = ((x & 15) << 4) | (z & 15);
if (y < columnMinY[column] || y > columnMaxY[column]) {
return SubterrainCell.OUTSIDE;
}
return planner.sample(plans, x, y, z);
}
}
private record CandidateTile(int chunkX, int chunkZ, List<SubterrainPlan> plans) {
}