diff --git a/apps/openmw/CMakeLists.txt b/apps/openmw/CMakeLists.txt index 482007090c..f61f5ead28 100644 --- a/apps/openmw/CMakeLists.txt +++ b/apps/openmw/CMakeLists.txt @@ -41,7 +41,7 @@ add_openmw_dir (mwscript locals scriptmanagerimp compilercontext interpretercontext cellextensions miscextensions guiextensions soundextensions skyextensions statsextensions containerextensions aiextensions controlextensions extensions globalscripts ref dialogueextensions - animationextensions transformationextensions consoleextensions userextensions + animationextensions transformationextensions consoleextensions userextensions locals ) add_openmw_dir (mwsound diff --git a/apps/openmw/mwgui/inventorywindow.cpp b/apps/openmw/mwgui/inventorywindow.cpp index ebbd69d80b..40771af166 100644 --- a/apps/openmw/mwgui/inventorywindow.cpp +++ b/apps/openmw/mwgui/inventorywindow.cpp @@ -13,7 +13,6 @@ #include "../mwbase/environment.hpp" #include "../mwbase/soundmanager.hpp" #include "../mwbase/windowmanager.hpp" -#include "../mwbase/scriptmanager.hpp" #include "../mwworld/containerstore.hpp" #include "../mwworld/class.hpp" @@ -245,27 +244,10 @@ namespace MWGui invStore.equip(slot, invStore.end()); std::string script = MWWorld::Class::get(*it).getScript(*it); - /* Unset OnPCEquip Variable on item's script, if it has a script with that variable declared */ + // Unset OnPCEquip Variable on item's script, if it has a script with that variable declared if(script != "") - { - Compiler::Locals locals = MWBase::Environment::get().getScriptManager()->getLocals(script); - int index = locals.getIndex("onpcequip"); - char type = locals.getType("onpcequip"); - if(index != -1) - { - switch(type) - { - case 's': - (*it).mRefData->getLocals().mShorts.at (index) = 0; break; - - case 'l': - (*it).mRefData->getLocals().mLongs.at (index) = 0; break; - - case 'f': - (*it).mRefData->getLocals().mFloats.at (index) = 0.0; break; - } - } - } + (*it).mRefData->getLocals().setVarByInt(script, "onpcequip", 0); + return; } } diff --git a/apps/openmw/mwscript/locals.cpp b/apps/openmw/mwscript/locals.cpp new file mode 100644 index 0000000000..53f744323e --- /dev/null +++ b/apps/openmw/mwscript/locals.cpp @@ -0,0 +1,41 @@ +#include "locals.hpp" + +#include "../mwbase/environment.hpp" +#include "../mwbase/scriptmanager.hpp" +#include + +namespace MWScript +{ + void Locals::configure (const ESM::Script& script) + { + mShorts.clear(); + mShorts.resize (script.mData.mNumShorts, 0); + mLongs.clear(); + mLongs.resize (script.mData.mNumLongs, 0); + mFloats.clear(); + mFloats.resize (script.mData.mNumFloats, 0); + } + + bool Locals::setVarByInt(const std::string& script, const std::string& var, int val) + { + Compiler::Locals locals = MWBase::Environment::get().getScriptManager()->getLocals(script); + int index = locals.getIndex(var); + char type = locals.getType(var); + if(index != -1) + { + switch(type) + { + case 's': + mShorts.at (index) = val; break; + + case 'l': + mLongs.at (index) = val; break; + + case 'f': + mFloats.at (index) = val; break; + } + return true; + } + return false; + } +} diff --git a/apps/openmw/mwscript/locals.hpp b/apps/openmw/mwscript/locals.hpp index ec02e2f126..e933c727f3 100644 --- a/apps/openmw/mwscript/locals.hpp +++ b/apps/openmw/mwscript/locals.hpp @@ -8,21 +8,16 @@ namespace MWScript { - struct Locals + class Locals { - std::vector mShorts; - std::vector mLongs; - std::vector mFloats; + public: + std::vector mShorts; + std::vector mLongs; + std::vector mFloats; + + void configure (const ESM::Script& script); + bool setVarByInt(const std::string& script, const std::string& var, int val); - void configure (const ESM::Script& script) - { - mShorts.clear(); - mShorts.resize (script.mData.mNumShorts, 0); - mLongs.clear(); - mLongs.resize (script.mData.mNumLongs, 0); - mFloats.clear(); - mFloats.resize (script.mData.mNumFloats, 0); - } }; } diff --git a/apps/openmw/mwworld/actionequip.cpp b/apps/openmw/mwworld/actionequip.cpp index 2b238ead99..b1236f829a 100644 --- a/apps/openmw/mwworld/actionequip.cpp +++ b/apps/openmw/mwworld/actionequip.cpp @@ -3,7 +3,6 @@ #include "../mwbase/environment.hpp" #include "../mwbase/world.hpp" #include "../mwbase/windowmanager.hpp" -#include "../mwbase/scriptmanager.hpp" #include @@ -113,24 +112,6 @@ namespace MWWorld /* Set OnPCEquip Variable on item's script, if the player is equipping it, and it has a script with that variable declared */ if(equipped && actor == MWBase::Environment::get().getWorld()->getPlayer().getPlayer() && script != "") - { - Compiler::Locals locals = MWBase::Environment::get().getScriptManager()->getLocals(script); - int index = locals.getIndex("onpcequip"); - char type = locals.getType("onpcequip"); - if(index != -1) - { - switch(type) - { - case 's': - (*it).mRefData->getLocals().mShorts.at (index) = 1; break; - - case 'l': - (*it).mRefData->getLocals().mLongs.at (index) = 1; break; - - case 'f': - (*it).mRefData->getLocals().mFloats.at (index) = 1.0; break; - } - } - } + (*it).mRefData->getLocals().setVarByInt(script, "onpcequip", 1); } } diff --git a/apps/openmw/mwworld/containerstore.cpp b/apps/openmw/mwworld/containerstore.cpp index 31eabb3424..eb2a14d5b8 100644 --- a/apps/openmw/mwworld/containerstore.cpp +++ b/apps/openmw/mwworld/containerstore.cpp @@ -88,27 +88,10 @@ MWWorld::ContainerStoreIterator MWWorld::ContainerStore::add (const Ptr& ptr) if(&(MWWorld::Class::get (player).getContainerStore (player)) == this) { - cell = 0; // Items in players inventory have cell set to 0, so their scripts will never be removed + cell = 0; // Items in player's inventory have cell set to 0, so their scripts will never be removed // Set OnPCAdd special variable, if it is declared - Compiler::Locals locals = MWBase::Environment::get().getScriptManager()->getLocals(script); - int index = locals.getIndex("onpcadd"); - char type = locals.getType("onpcadd"); - - if(index != -1) - { - switch(type) - { - case 's': - item.mRefData->getLocals().mShorts.at (index) = 1; break; - - case 'l': - item.mRefData->getLocals().mLongs.at (index) = 1; break; - - case 'f': - item.mRefData->getLocals().mFloats.at (index) = 1.0; break; - } - } + item.mRefData->getLocals().setVarByInt(script, "onpcadd", 1); } else cell = player.getCell(); diff --git a/apps/openmw/mwworld/worldimp.cpp b/apps/openmw/mwworld/worldimp.cpp index 0c8027975b..2926b76f8b 100644 --- a/apps/openmw/mwworld/worldimp.cpp +++ b/apps/openmw/mwworld/worldimp.cpp @@ -1277,27 +1277,9 @@ namespace MWWorld { std::string script = MWWorld::Class::get(item).getScript(item); - /* Set OnPCDrop Variable on item's script, if it has a script with that variable declared */ + // Set OnPCDrop Variable on item's script, if it has a script with that variable declared if(script != "") - { - Compiler::Locals locals = MWBase::Environment::get().getScriptManager()->getLocals(script); - int index = locals.getIndex("onpcdrop"); - char type = locals.getType("onpcdrop"); - if(index != -1) - { - switch(type) - { - case 's': - item.mRefData->getLocals().mShorts.at (index) = 1; break; - - case 'l': - item.mRefData->getLocals().mLongs.at (index) = 1; break; - - case 'f': - item.mRefData->getLocals().mFloats.at (index) = 1.0; break; - } - } - } + item.mRefData->getLocals().setVarByInt(script, "onpcdrop", 1); } bool World::placeObject (const Ptr& object, float cursorX, float cursorY)