From 8134c5b7602b5fe31c4d59082b707683c6763732 Mon Sep 17 00:00:00 2001 From: Marc Zinnschlag Date: Sat, 10 Jul 2010 13:19:04 +0200 Subject: [PATCH] integrated CellStore pointer into Ptr class --- apps/openmw/mwscript/interpretercontext.cpp | 27 ++-- apps/openmw/mwscript/interpretercontext.hpp | 8 +- apps/openmw/mwscript/soundextensions.cpp | 15 +- apps/openmw/mwworld/ptr.hpp | 16 +- apps/openmw/mwworld/world.cpp | 158 +++++++------------- apps/openmw/mwworld/world.hpp | 19 +-- 6 files changed, 99 insertions(+), 144 deletions(-) diff --git a/apps/openmw/mwscript/interpretercontext.cpp b/apps/openmw/mwscript/interpretercontext.cpp index a0f1d9d95..239c558bd 100644 --- a/apps/openmw/mwscript/interpretercontext.cpp +++ b/apps/openmw/mwscript/interpretercontext.cpp @@ -16,11 +16,9 @@ namespace MWScript { - InterpreterContext::PtrWithCell InterpreterContext::getReference ( + MWWorld::Ptr InterpreterContext::getReference ( const std::string& id, bool activeOnly) { - PtrWithCell ref; - if (!id.empty()) { return mEnvironment.mWorld->getPtr (id, activeOnly); @@ -30,15 +28,13 @@ namespace MWScript if (mReference.isEmpty()) throw std::runtime_error ("no implicit reference"); - return PtrWithCell (mReference, mEnvironment.mWorld->find (mReference)); + return mReference; } } - InterpreterContext::CPtrWithCell InterpreterContext::getReference ( + const MWWorld::Ptr InterpreterContext::getReference ( const std::string& id, bool activeOnly) const { - CPtrWithCell ref; - if (!id.empty()) { return mEnvironment.mWorld->getPtr (id, activeOnly); @@ -48,7 +44,7 @@ namespace MWScript if (mReference.isEmpty()) throw std::runtime_error ("no implicit reference"); - return CPtrWithCell (mReference, mEnvironment.mWorld->find (mReference)); + return mReference; } } @@ -179,15 +175,14 @@ namespace MWScript float InterpreterContext::getDistance (const std::string& name, const std::string& id) const { // TODO handle exterior cells (when ref and ref2 are located in different cells) - CPtrWithCell ref2 = getReference (id, false); + const MWWorld::Ptr ref2 = getReference (id, false); - std::pair ref = - mEnvironment.mWorld->getPtr (name, true); + const MWWorld::Ptr ref = mEnvironment.mWorld->getPtr (name, true); double diff[3]; for (int i=0; i<3; ++i) - diff[i] = ref.first.getCellRef().pos.pos[i] - ref2.first.getCellRef().pos.pos[i]; + diff[i] = ref.getCellRef().pos.pos[i] - ref2.getCellRef().pos.pos[i]; return std::sqrt (diff[0]*diff[0] + diff[1]*diff[1] + diff[2]*diff[2]); } @@ -204,19 +199,19 @@ namespace MWScript bool InterpreterContext::isDisabled (const std::string& id) const { - CPtrWithCell ref = getReference (id, false); - return !ref.first.getRefData().isEnabled(); + const MWWorld::Ptr ref = getReference (id, false); + return !ref.getRefData().isEnabled(); } void InterpreterContext::enable (const std::string& id) { - PtrWithCell ref = getReference (id, false); + MWWorld::Ptr ref = getReference (id, false); mEnvironment.mWorld->enable (ref); } void InterpreterContext::disable (const std::string& id) { - PtrWithCell ref = getReference (id, false); + MWWorld::Ptr ref = getReference (id, false); mEnvironment.mWorld->disable (ref); } diff --git a/apps/openmw/mwscript/interpretercontext.hpp b/apps/openmw/mwscript/interpretercontext.hpp index 14c0a9e85..ac719f151 100644 --- a/apps/openmw/mwscript/interpretercontext.hpp +++ b/apps/openmw/mwscript/interpretercontext.hpp @@ -22,12 +22,10 @@ namespace MWScript Locals *mLocals; MWWorld::Ptr mReference; - typedef std::pair PtrWithCell; - typedef std::pair CPtrWithCell; - PtrWithCell getReference (const std::string& id, bool activeOnly); - - CPtrWithCell getReference (const std::string& id, bool activeOnly) const; + MWWorld::Ptr getReference (const std::string& id, bool activeOnly); + + const MWWorld::Ptr getReference (const std::string& id, bool activeOnly) const; public: diff --git a/apps/openmw/mwscript/soundextensions.cpp b/apps/openmw/mwscript/soundextensions.cpp index 4a6be48b5..d7aada989 100644 --- a/apps/openmw/mwscript/soundextensions.cpp +++ b/apps/openmw/mwscript/soundextensions.cpp @@ -205,7 +205,7 @@ namespace MWScript std::string text = runtime.getStringLiteral (runtime[0]); runtime.pop(); - context.getSoundManager().say (context.getWorld().getPtr (id, true).first, + context.getSoundManager().say (context.getWorld().getPtr (id, true), file, text, context); } }; @@ -223,8 +223,7 @@ namespace MWScript runtime.pop(); runtime.push (context.getSoundManager().sayDone ( - context.getWorld().getPtr (id, true).first, - context)); + context.getWorld().getPtr (id, true), context)); } }; @@ -248,8 +247,7 @@ namespace MWScript runtime.pop(); context.getSoundManager().playSound3D ( - context.getWorld().getPtr (id, true).first, sound, - 1.0, 1.0, mLoop, context); + context.getWorld().getPtr (id, true), sound, 1.0, 1.0, mLoop, context); } }; @@ -279,8 +277,7 @@ namespace MWScript runtime.pop(); context.getSoundManager().playSound3D ( - context.getWorld().getPtr (id, true).first, sound, volume, - pitch, mLoop, context); + context.getWorld().getPtr (id, true), sound, volume, pitch, mLoop, context); } }; @@ -301,7 +298,7 @@ namespace MWScript runtime.pop(); context.getSoundManager().stopSound3D ( - context.getWorld().getPtr (id, true).first, sound, context); + context.getWorld().getPtr (id, true), sound, context); } }; @@ -321,7 +318,7 @@ namespace MWScript runtime.pop(); runtime.push (context.getSoundManager().getSoundPlaying ( - context.getWorld().getPtr (id, true).first, + context.getWorld().getPtr (id, true), runtime.getStringLiteral (index), context)); } }; diff --git a/apps/openmw/mwworld/ptr.hpp b/apps/openmw/mwworld/ptr.hpp index 9d031512d..a65fcb7cc 100644 --- a/apps/openmw/mwworld/ptr.hpp +++ b/apps/openmw/mwworld/ptr.hpp @@ -15,13 +15,18 @@ namespace MWWorld class Ptr { + public: + + typedef ESMS::CellStore CellStore; + boost::any mPtr; ESM::CellRef *mCellRef; RefData *mRefData; + CellStore *mCell; public: - Ptr() : mCellRef (0), mRefData (0) {} + Ptr() : mCellRef (0), mRefData (0), mCell (0) {} bool isEmpty() const { @@ -29,11 +34,12 @@ namespace MWWorld } template - Ptr (ESMS::LiveCellRef *liveCellRef) + Ptr (ESMS::LiveCellRef *liveCellRef, CellStore *cell) { mPtr = liveCellRef; mCellRef = &liveCellRef->ref; mRefData = &liveCellRef->mData; + mCell = cell; } template @@ -53,6 +59,12 @@ namespace MWWorld assert (mRefData); return *mRefData; } + + Ptr::CellStore *getCell() const + { + assert (mCell); + return mCell; + } }; } diff --git a/apps/openmw/mwworld/world.cpp b/apps/openmw/mwworld/world.cpp index 8fb635ef3..840a88d7d 100644 --- a/apps/openmw/mwworld/world.cpp +++ b/apps/openmw/mwworld/world.cpp @@ -15,7 +15,8 @@ namespace { template void listCellScripts (const ESMS::ESMStore& store, - ESMS::CellRefList& cellRefList, MWWorld::World::ScriptList& scriptList) + ESMS::CellRefList& cellRefList, MWWorld::World::ScriptList& scriptList, + MWWorld::Ptr::CellStore *cell) { for (typename ESMS::CellRefList::List::iterator iter ( cellRefList.list.begin()); @@ -28,116 +29,102 @@ namespace iter->mData.setLocals (*script); scriptList.push_back ( - std::make_pair (iter->base->script, MWWorld::Ptr (&*iter))); + std::make_pair (iter->base->script, MWWorld::Ptr (&*iter, cell))); } } } } - - template - bool hasReference (ESMS::CellRefList& cellRefList, const MWWorld::Ptr& ptr) - { - for (typename ESMS::CellRefList::List::iterator iter ( - cellRefList.list.begin()); - iter!=cellRefList.list.end(); ++iter) - { - if (&iter->mData==&ptr.getRefData()) - return true; - } - - return false; - } } namespace MWWorld { void World::insertInteriorScripts (ESMS::CellStore& cell) { - listCellScripts (mStore, cell.activators, mLocalScripts); - listCellScripts (mStore, cell.potions, mLocalScripts); - listCellScripts (mStore, cell.appas, mLocalScripts); - listCellScripts (mStore, cell.armors, mLocalScripts); - listCellScripts (mStore, cell.books, mLocalScripts); - listCellScripts (mStore, cell.clothes, mLocalScripts); - listCellScripts (mStore, cell.containers, mLocalScripts); - listCellScripts (mStore, cell.creatures, mLocalScripts); - listCellScripts (mStore, cell.doors, mLocalScripts); - listCellScripts (mStore, cell.ingreds, mLocalScripts); - listCellScripts (mStore, cell.lights, mLocalScripts); - listCellScripts (mStore, cell.lockpicks, mLocalScripts); - listCellScripts (mStore, cell.miscItems, mLocalScripts); - listCellScripts (mStore, cell.npcs, mLocalScripts); - listCellScripts (mStore, cell.probes, mLocalScripts); - listCellScripts (mStore, cell.repairs, mLocalScripts); - listCellScripts (mStore, cell.weapons, mLocalScripts); + listCellScripts (mStore, cell.activators, mLocalScripts, &cell); + listCellScripts (mStore, cell.potions, mLocalScripts, &cell); + listCellScripts (mStore, cell.appas, mLocalScripts, &cell); + listCellScripts (mStore, cell.armors, mLocalScripts, &cell); + listCellScripts (mStore, cell.books, mLocalScripts, &cell); + listCellScripts (mStore, cell.clothes, mLocalScripts, &cell); + listCellScripts (mStore, cell.containers, mLocalScripts, &cell); + listCellScripts (mStore, cell.creatures, mLocalScripts, &cell); + listCellScripts (mStore, cell.doors, mLocalScripts, &cell); + listCellScripts (mStore, cell.ingreds, mLocalScripts, &cell); + listCellScripts (mStore, cell.lights, mLocalScripts, &cell); + listCellScripts (mStore, cell.lockpicks, mLocalScripts, &cell); + listCellScripts (mStore, cell.miscItems, mLocalScripts, &cell); + listCellScripts (mStore, cell.npcs, mLocalScripts, &cell); + listCellScripts (mStore, cell.probes, mLocalScripts, &cell); + listCellScripts (mStore, cell.repairs, mLocalScripts, &cell); + listCellScripts (mStore, cell.weapons, mLocalScripts, &cell); } - Ptr World::getPtr (const std::string& name, CellStore& cell) + Ptr World::getPtr (const std::string& name, Ptr::CellStore& cell) { if (ESMS::LiveCellRef *ref = cell.activators.find (name)) - return ref; + return Ptr (ref, &cell); if (ESMS::LiveCellRef *ref = cell.potions.find (name)) - return ref; + return Ptr (ref, &cell); if (ESMS::LiveCellRef *ref = cell.appas.find (name)) - return ref; + return Ptr (ref, &cell); if (ESMS::LiveCellRef *ref = cell.armors.find (name)) - return ref; + return Ptr (ref, &cell); if (ESMS::LiveCellRef *ref = cell.books.find (name)) - return ref; + return Ptr (ref, &cell); if (ESMS::LiveCellRef *ref = cell.clothes.find (name)) - return ref; + return Ptr (ref, &cell); if (ESMS::LiveCellRef *ref = cell.containers.find (name)) - return ref; + return Ptr (ref, &cell); if (ESMS::LiveCellRef *ref = cell.creatures.find (name)) - return ref; + return Ptr (ref, &cell); if (ESMS::LiveCellRef *ref = cell.doors.find (name)) - return ref; + return Ptr (ref, &cell); if (ESMS::LiveCellRef *ref = cell.ingreds.find (name)) - return ref; + return Ptr (ref, &cell); if (ESMS::LiveCellRef *ref = cell.creatureLists.find (name)) - return ref; + return Ptr (ref, &cell); if (ESMS::LiveCellRef *ref = cell.itemLists.find (name)) - return ref; + return Ptr (ref, &cell); if (ESMS::LiveCellRef *ref = cell.lights.find (name)) - return ref; + return Ptr (ref, &cell); if (ESMS::LiveCellRef *ref = cell.lockpicks.find (name)) - return ref; + return Ptr (ref, &cell); if (ESMS::LiveCellRef *ref = cell.miscItems.find (name)) - return ref; + return Ptr (ref, &cell); if (ESMS::LiveCellRef *ref = cell.npcs.find (name)) - return ref; + return Ptr (ref, &cell); if (ESMS::LiveCellRef *ref = cell.probes.find (name)) - return ref; + return Ptr (ref, &cell); if (ESMS::LiveCellRef *ref = cell.repairs.find (name)) - return ref; + return Ptr (ref, &cell); if (ESMS::LiveCellRef *ref = cell.statics.find (name)) - return ref; + return Ptr (ref, &cell); if (ESMS::LiveCellRef *ref = cell.weapons.find (name)) - return ref; + return Ptr (ref, &cell); return Ptr(); } - MWRender::CellRender *World::searchRender (CellStore *store) + MWRender::CellRender *World::searchRender (Ptr::CellStore *store) { CellRenderCollection::iterator iter = mActiveCells.find (store); @@ -157,7 +144,7 @@ namespace MWWorld World::World (Render::OgreRenderer& renderer, const boost::filesystem::path& dataDir, const std::string& master, const std::string& startCell, bool newGame) - : mSkyManager (0), mScene (renderer), mPlayerPos (0) + : mSkyManager (0), mScene (renderer), mPlayerPos (0), mCurrentCell (0) { boost::filesystem::path masterPath (dataDir); masterPath /= master; @@ -169,6 +156,7 @@ namespace MWWorld mStore.load (mEsm); mInteriors[startCell].loadInt (startCell, mStore, mEsm); + mCurrentCell = &mInteriors[startCell]; insertInteriorScripts (mInteriors[startCell]); @@ -251,15 +239,12 @@ namespace MWWorld return iter->second; } - std::pair World::getPtr (const std::string& name, bool activeOnly) + Ptr World::getPtr (const std::string& name, bool activeOnly) { // the player is always in an active cell. if (name=="player") { - // TODO: find real cell (might need to be stored in playerPos). For now we - // use the first active cell. This will fail the moment we move into an - // exterior cell. - return std::make_pair (mPlayerPos->getPlayer(), mActiveCells.begin()->first); + return Ptr (mPlayerPos->getPlayer(), mCurrentCell); } // active cells @@ -269,7 +254,7 @@ namespace MWWorld Ptr ptr = getPtr (name, *iter->first); if (!ptr.isEmpty()) - return std::make_pair (ptr, iter->first); + return ptr; } if (!activeOnly) @@ -280,58 +265,29 @@ namespace MWWorld throw std::runtime_error ("unknown ID: " + name); } - void World::enable (std::pair& reference) + void World::enable (Ptr reference) { - if (!reference.first.getRefData().isEnabled()) + if (!reference.getRefData().isEnabled()) { - reference.first.getRefData().enable(); + reference.getRefData().enable(); - if (MWRender::CellRender *render = searchRender (reference.second)) + if (MWRender::CellRender *render = searchRender (reference.getCell())) { - render->enable (reference.first.getRefData().getHandle()); + render->enable (reference.getRefData().getHandle()); } } } - void World::disable (std::pair& reference) + void World::disable (Ptr reference) { - if (!reference.first.getRefData().isEnabled()) + if (!reference.getRefData().isEnabled()) { - reference.first.getRefData().enable(); + reference.getRefData().enable(); - if (MWRender::CellRender *render = searchRender (reference.second)) + if (MWRender::CellRender *render = searchRender (reference.getCell())) { - render->disable (reference.first.getRefData().getHandle()); + render->disable (reference.getRefData().getHandle()); } } } - - World::CellStore *World::find (const Ptr& ptr) - { - for (CellRenderCollection::iterator iter (mActiveCells.begin()); iter!=mActiveCells.end(); - ++iter) - { - if ( - hasReference (iter->first->activators, ptr) || - hasReference (iter->first->potions, ptr) || - hasReference (iter->first->appas, ptr) || - hasReference (iter->first->armors, ptr) || - hasReference (iter->first->books, ptr) || - hasReference (iter->first->clothes, ptr) || - hasReference (iter->first->containers, ptr) || - hasReference (iter->first->creatures, ptr) || - hasReference (iter->first->doors, ptr) || - hasReference (iter->first->ingreds, ptr) || - hasReference (iter->first->lights, ptr) || - hasReference (iter->first->lockpicks, ptr) || - hasReference (iter->first->miscItems, ptr) || - hasReference (iter->first->npcs, ptr) || - hasReference (iter->first->probes, ptr) || - hasReference (iter->first->repairs, ptr) || - hasReference (iter->first->weapons, ptr)) - return iter->first; - } - - throw std::runtime_error ("failed to locate reference in active cell"); - } } diff --git a/apps/openmw/mwworld/world.hpp b/apps/openmw/mwworld/world.hpp index 0c299b9f9..d6df3b826 100644 --- a/apps/openmw/mwworld/world.hpp +++ b/apps/openmw/mwworld/world.hpp @@ -36,20 +36,20 @@ namespace MWWorld public: typedef std::vector > ScriptList; - typedef ESMS::CellStore CellStore; private: - typedef std::map CellRenderCollection; + typedef std::map CellRenderCollection; MWRender::SkyManager* mSkyManager; MWRender::MWScene mScene; MWRender::PlayerPos *mPlayerPos; + Ptr::CellStore *mCurrentCell; // the cell, the player is in CellRenderCollection mActiveCells; CellRenderCollection mBufferedCells; // loaded, but not active (buffering not implementd yet) ESM::ESMReader mEsm; ESMS::ESMStore mStore; - std::map mInteriors; + std::map mInteriors; ScriptList mLocalScripts; std::map mGlobalVariables; @@ -59,9 +59,9 @@ namespace MWWorld void insertInteriorScripts (ESMS::CellStore& cell); - Ptr getPtr (const std::string& name, CellStore& cellStore); + Ptr getPtr (const std::string& name, Ptr::CellStore& cellStore); - MWRender::CellRender *searchRender (CellStore *store); + MWRender::CellRender *searchRender (Ptr::CellStore *store); public: @@ -82,16 +82,13 @@ namespace MWWorld Interpreter::Type_Data& getGlobalVariable (const std::string& name); - std::pair getPtr (const std::string& name, bool activeOnly); + Ptr getPtr (const std::string& name, bool activeOnly); ///< Return a pointer to a liveCellRef with the given name. /// \param activeOnly do non search inactive cells. - void enable (std::pair& reference); + void enable (Ptr reference); - void disable (std::pair& reference); - - CellStore *find (const Ptr& ptr); - ///< Only active cells are searched. + void disable (Ptr reference); }; }