summaryrefslogtreecommitdiffstats
path: root/source/World.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'source/World.cpp')
-rw-r--r--source/World.cpp146
1 files changed, 58 insertions, 88 deletions
diff --git a/source/World.cpp b/source/World.cpp
index e5e8977bd..2c31371d2 100644
--- a/source/World.cpp
+++ b/source/World.cpp
@@ -307,34 +307,49 @@ cWorld::~cWorld()
-void cWorld::SetWeather(eWeather a_Weather)
+void cWorld::CastThunderbolt (int a_BlockX, int a_BlockY, int a_BlockZ)
+{
+ BroadcastThunderbolt(a_BlockX, a_BlockY, a_BlockZ);
+}
+
+
+
+
+
+void cWorld::SetWeather(eWeather a_NewWeather)
{
- switch (a_Weather)
+ // Do the plugins agree? Do they want a different weather?
+ cRoot::Get()->GetPluginManager()->CallHookWeatherChanging(*this, a_NewWeather);
+
+ // Set new period for the selected weather:
+ switch (a_NewWeather)
{
- case eWeather_Sunny:
- case eWeather_Rain:
- case eWeather_ThunderStorm:
- {
- m_Weather = a_Weather;
- BroadcastWeather(a_Weather);
- break;
- }
-
+ case eWeather_Sunny: m_WeatherInterval = 14400 + (m_TickRand.randInt() % 4800); break; // 12 - 16 minutes
+ case eWeather_Rain: m_WeatherInterval = 9600 + (m_TickRand.randInt() % 7200); break; // 8 - 14 minutes
+ case eWeather_ThunderStorm: m_WeatherInterval = 2400 + (m_TickRand.randInt() % 4800); break; // 2 - 6 minutes
default:
{
- LOGWARN("Trying to set unknown weather %d", a_Weather);
+ LOGWARNING("Requested unknown weather %d, setting sunny for a minute instead.", a_NewWeather);
+ a_NewWeather = eWeather_Sunny;
+ m_WeatherInterval = 1200;
break;
}
- }
+ } // switch (NewWeather)
+ m_Weather = a_NewWeather;
+ BroadcastWeather(m_Weather);
+
+ // Let the plugins know about the change:
+ cPluginManager::Get()->CallHookWeatherChanged(*this);
}
-void cWorld::CastThunderbolt (int a_BlockX, int a_BlockY, int a_BlockZ)
+void cWorld::ChangeWeather(void)
{
- BroadcastThunderbolt(a_BlockX, a_BlockY, a_BlockZ);
+ // In the next tick the weather will be changed
+ m_WeatherInterval = 0;
}
@@ -533,92 +548,47 @@ void cWorld::Tick(float a_Dt)
-void cWorld::ChangeWeather()
+void cWorld::TickWeather(float a_Dt)
{
- unsigned randWeather = (m_TickRand.randInt() % 99);
-
- if (GetWeather() == eWeather_Sunny)
+ if (m_WeatherInterval > 0)
{
- if (randWeather < 20)
- {
- LOG("Starting rainstorm!");
- SetWeather( eWeather_Rain );
- }
+ // Not yet, wait for the weather period to end
+ m_WeatherInterval--;
}
-
- else if (GetWeather() == eWeather_Rain)
+ else
{
- if (randWeather < 5)
- {
- LOG("Thunderstorm!");
- SetWeather( eWeather_ThunderStorm );
- }
-
- else if (randWeather < 60)
- {
- LOG("Back to sunshine");
- SetWeather( eWeather_Sunny );
- }
- }
+ // Change weather:
- else if (GetWeather() == eWeather_ThunderStorm)
- {
- if (randWeather < 70)
- {
- SetWeather(eWeather_Sunny);
- LOG("Thunder ended abruptly, returning to lovely sunshine");
- }
- else if (randWeather < 85)
+ // Pick a new weather. Only reasonable transitions allowed:
+ eWeather NewWeather = m_Weather;
+ switch (m_Weather)
{
- SetWeather(eWeather_Rain);
- LOG("Thunder ended, but rain persists.");
- }
- else
- {
- return;
- }
- }
-}
-
-
-
-
-
-void cWorld::TickWeather(float a_Dt)
-{
- if (m_WeatherInterval == 0)
- {
- ChangeWeather();
-
- cRoot::Get()->GetPluginManager()->CallHookWeatherChanged(this);
-
- switch (GetWeather())
- {
- case eWeather_Sunny:
- m_WeatherInterval = 14400 + (m_TickRand.randInt() % 4800); // 12 - 16 minutes
- break;
+ case eWeather_Sunny: NewWeather = eWeather_Rain; break;
+ case eWeather_ThunderStorm: NewWeather = eWeather_Rain; break;
case eWeather_Rain:
- m_WeatherInterval = 9600 + (m_TickRand.randInt() % 7200); // 8 - 14 minutes
- break;
- case eWeather_ThunderStorm:
- m_WeatherInterval = 2400 + (m_TickRand.randInt() % 4800); // 2 - 6 minutes
+ {
+ // 1/8 chance of turning into a thunderstorm
+ NewWeather = ((m_TickRand.randInt() % 256) < 32) ? eWeather_ThunderStorm : eWeather_Sunny;
break;
+ }
+
default:
- LOG("Unknown weather occurred");
- break;
+ {
+ LOGWARNING("Unknown current weather: %d. Setting sunny.", m_Weather);
+ ASSERT(!"Unknown weather");
+ NewWeather = eWeather_Sunny;
+ }
}
- }
-
- else
- {
- m_WeatherInterval--;
- }
+
+ SetWeather(NewWeather);
+ } // else (m_WeatherInterval > 0)
- if ( GetWeather() == 2 ) // if thunderstorm
+ if (m_Weather == eWeather_ThunderStorm)
{
- if (m_TickRand.randInt() % 199 == 0) // 0.5% chance per tick of thunderbolt
+ // 0.5% chance per tick of thunderbolt
+ if (m_TickRand.randInt() % 199 == 0)
{
- CastThunderbolt ( 0, 0, 0 ); // TODO: find random possitions near players to cast thunderbolts.
+ CastThunderbolt(0, 0, 0); // TODO: find random possitions near players to cast thunderbolts.
}
}
}