fix(game): resolve dynamic warps for every warp trigger (#104)

This commit is contained in:
Fiereu authored and GitHub committed 2026-08-07 21:59:06 +02:00
1 parent b615b65334
commit 3b90027d18
3 files changed
+79 -29

No files matched your search

@@ -6,7 +6,6 @@ import de.fiereu.openmmo.common.enums.Direction
import de.fiereu.openmmo.common.enums.TileBehavior
import de.fiereu.openmmo.maps.MapDef
import de.fiereu.openmmo.maps.MapManager
import de.fiereu.openmmo.maps.WarpTile
import de.fiereu.openmmo.net.game.packets.EntityFaceTurnPacket
import de.fiereu.openmmo.net.game.packets.FaceDirectionPacket
import de.fiereu.openmmo.net.game.packets.GbaEntityMovePacket
@@ -14,7 +13,6 @@ import de.fiereu.openmmo.net.game.packets.MapData
import de.fiereu.openmmo.net.game.packets.MovementPacket
import de.fiereu.openmmo.server.game.session.PLAYER_STATE
import de.fiereu.openmmo.server.game.storage.CharacterStore
import de.fiereu.openmmo.server.game.storage.StoredCharacter
import io.github.oshai.kotlinlogging.KotlinLogging
import javax.inject.Inject
import javax.inject.Singleton
@@ -163,11 +161,7 @@ constructor(
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.
val resolved =
if (warp.dynamic) resolveDynamicWarp(characterStore.getCharacter(charId) ?: stored)
else warp
if (resolved != null) warpService.executeWarp(ctx, charId, resolved)
warpService.executeWarp(ctx, charId, warp)
return
}
@@ -193,27 +187,6 @@ constructor(
}
}
/**
* Builds the real warp for a MAP_DYNAMIC tile from the destination a script set on the player.
*/
private fun resolveDynamicWarp(stored: StoredCharacter): WarpTile? {
val d = stored.info.dynamicWarp
if (d == null) {
log.warn { "Dynamic warp tile stepped on but no dynamic warp is set for ${stored.info.id}" }
return null
}
return WarpTile(
x = 0,
y = 0,
targetRegionId = d.regionId,
targetBankId = d.bankId,
targetMapId = d.mapId,
targetX = d.x.toInt(),
targetY = d.y.toInt(),
exitFacing = d.facing,
)
}
/** Snap the client back to the position the server considers authoritative. */
private fun sendPositionReset(
ctx: SessionContext,
@@ -13,6 +13,7 @@ import de.fiereu.openmmo.server.game.session.PLAYER_STATE
import de.fiereu.openmmo.server.game.session.PlayerState
import de.fiereu.openmmo.server.game.session.SCRIPT_SCOPE
import de.fiereu.openmmo.server.game.storage.CharacterStore
import de.fiereu.openmmo.server.game.storage.StoredCharacter
import de.fiereu.openmmo.server.game.world.WarpExitRules
import io.github.oshai.kotlinlogging.KotlinLogging
import javax.inject.Inject
@@ -40,10 +41,12 @@ constructor(
private val presenceService: PresenceService,
) {
fun executeWarp(ctx: SessionContext, charId: Long, warp: WarpTile) {
fun executeWarp(ctx: SessionContext, charId: Long, tile: WarpTile) {
val state = ctx.attributes[PLAYER_STATE]
val stored = characterStore.getCharacter(charId) ?: return
val warp = if (!tile.dynamic) tile else resolveDynamicWarp(stored) ?: return
// Check the map first. Moving the player onto one we do not have would strand it there.
val destMap = mapManager.getMap(warp.targetRegionId, warp.targetBankId, warp.targetMapId)
if (destMap == null) {
@@ -149,6 +152,28 @@ constructor(
log.info { "Player $charId warped to bank=${warp.targetBankId} map=${warp.targetMapId}" }
}
/**
* Builds the real warp for a MAP_DYNAMIC tile, whose target fields are placeholders, from the
* destination a script set on the player.
*/
private fun resolveDynamicWarp(stored: StoredCharacter): WarpTile? {
val d = stored.info.dynamicWarp
if (d == null) {
log.warn { "Dynamic warp tile stepped on but no dynamic warp is set for ${stored.info.id}" }
return null
}
return WarpTile(
x = 0,
y = 0,
targetRegionId = d.regionId,
targetBankId = d.bankId,
targetMapId = d.mapId,
targetX = d.x.toInt(),
targetY = d.y.toInt(),
exitFacing = d.facing,
)
}
/**
* Releases the player if the client never asks for it. Without this a lost RequestPlayer leaves
* the player faded out and unable to move for the rest of the session.
@@ -58,6 +58,19 @@ private fun warpTo(bankId: Byte, mapId: Byte) =
targetY = 8,
)
// What codegen emits for a MAP_DYNAMIC tile. Every target is a placeholder.
private fun dynamicWarpTile() =
WarpTile(
x = 4,
y = 2,
targetRegionId = 0,
targetBankId = 0,
targetMapId = 0,
targetX = 0,
targetY = 0,
dynamic = true,
)
@OptIn(ExperimentalCoroutinesApi::class)
class WarpArrivalTest :
FunSpec({
@@ -180,6 +193,45 @@ class WarpArrivalTest :
}
}
test("a dynamic warp goes where the script pointed it, not to its placeholder target") {
runTest {
val store = CharacterStore(FakeCharacterRepository(), EntityIdService(), backgroundScope)
val charId = store.player()
val session =
FakeSession(characterId = charId, bankId = 75, mapId = 40).useScope(backgroundScope)
val f = Fixture(store)
val target = store.getCharacter(charId)!!.info.dynamicWarp!!
f.warps.executeWarp(session, charId, dynamicWarpTile())
runCurrent()
val info = store.getCharacter(charId)!!.info
info.positionRegionId shouldBe target.regionId
info.positionBankId shouldBe target.bankId
info.positionMapId shouldBe target.mapId
info.positionX shouldBe target.x
info.positionY shouldBe target.y
session.state().facingDirection shouldBe target.facing
}
}
test("a dynamic warp with no destination set leaves the player where it was") {
runTest {
val store = CharacterStore(FakeCharacterRepository(), EntityIdService(), backgroundScope)
val charId = store.createCharacter(1, "Red", CharacterGender.MALE, Region.KANTO).info.id
val session = FakeSession(characterId = charId).useScope(backgroundScope)
val f = Fixture(store)
val before = store.getCharacter(charId)!!.info
f.warps.executeWarp(session, charId, dynamicWarpTile())
advanceUntilIdle()
session.state().justWarped shouldBe false
store.getCharacter(charId)!!.info shouldBe before
session.sent shouldBe emptyList()
}
}
test("a warp for an unknown character does not gate movement") {
runTest {
val store = CharacterStore(FakeCharacterRepository(), EntityIdService(), backgroundScope)