Clean up client Networking by putting item & spell logic in LocalPlayer

coverity_scan^2
David Cernat 8 years ago
parent b8a6020af1
commit 78c6ab2a99

@ -53,7 +53,7 @@ void ItemFunctions::AddItem(unsigned short pid, const char* itemId, unsigned int
item.health = health; item.health = health;
player->inventorySendBuffer.items.push_back(item); player->inventorySendBuffer.items.push_back(item);
player->inventorySendBuffer.action = Inventory::ADDITEM; player->inventorySendBuffer.action = Inventory::ADD;
} }
void ItemFunctions::RemoveItem(unsigned short pid, const char* itemId, unsigned short count) noexcept void ItemFunctions::RemoveItem(unsigned short pid, const char* itemId, unsigned short count) noexcept
@ -66,7 +66,7 @@ void ItemFunctions::RemoveItem(unsigned short pid, const char* itemId, unsigned
item.count = count; item.count = count;
player->inventorySendBuffer.items.push_back(item); player->inventorySendBuffer.items.push_back(item);
player->inventorySendBuffer.action = Inventory::REMOVEITEM; player->inventorySendBuffer.action = Inventory::REMOVE;
} }
void ItemFunctions::ClearInventory(unsigned short pid) noexcept void ItemFunctions::ClearInventory(unsigned short pid) noexcept

