[General] Rework PlayerEquipment packets so they are of minimal size

Moreover, rename BaseNetCreature's equipedItems into equipmentItems.
new-script-api
David Cernat 7 years ago
parent 7e322f1f8b
commit d1ad0c91f8

@ -78,9 +78,13 @@ int Inventory::getChangesSize() const
void Inventory::equipItem(unsigned short slot, const std::string& refId, unsigned int count, int charge) void Inventory::equipItem(unsigned short slot, const std::string& refId, unsigned int count, int charge)
{ {
netActor->getNetCreature()->equipedItems[slot].refId = refId; netActor->getNetCreature()->equipmentItems[slot].refId = refId;
netActor->getNetCreature()->equipedItems[slot].count = count; netActor->getNetCreature()->equipmentItems[slot].count = count;
netActor->getNetCreature()->equipedItems[slot].charge = charge; netActor->getNetCreature()->equipmentItems[slot].charge = charge;
if (!Utils::vectorContains(&netActor->getNetCreature()->equipmentIndexChanges, slot))
netActor->getNetCreature()->equipmentIndexChanges.push_back(slot);
equipmentChanged = true; equipmentChanged = true;
} }
@ -126,14 +130,14 @@ void Inventory::removeItem(const std::string &refId, unsigned short count)
bool Inventory::hasItemEquipped(const std::string &refId) const bool Inventory::hasItemEquipped(const std::string &refId) const
{ {
for (int slot = 0; slot < MWWorld::InventoryStore::Slots; slot++) 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 true;
return false; return false;
} }
std::tuple<std::string, int, int> Inventory::getEquipmentItem(unsigned short slot) const std::tuple<std::string, int, int> 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); return make_tuple(item.refId, item.count, item.charge);
} }
@ -146,6 +150,8 @@ std::tuple<std::string, int, int> Inventory::getInventoryItem(unsigned int slot)
void Inventory::resetEquipmentFlag() void Inventory::resetEquipmentFlag()
{ {
equipmentChanged = false; equipmentChanged = false;
netActor->getNetCreature()->equipmentIndexChanges.clear();
} }
bool Inventory::isEquipmentChanged() bool Inventory::isEquipmentChanged()

@ -221,7 +221,7 @@ void Cell::readEquipment(ActorList& actorList)
DedicatedActor *actor = dedicatedActors[mapIndex]; DedicatedActor *actor = dedicatedActors[mapIndex];
for (int slot = 0; slot < 19; ++slot) for (int slot = 0; slot < 19; ++slot)
actor->equipedItems[slot] = baseActor->equipedItems[slot]; actor->equipmentItems[slot] = baseActor->equipmentItems[slot];
actor->setEquipment(); actor->setEquipment();
} }

