From 17e164ddaa03929cad910d6b055712982bb7d54f Mon Sep 17 00:00:00 2001 From: Bixilon Date: Tue, 22 Nov 2022 16:10:15 +0100 Subject: [PATCH] rename Chunk::highestSection, fix benchmark --- .../light/breaking/block/BlockLightBreakIT.kt | 4 ++-- .../light/place/block/BlockLightPlaceIT.kt | 4 ++-- .../minosoft/data/world/chunk/Chunk.kt | 20 ++++++++--------- .../world/chunk/light/BorderSectionLight.kt | 4 ++-- .../data/world/chunk/light/ChunkLight.kt | 22 +++++++++---------- .../data/world/chunk/light/SectionLight.kt | 12 +++++----- .../world/chunk/neighbours/ChunkNeighbours.kt | 2 +- .../gui/rendering/world/WorldRenderer.kt | 2 +- .../preparer/cull/SolidCullSectionPreparer.kt | 4 ++-- .../data/world/chunk/ChunkTestingUtil.kt | 5 +++-- 10 files changed, 40 insertions(+), 39 deletions(-) diff --git a/src/integration-test/kotlin/de/bixilon/minosoft/data/world/chunk/light/breaking/block/BlockLightBreakIT.kt b/src/integration-test/kotlin/de/bixilon/minosoft/data/world/chunk/light/breaking/block/BlockLightBreakIT.kt index 727c8d5e7..feece24f7 100644 --- a/src/integration-test/kotlin/de/bixilon/minosoft/data/world/chunk/light/breaking/block/BlockLightBreakIT.kt +++ b/src/integration-test/kotlin/de/bixilon/minosoft/data/world/chunk/light/breaking/block/BlockLightBreakIT.kt @@ -207,7 +207,7 @@ class BlockLightBreakIT { } fun bottomPropagation() { - val world = ConnectionTestUtil.createConnection(5).world + val world = ConnectionTestUtil.createConnection(3).world world.fill(Vec3i(-10, 0, -10), Vec3i(30, 1, 30), StoneTestO.state) world[Vec3i(8, 0, 8)] = TorchTest0.state world[Vec3i(8, 0, 8)] = null @@ -223,7 +223,7 @@ class BlockLightBreakIT { } fun topPropagation() { - val world = ConnectionTestUtil.createConnection(5).world + val world = ConnectionTestUtil.createConnection(3).world world.fill(Vec3i(-10, 254, -10), Vec3i(30, 255, 30), StoneTestO.state) world[Vec3i(8, 255, 8)] = TorchTest0.state world[Vec3i(8, 255, 8)] = null diff --git a/src/integration-test/kotlin/de/bixilon/minosoft/data/world/chunk/light/place/block/BlockLightPlaceIT.kt b/src/integration-test/kotlin/de/bixilon/minosoft/data/world/chunk/light/place/block/BlockLightPlaceIT.kt index 1f27df0b7..2e3280d9b 100644 --- a/src/integration-test/kotlin/de/bixilon/minosoft/data/world/chunk/light/place/block/BlockLightPlaceIT.kt +++ b/src/integration-test/kotlin/de/bixilon/minosoft/data/world/chunk/light/place/block/BlockLightPlaceIT.kt @@ -185,7 +185,7 @@ class BlockLightPlaceIT { } fun bottomPropagation() { - val world = ConnectionTestUtil.createConnection(5).world + val world = ConnectionTestUtil.createConnection(3).world world.fill(Vec3i(-10, 0, -10), Vec3i(30, 1, 30), StoneTestO.state) world[Vec3i(8, 0, 8)] = TorchTest0.state @@ -200,7 +200,7 @@ class BlockLightPlaceIT { } fun topPropagation() { - val world = ConnectionTestUtil.createConnection(4).world + val world = ConnectionTestUtil.createConnection(3).world world.fill(Vec3i(-10, 254, -10), Vec3i(30, 255, 30), StoneTestO.state) world[Vec3i(8, 255, 8)] = TorchTest0.state diff --git a/src/main/java/de/bixilon/minosoft/data/world/chunk/Chunk.kt b/src/main/java/de/bixilon/minosoft/data/world/chunk/Chunk.kt index 51875ab38..98b661efc 100644 --- a/src/main/java/de/bixilon/minosoft/data/world/chunk/Chunk.kt +++ b/src/main/java/de/bixilon/minosoft/data/world/chunk/Chunk.kt @@ -51,8 +51,8 @@ class Chunk( val lock = ThreadLock() val world = connection.world val light = ChunkLight(this) - val lowestSection = world.dimension!!.minSection - val highestSection = world.dimension!!.maxSection + val minSection = world.dimension!!.minSection + val maxSection = world.dimension!!.maxSection val cacheBiomes = world.cacheBiomeAccessor != null var blocksInitialized = false // All block data was received @@ -66,7 +66,7 @@ class Chunk( val isFullyLoaded: Boolean get() = isLoaded && neighbours.complete - operator fun get(sectionHeight: SectionHeight): ChunkSection? = sections?.getOrNull(sectionHeight - lowestSection) + operator fun get(sectionHeight: SectionHeight): ChunkSection? = sections?.getOrNull(sectionHeight - minSection) fun unsafeGet(x: Int, y: Int, z: Int): BlockState? { return this[y.sectionHeight]?.blocks?.unsafeGet(x, y.inSectionHeight, z) @@ -147,7 +147,7 @@ class Chunk( data.blocks?.let { for ((index, blocks) in it.withIndex()) { blocks ?: continue - val section = getOrPut(index + lowestSection) ?: return@let + val section = getOrPut(index + minSection) ?: return@let section.blocks = blocks } light.recalculateHeightmap() @@ -162,7 +162,7 @@ class Chunk( data.light?.let { for ((index, light) in it.withIndex()) { light ?: continue - val section = getOrPut(index + lowestSection) ?: return@let + val section = getOrPut(index + minSection) ?: return@let section.light.light = light } } @@ -187,7 +187,7 @@ class Chunk( lock.unlock() return null } - val sectionIndex = sectionHeight - lowestSection + val sectionIndex = sectionHeight - minSection if (sectionIndex < 0 || sectionIndex >= sections.size) { lock.unlock() return null @@ -214,7 +214,7 @@ class Chunk( if (sectionIndex > 0) { sections[sectionIndex - 1]?.neighbours?.set(Directions.O_UP, section) } - val highestIndex = highestSection - 1 + val highestIndex = maxSection - 1 if (sectionIndex < highestIndex) { sections[sectionIndex + 1]?.neighbours?.set(Directions.O_DOWN, section) } @@ -235,7 +235,7 @@ class Chunk( val sections = sections!! for ((index, section) in sections.withIndex()) { section ?: continue - section.tick(connection, chunkPosition, index + lowestSection) + section.tick(connection, chunkPosition, index + minSection) } } @@ -250,7 +250,7 @@ class Chunk( val neighbours: Array = connection.world.getChunkNeighbours(chunkPosition).unsafeCast() for ((sectionIndex, section) in sections!!.withIndex()) { section ?: continue - val sectionHeight = sectionIndex + lowestSection + val sectionHeight = sectionIndex + minSection section.buildBiomeCache(chunkPosition, sectionHeight, this, neighbours, cacheBiomeAccessor) } biomesInitialized = true @@ -270,7 +270,7 @@ class Chunk( private fun updateNeighbours(neighbours: Array, sectionHeight: Int) { for (nextSectionHeight in sectionHeight - 1..sectionHeight + 1) { - if (nextSectionHeight < lowestSection || nextSectionHeight > highestSection) { + if (nextSectionHeight < minSection || nextSectionHeight > maxSection) { continue } diff --git a/src/main/java/de/bixilon/minosoft/data/world/chunk/light/BorderSectionLight.kt b/src/main/java/de/bixilon/minosoft/data/world/chunk/light/BorderSectionLight.kt index b999597b2..72749ac48 100644 --- a/src/main/java/de/bixilon/minosoft/data/world/chunk/light/BorderSectionLight.kt +++ b/src/main/java/de/bixilon/minosoft/data/world/chunk/light/BorderSectionLight.kt @@ -128,9 +128,9 @@ class BorderSectionLight( val neighbourLevel = nextLevel - 1 if (top) { - chunk.sections?.getLast()?.light?.traceSkylightIncrease(x, ProtocolDefinition.SECTION_MAX_Y, z, neighbourLevel, Directions.DOWN, chunk.highestSection * ProtocolDefinition.SECTION_HEIGHT_Y + ProtocolDefinition.SECTION_MAX_Y) + chunk.sections?.getLast()?.light?.traceSkylightIncrease(x, ProtocolDefinition.SECTION_MAX_Y, z, neighbourLevel, Directions.DOWN, chunk.maxSection * ProtocolDefinition.SECTION_HEIGHT_Y + ProtocolDefinition.SECTION_MAX_Y) } else { - chunk.sections?.getFirst()?.light?.traceSkylightIncrease(x, 0, z, neighbourLevel, Directions.UP, chunk.lowestSection * ProtocolDefinition.SECTION_HEIGHT_Y) + chunk.sections?.getFirst()?.light?.traceSkylightIncrease(x, 0, z, neighbourLevel, Directions.UP, chunk.minSection * ProtocolDefinition.SECTION_HEIGHT_Y) } if (z > 0) { diff --git a/src/main/java/de/bixilon/minosoft/data/world/chunk/light/ChunkLight.kt b/src/main/java/de/bixilon/minosoft/data/world/chunk/light/ChunkLight.kt index a1b0dc94c..3a8303330 100644 --- a/src/main/java/de/bixilon/minosoft/data/world/chunk/light/ChunkLight.kt +++ b/src/main/java/de/bixilon/minosoft/data/world/chunk/light/ChunkLight.kt @@ -93,7 +93,7 @@ class ChunkLight(private val chunk: Chunk) { private fun fireLightChange(sections: Array, fireSameChunkEvent: Boolean) { val neighbours = chunk.neighbours.get() ?: return for ((index, section) in sections.withIndex()) { - fireLightChange(section ?: continue, index + chunk.lowestSection, neighbours, fireSameChunkEvent) + fireLightChange(section ?: continue, index + chunk.minSection, neighbours, fireSameChunkEvent) } } @@ -107,10 +107,10 @@ class ChunkLight(private val chunk: Chunk) { val inSectionHeight = y.inSectionHeight val heightmapIndex = (z shl 4) or x val index = inSectionHeight shl 8 or heightmapIndex - if (sectionHeight == chunk.lowestSection - 1) { + if (sectionHeight == chunk.minSection - 1) { return bottom[index].toInt() } - if (sectionHeight == chunk.highestSection + 1) { + if (sectionHeight == chunk.maxSection + 1) { return top[index].toInt() or SectionLight.SKY_LIGHT_MASK // top has always sky=15 } var light = chunk[sectionHeight]?.light?.get(index)?.toInt() ?: 0x00 @@ -181,7 +181,7 @@ class ChunkLight(private val chunk: Chunk) { return } chunk.lock.lock() - val maxY = chunk.highestSection * ProtocolDefinition.SECTION_HEIGHT_Y + val maxY = chunk.maxSection * ProtocolDefinition.SECTION_HEIGHT_Y for (x in 0 until ProtocolDefinition.SECTION_WIDTH_X) { for (z in 0 until ProtocolDefinition.SECTION_WIDTH_Z) { @@ -197,7 +197,7 @@ class ChunkLight(private val chunk: Chunk) { var y = Int.MIN_VALUE - sectionLoop@ for (sectionIndex in (startY.sectionHeight - chunk.lowestSection) downTo 0) { + sectionLoop@ for (sectionIndex in (startY.sectionHeight - chunk.minSection) downTo 0) { if (sectionIndex >= sections.size) { // starting from above world continue @@ -212,7 +212,7 @@ class ChunkLight(private val chunk: Chunk) { // can go through block continue } - y = (sectionIndex + chunk.lowestSection) * ProtocolDefinition.SECTION_HEIGHT_Y + sectionY + y = (sectionIndex + chunk.minSection) * ProtocolDefinition.SECTION_HEIGHT_Y + sectionY if (!light.skylightEnters) { y++ } @@ -234,8 +234,8 @@ class ChunkLight(private val chunk: Chunk) { // block is now higher // ToDo: Neighbours val sections = chunk.sections ?: Broken("Sections == null") - val maxIndex = previous.sectionHeight - chunk.lowestSection - val minIndex = now.sectionHeight - chunk.lowestSection + val maxIndex = previous.sectionHeight - chunk.minSection + val minIndex = now.sectionHeight - chunk.minSection for (index in maxIndex downTo minIndex) { val section = sections[index] ?: continue section.light.reset() @@ -365,8 +365,8 @@ class ChunkLight(private val chunk: Chunk) { chunk.getOrPut(skylightStartSectionHeight - 1) } - for (sectionHeight in minOf(skylightStartSectionHeight, chunk.highestSection) downTo maxOf(maxHeightSection + 1, chunk.lowestSection)) { - val section = chunk.sections?.get(sectionHeight - chunk.lowestSection) ?: continue + for (sectionHeight in minOf(skylightStartSectionHeight, chunk.maxSection) downTo maxOf(maxHeightSection + 1, chunk.minSection)) { + val section = chunk.sections?.get(sectionHeight - chunk.minSection) ?: continue // ToDo: Only update if affected by heightmap change section.light.update = true @@ -376,7 +376,7 @@ class ChunkLight(private val chunk: Chunk) { section.light.traceSkylightIncrease(x, y, z, ProtocolDefinition.MAX_LIGHT_LEVEL_I, null, baseY + y, false) } } - if (maxHeight.sectionHeight < chunk.lowestSection) { + if (maxHeight.sectionHeight < chunk.minSection) { // bottom section bottom.traceSkyIncrease(x, z, ProtocolDefinition.MAX_LIGHT_LEVEL_I) } else { diff --git a/src/main/java/de/bixilon/minosoft/data/world/chunk/light/SectionLight.kt b/src/main/java/de/bixilon/minosoft/data/world/chunk/light/SectionLight.kt index 696442e6b..4af513401 100644 --- a/src/main/java/de/bixilon/minosoft/data/world/chunk/light/SectionLight.kt +++ b/src/main/java/de/bixilon/minosoft/data/world/chunk/light/SectionLight.kt @@ -66,14 +66,14 @@ class SectionLight( val neighbours = section.neighbours ?: return val chunk = section.chunk if (y - light < 0) { - if (section.sectionHeight == chunk?.lowestSection) { + if (section.sectionHeight == chunk?.minSection) { chunk.light.bottom.decreaseCheckLevel(x, z, light - y, reset) } else { neighbours[Directions.O_DOWN]?.light?.decreaseCheckLevel(x, z, light - y, reset) } } if (y + light > ProtocolDefinition.SECTION_MAX_Y) { - if (section.sectionHeight == chunk?.highestSection) { + if (section.sectionHeight == chunk?.maxSection) { chunk.light.top.decreaseCheckLevel(x, z, light - (ProtocolDefinition.SECTION_MAX_Y - y), reset) } else { neighbours[Directions.O_UP]?.light?.decreaseCheckLevel(x, z, light - (ProtocolDefinition.SECTION_MAX_Y - y), reset) @@ -159,7 +159,7 @@ class SectionLight( if (target == null || (target != Directions.UP && lightProperties.propagatesLight(Directions.DOWN))) { if (y > 0) { traceBlockIncrease(x, y - 1, z, neighbourLuminance, Directions.DOWN) - } else if (section.sectionHeight == chunk.lowestSection) { + } else if (section.sectionHeight == chunk.minSection) { chunk.light.bottom.traceBlockIncrease(x, z, neighbourLuminance) } else { (neighbours[Directions.O_DOWN] ?: chunk.getOrPut(section.sectionHeight - 1, false))?.light?.traceBlockIncrease(x, ProtocolDefinition.SECTION_MAX_Y, z, neighbourLuminance, Directions.DOWN) @@ -168,7 +168,7 @@ class SectionLight( if (target == null || (target != Directions.DOWN && lightProperties.propagatesLight(Directions.UP))) { if (y < ProtocolDefinition.SECTION_MAX_Y) { traceBlockIncrease(x, y + 1, z, neighbourLuminance, Directions.UP) - } else if (section.sectionHeight == chunk.highestSection) { + } else if (section.sectionHeight == chunk.maxSection) { chunk.light.top.traceBlockIncrease(x, z, neighbourLuminance) } else { (neighbours[Directions.O_UP] ?: chunk.getOrPut(section.sectionHeight + 1, false))?.light?.traceBlockIncrease(x, 0, z, neighbourLuminance, Directions.UP) @@ -351,14 +351,14 @@ class SectionLight( if (target != Directions.UP && (target == null || lightProperties.propagatesLight(Directions.DOWN))) { if (y > 0) { traceSkylightIncrease(x, y - 1, z, nextNeighbourLevel, Directions.DOWN, totalY - 1) - } else if (section.sectionHeight != chunk.highestSection) { + } else if (section.sectionHeight != chunk.maxSection) { (neighbours[Directions.O_DOWN] ?: chunk.getOrPut(section.sectionHeight + 1, false))?.light?.traceSkylightIncrease(x, ProtocolDefinition.SECTION_MAX_Y, z, nextNeighbourLevel, Directions.DOWN, totalY - 1) } } if (target != Directions.DOWN && target != null && (lightProperties.propagatesLight(Directions.UP))) { if (y < ProtocolDefinition.SECTION_MAX_Y) { traceSkylightIncrease(x, y + 1, z, nextNeighbourLevel, Directions.UP, totalY + 1) - } else if (section.sectionHeight == chunk.lowestSection) { + } else if (section.sectionHeight == chunk.minSection) { chunk.light.bottom.traceSkyIncrease(x, z, nextLevel) } else { (neighbours[Directions.O_UP] ?: chunk.getOrPut(section.sectionHeight - 1, false))?.light?.traceSkylightIncrease(x, 0, z, nextNeighbourLevel, Directions.UP, totalY + 1) diff --git a/src/main/java/de/bixilon/minosoft/data/world/chunk/neighbours/ChunkNeighbours.kt b/src/main/java/de/bixilon/minosoft/data/world/chunk/neighbours/ChunkNeighbours.kt index ca5dbf3f7..8c6d7ca44 100644 --- a/src/main/java/de/bixilon/minosoft/data/world/chunk/neighbours/ChunkNeighbours.kt +++ b/src/main/java/de/bixilon/minosoft/data/world/chunk/neighbours/ChunkNeighbours.kt @@ -75,7 +75,7 @@ class ChunkNeighbours(val chunk: Chunk) { if (section == null) { continue } - val sectionNeighbours = ChunkUtil.getDirectNeighbours(neighbours, chunk, index + chunk.lowestSection) + val sectionNeighbours = ChunkUtil.getDirectNeighbours(neighbours, chunk, index + chunk.minSection) section.neighbours = sectionNeighbours } } diff --git a/src/main/java/de/bixilon/minosoft/gui/rendering/world/WorldRenderer.kt b/src/main/java/de/bixilon/minosoft/gui/rendering/world/WorldRenderer.kt index 8c55d8329..180227378 100644 --- a/src/main/java/de/bixilon/minosoft/gui/rendering/world/WorldRenderer.kt +++ b/src/main/java/de/bixilon/minosoft/gui/rendering/world/WorldRenderer.kt @@ -620,7 +620,7 @@ class WorldRenderer( // ToDo: Check if chunk is visible (not section, chunk) var queueChanges = 0 - for (sectionHeight in chunk.lowestSection until chunk.highestSection) { + for (sectionHeight in chunk.minSection until chunk.maxSection) { val section = chunk[sectionHeight] ?: continue val queued = internalQueueSection(chunkPosition, sectionHeight, chunk, section, false, neighbours = neighbours) if (queued) { diff --git a/src/main/java/de/bixilon/minosoft/gui/rendering/world/preparer/cull/SolidCullSectionPreparer.kt b/src/main/java/de/bixilon/minosoft/gui/rendering/world/preparer/cull/SolidCullSectionPreparer.kt index 67a7fc983..96e70f320 100644 --- a/src/main/java/de/bixilon/minosoft/gui/rendering/world/preparer/cull/SolidCullSectionPreparer.kt +++ b/src/main/java/de/bixilon/minosoft/gui/rendering/world/preparer/cull/SolidCullSectionPreparer.kt @@ -61,8 +61,8 @@ class SolidCullSectionPreparer( val random = Random(0L) val randomBlockModels = profile.antiMoirePattern - val isLowestSection = sectionHeight == chunk.lowestSection - val isHighestSection = sectionHeight == chunk.highestSection + val isLowestSection = sectionHeight == chunk.minSection + val isHighestSection = sectionHeight == chunk.maxSection val blocks = section.blocks val sectionLight = section.light val blockEntities: MutableSet> = mutableSetOf() diff --git a/src/test-util/kotlin/de/bixilon/minosoft/data/world/chunk/ChunkTestingUtil.kt b/src/test-util/kotlin/de/bixilon/minosoft/data/world/chunk/ChunkTestingUtil.kt index e6cfac5ae..6d301aba1 100644 --- a/src/test-util/kotlin/de/bixilon/minosoft/data/world/chunk/ChunkTestingUtil.kt +++ b/src/test-util/kotlin/de/bixilon/minosoft/data/world/chunk/ChunkTestingUtil.kt @@ -34,6 +34,7 @@ import de.bixilon.minosoft.gui.rendering.util.vec.vec2.Vec2iUtil.EMPTY import de.bixilon.minosoft.modding.event.master.EventMaster import de.bixilon.minosoft.protocol.network.connection.Connection import de.bixilon.minosoft.protocol.network.connection.play.PlayConnection +import de.bixilon.minosoft.util.KUtil.forceSet import de.bixilon.minosoft.util.KUtil.minosoft import org.objenesis.ObjenesisStd import org.testng.annotations.Test @@ -63,10 +64,10 @@ object ChunkTestingUtil { fun createEmptyChunk(position: ChunkPosition): Chunk { val objenesis = ObjenesisStd() val chunk = objenesis.newInstance(Chunk::class.java) - Chunk::highestSection.javaField!!.setValue(chunk, SECTIONS) Chunk::lock.javaField!!.setValue(chunk, ThreadLock()) - Chunk::chunkPosition.javaField!!.setValue(chunk, position) + chunk::chunkPosition.forceSet(position) Chunk::world.javaField!!.setValue(chunk, world) + Chunk::maxSection.javaField!!.setValue(chunk, chunk.world.dimension!!.maxSection) Chunk::connection.javaField!!.setValue(chunk, chunk.world.connection) Chunk::light.javaField!!.setValue(chunk, ChunkLight(chunk)) Chunk::neighbours.javaField!!.setValue(chunk, ChunkNeighbours(chunk))