2010-08-07 13:11:31 +00:00
|
|
|
|
|
|
|
#include "containerextensions.hpp"
|
|
|
|
|
2010-08-07 14:34:49 +00:00
|
|
|
#include <stdexcept>
|
|
|
|
|
2012-12-27 15:28:13 +00:00
|
|
|
#include <boost/format.hpp>
|
|
|
|
|
|
|
|
#include <MyGUI_LanguageManager.h>
|
|
|
|
|
2010-08-07 13:11:31 +00:00
|
|
|
#include <components/compiler/extensions.hpp>
|
2013-08-07 00:38:41 +00:00
|
|
|
#include <components/compiler/opcodes.hpp>
|
2010-08-07 13:11:31 +00:00
|
|
|
|
|
|
|
#include <components/interpreter/interpreter.hpp>
|
|
|
|
#include <components/interpreter/runtime.hpp>
|
|
|
|
#include <components/interpreter/opcodes.hpp>
|
|
|
|
|
2014-02-23 19:11:05 +00:00
|
|
|
#include <components/esm/loadskil.hpp>
|
|
|
|
|
2012-04-23 13:27:03 +00:00
|
|
|
#include "../mwbase/environment.hpp"
|
2012-12-26 22:57:53 +00:00
|
|
|
#include "../mwbase/windowmanager.hpp"
|
2012-04-23 13:27:03 +00:00
|
|
|
|
2010-08-07 13:11:31 +00:00
|
|
|
#include "../mwworld/class.hpp"
|
2012-02-23 11:34:51 +00:00
|
|
|
#include "../mwworld/containerstore.hpp"
|
2012-11-23 19:48:59 +00:00
|
|
|
#include "../mwworld/actionequip.hpp"
|
|
|
|
#include "../mwworld/inventorystore.hpp"
|
2010-08-07 13:11:31 +00:00
|
|
|
|
|
|
|
#include "interpretercontext.hpp"
|
2010-12-31 18:09:25 +00:00
|
|
|
#include "ref.hpp"
|
2010-08-07 13:11:31 +00:00
|
|
|
|
|
|
|
namespace MWScript
|
|
|
|
{
|
|
|
|
namespace Container
|
|
|
|
{
|
2010-12-31 18:09:25 +00:00
|
|
|
template<class R>
|
2010-08-07 13:11:31 +00:00
|
|
|
class OpAddItem : public Interpreter::Opcode0
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
virtual void execute (Interpreter::Runtime& runtime)
|
|
|
|
{
|
2010-12-31 18:09:25 +00:00
|
|
|
MWWorld::Ptr ptr = R()(runtime);
|
2010-08-07 13:11:31 +00:00
|
|
|
|
|
|
|
std::string item = runtime.getStringLiteral (runtime[0].mInteger);
|
|
|
|
runtime.pop();
|
|
|
|
|
|
|
|
Interpreter::Type_Integer count = runtime[0].mInteger;
|
|
|
|
runtime.pop();
|
|
|
|
|
2010-08-07 14:34:49 +00:00
|
|
|
if (count<0)
|
|
|
|
throw std::runtime_error ("second argument for AddItem must be non-negative");
|
|
|
|
|
2013-02-17 18:44:00 +00:00
|
|
|
// no-op
|
|
|
|
if (count == 0)
|
|
|
|
return;
|
|
|
|
|
2013-11-21 03:11:06 +00:00
|
|
|
MWWorld::Ptr itemPtr = *ptr.getClass().getContainerStore (ptr).add (item, count, ptr);
|
2010-08-07 13:11:31 +00:00
|
|
|
|
2013-05-09 21:19:04 +00:00
|
|
|
// Spawn a messagebox (only for items added to player's inventory and if player is talking to someone)
|
2014-01-08 17:39:44 +00:00
|
|
|
if (ptr == MWBase::Environment::get().getWorld ()->getPlayerPtr() )
|
2013-02-17 17:58:54 +00:00
|
|
|
{
|
2013-02-17 18:44:00 +00:00
|
|
|
// The two GMST entries below expand to strings informing the player of what, and how many of it has been added to their inventory
|
|
|
|
std::string msgBox;
|
2013-11-21 03:11:06 +00:00
|
|
|
std::string itemName = itemPtr.getClass().getName(itemPtr);
|
2013-02-17 18:44:00 +00:00
|
|
|
if (count == 1)
|
|
|
|
{
|
|
|
|
msgBox = MyGUI::LanguageManager::getInstance().replaceTags("#{sNotifyMessage60}");
|
|
|
|
msgBox = boost::str(boost::format(msgBox) % itemName);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
msgBox = MyGUI::LanguageManager::getInstance().replaceTags("#{sNotifyMessage61}");
|
|
|
|
msgBox = boost::str(boost::format(msgBox) % count % itemName);
|
|
|
|
}
|
2013-05-09 21:19:04 +00:00
|
|
|
std::vector <std::string> noButtons;
|
2014-02-11 17:26:57 +00:00
|
|
|
MWBase::Environment::get().getWindowManager()->messageBox(msgBox, noButtons, MWGui::ShowInDialogueMode_Only);
|
2013-02-17 18:44:00 +00:00
|
|
|
}
|
2010-08-07 13:11:31 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2010-12-31 18:09:25 +00:00
|
|
|
template<class R>
|
2010-08-07 14:21:07 +00:00
|
|
|
class OpGetItemCount : public Interpreter::Opcode0
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
virtual void execute (Interpreter::Runtime& runtime)
|
|
|
|
{
|
2010-12-31 18:09:25 +00:00
|
|
|
MWWorld::Ptr ptr = R()(runtime);
|
2010-08-07 14:21:07 +00:00
|
|
|
|
|
|
|
std::string item = runtime.getStringLiteral (runtime[0].mInteger);
|
|
|
|
runtime.pop();
|
|
|
|
|
2014-01-08 21:58:36 +00:00
|
|
|
MWWorld::ContainerStore& store = ptr.getClass().getContainerStore (ptr);
|
2010-08-07 14:21:07 +00:00
|
|
|
|
2014-01-08 21:58:36 +00:00
|
|
|
runtime.push (store.count(item));
|
2010-08-07 14:21:07 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2010-12-31 18:09:25 +00:00
|
|
|
template<class R>
|
2010-08-07 15:00:04 +00:00
|
|
|
class OpRemoveItem : public Interpreter::Opcode0
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
virtual void execute (Interpreter::Runtime& runtime)
|
|
|
|
{
|
2010-12-31 18:09:25 +00:00
|
|
|
MWWorld::Ptr ptr = R()(runtime);
|
2010-08-07 15:00:04 +00:00
|
|
|
|
|
|
|
std::string item = runtime.getStringLiteral (runtime[0].mInteger);
|
|
|
|
runtime.pop();
|
|
|
|
|
|
|
|
Interpreter::Type_Integer count = runtime[0].mInteger;
|
|
|
|
runtime.pop();
|
2013-02-17 18:44:00 +00:00
|
|
|
|
2010-08-07 15:00:04 +00:00
|
|
|
if (count<0)
|
|
|
|
throw std::runtime_error ("second argument for RemoveItem must be non-negative");
|
|
|
|
|
2013-02-17 18:44:00 +00:00
|
|
|
// no-op
|
|
|
|
if (count == 0)
|
|
|
|
return;
|
|
|
|
|
2012-02-23 11:34:51 +00:00
|
|
|
MWWorld::ContainerStore& store = MWWorld::Class::get (ptr).getContainerStore (ptr);
|
2013-08-12 23:19:33 +00:00
|
|
|
|
2013-11-15 01:08:36 +00:00
|
|
|
std::string itemName;
|
|
|
|
for (MWWorld::ContainerStoreIterator iter(store.begin()); iter != store.end(); ++iter)
|
2014-03-16 22:38:51 +00:00
|
|
|
if (::Misc::StringUtils::ciEqual(iter->getCellRef().mRefID, item))
|
2013-11-15 01:08:36 +00:00
|
|
|
itemName = iter->getClass().getName(*iter);
|
2013-02-17 18:44:00 +00:00
|
|
|
|
2013-08-12 23:19:33 +00:00
|
|
|
int numRemoved = store.remove(item, count, ptr);
|
2010-08-07 15:00:04 +00:00
|
|
|
|
2013-05-09 21:19:04 +00:00
|
|
|
// Spawn a messagebox (only for items removed from player's inventory)
|
2013-08-12 23:19:33 +00:00
|
|
|
if ((numRemoved > 0)
|
2014-01-08 17:39:44 +00:00
|
|
|
&& (ptr == MWBase::Environment::get().getWorld()->getPlayerPtr()))
|
2012-12-27 15:28:13 +00:00
|
|
|
{
|
2013-02-17 18:44:00 +00:00
|
|
|
// The two GMST entries below expand to strings informing the player of what, and how many of it has been removed from their inventory
|
|
|
|
std::string msgBox;
|
2013-08-12 23:19:33 +00:00
|
|
|
|
2013-02-17 18:44:00 +00:00
|
|
|
if(numRemoved > 1)
|
|
|
|
{
|
|
|
|
msgBox = MyGUI::LanguageManager::getInstance().replaceTags("#{sNotifyMessage63}");
|
|
|
|
msgBox = boost::str (boost::format(msgBox) % numRemoved % itemName);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
msgBox = MyGUI::LanguageManager::getInstance().replaceTags("#{sNotifyMessage62}");
|
|
|
|
msgBox = boost::str (boost::format(msgBox) % itemName);
|
|
|
|
}
|
2013-05-09 21:19:04 +00:00
|
|
|
std::vector <std::string> noButtons;
|
2014-02-11 17:26:57 +00:00
|
|
|
MWBase::Environment::get().getWindowManager()->messageBox(msgBox, noButtons, MWGui::ShowInDialogueMode_Only);
|
2013-02-17 18:44:00 +00:00
|
|
|
}
|
2010-08-07 15:00:04 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-11-23 19:48:59 +00:00
|
|
|
template <class R>
|
|
|
|
class OpEquip : public Interpreter::Opcode0
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
virtual void execute(Interpreter::Runtime &runtime)
|
|
|
|
{
|
|
|
|
MWWorld::Ptr ptr = R()(runtime);
|
|
|
|
|
|
|
|
std::string item = runtime.getStringLiteral (runtime[0].mInteger);
|
|
|
|
runtime.pop();
|
|
|
|
|
2014-01-19 10:42:58 +00:00
|
|
|
MWWorld::InventoryStore& invStore = ptr.getClass().getInventoryStore (ptr);
|
2012-11-23 19:48:59 +00:00
|
|
|
MWWorld::ContainerStoreIterator it = invStore.begin();
|
|
|
|
for (; it != invStore.end(); ++it)
|
|
|
|
{
|
2014-03-16 22:38:51 +00:00
|
|
|
if (::Misc::StringUtils::ciEqual(it->getCellRef().mRefID, item))
|
2012-11-23 19:48:59 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (it == invStore.end())
|
|
|
|
throw std::runtime_error("Item to equip not found");
|
|
|
|
|
|
|
|
MWWorld::ActionEquip action (*it);
|
|
|
|
action.execute(ptr);
|
2014-01-18 09:51:52 +00:00
|
|
|
|
|
|
|
if (ptr.getRefData().getHandle() == "player" && !ptr.getClass().getScript(ptr).empty())
|
|
|
|
ptr.getRefData().getLocals().setVarByInt(ptr.getClass().getScript(ptr), "onpcequip", 1);
|
2012-11-23 19:48:59 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-11-24 01:38:10 +00:00
|
|
|
template <class R>
|
|
|
|
class OpGetArmorType : public Interpreter::Opcode0
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
virtual void execute(Interpreter::Runtime &runtime)
|
|
|
|
{
|
|
|
|
MWWorld::Ptr ptr = R()(runtime);
|
|
|
|
|
|
|
|
Interpreter::Type_Integer location = runtime[0].mInteger;
|
|
|
|
runtime.pop();
|
|
|
|
|
|
|
|
int slot;
|
|
|
|
switch (location)
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
slot = MWWorld::InventoryStore::Slot_Helmet;
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
slot = MWWorld::InventoryStore::Slot_Cuirass;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
slot = MWWorld::InventoryStore::Slot_LeftPauldron;
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
slot = MWWorld::InventoryStore::Slot_RightPauldron;
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
slot = MWWorld::InventoryStore::Slot_Greaves;
|
|
|
|
break;
|
|
|
|
case 5:
|
|
|
|
slot = MWWorld::InventoryStore::Slot_Boots;
|
|
|
|
break;
|
|
|
|
case 6:
|
|
|
|
slot = MWWorld::InventoryStore::Slot_LeftGauntlet;
|
|
|
|
break;
|
|
|
|
case 7:
|
|
|
|
slot = MWWorld::InventoryStore::Slot_RightGauntlet;
|
|
|
|
break;
|
|
|
|
case 8:
|
|
|
|
slot = MWWorld::InventoryStore::Slot_CarriedLeft; // shield
|
|
|
|
break;
|
|
|
|
case 9:
|
|
|
|
slot = MWWorld::InventoryStore::Slot_LeftGauntlet;
|
|
|
|
break;
|
|
|
|
case 10:
|
|
|
|
slot = MWWorld::InventoryStore::Slot_RightGauntlet;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw std::runtime_error ("armor index out of range");
|
|
|
|
}
|
|
|
|
|
|
|
|
MWWorld::InventoryStore& invStore = MWWorld::Class::get(ptr).getInventoryStore (ptr);
|
|
|
|
|
|
|
|
MWWorld::ContainerStoreIterator it = invStore.getSlot (slot);
|
2012-11-28 01:48:21 +00:00
|
|
|
if (it == invStore.end() || it->getTypeName () != typeid(ESM::Armor).name())
|
2012-11-24 01:38:10 +00:00
|
|
|
{
|
|
|
|
runtime.push(-1);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
int skill = MWWorld::Class::get(*it).getEquipmentSkill (*it) ;
|
|
|
|
if (skill == ESM::Skill::HeavyArmor)
|
|
|
|
runtime.push(2);
|
|
|
|
else if (skill == ESM::Skill::MediumArmor)
|
|
|
|
runtime.push(1);
|
|
|
|
else if (skill == ESM::Skill::LightArmor)
|
|
|
|
runtime.push(0);
|
|
|
|
else
|
|
|
|
runtime.push(-1);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-11-24 01:59:44 +00:00
|
|
|
template <class R>
|
|
|
|
class OpHasItemEquipped : public Interpreter::Opcode0
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
virtual void execute(Interpreter::Runtime &runtime)
|
|
|
|
{
|
|
|
|
MWWorld::Ptr ptr = R()(runtime);
|
|
|
|
|
|
|
|
std::string item = runtime.getStringLiteral (runtime[0].mInteger);
|
|
|
|
runtime.pop();
|
|
|
|
|
|
|
|
MWWorld::InventoryStore& invStore = MWWorld::Class::get(ptr).getInventoryStore (ptr);
|
|
|
|
for (int slot = 0; slot < MWWorld::InventoryStore::Slots; ++slot)
|
|
|
|
{
|
|
|
|
MWWorld::ContainerStoreIterator it = invStore.getSlot (slot);
|
2014-03-16 22:38:51 +00:00
|
|
|
if (it != invStore.end() && ::Misc::StringUtils::ciEqual(it->getCellRef().mRefID, item))
|
2012-11-24 01:59:44 +00:00
|
|
|
{
|
|
|
|
runtime.push(1);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
runtime.push(0);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-11-25 00:54:37 +00:00
|
|
|
template <class R>
|
|
|
|
class OpHasSoulGem : public Interpreter::Opcode0
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
virtual void execute(Interpreter::Runtime &runtime)
|
|
|
|
{
|
|
|
|
MWWorld::Ptr ptr = R()(runtime);
|
2014-02-23 19:11:05 +00:00
|
|
|
|
2014-01-14 05:13:30 +00:00
|
|
|
const std::string &name = runtime.getStringLiteral (runtime[0].mInteger);
|
2012-11-25 00:54:37 +00:00
|
|
|
runtime.pop();
|
|
|
|
|
|
|
|
MWWorld::InventoryStore& invStore = MWWorld::Class::get(ptr).getInventoryStore (ptr);
|
|
|
|
for (MWWorld::ContainerStoreIterator it = invStore.begin(MWWorld::ContainerStore::Type_Miscellaneous);
|
|
|
|
it != invStore.end(); ++it)
|
|
|
|
{
|
|
|
|
|
2014-03-16 22:38:51 +00:00
|
|
|
if (::Misc::StringUtils::ciEqual(it->getCellRef().mSoul, name))
|
2012-11-25 00:54:37 +00:00
|
|
|
{
|
|
|
|
runtime.push(1);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
runtime.push(0);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-11-25 01:06:43 +00:00
|
|
|
template <class R>
|
|
|
|
class OpGetWeaponType : public Interpreter::Opcode0
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
virtual void execute(Interpreter::Runtime &runtime)
|
|
|
|
{
|
|
|
|
MWWorld::Ptr ptr = R()(runtime);
|
|
|
|
|
|
|
|
MWWorld::InventoryStore& invStore = MWWorld::Class::get(ptr).getInventoryStore (ptr);
|
|
|
|
MWWorld::ContainerStoreIterator it = invStore.getSlot (MWWorld::InventoryStore::Slot_CarriedRight);
|
2012-11-28 01:51:46 +00:00
|
|
|
if (it == invStore.end() || it->getTypeName () != typeid(ESM::Weapon).name())
|
2012-11-25 01:06:43 +00:00
|
|
|
{
|
|
|
|
runtime.push(-1);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
runtime.push(it->get<ESM::Weapon>()->mBase->mData.mType);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2010-08-07 13:11:31 +00:00
|
|
|
|
|
|
|
void installOpcodes (Interpreter::Interpreter& interpreter)
|
|
|
|
{
|
2013-08-07 00:38:41 +00:00
|
|
|
interpreter.installSegment5 (Compiler::Container::opcodeAddItem, new OpAddItem<ImplicitRef>);
|
|
|
|
interpreter.installSegment5 (Compiler::Container::opcodeAddItemExplicit, new OpAddItem<ExplicitRef>);
|
|
|
|
interpreter.installSegment5 (Compiler::Container::opcodeGetItemCount, new OpGetItemCount<ImplicitRef>);
|
|
|
|
interpreter.installSegment5 (Compiler::Container::opcodeGetItemCountExplicit, new OpGetItemCount<ExplicitRef>);
|
|
|
|
interpreter.installSegment5 (Compiler::Container::opcodeRemoveItem, new OpRemoveItem<ImplicitRef>);
|
|
|
|
interpreter.installSegment5 (Compiler::Container::opcodeRemoveItemExplicit, new OpRemoveItem<ExplicitRef>);
|
|
|
|
interpreter.installSegment5 (Compiler::Container::opcodeEquip, new OpEquip<ImplicitRef>);
|
|
|
|
interpreter.installSegment5 (Compiler::Container::opcodeEquipExplicit, new OpEquip<ExplicitRef>);
|
|
|
|
interpreter.installSegment5 (Compiler::Container::opcodeGetArmorType, new OpGetArmorType<ImplicitRef>);
|
|
|
|
interpreter.installSegment5 (Compiler::Container::opcodeGetArmorTypeExplicit, new OpGetArmorType<ExplicitRef>);
|
|
|
|
interpreter.installSegment5 (Compiler::Container::opcodeHasItemEquipped, new OpHasItemEquipped<ImplicitRef>);
|
|
|
|
interpreter.installSegment5 (Compiler::Container::opcodeHasItemEquippedExplicit, new OpHasItemEquipped<ExplicitRef>);
|
|
|
|
interpreter.installSegment5 (Compiler::Container::opcodeHasSoulGem, new OpHasSoulGem<ImplicitRef>);
|
|
|
|
interpreter.installSegment5 (Compiler::Container::opcodeHasSoulGemExplicit, new OpHasSoulGem<ExplicitRef>);
|
|
|
|
interpreter.installSegment5 (Compiler::Container::opcodeGetWeaponType, new OpGetWeaponType<ImplicitRef>);
|
|
|
|
interpreter.installSegment5 (Compiler::Container::opcodeGetWeaponTypeExplicit, new OpGetWeaponType<ExplicitRef>);
|
2010-08-07 13:11:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|