2022-06-26 16:42:29 +02:00
|
|
|
#ifndef OPENMW_MWMECHANICS_INVENTORY_H
|
|
|
|
#define OPENMW_MWMECHANICS_INVENTORY_H
|
|
|
|
|
|
|
|
#include "../mwbase/environment.hpp"
|
|
|
|
#include "../mwbase/world.hpp"
|
|
|
|
|
|
|
|
#include "../mwworld/esmstore.hpp"
|
|
|
|
|
2022-08-03 00:00:54 +02:00
|
|
|
#include <components/misc/strings/algorithm.hpp>
|
2022-06-26 16:42:29 +02:00
|
|
|
#include <components/esm3/loadcont.hpp>
|
|
|
|
|
|
|
|
#include <algorithm>
|
2022-08-23 16:59:03 +02:00
|
|
|
#include <string_view>
|
2022-06-26 16:42:29 +02:00
|
|
|
|
|
|
|
namespace MWMechanics
|
|
|
|
{
|
|
|
|
template <class T>
|
2022-08-23 16:59:03 +02:00
|
|
|
void modifyBaseInventory(std::string_view actorId, std::string_view itemId, int amount)
|
2022-06-26 16:42:29 +02:00
|
|
|
{
|
|
|
|
T copy = *MWBase::Environment::get().getWorld()->getStore().get<T>().find(actorId);
|
|
|
|
for (auto& it : copy.mInventory.mList)
|
|
|
|
{
|
|
|
|
if (Misc::StringUtils::ciEqual(it.mItem, itemId))
|
|
|
|
{
|
|
|
|
const int sign = it.mCount < 1 ? -1 : 1;
|
|
|
|
it.mCount = sign * std::max(it.mCount * sign + amount, 0);
|
|
|
|
MWBase::Environment::get().getWorld()->createOverrideRecord(copy);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (amount > 0)
|
|
|
|
{
|
|
|
|
ESM::ContItem cont;
|
|
|
|
cont.mItem = itemId;
|
|
|
|
cont.mCount = amount;
|
|
|
|
copy.mInventory.mList.push_back(cont);
|
|
|
|
MWBase::Environment::get().getWorld()->createOverrideRecord(copy);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|