From f8a6ec27999f283c2f02fe464bd4b989c8c75743 Mon Sep 17 00:00:00 2001 From: Fiereu Date: Fri, 7 Aug 2026 22:09:34 +0200 Subject: [PATCH] refactor(protocols): derive the delta packet masks from the fields that are set (#103) --- .../game/packets/LocalCharacterDeltaPacket.kt | 140 +++++---- .../packets/battle/BattleEntityDeltaPacket.kt | 277 +++++++++++------- .../net/game/BattleEntityDeltaPacketTest.kt | 102 +++++++ .../fixtures/game/s2c/0c/money_32763.bin | Bin 0 -> 6 bytes .../fixtures/game/s2c/16/acquired_32763.bin | Bin 0 -> 34 bytes .../fixtures/game/s2c/16/experience_32763.bin | Bin 0 -> 17 bytes .../fixtures/game/s2c/16/level_up_32763.bin | Bin 0 -> 38 bytes .../fixtures/game/s2c/16/move_slots_32763.bin | Bin 0 -> 28 bytes .../server/game/battle/AcquiredMonster.kt | 62 +--- .../server/game/battle/BattlePacketEmitter.kt | 48 +-- .../server/game/services/BattleServiceTest.kt | 2 +- 11 files changed, 378 insertions(+), 253 deletions(-) create mode 100644 protocols.game/src/test/kotlin/de/fiereu/openmmo/net/game/BattleEntityDeltaPacketTest.kt create mode 100644 protocols.game/src/test/resources/fixtures/game/s2c/0c/money_32763.bin create mode 100644 protocols.game/src/test/resources/fixtures/game/s2c/16/acquired_32763.bin create mode 100644 protocols.game/src/test/resources/fixtures/game/s2c/16/experience_32763.bin create mode 100644 protocols.game/src/test/resources/fixtures/game/s2c/16/level_up_32763.bin create mode 100644 protocols.game/src/test/resources/fixtures/game/s2c/16/move_slots_32763.bin diff --git a/protocols.game/src/main/kotlin/de/fiereu/openmmo/net/game/packets/LocalCharacterDeltaPacket.kt b/protocols.game/src/main/kotlin/de/fiereu/openmmo/net/game/packets/LocalCharacterDeltaPacket.kt index bf58527..c8f5145 100644 --- a/protocols.game/src/main/kotlin/de/fiereu/openmmo/net/game/packets/LocalCharacterDeltaPacket.kt +++ b/protocols.game/src/main/kotlin/de/fiereu/openmmo/net/game/packets/LocalCharacterDeltaPacket.kt @@ -2,74 +2,98 @@ package de.fiereu.openmmo.net.game.packets 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( - val mask: Short, - val money: Int?, - val mapId: Short?, - val mapFlag: Byte?, - val value4: Short?, - val value8a: Byte?, - val value8b: Byte?, - val value8c: Byte?, - val value16a: Short?, - val value16b: Short?, - val value32: Int?, - val value64Enum: Byte?, - val value64a: Short?, - val value64b: Short?, - val statusConditions: List?, - val value256: Byte?, + val money: Int? = null, + val map: MapLocation? = null, + val value4: Short? = null, + val value8: Value8Group? = null, + val value16: Value16Group? = null, + val value32: Int? = null, + val value64: Value64Group? = null, + val statusConditions: List? = null, + val value256: Byte? = null, ) +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() { override fun CodecScope.body(): LocalCharacterDeltaPacket { - val mask = field(S16LE) { it.mask } - val m = mask.toInt() - val money = if (m and 1 != 0) field(S32LE) { it.money!! } else null - val mapId = if (m and 2 != 0) field(S16LE) { it.mapId!! } else null - val mapFlag = if (m and 2 != 0) field(S8) { it.mapFlag!! } else null - val value4 = if (m and 4 != 0) field(S16LE) { it.value4!! } else null - val value8a = if (m and 8 != 0) field(S8) { it.value8a!! } else null - val value8b = if (m and 8 != 0) field(S8) { it.value8b!! } else null - val value8c = if (m and 8 != 0) field(S8) { it.value8c!! } else null - val value16a = if (m and 16 != 0) field(S16LE) { it.value16a!! } else null - val value16b = if (m and 16 != 0) field(S16LE) { it.value16b!! } else null - val value32 = if (m and 32 != 0) field(S32LE) { it.value32!! } else null - val value64Enum: Byte? - val value64a: Short? - val value64b: Short? - if (m and 64 != 0) { - value64Enum = field(S8) { it.value64Enum!! } - if (value64Enum.toInt() != 0) { - value64a = field(S16LE) { it.value64a!! } - value64b = field(S16LE) { it.value64b!! } - } else { - value64a = null - value64b = null - } - } else { - value64Enum = null - value64a = null - value64b = null - } + val m = field(S16LE) { it.mask() }.toInt() + + val money = optionalField(m and MONEY != 0, S32LE) { it.money } + val map = + if (m and MAP != 0) + MapLocation(field(S16LE) { it.map!!.mapId }, field(S8) { it.map!!.mapFlag }) + else null + val value4 = optionalField(m and VALUE_4 != 0, S16LE) { it.value4 } + val value8 = + if (m and VALUE_8 != 0) + Value8Group( + field(S8) { it.value8!!.a }, + field(S8) { it.value8!!.b }, + field(S8) { it.value8!!.c }, + ) + else null + val value16 = + if (m and VALUE_16 != 0) + Value16Group(field(S16LE) { it.value16!!.a }, field(S16LE) { it.value16!!.b }) + else null + val value32 = optionalField(m and VALUE_32 != 0, S32LE) { it.value32 } + val value64 = + if (m and VALUE_64 != 0) { + val kind = field(S8) { it.value64!!.kind } + if (kind.toInt() != 0) + Value64Group( + kind, + field(S16LE) { it.value64!!.a!! }, + field(S16LE) { it.value64!!.b!! }, + ) + else Value64Group(kind, null, null) + } else null val statusConditions = - if (m and 128 != 0) field(S8.listPrefixed(U8)) { it.statusConditions!! } else null - val value256 = if (m and 256 != 0) field(S8) { it.value256!! } else null + optionalField(m and STATUS_CONDITIONS != 0, S8.listPrefixed(U8)) { it.statusConditions } + val value256 = optionalField(m and VALUE_256 != 0, S8) { it.value256 } + return LocalCharacterDeltaPacket( - mask = mask, money = money, - mapId = mapId, - mapFlag = mapFlag, + map = map, value4 = value4, - value8a = value8a, - value8b = value8b, - value8c = value8c, - value16a = value16a, - value16b = value16b, + value8 = value8, + value16 = value16, value32 = value32, - value64Enum = value64Enum, - value64a = value64a, - value64b = value64b, + value64 = value64, statusConditions = statusConditions, value256 = value256, ) diff --git a/protocols.game/src/main/kotlin/de/fiereu/openmmo/net/game/packets/battle/BattleEntityDeltaPacket.kt b/protocols.game/src/main/kotlin/de/fiereu/openmmo/net/game/packets/battle/BattleEntityDeltaPacket.kt index 92fc253..a2ba5c6 100644 --- a/protocols.game/src/main/kotlin/de/fiereu/openmmo/net/game/packets/battle/BattleEntityDeltaPacket.kt +++ b/protocols.game/src/main/kotlin/de/fiereu/openmmo/net/game/packets/battle/BattleEntityDeltaPacket.kt @@ -4,131 +4,210 @@ import de.fiereu.bytecodec.* private val MoveSlotCodec: Codec> = 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>, 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, + 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( val entityId: Long, - val fieldMask: Int, - val experienceLevel: Byte?, - val experiencePoints: Int?, - val statValues: List?, - val moveSlots: List>?, - val ppUps: Byte?, - val currentHp: Short?, - val faintFlag: Byte?, - val speciesId: Short?, - val forme: Byte?, - val listType: Byte?, - val sortKey: Short?, - val evValues: List?, - val level: Short?, - val happiness: Short?, - val shininessSeed: Int?, - val experience: Int?, - val flagA: Byte?, - val flagB: Byte?, - val encounterType: Byte?, - val statusFlagsValue: Short?, - val warnA: Short?, - val warnB: Short?, - val natureId: Byte?, - val ribbons: Long?, - val packedIvs: Int?, - val effortValues: List?, - val originalSpecies: Long?, - val originalTrainerName: String?, - val originalTrainerId: Int?, - val shininessType: Byte?, - val gender: Byte?, - val ivs: List?, - val caughtBall: Byte?, - val statusFlags2: Short?, - val statusList: List?, - val status: Byte?, + val experience: Experience? = null, + val statValues: List? = null, + val moves: MoveSlots? = null, + // A capture shows this matching the monster's hp. The client calls it a level. + val currentHp: Short? = null, + val faintFlag: Byte? = null, + val species: Species? = null, + // The client reads these as a facing and a heading. No capture either way yet. + val listing: Listing? = null, + val evValues: List? = null, + val level: Short? = null, + val happiness: Short? = null, + val position: Position? = null, + val statusFlagsValue: Short? = null, + val mapPosition: MapPosition? = null, + val natureId: Byte? = null, + val value64: Long? = null, + val origin: Origin? = null, + val shininessType: Byte? = null, + val gender: Byte? = null, + val ivs: List? = null, + val caughtBall: Byte? = null, + val statusFlags2: Short? = null, + val packedIvs: Int? = null, + val statusList: List? = null, + val status: Byte? = null, ) +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() { override fun CodecScope.body(): BattleEntityDeltaPacket { val entityId = field(S64LE) { it.entityId } - val fieldMask = field(S32LE) { it.fieldMask } - val m = fieldMask + val m = field(S32LE) { it.mask() } - val experienceLevel = if (m and 1 != 0) field(S8) { it.experienceLevel!! } else null - val experiencePoints = if (m and 1 != 0) field(S32LE) { it.experiencePoints!! } else null - val statValues = if (m and 2 != 0) field(S16LE.repeat(6)) { it.statValues!! } else null - val moveSlots = if (m and 4 != 0) field(MoveSlotCodec.repeat(4)) { it.moveSlots!! } else null - val ppUps = if (m and 4 != 0) field(S8) { it.ppUps!! } else null - 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 - val speciesId = if (m and 32 != 0) field(S16LE) { it.speciesId!! } else null - val forme = if (m and 32 != 0) field(S8) { it.forme!! } else null - val listType = if (m and 64 != 0) field(S8) { it.listType!! } else null - val sortKey = if (m and 64 != 0) field(S16LE) { it.sortKey!! } else null - val evValues = if (m and 128 != 0) field(S16LE.repeat(6)) { it.evValues!! } else null - val level = if (m and 256 != 0) field(S16LE) { it.level!! } else null - 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 - val experience = if (m and 1024 != 0) field(S32LE) { it.experience!! } else null - val flagA = if (m and 1024 != 0) field(S8) { it.flagA!! } else null - val flagB = if (m and 1024 != 0) field(S8) { it.flagB!! } else null - val encounterType = if (m and 1024 != 0) field(S8) { it.encounterType!! } else null - val statusFlagsValue = if (m and 2048 != 0) field(S16LE) { it.statusFlagsValue!! } else null - val warnA = if (m and 4096 != 0) field(S16LE) { it.warnA!! } else null - val warnB = if (m and 4096 != 0) field(S16LE) { it.warnB!! } else null - val natureId = if (m and 8192 != 0) field(S8) { it.natureId!! } else null - val ribbons = if (m and 16384 != 0) field(S64LE) { it.ribbons!! } else null - val packedIvs = if (m and 2097152 != 0) field(S32LE) { it.packedIvs!! } else null + val experience = + if (m and EXPERIENCE != 0) + Experience( + field(S8) { it.experience!!.level }, + field(S32LE) { it.experience!!.points }, + ) + else null + val statValues = optionalField(m and STATS != 0, S16LE.repeat(6)) { it.statValues } + val moves = + if (m and MOVES != 0) + MoveSlots( + field(MoveSlotCodec.repeat(4)) { it.moves!!.slots }, + field(S8) { it.moves!!.ppUps }, + ) + else null + val currentHp = optionalField(m and CURRENT_HP != 0, S16LE) { it.currentHp } + val faintFlag = optionalField(m and FAINT != 0, S8) { it.faintFlag } + val species = + if (m and SPECIES != 0) + Species(field(S16LE) { it.species!!.speciesId }, field(S8) { it.species!!.forme }) + else null + val listing = + if (m and LISTING != 0) + Listing(field(S8) { it.listing!!.listType }, field(S16LE) { it.listing!!.sortKey }) + 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 originalSpecies = if (m and 32768 != 0) field(S64LE) { it.originalSpecies!! } else null - val originalTrainerName = - if (m and 32768 != 0) field(Utf16LeNullTerminated) { it.originalTrainerName!! } else null - val originalTrainerId = if (m and 32768 != 0) field(S32LE) { it.originalTrainerId!! } else null + val origin = + if (m and ORIGIN != 0) + Origin( + field(S16LE.repeat(4)) { it.origin!!.effortValues }, + 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 gender = if (m and 131072 != 0) field(S8) { it.gender!! } else null - val ivs = if (m and 262144 != 0) field(S16LE.repeat(5)) { it.ivs!! } else null - val caughtBall = if (m and 524288 != 0) field(S8) { it.caughtBall!! } else null - val statusFlags2 = if (m and 1048576 != 0) field(S16LE) { it.statusFlags2!! } else null - val statusList = - if (m and 4194304 != 0) field(S8.listPrefixed(U8)) { it.statusList!! } else null - val status = if (m and 8388608 != 0) field(S8) { it.status!! } else null + val shininessType = optionalField(m and SHININESS_TYPE != 0, S8) { it.shininessType } + val gender = optionalField(m and GENDER != 0, S8) { it.gender } + val ivs = optionalField(m and IVS != 0, S16LE.repeat(5)) { it.ivs } + val caughtBall = optionalField(m and CAUGHT_BALL != 0, S8) { it.caughtBall } + val statusFlags2 = optionalField(m and STATUS_FLAGS_2 != 0, S16LE) { it.statusFlags2 } + val statusList = optionalField(m and STATUS_LIST != 0, S8.listPrefixed(U8)) { it.statusList } + val status = optionalField(m and STATUS != 0, S8) { it.status } return BattleEntityDeltaPacket( entityId = entityId, - fieldMask = fieldMask, - experienceLevel = experienceLevel, - experiencePoints = experiencePoints, + experience = experience, statValues = statValues, - moveSlots = moveSlots, - ppUps = ppUps, + moves = moves, currentHp = currentHp, faintFlag = faintFlag, - speciesId = speciesId, - forme = forme, - listType = listType, - sortKey = sortKey, + species = species, + listing = listing, evValues = evValues, level = level, happiness = happiness, - shininessSeed = shininessSeed, - experience = experience, - flagA = flagA, - flagB = flagB, - encounterType = encounterType, + position = position, statusFlagsValue = statusFlagsValue, - warnA = warnA, - warnB = warnB, + mapPosition = mapPosition, natureId = natureId, - ribbons = ribbons, - packedIvs = packedIvs, - effortValues = effortValues, - originalSpecies = originalSpecies, - originalTrainerName = originalTrainerName, - originalTrainerId = originalTrainerId, + value64 = value64, + origin = origin, shininessType = shininessType, gender = gender, ivs = ivs, caughtBall = caughtBall, statusFlags2 = statusFlags2, + packedIvs = packedIvs, statusList = statusList, status = status, ) diff --git a/protocols.game/src/test/kotlin/de/fiereu/openmmo/net/game/BattleEntityDeltaPacketTest.kt b/protocols.game/src/test/kotlin/de/fiereu/openmmo/net/game/BattleEntityDeltaPacketTest.kt new file mode 100644 index 0000000..88e2347 --- /dev/null +++ b/protocols.game/src/test/kotlin/de/fiereu/openmmo/net/game/BattleEntityDeltaPacketTest.kt @@ -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(39, 22, 20, 27, 25, 22) + packet.currentHp shouldBe 12 + packet.evValues shouldBe listOf(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>( + 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(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) + } + }) diff --git a/protocols.game/src/test/resources/fixtures/game/s2c/0c/money_32763.bin b/protocols.game/src/test/resources/fixtures/game/s2c/0c/money_32763.bin new file mode 100644 index 0000000000000000000000000000000000000000..6a5a882d357c3816a5124adb1639fca674232e76 GIT binary patch literal 6 NcmZQ%$YEz-0005e0CfNW literal 0 HcmV?d00001 diff --git a/protocols.game/src/test/resources/fixtures/game/s2c/16/acquired_32763.bin b/protocols.game/src/test/resources/fixtures/game/s2c/16/acquired_32763.bin new file mode 100644 index 0000000000000000000000000000000000000000..715846e75cb8cf9eaabdaf27a7855ec68f2c75b0 GIT binary patch literal 34 ocmZQLz`-)Z^n#Q$69WT>10w^2Bm*AI@73Vr&JJ literal 0 HcmV?d00001 diff --git a/server.game/src/main/kotlin/de/fiereu/openmmo/server/game/battle/AcquiredMonster.kt b/server.game/src/main/kotlin/de/fiereu/openmmo/server/game/battle/AcquiredMonster.kt index f386e68..9d2d1b2 100644 --- a/server.game/src/main/kotlin/de/fiereu/openmmo/server/game/battle/AcquiredMonster.kt +++ b/server.game/src/main/kotlin/de/fiereu/openmmo/server/game/battle/AcquiredMonster.kt @@ -2,72 +2,18 @@ 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.net.game.packets.battle.Experience 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( + // The real server also sends a happiness here, which we do not model. + return BattleEntityDeltaPacket( entityId = mon.id, - mask = ACQUIRED_MASK, - experienceLevel = mon.level, - experiencePoints = mon.xp, + experience = Experience(mon.level, mon.xp), statValues = stats.asWireList(), currentHp = mon.hp, faintFlag = 0, ) } - -fun entityDelta( - entityId: Long, - mask: Int, - experienceLevel: Byte? = null, - experiencePoints: Int? = null, - statValues: List? = 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, - ) diff --git a/server.game/src/main/kotlin/de/fiereu/openmmo/server/game/battle/BattlePacketEmitter.kt b/server.game/src/main/kotlin/de/fiereu/openmmo/server/game/battle/BattlePacketEmitter.kt index 341b817..78330bd 100644 --- a/server.game/src/main/kotlin/de/fiereu/openmmo/server/game/battle/BattlePacketEmitter.kt +++ b/server.game/src/main/kotlin/de/fiereu/openmmo/server/game/battle/BattlePacketEmitter.kt @@ -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.BattleSwitchInPacket 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.typechart.TypeChart 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 // 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 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) { broadcast( battle, - delta( + BattleEntityDeltaPacket( entityId = entityId, - mask = DELTA_EXPERIENCE, - experienceLevel = reward.newLevel.toByte(), - experiencePoints = reward.newXp, + experience = Experience(reward.newLevel.toByte(), reward.newXp), ), ) // 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 broadcast( battle, - delta( - entityId = entityId, - mask = DELTA_LEVEL_UP, - statValues = reward.newStats.asWireList(), - currentHp = reward.newCurrentHp.toShort(), - ) - .copy(evValues = reward.newEvs.asWireList()), + BattleEntityDeltaPacket( + entityId = entityId, + statValues = reward.newStats.asWireList(), + currentHp = reward.newCurrentHp.toShort(), + evValues = reward.newEvs.asWireList(), + ), ) } @@ -256,26 +249,7 @@ class BattlePacketEmitter @Inject constructor(private val interestManager: Inter moveSlots: List>, ppUps: Byte, ): BattleEntityDeltaPacket = - delta(entityId, DELTA_MOVE_SLOTS).copy(moveSlots = moveSlots, ppUps = ppUps) - - fun delta( - entityId: Long, - mask: Int, - experienceLevel: Byte? = null, - experiencePoints: Int? = null, - statValues: List? = null, - currentHp: Short? = null, - faintFlag: Byte? = null, - ): BattleEntityDeltaPacket = - entityDelta( - entityId = entityId, - mask = mask, - experienceLevel = experienceLevel, - experiencePoints = experiencePoints, - statValues = statValues, - currentHp = currentHp, - faintFlag = faintFlag, - ) + BattleEntityDeltaPacket(entityId = entityId, moves = MoveSlots(moveSlots, ppUps)) // The decomp battle stat order. Not verified against the live client yet. private fun statIndex(stat: BattleStat): Byte = diff --git a/server.game/src/test/kotlin/de/fiereu/openmmo/server/game/services/BattleServiceTest.kt b/server.game/src/test/kotlin/de/fiereu/openmmo/server/game/services/BattleServiceTest.kt index 9b4fda8..b3cdc0d 100644 --- a/server.game/src/test/kotlin/de/fiereu/openmmo/server/game/services/BattleServiceTest.kt +++ b/server.game/src/test/kotlin/de/fiereu/openmmo/server/game/services/BattleServiceTest.kt @@ -225,7 +225,7 @@ class BattleServiceTest : // The level 2 Rattata yields its base experience scaled by level over seven. session.sent .filterIsInstance() - .any { it.fieldMask and 0x1 != 0 } + .any { it.experience != null } .shouldBeTrue() val saved = fx.repo.saved[charId].shouldNotBeNull() saved.pokemon.single().moves[0].pp shouldBe (35 - rounds).toByte()