@ -168,8 +168,8 @@ void DedicatedActor::setEquipment()
{ {
MWWorld::ContainerStoreIterator it = invStore.getSlot(slot); MWWorld::ContainerStoreIterator it = invStore.getSlot(slot);
const string &packetRefId = equipedItems[slot].refId; const string &packetRefId = equipmentItems[slot].refId;
int packetCharge = equipedItems[slot].charge; int packetCharge = equipmentItems[slot].charge;
std::string storeRefId = ""; std::string storeRefId = "";
bool equal = false; bool equal = false;
@ -186,7 +186,7 @@ void DedicatedActor::setEquipment()
if (packetRefId.empty() || equal) if (packetRefId.empty() || equal)
continue; continue;
int count = equipedItems[slot].count; int count = equipmentItems[slot].count;
if (hasItem(packetRefId, packetCharge)) if (hasItem(packetRefId, packetCharge))
equipItem(packetRefId, packetCharge); equipItem(packetRefId, packetCharge);

@ -166,7 +166,7 @@ void DedicatedPlayer::setEquipment()
{ {
MWWorld::ContainerStoreIterator it = invStore.getSlot(slot); MWWorld::ContainerStoreIterator it = invStore.getSlot(slot);
const string &dedicItem = equipedItems[slot].refId; const string &dedicItem = equipmentItems[slot].refId;
std::string item = ""; std::string item = "";
bool equal = false; bool equal = false;
if (it != invStore.end()) if (it != invStore.end())
@ -184,7 +184,7 @@ void DedicatedPlayer::setEquipment()
if (dedicItem.empty() || equal) if (dedicItem.empty() || equal)
continue; continue;
const int count = equipedItems[slot].count; const int count = equipmentItems[slot].count;
ptr.getClass().getContainerStore(ptr).add(dedicItem, count, ptr); ptr.getClass().getContainerStore(ptr).add(dedicItem, count, ptr);
for (const auto &ptr : invStore) for (const auto &ptr : invStore)

@ -194,7 +194,7 @@ void LocalActor::updateEquipment(bool forceUpdate)
for (int slot = 0; slot < MWWorld::InventoryStore::Slots; slot++) for (int slot = 0; slot < MWWorld::InventoryStore::Slots; slot++)
{ {
MWWorld::ContainerStoreIterator it = invStore.getSlot(slot); MWWorld::ContainerStoreIterator it = invStore.getSlot(slot);
auto &item = equipedItems[slot]; auto &item = equipmentItems[slot];
if (it != invStore.end()) if (it != invStore.end())
{ {

@ -235,9 +235,9 @@ void LocalPlayer::updateAttributes(bool forceUpdate)
ptrNpcStats.getSkillIncrease(i) != npcStats.mSkillIncrease[i] || ptrNpcStats.getSkillIncrease(i) != npcStats.mSkillIncrease[i] ||
forceUpdate) forceUpdate)
{ {
attributeIndexChanges.push_back(i);
ptrNpcStats.getAttribute(i).writeState(creatureStats.mAttributes[i]); ptrNpcStats.getAttribute(i).writeState(creatureStats.mAttributes[i]);
npcStats.mSkillIncrease[i] = ptrNpcStats.getSkillIncrease(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 || ptrNpcStats.getSkill(i).getProgress() != npcStats.mSkills[i].mProgress ||
forceUpdate) forceUpdate)
{ {
ptrNpcStats.getSkill(i).writeState(npcStats.mSkills[i]);
skillIndexChanges.push_back(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(); MWWorld::Ptr ptrPlayer = getPlayerPtr();
static bool equipmentChanged = false;
if (forceUpdate)
equipmentChanged = true;
MWWorld::InventoryStore &invStore = ptrPlayer.getClass().getInventoryStore(ptrPlayer); MWWorld::InventoryStore &invStore = ptrPlayer.getClass().getInventoryStore(ptrPlayer);
for (int slot = 0; slot < MWWorld::InventoryStore::Slots; slot++) for (int slot = 0; slot < MWWorld::InventoryStore::Slots; slot++)
{ {
auto &item = equipedItems[slot]; auto &item = equipmentItems[slot];
MWWorld::ContainerStoreIterator it = invStore.getSlot(slot); MWWorld::ContainerStoreIterator it = invStore.getSlot(slot);
if (it != invStore.end()) 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.refId = it->getCellRef().getRefId();
item.charge = it->getCellRef().getCharge(); item.charge = it->getCellRef().getCharge();
if (slot == MWWorld::InventoryStore::Slot_CarriedRight) if (slot == MWWorld::InventoryStore::Slot_CarriedRight)
{ {
MWMechanics::WeaponType weaptype; MWMechanics::WeaponType weaptype;
@ -431,18 +426,18 @@ void LocalPlayer::updateEquipment(bool forceUpdate)
} }
else if (!item.refId.empty()) else if (!item.refId.empty())
{ {
equipmentChanged = true; equipmentIndexChanges.push_back(slot);
item.refId = ""; item.refId = "";
item.count = 0; item.count = 0;
item.charge = 0; item.charge = 0;
} }
} }
if (equipmentChanged) if (equipmentIndexChanges.size() > 0)
{ {
getNetworking()->getPlayerPacket(ID_PLAYER_EQUIPMENT)->setPlayer(this); getNetworking()->getPlayerPacket(ID_PLAYER_EQUIPMENT)->setPlayer(this);
getNetworking()->getPlayerPacket(ID_PLAYER_EQUIPMENT)->Send(); 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++) for (int slot = 0; slot < MWWorld::InventoryStore::Slots; slot++)
{ {
mwmp::Item &currentItem = equipedItems[slot]; mwmp::Item &currentItem = equipmentItems[slot];
if (!currentItem.refId.empty()) 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 if (it == ptrInventory.end()) // If the item is not in our inventory, add it
{ {
auto equipped = equipedItems[slot]; auto equipmentItem = equipmentItems[slot];
try 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); ptrInventory.equip(slot, addIter, ptrPlayer);
} }
catch (std::exception&) 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 else

@ -26,7 +26,12 @@ namespace mwmp
bool isChangingRegion; bool isChangingRegion;
Attack attack; 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<int> equipmentIndexChanges;
Item equipmentItems[19];
InventoryChanges inventoryChanges; InventoryChanges inventoryChanges;
unsigned int movementFlags; unsigned int movementFlags;

@ -11,10 +11,10 @@ PacketActorEquipment::PacketActorEquipment(RakNet::RakPeerInterface *peer) : Act
void PacketActorEquipment::Actor(BaseActor &actor, bool send) void PacketActorEquipment::Actor(BaseActor &actor, bool send)
{ {
for (auto &&equipedItem : actor.equipedItems) for (auto &&equipmentItem : actor.equipmentItems)
{ {
RW(equipedItem.refId, send); RW(equipmentItem.refId, send);
RW(equipedItem.count, send); RW(equipmentItem.count, send);
RW(equipedItem.charge, send); RW(equipmentItem.charge, send);
} }
} }

@ -1,7 +1,3 @@
//
// Created by koncord on 07.01.16.
//
#include "PacketPlayerEquipment.hpp" #include "PacketPlayerEquipment.hpp"
#include <components/openmw-mp/NetworkMessages.hpp> #include <components/openmw-mp/NetworkMessages.hpp>
@ -16,10 +12,24 @@ void PacketPlayerEquipment::Packet(RakNet::BitStream *bs, bool send)
{ {
PlayerPacket::Packet(bs, send); PlayerPacket::Packet(bs, send);
for (auto &equipedItem : player->equipedItems) uint32_t count;
if (send)
count = static_cast<uint32_t>(player->equipmentIndexChanges.size());
RW(count, send);
if (!send)
{ {
RW(equipedItem.refId, send, true); player->equipmentIndexChanges.clear();
RW(equipedItem.count, send); player->equipmentIndexChanges.resize(count);
RW(equipedItem.charge, send); }
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);
} }
} }

Loading…
Cancel
Save