From c82d9a1e873feae70a845feae26b6cbb5572de79 Mon Sep 17 00:00:00 2001 From: Jordan Ayers Date: Mon, 18 Jan 2016 19:56:35 -0600 Subject: [PATCH] Adjust ContainerStore / InventoryStore to allow partial unequip of items. --- apps/openmw/mwworld/containerstore.cpp | 12 +++++++----- apps/openmw/mwworld/containerstore.hpp | 6 ++++-- apps/openmw/mwworld/inventorystore.cpp | 27 ++++++++++++++++++++++++++ apps/openmw/mwworld/inventorystore.hpp | 9 +++++++++ 4 files changed, 47 insertions(+), 7 deletions(-) diff --git a/apps/openmw/mwworld/containerstore.cpp b/apps/openmw/mwworld/containerstore.cpp index 45390405e..e07ba433a 100644 --- a/apps/openmw/mwworld/containerstore.cpp +++ b/apps/openmw/mwworld/containerstore.cpp @@ -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) diff --git a/apps/openmw/mwworld/containerstore.hpp b/apps/openmw/mwworld/containerstore.hpp index b7ec4bb74..6a8fc4761 100644 --- a/apps/openmw/mwworld/containerstore.hpp +++ b/apps/openmw/mwworld/containerstore.hpp @@ -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. diff --git a/apps/openmw/mwworld/inventorystore.cpp b/apps/openmw/mwworld/inventorystore.cpp index dbd2c6794..2c994f146 100644 --- a/apps/openmw/mwworld/inventorystore.cpp +++ b/apps/openmw/mwworld/inventorystore.cpp @@ -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; diff --git a/apps/openmw/mwworld/inventorystore.hpp b/apps/openmw/mwworld/inventorystore.hpp index 28c44bcec..2d7c9f6e9 100644 --- a/apps/openmw/mwworld/inventorystore.hpp +++ b/apps/openmw/mwworld/inventorystore.hpp @@ -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