First Commit

For Azeroth!
This commit is contained in:
Yehonal
2016-06-26 10:39:44 +02:00
commit e8e94a0a66
3777 changed files with 1419268 additions and 0 deletions

View File

@@ -0,0 +1,321 @@
/*
* Copyright (C)
* Copyright (C)
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/** \file
\ingroup world
*/
#include "Weather.h"
#include "WorldPacket.h"
#include "Player.h"
#include "World.h"
#include "Log.h"
#include "ObjectMgr.h"
#include "Util.h"
#include "ScriptMgr.h"
#include "Opcodes.h"
/// Create the Weather object
Weather::Weather(uint32 zone, WeatherData const* weatherChances)
: m_zone(zone), m_weatherChances(weatherChances)
{
m_timer.SetInterval(sWorld->getIntConfig(CONFIG_INTERVAL_CHANGEWEATHER));
m_type = WEATHER_TYPE_FINE;
m_grade = 0;
;//sLog->outDetail("WORLD: Starting weather system for zone %u (change every %u minutes).", m_zone, (uint32)(m_timer.GetInterval() / (MINUTE*IN_MILLISECONDS)));
}
/// Launch a weather update
bool Weather::Update(uint32 diff)
{
if (m_timer.GetCurrent() >= 0)
m_timer.Update(diff);
else
m_timer.SetCurrent(0);
///- If the timer has passed, ReGenerate the weather
if (m_timer.Passed())
{
m_timer.Reset();
// update only if Regenerate has changed the weather
if (ReGenerate())
{
///- Weather will be removed if not updated (no players in zone anymore)
if (!UpdateWeather())
return false;
}
}
sScriptMgr->OnWeatherUpdate(this, diff);
return true;
}
/// Calculate the new weather
bool Weather::ReGenerate()
{
if (!m_weatherChances)
{
m_type = WEATHER_TYPE_FINE;
m_grade = 0.0f;
return false;
}
/// Weather statistics:
///- 30% - no change
///- 30% - weather gets better (if not fine) or change weather type
///- 30% - weather worsens (if not fine)
///- 10% - radical change (if not fine)
uint32 u = urand(0, 99);
if (u < 30)
return false;
// remember old values
WeatherType old_type = m_type;
float old_grade = m_grade;
//78 days between January 1st and March 20nd; 365/4=91 days by season
// season source http://aa.usno.navy.mil/data/docs/EarthSeasons.html
time_t gtime = sWorld->GetGameTime();
struct tm ltime;
ACE_OS::localtime_r(&gtime, &ltime);
uint32 season = ((ltime.tm_yday - 78 + 365)/91)%4;
static char const* seasonName[WEATHER_SEASONS] = { "spring", "summer", "fall", "winter" };
;//sLog->outDetail("Generating a change in %s weather for zone %u.", seasonName[season], m_zone);
if ((u < 60) && (m_grade < 0.33333334f)) // Get fair
{
m_type = WEATHER_TYPE_FINE;
m_grade = 0.0f;
}
if ((u < 60) && (m_type != WEATHER_TYPE_FINE)) // Get better
{
m_grade -= 0.33333334f;
return true;
}
if ((u < 90) && (m_type != WEATHER_TYPE_FINE)) // Get worse
{
m_grade += 0.33333334f;
return true;
}
if (m_type != WEATHER_TYPE_FINE)
{
/// Radical change:
///- if light -> heavy
///- if medium -> change weather type
///- if heavy -> 50% light, 50% change weather type
if (m_grade < 0.33333334f)
{
m_grade = 0.9999f; // go nuts
return true;
}
else
{
if (m_grade > 0.6666667f)
{
// Severe change, but how severe?
uint32 rnd = urand(0, 99);
if (rnd < 50)
{
m_grade -= 0.6666667f;
return true;
}
}
m_type = WEATHER_TYPE_FINE; // clear up
m_grade = 0;
}
}
// At this point, only weather that isn't doing anything remains but that have weather data
uint32 chance1 = m_weatherChances->data[season].rainChance;
uint32 chance2 = chance1+ m_weatherChances->data[season].snowChance;
uint32 chance3 = chance2+ m_weatherChances->data[season].stormChance;
uint32 rnd = urand(0, 99);
if (rnd <= chance1)
m_type = WEATHER_TYPE_RAIN;
else if (rnd <= chance2)
m_type = WEATHER_TYPE_SNOW;
else if (rnd <= chance3)
m_type = WEATHER_TYPE_STORM;
else
m_type = WEATHER_TYPE_FINE;
/// New weather statistics (if not fine):
///- 85% light
///- 7% medium
///- 7% heavy
/// If fine 100% sun (no fog)
if (m_type == WEATHER_TYPE_FINE)
{
m_grade = 0.0f;
}
else if (u < 90)
{
m_grade = (float)rand_norm() * 0.3333f;
}
else
{
// Severe change, but how severe?
rnd = urand(0, 99);
if (rnd < 50)
m_grade = (float)rand_norm() * 0.3333f + 0.3334f;
else
m_grade = (float)rand_norm() * 0.3333f + 0.6667f;
}
// return true only in case weather changes
return m_type != old_type || m_grade != old_grade;
}
void Weather::SendWeatherUpdateToPlayer(Player* player)
{
WorldPacket data(SMSG_WEATHER, (4 + 4 + 1));
data << uint32(GetWeatherState());
data << (float)m_grade;
data << uint8(0);
}
/// Send the new weather to all players in the zone
bool Weather::UpdateWeather()
{
///- Send the weather packet to all players in this zone
if (m_grade >= 1)
m_grade = 0.9999f;
else if (m_grade < 0)
m_grade = 0.0001f;
WeatherState state = GetWeatherState();
WorldPacket data(SMSG_WEATHER, (4 + 4 + 1));
data << uint32(state);
data << (float)m_grade;
data << uint8(0);
//- Returns false if there were no players found to update
if (!sWorld->SendZoneMessage(m_zone, &data))
return false;
///- Log the event
char const* wthstr;
switch (state)
{
case WEATHER_STATE_FOG:
wthstr = "fog";
break;
case WEATHER_STATE_LIGHT_RAIN:
wthstr = "light rain";
break;
case WEATHER_STATE_MEDIUM_RAIN:
wthstr = "medium rain";
break;
case WEATHER_STATE_HEAVY_RAIN:
wthstr = "heavy rain";
break;
case WEATHER_STATE_LIGHT_SNOW:
wthstr = "light snow";
break;
case WEATHER_STATE_MEDIUM_SNOW:
wthstr = "medium snow";
break;
case WEATHER_STATE_HEAVY_SNOW:
wthstr = "heavy snow";
break;
case WEATHER_STATE_LIGHT_SANDSTORM:
wthstr = "light sandstorm";
break;
case WEATHER_STATE_MEDIUM_SANDSTORM:
wthstr = "medium sandstorm";
break;
case WEATHER_STATE_HEAVY_SANDSTORM:
wthstr = "heavy sandstorm";
break;
case WEATHER_STATE_THUNDERS:
wthstr = "thunders";
break;
case WEATHER_STATE_BLACKRAIN:
wthstr = "blackrain";
break;
case WEATHER_STATE_FINE:
default:
wthstr = "fine";
break;
}
;//sLog->outDetail("Change the weather of zone %u to %s.", m_zone, wthstr);
sScriptMgr->OnWeatherChange(this, state, m_grade);
return true;
}
/// Set the weather
void Weather::SetWeather(WeatherType type, float grade)
{
if (m_type == type && m_grade == grade)
return;
m_type = type;
m_grade = grade;
UpdateWeather();
}
/// Get the sound number associated with the current weather
WeatherState Weather::GetWeatherState() const
{
if (m_grade<0.27f)
return WEATHER_STATE_FINE;
switch (m_type)
{
case WEATHER_TYPE_RAIN:
if (m_grade<0.40f)
return WEATHER_STATE_LIGHT_RAIN;
else if (m_grade<0.70f)
return WEATHER_STATE_MEDIUM_RAIN;
else
return WEATHER_STATE_HEAVY_RAIN;
case WEATHER_TYPE_SNOW:
if (m_grade<0.40f)
return WEATHER_STATE_LIGHT_SNOW;
else if (m_grade<0.70f)
return WEATHER_STATE_MEDIUM_SNOW;
else
return WEATHER_STATE_HEAVY_SNOW;
case WEATHER_TYPE_STORM:
if (m_grade<0.40f)
return WEATHER_STATE_LIGHT_SANDSTORM;
else if (m_grade<0.70f)
return WEATHER_STATE_MEDIUM_SANDSTORM;
else
return WEATHER_STATE_HEAVY_SANDSTORM;
case WEATHER_TYPE_BLACKRAIN:
return WEATHER_STATE_BLACKRAIN;
case WEATHER_TYPE_THUNDERS:
return WEATHER_STATE_THUNDERS;
case WEATHER_TYPE_FINE:
default:
return WEATHER_STATE_FINE;
}
}

