Skip to content

Commit

Permalink
feat: block light is now working
Browse files Browse the repository at this point in the history
  • Loading branch information
smartcmd committed Nov 8, 2024
1 parent e1a2c5a commit 56ae21b
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,6 @@ public interface LightService {
void onChunkUnload(Chunk chunk);

int getQueuedUpdateCount();

int getLightDampening(int x, int y, int z);
}
2 changes: 1 addition & 1 deletion data/resources/block_states.json

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -282,8 +282,9 @@ public void prepareCommandTree(CommandTree tree) {
}, SenderType.PLAYER)
.root()
.key("getlightdata")
.pos("pos")
.exec((context, player) -> {
var floorLoc = player.getLocation().floor(new Vector3f());
var floorLoc = ((Vector3f) context.getResult(1)).floor();
int x = (int) floorLoc.x;
int y = (int) floorLoc.y;
int z = (int) floorLoc.z;
Expand All @@ -292,6 +293,7 @@ public void prepareCommandTree(CommandTree tree) {
player.sendText("BlockLight: " + lightService.getBlockLight(x, y, z));
player.sendText("SkyLight: " + lightService.getSkyLight(x, y, z));
player.sendText("InternalSkyLight: " + lightService.getInternalSkyLight(x, y, z));
player.sendText("LightDampening: " + lightService.getLightDampening(x, y, z));
player.sendText("QueuedUpdateCount: " + lightService.getQueuedUpdateCount());
return context.success();
}, SenderType.PLAYER);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@ public void set(@Range(from = 0, to = 4096) int index, @Range(from = 0, to = 15)
int k = ~(15 << 4 * j);
int l = (value & 15) << 4 * j;
int oldValue = bytes[i] >> 4 * j & 15;
if (value == oldValue) {
return;
}
checkSum = checkSum - oldValue + value;
if (checkSum == 0 || checkSum == CHECK_SUM_MAX) {
bytes = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ public void beforeSetChunk(Dimension dimension) {
unsafeChunk.beforeSetChunk(dimension);
unsafeChunk.setBlockChangeCallback((x, y, z, blockState, layer) -> {
if (layer != 0) return;
dimension.getLightService().onBlockChange(x, y, z, blockState.getBlockStateData().lightEmission(), blockState.getBlockStateData().lightDampening());
dimension.getLightService().onBlockChange(x + (unsafeChunk.x << 4), y, z + (unsafeChunk.z << 4), blockState.getBlockStateData().lightEmission(), blockState.getBlockStateData().lightDampening());
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,7 @@ protected void addChunk(Chunk chunk) {
var chunkLightHeightMap = hasSkyLight ? new HeightMap((short) (dimension.getDimensionInfo().minHeight() - 1)) : null;
AllayUnsafeChunk unsafeChunk = (AllayUnsafeChunk) chunk.toUnsafeChunk();
for (int y = maxHeight; y >= minHeight; y--) {
var sectionY = y >> 4;
var section = unsafeChunk.getSection(sectionY);
var section = unsafeChunk.getSection(y >> 4);
if (section == null) {
y -= 15;
continue;
Expand All @@ -86,12 +85,12 @@ protected void addChunk(Chunk chunk) {
int finalX = x;
int finalY = y;
int finalZ = z;
queue.add(() -> setBlockLightAndPropagate((chunk.getX() << 4) + finalX, finalY, (chunk.getZ() << 4) + finalZ, lightEmission));
queue.add(() -> setBlockLightAndPropagate((chunk.getX() << 4) + finalX, finalY, (chunk.getZ() << 4) + finalZ, 0, lightEmission));
}
var lightDampening = blockStateData.lightDampening();
if (lightDampening == 0) continue;

chunkLightDampening[sectionY].set(x, y & 0xf, z, lightDampening);
chunkLightDampening[(y - minHeight) >> 4].set(x, y & 0xf, z, lightDampening);
if (chunkLightHeightMap != null && chunkLightHeightMap.get(x, z) == minHeight - 1) {
chunkLightHeightMap.set(x, z, (short) y);
}
Expand Down Expand Up @@ -144,23 +143,23 @@ public int getInternalSkyLight(int x, int y, int z) {
@Override
public void onBlockChange(int x, int y, int z, int lightEmission, int lightDampening) {
queue.add(() -> {
setLightDampening(x, y, z, lightDampening);
if (getBlockLight(x, y, z) != lightEmission || getLightDampening(x, y, z) != lightDampening) {
setBlockLightAndPropagate(x, y, z, lightEmission);
var oldBlockDampening = getLightDampening(x, y, z);
if (oldBlockDampening != lightDampening) {
setLightDampening(x, y, z, lightDampening);
}

var oldBlockLight = getBlockLight(x, y, z);
setBlockLightAndPropagate(x, y, z, oldBlockLight, lightEmission);
});
}

protected void setBlockLightAndPropagate(int x, int y, int z, int lightValue) {
var oldLightValue = getBlockLight(x, y, z);
// this is very important: we need to set the light value
// for positions we add into the queue. the propagator WILL NOT
// do it for us! Remember, it only sets NEIGHBOR light values
setBlockLight(x, y, z, lightValue);
lightIncreaseQueue.add(new LightUpdateEntry(x, y, z, lightValue));
if (lightValue > oldLightValue) {
protected void setBlockLightAndPropagate(int x, int y, int z, int oldLightValue, int newLightValue) {
setBlockLight(x, y, z, newLightValue);
if (newLightValue > oldLightValue) {
lightIncreaseQueue.add(new LightUpdateEntry(x, y, z, newLightValue));
propagateIncrease();
} else if (lightValue < oldLightValue) {
} else {
lightDecreaseQueue.add(new LightUpdateEntry(x, y, z, oldLightValue));
propagateDecrease();
}
}
Expand Down Expand Up @@ -213,7 +212,7 @@ protected void propagateDecrease() {
int currentNeighborLightValue = getBlockLight(ox, oy, oz);
if (currentNeighborLightValue != 0 && currentNeighborLightValue < lightValue) {
setBlockLight(ox, oy, oz, 0);
lightDecreaseQueue.add(new LightUpdateEntry(ox, oy, oz, 0));
lightDecreaseQueue.add(new LightUpdateEntry(ox, oy, oz, currentNeighborLightValue));
} else if (currentNeighborLightValue >= lightValue) {
lightIncreaseQueue.add(new LightUpdateEntry(ox, oy, oz, currentNeighborLightValue));
}
Expand All @@ -234,8 +233,10 @@ public void onChunkUnload(Chunk chunk) {
lightDampening.remove(hash);
blockLight.remove(hash);
if (hasSkyLight) {
lightHeightMap.remove(hash);
skyLight.remove(hash);
}
chunks.remove(hash);
});
}

Expand All @@ -244,7 +245,8 @@ public int getQueuedUpdateCount() {
return queue.size();
}

protected int getLightDampening(int x, int y, int z) {
@Override
public int getLightDampening(int x, int y, int z) {
return get(lightDampening, x, y, z, 0);
}

Expand All @@ -270,13 +272,12 @@ protected void set(Long2ObjectMap<ChunkSectionNibbleArray[]> target, int x, int
if (!chunks.contains(hash)) {
return;
}
var array = target.get(HashUtils.hashXZ(x >> 4, z >> 4));
var array = target.get(hash);
array[(y - minHeight) >> 4].set(x & 15, y & 15, z & 15, value);
}

protected boolean isPosLoaded(int x, int y, int z) {
return chunks.contains(HashUtils.hashXZ(x >> 4, z >> 4)) &&
y >= minHeight && y <= maxHeight;
return y >= minHeight && y <= maxHeight && chunks.contains(HashUtils.hashXZ(x >> 4, z >> 4));
}

public static int calculateSkylightReduction(long time, Set<Weather> weathers) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ void testSetAndGet() {
var array = new ChunkSectionNibbleArray(new byte[2048]);
array.set(1, 2, 3, 15);
assertEquals(15, array.get(1, 2, 3));

array = new ChunkSectionNibbleArray();
array.set(1, 2, 7, 15);
assertEquals(15, array.get(1, 2, 7));
}

@Test
Expand Down

0 comments on commit 56ae21b

Please sign in to comment.