Rename BasePlayer Inventory & Spellbook into PacketItems & PacketSpells

This avoids confusion when either of those is used to store and send a single item, and no longer requires coming up with confusing variable names like realSpellbook or realInventory for actual full spellbooks and inventories.
coverity_scan^2
David Cernat 8 years ago
parent be851f5e1a
commit 33e85c54de

@ -238,21 +238,21 @@ void Networking::processPlayerPacket(RakNet::Packet *packet)
myPacket->Read(player);
string str;
for (auto spell : player->spellbook.spells)
for (auto spell : player->packetSpells.spells)
{
str += spell.mId;
if (spell.mId != player->spellbook.spells.back().mId)
if (spell.mId != player->packetSpells.spells.back().mId)
str += ";";
if (player->spellbook.action == Spellbook::ADD)
player->realSpellbook.push_back(spell);
else if (player->spellbook.action == Spellbook::REMOVE)
if (player->packetSpells.action == PacketSpells::ADD)
player->spellbook.push_back(spell);
else if (player->packetSpells.action == PacketSpells::REMOVE)
{
player->realSpellbook.erase(remove_if(player->realSpellbook.begin(), player->realSpellbook.end(), [&spell](ESM::Spell s)->bool
{return spell.mId == s.mId; }), player->realSpellbook.end());
player->spellbook.erase(remove_if(player->spellbook.begin(), player->spellbook.end(), [&spell](ESM::Spell s)->bool
{return spell.mId == s.mId; }), player->spellbook.end());
}
}
Script::Call<Script::CallbackIdentity("OnPlayerChangeSpellbook")>(player->getId(), player->spellbook.action, str.c_str());
Script::Call<Script::CallbackIdentity("OnPlayerChangeSpellbook")>(player->getId(), player->packetSpells.action, str.c_str());
break;
}

@ -68,9 +68,9 @@ public:
virtual ~Player();
public:
mwmp::Inventory inventorySendBuffer;
mwmp::Spellbook spellbookSendBuffer;
std::vector<ESM::Spell> realSpellbook;
mwmp::PacketItems packetItemsBuffer;
mwmp::PacketSpells packetSpellsBuffer;
std::vector<ESM::Spell> spellbook;
private:
bool handshakeState;

@ -21,7 +21,7 @@ unsigned int ItemFunctions::GetInventorySize(unsigned short pid) noexcept
Player *player;
GET_PLAYER(pid, player, 0);
return player->inventory.count;
return player->packetItems.count;
}
void ItemFunctions::EquipItem(unsigned short pid, unsigned short slot, const char *itemId, unsigned int count, int health) noexcept
@ -52,8 +52,8 @@ void ItemFunctions::AddItem(unsigned short pid, const char* itemId, unsigned int
item.count = count;
item.health = health;
player->inventorySendBuffer.items.push_back(item);
player->inventorySendBuffer.action = Inventory::ADD;
player->packetItemsBuffer.items.push_back(item);
player->packetItemsBuffer.action = PacketItems::ADD;
}
void ItemFunctions::RemoveItem(unsigned short pid, const char* itemId, unsigned short count) noexcept
@ -65,8 +65,8 @@ void ItemFunctions::RemoveItem(unsigned short pid, const char* itemId, unsigned
item.refid = itemId;
item.count = count;
player->inventorySendBuffer.items.push_back(item);
player->inventorySendBuffer.action = Inventory::REMOVE;
player->packetItemsBuffer.items.push_back(item);
player->packetItemsBuffer.action = PacketItems::REMOVE;
}
void ItemFunctions::ClearInventory(unsigned short pid) noexcept
@ -74,8 +74,8 @@ void ItemFunctions::ClearInventory(unsigned short pid) noexcept
Player *player;
GET_PLAYER(pid, player, );
player->inventorySendBuffer.items.clear();
player->inventorySendBuffer.action = Inventory::UPDATE;
player->packetItemsBuffer.items.clear();
player->packetItemsBuffer.action = PacketItems::UPDATE;
}
bool ItemFunctions::HasItemEquipped(unsigned short pid, const char* itemId)
@ -118,10 +118,10 @@ const char *ItemFunctions::GetInventoryItemId(unsigned short pid, unsigned int i
Player *player;
GET_PLAYER(pid, player, "");
if (i >= player->inventory.count)
if (i >= player->packetItems.count)
return "invalid";
return player->inventory.items.at(i).refid.c_str();
return player->packetItems.items.at(i).refid.c_str();
}
int ItemFunctions::GetInventoryItemCount(unsigned short pid, unsigned int i) noexcept
@ -129,7 +129,7 @@ int ItemFunctions::GetInventoryItemCount(unsigned short pid, unsigned int i) noe
Player *player;
GET_PLAYER(pid, player, 0);
return player->inventory.items.at(i).count;
return player->packetItems.items.at(i).count;
}
int ItemFunctions::GetInventoryItemHealth(unsigned short pid, unsigned int i) noexcept
@ -137,7 +137,7 @@ int ItemFunctions::GetInventoryItemHealth(unsigned short pid, unsigned int i) no
Player *player;
GET_PLAYER(pid, player, 0);
return player->inventory.items.at(i).health;
return player->packetItems.items.at(i).health;
}
void ItemFunctions::SendEquipment(unsigned short pid) noexcept
@ -149,12 +149,12 @@ void ItemFunctions::SendEquipment(unsigned short pid) noexcept
mwmp::Networking::get().getPlayerController()->GetPacket(ID_GAME_EQUIPMENT)->Send(player, true);
}
void ItemFunctions::SendInventory(unsigned short pid) noexcept
void ItemFunctions::SendItems(unsigned short pid) noexcept
{
Player *player;
GET_PLAYER(pid, player, );
std::swap(player->inventory, player->inventorySendBuffer);
std::swap(player->packetItems, player->packetItemsBuffer);
mwmp::Networking::get().getPlayerController()->GetPacket(ID_GAME_INVENTORY)->Send(player, false);
player->inventory = std::move(player->inventorySendBuffer);
player->inventorySendBuffer.items.clear();
player->packetItems = std::move(player->packetItemsBuffer);
player->packetItemsBuffer.items.clear();
}

