block changing: minimal performance improvements

This commit is contained in:
Bixilon 2023-05-31 21:38:05 +02:00
parent 9b1ae46aeb
commit 11f207fc54
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
3 changed files with 18 additions and 11 deletions

View File

@ -34,7 +34,7 @@ open class BlockState(
override fun equals(other: Any?): Boolean { override fun equals(other: Any?): Boolean {
if (other is ResourceLocation) return other == block.identifier if (other is ResourceLocation) return other == block.identifier
if (other is BlockState) return other.block == block if (other is BlockState) return other.block == block && other.luminance == luminance
return false return false
} }

View File

@ -37,7 +37,7 @@ open class PropertyBlockState(
override fun equals(other: Any?): Boolean { override fun equals(other: Any?): Boolean {
if (other is ResourceLocation) return other == block.identifier if (other is ResourceLocation) return other == block.identifier
if (other is PropertyBlockState) return other.block == this.block && other.properties == this.properties if (other is PropertyBlockState) return other.block == block && other.luminance == luminance && other.properties == this.properties
return false return false
} }

View File

@ -142,27 +142,34 @@ class Chunk(
if (updates.isEmpty()) return if (updates.isEmpty()) return
if (updates.size == 1) return apply(updates.first()) if (updates.size == 1) return apply(updates.first())
lock.lock()
val executed: MutableSet<ChunkLocalBlockUpdate.LocalUpdate> = hashSetOf() val executed: MutableSet<ChunkLocalBlockUpdate.LocalUpdate> = hashSetOf()
val sections: MutableSet<ChunkSection> = hashSetOf() val sections: MutableSet<ChunkSection> = hashSetOf()
lock.lock()
for (update in updates) { for (update in updates) {
val section = getOrPut(update.position.y.sectionHeight, lock = false) ?: continue val sectionHeight = update.position.y.sectionHeight
var section = this[sectionHeight]
if (update.state == null && section == null) continue
section = getOrPut(sectionHeight, lock = false) ?: continue
val previous = section.blocks.noOcclusionSet(update.position.x, update.position.y.inSectionHeight, update.position.z, update.state) val previous = section.blocks.noOcclusionSet(update.position.x, update.position.y.inSectionHeight, update.position.z, update.state)
if (previous == update.state) continue if (previous == update.state) continue
getOrPutBlockEntity(update.position) getOrPutBlockEntity(update.position)
executed += update executed += update
sections += section sections += section
} }
if (executed.isNotEmpty()) {
for (section in sections) { if (executed.isEmpty()) {
section.blocks.occlusion.recalculate(true) return lock.unlock()
} }
light.recalculateHeightmap() light.recalculateHeightmap()
light.recalculate() light.recalculate()
for (section in sections) {
section.blocks.occlusion.recalculate(true)
} }
lock.unlock() lock.unlock()
if (executed.isEmpty()) return
ChunkLocalBlockUpdate(chunkPosition, this, executed).fire(connection) ChunkLocalBlockUpdate(chunkPosition, this, executed).fire(connection)
} }