feat(game): add rudimentary edge transition preloading (Littleroot to Oldale)

This commit is contained in:
LananaHWP authored and Fiereu committed 2026-05-26 16:15:29 +02:00
1 parent 9486b83cf4
commit 80db48878c
1 file changed
+168 -142
@@ -17,6 +17,7 @@ import de.fiereu.openmmo.protocols.game.packets.InteractiveResponsePacket
import de.fiereu.openmmo.protocols.game.packets.JoinGamePacket import de.fiereu.openmmo.protocols.game.packets.JoinGamePacket
import de.fiereu.openmmo.protocols.game.packets.KeepAlivePacket import de.fiereu.openmmo.protocols.game.packets.KeepAlivePacket
import de.fiereu.openmmo.protocols.game.packets.LoadEntityPacket import de.fiereu.openmmo.protocols.game.packets.LoadEntityPacket
import de.fiereu.openmmo.protocols.game.packets.MapData
import de.fiereu.openmmo.protocols.game.packets.MapTransitionAckPacket import de.fiereu.openmmo.protocols.game.packets.MapTransitionAckPacket
import de.fiereu.openmmo.protocols.game.packets.MapTransitionPacket import de.fiereu.openmmo.protocols.game.packets.MapTransitionPacket
import de.fiereu.openmmo.protocols.game.packets.MovementPacket import de.fiereu.openmmo.protocols.game.packets.MovementPacket
@@ -326,7 +327,20 @@ class GameProtocolHandler(
log.info { log.info {
"Sending LoadMap for ${info.positionRegionId}:${info.positionBankId}:${info.positionMapId}" "Sending LoadMap for ${info.positionRegionId}:${info.positionBankId}:${info.positionMapId}"
} }
// Login uses both flags for initial map load (interior start)
event.respond(MapManager.createLoadMapPacket(map, reloadPlayer = true, deleteCache = true)) event.respond(MapManager.createLoadMapPacket(map, reloadPlayer = true, deleteCache = true))
// Preload connected maps
for (conn in map.connections) {
val connectedMap = MapManager.getMap(1, conn.targetBank.toByte(), conn.targetMap.toByte())
if (connectedMap != null) {
event.ctx
.channel()
.writeAndFlush(
MapManager.createLoadMapPacket(
connectedMap, reloadPlayer = false, deleteCache = true))
log.info { "Preloaded connected map ${conn.targetBank}:${conn.targetMap}" }
}
}
} else { } else {
log.warn { log.warn {
"Map not found for position ${info.positionRegionId}:${info.positionBankId}:${info.positionMapId}" "Map not found for position ${info.positionRegionId}:${info.positionBankId}:${info.positionMapId}"
@@ -368,10 +382,12 @@ class GameProtocolHandler(
} }
val info = stored.info val info = stored.info
log.info { "Sending LoadEntity + 0x90 + RenderScreen for character '${info.name}'" } log.info { "Sending LoadEntity + 0x90 for character '${info.name}'" }
val facing = session.facingDirection val facing = session.facingDirection
val loadEntity = createLoadEntity(info, facing) val loadEntity = createLoadEntity(info, facing)
sendEntityWith90(event.ctx, loadEntity) // Send LoadEntity + 0x90 first (RenderScreen(true) must come last)
event.ctx.channel().writeAndFlush(loadEntity)
send90(event.ctx, loadEntity.entityId, loadEntity.skin)
spawnNpcsForMap(event.ctx, info.positionBankId.toInt(), info.positionMapId.toInt()) spawnNpcsForMap(event.ctx, info.positionBankId.toInt(), info.positionMapId.toInt())
// Multiplayer: broadcast LoadEntity of self to other players in same map, // Multiplayer: broadcast LoadEntity of self to other players in same map,
@@ -394,20 +410,22 @@ class GameProtocolHandler(
for (other in others) { for (other in others) {
val otherStored = CharacterStore.getCharacter(other.characterId ?: continue) ?: continue val otherStored = CharacterStore.getCharacter(other.characterId ?: continue) ?: continue
val otherParty = otherStored.pokemon val otherParty = otherStored.pokemon
event.respond( event.ctx
LoadEntityPacket( .channel()
entityId = other.characterId!!, .write(
skin = SkinSet(), LoadEntityPacket(
name = otherStored.info.name, entityId = other.characterId!!,
regionId = other.regionId, skin = SkinSet(),
bankId = other.bankId, name = otherStored.info.name,
mapId = other.mapId, regionId = other.regionId,
x = other.x.toInt(), bankId = other.bankId,
y = other.y.toInt(), mapId = other.mapId,
facing = other.facingDirection, x = other.x.toInt(),
status = de.fiereu.openmmo.common.enums.EntityStatus.NONE, y = other.y.toInt(),
hasFollower = otherParty.isNotEmpty(), facing = other.facingDirection,
followerDexId = (otherParty.firstOrNull()?.dexId ?: 0).toShort())) status = de.fiereu.openmmo.common.enums.EntityStatus.NONE,
hasFollower = otherParty.isNotEmpty(),
followerDexId = (otherParty.firstOrNull()?.dexId ?: 0).toShort()))
} }
// Broadcast LoadEntity of new player to all existing players in this map // Broadcast LoadEntity of new player to all existing players in this map
@@ -428,6 +446,8 @@ class GameProtocolHandler(
for (other in others) { for (other in others) {
other.channel.writeAndFlush(selfEntity) other.channel.writeAndFlush(selfEntity)
} }
// RenderScreen(true) must be the last packet — real server sends it after all entities/NPCs
event.ctx.channel().writeAndFlush(RenderScreenPacket(true))
log.info { "Player $charId spawned in bank=$bankId map=$mapId; ${others.size} others present" } log.info { "Player $charId spawned in bank=$bankId map=$mapId; ${others.size} others present" }
} }
@@ -547,8 +567,9 @@ class GameProtocolHandler(
} }
// Real server warp sequence (from packets.db): // Real server warp sequence (from packets.db):
// MapTransition → RenderScreen(false) → LoadEntity → MapTransitionAck → LoadMap → LoadEntity → // MapTransition → RenderScreen(false) → LoadEntity(0x05) → MapTransitionAck → LoadMap(×N) →
// 0x90 // [client sends RequestPlayer(0x05)] → LoadEntity + 0x90 + NpcSpawn + others' EntityInfo +
// RenderScreen(true)
ctx.channel().writeAndFlush(MapTransitionPacket()) ctx.channel().writeAndFlush(MapTransitionPacket())
ctx.channel().writeAndFlush(RenderScreenPacket(false)) ctx.channel().writeAndFlush(RenderScreenPacket(false))
ctx.channel().writeAndFlush(createLoadEntity(newInfo)) ctx.channel().writeAndFlush(createLoadEntity(newInfo))
@@ -556,71 +577,75 @@ class GameProtocolHandler(
val map = MapManager.getMap(warp.targetRegionId, warp.targetBankId, warp.targetMapId) val map = MapManager.getMap(warp.targetRegionId, warp.targetBankId, warp.targetMapId)
if (map != null) { if (map != null) {
// Interior maps (no connections): flags=0x03 (both). Exterior: flags=0x01 (deleteCache only)
val isInterior = map.connections.isEmpty()
ctx.channel() ctx.channel()
.writeAndFlush( .writeAndFlush(
MapManager.createLoadMapPacket(map, reloadPlayer = true, deleteCache = true)) MapManager.createLoadMapPacket(map, reloadPlayer = isInterior, deleteCache = true))
// Preload connected maps — real server sends these AFTER MapTransitionAck with
// flags=02
// (reloadPlayer=true, deleteCache=false)
for (conn in map.connections) {
val connectedMap = MapManager.getMap(1, conn.targetBank.toByte(), conn.targetMap.toByte())
if (connectedMap != null) {
ctx.channel()
.writeAndFlush(
MapManager.createLoadMapPacket(
connectedMap, reloadPlayer = true, deleteCache = false))
}
}
} else { } else {
log.warn { log.warn {
"Map not found for warp target ${warp.targetRegionId}:${warp.targetBankId}:${warp.targetMapId}" "Map not found for warp target ${warp.targetRegionId}:${warp.targetBankId}:${warp.targetMapId}"
} }
} }
// Post-LoadMap entity + skin (no RenderScreen(true) — client shows on its own) log.info { "Player $charId warped to bank=${warp.targetBankId} map=${warp.targetMapId}" }
ctx.channel().writeAndFlush(createLoadEntity(newInfo)) }
send90(ctx, newInfo.id, SkinSet())
spawnNpcsForMap(ctx, warp.targetBankId.toInt(), warp.targetMapId.toInt())
// Multiplayer: broadcast LoadEntity to players on the new map private fun edgeTransition(
val newRegionId = warp.targetRegionId.toInt() ctx: ChannelHandlerContext,
val newBankId = warp.targetBankId.toInt() charId: Long,
val newMapId = warp.targetMapId.toInt() connection: MapData.GbaConnection,
val stored2 = CharacterStore.getCharacter(charId) targetX: Byte,
val party = stored2?.pokemon ?: emptyList() targetY: Byte,
val selfEntity = direction: Direction
LoadEntityPacket( ) {
entityId = charId, val targetBank = connection.targetBank.toByte()
skin = SkinSet(), val targetMap = connection.targetMap.toByte()
name = stored2?.info?.name ?: "Player", val map = MapManager.getMap(1, targetBank, targetMap) ?: return
regionId = newRegionId, // Update session + store to new map and position
bankId = newBankId, val session = SessionManager.getSessionByCharacterId(charId)
mapId = newMapId, session?.bankId = targetBank.toInt()
x = warp.targetX.toInt(), session?.mapId = targetMap.toInt()
y = warp.targetY.toInt(), session?.x = targetX.toShort()
facing = session?.facingDirection ?: Direction.DOWN, session?.y = targetY.toShort()
status = de.fiereu.openmmo.common.enums.EntityStatus.NONE, CharacterStore.updatePosition(charId, targetX.toShort(), targetY.toShort())
hasFollower = party.isNotEmpty(), // Real server sends LoadMap (flags=00) during edge transition (capture ID 108)
followerDexId = (party.firstOrNull()?.dexId ?: 0).toShort()) ctx.channel()
val newMapPlayers = SessionManager.getOthersInMap(charId, newRegionId, newBankId, newMapId) .writeAndFlush(
for (other in newMapPlayers) { MapManager.createLoadMapPacket(map, reloadPlayer = false, deleteCache = false))
other.channel.write(selfEntity) // Preload connected maps of the target map (deep preload — real server sends these
} // at ID 108)
for (other in newMapPlayers) { for (conn in map.connections) {
other.channel.flush() val nextMap = MapManager.getMap(1, conn.targetBank.toByte(), conn.targetMap.toByte())
} if (nextMap != null) {
// Send LoadEntity of new-map players to the warping player (batch without flushing) ctx.channel()
for (other in newMapPlayers) { .writeAndFlush(
val otherStored = CharacterStore.getCharacter(other.characterId ?: continue) ?: continue MapManager.createLoadMapPacket(nextMap, reloadPlayer = false, deleteCache = false))
val otherParty = otherStored.pokemon }
ctx.channel()
.write(
LoadEntityPacket(
entityId = other.characterId!!,
skin = SkinSet(),
name = otherStored.info.name,
regionId = other.regionId,
bankId = other.bankId,
mapId = other.mapId,
x = other.x.toInt(),
y = other.y.toInt(),
facing = other.facingDirection,
status = de.fiereu.openmmo.common.enums.EntityStatus.NONE,
hasFollower = otherParty.isNotEmpty(),
followerDexId = (otherParty.firstOrNull()?.dexId ?: 0).toShort()))
}
ctx.channel().flush()
log.info {
"Player $charId warped to bank=$newBankId map=$newMapId; ${newMapPlayers.size} others present"
} }
// Spawn NPCs of the new map
spawnNpcsForMap(ctx, targetBank.toInt(), targetMap.toInt())
// Send EntityMove to the moving player (movement confirmation) and broadcast to
// others
val seq = sequenceCounter.incrementAndGet().toByte()
val movePkt =
EntityMovePacket(
entityId = charId, x = targetX, y = targetY, direction = direction, seq = seq)
ctx.channel().writeAndFlush(movePkt)
broadcastExcept(ctx.channel(), movePkt)
log.info { "Player $charId edge-transitioned to bank=$targetBank map=$targetMap" }
} }
private fun sendEntityWith90(ctx: ChannelHandlerContext, loadEntity: LoadEntityPacket) { private fun sendEntityWith90(ctx: ChannelHandlerContext, loadEntity: LoadEntityPacket) {
@@ -710,22 +735,8 @@ class GameProtocolHandler(
val prevY = prevStored?.info?.positionY?.toInt() val prevY = prevStored?.info?.positionY?.toInt()
val isWallBump = prevX == msg.x && prevY == msg.y val isWallBump = prevX == msg.x && prevY == msg.y
CharacterStore.updatePosition(charId, msg.x.toShort(), msg.y.toShort()) // Check for warps and edge transitions BEFORE updating position / broadcasting EntityMove.
// Real server sends LoadMap *before* EntityMove for edge transitions.
// Update session position for multiplayer tracking
session.x = msg.x.toShort()
session.y = msg.y.toShort()
val seq = sequenceCounter.incrementAndGet().toByte()
broadcastExcept(
event.ctx.channel(),
EntityMovePacket(
entityId = charId,
x = msg.x.toByte(),
y = msg.y.toByte(),
direction = msg.direction,
seq = seq))
val stored = CharacterStore.getCharacter(charId) val stored = CharacterStore.getCharacter(charId)
if (stored != null) { if (stored != null) {
val currentMap = val currentMap =
@@ -736,7 +747,7 @@ class GameProtocolHandler(
if (session.justWarped) { if (session.justWarped) {
session.justWarped = false session.justWarped = false
} else { } else {
// Direct warp: player stepped onto a warp tile (optional facing direction check) // Direct warp: player stepped onto a warp tile
val warp = val warp =
currentMap.warps.find { w -> currentMap.warps.find { w ->
w.x == msg.x && w.x == msg.x &&
@@ -752,7 +763,6 @@ class GameProtocolHandler(
} }
// Adjacent warp: player is standing in front of a door tile, facing it // Adjacent warp: player is standing in front of a door tile, facing it
// (matches real server behavior from packets.db)
val adjX = val adjX =
msg.x + msg.x +
when (msg.direction) { when (msg.direction) {
@@ -782,62 +792,77 @@ class GameProtocolHandler(
} }
} }
// Edge transition: player walked off the map edge (out of bounds) // Map edge transition: client pre-computes position on connected map.
val gbaDirection = // Detect by checking for an impossible single-step distance from the
when { // previous position, then use prevPos to identify the edge.
msg.x < 0 -> 3 // West if (prevX != null && prevY != null && !isWallBump) {
msg.x >= currentMap.width -> 4 // East val dx = kotlin.math.abs(msg.x - prevX)
msg.y < 0 -> 2 // North val dy = kotlin.math.abs(msg.y - prevY)
msg.y >= currentMap.height -> 1 // South if (dx > 1 || dy > 1) {
else -> null val gbaDirection =
} when {
if (gbaDirection != null) { prevY == 0 -> 2 // North
val connection = currentMap.connections.find { it.direction == gbaDirection } prevY == currentMap.height - 1 -> 1 // South
if (connection != null) { prevX == 0 -> 3 // West
val targetMap = prevX == currentMap.width - 1 -> 4 // East
MapManager.getMap(1, connection.targetBank.toByte(), connection.targetMap.toByte()) else -> null
if (targetMap != null) {
val targetX: Int
val targetY: Int
when (gbaDirection) {
3 -> {
targetX = targetMap.width - 1
targetY = msg.y.coerceIn(0, targetMap.height - 1)
} }
4 -> { if (gbaDirection != null) {
targetX = 0 val connection = currentMap.connections.find { it.direction == gbaDirection }
targetY = msg.y.coerceIn(0, targetMap.height - 1) if (connection != null) {
val targetMap =
MapManager.getMap(
1, connection.targetBank.toByte(), connection.targetMap.toByte())
if (targetMap != null) {
val targetX =
when (gbaDirection) {
3 -> targetMap.width - 1
4 -> 0
else -> msg.x.coerceIn(0, targetMap.width - 1)
}
val targetY =
when (gbaDirection) {
1 -> 0
2 -> targetMap.height - 1
else -> msg.y.coerceIn(0, targetMap.height - 1)
}
log.info {
"MAP TRANSITION prev=($prevX, $prevY) dir=${msg.direction} → bank=${connection.targetBank} map=${connection.targetMap} ($targetX, $targetY)"
}
edgeTransition(
event.ctx,
charId,
connection,
targetX.toByte(),
targetY.toByte(),
msg.direction)
return
} }
2 -> {
targetX = msg.x.coerceIn(0, targetMap.width - 1)
targetY = targetMap.height - 1
}
1 -> {
targetX = msg.x.coerceIn(0, targetMap.width - 1)
targetY = 0
}
else -> return
} }
val edgeWarp =
WarpTile(
x = msg.x.coerceIn(0, currentMap.width - 1),
y = msg.y.coerceIn(0, currentMap.height - 1),
targetRegionId = 1,
targetBankId = connection.targetBank.toByte(),
targetMapId = connection.targetMap.toByte(),
targetX = targetX,
targetY = targetY,
)
log.info {
"EDGE TRANSITION at (${msg.x}, ${msg.y}) dir=${msg.direction} → bank=${connection.targetBank} map=${connection.targetMap} ($targetX, $targetY)"
}
warpCharacter(event.ctx, charId, edgeWarp)
return
} }
} }
} }
} }
} }
// No transition: normal movement update + EntityMove broadcast
CharacterStore.updatePosition(charId, msg.x.toShort(), msg.y.toShort())
session.x = msg.x.toShort()
session.y = msg.y.toShort()
if (!isWallBump) {
val seq = sequenceCounter.incrementAndGet().toByte()
val movePkt =
EntityMovePacket(
entityId = charId,
x = msg.x.toByte(),
y = msg.y.toByte(),
direction = msg.direction,
seq = seq)
// Send EntityMove to the moving player (movement confirmation) and broadcast to
// others
event.ctx.channel().writeAndFlush(movePkt)
broadcastExcept(event.ctx.channel(), movePkt)
}
} }
fun onFaceDirection(event: PacketEvent<FaceDirectionPacket>) { fun onFaceDirection(event: PacketEvent<FaceDirectionPacket>) {
@@ -850,14 +875,15 @@ class GameProtocolHandler(
val seq = sequenceCounter.incrementAndGet().toByte() val seq = sequenceCounter.incrementAndGet().toByte()
val stored = CharacterStore.getCharacter(charId) ?: return val stored = CharacterStore.getCharacter(charId) ?: return
broadcastExcept( val movePkt =
event.ctx.channel(),
EntityMovePacket( EntityMovePacket(
entityId = charId, entityId = charId,
x = stored.info.positionX.toByte(), x = stored.info.positionX.toByte(),
y = stored.info.positionY.toByte(), y = stored.info.positionY.toByte(),
direction = msg.direction, direction = msg.direction,
seq = seq)) seq = seq)
event.ctx.channel().writeAndFlush(movePkt)
broadcastExcept(event.ctx.channel(), movePkt)
} }
fun onEntityInteract(event: PacketEvent<EntityInteractPacket>) { fun onEntityInteract(event: PacketEvent<EntityInteractPacket>) {