feat(battle): add the wild battle system with structured codecs (#58)
This commit is contained in:
1 parent
214663531a
commit
651a1fbb67
30 files changed
+2429
-522
No files matched your search
+40
@@ -0,0 +1,40 @@
|
||||
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.BattleActionEvent
|
||||
import de.fiereu.openmmo.net.game.packets.battle.BattleEffectTarget
|
||||
import de.fiereu.openmmo.net.game.packets.battle.BattleEntityMoveEventPacket
|
||||
import de.fiereu.openmmo.net.game.packets.battle.BattleEntityMoveEventPacketCodec
|
||||
import de.fiereu.openmmo.net.game.packets.battle.BattleEventBody
|
||||
import io.kotest.core.spec.style.FunSpec
|
||||
import io.kotest.matchers.shouldBe
|
||||
|
||||
class BattleEntityMoveEventPacketTest :
|
||||
FunSpec({
|
||||
test("round-trips every body type and entity-flag combination") {
|
||||
val packet =
|
||||
BattleEntityMoveEventPacket(
|
||||
sourceEntity = 0x1122334455667788L,
|
||||
sourceMove = 0x0200,
|
||||
kind = 1,
|
||||
targets =
|
||||
listOf(
|
||||
BattleEffectTarget(
|
||||
entityId = 10L,
|
||||
targetMove = 0x0200,
|
||||
subEvents =
|
||||
listOf(
|
||||
BattleActionEvent(null, null, BattleEventBody.HpUpdate(14)),
|
||||
BattleActionEvent(20L, null, BattleEventBody.StatChange(1, -2)),
|
||||
BattleActionEvent(null, 30L, BattleEventBody.Faint(true)),
|
||||
BattleActionEvent(
|
||||
40L, 50L, BattleEventBody.EffectivenessMessage),
|
||||
BattleActionEvent(null, null, BattleEventBody.MoveFailed(33)))),
|
||||
BattleEffectTarget(
|
||||
entityId = 60L, targetMove = 0, subEvents = emptyList())))
|
||||
|
||||
val bytes = BattleEntityMoveEventPacketCodec.encodeToBytes(packet)
|
||||
BattleEntityMoveEventPacketCodec.decodeBytes(bytes) shouldBe packet
|
||||
}
|
||||
})
|
||||
+76
-6
@@ -2,14 +2,14 @@ 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.BattleFieldStatePacket
|
||||
import de.fiereu.openmmo.net.game.packets.battle.BattleFieldStatePacketCodec
|
||||
import de.fiereu.openmmo.net.game.packets.battle.BattleMonBlock
|
||||
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.
|
||||
// A two-monster party wild encounter (Snivy active, Patrat benched), scrubbed of capture data.
|
||||
private const val CAPTURED_TWO_PARTY =
|
||||
"AgAAAAAAAAAAAAAAAP8AAAAAABYAAAD/ACAABlQAZQBzAHQAAAAAAJABAAAAAAD/AAJMAxqsDwADgAGkAAQAAAAAAAECAAABAMABAAAAAADvAQYAAAAAAAAWABYAAAAA/wMBQQAhACsAAAAAAAABAQDAAgAAAAAA+AECAAABAAAADgAOAAAAAP8DATIAIQAAAAAAAAABAADvAQYAAAAAAAAD/wAAAABmZmZmAQYAAAAAAAEBAAABAMADAAAAAAD4AQIAAAEAAAAOAA4AAAAA/wMAAQAA+AECAAAAAAEAA/8AAAAAZmZmZgAAAAA="
|
||||
|
||||
@@ -20,9 +20,79 @@ class BattleFieldStatePacketTest :
|
||||
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()
|
||||
decoded.activeSlot shouldBe 0
|
||||
BattleFieldStatePacketCodec.encodeToBytes(decoded) shouldBe bytes
|
||||
}
|
||||
|
||||
test("decodes the structured mon fields from the capture") {
|
||||
val bytes = Base64.getDecoder().decode(CAPTURED_TWO_PARTY)
|
||||
val decoded = BattleFieldStatePacketCodec.decodeBytes(bytes)
|
||||
|
||||
val snivy = decoded.playerParty[0]
|
||||
snivy.slot shouldBe 0
|
||||
snivy.species shouldBe 495.toShort()
|
||||
snivy.level shouldBe 6.toByte()
|
||||
snivy.gender shouldBe 0.toByte()
|
||||
snivy.abilityId shouldBe 65.toShort()
|
||||
snivy.maxHp shouldBe 22.toShort()
|
||||
snivy.currentHp shouldBe 22.toShort()
|
||||
snivy.movesPresent shouldBe true
|
||||
snivy.moveIds shouldBe listOf<Short>(33, 43, 0, 0)
|
||||
|
||||
val patrat = decoded.playerParty[1]
|
||||
patrat.slot shouldBe 1
|
||||
patrat.species shouldBe 504.toShort()
|
||||
patrat.level shouldBe 2.toByte()
|
||||
patrat.gender shouldBe 1.toByte()
|
||||
patrat.abilityId shouldBe 50.toShort()
|
||||
patrat.moveIds shouldBe listOf<Short>(33, 0, 0, 0)
|
||||
|
||||
val wild = decoded.wildParty.single()
|
||||
wild.species shouldBe 504.toShort()
|
||||
wild.level shouldBe 2.toByte()
|
||||
wild.maxHp shouldBe 14.toShort()
|
||||
wild.currentHp shouldBe 14.toShort()
|
||||
wild.movesPresent shouldBe false
|
||||
}
|
||||
|
||||
test("round-trips a synthetic field state for a species without a capture") {
|
||||
val rattata =
|
||||
BattleMonBlock(
|
||||
slot = 0,
|
||||
entityId = 0x123C000L,
|
||||
species = 19,
|
||||
level = 7,
|
||||
gender = 1,
|
||||
abilityId = 50,
|
||||
maxHp = 21,
|
||||
currentHp = 17,
|
||||
movesPresent = true,
|
||||
moveIds = listOf(33, 39, 45, 98),
|
||||
)
|
||||
val wild =
|
||||
BattleMonBlock(
|
||||
slot = 0,
|
||||
entityId = 0x456C000L,
|
||||
species = 74,
|
||||
level = 9,
|
||||
gender = 0,
|
||||
abilityId = 0,
|
||||
maxHp = 26,
|
||||
currentHp = 26,
|
||||
movesPresent = false,
|
||||
moveIds = listOf(0, 0, 0, 0),
|
||||
)
|
||||
val packet =
|
||||
BattleFieldStatePacket(
|
||||
playerName = "Ash",
|
||||
playerId = 0x19000L,
|
||||
playerParty = listOf(rattata),
|
||||
activeSlot = 0,
|
||||
wildParty = listOf(wild),
|
||||
)
|
||||
val decoded =
|
||||
BattleFieldStatePacketCodec.decodeBytes(
|
||||
BattleFieldStatePacketCodec.encodeToBytes(packet))
|
||||
decoded shouldBe packet
|
||||
}
|
||||
})
|
||||
+33
-13
@@ -2,7 +2,7 @@ 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.BattleMonBlock
|
||||
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
|
||||
@@ -10,39 +10,59 @@ import io.kotest.matchers.shouldBe
|
||||
|
||||
private fun ByteArray.hex(): String = joinToString("") { "%02x".format(it) }
|
||||
|
||||
private fun patrat(): BattleMonBlock =
|
||||
BattleMonBlock(
|
||||
slot = 1,
|
||||
entityId = 0x000000000003C000L,
|
||||
species = 504,
|
||||
level = 2,
|
||||
gender = 1,
|
||||
abilityId = 50,
|
||||
maxHp = 14,
|
||||
currentHp = 14,
|
||||
movesPresent = true,
|
||||
moveIds = listOf(33, 0, 0, 0),
|
||||
)
|
||||
|
||||
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).
|
||||
// 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),
|
||||
mon =
|
||||
BattleMonBlock(
|
||||
slot = 0,
|
||||
entityId = 0,
|
||||
species = 495,
|
||||
level = 6,
|
||||
gender = 0,
|
||||
abilityId = 0,
|
||||
maxHp = 0,
|
||||
currentHp = 0,
|
||||
movesPresent = false,
|
||||
moveIds = listOf(0, 0, 0, 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.
|
||||
// A benched monster coming out carries its full block then its active detail. The block
|
||||
// matches the captured Patrat bytes now built from structured fields.
|
||||
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,
|
||||
)
|
||||
BattleSwitchInPacket(newSlot = 1, oldSlot = 0, mon = patrat(), 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()
|
||||
decoded.mon shouldBe patrat()
|
||||
}
|
||||
})
|
||||
Reference in new issue
Block a user