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 b082da2..edc447a 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 @@ -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, 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 2642c64..f33f8cf 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 @@ -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. diff --git a/server.game/src/test/kotlin/de/fiereu/openmmo/server/game/services/WarpArrivalTest.kt b/server.game/src/test/kotlin/de/fiereu/openmmo/server/game/services/WarpArrivalTest.kt index f28cd87..f93232e 100644 --- a/server.game/src/test/kotlin/de/fiereu/openmmo/server/game/services/WarpArrivalTest.kt +++ b/server.game/src/test/kotlin/de/fiereu/openmmo/server/game/services/WarpArrivalTest.kt @@ -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)