TrinityCore
Loading...
Searching...
No Matches
Weather.cpp
Go to the documentation of this file.
1/*
2 * This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
22#include "Weather.h"
23#include "GameTime.h"
24#include "Log.h"
25#include "Map.h"
26#include "MiscPackets.h"
27#include "Player.h"
28#include "Random.h"
29#include "ScriptMgr.h"
30#include "Util.h"
31#include "World.h"
32
34Weather::Weather(Map* map, uint32 zoneId, WeatherData const* weatherChances)
35 : m_map(map), m_zone(zoneId), m_weatherChances(weatherChances)
36{
39 m_intensity = 0;
40
41 TC_LOG_INFO("misc", "WORLD: Starting weather system for zone {} (change every {} minutes).", m_zone, (uint32)(m_timer.GetInterval() / (MINUTE*IN_MILLISECONDS)));
42}
43
46{
47 if (m_timer.GetCurrent() >= 0)
48 m_timer.Update(diff);
49 else
51
53 if (m_timer.Passed())
54 {
55 m_timer.Reset();
56 // update only if Regenerate has changed the weather
57 if (ReGenerate())
58 {
60 if (!UpdateWeather())
61 return false;
62 }
63 }
64
65 sScriptMgr->OnWeatherUpdate(this, diff);
66 return true;
67}
68
71{
73 {
75 m_intensity = 0.0f;
76 return false;
77 }
78
84 uint32 u = urand(0, 99);
85
86 if (u < 30)
87 return false;
88
89 // remember old values
90 WeatherType old_type = m_type;
91 float old_intensity = m_intensity;
92
93 //78 days between January 1st and March 20nd; 365/4=91 days by season
94 // season source http://aa.usno.navy.mil/data/docs/EarthSeasons.html
95 time_t gtime = GameTime::GetGameTime();
96 struct tm ltime;
97 localtime_r(&gtime, &ltime);
98 uint32 season = ((ltime.tm_yday - 78 + 365)/91)%4;
99
100 static char const* seasonName[WEATHER_SEASONS] = { "spring", "summer", "fall", "winter" };
101
102 TC_LOG_INFO("misc", "Generating a change in {} weather for zone {}.", seasonName[season], m_zone);
103
104 if ((u < 60) && (m_intensity < 0.33333334f)) // Get fair
105 {
107 m_intensity = 0.0f;
108 }
109
110 if ((u < 60) && (m_type != WEATHER_TYPE_FINE)) // Get better
111 {
112 m_intensity -= 0.33333334f;
113 return true;
114 }
115
116 if ((u < 90) && (m_type != WEATHER_TYPE_FINE)) // Get worse
117 {
118 m_intensity += 0.33333334f;
119 return true;
120 }
121
123 {
128
129 if (m_intensity < 0.33333334f)
130 {
131 m_intensity = 0.9999f; // go nuts
132 return true;
133 }
134 else
135 {
136 if (m_intensity > 0.6666667f)
137 {
138 // Severe change, but how severe?
139 uint32 rnd = urand(0, 99);
140 if (rnd < 50)
141 {
142 m_intensity -= 0.6666667f;
143 return true;
144 }
145 }
146 m_type = WEATHER_TYPE_FINE; // clear up
147 m_intensity = 0;
148 }
149 }
150
151 // At this point, only weather that isn't doing anything remains but that have weather data
152 uint32 chance1 = m_weatherChances->data[season].rainChance;
153 uint32 chance2 = chance1+ m_weatherChances->data[season].snowChance;
154 uint32 chance3 = chance2+ m_weatherChances->data[season].stormChance;
155
156 uint32 rnd = urand(1, 100);
157 if (rnd <= chance1)
159 else if (rnd <= chance2)
161 else if (rnd <= chance3)
163 else
165
171
173 {
174 m_intensity = 0.0f;
175 }
176 else if (u < 90)
177 {
178 m_intensity = (float)rand_norm() * 0.3333f;
179 }
180 else
181 {
182 // Severe change, but how severe?
183 rnd = urand(0, 99);
184 if (rnd < 50)
185 m_intensity = (float)rand_norm() * 0.3333f + 0.3334f;
186 else
187 m_intensity = (float)rand_norm() * 0.3333f + 0.6667f;
188 }
189
190 // return true only in case weather changes
191 return m_type != old_type || m_intensity != old_intensity;
192}
193
199
205
208{
210 if (m_intensity >= 1)
211 m_intensity = 0.9999f;
212 else if (m_intensity < 0)
213 m_intensity = 0.0001f;
214
216
218
219 //- Returns false if there were no players found to update
220 if (!m_map->SendZoneMessage(m_zone, weather.Write()))
221 return false;
222
224 char const* wthstr;
225 switch (state)
226 {
228 wthstr = "fog";
229 break;
231 wthstr = "light rain";
232 break;
234 wthstr = "medium rain";
235 break;
237 wthstr = "heavy rain";
238 break;
240 wthstr = "light snow";
241 break;
243 wthstr = "medium snow";
244 break;
246 wthstr = "heavy snow";
247 break;
249 wthstr = "light sandstorm";
250 break;
252 wthstr = "medium sandstorm";
253 break;
255 wthstr = "heavy sandstorm";
256 break;
258 wthstr = "thunders";
259 break;
261 wthstr = "blackrain";
262 break;
264 default:
265 wthstr = "fine";
266 break;
267 }
268
269 TC_LOG_INFO("misc", "Change the weather of zone {} to {}.", m_zone, wthstr);
270 sScriptMgr->OnWeatherChange(this, state, m_intensity);
271 return true;
272}
273
275void Weather::SetWeather(WeatherType type, float intensity)
276{
277 if (m_type == type && m_intensity == intensity)
278 return;
279
280 m_type = type;
281 m_intensity = intensity;
283}
284
287{
288 if (m_intensity < 0.27f)
289 return WEATHER_STATE_FINE;
290
291 switch (m_type)
292 {
294 if (m_intensity < 0.40f)
296 else if (m_intensity < 0.70f)
298 else
301 if (m_intensity < 0.40f)
303 else if (m_intensity < 0.70f)
305 else
308 if (m_intensity < 0.40f)
310 else if (m_intensity < 0.70f)
312 else
319 default:
320 return WEATHER_STATE_FINE;
321 }
322}
@ IN_MILLISECONDS
Definition Common.h:35
@ MINUTE
Definition Common.h:29
uint32_t uint32
Definition Define.h:133
#define TC_LOG_INFO(filterType__,...)
Definition Log.h:159
uint32 urand(uint32 min, uint32 max)
Definition Random.cpp:42
double rand_norm()
Definition Random.cpp:75
#define sScriptMgr
Definition ScriptMgr.h:1168
WeatherType
@ WEATHER_TYPE_RAIN
@ WEATHER_TYPE_STORM
@ WEATHER_TYPE_FINE
@ WEATHER_TYPE_SNOW
@ WEATHER_TYPE_THUNDERS
@ WEATHER_TYPE_BLACKRAIN
Definition Map.h:281
bool SendZoneMessage(uint32 zone, WorldPacket const *packet, WorldSession const *self=nullptr, uint32 team=0) const
Send a packet to all players (or players selected team) in the zone (except self if mentioned)
Definition Map.cpp:3675
void SendDirectMessage(WorldPacket const *data) const
Definition Player.cpp:6161
WorldPacket const * Write() override
#define sWorld
Definition World.h:900
WeatherType m_type
Definition Weather.h:90
uint32 m_zone
Definition Weather.h:89
float m_intensity
Definition Weather.h:91
WeatherState
Definition Weather.h:47
Map * m_map
Definition Weather.h:88
WeatherSeasonChances data[WEATHER_SEASONS]
Definition Weather.h:42
#define WEATHER_SEASONS
Definition Weather.h:32
void SetWeather(WeatherType type, float intensity)
Set the weather.
Definition Weather.cpp:275
Weather(Map *map, uint32 zoneId, WeatherData const *weatherChances)
Create the Weather object.
Definition Weather.cpp:34
static void SendFineWeatherUpdateToPlayer(Player *player)
Definition Weather.cpp:200
bool UpdateWeather()
Send the new weather to all players in the zone.
Definition Weather.cpp:207
IntervalTimer m_timer
Definition Weather.h:92
bool ReGenerate()
Calculate the new weather.
Definition Weather.cpp:70
WeatherData const * m_weatherChances
Definition Weather.h:93
WeatherState GetWeatherState() const
Get the sound number associated with the current weather.
Definition Weather.cpp:286
bool Update(uint32 diff)
Launch a weather update.
Definition Weather.cpp:45
void SendWeatherUpdateToPlayer(Player *player)
Definition Weather.cpp:194
@ CONFIG_INTERVAL_CHANGEWEATHER
Definition World.h:215
@ WEATHER_STATE_THUNDERS
Definition Weather.h:60
@ WEATHER_STATE_HEAVY_RAIN
Definition Weather.h:53
@ WEATHER_STATE_HEAVY_SANDSTORM
Definition Weather.h:59
@ WEATHER_STATE_MEDIUM_SNOW
Definition Weather.h:55
@ WEATHER_STATE_MEDIUM_RAIN
Definition Weather.h:52
@ WEATHER_STATE_MEDIUM_SANDSTORM
Definition Weather.h:58
@ WEATHER_STATE_FINE
Definition Weather.h:48
@ WEATHER_STATE_LIGHT_SNOW
Definition Weather.h:54
@ WEATHER_STATE_BLACKRAIN
Definition Weather.h:61
@ WEATHER_STATE_HEAVY_SNOW
Definition Weather.h:56
@ WEATHER_STATE_LIGHT_SANDSTORM
Definition Weather.h:57
@ WEATHER_STATE_FOG
Definition Weather.h:49
@ WEATHER_STATE_LIGHT_RAIN
Definition Weather.h:51
time_t GetGameTime()
Definition GameTime.cpp:42
void SetInterval(time_t interval)
Definition Timer.h:94
time_t GetCurrent() const
Definition Timer.h:104
time_t GetInterval() const
Definition Timer.h:99
bool Passed()
Definition Timer.h:78
void Update(time_t diff)
Definition Timer.h:71
void SetCurrent(time_t current)
Definition Timer.h:89
void Reset()
Definition Timer.h:83