From b0965f094ad759c8e0c5ddd39f5ea44d107342d8 Mon Sep 17 00:00:00 2001 From: David Cernat Date: Fri, 24 Nov 2017 14:28:05 +0200 Subject: [PATCH] [General] Rework PlayerAttribute packets so they are of minimal size Previously, whenever a single attribute value changed for a player, that player then sent a PlayerAttribute packet with all values for all 8 attributes. This did not cause anywhere as much packet spam as PlayerSkill used to, but there was no good reason not to fix it as well. --- apps/openmw-mp/Player.cpp | 9 +++++++ apps/openmw/mwmp/LocalPlayer.cpp | 15 +++++------ components/openmw-mp/Base/BasePlayer.hpp | 9 +++++++ .../Packets/Player/PacketPlayerAttribute.cpp | 25 ++++++++++++++----- 4 files changed, 43 insertions(+), 15 deletions(-) diff --git a/apps/openmw-mp/Player.cpp b/apps/openmw-mp/Player.cpp index d077acfde..1e91b80e3 100644 --- a/apps/openmw-mp/Player.cpp +++ b/apps/openmw-mp/Player.cpp @@ -164,6 +164,8 @@ void Player::update() packet->setPlayer(basePlayer); packet->Send(false); packet->Send(true); + + attributeChanges.attributeIndexes.clear(); } if (skillsChanged) @@ -485,6 +487,10 @@ void Player::setAttribute(unsigned short id, int base, int current) creatureStats.mAttributes[id].mBase = base; creatureStats.mAttributes[id].mCurrent = current; + + if (std::find(attributeChanges.attributeIndexes.begin(), attributeChanges.attributeIndexes.end(), id) == attributeChanges.attributeIndexes.end()) + attributeChanges.attributeIndexes.push_back(id); + attributesChanged = true; } @@ -525,6 +531,9 @@ void Player::setSkillIncrease(unsigned short attributeId, int increase) npcStats.mSkillIncrease[attributeId] = increase; + if (std::find(attributeChanges.attributeIndexes.begin(), attributeChanges.attributeIndexes.end(), attributeId) == attributeChanges.attributeIndexes.end()) + attributeChanges.attributeIndexes.push_back(attributeId); + attributesChanged = true; } diff --git a/apps/openmw/mwmp/LocalPlayer.cpp b/apps/openmw/mwmp/LocalPlayer.cpp index 0e43a4c6f..d47f239c7 100644 --- a/apps/openmw/mwmp/LocalPlayer.cpp +++ b/apps/openmw/mwmp/LocalPlayer.cpp @@ -228,27 +228,24 @@ void LocalPlayer::updateAttributes(bool forceUpdate) MWWorld::Ptr ptrPlayer = getPlayerPtr(); const MWMechanics::NpcStats &ptrNpcStats = ptrPlayer.getClass().getNpcStats(ptrPlayer); - bool attributesChanged = false; for (int i = 0; i < 8; ++i) { - if (ptrNpcStats.getAttribute(i).getBase() != creatureStats.mAttributes[i].mBase) + if (ptrNpcStats.getAttribute(i).getBase() != creatureStats.mAttributes[i].mBase || + ptrNpcStats.getSkillIncrease(i) != npcStats.mSkillIncrease[i] || + forceUpdate) { ptrNpcStats.getAttribute(i).writeState(creatureStats.mAttributes[i]); - attributesChanged = true; - } - - if (ptrNpcStats.getSkillIncrease(i) != npcStats.mSkillIncrease[i]) - { npcStats.mSkillIncrease[i] = ptrNpcStats.getSkillIncrease(i); - attributesChanged = true; + attributeChanges.attributeIndexes.push_back(i); } } - if (attributesChanged || forceUpdate) + if (attributeChanges.attributeIndexes.size() > 0) { getNetworking()->getPlayerPacket(ID_PLAYER_ATTRIBUTE)->setPlayer(this); getNetworking()->getPlayerPacket(ID_PLAYER_ATTRIBUTE)->Send(); + attributeChanges.attributeIndexes.clear(); } } diff --git a/components/openmw-mp/Base/BasePlayer.hpp b/components/openmw-mp/Base/BasePlayer.hpp index de54314e7..01073c1f2 100644 --- a/components/openmw-mp/Base/BasePlayer.hpp +++ b/components/openmw-mp/Base/BasePlayer.hpp @@ -79,6 +79,14 @@ namespace mwmp int type; // 0 - Cell load, 1 - Cell unload }; + // Track only the indexes of the attributes that have been changed, + // with the attribute values themselves being stored in creatureStats.mAttributes + struct AttributeChanges + { + std::vector attributeIndexes; + unsigned int count; + }; + // Track only the indexes of the skills that have been changed, // with the skill values themselves being stored in npcStats.mSkills struct SkillChanges @@ -213,6 +221,7 @@ namespace mwmp int day; double hour; + AttributeChanges attributeChanges; SkillChanges skillChanges; SpellbookChanges spellbookChanges; diff --git a/components/openmw-mp/Packets/Player/PacketPlayerAttribute.cpp b/components/openmw-mp/Packets/Player/PacketPlayerAttribute.cpp index 089212d7a..0ba1fc1db 100644 --- a/components/openmw-mp/Packets/Player/PacketPlayerAttribute.cpp +++ b/components/openmw-mp/Packets/Player/PacketPlayerAttribute.cpp @@ -1,8 +1,5 @@ -// -// Created by koncord on 08.03.16. -// - #include "PacketPlayerAttribute.hpp" + #include using namespace mwmp; @@ -16,7 +13,23 @@ void PacketPlayerAttribute::Packet(RakNet::BitStream *bs, bool send) { PlayerPacket::Packet(bs, send); - RW(player->creatureStats.mAttributes, send); + if (send) + player->attributeChanges.count = (unsigned int)(player->attributeChanges.attributeIndexes.size()); + else + player->attributeChanges.attributeIndexes.clear(); - RW(player->npcStats.mSkillIncrease, send); + RW(player->attributeChanges.count, send); + + for (unsigned int i = 0; i < player->attributeChanges.count; i++) + { + int attributeId; + + if (send) + attributeId = player->attributeChanges.attributeIndexes.at(i); + + RW(attributeId, send); + + RW(player->creatureStats.mAttributes[attributeId], send); + RW(player->npcStats.mSkillIncrease[attributeId], send); + } }