Issue #217: added getWeight function to ContainerStore

actorid
Marc Zinnschlag 13 years ago
parent 3dcfcf46cb
commit 47828f91f0

@ -8,8 +8,29 @@
#include <components/esm/loadcont.hpp>
#include "manualref.hpp"
#include "refdata.hpp"
MWWorld::ContainerStore::ContainerStore() : mStateId (0) {}
namespace
{
template<typename T>
float getTotalWeight (const ESMS::CellRefList<T, MWWorld::RefData>& cellRefList)
{
float sum = 0;
for (typename ESMS::CellRefList<T, MWWorld::RefData>::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())

@ -53,6 +53,8 @@ namespace MWWorld
ESMS::CellRefList<ESM::Repair, RefData> repairs;
ESMS::CellRefList<ESM::Weapon, RefData> 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.

Loading…
Cancel
Save