View File

@@ -0,0 +1,92 @@
/*
* Copyright (C)
* Copyright (C)
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/// \addtogroup world
/// @{
/// \file
#ifndef __WEATHER_H
#define __WEATHER_H
#include "Common.h"
#include "SharedDefines.h"
#include "Timer.h"
class Player;
#define WEATHER_SEASONS 4
struct WeatherSeasonChances
{
uint32 rainChance;
uint32 snowChance;
uint32 stormChance;
};
struct WeatherData
{
WeatherSeasonChances data[WEATHER_SEASONS];
uint32 ScriptId;
};
enum WeatherState
{
WEATHER_STATE_FINE = 0,
WEATHER_STATE_FOG = 1,
WEATHER_STATE_LIGHT_RAIN = 3,
WEATHER_STATE_MEDIUM_RAIN = 4,
WEATHER_STATE_HEAVY_RAIN = 5,
WEATHER_STATE_LIGHT_SNOW = 6,
WEATHER_STATE_MEDIUM_SNOW = 7,
WEATHER_STATE_HEAVY_SNOW = 8,
WEATHER_STATE_LIGHT_SANDSTORM = 22,
WEATHER_STATE_MEDIUM_SANDSTORM = 41,
WEATHER_STATE_HEAVY_SANDSTORM = 42,
WEATHER_STATE_THUNDERS = 86,
WEATHER_STATE_BLACKRAIN = 90,
WEATHER_STATE_BLACKSNOW = 106
};
/// Weather for one zone
class Weather
{
public:
Weather(uint32 zone, WeatherData const* weatherChances);
~Weather() { };
bool Update(uint32 diff);
bool ReGenerate();
bool UpdateWeather();
void SendWeatherUpdateToPlayer(Player* player);
void SetWeather(WeatherType type, float grade);
/// For which zone is this weather?
uint32 GetZone() const { return m_zone; };
uint32 GetScriptId() const { return m_weatherChances->ScriptId; }
private:
WeatherState GetWeatherState() const;
uint32 m_zone;
WeatherType m_type;
float m_grade;
IntervalTimer m_timer;
WeatherData const* m_weatherChances;
};
#endif

View File

@@ -0,0 +1,172 @@
/*
* Copyright (C)
* Copyright (C)
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/** \file
\ingroup world
*/
#include "WeatherMgr.h"
#include "Weather.h"
#include "Log.h"
#include "ObjectMgr.h"
#include "AutoPtr.h"
#include "Player.h"
#include "WorldPacket.h"
#include "Opcodes.h"
namespace WeatherMgr
{
namespace
{
typedef UNORDERED_MAP<uint32, Trinity::AutoPtr<Weather, ACE_Null_Mutex> > WeatherMap;
typedef 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;
}
}
/// Find a Weather object by the given zoneid
Weather* FindWeather(uint32 id)
{
WeatherMap::const_iterator itr = m_weathers.find(id);
return (itr != m_weathers.end()) ? itr->second.get() : 0;
}
/// 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;
}
void LoadWeatherData()
{
uint32 oldMSTime = getMSTime();
uint32 count = 0;
QueryResult result = WorldDatabase.Query("SELECT "
"zone, spring_rain_chance, spring_snow_chance, spring_storm_chance,"
"summer_rain_chance, summer_snow_chance, summer_storm_chance,"
"fall_rain_chance, fall_snow_chance, fall_storm_chance,"
"winter_rain_chance, winter_snow_chance, winter_storm_chance,"
"ScriptName FROM game_weather");
if (!result)
{
sLog->outErrorDb(">> Loaded 0 weather definitions. DB table `game_weather` is empty.");
sLog->outString();
return;
}
do
{
Field* fields = result->Fetch();
uint32 zone_id = fields[0].GetUInt32();
WeatherData& wzc = mWeatherZoneMap[zone_id];
for (uint8 season = 0; season < WEATHER_SEASONS; ++season)
{
wzc.data[season].rainChance = fields[season * (MAX_WEATHER_TYPE-1) + 1].GetUInt8();
wzc.data[season].snowChance = fields[season * (MAX_WEATHER_TYPE-1) + 2].GetUInt8();
wzc.data[season].stormChance = fields[season * (MAX_WEATHER_TYPE-1) + 3].GetUInt8();
if (wzc.data[season].rainChance > 100)
{
wzc.data[season].rainChance = 25;
sLog->outErrorDb("Weather for zone %u season %u has wrong rain chance > 100%%", zone_id, season);
}
if (wzc.data[season].snowChance > 100)
{
wzc.data[season].snowChance = 25;
sLog->outErrorDb("Weather for zone %u season %u has wrong snow chance > 100%%", zone_id, season);
}
if (wzc.data[season].stormChance > 100)
{
wzc.data[season].stormChance = 25;
sLog->outErrorDb("Weather for zone %u season %u has wrong storm chance > 100%%", zone_id, season);
}
}
wzc.ScriptId = sObjectMgr->GetScriptId(fields[13].GetCString());
++count;
}
while (result->NextRow());
sLog->outString(">> Loaded %u weather definitions in %u ms", count, GetMSTimeDiffToNow(oldMSTime));
sLog->outString();
}
void SendFineWeatherUpdateToPlayer(Player* player)
{
WorldPacket data(SMSG_WEATHER, (4 + 4 + 1));
data << (uint32)WEATHER_STATE_FINE;
data << (float)0.0f;
data << uint8(0);
player->GetSession()->SendPacket(&data);
}
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

View File

@@ -0,0 +1,44 @@
/*
* Copyright (C)
* Copyright (C)
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/// \addtogroup world
/// @{
/// \file
#ifndef __WEATHERMGR_H
#define __WEATHERMGR_H
#include "Define.h"
class Weather;
class Player;
namespace WeatherMgr
{
void LoadWeatherData();
Weather* FindWeather(uint32 id);
Weather* AddWeather(uint32 zone_id);
void RemoveWeather(uint32 zone_id);
void SendFineWeatherUpdateToPlayer(Player* player);
void Update(uint32 diff);
}
#endif