diff --git a/core/src/main/java/art/arcane/iris/core/studio/map/ChunkBorders.java b/core/src/main/java/art/arcane/iris/core/studio/map/ChunkBorders.java new file mode 100644 index 000000000..aac6cabaf --- /dev/null +++ b/core/src/main/java/art/arcane/iris/core/studio/map/ChunkBorders.java @@ -0,0 +1,163 @@ +/* + * 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.map; + +import it.unimi.dsi.fastutil.booleans.BooleanArrayList; +import it.unimi.dsi.fastutil.shorts.ShortArrayList; + +import java.util.ArrayList; +import java.util.List; + +/** + * The cells along a chunk's four sides. A chunk judges what lies beside it by the terrain's height there, which knows + * nothing dug out of the ground, such as a structure set into a hill or a cave mouth across the chunk's edge, so it + * leaves out the faces it has against such open air. Once the chunk beside it is known, the faces it left out against + * that chunk's open air are shown after all. + * + * @param walls each side's cells, in the order of {@link VisibleBlocks#SIDES} from north on + */ +record ChunkBorders(Wall[] walls) { + /** The first horizontal side in {@link VisibleBlocks#SIDES}; north, south, west and east follow in that order. */ + static final int NORTH = 2; + static final int SOUTH = 3; + static final int WEST = 4; + static final int EAST = 5; + private static final int ACROSS = 16; + private static final int LAST = ACROSS - 1; + + /** Takes a face a chunk shows on {@code side}, at its x, y and z in the chunk. */ + interface FaceSink { + void add(int x, int y, int z, String block, int side); + } + + /** + * 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 hidden the runs of blocks that left out their face toward this side + */ + record Wall(Runs open, Runs hidden) { + int bytes() { + return open.bytes() + hidden.bytes(); + } + } + + /** + * Runs of cells, those of each place along the side together, from the west or the north. + * + * @param firstOfPlace where each place's runs start, with one more for where the last place's end + * @param bottoms each run's lowest cell + * @param tops the cell above each run's highest + * @param blocks each run's block, or null for runs of open air + * @param fluid whether each run holds water + */ + record Runs(int[] firstOfPlace, short[] bottoms, short[] tops, String[] blocks, boolean[] fluid) { + // A bottom, a top, a shared key and the fluid flag. + private static final int RUN_BYTES = Short.BYTES + Short.BYTES + Integer.BYTES + 1; + + int bytes() { + return firstOfPlace.length * Integer.BYTES + tops.length * RUN_BYTES; + } + } + + int bytes() { + int bytes = 0; + for (Wall wall : walls) { + bytes += wall.bytes(); + } + return bytes; + } + + /** + * Each face this chunk left out on {@code side} that faces open air in {@code beside}, the chunk on that side. Water + * shows no face against water, as within a chunk. + */ + void facesAgainst(ChunkBorders beside, int side, FaceSink faces) { + Runs hidden = walls[side - NORTH].hidden(); + Runs open = beside.walls[opposite(side) - NORTH].open(); + for (int place = 0; place < ACROSS; place++) { + for (int run = hidden.firstOfPlace()[place]; run < hidden.firstOfPlace()[place + 1]; run++) { + for (int air = open.firstOfPlace()[place]; air < open.firstOfPlace()[place + 1]; air++) { + if (hidden.fluid()[run] && open.fluid()[air]) { + continue; + } + int top = Math.min(hidden.tops()[run], open.tops()[air]); + for (int y = Math.max(hidden.bottoms()[run], open.bottoms()[air]); y < top; y++) { + faces.add(xOf(side, place), y, zOf(side, place), hidden.blocks()[run], side); + } + } + } + } + } + + static int opposite(int side) { + return side ^ 1; + } + + static int xOf(int side, int place) { + return switch (side) { + case WEST -> 0; + case EAST -> LAST; + default -> place; + }; + } + + static int zOf(int side, int place) { + return switch (side) { + case NORTH -> 0; + case SOUTH -> LAST; + default -> place; + }; + } + + /** Gathers a side's runs place by place, from the bottom of each place up. */ + static final class RunsBuilder { + private final int[] firstOfPlace = new int[ACROSS + 1]; + private final ShortArrayList bottoms = new ShortArrayList(); + private final ShortArrayList tops = new ShortArrayList(); + private final List blocks = new ArrayList<>(); + private final BooleanArrayList fluid = new BooleanArrayList(); + private int place; + + /** Starts the runs of the next place along the side. */ + void nextPlace() { + firstOfPlace[++place] = tops.size(); + } + + /** Adds the cell at {@code y} of the current place, joining the run below where it is of the same. */ + void add(int y, String block, boolean holdsFluid) { + int last = tops.size() - 1; + boolean joins = last >= firstOfPlace[place] && tops.getShort(last) == y && fluid.getBoolean(last) == holdsFluid + && (block == null ? blocks.get(last) == null : block.equals(blocks.get(last))); + if (joins) { + tops.set(last, (short) (y + 1)); + return; + } + bottoms.add((short) y); + tops.add((short) (y + 1)); + blocks.add(block); + fluid.add(holdsFluid); + } + + Runs build() { + firstOfPlace[ACROSS] = tops.size(); + return new Runs(firstOfPlace, bottoms.toShortArray(), tops.toShortArray(), blocks.toArray(String[]::new), fluid.toBooleanArray()); + } + } +} diff --git a/core/src/main/java/art/arcane/iris/core/studio/map/GeneratedChunks.java b/core/src/main/java/art/arcane/iris/core/studio/map/GeneratedChunks.java index f435d2591..fe9f921a4 100644 --- a/core/src/main/java/art/arcane/iris/core/studio/map/GeneratedChunks.java +++ b/core/src/main/java/art/arcane/iris/core/studio/map/GeneratedChunks.java @@ -30,10 +30,13 @@ 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.longs.Long2IntOpenHashMap; import it.unimi.dsi.fastutil.longs.Long2ObjectMap; import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap; import it.unimi.dsi.fastutil.longs.LongArrayList; import it.unimi.dsi.fastutil.longs.LongList; +import it.unimi.dsi.fastutil.longs.LongOpenHashSet; +import it.unimi.dsi.fastutil.longs.LongSet; import it.unimi.dsi.fastutil.shorts.ShortArrayList; import java.util.ArrayList; @@ -42,6 +45,7 @@ import java.util.List; import java.util.Map; import java.util.Optional; import java.util.concurrent.CancellationException; +import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletionException; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ExecutionException; @@ -129,6 +133,11 @@ final class GeneratedChunks implements ColumnTops { GENERATORS.execute(task); generating.add(task); } + if (showsBlocksOneByOne(grid)) { + for (long key : neighboursAround(columnsByChunk.keySet())) { + generating.add(generateBeside(key, grid, chunks)); + } + } try { for (Future task : generating) { awaitUnlessCancelled(task, grid.cancelled()); @@ -140,6 +149,42 @@ final class GeneratedChunks implements ColumnTops { return new PreparedTile(found, chunks); } + // A tile's blocks show their faces on its edge against what the chunks beside it really hold, so the tile is the same + // whichever of its neighbours were generated first. Those chunks are the next tiles' anyway. + private static boolean showsBlocksOneByOne(TileGrid grid) { + return grid.step() <= 1D; + } + + private Future generateBeside(long key, TileGrid grid, Map chunks) { + ChunkTop known = tops.getIfPresent(key); + if (known != null) { + chunks.put(key, known); + return CompletableFuture.completedFuture(null); + } + ChunkTask task = new ChunkTask(() -> { + if (!grid.cancelled().getAsBoolean()) { + chunks.put(key, tops.get(key)); + } + }, grid.step()); + GENERATORS.execute(task); + return task; + } + + private static LongSet neighboursAround(LongSet chunks) { + LongSet around = new LongOpenHashSet(); + for (long key : chunks) { + int chunkX = (int) (key >> Integer.SIZE); + int chunkZ = (int) key; + for (int side = ChunkBorders.NORTH; side <= ChunkBorders.EAST; side++) { + long beside = keyOf(chunkX + VisibleBlocks.SIDES[side][0], chunkZ + VisibleBlocks.SIDES[side][2]); + if (!chunks.contains(beside)) { + around.add(beside); + } + } + } + return around; + } + // A tile a block a column has no runs; a coarser one has those of its columns' areas, or of the largest a chunk has. private static int runLevelOf(double blocksPerColumn) { int level = (int) Math.round(Math.log(blocksPerColumn) / Math.log(2)); @@ -174,42 +219,87 @@ final class GeneratedChunks implements ColumnTops { @Override public TileVoxels voxelsIn(int minX, int minZ, int size) { - TileVoxelsBuilder voxels = new TileVoxelsBuilder(); + 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++) { - long key = keyOf(chunkX, chunkZ); - ChunkTop chunk = chunks.get(key); - voxels.addInside(chunk != null ? chunk : tops.get(key), chunkX << CHUNK_SHIFT, chunkZ << CHUNK_SHIFT, minX, minZ, size); + ChunkTop chunk = chunkAt(chunkX, chunkZ); + voxels.addVisible(chunk.visible(), chunkX << CHUNK_SHIFT, chunkZ << CHUNK_SHIFT); + addFacesAgainstNeighbours(voxels, chunk, chunkX, chunkZ); } } return voxels.build(); } + + // A neighbour neither the tile nor the cache holds was never generated, and the chunk's own judgement stands. + 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); + if (beside != null) { + int chunkMinX = chunkX << CHUNK_SHIFT; + int chunkMinZ = chunkZ << CHUNK_SHIFT; + chunk.visible().borders().facesAgainst(beside.visible().borders(), side, + (x, y, z, block, faceSide) -> voxels.add(chunkMinX + x, y, chunkMinZ + z, block, 1 << faceSide)); + } + } + } + + private ChunkTop chunkAt(int chunkX, int chunkZ) { + long key = keyOf(chunkX, chunkZ); + ChunkTop chunk = chunks.get(key); + return chunk != null ? chunk : tops.get(key); + } } + /** The visible blocks of a square of the world, by their place in it; those outside the square are left out. */ private static final class TileVoxelsBuilder { + private static final int NOT_ADDED = -1; + private final Long2IntOpenHashMap indexByPlace = new Long2IntOpenHashMap(); + private final int minX; + private final int minZ; + private final int size; private final ByteArrayList x = new ByteArrayList(); private final ByteArrayList z = new ByteArrayList(); private final ShortArrayList y = new ShortArrayList(); private final List blocks = new ArrayList<>(); private final ByteArrayList faces = new ByteArrayList(); - void addInside(ChunkTop chunk, int chunkMinX, int chunkMinZ, int minX, int minZ, int size) { - VisibleBlocks visible = chunk.visible(); + TileVoxelsBuilder(int minX, int minZ, int size) { + this.minX = minX; + this.minZ = minZ; + this.size = size; + } + + void addVisible(VisibleBlocks visible, int chunkMinX, int chunkMinZ) { for (int at = 0; at < visible.count(); at++) { int position = visible.positions()[at]; - int squareX = chunkMinX + (position & CHUNK_MASK) - minX; - int squareZ = chunkMinZ + ((position >> CHUNK_SHIFT) & CHUNK_MASK) - minZ; - if (squareX < 0 || squareZ < 0 || squareX >= size || squareZ >= size) { - continue; - } - x.add((byte) squareX); - z.add((byte) squareZ); - y.add((short) (position >> VisibleBlocks.POSITION_Y_SHIFT)); - blocks.add(visible.blocks()[at]); - faces.add(visible.faces()[at]); + add(chunkMinX + (position & CHUNK_MASK), position >> VisibleBlocks.POSITION_Y_SHIFT, + chunkMinZ + ((position >> CHUNK_SHIFT) & CHUNK_MASK), visible.blocks()[at], visible.faces()[at]); } } + /** Adds a block at its place in the world with the sides it shows, or those sides to the block already there. */ + void add(int worldX, int blockY, int worldZ, String block, int sides) { + int squareX = worldX - minX; + int squareZ = worldZ - minZ; + if (squareX < 0 || squareZ < 0 || squareX >= size || squareZ >= size) { + return; + } + long place = ((long) blockY << Integer.SIZE) | ((long) squareZ << Short.SIZE) | squareX; + int known = indexByPlace.getOrDefault(place, NOT_ADDED); + if (known != NOT_ADDED) { + faces.set(known, (byte) (faces.getByte(known) | sides)); + return; + } + indexByPlace.put(place, faces.size()); + x.add((byte) squareX); + z.add((byte) squareZ); + y.add((short) blockY); + blocks.add(block); + faces.add((byte) sides); + } + TileVoxels build() { return new TileVoxels(x.toByteArray(), z.toByteArray(), y.toShortArray(), blocks.toArray(String[]::new), faces.toByteArray()); } @@ -364,7 +454,8 @@ final class GeneratedChunks implements ColumnTops { for (ColumnRuns level : this.runs) { runs += level.tops().length; } - return CHUNK_TOP_BYTES + raisedBlocks * RAISED_BLOCK_BYTES + visible.count() * VISIBLE_BLOCK_BYTES + runs * RUN_BYTES; + return CHUNK_TOP_BYTES + raisedBlocks * RAISED_BLOCK_BYTES + visible.count() * VISIBLE_BLOCK_BYTES + runs * RUN_BYTES + + visible.borders().bytes(); } } diff --git a/core/src/main/java/art/arcane/iris/core/studio/map/VisibleBlocks.java b/core/src/main/java/art/arcane/iris/core/studio/map/VisibleBlocks.java index 9dd012b22..48af3f836 100644 --- a/core/src/main/java/art/arcane/iris/core/studio/map/VisibleBlocks.java +++ b/core/src/main/java/art/arcane/iris/core/studio/map/VisibleBlocks.java @@ -39,9 +39,10 @@ import java.util.function.UnaryOperator; * dimension's bottom * @param blocks each block's key * @param faces the sides of each block that face the open air, a bit each in {@link #SIDES}' order + * @param borders the cells along the chunk's sides, for the faces it shows once the chunks beside it are known; two + * visible blocks are equal by the faces they show without them */ -record VisibleBlocks(int[] positions, String[] blocks, byte[] faces) { - static final VisibleBlocks NONE = new VisibleBlocks(new int[0], new String[0], new byte[0]); +record VisibleBlocks(int[] positions, String[] blocks, byte[] faces, ChunkBorders borders) { /** The order of the sides in {@link #faces}: down, up, north, south, west, east. */ static final int[][] SIDES = {{0, -1, 0}, {0, 1, 0}, {0, 0, -1}, {0, 0, 1}, {-1, 0, 0}, {1, 0, 0}}; static final int POSITION_Y_SHIFT = 8; @@ -147,7 +148,36 @@ record VisibleBlocks(int[] positions, String[] blocks, byte[] faces) { } } } - return positions.isEmpty() ? NONE : new VisibleBlocks(positions.toIntArray(), blocks.toArray(String[]::new), faces.toByteArray()); + return new VisibleBlocks(positions.toIntArray(), blocks.toArray(String[]::new), faces.toByteArray(), borders()); + } + + private ChunkBorders borders() { + ChunkBorders.Wall[] walls = new ChunkBorders.Wall[ChunkBorders.EAST - ChunkBorders.NORTH + 1]; + for (int side = ChunkBorders.NORTH; side <= ChunkBorders.EAST; side++) { + walls[side - ChunkBorders.NORTH] = wallOn(side); + } + return new ChunkBorders(walls); + } + + private ChunkBorders.Wall wallOn(int side) { + ChunkBorders.RunsBuilder open = new ChunkBorders.RunsBuilder(); + ChunkBorders.RunsBuilder hidden = new ChunkBorders.RunsBuilder(); + for (int place = 0; place < ACROSS; place++) { + int x = ChunkBorders.xOf(side, place); + int z = ChunkBorders.zOf(side, place); + for (int y = 0; y < height; y++) { + int cell = cellOf(x, y, z); + if (skyReaches[cell]) { + open.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(); + hidden.nextPlace(); + } + return new ChunkBorders.Wall(open.build(), hidden.build()); } // Fills every open cell the sky reaches, from the top of the chunk and from beside it where the ground around @@ -212,8 +242,7 @@ record VisibleBlocks(int[] positions, String[] blocks, byte[] faces) { int neighbour = cellOf(nx, ny, nz); faces = skyReaches[neighbour] && !(isFluid && fluid[neighbour]); } else { - Outside outside = outsideAt(nx, ny, nz); - faces = outside == Outside.AIR || (outside == Outside.FLUID && !isFluid); + faces = facesOutside(x, y, z, side); } if (faces) { sides |= 1 << side; @@ -222,6 +251,12 @@ record VisibleBlocks(int[] positions, String[] blocks, byte[] faces) { return sides; } + // Whether a side facing out of the chunk faces open air there, by the terrain's height beside the chunk. + private boolean facesOutside(int x, int y, int z, int side) { + Outside outside = outsideAt(x + SIDES[side][0], y + SIDES[side][1], z + SIDES[side][2]); + return outside == Outside.AIR || (outside == Outside.FLUID && !fluid[cellOf(x, y, z)]); + } + private Outside outsideAt(int x, int y, int z) { if (y >= height) { return Outside.AIR; diff --git a/core/src/test/java/art/arcane/iris/core/studio/map/ChunkBordersTest.java b/core/src/test/java/art/arcane/iris/core/studio/map/ChunkBordersTest.java new file mode 100644 index 000000000..c2345a1b3 --- /dev/null +++ b/core/src/test/java/art/arcane/iris/core/studio/map/ChunkBordersTest.java @@ -0,0 +1,95 @@ +package art.arcane.iris.core.studio.map; + +import art.arcane.iris.spi.PlatformBlockState; +import art.arcane.iris.util.project.hunk.Hunk; +import org.junit.Test; + +import java.util.ArrayList; +import java.util.List; +import java.util.function.UnaryOperator; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +public class ChunkBordersTest { + private static final int HEIGHT = 12; + private static final int GROUND_Y = 5; + private static final int NO_FLUID = -1; + private static final PlatformBlockState STONE = block("minecraft:stone", false); + private static final PlatformBlockState WATER = block("minecraft:water", true); + + @Test + public void groundBesideAHoleDugAcrossTheChunksEdgeShowsItsSideIntoTheHole() { + Hunk dug = groundUpTo(GROUND_Y); + dug.set(0, GROUND_Y, 7, null); + dug.set(0, GROUND_Y - 1, 7, null); + + List faces = facesOf(groundUpTo(GROUND_Y), dug, ChunkBorders.EAST); + + assertEquals(List.of("15,4,7 minecraft:stone", "15,5,7 minecraft:stone"), faces); + } + + @Test + public void groundBesideGroundOfTheSameHeightShowsNoSide() { + assertTrue(facesOf(groundUpTo(GROUND_Y), groundUpTo(GROUND_Y), ChunkBorders.EAST).isEmpty()); + } + + @Test + public void waterShowsItsSideAgainstAHoleAcrossTheEdgeButNotAgainstWater() { + Hunk pond = groundUpTo(GROUND_Y); + pond.set(15, GROUND_Y, 7, WATER); + Hunk pondBeside = groundUpTo(GROUND_Y); + pondBeside.set(0, GROUND_Y, 7, WATER); + Hunk holeBeside = groundUpTo(GROUND_Y); + holeBeside.set(0, GROUND_Y, 7, null); + + assertTrue(facesOf(pond, pondBeside, ChunkBorders.EAST).isEmpty()); + assertEquals(List.of("15,5,7 minecraft:water"), facesOf(pond, holeBeside, ChunkBorders.EAST)); + } + + @Test + public void eachSideMeetsTheOppositeSideOfTheChunkBesideIt() { + Hunk dug = groundUpTo(GROUND_Y); + dug.set(3, GROUND_Y, 15, null); + + List faces = facesOf(groundUpTo(GROUND_Y), dug, ChunkBorders.NORTH); + + assertEquals(List.of("3,5,0 minecraft:stone"), faces); + } + + // 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 facesOf(Hunk chunk, Hunk beside, int side) { + ChunkBorders borders = VisibleBlocks.of(chunk, (x, z) -> GROUND_Y, NO_FLUID, UnaryOperator.identity()).borders(); + ChunkBorders besideBorders = VisibleBlocks.of(beside, (x, z) -> GROUND_Y, NO_FLUID, UnaryOperator.identity()).borders(); + List faces = new ArrayList<>(); + borders.facesAgainst(besideBorders, side, (x, y, z, block, faceSide) -> { + assertEquals(side, faceSide); + faces.add(x + "," + y + "," + z + " " + block); + }); + return faces; + } + + private static Hunk groundUpTo(int topY) { + Hunk chunk = Hunk.newArrayHunk(16, HEIGHT, 16); + for (int y = 0; y <= topY; y++) { + for (int z = 0; z < 16; z++) { + for (int x = 0; x < 16; x++) { + chunk.set(x, y, z, STONE); + } + } + } + return chunk; + } + + private static PlatformBlockState block(String key, boolean fluid) { + PlatformBlockState block = mock(PlatformBlockState.class); + when(block.key()).thenReturn(key); + when(block.isOccluding()).thenReturn(!fluid); + when(block.isSolid()).thenReturn(!fluid); + when(block.isFluid()).thenReturn(fluid); + return block; + } +}