Adjust ContainerStore / InventoryStore to allow partial unequip of items.

coverity_scan
Jordan Ayers 9 years ago
parent 2ce2e8a1e7
commit c82d9a1e87

@ -136,16 +136,18 @@ int MWWorld::ContainerStore::count(const std::string &id)
return total;
}
void MWWorld::ContainerStore::unstack(const Ptr &ptr, const Ptr& container)
MWWorld::ContainerStoreIterator MWWorld::ContainerStore::unstack(const Ptr &ptr, const Ptr& container, int count)
{
if (ptr.getRefData().getCount() <= 1)
return;
MWWorld::ContainerStoreIterator it = addNewStack(ptr, ptr.getRefData().getCount()-1);
if (ptr.getRefData().getCount() <= count)
return end();
MWWorld::ContainerStoreIterator it = addNewStack(ptr, ptr.getRefData().getCount()-count);
const std::string script = it->getClass().getScript(*it);
if (!script.empty())
MWBase::Environment::get().getWorld()->getLocalScripts().add(script, *it);
remove(ptr, ptr.getRefData().getCount()-1, container);
remove(ptr, ptr.getRefData().getCount()-count, container);
return it;
}
MWWorld::ContainerStoreIterator MWWorld::ContainerStore::restack(const MWWorld::Ptr& item)

@ -130,8 +130,10 @@ namespace MWWorld
///
/// @return the number of items actually removed
void unstack (const Ptr& ptr, const Ptr& container);
///< Unstack an item in this container. The item's count will be set to 1, then a new stack will be added with (origCount-1).
ContainerStoreIterator unstack (const Ptr& ptr, const Ptr& container, int count = 1);
///< Unstack an item in this container. The item's count will be set to count, then a new stack will be added with (origCount-count).
///
/// @return an iterator to the new stack, or end() if no new stack was created.
MWWorld::ContainerStoreIterator restack (const MWWorld::Ptr& item);
///< Attempt to re-stack an item in this container.

@ -574,6 +574,33 @@ MWWorld::ContainerStoreIterator MWWorld::InventoryStore::unequipItem(const MWWor
throw std::runtime_error ("attempt to unequip an item that is not currently equipped");
}
MWWorld::ContainerStoreIterator MWWorld::InventoryStore::unequipItemQuantity(const Ptr& item, const Ptr& actor, int count)
{
if (!isEquipped(item))
throw std::runtime_error ("attempt to unequip an item that is not currently equipped");
if (count <= 0)
throw std::runtime_error ("attempt to unequip nothing (count <= 0)");
if (count > item.getRefData().getCount())
throw std::runtime_error ("attempt to unequip more items than equipped");
if (count == item.getRefData().getCount())
return unequipItem(item, actor);
// Move items to an existing stack if possible, otherwise split count items out into a new stack.
// Moving counts manually here, since ContainerStore's restack can't target unequipped stacks.
for (MWWorld::ContainerStoreIterator iter (begin()); iter != end(); ++iter)
{
if (stacks(*iter, item) && !isEquipped(*iter))
{
iter->getRefData().setCount(iter->getRefData().getCount() + count);
item.getRefData().setCount(item.getRefData().getCount() - count);
return iter;
}
}
return unstack(item, actor, item.getRefData().getCount() - count);
}
MWWorld::InventoryStoreListener* MWWorld::InventoryStore::getListener()
{
return mListener;

@ -188,6 +188,15 @@ namespace MWWorld
/// (it can be re-stacked so its count may be different than when it
/// was equipped).
ContainerStoreIterator unequipItemQuantity(const Ptr& item, const Ptr& actor, int count);
///< Unequip a specific quantity of an item identified by its Ptr.
/// An exception is thrown if the item is not currently equipped,
/// if count <= 0, or if count > the item stack size.
///
/// @return an iterator to the unequipped items that were previously
/// in the slot (they can be re-stacked so its count may be different
/// than the requested count).
void setListener (InventoryStoreListener* listener, const Ptr& actor);
///< Set a listener for various events, see \a InventoryStoreListener

Loading…
Cancel
Save