diff --git a/apps/openmw/mwworld/containerstore.cpp b/apps/openmw/mwworld/containerstore.cpp index 243d5c3516..2800b6f3c1 100644 --- a/apps/openmw/mwworld/containerstore.cpp +++ b/apps/openmw/mwworld/containerstore.cpp @@ -8,8 +8,29 @@ #include #include "manualref.hpp" +#include "refdata.hpp" -MWWorld::ContainerStore::ContainerStore() : mStateId (0) {} +namespace +{ + template + float getTotalWeight (const ESMS::CellRefList& cellRefList) + { + float sum = 0; + + for (typename ESMS::CellRefList::List::const_iterator iter ( + cellRefList.list.begin()); + iter!=cellRefList.list.end(); + ++iter) + { + if (iter->mData.getCount()>0) + sum += iter->mData.getCount()*iter->base->data.weight; + } + + return sum; + } +} + +MWWorld::ContainerStore::ContainerStore() : mStateId (0), mCachedWeight (0), mWeightUpToDate (false) {} MWWorld::ContainerStore::~ContainerStore() {} @@ -87,6 +108,7 @@ void MWWorld::ContainerStore::clear() void MWWorld::ContainerStore::flagAsModified() { ++mStateId; + mWeightUpToDate = false; } int MWWorld::ContainerStore::getStateId() const @@ -94,6 +116,31 @@ int MWWorld::ContainerStore::getStateId() const return mStateId; } +float MWWorld::ContainerStore::getWeight() const +{ + if (!mWeightUpToDate) + { + mCachedWeight = 0; + + mCachedWeight += getTotalWeight (potions); + mCachedWeight += getTotalWeight (appas); + mCachedWeight += getTotalWeight (armors); + mCachedWeight += getTotalWeight (books); + mCachedWeight += getTotalWeight (clothes); + mCachedWeight += getTotalWeight (ingreds); + mCachedWeight += getTotalWeight (lights); + mCachedWeight += getTotalWeight (lockpicks); + mCachedWeight += getTotalWeight (miscItems); + mCachedWeight += getTotalWeight (probes); + mCachedWeight += getTotalWeight (repairs); + mCachedWeight += getTotalWeight (weapons); + + mWeightUpToDate = true; + } + + return mCachedWeight; +} + int MWWorld::ContainerStore::getType (const Ptr& ptr) { if (ptr.isEmpty()) diff --git a/apps/openmw/mwworld/containerstore.hpp b/apps/openmw/mwworld/containerstore.hpp index 669b394ca8..da5424fe08 100644 --- a/apps/openmw/mwworld/containerstore.hpp +++ b/apps/openmw/mwworld/containerstore.hpp @@ -53,6 +53,8 @@ namespace MWWorld ESMS::CellRefList repairs; ESMS::CellRefList weapons; int mStateId; + mutable float mCachedWeight; + mutable bool mWeightUpToDate; public: @@ -87,6 +89,9 @@ namespace MWWorld /// are accessed in a way that may be used to modify the item. /// \note This method of change-tracking will ocasionally yield false positives. + float getWeight() const; + ///< Return total weight of the items contained in *this. + static int getType (const Ptr& ptr); ///< This function throws an exception, if ptr does not point to an object, that can be /// put into a container.