From 78c6ab2a993c6920d47bb11f0252c036ea478435 Mon Sep 17 00:00:00 2001 From: David Cernat Date: Thu, 19 Jan 2017 15:18:37 +0200 Subject: [PATCH] Clean up client Networking by putting item & spell logic in LocalPlayer --- apps/openmw-mp/Script/Functions/Items.cpp | 4 +- apps/openmw/mwmp/LocalPlayer.cpp | 80 +++++++++++++++++++++++ apps/openmw/mwmp/LocalPlayer.hpp | 8 +++ apps/openmw/mwmp/Networking.cpp | 67 +++++-------------- components/openmw-mp/Base/BasePlayer.hpp | 8 +-- 5 files changed, 111 insertions(+), 56 deletions(-) diff --git a/apps/openmw-mp/Script/Functions/Items.cpp b/apps/openmw-mp/Script/Functions/Items.cpp index 892de79cc..ec251e77b 100644 --- a/apps/openmw-mp/Script/Functions/Items.cpp +++ b/apps/openmw-mp/Script/Functions/Items.cpp @@ -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 diff --git a/apps/openmw/mwmp/LocalPlayer.cpp b/apps/openmw/mwmp/LocalPlayer.cpp index 2a7995eb9..16bd4e3e6 100644 --- a/apps/openmw/mwmp/LocalPlayer.cpp +++ b/apps/openmw/mwmp/LocalPlayer.cpp @@ -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::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::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(); diff --git a/apps/openmw/mwmp/LocalPlayer.hpp b/apps/openmw/mwmp/LocalPlayer.hpp index 2585a72fe..7668f679a 100644 --- a/apps/openmw/mwmp/LocalPlayer.hpp +++ b/apps/openmw/mwmp/LocalPlayer.hpp @@ -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(); diff --git a/apps/openmw/mwmp/Networking.cpp b/apps/openmw/mwmp/Networking.cpp index c68fe89b8..dd2e69518 100644 --- a/apps/openmw/mwmp/Networking.cpp +++ b/apps/openmw/mwmp/Networking.cpp @@ -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::const_iterator spell = spellbook.spells.begin(); spell != spellbook.spells.end(); spell++) - spells.add (spell->mId); - else if (spellbook.action == Spellbook::REMOVE) - for (vector::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::const_iterator spell = spellbook.spells.begin(); spell != spellbook.spells.end(); spell++) - spells.add (spell->mId); + getLocalPlayer()->setSpellbook(); } } } diff --git a/components/openmw-mp/Base/BasePlayer.hpp b/components/openmw-mp/Base/BasePlayer.hpp index 426fa5050..fdaefbd46 100644 --- a/components/openmw-mp/Base/BasePlayer.hpp +++ b/components/openmw-mp/Base/BasePlayer.hpp @@ -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