Core/Weather: Fixed weather updates to be thread safe

This commit is contained in:
Shauren
2017-09-14 22:33:40 +02:00
parent 6d2bc7abf4
commit 6eb9973947
11 changed files with 103 additions and 146 deletions

View File

@@ -21,64 +21,24 @@
*/
#include "WeatherMgr.h"
#include "Containers.h"
#include "DatabaseEnv.h"
#include "Log.h"
#include "MiscPackets.h"
#include "ObjectMgr.h"
#include "Player.h"
#include "Timer.h"
#include "Weather.h"
#include "WorldSession.h"
namespace WeatherMgr
{
namespace
{
typedef std::unordered_map<uint32, std::shared_ptr<Weather> > WeatherMap;
typedef std::unordered_map<uint32, WeatherData> WeatherZoneMap;
WeatherMap m_weathers;
WeatherZoneMap mWeatherZoneMap;
WeatherData const* GetWeatherData(uint32 zone_id)
{
WeatherZoneMap::const_iterator itr = mWeatherZoneMap.find(zone_id);
return (itr != mWeatherZoneMap.end()) ? &itr->second : NULL;
}
std::unordered_map<uint32, WeatherData> _weatherData;
}
/// Find a Weather object by the given zoneid
Weather* FindWeather(uint32 id)
WeatherData const* GetWeatherData(uint32 zone_id)
{
WeatherMap::const_iterator itr = m_weathers.find(id);
return (itr != m_weathers.end()) ? itr->second.get() : nullptr;
}
/// Remove a Weather object for the given zoneid
void RemoveWeather(uint32 id)
{
// not called at the moment. Kept for completeness
WeatherMap::iterator itr = m_weathers.find(id);
if (itr != m_weathers.end())
m_weathers.erase(itr);
}
/// Add a Weather object to the list
Weather* AddWeather(uint32 zone_id)
{
WeatherData const* weatherChances = GetWeatherData(zone_id);
// zone does not have weather, ignore
if (!weatherChances)
return NULL;
Weather* w = new Weather(zone_id, weatherChances);
m_weathers[w->GetZone()].reset(w);
w->ReGenerate();
w->UpdateWeather();
return w;
return Trinity::Containers::MapGetValuePtr(_weatherData, zone_id);
}
void LoadWeatherData()
@@ -106,7 +66,7 @@ void LoadWeatherData()
uint32 zone_id = fields[0].GetUInt32();
WeatherData& wzc = mWeatherZoneMap[zone_id];
WeatherData& wzc = _weatherData[zone_id];
for (uint8 season = 0; season < WEATHER_SEASONS; ++season)
{
@@ -142,26 +102,4 @@ void LoadWeatherData()
TC_LOG_INFO("server.loading", ">> Loaded %u weather definitions in %u ms", count, GetMSTimeDiffToNow(oldMSTime));
}
void SendFineWeatherUpdateToPlayer(Player* player)
{
WorldPackets::Misc::Weather weather(WEATHER_STATE_FINE);
player->GetSession()->SendPacket(weather.Write());
}
void Update(uint32 diff)
{
///- Send an update signal to Weather objects
WeatherMap::iterator itr, next;
for (itr = m_weathers.begin(); itr != m_weathers.end(); itr = next)
{
next = itr;
++next;
///- and remove Weather objects for zones with no player
// As interval > WorldTick
if (!itr->second->Update(diff))
m_weathers.erase(itr);
}
}
} // namespace