fix(game): keep the player still while a script is running (#102)

This commit is contained in:
Fiereu authored and GitHub committed 2026-08-07 22:01:49 +02:00
1 parent 5f3bc9415e
commit 67faf61498
3 files changed
+226 -7

No files matched your search

@@ -66,7 +66,6 @@ constructor(
val state = ctx.attributes[PLAYER_STATE] ?: return
val charId = state.characterId ?: return
val msg = event.packet
state.facingDirection = msg.direction
log.debug { "Movement: char=$charId from (${msg.x}, ${msg.y}) dir=${msg.direction}" }
val stored = characterStore.getCharacter(charId) ?: return
@@ -92,17 +91,26 @@ constructor(
val atServerTile = msg.x == fromX && msg.y == fromY
if (state.justWarped) {
when {
// Drop every step until the client asks for its player, else one left over from the old map
// can fire a second warp.
return
} else if (!atServerTile) {
log.debug {
"DESYNC: char=$charId claims (${msg.x}, ${msg.y}), server has ($fromX, $fromY), resetting"
}
state.justWarped -> return
// A script owns the player, like the decomp's lockall.
state.inDialog -> {
sendPositionReset(ctx, charId, currentMap, fromX, fromY, state.facingDirection)
return
}
!atServerTile -> {
log.debug {
"DESYNC: char=$charId claims (${msg.x}, ${msg.y}), server has ($fromX, $fromY), resetting"
}
sendPositionReset(ctx, charId, currentMap, fromX, fromY, msg.direction)
return
}
}
// Only once the step is accepted, so a locked player keeps the facing its script left.
state.facingDirection = msg.direction
var toX = fromX + msg.direction.dx
var toY = fromY + msg.direction.dy
@@ -221,6 +229,26 @@ constructor(
val ctx = event.session
val state = ctx.attributes[PLAYER_STATE] ?: return
val charId = state.characterId ?: return
if (state.inDialog) {
// The client already turned itself, so put it back. A face turn is only ever sent about
// other entities, so the correction rides on the same packet a blocked step uses.
val stored = characterStore.getCharacter(charId) ?: return
val map =
mapManager.getMap(
stored.info.positionRegionId,
stored.info.positionBankId,
stored.info.positionMapId,
) ?: return
sendPositionReset(
ctx,
charId,
map,
stored.info.positionX.toInt(),
stored.info.positionY.toInt(),
state.facingDirection,
)
return
}
val msg = event.packet
state.facingDirection = msg.direction
@@ -0,0 +1,107 @@
package de.fiereu.openmmo.server.game.services
import de.fiereu.network.PacketEvent
import de.fiereu.openmmo.common.enums.CharacterGender
import de.fiereu.openmmo.common.enums.Direction
import de.fiereu.openmmo.common.enums.Region
import de.fiereu.openmmo.maps.MapDef
import de.fiereu.openmmo.maps.MapManager
import de.fiereu.openmmo.net.game.packets.FaceDirectionPacket
import de.fiereu.openmmo.net.game.packets.GbaEntityMovePacket
import de.fiereu.openmmo.net.game.packets.MovementPacket
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 de.fiereu.openmmo.server.game.testsupport.movementService
import io.kotest.core.spec.style.FunSpec
import io.kotest.matchers.shouldBe
import java.util.Base64
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.runTest
// Our own empty map, so the test does not depend on real tile data.
private const val BANK = 120
private const val MAP = 120
private const val START_X: Short = 2
private const val START_Y: Short = 2
private fun openMapManager(): MapManager {
val size = 5
val map =
MapDef(
regionId = 1,
bankId = BANK.toByte(),
mapId = MAP.toByte(),
width = size,
height = size,
blockData = Base64.getEncoder().encodeToString(ByteArray(size * size * 2)),
behaviorData = Base64.getEncoder().encodeToString(ByteArray(size * size)),
)
return MapManager().also { it.register(map) }
}
@OptIn(ExperimentalCoroutinesApi::class)
class CutsceneMovementLockTest :
FunSpec({
suspend fun CharacterStore.playerAt(): Pair<FakeSession, Long> {
val charId = createCharacter(1, "May", CharacterGender.FEMALE, Region.HOENN).info.id
updatePosition(charId, START_X, START_Y, BANK.toByte(), MAP.toByte())
val session = FakeSession(characterId = charId, bankId = BANK, mapId = MAP)
session.state().x = START_X
session.state().y = START_Y
return session to charId
}
test("a running script keeps the player on its tile") {
runTest {
val store = CharacterStore(FakeCharacterRepository(), EntityIdService(), backgroundScope)
val (session, charId) = store.playerAt()
session.state().inDialog = true
movementService(store, openMapManager())
.onMovement(
PacketEvent(
MovementPacket(START_X.toInt(), START_Y.toInt(), Direction.UP), session))
val info = store.getCharacter(charId)!!.info
info.positionX shouldBe START_X
info.positionY shouldBe START_Y
val reset = session.sent.filterIsInstance<GbaEntityMovePacket>().single()
(reset.x to reset.y) shouldBe (START_X.toInt() to START_Y.toInt())
reset.direction shouldBe Direction.DOWN
session.state().facingDirection shouldBe Direction.DOWN
}
}
test("a running script blocks turning in place") {
runTest {
val store = CharacterStore(FakeCharacterRepository(), EntityIdService(), backgroundScope)
val (session, charId) = store.playerAt()
session.state().inDialog = true
movementService(store, openMapManager())
.onFaceDirection(PacketEvent(FaceDirectionPacket(Direction.LEFT), session))
session.state().facingDirection shouldBe Direction.DOWN
val reset = session.sent.filterIsInstance<GbaEntityMovePacket>().single()
reset.direction shouldBe Direction.DOWN
(reset.x to reset.y) shouldBe (START_X.toInt() to START_Y.toInt())
}
}
test("the player moves again once the script is over") {
runTest {
val store = CharacterStore(FakeCharacterRepository(), EntityIdService(), backgroundScope)
val (session, charId) = store.playerAt()
session.state().inDialog = false
movementService(store, openMapManager())
.onMovement(
PacketEvent(
MovementPacket(START_X.toInt(), START_Y.toInt(), Direction.UP), session))
store.getCharacter(charId)!!.info.positionY shouldBe (START_Y - 1).toShort()
}
}
})
@@ -0,0 +1,84 @@
package de.fiereu.openmmo.server.game.testsupport
import de.fiereu.openmmo.maps.MapManager
import de.fiereu.openmmo.moves.MoveRegistry
import de.fiereu.openmmo.pokemon.LearnsetRegistry
import de.fiereu.openmmo.pokemon.SpeciesRegistry
import de.fiereu.openmmo.server.game.battle.BattlePacketEmitter
import de.fiereu.openmmo.server.game.battle.BattleRegistry
import de.fiereu.openmmo.server.game.battle.BattleRewards
import de.fiereu.openmmo.server.game.battle.MoveLearner
import de.fiereu.openmmo.server.game.battle.TurnEngine
import de.fiereu.openmmo.server.game.battle.WildMonFactory
import de.fiereu.openmmo.server.game.script.ScriptRegistry
import de.fiereu.openmmo.server.game.script.ScriptRunner
import de.fiereu.openmmo.server.game.services.BattleService
import de.fiereu.openmmo.server.game.services.DialogService
import de.fiereu.openmmo.server.game.services.EncounterService
import de.fiereu.openmmo.server.game.services.MapLoadService
import de.fiereu.openmmo.server.game.services.MapScriptService
import de.fiereu.openmmo.server.game.services.MovementService
import de.fiereu.openmmo.server.game.services.NpcService
import de.fiereu.openmmo.server.game.services.PresenceService
import de.fiereu.openmmo.server.game.services.ScriptMovementService
import de.fiereu.openmmo.server.game.services.ScriptWarpService
import de.fiereu.openmmo.server.game.services.StoryPlayerService
import de.fiereu.openmmo.server.game.services.StoryService
import de.fiereu.openmmo.server.game.services.WarpService
import de.fiereu.openmmo.server.game.storage.CharacterStore
import de.fiereu.openmmo.server.game.storage.EntityIdService
import de.fiereu.openmmo.server.game.world.interest.InterestManager
import de.fiereu.openmmo.server.game.world.interest.PassThroughInterestPolicy
import de.fiereu.openmmo.typechart.TypeChart
/**
* A real [MovementService] with every collaborator built for real, since none of them are worth
* faking. Pass a [mapManager] that already holds the maps the test walks on.
*/
fun movementService(
store: CharacterStore,
mapManager: MapManager = MapManager(),
): MovementService {
val mapLoad = MapLoadService(mapManager)
val interest = InterestManager()
val presence = PresenceService(interest, PassThroughInterestPolicy(), mapLoad, store)
val npcs = NpcService(mapManager, store)
val story = StoryService(store)
val species = SpeciesRegistry()
val moves = MoveRegistry()
val wildMons = WildMonFactory(species, moves, LearnsetRegistry(), EntityIdService())
val battles =
BattleService(
characterStore = store,
battles = BattleRegistry(),
engine = TurnEngine(moves, TypeChart()),
wildMons = wildMons,
emitter = BattlePacketEmitter(interest),
rewards = BattleRewards(),
moveLearner = MoveLearner(LearnsetRegistry(), moves),
interestManager = interest,
speciesRegistry = species,
moveRegistry = moves,
)
val registry = ScriptRegistry(emptyMap())
val runner =
ScriptRunner(
DialogService(),
story,
ScriptMovementService(mapManager, npcs, store),
ScriptWarpService(mapManager, mapLoad, store, presence),
StoryPlayerService(store, wildMons, species, moves),
battles,
store,
)
return MovementService(
WarpService(mapLoad, mapManager, store, presence),
mapLoad,
npcs,
presence,
mapManager,
store,
EncounterService(store, battles),
MapScriptService(registry, runner, story),
)
}