More object placement fixes

This commit is contained in:
DBotThePony 2024-04-18 19:18:47 +07:00
parent 432df454e2
commit 432f77a676
Signed by: DBot
GPG Key ID: DCC23B5715498507
4 changed files with 10 additions and 13 deletions

View File

@ -491,7 +491,7 @@ class DungeonWorld(val parent: ServerWorld, val random: RandomGenerator, val mar
parent.eventLoop.supplyAsync { parent.eventLoop.supplyAsync {
for ((obj, direction) in placedObjects) { for ((obj, direction) in placedObjects) {
try { 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) { if (orientation != -1) {
obj.orientationIndex = orientation.toLong() obj.orientationIndex = orientation.toLong()

View File

@ -372,7 +372,7 @@ class TiledMap(data: JsonData) : TileMap() {
this.polyline = ImmutableList.of() 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) pos = calcPos.copy(y = this@TiledMap.size.y - calcPos.y - 1)
size = Vector2i(data.width, data.height) / PIXELS_IN_STARBOUND_UNITi size = Vector2i(data.width, data.height) / PIXELS_IN_STARBOUND_UNITi

View File

@ -85,15 +85,12 @@ data class ObjectDefinition(
val flickerPeriod: PeriodicFunction? = null, val flickerPeriod: PeriodicFunction? = null,
val orientations: ImmutableList<ObjectOrientation>, val orientations: ImmutableList<ObjectOrientation>,
) { ) {
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 // If we are given a direction affinity, try and find an orientation with a
// matching affinity *first* // matching affinity *first*
if (directionAffinity != null) { if (directionAffinity != null) {
for ((i, orientation) in orientations.withIndex()) { for ((i, orientation) in orientations.withIndex()) {
if (orientation.directionAffinity == null || orientation.directionAffinity != directionAffinity) if (orientation.directionAffinity == directionAffinity && orientation.placementValid(world, position, ignoreProtectedDungeons) && orientation.anchorsValid(world, position))
continue
if (orientation.placementValid(world, position) && orientation.anchorsValid(world, position))
return i return i
} }
} }

View File

@ -63,7 +63,7 @@ data class ObjectOrientation(
val touchDamage: JsonReference.Object?, val touchDamage: JsonReference.Object?,
val particleEmitters: ArrayList<ParticleEmissionEntry>, val particleEmitters: ArrayList<ParticleEmissionEntry>,
) { ) {
fun placementValid(world: World<*, *>, position: Vector2i): Boolean { fun placementValid(world: World<*, *>, position: Vector2i, ignoreProtectedDungeons: Boolean = false): Boolean {
if (occupySpaces.isEmpty()) if (occupySpaces.isEmpty())
return true return true
@ -71,7 +71,7 @@ data class ObjectOrientation(
val cell = world.chunkMap.getCell(it + position) val cell = world.chunkMap.getCell(it + position)
//if (!cell.foreground.material.isEmptyTile) println("not empty tile: ${it + position}, space $it, pos $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}") //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())) { for (v in obj.get("anchors", JsonArray())) {
when (v.asString.lowercase()) { 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)) } "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 == boundingBox.maxs.x }.forEach { anchors.add(Anchor(false, it + Vector2i.POSITIVE_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 == boundingBox.maxs.y }.forEach { anchors.add(Anchor(false, it + Vector2i.POSITIVE_Y, 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 == boundingBox.mins.y }.forEach { anchors.add(Anchor(false, it + Vector2i.NEGATIVE_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)) } "background" -> occupySpaces.forEach { anchors.add(Anchor(true, it + Vector2i.NEGATIVE_Y, requireTilledAnchors, requireSoilAnchors, anchorMaterial)) }
else -> throw JsonSyntaxException("Unknown anchor type $v") else -> throw JsonSyntaxException("Unknown anchor type $v")
} }