feat(game): stub wild battle flow with catch, bag, switching, field state, and flee

This commit is contained in:
Fiereu committed 2026-07-19 22:35:04 +02:00
1 parent 9732c9373c
commit d618cff20e
92 files changed
+1499 -272

No files matched your search

@@ -0,0 +1,82 @@
package de.fiereu.openmmo.net.game
import de.fiereu.bytecodec.test.decodeBytes
import de.fiereu.bytecodec.test.encodeToBytes
import de.fiereu.openmmo.common.enums.BattleAction
import de.fiereu.openmmo.net.game.packets.battle.BattleActionSelectPacket
import de.fiereu.openmmo.net.game.packets.battle.BattleActionSelectPacketCodec
import io.kotest.core.spec.style.FunSpec
import io.kotest.matchers.shouldBe
class BattleActionSelectPacketTest :
FunSpec({
test("decodes a captured move selection") {
val bytes = byteArrayOf(0x00, 0x00, 0x2B, 0x00, 0x00)
val decoded = BattleActionSelectPacketCodec.decodeBytes(bytes)
decoded shouldBe
BattleActionSelectPacket(
slotRefPacked = 0,
action = BattleAction.MOVE,
moveOrItemId = 43,
targetEntityId = 0,
extraFlag = 0,
)
BattleActionSelectPacketCodec.encodeToBytes(decoded) shouldBe bytes
}
test("decodes a captured item throw") {
val bytes =
byteArrayOf(
0x00,
0x01,
0x8C.toByte(),
0x13,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0x00,
0xFF.toByte())
val decoded = BattleActionSelectPacketCodec.decodeBytes(bytes)
decoded shouldBe
BattleActionSelectPacket(
slotRefPacked = 0,
action = BattleAction.ITEM,
moveOrItemId = 5004,
targetEntityId = 0,
extraFlag = -1,
)
BattleActionSelectPacketCodec.encodeToBytes(decoded) shouldBe bytes
}
test("decodes a run selection with no tail") {
val bytes = byteArrayOf(0x00, 0x03)
val decoded = BattleActionSelectPacketCodec.decodeBytes(bytes)
decoded shouldBe
BattleActionSelectPacket(
slotRefPacked = 0,
action = BattleAction.RUN,
moveOrItemId = 0,
targetEntityId = 0,
extraFlag = 0,
)
BattleActionSelectPacketCodec.encodeToBytes(decoded) shouldBe bytes
}
test("decodes a captured switch to a party index") {
val bytes = byteArrayOf(0x00, 0x02, 0x01, 0x00)
val decoded = BattleActionSelectPacketCodec.decodeBytes(bytes)
decoded shouldBe
BattleActionSelectPacket(
slotRefPacked = 0,
action = BattleAction.SWITCH,
moveOrItemId = 1,
targetEntityId = 0,
extraFlag = 0,
)
BattleActionSelectPacketCodec.encodeToBytes(decoded) shouldBe bytes
}
})
@@ -0,0 +1,28 @@
package de.fiereu.openmmo.net.game
import de.fiereu.bytecodec.test.decodeBytes
import de.fiereu.bytecodec.test.encodeToBytes
import de.fiereu.openmmo.net.game.packets.battle.BattleFieldStatePacketCodec
import io.kotest.core.spec.style.FunSpec
import io.kotest.matchers.shouldBe
import java.util.Base64
// A two-monster party wild encounter (Snivy active, Patrat benched), scrubbed of capture data. The
// player side carries a per-species stat and move block and a per-slot trailer, all reproduced by
// the codec.
private const val CAPTURED_TWO_PARTY =
"AgAAAAAAAAAAAAAAAP8AAAAAABYAAAD/ACAABlQAZQBzAHQAAAAAAJABAAAAAAD/AAJMAxqsDwADgAGkAAQAAAAAAAECAAABAMABAAAAAADvAQYAAAAAAAAWABYAAAAA/wMBQQAhACsAAAAAAAABAQDAAgAAAAAA+AECAAABAAAADgAOAAAAAP8DATIAIQAAAAAAAAABAADvAQYAAAAAAAAD/wAAAABmZmZmAQYAAAAAAAEBAAABAMADAAAAAAD4AQIAAAEAAAAOAA4AAAAA/wMAAQAA+AECAAAAAAEAA/8AAAAAZmZmZgAAAAA="
class BattleFieldStatePacketTest :
FunSpec({
test("round-trips a two-party wild field state byte for byte") {
val bytes = Base64.getDecoder().decode(CAPTURED_TWO_PARTY)
val decoded = BattleFieldStatePacketCodec.decodeBytes(bytes)
decoded.playerName shouldBe "Test"
decoded.playerParty.size shouldBe 2
decoded.playerParty[0].species shouldBe 495.toShort()
decoded.playerParty[1].species shouldBe 504.toShort()
decoded.wildMon.species shouldBe 504.toShort()
BattleFieldStatePacketCodec.encodeToBytes(decoded) shouldBe bytes
}
})
@@ -0,0 +1,48 @@
package de.fiereu.openmmo.net.game
import de.fiereu.bytecodec.test.decodeBytes
import de.fiereu.bytecodec.test.encodeToBytes
import de.fiereu.openmmo.net.game.packets.battle.BattleSwitchInMon
import de.fiereu.openmmo.net.game.packets.battle.BattleSwitchInPacket
import de.fiereu.openmmo.net.game.packets.battle.BattleSwitchInPacketCodec
import io.kotest.core.spec.style.FunSpec
import io.kotest.matchers.shouldBe
private fun ByteArray.hex(): String = joinToString("") { "%02x".format(it) }
class BattleSwitchInPacketTest :
FunSpec({
// Switching back to an already-active monster sends only the 21-byte active detail.
// Reproduced
// byte for byte from a real capture (Snivy, slot 0, previous slot 1).
test("encodes a return switch-in as just the active detail") {
val packet =
BattleSwitchInPacket(
newSlot = 0,
oldSlot = 1,
mon = BattleSwitchInMon(entityId = 0, species = 495, hp = 0),
fullBlock = false,
)
val bytes = BattleSwitchInPacketCodec.encodeToBytes(packet)
bytes.hex() shouldBe "000000010000ef010600000000000003ff0000000066666666"
}
// A benched monster coming out carries its full block then its active detail.
test("round-trips a full-block switch-in") {
val packet =
BattleSwitchInPacket(
newSlot = 1,
oldSlot = 0,
mon = BattleSwitchInMon(entityId = 0x000000000003C000L, species = 504, hp = 14),
fullBlock = true,
)
val bytes = BattleSwitchInPacketCodec.encodeToBytes(packet)
bytes.size shouldBe 65
val decoded = BattleSwitchInPacketCodec.decodeBytes(bytes)
decoded.newSlot shouldBe 1
decoded.oldSlot shouldBe 0
decoded.fullBlock shouldBe true
decoded.mon.species shouldBe 504.toShort()
decoded.mon.hp shouldBe 14.toShort()
}
})
@@ -0,0 +1,128 @@
package de.fiereu.openmmo.net.game
import de.fiereu.bytecodec.test.decodeBytes
import de.fiereu.bytecodec.test.encodeToBytes
import de.fiereu.openmmo.common.Pokemon
import de.fiereu.openmmo.common.PokemonMove
import de.fiereu.openmmo.common.enums.EVs
import de.fiereu.openmmo.common.enums.IVs
import de.fiereu.openmmo.common.enums.PokemonContainer
import de.fiereu.openmmo.net.game.codecs.PokemonCodec
import de.fiereu.openmmo.net.game.packets.PokemonContainerPacket
import de.fiereu.openmmo.net.game.packets.PokemonContainerPacketCodec
import io.kotest.core.spec.style.FunSpec
import io.kotest.matchers.shouldBe
import java.time.LocalDateTime
import java.util.Base64
private fun testMon(id: Long, dex: Int, slot: Short) =
Pokemon(
id = id,
ownerId = 0x9000L,
container = PokemonContainer.PARTY,
containerSlot = slot,
dexId = dex,
seed = 0,
ot = "Test",
nickname = "",
level = 5,
hp = 20,
xp = 100,
eVs = EVs(),
iVs = IVs(),
moves = List(4) { PokemonMove(0, 0) },
isShiny = false,
hasHiddenAbility = false,
isAlpha = false,
isSecret = false,
isFatefulEncounter = false,
isRaidEncounter = false,
caughtAt = LocalDateTime.of(2026, 1, 1, 0, 0, 0),
)
private const val CAPTURED_PARTY =
"AQEBAMABAAAAAAAAAACQAQAAAAAAAJABAAAAAAABAADvAbpwfdsAkAEAAAAAAFQAZQBzAHQAAAAAAAAABRQAAAClAAAAADIAIQArAAAAAAAjHgAAAAAAAAAAAAAAAAAAAQAAAAAAAAAEBQL/////AwDvvfceAAAAIAAAAAAAAACGhlNqAAD//wA="
class PokemonContainerPacketTest :
FunSpec({
test("round-trips a captured party container and exposes the monster fields") {
val bytes = Base64.getDecoder().decode(CAPTURED_PARTY)
val decoded = PokemonContainerPacketCodec.decodeBytes(bytes)
val mon = decoded.pokemon.single()
mon.dexId shouldBe 495
mon.ot shouldBe "Test"
mon.level shouldBe 5.toByte()
mon.hp shouldBe 20.toShort()
mon.moves.map { it.id } shouldBe listOf<Short>(33, 43, 0, 0)
mon.containerSlot shouldBe 0.toShort()
// Starter monsters roll fixed 15s across every stat.
mon.iVs.total shouldBe 90
mon.iVs.hp shouldBe 15
mon.caughtAt shouldBe LocalDateTime.of(2026, 7, 12, 12, 20, 22)
mon.isShiny shouldBe false
mon.isEgg shouldBe false
PokemonContainerPacketCodec.encodeToBytes(decoded) shouldBe bytes
}
test("EVs, rarity flags, and egg survive a monster codec round-trip") {
val mon = PokemonContainerPacketCodec.decodeBytes(bytes()).pokemon.single()
// Distinct per-stat values catch a wrong wire order (speed is the fourth EV byte).
val evs =
EVs().apply {
hp = 4
atk = 8
def = 12
spAtk = 16
spDef = 20
spd = 24
}
val rare =
mon.copy(
eVs = evs,
isShiny = true,
hasHiddenAbility = false,
isAlpha = true,
isSecret = true,
isFatefulEncounter = false,
isRaidEncounter = true,
isEgg = true,
)
val roundTripped = PokemonCodec.decodeBytes(PokemonCodec.encodeToBytes(rare))
roundTripped.eVs.hp shouldBe 4
roundTripped.eVs.atk shouldBe 8
roundTripped.eVs.def shouldBe 12
roundTripped.eVs.spAtk shouldBe 16
roundTripped.eVs.spDef shouldBe 20
roundTripped.eVs.spd shouldBe 24
roundTripped.isShiny shouldBe true
roundTripped.hasHiddenAbility shouldBe false
roundTripped.isAlpha shouldBe true
roundTripped.isSecret shouldBe true
roundTripped.isFatefulEncounter shouldBe false
roundTripped.isRaidEncounter shouldBe true
roundTripped.isEgg shouldBe true
}
test("a two-monster party container round-trips") {
val packet =
PokemonContainerPacket(
container = PokemonContainer.PARTY,
hasChange = true,
delete = false,
pokemon =
listOf(
testMon(0x000000000001C000L, dex = 495, slot = 0),
testMon(0x000000000002C000L, dex = 504, slot = 1),
),
)
val bytes = PokemonContainerPacketCodec.encodeToBytes(packet)
val decoded = PokemonContainerPacketCodec.decodeBytes(bytes)
decoded.pokemon.size shouldBe 2
decoded.pokemon[0].dexId shouldBe 495
decoded.pokemon[1].dexId shouldBe 504
decoded.pokemon[1].containerSlot shouldBe 1.toShort()
PokemonContainerPacketCodec.encodeToBytes(decoded) shouldBe bytes
}
})
private fun bytes(): ByteArray = Base64.getDecoder().decode(CAPTURED_PARTY)