feat(game): handle npc dialog replies and send correct dialog actions (#63)

This commit is contained in:
Fiereu authored and GitHub committed 2026-07-25 19:18:58 +02:00
1 parent 35a7966a97
commit e89ae6e6df
10 files changed
+97 -74

No files matched your search

@@ -6,6 +6,7 @@ import de.fiereu.network.c2s
import de.fiereu.network.s2c
import de.fiereu.openmmo.net.game.packets.*
import de.fiereu.openmmo.net.game.packets.battle.*
import de.fiereu.openmmo.net.game.packets.dialog.*
import de.fiereu.openmmo.net.game.packets.guild.*
object GameProtocol : Protocol() {
@@ -97,7 +98,7 @@ object GameProtocol : Protocol() {
c2s<NullPacket>(0x20u, NullPacketCodec)
s2c<TokenPayloadPacket>(0x20u, TokenPayloadPacketCodec)
c2s<WarpTileInteractPacket>(0x21u, WarpTileInteractPacketCodec)
c2s<DialogActionResponsePacket>(0x21u, DialogActionResponsePacketCodec)
s2c<DialogActionPacket>(0x21u, DialogActionPacketCodec)
bidi<EntityInteractPacket>(0x22u, EntityInteractPacketCodec)
@@ -1,26 +0,0 @@
package de.fiereu.openmmo.net.game.packets
import de.fiereu.bytecodec.*
data class InteractivePacket(
val id: Int,
val type: Int,
val unk1: Int,
val unk2: Int,
val targetEntityId: Long,
val unk3: Int,
val unk4: Int,
)
object InteractivePacketCodec : PacketCodec<InteractivePacket>() {
override fun CodecScope<InteractivePacket>.body(): InteractivePacket {
val id = field(U8) { it.id }
val type = field(U8) { it.type }
val unk1 = field(S32LE) { it.unk1 }
val unk2 = field(S32LE) { it.unk2 }
val targetEntityId = field(S64LE) { it.targetEntityId }
val unk3 = field(U16LE) { it.unk3 }
val unk4 = field(U16LE) { it.unk4 }
return InteractivePacket(id, type, unk1, unk2, targetEntityId, unk3, unk4)
}
}
@@ -1,15 +0,0 @@
package de.fiereu.openmmo.net.game.packets
import de.fiereu.bytecodec.CodecScope
import de.fiereu.bytecodec.PacketCodec
import de.fiereu.bytecodec.U8
data class InteractiveResponsePacket(val id: Int, val unk: Int)
object InteractiveResponsePacketCodec : PacketCodec<InteractiveResponsePacket>() {
override fun CodecScope<InteractiveResponsePacket>.body(): InteractiveResponsePacket {
val id = field(U8) { it.id }
val unk = field(U8) { it.unk }
return InteractiveResponsePacket(id, unk)
}
}
@@ -1,18 +0,0 @@
package de.fiereu.openmmo.net.game.packets
import de.fiereu.bytecodec.CodecScope
import de.fiereu.bytecodec.PacketCodec
import de.fiereu.bytecodec.S8
data class WarpTileInteractPacket(
val floorId: Byte,
val warpMode: Byte,
)
object WarpTileInteractPacketCodec : PacketCodec<WarpTileInteractPacket>() {
override fun CodecScope<WarpTileInteractPacket>.body(): WarpTileInteractPacket {
val floorId = field(S8) { it.floorId }
val warpMode = field(S8) { it.warpMode }
return WarpTileInteractPacket(floorId, warpMode)
}
}
@@ -1,4 +1,4 @@
package de.fiereu.openmmo.net.game.packets
package de.fiereu.openmmo.net.game.packets.dialog
import de.fiereu.bytecodec.*
@@ -0,0 +1,15 @@
package de.fiereu.openmmo.net.game.packets.dialog
import de.fiereu.bytecodec.CodecScope
import de.fiereu.bytecodec.PacketCodec
import de.fiereu.bytecodec.U8
data class DialogActionResponsePacket(val id: Int, val unk: Int)
object DialogActionResponsePacketCodec : PacketCodec<DialogActionResponsePacket>() {
override fun CodecScope<DialogActionResponsePacket>.body(): DialogActionResponsePacket {
val id = field(U8) { it.id }
val unk = field(U8) { it.unk }
return DialogActionResponsePacket(id, unk)
}
}
@@ -0,0 +1,44 @@
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.dialog.CreatureDataArg
import de.fiereu.openmmo.net.game.packets.dialog.DialogActionPacketCodec
import io.kotest.core.spec.style.FunSpec
import io.kotest.matchers.shouldBe
private fun bytes(hex: String): ByteArray =
hex.chunked(2).map { it.toInt(16).toByte() }.toByteArray()
private fun ByteArray.hex(): String = joinToString("") { "%02x".format(it) }
class DialogActionPacketTest :
FunSpec({
// Every case is a real 0x21 dialog action from the NPC interaction capture.
val captures =
listOf(
"0c2e0d00b52903e008bf86c4c01ab004000000",
"006400000000ffffffffffffffff0000000000",
"0e04f90600010ae008bf86c4c01ab004000000",
"0d04f80600010ae008bf86c4c01ab004000001000992d00300",
)
test("round-trips every captured dialog action byte for byte") {
captures.forEach { hex ->
val packet = DialogActionPacketCodec.decodeBytes(bytes(hex))
DialogActionPacketCodec.encodeToBytes(packet).hex() shouldBe hex
}
}
test("decodes the fields of a dialog action carrying a creature arg") {
val packet =
DialogActionPacketCodec.decodeBytes(
bytes("0d04f80600010ae008bf86c4c01ab004000001000992d00300"))
packet.flags shouldBe 0x0d.toByte()
packet.actionType shouldBe 0x04.toByte()
packet.textId shouldBe 0x010006f8
packet.entityId shouldBe 0x1ac0c486bf08e00aL
packet.contextValue shouldBe 0x04b0
packet.messageArgs shouldBe listOf(CreatureDataArg(0x03d09209, emptyList()))
}
})
@@ -0,0 +1,20 @@
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.dialog.DialogActionResponsePacket
import de.fiereu.openmmo.net.game.packets.dialog.DialogActionResponsePacketCodec
import io.kotest.core.spec.style.FunSpec
import io.kotest.matchers.shouldBe
class DialogActionResponsePacketTest :
FunSpec({
// The client replies to a 0x21 dialog action with the action id and a response byte. This is
// the "0c 00" the capture sends back after the "0c" dialog action.
test("round-trips the captured dialog reply") {
val bytes = byteArrayOf(0x0c, 0x00)
val packet = DialogActionResponsePacketCodec.decodeBytes(bytes)
packet shouldBe DialogActionResponsePacket(id = 0x0c, unk = 0)
DialogActionResponsePacketCodec.encodeToBytes(packet) shouldBe bytes
}
})