Project SkyFire Core
SkyFire 5.4.8 server core API documentation
Loading...
Searching...
No Matches
WeatherMgr.cpp
Go to the documentation of this file.
1/*
2* This file is part of Project SkyFire https://www.projectskyfire.org.
3* See LICENSE.md file for Copyright information
4*/
5
9
10#include "AutoPtr.h"
11#include "Log.h"
12#include "ObjectMgr.h"
13#include "Opcodes.h"
14#include "Player.h"
15#include "Weather.h"
16#include "WeatherMgr.h"
17#include "WorldPacket.h"
18#include "WorldSession.h"
19
20namespace WeatherMgr
21{
22 namespace
23 {
25 typedef UNORDERED_MAP<uint32, WeatherData> WeatherZoneMap;
26
27 WeatherMap m_weathers;
28 WeatherZoneMap mWeatherZoneMap;
29
30 WeatherData const* GetWeatherData(uint32 zone_id)
31 {
32 WeatherZoneMap::const_iterator itr = mWeatherZoneMap.find(zone_id);
33 return (itr != mWeatherZoneMap.end()) ? &itr->second : NULL;
34 }
35 }
36
39 {
40 WeatherMap::const_iterator itr = m_weathers.find(id);
41 return (itr != m_weathers.end()) ? itr->second.get() : 0;
42 }
43
46 {
47 // not called at the moment. Kept for completeness
48 WeatherMap::iterator itr = m_weathers.find(id);
49
50 if (itr != m_weathers.end())
51 m_weathers.erase(itr);
52 }
53
56 {
57 WeatherData const* weatherChances = GetWeatherData(zone_id);
58
59 // zone does not have weather, ignore
60 if (!weatherChances)
61 return NULL;
62
63 Weather* w = new Weather(zone_id, weatherChances);
64 m_weathers[w->GetZone()].reset(w);
65 w->ReGenerate();
66 w->UpdateWeather();
67
68 return w;
69 }
70
72 {
73 uint32 oldMSTime = getMSTime();
74
75 uint32 count = 0;
76
77 QueryResult result = WorldDatabase.Query("SELECT "
78 "zone, spring_rain_chance, spring_snow_chance, spring_storm_chance,"
79 "summer_rain_chance, summer_snow_chance, summer_storm_chance,"
80 "fall_rain_chance, fall_snow_chance, fall_storm_chance,"
81 "winter_rain_chance, winter_snow_chance, winter_storm_chance,"
82 "ScriptName FROM game_weather");
83
84 if (!result)
85 {
86 SF_LOG_ERROR("server.loading", ">> Loaded 0 weather definitions. DB table `game_weather` is empty.");
87 return;
88 }
89
90 do
91 {
92 Field* fields = result->Fetch();
93
94 uint32 zone_id = fields[0].GetUInt32();
95
96 WeatherData& wzc = mWeatherZoneMap[zone_id];
97
98 for (uint8 season = 0; season < WEATHER_SEASONS; ++season)
99 {
100 wzc.data[season].rainChance = fields[season * (MAX_WEATHER_TYPE - 1) + 1].GetUInt8();
101 wzc.data[season].snowChance = fields[season * (MAX_WEATHER_TYPE - 1) + 2].GetUInt8();
102 wzc.data[season].stormChance = fields[season * (MAX_WEATHER_TYPE - 1) + 3].GetUInt8();
103
104 if (wzc.data[season].rainChance > 100)
105 {
106 wzc.data[season].rainChance = 25;
107 SF_LOG_ERROR("sql.sql", "Weather for zone %u season %u has wrong rain chance > 100%%", zone_id, season);
108 }
109
110 if (wzc.data[season].snowChance > 100)
111 {
112 wzc.data[season].snowChance = 25;
113 SF_LOG_ERROR("sql.sql", "Weather for zone %u season %u has wrong snow chance > 100%%", zone_id, season);
114 }
115
116 if (wzc.data[season].stormChance > 100)
117 {
118 wzc.data[season].stormChance = 25;
119 SF_LOG_ERROR("sql.sql", "Weather for zone %u season %u has wrong storm chance > 100%%", zone_id, season);
120 }
121 }
122
123 wzc.ScriptId = sObjectMgr->GetScriptId(fields[13].GetCString());
124
125 ++count;
126 } while (result->NextRow());
127
128 SF_LOG_INFO("server.loading", ">> Loaded %u weather definitions in %u ms", count, GetMSTimeDiffToNow(oldMSTime));
129 }
130
132 {
133 WorldPacket data(SMSG_WEATHER, 4 + 4 + 1);
134 data << (uint32)WEATHER_STATE_FINE; // WeatherID
135 data << float(0.0f); // Intensity
136 data.WriteBit(false); // Abrupt
137 data.FlushBits();
138 player->GetSession()->SendPacket(&data);
139 }
140
141 void Update(uint32 diff)
142 {
144 WeatherMap::iterator itr, next;
145 for (itr = m_weathers.begin(); itr != m_weathers.end(); itr = next)
146 {
147 next = itr;
148 ++next;
149
151 // As interval > WorldTick
152 if (!itr->second->Update(diff))
153 m_weathers.erase(itr);
154 }
155 }
156
157} // namespace
std::uint8_t uint8
Definition Define.h:79
std::uint32_t uint32
Definition Define.h:77
#define SF_LOG_ERROR(filterType__,...)
Definition Log.h:143
#define SF_LOG_INFO(filterType__,...)
Definition Log.h:137
#define sObjectMgr
Definition ObjectMgr.h:1617
Skyfire::AutoPtr< ResultSet, Skyfire::Mutex > QueryResult
Definition QueryResult.h:48
#define MAX_WEATHER_TYPE
uint32 GetMSTimeDiffToNow(uint32 oldMSTime)
Definition Timer.h:22
uint32 getMSTime()
Definition Timer.h:12
#define UNORDERED_MAP
bool WriteBit(uint32 bit)
Definition ByteBuffer.h:164
void FlushBits()
Definition ByteBuffer.h:154
Definition Field.h:16
uint32 GetUInt32() const
Definition Field.h:105
WorldSession * GetSession() const
Definition Player.h:2417
Weather for one zone.
Definition Weather.h:53
void SendPacket(WorldPacket const *packet, bool forced=false)
Send a packet to the client.
WorldDatabaseWorkerPool WorldDatabase
Accessor to the world database.
Definition Main.cpp:40
@ SMSG_WEATHER
Definition Opcodes.h:1065
Weather * AddWeather(uint32 zone_id)
Add a Weather object to the list.
void RemoveWeather(uint32 id)
Remove a Weather object for the given zoneid.
void Update(uint32 diff)
WeatherSeasonChances data[WEATHER_SEASONS]
Definition Weather.h:29
uint32 GetZone() const
For which zone is this weather?
Definition Weather.h:66
#define WEATHER_SEASONS
Definition Weather.h:19
void LoadWeatherData()
uint32 ScriptId
Definition Weather.h:30
void SendFineWeatherUpdateToPlayer(Player *player)
bool UpdateWeather()
Send the new weather to all players in the zone.
Definition Weather.cpp:194
Weather * FindWeather(uint32 id)
Find a Weather object by the given zoneid.
bool ReGenerate()
Calculate the new weather.
Definition Weather.cpp:59
@ WEATHER_STATE_FINE
Definition Weather.h:35