From 41ff99ae4fd7d7dce3ca3af7c21f1d70b8c5df56 Mon Sep 17 00:00:00 2001 From: Fiereu Date: Sun, 26 Jul 2026 14:34:49 +0200 Subject: [PATCH] feat(game): scripted npc and player movement for cutscenes --- .../server/game/script/MovementStep.kt | 19 ++++ .../server/game/script/ScriptContext.kt | 13 +++ .../server/game/script/ScriptRunner.kt | 4 +- .../game/services/ScriptMovementService.kt | 100 ++++++++++++++++++ .../game/script/MapTransitionScriptTest.kt | 7 +- .../services/ScriptMovementServiceTest.kt | 82 ++++++++++++++ 6 files changed, 223 insertions(+), 2 deletions(-) create mode 100644 server.game/src/main/kotlin/de/fiereu/openmmo/server/game/script/MovementStep.kt create mode 100644 server.game/src/main/kotlin/de/fiereu/openmmo/server/game/services/ScriptMovementService.kt create mode 100644 server.game/src/test/kotlin/de/fiereu/openmmo/server/game/services/ScriptMovementServiceTest.kt diff --git a/server.game/src/main/kotlin/de/fiereu/openmmo/server/game/script/MovementStep.kt b/server.game/src/main/kotlin/de/fiereu/openmmo/server/game/script/MovementStep.kt new file mode 100644 index 0000000..4c5bc91 --- /dev/null +++ b/server.game/src/main/kotlin/de/fiereu/openmmo/server/game/script/MovementStep.kt @@ -0,0 +1,19 @@ +package de.fiereu.openmmo.server.game.script + +import de.fiereu.openmmo.common.enums.Direction + +/** + * One step of an applymovement sequence: either a walk one tile in [direction] or a turn in place + * to face [direction]. This is the small, game agnostic vocabulary scripts use, decomp movement + * templates are ported into lists of these. + */ +enum class MovementStep(val direction: Direction, val walks: Boolean) { + WALK_UP(Direction.UP, true), + WALK_DOWN(Direction.DOWN, true), + WALK_LEFT(Direction.LEFT, true), + WALK_RIGHT(Direction.RIGHT, true), + FACE_UP(Direction.UP, false), + FACE_DOWN(Direction.DOWN, false), + FACE_LEFT(Direction.LEFT, false), + FACE_RIGHT(Direction.RIGHT, false), +} diff --git a/server.game/src/main/kotlin/de/fiereu/openmmo/server/game/script/ScriptContext.kt b/server.game/src/main/kotlin/de/fiereu/openmmo/server/game/script/ScriptContext.kt index 303c423..17de0af 100644 --- a/server.game/src/main/kotlin/de/fiereu/openmmo/server/game/script/ScriptContext.kt +++ b/server.game/src/main/kotlin/de/fiereu/openmmo/server/game/script/ScriptContext.kt @@ -3,6 +3,7 @@ package de.fiereu.openmmo.server.game.script import de.fiereu.network.SessionContext import de.fiereu.openmmo.common.dialog.DialogLine import de.fiereu.openmmo.server.game.services.DialogService +import de.fiereu.openmmo.server.game.services.ScriptMovementService import de.fiereu.openmmo.server.game.services.StoryService import de.fiereu.openmmo.server.game.session.PlayerState @@ -15,6 +16,7 @@ internal constructor( val entityId: Long, private val dialog: DialogService, private val story: StoryService, + private val movement: ScriptMovementService, ) { private val characterId: Long? get() = state.characterId @@ -43,6 +45,17 @@ internal constructor( characterId?.let { story.setVar(it, key, value) } } + /** + * Walk the map npc with decomp local id [localId] (its entityIdx) through [steps] and wait for + * the whole path to finish. This is applymovement plus waitmovement for an npc. + */ + suspend fun moveNpc(localId: Int, vararg steps: MovementStep) = + movement.moveNpc(session, state, localId, steps.toList()) + + /** Walk the player's own avatar through [steps] and wait for it to finish. */ + suspend fun moveSelf(vararg steps: MovementStep) = + movement.moveSelf(session, state, steps.toList()) + private companion object { // Sign boxes have no speaker, npc boxes point at the entity. const val SIGN = 3 diff --git a/server.game/src/main/kotlin/de/fiereu/openmmo/server/game/script/ScriptRunner.kt b/server.game/src/main/kotlin/de/fiereu/openmmo/server/game/script/ScriptRunner.kt index c772b71..1c5b117 100644 --- a/server.game/src/main/kotlin/de/fiereu/openmmo/server/game/script/ScriptRunner.kt +++ b/server.game/src/main/kotlin/de/fiereu/openmmo/server/game/script/ScriptRunner.kt @@ -2,6 +2,7 @@ package de.fiereu.openmmo.server.game.script import de.fiereu.network.SessionContext import de.fiereu.openmmo.server.game.services.DialogService +import de.fiereu.openmmo.server.game.services.ScriptMovementService import de.fiereu.openmmo.server.game.services.StoryService import de.fiereu.openmmo.server.game.session.PlayerState import de.fiereu.openmmo.server.game.session.SCRIPT_SCOPE @@ -28,6 +29,7 @@ class ScriptRunner constructor( private val dialogService: DialogService, private val storyService: StoryService, + private val movementService: ScriptMovementService, ) { fun run(session: SessionContext, state: PlayerState, script: Script, entityId: Long) { state.inDialog = true @@ -35,7 +37,7 @@ constructor( session.attributes.getOrPut(SCRIPT_SCOPE) { CoroutineScope(SupervisorJob() + Dispatchers.Default) } - val ctx = ScriptContext(session, state, entityId, dialogService, storyService) + val ctx = ScriptContext(session, state, entityId, dialogService, storyService, movementService) scope.launch { try { script.run(ctx) diff --git a/server.game/src/main/kotlin/de/fiereu/openmmo/server/game/services/ScriptMovementService.kt b/server.game/src/main/kotlin/de/fiereu/openmmo/server/game/services/ScriptMovementService.kt new file mode 100644 index 0000000..fb4e8fd --- /dev/null +++ b/server.game/src/main/kotlin/de/fiereu/openmmo/server/game/services/ScriptMovementService.kt @@ -0,0 +1,100 @@ +package de.fiereu.openmmo.server.game.services + +import de.fiereu.network.SessionContext +import de.fiereu.openmmo.common.enums.Direction +import de.fiereu.openmmo.maps.MapManager +import de.fiereu.openmmo.net.game.packets.EntityFaceTurnPacket +import de.fiereu.openmmo.net.game.packets.EntityMovePacket +import de.fiereu.openmmo.server.game.script.MovementStep +import de.fiereu.openmmo.server.game.session.PlayerState +import de.fiereu.openmmo.server.game.storage.CharacterStore +import javax.inject.Inject +import javax.inject.Singleton +import kotlinx.coroutines.delay + +/** + * Drives scripted overworld movement (the decomp applymovement/waitmovement). Steps play one tile + * at a time with a short delay between them, so the call only returns once the whole path is done, + * which is exactly waitmovement. + * + * Story cutscenes are per player, so the movement is sent to the acting player only and is not + * broadcast to other players who happen to share the map. It reuses [EntityMovePacket], the same + * packet the server already uses to show other entities walking. + */ +@Singleton +class ScriptMovementService +@Inject +constructor( + private val mapManager: MapManager, + private val npcService: NpcService, + private val characterStore: CharacterStore, +) { + data class Pose(val x: Int, val y: Int, val facing: Direction) + + /** Walk a map npc (its decomp local id, that is its entityIdx) through [steps] for the player. */ + suspend fun moveNpc( + session: SessionContext, + state: PlayerState, + localId: Int, + steps: List, + ) { + val charId = state.characterId ?: return + val info = characterStore.getCharacter(charId)?.info ?: return + val map = + mapManager.getMap(info.positionRegionId, info.positionBankId, info.positionMapId) ?: return + val npc = map.npcs.firstOrNull { it.entityIdx == localId } ?: return + val entityId = + npcService.getNpcEntityId(info.positionBankId.toInt(), info.positionMapId.toInt(), localId) + ?: return + drive(session, entityId, Pose(npc.x, npc.y, npc.facing), steps) + } + + /** Walk the player's own avatar through [steps] and commit the final tile as authoritative. */ + suspend fun moveSelf( + session: SessionContext, + state: PlayerState, + steps: List, + ) { + val charId = state.characterId ?: return + val info = characterStore.getCharacter(charId)?.info ?: return + val start = Pose(info.positionX.toInt(), info.positionY.toInt(), state.facingDirection) + val end = drive(session, info.id, start, steps) + // Keep the server position in step with where the cutscene left the player, otherwise the next + // move packet from the client looks like a desync and the player is snapped back. + characterStore.updatePosition(charId, end.x.toShort(), end.y.toShort()) + state.x = end.x.toShort() + state.y = end.y.toShort() + state.facingDirection = end.facing + } + + /** Sends one packet per step from [start] and waits between them. Returns the final pose. */ + suspend fun drive( + session: SessionContext, + entityId: Long, + start: Pose, + steps: List, + ): Pose { + var pose = start + for (step in steps) { + pose = + if (step.walks) { + val nx = pose.x + step.direction.dx + val ny = pose.y + step.direction.dy + session.send(EntityMovePacket(entityId, nx, ny, step.direction)) + delay(WALK_STEP_MS) + Pose(nx, ny, step.direction) + } else { + session.send(EntityFaceTurnPacket(entityId, step.direction.ordinal.toByte())) + delay(FACE_STEP_MS) + pose.copy(facing = step.direction) + } + } + return pose + } + + private companion object { + // Rough GBA step timings, tune against the client if the animation and server drift apart. + const val WALK_STEP_MS = 250L + const val FACE_STEP_MS = 120L + } +} diff --git a/server.game/src/test/kotlin/de/fiereu/openmmo/server/game/script/MapTransitionScriptTest.kt b/server.game/src/test/kotlin/de/fiereu/openmmo/server/game/script/MapTransitionScriptTest.kt index 6065ce9..3e15989 100644 --- a/server.game/src/test/kotlin/de/fiereu/openmmo/server/game/script/MapTransitionScriptTest.kt +++ b/server.game/src/test/kotlin/de/fiereu/openmmo/server/game/script/MapTransitionScriptTest.kt @@ -1,7 +1,10 @@ package de.fiereu.openmmo.server.game.script +import de.fiereu.openmmo.maps.MapManager import de.fiereu.openmmo.server.game.script.generated.hoenn.LittlerootTown_OnTransition import de.fiereu.openmmo.server.game.services.DialogService +import de.fiereu.openmmo.server.game.services.NpcService +import de.fiereu.openmmo.server.game.services.ScriptMovementService import de.fiereu.openmmo.server.game.services.StoryService import de.fiereu.openmmo.server.game.session.PLAYER_STATE import de.fiereu.openmmo.server.game.storage.CharacterStore @@ -25,7 +28,9 @@ class MapTransitionScriptTest : val session = FakeSession(characterId = charId) val state = session.attributes[PLAYER_STATE]!! - val ctx = ScriptContext(session, state, entityId = -1, DialogService(), story) + val mapManager = MapManager() + val movement = ScriptMovementService(mapManager, NpcService(mapManager), store) + val ctx = ScriptContext(session, state, entityId = -1, DialogService(), story, movement) LittlerootTown_OnTransition.run(ctx) diff --git a/server.game/src/test/kotlin/de/fiereu/openmmo/server/game/services/ScriptMovementServiceTest.kt b/server.game/src/test/kotlin/de/fiereu/openmmo/server/game/services/ScriptMovementServiceTest.kt new file mode 100644 index 0000000..38fa814 --- /dev/null +++ b/server.game/src/test/kotlin/de/fiereu/openmmo/server/game/services/ScriptMovementServiceTest.kt @@ -0,0 +1,82 @@ +package de.fiereu.openmmo.server.game.services + +import de.fiereu.openmmo.common.enums.Direction +import de.fiereu.openmmo.maps.MapManager +import de.fiereu.openmmo.net.game.packets.EntityFaceTurnPacket +import de.fiereu.openmmo.net.game.packets.EntityMovePacket +import de.fiereu.openmmo.server.game.script.MovementStep +import de.fiereu.openmmo.server.game.storage.CharacterStore +import de.fiereu.openmmo.server.game.storage.EntityIdService +import de.fiereu.openmmo.server.game.testsupport.FakeCharacterRepository +import de.fiereu.openmmo.server.game.testsupport.FakeSession +import io.kotest.core.spec.style.FunSpec +import io.kotest.matchers.shouldBe +import kotlinx.coroutines.ExperimentalCoroutinesApi +import kotlinx.coroutines.test.runTest + +@OptIn(ExperimentalCoroutinesApi::class) +class ScriptMovementServiceTest : + FunSpec({ + fun service(store: CharacterStore): ScriptMovementService { + val mapManager = MapManager() + return ScriptMovementService(mapManager, NpcService(mapManager), store) + } + + test("drive walks one tile per step and returns the final pose") { + runTest { + val store = CharacterStore(FakeCharacterRepository(), EntityIdService(), backgroundScope) + val session = FakeSession() + val start = ScriptMovementService.Pose(5, 5, Direction.DOWN) + + val end = + service(store) + .drive( + session, + entityId = 42L, + start = start, + steps = + listOf( + MovementStep.WALK_UP, MovementStep.WALK_UP, MovementStep.WALK_LEFT), + ) + + end shouldBe ScriptMovementService.Pose(4, 3, Direction.LEFT) + session.sent shouldBe + listOf( + EntityMovePacket(42L, 5, 4, Direction.UP), + EntityMovePacket(42L, 5, 3, Direction.UP), + EntityMovePacket(42L, 4, 3, Direction.LEFT), + ) + } + } + + test("a face step turns in place without moving") { + runTest { + val store = CharacterStore(FakeCharacterRepository(), EntityIdService(), backgroundScope) + val session = FakeSession() + val start = ScriptMovementService.Pose(8, 2, Direction.DOWN) + + val end = service(store).drive(session, 7L, start, listOf(MovementStep.FACE_RIGHT)) + + end shouldBe ScriptMovementService.Pose(8, 2, Direction.RIGHT) + session.sent shouldBe listOf(EntityFaceTurnPacket(7L, Direction.RIGHT.ordinal.toByte())) + } + } + + test("moveSelf commits the final tile to the character store") { + runTest { + val store = CharacterStore(FakeCharacterRepository(), EntityIdService(), backgroundScope) + val created = store.createCharacter(1, "Ash") + val charId = created.info.id + val session = FakeSession(characterId = charId) + val state = session.attributes[de.fiereu.openmmo.server.game.session.PLAYER_STATE]!! + val startX = created.info.positionX.toInt() + val startY = created.info.positionY.toInt() + + service(store) + .moveSelf(session, state, listOf(MovementStep.WALK_DOWN, MovementStep.WALK_DOWN)) + + store.getCharacter(charId)!!.info.positionY.toInt() shouldBe startY + 2 + store.getCharacter(charId)!!.info.positionX.toInt() shouldBe startX + } + } + })