From b97322b4b113b7049c98f569a57a3820f9b6f429 Mon Sep 17 00:00:00 2001 From: David Cernat Date: Sat, 26 Oct 2019 14:01:08 +0300 Subject: [PATCH] [General] Modernize packet style for PlayerInventory --- apps/openmw-mp/Script/Functions/Items.cpp | 6 ++--- components/openmw-mp/Base/BasePlayer.hpp | 2 -- .../Packets/Player/PacketPlayerInventory.cpp | 22 +++++++++---------- 3 files changed, 13 insertions(+), 17 deletions(-) diff --git a/apps/openmw-mp/Script/Functions/Items.cpp b/apps/openmw-mp/Script/Functions/Items.cpp index ba9bf7d88..400f7e9de 100644 --- a/apps/openmw-mp/Script/Functions/Items.cpp +++ b/apps/openmw-mp/Script/Functions/Items.cpp @@ -27,7 +27,7 @@ unsigned int ItemFunctions::GetInventoryChangesSize(unsigned short pid) noexcept Player *player; GET_PLAYER(pid, player, 0); - return player->inventoryChanges.count; + return player->inventoryChanges.items.size(); } unsigned int ItemFunctions::GetInventoryChangesAction(unsigned short pid) noexcept @@ -133,7 +133,7 @@ const char *ItemFunctions::GetInventoryItemRefId(unsigned short pid, unsigned in Player *player; GET_PLAYER(pid, player, ""); - if (index >= player->inventoryChanges.count) + if (index >= player->inventoryChanges.items.size()) return "invalid"; return player->inventoryChanges.items.at(index).refId.c_str(); @@ -168,7 +168,7 @@ const char *ItemFunctions::GetInventoryItemSoul(unsigned short pid, unsigned int Player *player; GET_PLAYER(pid, player, ""); - if (index >= player->inventoryChanges.count) + if (index >= player->inventoryChanges.items.size()) return "invalid"; return player->inventoryChanges.items.at(index).soul.c_str(); diff --git a/components/openmw-mp/Base/BasePlayer.hpp b/components/openmw-mp/Base/BasePlayer.hpp index e3ca16fa0..5e1b1184d 100644 --- a/components/openmw-mp/Base/BasePlayer.hpp +++ b/components/openmw-mp/Base/BasePlayer.hpp @@ -105,7 +105,6 @@ namespace mwmp struct InventoryChanges { std::vector items; - unsigned int count; enum ACTION_TYPE { SET = 0, @@ -172,7 +171,6 @@ namespace mwmp BasePlayer(RakNet::RakNetGUID guid) : guid(guid) { inventoryChanges.action = 0; - inventoryChanges.count = 0; spellbookChanges.action = 0; exchangeFullInfo = false; diff --git a/components/openmw-mp/Packets/Player/PacketPlayerInventory.cpp b/components/openmw-mp/Packets/Player/PacketPlayerInventory.cpp index 16031da2f..2bf6389e3 100644 --- a/components/openmw-mp/Packets/Player/PacketPlayerInventory.cpp +++ b/components/openmw-mp/Packets/Player/PacketPlayerInventory.cpp @@ -15,27 +15,25 @@ void PacketPlayerInventory::Packet(RakNet::BitStream *bs, bool send) RW(player->inventoryChanges.action, send); + uint32_t count; + if (send) - player->inventoryChanges.count = (unsigned int) (player->inventoryChanges.items.size()); - else - player->inventoryChanges.items.clear(); + count = static_cast(player->inventoryChanges.items.size()); - RW(player->inventoryChanges.count, send); + RW(count, send); - for (unsigned int i = 0; i < player->inventoryChanges.count; i++) + if (!send) { - Item item; - - if (send) - item = player->inventoryChanges.items.at(i); + player->inventoryChanges.items.clear(); + player->inventoryChanges.items.resize(count); + } + for (auto &&item : player->inventoryChanges.items) + { RW(item.refId, send, true); RW(item.count, send); RW(item.charge, send); RW(item.enchantmentCharge, send); RW(item.soul, send, true); - - if (!send) - player->inventoryChanges.items.push_back(item); } }