1
0
Fork 0
mirror of https://github.com/OpenMW/openmw.git synced 2025-02-03 14:15:35 +00:00

Add remove methods to MWWorld::ContainerStore

This commit is contained in:
Emanuel Guevel 2013-08-13 01:19:33 +02:00
parent a198751f21
commit 23b8206bdc
4 changed files with 55 additions and 0 deletions

View file

@ -198,6 +198,40 @@ MWWorld::ContainerStoreIterator MWWorld::ContainerStore::addImpl (const Ptr& ptr
return it; return it;
} }
int MWWorld::ContainerStore::remove(const std::string& itemId, int count, const Ptr& actor)
{
int toRemove = count;
for (ContainerStoreIterator iter(begin()); iter != end() && toRemove > 0; ++iter)
if (Misc::StringUtils::ciEqual(iter->getCellRef().mRefID, itemId))
toRemove -= remove(*iter, toRemove, actor);
// number of removed items
return count - toRemove;
}
int MWWorld::ContainerStore::remove(const Ptr& item, int count, const Ptr& actor)
{
assert(this == item.getContainerStore());
int toRemove = count;
RefData& itemRef = item.getRefData();
if (itemRef.getCount() <= toRemove)
{
toRemove -= itemRef.getCount();
itemRef.setCount(0);
}
else
{
itemRef.setCount(itemRef.getCount() - toRemove);
toRemove = 0;
}
// number of removed items
return count - toRemove;
}
void MWWorld::ContainerStore::fill (const ESM::InventoryList& items, const std::string& owner, const MWWorld::ESMStore& store) void MWWorld::ContainerStore::fill (const ESM::InventoryList& items, const std::string& owner, const MWWorld::ESMStore& store)
{ {
for (std::vector<ESM::ContItem>::const_iterator iter (items.mList.begin()); iter!=items.mList.end(); for (std::vector<ESM::ContItem>::const_iterator iter (items.mList.begin()); iter!=items.mList.end();

View file

@ -75,6 +75,16 @@ 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.
int remove(const std::string& itemId, int count, const Ptr& actor);
///< Remove \a count item(s) designated by \a itemId from this container.
///
/// @return the number of items actually removed
virtual int remove(const Ptr& item, int count, const Ptr& actor);
///< Remove \a count item(s) designated by \a item from this inventory.
///
/// @return the number of items actually removed
protected: protected:
ContainerStoreIterator addImpl (const Ptr& ptr); ContainerStoreIterator addImpl (const Ptr& ptr);
///< Add the item to this container (no stacking) ///< Add the item to this container (no stacking)

View file

@ -325,3 +325,8 @@ MWWorld::ContainerStoreIterator MWWorld::InventoryStore::getSelectedEnchantItem(
{ {
return mSelectedEnchantItem; return mSelectedEnchantItem;
} }
int MWWorld::InventoryStore::remove(const Ptr& item, int count, const Ptr& actor)
{
return ContainerStore::remove(item, count, actor);
}

View file

@ -108,6 +108,12 @@ namespace MWWorld
///< @return true if the two specified objects can stack with each other ///< @return true if the two specified objects can stack with each other
/// @note ptr1 is the item that is already in this container /// @note ptr1 is the item that is already in this container
virtual int remove(const Ptr& item, int count, const Ptr& actor);
///< Remove \a count item(s) designated by \a item from this inventory.
///
/// \todo check if the item is equipped and do stuff
///
/// @return the number of items actually removed
}; };
} }