fix(game): send the battle reward packet so the client shows the exp gained (#101)

This commit is contained in:
Fiereu authored and GitHub committed 2026-08-07 22:00:55 +02:00
1 parent 3b90027d18
commit 5f3bc9415e
4 files changed
+124 -39

No files matched your search

@@ -0,0 +1,73 @@
package de.fiereu.openmmo.server.game.battle
import de.fiereu.openmmo.common.Pokemon
import de.fiereu.openmmo.net.game.packets.battle.BattleEntityDeltaPacket
import de.fiereu.openmmo.pokemon.SpeciesDef
// The real server follows the record on 0x14 with this. Its mask 0x21B also carries a happiness
// we do not model.
private const val ACQUIRED_MASK = 0x1 or 0x2 or 0x8 or 0x10
/** Tells the client about a monster the player just obtained. */
fun acquiredMonsterDelta(mon: Pokemon, species: SpeciesDef): BattleEntityDeltaPacket {
val stats = StatCalculator.computeAll(species, mon)
return entityDelta(
entityId = mon.id,
mask = ACQUIRED_MASK,
experienceLevel = mon.level,
experiencePoints = mon.xp,
statValues = stats.asWireList(),
currentHp = mon.hp,
faintFlag = 0,
)
}
fun entityDelta(
entityId: Long,
mask: Int,
experienceLevel: Byte? = null,
experiencePoints: Int? = null,
statValues: List<Short>? = null,
currentHp: Short? = null,
faintFlag: Byte? = null,
): BattleEntityDeltaPacket =
BattleEntityDeltaPacket(
entityId = entityId,
fieldMask = mask,
experienceLevel = experienceLevel,
experiencePoints = experiencePoints,
statValues = statValues,
moveSlots = null,
ppUps = null,
currentHp = currentHp,
faintFlag = faintFlag,
speciesId = null,
forme = null,
listType = null,
sortKey = null,
evValues = null,
level = null,
happiness = null,
shininessSeed = null,
experience = null,
flagA = null,
flagB = null,
encounterType = null,
statusFlagsValue = null,
warnA = null,
warnB = null,
natureId = null,
ribbons = null,
packedIvs = null,
effortValues = null,
originalSpecies = null,
originalTrainerName = null,
originalTrainerId = null,
shininessType = null,
gender = null,
ivs = null,
caughtBall = null,
statusFlags2 = null,
statusList = null,
status = null,
)
@@ -2,6 +2,7 @@ package de.fiereu.openmmo.server.game.battle
import de.fiereu.openmmo.common.Pokemon import de.fiereu.openmmo.common.Pokemon
import de.fiereu.openmmo.common.enums.ChatType import de.fiereu.openmmo.common.enums.ChatType
import de.fiereu.openmmo.common.enums.EVs
import de.fiereu.openmmo.common.enums.Language import de.fiereu.openmmo.common.enums.Language
import de.fiereu.openmmo.common.enums.PokemonContainer import de.fiereu.openmmo.common.enums.PokemonContainer
import de.fiereu.openmmo.net.game.packets.ChatMessagePacket import de.fiereu.openmmo.net.game.packets.ChatMessagePacket
@@ -19,6 +20,7 @@ import de.fiereu.openmmo.net.game.packets.battle.BattleQueuedEventPacket
import de.fiereu.openmmo.net.game.packets.battle.BattleSidePacket import de.fiereu.openmmo.net.game.packets.battle.BattleSidePacket
import de.fiereu.openmmo.net.game.packets.battle.BattleSlotEventEnumPacket import de.fiereu.openmmo.net.game.packets.battle.BattleSlotEventEnumPacket
import de.fiereu.openmmo.net.game.packets.battle.BattleSlotFlagEventPacket import de.fiereu.openmmo.net.game.packets.battle.BattleSlotFlagEventPacket
import de.fiereu.openmmo.net.game.packets.battle.BattleStatCountersPacket
import de.fiereu.openmmo.net.game.packets.battle.BattleSwitchInPacket import de.fiereu.openmmo.net.game.packets.battle.BattleSwitchInPacket
import de.fiereu.openmmo.net.game.packets.battle.BattleTileMapPacket import de.fiereu.openmmo.net.game.packets.battle.BattleTileMapPacket
import de.fiereu.openmmo.server.game.world.interest.InterestManager import de.fiereu.openmmo.server.game.world.interest.InterestManager
@@ -43,6 +45,10 @@ private const val PLAYER_SIDE: Byte = 1
// event type in every capture: a hit carries 0x0200, other events carry 0. // event type in every capture: a hit carries 0x0200, other events carry 0.
private const val DELTA_MOVE_SLOTS = 0x4 private const val DELTA_MOVE_SLOTS = 0x4
// The real server sends the experience alone, then a second delta on a level up.
private const val DELTA_EXPERIENCE = 0x1
private const val DELTA_LEVEL_UP = 0x2 or 0x8 or 0x80
private const val HP_TARGET_MOVE: Short = 0x0200 private const val HP_TARGET_MOVE: Short = 0x0200
private const val DEFAULT_TARGET_MOVE: Short = 0 private const val DEFAULT_TARGET_MOVE: Short = 0
@@ -184,21 +190,43 @@ class BattlePacketEmitter @Inject constructor(private val interestManager: Inter
} }
fun sendVictoryDelta(battle: BattleInstance, entityId: Long, reward: RewardResult) { fun sendVictoryDelta(battle: BattleInstance, entityId: Long, reward: RewardResult) {
var mask = 0x1
if (reward.leveled) mask = mask or 0x2 or 0x8
broadcast( broadcast(
battle, battle,
delta( delta(
entityId = entityId, entityId = entityId,
mask = mask, mask = DELTA_EXPERIENCE,
experienceLevel = reward.newLevel.toByte(), experienceLevel = reward.newLevel.toByte(),
experiencePoints = reward.newXp, experiencePoints = reward.newXp,
statValues = if (reward.leveled) reward.newStats.asWireList() else null,
currentHp = if (reward.leveled) reward.newCurrentHp.toShort() else null,
), ),
) )
// The delta above moves the bar, the reward text reads its number from here.
broadcast(battle, experienceReward(entityId, reward.xpGained))
if (!reward.leveled) return
broadcast(
battle,
delta(
entityId = entityId,
mask = DELTA_LEVEL_UP,
statValues = reward.newStats.asWireList(),
currentHp = reward.newCurrentHp.toShort(),
)
.copy(evValues = reward.newEvs.asWireList()),
)
} }
// The other six counters are rewards we do not award yet.
private fun experienceReward(entityId: Long, gained: Int): BattleStatCountersPacket =
BattleStatCountersPacket(
entityId = entityId,
baseCounter = gained,
counter1 = null,
counter2 = null,
counter3 = null,
counter4 = null,
counter5 = null,
counter6 = null,
)
fun sendNotice(battle: BattleInstance, message: String) { fun sendNotice(battle: BattleInstance, message: String) {
battle.session.send(notice(message)) battle.session.send(notice(message))
} }
@@ -239,45 +267,14 @@ class BattlePacketEmitter @Inject constructor(private val interestManager: Inter
currentHp: Short? = null, currentHp: Short? = null,
faintFlag: Byte? = null, faintFlag: Byte? = null,
): BattleEntityDeltaPacket = ): BattleEntityDeltaPacket =
BattleEntityDeltaPacket( entityDelta(
entityId = entityId, entityId = entityId,
fieldMask = mask, mask = mask,
experienceLevel = experienceLevel, experienceLevel = experienceLevel,
experiencePoints = experiencePoints, experiencePoints = experiencePoints,
statValues = statValues, statValues = statValues,
moveSlots = null,
ppUps = null,
currentHp = currentHp, currentHp = currentHp,
faintFlag = faintFlag, faintFlag = faintFlag,
speciesId = null,
forme = null,
listType = null,
sortKey = null,
evValues = null,
level = null,
happiness = null,
shininessSeed = null,
experience = null,
flagA = null,
flagB = null,
encounterType = null,
statusFlagsValue = null,
warnA = null,
warnB = null,
natureId = null,
ribbons = null,
packedIvs = null,
effortValues = null,
originalSpecies = null,
originalTrainerName = null,
originalTrainerId = null,
shininessType = null,
gender = null,
ivs = null,
caughtBall = null,
statusFlags2 = null,
statusList = null,
status = null,
) )
// The decomp battle stat order. Not verified against the live client yet. // The decomp battle stat order. Not verified against the live client yet.
@@ -294,10 +291,14 @@ class BattlePacketEmitter @Inject constructor(private val interestManager: Inter
} }
// The decomp in-game stat order. Not verified against the live client yet. // The decomp in-game stat order. Not verified against the live client yet.
private fun ComputedStats.asWireList(): List<Short> = private fun statOrder(hp: Int, atk: Int, def: Int, spd: Int, spAtk: Int, spDef: Int): List<Short> =
listOf( listOf(
hp.toShort(), atk.toShort(), def.toShort(), spd.toShort(), spAtk.toShort(), spDef.toShort()) hp.toShort(), atk.toShort(), def.toShort(), spd.toShort(), spAtk.toShort(), spDef.toShort())
internal fun ComputedStats.asWireList(): List<Short> = statOrder(hp, atk, def, spd, spAtk, spDef)
private fun EVs.asWireList(): List<Short> = statOrder(hp, atk, def, spd, spAtk, spDef)
fun notice(message: String): ChatMessagePacket = fun notice(message: String): ChatMessagePacket =
ChatMessagePacket( ChatMessagePacket(
type = ChatType.GAME_NOTIFICATIONS, type = ChatType.GAME_NOTIFICATIONS,
@@ -26,6 +26,7 @@ import de.fiereu.openmmo.server.game.battle.MoveLearner
import de.fiereu.openmmo.server.game.battle.StatCalculator import de.fiereu.openmmo.server.game.battle.StatCalculator
import de.fiereu.openmmo.server.game.battle.TurnEngine import de.fiereu.openmmo.server.game.battle.TurnEngine
import de.fiereu.openmmo.server.game.battle.WildMonFactory import de.fiereu.openmmo.server.game.battle.WildMonFactory
import de.fiereu.openmmo.server.game.battle.acquiredMonsterDelta
import de.fiereu.openmmo.server.game.battle.notice import de.fiereu.openmmo.server.game.battle.notice
import de.fiereu.openmmo.server.game.session.PLAYER_STATE import de.fiereu.openmmo.server.game.session.PLAYER_STATE
import de.fiereu.openmmo.server.game.storage.CharacterStore import de.fiereu.openmmo.server.game.storage.CharacterStore
@@ -338,6 +339,7 @@ constructor(
// The caught monster is sent as a full 148-byte record on opcode 0x14 before the ball-throw // The caught monster is sent as a full 148-byte record on opcode 0x14 before the ball-throw
// event, so the client can resolve the monster when the throw lands. // event, so the client can resolve the monster when the throw lands.
battle.session.send(SocialListEntryAddPacket(caught)) battle.session.send(SocialListEntryAddPacket(caught))
battle.session.send(acquiredMonsterDelta(caught, battle.wild.species))
// "Player threw a Poke Ball" event. // "Player threw a Poke Ball" event.
battle.session.send( battle.session.send(
BattleListEventPacket( BattleListEventPacket(
@@ -13,6 +13,7 @@ import de.fiereu.openmmo.pokemon.SpeciesRegistry
import de.fiereu.openmmo.server.game.battle.BattleRng import de.fiereu.openmmo.server.game.battle.BattleRng
import de.fiereu.openmmo.server.game.battle.StatCalculator import de.fiereu.openmmo.server.game.battle.StatCalculator
import de.fiereu.openmmo.server.game.battle.WildMonFactory import de.fiereu.openmmo.server.game.battle.WildMonFactory
import de.fiereu.openmmo.server.game.battle.acquiredMonsterDelta
import de.fiereu.openmmo.server.game.session.PlayerState import de.fiereu.openmmo.server.game.session.PlayerState
import de.fiereu.openmmo.server.game.storage.CharacterStore import de.fiereu.openmmo.server.game.storage.CharacterStore
import javax.inject.Inject import javax.inject.Inject
@@ -52,6 +53,14 @@ constructor(
characters.addPokemon(characterId, pokemon) characters.addPokemon(characterId, pokemon)
// Send the granted Pokemon's full record. // Send the granted Pokemon's full record.
session.send(SocialListEntryAddPacket(pokemon)) session.send(SocialListEntryAddPacket(pokemon))
species.get(dexId)?.let { session.send(acquiredMonsterDelta(pokemon, it)) }
session.send(
PokemonContainerPacket(
container = PokemonContainer.PARTY,
hasChange = true,
delete = false,
pokemon = characters.getCharacter(characterId)?.pokemon ?: listOf(pokemon),
))
return pokemon return pokemon
} }