refactor(protocols): derive the delta packet masks from the fields that are set (#103)

This commit is contained in:
Fiereu authored and GitHub committed 2026-08-07 22:09:34 +02:00
1 parent 67faf61498
commit f8a6ec2799
11 files changed
+378 -253

No files matched your search

@@ -2,74 +2,98 @@ package de.fiereu.openmmo.net.game.packets
import de.fiereu.bytecodec.* import de.fiereu.bytecodec.*
private const val MONEY = 0x1
private const val MAP = 0x2
private const val VALUE_4 = 0x4
private const val VALUE_8 = 0x8
private const val VALUE_16 = 0x10
private const val VALUE_32 = 0x20
private const val VALUE_64 = 0x40
private const val STATUS_CONDITIONS = 0x80
private const val VALUE_256 = 0x100
data class MapLocation(val mapId: Short, val mapFlag: Byte)
data class Value8Group(val a: Byte, val b: Byte, val c: Byte)
data class Value16Group(val a: Short, val b: Short)
/** [a] and [b] are only on the wire when [kind] is not zero. */
data class Value64Group(val kind: Byte, val a: Short?, val b: Short?)
/** A partial update to the player's own character. A null group is left off the wire. */
data class LocalCharacterDeltaPacket( data class LocalCharacterDeltaPacket(
val mask: Short, val money: Int? = null,
val money: Int?, val map: MapLocation? = null,
val mapId: Short?, val value4: Short? = null,
val mapFlag: Byte?, val value8: Value8Group? = null,
val value4: Short?, val value16: Value16Group? = null,
val value8a: Byte?, val value32: Int? = null,
val value8b: Byte?, val value64: Value64Group? = null,
val value8c: Byte?, val statusConditions: List<Byte>? = null,
val value16a: Short?, val value256: Byte? = null,
val value16b: Short?,
val value32: Int?,
val value64Enum: Byte?,
val value64a: Short?,
val value64b: Short?,
val statusConditions: List<Byte>?,
val value256: Byte?,
) )
private fun LocalCharacterDeltaPacket.mask(): Short {
var m = 0
if (money != null) m = m or MONEY
if (map != null) m = m or MAP
if (value4 != null) m = m or VALUE_4
if (value8 != null) m = m or VALUE_8
if (value16 != null) m = m or VALUE_16
if (value32 != null) m = m or VALUE_32
if (value64 != null) m = m or VALUE_64
if (statusConditions != null) m = m or STATUS_CONDITIONS
if (value256 != null) m = m or VALUE_256
return m.toShort()
}
object LocalCharacterDeltaPacketCodec : PacketCodec<LocalCharacterDeltaPacket>() { object LocalCharacterDeltaPacketCodec : PacketCodec<LocalCharacterDeltaPacket>() {
override fun CodecScope<LocalCharacterDeltaPacket>.body(): LocalCharacterDeltaPacket { override fun CodecScope<LocalCharacterDeltaPacket>.body(): LocalCharacterDeltaPacket {
val mask = field(S16LE) { it.mask } val m = field(S16LE) { it.mask() }.toInt()
val m = mask.toInt()
val money = if (m and 1 != 0) field(S32LE) { it.money!! } else null val money = optionalField(m and MONEY != 0, S32LE) { it.money }
val mapId = if (m and 2 != 0) field(S16LE) { it.mapId!! } else null val map =
val mapFlag = if (m and 2 != 0) field(S8) { it.mapFlag!! } else null if (m and MAP != 0)
val value4 = if (m and 4 != 0) field(S16LE) { it.value4!! } else null MapLocation(field(S16LE) { it.map!!.mapId }, field(S8) { it.map!!.mapFlag })
val value8a = if (m and 8 != 0) field(S8) { it.value8a!! } else null else null
val value8b = if (m and 8 != 0) field(S8) { it.value8b!! } else null val value4 = optionalField(m and VALUE_4 != 0, S16LE) { it.value4 }
val value8c = if (m and 8 != 0) field(S8) { it.value8c!! } else null val value8 =
val value16a = if (m and 16 != 0) field(S16LE) { it.value16a!! } else null if (m and VALUE_8 != 0)
val value16b = if (m and 16 != 0) field(S16LE) { it.value16b!! } else null Value8Group(
val value32 = if (m and 32 != 0) field(S32LE) { it.value32!! } else null field(S8) { it.value8!!.a },
val value64Enum: Byte? field(S8) { it.value8!!.b },
val value64a: Short? field(S8) { it.value8!!.c },
val value64b: Short? )
if (m and 64 != 0) { else null
value64Enum = field(S8) { it.value64Enum!! } val value16 =
if (value64Enum.toInt() != 0) { if (m and VALUE_16 != 0)
value64a = field(S16LE) { it.value64a!! } Value16Group(field(S16LE) { it.value16!!.a }, field(S16LE) { it.value16!!.b })
value64b = field(S16LE) { it.value64b!! } else null
} else { val value32 = optionalField(m and VALUE_32 != 0, S32LE) { it.value32 }
value64a = null val value64 =
value64b = null if (m and VALUE_64 != 0) {
} val kind = field(S8) { it.value64!!.kind }
} else { if (kind.toInt() != 0)
value64Enum = null Value64Group(
value64a = null kind,
value64b = null field(S16LE) { it.value64!!.a!! },
} field(S16LE) { it.value64!!.b!! },
)
else Value64Group(kind, null, null)
} else null
val statusConditions = val statusConditions =
if (m and 128 != 0) field(S8.listPrefixed(U8)) { it.statusConditions!! } else null optionalField(m and STATUS_CONDITIONS != 0, S8.listPrefixed(U8)) { it.statusConditions }
val value256 = if (m and 256 != 0) field(S8) { it.value256!! } else null val value256 = optionalField(m and VALUE_256 != 0, S8) { it.value256 }
return LocalCharacterDeltaPacket( return LocalCharacterDeltaPacket(
mask = mask,
money = money, money = money,
mapId = mapId, map = map,
mapFlag = mapFlag,
value4 = value4, value4 = value4,
value8a = value8a, value8 = value8,
value8b = value8b, value16 = value16,
value8c = value8c,
value16a = value16a,
value16b = value16b,
value32 = value32, value32 = value32,
value64Enum = value64Enum, value64 = value64,
value64a = value64a,
value64b = value64b,
statusConditions = statusConditions, statusConditions = statusConditions,
value256 = value256, value256 = value256,
) )
@@ -4,131 +4,210 @@ import de.fiereu.bytecodec.*
private val MoveSlotCodec: Codec<Pair<Short, Byte>> = S16LE.then(S8) private val MoveSlotCodec: Codec<Pair<Short, Byte>> = S16LE.then(S8)
// One bit per group, set when the group is.
private const val EXPERIENCE = 0x1
private const val STATS = 0x2
private const val MOVES = 0x4
private const val CURRENT_HP = 0x8
private const val FAINT = 0x10
private const val SPECIES = 0x20
private const val LISTING = 0x40
private const val EVS = 0x80
private const val LEVEL = 0x100
private const val HAPPINESS = 0x200
private const val POSITION = 0x400
private const val STATUS_FLAGS = 0x800
private const val MAP_POSITION = 0x1000
private const val NATURE = 0x2000
private const val VALUE_64 = 0x4000
private const val ORIGIN = 0x8000
private const val SHININESS_TYPE = 0x10000
private const val GENDER = 0x20000
private const val IVS = 0x40000
private const val CAUGHT_BALL = 0x80000
private const val STATUS_FLAGS_2 = 0x100000
private const val PACKED_IVS = 0x200000
private const val STATUS_LIST = 0x400000
private const val STATUS = 0x800000
data class Experience(val level: Byte, val points: Int)
data class MoveSlots(val slots: List<Pair<Short, Byte>>, val ppUps: Byte)
data class Species(val speciesId: Short, val forme: Byte)
data class Listing(val listType: Byte, val sortKey: Short)
data class Position(val x: Int, val y: Int, val flagA: Byte, val flagB: Byte, val flagC: Byte)
/** Map square the client turns into a location name. */
data class MapPosition(val mapX: Short, val mapY: Short)
data class Origin(
val effortValues: List<Short>,
val species: Long,
val trainerName: String,
val trainerId: Int,
)
/** A partial update to one entity. A null group is left off the wire. */
data class BattleEntityDeltaPacket( data class BattleEntityDeltaPacket(
val entityId: Long, val entityId: Long,
val fieldMask: Int, val experience: Experience? = null,
val experienceLevel: Byte?, val statValues: List<Short>? = null,
val experiencePoints: Int?, val moves: MoveSlots? = null,
val statValues: List<Short>?, // A capture shows this matching the monster's hp. The client calls it a level.
val moveSlots: List<Pair<Short, Byte>>?, val currentHp: Short? = null,
val ppUps: Byte?, val faintFlag: Byte? = null,
val currentHp: Short?, val species: Species? = null,
val faintFlag: Byte?, // The client reads these as a facing and a heading. No capture either way yet.
val speciesId: Short?, val listing: Listing? = null,
val forme: Byte?, val evValues: List<Short>? = null,
val listType: Byte?, val level: Short? = null,
val sortKey: Short?, val happiness: Short? = null,
val evValues: List<Short>?, val position: Position? = null,
val level: Short?, val statusFlagsValue: Short? = null,
val happiness: Short?, val mapPosition: MapPosition? = null,
val shininessSeed: Int?, val natureId: Byte? = null,
val experience: Int?, val value64: Long? = null,
val flagA: Byte?, val origin: Origin? = null,
val flagB: Byte?, val shininessType: Byte? = null,
val encounterType: Byte?, val gender: Byte? = null,
val statusFlagsValue: Short?, val ivs: List<Short>? = null,
val warnA: Short?, val caughtBall: Byte? = null,
val warnB: Short?, val statusFlags2: Short? = null,
val natureId: Byte?, val packedIvs: Int? = null,
val ribbons: Long?, val statusList: List<Byte>? = null,
val packedIvs: Int?, val status: Byte? = null,
val effortValues: List<Short>?,
val originalSpecies: Long?,
val originalTrainerName: String?,
val originalTrainerId: Int?,
val shininessType: Byte?,
val gender: Byte?,
val ivs: List<Short>?,
val caughtBall: Byte?,
val statusFlags2: Short?,
val statusList: List<Byte>?,
val status: Byte?,
) )
private fun BattleEntityDeltaPacket.mask(): Int {
var m = 0
if (experience != null) m = m or EXPERIENCE
if (statValues != null) m = m or STATS
if (moves != null) m = m or MOVES
if (currentHp != null) m = m or CURRENT_HP
if (faintFlag != null) m = m or FAINT
if (species != null) m = m or SPECIES
if (listing != null) m = m or LISTING
if (evValues != null) m = m or EVS
if (level != null) m = m or LEVEL
if (happiness != null) m = m or HAPPINESS
if (position != null) m = m or POSITION
if (statusFlagsValue != null) m = m or STATUS_FLAGS
if (mapPosition != null) m = m or MAP_POSITION
if (natureId != null) m = m or NATURE
if (value64 != null) m = m or VALUE_64
if (origin != null) m = m or ORIGIN
if (shininessType != null) m = m or SHININESS_TYPE
if (gender != null) m = m or GENDER
if (ivs != null) m = m or IVS
if (caughtBall != null) m = m or CAUGHT_BALL
if (statusFlags2 != null) m = m or STATUS_FLAGS_2
if (packedIvs != null) m = m or PACKED_IVS
if (statusList != null) m = m or STATUS_LIST
if (status != null) m = m or STATUS
return m
}
object BattleEntityDeltaPacketCodec : PacketCodec<BattleEntityDeltaPacket>() { object BattleEntityDeltaPacketCodec : PacketCodec<BattleEntityDeltaPacket>() {
override fun CodecScope<BattleEntityDeltaPacket>.body(): BattleEntityDeltaPacket { override fun CodecScope<BattleEntityDeltaPacket>.body(): BattleEntityDeltaPacket {
val entityId = field(S64LE) { it.entityId } val entityId = field(S64LE) { it.entityId }
val fieldMask = field(S32LE) { it.fieldMask } val m = field(S32LE) { it.mask() }
val m = fieldMask
val experienceLevel = if (m and 1 != 0) field(S8) { it.experienceLevel!! } else null val experience =
val experiencePoints = if (m and 1 != 0) field(S32LE) { it.experiencePoints!! } else null if (m and EXPERIENCE != 0)
val statValues = if (m and 2 != 0) field(S16LE.repeat(6)) { it.statValues!! } else null Experience(
val moveSlots = if (m and 4 != 0) field(MoveSlotCodec.repeat(4)) { it.moveSlots!! } else null field(S8) { it.experience!!.level },
val ppUps = if (m and 4 != 0) field(S8) { it.ppUps!! } else null field(S32LE) { it.experience!!.points },
val currentHp = if (m and 8 != 0) field(S16LE) { it.currentHp!! } else null )
val faintFlag = if (m and 16 != 0) field(S8) { it.faintFlag!! } else null else null
val speciesId = if (m and 32 != 0) field(S16LE) { it.speciesId!! } else null val statValues = optionalField(m and STATS != 0, S16LE.repeat(6)) { it.statValues }
val forme = if (m and 32 != 0) field(S8) { it.forme!! } else null val moves =
val listType = if (m and 64 != 0) field(S8) { it.listType!! } else null if (m and MOVES != 0)
val sortKey = if (m and 64 != 0) field(S16LE) { it.sortKey!! } else null MoveSlots(
val evValues = if (m and 128 != 0) field(S16LE.repeat(6)) { it.evValues!! } else null field(MoveSlotCodec.repeat(4)) { it.moves!!.slots },
val level = if (m and 256 != 0) field(S16LE) { it.level!! } else null field(S8) { it.moves!!.ppUps },
val happiness = if (m and 512 != 0) field(S16LE) { it.happiness!! } else null )
val shininessSeed = if (m and 1024 != 0) field(S32LE) { it.shininessSeed!! } else null else null
val experience = if (m and 1024 != 0) field(S32LE) { it.experience!! } else null val currentHp = optionalField(m and CURRENT_HP != 0, S16LE) { it.currentHp }
val flagA = if (m and 1024 != 0) field(S8) { it.flagA!! } else null val faintFlag = optionalField(m and FAINT != 0, S8) { it.faintFlag }
val flagB = if (m and 1024 != 0) field(S8) { it.flagB!! } else null val species =
val encounterType = if (m and 1024 != 0) field(S8) { it.encounterType!! } else null if (m and SPECIES != 0)
val statusFlagsValue = if (m and 2048 != 0) field(S16LE) { it.statusFlagsValue!! } else null Species(field(S16LE) { it.species!!.speciesId }, field(S8) { it.species!!.forme })
val warnA = if (m and 4096 != 0) field(S16LE) { it.warnA!! } else null else null
val warnB = if (m and 4096 != 0) field(S16LE) { it.warnB!! } else null val listing =
val natureId = if (m and 8192 != 0) field(S8) { it.natureId!! } else null if (m and LISTING != 0)
val ribbons = if (m and 16384 != 0) field(S64LE) { it.ribbons!! } else null Listing(field(S8) { it.listing!!.listType }, field(S16LE) { it.listing!!.sortKey })
val packedIvs = if (m and 2097152 != 0) field(S32LE) { it.packedIvs!! } else null else null
val evValues = optionalField(m and EVS != 0, S16LE.repeat(6)) { it.evValues }
val level = optionalField(m and LEVEL != 0, S16LE) { it.level }
val happiness = optionalField(m and HAPPINESS != 0, S16LE) { it.happiness }
val position =
if (m and POSITION != 0)
Position(
field(S32LE) { it.position!!.x },
field(S32LE) { it.position!!.y },
field(S8) { it.position!!.flagA },
field(S8) { it.position!!.flagB },
field(S8) { it.position!!.flagC },
)
else null
val statusFlagsValue = optionalField(m and STATUS_FLAGS != 0, S16LE) { it.statusFlagsValue }
val mapPosition =
if (m and MAP_POSITION != 0)
MapPosition(
field(S16LE) { it.mapPosition!!.mapX },
field(S16LE) { it.mapPosition!!.mapY },
)
else null
val natureId = optionalField(m and NATURE != 0, S8) { it.natureId }
val value64 = optionalField(m and VALUE_64 != 0, S64LE) { it.value64 }
// Read out of bit order, between 0x4000 and 0x8000. Confirmed against the client.
val packedIvs = optionalField(m and PACKED_IVS != 0, S32LE) { it.packedIvs }
val effortValues = if (m and 32768 != 0) field(S16LE.repeat(4)) { it.effortValues!! } else null val origin =
val originalSpecies = if (m and 32768 != 0) field(S64LE) { it.originalSpecies!! } else null if (m and ORIGIN != 0)
val originalTrainerName = Origin(
if (m and 32768 != 0) field(Utf16LeNullTerminated) { it.originalTrainerName!! } else null field(S16LE.repeat(4)) { it.origin!!.effortValues },
val originalTrainerId = if (m and 32768 != 0) field(S32LE) { it.originalTrainerId!! } else null field(S64LE) { it.origin!!.species },
field(Utf16LeNullTerminated) { it.origin!!.trainerName },
field(S32LE) { it.origin!!.trainerId },
)
else null
val shininessType = if (m and 65536 != 0) field(S8) { it.shininessType!! } else null val shininessType = optionalField(m and SHININESS_TYPE != 0, S8) { it.shininessType }
val gender = if (m and 131072 != 0) field(S8) { it.gender!! } else null val gender = optionalField(m and GENDER != 0, S8) { it.gender }
val ivs = if (m and 262144 != 0) field(S16LE.repeat(5)) { it.ivs!! } else null val ivs = optionalField(m and IVS != 0, S16LE.repeat(5)) { it.ivs }
val caughtBall = if (m and 524288 != 0) field(S8) { it.caughtBall!! } else null val caughtBall = optionalField(m and CAUGHT_BALL != 0, S8) { it.caughtBall }
val statusFlags2 = if (m and 1048576 != 0) field(S16LE) { it.statusFlags2!! } else null val statusFlags2 = optionalField(m and STATUS_FLAGS_2 != 0, S16LE) { it.statusFlags2 }
val statusList = val statusList = optionalField(m and STATUS_LIST != 0, S8.listPrefixed(U8)) { it.statusList }
if (m and 4194304 != 0) field(S8.listPrefixed(U8)) { it.statusList!! } else null val status = optionalField(m and STATUS != 0, S8) { it.status }
val status = if (m and 8388608 != 0) field(S8) { it.status!! } else null
return BattleEntityDeltaPacket( return BattleEntityDeltaPacket(
entityId = entityId, entityId = entityId,
fieldMask = fieldMask, experience = experience,
experienceLevel = experienceLevel,
experiencePoints = experiencePoints,
statValues = statValues, statValues = statValues,
moveSlots = moveSlots, moves = moves,
ppUps = ppUps,
currentHp = currentHp, currentHp = currentHp,
faintFlag = faintFlag, faintFlag = faintFlag,
speciesId = speciesId, species = species,
forme = forme, listing = listing,
listType = listType,
sortKey = sortKey,
evValues = evValues, evValues = evValues,
level = level, level = level,
happiness = happiness, happiness = happiness,
shininessSeed = shininessSeed, position = position,
experience = experience,
flagA = flagA,
flagB = flagB,
encounterType = encounterType,
statusFlagsValue = statusFlagsValue, statusFlagsValue = statusFlagsValue,
warnA = warnA, mapPosition = mapPosition,
warnB = warnB,
natureId = natureId, natureId = natureId,
ribbons = ribbons, value64 = value64,
packedIvs = packedIvs, origin = origin,
effortValues = effortValues,
originalSpecies = originalSpecies,
originalTrainerName = originalTrainerName,
originalTrainerId = originalTrainerId,
shininessType = shininessType, shininessType = shininessType,
gender = gender, gender = gender,
ivs = ivs, ivs = ivs,
caughtBall = caughtBall, caughtBall = caughtBall,
statusFlags2 = statusFlags2, statusFlags2 = statusFlags2,
packedIvs = packedIvs,
statusList = statusList, statusList = statusList,
status = status, status = status,
) )
@@ -0,0 +1,102 @@
package de.fiereu.openmmo.net.game
import de.fiereu.openmmo.common.test.assertBytesRoundtrip
import de.fiereu.openmmo.common.test.assertValueRoundtrip
import de.fiereu.openmmo.common.test.decodeBytes
import de.fiereu.openmmo.common.test.encodeToBytes
import de.fiereu.openmmo.common.test.fixture
import de.fiereu.openmmo.net.game.packets.LocalCharacterDeltaPacket
import de.fiereu.openmmo.net.game.packets.LocalCharacterDeltaPacketCodec
import de.fiereu.openmmo.net.game.packets.battle.BattleEntityDeltaPacket
import de.fiereu.openmmo.net.game.packets.battle.BattleEntityDeltaPacketCodec
import de.fiereu.openmmo.net.game.packets.battle.Experience
import io.kotest.core.spec.style.FunSpec
import io.kotest.matchers.nulls.shouldBeNull
import io.kotest.matchers.shouldBe
private const val ENTITY_ID = 0x1AD03598_0408C000L
private val CAPTURED =
listOf(
"experience_32763.bin",
"level_up_32763.bin",
"move_slots_32763.bin",
"acquired_32763.bin",
)
private fun readDelta(name: String): BattleEntityDeltaPacket =
BattleEntityDeltaPacketCodec.decodeBytes(fixture("game/s2c/16/$name"))
class BattleEntityDeltaPacketTest :
FunSpec({
test("an experience delta carries only the level and the running total") {
val packet = readDelta("experience_32763.bin")
packet.entityId shouldBe ENTITY_ID
packet.experience shouldBe Experience(level = 14, points = 1644)
packet.statValues.shouldBeNull()
packet.currentHp.shouldBeNull()
}
test("a level up delta carries stats, hp and EVs but no experience") {
val packet = readDelta("level_up_32763.bin")
packet.experience.shouldBeNull()
packet.statValues shouldBe listOf<Short>(39, 22, 20, 27, 25, 22)
packet.currentHp shouldBe 12
packet.evValues shouldBe listOf<Short>(5, 2, 14, 9, 0, 1)
}
test("a move slot delta carries four slots, pp ups, hp and the faint flag") {
val packet = readDelta("move_slots_32763.bin")
packet.moves?.slots shouldBe
listOf<Pair<Short, Byte>>(
225.toShort() to 20,
232.toShort() to 35,
108.toShort() to 20,
52.toShort() to 25,
)
packet.moves?.ppUps shouldBe 0
packet.currentHp shouldBe 39
packet.faintFlag shouldBe 0
}
test("an acquired monster delta carries experience, stats, hp, faint and happiness") {
val packet = readDelta("acquired_32763.bin")
packet.experience shouldBe Experience(level = 8, points = 320)
packet.statValues shouldBe listOf<Short>(25, 14, 13, 16, 15, 14)
packet.currentHp shouldBe 14
packet.faintFlag shouldBe 0
packet.happiness shouldBe 75
}
// Only passes if every derived bit matches the one the real server sent.
test("every captured delta re-encodes to the same bytes") {
CAPTURED.forEach {
BattleEntityDeltaPacketCodec.assertBytesRoundtrip(fixture("game/s2c/16/$it"))
}
}
test("setting a group puts its fields on the wire and nothing else") {
val packet =
BattleEntityDeltaPacket(
entityId = ENTITY_ID,
experience = Experience(level = 6, points = 197),
currentHp = 5,
)
// Entity id, mask, level, points, hp.
BattleEntityDeltaPacketCodec.encodeToBytes(packet).size shouldBe (8 + 4 + 1 + 4 + 2)
BattleEntityDeltaPacketCodec.assertValueRoundtrip(packet)
}
test("a local character delta carries the money it was given") {
val bytes = fixture("game/s2c/0c/money_32763.bin")
LocalCharacterDeltaPacketCodec.decodeBytes(bytes) shouldBe
LocalCharacterDeltaPacket(money = 1900)
LocalCharacterDeltaPacketCodec.assertBytesRoundtrip(bytes)
}
})
@@ -2,72 +2,18 @@ package de.fiereu.openmmo.server.game.battle
import de.fiereu.openmmo.common.Pokemon import de.fiereu.openmmo.common.Pokemon
import de.fiereu.openmmo.net.game.packets.battle.BattleEntityDeltaPacket import de.fiereu.openmmo.net.game.packets.battle.BattleEntityDeltaPacket
import de.fiereu.openmmo.net.game.packets.battle.Experience
import de.fiereu.openmmo.pokemon.SpeciesDef 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. */ /** Tells the client about a monster the player just obtained. */
fun acquiredMonsterDelta(mon: Pokemon, species: SpeciesDef): BattleEntityDeltaPacket { fun acquiredMonsterDelta(mon: Pokemon, species: SpeciesDef): BattleEntityDeltaPacket {
val stats = StatCalculator.computeAll(species, mon) val stats = StatCalculator.computeAll(species, mon)
return entityDelta( // The real server also sends a happiness here, which we do not model.
return BattleEntityDeltaPacket(
entityId = mon.id, entityId = mon.id,
mask = ACQUIRED_MASK, experience = Experience(mon.level, mon.xp),
experienceLevel = mon.level,
experiencePoints = mon.xp,
statValues = stats.asWireList(), statValues = stats.asWireList(),
currentHp = mon.hp, currentHp = mon.hp,
faintFlag = 0, 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,
)
@@ -23,6 +23,8 @@ 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.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.net.game.packets.battle.Experience
import de.fiereu.openmmo.net.game.packets.battle.MoveSlots
import de.fiereu.openmmo.server.game.world.interest.InterestManager import de.fiereu.openmmo.server.game.world.interest.InterestManager
import de.fiereu.openmmo.typechart.TypeChart import de.fiereu.openmmo.typechart.TypeChart
import javax.inject.Inject import javax.inject.Inject
@@ -43,12 +45,6 @@ private const val PLAYER_SIDE: Byte = 1
// The target move short the live server sends for each event. Meaning unknown, but it is fixed per // The target move short the live server sends for each event. Meaning unknown, but it is fixed per
// 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
// 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
@@ -192,11 +188,9 @@ class BattlePacketEmitter @Inject constructor(private val interestManager: Inter
fun sendVictoryDelta(battle: BattleInstance, entityId: Long, reward: RewardResult) { fun sendVictoryDelta(battle: BattleInstance, entityId: Long, reward: RewardResult) {
broadcast( broadcast(
battle, battle,
delta( BattleEntityDeltaPacket(
entityId = entityId, entityId = entityId,
mask = DELTA_EXPERIENCE, experience = Experience(reward.newLevel.toByte(), reward.newXp),
experienceLevel = reward.newLevel.toByte(),
experiencePoints = reward.newXp,
), ),
) )
// The delta above moves the bar, the reward text reads its number from here. // The delta above moves the bar, the reward text reads its number from here.
@@ -204,13 +198,12 @@ class BattlePacketEmitter @Inject constructor(private val interestManager: Inter
if (!reward.leveled) return if (!reward.leveled) return
broadcast( broadcast(
battle, battle,
delta( BattleEntityDeltaPacket(
entityId = entityId, entityId = entityId,
mask = DELTA_LEVEL_UP, statValues = reward.newStats.asWireList(),
statValues = reward.newStats.asWireList(), currentHp = reward.newCurrentHp.toShort(),
currentHp = reward.newCurrentHp.toShort(), evValues = reward.newEvs.asWireList(),
) ),
.copy(evValues = reward.newEvs.asWireList()),
) )
} }
@@ -256,26 +249,7 @@ class BattlePacketEmitter @Inject constructor(private val interestManager: Inter
moveSlots: List<Pair<Short, Byte>>, moveSlots: List<Pair<Short, Byte>>,
ppUps: Byte, ppUps: Byte,
): BattleEntityDeltaPacket = ): BattleEntityDeltaPacket =
delta(entityId, DELTA_MOVE_SLOTS).copy(moveSlots = moveSlots, ppUps = ppUps) BattleEntityDeltaPacket(entityId = entityId, moves = MoveSlots(moveSlots, ppUps))
fun delta(
entityId: Long,
mask: Int,
experienceLevel: Byte? = null,
experiencePoints: Int? = null,
statValues: List<Short>? = null,
currentHp: Short? = null,
faintFlag: Byte? = null,
): BattleEntityDeltaPacket =
entityDelta(
entityId = entityId,
mask = mask,
experienceLevel = experienceLevel,
experiencePoints = experiencePoints,
statValues = statValues,
currentHp = currentHp,
faintFlag = faintFlag,
)
// The decomp battle stat order. Not verified against the live client yet. // The decomp battle stat order. Not verified against the live client yet.
private fun statIndex(stat: BattleStat): Byte = private fun statIndex(stat: BattleStat): Byte =
@@ -225,7 +225,7 @@ class BattleServiceTest :
// The level 2 Rattata yields its base experience scaled by level over seven. // The level 2 Rattata yields its base experience scaled by level over seven.
session.sent session.sent
.filterIsInstance<BattleEntityDeltaPacket>() .filterIsInstance<BattleEntityDeltaPacket>()
.any { it.fieldMask and 0x1 != 0 } .any { it.experience != null }
.shouldBeTrue() .shouldBeTrue()
val saved = fx.repo.saved[charId].shouldNotBeNull() val saved = fx.repo.saved[charId].shouldNotBeNull()
saved.pokemon.single().moves[0].pp shouldBe (35 - rounds).toByte() saved.pokemon.single().moves[0].pp shouldBe (35 - rounds).toByte()