From 432f77a6766d451b9b8f012f139a050cfc85888f Mon Sep 17 00:00:00 2001 From: DBotThePony Date: Thu, 18 Apr 2024 19:18:47 +0700 Subject: [PATCH] More object placement fixes --- .../kstarbound/defs/dungeon/DungeonWorld.kt | 2 +- .../dbotthepony/kstarbound/defs/dungeon/TiledMap.kt | 2 +- .../kstarbound/defs/object/ObjectDefinition.kt | 7 ++----- .../kstarbound/defs/object/ObjectOrientation.kt | 12 ++++++------ 4 files changed, 10 insertions(+), 13 deletions(-) diff --git a/src/main/kotlin/ru/dbotthepony/kstarbound/defs/dungeon/DungeonWorld.kt b/src/main/kotlin/ru/dbotthepony/kstarbound/defs/dungeon/DungeonWorld.kt index c737881d..93fe3fc2 100644 --- a/src/main/kotlin/ru/dbotthepony/kstarbound/defs/dungeon/DungeonWorld.kt +++ b/src/main/kotlin/ru/dbotthepony/kstarbound/defs/dungeon/DungeonWorld.kt @@ -491,7 +491,7 @@ class DungeonWorld(val parent: ServerWorld, val random: RandomGenerator, val mar parent.eventLoop.supplyAsync { for ((obj, direction) in placedObjects) { try { - val orientation = obj!!.config.value.findValidOrientation(parent, obj.tilePosition, direction) + val orientation = obj!!.config.value.findValidOrientation(parent, obj.tilePosition, direction, true) if (orientation != -1) { obj.orientationIndex = orientation.toLong() diff --git a/src/main/kotlin/ru/dbotthepony/kstarbound/defs/dungeon/TiledMap.kt b/src/main/kotlin/ru/dbotthepony/kstarbound/defs/dungeon/TiledMap.kt index f1d59964..100324b6 100644 --- a/src/main/kotlin/ru/dbotthepony/kstarbound/defs/dungeon/TiledMap.kt +++ b/src/main/kotlin/ru/dbotthepony/kstarbound/defs/dungeon/TiledMap.kt @@ -372,7 +372,7 @@ class TiledMap(data: JsonData) : TileMap() { this.polyline = ImmutableList.of() } - val calcPos = Vector2i(data.x, data.y) / PIXELS_IN_STARBOUND_UNITi + Vector2i(((merged["imagePositionX"]?.asDouble ?: 0.0) / PIXELS_IN_STARBOUND_UNIT).toInt(), -((merged["imagePositionY"]?.asDouble ?: 0.0) / PIXELS_IN_STARBOUND_UNIT).toInt()) + val calcPos = Vector2i(data.x, data.y) / PIXELS_IN_STARBOUND_UNITi + Vector2i(-((merged["imagePositionX"]?.asDouble ?: 0.0) / PIXELS_IN_STARBOUND_UNIT).toInt(), ((merged["imagePositionY"]?.asDouble ?: 0.0) / PIXELS_IN_STARBOUND_UNIT).toInt()) pos = calcPos.copy(y = this@TiledMap.size.y - calcPos.y - 1) size = Vector2i(data.width, data.height) / PIXELS_IN_STARBOUND_UNITi diff --git a/src/main/kotlin/ru/dbotthepony/kstarbound/defs/object/ObjectDefinition.kt b/src/main/kotlin/ru/dbotthepony/kstarbound/defs/object/ObjectDefinition.kt index c6d21bca..e6a3f0fc 100644 --- a/src/main/kotlin/ru/dbotthepony/kstarbound/defs/object/ObjectDefinition.kt +++ b/src/main/kotlin/ru/dbotthepony/kstarbound/defs/object/ObjectDefinition.kt @@ -85,15 +85,12 @@ data class ObjectDefinition( val flickerPeriod: PeriodicFunction? = null, val orientations: ImmutableList, ) { - fun findValidOrientation(world: World<*, *>, position: Vector2i, directionAffinity: Direction? = null): Int { + fun findValidOrientation(world: World<*, *>, position: Vector2i, directionAffinity: Direction? = null, ignoreProtectedDungeons: Boolean = false): Int { // If we are given a direction affinity, try and find an orientation with a // matching affinity *first* if (directionAffinity != null) { for ((i, orientation) in orientations.withIndex()) { - if (orientation.directionAffinity == null || orientation.directionAffinity != directionAffinity) - continue - - if (orientation.placementValid(world, position) && orientation.anchorsValid(world, position)) + if (orientation.directionAffinity == directionAffinity && orientation.placementValid(world, position, ignoreProtectedDungeons) && orientation.anchorsValid(world, position)) return i } } diff --git a/src/main/kotlin/ru/dbotthepony/kstarbound/defs/object/ObjectOrientation.kt b/src/main/kotlin/ru/dbotthepony/kstarbound/defs/object/ObjectOrientation.kt index 72456e54..b44fe702 100644 --- a/src/main/kotlin/ru/dbotthepony/kstarbound/defs/object/ObjectOrientation.kt +++ b/src/main/kotlin/ru/dbotthepony/kstarbound/defs/object/ObjectOrientation.kt @@ -63,7 +63,7 @@ data class ObjectOrientation( val touchDamage: JsonReference.Object?, val particleEmitters: ArrayList, ) { - fun placementValid(world: World<*, *>, position: Vector2i): Boolean { + fun placementValid(world: World<*, *>, position: Vector2i, ignoreProtectedDungeons: Boolean = false): Boolean { if (occupySpaces.isEmpty()) return true @@ -71,7 +71,7 @@ data class ObjectOrientation( val cell = world.chunkMap.getCell(it + position) //if (!cell.foreground.material.isEmptyTile) println("not empty tile: ${it + position}, space $it, pos $position") //if (cell.dungeonId in world.protectedDungeonIDs) println("position is protected: ${it + position}") - cell.foreground.material.isEmptyTile && cell.dungeonId !in world.protectedDungeonIDs + cell.foreground.material.isEmptyTile && (ignoreProtectedDungeons || cell.dungeonId !in world.protectedDungeonIDs) } } @@ -246,10 +246,10 @@ data class ObjectOrientation( for (v in obj.get("anchors", JsonArray())) { when (v.asString.lowercase()) { - "left" -> occupySpaces.stream().filter { it.x == boundingBox.mins.x }.forEach { anchors.add(Anchor(false, it + Vector2i.NEGATIVE_X, requireTilledAnchors, requireSoilAnchors, anchorMaterial)) } - "right" -> occupySpaces.stream().filter { it.x == boundingBox.maxs.x }.forEach { anchors.add(Anchor(false, it + Vector2i.POSITIVE_X, requireTilledAnchors, requireSoilAnchors, anchorMaterial)) } - "top" -> occupySpaces.stream().filter { it.y == boundingBox.maxs.y }.forEach { anchors.add(Anchor(false, it + Vector2i.POSITIVE_Y, requireTilledAnchors, requireSoilAnchors, anchorMaterial)) } - "bottom" -> occupySpaces.stream().filter { it.y == boundingBox.mins.y }.forEach { anchors.add(Anchor(false, it + Vector2i.NEGATIVE_Y, requireTilledAnchors, requireSoilAnchors, anchorMaterial)) } + "left" -> occupySpaces.stream().filter { it.x == minX }.forEach { anchors.add(Anchor(false, it + Vector2i.NEGATIVE_X, requireTilledAnchors, requireSoilAnchors, anchorMaterial)) } + "right" -> occupySpaces.stream().filter { it.x == maxX }.forEach { anchors.add(Anchor(false, it + Vector2i.POSITIVE_X, requireTilledAnchors, requireSoilAnchors, anchorMaterial)) } + "top" -> occupySpaces.stream().filter { it.y == maxY }.forEach { anchors.add(Anchor(false, it + Vector2i.POSITIVE_Y, requireTilledAnchors, requireSoilAnchors, anchorMaterial)) } + "bottom" -> occupySpaces.stream().filter { it.y == minY }.forEach { anchors.add(Anchor(false, it + Vector2i.NEGATIVE_Y, requireTilledAnchors, requireSoilAnchors, anchorMaterial)) } "background" -> occupySpaces.forEach { anchors.add(Anchor(true, it + Vector2i.NEGATIVE_Y, requireTilledAnchors, requireSoilAnchors, anchorMaterial)) } else -> throw JsonSyntaxException("Unknown anchor type $v") }