@ -27,7 +27,7 @@
{"GetInventoryItemHealth", ItemFunctions::GetInventoryItemHealth},\
\
{"SendEquipment", ItemFunctions::SendEquipment},\
{"SendInventory", ItemFunctions::SendInventory}
{"SendInventory", ItemFunctions::SendItems}
class ItemFunctions
{
@ -54,7 +54,7 @@ public:
static int GetInventoryItemHealth(unsigned short pid, unsigned int i) noexcept;
static void SendEquipment(unsigned short pid) noexcept;
static void SendInventory(unsigned short pid) noexcept;
static void SendItems(unsigned short pid) noexcept;
private:
};

@ -11,7 +11,7 @@ unsigned int SpellFunctions::GetSpellbookSize(unsigned short pid) noexcept
Player *player;
GET_PLAYER(pid, player, 0);
return player->realSpellbook.size();
return player->spellbook.size();
}
void SpellFunctions::AddSpell(unsigned short pid, const char* spellId) noexcept
@ -22,8 +22,8 @@ void SpellFunctions::AddSpell(unsigned short pid, const char* spellId) noexcept
ESM::Spell spell;
spell.mId = spellId;
player->spellbookSendBuffer.spells.push_back(spell);
player->spellbookSendBuffer.action = Spellbook::ADD;
player->packetSpellsBuffer.spells.push_back(spell);
player->packetSpellsBuffer.action = PacketSpells::ADD;
}
void SpellFunctions::RemoveSpell(unsigned short pid, const char* spellId) noexcept
@ -34,8 +34,8 @@ void SpellFunctions::RemoveSpell(unsigned short pid, const char* spellId) noexce
ESM::Spell spell;
spell.mId = spellId;
player->spellbookSendBuffer.spells.push_back(spell);
player->spellbookSendBuffer.action = Spellbook::REMOVE;
player->packetSpellsBuffer.spells.push_back(spell);
player->packetSpellsBuffer.action = PacketSpells::REMOVE;
}
void SpellFunctions::ClearSpellbook(unsigned short pid) noexcept
@ -43,8 +43,8 @@ void SpellFunctions::ClearSpellbook(unsigned short pid) noexcept
Player *player;
GET_PLAYER(pid, player, );
player->spellbookSendBuffer.spells.clear();
player->spellbookSendBuffer.action = Spellbook::UPDATE;
player->packetSpellsBuffer.spells.clear();
player->packetSpellsBuffer.action = PacketSpells::UPDATE;
}
bool SpellFunctions::HasSpell(unsigned short pid, const char* spellId)
@ -52,8 +52,8 @@ bool SpellFunctions::HasSpell(unsigned short pid, const char* spellId)
Player *player;
GET_PLAYER(pid, player, false);
for (unsigned int i = 0; i < player->realSpellbook.size(); i++)
if (Misc::StringUtils::ciEqual(player->realSpellbook.at(i).mId, spellId))
for (unsigned int i = 0; i < player->spellbook.size(); i++)
if (Misc::StringUtils::ciEqual(player->spellbook.at(i).mId, spellId))
return true;
return false;
}
@ -63,28 +63,28 @@ const char *SpellFunctions::GetSpellId(unsigned short pid, unsigned int i) noexc
Player *player;
GET_PLAYER(pid, player, "");
if (i >= player->realSpellbook.size())
if (i >= player->spellbook.size())
return "invalid";
return player->realSpellbook.at(i).mId.c_str();
return player->spellbook.at(i).mId.c_str();
}
void SpellFunctions::SendSpellbook(unsigned short pid) noexcept
void SpellFunctions::SendSpells(unsigned short pid) noexcept
{
Player *player;
GET_PLAYER(pid, player, );
for (auto spell : player->spellbookSendBuffer.spells)
for (auto spell : player->packetSpellsBuffer.spells)
{
if (player->spellbookSendBuffer.action == Spellbook::ADD)
player->realSpellbook.push_back(spell);
else if (player->spellbook.action == Spellbook::REMOVE)
player->realSpellbook.erase(remove_if(player->realSpellbook.begin(), player->realSpellbook.end(), [&spell](ESM::Spell s)->bool
{return spell.mId == s.mId; }), player->realSpellbook.end());
if (player->packetSpellsBuffer.action == PacketSpells::ADD)
player->spellbook.push_back(spell);
else if (player->packetSpells.action == PacketSpells::REMOVE)
player->spellbook.erase(remove_if(player->spellbook.begin(), player->spellbook.end(), [&spell](ESM::Spell s)->bool
{return spell.mId == s.mId; }), player->spellbook.end());
}
std::swap(player->spellbook, player->spellbookSendBuffer);
std::swap(player->packetSpells, player->packetSpellsBuffer);
mwmp::Networking::get().getPlayerController()->GetPacket(ID_GAME_SPELLBOOK)->Send(player, false);
player->spellbook = std::move(player->spellbookSendBuffer);
player->spellbookSendBuffer.spells.clear();
player->packetSpells = std::move(player->packetSpellsBuffer);
player->packetSpellsBuffer.spells.clear();
}