@ -618,6 +618,56 @@ void LocalPlayer::updateDrawStateAndFlags(bool forceUpdate)
} }
} }
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++)
{
mwmp::Item item = inventory.items[i];
MWWorld::Ptr itemPtr = *ptrStore.add(item.refid, item.count, ptrPlayer);
if (item.health != -1)
itemPtr.getCellRef().setCharge(item.health);
}
}
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++)
ptrSpells.add(spell->mId);
}
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++)
{
mwmp::Item item = inventory.items[i];
ptrStore.remove(item.refid, item.count, ptrPlayer);
}
}
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++)
{
ptrSpells.remove(spell->mId);
MWBase::WindowManager *wm = MWBase::Environment::get().getWindowManager();
if (spell->mId == wm->getSelectedSpell())
wm->unsetSelectedSpell();
}
}
void LocalPlayer::setDynamicStats() void LocalPlayer::setDynamicStats()
{ {
MWBase::World *world = MWBase::Environment::get().getWorld(); MWBase::World *world = MWBase::Environment::get().getWorld();
@ -802,6 +852,36 @@ void LocalPlayer::setEquipment()
} }
} }
void LocalPlayer::setInventory()
{
MWWorld::Ptr ptrPlayer = MWBase::Environment::get().getWorld()->getPlayerPtr();
MWWorld::ContainerStore &ptrStore = ptrPlayer.getClass().getContainerStore(ptrPlayer);
// Clear items in inventory
ptrStore.clear();
// Proceed by adding items
addItems();
// Don't automatically setEquipment() here, or the player could end
// up getting a new set of their starting clothes, or other items
// supposed to no longer exist
//
// Instead, expect server scripts to do that manually
}
void LocalPlayer::setSpellbook()
{
MWWorld::Ptr ptrPlayer = MWBase::Environment::get().getWorld()->getPlayerPtr();
MWMechanics::Spells &ptrSpells = ptrPlayer.getClass().getCreatureStats(ptrPlayer).getSpells();
// Clear spells in spellbook
ptrSpells.clear();
// Proceed by adding spells
addSpells();
}
void LocalPlayer::sendClass() void LocalPlayer::sendClass()
{ {
MWBase::World *world = MWBase::Environment::get().getWorld(); MWBase::World *world = MWBase::Environment::get().getWorld();

@ -36,6 +36,12 @@ namespace mwmp
void updateDeadState(bool forceUpdate = false); void updateDeadState(bool forceUpdate = false);
void updateDrawStateAndFlags(bool forceUpdate = false); void updateDrawStateAndFlags(bool forceUpdate = false);
void addItems();
void addSpells();
void removeItems();
void removeSpells();
void setDynamicStats(); void setDynamicStats();
void setAttributes(); void setAttributes();
void setSkills(); void setSkills();
@ -44,6 +50,8 @@ namespace mwmp
void setCell(); void setCell();
void setClass(); void setClass();
void setEquipment(); void setEquipment();
void setInventory();
void setSpellbook();
void sendClass(); void sendClass();
void sendInventory(); void sendInventory();

@ -299,45 +299,19 @@ void Networking::processPlayerPacket(RakNet::Packet *packet)
else else
{ {
myPacket->Packet(&bsIn, getLocalPlayer(), false); myPacket->Packet(&bsIn, getLocalPlayer(), false);
MWWorld::Ptr ptrPlayer = MWBase::Environment::get().getWorld()->getPlayerPtr(); int inventoryAction = getLocalPlayer()->inventory.action;
MWWorld::ContainerStore &conStore = ptrPlayer.getClass().getContainerStore(ptrPlayer);
if (getLocalPlayer()->inventory.action == Inventory::ADDITEM) if (inventoryAction == Inventory::ADD)
{ {
for (unsigned int i = 0; i < getLocalPlayer()->inventory.count; i++) getLocalPlayer()->addItems();
{
mwmp::Item item = getLocalPlayer()->inventory.items[i];
MWWorld::Ptr itemPtr = *conStore.add(item.refid, item.count, ptrPlayer);
if (item.health != -1)
itemPtr.getCellRef().setCharge(item.health);
}
} }
else if (getLocalPlayer()->inventory.action == Inventory::REMOVEITEM) else if (inventoryAction == Inventory::REMOVE)
{ {
for (unsigned int i = 0; i < getLocalPlayer()->inventory.count; i++) getLocalPlayer()->removeItems();
{
mwmp::Item item = getLocalPlayer()->inventory.items[i];
conStore.remove(item.refid, item.count, ptrPlayer);
}
} }
else // update else // Inventory::UPDATE
{ {
// Clear items in inventory getLocalPlayer()->setInventory();
conStore.clear();
for (unsigned int i = 0; i < getLocalPlayer()->inventory.count; i++)
{
mwmp::Item item = getLocalPlayer()->inventory.items[i];
MWWorld::Ptr itemPtr = *conStore.add(item.refid, item.count, ptrPlayer);
if (item.health != -1)
itemPtr.getCellRef().setCharge(item.health);
}
// Don't automatically setEquipment() here, or the player could end
// up getting a new set of their starting clothes, or other items
// supposed to no longer exist
//
// Instead, expect server scripts to do that manually
} }
} }
} }
@ -354,26 +328,19 @@ void Networking::processPlayerPacket(RakNet::Packet *packet)
else else
{ {
myPacket->Packet(&bsIn, getLocalPlayer(), false); myPacket->Packet(&bsIn, getLocalPlayer(), false);
const Spellbook& spellbook = getLocalPlayer()->spellbook; int spellbookAction = getLocalPlayer()->spellbook.action;
MWWorld::Ptr playerptr = MWBase::Environment::get().getWorld()->getPlayerPtr();
MWMechanics::Spells &spells = playerptr.getClass().getCreatureStats (playerptr).getSpells();
if (spellbook.action == Spellbook::ADD)
for (vector<ESM::Spell>::const_iterator spell = spellbook.spells.begin(); spell != spellbook.spells.end(); spell++)
spells.add (spell->mId);
else if (spellbook.action == Spellbook::REMOVE)
for (vector<ESM::Spell>::const_iterator spell = spellbook.spells.begin(); spell != spellbook.spells.end(); spell++)
{
spells.remove (spell->mId);
MWBase::WindowManager *wm = MWBase::Environment::get().getWindowManager(); if (spellbookAction == Spellbook::ADD)
if (spell->mId == wm->getSelectedSpell()) {
wm->unsetSelectedSpell(); getLocalPlayer()->addSpells();
} }
else if (spellbookAction == Spellbook::REMOVE)
{
getLocalPlayer()->removeSpells();
}
else // Spellbook::UPDATE else // Spellbook::UPDATE
{ {
spells.clear(); getLocalPlayer()->setSpellbook();
for (vector<ESM::Spell>::const_iterator spell = spellbook.spells.begin(); spell != spellbook.spells.end(); spell++)
spells.add (spell->mId);
} }
} }
} }

@ -53,10 +53,10 @@ namespace mwmp
enum ACTION_TYPE enum ACTION_TYPE
{ {
UPDATE = 0, UPDATE = 0,
ADDITEM, ADD,
REMOVEITEM REMOVE
}; };
int action; //0 - FullUpdate, 1 - AddItem, 2 - RemoveItem int action; //0 - Full update, 1 - Add item, 2 - Remove item
}; };
struct Spellbook struct Spellbook
@ -69,7 +69,7 @@ namespace mwmp
ADD, ADD,
REMOVE REMOVE
}; };
int action; //0 - Update, 1 - Add, 2 - Remove int action; //0 - Full update, 1 - Add spell, 2 - Remove spell
}; };
class BasePlayer class BasePlayer

Loading…
Cancel
Save