Reach hollows whose only way to the sky crosses a chunk's edge

A chunk traces the sky's open air within itself, so a pit dug under an overhang whose opening lay in the chunk beside it
seemed closed off, and its walls left a hole in the 3D world. Each chunk keeps the open cells along its sides the sky
did not reach; where one touches the open air of the chunk beside it, the chunk is traced again from there, until none
is left.
This commit is contained in:
piexel committed 2026-11-12 12:17:45 +01:00
1 parent 541419285d
commit 87b088787b
4 files changed
+170 -18

No files matched your search

@@ -19,6 +19,8 @@
package art.arcane.iris.core.studio.map;
import it.unimi.dsi.fastutil.booleans.BooleanArrayList;
import it.unimi.dsi.fastutil.ints.IntArrayList;
import it.unimi.dsi.fastutil.ints.IntList;
import it.unimi.dsi.fastutil.shorts.ShortArrayList;
import java.util.ArrayList;
@@ -50,11 +52,12 @@ record ChunkBorders(Wall[] walls) {
* One side's cells, each a run of cells one above the other.
*
* @param open the runs the sky's open air fills; {@link Runs#fluid} tells which hold water
* @param sealed the runs of cells as open, which the sky was not found to reach from within the chunk
* @param hidden the runs of blocks that left out their face toward this side
*/
record Wall(Runs open, Runs hidden) {
record Wall(Runs open, Runs sealed, Runs hidden) {
int bytes() {
return open.bytes() + hidden.bytes();
return open.bytes() + sealed.bytes() + hidden.bytes();
}
}
@@ -106,6 +109,28 @@ record ChunkBorders(Wall[] walls) {
}
}
/**
* The cells along {@code side} the chunk took for closed off from the sky that touch the open air of {@code beside},
* the chunk on that side: a hollow whose only way out is across the edge, such as a pit dug under an overhang. Each
* is a place in the chunk as {@link VisibleBlocks#positions()} gives it.
*/
IntList reachedFrom(ChunkBorders beside, int side) {
Runs sealed = walls[side - NORTH].sealed();
Runs open = beside.walls[opposite(side) - NORTH].open();
IntList reached = new IntArrayList();
for (int place = 0; place < ACROSS; place++) {
for (int run = sealed.firstOfPlace()[place]; run < sealed.firstOfPlace()[place + 1]; run++) {
for (int air = open.firstOfPlace()[place]; air < open.firstOfPlace()[place + 1]; air++) {
int top = Math.min(sealed.tops()[run], open.tops()[air]);
for (int y = Math.max(sealed.bottoms()[run], open.bottoms()[air]); y < top; y++) {
reached.add(VisibleBlocks.positionOf(xOf(side, place), y, zOf(side, place)));
}
}
}
}
return reached;
}
static int opposite(int side) {
return side ^ 1;
}
@@ -30,6 +30,9 @@ import art.arcane.iris.util.project.hunk.Hunk;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.github.benmanes.caffeine.cache.LoadingCache;
import it.unimi.dsi.fastutil.bytes.ByteArrayList;
import it.unimi.dsi.fastutil.ints.IntOpenHashSet;
import it.unimi.dsi.fastutil.ints.IntSet;
import it.unimi.dsi.fastutil.ints.IntSets;
import it.unimi.dsi.fastutil.longs.Long2IntOpenHashMap;
import it.unimi.dsi.fastutil.longs.Long2ObjectMap;
import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap;
@@ -85,6 +88,8 @@ final class GeneratedChunks implements ColumnTops {
private static final int GENERATING_THREADS = Math.max(2, Runtime.getRuntime().availableProcessors() / 2);
private static final long IDLE_THREAD_SECONDS = 30;
private static final long CANCEL_CHECK_MILLIS = 250;
// A hollow winding back and forth across chunk edges takes a round a crossing; past this many the rest stays closed.
private static final int MOST_REACHING_ROUNDS = 8;
private static final ThreadPoolExecutor GENERATORS = idleEndingThreads();
private final Engine engine;
@@ -219,9 +224,14 @@ final class GeneratedChunks implements ColumnTops {
@Override
public TileVoxels voxelsIn(int minX, int minZ, int size) {
int minChunkX = minX >> CHUNK_SHIFT;
int minChunkZ = minZ >> CHUNK_SHIFT;
int maxChunkX = (minX + size - 1) >> CHUNK_SHIFT;
int maxChunkZ = (minZ + size - 1) >> CHUNK_SHIFT;
reachHollowsAcrossEdges(minChunkX - 1, minChunkZ - 1, maxChunkX + 1, maxChunkZ + 1);
TileVoxelsBuilder voxels = new TileVoxelsBuilder(minX, minZ, size);
for (int chunkZ = minZ >> CHUNK_SHIFT; chunkZ <= (minZ + size - 1) >> CHUNK_SHIFT; chunkZ++) {
for (int chunkX = minX >> CHUNK_SHIFT; chunkX <= (minX + size - 1) >> CHUNK_SHIFT; chunkX++) {
for (int chunkZ = minChunkZ; chunkZ <= maxChunkZ; chunkZ++) {
for (int chunkX = minChunkX; chunkX <= maxChunkX; chunkX++) {
ChunkTop chunk = chunkAt(chunkX, chunkZ);
voxels.addVisible(chunk.visible(), chunkX << CHUNK_SHIFT, chunkZ << CHUNK_SHIFT);
addFacesAgainstNeighbours(voxels, chunk, chunkX, chunkZ);
@@ -230,12 +240,54 @@ final class GeneratedChunks implements ColumnTops {
return voxels.build();
}
// A chunk traces the sky's open air within itself, so a hollow whose only way out crosses its edge, such as a
// pit dug under an overhang, seems closed off. Each chunk such a hollow leads into is traced again from where the
// open air beside it touches the hollow, until none is left; the hollows it opens can lead on into the next.
private void reachHollowsAcrossEdges(int minChunkX, int minChunkZ, int maxChunkX, int maxChunkZ) {
boolean anyReached = true;
for (int round = 0; anyReached && round < MOST_REACHING_ROUNDS; round++) {
anyReached = false;
for (int chunkZ = minChunkZ; chunkZ <= maxChunkZ; chunkZ++) {
for (int chunkX = minChunkX; chunkX <= maxChunkX; chunkX++) {
anyReached |= retraceFromNeighbours(chunkX, chunkZ);
}
}
}
}
private boolean retraceFromNeighbours(int chunkX, int chunkZ) {
long key = keyOf(chunkX, chunkZ);
ChunkTop chunk = knownChunk(key);
if (chunk == null) {
return false;
}
IntSet reached = new IntOpenHashSet();
for (int side = ChunkBorders.NORTH; side <= ChunkBorders.EAST; side++) {
ChunkTop beside = knownChunk(keyOf(chunkX + VisibleBlocks.SIDES[side][0], chunkZ + VisibleBlocks.SIDES[side][2]));
if (beside != null) {
reached.addAll(chunk.visible().borders().reachedFrom(beside.visible().borders(), side));
}
}
if (reached.isEmpty()) {
return false;
}
reached.addAll(chunk.reachedBeside());
ChunkTop retraced = generateUnchecked(key, reached);
chunks.put(key, retraced);
tops.put(key, retraced);
return true;
}
// A neighbour neither the tile nor the cache holds was never generated, and the chunk's own judgement stands.
private ChunkTop knownChunk(long key) {
ChunkTop chunk = chunks.get(key);
return chunk != null ? chunk : tops.getIfPresent(key);
}
private void addFacesAgainstNeighbours(TileVoxelsBuilder voxels, ChunkTop chunk, int chunkX, int chunkZ) {
for (int side = ChunkBorders.NORTH; side <= ChunkBorders.EAST; side++) {
int[] step = VisibleBlocks.SIDES[side];
long key = keyOf(chunkX + step[0], chunkZ + step[2]);
ChunkTop beside = chunks.containsKey(key) ? chunks.get(key) : tops.getIfPresent(key);
ChunkTop beside = knownChunk(keyOf(chunkX + step[0], chunkZ + step[2]));
if (beside != null) {
int chunkMinX = chunkX << CHUNK_SHIFT;
int chunkMinZ = chunkZ << CHUNK_SHIFT;
@@ -408,6 +460,19 @@ final class GeneratedChunks implements ColumnTops {
}
private ChunkTop generate(long key) throws Exception {
return generate(key, IntSets.EMPTY_SET);
}
private ChunkTop generateUnchecked(long key, IntSet reachedBeside) {
try {
return generate(key, reachedBeside);
} catch (Exception failed) {
throw new CompletionException(failed);
}
}
/** @param reachedBeside the cells along the chunk's sides the open air of the chunks beside it reaches */
private ChunkTop generate(long key, IntSet reachedBeside) throws Exception {
int chunkX = (int) (key >> Integer.SIZE);
int chunkZ = (int) key;
int height = engine.getHeight();
@@ -423,7 +488,8 @@ final class GeneratedChunks implements ColumnTops {
for (int level = 1; level <= ColumnRuns.MOST_LEVEL; level++) {
runs[level - 1] = ColumnRuns.of(blocks, level, terrainY, this::seenByPlayers);
}
return new ChunkTop(topsOf(blocks, terrainY, this::seenByPlayers), VisibleBlocks.of(blocks, terrainY, fluidHeight, this::seenByPlayers), runs);
return new ChunkTop(topsOf(blocks, terrainY, this::seenByPlayers), VisibleBlocks.of(blocks, terrainY, fluidHeight, this::seenByPlayers, reachedBeside),
runs, reachedBeside);
}
// A plugin's block can be solid on the server where players see grass they walk through.
@@ -442,9 +508,10 @@ final class GeneratedChunks implements ColumnTops {
}
/**
* @param runs the chunk's runs of solid blocks in areas 2, 4, 8 and 16 blocks across, for coarser tiles
* @param runs the chunk's runs of solid blocks in areas 2, 4, 8 and 16 blocks across, for coarser tiles
* @param reachedBeside the cells along its sides found reached through the chunks beside it, which it was traced from
*/
private record ChunkTop(ColumnTop[] columns, VisibleBlocks visible, ColumnRuns[] runs) {
private record ChunkTop(ColumnTop[] columns, VisibleBlocks visible, ColumnRuns[] runs, IntSet reachedBeside) {
int bytes() {
int raisedBlocks = 0;
for (ColumnTop column : columns) {
@@ -455,7 +522,7 @@ final class GeneratedChunks implements ColumnTops {
runs += level.tops().length;
}
return CHUNK_TOP_BYTES + raisedBlocks * RAISED_BLOCK_BYTES + visible.count() * VISIBLE_BLOCK_BYTES + runs * RUN_BYTES
+ visible.borders().bytes();
+ visible.borders().bytes() + reachedBeside.size() * Integer.BYTES;
}
}
@@ -22,6 +22,8 @@ import art.arcane.iris.spi.PlatformBlockState;
import art.arcane.iris.util.project.hunk.Hunk;
import it.unimi.dsi.fastutil.bytes.ByteArrayList;
import it.unimi.dsi.fastutil.ints.IntArrayList;
import it.unimi.dsi.fastutil.ints.IntSet;
import it.unimi.dsi.fastutil.ints.IntSets;
import java.util.ArrayList;
import java.util.Arrays;
@@ -63,7 +65,21 @@ record VisibleBlocks(int[] positions, String[] blocks, byte[] faces, ChunkBorder
* @param seenByPlayers the state players see in place of a block, which decides what hides what
*/
static VisibleBlocks of(Hunk<PlatformBlockState> chunk, IntBinaryOperator terrainY, int fluidHeight, UnaryOperator<PlatformBlockState> seenByPlayers) {
return new Finder(chunk, terrainY, fluidHeight, seenByPlayers).find();
return of(chunk, terrainY, fluidHeight, seenByPlayers, IntSets.EMPTY_SET);
}
/**
* @param reachedBeside cells along the chunk's sides, as {@link #positionOf} places them, that the open air of the
* chunks beside it reaches, which the terrain's height there does not tell
*/
static VisibleBlocks of(Hunk<PlatformBlockState> chunk, IntBinaryOperator terrainY, int fluidHeight, UnaryOperator<PlatformBlockState> seenByPlayers,
IntSet reachedBeside) {
return new Finder(chunk, terrainY, fluidHeight, seenByPlayers, reachedBeside).find();
}
/** A block's place in its chunk, as {@link #positions} holds it. */
static int positionOf(int x, int y, int z) {
return x | z << Z_SHIFT | y << POSITION_Y_SHIFT;
}
@Override
@@ -91,11 +107,14 @@ record VisibleBlocks(int[] positions, String[] blocks, byte[] faces, ChunkBorder
private final boolean[] open;
private final boolean[] fluid;
private final boolean[] skyReaches;
private final IntSet reachedBeside;
Finder(Hunk<PlatformBlockState> chunk, IntBinaryOperator terrainY, int fluidHeight, UnaryOperator<PlatformBlockState> seenByPlayers) {
Finder(Hunk<PlatformBlockState> chunk, IntBinaryOperator terrainY, int fluidHeight, UnaryOperator<PlatformBlockState> seenByPlayers,
IntSet reachedBeside) {
this.chunk = chunk;
this.terrainY = terrainY;
this.fluidHeight = fluidHeight;
this.reachedBeside = reachedBeside;
this.height = chunk.getHeight();
int cells = ACROSS * height * ACROSS;
this.seen = new PlatformBlockState[cells];
@@ -142,7 +161,7 @@ record VisibleBlocks(int[] positions, String[] blocks, byte[] faces, ChunkBorder
if (sides == 0 && !(open[cell] && skyReaches[cell])) {
continue;
}
positions.add(x | z << Z_SHIFT | y << POSITION_Y_SHIFT);
positions.add(positionOf(x, y, z));
blocks.add(chunk.get(x, y, z).key());
faces.add((byte) sides);
}
@@ -160,7 +179,8 @@ record VisibleBlocks(int[] positions, String[] blocks, byte[] faces, ChunkBorder
}
private ChunkBorders.Wall wallOn(int side) {
ChunkBorders.RunsBuilder open = new ChunkBorders.RunsBuilder();
ChunkBorders.RunsBuilder reached = new ChunkBorders.RunsBuilder();
ChunkBorders.RunsBuilder sealed = new ChunkBorders.RunsBuilder();
ChunkBorders.RunsBuilder hidden = new ChunkBorders.RunsBuilder();
for (int place = 0; place < ACROSS; place++) {
int x = ChunkBorders.xOf(side, place);
@@ -168,16 +188,19 @@ record VisibleBlocks(int[] positions, String[] blocks, byte[] faces, ChunkBorder
for (int y = 0; y < height; y++) {
int cell = cellOf(x, y, z);
if (skyReaches[cell]) {
open.add(y, null, fluid[cell]);
reached.add(y, null, fluid[cell]);
} else if (open[cell]) {
sealed.add(y, null, fluid[cell]);
}
if (seen[cell] != null && !facesOutside(x, y, z, side)) {
hidden.add(y, chunk.get(x, y, z).key(), fluid[cell]);
}
}
open.nextPlace();
reached.nextPlace();
sealed.nextPlace();
hidden.nextPlace();
}
return new ChunkBorders.Wall(open.build(), hidden.build());
return new ChunkBorders.Wall(reached.build(), sealed.build(), hidden.build());
}
// Fills every open cell the sky reaches, from the top of the chunk and from beside it where the ground around
@@ -189,7 +212,7 @@ record VisibleBlocks(int[] positions, String[] blocks, byte[] faces, ChunkBorder
for (int z = 0; z < ACROSS; z++) {
for (int x = 0; x < ACROSS; x++) {
int cell = cellOf(x, y, z);
if (open[cell] && touchesOpenOutside(x, y, z)) {
if (open[cell] && (touchesOpenOutside(x, y, z) || reachedBeside.contains(positionOf(x, y, z)))) {
skyReaches[cell] = true;
queue[queued++] = cell;
}
@@ -2,6 +2,8 @@ package art.arcane.iris.core.studio.map;
import art.arcane.iris.spi.PlatformBlockState;
import art.arcane.iris.util.project.hunk.Hunk;
import it.unimi.dsi.fastutil.ints.IntOpenHashSet;
import it.unimi.dsi.fastutil.ints.IntSet;
import org.junit.Test;
import java.util.ArrayList;
@@ -9,6 +11,7 @@ import java.util.List;
import java.util.function.UnaryOperator;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@@ -59,6 +62,40 @@ public class ChunkBordersTest {
assertEquals(List.of("3,5,0 minecraft:stone"), faces);
}
@Test
public void aHollowUnderAnOverhangOpenOnlyAcrossTheEdgeIsReachedThroughTheChunkBesideIt() {
int top = 7;
Hunk<PlatformBlockState> overhang = groundUpTo(top);
for (int x = 0; x <= 1; x++) {
overhang.set(x, 4, 7, null);
overhang.set(x, 5, 7, null);
}
Hunk<PlatformBlockState> shaft = groundUpTo(top);
for (int y = 4; y <= top; y++) {
shaft.set(15, y, 7, null);
}
ChunkBorders shaftBorders = VisibleBlocks.of(shaft, (x, z) -> top, NO_FLUID, UnaryOperator.identity()).borders();
VisibleBlocks closed = VisibleBlocks.of(overhang, (x, z) -> top, NO_FLUID, UnaryOperator.identity());
IntSet reached = new IntOpenHashSet(closed.borders().reachedFrom(shaftBorders, ChunkBorders.WEST));
VisibleBlocks opened = VisibleBlocks.of(overhang, (x, z) -> top, NO_FLUID, UnaryOperator.identity(), reached);
assertEquals(IntSet.of(VisibleBlocks.positionOf(0, 4, 7), VisibleBlocks.positionOf(0, 5, 7)), reached);
assertFalse(shows(closed, VisibleBlocks.positionOf(2, 4, 7)));
assertTrue(shows(opened, VisibleBlocks.positionOf(2, 4, 7)));
assertTrue(shows(opened, VisibleBlocks.positionOf(1, 3, 7)));
assertTrue(opened.borders().reachedFrom(shaftBorders, ChunkBorders.WEST).isEmpty());
}
private static boolean shows(VisibleBlocks visible, int position) {
for (int at = 0; at < visible.count(); at++) {
if (visible.positions()[at] == position && visible.faces()[at] != 0) {
return true;
}
}
return false;
}
// The faces the first chunk shows on its side against the second, which lies on that side; the terrain's height
// beside each is the ground's, which knows nothing dug.
private static List<String> facesOf(Hunk<PlatformBlockState> chunk, Hunk<PlatformBlockState> beside, int side) {