1
0
mirror of https://gitlab.com/OpenMW/openmw.git synced 2025-01-27 03:35:27 +00:00

Removal of duplicit exterior checking and unneded string copiing.

This commit is contained in:
Miroslav Puda 2013-06-19 16:18:43 +02:00
parent 8a45686e0a
commit bf31e5385c
2 changed files with 245 additions and 229 deletions

View File

@ -348,10 +348,17 @@ void WeatherManager::update(float duration)
mWeatherUpdateTime -= timePassed; mWeatherUpdateTime -= timePassed;
bool exterior = (MWBase::Environment::get().getWorld()->isCellExterior() || MWBase::Environment::get().getWorld()->isCellQuasiExterior()); const bool exterior = (MWBase::Environment::get().getWorld()->isCellExterior() || MWBase::Environment::get().getWorld()->isCellQuasiExterior());
if (!exterior)
if (exterior)
{ {
mRendering->sunDisable(false);
mRendering->skyDisable();
mRendering->getSkyManager()->setLightningStrength(0.f);
stopSounds(true);
return;
}
// Exterior
std::string regionstr = Misc::StringUtils::lowerCase(MWBase::Environment::get().getWorld()->getPlayer().getPlayer().getCell()->mCell->mRegion); std::string regionstr = Misc::StringUtils::lowerCase(MWBase::Environment::get().getWorld()->getPlayer().getPlayer().getCell()->mCell->mRegion);
if (mWeatherUpdateTime <= 0 || regionstr != mCurrentRegion) if (mWeatherUpdateTime <= 0 || regionstr != mCurrentRegion)
@ -371,36 +378,7 @@ void WeatherManager::update(float duration)
if (region != 0) if (region != 0)
{ {
/* weatherType = nextWeather(region);
* All probabilities must add to 100 (responsibility of the user).
* If chances A and B has values 30 and 70 then by generating
* 100 numbers 1..100, 30% will be lesser or equal 30 and
* 70% will be greater than 30 (in theory).
*/
const int probability[] = {
region->mData.mClear,
region->mData.mCloudy,
region->mData.mFoggy,
region->mData.mOvercast,
region->mData.mRain,
region->mData.mThunder,
region->mData.mAsh,
region->mData.mBlight,
region->mData.mA,
region->mData.mB
}; // 10 elements
int chance = (rand() % 100) + 1; // 1..100
int sum = 0;
for (int i = 0; i < 10; ++i)
{
sum += probability[i];
if (chance < sum)
{
weatherType = (Weather::Type)i;
break;
}
}
} }
} }
@ -509,7 +487,7 @@ void WeatherManager::update(float duration)
mRendering->getSkyManager()->secundaDisable(); mRendering->getSkyManager()->secundaDisable();
} }
if (mCurrentWeather == Weather::Type_Thunderstorm && mNextWeather == Weather::Type_Unknown && exterior) if (mCurrentWeather == Weather::Type_Thunderstorm && mNextWeather == Weather::Type_Unknown)
{ {
if (mThunderFlash > 0) if (mThunderFlash > 0)
{ {
@ -519,12 +497,12 @@ void WeatherManager::update(float duration)
{ {
// pick a random sound // pick a random sound
int sound = rand() % 4; int sound = rand() % 4;
std::string soundname; std::string* soundName;
if (sound == 0) soundname = mThunderSoundID0; if (sound == 0) soundName = &mThunderSoundID0;
else if (sound == 1) soundname = mThunderSoundID1; else if (sound == 1) soundName = &mThunderSoundID1;
else if (sound == 2) soundname = mThunderSoundID2; else if (sound == 2) soundName = &mThunderSoundID2;
else if (sound == 3) soundname = mThunderSoundID3; else if (sound == 3) soundName = &mThunderSoundID3;
MWBase::Environment::get().getSoundManager()->playSound(soundname, 1.0, 1.0); MWBase::Environment::get().getSoundManager()->playSound(*soundName, 1.0, 1.0);
mThunderSoundDelay = 1000; mThunderSoundDelay = 1000;
} }
@ -561,30 +539,19 @@ void WeatherManager::update(float duration)
mRendering->setSunColour(mResult.mSunColor); mRendering->setSunColour(mResult.mSunColor);
mRendering->getSkyManager()->setWeather(mResult); mRendering->getSkyManager()->setWeather(mResult);
}
else
{
mRendering->sunDisable(false);
mRendering->skyDisable();
mRendering->getSkyManager()->setLightningStrength(0.f);
}
// play sounds
std::string ambientSnd = (mNextWeather == Weather::Type_Unknown ? mWeatherSettings[mCurrentWeather].mAmbientLoopSoundID : ""); // Play sounds
if (!exterior) ambientSnd = ""; if (mNextWeather == Weather::Type_Unknown)
if (ambientSnd != "")
{ {
std::string ambientSnd = mWeatherSettings[mCurrentWeather].mAmbientLoopSoundID;
if (std::find(mSoundsPlaying.begin(), mSoundsPlaying.end(), ambientSnd) == mSoundsPlaying.end()) if (std::find(mSoundsPlaying.begin(), mSoundsPlaying.end(), ambientSnd) == mSoundsPlaying.end())
{ {
mSoundsPlaying.push_back(ambientSnd); mSoundsPlaying.push_back(ambientSnd);
MWBase::Environment::get().getSoundManager()->playSound(ambientSnd, 1.0, 1.0, MWBase::SoundManager::Play_Loop); MWBase::Environment::get().getSoundManager()->playSound(ambientSnd, 1.0, 1.0, MWBase::SoundManager::Play_Loop);
} }
}
std::string rainSnd = (mNextWeather == Weather::Type_Unknown ? mWeatherSettings[mCurrentWeather].mRainLoopSoundID : ""); std::string rainSnd = mWeatherSettings[mCurrentWeather].mRainLoopSoundID;
if (!exterior) rainSnd = "";
if (rainSnd != "")
{
if (std::find(mSoundsPlaying.begin(), mSoundsPlaying.end(), rainSnd) == mSoundsPlaying.end()) if (std::find(mSoundsPlaying.begin(), mSoundsPlaying.end(), rainSnd) == mSoundsPlaying.end())
{ {
mSoundsPlaying.push_back(rainSnd); mSoundsPlaying.push_back(rainSnd);
@ -592,20 +559,61 @@ void WeatherManager::update(float duration)
} }
} }
// stop sounds stopSounds(false);
std::vector<std::string>::iterator it=mSoundsPlaying.begin(); }
void WeatherManager::stopSounds(bool stopAll)
{
std::vector<std::string>::iterator it = mSoundsPlaying.begin();
while (it!=mSoundsPlaying.end()) while (it!=mSoundsPlaying.end())
{ {
if ( *it != ambientSnd && *it != rainSnd) if (stopAll || \
!(*it == mWeatherSettings[mCurrentWeather].mAmbientLoopSoundID || \
*it == mWeatherSettings[mCurrentWeather].mRainLoopSoundID))
{ {
MWBase::Environment::get().getSoundManager()->stopSound(*it); MWBase::Environment::get().getSoundManager()->stopSound(*it);
it = mSoundsPlaying.erase(it); it = mSoundsPlaying.erase(it);
} }
else
++it; ++it;
} }
} }
Weather::Type WeatherManager::nextWeather(const ESM::Region* region)
{
/*
* All probabilities must add to 100 (responsibility of the user).
* If chances A and B has values 30 and 70 then by generating
* 100 numbers 1..100, 30% will be lesser or equal 30 and
* 70% will be greater than 30 (in theory).
*/
const int probability[] = {
region->mData.mClear,
region->mData.mCloudy,
region->mData.mFoggy,
region->mData.mOvercast,
region->mData.mRain,
region->mData.mThunder,
region->mData.mAsh,
region->mData.mBlight,
region->mData.mA,
region->mData.mB
}; // 10 elements
int chance = (rand() % 100) + 1; // 1..100
int sum = 0;
int i = 0;
for (; i < 10; ++i)
{
sum += probability[i];
if (chance < sum)
{
break;
}
}
return (Weather::Type) i;
}
void WeatherManager::setHour(const float hour) void WeatherManager::setHour(const float hour)
{ {
mHour = hour; mHour = hour;

View File

@ -4,6 +4,11 @@
#include <OgreString.h> #include <OgreString.h>
#include <OgreColourValue.h> #include <OgreColourValue.h>
namespace ESM
{
struct Region;
}
namespace MWRender namespace MWRender
{ {
class RenderingManager; class RenderingManager;
@ -146,6 +151,8 @@ namespace MWWorld
*/ */
void update(float duration); void update(float duration);
void stopSounds(bool stopAll);
void setHour(const float hour); void setHour(const float hour);
float getWindSpeed() const; float getWindSpeed() const;
@ -195,6 +202,7 @@ namespace MWWorld
float calculateAngleFade (const std::string& moonName, float angle) const; float calculateAngleFade (const std::string& moonName, float angle) const;
void setWeather(Weather::Type weatherType, bool instant=false); void setWeather(Weather::Type weatherType, bool instant=false);
Weather::Type nextWeather(const ESM::Region* region);
WeatherResult mResult; WeatherResult mResult;
float mSunriseTime; float mSunriseTime;
float mSunsetTime; float mSunsetTime;