mirror of
https://github.com/TES3MP/openmw-tes3mp.git
synced 2025-01-19 21:53:51 +00:00
Add a utility function for counting items in a container
This commit is contained in:
parent
c55f9bd0aa
commit
372cd437d1
3 changed files with 14 additions and 8 deletions
|
@ -87,15 +87,9 @@ namespace MWScript
|
|||
std::string item = runtime.getStringLiteral (runtime[0].mInteger);
|
||||
runtime.pop();
|
||||
|
||||
MWWorld::ContainerStore& store = MWWorld::Class::get (ptr).getContainerStore (ptr);
|
||||
MWWorld::ContainerStore& store = ptr.getClass().getContainerStore (ptr);
|
||||
|
||||
Interpreter::Type_Integer sum = 0;
|
||||
|
||||
for (MWWorld::ContainerStoreIterator iter (store.begin()); iter!=store.end(); ++iter)
|
||||
if (Misc::StringUtils::ciEqual(iter->getCellRef().mRefID, item))
|
||||
sum += iter->getRefData().getCount();
|
||||
|
||||
runtime.push (sum);
|
||||
runtime.push (store.count(item));
|
||||
}
|
||||
};
|
||||
|
||||
|
|
|
@ -77,6 +77,15 @@ MWWorld::ContainerStoreIterator MWWorld::ContainerStore::end()
|
|||
return ContainerStoreIterator (this);
|
||||
}
|
||||
|
||||
int MWWorld::ContainerStore::count(const std::string &id)
|
||||
{
|
||||
int total=0;
|
||||
for (MWWorld::ContainerStoreIterator iter (begin()); iter!=end(); ++iter)
|
||||
if (Misc::StringUtils::ciEqual(iter->getCellRef().mRefID, id))
|
||||
total += iter->getRefData().getCount();
|
||||
return total;
|
||||
}
|
||||
|
||||
void MWWorld::ContainerStore::unstack(const Ptr &ptr, const Ptr& container)
|
||||
{
|
||||
if (ptr.getRefData().getCount() <= 1)
|
||||
|
|
|
@ -92,6 +92,9 @@ namespace MWWorld
|
|||
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).
|
||||
|
||||
/// @return How many items with refID \a id are in this container?
|
||||
int count (const std::string& id);
|
||||
|
||||
protected:
|
||||
ContainerStoreIterator addNewStack (const Ptr& ptr, int count);
|
||||
///< Add the item to this container (do not try to stack it onto existing items)
|
||||
|
|
Loading…
Reference in a new issue