diff --git a/apps/openmw/engine.cpp b/apps/openmw/engine.cpp index 46c34f1b9..8b21f6e63 100644 --- a/apps/openmw/engine.cpp +++ b/apps/openmw/engine.cpp @@ -40,6 +40,7 @@ #include #include "mwgui/class.hpp" + void OMW::Engine::executeLocalScripts() { for (MWWorld::World::ScriptList::const_iterator iter ( @@ -107,27 +108,61 @@ bool OMW::Engine::frameStarted(const Ogre::FrameEvent& evt) // Play some good 'ol tunes startRandomTitle(); } + + std::string effect; + - //If the region has changed + MWWorld::Ptr::CellStore *current = mEnvironment.mWorld->getPlayerPos().getPlayer().getCell(); - if(!(current->cell->data.flags & current->cell->Interior) && (test.name != current->cell->region)){ - test = *(mEnvironment.mWorld->getStore().regions.find(current->cell->region)); + //If the region has changed + if(!(current->cell->data.flags & current->cell->Interior) && timer.elapsed() >= 10){ + timer.restart(); + if (test.name != current->cell->region) + { + total = 0; + test = *(mEnvironment.mWorld->getStore().regions.find(current->cell->region)); + } + if(test.soundList.size() > 0) { std::vector::iterator soundIter = test.soundList.begin(); - mEnvironment.mWorld->getPlayerPos().getPlayer().getCell(); //mEnvironment.mSoundManager + if(total == 0){ + while (!(soundIter == test.soundList.end())) + { + ESM::NAME32 go = soundIter->sound; + int chance = (int) soundIter->chance; + //std::cout << "Sound: " << go.name <<" Chance:" << chance << "\n"; + soundIter++; + total += chance; + } + } + + srand ( time(NULL) ); + int r = rand() % total; //old random code + int pos = 0; + soundIter = test.soundList.begin(); while (!(soundIter == test.soundList.end())) { - ESM::NAME32 go = soundIter->sound; - char chance = soundIter->chance; - std::cout << "Sound: " << go.name <<" Chance:" << (int) chance << "\n"; + const ESM::NAME32 go = soundIter->sound; + int chance = (int) soundIter->chance; + //std::cout << "Sound: " << go.name <<" Chance:" << chance << "\n"; soundIter++; + if( r - pos < chance) + { + effect = go.name; + //play sound + std::cout << "Sound: " << go.name <<" Chance:" << chance << "\n"; + mEnvironment.mSoundManager->playSound(effect, 20.0, 1.0); + + break; + + } + pos += chance; } - } - + //mEnvironment.mSoundManager->playSound(effect, 1.0, 1.0); //printf("REGION: %s\n", test.name); } @@ -296,6 +331,7 @@ void OMW::Engine::go() MP3Lookup(); test.name = ""; + total = 0; diff --git a/apps/openmw/engine.hpp b/apps/openmw/engine.hpp index 500fe2808..54748b2c6 100644 --- a/apps/openmw/engine.hpp +++ b/apps/openmw/engine.hpp @@ -12,6 +12,8 @@ #include "mwworld/environment.hpp" #include "mwworld/ptr.hpp" +#include + namespace Compiler { @@ -62,6 +64,7 @@ namespace OMW bool mVerboseScripts; bool mNewGame; bool mUseSound; + int total; MWWorld::Environment mEnvironment; MWScript::ScriptManager *mScriptManager; @@ -69,6 +72,7 @@ namespace OMW Compiler::Context *mScriptContext; OEngine::GUI::MyGUIManager *mGuiManager; ESM::Region test; + boost::timer timer; int focusFrameCounter; static const int focusUpdateFrame = 10;