fix(game): stop the player from stopping when walking to another map (#98)

* fix(game): stop moving the player when they cross a map connection

* fix(game): only send map data the client does not already have
This commit is contained in:
Fiereu authored and GitHub committed 2026-08-07 14:18:14 +02:00
1 parent 14fbc05bab
commit d4940f85cf
6 files changed
+27 -16

No files matched your search

@@ -294,6 +294,7 @@ constructor(
"Placing '${info.name}' at ${info.positionRegionId}:${info.positionBankId}:" +
"${info.positionMapId} (${info.positionX}, ${info.positionY})"
}
mapLoadService.resetClientCache(ctx, map)
ctx.send(mapManager.createLoadMapPacket(map, reloadPlayer = true, deleteCache = true))
mapLoadService.preloadConnectedMaps(ctx, map)
} else {
@@ -10,6 +10,8 @@ import de.fiereu.openmmo.maps.MapManager
import de.fiereu.openmmo.net.game.codecs.SkinSet
import de.fiereu.openmmo.net.game.packets.LoadEntityPacket
import de.fiereu.openmmo.net.game.packets.MapData
import de.fiereu.openmmo.server.game.session.PLAYER_STATE
import de.fiereu.openmmo.server.game.session.mapCacheKey
import javax.inject.Inject
import javax.inject.Singleton
@@ -43,21 +45,32 @@ constructor(
)
}
/**
* Forget what the client has cached. Call this alongside a LoadMap that carries deleteCache,
* since the client throws its own cache away when it sees that flag.
*/
fun resetClientCache(ctx: SessionContext, map: MapDef) {
val state = ctx.attributes[PLAYER_STATE] ?: return
state.loadedMaps.clear()
state.loadedMaps.add(mapCacheKey(map.regionId.toInt(), map.bankId.toInt(), map.mapId.toInt()))
}
fun preloadConnectedMaps(
ctx: SessionContext,
map: MapDef,
depth: Int = 2,
reloadPlayer: Boolean = false,
) {
val loaded = mutableSetOf<String>()
loaded.add("${map.bankId}:${map.mapId}")
val loaded = ctx.attributes[PLAYER_STATE]?.loadedMaps ?: mutableSetOf()
val regionId = map.regionId.toInt()
loaded.add(mapCacheKey(regionId, map.bankId.toInt(), map.mapId.toInt()))
fun preload(connections: List<MapData.GbaConnection>, remaining: Int) {
if (remaining <= 0) return
for (conn in connections) {
val key = "${conn.targetBank}:${conn.targetMap}"
val key = mapCacheKey(regionId, conn.targetBank, conn.targetMap)
if (!loaded.add(key)) continue
// Connections stay inside one region.
val connected = mapManager.getMap(map.regionId.toInt(), conn.targetBank, conn.targetMap)
val connected = mapManager.getMap(regionId, conn.targetBank, conn.targetMap)
if (connected != null) {
ctx.send(
mapManager.createLoadMapPacket(
@@ -149,15 +149,7 @@ constructor(
Direction.DOWN -> 0
else -> (fromY - connection.unknown).coerceIn(0, targetMap.height - 1)
}
edgeTransition(
ctx,
charId,
currentMap.regionId,
connection,
entryX.toByte(),
entryY.toByte(),
msg.direction,
)
edgeTransition(ctx, charId, currentMap.regionId, connection, entryX.toByte(), entryY.toByte())
return
}
@@ -278,7 +270,6 @@ constructor(
connection: MapData.GbaConnection,
targetX: Byte,
targetY: Byte,
direction: Direction,
) {
val targetBank = connection.targetBank.toByte()
val targetMap = connection.targetMap.toByte()
@@ -304,8 +295,6 @@ constructor(
mapLoadService.preloadConnectedMaps(ctx, map, depth = 1)
npcService.spawnNpcsForMap(ctx, targetBank.toInt(), targetMap.toInt(), regionId.toInt())
ctx.send(gbaMovePacket(charId, map, targetX.toInt(), targetY.toInt(), direction))
if (state != null) mapScriptService.onMapEnter(ctx, state, map)
log.info { "Player $charId edge-transitioned to bank=$targetBank map=$targetMap" }
@@ -53,6 +53,7 @@ constructor(
session.send(MapTransitionPacket())
session.send(RenderScreenPacket(false))
session.send(MapTransitionAckPacket(2))
mapLoadService.resetClientCache(session, map)
session.send(mapManager.createLoadMapPacket(map, reloadPlayer = true, deleteCache = true))
mapLoadService.preloadConnectedMaps(session, map, depth = 1, reloadPlayer = true)
session.send(mapLoadService.createLoadEntity(info, destination.facing))
@@ -116,6 +116,7 @@ constructor(
ctx.send(MapTransitionAckPacket(2))
if (destMap != null) {
mapLoadService.resetClientCache(ctx, destMap)
ctx.send(mapManager.createLoadMapPacket(destMap, reloadPlayer = true, deleteCache = true))
mapLoadService.preloadConnectedMaps(ctx, destMap, depth = 1, reloadPlayer = true)
} else {
@@ -17,4 +17,10 @@ data class PlayerState(
var y: Short = 2,
/** Trusts one source tile after scripted movement. */
var acceptNextMoveSource: Boolean = false,
/** Maps the client already holds. A warp sends deleteCache, which empties this. */
val loadedMaps: MutableSet<Int> = mutableSetOf(),
)
/** Packs a map address into one key for [PlayerState.loadedMaps]. */
fun mapCacheKey(regionId: Int, bankId: Int, mapId: Int): Int =
(regionId shl 16) or (bankId shl 8) or mapId