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

pull/133/head
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;
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
@ -66,7 +66,7 @@ void ItemFunctions::RemoveItem(unsigned short pid, const char* itemId, unsigned
item.count = count;
player->inventorySendBuffer.items.push_back(item);
player->inventorySendBuffer.action = Inventory::REMOVEITEM;
player->inventorySendBuffer.action = Inventory::REMOVE;
}
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()
{
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()
{
MWBase::World *world = MWBase::Environment::get().getWorld();

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

@ -299,45 +299,19 @@ void Networking::processPlayerPacket(RakNet::Packet *packet)
else
{
myPacket->Packet(&bsIn, getLocalPlayer(), false);
MWWorld::Ptr ptrPlayer = MWBase::Environment::get().getWorld()->getPlayerPtr();
MWWorld::ContainerStore &conStore = ptrPlayer.getClass().getContainerStore(ptrPlayer);
int inventoryAction = getLocalPlayer()->inventory.action;
if (getLocalPlayer()->inventory.action == Inventory::ADDITEM)
if (inventoryAction == Inventory::ADD)
{
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);
}
getLocalPlayer()->addItems();
}
else if (getLocalPlayer()->inventory.action == Inventory::REMOVEITEM)
else if (inventoryAction == Inventory::REMOVE)
{
for (unsigned int i = 0; i < getLocalPlayer()->inventory.count; i++)
{
mwmp::Item item = getLocalPlayer()->inventory.items[i];
conStore.remove(item.refid, item.count, ptrPlayer);
}
getLocalPlayer()->removeItems();
}
else // update
else // Inventory::UPDATE
{
// Clear items in inventory
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
getLocalPlayer()->setInventory();
}
}
}
@ -354,26 +328,19 @@ void Networking::processPlayerPacket(RakNet::Packet *packet)
else
{
myPacket->Packet(&bsIn, getLocalPlayer(), false);
const Spellbook& spellbook = getLocalPlayer()->spellbook;
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);
int spellbookAction = getLocalPlayer()->spellbook.action;
MWBase::WindowManager *wm = MWBase::Environment::get().getWindowManager();
if (spell->mId == wm->getSelectedSpell())
wm->unsetSelectedSpell();
}
if (spellbookAction == Spellbook::ADD)
{
getLocalPlayer()->addSpells();
}
else if (spellbookAction == Spellbook::REMOVE)
{
getLocalPlayer()->removeSpells();
}
else // Spellbook::UPDATE
{
spells.clear();
for (vector<ESM::Spell>::const_iterator spell = spellbook.spells.begin(); spell != spellbook.spells.end(); spell++)
spells.add (spell->mId);
getLocalPlayer()->setSpellbook();
}
}
}

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

Loading…
Cancel
Save