From d927d1d240672ef3ad3c2911d23fec335adac954 Mon Sep 17 00:00:00 2001 From: minjaesong Date: Sun, 21 Jan 2024 03:51:08 +0900 Subject: [PATCH] fix: door spawn required wall be exist on the cells that don't matter --- src/net/torvald/terrarum/Terrarum.kt | 6 +++++ .../modulebasegame/gameactors/FixtureBase.kt | 8 +----- .../gameactors/FixtureSwingingDoorBase.kt | 27 +++++++++++++++++++ 3 files changed, 34 insertions(+), 7 deletions(-) diff --git a/src/net/torvald/terrarum/Terrarum.kt b/src/net/torvald/terrarum/Terrarum.kt index 2c76c123e..2e9795e73 100644 --- a/src/net/torvald/terrarum/Terrarum.kt +++ b/src/net/torvald/terrarum/Terrarum.kt @@ -1013,3 +1013,9 @@ fun distBetween(a: ActorWithBody, bpos: Vector2): Double { } const val hashStrMap = "YBNDRFG8EJKMCPQXOTLVWIS2A345H769" fun getHashStr(length: Int = 5) = (0 until length).map { hashStrMap[Math.random().times(32).toInt()] }.joinToString("") + +fun List.cartesianProduct(other: List) = this.flatMap { thisIt -> + other.map { otherIt -> + thisIt to otherIt + } +} \ No newline at end of file diff --git a/src/net/torvald/terrarum/modulebasegame/gameactors/FixtureBase.kt b/src/net/torvald/terrarum/modulebasegame/gameactors/FixtureBase.kt index 0c27b9db0..f59b00c68 100644 --- a/src/net/torvald/terrarum/modulebasegame/gameactors/FixtureBase.kt +++ b/src/net/torvald/terrarum/modulebasegame/gameactors/FixtureBase.kt @@ -218,12 +218,6 @@ open class FixtureBase : ActorWithBody, CuedByTerrainChange { } } - private fun List.cartesianProduct(other: List) = this.flatMap { thisIt -> - other.map { otherIt -> - thisIt to otherIt - } - } - val everyBlockboxPos: List>? get() = worldBlockPos?.let { (posX, posY) -> (posX until posX + blockBox.width).toList().cartesianProduct((posY until posY + blockBox.height).toList()) @@ -254,7 +248,7 @@ open class FixtureBase : ActorWithBody, CuedByTerrainChange { return canSpawnHere0(posX, posY) } - private fun canSpawnHere0(posX: Int, posY: Int): Boolean { + open fun canSpawnHere0(posX: Int, posY: Int): Boolean { val everyBlockboxPos = (posX until posX + blockBox.width).toList().cartesianProduct((posY until posY + blockBox.height).toList()) // check for existing blocks (and fixtures) diff --git a/src/net/torvald/terrarum/modulebasegame/gameactors/FixtureSwingingDoorBase.kt b/src/net/torvald/terrarum/modulebasegame/gameactors/FixtureSwingingDoorBase.kt index eebf4901c..185fef66a 100644 --- a/src/net/torvald/terrarum/modulebasegame/gameactors/FixtureSwingingDoorBase.kt +++ b/src/net/torvald/terrarum/modulebasegame/gameactors/FixtureSwingingDoorBase.kt @@ -145,6 +145,33 @@ open class FixtureSwingingDoorBase : FixtureBase { override fun spawn(posX: Int, posY: Int, installersUUID: UUID?): Boolean = spawn(posX, posY, installersUUID, tilewiseHitboxWidth, tilewiseHitboxHeight) + override fun canSpawnHere0(posX: Int, posY: Int): Boolean { + val everyBlockboxPos = (posX until posX + blockBox.width).toList().cartesianProduct((posY until posY + blockBox.height).toList()) + + val xoffToFrame = (tilewiseHitboxWidth - twClosed) / 2 + val everyDoorframePos = (posX + xoffToFrame until posX + blockBox.width - 1).toList().cartesianProduct((posY until posY + blockBox.height).toList()) + + // check for existing blocks (and fixtures) + var cannotSpawn = false + worldBlockPos = Point2i(posX, posY) + + cannotSpawn = everyBlockboxPos.any { (x, y) -> !BlockCodex[world!!.getTileFromTerrain(x, y)].hasTag("INCONSEQUENTIAL") } + + // check for walls, if spawnNeedsWall = true + if (spawnNeedsWall) { + cannotSpawn = cannotSpawn or everyDoorframePos.any { (x, y) -> !BlockCodex[world!!.getTileFromWall(x, y)].isSolid } + } + + // check for floors, if spawnNeedsFloor == true + if (spawnNeedsFloor) { + val y = posY + blockBox.height + val xs = posX + xoffToFrame until posX + blockBox.width - xoffToFrame + cannotSpawn = cannotSpawn or xs.any { x -> !BlockCodex[world!!.getTileFromTerrain(x, y)].isSolid } + } + + return !cannotSpawn + } + override fun reload() { super.reload()