@ -11,7 +11,7 @@
{"HasSpell", SpellFunctions::HasSpell},\
{"GetSpellId", SpellFunctions::GetSpellId},\
\
{"SendSpellbook", SpellFunctions::SendSpellbook}
{"SendSpellbook", SpellFunctions::SendSpells}
class SpellFunctions
{
@ -26,7 +26,7 @@ public:
static bool HasSpell(unsigned short pid, const char* itemName);
static const char *GetSpellId(unsigned short pid, unsigned int i) noexcept;
static void SendSpellbook(unsigned short pid) noexcept;
static void SendSpells(unsigned short pid) noexcept;
private:
};

@ -432,7 +432,7 @@ void LocalPlayer::updateInventory(bool forceUpdate)
if (!invChanged)
{
for (vector<Item>::iterator iter = inventory.items.begin(); iter != inventory.items.end(); ++iter)
for (vector<Item>::iterator iter = packetItems.items.begin(); iter != packetItems.items.end(); ++iter)
{
MWWorld::ContainerStoreIterator result(ptrInventory.begin());
for (; result != ptrInventory.end(); ++result)
@ -466,15 +466,15 @@ void LocalPlayer::updateInventory(bool forceUpdate)
item.count = iter->getRefData().getCount();
item.health = iter->getCellRef().getCharge();
vector<Item>::iterator result = inventory.items.begin();
vector<Item>::iterator result = packetItems.items.begin();
for (; result != inventory.items.end(); result++)
for (; result != packetItems.items.end(); result++)
{
if ((*result) == item)
break;
}
if (result == inventory.items.end())
if (result == packetItems.items.end())
{
invChanged = true;
break;
@ -623,9 +623,9 @@ void LocalPlayer::addItems()
MWWorld::Ptr ptrPlayer = MWBase::Environment::get().getWorld()->getPlayerPtr();
MWWorld::ContainerStore &ptrStore = ptrPlayer.getClass().getContainerStore(ptrPlayer);
for (unsigned int i = 0; i < inventory.count; i++)
for (unsigned int i = 0; i < packetItems.count; i++)
{
mwmp::Item item = inventory.items[i];
mwmp::Item item = packetItems.items[i];
MWWorld::Ptr itemPtr = *ptrStore.add(item.refid, item.count, ptrPlayer);
if (item.health != -1)
itemPtr.getCellRef().setCharge(item.health);
@ -637,7 +637,7 @@ void LocalPlayer::addSpells()
MWWorld::Ptr ptrPlayer = MWBase::Environment::get().getWorld()->getPlayerPtr();
MWMechanics::Spells &ptrSpells = ptrPlayer.getClass().getCreatureStats(ptrPlayer).getSpells();
for (vector<ESM::Spell>::const_iterator spell = spellbook.spells.begin(); spell != spellbook.spells.end(); spell++)
for (vector<ESM::Spell>::const_iterator spell = packetSpells.spells.begin(); spell != packetSpells.spells.end(); spell++)
ptrSpells.add(spell->mId);
}
@ -646,9 +646,9 @@ void LocalPlayer::removeItems()
MWWorld::Ptr ptrPlayer = MWBase::Environment::get().getWorld()->getPlayerPtr();
MWWorld::ContainerStore &ptrStore = ptrPlayer.getClass().getContainerStore(ptrPlayer);
for (unsigned int i = 0; i < inventory.count; i++)
for (unsigned int i = 0; i < packetItems.count; i++)
{
mwmp::Item item = inventory.items[i];
mwmp::Item item = packetItems.items[i];
ptrStore.remove(item.refid, item.count, ptrPlayer);
}
}
@ -658,7 +658,7 @@ void LocalPlayer::removeSpells()
MWWorld::Ptr ptrPlayer = MWBase::Environment::get().getWorld()->getPlayerPtr();
MWMechanics::Spells &ptrSpells = ptrPlayer.getClass().getCreatureStats(ptrPlayer).getSpells();
for (vector<ESM::Spell>::const_iterator spell = spellbook.spells.begin(); spell != spellbook.spells.end(); spell++)
for (vector<ESM::Spell>::const_iterator spell = packetSpells.spells.begin(); spell != packetSpells.spells.end(); spell++)
{
ptrSpells.remove(spell->mId);
@ -907,7 +907,7 @@ void LocalPlayer::sendInventory()
MWWorld::InventoryStore &ptrInventory = ptrPlayer.getClass().getInventoryStore(ptrPlayer);
mwmp::Item item;
inventory.items.clear();
packetItems.items.clear();
for (MWWorld::ContainerStoreIterator iter(ptrInventory.begin()); iter != ptrInventory.end(); ++iter)
{
@ -918,11 +918,11 @@ void LocalPlayer::sendInventory()
item.count = iter->getRefData().getCount();
item.health = iter->getCellRef().getCharge();
inventory.items.push_back(item);
packetItems.items.push_back(item);
}
inventory.count = (unsigned int)inventory.items.size();
inventory.action = Inventory::UPDATE;
packetItems.count = (unsigned int) packetItems.items.size();
packetItems.action = PacketItems::UPDATE;
Main::get().getNetworking()->getPlayerPacket(ID_GAME_INVENTORY)->Send(this);
}
@ -931,13 +931,13 @@ void LocalPlayer::sendSpellAddition(std::string id)
if (id.find("$dynamic") != string::npos) // skip custom spells
return;
spellbook.spells.clear();
packetSpells.spells.clear();
ESM::Spell spell;
spell.mId = id;
spellbook.spells.push_back(spell);
packetSpells.spells.push_back(spell);
spellbook.action = Spellbook::ADD;
packetSpells.action = PacketSpells::ADD;
Main::get().getNetworking()->getPlayerPacket(ID_GAME_SPELLBOOK)->Send(this);
}
@ -946,13 +946,13 @@ void LocalPlayer::sendSpellRemoval(std::string id)
if (id.find("$dynamic") != string::npos) // skip custom spells
return;
spellbook.spells.clear();
packetSpells.spells.clear();
ESM::Spell spell;
spell.mId = id;
spellbook.spells.push_back(spell);
packetSpells.spells.push_back(spell);
spellbook.action = Spellbook::REMOVE;
packetSpells.action = PacketSpells::REMOVE;
Main::get().getNetworking()->getPlayerPacket(ID_GAME_SPELLBOOK)->Send(this);
}

