[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.
new-script-api
David Cernat 7 years ago
parent ef79a98544
commit b0965f094a

@ -164,6 +164,8 @@ void Player::update()
packet->setPlayer(basePlayer); packet->setPlayer(basePlayer);
packet->Send(false); packet->Send(false);
packet->Send(true); packet->Send(true);
attributeChanges.attributeIndexes.clear();
} }
if (skillsChanged) if (skillsChanged)
@ -485,6 +487,10 @@ void Player::setAttribute(unsigned short id, int base, int current)
creatureStats.mAttributes[id].mBase = base; creatureStats.mAttributes[id].mBase = base;
creatureStats.mAttributes[id].mCurrent = current; 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; attributesChanged = true;
} }
@ -525,6 +531,9 @@ void Player::setSkillIncrease(unsigned short attributeId, int increase)
npcStats.mSkillIncrease[attributeId] = 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; attributesChanged = true;
} }

@ -228,27 +228,24 @@ void LocalPlayer::updateAttributes(bool forceUpdate)
MWWorld::Ptr ptrPlayer = getPlayerPtr(); MWWorld::Ptr ptrPlayer = getPlayerPtr();
const MWMechanics::NpcStats &ptrNpcStats = ptrPlayer.getClass().getNpcStats(ptrPlayer); const MWMechanics::NpcStats &ptrNpcStats = ptrPlayer.getClass().getNpcStats(ptrPlayer);
bool attributesChanged = false;
for (int i = 0; i < 8; ++i) 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]); ptrNpcStats.getAttribute(i).writeState(creatureStats.mAttributes[i]);
attributesChanged = true;
}
if (ptrNpcStats.getSkillIncrease(i) != npcStats.mSkillIncrease[i])
{
npcStats.mSkillIncrease[i] = ptrNpcStats.getSkillIncrease(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)->setPlayer(this);
getNetworking()->getPlayerPacket(ID_PLAYER_ATTRIBUTE)->Send(); getNetworking()->getPlayerPacket(ID_PLAYER_ATTRIBUTE)->Send();
attributeChanges.attributeIndexes.clear();
} }
} }

@ -79,6 +79,14 @@ namespace mwmp
int type; // 0 - Cell load, 1 - Cell unload 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<int> attributeIndexes;
unsigned int count;
};
// Track only the indexes of the skills that have been changed, // Track only the indexes of the skills that have been changed,
// with the skill values themselves being stored in npcStats.mSkills // with the skill values themselves being stored in npcStats.mSkills
struct SkillChanges struct SkillChanges
@ -213,6 +221,7 @@ namespace mwmp
int day; int day;
double hour; double hour;
AttributeChanges attributeChanges;
SkillChanges skillChanges; SkillChanges skillChanges;
SpellbookChanges spellbookChanges; SpellbookChanges spellbookChanges;

@ -1,8 +1,5 @@
//
// Created by koncord on 08.03.16.
//
#include "PacketPlayerAttribute.hpp" #include "PacketPlayerAttribute.hpp"
#include <components/openmw-mp/NetworkMessages.hpp> #include <components/openmw-mp/NetworkMessages.hpp>
using namespace mwmp; using namespace mwmp;
@ -16,7 +13,23 @@ void PacketPlayerAttribute::Packet(RakNet::BitStream *bs, bool send)
{ {
PlayerPacket::Packet(bs, 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->attributeChanges.count, send);
for (unsigned int i = 0; i < player->attributeChanges.count; i++)
{
int attributeId;
if (send)
attributeId = player->attributeChanges.attributeIndexes.at(i);
RW(player->npcStats.mSkillIncrease, send); RW(attributeId, send);
RW(player->creatureStats.mAttributes[attributeId], send);
RW(player->npcStats.mSkillIncrease[attributeId], send);
}
} }

Loading…
Cancel
Save