mirror of
https://github.com/VolmitSoftware/Iris.git
synced 2026-11-28 16:54:04 +00:00
Draw the 3D world's far ground as runs of blocks
Far tiles showed one height and one block per column, sampled from a single column, so trees became pillars of leaves or vanished. For each generated chunk the server now finds, in areas 2 to 16 blocks across, the runs of heights that are solid in at least a quarter of the area, split where the main block changes and ending at the ground, as Distant Horizons draws far terrain. Coarser tiles carry these runs when asked for the ground's shape, and the studio draws each as a box with its top, its underside where it floats, and the sides its neighbours leave open, in the average colours of its block's pictures, which near textures fade to.
This commit is contained in:
1 parent
dccdd8c09d
commit
428f9cfb0e
19 files changed
+765
-46
No files matched your search
@@ -40,16 +40,18 @@ import java.util.function.Function;
|
||||
* | 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
|
||||
* i16 runCounts[grid²] | padding to 4 | i16 runBottoms[runs] | padding to 4 | i16 runTops[runs] | padding to 4
|
||||
* | i16 runBlocks[runs] | padding to 4, when the header counts runs
|
||||
* </pre>
|
||||
* {@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.
|
||||
* blocks seen from the open air, and {@code runs}, the sum of the run counts, each -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 static final int NONE = -1;
|
||||
|
||||
private MapTileEncoding() {
|
||||
}
|
||||
@@ -64,9 +66,11 @@ final class MapTileEncoding {
|
||||
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();
|
||||
int voxelCount = voxels == null ? NONE : voxels.count();
|
||||
MapTile.Runs runs = blocks == null ? null : blocks.runs();
|
||||
int runCount = runs == null ? NONE : runs.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, voxelCount))
|
||||
hasHeights, hasPickNoise, blocks == null ? null : statesPlayersSee(blocks.names(), pluginLooks), raised, voxelCount, runCount))
|
||||
.getBytes(StandardCharsets.UTF_8);
|
||||
int cells = tile.gridSize() * tile.gridSize();
|
||||
int length = aligned(Integer.BYTES + header.length)
|
||||
@@ -74,7 +78,8 @@ final class MapTileEncoding {
|
||||
+ (hasHeights ? cells * Float.BYTES : 0)
|
||||
+ (hasPickNoise ? cells * Float.BYTES : 0)
|
||||
+ (blocks == null ? 0 : aligned(cells * Short.BYTES) * 3 + aligned(raised * Short.BYTES) * 2)
|
||||
+ (voxels == null ? 0 : aligned(voxelCount) * 3 + aligned(voxelCount * Short.BYTES) * 2);
|
||||
+ (voxels == null ? 0 : aligned(voxelCount) * 3 + aligned(voxelCount * Short.BYTES) * 2)
|
||||
+ (runs == null ? 0 : aligned(cells * Short.BYTES) + aligned(runCount * Short.BYTES) * 3);
|
||||
ByteBuffer buffer = ByteBuffer.allocate(length).order(ByteOrder.LITTLE_ENDIAN);
|
||||
buffer.putInt(header.length).put(header);
|
||||
align(buffer);
|
||||
@@ -102,6 +107,12 @@ final class MapTileEncoding {
|
||||
putShorts(buffer, voxels.blocks());
|
||||
putBytes(buffer, voxels.faces());
|
||||
}
|
||||
if (runs != null) {
|
||||
putShorts(buffer, runs.counts());
|
||||
putShorts(buffer, runs.bottoms());
|
||||
putShorts(buffer, runs.tops());
|
||||
putShorts(buffer, runs.blocks());
|
||||
}
|
||||
return buffer.array();
|
||||
}
|
||||
|
||||
@@ -130,6 +141,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, int voxels) {
|
||||
boolean heights, boolean pickNoise, List<String> blocks, int raised, int voxels, int runs) {
|
||||
}
|
||||
}
|
||||
@@ -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), query.flag("voxels"), cancelled);
|
||||
tile = withBlocks(session, tile, startX, startZ, blockSource(query), query.flag("shape"), 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,
|
||||
boolean withVoxels, BooleanSupplier cancelled) {
|
||||
boolean withShape, BooleanSupplier cancelled) {
|
||||
try {
|
||||
return session.renderer().withBlocks(tile, startX, startZ, source, withVoxels, cancelled);
|
||||
return session.renderer().withBlocks(tile, startX, startZ, source, withShape, cancelled);
|
||||
} catch (IllegalArgumentException tooCoarse) {
|
||||
throw new RequestError(STATUS_BAD_REQUEST, tooCoarse.getMessage());
|
||||
}
|
||||
|
||||
@@ -0,0 +1,217 @@
|
||||
/*
|
||||
* 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.shorts.ShortArrayList;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.function.IntBinaryOperator;
|
||||
import java.util.function.UnaryOperator;
|
||||
|
||||
/**
|
||||
* A chunk seen from afar: its blocks in square areas {@code 2^level} blocks across, each area a column of runs of
|
||||
* solid blocks, highest first, as Distant Horizons draws far terrain. A height of an area counts as solid where at least
|
||||
* a quarter of its blocks are, so a tree is a canopy on a trunk with air beneath, not a pillar down to the ground. The
|
||||
* runs go down to the area's ground and no further; the last of each area is that ground, solid all the way down.
|
||||
* Plants and water fill no run, as they are drawn their own way.
|
||||
*
|
||||
* @param counts how many runs each area has, the areas row by row from the north west
|
||||
* @param bottoms the lowest height of each run, area after area
|
||||
* @param tops the highest height of each run
|
||||
* @param blocks the key of the block most of each run's top shows
|
||||
*/
|
||||
record ColumnRuns(short[] counts, short[] bottoms, short[] tops, String[] blocks) {
|
||||
static final int MOST_LEVEL = 4;
|
||||
private static final int ACROSS = 16;
|
||||
private static final int SOLID_SHARE_DIVISOR = 4;
|
||||
|
||||
int areasAcross() {
|
||||
return (int) Math.round(Math.sqrt(counts.length));
|
||||
}
|
||||
|
||||
/** Where the runs of the area at {@code index} start in {@link #bottoms}. */
|
||||
int firstRunOf(int index) {
|
||||
int first = 0;
|
||||
for (int area = 0; area < index; area++) {
|
||||
first += counts[area];
|
||||
}
|
||||
return first;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param level the areas are {@code 2^level} blocks across, 1 to {@link #MOST_LEVEL}
|
||||
* @param terrainY the terrain's top in a column by its x and z in the chunk
|
||||
* @param seenByPlayers the state players see in place of a block, which decides whether it is solid
|
||||
*/
|
||||
static ColumnRuns of(Hunk<PlatformBlockState> chunk, int level, IntBinaryOperator terrainY, UnaryOperator<PlatformBlockState> seenByPlayers) {
|
||||
int areaBlocks = 1 << level;
|
||||
int areasAcross = ACROSS / areaBlocks;
|
||||
int fewestSolid = Math.max(1, areaBlocks * areaBlocks / SOLID_SHARE_DIVISOR);
|
||||
short[] counts = new short[areasAcross * areasAcross];
|
||||
ShortArrayList bottoms = new ShortArrayList();
|
||||
ShortArrayList tops = new ShortArrayList();
|
||||
List<String> blocks = new ArrayList<>();
|
||||
for (int areaZ = 0; areaZ < areasAcross; areaZ++) {
|
||||
for (int areaX = 0; areaX < areasAcross; areaX++) {
|
||||
Area area = new Area(chunk, areaX * areaBlocks, areaZ * areaBlocks, areaBlocks, seenByPlayers);
|
||||
int groundY = area.lowestOf(terrainY);
|
||||
int found = area.addRuns(fewestSolid, groundY, bottoms, tops, blocks);
|
||||
counts[areaZ * areasAcross + areaX] = (short) found;
|
||||
}
|
||||
}
|
||||
return new ColumnRuns(counts, bottoms.toShortArray(), tops.toShortArray(), blocks.toArray(String[]::new));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object other) {
|
||||
return other instanceof ColumnRuns runs && Arrays.equals(counts, runs.counts) && Arrays.equals(bottoms, runs.bottoms)
|
||||
&& Arrays.equals(tops, runs.tops) && Arrays.equals(blocks, runs.blocks);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Arrays.hashCode(counts) * 31 + Arrays.hashCode(tops);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "ColumnRuns" + Arrays.toString(counts) + Arrays.toString(bottoms) + Arrays.toString(tops) + Arrays.toString(blocks);
|
||||
}
|
||||
|
||||
/** The runs of one area as they are found from the top down. */
|
||||
private static final class Runs {
|
||||
private static final int NONE = -1;
|
||||
private final ShortArrayList bottoms;
|
||||
private final ShortArrayList tops;
|
||||
private final List<String> blocks;
|
||||
private int top = NONE;
|
||||
private int bottom;
|
||||
private String block;
|
||||
private int found;
|
||||
|
||||
Runs(ShortArrayList bottoms, ShortArrayList tops, List<String> blocks) {
|
||||
this.bottoms = bottoms;
|
||||
this.tops = tops;
|
||||
this.blocks = blocks;
|
||||
}
|
||||
|
||||
void extendTo(int y, String shown) {
|
||||
if (top != NONE && !shown.equals(block)) {
|
||||
end(bottom);
|
||||
}
|
||||
if (top == NONE) {
|
||||
top = y;
|
||||
block = shown;
|
||||
}
|
||||
bottom = y;
|
||||
}
|
||||
|
||||
void endAbove(int y) {
|
||||
if (top != NONE) {
|
||||
end(y + 1);
|
||||
}
|
||||
}
|
||||
|
||||
// The ground reaches down out of sight, so its run has no bottom to draw.
|
||||
void endAsGround() {
|
||||
if (top != NONE) {
|
||||
end(0);
|
||||
}
|
||||
}
|
||||
|
||||
private void end(int runBottom) {
|
||||
bottoms.add((short) runBottom);
|
||||
tops.add((short) top);
|
||||
blocks.add(block);
|
||||
found++;
|
||||
top = NONE;
|
||||
}
|
||||
}
|
||||
|
||||
/** One square area of a chunk, read a height at a time. */
|
||||
private record Area(Hunk<PlatformBlockState> chunk, int minX, int minZ, int across, UnaryOperator<PlatformBlockState> seenByPlayers) {
|
||||
int lowestOf(IntBinaryOperator terrainY) {
|
||||
int lowest = Integer.MAX_VALUE;
|
||||
for (int z = minZ; z < minZ + across; z++) {
|
||||
for (int x = minX; x < minX + across; x++) {
|
||||
lowest = Math.min(lowest, terrainY.applyAsInt(x, z));
|
||||
}
|
||||
}
|
||||
return lowest;
|
||||
}
|
||||
|
||||
// From the top down, a run holds the solid heights in a row that mostly show one block, so a canopy, the trunk
|
||||
// under it and the ground are runs of their own. Once a run reaches the ground, it is the ground and the last.
|
||||
int addRuns(int fewestSolid, int groundY, ShortArrayList bottoms, ShortArrayList tops, List<String> blocks) {
|
||||
Runs runs = new Runs(bottoms, tops, blocks);
|
||||
for (int y = chunk.getHeight() - 1; y >= 0; y--) {
|
||||
if (solidCountAt(y) < fewestSolid) {
|
||||
runs.endAbove(y);
|
||||
continue;
|
||||
}
|
||||
runs.extendTo(y, mostShownAt(y));
|
||||
if (y <= groundY) {
|
||||
runs.endAsGround();
|
||||
return runs.found;
|
||||
}
|
||||
}
|
||||
runs.endAsGround();
|
||||
return runs.found;
|
||||
}
|
||||
|
||||
private int solidCountAt(int y) {
|
||||
int solid = 0;
|
||||
for (int z = minZ; z < minZ + across; z++) {
|
||||
for (int x = minX; x < minX + across; x++) {
|
||||
if (isSolid(chunk.get(x, y, z))) {
|
||||
solid++;
|
||||
}
|
||||
}
|
||||
}
|
||||
return solid;
|
||||
}
|
||||
|
||||
private String mostShownAt(int y) {
|
||||
Map<String, Integer> shown = new HashMap<>();
|
||||
for (int z = minZ; z < minZ + across; z++) {
|
||||
for (int x = minX; x < minX + across; x++) {
|
||||
PlatformBlockState block = chunk.get(x, y, z);
|
||||
if (isSolid(block)) {
|
||||
shown.merge(block.key(), 1, Integer::sum);
|
||||
}
|
||||
}
|
||||
}
|
||||
return shown.entrySet().stream().max(Map.Entry.comparingByValue()).map(Map.Entry::getKey).orElseThrow();
|
||||
}
|
||||
|
||||
private boolean isSolid(PlatformBlockState block) {
|
||||
if (block == null || block.isAir()) {
|
||||
return false;
|
||||
}
|
||||
PlatformBlockState seen = seenByPlayers.apply(block);
|
||||
return !seen.isAir() && !seen.isFluid() && seen.isSolid();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -43,6 +43,18 @@ interface ColumnTops {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* The runs of solid blocks of the area a column of a coarser tile stands for, highest first, as {@link ColumnRuns}
|
||||
* finds them; null where the source knows only the tops of its columns.
|
||||
*/
|
||||
default AreaRuns runsAt(int worldX, int worldZ) {
|
||||
return null;
|
||||
}
|
||||
|
||||
/** One area's runs of solid blocks, highest first, the last being the ground. */
|
||||
record AreaRuns(short[] bottoms, short[] tops, String[] blocks) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Blocks seen in a square of columns, each with the sides that face the open air.
|
||||
*
|
||||
|
||||
@@ -37,6 +37,7 @@ import it.unimi.dsi.fastutil.longs.LongList;
|
||||
import it.unimi.dsi.fastutil.shorts.ShortArrayList;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
@@ -72,6 +73,8 @@ final class GeneratedChunks implements ColumnTops {
|
||||
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;
|
||||
// Its bottom, its top and a shared key.
|
||||
private static final int RUN_BYTES = Short.BYTES + Short.BYTES + Integer.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.
|
||||
@@ -105,20 +108,21 @@ final class GeneratedChunks implements ColumnTops {
|
||||
@Override
|
||||
public ColumnTops prepared(TileGrid grid) {
|
||||
boolean keepsChunks = grid.step() < BLOCKS_ACROSS;
|
||||
int runLevel = runLevelOf(grid.step());
|
||||
Long2ObjectMap<LongList> columnsByChunk = columnsByChunkOf(grid);
|
||||
Map<Long, ColumnTop> found = new ConcurrentHashMap<>();
|
||||
Map<Long, ColumnSample> 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) {
|
||||
read(key, known, keepsChunks, chunk.getValue(), found, chunks);
|
||||
read(key, known, keepsChunks, runLevel, chunk.getValue(), found, chunks);
|
||||
continue;
|
||||
}
|
||||
Runnable generation = () -> {
|
||||
if (!grid.cancelled().getAsBoolean()) {
|
||||
read(key, keepsChunks ? tops.get(key) : topWithoutKeeping(key), keepsChunks, chunk.getValue(), found, chunks);
|
||||
read(key, keepsChunks ? tops.get(key) : topWithoutKeeping(key), keepsChunks, runLevel, chunk.getValue(), found, chunks);
|
||||
}
|
||||
};
|
||||
ChunkTask task = new ChunkTask(generation, grid.step());
|
||||
@@ -136,20 +140,36 @@ final class GeneratedChunks implements ColumnTops {
|
||||
return new PreparedTile(found, chunks);
|
||||
}
|
||||
|
||||
// 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));
|
||||
return blocksPerColumn < 2D ? 0 : Math.min(level, ColumnRuns.MOST_LEVEL);
|
||||
}
|
||||
|
||||
/** One column of a tile as read from its chunk: its top, and the runs of the area it stands for, if any. */
|
||||
private record ColumnSample(ColumnTop top, AreaRuns runs) {
|
||||
}
|
||||
|
||||
/** 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, ColumnSample> columns;
|
||||
private final Map<Long, ChunkTop> chunks;
|
||||
|
||||
PreparedTile(Map<Long, ColumnTop> columns, Map<Long, ChunkTop> chunks) {
|
||||
PreparedTile(Map<Long, ColumnSample> 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);
|
||||
ColumnSample column = columns.get(keyOf(worldX, worldZ));
|
||||
return column != null ? column.top() : GeneratedChunks.this.at(worldX, worldZ);
|
||||
}
|
||||
|
||||
@Override
|
||||
public AreaRuns runsAt(int worldX, int worldZ) {
|
||||
ColumnSample column = columns.get(keyOf(worldX, worldZ));
|
||||
return column == null ? null : column.runs();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -208,8 +228,9 @@ final class GeneratedChunks implements ColumnTops {
|
||||
return columnsByChunk;
|
||||
}
|
||||
|
||||
private static void read(long key, ChunkTop chunk, boolean keepsChunk, LongList columns, Map<Long, ColumnTop> found, Map<Long, ChunkTop> chunks) {
|
||||
readColumns(chunk, columns, found);
|
||||
private static void read(long key, ChunkTop chunk, boolean keepsChunk, int runLevel, LongList columns, Map<Long, ColumnSample> found,
|
||||
Map<Long, ChunkTop> chunks) {
|
||||
readColumns(chunk, runLevel, columns, found);
|
||||
if (keepsChunk) {
|
||||
chunks.put(key, chunk);
|
||||
}
|
||||
@@ -227,14 +248,25 @@ final class GeneratedChunks implements ColumnTops {
|
||||
}
|
||||
}
|
||||
|
||||
private static void readColumns(ChunkTop chunk, LongList columns, Map<Long, ColumnTop> found) {
|
||||
private static void readColumns(ChunkTop chunk, int runLevel, LongList columns, Map<Long, ColumnSample> found) {
|
||||
for (long column : columns) {
|
||||
int worldX = (int) (column >> Integer.SIZE);
|
||||
int worldZ = (int) column;
|
||||
found.put(column, chunk.columns[(worldZ & CHUNK_MASK) * BLOCKS_ACROSS + (worldX & CHUNK_MASK)]);
|
||||
ColumnTop top = chunk.columns[(worldZ & CHUNK_MASK) * BLOCKS_ACROSS + (worldX & CHUNK_MASK)];
|
||||
AreaRuns runs = runLevel == 0 ? null : runsOf(chunk.runs[runLevel - 1], worldX & CHUNK_MASK, worldZ & CHUNK_MASK, runLevel);
|
||||
found.put(column, new ColumnSample(top, runs));
|
||||
}
|
||||
}
|
||||
|
||||
private static AreaRuns runsOf(ColumnRuns runs, int x, int z, int level) {
|
||||
int areasAcross = BLOCKS_ACROSS >> level;
|
||||
int area = (z >> level) * areasAcross + (x >> level);
|
||||
int first = runs.firstRunOf(area);
|
||||
int last = first + runs.counts()[area];
|
||||
return new AreaRuns(Arrays.copyOfRange(runs.bottoms(), first, last), Arrays.copyOfRange(runs.tops(), first, last),
|
||||
Arrays.copyOfRange(runs.blocks(), first, last));
|
||||
}
|
||||
|
||||
private static void awaitUnlessCancelled(Future<?> chunk, BooleanSupplier cancelled) {
|
||||
while (true) {
|
||||
MapTileRenderer.checkCancelled(cancelled);
|
||||
@@ -296,7 +328,11 @@ final class GeneratedChunks implements ColumnTops {
|
||||
}
|
||||
IntBinaryOperator terrainY = (x, z) -> engine.getHeight((chunkX << CHUNK_SHIFT) + x, (chunkZ << CHUNK_SHIFT) + z, true);
|
||||
int fluidHeight = engine.getDimension().getFluidHeight();
|
||||
return new ChunkTop(topsOf(blocks, terrainY, this::seenByPlayers), VisibleBlocks.of(blocks, terrainY, fluidHeight, this::seenByPlayers));
|
||||
ColumnRuns[] runs = new ColumnRuns[ColumnRuns.MOST_LEVEL];
|
||||
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);
|
||||
}
|
||||
|
||||
// A plugin's block can be solid on the server where players see grass they walk through.
|
||||
@@ -314,13 +350,20 @@ final class GeneratedChunks implements ColumnTops {
|
||||
return ((long) chunkX << Integer.SIZE) | (chunkZ & 0xFFFFFFFFL);
|
||||
}
|
||||
|
||||
private record ChunkTop(ColumnTop[] columns, VisibleBlocks visible) {
|
||||
/**
|
||||
* @param runs the chunk's runs of solid blocks in areas 2, 4, 8 and 16 blocks across, for coarser tiles
|
||||
*/
|
||||
private record ChunkTop(ColumnTop[] columns, VisibleBlocks visible, ColumnRuns[] runs) {
|
||||
int bytes() {
|
||||
int raisedBlocks = 0;
|
||||
for (ColumnTop column : columns) {
|
||||
raisedBlocks += column.raised().count();
|
||||
}
|
||||
return CHUNK_TOP_BYTES + raisedBlocks * RAISED_BLOCK_BYTES + visible.count() * VISIBLE_BLOCK_BYTES;
|
||||
int runs = 0;
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -47,12 +47,28 @@ public record MapTile(int size, double blocksPerPixel, MapLabel.Kind subjectKind
|
||||
* @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
|
||||
* @param runs the runs of solid blocks of the area each cell stands for, in a coarser tile; else null
|
||||
*/
|
||||
public record Blocks(List<String> names, short[] ground, short[] cover, short[] raisedCounts, short[] raisedY, short[] raised,
|
||||
Voxels voxels) {
|
||||
Voxels voxels, Runs runs) {
|
||||
public static final short NO_BLOCK = -1;
|
||||
}
|
||||
|
||||
/**
|
||||
* The ground of a coarser tile in 3D: for each cell, the runs of solid blocks of the area it stands for, highest
|
||||
* first, the last being the ground, which reaches down out of sight.
|
||||
*
|
||||
* @param counts how many runs each cell has
|
||||
* @param bottoms the lowest height of each run, cell after cell
|
||||
* @param tops the highest height of each run
|
||||
* @param blocks the index in the tile's block names of the block most of each run's top shows
|
||||
*/
|
||||
public record Runs(short[] counts, short[] bottoms, short[] tops, short[] blocks) {
|
||||
public int count() {
|
||||
return tops.length;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*
|
||||
|
||||
@@ -77,12 +77,13 @@ 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
|
||||
* @param withShape whether the tile also carries the ground's shape in 3D, where the source knows it: every block
|
||||
* seen from the open air in a tile of a block a pixel, the runs of solid blocks of each cell's area
|
||||
* in a coarser one
|
||||
* @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, boolean withVoxels, BooleanSupplier cancelled) {
|
||||
public MapTile withBlocks(MapTile tile, double startX, double startZ, BlockSource source, boolean withShape, BooleanSupplier cancelled) {
|
||||
double step = tile.blocksPerPixel();
|
||||
if (step > source.largestBlocksPerPixel()) {
|
||||
throw new IllegalArgumentException("Blocks from " + source.id() + " are drawn at " + source.largestBlocksPerPixel()
|
||||
@@ -117,11 +118,36 @@ public final class MapTileRenderer {
|
||||
}
|
||||
}
|
||||
}
|
||||
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);
|
||||
MapTile.Voxels voxels = withShape && step == 1D ? voxelsOf(tops, startX, startZ, tile.size(), names) : null;
|
||||
MapTile.Runs runs = withShape && step > 1D ? runsOf(tops, grid, names) : null;
|
||||
MapTile.Blocks blocks = new MapTile.Blocks(names.names(), ground, cover, raisedCounts, raisedY.toShortArray(), raised.toShortArray(),
|
||||
voxels, runs);
|
||||
return tile.withBlocks(blocks, heights);
|
||||
}
|
||||
|
||||
private static MapTile.Runs runsOf(ColumnTops tops, TileGrid grid, BlockNames names) {
|
||||
short[] counts = new short[grid.size() * grid.size()];
|
||||
ShortArrayList bottoms = new ShortArrayList();
|
||||
ShortArrayList runTops = new ShortArrayList();
|
||||
ShortArrayList blocks = new ShortArrayList();
|
||||
for (int gridZ = 0; gridZ < grid.size(); gridZ++) {
|
||||
int worldZ = (int) Math.floor(grid.worldZ(gridZ));
|
||||
for (int gridX = 0; gridX < grid.size(); gridX++) {
|
||||
ColumnTops.AreaRuns area = tops.runsAt((int) Math.floor(grid.worldX(gridX)), worldZ);
|
||||
if (area == null) {
|
||||
return null;
|
||||
}
|
||||
counts[gridZ * grid.size() + gridX] = (short) area.tops().length;
|
||||
bottoms.addElements(bottoms.size(), area.bottoms());
|
||||
runTops.addElements(runTops.size(), area.tops());
|
||||
for (String block : area.blocks()) {
|
||||
blocks.add(names.indexOf(block));
|
||||
}
|
||||
}
|
||||
}
|
||||
return new MapTile.Runs(counts, bottoms.toShortArray(), runTops.toShortArray(), blocks.toShortArray());
|
||||
}
|
||||
|
||||
// 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);
|
||||
|
||||
@@ -0,0 +1,88 @@
|
||||
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.function.UnaryOperator;
|
||||
|
||||
import static org.junit.Assert.assertArrayEquals;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
public class ColumnRunsTest {
|
||||
private static final int HEIGHT = 16;
|
||||
private static final int GROUND_Y = 3;
|
||||
private static final PlatformBlockState GRASS = block("minecraft:grass_block");
|
||||
private static final PlatformBlockState LOG = block("minecraft:oak_log");
|
||||
private static final PlatformBlockState LEAVES = block("minecraft:oak_leaves");
|
||||
|
||||
@Test
|
||||
public void flatGroundIsOneRunPerArea() {
|
||||
ColumnRuns runs = ColumnRuns.of(groundUpTo(GROUND_Y), 1, (x, z) -> GROUND_Y, UnaryOperator.identity());
|
||||
|
||||
assertEquals(64, runs.counts().length);
|
||||
assertEquals(1, runs.counts()[0]);
|
||||
assertEquals(GROUND_Y, runs.tops()[0]);
|
||||
assertEquals("minecraft:grass_block", runs.blocks()[0]);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void aTreeIsACanopyOnATrunkOnTheGroundNotAPillar() {
|
||||
Hunk<PlatformBlockState> chunk = groundUpTo(GROUND_Y);
|
||||
for (int y = GROUND_Y + 1; y <= GROUND_Y + 4; y++) {
|
||||
chunk.set(0, y, 0, LOG);
|
||||
}
|
||||
for (int z = 0; z < 2; z++) {
|
||||
for (int x = 0; x < 2; x++) {
|
||||
chunk.set(x, GROUND_Y + 5, z, LEAVES);
|
||||
chunk.set(x, GROUND_Y + 6, z, LEAVES);
|
||||
}
|
||||
}
|
||||
|
||||
ColumnRuns runs = ColumnRuns.of(chunk, 1, (x, z) -> GROUND_Y, UnaryOperator.identity());
|
||||
|
||||
assertEquals(3, runs.counts()[0]);
|
||||
assertArrayEquals(new short[]{GROUND_Y + 6, GROUND_Y + 4, GROUND_Y}, new short[]{runs.tops()[0], runs.tops()[1], runs.tops()[2]});
|
||||
assertArrayEquals(new short[]{GROUND_Y + 5, GROUND_Y + 1, 0}, new short[]{runs.bottoms()[0], runs.bottoms()[1], runs.bottoms()[2]});
|
||||
assertArrayEquals(new String[]{"minecraft:oak_leaves", "minecraft:oak_log", "minecraft:grass_block"}, new String[]{runs.blocks()[0], runs.blocks()[1], runs.blocks()[2]});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void aCanopyOverOpenGroundFloatsWithAirUnderIt() {
|
||||
Hunk<PlatformBlockState> chunk = groundUpTo(GROUND_Y);
|
||||
for (int z = 2; z < 4; z++) {
|
||||
for (int x = 2; x < 4; x++) {
|
||||
chunk.set(x, GROUND_Y + 5, z, LEAVES);
|
||||
}
|
||||
}
|
||||
|
||||
ColumnRuns runs = ColumnRuns.of(chunk, 1, (x, z) -> GROUND_Y, UnaryOperator.identity());
|
||||
int area = 1 * 8 + 1;
|
||||
int first = runs.firstRunOf(area);
|
||||
|
||||
assertEquals(2, runs.counts()[area]);
|
||||
assertEquals(GROUND_Y + 5, runs.bottoms()[first]);
|
||||
assertEquals(GROUND_Y, runs.tops()[first + 1]);
|
||||
}
|
||||
|
||||
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, GRASS);
|
||||
}
|
||||
}
|
||||
}
|
||||
return chunk;
|
||||
}
|
||||
|
||||
private static PlatformBlockState block(String key) {
|
||||
PlatformBlockState block = mock(PlatformBlockState.class);
|
||||
when(block.key()).thenReturn(key);
|
||||
when(block.isSolid()).thenReturn(true);
|
||||
return block;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user