diff --git a/CMakeLists.txt b/CMakeLists.txt index ab7b531c2e..815a3913eb 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -61,7 +61,8 @@ set(GAMEWORLD apps/openmw/mwworld/world.cpp) set(GAMEWORLD_HEADER apps/openmw/mwworld/refdata.hpp - apps/openmw/mwworld/world.hpp) + apps/openmw/mwworld/world.hpp + apps/openmw/mwworld/ptr.hpp) source_group(apps\\openmw\\mwworld FILES ${GAMEWORLD} ${GAMEWORLD_HEADER}) diff --git a/apps/openmw/engine.cpp b/apps/openmw/engine.cpp index f865bb54cb..e801850978 100644 --- a/apps/openmw/engine.cpp +++ b/apps/openmw/engine.cpp @@ -17,13 +17,15 @@ #include "mwsound/soundmanager.hpp" #include "mwworld/world.hpp" +#include "mwworld/ptr.hpp" void OMW::Engine::executeLocalScripts() { for (MWWorld::World::ScriptList::const_iterator iter (mWorld->getLocalScripts().begin()); iter!=mWorld->getLocalScripts().end(); ++iter) { - MWScript::InterpreterContext interpreterContext (*mWorld, *mSoundManager, iter->second); + MWScript::InterpreterContext interpreterContext (*mWorld, *mSoundManager, + &iter->second.getRefData().getLocals(), MWWorld::Ptr (iter->second)); mScriptManager->run (iter->first, interpreterContext); } } diff --git a/apps/openmw/mwscript/interpretercontext.cpp b/apps/openmw/mwscript/interpretercontext.cpp index 701c7083e5..2d55fb9898 100644 --- a/apps/openmw/mwscript/interpretercontext.cpp +++ b/apps/openmw/mwscript/interpretercontext.cpp @@ -10,8 +10,8 @@ namespace MWScript { InterpreterContext::InterpreterContext (MWWorld::World& world, - MWSound::SoundManager& soundManager, MWScript::Locals *locals) - : mWorld (world), mSoundManager (soundManager), mLocals (locals) + MWSound::SoundManager& soundManager, MWScript::Locals *locals, MWWorld::Ptr reference) + : mWorld (world), mSoundManager (soundManager), mLocals (locals), mReference (reference) {} int InterpreterContext::getLocalShort (int index) const diff --git a/apps/openmw/mwscript/interpretercontext.hpp b/apps/openmw/mwscript/interpretercontext.hpp index 2087755540..a7b2ba8290 100644 --- a/apps/openmw/mwscript/interpretercontext.hpp +++ b/apps/openmw/mwscript/interpretercontext.hpp @@ -3,6 +3,8 @@ #include +#include "../mwworld/ptr.hpp" + namespace MWWorld { class World; @@ -22,11 +24,12 @@ namespace MWScript MWWorld::World& mWorld; MWSound::SoundManager& mSoundManager; Locals *mLocals; + MWWorld::Ptr mReference; public: InterpreterContext (MWWorld::World& world, MWSound::SoundManager& soundManager, - MWScript::Locals *locals); + MWScript::Locals *locals, MWWorld::Ptr reference); ///< The ownership of \a locals is not transferred. 0-pointer allowed. virtual int getLocalShort (int index) const; diff --git a/apps/openmw/mwworld/ptr.hpp b/apps/openmw/mwworld/ptr.hpp new file mode 100644 index 0000000000..11ec4baf7e --- /dev/null +++ b/apps/openmw/mwworld/ptr.hpp @@ -0,0 +1,55 @@ +#ifndef GAME_MWWORLD_PTR_H +#define GAME_MWWORLD_PTR_H + +#include + +#include + +#include + +#include "refdata.hpp" + +namespace MWWorld +{ + /// \brief Pointer to a LiveCellRef + + class Ptr + { + boost::any mPtr; + ESM::CellRef *mCellRef; + RefData *mRefData; + + public: + + Ptr() : mCellRef (0), mRefData (0) {} + + template + Ptr (ESMS::LiveCellRef *liveCellRef) + { + mPtr = liveCellRef; + mCellRef = &liveCellRef->ref; + mRefData = &liveCellRef->mData; + } + + template + ESMS::LiveCellRef *get() const + { + return boost::any_cast*> (mPtr); + } + + ESM::CellRef& getCellRef() const + { + assert (mCellRef); + return *mCellRef; + } + + RefData& getRefData() const + { + assert (mRefData); + return *mRefData; + } + }; +} + +#endif + diff --git a/apps/openmw/mwworld/world.cpp b/apps/openmw/mwworld/world.cpp index 2ec6fd54be..aae72e5bc3 100644 --- a/apps/openmw/mwworld/world.cpp +++ b/apps/openmw/mwworld/world.cpp @@ -26,7 +26,7 @@ namespace iter->mData.setLocals (*script); scriptList.push_back ( - std::make_pair (iter->base->script, &iter->mData.getLocals())); + std::make_pair (iter->base->script, MWWorld::Ptr (&*iter))); } } } diff --git a/apps/openmw/mwworld/world.hpp b/apps/openmw/mwworld/world.hpp index f81fb5c213..ce18e9368e 100644 --- a/apps/openmw/mwworld/world.hpp +++ b/apps/openmw/mwworld/world.hpp @@ -12,6 +12,7 @@ #include "../mwrender/mwscene.hpp" #include "refdata.hpp" +#include "ptr.hpp" namespace Render { @@ -32,7 +33,7 @@ namespace MWWorld { public: - typedef std::vector > ScriptList; + typedef std::vector > ScriptList; private: