From b9520c11da06089712def7f839e1bff71280244d Mon Sep 17 00:00:00 2001 From: David Cernat Date: Fri, 20 Apr 2018 22:46:16 +0300 Subject: [PATCH] [General] Rework PlayerEquipment packets so they are of minimal size Moreover, rename BaseNetCreature's equipedItems into equipmentItems. (cherry picked from commit d1ad0c91f8c9208d2cfcc634eb234abf53f2a739) --- apps/openmw-mp/Script/Functions/Actors.cpp | 16 +++++----- apps/openmw-mp/Script/Functions/Items.cpp | 23 +++++++++------ 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 | 29 ++++++++----------- components/openmw-mp/Base/BaseActor.hpp | 2 +- components/openmw-mp/Base/BasePlayer.hpp | 6 +++- .../Packets/Actor/PacketActorEquipment.cpp | 10 +++---- .../Packets/Player/PacketPlayerEquipment.cpp | 29 ++++++++++++------- 11 files changed, 71 insertions(+), 58 deletions(-) diff --git a/apps/openmw-mp/Script/Functions/Actors.cpp b/apps/openmw-mp/Script/Functions/Actors.cpp index c195f4b4b..e41b2e9a6 100644 --- a/apps/openmw-mp/Script/Functions/Actors.cpp +++ b/apps/openmw-mp/Script/Functions/Actors.cpp @@ -150,22 +150,22 @@ double ActorFunctions::GetActorFatigueModified(unsigned int i) noexcept const char *ActorFunctions::GetActorEquipmentItemRefId(unsigned int i, unsigned short slot) noexcept { - return readActorList->baseActors.at(i).equipedItems[slot].refId.c_str(); + return readActorList->baseActors.at(i).equipmentItems[slot].refId.c_str(); } int ActorFunctions::GetActorEquipmentItemCount(unsigned int i, unsigned short slot) noexcept { - return readActorList->baseActors.at(i).equipedItems[slot].count; + return readActorList->baseActors.at(i).equipmentItems[slot].count; } int ActorFunctions::GetActorEquipmentItemCharge(unsigned int i, unsigned short slot) noexcept { - return readActorList->baseActors.at(i).equipedItems[slot].charge; + return readActorList->baseActors.at(i).equipmentItems[slot].charge; } double ActorFunctions::GetActorEquipmentItemEnchantmentCharge(unsigned int i, unsigned short slot) noexcept { - return readActorList->baseActors.at(i).equipedItems[slot].enchantmentCharge; + return readActorList->baseActors.at(i).equipmentItems[slot].enchantmentCharge; } bool ActorFunctions::DoesActorHavePosition(unsigned int i) noexcept @@ -269,10 +269,10 @@ void ActorFunctions::SetActorFatigueModified(double value) noexcept void ActorFunctions::EquipActorItem(unsigned short slot, const char *refId, unsigned int count, int charge, double enchantmentCharge) noexcept { - tempActor.equipedItems[slot].refId = refId; - tempActor.equipedItems[slot].count = count; - tempActor.equipedItems[slot].charge = charge; - tempActor.equipedItems[slot].enchantmentCharge = enchantmentCharge; + tempActor.equipmentItems[slot].refId = refId; + tempActor.equipmentItems[slot].count = count; + tempActor.equipmentItems[slot].charge = charge; + tempActor.equipmentItems[slot].enchantmentCharge = enchantmentCharge; } void ActorFunctions::UnequipActorItem(unsigned short slot) noexcept diff --git a/apps/openmw-mp/Script/Functions/Items.cpp b/apps/openmw-mp/Script/Functions/Items.cpp index 1bc35a829..3d717cd41 100644 --- a/apps/openmw-mp/Script/Functions/Items.cpp +++ b/apps/openmw-mp/Script/Functions/Items.cpp @@ -40,10 +40,13 @@ void ItemFunctions::EquipItem(unsigned short pid, unsigned short slot, const cha Player *player; GET_PLAYER(pid, player,); - player->equipedItems[slot].refId = refId; - player->equipedItems[slot].count = count; - player->equipedItems[slot].charge = charge; - player->equipedItems[slot].enchantmentCharge = enchantmentCharge; + player->equipmentItems[slot].refId = refId; + player->equipmentItems[slot].count = count; + player->equipmentItems[slot].charge = charge; + player->equipmentItems[slot].enchantmentCharge = enchantmentCharge; + + if (!Utils::vectorContains(&player->equipmentIndexChanges, slot)) + player->equipmentIndexChanges.push_back(slot); } void ItemFunctions::UnequipItem(unsigned short pid, unsigned short slot) noexcept @@ -88,7 +91,7 @@ bool ItemFunctions::HasItemEquipped(unsigned short pid, const char* refId) GET_PLAYER(pid, player, false); for (int slot = 0; slot < MWWorld::InventoryStore::Slots; slot++) - if (Misc::StringUtils::ciEqual(player->equipedItems[slot].refId, refId)) + if (Misc::StringUtils::ciEqual(player->equipmentItems[slot].refId, refId)) return true; return false; } @@ -98,7 +101,7 @@ const char *ItemFunctions::GetEquipmentItemRefId(unsigned short pid, unsigned sh Player *player; GET_PLAYER(pid, player, 0); - return player->equipedItems[slot].refId.c_str(); + return player->equipmentItems[slot].refId.c_str(); } int ItemFunctions::GetEquipmentItemCount(unsigned short pid, unsigned short slot) noexcept @@ -106,7 +109,7 @@ int ItemFunctions::GetEquipmentItemCount(unsigned short pid, unsigned short slot Player *player; GET_PLAYER(pid, player, 0); - return player->equipedItems[slot].count; + return player->equipmentItems[slot].count; } int ItemFunctions::GetEquipmentItemCharge(unsigned short pid, unsigned short slot) noexcept @@ -114,7 +117,7 @@ int ItemFunctions::GetEquipmentItemCharge(unsigned short pid, unsigned short slo Player *player; GET_PLAYER(pid, player, 0); - return player->equipedItems[slot].charge; + return player->equipmentItems[slot].charge; } double ItemFunctions::GetEquipmentItemEnchantmentCharge(unsigned short pid, unsigned short slot) noexcept @@ -122,7 +125,7 @@ double ItemFunctions::GetEquipmentItemEnchantmentCharge(unsigned short pid, unsi Player *player; GET_PLAYER(pid, player, 0); - return player->equipedItems[slot].enchantmentCharge; + return player->equipmentItems[slot].enchantmentCharge; } const char *ItemFunctions::GetInventoryItemRefId(unsigned short pid, unsigned int i) noexcept @@ -168,6 +171,8 @@ void ItemFunctions::SendEquipment(unsigned short pid) noexcept mwmp::Networking::get().getPlayerPacketController()->GetPacket(ID_PLAYER_EQUIPMENT)->setPlayer(player); mwmp::Networking::get().getPlayerPacketController()->GetPacket(ID_PLAYER_EQUIPMENT)->Send(false); mwmp::Networking::get().getPlayerPacketController()->GetPacket(ID_PLAYER_EQUIPMENT)->Send(true); + + player->equipmentIndexChanges.clear(); } void ItemFunctions::SendInventoryChanges(unsigned short pid, bool toOthers) noexcept diff --git a/apps/openmw/mwmp/Cell.cpp b/apps/openmw/mwmp/Cell.cpp index a70de539e..364121b80 100644 --- a/apps/openmw/mwmp/Cell.cpp +++ b/apps/openmw/mwmp/Cell.cpp @@ -223,7 +223,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 6efad4ded..61260cf3d 100644 --- a/apps/openmw/mwmp/DedicatedActor.cpp +++ b/apps/openmw/mwmp/DedicatedActor.cpp @@ -168,15 +168,15 @@ void DedicatedActor::setEquipment() for (int slot = 0; slot < MWWorld::InventoryStore::Slots; ++slot) { - int count = equipedItems[slot].count; + int count = equipmentItems[slot].count; // If we've somehow received a corrupted item with a count lower than 0, ignore it if (count < 0) continue; 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; diff --git a/apps/openmw/mwmp/DedicatedPlayer.cpp b/apps/openmw/mwmp/DedicatedPlayer.cpp index a5a4d4bed..453115c19 100644 --- a/apps/openmw/mwmp/DedicatedPlayer.cpp +++ b/apps/openmw/mwmp/DedicatedPlayer.cpp @@ -312,7 +312,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()) @@ -330,7 +330,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 9d86d886d..79be0e6e1 100644 --- a/apps/openmw/mwmp/LocalActor.cpp +++ b/apps/openmw/mwmp/LocalActor.cpp @@ -193,7 +193,7 @@ void LocalActor::updateEquipment(bool forceUpdate) MWWorld::InventoryStore &invStore = ptr.getClass().getInventoryStore(ptr); 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()) diff --git a/apps/openmw/mwmp/LocalPlayer.cpp b/apps/openmw/mwmp/LocalPlayer.cpp index f449ff4d5..1dc50d1dd 100644 --- a/apps/openmw/mwmp/LocalPlayer.cpp +++ b/apps/openmw/mwmp/LocalPlayer.cpp @@ -244,9 +244,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); } } @@ -274,8 +274,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]); } } @@ -424,22 +424,17 @@ 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(); @@ -449,7 +444,7 @@ void LocalPlayer::updateEquipment(bool forceUpdate) } else if (!item.refId.empty()) { - equipmentChanged = true; + equipmentIndexChanges.push_back(slot); item.refId = ""; item.count = 0; item.charge = -1; @@ -457,11 +452,11 @@ void LocalPlayer::updateEquipment(bool forceUpdate) } } - if (equipmentChanged) + if (equipmentIndexChanges.size() > 0) { getNetworking()->getPlayerPacket(ID_PLAYER_EQUIPMENT)->setPlayer(this); getNetworking()->getPlayerPacket(ID_PLAYER_EQUIPMENT)->Send(); - equipmentChanged = false; + equipmentIndexChanges.clear(); } } @@ -1061,7 +1056,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()) { @@ -1071,16 +1066,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/BaseActor.hpp b/components/openmw-mp/Base/BaseActor.hpp index be8f50e28..a3c1ba3f2 100644 --- a/components/openmw-mp/Base/BaseActor.hpp +++ b/components/openmw-mp/Base/BaseActor.hpp @@ -42,7 +42,7 @@ namespace mwmp bool hasPositionData; bool hasStatsDynamicData; - Item equipedItems[19]; + Item equipmentItems[19]; }; class BaseActorList diff --git a/components/openmw-mp/Base/BasePlayer.hpp b/components/openmw-mp/Base/BasePlayer.hpp index 69bde88f8..219d8e76f 100644 --- a/components/openmw-mp/Base/BasePlayer.hpp +++ b/components/openmw-mp/Base/BasePlayer.hpp @@ -251,6 +251,10 @@ namespace mwmp // with the skill values themselves being stored in npcStats.mSkills std::vector skillIndexChanges; + // Track only the indexes of the equipment items that have been changed, + // with the items themselves being stored in equipmentItems + std::vector equipmentIndexChanges; + InventoryChanges inventoryChanges; SpellbookChanges spellbookChanges; QuickKeyChanges quickKeyChanges; @@ -289,7 +293,7 @@ namespace mwmp ESM::Creature creature; ESM::CreatureStats creatureStats; ESM::Class charClass; - Item equipedItems[19]; + Item equipmentItems[19]; Attack attack; std::string birthsign; std::string chatMessage; diff --git a/components/openmw-mp/Packets/Actor/PacketActorEquipment.cpp b/components/openmw-mp/Packets/Actor/PacketActorEquipment.cpp index 3e613c78d..6cdec8ec8 100644 --- a/components/openmw-mp/Packets/Actor/PacketActorEquipment.cpp +++ b/components/openmw-mp/Packets/Actor/PacketActorEquipment.cpp @@ -11,11 +11,11 @@ PacketActorEquipment::PacketActorEquipment(RakNet::RakPeerInterface *peer) : Act void PacketActorEquipment::Actor(BaseActor &actor, bool send) { - for (int j = 0; j < 19; j++) + for (auto &&equipmentItem : actor.equipmentItems) { - RW(actor.equipedItems[j].refId, send); - RW(actor.equipedItems[j].count, send); - RW(actor.equipedItems[j].charge, send); - RW(actor.equipedItems[j].enchantmentCharge, send); + RW(equipmentItem.refId, send); + RW(equipmentItem.count, send); + RW(equipmentItem.charge, send); + RW(equipmentItem.enchantmentCharge, send); } } diff --git a/components/openmw-mp/Packets/Player/PacketPlayerEquipment.cpp b/components/openmw-mp/Packets/Player/PacketPlayerEquipment.cpp index 4b54c006c..63d7a0b1f 100644 --- a/components/openmw-mp/Packets/Player/PacketPlayerEquipment.cpp +++ b/components/openmw-mp/Packets/Player/PacketPlayerEquipment.cpp @@ -1,11 +1,6 @@ -// -// Created by koncord on 07.01.16. -// - #include "PacketPlayerEquipment.hpp" #include - using namespace mwmp; PacketPlayerEquipment::PacketPlayerEquipment(RakNet::RakPeerInterface *peer) : PlayerPacket(peer) @@ -17,11 +12,25 @@ void PacketPlayerEquipment::Packet(RakNet::BitStream *bs, bool send) { PlayerPacket::Packet(bs, send); - for (int i = 0; i < 19; i++) + uint32_t count; + if (send) + count = static_cast(player->equipmentIndexChanges.size()); + + RW(count, send); + + if (!send) { - RW(player->equipedItems[i].refId, send, 1); - RW(player->equipedItems[i].count, send); - RW(player->equipedItems[i].charge, send); - RW(player->equipedItems[i].enchantmentCharge, 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); + RW(player->equipmentItems[equipmentIndex].enchantmentCharge, send); } }