fix skylight tracing through neighbours

This commit is contained in:
Bixilon 2022-09-25 17:00:14 +02:00
parent 0901ac07c7
commit 68af0e1f0f
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
2 changed files with 34 additions and 38 deletions

View File

@ -521,14 +521,13 @@ class Chunk(
} }
private inline fun traceSkylightDown(x: Int, z: Int) { private inline fun traceSkylightDown(x: Int, z: Int) {
val heightmapIndex = (z shl 4) or x traceSkylightDown((z shl 4) or x)
traceSkylightDown(heightmapIndex)
} }
private fun traceSkylightDown(heightmapIndex: Int): Int { private fun traceSkylightDown(heightmapIndex: Int): Int {
val maxHeight = skylightHeightmap[heightmapIndex] val maxHeight = skylightHeightmap[heightmapIndex]
// ToDo: only update changed ones // ToDo: only mark changed ones as updated
for (sectionHeight in highestSection - 1 downTo maxHeight.sectionHeight + 1) { for (sectionHeight in highestSection - 1 downTo maxHeight.sectionHeight + 1) {
val section = sections?.get(sectionHeight - lowestSection) ?: continue val section = sections?.get(sectionHeight - lowestSection) ?: continue
section.light.update = true section.light.update = true
@ -556,30 +555,27 @@ class Chunk(
val maxHeight = skylightHeightmap[heightmapIndex] val maxHeight = skylightHeightmap[heightmapIndex]
var skylightStart: Int
// ToDo: Don't traceSkylightDown 5x // ToDo: Don't traceSkylightDown 5x
skylightStart = if (x > 0) { val skylightStart: Int = maxOf(
traceSkylightDown(heightmapIndex - 1) if (x > 0) {
} else { traceSkylightDown(heightmapIndex - 1)
neighbours[ChunkNeighbours.EAST].traceSkylightDown((z shl 4) or ProtocolDefinition.SECTION_MAX_X) } else {
} neighbours[ChunkNeighbours.EAST].traceSkylightDown((z shl 4) or ProtocolDefinition.SECTION_MAX_X)
skylightStart = maxOf( },
skylightStart, if (x < ProtocolDefinition.SECTION_MAX_X) {
if (x < ProtocolDefinition.SECTION_MAX_X) {
traceSkylightDown(heightmapIndex + 1) traceSkylightDown(heightmapIndex + 1)
} else { } else {
neighbours[ChunkNeighbours.WEST].traceSkylightDown(((z shl 4) or 0)) neighbours[ChunkNeighbours.WEST].traceSkylightDown(((z shl 4) or 0))
} },
)
skylightStart = maxOf( if (z > 0) {
skylightStart, if (z > 0) {
traceSkylightDown(((z - 1) shl 4) or x) traceSkylightDown(((z - 1) shl 4) or x)
} else { } else {
neighbours[ChunkNeighbours.SOUTH].skylightHeightmap[(ProtocolDefinition.SECTION_MAX_Z shl 4) or x] neighbours[ChunkNeighbours.SOUTH].skylightHeightmap[(ProtocolDefinition.SECTION_MAX_Z shl 4) or x]
} },
)
skylightStart = maxOf( if (z < ProtocolDefinition.SECTION_MAX_Z) {
skylightStart, if (z < ProtocolDefinition.SECTION_MAX_Z) {
traceSkylightDown(((z + 1) shl 4) or x) traceSkylightDown(((z + 1) shl 4) or x)
} else { } else {
neighbours[ChunkNeighbours.NORTH].traceSkylightDown((0 shl 4) or x) neighbours[ChunkNeighbours.NORTH].traceSkylightDown((0 shl 4) or x)

View File

@ -273,36 +273,36 @@ class SectionLight(
if (y < ProtocolDefinition.SECTION_MAX_Y) { if (y < ProtocolDefinition.SECTION_MAX_Y) {
traceSkylight(x, y + 1, z, nextNeighbourLevel, Directions.UP, totalY + 1, false) traceSkylight(x, y + 1, z, nextNeighbourLevel, Directions.UP, totalY + 1, false)
} else { } else {
// ToDo: Bottom light // ToDo: Trace through bottom light
neighbours[Directions.O_DOWN]?.light?.traceSkylight(x, ProtocolDefinition.SECTION_MAX_Y, z, nextLevel, direction, totalY, false) neighbours[Directions.O_DOWN]?.light?.traceSkylight(x, ProtocolDefinition.SECTION_MAX_Y, z, nextLevel, direction, totalY, false)
} }
} }
if (direction != Directions.SOUTH && (direction == null || light.propagatesLight(direction, Directions.NORTH))) {
if (z > 0) {
traceSkylight(x, y, z - 1, nextNeighbourLevel, Directions.NORTH, totalY, false)
} else {
neighbours[Directions.O_SOUTH]?.light?.traceSkylight(x, y, ProtocolDefinition.SECTION_MAX_Z, nextNeighbourLevel, Directions.NORTH, totalY, false)
}
}
if (direction != Directions.NORTH && (direction == null || light.propagatesLight(direction, Directions.SOUTH))) { if (direction != Directions.NORTH && (direction == null || light.propagatesLight(direction, Directions.SOUTH))) {
if (z < ProtocolDefinition.SECTION_MAX_Z) { if (z > 0) {
traceSkylight(x, y, z + 1, nextNeighbourLevel, Directions.SOUTH, totalY, false) traceSkylight(x, y, z - 1, nextNeighbourLevel, Directions.SOUTH, totalY, false)
} else { } else {
neighbours[Directions.O_NORTH]?.light?.traceSkylight(x, y, 0, nextNeighbourLevel, Directions.SOUTH, totalY, false) neighbours[Directions.O_NORTH]?.light?.traceSkylight(x, y, ProtocolDefinition.SECTION_MAX_Z, nextNeighbourLevel, Directions.SOUTH, totalY, false)
} }
} }
if (direction != Directions.EAST && (direction == null || light.propagatesLight(direction, Directions.WEST))) { if (direction != Directions.SOUTH && (direction == null || light.propagatesLight(direction, Directions.NORTH))) {
if (x > 0) { if (z < ProtocolDefinition.SECTION_MAX_Z) {
traceSkylight(x - 1, y, z, nextNeighbourLevel, Directions.WEST, totalY, false) traceSkylight(x, y, z + 1, nextNeighbourLevel, Directions.NORTH, totalY, false)
} else { } else {
neighbours[Directions.O_EAST]?.light?.traceSkylight(ProtocolDefinition.SECTION_MAX_X, y, z, nextLevel, direction, totalY, false) neighbours[Directions.O_SOUTH]?.light?.traceSkylight(x, y, 0, nextNeighbourLevel, Directions.NORTH, totalY, false)
} }
} }
if (direction != Directions.WEST && (direction == null || light.propagatesLight(direction, Directions.EAST))) { if (direction != Directions.WEST && (direction == null || light.propagatesLight(direction, Directions.EAST))) {
if (x < ProtocolDefinition.SECTION_MAX_X) { if (x > 0) {
traceSkylight(x + 1, y, z, nextNeighbourLevel, Directions.EAST, totalY, false) traceSkylight(x - 1, y, z, nextNeighbourLevel, Directions.EAST, totalY, false)
} else { } else {
neighbours[Directions.O_WEST]?.light?.traceSkylight(0, y, z, nextLevel, direction, totalY, false) neighbours[Directions.O_WEST]?.light?.traceSkylight(ProtocolDefinition.SECTION_MAX_X, y, z, nextLevel, direction, totalY, false)
}
}
if (direction != Directions.EAST && (direction == null || light.propagatesLight(direction, Directions.WEST))) {
if (x < ProtocolDefinition.SECTION_MAX_X) {
traceSkylight(x + 1, y, z, nextNeighbourLevel, Directions.WEST, totalY, false)
} else {
neighbours[Directions.O_EAST]?.light?.traceSkylight(0, y, z, nextLevel, direction, totalY, false)
} }
} }
} }