@ -299,17 +299,17 @@ void Networking::processPlayerPacket(RakNet::Packet *packet)
else
{
myPacket->Packet(&bsIn, getLocalPlayer(), false);
int inventoryAction = getLocalPlayer()->inventory.action;
int inventoryAction = getLocalPlayer()->packetItems.action;
if (inventoryAction == Inventory::ADD)
if (inventoryAction == PacketItems::ADD)
{
getLocalPlayer()->addItems();
}
else if (inventoryAction == Inventory::REMOVE)
else if (inventoryAction == PacketItems::REMOVE)
{
getLocalPlayer()->removeItems();
}
else // Inventory::UPDATE
else // PacketItems::UPDATE
{
getLocalPlayer()->setInventory();
}
@ -328,17 +328,17 @@ void Networking::processPlayerPacket(RakNet::Packet *packet)
else
{
myPacket->Packet(&bsIn, getLocalPlayer(), false);
int spellbookAction = getLocalPlayer()->spellbook.action;
int spellbookAction = getLocalPlayer()->packetSpells.action;
if (spellbookAction == Spellbook::ADD)
if (spellbookAction == PacketSpells::ADD)
{
getLocalPlayer()->addSpells();
}
else if (spellbookAction == Spellbook::REMOVE)
else if (spellbookAction == PacketSpells::REMOVE)
{
getLocalPlayer()->removeSpells();
}
else // Spellbook::UPDATE
else // PacketSpells::UPDATE
{
getLocalPlayer()->setSpellbook();
}

@ -46,7 +46,7 @@ namespace mwmp
}
};
struct Inventory
struct PacketItems
{
std::vector<Item> items;
unsigned int count;
@ -59,7 +59,7 @@ namespace mwmp
int action; //0 - Full update, 1 - Add item, 2 - Remove item
};
struct Spellbook
struct PacketSpells
{
std::vector<ESM::Spell> spells;
unsigned int count;
@ -101,9 +101,9 @@ namespace mwmp
BasePlayer(RakNet::RakNetGUID guid) : guid(guid)
{
inventory.action = 0;
inventory.count = 0;
spellbook.action = 0;
packetItems.action = 0;
packetItems.count = 0;
packetItems.action = 0;
}
BasePlayer()
@ -188,8 +188,8 @@ namespace mwmp
int month;
int day;
double hour;
Inventory inventory;
Spellbook spellbook;
PacketItems packetItems;
PacketSpells packetSpells;
bool consoleAllowed;
bool ignorePosPacket;
ESM::ActiveSpells activeSpells;

@ -17,22 +17,22 @@ void PacketInventory::Packet(RakNet::BitStream *bs, BasePlayer *player, bool sen
{
PlayerPacket::Packet(bs, player, send);
RW(player->inventory.action, send);
RW(player->packetItems.action, send);
if (!send)
player->inventory.items.clear();
player->packetItems.items.clear();
else
player->inventory.count = (unsigned int) (player->inventory.items.size());
player->packetItems.count = (unsigned int) (player->packetItems.items.size());
RW(player->inventory.count, send);
RW(player->packetItems.count, send);
for (unsigned int i = 0; i < player->inventory.count; i++)
for (unsigned int i = 0; i < player->packetItems.count; i++)
{
Item item;
if (send)
{
item = player->inventory.items[i];
item = player->packetItems.items[i];
RW(item.refid, send);
RW(item.count, send);
RW(item.health, send);
@ -42,7 +42,7 @@ void PacketInventory::Packet(RakNet::BitStream *bs, BasePlayer *player, bool sen
RW(item.refid, send);
RW(item.count, send);
RW(item.health, send);
player->inventory.items.push_back(item);
player->packetItems.items.push_back(item);
}
}
}

@ -13,28 +13,28 @@ void PacketSpellbook::Packet(RakNet::BitStream *bs, BasePlayer *player, bool sen
{
PlayerPacket::Packet(bs, player, send);
RW(player->spellbook.action, send);
RW(player->packetSpells.action, send);
if (!send)
player->spellbook.spells.clear();
player->packetSpells.spells.clear();
else
player->spellbook.count = (unsigned int) (player->spellbook.spells.size());
player->packetSpells.count = (unsigned int) (player->packetSpells.spells.size());
RW(player->spellbook.count, send);
RW(player->packetSpells.count, send);
for (unsigned int i = 0; i < player->spellbook.count; i++)
for (unsigned int i = 0; i < player->packetSpells.count; i++)
{
ESM::Spell spell;
if (send)
{
spell = player->spellbook.spells[i];
spell = player->packetSpells.spells[i];
RW(spell.mId, send);
}
else
{
RW(spell.mId, send);
player->spellbook.spells.push_back(spell);
player->packetSpells.spells.push_back(spell);
}
}

Loading…
Cancel
Save