fix(game): use the npc's own region for map and sprite lookups (#92)

This commit is contained in:
Fiereu authored and GitHub committed 2026-08-07 09:28:59 +02:00
1 parent 4289ecca5e
commit 6427c3498b
9 files changed
+161 -133

No files matched your search

@@ -4,8 +4,9 @@ import de.fiereu.bytecodec.*
data class NpcSpawnPacket( data class NpcSpawnPacket(
val entityId: Long, val entityId: Long,
val unk1: Int, /** Which region's sprite table [graphicsId] is looked up in. */
val unk2: Int, val spriteRegionId: Int,
val graphicsId: Int,
val unk3: Int, val unk3: Int,
val unk4: Int, val unk4: Int,
val regionId: Int, val regionId: Int,
@@ -21,8 +22,8 @@ data class NpcSpawnPacket(
object NpcSpawnPacketCodec : PacketCodec<NpcSpawnPacket>() { object NpcSpawnPacketCodec : PacketCodec<NpcSpawnPacket>() {
override fun CodecScope<NpcSpawnPacket>.body(): NpcSpawnPacket { override fun CodecScope<NpcSpawnPacket>.body(): NpcSpawnPacket {
val entityId = field(S64LE) { it.entityId } val entityId = field(S64LE) { it.entityId }
val unk1 = field(U8) { it.unk1 } val spriteRegionId = field(U8) { it.spriteRegionId }
val unk2 = field(U16LE) { it.unk2 } val graphicsId = field(U16LE) { it.graphicsId }
val unk3 = field(U16LE) { it.unk3 } val unk3 = field(U16LE) { it.unk3 }
val unk4 = field(U16LE) { it.unk4 } val unk4 = field(U16LE) { it.unk4 }
val regionId = field(U8) { it.regionId } val regionId = field(U8) { it.regionId }
@@ -34,6 +35,18 @@ object NpcSpawnPacketCodec : PacketCodec<NpcSpawnPacket>() {
val facing = field(U8) { it.facing } val facing = field(U8) { it.facing }
val unk6 = field(U16LE) { it.unk6 } val unk6 = field(U16LE) { it.unk6 }
return NpcSpawnPacket( return NpcSpawnPacket(
entityId, unk1, unk2, unk3, unk4, regionId, bankId, mapId, x, y, facing, unk5, unk6) entityId,
spriteRegionId,
graphicsId,
unk3,
unk4,
regionId,
bankId,
mapId,
x,
y,
facing,
unk5,
unk6)
} }
} }
@@ -44,11 +44,12 @@ constructor(
stored.info.positionBankId, stored.info.positionBankId,
stored.info.positionMapId, stored.info.positionMapId,
) ?: return ) ?: return
val regionId = stored.info.positionRegionId.toInt()
val bankId = stored.info.positionBankId.toInt() val bankId = stored.info.positionBankId.toInt()
val mapId = stored.info.positionMapId.toInt() val mapId = stored.info.positionMapId.toInt()
for (npc in currentMap.npcs) { for (npc in currentMap.npcs) {
if (npcService.getNpcEntityId(bankId, mapId, npc.entityIdx) == npcEntityId) { if (npcService.getNpcEntityId(regionId, bankId, mapId, npc.entityIdx) == npcEntityId) {
val script = scriptRegistry.forLabel(npc.script) val script = scriptRegistry.forLabel(npc.script)
if (script != null) { if (script != null) {
runScript(session, state, script, npcEntityId) runScript(session, state, script, npcEntityId)
@@ -290,6 +290,10 @@ constructor(
) { ) {
val map = mapManager.getMap(info.positionRegionId, info.positionBankId, info.positionMapId) val map = mapManager.getMap(info.positionRegionId, info.positionBankId, info.positionMapId)
if (map != null) { if (map != null) {
log.info {
"Placing '${info.name}' at ${info.positionRegionId}:${info.positionBankId}:" +
"${info.positionMapId} (${info.positionX}, ${info.positionY})"
}
ctx.send(mapManager.createLoadMapPacket(map, reloadPlayer = true, deleteCache = true)) ctx.send(mapManager.createLoadMapPacket(map, reloadPlayer = true, deleteCache = true))
mapLoadService.preloadConnectedMaps(ctx, map) mapLoadService.preloadConnectedMaps(ctx, map)
} else { } else {
@@ -56,7 +56,8 @@ constructor(
for (conn in connections) { for (conn in connections) {
val key = "${conn.targetBank}:${conn.targetMap}" val key = "${conn.targetBank}:${conn.targetMap}"
if (!loaded.add(key)) continue if (!loaded.add(key)) continue
val connected = mapManager.getMap(1, conn.targetBank, conn.targetMap) // Connections stay inside one region.
val connected = mapManager.getMap(map.regionId.toInt(), conn.targetBank, conn.targetMap)
if (connected != null) { if (connected != null) {
ctx.send( ctx.send(
mapManager.createLoadMapPacket( mapManager.createLoadMapPacket(
@@ -128,8 +128,11 @@ constructor(
// Walking off the edge of a map hands the player to the neighbouring map, if there is one. // Walking off the edge of a map hands the player to the neighbouring map, if there is one.
if (toX !in 0 until currentMap.width || toY !in 0 until currentMap.height) { if (toX !in 0 until currentMap.width || toY !in 0 until currentMap.height) {
val connection = currentMap.connections.find { it.direction == msg.direction } val connection = currentMap.connections.find { it.direction == msg.direction }
// Connections stay inside one region.
val targetMap = val targetMap =
connection?.let { mapManager.getMap(1, it.targetBank.toByte(), it.targetMap.toByte()) } connection?.let {
mapManager.getMap(currentMap.regionId, it.targetBank.toByte(), it.targetMap.toByte())
}
if (connection == null || targetMap == null) { if (connection == null || targetMap == null) {
sendPositionReset(ctx, charId, currentMap, fromX, fromY, msg.direction) sendPositionReset(ctx, charId, currentMap, fromX, fromY, msg.direction)
return return
@@ -146,7 +149,15 @@ constructor(
Direction.DOWN -> 0 Direction.DOWN -> 0
else -> (fromY - connection.unknown).coerceIn(0, targetMap.height - 1) else -> (fromY - connection.unknown).coerceIn(0, targetMap.height - 1)
} }
edgeTransition(ctx, charId, connection, entryX.toByte(), entryY.toByte(), msg.direction) edgeTransition(
ctx,
charId,
currentMap.regionId,
connection,
entryX.toByte(),
entryY.toByte(),
msg.direction,
)
return return
} }
@@ -262,6 +273,7 @@ constructor(
private fun edgeTransition( private fun edgeTransition(
ctx: SessionContext, ctx: SessionContext,
charId: Long, charId: Long,
regionId: Byte,
connection: MapData.GbaConnection, connection: MapData.GbaConnection,
targetX: Byte, targetX: Byte,
targetY: Byte, targetY: Byte,
@@ -269,7 +281,7 @@ constructor(
) { ) {
val targetBank = connection.targetBank.toByte() val targetBank = connection.targetBank.toByte()
val targetMap = connection.targetMap.toByte() val targetMap = connection.targetMap.toByte()
val map = mapManager.getMap(1, targetBank, targetMap) ?: return val map = mapManager.getMap(regionId, targetBank, targetMap) ?: return
val state = ctx.attributes[PLAYER_STATE] val state = ctx.attributes[PLAYER_STATE]
if (state != null) { if (state != null) {
@@ -289,7 +301,7 @@ constructor(
presenceService.refresh(ctx) presenceService.refresh(ctx)
mapLoadService.preloadConnectedMaps(ctx, map, depth = 1) mapLoadService.preloadConnectedMaps(ctx, map, depth = 1)
npcService.spawnNpcsForMap(ctx, targetBank.toInt(), targetMap.toInt(), state?.regionId ?: 1) npcService.spawnNpcsForMap(ctx, targetBank.toInt(), targetMap.toInt(), regionId.toInt())
ctx.send(gbaMovePacket(charId, map, targetX.toInt(), targetY.toInt(), direction)) ctx.send(gbaMovePacket(charId, map, targetX.toInt(), targetY.toInt(), direction))
@@ -29,75 +29,16 @@ constructor(
private val npcEntityIdCounter = AtomicLong(0x1A69000000000000L) private val npcEntityIdCounter = AtomicLong(0x1A69000000000000L)
private val npcEntityIds = mutableMapOf<String, Long>() private val npcEntityIds = mutableMapOf<String, Long>()
fun getNpcEntityId(bankId: Int, mapId: Int, entityIdx: Int): Long? { fun getNpcEntityId(regionId: Int, bankId: Int, mapId: Int, entityIdx: Int): Long? {
return npcEntityIds["$bankId:$mapId:$entityIdx"] return npcEntityIds[key(regionId, bankId, mapId, entityIdx)]
} }
fun spawnNpcsForMap(ctx: SessionContext, bankId: Int, mapId: Int, regionId: Int = 1) { fun spawnNpcsForMap(ctx: SessionContext, bankId: Int, mapId: Int, regionId: Int) {
val map = mapManager.getMap(regionId, bankId, mapId) ?: return val map = mapManager.getMap(regionId, bankId, mapId) ?: return
val stored = ctx.attributes[PLAYER_STATE]?.characterId?.let(characterStore::getCharacter) val stored = ctx.attributes[PLAYER_STATE]?.characterId?.let(characterStore::getCharacter)
val storyFlags = stored?.storyFlags.orEmpty() val storyFlags = stored?.storyFlags.orEmpty()
val storyVars = stored?.storyVars.orEmpty() val storyVars = stored?.storyVars.orEmpty()
// Generated Petalburg data now supplies these NPCs.
if (regionId == 1 && USE_LEGACY_PETALBURG_SPAWNS && bankId == 74 && mapId == 11) {
data class NpcEntry(
val entityIdx: Int,
val x: Int,
val y: Int,
val gfx: Int,
val unk1: Int,
val unk3: Int,
val unk4: Int,
val facing: Int,
val unk6: Int,
)
val all16 =
listOf(
NpcEntry(7, 15, 19, 9, 1, 768, 513, 0, 8),
NpcEntry(6, 4, 8, 59, 1, 256, 0, 0, 8),
NpcEntry(5, 35, 20, 59, 1, 2048, 0, 0, 8),
NpcEntry(4, 45, 7, 59, 1, 2048, 0, 0, 8),
NpcEntry(3, 26, 20, 23, 1, 256, 0, 0, 0),
NpcEntry(2, 26, 17, 117, 1, 2563, 0, 3, 0),
NpcEntry(1, 19, 11, 82, 1, 256, 0, 0, 8),
NpcEntry(0, 19, 10, 82, 1, 256, 0, 0, 8),
NpcEntry(15, 16, 24, 0, 10, 25600, 0, 0, 8),
NpcEntry(14, 15, 24, 600, 10, 0, 0, 0, 0),
NpcEntry(13, 36, 23, 246, 10, 0, 0, 0, 8),
NpcEntry(12, 33, 5, 10, 1, 256, 257, 0, 8),
NpcEntry(11, 4, 26, 59, 1, 256, 257, 0, 8),
NpcEntry(10, 30, 34, 13, 1, 512, 258, 0, 8),
NpcEntry(9, 4, 14, 36, 1, 5121, 0, 1, 9),
NpcEntry(8, 7, 32, 36, 1, 5632, 0, 0, 8),
)
val baseEntityId = 0x000000000001E000L
for (npc in all16) {
val entityId = baseEntityId or npc.entityIdx.toLong()
log.info {
">> Petalburg NpcSpawn[${npc.entityIdx}] entId=0x${entityId.toString(16)} pos=(${npc.x},${npc.y}) gfx=${npc.gfx}"
}
val spawnPacket =
NpcSpawnPacket(
entityId = entityId,
unk1 = npc.unk1,
unk2 = npc.gfx,
unk3 = npc.unk3,
unk4 = npc.unk4,
regionId = 1,
bankId = bankId,
mapId = mapId,
x = npc.x,
y = npc.y,
facing = npc.facing,
unk5 = 2,
unk6 = npc.unk6,
)
ctx.send(spawnPacket)
}
return
}
for (npc in map.npcs) { for (npc in map.npcs) {
// Decoration slots are not normal NPCs. // Decoration slots are not normal NPCs.
if (npc.hideFlag.substringAfter('/').startsWith(DECORATION_FLAG_PREFIX)) continue if (npc.hideFlag.substringAfter('/').startsWith(DECORATION_FLAG_PREFIX)) continue
@@ -107,25 +48,31 @@ constructor(
val resolved = val resolved =
resolveDynamicGraphics( resolveDynamicGraphics(
ctx, applyStoryPlacement(bankId, mapId, npc, storyFlags, storyVars)) ctx, applyStoryPlacement(bankId, mapId, npc, storyFlags, storyVars))
ctx.send(buildSpawnPacket(resolved, entityIdFor(bankId, mapId, npc.entityIdx), bankId, mapId)) ctx.send(
buildSpawnPacket(
resolved,
entityIdFor(regionId, bankId, mapId, npc.entityIdx),
regionId,
bankId,
mapId,
))
} }
} }
/** Allocate (or return) the stable entity id for a map npc by its decomp local id. */ /** Allocate (or return) the stable entity id for a map npc by its decomp local id. */
fun entityIdFor(bankId: Int, mapId: Int, entityIdx: Int): Long = fun entityIdFor(regionId: Int, bankId: Int, mapId: Int, entityIdx: Int): Long =
npcEntityIds.getOrPut("$bankId:$mapId:$entityIdx") { npcEntityIdCounter.incrementAndGet() } npcEntityIds.getOrPut(key(regionId, bankId, mapId, entityIdx)) {
npcEntityIdCounter.incrementAndGet()
}
/** Spawn a single npc (including a normally hidden one) for one player, for cutscenes. */ /** Spawn a single npc (including a normally hidden one) for one player, for cutscenes. */
fun spawnNpc(ctx: SessionContext, bankId: Int, mapId: Int, localId: Int) { fun spawnNpc(ctx: SessionContext, regionId: Int, bankId: Int, mapId: Int, localId: Int) {
val npc = mapManager.getMap(1, bankId, mapId)?.npcs?.firstOrNull { it.entityIdx == localId } val npc = findNpc(regionId, bankId, mapId, localId) ?: return
if (npc == null) {
log.warn { "spawnNpc: npc $localId not found on $bankId:$mapId" }
return
}
ctx.send( ctx.send(
buildSpawnPacket( buildSpawnPacket(
resolveDynamicGraphics(ctx, npc), resolveDynamicGraphics(ctx, npc),
entityIdFor(bankId, mapId, localId), entityIdFor(regionId, bankId, mapId, localId),
regionId,
bankId, bankId,
mapId, mapId,
)) ))
@@ -134,21 +81,19 @@ constructor(
/** Spawns an NPC at a cutscene position. */ /** Spawns an NPC at a cutscene position. */
fun spawnNpcAt( fun spawnNpcAt(
ctx: SessionContext, ctx: SessionContext,
regionId: Int,
bankId: Int, bankId: Int,
mapId: Int, mapId: Int,
localId: Int, localId: Int,
x: Int, x: Int,
y: Int, y: Int,
) { ) {
val npc = mapManager.getMap(1, bankId, mapId)?.npcs?.firstOrNull { it.entityIdx == localId } val npc = findNpc(regionId, bankId, mapId, localId) ?: return
if (npc == null) {
log.warn { "spawnNpcAt: npc $localId not found on $bankId:$mapId" }
return
}
ctx.send( ctx.send(
buildSpawnPacket( buildSpawnPacket(
resolveDynamicGraphics(ctx, npc.copy(x = x, y = y)), resolveDynamicGraphics(ctx, npc.copy(x = x, y = y)),
entityIdFor(bankId, mapId, localId), entityIdFor(regionId, bankId, mapId, localId),
regionId,
bankId, bankId,
mapId, mapId,
)) ))
@@ -157,21 +102,18 @@ constructor(
/** Repositions an existing NPC. */ /** Repositions an existing NPC. */
fun repositionNpc( fun repositionNpc(
ctx: SessionContext, ctx: SessionContext,
regionId: Int,
bankId: Int, bankId: Int,
mapId: Int, mapId: Int,
localId: Int, localId: Int,
x: Int, x: Int,
y: Int, y: Int,
) { ) {
val npc = mapManager.getMap(1, bankId, mapId)?.npcs?.firstOrNull { it.entityIdx == localId } val npc = findNpc(regionId, bankId, mapId, localId) ?: return
if (npc == null) {
log.warn { "repositionNpc: npc $localId not found on $bankId:$mapId" }
return
}
ctx.send( ctx.send(
NpcUpdatePacket( NpcUpdatePacket(
entityId = entityIdFor(bankId, mapId, localId), entityId = entityIdFor(regionId, bankId, mapId, localId),
regionId = 1, regionId = regionId,
bankId = bankId, bankId = bankId,
mapId = mapId, mapId = mapId,
x = x, x = x,
@@ -183,18 +125,30 @@ constructor(
} }
/** Removes a cutscene NPC. */ /** Removes a cutscene NPC. */
fun despawnNpc(ctx: SessionContext, bankId: Int, mapId: Int, localId: Int) { fun despawnNpc(ctx: SessionContext, regionId: Int, bankId: Int, mapId: Int, localId: Int) {
val entityId = getNpcEntityId(bankId, mapId, localId) ?: return val entityId = getNpcEntityId(regionId, bankId, mapId, localId) ?: return
ctx.send(EntityLeavePacket(entityId)) ctx.send(EntityLeavePacket(entityId))
} }
private fun findNpc(regionId: Int, bankId: Int, mapId: Int, localId: Int): NpcDef? {
val npc =
mapManager.getMap(regionId, bankId, mapId)?.npcs?.firstOrNull { it.entityIdx == localId }
if (npc == null) log.warn { "npc $localId not found on $regionId:$bankId:$mapId" }
return npc
}
private fun key(regionId: Int, bankId: Int, mapId: Int, entityIdx: Int) =
"$regionId:$bankId:$mapId:$entityIdx"
private fun buildSpawnPacket( private fun buildSpawnPacket(
npc: NpcDef, npc: NpcDef,
entityId: Long, entityId: Long,
regionId: Int,
bankId: Int, bankId: Int,
mapId: Int, mapId: Int,
): NpcSpawnPacket { ): NpcSpawnPacket {
val movementId = npc.movementType.forRegion(Region.HOENN).id val region = requireNotNull(Region.byId(regionId)) { "Unknown region id $regionId" }
val movementId = npc.movementType.forRegion(region).id
val unk3 = ((movementId and 0xFF) shl 8) or 0x02 val unk3 = ((movementId and 0xFF) shl 8) or 0x02
val unk4 = val unk4 =
if (movementId in 1..6 || (movementId in 25..52)) { if (movementId in 1..6 || (movementId in 25..52)) {
@@ -204,11 +158,11 @@ constructor(
} }
return NpcSpawnPacket( return NpcSpawnPacket(
entityId = entityId, entityId = entityId,
unk1 = 1, spriteRegionId = regionId,
unk2 = npc.graphicsId, graphicsId = npc.graphicsId,
unk3 = unk3, unk3 = unk3,
unk4 = unk4, unk4 = unk4,
regionId = 1, regionId = regionId,
bankId = bankId, bankId = bankId,
mapId = mapId, mapId = mapId,
x = npc.x, x = npc.x,
@@ -268,7 +222,6 @@ constructor(
private companion object { private companion object {
const val DECORATION_FLAG_PREFIX = "FLAG_DECORATION_" const val DECORATION_FLAG_PREFIX = "FLAG_DECORATION_"
const val USE_LEGACY_PETALBURG_SPAWNS = false
const val DYNAMIC_GFX_VAR_0 = 240 const val DYNAMIC_GFX_VAR_0 = 240
const val RIVAL_BRENDAN_NORMAL = 100 const val RIVAL_BRENDAN_NORMAL = 100
const val RIVAL_MAY_NORMAL = 105 const val RIVAL_MAY_NORMAL = 105
@@ -45,7 +45,12 @@ constructor(
mapManager.getMap(info.positionRegionId, info.positionBankId, info.positionMapId) ?: return mapManager.getMap(info.positionRegionId, info.positionBankId, info.positionMapId) ?: return
val npc = map.npcs.firstOrNull { it.entityIdx == localId } ?: return val npc = map.npcs.firstOrNull { it.entityIdx == localId } ?: return
val entityId = val entityId =
npcService.entityIdFor(info.positionBankId.toInt(), info.positionMapId.toInt(), localId) npcService.entityIdFor(
info.positionRegionId.toInt(),
info.positionBankId.toInt(),
info.positionMapId.toInt(),
localId,
)
drive(session, entityId, Pose(npc.x, npc.y, npc.facing), steps) drive(session, entityId, Pose(npc.x, npc.y, npc.facing), steps)
} }
@@ -63,7 +68,11 @@ constructor(
paths.mapNotNull { (localId, steps) -> paths.mapNotNull { (localId, steps) ->
if (map.npcs.none { it.entityIdx == localId }) return@mapNotNull null if (map.npcs.none { it.entityIdx == localId }) return@mapNotNull null
npcService.entityIdFor( npcService.entityIdFor(
info.positionBankId.toInt(), info.positionMapId.toInt(), localId) to steps info.positionRegionId.toInt(),
info.positionBankId.toInt(),
info.positionMapId.toInt(),
localId,
) to steps
} }
resolved.forEach { (entityId, steps) -> sendActions(session, entityId, steps) } resolved.forEach { (entityId, steps) -> sendActions(session, entityId, steps) }
delay(resolved.maxOfOrNull { (_, steps) -> durationMs(steps) } ?: 0) delay(resolved.maxOfOrNull { (_, steps) -> durationMs(steps) } ?: 0)
@@ -84,7 +93,11 @@ constructor(
paths.mapNotNull { (localId, steps) -> paths.mapNotNull { (localId, steps) ->
if (map.npcs.none { it.entityIdx == localId }) return@mapNotNull null if (map.npcs.none { it.entityIdx == localId }) return@mapNotNull null
npcService.entityIdFor( npcService.entityIdFor(
info.positionBankId.toInt(), info.positionMapId.toInt(), localId) to steps info.positionRegionId.toInt(),
info.positionBankId.toInt(),
info.positionMapId.toInt(),
localId,
) to steps
} }
sendActions(session, info.id, selfSteps) sendActions(session, info.id, selfSteps)
resolved.forEach { (entityId, steps) -> sendActions(session, entityId, steps) } resolved.forEach { (entityId, steps) -> sendActions(session, entityId, steps) }
@@ -106,7 +119,13 @@ constructor(
fun showNpc(session: SessionContext, state: PlayerState, localId: Int) { fun showNpc(session: SessionContext, state: PlayerState, localId: Int) {
val charId = state.characterId ?: return val charId = state.characterId ?: return
val info = characterStore.getCharacter(charId)?.info ?: return val info = characterStore.getCharacter(charId)?.info ?: return
npcService.spawnNpc(session, info.positionBankId.toInt(), info.positionMapId.toInt(), localId) npcService.spawnNpc(
session,
info.positionRegionId.toInt(),
info.positionBankId.toInt(),
info.positionMapId.toInt(),
localId,
)
} }
/** Show a normally hidden map npc at an overridden cutscene position. */ /** Show a normally hidden map npc at an overridden cutscene position. */
@@ -114,7 +133,14 @@ constructor(
val charId = state.characterId ?: return val charId = state.characterId ?: return
val info = characterStore.getCharacter(charId)?.info ?: return val info = characterStore.getCharacter(charId)?.info ?: return
npcService.spawnNpcAt( npcService.spawnNpcAt(
session, info.positionBankId.toInt(), info.positionMapId.toInt(), localId, x, y) session,
info.positionRegionId.toInt(),
info.positionBankId.toInt(),
info.positionMapId.toInt(),
localId,
x,
y,
)
} }
/** Relocate an npc that the normal map spawn already created. */ /** Relocate an npc that the normal map spawn already created. */
@@ -122,7 +148,14 @@ constructor(
val charId = state.characterId ?: return val charId = state.characterId ?: return
val info = characterStore.getCharacter(charId)?.info ?: return val info = characterStore.getCharacter(charId)?.info ?: return
npcService.repositionNpc( npcService.repositionNpc(
session, info.positionBankId.toInt(), info.positionMapId.toInt(), localId, x, y) session,
info.positionRegionId.toInt(),
info.positionBankId.toInt(),
info.positionMapId.toInt(),
localId,
x,
y,
)
} }
/** Repositions the player and synchronizes server state. */ /** Repositions the player and synchronizes server state. */
@@ -156,14 +189,25 @@ constructor(
fun removeNpc(session: SessionContext, state: PlayerState, localId: Int) { fun removeNpc(session: SessionContext, state: PlayerState, localId: Int) {
val charId = state.characterId ?: return val charId = state.characterId ?: return
val info = characterStore.getCharacter(charId)?.info ?: return val info = characterStore.getCharacter(charId)?.info ?: return
npcService.despawnNpc(session, info.positionBankId.toInt(), info.positionMapId.toInt(), localId) npcService.despawnNpc(
session,
info.positionRegionId.toInt(),
info.positionBankId.toInt(),
info.positionMapId.toInt(),
localId,
)
} }
/** Resolves local NPC ids to entity ids. */ /** Resolves local NPC ids to entity ids. */
fun npcEntityId(state: PlayerState, localId: Int): Long? { fun npcEntityId(state: PlayerState, localId: Int): Long? {
val charId = state.characterId ?: return null val charId = state.characterId ?: return null
val info = characterStore.getCharacter(charId)?.info ?: return null val info = characterStore.getCharacter(charId)?.info ?: return null
return npcService.entityIdFor(info.positionBankId.toInt(), info.positionMapId.toInt(), localId) return npcService.entityIdFor(
info.positionRegionId.toInt(),
info.positionBankId.toInt(),
info.positionMapId.toInt(),
localId,
)
} }
/** Returns the created player's gender. */ /** Returns the created player's gender. */
@@ -94,7 +94,7 @@ class MapTransitionScriptTest :
val npcs = NpcService(mapManager, store) val npcs = NpcService(mapManager, store)
val movement = ScriptMovementService(mapManager, npcs, store) val movement = ScriptMovementService(mapManager, npcs, store)
val ctx = ScriptContext(session, state, entityId = -1, DialogService(), story, movement) val ctx = ScriptContext(session, state, entityId = -1, DialogService(), story, movement)
val momEntityId = npcs.entityIdFor(51, 3, 13) val momEntityId = npcs.entityIdFor(1, 51, 3, 13)
val cutscene = launch { setWallClock(ctx, female = true) } val cutscene = launch { setWallClock(ctx, female = true) }
advanceUntilIdle() advanceUntilIdle()
@@ -277,8 +277,8 @@ class MapTransitionScriptTest :
val npcs = NpcService(mapManager, store) val npcs = NpcService(mapManager, store)
val movement = ScriptMovementService(mapManager, npcs, store) val movement = ScriptMovementService(mapManager, npcs, store)
val ctx = ScriptContext(session, state, entityId = -1, DialogService(), story, movement) val ctx = ScriptContext(session, state, entityId = -1, DialogService(), story, movement)
val birchId = npcs.entityIdFor(50, 16, 1) val birchId = npcs.entityIdFor(1, 50, 16, 1)
val zigzagoonId = npcs.entityIdFor(50, 16, 3) val zigzagoonId = npcs.entityIdFor(1, 50, 16, 3)
val cutscene = launch { Route101_EventScript_StartBirchRescue.run(ctx) } val cutscene = launch { Route101_EventScript_StartBirchRescue.run(ctx) }
advanceUntilIdle() advanceUntilIdle()
@@ -321,7 +321,7 @@ class MapTransitionScriptTest :
ScriptContext( ScriptContext(
session, session,
state, state,
entityId = npcs.entityIdFor(51, 1, 14), entityId = npcs.entityIdFor(1, 51, 1, 14),
dialog, dialog,
story, story,
movement, movement,
@@ -25,18 +25,18 @@ class NpcStoryFlagSyncTest :
val session = FakeSession(characterId = character.info.id, bankId = 50, mapId = 9) val session = FakeSession(characterId = character.info.id, bankId = 50, mapId = 9)
val npcs = NpcService(MapManager(), store) val npcs = NpcService(MapManager(), store)
npcs.spawnNpcsForMap(session, bankId = 50, mapId = 9) npcs.spawnNpcsForMap(session, bankId = 50, mapId = 9, regionId = 1)
// Only Brendan's truck starts visible. // Only Brendan's truck starts visible.
(npcs.getNpcEntityId(50, 9, 4) != null) shouldBe true (npcs.getNpcEntityId(1, 50, 9, 4) != null) shouldBe true
npcs.getNpcEntityId(50, 9, 5) shouldBe null npcs.getNpcEntityId(1, 50, 9, 5) shouldBe null
npcs.getNpcEntityId(50, 9, 3) shouldBe null npcs.getNpcEntityId(1, 50, 9, 3) shouldBe null
StoryService(store) StoryService(store)
.clearFlag(character.info.id, HoennFlags.FLAG_HIDE_LITTLEROOT_TOWN_MOM_OUTSIDE) .clearFlag(character.info.id, HoennFlags.FLAG_HIDE_LITTLEROOT_TOWN_MOM_OUTSIDE)
npcs.spawnNpcsForMap(session, bankId = 50, mapId = 9) npcs.spawnNpcsForMap(session, bankId = 50, mapId = 9, regionId = 1)
(npcs.getNpcEntityId(50, 9, 3) != null) shouldBe true (npcs.getNpcEntityId(1, 50, 9, 3) != null) shouldBe true
} }
} }
@@ -47,10 +47,10 @@ class NpcStoryFlagSyncTest :
val session = FakeSession(characterId = character.info.id, bankId = 51, mapId = 3) val session = FakeSession(characterId = character.info.id, bankId = 51, mapId = 3)
val npcs = NpcService(MapManager(), store) val npcs = NpcService(MapManager(), store)
npcs.spawnNpcsForMap(session, bankId = 51, mapId = 3) npcs.spawnNpcsForMap(session, bankId = 51, mapId = 3, regionId = 1)
// Decoration slots are not normal NPCs. // Decoration slots are not normal NPCs.
(1..12).forEach { localId -> npcs.getNpcEntityId(51, 3, localId) shouldBe null } (1..12).forEach { localId -> npcs.getNpcEntityId(1, 51, 3, localId) shouldBe null }
} }
} }
@@ -63,9 +63,9 @@ class NpcStoryFlagSyncTest :
story.setVar(character.info.id, HoennVars.VAR_LITTLEROOT_TOWN_STATE, 1) story.setVar(character.info.id, HoennVars.VAR_LITTLEROOT_TOWN_STATE, 1)
val npcs = NpcService(MapManager(), store) val npcs = NpcService(MapManager(), store)
npcs.spawnNpcsForMap(session, bankId = 50, mapId = 9) npcs.spawnNpcsForMap(session, bankId = 50, mapId = 9, regionId = 1)
val twinId = npcs.getNpcEntityId(50, 9, 0) val twinId = npcs.getNpcEntityId(1, 50, 9, 0)
val twin = val twin =
session.sent.filterIsInstance<NpcSpawnPacket>().single { it.entityId == twinId } session.sent.filterIsInstance<NpcSpawnPacket>().single { it.entityId == twinId }
twin.x shouldBe 10 twin.x shouldBe 10
@@ -83,12 +83,12 @@ class NpcStoryFlagSyncTest :
StoryService(femaleStore).clearFlag(female.info.id, HoennFlags.FLAG_HIDE_ROUTE_103_RIVAL) StoryService(femaleStore).clearFlag(female.info.id, HoennFlags.FLAG_HIDE_ROUTE_103_RIVAL)
val femaleSession = FakeSession(characterId = female.info.id, bankId = 50, mapId = 18) val femaleSession = FakeSession(characterId = female.info.id, bankId = 50, mapId = 18)
val femaleNpcs = NpcService(mapManager, femaleStore) val femaleNpcs = NpcService(mapManager, femaleStore)
femaleNpcs.spawnNpcsForMap(femaleSession, bankId = 50, mapId = 18) femaleNpcs.spawnNpcsForMap(femaleSession, bankId = 50, mapId = 18, regionId = 1)
val brendanId = femaleNpcs.getNpcEntityId(50, 18, 1) val brendanId = femaleNpcs.getNpcEntityId(1, 50, 18, 1)
femaleSession.sent femaleSession.sent
.filterIsInstance<NpcSpawnPacket>() .filterIsInstance<NpcSpawnPacket>()
.single { it.entityId == brendanId } .single { it.entityId == brendanId }
.unk2 shouldBe 100 .graphicsId shouldBe 100
val maleStore = val maleStore =
CharacterStore(FakeCharacterRepository(), EntityIdService(), backgroundScope) CharacterStore(FakeCharacterRepository(), EntityIdService(), backgroundScope)
@@ -96,12 +96,12 @@ class NpcStoryFlagSyncTest :
StoryService(maleStore).clearFlag(male.info.id, HoennFlags.FLAG_HIDE_ROUTE_103_RIVAL) StoryService(maleStore).clearFlag(male.info.id, HoennFlags.FLAG_HIDE_ROUTE_103_RIVAL)
val maleSession = FakeSession(characterId = male.info.id, bankId = 50, mapId = 18) val maleSession = FakeSession(characterId = male.info.id, bankId = 50, mapId = 18)
val maleNpcs = NpcService(mapManager, maleStore) val maleNpcs = NpcService(mapManager, maleStore)
maleNpcs.spawnNpcsForMap(maleSession, bankId = 50, mapId = 18) maleNpcs.spawnNpcsForMap(maleSession, bankId = 50, mapId = 18, regionId = 1)
val mayId = maleNpcs.getNpcEntityId(50, 18, 1) val mayId = maleNpcs.getNpcEntityId(1, 50, 18, 1)
maleSession.sent maleSession.sent
.filterIsInstance<NpcSpawnPacket>() .filterIsInstance<NpcSpawnPacket>()
.single { it.entityId == mayId } .single { it.entityId == mayId }
.unk2 shouldBe 105 .graphicsId shouldBe 105
} }
} }
@@ -113,9 +113,9 @@ class NpcStoryFlagSyncTest :
val session = FakeSession(characterId = character.info.id, bankId = 50, mapId = 16) val session = FakeSession(characterId = character.info.id, bankId = 50, mapId = 16)
val npcs = NpcService(MapManager(), store) val npcs = NpcService(MapManager(), store)
npcs.spawnNpcsForMap(session, bankId = 50, mapId = 16) npcs.spawnNpcsForMap(session, bankId = 50, mapId = 16, regionId = 1)
npcs.getNpcEntityId(50, 16, 3) shouldBe null npcs.getNpcEntityId(1, 50, 16, 3) shouldBe null
} }
} }
}) })