feat(game): open the mart shop and key items by the ids the client reads (#151)

This commit is contained in:
Fiereu authored and GitHub committed 2026-08-10 14:52:44 +02:00
1 parent a69fd0d07f
commit 499d20fdb3
46 files changed
+7863 -269

No files matched your search

@@ -106,7 +106,7 @@ object GameProtocol : Protocol() {
bidi<EntityInteractPacket>(0x22u, EntityInteractPacketCodec)
c2s<ExchangeItemRequestPacket>(0x23u, ExchangeItemRequestPacketCodec)
s2c<EventOverlayStatePacket>(0x23u, EventOverlayStatePacketCodec)
s2c<ShopCatalogPacket>(0x23u, ShopCatalogPacketCodec)
c2s<ShopSellRequestPacket>(0x24u, ShopSellRequestPacketCodec)
s2c<NpcPanelTogglePacket>(0x24u, NpcPanelTogglePacketCodec)
@@ -1,134 +0,0 @@
package de.fiereu.openmmo.net.game.packets
import de.fiereu.bytecodec.*
private const val GRID_OVERLAY_TYPE: Int = 1
data class OverlayGridPoint(val x: Short, val y: Short)
data class OverlayRegion(
val regionId: Short,
val secondary: Short,
val gridPoints: List<OverlayGridPoint>?,
val value: Int?,
)
data class OverlayHole(val regionId: Short, val secondary: Short)
data class EventOverlayStatePacket(
val active: Boolean,
val overlayType: Byte,
val flagBits: Byte,
val ignoredEnum: Byte,
val timeRemaining: Int?,
val valueA: Int?,
val valueB: Int?,
val anchorEntityId: Long?,
val regions: List<OverlayRegion>,
val holes: List<OverlayHole>,
)
object EventOverlayStatePacketCodec : PacketCodec<EventOverlayStatePacket>() {
override fun CodecScope<EventOverlayStatePacket>.body(): EventOverlayStatePacket {
val codec = EventOverlayStateCodec
return field(codec) { it }
}
}
private val EventOverlayStateCodec: Codec<EventOverlayStatePacket> =
object : Codec<EventOverlayStatePacket> {
override fun read(buf: ReadBuffer): EventOverlayStatePacket {
val typeByte = S8.read(buf)
val active = typeByte.toInt() != -1
if (!active) {
return EventOverlayStatePacket(
active = false,
overlayType = typeByte,
flagBits = 0,
ignoredEnum = 0,
timeRemaining = null,
valueA = null,
valueB = null,
anchorEntityId = null,
regions = emptyList(),
holes = emptyList(),
)
}
val grid = typeByte.toInt() == GRID_OVERLAY_TYPE
val flagBits = S8.read(buf)
val fb = flagBits.toInt()
val ignoredEnum = S8.read(buf)
val timeRemaining = if (fb and 32 != 0) S32LE.read(buf) else null
val valueA = if (fb and 8 != 0) S32LE.read(buf) else null
val valueB = if (fb and 16 != 0) S32LE.read(buf) else null
val anchorEntityId = if (fb and 64 != 0) S64LE.read(buf) else null
val regionCount = U16LE.read(buf)
val regions = ArrayList<OverlayRegion>(regionCount)
repeat(regionCount) {
val regionId = S16LE.read(buf)
val secondary = S16LE.read(buf)
if (grid) {
val n = U8.read(buf)
val points = ArrayList<OverlayGridPoint>(n)
repeat(n) { points.add(OverlayGridPoint(S16LE.read(buf), S16LE.read(buf))) }
regions.add(OverlayRegion(regionId, secondary, points, null))
} else {
val v = S32LE.read(buf)
regions.add(OverlayRegion(regionId, secondary, null, v))
}
}
val holes = ArrayList<OverlayHole>()
if (grid && fb and 4 != 0) {
val holeCount = U16LE.read(buf)
repeat(holeCount) { holes.add(OverlayHole(S16LE.read(buf), S16LE.read(buf))) }
}
return EventOverlayStatePacket(
active = true,
overlayType = typeByte,
flagBits = flagBits,
ignoredEnum = ignoredEnum,
timeRemaining = timeRemaining,
valueA = valueA,
valueB = valueB,
anchorEntityId = anchorEntityId,
regions = regions,
holes = holes,
)
}
override fun write(buf: WriteBuffer, value: EventOverlayStatePacket) {
S8.write(buf, value.overlayType)
if (!value.active) return
val grid = value.overlayType.toInt() == GRID_OVERLAY_TYPE
S8.write(buf, value.flagBits)
val fb = value.flagBits.toInt()
S8.write(buf, value.ignoredEnum)
if (fb and 32 != 0) S32LE.write(buf, value.timeRemaining!!)
if (fb and 8 != 0) S32LE.write(buf, value.valueA!!)
if (fb and 16 != 0) S32LE.write(buf, value.valueB!!)
if (fb and 64 != 0) S64LE.write(buf, value.anchorEntityId!!)
U16LE.write(buf, value.regions.size)
for (r in value.regions) {
S16LE.write(buf, r.regionId)
S16LE.write(buf, r.secondary)
if (grid) {
val points =
r.gridPoints ?: throw MalformedPacketException("grid overlay region missing points")
U8.write(buf, points.size)
for (p in points) {
S16LE.write(buf, p.x)
S16LE.write(buf, p.y)
}
} else {
S32LE.write(buf, r.value!!)
}
}
if (grid && fb and 4 != 0) {
U16LE.write(buf, value.holes.size)
for (h in value.holes) {
S16LE.write(buf, h.regionId)
S16LE.write(buf, h.secondary)
}
}
}
}
@@ -0,0 +1,59 @@
package de.fiereu.openmmo.net.game.packets
import de.fiereu.bytecodec.*
/** [stock] is 0x7FFF on every captured mart line, which the client reads as an endless shelf. */
data class ShopItem(
val itemId: Short,
val unknownA: Short,
val stock: Short,
val price: Int,
)
object ShopItemCodec : PacketCodec<ShopItem>() {
override fun CodecScope<ShopItem>.body(): ShopItem {
val itemId = field(S16LE) { it.itemId }
val unknownA = field(S16LE) { it.unknownA }
val stock = field(S16LE) { it.stock }
val price = field(S32LE) { it.price }
return ShopItem(itemId, unknownA, stock, price)
}
}
/**
* Opens or closes the shop window. A close is the single byte [CLOSED] with no body, so everything
* after the first byte only rides along on an open.
*/
data class ShopCatalogPacket(
val kind: Byte,
val shopId: Short? = null,
val unknownA: Int? = null,
val npcEntityId: Long? = null,
val items: List<ShopItem> = emptyList(),
) {
companion object {
const val OPEN: Byte = 0
const val CLOSED: Byte = -1
/**
* [unknownA] is the same value in every capture, across three different shops, so it is sent
* back rather than derived.
*/
fun open(shopId: Short, npcEntityId: Long, items: List<ShopItem>): ShopCatalogPacket =
ShopCatalogPacket(OPEN, shopId, CAPTURED_UNKNOWN_A, npcEntityId, items)
private const val CAPTURED_UNKNOWN_A = 0x1A5190
}
}
object ShopCatalogPacketCodec : PacketCodec<ShopCatalogPacket>() {
override fun CodecScope<ShopCatalogPacket>.body(): ShopCatalogPacket {
val kind = field(S8) { it.kind }
val open = kind == ShopCatalogPacket.OPEN
val shopId = optionalField(open, S16LE) { it.shopId }
val unknownA = optionalField(open, S32LE) { it.unknownA }
val npcEntityId = optionalField(open, S64LE) { it.npcEntityId }
val items = optionalField(open, ShopItemCodec.listPrefixed(U16LE)) { it.items }
return ShopCatalogPacket(kind, shopId, unknownA, npcEntityId, items ?: emptyList())
}
}
@@ -0,0 +1,46 @@
package de.fiereu.openmmo.net.game
import de.fiereu.openmmo.common.test.assertBytesRoundtrip
import de.fiereu.openmmo.common.test.decodeBytes
import de.fiereu.openmmo.common.test.fixture
import de.fiereu.openmmo.net.game.packets.ExchangeItemRequestPacketCodec
import de.fiereu.openmmo.net.game.packets.ShopCatalogPacket
import de.fiereu.openmmo.net.game.packets.ShopCatalogPacketCodec
import io.kotest.core.spec.style.FunSpec
import io.kotest.matchers.shouldBe
class ShopCatalogPacketTest :
FunSpec({
test("the viridian mart catalog decodes to its shelves and their prices") {
val bytes = fixture("game/s2c/23/mart_catalog.bin")
val packet = ShopCatalogPacketCodec.decodeBytes(bytes)
packet.kind shouldBe ShopCatalogPacket.OPEN
packet.shopId shouldBe 103
packet.npcEntityId shouldBe 0x1AD3B85EBE48E000L
packet.items.map { it.itemId.toInt() } shouldBe listOf(5004, 5017, 5018, 5022, 5019)
packet.items.map { it.price } shouldBe listOf(200, 200, 200, 300, 300)
// Every line carries the same endless shelf marker.
packet.items.map { it.stock.toInt() }.toSet() shouldBe setOf(0x7FFF)
ShopCatalogPacketCodec.assertBytesRoundtrip(bytes)
}
test("a close carries nothing but its marker") {
val bytes = fixture("game/s2c/23/shop_closed.bin")
ShopCatalogPacketCodec.decodeBytes(bytes).kind shouldBe ShopCatalogPacket.CLOSED
ShopCatalogPacketCodec.assertBytesRoundtrip(bytes)
}
test("a buy names the item and how many of it") {
val bytes = fixture("game/c2s/23/buy_poke_balls.bin")
val packet = ExchangeItemRequestPacketCodec.decodeBytes(bytes)
packet.itemEntityRef.toInt() shouldBe 5004
packet.quantity.toInt() shouldBe 3
ExchangeItemRequestPacketCodec.assertBytesRoundtrip(bytes)
}
})