From 2fb7c610076f1946f8661ef1931e22108465825d Mon Sep 17 00:00:00 2001 From: Bixilon Date: Wed, 31 May 2023 18:27:21 +0200 Subject: [PATCH] optimize Section flood filling the position is now not wrapped inside a Number object by the compiler anymore --- .../world/container/block/SectionOcclusion.kt | 38 +++++++++---------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/src/main/java/de/bixilon/minosoft/data/world/container/block/SectionOcclusion.kt b/src/main/java/de/bixilon/minosoft/data/world/container/block/SectionOcclusion.kt index 5d48e243f..dd7259571 100644 --- a/src/main/java/de/bixilon/minosoft/data/world/container/block/SectionOcclusion.kt +++ b/src/main/java/de/bixilon/minosoft/data/world/container/block/SectionOcclusion.kt @@ -46,34 +46,34 @@ class SectionOcclusion( update(calculateOcclusion(floodFill()), notify) } + private fun trace(regions: ShortArray, x: Int, y: Int, z: Int, nextId: Short) { + val index = y shl 8 or (z shl 4) or x + if (regions[index] > 0) { + return + } + val state = provider[index] + if (state.isFullyOpaque()) { + return + } + regions[index] = nextId + if (x > 0) trace(regions, x - 1, y, z, nextId) + if (x < ProtocolDefinition.SECTION_MAX_X) trace(regions, x + 1, y, z, nextId) + if (y > 0) trace(regions, x, y - 1, z, nextId) + if (y < ProtocolDefinition.SECTION_MAX_Y) trace(regions, x, y + 1, z, nextId) + if (z > 0) trace(regions, x, y, z - 1, nextId) + if (z < ProtocolDefinition.SECTION_MAX_Z) trace(regions, x, y, z + 1, nextId) + } private fun floodFill(): ShortArray { // mark regions and check direct neighbours val regions = ShortArray(ProtocolDefinition.BLOCKS_PER_SECTION) - fun trace(x: Int, y: Int, z: Int, nextId: Short) { - val index = y shl 8 or (z shl 4) or x - if (regions[index] > 0) { - return - } - val state = provider[index] - if (state.isFullyOpaque()) { - return - } - regions[index] = nextId - if (x > 0) trace(x - 1, y, z, nextId) - if (x < ProtocolDefinition.SECTION_MAX_X) trace(x + 1, y, z, nextId) - if (y > 0) trace(x, y - 1, z, nextId) - if (y < ProtocolDefinition.SECTION_MAX_Y) trace(x, y + 1, z, nextId) - if (z > 0) trace(x, y, z - 1, nextId) - if (z < ProtocolDefinition.SECTION_MAX_Z) trace(x, y, z + 1, nextId) - } - var next: Short = 1 + var next: Short = 0 for (y in 0 until ProtocolDefinition.SECTION_HEIGHT_Y) { for (z in 0 until ProtocolDefinition.SECTION_WIDTH_Z) { for (x in 0 until ProtocolDefinition.SECTION_WIDTH_X) { - trace(x, y, z, next++) + trace(regions, x, y, z, ++next) } } }