Light the 3D world as Minecraft's smooth lighting does

The server works out the sky's light in each cell of a chunk, full under the open sky and a level less through each
leaf, block of water or step aside, and for each visible face the light of the cell it faces and how many opaque cells
stand around each of its corners. The studio darkens the faces' corners by them, splitting each face along its brighter
corners, so terrace steps, cliff feet and the ground under trees and overhangs read in depth. Far runs carry the mean
light and corner shade of the block tops they stand for, so far ground darkens as the near ground does.
This commit is contained in:
piexel committed 2026-11-12 13:01:27 +01:00
1 parent 87b088787b
commit d4da60a253
19 files changed
+1011 -203

No files matched your search

@@ -39,13 +39,15 @@ import java.util.function.Function;
* 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
* | i16 voxelBlock[voxels] | padding to 4 | u8 voxelFaces[voxels] | padding to 4 | u8 voxelLights[voxels] | padding to 4
* | u16 voxelShades[shades] | 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 | i16 runCovers[runs] | padding to 4 | u8 runCoverShares[runs] | padding to 4,
* when the header counts runs
* | i16 runBlocks[runs] | padding to 4 | i16 runCovers[runs] | padding to 4 | u8 runCoverShares[runs] | padding to 4
* | u8 runLights[runs] | padding to 4 | u8 runOcclusions[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, and {@code runs}, the sum of the run counts, each -1 for a tile without them.
* {@code shades} is the count of faces the voxel faces name, one a set bit.
* 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.
@@ -79,8 +81,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)
+ (runs == null ? 0 : aligned(cells * Short.BYTES) + aligned(runCount * Short.BYTES) * 4 + aligned(runCount));
+ (voxels == null ? 0 : aligned(voxelCount) * 4 + aligned(voxelCount * Short.BYTES) * 2 + aligned(voxels.shades().length * Short.BYTES))
+ (runs == null ? 0 : aligned(cells * Short.BYTES) + aligned(runCount * Short.BYTES) * 4 + aligned(runCount) * 3);
ByteBuffer buffer = ByteBuffer.allocate(length).order(ByteOrder.LITTLE_ENDIAN);
buffer.putInt(header.length).put(header);
align(buffer);
@@ -107,6 +109,8 @@ final class MapTileEncoding {
putShorts(buffer, voxels.y());
putShorts(buffer, voxels.blocks());
putBytes(buffer, voxels.faces());
putBytes(buffer, voxels.lights());
putShorts(buffer, voxels.shades());
}
if (runs != null) {
putShorts(buffer, runs.counts());
@@ -115,6 +119,8 @@ final class MapTileEncoding {
putShorts(buffer, runs.blocks());
putShorts(buffer, runs.covers());
putBytes(buffer, runs.coverShares());
putBytes(buffer, runs.lights());
putBytes(buffer, runs.occlusions());
}
return buffer.array();
}
@@ -19,8 +19,9 @@
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.bytes.ByteArrayList;
import it.unimi.dsi.fastutil.ints.Int2ByteMap;
import it.unimi.dsi.fastutil.ints.Int2ByteOpenHashMap;
import it.unimi.dsi.fastutil.shorts.ShortArrayList;
import java.util.ArrayList;
@@ -43,9 +44,9 @@ record ChunkBorders(Wall[] walls) {
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. */
/** Takes a face a chunk shows on {@code side}, at its x, y and z in the chunk, lit by the cell it faces. */
interface FaceSink {
void add(int x, int y, int z, String block, int side);
void add(int x, int y, int z, String block, int side, int light);
}
/**
@@ -69,10 +70,11 @@ record ChunkBorders(Wall[] walls) {
* @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
* @param light the sky's light in each run of open air, 0 to 15; 0 for others
*/
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;
record Runs(int[] firstOfPlace, short[] bottoms, short[] tops, String[] blocks, boolean[] fluid, byte[] light) {
// A bottom, a top, a shared key, the fluid flag and the light.
private static final int RUN_BYTES = Short.BYTES + Short.BYTES + Integer.BYTES + 2;
int bytes() {
return firstOfPlace.length * Integer.BYTES + tops.length * RUN_BYTES;
@@ -102,7 +104,7 @@ record ChunkBorders(Wall[] walls) {
}
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);
faces.add(xOf(side, place), y, zOf(side, place), hidden.blocks()[run], side, open.light()[air]);
}
}
}
@@ -112,18 +114,18 @@ 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.
* is a place in the chunk as {@link VisibleBlocks#positions()} gives it, with the light of the cell beside it.
*/
IntList reachedFrom(ChunkBorders beside, int side) {
Int2ByteMap reachedFrom(ChunkBorders beside, int side) {
Runs sealed = walls[side - NORTH].sealed();
Runs open = beside.walls[opposite(side) - NORTH].open();
IntList reached = new IntArrayList();
Int2ByteMap reached = new Int2ByteOpenHashMap();
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)));
reached.put(VisibleBlocks.positionOf(xOf(side, place), y, zOf(side, place)), open.light()[air]);
}
}
}
@@ -158,6 +160,7 @@ record ChunkBorders(Wall[] walls) {
private final ShortArrayList tops = new ShortArrayList();
private final List<String> blocks = new ArrayList<>();
private final BooleanArrayList fluid = new BooleanArrayList();
private final ByteArrayList light = new ByteArrayList();
private int place;
/** Starts the runs of the next place along the side. */
@@ -166,10 +169,10 @@ record ChunkBorders(Wall[] walls) {
}
/** 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) {
void add(int y, String block, boolean holdsFluid, int cellLight) {
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)));
&& light.getByte(last) == cellLight && (block == null ? blocks.get(last) == null : block.equals(blocks.get(last)));
if (joins) {
tops.set(last, (short) (y + 1));
return;
@@ -178,11 +181,13 @@ record ChunkBorders(Wall[] walls) {
tops.add((short) (y + 1));
blocks.add(block);
fluid.add(holdsFluid);
light.add((byte) cellLight);
}
Runs build() {
firstOfPlace[ACROSS] = tops.size();
return new Runs(firstOfPlace, bottoms.toShortArray(), tops.toShortArray(), blocks.toArray(String[]::new), fluid.toBooleanArray());
return new Runs(firstOfPlace, bottoms.toShortArray(), tops.toShortArray(), blocks.toArray(String[]::new), fluid.toBooleanArray(),
light.toByteArray());
}
}
}
@@ -0,0 +1,330 @@
/*
* 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.ints.Int2ByteMap;
import it.unimi.dsi.fastutil.ints.IntArrayFIFOQueue;
import java.util.Set;
import java.util.function.IntBinaryOperator;
import java.util.function.UnaryOperator;
/**
* A chunk's cells as players see them, with the sky's light in each, as Minecraft lights a world: full under the open
* sky, through air without loss, one level less through each leaf or block of water and one less with each step aside.
* Beside the chunk, the terrain's height alone tells what lies there: open air above it, the sea below the fluid's height.
* <p>
* A block face is shaded by the light of the cell it faces and by how many of the blocks around each of its corners are
* opaque, as Minecraft's smooth lighting darkens the corners where blocks meet.
*/
final class ChunkCells {
static final int FULL_LIGHT = 15;
/** Where a face's light lies in its shade; each corner's opaque neighbours follow in two bits each. */
static final int LIGHT_BITS = 4;
static final int CORNER_BITS = 2;
static final int MOST_OPAQUE_AROUND_CORNER = 3;
private static final int LIGHT_MASK = (1 << LIGHT_BITS) - 1;
private static final int CORNERS = 4;
private static final int ACROSS = 16;
private static final int UP = 1;
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 String LEAVES_SUFFIX = "_leaves";
/**
* Each side's face corners as the studio draws them, a cube's corner each, in {@link VisibleBlocks#SIDES}' order:
* top left, bottom left, bottom right and top right as seen from outside.
*/
private static final int[][][] FACE_CORNERS = {
{{0, 0, 1}, {0, 0, 0}, {1, 0, 0}, {1, 0, 1}},
{{0, 1, 0}, {0, 1, 1}, {1, 1, 1}, {1, 1, 0}},
{{1, 1, 0}, {1, 0, 0}, {0, 0, 0}, {0, 1, 0}},
{{0, 1, 1}, {0, 0, 1}, {1, 0, 1}, {1, 1, 1}},
{{0, 1, 0}, {0, 0, 0}, {0, 0, 1}, {0, 1, 1}},
{{1, 1, 1}, {1, 0, 1}, {1, 0, 0}, {1, 1, 0}}
};
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 byte[] dimming;
private final byte[] light;
private ChunkCells(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.dimming = new byte[cells];
this.light = new byte[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]);
dimming[cell] = (byte) (fluid[cell] || (seen[cell] != null && materialOf(seen[cell].key()).endsWith(LEAVES_SUFFIX)) ? 1 : 0);
}
}
}
}
/**
* @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
* @param fluidHeight the height the sea fills to
* @param seenByPlayers the state players see in place of a block, which decides what hides what
* @param litBeside cells along the chunk's sides, as {@link VisibleBlocks#positionOf} places them, that the open
* air of the chunks beside it reaches, each with the light of the cell beside it there
*/
static ChunkCells of(Hunk<PlatformBlockState> chunk, IntBinaryOperator terrainY, int fluidHeight, UnaryOperator<PlatformBlockState> seenByPlayers,
Int2ByteMap litBeside) {
ChunkCells cells = new ChunkCells(chunk, terrainY, fluidHeight, seenByPlayers);
cells.spreadSkyLight(litBeside);
return cells;
}
int height() {
return height;
}
Hunk<PlatformBlockState> chunk() {
return chunk;
}
/** The block players see in a cell of the chunk, or null for air. */
PlatformBlockState seenAt(int x, int y, int z) {
return seen[cellOf(x, y, z)];
}
boolean isOpen(int x, int y, int z) {
return open[cellOf(x, y, z)];
}
boolean holdsFluid(int x, int y, int z) {
return fluid[cellOf(x, y, z)];
}
/** What lies beside the chunk, judged by the terrain's height there alone. */
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;
}
/** The sky's light in a cell, 0 to {@link #FULL_LIGHT}, in the chunk or beside it. */
int lightAt(int x, int y, int z) {
if (isInside(x, y, z)) {
return light[cellOf(x, y, z)];
}
return switch (outsideAt(x, y, z)) {
case AIR -> FULL_LIGHT;
case FLUID -> Math.max(0, FULL_LIGHT - (fluidHeight - y));
case SOLID -> 0;
};
}
/** The light a block shows on faces that stand inside it, such as a plant's: its own cell's, or the brightest beside a solid one. */
int blockLightAt(int x, int y, int z) {
if (open[cellOf(x, y, z)]) {
return light[cellOf(x, y, z)];
}
int brightest = 0;
for (int[] side : VisibleBlocks.SIDES) {
brightest = Math.max(brightest, lightAt(x + side[0], y + side[1], z + side[2]));
}
return brightest;
}
/**
* The shade of a block's face on a side: the light of the cell it faces, then for each of its corners, in
* {@link #FACE_CORNERS}' order, how many of the three cells around the corner in front of the face are opaque, as
* Minecraft counts them; two opaque sides hide the corner between them too.
*/
short faceShade(int x, int y, int z, int side) {
int[] normal = VisibleBlocks.SIDES[side];
int frontX = x + normal[0];
int frontY = y + normal[1];
int frontZ = z + normal[2];
int shade = lightAt(frontX, frontY, frontZ);
for (int corner = 0; corner < CORNERS; corner++) {
shade |= opaqueAround(frontX, frontY, frontZ, normal, FACE_CORNERS[side][corner]) << (LIGHT_BITS + corner * CORNER_BITS);
}
return (short) shade;
}
/** A face's shade lit by {@code light} with no corner darkened, for a face whose surroundings lie in another chunk. */
static short plainShade(int light) {
return (short) light;
}
static int lightOf(short shade) {
return shade & LIGHT_MASK;
}
/** How many opaque cells the corners of a face shaded {@code shade} have together, 0 to 12. */
static int opaqueAroundCorners(short shade) {
int opaque = 0;
for (int corner = 0; corner < CORNERS; corner++) {
opaque += (shade >> (LIGHT_BITS + corner * CORNER_BITS)) & MOST_OPAQUE_AROUND_CORNER;
}
return opaque;
}
/** The shade of the top of the block at a cell. */
short topShade(int x, int y, int z) {
return faceShade(x, y, z, UP);
}
private int opaqueAround(int frontX, int frontY, int frontZ, int[] normal, int[] corner) {
int[] first = new int[3];
int[] second = new int[3];
int found = 0;
for (int axis = 0; axis < 3; axis++) {
if (normal[axis] != 0) {
continue;
}
int[] along = found++ == 0 ? first : second;
along[axis] = corner[axis] == 1 ? 1 : -1;
}
boolean firstOpaque = isOpaque(frontX + first[0], frontY + first[1], frontZ + first[2]);
boolean secondOpaque = isOpaque(frontX + second[0], frontY + second[1], frontZ + second[2]);
if (firstOpaque && secondOpaque) {
return MOST_OPAQUE_AROUND_CORNER;
}
boolean between = isOpaque(frontX + first[0] + second[0], frontY + first[1] + second[1], frontZ + first[2] + second[2]);
return (firstOpaque ? 1 : 0) + (secondOpaque ? 1 : 0) + (between ? 1 : 0);
}
private boolean isOpaque(int x, int y, int z) {
return isInside(x, y, z) ? !open[cellOf(x, y, z)] : outsideAt(x, y, z) == Outside.SOLID;
}
// Straight down from the sky in each column, then aside and on, a level less a step, as far as the light reaches.
private void spreadSkyLight(Int2ByteMap litBeside) {
IntArrayFIFOQueue lit = new IntArrayFIFOQueue();
for (int z = 0; z < ACROSS; z++) {
for (int x = 0; x < ACROSS; x++) {
int level = FULL_LIGHT;
for (int y = height - 1; y >= 0 && open[cellOf(x, y, z)]; y--) {
level -= dimming[cellOf(x, y, z)];
if (level <= 0) {
break;
}
light(cellOf(x, y, z), level, lit);
}
}
}
for (int y = 0; y < height; y++) {
for (int z = 0; z < ACROSS; z++) {
for (int x = 0; x < ACROSS; x++) {
if (isOnSide(x, z)) {
lightFromBeside(x, y, z, lit);
}
}
}
}
for (Int2ByteMap.Entry beside : litBeside.int2ByteEntrySet()) {
int position = beside.getIntKey();
int cell = cellOf(position & (ACROSS - 1), position >> VisibleBlocks.POSITION_Y_SHIFT, (position >> 4) & (ACROSS - 1));
if (open[cell]) {
light(cell, beside.getByteValue() - 1 - dimming[cell], lit);
}
}
while (!lit.isEmpty()) {
int cell = lit.dequeueInt();
int x = cell % ACROSS;
int z = (cell / ACROSS) % ACROSS;
int y = cell / (ACROSS * ACROSS);
for (int[] side : VisibleBlocks.SIDES) {
int nx = x + side[0];
int ny = y + side[1];
int nz = z + side[2];
if (isInside(nx, ny, nz) && open[cellOf(nx, ny, nz)]) {
int neighbour = cellOf(nx, ny, nz);
light(neighbour, light[cell] - 1 - dimming[neighbour], lit);
}
}
}
}
private void lightFromBeside(int x, int y, int z, IntArrayFIFOQueue lit) {
int cell = cellOf(x, y, z);
if (!open[cell]) {
return;
}
for (int side = 2; side < VisibleBlocks.SIDES.length; side++) {
int[] step = VisibleBlocks.SIDES[side];
if (!isInside(x + step[0], y, z + step[2])) {
light(cell, lightAt(x + step[0], y, z + step[2]) - 1 - dimming[cell], lit);
}
}
}
private void light(int cell, int level, IntArrayFIFOQueue lit) {
if (level > light[cell]) {
light[cell] = (byte) level;
lit.enqueue(cell);
}
}
private static boolean isOnSide(int x, int z) {
return x == 0 || z == 0 || x == ACROSS - 1 || z == ACROSS - 1;
}
// 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);
}
boolean isInside(int x, int y, int z) {
return x >= 0 && x < ACROSS && z >= 0 && z < ACROSS && y >= 0 && y < height;
}
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. */
enum Outside {
SOLID,
FLUID,
AIR
}
}
@@ -44,10 +44,17 @@ import java.util.function.UnaryOperator;
* @param blocks the key of the block most of each run's top shows
* @param covers the key of the plant most often standing on each run's top, or null where none does
* @param coverShares how much of each run's top its plants stand on, 0 for none to {@link #WHOLE_SHARE} for all of it
* @param lights the sky's light on each run's top, the mean of its blocks' tops in {@link #LIGHT_STEPS} of a level
* @param occlusions how many opaque cells stand around each corner of each run's top, the mean of its blocks' tops in
* {@link #OCCLUSION_STEPS} of a cell, so far ground is as dark as the near ground it stands for
*/
record ColumnRuns(short[] counts, short[] bottoms, short[] tops, String[] blocks, String[] covers, byte[] coverShares) {
record ColumnRuns(short[] counts, short[] bottoms, short[] tops, String[] blocks, String[] covers, byte[] coverShares, byte[] lights,
byte[] occlusions) {
static final int MOST_LEVEL = 4;
static final int WHOLE_SHARE = 255;
static final int LIGHT_STEPS = 16;
static final int OCCLUSION_STEPS = 64;
private static final int CORNERS = 4;
private static final int ACROSS = 16;
private static final int SOLID_SHARE_DIVISOR = 4;
@@ -64,8 +71,10 @@ record ColumnRuns(short[] counts, short[] bottoms, short[] tops, String[] blocks
* @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
* @param cells the chunk's cells with their light, which shade the runs' tops
*/
static ColumnRuns of(Hunk<PlatformBlockState> chunk, int level, IntBinaryOperator terrainY, UnaryOperator<PlatformBlockState> seenByPlayers) {
static ColumnRuns of(Hunk<PlatformBlockState> chunk, int level, IntBinaryOperator terrainY, UnaryOperator<PlatformBlockState> seenByPlayers,
ChunkCells cells) {
int areaBlocks = 1 << level;
int areasAcross = ACROSS / areaBlocks;
int fewestSolid = Math.max(1, areaBlocks * areaBlocks / SOLID_SHARE_DIVISOR);
@@ -73,7 +82,7 @@ record ColumnRuns(short[] counts, short[] bottoms, short[] tops, String[] blocks
Runs runs = new Runs();
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);
Area area = new Area(chunk, areaX * areaBlocks, areaZ * areaBlocks, areaBlocks, seenByPlayers, cells);
int before = runs.found;
area.addRuns(fewestSolid, area.lowestOf(terrainY), runs);
counts[areaZ * areasAcross + areaX] = (short) (runs.found - before);
@@ -86,7 +95,7 @@ record ColumnRuns(short[] counts, short[] bottoms, short[] tops, String[] blocks
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) && Arrays.equals(covers, runs.covers)
&& Arrays.equals(coverShares, runs.coverShares);
&& Arrays.equals(coverShares, runs.coverShares) && Arrays.equals(lights, runs.lights) && Arrays.equals(occlusions, runs.occlusions);
}
@Override
@@ -105,6 +114,10 @@ record ColumnRuns(short[] counts, short[] bottoms, short[] tops, String[] blocks
static final Cover NONE = new Cover(null, 0);
}
/** How a run's top is lit, in {@link #LIGHT_STEPS} and {@link #OCCLUSION_STEPS}. */
private record Shade(int light, int occlusion) {
}
/** The runs of every area as they are found, area after area and each area's from the top down. */
private static final class Runs {
private static final int NONE = -1;
@@ -113,10 +126,13 @@ record ColumnRuns(short[] counts, short[] bottoms, short[] tops, String[] blocks
private final List<String> blocks = new ArrayList<>();
private final List<String> covers = new ArrayList<>();
private final ByteArrayList coverShares = new ByteArrayList();
private final ByteArrayList lights = new ByteArrayList();
private final ByteArrayList occlusions = new ByteArrayList();
private int top = NONE;
private int bottom;
private String block;
private Cover cover;
private Shade shade;
private int found;
boolean isOpen() {
@@ -127,11 +143,12 @@ record ColumnRuns(short[] counts, short[] bottoms, short[] tops, String[] blocks
return isOpen() && shown.equals(block);
}
void start(int y, String shown, Cover onTop) {
void start(int y, String shown, Cover onTop, Shade topShade) {
top = y;
bottom = y;
block = shown;
cover = onTop;
shade = topShade;
}
void extendTo(int y) {
@@ -157,18 +174,21 @@ record ColumnRuns(short[] counts, short[] bottoms, short[] tops, String[] blocks
blocks.add(block);
covers.add(cover.block());
coverShares.add((byte) cover.share());
lights.add((byte) shade.light());
occlusions.add((byte) shade.occlusion());
found++;
top = NONE;
}
ColumnRuns toColumnRuns(short[] counts) {
return new ColumnRuns(counts, bottoms.toShortArray(), tops.toShortArray(), blocks.toArray(String[]::new),
covers.toArray(String[]::new), coverShares.toByteArray());
covers.toArray(String[]::new), coverShares.toByteArray(), lights.toByteArray(), occlusions.toByteArray());
}
}
/** 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) {
private record Area(Hunk<PlatformBlockState> chunk, int minX, int minZ, int across, UnaryOperator<PlatformBlockState> seenByPlayers,
ChunkCells cells) {
int lowestOf(IntBinaryOperator terrainY) {
int lowest = Integer.MAX_VALUE;
for (int z = minZ; z < minZ + across; z++) {
@@ -192,7 +212,7 @@ record ColumnRuns(short[] counts, short[] bottoms, short[] tops, String[] blocks
runs.extendTo(y);
} else {
runs.end();
runs.start(y, shown, coverOn(y));
runs.start(y, shown, coverOn(y), shadeOfTopsAt(y));
}
if (y <= groundY) {
runs.endAsGround();
@@ -246,6 +266,38 @@ record ColumnRuns(short[] counts, short[] bottoms, short[] tops, String[] blocks
return standing == 0 ? Cover.NONE : new Cover(mostOf(plants), standing * WHOLE_SHARE / (across * across));
}
// The mean shade of the tops of the area's blocks at the height that are open above, as the near ground shows
// them; where none is, the light above the area, unshaded.
private Shade shadeOfTopsAt(int y) {
int light = 0;
int occlusion = 0;
int tops = 0;
for (int z = minZ; z < minZ + across; z++) {
for (int x = minX; x < minX + across; x++) {
if (isSolid(chunk.get(x, y, z)) && (y + 1 >= chunk.getHeight() || cells.isOpen(x, y + 1, z))) {
short shade = cells.topShade(x, y, z);
light += ChunkCells.lightOf(shade);
occlusion += ChunkCells.opaqueAroundCorners(shade);
tops++;
}
}
}
if (tops == 0) {
return new Shade(lightAbove(y) * LIGHT_STEPS, 0);
}
return new Shade(Math.round((float) light * LIGHT_STEPS / tops), Math.round((float) occlusion * OCCLUSION_STEPS / (tops * CORNERS)));
}
private int lightAbove(int y) {
int brightest = 0;
for (int z = minZ; z < minZ + across; z++) {
for (int x = minX; x < minX + across; x++) {
brightest = Math.max(brightest, cells.lightAt(x, y + 1, z));
}
}
return brightest;
}
private static String mostOf(Map<String, Integer> counts) {
return counts.entrySet().stream().max(Map.Entry.comparingByValue()).map(Map.Entry::getKey).orElseThrow();
}
@@ -53,21 +53,25 @@ interface ColumnTops {
/**
* One area's runs of solid blocks, highest first, the last being the ground, with the plant most often standing on
* each run's top, or null, and how much of the top plants stand on, out of {@link ColumnRuns#WHOLE_SHARE}.
* each run's top, or null, how much of the top plants stand on, out of {@link ColumnRuns#WHOLE_SHARE}, and how the
* top is lit, as {@link ColumnRuns#lights} and {@link ColumnRuns#occlusions} give it.
*/
record AreaRuns(short[] bottoms, short[] tops, String[] blocks, String[] covers, byte[] coverShares) {
record AreaRuns(short[] bottoms, short[] tops, String[] blocks, String[] covers, byte[] coverShares, byte[] lights, byte[] occlusions) {
}
/**
* Blocks seen in a square of columns, each with the sides that face the open air.
* Blocks seen in a square of columns, each with the sides that face the open air and how they are lit.
*
* @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
* @param lights the light each block shows on faces that stand inside it, as {@link VisibleBlocks#lights} gives it
* @param shades the shade of each face {@link #faces} names, block after block and side after side, as
* {@link ChunkCells#faceShade} gives it
*/
record TileVoxels(byte[] x, byte[] z, short[] y, String[] blocks, byte[] faces) {
record TileVoxels(byte[] x, byte[] z, short[] y, String[] blocks, byte[] faces, byte[] lights, short[] shades) {
}
/**
@@ -30,9 +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.ints.Int2ByteMap;
import it.unimi.dsi.fastutil.ints.Int2ByteMaps;
import it.unimi.dsi.fastutil.ints.Int2ByteOpenHashMap;
import it.unimi.dsi.fastutil.longs.Long2IntOpenHashMap;
import it.unimi.dsi.fastutil.longs.Long2ObjectMap;
import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap;
@@ -78,10 +78,10 @@ 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;
// Its bottom, its top and a shared key.
private static final int RUN_BYTES = Short.BYTES + Short.BYTES + Integer.BYTES;
// Its place, a shared key, the sides it shows and its light.
private static final int VISIBLE_BLOCK_BYTES = Integer.BYTES + Integer.BYTES + Byte.BYTES + Byte.BYTES;
// Its bottom, its top, a shared key and its top's light and occlusion.
private static final int RUN_BYTES = Short.BYTES + Short.BYTES + Integer.BYTES + Byte.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.
@@ -261,23 +261,30 @@ final class GeneratedChunks implements ColumnTops {
if (chunk == null) {
return false;
}
IntSet reached = new IntOpenHashSet();
Int2ByteMap reached = new Int2ByteOpenHashMap();
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));
keepBrightest(reached, chunk.visible().borders().reachedFrom(beside.visible().borders(), side));
}
}
if (reached.isEmpty()) {
return false;
}
reached.addAll(chunk.reachedBeside());
keepBrightest(reached, chunk.reachedBeside());
ChunkTop retraced = generateUnchecked(key, reached);
chunks.put(key, retraced);
tops.put(key, retraced);
return true;
}
// A cell on a chunk's corner touches two chunks beside it, and takes the brighter light.
private static void keepBrightest(Int2ByteMap into, Int2ByteMap lights) {
for (Int2ByteMap.Entry light : lights.int2ByteEntrySet()) {
into.mergeByte(light.getIntKey(), light.getByteValue(), (known, found) -> (byte) Math.max(known, found));
}
}
// 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);
@@ -292,7 +299,7 @@ final class GeneratedChunks implements ColumnTops {
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));
(x, y, z, block, faceSide, light) -> voxels.addFace(chunkMinX + x, y, chunkMinZ + z, block, faceSide, light));
}
}
}
@@ -316,6 +323,10 @@ final class GeneratedChunks implements ColumnTops {
private final ShortArrayList y = new ShortArrayList();
private final List<String> blocks = new ArrayList<>();
private final ByteArrayList faces = new ByteArrayList();
private final ByteArrayList lights = new ByteArrayList();
// Each block's face shades by side, SIDES of them a block, as a face shown later can come between.
private final ShortArrayList shadesBySide = new ShortArrayList();
private int shownFaces;
TileVoxelsBuilder(int minX, int minZ, int size) {
this.minX = minX;
@@ -324,36 +335,76 @@ final class GeneratedChunks implements ColumnTops {
}
void addVisible(VisibleBlocks visible, int chunkMinX, int chunkMinZ) {
int shade = 0;
for (int at = 0; at < visible.count(); at++) {
int position = visible.positions()[at];
add(chunkMinX + (position & CHUNK_MASK), position >> VisibleBlocks.POSITION_Y_SHIFT,
chunkMinZ + ((position >> CHUNK_SHIFT) & CHUNK_MASK), visible.blocks()[at], visible.faces()[at]);
int index = indexOf(chunkMinX + (position & CHUNK_MASK), position >> VisibleBlocks.POSITION_Y_SHIFT,
chunkMinZ + ((position >> CHUNK_SHIFT) & CHUNK_MASK), visible.blocks()[at], visible.lights()[at]);
for (int side = 0; side < VisibleBlocks.SIDES.length; side++) {
if ((visible.faces()[at] & 1 << side) == 0) {
continue;
}
short faceShade = visible.shades()[shade++];
if (index != NOT_ADDED) {
show(index, side, faceShade);
}
}
}
}
/** 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) {
/** Adds a block's face on a side at its place in the world, lit by {@code light} with no corner darkened. */
void addFace(int worldX, int blockY, int worldZ, String block, int side, int light) {
int at = indexOf(worldX, blockY, worldZ, block, (byte) 0);
if (at != NOT_ADDED) {
show(at, side, ChunkCells.plainShade(light));
}
}
// The block's index, added at its place in the world unless already there; none outside the square.
private int indexOf(int worldX, int blockY, int worldZ, String block, byte light) {
int squareX = worldX - minX;
int squareZ = worldZ - minZ;
if (squareX < 0 || squareZ < 0 || squareX >= size || squareZ >= size) {
return;
return NOT_ADDED;
}
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;
return known;
}
indexByPlace.put(place, faces.size());
int added = faces.size();
indexByPlace.put(place, added);
x.add((byte) squareX);
z.add((byte) squareZ);
y.add((short) blockY);
blocks.add(block);
faces.add((byte) sides);
faces.add((byte) 0);
lights.add(light);
shadesBySide.size(shadesBySide.size() + VisibleBlocks.SIDES.length);
return added;
}
private void show(int at, int side, short shade) {
byte shown = faces.getByte(at);
if ((shown & 1 << side) == 0) {
faces.set(at, (byte) (shown | 1 << side));
shownFaces++;
}
shadesBySide.set(at * VisibleBlocks.SIDES.length + side, shade);
}
TileVoxels build() {
return new TileVoxels(x.toByteArray(), z.toByteArray(), y.toShortArray(), blocks.toArray(String[]::new), faces.toByteArray());
short[] shades = new short[shownFaces];
int shade = 0;
for (int at = 0; at < faces.size(); at++) {
for (int side = 0; side < VisibleBlocks.SIDES.length; side++) {
if ((faces.getByte(at) & 1 << side) != 0) {
shades[shade++] = shadesBySide.getShort(at * VisibleBlocks.SIDES.length + side);
}
}
}
return new TileVoxels(x.toByteArray(), z.toByteArray(), y.toShortArray(), blocks.toArray(String[]::new), faces.toByteArray(),
lights.toByteArray(), shades);
}
}
@@ -407,7 +458,8 @@ final class GeneratedChunks implements ColumnTops {
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), Arrays.copyOfRange(runs.covers(), first, last),
Arrays.copyOfRange(runs.coverShares(), first, last));
Arrays.copyOfRange(runs.coverShares(), first, last), Arrays.copyOfRange(runs.lights(), first, last),
Arrays.copyOfRange(runs.occlusions(), first, last));
}
private static void awaitUnlessCancelled(Future<?> chunk, BooleanSupplier cancelled) {
@@ -460,10 +512,10 @@ final class GeneratedChunks implements ColumnTops {
}
private ChunkTop generate(long key) throws Exception {
return generate(key, IntSets.EMPTY_SET);
return generate(key, Int2ByteMaps.EMPTY_MAP);
}
private ChunkTop generateUnchecked(long key, IntSet reachedBeside) {
private ChunkTop generateUnchecked(long key, Int2ByteMap reachedBeside) {
try {
return generate(key, reachedBeside);
} catch (Exception failed) {
@@ -471,8 +523,8 @@ final class GeneratedChunks implements ColumnTops {
}
}
/** @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 {
/** @param reachedBeside the cells along the chunk's sides the open air of the chunks beside it reaches, with its light */
private ChunkTop generate(long key, Int2ByteMap reachedBeside) throws Exception {
int chunkX = (int) (key >> Integer.SIZE);
int chunkZ = (int) key;
int height = engine.getHeight();
@@ -484,12 +536,12 @@ 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();
ChunkCells cells = ChunkCells.of(blocks, terrainY, fluidHeight, this::seenByPlayers, reachedBeside);
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);
runs[level - 1] = ColumnRuns.of(blocks, level, terrainY, this::seenByPlayers, cells);
}
return new ChunkTop(topsOf(blocks, terrainY, this::seenByPlayers), VisibleBlocks.of(blocks, terrainY, fluidHeight, this::seenByPlayers, reachedBeside),
runs, reachedBeside);
return new ChunkTop(topsOf(blocks, terrainY, this::seenByPlayers), VisibleBlocks.of(cells, reachedBeside), runs, reachedBeside);
}
// A plugin's block can be solid on the server where players see grass they walk through.
@@ -509,9 +561,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 reachedBeside the cells along its sides found reached through the chunks beside it, which it was traced from
* @param reachedBeside the cells along its sides found reached through the chunks beside it, which it was traced from,
* with the light there
*/
private record ChunkTop(ColumnTop[] columns, VisibleBlocks visible, ColumnRuns[] runs, IntSet reachedBeside) {
private record ChunkTop(ColumnTop[] columns, VisibleBlocks visible, ColumnRuns[] runs, Int2ByteMap reachedBeside) {
int bytes() {
int raisedBlocks = 0;
for (ColumnTop column : columns) {
@@ -522,7 +575,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() + reachedBeside.size() * Integer.BYTES;
+ visible.shades().length * Short.BYTES + visible.borders().bytes() + reachedBeside.size() * (Integer.BYTES + Byte.BYTES);
}
}
@@ -64,8 +64,11 @@ public record MapTile(int size, double blocksPerPixel, MapLabel.Kind subjectKind
* @param blocks the index in the tile's block names of the block most of each run's top shows
* @param covers the index of the plant most often standing on each run's top, or {@link Blocks#NO_BLOCK}
* @param coverShares how much of each run's top plants stand on, 0 to 255 for all of it
* @param lights the sky's light on each run's top, in sixteenths of a level, 0 to 240
* @param occlusions how many opaque cells stand around each corner of each run's top, in sixty-fourths, 0 to 192
*/
public record Runs(short[] counts, short[] bottoms, short[] tops, short[] blocks, short[] covers, byte[] coverShares) {
public record Runs(short[] counts, short[] bottoms, short[] tops, short[] blocks, short[] covers, byte[] coverShares, byte[] lights,
byte[] occlusions) {
public int count() {
return tops.length;
}
@@ -79,8 +82,12 @@ public record MapTile(int size, double blocksPerPixel, MapLabel.Kind subjectKind
* @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
* @param lights the sky's light each block shows on faces that stand inside it, such as a plant's, 0 to 15
* @param shades the shade of each face {@code faces} names, block after block and side after side: the light of the
* cell it faces in the lowest 4 bits, then for each corner, 2 bits each, how many of the 3 cells around
* it are opaque
*/
public record Voxels(byte[] x, byte[] z, short[] y, short[] blocks, byte[] faces) {
public record Voxels(byte[] x, byte[] z, short[] y, short[] blocks, byte[] faces, byte[] lights, short[] shades) {
public int count() {
return x.length;
}
@@ -133,6 +133,8 @@ public final class MapTileRenderer {
ShortArrayList blocks = new ShortArrayList();
ShortArrayList covers = new ShortArrayList();
ByteArrayList coverShares = new ByteArrayList();
ByteArrayList lights = new ByteArrayList();
ByteArrayList occlusions = new ByteArrayList();
for (int gridZ = 0; gridZ < grid.size(); gridZ++) {
int worldZ = (int) Math.floor(grid.worldZ(gridZ));
for (int gridX = 0; gridX < grid.size(); gridX++) {
@@ -148,10 +150,12 @@ public final class MapTileRenderer {
covers.add(names.indexOf(area.covers()[run]));
}
coverShares.addElements(coverShares.size(), area.coverShares());
lights.addElements(lights.size(), area.lights());
occlusions.addElements(occlusions.size(), area.occlusions());
}
}
return new MapTile.Runs(counts, bottoms.toShortArray(), runTops.toShortArray(), blocks.toShortArray(), covers.toShortArray(),
coverShares.toByteArray());
coverShares.toByteArray(), lights.toByteArray(), occlusions.toByteArray());
}
// Only a tile of a block a pixel shows its blocks one by one; coarser ones show the tops of their columns.
@@ -164,7 +168,7 @@ public final class MapTileRenderer {
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());
return new MapTile.Voxels(found.x(), found.z(), found.y(), blocks, found.faces(), found.lights(), found.shades());
}
private ColumnTops topsOf(BlockSource source) {
@@ -21,36 +21,38 @@ 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.Int2ByteMap;
import it.unimi.dsi.fastutil.ints.Int2ByteMaps;
import it.unimi.dsi.fastutil.ints.IntArrayList;
import it.unimi.dsi.fastutil.ints.IntSet;
import it.unimi.dsi.fastutil.ints.IntSets;
import it.unimi.dsi.fastutil.shorts.ShortArrayList;
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.
* The blocks of a chunk that can be seen from the open air above it, each with the sides that face that air and how
* they are lit. 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
* @param lights the sky's light each block shows on faces that stand inside it, such as a plant's, 0 to 15
* @param shades the shade of each face {@link #faces} names, block after block and side after side, as
* {@link ChunkCells#faceShade} gives it
* @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, ChunkBorders borders) {
record VisibleBlocks(int[] positions, String[] blocks, byte[] faces, byte[] lights, short[] shades, 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;
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() {
@@ -65,16 +67,15 @@ 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 of(chunk, terrainY, fluidHeight, seenByPlayers, IntSets.EMPTY_SET);
return of(ChunkCells.of(chunk, terrainY, fluidHeight, seenByPlayers, Int2ByteMaps.EMPTY_MAP), Int2ByteMaps.EMPTY_MAP);
}
/**
* @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();
static VisibleBlocks of(ChunkCells cells, Int2ByteMap reachedBeside) {
return new Finder(cells, reachedBeside).find();
}
/** A block's place in its chunk, as {@link #positions} holds it. */
@@ -84,8 +85,8 @@ record VisibleBlocks(int[] positions, String[] blocks, byte[] faces, ChunkBorder
@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);
return other instanceof VisibleBlocks visible && Arrays.equals(positions, visible.positions) && Arrays.equals(blocks, visible.blocks)
&& Arrays.equals(faces, visible.faces) && Arrays.equals(lights, visible.lights) && Arrays.equals(shades, visible.shades);
}
@Override
@@ -99,49 +100,18 @@ record VisibleBlocks(int[] positions, String[] blocks, byte[] faces, ChunkBorder
}
private static final class Finder {
private final ChunkCells cells;
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;
private final IntSet reachedBeside;
private final Int2ByteMap reachedBeside;
Finder(Hunk<PlatformBlockState> chunk, IntBinaryOperator terrainY, int fluidHeight, UnaryOperator<PlatformBlockState> seenByPlayers,
IntSet reachedBeside) {
this.chunk = chunk;
this.terrainY = terrainY;
this.fluidHeight = fluidHeight;
Finder(ChunkCells cells, Int2ByteMap reachedBeside) {
this.cells = cells;
this.chunk = cells.chunk();
this.height = cells.height();
this.reachedBeside = reachedBeside;
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);
this.skyReaches = new boolean[ACROSS * height * ACROSS];
}
VisibleBlocks find() {
@@ -149,25 +119,33 @@ record VisibleBlocks(int[] positions, String[] blocks, byte[] faces, ChunkBorder
IntArrayList positions = new IntArrayList();
List<String> blocks = new ArrayList<>();
ByteArrayList faces = new ByteArrayList();
ByteArrayList lights = new ByteArrayList();
ShortArrayList shades = new ShortArrayList();
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) {
if (cells.seenAt(x, y, z) == 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])) {
if (sides == 0 && !(cells.isOpen(x, y, z) && skyReaches[ChunkCells.cellOf(x, y, z)])) {
continue;
}
positions.add(positionOf(x, y, z));
blocks.add(chunk.get(x, y, z).key());
faces.add((byte) sides);
lights.add((byte) cells.blockLightAt(x, y, z));
for (int side = 0; side < SIDES.length; side++) {
if ((sides & 1 << side) != 0) {
shades.add(cells.faceShade(x, y, z, side));
}
}
}
}
}
return new VisibleBlocks(positions.toIntArray(), blocks.toArray(String[]::new), faces.toByteArray(), borders());
return new VisibleBlocks(positions.toIntArray(), blocks.toArray(String[]::new), faces.toByteArray(), lights.toByteArray(),
shades.toShortArray(), borders());
}
private ChunkBorders borders() {
@@ -186,14 +164,14 @@ record VisibleBlocks(int[] positions, String[] blocks, byte[] faces, ChunkBorder
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]) {
reached.add(y, null, fluid[cell]);
} else if (open[cell]) {
sealed.add(y, null, fluid[cell]);
boolean holdsFluid = cells.holdsFluid(x, y, z);
if (skyReaches[ChunkCells.cellOf(x, y, z)]) {
reached.add(y, null, holdsFluid, cells.lightAt(x, y, z));
} else if (cells.isOpen(x, y, z)) {
sealed.add(y, null, holdsFluid, 0);
}
if (seen[cell] != null && !facesOutside(x, y, z, side)) {
hidden.add(y, chunk.get(x, y, z).key(), fluid[cell]);
if (cells.seenAt(x, y, z) != null && !facesOutside(x, y, z, side)) {
hidden.add(y, chunk.get(x, y, z).key(), holdsFluid, 0);
}
}
reached.nextPlace();
@@ -204,15 +182,15 @@ record VisibleBlocks(int[] positions, String[] blocks, byte[] faces, ChunkBorder
}
// Fills every open cell the sky reaches, from the top of the chunk and from beside it where the ground around
// lies lower.
// lies lower or the chunk beside it is known to be open.
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) || reachedBeside.contains(positionOf(x, y, z)))) {
if (cells.isOpen(x, y, z) && (touchesOpenOutside(x, y, z) || reachedBeside.containsKey(positionOf(x, y, z)))) {
int cell = ChunkCells.cellOf(x, y, z);
skyReaches[cell] = true;
queue[queued++] = cell;
}
@@ -228,11 +206,11 @@ record VisibleBlocks(int[] positions, String[] blocks, byte[] faces, ChunkBorder
int nx = x + side[0];
int ny = y + side[1];
int nz = z + side[2];
if (!isInside(nx, ny, nz)) {
if (!cells.isInside(nx, ny, nz)) {
continue;
}
int neighbour = cellOf(nx, ny, nz);
if (open[neighbour] && !skyReaches[neighbour]) {
int neighbour = ChunkCells.cellOf(nx, ny, nz);
if (cells.isOpen(nx, ny, nz) && !skyReaches[neighbour]) {
skyReaches[neighbour] = true;
queue[queued++] = neighbour;
}
@@ -245,7 +223,7 @@ record VisibleBlocks(int[] positions, String[] blocks, byte[] faces, ChunkBorder
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) {
if (!cells.isInside(nx, ny, nz) && cells.outsideAt(nx, ny, nz) != ChunkCells.Outside.SOLID) {
return true;
}
}
@@ -254,16 +232,15 @@ record VisibleBlocks(int[] positions, String[] blocks, byte[] faces, ChunkBorder
// 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)];
boolean isFluid = cells.holdsFluid(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]);
if (cells.isInside(nx, ny, nz)) {
faces = skyReaches[ChunkCells.cellOf(nx, ny, nz)] && !(isFluid && cells.holdsFluid(nx, ny, nz));
} else {
faces = facesOutside(x, y, z, side);
}
@@ -276,33 +253,8 @@ record VisibleBlocks(int[] positions, String[] blocks, byte[] faces, ChunkBorder
// 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)]);
ChunkCells.Outside outside = cells.outsideAt(x + SIDES[side][0], y + SIDES[side][1], z + SIDES[side][2]);
return outside == ChunkCells.Outside.AIR || (outside == ChunkCells.Outside.FLUID && !cells.holdsFluid(x, y, z));
}
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
}
}
@@ -2,7 +2,7 @@ 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.Int2ByteMap;
import it.unimi.dsi.fastutil.ints.IntSet;
import org.junit.Test;
@@ -77,10 +77,10 @@ public class ChunkBordersTest {
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);
Int2ByteMap reached = closed.borders().reachedFrom(shaftBorders, ChunkBorders.WEST);
VisibleBlocks opened = VisibleBlocks.of(ChunkCells.of(overhang, (x, z) -> top, NO_FLUID, UnaryOperator.identity(), reached), reached);
assertEquals(IntSet.of(VisibleBlocks.positionOf(0, 4, 7), VisibleBlocks.positionOf(0, 5, 7)), reached);
assertEquals(IntSet.of(VisibleBlocks.positionOf(0, 4, 7), VisibleBlocks.positionOf(0, 5, 7)), reached.keySet());
assertFalse(shows(closed, VisibleBlocks.positionOf(2, 4, 7)));
assertTrue(shows(opened, VisibleBlocks.positionOf(2, 4, 7)));
assertTrue(shows(opened, VisibleBlocks.positionOf(1, 3, 7)));
@@ -102,7 +102,7 @@ public class ChunkBordersTest {
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<String> faces = new ArrayList<>();
borders.facesAgainst(besideBorders, side, (x, y, z, block, faceSide) -> {
borders.facesAgainst(besideBorders, side, (x, y, z, block, faceSide, light) -> {
assertEquals(side, faceSide);
faces.add(x + "," + y + "," + z + " " + block);
});
@@ -0,0 +1,113 @@
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.Int2ByteMaps;
import org.junit.Test;
import java.util.function.UnaryOperator;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
public class ChunkCellsTest {
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 = 0;
private static final int UP = 1;
private static final PlatformBlockState STONE = block("minecraft:stone", true);
private static final PlatformBlockState LEAVES = block("minecraft:oak_leaves", false);
@Test
public void openGroundIsFullyLitWithNoCornerDarkened() {
short shade = cellsOf(groundUpTo(GROUND_Y)).topShade(7, GROUND_Y, 7);
assertEquals(ChunkCells.FULL_LIGHT, ChunkCells.lightOf(shade));
assertEquals(0, ChunkCells.opaqueAroundCorners(shade));
}
@Test
public void eachLeafAboveDimsTheSkyByALevel() {
Hunk<PlatformBlockState> chunk = groundUpTo(GROUND_Y);
fillLayer(chunk, GROUND_Y + 3, LEAVES);
fillLayer(chunk, GROUND_Y + 4, LEAVES);
assertEquals(ChunkCells.FULL_LIGHT - 2, ChunkCells.lightOf(cellsOf(chunk).topShade(7, GROUND_Y, 7)));
}
@Test
public void groundUnderARoofIsLitFromTheSideLessAStepAtATime() {
Hunk<PlatformBlockState> chunk = groundUpTo(GROUND_Y);
fillLayer(chunk, GROUND_Y + 3, STONE);
for (int z = 0; z < 16; z++) {
chunk.set(15, GROUND_Y + 3, z, null);
}
ChunkCells cells = cellsOf(chunk);
assertEquals(ChunkCells.FULL_LIGHT, cells.lightAt(15, GROUND_Y + 1, 7));
assertEquals(ChunkCells.FULL_LIGHT - 3, cells.lightAt(12, GROUND_Y + 1, 7));
}
@Test
public void groundAtTheFootOfAWallDarkensTheCornersAgainstIt() {
Hunk<PlatformBlockState> chunk = groundUpTo(GROUND_Y);
for (int z = 0; z < 16; z++) {
chunk.set(8, GROUND_Y + 1, z, STONE);
}
short shade = cellsOf(chunk).topShade(7, GROUND_Y, 7);
// The up face's corners run from the north west: two lie against the wall to the east, each beside it and past it.
assertEquals(4, ChunkCells.opaqueAroundCorners(shade));
assertEquals(ChunkCells.FULL_LIGHT, ChunkCells.lightOf(shade));
}
@Test
public void aFaceAgainstOpaqueSidesOnBothHandsHidesTheCornerBetweenThem() {
Hunk<PlatformBlockState> chunk = groundUpTo(GROUND_Y);
chunk.set(8, GROUND_Y + 1, 7, STONE);
chunk.set(7, GROUND_Y + 1, 8, STONE);
short shade = cellsOf(chunk).topShade(7, GROUND_Y, 7);
// The south east corner has both sides opaque, so counts as all three; the corners on one side count one each.
assertEquals(ChunkCells.MOST_OPAQUE_AROUND_CORNER + 2, ChunkCells.opaqueAroundCorners(shade));
}
@Test
public void theUndersideOfBuriedGroundIsDark() {
assertEquals(0, ChunkCells.lightOf(cellsOf(groundUpTo(GROUND_Y)).faceShade(7, 1, 7, DOWN)));
assertEquals(ChunkCells.FULL_LIGHT, ChunkCells.lightOf(cellsOf(groundUpTo(GROUND_Y)).faceShade(7, GROUND_Y, 7, UP)));
}
private static ChunkCells cellsOf(Hunk<PlatformBlockState> chunk) {
return ChunkCells.of(chunk, (x, z) -> GROUND_Y, NO_FLUID, UnaryOperator.identity(), Int2ByteMaps.EMPTY_MAP);
}
private static void fillLayer(Hunk<PlatformBlockState> chunk, int y, PlatformBlockState block) {
for (int z = 0; z < 16; z++) {
for (int x = 0; x < 16; x++) {
chunk.set(x, y, z, block);
}
}
}
private static Hunk<PlatformBlockState> groundUpTo(int topY) {
Hunk<PlatformBlockState> chunk = Hunk.newArrayHunk(16, HEIGHT, 16);
for (int y = 0; y <= topY; y++) {
fillLayer(chunk, y, STONE);
}
return chunk;
}
private static PlatformBlockState block(String key, boolean occluding) {
PlatformBlockState block = mock(PlatformBlockState.class);
when(block.key()).thenReturn(key);
when(block.isOccluding()).thenReturn(occluding);
when(block.isSolid()).thenReturn(true);
return block;
}
}
@@ -2,26 +2,29 @@ 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.Int2ByteMaps;
import org.junit.Test;
import java.util.function.UnaryOperator;
import static org.junit.Assert.assertArrayEquals;
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 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");
private static final int NO_FLUID = -1;
private static final PlatformBlockState GRASS = block("minecraft:grass_block", true);
private static final PlatformBlockState LOG = block("minecraft:oak_log", true);
private static final PlatformBlockState LEAVES = block("minecraft:oak_leaves", false);
private static final PlatformBlockState GRASS_PLANT = plant("minecraft:short_grass");
@Test
public void flatGroundIsOneRunPerArea() {
ColumnRuns runs = ColumnRuns.of(groundUpTo(GROUND_Y), 1, (x, z) -> GROUND_Y, UnaryOperator.identity());
ColumnRuns runs = runsOf(groundUpTo(GROUND_Y));
assertEquals(64, runs.counts().length);
assertEquals(1, runs.counts()[0]);
@@ -42,7 +45,7 @@ public class ColumnRunsTest {
}
}
ColumnRuns runs = ColumnRuns.of(chunk, 1, (x, z) -> GROUND_Y, UnaryOperator.identity());
ColumnRuns runs = runsOf(chunk);
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]});
@@ -59,7 +62,7 @@ public class ColumnRunsTest {
}
}
ColumnRuns runs = ColumnRuns.of(chunk, 1, (x, z) -> GROUND_Y, UnaryOperator.identity());
ColumnRuns runs = runsOf(chunk);
int area = 1 * 8 + 1;
int first = runs.firstRunOf(area);
@@ -73,13 +76,51 @@ public class ColumnRunsTest {
Hunk<PlatformBlockState> chunk = groundUpTo(GROUND_Y);
chunk.set(0, GROUND_Y + 1, 0, GRASS_PLANT);
ColumnRuns runs = ColumnRuns.of(chunk, 1, (x, z) -> GROUND_Y, UnaryOperator.identity());
ColumnRuns runs = runsOf(chunk);
assertEquals("minecraft:short_grass", runs.covers()[0]);
assertEquals(ColumnRuns.WHOLE_SHARE / 4, runs.coverShares()[0] & 0xFF);
assertEquals(null, runs.covers()[1]);
}
@Test
public void openGroundIsLitFullyAndGroundUnderACanopyIsDarker() {
Hunk<PlatformBlockState> chunk = groundUpTo(GROUND_Y);
for (int z = 0; z < 8; z++) {
for (int x = 0; x < 8; x++) {
chunk.set(x, GROUND_Y + 3, z, LEAVES);
chunk.set(x, GROUND_Y + 4, z, LEAVES);
}
}
ColumnRuns runs = runsOf(chunk);
int shadedArea = 1 * 8 + 1;
int openArea = 7 * 8 + 7;
int ground = runs.firstRunOf(shadedArea) + runs.counts()[shadedArea] - 1;
assertEquals(ChunkCells.FULL_LIGHT * ColumnRuns.LIGHT_STEPS, runs.lights()[runs.firstRunOf(openArea)] & 0xFF);
assertEquals(0, runs.occlusions()[runs.firstRunOf(openArea)]);
assertTrue((runs.lights()[ground] & 0xFF) < ChunkCells.FULL_LIGHT * ColumnRuns.LIGHT_STEPS);
}
@Test
public void groundAtTheFootOfAWallHasItsCornersDarkened() {
Hunk<PlatformBlockState> chunk = groundUpTo(GROUND_Y);
for (int z = 0; z < 16; z++) {
chunk.set(4, GROUND_Y + 1, z, LOG);
}
ColumnRuns runs = runsOf(chunk);
int besideWall = 4 * 8 + 1;
assertTrue(runs.occlusions()[runs.firstRunOf(besideWall) + runs.counts()[besideWall] - 1] > 0);
}
private static ColumnRuns runsOf(Hunk<PlatformBlockState> chunk) {
ChunkCells cells = ChunkCells.of(chunk, (x, z) -> GROUND_Y, NO_FLUID, UnaryOperator.identity(), Int2ByteMaps.EMPTY_MAP);
return ColumnRuns.of(chunk, 1, (x, z) -> GROUND_Y, UnaryOperator.identity(), cells);
}
private static Hunk<PlatformBlockState> groundUpTo(int topY) {
Hunk<PlatformBlockState> chunk = Hunk.newArrayHunk(16, HEIGHT, 16);
for (int y = 0; y <= topY; y++) {
@@ -92,10 +133,11 @@ public class ColumnRunsTest {
return chunk;
}
private static PlatformBlockState block(String key) {
private static PlatformBlockState block(String key, boolean occluding) {
PlatformBlockState block = mock(PlatformBlockState.class);
when(block.key()).thenReturn(key);
when(block.isSolid()).thenReturn(true);
when(block.isOccluding()).thenReturn(occluding);
return block;
}