2012-05-16 14:08:55 +00:00
|
|
|
#include "actionequip.hpp"
|
|
|
|
|
|
|
|
#include "../mwbase/environment.hpp"
|
2012-07-03 10:30:50 +00:00
|
|
|
#include "../mwbase/world.hpp"
|
2013-01-09 03:03:14 +00:00
|
|
|
#include "../mwbase/windowmanager.hpp"
|
2012-07-03 10:30:50 +00:00
|
|
|
|
2015-08-21 09:12:39 +00:00
|
|
|
#include "../mwmechanics/actorutil.hpp"
|
|
|
|
|
2013-01-31 00:34:16 +00:00
|
|
|
#include <components/compiler/locals.hpp>
|
2012-07-03 10:30:50 +00:00
|
|
|
|
|
|
|
#include "inventorystore.hpp"
|
|
|
|
#include "player.hpp"
|
|
|
|
#include "class.hpp"
|
2012-05-16 14:08:55 +00:00
|
|
|
|
|
|
|
namespace MWWorld
|
|
|
|
{
|
2012-09-04 13:27:10 +00:00
|
|
|
ActionEquip::ActionEquip (const MWWorld::Ptr& object) : Action (false, object)
|
2012-05-16 14:08:55 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-07-27 10:00:10 +00:00
|
|
|
void ActionEquip::executeImp (const Ptr& actor)
|
2012-05-16 14:08:55 +00:00
|
|
|
{
|
2013-04-09 14:14:08 +00:00
|
|
|
MWWorld::Ptr object = getTarget();
|
2014-01-19 10:42:58 +00:00
|
|
|
MWWorld::InventoryStore& invStore = actor.getClass().getInventoryStore(actor);
|
2012-05-16 14:08:55 +00:00
|
|
|
|
2014-01-19 10:42:58 +00:00
|
|
|
std::pair <int, std::string> result = object.getClass().canBeEquipped (object, actor);
|
2013-04-15 00:56:23 +00:00
|
|
|
|
|
|
|
// display error message if the player tried to equip something
|
2015-08-21 09:12:39 +00:00
|
|
|
if (!result.second.empty() && actor == MWMechanics::getPlayer())
|
2013-04-15 00:56:23 +00:00
|
|
|
MWBase::Environment::get().getWindowManager()->messageBox(result.second);
|
|
|
|
|
|
|
|
switch(result.first)
|
2013-04-09 17:46:27 +00:00
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
return;
|
2014-12-12 15:49:22 +00:00
|
|
|
default:
|
2013-04-09 17:46:27 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2012-05-16 14:08:55 +00:00
|
|
|
// slots that this item can be equipped in
|
2014-05-22 18:37:22 +00:00
|
|
|
std::pair<std::vector<int>, bool> slots_ = getTarget().getClass().getEquipmentSlots(getTarget());
|
2014-06-05 12:54:07 +00:00
|
|
|
if (slots_.first.empty())
|
|
|
|
return;
|
2012-05-16 14:08:55 +00:00
|
|
|
|
|
|
|
// retrieve ContainerStoreIterator to the item
|
|
|
|
MWWorld::ContainerStoreIterator it = invStore.begin();
|
|
|
|
for (; it != invStore.end(); ++it)
|
|
|
|
{
|
2013-04-09 14:14:08 +00:00
|
|
|
if (*it == object)
|
2012-05-16 14:08:55 +00:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-24 21:50:54 +00:00
|
|
|
if (it == invStore.end())
|
|
|
|
{
|
|
|
|
std::stringstream error;
|
|
|
|
error << "ActionEquip can't find item " << object.getCellRef().getRefId();
|
|
|
|
throw std::runtime_error(error.str());
|
|
|
|
}
|
2013-02-02 12:24:28 +00:00
|
|
|
|
2012-05-16 14:08:55 +00:00
|
|
|
// equip the item in the first free slot
|
2014-06-05 12:54:07 +00:00
|
|
|
std::vector<int>::const_iterator slot=slots_.first.begin();
|
|
|
|
for (;slot!=slots_.first.end(); ++slot)
|
2012-05-16 14:08:55 +00:00
|
|
|
{
|
2013-11-17 12:37:19 +00:00
|
|
|
// if the item is equipped already, nothing to do
|
|
|
|
if (invStore.getSlot(*slot) == it)
|
|
|
|
return;
|
2013-04-02 09:42:29 +00:00
|
|
|
|
2014-06-05 12:54:07 +00:00
|
|
|
if (invStore.getSlot(*slot) == invStore.end())
|
2012-05-16 14:08:55 +00:00
|
|
|
{
|
2014-06-05 12:54:07 +00:00
|
|
|
// slot is not occupied
|
2013-08-13 00:06:46 +00:00
|
|
|
invStore.equip(*slot, it, actor);
|
2012-05-16 14:08:55 +00:00
|
|
|
break;
|
|
|
|
}
|
2014-06-05 12:54:07 +00:00
|
|
|
}
|
2012-05-16 14:08:55 +00:00
|
|
|
|
2014-06-05 12:54:07 +00:00
|
|
|
// all slots are occupied -> cycle
|
|
|
|
// move all slots one towards begin(), then equip the item in the slot that is now free
|
|
|
|
if (slot == slots_.first.end())
|
|
|
|
{
|
|
|
|
for (slot=slots_.first.begin();slot!=slots_.first.end(); ++slot)
|
2012-05-16 14:08:55 +00:00
|
|
|
{
|
2014-06-05 12:54:07 +00:00
|
|
|
invStore.unequipSlot(*slot, actor);
|
|
|
|
if (slot+1 != slots_.first.end())
|
|
|
|
invStore.equip(*slot, invStore.getSlot(*(slot+1)), actor);
|
|
|
|
else
|
|
|
|
invStore.equip(*slot, it, actor);
|
2012-05-16 14:08:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|