Bound items: store item ID instead of pointer

0.6.3
Andrei Kortunov 7 years ago
parent 4de9d9fa77
commit f977c6876f

@ -262,7 +262,7 @@ namespace MWMechanics
MWBase::Environment::get().getWorld()->getPlayer().setDrawState(MWMechanics::DrawState_Weapon);
if (prevItem != store.end())
mPreviousItems[itemId] = *prevItem;
mPreviousItems[itemId] = (*prevItem).getCellRef().getRefId();
}
void Actors::removeBoundItem (const std::string& itemId, const MWWorld::Ptr& actor)
@ -279,26 +279,34 @@ namespace MWMechanics
if (actor != MWMechanics::getPlayer())
return;
MWWorld::Ptr prevItem = mPreviousItems[itemId];
std::string prevItemId = mPreviousItems[itemId];
mPreviousItems.erase(itemId);
if (prevItem.isEmpty())
if (prevItemId.empty())
return;
// check if the item is still in the player's inventory
MWWorld::ContainerStoreIterator it = store.begin();
for (; it != store.end(); ++it)
// Find the item by id
MWWorld::Ptr item;
for (MWWorld::ContainerStoreIterator iter = store.begin(); iter != store.end(); ++iter)
{
if (*it == prevItem)
break;
if (Misc::StringUtils::ciEqual(iter->getCellRef().getRefId(), prevItemId))
{
if (item.isEmpty() ||
// Prefer the stack with the lowest remaining uses
!item.getClass().hasItemHealth(*iter) ||
iter->getClass().getItemHealth(*iter) < item.getClass().getItemHealth(item))
{
item = *iter;
}
}
}
// we should equip previous item only if expired bound item was equipped.
if (it == store.end() || !wasEquipped)
if (item.isEmpty() || !wasEquipped)
return;
MWWorld::ActionEquip action(prevItem);
MWWorld::ActionEquip action(item);
action.execute(actor);
}

@ -25,7 +25,7 @@ namespace MWMechanics
class Actors
{
std::map<std::string, int> mDeathCount;
typedef std::map<std::string, MWWorld::Ptr> PreviousItems;
typedef std::map<std::string, std::string> PreviousItems;
PreviousItems mPreviousItems;
void addBoundItem (const std::string& itemId, const MWWorld::Ptr& actor);

Loading…
Cancel
Save