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;
bool exterior = (MWBase::Environment::get().getWorld()->isCellExterior() || MWBase::Environment::get().getWorld()->isCellQuasiExterior());
if (exterior)
const bool exterior = (MWBase::Environment::get().getWorld()->isCellExterior() || MWBase::Environment::get().getWorld()->isCellQuasiExterior());
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);
if (mWeatherUpdateTime <= 0 || regionstr != mCurrentRegion)
@ -371,36 +378,7 @@ void WeatherManager::update(float duration)
if (region != 0)
{
/*
* 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;
}
}
weatherType = nextWeather(region);
}
}
@ -509,7 +487,7 @@ void WeatherManager::update(float duration)
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)
{
@ -519,12 +497,12 @@ void WeatherManager::update(float duration)
{
// pick a random sound
int sound = rand() % 4;
std::string soundname;
if (sound == 0) soundname = mThunderSoundID0;
else if (sound == 1) soundname = mThunderSoundID1;
else if (sound == 2) soundname = mThunderSoundID2;
else if (sound == 3) soundname = mThunderSoundID3;
MWBase::Environment::get().getSoundManager()->playSound(soundname, 1.0, 1.0);
std::string* soundName;
if (sound == 0) soundName = &mThunderSoundID0;
else if (sound == 1) soundName = &mThunderSoundID1;
else if (sound == 2) soundName = &mThunderSoundID2;
else if (sound == 3) soundName = &mThunderSoundID3;
MWBase::Environment::get().getSoundManager()->playSound(*soundName, 1.0, 1.0);
mThunderSoundDelay = 1000;
}
@ -561,30 +539,19 @@ void WeatherManager::update(float duration)
mRendering->setSunColour(mResult.mSunColor);
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 : "");
if (!exterior) ambientSnd = "";
if (ambientSnd != "")
// Play sounds
if (mNextWeather == Weather::Type_Unknown)
{
std::string ambientSnd = mWeatherSettings[mCurrentWeather].mAmbientLoopSoundID;
if (std::find(mSoundsPlaying.begin(), mSoundsPlaying.end(), ambientSnd) == mSoundsPlaying.end())
{
mSoundsPlaying.push_back(ambientSnd);
MWBase::Environment::get().getSoundManager()->playSound(ambientSnd, 1.0, 1.0, MWBase::SoundManager::Play_Loop);
}
}
std::string rainSnd = (mNextWeather == Weather::Type_Unknown ? mWeatherSettings[mCurrentWeather].mRainLoopSoundID : "");
if (!exterior) rainSnd = "";
if (rainSnd != "")
{
std::string rainSnd = mWeatherSettings[mCurrentWeather].mRainLoopSoundID;
if (std::find(mSoundsPlaying.begin(), mSoundsPlaying.end(), rainSnd) == mSoundsPlaying.end())
{
mSoundsPlaying.push_back(rainSnd);
@ -592,20 +559,61 @@ void WeatherManager::update(float duration)
}
}
// stop sounds
std::vector<std::string>::iterator it=mSoundsPlaying.begin();
stopSounds(false);
}
void WeatherManager::stopSounds(bool stopAll)
{
std::vector<std::string>::iterator it = mSoundsPlaying.begin();
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);
it = mSoundsPlaying.erase(it);
}
else
++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)
{
mHour = hour;

View file

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