diff --git a/codegen/src/generator/kotlin/de/fiereu/openmmo/codegen/maps/MetatileBehaviors.kt b/codegen/src/generator/kotlin/de/fiereu/openmmo/codegen/maps/MetatileBehaviors.kt index ab9b236..24fd236 100644 --- a/codegen/src/generator/kotlin/de/fiereu/openmmo/codegen/maps/MetatileBehaviors.kt +++ b/codegen/src/generator/kotlin/de/fiereu/openmmo/codegen/maps/MetatileBehaviors.kt @@ -83,7 +83,8 @@ private constructor( ) } - // Only the behaviors we act on are mapped, everything else stays NORMAL. + // Only the behaviors we act on are mapped, everything else stays NORMAL. The two games give + // the same tile different names, so this matches on names and not on numbers. private fun classify(name: String): TileBehavior? = when (name) { "MB_TALL_GRASS" -> TileBehavior.TALL_GRASS @@ -92,6 +93,36 @@ private constructor( "MB_JUMP_WEST" -> TileBehavior.JUMP_WEST "MB_JUMP_NORTH" -> TileBehavior.JUMP_NORTH "MB_JUMP_SOUTH" -> TileBehavior.JUMP_SOUTH + // The only door the decomp opens with TryDoorWarp. + "MB_ANIMATED_DOOR", + "MB_WARP_DOOR" -> TileBehavior.DOOR + "MB_NON_ANIMATED_DOOR", + "MB_WATER_DOOR", + "MB_CAVE_DOOR" -> TileBehavior.NON_ANIMATED_DOOR + // The rest of what the decomp's IsWarpMetatileBehavior accepts, in both games. + "MB_LADDER", + "MB_UP_ESCALATOR", + "MB_DOWN_ESCALATOR", + "MB_REGULAR_WARP", + "MB_FALL_WARP", + "MB_DEEP_SOUTH_WARP", + "MB_UNION_ROOM_WARP", + "MB_BRIDGE_OVER_OCEAN", + "MB_LAVARIDGE_GYM_B1F_WARP", + "MB_LAVARIDGE_GYM_1F_WARP", + "MB_LAVARIDGE_1F_WARP", + "MB_AQUA_HIDEOUT_WARP", + "MB_MT_PYRE_HOLE", + "MB_MOSSDEEP_GYM_WARP" -> TileBehavior.LADDER + "MB_UP_RIGHT_STAIR_WARP", + "MB_DOWN_RIGHT_STAIR_WARP" -> TileBehavior.STAIR_WARP_EAST + "MB_UP_LEFT_STAIR_WARP", + "MB_DOWN_LEFT_STAIR_WARP" -> TileBehavior.STAIR_WARP_WEST + "MB_NORTH_ARROW_WARP" -> TileBehavior.NORTH_ARROW_WARP + "MB_SOUTH_ARROW_WARP", + "MB_WATER_SOUTH_ARROW_WARP" -> TileBehavior.SOUTH_ARROW_WARP + "MB_EAST_ARROW_WARP" -> TileBehavior.EAST_ARROW_WARP + "MB_WEST_ARROW_WARP" -> TileBehavior.WEST_ARROW_WARP else -> null } diff --git a/common/src/main/kotlin/de/fiereu/openmmo/common/enums/TileBehavior.kt b/common/src/main/kotlin/de/fiereu/openmmo/common/enums/TileBehavior.kt index eb1a434..1130d2c 100644 --- a/common/src/main/kotlin/de/fiereu/openmmo/common/enums/TileBehavior.kt +++ b/common/src/main/kotlin/de/fiereu/openmmo/common/enums/TileBehavior.kt @@ -12,4 +12,32 @@ enum class TileBehavior { JUMP_WEST, JUMP_NORTH, JUMP_SOUTH, + DOOR, + /** Cave and water doors. They look like doors but warp like a ladders. */ + NON_ANIMATED_DOOR, + /** Ladders, escalators and warp pads, which warp as soon as the player steps on them. */ + LADDER, + STAIR_WARP_EAST, + STAIR_WARP_WEST, + /** Arrow warps are named after the direction the player walks to use them. */ + NORTH_ARROW_WARP, + SOUTH_ARROW_WARP, + EAST_ARROW_WARP, + WEST_ARROW_WARP; + + /** The direction the player must walk while standing here to be warped. */ + val warpsWhenWalking: Direction? + get() = + when (this) { + STAIR_WARP_EAST, + EAST_ARROW_WARP -> Direction.RIGHT + STAIR_WARP_WEST, + WEST_ARROW_WARP -> Direction.LEFT + NORTH_ARROW_WARP -> Direction.UP + SOUTH_ARROW_WARP -> Direction.DOWN + else -> null + } + + val warpsOnStep: Boolean + get() = this == LADDER || this == NON_ANIMATED_DOOR } diff --git a/server.game/src/main/kotlin/de/fiereu/openmmo/server/game/services/MovementService.kt b/server.game/src/main/kotlin/de/fiereu/openmmo/server/game/services/MovementService.kt index da4d980..9ae3d93 100644 --- a/server.game/src/main/kotlin/de/fiereu/openmmo/server/game/services/MovementService.kt +++ b/server.game/src/main/kotlin/de/fiereu/openmmo/server/game/services/MovementService.kt @@ -115,14 +115,14 @@ constructor( toY = landing.second } - // Some warps trigger from their current tile. - val onTileWarp = - currentMap.warps.find { w -> - w.x == fromX && w.y == fromY && w.facingDirection == msg.direction - } - if (onTileWarp != null) { - warpService.executeWarp(ctx, charId, onTileWarp) - return + // Stairs and arrow warps fire from the tile the player stands on. + val standingBehavior = currentMap.tileAt(fromX, fromY)?.behavior + if (standingBehavior?.warpsWhenWalking == msg.direction) { + val onTileWarp = currentMap.warps.find { it.x == fromX && it.y == fromY } + if (onTileWarp != null) { + warpService.executeWarp(ctx, charId, onTileWarp) + return + } } // Walking off the edge of a map hands the player to the neighbouring map, if there is one. @@ -161,13 +161,14 @@ constructor( return } - // Warps fire on the destination tile. - val warp = - currentMap.warps.find { w -> - w.x == toX && - w.y == toY && - (w.facingDirection == null || w.facingDirection == msg.direction) + // A door only warps when walked into from below, a ladder warps on the step itself. + val targetBehavior = currentMap.tileAt(toX, toY)?.behavior + val stepsIntoWarp = + when (targetBehavior) { + TileBehavior.DOOR -> msg.direction == Direction.UP + else -> targetBehavior?.warpsOnStep == true } + val warp = if (!stepsIntoWarp) null else currentMap.warps.find { w -> w.x == toX && w.y == toY } if (warp != null) { log.info { "WARP at ($toX, $toY) facing ${msg.direction}" } // A MAP_DYNAMIC warp (the truck exit) resolves to the destination a script set on the player. diff --git a/server.game/src/main/kotlin/de/fiereu/openmmo/server/game/services/WarpService.kt b/server.game/src/main/kotlin/de/fiereu/openmmo/server/game/services/WarpService.kt index 364e9b0..7b5f626 100644 --- a/server.game/src/main/kotlin/de/fiereu/openmmo/server/game/services/WarpService.kt +++ b/server.game/src/main/kotlin/de/fiereu/openmmo/server/game/services/WarpService.kt @@ -39,12 +39,13 @@ constructor( val knownOverride = WarpExitRules.getKnownOverride(sourceMap, destMap, warp.targetX, warp.targetY) + val destBehavior = destMap?.tileAt(warp.targetX, warp.targetY)?.behavior val warpFacing = warp.exitFacing ?: knownOverride?.facing ?: WarpExitRules.inferExitFacing( - destTileBehavior = null, + destTileBehavior = destBehavior, destMap = destMap, destX = warp.targetX, destY = warp.targetY, @@ -58,7 +59,11 @@ constructor( val shouldAutoStepOffWarp = knownOverride?.autoStep - ?: WarpExitRules.shouldAutoStep(sourceMap = sourceMap, destMap = destMap) + ?: WarpExitRules.shouldAutoStep( + sourceMap = sourceMap, + destMap = destMap, + destTileBehavior = destBehavior, + ) if (shouldAutoStepOffWarp && destMap != null) { val destWarp = destMap.warps.find { it.x == offsetX && it.y == offsetY } if (destWarp != null) { diff --git a/server.game/src/main/kotlin/de/fiereu/openmmo/server/game/world/WarpExitRules.kt b/server.game/src/main/kotlin/de/fiereu/openmmo/server/game/world/WarpExitRules.kt index be8facd..9de32a3 100644 --- a/server.game/src/main/kotlin/de/fiereu/openmmo/server/game/world/WarpExitRules.kt +++ b/server.game/src/main/kotlin/de/fiereu/openmmo/server/game/world/WarpExitRules.kt @@ -2,6 +2,7 @@ package de.fiereu.openmmo.server.game.world import de.fiereu.openmmo.common.enums.Direction import de.fiereu.openmmo.common.enums.MapType +import de.fiereu.openmmo.common.enums.TileBehavior import de.fiereu.openmmo.maps.MapDef data class WarpExitOverride( @@ -12,7 +13,7 @@ data class WarpExitOverride( object WarpExitRules { fun inferExitFacing( - destTileBehavior: String?, + destTileBehavior: TileBehavior?, destMap: MapDef?, destX: Int, destY: Int, @@ -20,13 +21,15 @@ object WarpExitRules { ): Direction { if (destMap == null) return Direction.DOWN + // The tile itself is exact, so it wins over the guesses below. when (destTileBehavior) { - "MB_NON_ANIMATED_DOOR", - "MB_ANIMATED_DOOR" -> return Direction.DOWN - "MB_NORTH_ARROW_WARP" -> return Direction.DOWN - "MB_SOUTH_ARROW_WARP" -> return Direction.UP - "MB_WEST_ARROW_WARP" -> return Direction.RIGHT - "MB_EAST_ARROW_WARP" -> return Direction.LEFT + TileBehavior.DOOR, + TileBehavior.NON_ANIMATED_DOOR -> return Direction.DOWN + TileBehavior.NORTH_ARROW_WARP -> return Direction.DOWN + TileBehavior.SOUTH_ARROW_WARP -> return Direction.UP + TileBehavior.WEST_ARROW_WARP -> return Direction.RIGHT + TileBehavior.EAST_ARROW_WARP -> return Direction.LEFT + else -> Unit } val sourceMapType = sourceMap?.mapType @@ -103,9 +106,20 @@ object WarpExitRules { fun shouldAutoStep( sourceMap: MapDef?, destMap: MapDef?, + destTileBehavior: TileBehavior? = null, ): Boolean { if (sourceMap == null || destMap == null) return false + // An animated door drops the player below it, every other warp tile keeps them on it. + when (destTileBehavior) { + TileBehavior.DOOR -> return true + TileBehavior.NON_ANIMATED_DOOR, + TileBehavior.LADDER, + TileBehavior.STAIR_WARP_EAST, + TileBehavior.STAIR_WARP_WEST -> return false + else -> Unit + } + val sourceBuilding = sourceMap.mapType == MapType.INSIDE || sourceMap.mapType == MapType.SECRET_BASE diff --git a/server.game/src/test/kotlin/de/fiereu/openmmo/server/game/world/WarpTileBehaviorTest.kt b/server.game/src/test/kotlin/de/fiereu/openmmo/server/game/world/WarpTileBehaviorTest.kt new file mode 100644 index 0000000..6758116 --- /dev/null +++ b/server.game/src/test/kotlin/de/fiereu/openmmo/server/game/world/WarpTileBehaviorTest.kt @@ -0,0 +1,74 @@ +package de.fiereu.openmmo.server.game.world + +import de.fiereu.openmmo.common.enums.Direction +import de.fiereu.openmmo.common.enums.TileBehavior +import de.fiereu.openmmo.maps.MapManager +import io.kotest.core.spec.style.FunSpec +import io.kotest.matchers.shouldBe + +private const val KANTO = 0 +private const val TOWNS_AND_ROUTES_BANK = 3 +private const val PALLET_TOWN_MAP = 0 +private const val ROUTE_4_MAP = 22 +private const val INDOOR_PALLET_BANK = 4 +private const val PLAYERS_HOUSE_1F = 0 +private const val PLAYERS_HOUSE_2F = 1 + +class WarpTileBehaviorTest : + FunSpec({ + val maps = MapManager() + + test("Kanto doors and stairs are classified from the decomp") { + val town = maps.getMap(KANTO, TOWNS_AND_ROUTES_BANK, PALLET_TOWN_MAP)!! + town.tileAt(6, 7)!!.behavior shouldBe TileBehavior.DOOR + + // Right goes up, left comes back down. + val house1f = maps.getMap(KANTO, INDOOR_PALLET_BANK, PLAYERS_HOUSE_1F)!! + val house2f = maps.getMap(KANTO, INDOOR_PALLET_BANK, PLAYERS_HOUSE_2F)!! + house1f.tileAt(10, 2)!!.behavior shouldBe TileBehavior.STAIR_WARP_EAST + house2f.tileAt(10, 2)!!.behavior shouldBe TileBehavior.STAIR_WARP_WEST + house1f.tileAt(10, 2)!!.behavior.warpsWhenWalking shouldBe Direction.RIGHT + house2f.tileAt(10, 2)!!.behavior.warpsWhenWalking shouldBe Direction.LEFT + } + + test("standing on stairs only warps when walking the way they point") { + val stairs = maps.getMap(KANTO, INDOOR_PALLET_BANK, PLAYERS_HOUSE_1F)!!.tileAt(10, 2)!! + + stairs.behavior.warpsWhenWalking shouldBe Direction.RIGHT + (stairs.behavior.warpsWhenWalking == Direction.LEFT) shouldBe false + (stairs.behavior.warpsWhenWalking == Direction.UP) shouldBe false + (stairs.behavior.warpsWhenWalking == Direction.DOWN) shouldBe false + stairs.behavior.warpsOnStep shouldBe false + } + + test("a door drops the player out facing down and off the tile") { + val town = maps.getMap(KANTO, TOWNS_AND_ROUTES_BANK, PALLET_TOWN_MAP)!! + val house = maps.getMap(KANTO, INDOOR_PALLET_BANK, PLAYERS_HOUSE_1F)!! + + WarpExitRules.inferExitFacing( + destTileBehavior = TileBehavior.DOOR, + destMap = town, + destX = 6, + destY = 7, + sourceMap = house, + ) shouldBe Direction.DOWN + WarpExitRules.shouldAutoStep(house, town, TileBehavior.DOOR) shouldBe true + TileBehavior.DOOR.warpsWhenWalking shouldBe null + } + + test("a cave entrance warps on the step, in any direction") { + val route4 = maps.getMap(KANTO, TOWNS_AND_ROUTES_BANK, ROUTE_4_MAP)!! + val entrance = route4.tileAt(19, 5)!!.behavior + + entrance shouldBe TileBehavior.NON_ANIMATED_DOOR + entrance.warpsOnStep shouldBe true + entrance.warpsWhenWalking shouldBe null + } + + test("stairs leave the player standing on the tile") { + val house1f = maps.getMap(KANTO, INDOOR_PALLET_BANK, PLAYERS_HOUSE_1F)!! + val house2f = maps.getMap(KANTO, INDOOR_PALLET_BANK, PLAYERS_HOUSE_2F)!! + + WarpExitRules.shouldAutoStep(house1f, house2f, TileBehavior.STAIR_WARP_EAST) shouldBe false + } + })