light: properly recalculate solid block changes

This commit is contained in:
Bixilon 2022-08-28 22:52:18 +02:00
parent ac054ed581
commit f5c6fe8304
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4

View File

@ -44,42 +44,45 @@ class SectionLight(
// that is kind of hacky, but far easier and kind of faster // that is kind of hacky, but far easier and kind of faster
val light = this.light[getIndex(x, y, z)].toInt() and 0x0F val light = this.light[getIndex(x, y, z)].toInt() and 0x0F
decreaseLight(x, y, z, light) decreaseLight(x, y, z, light, true) // just clear the light
decreaseLight(x, y, z, light, false) //
} }
private fun decreaseLight(x: Int, y: Int, z: Int, light: Int) { private fun decreaseLight(x: Int, y: Int, z: Int, light: Int, reset: Boolean) {
decreaseCheckLevel(x, z, light) decreaseCheckLevel(x, z, light, reset)
val neighbours = section.neighbours ?: return val neighbours = section.neighbours ?: return
if (y - light < 0) { if (y - light < 0) {
neighbours[Directions.O_DOWN]?.light?.decreaseCheckLevel(x, z, light - y) neighbours[Directions.O_DOWN]?.light?.decreaseCheckLevel(x, z, light - y, reset)
} }
if (y + light > ProtocolDefinition.SECTION_MAX_Y) { if (y + light > ProtocolDefinition.SECTION_MAX_Y) {
neighbours[Directions.O_UP]?.light?.decreaseCheckLevel(x, z, light - (ProtocolDefinition.SECTION_MAX_Y - y)) neighbours[Directions.O_UP]?.light?.decreaseCheckLevel(x, z, light - (ProtocolDefinition.SECTION_MAX_Y - y), reset)
} }
} }
private fun decreaseCheckLevel(x: Int, z: Int, light: Int) { private fun decreaseCheckLevel(x: Int, z: Int, light: Int, reset: Boolean) {
decreaseCheckX(z, light) decreaseCheckX(z, light, reset)
val neighbours = section.neighbours ?: return val neighbours = section.neighbours ?: return
if (x - light < 0) { if (x - light < 0) {
neighbours[Directions.O_WEST]?.light?.decreaseCheckX(z, light - x) neighbours[Directions.O_WEST]?.light?.decreaseCheckX(z, light - x, reset)
} }
if (x + light > ProtocolDefinition.SECTION_MAX_X) { if (x + light > ProtocolDefinition.SECTION_MAX_X) {
neighbours[Directions.O_EAST]?.light?.decreaseCheckX(z, light - (ProtocolDefinition.SECTION_MAX_X - x)) neighbours[Directions.O_EAST]?.light?.decreaseCheckX(z, light - (ProtocolDefinition.SECTION_MAX_X - x), reset)
} }
} }
private fun decreaseCheckX(z: Int, light: Int) { private fun decreaseCheckX(z: Int, light: Int, reset: Boolean) {
val neighbours = section.neighbours ?: return val neighbours = section.neighbours ?: return
recalculate() if (reset) resetLight() else calculate()
if (z - light < 0) { if (z - light < 0) {
neighbours[Directions.O_NORTH]?.light?.recalculate() val neighbour = neighbours[Directions.O_NORTH]?.light
if (reset) neighbour?.resetLight() else neighbour?.calculate()
} }
if (z + light > ProtocolDefinition.SECTION_MAX_Z) { if (z + light > ProtocolDefinition.SECTION_MAX_Z) {
neighbours[Directions.O_SOUTH]?.light?.recalculate() val neighbour = neighbours[Directions.O_SOUTH]?.light
if (reset) neighbour?.resetLight() else neighbour?.calculate()
} }
} }