Add a utility function to add items to a ContainerStore by RefID

This commit is contained in:
scrawl 2013-11-21 04:11:06 +01:00
parent 6641fd4635
commit bab657fe2b
8 changed files with 23 additions and 26 deletions

View file

@ -210,8 +210,7 @@ namespace MWGui
if (amount > 0) if (amount > 0)
{ {
MWWorld::ManualRef ref(MWBase::Environment::get().getWorld()->getStore(), "Gold_001", amount); playerStore.add("gold_001", amount, player);
playerStore.add(ref.getPtr(), player);
} }
else else
{ {

View file

@ -32,17 +32,17 @@
namespace namespace
{ {
void adjustBoundItem (const std::string& item, bool bound, const MWWorld::Ptr& ptr) void adjustBoundItem (const std::string& item, bool bound, const MWWorld::Ptr& actor)
{ {
if (bound) if (bound)
{ {
MWWorld::ManualRef ref (MWBase::Environment::get().getWorld()->getStore(), item, 1); MWWorld::Ptr newPtr = *actor.getClass().getContainerStore(actor).add(item, 1, actor);
MWWorld::ActionEquip action(*ptr.getClass().getContainerStore(ptr).add(ref.getPtr(), ptr)); MWWorld::ActionEquip action(newPtr);
action.execute(ptr); action.execute(actor);
} }
else else
{ {
ptr.getClass().getContainerStore(ptr).remove(item, 1, ptr); actor.getClass().getContainerStore(actor).remove(item, 1, actor);
} }
} }

View file

@ -287,8 +287,7 @@ void MWMechanics::Alchemy::addPotion (const std::string& name)
record = MWBase::Environment::get().getWorld()->createRecord (newRecord); record = MWBase::Environment::get().getWorld()->createRecord (newRecord);
} }
MWWorld::ManualRef ref (MWBase::Environment::get().getWorld()->getStore(), record->mId); mAlchemist.getClass().getContainerStore (mAlchemist).add (record->mId, 1, mAlchemist);
MWWorld::Class::get (mAlchemist).getContainerStore (mAlchemist).add (ref.getPtr(), mAlchemist);
} }
void MWMechanics::Alchemy::increaseSkill() void MWMechanics::Alchemy::increaseSkill()

View file

