improve light calculation

This commit is contained in:
Bixilon 2022-06-30 17:48:03 +02:00
parent a3ca8e37ee
commit ee459bee47
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4

View File

@ -50,44 +50,13 @@ class SectionLight(
*/ */
private fun startDecreaseTrace(x: Int, y: Int, z: Int, luminance: Byte) { private fun startDecreaseTrace(x: Int, y: Int, z: Int, luminance: Byte) {
val index = getIndex(x, y, z) traceDecrease(x, y, z, luminance, 16) // invalid light
val current = light[index] // and 0x0F
if (current == luminance) {
// everything is fine
return
}
light[index] = luminance
val expectedNeighbourLevel = if (luminance <= 1) 0 else (luminance - 1).toByte()
var neighbour = luminance
if (x > 0) {
neighbour = maxOf(neighbour, (traceDecrease(x - 1, y, z, expectedNeighbourLevel, current) - 1).toByte())
}
if (x < ProtocolDefinition.SECTION_MAX_X) {
neighbour = maxOf(neighbour, (traceDecrease(x + 1, y, z, expectedNeighbourLevel, current) - 1).toByte())
}
if (y > 0) {
neighbour = maxOf(neighbour, (traceDecrease(x, y - 1, z, expectedNeighbourLevel, current) - 1).toByte())
}
if (y < ProtocolDefinition.SECTION_MAX_Y) {
neighbour = maxOf(neighbour, (traceDecrease(x, y + 1, z, expectedNeighbourLevel, current) - 1).toByte())
}
if (z > 0) {
neighbour = maxOf(neighbour, (traceDecrease(x, y, z - 1, expectedNeighbourLevel, current) - 1).toByte())
}
if (z < ProtocolDefinition.SECTION_MAX_Z) {
neighbour = maxOf(neighbour, (traceDecrease(x, y, z + 1, expectedNeighbourLevel, current) - 1).toByte())
}
light[index] = neighbour
} }
fun traceDecrease(x: Int, y: Int, z: Int, expectedLuminance: Byte, previous: Byte): Byte { fun traceDecrease(x: Int, y: Int, z: Int, expectedLuminance: Byte, previous: Byte): Byte {
val index = getIndex(x, y, z) val index = getIndex(x, y, z)
val light = light[index] val light = light[index]
if (light <= expectedLuminance) { if (light == expectedLuminance) {
// smaller should never happen
return expectedLuminance return expectedLuminance
} }
if (light >= previous) { if (light >= previous) {