From 186144c8ccc85419490597a274c9730e1c1c021a Mon Sep 17 00:00:00 2001 From: DBotThePony Date: Tue, 22 Feb 2022 07:35:23 +0700 Subject: [PATCH] Use AVL tree for chunk map --- .../kotlin/ru/dbotthepony/kstarbound/Main.kt | 4 +-- .../dbotthepony/kstarbound/io/StarboundPak.kt | 2 +- .../ru/dbotthepony/kstarbound/world/Chunk.kt | 27 ++++++++++++++++++- .../ru/dbotthepony/kstarbound/world/World.kt | 12 ++++++--- 4 files changed, 37 insertions(+), 8 deletions(-) diff --git a/src/main/kotlin/ru/dbotthepony/kstarbound/Main.kt b/src/main/kotlin/ru/dbotthepony/kstarbound/Main.kt index 7101ad8f..f840037a 100644 --- a/src/main/kotlin/ru/dbotthepony/kstarbound/Main.kt +++ b/src/main/kotlin/ru/dbotthepony/kstarbound/Main.kt @@ -189,8 +189,8 @@ fun main() { client.camera.pos.y = 10f - client.gl.box2dRenderer.drawShapes = true - client.gl.box2dRenderer.drawPairs = true + client.gl.box2dRenderer.drawShapes = false + client.gl.box2dRenderer.drawPairs = false client.gl.box2dRenderer.drawAABB = false client.gl.box2dRenderer.drawJoints = false diff --git a/src/main/kotlin/ru/dbotthepony/kstarbound/io/StarboundPak.kt b/src/main/kotlin/ru/dbotthepony/kstarbound/io/StarboundPak.kt index c134e5b0..8d120de0 100644 --- a/src/main/kotlin/ru/dbotthepony/kstarbound/io/StarboundPak.kt +++ b/src/main/kotlin/ru/dbotthepony/kstarbound/io/StarboundPak.kt @@ -58,7 +58,7 @@ class StarboundPakFile( } fun read(storage: StarboundPak, reader: DataInputStream): StarboundPakFile { - val readLength = reader.read() + val readLength = reader.readVarInt() val name = reader.readASCIIString(readLength) require(name[0] == '/') { "$name appears to be not an absolute filename" } val offset = reader.readLong() diff --git a/src/main/kotlin/ru/dbotthepony/kstarbound/world/Chunk.kt b/src/main/kotlin/ru/dbotthepony/kstarbound/world/Chunk.kt index e985f3ec..6d9397f3 100644 --- a/src/main/kotlin/ru/dbotthepony/kstarbound/world/Chunk.kt +++ b/src/main/kotlin/ru/dbotthepony/kstarbound/world/Chunk.kt @@ -163,7 +163,7 @@ const val CHUNK_SIZE_FF = CHUNK_SIZE - 1 const val CHUNK_SIZEf = CHUNK_SIZE.toFloat() const val CHUNK_SIZEd = CHUNK_SIZE.toDouble() -data class ChunkPos(val x: Int, val y: Int) { +class ChunkPos(val x: Int, val y: Int) : Comparable { constructor(pos: IStruct2i) : this(pos.component1(), pos.component2()) val firstBlock get() = Vector2i(x shl CHUNK_SHIFT, y shl CHUNK_SHIFT) @@ -185,6 +185,31 @@ data class ChunkPos(val x: Int, val y: Int) { return ChunkPos(x - 1, y) } + override fun equals(other: Any?): Boolean { + if (other is ChunkPos) + return other.x == x && other.y == y + + return false + } + + override fun hashCode(): Int { + return (y shl 16) xor x + } + + override fun toString(): String { + return "ChunkPos[$x $y]" + } + + override fun compareTo(other: ChunkPos): Int { + if (x > other.x) { + return 1 + } else if (x < other.x) { + return -1 + } + + return y.compareTo(other.y) + } + companion object { val ZERO = ChunkPos(0, 0) diff --git a/src/main/kotlin/ru/dbotthepony/kstarbound/world/World.kt b/src/main/kotlin/ru/dbotthepony/kstarbound/world/World.kt index 1c7ab2d9..2addc917 100644 --- a/src/main/kotlin/ru/dbotthepony/kstarbound/world/World.kt +++ b/src/main/kotlin/ru/dbotthepony/kstarbound/world/World.kt @@ -1,5 +1,7 @@ package ru.dbotthepony.kstarbound.world +import it.unimi.dsi.fastutil.objects.Object2ObjectAVLTreeMap +import it.unimi.dsi.fastutil.objects.Object2ObjectFunction import ru.dbotthepony.kbox2d.api.ContactImpulse import ru.dbotthepony.kbox2d.api.IContactFilter import ru.dbotthepony.kbox2d.api.IContactListener @@ -140,7 +142,9 @@ class Timer(val period: Double, val executionTimes: Int, val func: (Timer) -> Un } abstract class World, ChunkType : Chunk>(val seed: Long = 0L) { - protected val chunkMap = HashMap>() + protected val chunkMap = Object2ObjectAVLTreeMap> cmp@{ a, b -> + return@cmp a.compareTo(b) + } /** * Chunks, which have their collision mesh changed @@ -335,7 +339,7 @@ abstract class World, ChunkType : Chunk, ChunkType : Chunk {