1
0
mirror of https://gitlab.com/OpenMW/openmw.git synced 2025-01-27 12:35:46 +00:00
OpenMW/apps/openmw/mwworld/inventorystore.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

764 lines
24 KiB
C++
Raw Normal View History

#include "inventorystore.hpp"
2012-03-13 13:31:11 +01:00
2012-03-13 14:04:19 +01:00
#include <algorithm>
2012-03-13 13:31:11 +01:00
#include <iterator>
2012-03-13 14:04:19 +01:00
#include <components/esm3/inventorystate.hpp>
#include "../mwbase/environment.hpp"
#include "../mwbase/world.hpp"
2015-08-21 21:12:39 +12:00
#include "../mwmechanics/actorutil.hpp"
2012-04-08 12:26:21 +02:00
#include "../mwmechanics/npcstats.hpp"
#include "../mwmechanics/weapontype.hpp"
2012-04-08 12:26:21 +02:00
2012-03-13 14:04:19 +01:00
#include "class.hpp"
2012-10-01 19:17:04 +04:00
#include "esmstore.hpp"
2012-03-13 13:31:11 +01:00
void MWWorld::InventoryStore::copySlots(const InventoryStore& store)
{
// some const-trickery, required because of a flaw in the handling of MW-references and the
// resulting workarounds
for (std::vector<ContainerStoreIterator>::const_iterator iter(const_cast<InventoryStore&>(store).mSlots.begin());
iter != const_cast<InventoryStore&>(store).mSlots.end(); ++iter)
{
std::size_t distance = std::distance(const_cast<InventoryStore&>(store).begin(), *iter);
ContainerStoreIterator slot = begin();
std::advance(slot, distance);
mSlots.push_back(slot);
}
// some const-trickery, required because of a flaw in the handling of MW-references and the
// resulting workarounds
std::size_t distance = std::distance(
const_cast<InventoryStore&>(store).begin(), const_cast<InventoryStore&>(store).mSelectedEnchantItem);
ContainerStoreIterator slot = begin();
std::advance(slot, distance);
mSelectedEnchantItem = slot;
2012-03-13 13:31:11 +01:00
}
void MWWorld::InventoryStore::initSlots(TSlots& slots_)
2012-03-13 13:31:11 +01:00
{
for (int i = 0; i < Slots; ++i)
slots_.push_back(end());
}
void MWWorld::InventoryStore::storeEquipmentState(
const MWWorld::LiveCellRefBase& ref, int index, ESM::InventoryState& inventory) const
{
for (int i = 0; i < static_cast<int>(mSlots.size()); ++i)
if (mSlots[i].getType() != -1 && mSlots[i]->getBase() == &ref)
{
inventory.mEquipmentSlots[index] = i;
}
if (mSelectedEnchantItem.getType() != -1 && mSelectedEnchantItem->getBase() == &ref)
inventory.mSelectedEnchantItem = index;
}
void MWWorld::InventoryStore::readEquipmentState(
const MWWorld::ContainerStoreIterator& iter, int index, const ESM::InventoryState& inventory)
{
if (index == inventory.mSelectedEnchantItem)
mSelectedEnchantItem = iter;
std::map<int, int>::const_iterator found = inventory.mEquipmentSlots.find(index);
if (found != inventory.mEquipmentSlots.end())
{
if (found->second < 0 || found->second >= MWWorld::InventoryStore::Slots)
throw std::runtime_error("Invalid slot index in inventory state");
// make sure the item can actually be equipped in this slot
int slot = found->second;
std::pair<std::vector<int>, bool> allowedSlots = iter->getClass().getEquipmentSlots(*iter);
if (!allowedSlots.first.size())
return;
if (std::find(allowedSlots.first.begin(), allowedSlots.first.end(), slot) == allowedSlots.first.end())
slot = allowedSlots.first.front();
// unstack if required
if (!allowedSlots.second && iter->getRefData().getCount() > 1)
{
int count = iter->getRefData().getCount(false);
MWWorld::ContainerStoreIterator newIter = addNewStack(*iter, count > 0 ? 1 : -1);
iter->getRefData().setCount(subtractItems(count, 1));
mSlots[slot] = newIter;
}
else
mSlots[slot] = iter;
}
}
MWWorld::InventoryStore::InventoryStore()
: ContainerStore()
, mInventoryListener(nullptr)
, mUpdatesEnabled(true)
, mFirstAutoEquip(true)
, mSelectedEnchantItem(end())
{
initSlots(mSlots);
2012-03-13 13:31:11 +01:00
}
MWWorld::InventoryStore::InventoryStore(const InventoryStore& store)
2014-09-26 17:12:48 +02:00
: ContainerStore(store)
, mInventoryListener(store.mInventoryListener)
2014-09-26 17:12:48 +02:00
, mUpdatesEnabled(store.mUpdatesEnabled)
, mFirstAutoEquip(store.mFirstAutoEquip)
, mSelectedEnchantItem(end())
2012-03-13 13:31:11 +01:00
{
copySlots(store);
}
MWWorld::InventoryStore& MWWorld::InventoryStore::operator=(const InventoryStore& store)
{
if (this == &store)
return *this;
mListener = store.mListener;
mInventoryListener = store.mInventoryListener;
mFirstAutoEquip = store.mFirstAutoEquip;
mRechargingItemsUpToDate = false;
2012-03-13 13:31:11 +01:00
ContainerStore::operator=(store);
mSlots.clear();
copySlots(store);
return *this;
}
MWWorld::ContainerStoreIterator MWWorld::InventoryStore::add(
const Ptr& itemPtr, int count, bool allowAutoEquip, bool resolve)
{
const MWWorld::ContainerStoreIterator& retVal
= MWWorld::ContainerStore::add(itemPtr, count, allowAutoEquip, resolve);
// Auto-equip items if an armor/clothing item is added, but not for the player nor werewolves
if (allowAutoEquip && mActor != MWMechanics::getPlayer() && mActor.getClass().isNpc()
&& !mActor.getClass().getNpcStats(mActor).isWerewolf())
{
auto type = itemPtr.getType();
if (type == ESM::Armor::sRecordId || type == ESM::Clothing::sRecordId)
autoEquip();
}
if (mListener)
mListener->itemAdded(*retVal, count);
return retVal;
}
void MWWorld::InventoryStore::equip(int slot, const ContainerStoreIterator& iterator)
2012-03-13 13:31:11 +01:00
{
if (iterator == end())
throw std::runtime_error("can't equip end() iterator, use unequip function instead");
2012-03-13 13:31:11 +01:00
if (slot < 0 || slot >= static_cast<int>(mSlots.size()))
throw std::runtime_error("slot number out of range");
2012-03-13 14:04:19 +01:00
if (iterator.getContainerStore() != this)
throw std::runtime_error("attempt to equip an item that is not in the inventory");
std::pair<std::vector<int>, bool> slots_;
2012-03-13 14:04:19 +01:00
slots_ = iterator->getClass().getEquipmentSlots(*iterator);
if (std::find(slots_.first.begin(), slots_.first.end(), slot) == slots_.first.end())
throw std::runtime_error("invalid slot");
2012-03-13 14:04:19 +01:00
if (mSlots[slot] != end())
unequipSlot(slot);
2012-03-13 14:04:19 +01:00
// unstack item pointed to by iterator if required
if (iterator != end() && !slots_.second
&& iterator->getRefData().getCount() > 1) // if slots.second is true, item can stay stacked when equipped
{
unstack(*iterator);
}
2012-03-13 13:31:11 +01:00
mSlots[slot] = iterator;
2012-03-21 12:48:05 +01:00
flagAsModified();
2013-10-25 22:16:52 +02:00
fireEquipmentChangedEvent();
2012-03-13 13:31:11 +01:00
}
void MWWorld::InventoryStore::unequipAll()
{
mUpdatesEnabled = false;
for (int slot = 0; slot < MWWorld::InventoryStore::Slots; ++slot)
unequipSlot(slot);
mUpdatesEnabled = true;
fireEquipmentChangedEvent();
}
2012-03-13 13:31:11 +01:00
MWWorld::ContainerStoreIterator MWWorld::InventoryStore::getSlot(int slot)
{
return findSlot(slot);
2012-03-13 13:31:11 +01:00
}
MWWorld::ConstContainerStoreIterator MWWorld::InventoryStore::getSlot(int slot) const
{
return findSlot(slot);
2012-03-13 13:31:11 +01:00
}
MWWorld::ContainerStoreIterator MWWorld::InventoryStore::findSlot(int slot) const
{
if (slot < 0 || slot >= static_cast<int>(mSlots.size()))
throw std::runtime_error("slot number out of range");
if (mSlots[slot] == end())
return mSlots[slot];
if (mSlots[slot]->getRefData().getCount() < 1)
{
// Object has been deleted
// This should no longer happen, since the new remove function will unequip first
throw std::runtime_error(
"Invalid slot, make sure you are not calling RefData::setCount for a container object");
}
return mSlots[slot];
}
void MWWorld::InventoryStore::autoEquipWeapon(TSlots& slots_)
{
if (!mActor.getClass().isNpc())
{
// In original game creatures do not autoequip weapon, but we need it for weapon sheathing.
// The only case when the difference is noticable - when this creature sells weapon.
// So just disable weapon autoequipping for creatures which sells weapon.
int services = mActor.getClass().getServices(mActor);
bool sellsWeapon = services & (ESM::NPC::Weapon | ESM::NPC::MagicItems);
if (sellsWeapon)
return;
}
static const ESM::Skill::SkillEnum weaponSkills[] = {
ESM::Skill::LongBlade,
ESM::Skill::Axe,
ESM::Skill::Spear,
ESM::Skill::ShortBlade,
ESM::Skill::Marksman,
ESM::Skill::BluntWeapon,
};
const size_t weaponSkillsLength = sizeof(weaponSkills) / sizeof(weaponSkills[0]);
bool weaponSkillVisited[weaponSkillsLength] = { false };
// give arrows/bolt with max damage by default
int arrowMax = 0;
int boltMax = 0;
ContainerStoreIterator arrow(end());
ContainerStoreIterator bolt(end());
// rate ammo
for (ContainerStoreIterator iter(begin(ContainerStore::Type_Weapon)); iter != end(); ++iter)
{
const ESM::Weapon* esmWeapon = iter->get<ESM::Weapon>()->mBase;
if (esmWeapon->mData.mType == ESM::Weapon::Arrow)
{
if (esmWeapon->mData.mChop[1] >= arrowMax)
{
arrowMax = esmWeapon->mData.mChop[1];
arrow = iter;
}
}
else if (esmWeapon->mData.mType == ESM::Weapon::Bolt)
{
if (esmWeapon->mData.mChop[1] >= boltMax)
{
boltMax = esmWeapon->mData.mChop[1];
bolt = iter;
}
}
}
// rate weapon
for (int i = 0; i < static_cast<int>(weaponSkillsLength); ++i)
{
float max = 0;
int maxWeaponSkill = -1;
for (int j = 0; j < static_cast<int>(weaponSkillsLength); ++j)
{
float skillValue = mActor.getClass().getSkill(mActor, static_cast<int>(weaponSkills[j]));
if (skillValue > max && !weaponSkillVisited[j])
{
max = skillValue;
maxWeaponSkill = j;
}
}
if (maxWeaponSkill == -1)
break;
max = 0;
ContainerStoreIterator weapon(end());
for (ContainerStoreIterator iter(begin(ContainerStore::Type_Weapon)); iter != end(); ++iter)
{
const ESM::Weapon* esmWeapon = iter->get<ESM::Weapon>()->mBase;
if (MWMechanics::getWeaponType(esmWeapon->mData.mType)->mWeaponClass == ESM::WeaponType::Ammo)
continue;
if (iter->getClass().getEquipmentSkill(*iter) == weaponSkills[maxWeaponSkill])
{
if (esmWeapon->mData.mChop[1] >= max)
{
max = esmWeapon->mData.mChop[1];
weapon = iter;
}
if (esmWeapon->mData.mSlash[1] >= max)
{
max = esmWeapon->mData.mSlash[1];
weapon = iter;
}
if (esmWeapon->mData.mThrust[1] >= max)
{
max = esmWeapon->mData.mThrust[1];
weapon = iter;
}
}
}
if (weapon != end() && weapon->getClass().canBeEquipped(*weapon, mActor).first)
{
// Do not equip ranged weapons, if there is no suitable ammo
bool hasAmmo = true;
const MWWorld::LiveCellRef<ESM::Weapon>* ref = weapon->get<ESM::Weapon>();
int type = ref->mBase->mData.mType;
int ammotype = MWMechanics::getWeaponType(type)->mAmmoType;
if (ammotype == ESM::Weapon::Arrow)
{
if (arrow == end())
hasAmmo = false;
else
slots_[Slot_Ammunition] = arrow;
}
else if (ammotype == ESM::Weapon::Bolt)
{
if (bolt == end())
hasAmmo = false;
else
slots_[Slot_Ammunition] = bolt;
}
if (hasAmmo)
{
std::pair<std::vector<int>, bool> itemsSlots = weapon->getClass().getEquipmentSlots(*weapon);
if (!itemsSlots.first.empty())
{
if (!itemsSlots.second)
{
if (weapon->getRefData().getCount() > 1)
{
unstack(*weapon);
}
}
int slot = itemsSlots.first.front();
slots_[slot] = weapon;
if (ammotype == ESM::Weapon::None)
slots_[Slot_Ammunition] = end();
}
break;
}
}
weaponSkillVisited[maxWeaponSkill] = true;
}
}
void MWWorld::InventoryStore::autoEquipArmor(TSlots& slots_)
{
// Only NPCs can wear armor for now.
// For creatures we equip only shields.
if (!mActor.getClass().isNpc())
{
autoEquipShield(slots_);
return;
}
2023-04-20 21:07:53 +02:00
const MWWorld::Store<ESM::GameSetting>& store = MWBase::Environment::get().getESMStore()->get<ESM::GameSetting>();
2018-08-29 18:38:12 +03:00
static float fUnarmoredBase1 = store.find("fUnarmoredBase1")->mValue.getFloat();
static float fUnarmoredBase2 = store.find("fUnarmoredBase2")->mValue.getFloat();
float unarmoredSkill = mActor.getClass().getSkill(mActor, ESM::Skill::Unarmored);
float unarmoredRating = (fUnarmoredBase1 * unarmoredSkill) * (fUnarmoredBase2 * unarmoredSkill);
for (ContainerStoreIterator iter(begin(ContainerStore::Type_Clothing | ContainerStore::Type_Armor)); iter != end();
++iter)
{
Ptr test = *iter;
switch (test.getClass().canBeEquipped(test, mActor).first)
{
case 0:
continue;
default:
break;
}
if (iter.getType() == ContainerStore::Type_Armor
&& test.getClass().getEffectiveArmorRating(test, mActor) <= std::max(unarmoredRating, 0.f))
{
continue;
}
std::pair<std::vector<int>, bool> itemsSlots = iter->getClass().getEquipmentSlots(*iter);
// checking if current item pointed by iter can be equipped
for (int slot : itemsSlots.first)
{
2017-10-13 14:47:26 +02:00
// if true then it means slot is equipped already
// check if slot may require swapping if current item is more valuable
if (slots_.at(slot) != end())
{
Ptr old = *slots_.at(slot);
if (iter.getType() == ContainerStore::Type_Armor)
2012-04-08 12:26:21 +02:00
{
if (old.getType() == ESM::Armor::sRecordId)
{
if (old.get<ESM::Armor>()->mBase->mData.mType < test.get<ESM::Armor>()->mBase->mData.mType)
continue;
if (old.get<ESM::Armor>()->mBase->mData.mType == test.get<ESM::Armor>()->mBase->mData.mType)
{
if (old.getClass().getEffectiveArmorRating(old, mActor)
>= test.getClass().getEffectiveArmorRating(test, mActor))
// old armor had better armor rating
continue;
}
}
// suitable armor should replace already equipped clothing
2012-04-08 12:26:21 +02:00
}
else if (iter.getType() == ContainerStore::Type_Clothing)
{
2017-10-13 14:47:26 +02:00
// if left ring is equipped
if (slot == Slot_LeftRing)
{
// if there is a place for right ring dont swap it
if (slots_.at(Slot_RightRing) == end())
{
continue;
}
else // if right ring is equipped too
{
Ptr rightRing = *slots_.at(Slot_RightRing);
// we want to swap cheaper ring only if both are equipped
if (old.getClass().getValue(old) >= rightRing.getClass().getValue(rightRing))
continue;
}
}
if (old.getType() == ESM::Clothing::sRecordId)
2012-04-08 12:26:21 +02:00
{
// check value
if (old.getClass().getValue(old) >= test.getClass().getValue(test))
// old clothing was more valuable
continue;
2012-04-08 12:26:21 +02:00
}
else
// suitable clothing should NOT replace already equipped armor
continue;
}
}
2012-05-13 11:52:17 +02:00
if (!itemsSlots.second) // if itemsSlots.second is true, item can stay stacked when equipped
{
// unstack item pointed to by iterator if required
if (iter->getRefData().getCount() > 1)
{
unstack(*iter);
2012-05-13 11:52:17 +02:00
}
}
2017-10-13 14:47:26 +02:00
// if we are here it means item can be equipped or swapped
slots_[slot] = iter;
break;
}
}
}
2012-05-13 14:58:38 +02:00
void MWWorld::InventoryStore::autoEquipShield(TSlots& slots_)
{
for (ContainerStoreIterator iter(begin(ContainerStore::Type_Armor)); iter != end(); ++iter)
{
if (iter->get<ESM::Armor>()->mBase->mData.mType != ESM::Armor::Shield)
continue;
if (iter->getClass().canBeEquipped(*iter, mActor).first != 1)
continue;
std::pair<std::vector<int>, bool> shieldSlots = iter->getClass().getEquipmentSlots(*iter);
int slot = shieldSlots.first[0];
const ContainerStoreIterator& shield = slots_[slot];
if (shield != end() && shield.getType() == Type_Armor
&& shield->get<ESM::Armor>()->mBase->mData.mType == ESM::Armor::Shield)
{
if (shield->getClass().getItemHealth(*shield) >= iter->getClass().getItemHealth(*iter))
continue;
}
slots_[slot] = iter;
}
}
void MWWorld::InventoryStore::autoEquip()
{
TSlots slots_;
initSlots(slots_);
// Disable model update during auto-equip
mUpdatesEnabled = false;
// Autoequip clothing, armor and weapons.
// Equipping lights is handled in Actors::updateEquippedLight based on environment light.
// Note: creatures ignore equipment armor rating and only equip shields
// Use custom logic for them - select shield based on its health instead of armor rating
autoEquipWeapon(slots_);
autoEquipArmor(slots_);
bool changed = false;
for (std::size_t i = 0; i < slots_.size(); ++i)
{
if (slots_[i] != mSlots[i])
{
changed = true;
break;
}
}
mUpdatesEnabled = true;
if (changed)
{
mSlots.swap(slots_);
fireEquipmentChangedEvent();
flagAsModified();
}
}
MWWorld::ContainerStoreIterator MWWorld::InventoryStore::getPreferredShield()
{
TSlots slots;
initSlots(slots);
autoEquipArmor(slots);
return slots[Slot_CarriedLeft];
}
2017-03-08 01:28:56 +01:00
bool MWWorld::InventoryStore::stacks(const ConstPtr& ptr1, const ConstPtr& ptr2) const
2012-05-13 14:58:38 +02:00
{
2013-11-12 23:12:56 +01:00
bool canStack = MWWorld::ContainerStore::stacks(ptr1, ptr2);
2012-05-13 14:58:38 +02:00
if (!canStack)
return false;
// don't stack if either item is currently equipped
2012-05-13 14:58:38 +02:00
for (TSlots::const_iterator iter(mSlots.begin()); iter != mSlots.end(); ++iter)
{
2013-11-12 23:12:56 +01:00
if (*iter != end() && (ptr1 == **iter || ptr2 == **iter))
{
bool stackWhenEquipped = (*iter)->getClass().getEquipmentSlots(**iter).second;
if (!stackWhenEquipped)
return false;
}
2012-05-13 14:58:38 +02:00
}
return true;
}
2012-05-29 12:35:03 +02:00
void MWWorld::InventoryStore::setSelectedEnchantItem(const ContainerStoreIterator& iterator)
{
mSelectedEnchantItem = iterator;
}
MWWorld::ContainerStoreIterator MWWorld::InventoryStore::getSelectedEnchantItem()
{
return mSelectedEnchantItem;
}
int MWWorld::InventoryStore::remove(const Ptr& item, int count, bool equipReplacement, bool resolve)
2017-10-06 10:54:25 +04:00
{
int retCount = ContainerStore::remove(item, count, equipReplacement, resolve);
bool wasEquipped = false;
if (!item.getRefData().getCount())
{
for (int slot = 0; slot < MWWorld::InventoryStore::Slots; ++slot)
{
if (mSlots[slot] == end())
continue;
if (*mSlots[slot] == item)
{
unequipSlot(slot);
wasEquipped = true;
break;
}
}
}
// If an armor/clothing item is removed, try to find a replacement,
// but not for the player nor werewolves, and not if the RemoveItem script command
// was used (equipReplacement is false)
if (equipReplacement && wasEquipped && (mActor != MWMechanics::getPlayer()) && mActor.getClass().isNpc()
&& !mActor.getClass().getNpcStats(mActor).isWerewolf())
{
auto type = item.getType();
if (type == ESM::Armor::sRecordId || type == ESM::Clothing::sRecordId)
autoEquip();
}
if (item.getRefData().getCount() == 0 && mSelectedEnchantItem != end() && *mSelectedEnchantItem == item)
{
mSelectedEnchantItem = end();
}
if (mListener)
mListener->itemRemoved(item, retCount);
return retCount;
}
MWWorld::ContainerStoreIterator MWWorld::InventoryStore::unequipSlot(int slot, bool applyUpdates)
{
if (slot < 0 || slot >= static_cast<int>(mSlots.size()))
throw std::runtime_error("slot number out of range");
ContainerStoreIterator it = mSlots[slot];
if (it != end())
{
ContainerStoreIterator retval = it;
2013-11-14 19:54:00 +01:00
// empty this slot
mSlots[slot] = end();
if (it->getRefData().getCount())
{
retval = restack(*it);
if (mActor == MWMechanics::getPlayer())
{
// Unset OnPCEquip Variable on item's script, if it has a script with that variable declared
Initial commit: In ESM structures, replace the string members that are RefIds to other records, to a new strong type The strong type is actually just a string underneath, but this will help in the future to have a distinction so it's easier to search and replace when we use an integer ID Slowly going through all the changes to make, still hundreds of errors a lot of functions/structures use std::string or stringview to designate an ID. So it takes time Continues slowly replacing ids. There are technically more and more compilation errors I have good hope that there is a point where the amount of errors will dramatically go down as all the main functions use the ESM::RefId type Continue moving forward, changes to the stores slowly moving along Starting to see the fruit of those changes. still many many error, but more and more Irun into a situation where a function is sandwiched between two functions that use the RefId type. More replacements. Things are starting to get easier I can see more and more often the issue is that the function is awaiting a RefId, but is given a string there is less need to go down functions and to fix a long list of them. Still moving forward, and for the first time error count is going down! Good pace, not sure about topics though, mId and mName are actually the same thing and are used interchangeably Cells are back to using string for the name, haven't fixed everything yet. Many other changes Under the bar of 400 compilation errors. more good progress <100 compile errors! More progress Game settings store can use string for find, it was a bit absurd how every use of it required to create refId from string some more progress on other fronts Mostly game settings clean one error opened a lot of other errors. Down to 18, but more will prbably appear only link errors left?? Fixed link errors OpenMW compiles, and launches, with some issues, but still!
2022-09-25 13:17:09 +02:00
const ESM::RefId& script = it->getClass().getScript(*it);
2022-08-11 22:51:55 +02:00
if (!script.empty())
(*it).getRefData().getLocals().setVarByInt(script, "onpcequip", 0);
}
if ((mSelectedEnchantItem != end()) && (mSelectedEnchantItem == it))
{
mSelectedEnchantItem = end();
}
}
if (applyUpdates)
{
fireEquipmentChangedEvent();
}
return retval;
}
return it;
}
2013-11-01 00:21:15 +01:00
MWWorld::ContainerStoreIterator MWWorld::InventoryStore::unequipItem(const MWWorld::Ptr& item)
2013-11-01 00:21:15 +01:00
{
for (int slot = 0; slot < MWWorld::InventoryStore::Slots; ++slot)
{
MWWorld::ContainerStoreIterator equipped = getSlot(slot);
if (equipped != end() && *equipped == item)
return unequipSlot(slot);
2013-11-01 00:21:15 +01:00
}
throw std::runtime_error("attempt to unequip an item that is not currently equipped");
}
2013-10-25 22:16:52 +02:00
MWWorld::ContainerStoreIterator MWWorld::InventoryStore::unequipItemQuantity(const Ptr& item, int count)
{
if (!isEquipped(item))
throw std::runtime_error("attempt to unequip an item that is not currently equipped");
if (count <= 0)
throw std::runtime_error("attempt to unequip nothing (count <= 0)");
if (count > item.getRefData().getCount())
throw std::runtime_error("attempt to unequip more items than equipped");
if (count == item.getRefData().getCount())
return unequipItem(item);
// Move items to an existing stack if possible, otherwise split count items out into a new stack.
// Moving counts manually here, since ContainerStore's restack can't target unequipped stacks.
for (MWWorld::ContainerStoreIterator iter(begin()); iter != end(); ++iter)
{
if (stacks(*iter, item) && !isEquipped(*iter))
{
iter->getRefData().setCount(addItems(iter->getRefData().getCount(false), count));
item.getRefData().setCount(subtractItems(item.getRefData().getCount(false), count));
return iter;
}
}
return unstack(item, item.getRefData().getCount() - count);
}
MWWorld::InventoryStoreListener* MWWorld::InventoryStore::getInvListener() const
2015-06-07 16:50:34 +02:00
{
return mInventoryListener;
2015-06-07 16:50:34 +02:00
}
void MWWorld::InventoryStore::setInvListener(InventoryStoreListener* listener)
2013-10-25 22:16:52 +02:00
{
mInventoryListener = listener;
}
void MWWorld::InventoryStore::fireEquipmentChangedEvent()
{
if (!mUpdatesEnabled)
return;
if (mInventoryListener)
mInventoryListener->equipmentChanged();
// if player, update inventory window
2015-04-19 02:39:10 +02:00
/*
if (mActor == MWMechanics::getPlayer())
{
MWBase::Environment::get().getWindowManager()->getInventoryWindow()->updateItemView();
}
2015-04-19 02:39:10 +02:00
*/
2013-10-25 22:16:52 +02:00
}
void MWWorld::InventoryStore::clear()
{
mSlots.clear();
initSlots(mSlots);
ContainerStore::clear();
}
bool MWWorld::InventoryStore::isEquipped(const MWWorld::ConstPtr& item)
{
for (int i = 0; i < MWWorld::InventoryStore::Slots; ++i)
{
if (getSlot(i) != end() && *getSlot(i) == item)
return true;
}
return false;
}
bool MWWorld::InventoryStore::isFirstEquip()
{
bool first = mFirstAutoEquip;
mFirstAutoEquip = false;
return first;
}