fix(game): trigger warps by their tile behaviour (#93)

* fix(game): use the npc's own region for map and sprite lookups

* fix(game): trigger warps by their tile behaviour

* fix(game): classify every warp metatile behaviour the decomp accepts
This commit is contained in:
Fiereu authored and GitHub committed 2026-08-07 10:57:17 +02:00
1 parent 9866c1b0fb
commit 14fbc05bab
6 files changed
+177 -24

No files matched your search

@@ -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? = private fun classify(name: String): TileBehavior? =
when (name) { when (name) {
"MB_TALL_GRASS" -> TileBehavior.TALL_GRASS "MB_TALL_GRASS" -> TileBehavior.TALL_GRASS
@@ -92,6 +93,36 @@ private constructor(
"MB_JUMP_WEST" -> TileBehavior.JUMP_WEST "MB_JUMP_WEST" -> TileBehavior.JUMP_WEST
"MB_JUMP_NORTH" -> TileBehavior.JUMP_NORTH "MB_JUMP_NORTH" -> TileBehavior.JUMP_NORTH
"MB_JUMP_SOUTH" -> TileBehavior.JUMP_SOUTH "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 else -> null
} }
@@ -12,4 +12,32 @@ enum class TileBehavior {
JUMP_WEST, JUMP_WEST,
JUMP_NORTH, JUMP_NORTH,
JUMP_SOUTH, 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
} }
@@ -115,14 +115,14 @@ constructor(
toY = landing.second toY = landing.second
} }
// Some warps trigger from their current tile. // Stairs and arrow warps fire from the tile the player stands on.
val onTileWarp = val standingBehavior = currentMap.tileAt(fromX, fromY)?.behavior
currentMap.warps.find { w -> if (standingBehavior?.warpsWhenWalking == msg.direction) {
w.x == fromX && w.y == fromY && w.facingDirection == msg.direction val onTileWarp = currentMap.warps.find { it.x == fromX && it.y == fromY }
} if (onTileWarp != null) {
if (onTileWarp != null) { warpService.executeWarp(ctx, charId, onTileWarp)
warpService.executeWarp(ctx, charId, onTileWarp) return
return }
} }
// Walking off the edge of a map hands the player to the neighbouring map, if there is one. // Walking off the edge of a map hands the player to the neighbouring map, if there is one.
@@ -161,13 +161,14 @@ constructor(
return return
} }
// Warps fire on the destination tile. // A door only warps when walked into from below, a ladder warps on the step itself.
val warp = val targetBehavior = currentMap.tileAt(toX, toY)?.behavior
currentMap.warps.find { w -> val stepsIntoWarp =
w.x == toX && when (targetBehavior) {
w.y == toY && TileBehavior.DOOR -> msg.direction == Direction.UP
(w.facingDirection == null || w.facingDirection == msg.direction) else -> targetBehavior?.warpsOnStep == true
} }
val warp = if (!stepsIntoWarp) null else currentMap.warps.find { w -> w.x == toX && w.y == toY }
if (warp != null) { if (warp != null) {
log.info { "WARP at ($toX, $toY) facing ${msg.direction}" } 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. // A MAP_DYNAMIC warp (the truck exit) resolves to the destination a script set on the player.
@@ -39,12 +39,13 @@ constructor(
val knownOverride = val knownOverride =
WarpExitRules.getKnownOverride(sourceMap, destMap, warp.targetX, warp.targetY) WarpExitRules.getKnownOverride(sourceMap, destMap, warp.targetX, warp.targetY)
val destBehavior = destMap?.tileAt(warp.targetX, warp.targetY)?.behavior
val warpFacing = val warpFacing =
warp.exitFacing warp.exitFacing
?: knownOverride?.facing ?: knownOverride?.facing
?: WarpExitRules.inferExitFacing( ?: WarpExitRules.inferExitFacing(
destTileBehavior = null, destTileBehavior = destBehavior,
destMap = destMap, destMap = destMap,
destX = warp.targetX, destX = warp.targetX,
destY = warp.targetY, destY = warp.targetY,
@@ -58,7 +59,11 @@ constructor(
val shouldAutoStepOffWarp = val shouldAutoStepOffWarp =
knownOverride?.autoStep knownOverride?.autoStep
?: WarpExitRules.shouldAutoStep(sourceMap = sourceMap, destMap = destMap) ?: WarpExitRules.shouldAutoStep(
sourceMap = sourceMap,
destMap = destMap,
destTileBehavior = destBehavior,
)
if (shouldAutoStepOffWarp && destMap != null) { if (shouldAutoStepOffWarp && destMap != null) {
val destWarp = destMap.warps.find { it.x == offsetX && it.y == offsetY } val destWarp = destMap.warps.find { it.x == offsetX && it.y == offsetY }
if (destWarp != null) { if (destWarp != null) {
@@ -2,6 +2,7 @@ package de.fiereu.openmmo.server.game.world
import de.fiereu.openmmo.common.enums.Direction import de.fiereu.openmmo.common.enums.Direction
import de.fiereu.openmmo.common.enums.MapType import de.fiereu.openmmo.common.enums.MapType
import de.fiereu.openmmo.common.enums.TileBehavior
import de.fiereu.openmmo.maps.MapDef import de.fiereu.openmmo.maps.MapDef
data class WarpExitOverride( data class WarpExitOverride(
@@ -12,7 +13,7 @@ data class WarpExitOverride(
object WarpExitRules { object WarpExitRules {
fun inferExitFacing( fun inferExitFacing(
destTileBehavior: String?, destTileBehavior: TileBehavior?,
destMap: MapDef?, destMap: MapDef?,
destX: Int, destX: Int,
destY: Int, destY: Int,
@@ -20,13 +21,15 @@ object WarpExitRules {
): Direction { ): Direction {
if (destMap == null) return Direction.DOWN if (destMap == null) return Direction.DOWN
// The tile itself is exact, so it wins over the guesses below.
when (destTileBehavior) { when (destTileBehavior) {
"MB_NON_ANIMATED_DOOR", TileBehavior.DOOR,
"MB_ANIMATED_DOOR" -> return Direction.DOWN TileBehavior.NON_ANIMATED_DOOR -> return Direction.DOWN
"MB_NORTH_ARROW_WARP" -> return Direction.DOWN TileBehavior.NORTH_ARROW_WARP -> return Direction.DOWN
"MB_SOUTH_ARROW_WARP" -> return Direction.UP TileBehavior.SOUTH_ARROW_WARP -> return Direction.UP
"MB_WEST_ARROW_WARP" -> return Direction.RIGHT TileBehavior.WEST_ARROW_WARP -> return Direction.RIGHT
"MB_EAST_ARROW_WARP" -> return Direction.LEFT TileBehavior.EAST_ARROW_WARP -> return Direction.LEFT
else -> Unit
} }
val sourceMapType = sourceMap?.mapType val sourceMapType = sourceMap?.mapType
@@ -103,9 +106,20 @@ object WarpExitRules {
fun shouldAutoStep( fun shouldAutoStep(
sourceMap: MapDef?, sourceMap: MapDef?,
destMap: MapDef?, destMap: MapDef?,
destTileBehavior: TileBehavior? = null,
): Boolean { ): Boolean {
if (sourceMap == null || destMap == null) return false 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 = val sourceBuilding =
sourceMap.mapType == MapType.INSIDE || sourceMap.mapType == MapType.SECRET_BASE sourceMap.mapType == MapType.INSIDE || sourceMap.mapType == MapType.SECRET_BASE
@@ -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
}
})