From d1ad0c91f8c9208d2cfcc634eb234abf53f2a739 Mon Sep 17 00:00:00 2001 From: David Cernat Date: Thu, 30 Nov 2017 12:31:54 +0200 Subject: [PATCH] [General] Rework PlayerEquipment packets so they are of minimal size Moreover, rename BaseNetCreature's equipedItems into equipmentItems. --- apps/openmw-mp/Inventory.cpp | 16 +++++++--- apps/openmw/mwmp/Cell.cpp | 2 +- apps/openmw/mwmp/DedicatedActor.cpp | 6 ++-- apps/openmw/mwmp/DedicatedPlayer.cpp | 4 +-- apps/openmw/mwmp/LocalActor.cpp | 2 +- apps/openmw/mwmp/LocalPlayer.cpp | 31 ++++++++----------- components/openmw-mp/Base/BaseNetCreature.hpp | 7 ++++- .../Packets/Actor/PacketActorEquipment.cpp | 8 ++--- .../Packets/Player/PacketPlayerEquipment.cpp | 26 +++++++++++----- 9 files changed, 59 insertions(+), 43 deletions(-) diff --git a/apps/openmw-mp/Inventory.cpp b/apps/openmw-mp/Inventory.cpp index 75a1fac74..dc701865e 100644 --- a/apps/openmw-mp/Inventory.cpp +++ b/apps/openmw-mp/Inventory.cpp @@ -78,9 +78,13 @@ int Inventory::getChangesSize() const void Inventory::equipItem(unsigned short slot, const std::string& refId, unsigned int count, int charge) { - netActor->getNetCreature()->equipedItems[slot].refId = refId; - netActor->getNetCreature()->equipedItems[slot].count = count; - netActor->getNetCreature()->equipedItems[slot].charge = charge; + netActor->getNetCreature()->equipmentItems[slot].refId = refId; + netActor->getNetCreature()->equipmentItems[slot].count = count; + netActor->getNetCreature()->equipmentItems[slot].charge = charge; + + if (!Utils::vectorContains(&netActor->getNetCreature()->equipmentIndexChanges, slot)) + netActor->getNetCreature()->equipmentIndexChanges.push_back(slot); + equipmentChanged = true; } @@ -126,14 +130,14 @@ void Inventory::removeItem(const std::string &refId, unsigned short count) bool Inventory::hasItemEquipped(const std::string &refId) const { for (int slot = 0; slot < MWWorld::InventoryStore::Slots; slot++) - if (Misc::StringUtils::ciEqual(netActor->getNetCreature()->equipedItems[slot].refId, refId)) + if (Misc::StringUtils::ciEqual(netActor->getNetCreature()->equipmentItems[slot].refId, refId)) return true; return false; } std::tuple Inventory::getEquipmentItem(unsigned short slot) const { - const auto &item = netActor->getNetCreature()->equipedItems[slot]; + const auto &item = netActor->getNetCreature()->equipmentItems[slot]; return make_tuple(item.refId, item.count, item.charge); } @@ -146,6 +150,8 @@ std::tuple Inventory::getInventoryItem(unsigned int slot) void Inventory::resetEquipmentFlag() { equipmentChanged = false; + + netActor->getNetCreature()->equipmentIndexChanges.clear(); } bool Inventory::isEquipmentChanged() diff --git a/apps/openmw/mwmp/Cell.cpp b/apps/openmw/mwmp/Cell.cpp index d64d208dd..b63716d4e 100644 --- a/apps/openmw/mwmp/Cell.cpp +++ b/apps/openmw/mwmp/Cell.cpp @@ -221,7 +221,7 @@ void Cell::readEquipment(ActorList& actorList) DedicatedActor *actor = dedicatedActors[mapIndex]; for (int slot = 0; slot < 19; ++slot) - actor->equipedItems[slot] = baseActor->equipedItems[slot]; + actor->equipmentItems[slot] = baseActor->equipmentItems[slot]; actor->setEquipment(); } diff --git a/apps/openmw/mwmp/DedicatedActor.cpp b/apps/openmw/mwmp/DedicatedActor.cpp index 01ad37743..c73c027f6 100644 --- a/apps/openmw/mwmp/DedicatedActor.cpp +++ b/apps/openmw/mwmp/DedicatedActor.cpp @@ -168,8 +168,8 @@ void DedicatedActor::setEquipment() { MWWorld::ContainerStoreIterator it = invStore.getSlot(slot); - const string &packetRefId = equipedItems[slot].refId; - int packetCharge = equipedItems[slot].charge; + const string &packetRefId = equipmentItems[slot].refId; + int packetCharge = equipmentItems[slot].charge; std::string storeRefId = ""; bool equal = false; @@ -186,7 +186,7 @@ void DedicatedActor::setEquipment() if (packetRefId.empty() || equal) continue; - int count = equipedItems[slot].count; + int count = equipmentItems[slot].count; if (hasItem(packetRefId, packetCharge)) equipItem(packetRefId, packetCharge); diff --git a/apps/openmw/mwmp/DedicatedPlayer.cpp b/apps/openmw/mwmp/DedicatedPlayer.cpp index f5f8d3bfd..bdabede71 100644 --- a/apps/openmw/mwmp/DedicatedPlayer.cpp +++ b/apps/openmw/mwmp/DedicatedPlayer.cpp @@ -166,7 +166,7 @@ void DedicatedPlayer::setEquipment() { MWWorld::ContainerStoreIterator it = invStore.getSlot(slot); - const string &dedicItem = equipedItems[slot].refId; + const string &dedicItem = equipmentItems[slot].refId; std::string item = ""; bool equal = false; if (it != invStore.end()) @@ -184,7 +184,7 @@ void DedicatedPlayer::setEquipment() if (dedicItem.empty() || equal) continue; - const int count = equipedItems[slot].count; + const int count = equipmentItems[slot].count; ptr.getClass().getContainerStore(ptr).add(dedicItem, count, ptr); for (const auto &ptr : invStore) diff --git a/apps/openmw/mwmp/LocalActor.cpp b/apps/openmw/mwmp/LocalActor.cpp index 038143adc..e45f4b8c5 100644 --- a/apps/openmw/mwmp/LocalActor.cpp +++ b/apps/openmw/mwmp/LocalActor.cpp @@ -194,7 +194,7 @@ void LocalActor::updateEquipment(bool forceUpdate) for (int slot = 0; slot < MWWorld::InventoryStore::Slots; slot++) { MWWorld::ContainerStoreIterator it = invStore.getSlot(slot); - auto &item = equipedItems[slot]; + auto &item = equipmentItems[slot]; if (it != invStore.end()) { diff --git a/apps/openmw/mwmp/LocalPlayer.cpp b/apps/openmw/mwmp/LocalPlayer.cpp index e6726e816..89c76c853 100644 --- a/apps/openmw/mwmp/LocalPlayer.cpp +++ b/apps/openmw/mwmp/LocalPlayer.cpp @@ -235,9 +235,9 @@ void LocalPlayer::updateAttributes(bool forceUpdate) ptrNpcStats.getSkillIncrease(i) != npcStats.mSkillIncrease[i] || forceUpdate) { + attributeIndexChanges.push_back(i); ptrNpcStats.getAttribute(i).writeState(creatureStats.mAttributes[i]); npcStats.mSkillIncrease[i] = ptrNpcStats.getSkillIncrease(i); - attributeIndexChanges.push_back(i); } } @@ -265,8 +265,8 @@ void LocalPlayer::updateSkills(bool forceUpdate) ptrNpcStats.getSkill(i).getProgress() != npcStats.mSkills[i].mProgress || forceUpdate) { - ptrNpcStats.getSkill(i).writeState(npcStats.mSkills[i]); skillIndexChanges.push_back(i); + ptrNpcStats.getSkill(i).writeState(npcStats.mSkills[i]); } } @@ -399,24 +399,19 @@ void LocalPlayer::updateEquipment(bool forceUpdate) { MWWorld::Ptr ptrPlayer = getPlayerPtr(); - static bool equipmentChanged = false; - - if (forceUpdate) - equipmentChanged = true; - MWWorld::InventoryStore &invStore = ptrPlayer.getClass().getInventoryStore(ptrPlayer); for (int slot = 0; slot < MWWorld::InventoryStore::Slots; slot++) { - auto &item = equipedItems[slot]; + auto &item = equipmentItems[slot]; MWWorld::ContainerStoreIterator it = invStore.getSlot(slot); if (it != invStore.end()) { - if (!::Misc::StringUtils::ciEqual(it->getCellRef().getRefId(), equipedItems[slot].refId)) + if (!::Misc::StringUtils::ciEqual(it->getCellRef().getRefId(), equipmentItems[slot].refId) || forceUpdate) { - equipmentChanged = true; - + equipmentIndexChanges.push_back(slot); item.refId = it->getCellRef().getRefId(); item.charge = it->getCellRef().getCharge(); + if (slot == MWWorld::InventoryStore::Slot_CarriedRight) { MWMechanics::WeaponType weaptype; @@ -431,18 +426,18 @@ void LocalPlayer::updateEquipment(bool forceUpdate) } else if (!item.refId.empty()) { - equipmentChanged = true; + equipmentIndexChanges.push_back(slot); item.refId = ""; item.count = 0; item.charge = 0; } } - if (equipmentChanged) + if (equipmentIndexChanges.size() > 0) { getNetworking()->getPlayerPacket(ID_PLAYER_EQUIPMENT)->setPlayer(this); getNetworking()->getPlayerPacket(ID_PLAYER_EQUIPMENT)->Send(); - equipmentChanged = false; + equipmentIndexChanges.clear(); } } @@ -901,7 +896,7 @@ void LocalPlayer::setEquipment() for (int slot = 0; slot < MWWorld::InventoryStore::Slots; slot++) { - mwmp::Item ¤tItem = equipedItems[slot]; + mwmp::Item ¤tItem = equipmentItems[slot]; if (!currentItem.refId.empty()) { @@ -911,16 +906,16 @@ void LocalPlayer::setEquipment() if (it == ptrInventory.end()) // If the item is not in our inventory, add it { - auto equipped = equipedItems[slot]; + auto equipmentItem = equipmentItems[slot]; try { - auto addIter = ptrInventory.ContainerStore::add(equipped.refId.c_str(), equipped.count, ptrPlayer); + auto addIter = ptrInventory.ContainerStore::add(equipmentItem.refId.c_str(), equipmentItem.count, ptrPlayer); ptrInventory.equip(slot, addIter, ptrPlayer); } catch (std::exception&) { - LOG_APPEND(Log::LOG_INFO, "- Ignored addition of invalid equipment item %s", equipped.refId.c_str()); + LOG_APPEND(Log::LOG_INFO, "- Ignored addition of invalid equipment item %s", equipmentItem.refId.c_str()); } } else diff --git a/components/openmw-mp/Base/BaseNetCreature.hpp b/components/openmw-mp/Base/BaseNetCreature.hpp index 07263999c..6937eae41 100644 --- a/components/openmw-mp/Base/BaseNetCreature.hpp +++ b/components/openmw-mp/Base/BaseNetCreature.hpp @@ -26,7 +26,12 @@ namespace mwmp bool isChangingRegion; Attack attack; - Item equipedItems[19]; + + // Track only the indexes of the equipment items that have been changed, + // with the items themselves being stored in equipmentItems + std::vector equipmentIndexChanges; + + Item equipmentItems[19]; InventoryChanges inventoryChanges; unsigned int movementFlags; diff --git a/components/openmw-mp/Packets/Actor/PacketActorEquipment.cpp b/components/openmw-mp/Packets/Actor/PacketActorEquipment.cpp index 42a519347..47a96a1c7 100644 --- a/components/openmw-mp/Packets/Actor/PacketActorEquipment.cpp +++ b/components/openmw-mp/Packets/Actor/PacketActorEquipment.cpp @@ -11,10 +11,10 @@ PacketActorEquipment::PacketActorEquipment(RakNet::RakPeerInterface *peer) : Act void PacketActorEquipment::Actor(BaseActor &actor, bool send) { - for (auto &&equipedItem : actor.equipedItems) + for (auto &&equipmentItem : actor.equipmentItems) { - RW(equipedItem.refId, send); - RW(equipedItem.count, send); - RW(equipedItem.charge, send); + RW(equipmentItem.refId, send); + RW(equipmentItem.count, send); + RW(equipmentItem.charge, send); } } diff --git a/components/openmw-mp/Packets/Player/PacketPlayerEquipment.cpp b/components/openmw-mp/Packets/Player/PacketPlayerEquipment.cpp index faee42358..f3c2b8069 100644 --- a/components/openmw-mp/Packets/Player/PacketPlayerEquipment.cpp +++ b/components/openmw-mp/Packets/Player/PacketPlayerEquipment.cpp @@ -1,7 +1,3 @@ -// -// Created by koncord on 07.01.16. -// - #include "PacketPlayerEquipment.hpp" #include @@ -16,10 +12,24 @@ void PacketPlayerEquipment::Packet(RakNet::BitStream *bs, bool send) { PlayerPacket::Packet(bs, send); - for (auto &equipedItem : player->equipedItems) + uint32_t count; + if (send) + count = static_cast(player->equipmentIndexChanges.size()); + + RW(count, send); + + if (!send) { - RW(equipedItem.refId, send, true); - RW(equipedItem.count, send); - RW(equipedItem.charge, send); + player->equipmentIndexChanges.clear(); + player->equipmentIndexChanges.resize(count); + } + + for (auto &&equipmentIndex : player->equipmentIndexChanges) + { + RW(equipmentIndex, send); + + RW(player->equipmentItems[equipmentIndex].refId, send); + RW(player->equipmentItems[equipmentIndex].count, send); + RW(player->equipmentItems[equipmentIndex].charge, send); } }