From 16522ddc59dfe697c3b4fe73f6b91beae96222bc Mon Sep 17 00:00:00 2001 From: scrawl Date: Sat, 12 May 2012 23:09:03 +0200 Subject: [PATCH] InventoryStore re-stacking unequipped items --- apps/openmw/mwworld/containerstore.cpp | 19 ++++++++++++++----- apps/openmw/mwworld/containerstore.hpp | 3 +++ apps/openmw/mwworld/inventorystore.cpp | 20 ++++++++++++++++++-- 3 files changed, 35 insertions(+), 7 deletions(-) diff --git a/apps/openmw/mwworld/containerstore.cpp b/apps/openmw/mwworld/containerstore.cpp index 32e2da0ae..e28346de5 100644 --- a/apps/openmw/mwworld/containerstore.cpp +++ b/apps/openmw/mwworld/containerstore.cpp @@ -45,18 +45,27 @@ MWWorld::ContainerStoreIterator MWWorld::ContainerStore::end() return ContainerStoreIterator (this); } +bool MWWorld::ContainerStore::stacks(const Ptr& ptr1, const Ptr& ptr2) const +{ + /// \todo add current weapon/armor health, remaining lockpick/repair uses, current enchantment charge here as soon as they are implemented + if ( ptr1.mCellRef->refID == ptr2.mCellRef->refID + && (MWWorld::Class::get(ptr1).getScript(ptr1) == "" && MWWorld::Class::get(ptr2).getScript(ptr2) == "") // item with a script never stacks + && ptr1.mCellRef->owner == ptr2.mCellRef->owner + && ptr1.mCellRef->soul == ptr2.mCellRef->soul + && ptr1.mCellRef->charge == ptr2.mCellRef->charge) + return true; + + return false; +} + void MWWorld::ContainerStore::add (const Ptr& ptr) { int type = getType(ptr); // determine whether to stack or not - // item stacking depends on owner, script, enchantment and name for (MWWorld::ContainerStoreIterator iter (begin(type)); iter!=end(); ++iter) { - if ( iter->mCellRef->refID == ptr.mCellRef->refID - && MWWorld::Class::get(*iter).getScript(*iter) == MWWorld::Class::get(ptr).getScript(ptr) - && MWWorld::Class::get(*iter).getEnchantment(*iter) == MWWorld::Class::get(ptr).getEnchantment(ptr) - && iter->mCellRef->owner == ptr.mCellRef->owner) + if (stacks(*iter, ptr)) { // stack iter->getRefData().setCount( iter->getRefData().getCount() + ptr.getRefData().getCount() ); diff --git a/apps/openmw/mwworld/containerstore.hpp b/apps/openmw/mwworld/containerstore.hpp index da5424fe0..2d330f6ea 100644 --- a/apps/openmw/mwworld/containerstore.hpp +++ b/apps/openmw/mwworld/containerstore.hpp @@ -66,6 +66,9 @@ namespace MWWorld ContainerStoreIterator end(); + bool stacks (const Ptr& ptr1, const Ptr& ptr2) const; + ///< @return true if the two specified objects can stack with each other + void add (const Ptr& ptr); ///< Add the item pointed to by \a ptr to this container. /// diff --git a/apps/openmw/mwworld/inventorystore.cpp b/apps/openmw/mwworld/inventorystore.cpp index 576f2371f..f149513df 100644 --- a/apps/openmw/mwworld/inventorystore.cpp +++ b/apps/openmw/mwworld/inventorystore.cpp @@ -67,9 +67,25 @@ void MWWorld::InventoryStore::equip (int slot, const ContainerStoreIterator& ite throw std::runtime_error ("invalid slot"); } - /// \todo restack item previously in this slot (if required) + // restack item previously in this slot (if required) + if (mSlots[slot] != end()) + { + for (MWWorld::ContainerStoreIterator iter (begin()); iter!=end(); ++iter) + { + if (stacks(*iter, *mSlots[slot])) + { + iter->getRefData().setCount( iter->getRefData().getCount() + mSlots[slot]->getRefData().getCount() ); + mSlots[slot]->getRefData().setCount(0); + break; + } + } + } - /// \todo unstack item pointed to by iterator if required) + // unstack item pointed to by iterator if required + if (iterator->getRefData().getCount() > 1) + { + /// \ŧodo ??? + } mSlots[slot] = iterator;