TrinityCore
WeatherMgr.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 "WeatherMgr.h"
23#include "Containers.h"
24#include "DatabaseEnv.h"
25#include "Log.h"
26#include "ObjectMgr.h"
27#include "Timer.h"
28#include "Weather.h"
29
30namespace WeatherMgr
31{
32
33namespace
34{
35 std::unordered_map<uint32, WeatherData> _weatherData;
36}
37
39{
40 return Trinity::Containers::MapGetValuePtr(_weatherData, zone_id);
41}
42
44{
45 uint32 oldMSTime = getMSTime();
46
47 uint32 count = 0;
48
49 QueryResult result = WorldDatabase.Query("SELECT "
50 "zone, spring_rain_chance, spring_snow_chance, spring_storm_chance,"
51 "summer_rain_chance, summer_snow_chance, summer_storm_chance,"
52 "fall_rain_chance, fall_snow_chance, fall_storm_chance,"
53 "winter_rain_chance, winter_snow_chance, winter_storm_chance,"
54 "ScriptName FROM game_weather");
55
56 if (!result)
57 {
58 TC_LOG_INFO("server.loading", ">> Loaded 0 weather definitions. DB table `game_weather` is empty.");
59 return;
60 }
61
62 do
63 {
64 Field* fields = result->Fetch();
65
66 uint32 zone_id = fields[0].GetUInt32();
67
68 WeatherData& wzc = _weatherData[zone_id];
69
70 for (uint8 season = 0; season < WEATHER_SEASONS; ++season)
71 {
72 wzc.data[season].rainChance = fields[season * (MAX_WEATHER_TYPE-1) + 1].GetUInt8();
73 wzc.data[season].snowChance = fields[season * (MAX_WEATHER_TYPE-1) + 2].GetUInt8();
74 wzc.data[season].stormChance = fields[season * (MAX_WEATHER_TYPE-1) + 3].GetUInt8();
75
76 if (wzc.data[season].rainChance > 100)
77 {
78 wzc.data[season].rainChance = 25;
79 TC_LOG_ERROR("sql.sql", "Weather for zone {} season {} has wrong rain chance > 100%", zone_id, season);
80 }
81
82 if (wzc.data[season].snowChance > 100)
83 {
84 wzc.data[season].snowChance = 25;
85 TC_LOG_ERROR("sql.sql", "Weather for zone {} season {} has wrong snow chance > 100%", zone_id, season);
86 }
87
88 if (wzc.data[season].stormChance > 100)
89 {
90 wzc.data[season].stormChance = 25;
91 TC_LOG_ERROR("sql.sql", "Weather for zone {} season {} has wrong storm chance > 100%", zone_id, season);
92 }
93 }
94
95 wzc.ScriptId = sObjectMgr->GetScriptId(fields[13].GetString());
96
97 ++count;
98 }
99 while (result->NextRow());
100
101 TC_LOG_INFO("server.loading", ">> Loaded {} weather definitions in {} ms", count, GetMSTimeDiffToNow(oldMSTime));
102}
103
104} // namespace
std::shared_ptr< ResultSet > QueryResult
DatabaseWorkerPool< WorldDatabaseConnection > WorldDatabase
Accessor to the world database.
Definition: DatabaseEnv.cpp:20
uint8_t uint8
Definition: Define.h:144
uint32_t uint32
Definition: Define.h:142
#define TC_LOG_ERROR(filterType__,...)
Definition: Log.h:165
#define TC_LOG_INFO(filterType__,...)
Definition: Log.h:159
#define sObjectMgr
Definition: ObjectMgr.h:1946
#define MAX_WEATHER_TYPE
uint32 GetMSTimeDiffToNow(uint32 oldMSTime)
Definition: Timer.h:57
uint32 getMSTime()
Definition: Timer.h:33
Class used to access individual fields of database query result.
Definition: Field.h:90
uint32 GetUInt32() const
Definition: Field.cpp:62
void LoadWeatherData()
Definition: WeatherMgr.cpp:43
WeatherSeasonChances data[WEATHER_SEASONS]
Definition: Weather.h:41
#define WEATHER_SEASONS
Definition: Weather.h:31
uint32 ScriptId
Definition: Weather.h:42
uint32 stormChance
Definition: Weather.h:36
WeatherData const * GetWeatherData(uint32 zone_id)
Definition: WeatherMgr.cpp:38
auto MapGetValuePtr(M &map, typename M::key_type const &key)
Definition: MapUtils.h:29