From 2360beb77bd15d8be4cc0c6ae2263368eab6a6d8 Mon Sep 17 00:00:00 2001 From: Martin Michelsen Date: Mon, 4 Dec 2023 18:43:37 -0800 Subject: [PATCH] sort bank contents before sending to client --- src/ItemData.cc | 11 +++++++++++ src/ItemData.hh | 6 ++++++ src/PlayerSubordinates.cc | 4 ++++ src/PlayerSubordinates.hh | 6 ++++++ src/SendCommands.cc | 3 ++- 5 files changed, 29 insertions(+), 1 deletion(-) diff --git a/src/ItemData.cc b/src/ItemData.cc index b10be8e9..a72ff8cc 100644 --- a/src/ItemData.cc +++ b/src/ItemData.cc @@ -43,6 +43,17 @@ bool ItemData::operator!=(const ItemData& other) const { return !this->operator==(other); } +bool ItemData::operator<(const ItemData& other) const { + for (size_t z = 0; z < 3; z++) { + if (this->data1db[z] < other.data1db[z]) { + return true; + } else if (this->data1db[z] > other.data1db[z]) { + return false; + } + } + return (this->data2db < other.data2db); +} + void ItemData::clear() { this->data1d.clear(0); this->id = 0xFFFFFFFF; diff --git a/src/ItemData.hh b/src/ItemData.hh index c003873c..e0506969 100644 --- a/src/ItemData.hh +++ b/src/ItemData.hh @@ -111,13 +111,17 @@ struct ItemData { // 0x14 bytes union { parray data1; parray data1w; + parray data1wb; parray data1d; + parray data1db; } __attribute__((packed)); le_uint32_t id; union { parray data2; parray data2w; + parray data2wb; le_uint32_t data2d; + be_uint32_t data2db; } __attribute__((packed)); ItemData(); @@ -128,6 +132,8 @@ struct ItemData { // 0x14 bytes bool operator==(const ItemData& other) const; bool operator!=(const ItemData& other) const; + bool operator<(const ItemData& other) const; + void clear(); static ItemData from_data(const std::string& data); diff --git a/src/PlayerSubordinates.cc b/src/PlayerSubordinates.cc index 4fff22ec..23607822 100644 --- a/src/PlayerSubordinates.cc +++ b/src/PlayerSubordinates.cc @@ -683,6 +683,10 @@ size_t PlayerBank::find_item(uint32_t item_id) { throw out_of_range("item not present"); } +void PlayerBank::sort() { + std::sort(this->items.data(), this->items.data() + this->num_items); +} + BattleRules::BattleRules(const JSON& json) { static const JSON empty_list = JSON::list(); diff --git a/src/PlayerSubordinates.hh b/src/PlayerSubordinates.hh index 14dc4dab..7820a199 100644 --- a/src/PlayerSubordinates.hh +++ b/src/PlayerSubordinates.hh @@ -63,6 +63,10 @@ struct PlayerBankItem { /* 14 */ le_uint16_t amount = 0; /* 16 */ le_uint16_t present = 0; /* 18 */ + + inline bool operator<(const PlayerBankItem& other) const { + return this->data < other.data; + } } __attribute__((packed)); struct PlayerInventory { @@ -99,6 +103,8 @@ struct PlayerBank { void add_item(const ItemData& item); ItemData remove_item_by_index(size_t index, uint32_t amount); size_t find_item(uint32_t item_id); + + void sort(); } __attribute__((packed)); struct PlayerDispDataBB; diff --git a/src/SendCommands.cc b/src/SendCommands.cc index 30f5d808..a6a84b3c 100644 --- a/src/SendCommands.cc +++ b/src/SendCommands.cc @@ -2361,7 +2361,8 @@ void send_bank(shared_ptr c) { } auto p = c->game_data.character(); - const auto& bank = c->game_data.current_bank(); + auto& bank = c->game_data.current_bank(); + bank.sort(); const auto* items_it = bank.items.data(); vector items(items_it, items_it + bank.num_items);