mirror of
https://github.com/VolmitSoftware/Iris.git
synced 2026-11-28 08:44:50 +00:00
Draw the 3D world's near ground block by block
The near ground was drawn from the top of each column, with the blocks raised above it, so overhangs, the wood under leaves and anything beside a trunk were lost. The server now finds, for each chunk it generates, every block the open air reaches: from the top of the chunk and from beside it above the terrain around, through leaves and water too, so caves closed to the sky stay out. Each block comes with the sides that face that air. Tiles a block a column carry these blocks when asked, and the studio draws each from its model, leaving out a face its model lets a neighbour hide unless that side faces the air. Blocks without a model are cubes of their look or colour; water shows the sides that face the air, and none against plants that grow in it.
This commit is contained in:
1 parent
93d6ffe70e
commit
dccdd8c09d
22 files changed
+975
-42
No files matched your search
@@ -38,14 +38,18 @@ import java.util.function.Function;
|
||||
* f32 heights[grid²], when the header says so | f32 pickNoise[grid²], when the header says so
|
||||
* i16 ground[grid²] | padding to 4 | i16 cover[grid²] | padding to 4 | i16 raisedCounts[grid²] | padding to 4
|
||||
* | i16 raisedY[raised] | padding to 4 | i16 raised[raised] | padding to 4, when the header names blocks
|
||||
* u8 voxelX[voxels] | padding to 4 | u8 voxelZ[voxels] | padding to 4 | i16 voxelY[voxels] | padding to 4
|
||||
* | i16 voxelBlock[voxels] | padding to 4 | u8 voxelFaces[voxels] | padding to 4, when the header counts voxels
|
||||
* </pre>
|
||||
* {@code raised} is the sum of the raised counts, which the header also holds.
|
||||
* {@code raised} is the sum of the raised counts, which the header also holds, as it holds {@code voxels}, the count of
|
||||
* blocks seen from the open air, or -1 for a tile without them.
|
||||
* The header holds the size, the scale, the subject kind, the labels the grids index, and the blocks the ground and
|
||||
* cover grids index when the tile carries the tops of its columns, each as the state players see. Every array starts on a
|
||||
* multiple of 4 bytes, so the studio views them in place.
|
||||
*/
|
||||
final class MapTileEncoding {
|
||||
private static final int ALIGNMENT_BYTES = 4;
|
||||
private static final int NO_VOXELS = -1;
|
||||
|
||||
private MapTileEncoding() {
|
||||
}
|
||||
@@ -59,15 +63,18 @@ final class MapTileEncoding {
|
||||
boolean hasPickNoise = tile.pickNoise() != null;
|
||||
MapTile.Blocks blocks = tile.blocks();
|
||||
int raised = blocks == null ? 0 : blocks.raised().length;
|
||||
MapTile.Voxels voxels = blocks == null ? null : blocks.voxels();
|
||||
int voxelCount = voxels == null ? NO_VOXELS : voxels.count();
|
||||
byte[] header = gson.toJson(new Header(tile.size(), tile.blocksPerPixel(), tile.subjectKind(), tile.labels(),
|
||||
hasHeights, hasPickNoise, blocks == null ? null : statesPlayersSee(blocks.names(), pluginLooks), raised))
|
||||
hasHeights, hasPickNoise, blocks == null ? null : statesPlayersSee(blocks.names(), pluginLooks), raised, voxelCount))
|
||||
.getBytes(StandardCharsets.UTF_8);
|
||||
int cells = tile.gridSize() * tile.gridSize();
|
||||
int length = aligned(Integer.BYTES + header.length)
|
||||
+ aligned(cells * Short.BYTES) * 2
|
||||
+ (hasHeights ? cells * Float.BYTES : 0)
|
||||
+ (hasPickNoise ? cells * Float.BYTES : 0)
|
||||
+ (blocks == null ? 0 : aligned(cells * Short.BYTES) * 3 + aligned(raised * Short.BYTES) * 2);
|
||||
+ (blocks == null ? 0 : aligned(cells * Short.BYTES) * 3 + aligned(raised * Short.BYTES) * 2)
|
||||
+ (voxels == null ? 0 : aligned(voxelCount) * 3 + aligned(voxelCount * Short.BYTES) * 2);
|
||||
ByteBuffer buffer = ByteBuffer.allocate(length).order(ByteOrder.LITTLE_ENDIAN);
|
||||
buffer.putInt(header.length).put(header);
|
||||
align(buffer);
|
||||
@@ -88,9 +95,21 @@ final class MapTileEncoding {
|
||||
putShorts(buffer, blocks.raisedY());
|
||||
putShorts(buffer, blocks.raised());
|
||||
}
|
||||
if (voxels != null) {
|
||||
putBytes(buffer, voxels.x());
|
||||
putBytes(buffer, voxels.z());
|
||||
putShorts(buffer, voxels.y());
|
||||
putShorts(buffer, voxels.blocks());
|
||||
putBytes(buffer, voxels.faces());
|
||||
}
|
||||
return buffer.array();
|
||||
}
|
||||
|
||||
private static void putBytes(ByteBuffer buffer, byte[] values) {
|
||||
buffer.put(values);
|
||||
align(buffer);
|
||||
}
|
||||
|
||||
private static void putShorts(ByteBuffer buffer, short[] grid) {
|
||||
buffer.asShortBuffer().put(grid);
|
||||
buffer.position(aligned(buffer.position() + grid.length * Short.BYTES));
|
||||
@@ -111,6 +130,6 @@ final class MapTileEncoding {
|
||||
}
|
||||
|
||||
private record Header(int size, double blocksPerPixel, MapLabel.Kind subjectKind, List<MapLabel> labels,
|
||||
boolean heights, boolean pickNoise, List<String> blocks, int raised) {
|
||||
boolean heights, boolean pickNoise, List<String> blocks, int raised, int voxels) {
|
||||
}
|
||||
}
|
||||
@@ -632,7 +632,7 @@ public final class StudioBridge implements AutoCloseable {
|
||||
BooleanSupplier cancelled = () -> dropped.get() || closed || engine.isClosing() || engine.isClosed();
|
||||
MapTile tile = session.renderer().render(layer, startX, startZ, blocksPerPixel, size, tileExtras(query), cancelled);
|
||||
if (query.has("blocks")) {
|
||||
tile = withBlocks(session, tile, startX, startZ, blockSource(query), cancelled);
|
||||
tile = withBlocks(session, tile, startX, startZ, blockSource(query), query.flag("voxels"), cancelled);
|
||||
}
|
||||
return MapTileEncoding.encode(tile, gson, StudioBridge::pluginLookOf);
|
||||
}
|
||||
@@ -661,9 +661,9 @@ public final class StudioBridge implements AutoCloseable {
|
||||
}
|
||||
|
||||
private static MapTile withBlocks(StudioWorld.Session session, MapTile tile, double startX, double startZ, BlockSource source,
|
||||
BooleanSupplier cancelled) {
|
||||
boolean withVoxels, BooleanSupplier cancelled) {
|
||||
try {
|
||||
return session.renderer().withBlocks(tile, startX, startZ, source, cancelled);
|
||||
return session.renderer().withBlocks(tile, startX, startZ, source, withVoxels, cancelled);
|
||||
} catch (IllegalArgumentException tooCoarse) {
|
||||
throw new RequestError(STATUS_BAD_REQUEST, tooCoarse.getMessage());
|
||||
}
|
||||
|
||||
@@ -35,6 +35,26 @@ interface ColumnTops {
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* The blocks seen in the square of columns {@code size} across from ({@code minX}, {@code minZ}), or null where the
|
||||
* source knows only the tops of its columns.
|
||||
*/
|
||||
default TileVoxels voxelsIn(int minX, int minZ, int size) {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Blocks seen in a square of columns, each with the sides that face the open air.
|
||||
*
|
||||
* @param x each block's column from the square's west edge
|
||||
* @param z each block's column from the square's north edge
|
||||
* @param y each block's height above the dimension's bottom
|
||||
* @param blocks each block's key
|
||||
* @param faces the sides of each block that face the open air, in {@link VisibleBlocks#SIDES}' order
|
||||
*/
|
||||
record TileVoxels(byte[] x, byte[] z, short[] y, String[] blocks, byte[] faces) {
|
||||
}
|
||||
|
||||
/**
|
||||
* The top of one column.
|
||||
*
|
||||
|
||||
@@ -29,6 +29,7 @@ import art.arcane.iris.util.project.context.IrisContext;
|
||||
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.Long2ObjectMap;
|
||||
import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap;
|
||||
import it.unimi.dsi.fastutil.longs.LongArrayList;
|
||||
@@ -69,6 +70,8 @@ final class GeneratedChunks implements ColumnTops {
|
||||
// blocks out to the horizon holds tens of thousands of chunks.
|
||||
private static final int CHUNK_TOP_BYTES = 5 * 1024;
|
||||
private static final int RAISED_BLOCK_BYTES = Short.BYTES + Integer.BYTES;
|
||||
// Its place, a shared key and the sides it shows.
|
||||
private static final int VISIBLE_BLOCK_BYTES = Integer.BYTES + Integer.BYTES + Byte.BYTES;
|
||||
private static final long MOST_BYTES_KEPT = 512L * 1024 * 1024;
|
||||
private static final String LEASE_NAME = "studio_block_preview";
|
||||
// Half the cores, as the bridge does, so a studio filling its view leaves the server room to run.
|
||||
@@ -104,15 +107,21 @@ final class GeneratedChunks implements ColumnTops {
|
||||
boolean keepsChunks = grid.step() < BLOCKS_ACROSS;
|
||||
Long2ObjectMap<LongList> columnsByChunk = columnsByChunkOf(grid);
|
||||
Map<Long, ColumnTop> found = new ConcurrentHashMap<>();
|
||||
Map<Long, ChunkTop> chunks = new ConcurrentHashMap<>();
|
||||
List<Future<?>> generating = new ArrayList<>();
|
||||
for (Long2ObjectMap.Entry<LongList> chunk : columnsByChunk.long2ObjectEntrySet()) {
|
||||
long key = chunk.getLongKey();
|
||||
ChunkTop known = tops.getIfPresent(key);
|
||||
if (known != null) {
|
||||
readColumns(known, chunk.getValue(), found);
|
||||
read(key, known, keepsChunks, chunk.getValue(), found, chunks);
|
||||
continue;
|
||||
}
|
||||
ChunkTask task = new ChunkTask(() -> readGenerated(key, keepsChunks, chunk.getValue(), found, grid.cancelled()), grid.step());
|
||||
Runnable generation = () -> {
|
||||
if (!grid.cancelled().getAsBoolean()) {
|
||||
read(key, keepsChunks ? tops.get(key) : topWithoutKeeping(key), keepsChunks, chunk.getValue(), found, chunks);
|
||||
}
|
||||
};
|
||||
ChunkTask task = new ChunkTask(generation, grid.step());
|
||||
GENERATORS.execute(task);
|
||||
generating.add(task);
|
||||
}
|
||||
@@ -124,10 +133,66 @@ final class GeneratedChunks implements ColumnTops {
|
||||
generating.forEach(task -> task.cancel(false));
|
||||
throw cancelled;
|
||||
}
|
||||
return (worldX, worldZ) -> {
|
||||
ColumnTop column = found.get(keyOf(worldX, worldZ));
|
||||
return column != null ? column : at(worldX, worldZ);
|
||||
};
|
||||
return new PreparedTile(found, chunks);
|
||||
}
|
||||
|
||||
/** A tile's columns, and the chunks it keeps, read as its chunks were generated. */
|
||||
private final class PreparedTile implements ColumnTops {
|
||||
private final Map<Long, ColumnTop> columns;
|
||||
private final Map<Long, ChunkTop> chunks;
|
||||
|
||||
PreparedTile(Map<Long, ColumnTop> columns, Map<Long, ChunkTop> chunks) {
|
||||
this.columns = columns;
|
||||
this.chunks = chunks;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ColumnTop at(int worldX, int worldZ) {
|
||||
ColumnTop column = columns.get(keyOf(worldX, worldZ));
|
||||
return column != null ? column : GeneratedChunks.this.at(worldX, worldZ);
|
||||
}
|
||||
|
||||
@Override
|
||||
public TileVoxels voxelsIn(int minX, int minZ, int size) {
|
||||
TileVoxelsBuilder voxels = new TileVoxelsBuilder();
|
||||
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);
|
||||
}
|
||||
}
|
||||
return voxels.build();
|
||||
}
|
||||
}
|
||||
|
||||
private static final class TileVoxelsBuilder {
|
||||
private final ByteArrayList x = new ByteArrayList();
|
||||
private final ByteArrayList z = new ByteArrayList();
|
||||
private final ShortArrayList y = new ShortArrayList();
|
||||
private final List<String> 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();
|
||||
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]);
|
||||
}
|
||||
}
|
||||
|
||||
TileVoxels build() {
|
||||
return new TileVoxels(x.toByteArray(), z.toByteArray(), y.toShortArray(), blocks.toArray(String[]::new), faces.toByteArray());
|
||||
}
|
||||
}
|
||||
|
||||
private static Long2ObjectMap<LongList> columnsByChunkOf(TileGrid grid) {
|
||||
@@ -143,11 +208,11 @@ final class GeneratedChunks implements ColumnTops {
|
||||
return columnsByChunk;
|
||||
}
|
||||
|
||||
private void readGenerated(long key, boolean keepsChunk, LongList columns, Map<Long, ColumnTop> found, BooleanSupplier cancelled) {
|
||||
if (cancelled.getAsBoolean()) {
|
||||
return;
|
||||
private static void read(long key, ChunkTop chunk, boolean keepsChunk, LongList columns, Map<Long, ColumnTop> found, Map<Long, ChunkTop> chunks) {
|
||||
readColumns(chunk, columns, found);
|
||||
if (keepsChunk) {
|
||||
chunks.put(key, chunk);
|
||||
}
|
||||
readColumns(keepsChunk ? tops.get(key) : topWithoutKeeping(key), columns, found);
|
||||
}
|
||||
|
||||
private ChunkTop topWithoutKeeping(long key) {
|
||||
@@ -230,7 +295,8 @@ final class GeneratedChunks implements ColumnTops {
|
||||
engine.getGeneration().generate(chunkX << CHUNK_SHIFT, chunkZ << CHUNK_SHIFT, blocks, biomes, false, lease.sessionId());
|
||||
}
|
||||
IntBinaryOperator terrainY = (x, z) -> engine.getHeight((chunkX << CHUNK_SHIFT) + x, (chunkZ << CHUNK_SHIFT) + z, true);
|
||||
return new ChunkTop(topsOf(blocks, terrainY, this::seenByPlayers));
|
||||
int fluidHeight = engine.getDimension().getFluidHeight();
|
||||
return new ChunkTop(topsOf(blocks, terrainY, this::seenByPlayers), VisibleBlocks.of(blocks, terrainY, fluidHeight, this::seenByPlayers));
|
||||
}
|
||||
|
||||
// A plugin's block can be solid on the server where players see grass they walk through.
|
||||
@@ -248,13 +314,13 @@ final class GeneratedChunks implements ColumnTops {
|
||||
return ((long) chunkX << Integer.SIZE) | (chunkZ & 0xFFFFFFFFL);
|
||||
}
|
||||
|
||||
private record ChunkTop(ColumnTop[] columns) {
|
||||
private record ChunkTop(ColumnTop[] columns, VisibleBlocks visible) {
|
||||
int bytes() {
|
||||
int raisedBlocks = 0;
|
||||
for (ColumnTop column : columns) {
|
||||
raisedBlocks += column.raised().count();
|
||||
}
|
||||
return CHUNK_TOP_BYTES + raisedBlocks * RAISED_BLOCK_BYTES;
|
||||
return CHUNK_TOP_BYTES + raisedBlocks * RAISED_BLOCK_BYTES + visible.count() * VISIBLE_BLOCK_BYTES;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -46,11 +46,28 @@ public record MapTile(int size, double blocksPerPixel, MapLabel.Kind subjectKind
|
||||
* @param raisedCounts how many solid blocks stand above each cell's ground, such as a tree's wood and leaves
|
||||
* @param raisedY the height of each raised block, cell after cell and each cell's highest first
|
||||
* @param raised each raised block, in the order of {@code raisedY}
|
||||
* @param voxels the blocks seen in the tile's own pixels, a block a pixel, or null for a tile at another scale
|
||||
*/
|
||||
public record Blocks(List<String> names, short[] ground, short[] cover, short[] raisedCounts, short[] raisedY, short[] raised) {
|
||||
public record Blocks(List<String> names, short[] ground, short[] cover, short[] raisedCounts, short[] raisedY, short[] raised,
|
||||
Voxels voxels) {
|
||||
public static final short NO_BLOCK = -1;
|
||||
}
|
||||
|
||||
/**
|
||||
* The blocks that can be seen from the open air in a tile a block a pixel, with the sides of each that face it.
|
||||
*
|
||||
* @param x each block's pixel from the tile's west edge
|
||||
* @param z each block's pixel from the tile's north edge
|
||||
* @param y each block's height above the dimension's bottom
|
||||
* @param blocks the index of each block in the tile's block names
|
||||
* @param faces the sides of each block that face the open air, a bit each: down, up, north, south, west, east
|
||||
*/
|
||||
public record Voxels(byte[] x, byte[] z, short[] y, short[] blocks, byte[] faces) {
|
||||
public int count() {
|
||||
return x.length;
|
||||
}
|
||||
}
|
||||
|
||||
/** This tile with {@code blocks} on top of its columns, at {@code heights} where it carries heights. */
|
||||
MapTile withBlocks(Blocks blocks, float[] heights) {
|
||||
return new MapTile(size, blocksPerPixel, subjectKind, labels, subjects, regions, heights, pickNoise, blocks);
|
||||
|
||||
@@ -77,10 +77,12 @@ public final class MapTileRenderer {
|
||||
* {@code source}, and the solid blocks raised above it. Where the tile carries heights they become the heights of
|
||||
* that ground: the terrain's top as generated, under what stands on it.
|
||||
*
|
||||
* @param withVoxels whether a tile of a block a pixel also carries every block seen from the open air, where the
|
||||
* source knows them
|
||||
* @throws IllegalArgumentException when the tile is coarser than the source is drawn at
|
||||
* @throws CancellationException as soon as {@code cancelled} answers true
|
||||
*/
|
||||
public MapTile withBlocks(MapTile tile, double startX, double startZ, BlockSource source, BooleanSupplier cancelled) {
|
||||
public MapTile withBlocks(MapTile tile, double startX, double startZ, BlockSource source, boolean withVoxels, BooleanSupplier cancelled) {
|
||||
double step = tile.blocksPerPixel();
|
||||
if (step > source.largestBlocksPerPixel()) {
|
||||
throw new IllegalArgumentException("Blocks from " + source.id() + " are drawn at " + source.largestBlocksPerPixel()
|
||||
@@ -115,10 +117,24 @@ public final class MapTileRenderer {
|
||||
}
|
||||
}
|
||||
}
|
||||
MapTile.Blocks blocks = new MapTile.Blocks(names.names(), ground, cover, raisedCounts, raisedY.toShortArray(), raised.toShortArray());
|
||||
MapTile.Voxels voxels = withVoxels && step == 1D ? voxelsOf(tops, startX, startZ, tile.size(), names) : null;
|
||||
MapTile.Blocks blocks = new MapTile.Blocks(names.names(), ground, cover, raisedCounts, raisedY.toShortArray(), raised.toShortArray(), voxels);
|
||||
return tile.withBlocks(blocks, heights);
|
||||
}
|
||||
|
||||
// Only a tile of a block a pixel shows its blocks one by one; coarser ones show the tops of their columns.
|
||||
private static MapTile.Voxels voxelsOf(ColumnTops tops, double startX, double startZ, int size, BlockNames names) {
|
||||
ColumnTops.TileVoxels found = tops.voxelsIn((int) Math.floor(startX), (int) Math.floor(startZ), size);
|
||||
if (found == null) {
|
||||
return null;
|
||||
}
|
||||
short[] blocks = new short[found.blocks().length];
|
||||
for (int at = 0; at < blocks.length; at++) {
|
||||
blocks[at] = names.indexOf(found.blocks()[at]);
|
||||
}
|
||||
return new MapTile.Voxels(found.x(), found.z(), found.y(), blocks, found.faces());
|
||||
}
|
||||
|
||||
private ColumnTops topsOf(BlockSource source) {
|
||||
return switch (source) {
|
||||
case GENERATED -> generated;
|
||||
|
||||
@@ -0,0 +1,250 @@
|
||||
/*
|
||||
* 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 <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
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.bytes.ByteArrayList;
|
||||
import it.unimi.dsi.fastutil.ints.IntArrayList;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.function.IntBinaryOperator;
|
||||
import java.util.function.UnaryOperator;
|
||||
|
||||
/**
|
||||
* The blocks of a chunk that can be seen from the open air above it, each with the sides that face that air. The open
|
||||
* air is every cell no opaque block fills that the sky reaches, through water and leaves too, so the ground, cliffs,
|
||||
* overhangs, the wood inside a tree and the floor of a lake are seen, and a cave closed to the sky is not.
|
||||
*
|
||||
* @param positions each block's place in the chunk: x in the lowest 4 bits, z in the next 4, then its height above the
|
||||
* 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
|
||||
*/
|
||||
record VisibleBlocks(int[] positions, String[] blocks, byte[] faces) {
|
||||
static final VisibleBlocks NONE = new VisibleBlocks(new int[0], new String[0], new byte[0]);
|
||||
/** 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;
|
||||
private static final int ACROSS = 16;
|
||||
private static final Set<String> ALWAYS_IN_WATER = Set.of("minecraft:kelp", "minecraft:kelp_plant", "minecraft:seagrass",
|
||||
"minecraft:tall_seagrass", "minecraft:bubble_column");
|
||||
private static final int Z_SHIFT = 4;
|
||||
|
||||
int count() {
|
||||
return positions.length;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param terrainY the terrain's top in a column by its x and z from the chunk's corner, which reaches into the
|
||||
* chunks around it; beside the chunk, what lies above it counts as open air, or water below the
|
||||
* fluid's height
|
||||
* @param fluidHeight the height the sea fills to
|
||||
* @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();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object other) {
|
||||
return other instanceof VisibleBlocks visible && Arrays.equals(positions, visible.positions)
|
||||
&& Arrays.equals(blocks, visible.blocks) && Arrays.equals(faces, visible.faces);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return 31 * (31 * Arrays.hashCode(positions) + Arrays.hashCode(blocks)) + Arrays.hashCode(faces);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "VisibleBlocks" + Arrays.toString(positions) + Arrays.toString(blocks) + Arrays.toString(faces);
|
||||
}
|
||||
|
||||
private static final class Finder {
|
||||
private final Hunk<PlatformBlockState> chunk;
|
||||
private final IntBinaryOperator terrainY;
|
||||
private final int fluidHeight;
|
||||
private final int height;
|
||||
private final PlatformBlockState[] seen;
|
||||
private final boolean[] open;
|
||||
private final boolean[] fluid;
|
||||
private final boolean[] skyReaches;
|
||||
|
||||
Finder(Hunk<PlatformBlockState> chunk, IntBinaryOperator terrainY, int fluidHeight, UnaryOperator<PlatformBlockState> seenByPlayers) {
|
||||
this.chunk = chunk;
|
||||
this.terrainY = terrainY;
|
||||
this.fluidHeight = fluidHeight;
|
||||
this.height = chunk.getHeight();
|
||||
int cells = ACROSS * height * ACROSS;
|
||||
this.seen = new PlatformBlockState[cells];
|
||||
this.open = new boolean[cells];
|
||||
this.fluid = new boolean[cells];
|
||||
this.skyReaches = new boolean[cells];
|
||||
for (int y = 0; y < height; y++) {
|
||||
for (int z = 0; z < ACROSS; z++) {
|
||||
for (int x = 0; x < ACROSS; x++) {
|
||||
PlatformBlockState block = chunk.get(x, y, z);
|
||||
int cell = cellOf(x, y, z);
|
||||
seen[cell] = block == null || block.isAir() ? null : seenByPlayers.apply(block);
|
||||
open[cell] = seen[cell] == null || !seen[cell].isOccluding();
|
||||
fluid[cell] = seen[cell] != null && holdsFluid(seen[cell]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Water around a waterlogged block, or one that only grows in water, has no side against it.
|
||||
private static boolean holdsFluid(PlatformBlockState block) {
|
||||
return block.isFluid() || block.isWaterLogged() || ALWAYS_IN_WATER.contains(materialOf(block.key()));
|
||||
}
|
||||
|
||||
private static String materialOf(String key) {
|
||||
int properties = key.indexOf('[');
|
||||
return properties < 0 ? key : key.substring(0, properties);
|
||||
}
|
||||
|
||||
VisibleBlocks find() {
|
||||
spreadTheSky();
|
||||
IntArrayList positions = new IntArrayList();
|
||||
List<String> blocks = new ArrayList<>();
|
||||
ByteArrayList faces = new ByteArrayList();
|
||||
for (int y = 0; y < height; y++) {
|
||||
for (int z = 0; z < ACROSS; z++) {
|
||||
for (int x = 0; x < ACROSS; x++) {
|
||||
int cell = cellOf(x, y, z);
|
||||
if (seen[cell] == null) {
|
||||
continue;
|
||||
}
|
||||
int sides = sidesFacingTheSky(x, y, z);
|
||||
// A plant or a leaf the sky reaches shows whatever it touches; its model may have faces no side hides.
|
||||
if (sides == 0 && !(open[cell] && skyReaches[cell])) {
|
||||
continue;
|
||||
}
|
||||
positions.add(x | z << Z_SHIFT | y << POSITION_Y_SHIFT);
|
||||
blocks.add(chunk.get(x, y, z).key());
|
||||
faces.add((byte) sides);
|
||||
}
|
||||
}
|
||||
}
|
||||
return positions.isEmpty() ? NONE : new VisibleBlocks(positions.toIntArray(), blocks.toArray(String[]::new), faces.toByteArray());
|
||||
}
|
||||
|
||||
// Fills every open cell the sky reaches, from the top of the chunk and from beside it where the ground around
|
||||
// lies lower.
|
||||
private void spreadTheSky() {
|
||||
int[] queue = new int[skyReaches.length];
|
||||
int queued = 0;
|
||||
for (int y = 0; y < height; y++) {
|
||||
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)) {
|
||||
skyReaches[cell] = true;
|
||||
queue[queued++] = cell;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
for (int next = 0; next < queued; next++) {
|
||||
int cell = queue[next];
|
||||
int x = cell % ACROSS;
|
||||
int z = (cell / ACROSS) % ACROSS;
|
||||
int y = cell / (ACROSS * ACROSS);
|
||||
for (int[] side : SIDES) {
|
||||
int nx = x + side[0];
|
||||
int ny = y + side[1];
|
||||
int nz = z + side[2];
|
||||
if (!isInside(nx, ny, nz)) {
|
||||
continue;
|
||||
}
|
||||
int neighbour = cellOf(nx, ny, nz);
|
||||
if (open[neighbour] && !skyReaches[neighbour]) {
|
||||
skyReaches[neighbour] = true;
|
||||
queue[queued++] = neighbour;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private boolean touchesOpenOutside(int x, int y, int z) {
|
||||
for (int[] side : SIDES) {
|
||||
int nx = x + side[0];
|
||||
int ny = y + side[1];
|
||||
int nz = z + side[2];
|
||||
if (!isInside(nx, ny, nz) && outsideAt(nx, ny, nz) != Outside.SOLID) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// A side faces the sky where the cell beside it is open air the sky reaches; water hides the sides of water.
|
||||
private int sidesFacingTheSky(int x, int y, int z) {
|
||||
boolean isFluid = fluid[cellOf(x, y, z)];
|
||||
int sides = 0;
|
||||
for (int side = 0; side < SIDES.length; side++) {
|
||||
int nx = x + SIDES[side][0];
|
||||
int ny = y + SIDES[side][1];
|
||||
int nz = z + SIDES[side][2];
|
||||
boolean faces;
|
||||
if (isInside(nx, ny, nz)) {
|
||||
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);
|
||||
}
|
||||
if (faces) {
|
||||
sides |= 1 << side;
|
||||
}
|
||||
}
|
||||
return sides;
|
||||
}
|
||||
|
||||
private Outside outsideAt(int x, int y, int z) {
|
||||
if (y >= height) {
|
||||
return Outside.AIR;
|
||||
}
|
||||
if (y < 0 || y <= terrainY.applyAsInt(x, z)) {
|
||||
return Outside.SOLID;
|
||||
}
|
||||
return y <= fluidHeight ? Outside.FLUID : Outside.AIR;
|
||||
}
|
||||
|
||||
private boolean isInside(int x, int y, int z) {
|
||||
return x >= 0 && x < ACROSS && z >= 0 && z < ACROSS && y >= 0 && y < height;
|
||||
}
|
||||
|
||||
private static int cellOf(int x, int y, int z) {
|
||||
return (y * ACROSS + z) * ACROSS + x;
|
||||
}
|
||||
}
|
||||
|
||||
/** What lies beside the chunk, judged by the terrain's height there alone. */
|
||||
private enum Outside {
|
||||
SOLID,
|
||||
FLUID,
|
||||
AIR
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,144 @@
|
||||
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.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.function.UnaryOperator;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
public class VisibleBlocksTest {
|
||||
private static final int HEIGHT = 12;
|
||||
private static final int GROUND_Y = 3;
|
||||
private static final int NO_FLUID = -1;
|
||||
private static final int DOWN = 1;
|
||||
private static final int UP = 1 << 1;
|
||||
private static final int NORTH = 1 << 2;
|
||||
private static final int SOUTH = 1 << 3;
|
||||
private static final int WEST = 1 << 4;
|
||||
private static final int EAST = 1 << 5;
|
||||
private static final PlatformBlockState STONE = block("minecraft:stone", true, false);
|
||||
private static final PlatformBlockState LOG = block("minecraft:oak_log", true, false);
|
||||
private static final PlatformBlockState LEAVES = block("minecraft:oak_leaves", false, false);
|
||||
private static final PlatformBlockState WATER = block("minecraft:water", false, true);
|
||||
private static final PlatformBlockState KELP = block("minecraft:kelp_plant", false, false);
|
||||
|
||||
@Test
|
||||
public void flatGroundShowsOnlyTheTopOfItsHighestLayer() {
|
||||
Map<Long, Integer> seen = facesByPlace(VisibleBlocks.of(groundUpTo(GROUND_Y), (x, z) -> GROUND_Y, NO_FLUID, UnaryOperator.identity()));
|
||||
|
||||
assertEquals(16 * 16, seen.size());
|
||||
assertEquals(UP, (int) seen.get(place(7, GROUND_Y, 7)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void aTreeShowsTheSidesOfItsWoodAndEveryLeafFaceThatTouchesTheOpenAir() {
|
||||
Hunk<PlatformBlockState> chunk = groundUpTo(GROUND_Y);
|
||||
chunk.set(5, 4, 5, LOG);
|
||||
chunk.set(5, 5, 5, LOG);
|
||||
chunk.set(5, 6, 5, LEAVES);
|
||||
chunk.set(6, 6, 5, LEAVES);
|
||||
|
||||
Map<Long, Integer> seen = facesByPlace(VisibleBlocks.of(chunk, (x, z) -> GROUND_Y, NO_FLUID, UnaryOperator.identity()));
|
||||
|
||||
assertEquals(NORTH | SOUTH | WEST | EAST, (int) seen.get(place(5, 4, 5)));
|
||||
// The leaf on the trunk shows its side on the other leaf, as leaves are seen through.
|
||||
assertEquals(UP | NORTH | SOUTH | WEST | EAST, (int) seen.get(place(5, 6, 5)));
|
||||
assertEquals(DOWN | UP | NORTH | SOUTH | WEST | EAST, (int) seen.get(place(6, 6, 5)));
|
||||
assertFalse(seen.containsKey(place(5, GROUND_Y, 5)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void aCaveClosedToTheSkyIsNotSeen() {
|
||||
Hunk<PlatformBlockState> chunk = groundUpTo(GROUND_Y);
|
||||
chunk.set(8, 1, 8, null);
|
||||
|
||||
Map<Long, Integer> seen = facesByPlace(VisibleBlocks.of(chunk, (x, z) -> GROUND_Y, NO_FLUID, UnaryOperator.identity()));
|
||||
|
||||
assertFalse(seen.containsKey(place(8, 0, 8)));
|
||||
assertFalse(seen.containsKey(place(8, 2, 8)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void groundStandingAboveTheTerrainBesideTheChunkShowsItsSideThere() {
|
||||
Hunk<PlatformBlockState> chunk = groundUpTo(GROUND_Y);
|
||||
|
||||
Map<Long, Integer> seen = facesByPlace(VisibleBlocks.of(chunk, (x, z) -> x < 0 ? GROUND_Y - 2 : GROUND_Y, NO_FLUID, UnaryOperator.identity()));
|
||||
|
||||
assertEquals(UP | WEST, (int) seen.get(place(0, GROUND_Y, 7)));
|
||||
assertEquals(WEST, (int) seen.get(place(0, GROUND_Y - 1, 7)));
|
||||
assertFalse(seen.containsKey(place(0, GROUND_Y - 2, 7)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void theFloorOfALakeShowsThroughItsWaterWhichShowsOnlyItsSurface() {
|
||||
Hunk<PlatformBlockState> chunk = groundUpTo(GROUND_Y);
|
||||
for (int z = 0; z < 16; z++) {
|
||||
for (int x = 0; x < 16; x++) {
|
||||
chunk.set(x, GROUND_Y + 1, z, WATER);
|
||||
}
|
||||
}
|
||||
|
||||
Map<Long, Integer> seen = facesByPlace(VisibleBlocks.of(chunk, (x, z) -> GROUND_Y, GROUND_Y + 1, UnaryOperator.identity()));
|
||||
|
||||
assertEquals(UP, (int) seen.get(place(7, GROUND_Y, 7)));
|
||||
assertEquals(UP, (int) seen.get(place(7, GROUND_Y + 1, 7)));
|
||||
assertEquals(UP, (int) seen.get(place(0, GROUND_Y + 1, 7)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void waterShowsNoSideAgainstAPlantThatGrowsInIt() {
|
||||
Hunk<PlatformBlockState> chunk = groundUpTo(GROUND_Y);
|
||||
for (int z = 0; z < 16; z++) {
|
||||
for (int x = 0; x < 16; x++) {
|
||||
chunk.set(x, GROUND_Y + 1, z, WATER);
|
||||
chunk.set(x, GROUND_Y + 2, z, WATER);
|
||||
}
|
||||
}
|
||||
chunk.set(7, GROUND_Y + 1, 7, KELP);
|
||||
|
||||
Map<Long, Integer> seen = facesByPlace(VisibleBlocks.of(chunk, (x, z) -> GROUND_Y, GROUND_Y + 2, UnaryOperator.identity()));
|
||||
|
||||
assertEquals(0, (int) seen.get(place(6, GROUND_Y + 1, 7)));
|
||||
}
|
||||
|
||||
private static Hunk<PlatformBlockState> groundUpTo(int topY) {
|
||||
Hunk<PlatformBlockState> 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 Map<Long, Integer> facesByPlace(VisibleBlocks visible) {
|
||||
Map<Long, Integer> faces = new HashMap<>();
|
||||
for (int at = 0; at < visible.count(); at++) {
|
||||
int position = visible.positions()[at];
|
||||
faces.put(place(position & 15, position >> VisibleBlocks.POSITION_Y_SHIFT, (position >> 4) & 15), visible.faces()[at] & 0xFF);
|
||||
}
|
||||
return faces;
|
||||
}
|
||||
|
||||
private static long place(int x, int y, int z) {
|
||||
return ((long) y << 16) | (z << 8) | x;
|
||||
}
|
||||
|
||||
private static PlatformBlockState block(String key, boolean occluding, boolean fluid) {
|
||||
PlatformBlockState block = mock(PlatformBlockState.class);
|
||||
when(block.key()).thenReturn(key);
|
||||
when(block.isOccluding()).thenReturn(occluding);
|
||||
when(block.isSolid()).thenReturn(!fluid);
|
||||
when(block.isFluid()).thenReturn(fluid);
|
||||
return block;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user