From 362605860b13a57da86375e71271d96376797cf5 Mon Sep 17 00:00:00 2001 From: Marc Zinnschlag Date: Tue, 27 Jul 2010 20:05:05 +0800 Subject: [PATCH] register actors in active cells with MechanicsManager --- apps/openmw/engine.cpp | 2 +- apps/openmw/mwmechanics/mechanicsmanager.cpp | 18 ++++++++ apps/openmw/mwmechanics/mechanicsmanager.hpp | 15 ++++++- apps/openmw/mwworld/world.cpp | 45 ++++++++++++++++---- apps/openmw/mwworld/world.hpp | 5 ++- 5 files changed, 72 insertions(+), 13 deletions(-) diff --git a/apps/openmw/engine.cpp b/apps/openmw/engine.cpp index e7e682e310..d515fcac17 100644 --- a/apps/openmw/engine.cpp +++ b/apps/openmw/engine.cpp @@ -187,7 +187,7 @@ void OMW::Engine::go() loadBSA(); // Create the world - mEnvironment.mWorld = new MWWorld::World (mOgre, mDataDir, mMaster, mNewGame); + mEnvironment.mWorld = new MWWorld::World (mOgre, mDataDir, mMaster, mNewGame, mEnvironment); // Set up the GUI system mGuiManager = new OEngine::GUI::MyGUIManager(mOgre.getWindow(), diff --git a/apps/openmw/mwmechanics/mechanicsmanager.cpp b/apps/openmw/mwmechanics/mechanicsmanager.cpp index 2d28b03ac5..4287c6a64e 100644 --- a/apps/openmw/mwmechanics/mechanicsmanager.cpp +++ b/apps/openmw/mwmechanics/mechanicsmanager.cpp @@ -39,5 +39,23 @@ namespace MWMechanics mWindowManager.setLabel (names[i][0], label); } } + + void MechanicsManager::addActor (const MWWorld::Ptr& ptr) + { + mActors.insert (ptr); + } + + void MechanicsManager::dropActors (const MWWorld::Ptr::CellStore *cellStore) + { + std::set::iterator iter = mActors.begin(); + + while (iter!=mActors.end()) + if (iter->getCell()==cellStore) + { + mActors.erase (iter++); + } + else + ++iter; + } } diff --git a/apps/openmw/mwmechanics/mechanicsmanager.hpp b/apps/openmw/mwmechanics/mechanicsmanager.hpp index 2ffc580aac..0a46a8f266 100644 --- a/apps/openmw/mwmechanics/mechanicsmanager.hpp +++ b/apps/openmw/mwmechanics/mechanicsmanager.hpp @@ -1,6 +1,10 @@ #ifndef GAME_MWMECHANICS_MECHANICSMANAGER_H #define GAME_MWMECHANICS_MECHANICSMANAGER_H +#include + +#include "../mwworld/ptr.hpp" + namespace ESMS { class ESMStore; @@ -15,14 +19,21 @@ namespace MWMechanics { class MechanicsManager { - const ESMS::ESMStore& mStore; - MWGui::WindowManager& mWindowManager; + const ESMS::ESMStore& mStore; + MWGui::WindowManager& mWindowManager; + std::set mActors; public: MechanicsManager (const ESMS::ESMStore& store, MWGui::WindowManager& windowManager); void configureGUI(); + + void addActor (const MWWorld::Ptr& ptr); + ///< Register an actor for stats management + + void dropActors (const MWWorld::Ptr::CellStore *cellStore); + ///< Deregister all actors in the given cell. }; } diff --git a/apps/openmw/mwworld/world.cpp b/apps/openmw/mwworld/world.cpp index 3c2ea3256a..a3f82417c1 100644 --- a/apps/openmw/mwworld/world.cpp +++ b/apps/openmw/mwworld/world.cpp @@ -4,12 +4,15 @@ #include #include -#include "components/bsa/bsa_archive.hpp" +#include -#include "apps/openmw/mwrender/sky.hpp" -#include "apps/openmw/mwrender/interior.hpp" +#include "../mwrender/sky.hpp" +#include "../mwrender/interior.hpp" + +#include "../mwmechanics/mechanicsmanager.hpp" #include "ptr.hpp" +#include "environment.hpp" namespace { @@ -164,9 +167,9 @@ namespace MWWorld } World::World (OEngine::Render::OgreRenderer& renderer, const boost::filesystem::path& dataDir, - const std::string& master, bool newGame) + const std::string& master, bool newGame, Environment& environment) : mSkyManager (0), mScene (renderer), mPlayerPos (0), mCurrentCell (0), mGlobalVariables (0), - mSky (false), mCellChanged (false) + mSky (false), mCellChanged (false), mEnvironment (environment) { boost::filesystem::path masterPath (dataDir); masterPath /= master; @@ -413,35 +416,59 @@ namespace MWWorld { // Load cell. mInteriors[cellName].loadInt (cellName, mStore, mEsm); + Ptr::CellStore *cell = &mInteriors[cellName]; // remove active CellRenderCollection::iterator active = mActiveCells.begin(); if (active!=mActiveCells.end()) { + mEnvironment.mMechanicsManager->dropActors (active->first); active->second->destroy(); delete active->second; mActiveCells.erase (active); } + // register local scripts mLocalScripts.clear(); // FIXME won't work with exteriors - insertInteriorScripts (mInteriors[cellName]); + insertInteriorScripts (*cell); + // adjust player mPlayerPos->setPos (position.pos[0], position.pos[1], position.pos[2]); - mPlayerPos->setCell (&mInteriors[cellName]); + mPlayerPos->setCell (cell); // TODO orientation // This connects the cell data with the rendering scene. std::pair result = - mActiveCells.insert (std::make_pair (&mInteriors[cellName], - new MWRender::InteriorCellRender (mInteriors[cellName], mScene))); + mActiveCells.insert (std::make_pair (cell, + new MWRender::InteriorCellRender (*cell, mScene))); if (result.second) { // Load the cell and insert it into the renderer result.first->second->show(); } + + // Actors + mEnvironment.mMechanicsManager->addActor (mPlayerPos->getPlayer()); + for (ESMS::CellRefList::List::iterator iter ( + cell->creatures.list.begin()); + iter!=cell->creatures.list.end(); ++iter) + { + Ptr ptr (&*iter, cell); + mEnvironment.mMechanicsManager->addActor (ptr); + } + + for (ESMS::CellRefList::List::iterator iter ( + cell->npcs.list.begin()); + iter!=cell->npcs.list.end(); ++iter) + { + Ptr ptr (&*iter, cell); + mEnvironment.mMechanicsManager->addActor (ptr); + } + + // Sky system if (mSky) { toggleSky(); diff --git a/apps/openmw/mwworld/world.hpp b/apps/openmw/mwworld/world.hpp index d1cf2de31d..953fe93e43 100644 --- a/apps/openmw/mwworld/world.hpp +++ b/apps/openmw/mwworld/world.hpp @@ -33,6 +33,8 @@ namespace MWRender namespace MWWorld { + class Environment; + /// \brief The game world and its visual representation class World @@ -58,6 +60,7 @@ namespace MWWorld MWWorld::Globals *mGlobalVariables; bool mSky; bool mCellChanged; + Environment& mEnvironment; // not implemented World (const World&); @@ -74,7 +77,7 @@ namespace MWWorld public: World (OEngine::Render::OgreRenderer& renderer, const boost::filesystem::path& master, - const std::string& dataDir, bool newGame); + const std::string& dataDir, bool newGame, Environment& environment); ~World();