Project SkyFire Core
SkyFire 5.4.8 server core API documentation
Toggle main menu visibility
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
20
namespace
WeatherMgr
21
{
22
namespace
23
{
24
typedef
UNORDERED_MAP<uint32, Skyfire::AutoPtr<Weather, Skyfire::NullMutex>
> WeatherMap;
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
38
Weather
*
FindWeather
(
uint32
id
)
39
{
40
WeatherMap::const_iterator itr = m_weathers.find(
id
);
41
return
(itr != m_weathers.end()) ? itr->second.get() : 0;
42
}
43
45
void
RemoveWeather
(
uint32
id
)
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
55
Weather
*
AddWeather
(
uint32
zone_id)
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
71
void
LoadWeatherData
()
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
131
void
SendFineWeatherUpdateToPlayer
(
Player
* player)
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
AutoPtr.h
uint8
std::uint8_t uint8
Definition
Define.h:79
uint32
std::uint32_t uint32
Definition
Define.h:77
Log.h
SF_LOG_ERROR
#define SF_LOG_ERROR(filterType__,...)
Definition
Log.h:143
SF_LOG_INFO
#define SF_LOG_INFO(filterType__,...)
Definition
Log.h:137
ObjectMgr.h
sObjectMgr
#define sObjectMgr
Definition
ObjectMgr.h:1617
Opcodes.h
Player.h
QueryResult
Skyfire::AutoPtr< ResultSet, Skyfire::Mutex > QueryResult
Definition
QueryResult.h:48
MAX_WEATHER_TYPE
#define MAX_WEATHER_TYPE
Definition
SharedDefines.h:3659
GetMSTimeDiffToNow
uint32 GetMSTimeDiffToNow(uint32 oldMSTime)
Definition
Timer.h:22
getMSTime
uint32 getMSTime()
Definition
Timer.h:12
UNORDERED_MAP
#define UNORDERED_MAP
Definition
UnorderedMap.h:58
Weather.h
WeatherMgr.h
WorldPacket.h
WorldSession.h
ByteBuffer::WriteBit
bool WriteBit(uint32 bit)
Definition
ByteBuffer.h:164
ByteBuffer::FlushBits
void FlushBits()
Definition
ByteBuffer.h:154
Field
Definition
Field.h:16
Field::GetUInt32
uint32 GetUInt32() const
Definition
Field.h:105
Player
Definition
Player.h:1289
Player::GetSession
WorldSession * GetSession() const
Definition
Player.h:2417
Weather
Weather for one zone.
Definition
Weather.h:53
WorldPacket
Definition
WorldPacket.h:16
WorldSession::SendPacket
void SendPacket(WorldPacket const *packet, bool forced=false)
Send a packet to the client.
Definition
WorldSession.cpp:196
WorldDatabase
WorldDatabaseWorkerPool WorldDatabase
Accessor to the world database.
Definition
Main.cpp:40
SMSG_WEATHER
@ SMSG_WEATHER
Definition
Opcodes.h:1065
WeatherMgr::AddWeather
Weather * AddWeather(uint32 zone_id)
Add a Weather object to the list.
Definition
WeatherMgr.cpp:55
WeatherMgr::RemoveWeather
void RemoveWeather(uint32 id)
Remove a Weather object for the given zoneid.
Definition
WeatherMgr.cpp:45
WeatherMgr::Update
void Update(uint32 diff)
Definition
WeatherMgr.cpp:141
WeatherSeasonChances::snowChance
uint32 snowChance
Definition
Weather.h:23
WeatherData::data
WeatherSeasonChances data[WEATHER_SEASONS]
Definition
Weather.h:29
Weather::GetZone
uint32 GetZone() const
For which zone is this weather?
Definition
Weather.h:66
WEATHER_SEASONS
#define WEATHER_SEASONS
Definition
Weather.h:19
WeatherMgr::LoadWeatherData
void LoadWeatherData()
Definition
WeatherMgr.cpp:71
WeatherSeasonChances::rainChance
uint32 rainChance
Definition
Weather.h:22
WeatherData::ScriptId
uint32 ScriptId
Definition
Weather.h:30
WeatherSeasonChances::stormChance
uint32 stormChance
Definition
Weather.h:24
WeatherMgr::SendFineWeatherUpdateToPlayer
void SendFineWeatherUpdateToPlayer(Player *player)
Definition
WeatherMgr.cpp:131
Weather::UpdateWeather
bool UpdateWeather()
Send the new weather to all players in the zone.
Definition
Weather.cpp:194
WeatherMgr::FindWeather
Weather * FindWeather(uint32 id)
Find a Weather object by the given zoneid.
Definition
WeatherMgr.cpp:38
Weather::ReGenerate
bool ReGenerate()
Calculate the new weather.
Definition
Weather.cpp:59
WEATHER_STATE_FINE
@ WEATHER_STATE_FINE
Definition
Weather.h:35
WeatherMgr
Definition
WeatherMgr.cpp:21
WeatherData
Definition
Weather.h:28
src
server
game
Weather
WeatherMgr.cpp
Generated on
for Project SkyFire Core by
1.17.0