@ -62,10 +62,7 @@ namespace MWMechanics
//Exception for Azura Star, new one will be added after enchanting //Exception for Azura Star, new one will be added after enchanting
if(boost::iequals(mSoulGemPtr.get<ESM::Miscellaneous>()->mBase->mId, "Misc_SoulGem_Azura")) if(boost::iequals(mSoulGemPtr.get<ESM::Miscellaneous>()->mBase->mId, "Misc_SoulGem_Azura"))
{ store.add("Misc_SoulGem_Azura", 1, player);
MWWorld::ManualRef azura (MWBase::Environment::get().getWorld()->getStore(), "Misc_SoulGem_Azura");
store.add(azura.getPtr(), player);
}
if(mSelfEnchanting) if(mSelfEnchanting)
{ {

View file

@ -17,7 +17,6 @@
#include "../mwbase/environment.hpp" #include "../mwbase/environment.hpp"
#include "../mwbase/windowmanager.hpp" #include "../mwbase/windowmanager.hpp"
#include "../mwworld/manualref.hpp"
#include "../mwworld/class.hpp" #include "../mwworld/class.hpp"
#include "../mwworld/containerstore.hpp" #include "../mwworld/containerstore.hpp"
#include "../mwworld/actionequip.hpp" #include "../mwworld/actionequip.hpp"
@ -53,24 +52,23 @@ namespace MWScript
if (count == 0) if (count == 0)
return; return;
MWWorld::ManualRef ref (MWBase::Environment::get().getWorld()->getStore(), item, count); MWWorld::Ptr itemPtr = *ptr.getClass().getContainerStore (ptr).add (item, count, ptr);
// Configure item's script variables // Configure item's script variables
std::string script = MWWorld::Class::get(ref.getPtr()).getScript(ref.getPtr()); std::string script = MWWorld::Class::get(itemPtr).getScript(itemPtr);
if (script != "") if (script != "")
{ {
const ESM::Script *esmscript = MWBase::Environment::get().getWorld()->getStore().get<ESM::Script>().find (script); const ESM::Script *esmscript = MWBase::Environment::get().getWorld()->getStore().get<ESM::Script>().find (script);
ref.getPtr().getRefData().setLocals(*esmscript); itemPtr.getRefData().setLocals(*esmscript);
} }
MWWorld::Class::get (ptr).getContainerStore (ptr).add (ref.getPtr(), ptr);
// Spawn a messagebox (only for items added to player's inventory and if player is talking to someone) // Spawn a messagebox (only for items added to player's inventory and if player is talking to someone)
if (ptr == MWBase::Environment::get().getWorld ()->getPlayer ().getPlayer() ) if (ptr == MWBase::Environment::get().getWorld ()->getPlayer ().getPlayer() )
{ {
// The two GMST entries below expand to strings informing the player of what, and how many of it has been added to their inventory // The two GMST entries below expand to strings informing the player of what, and how many of it has been added to their inventory
std::string msgBox; std::string msgBox;
std::string itemName = MWWorld::Class::get(ref.getPtr()).getName(ref.getPtr()); std::string itemName = itemPtr.getClass().getName(itemPtr);
if (count == 1) if (count == 1)
{ {
msgBox = MyGUI::LanguageManager::getInstance().replaceTags("#{sNotifyMessage60}"); msgBox = MyGUI::LanguageManager::getInstance().replaceTags("#{sNotifyMessage60}");

View file

@ -19,7 +19,6 @@
#include "../mwworld/class.hpp" #include "../mwworld/class.hpp"
#include "../mwworld/player.hpp" #include "../mwworld/player.hpp"
#include "../mwworld/manualref.hpp"
#include "../mwworld/containerstore.hpp" #include "../mwworld/containerstore.hpp"
#include "../mwmechanics/npcstats.hpp" #include "../mwmechanics/npcstats.hpp"
@ -348,12 +347,8 @@ namespace MWScript
const MWWorld::ESMStore& store = MWBase::Environment::get().getWorld()->getStore(); const MWWorld::ESMStore& store = MWBase::Environment::get().getWorld()->getStore();
store.get<ESM::Creature>().find(creature); // This line throws an exception if it can't find the creature store.get<ESM::Creature>().find(creature); // This line throws an exception if it can't find the creature
MWWorld::ManualRef ref (MWBase::Environment::get().getWorld()->getStore(), gem, 1); MWWorld::Ptr item = *ptr.getClass().getContainerStore(ptr).add(gem, 1, ptr);
item.getCellRef().mSoul = creature;
ref.getPtr().getCellRef().mSoul = creature;
MWWorld::Class::get (ptr).getContainerStore (ptr).add (ref.getPtr(), ptr);
} }
}; };

View file

@ -118,6 +118,12 @@ bool MWWorld::ContainerStore::stacks(const Ptr& ptr1, const Ptr& ptr2)
|| cls2.getItemMaxHealth(ptr2) == ptr2.getCellRef().mCharge); || cls2.getItemMaxHealth(ptr2) == ptr2.getCellRef().mCharge);
} }
MWWorld::ContainerStoreIterator MWWorld::ContainerStore::add(const std::string &id, int count, const Ptr &actorPtr)
{
MWWorld::ManualRef ref(MWBase::Environment::get().getWorld()->getStore(), id, count);
return add(ref.getPtr(), actorPtr);
}
MWWorld::ContainerStoreIterator MWWorld::ContainerStore::add (const Ptr& itemPtr, const Ptr& actorPtr) MWWorld::ContainerStoreIterator MWWorld::ContainerStore::add (const Ptr& itemPtr, const Ptr& actorPtr)
{ {
MWWorld::ContainerStoreIterator it = addImp(itemPtr); MWWorld::ContainerStoreIterator it = addImp(itemPtr);

View file

@ -74,6 +74,9 @@ namespace MWWorld
/// ///
/// @return if stacking happened, return iterator to the item that was stacked against, otherwise iterator to the newly inserted item. /// @return if stacking happened, return iterator to the item that was stacked against, otherwise iterator to the newly inserted item.
ContainerStoreIterator add(const std::string& id, int count, const Ptr& actorPtr);
///< Utility to construct a ManualRef and call add(ptr, actorPtr)
int remove(const std::string& itemId, int count, const Ptr& actor); int remove(const std::string& itemId, int count, const Ptr& actor);
///< Remove \a count item(s) designated by \a itemId from this container. ///< Remove \a count item(s) designated by \a itemId from this container.
/// ///