TrinityCore
OutdoorPvPMgr.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
18#include "OutdoorPvPMgr.h"
19#include "Containers.h"
20#include "DatabaseEnv.h"
21#include "DB2Stores.h"
22#include "DisableMgr.h"
23#include "Log.h"
24#include "Map.h"
25#include "ObjectMgr.h"
26#include "Player.h"
27#include "ScriptMgr.h"
28
30{
31 m_UpdateTimer = 0;
32}
33
35
37{
38 m_OutdoorPvPByMap.clear();
39
40 m_OutdoorPvPDatas.fill(0);
41
42 m_OutdoorPvPMap.clear();
43}
44
46{
48 return &instance;
49}
50
52{
53 uint32 oldMSTime = getMSTime();
54
55 // 0 1
56 QueryResult result = WorldDatabase.Query("SELECT TypeId, ScriptName FROM outdoorpvp_template");
57 if (!result)
58 {
59 TC_LOG_INFO("server.loading", ">> Loaded 0 outdoor PvP definitions. DB table `outdoorpvp_template` is empty.");
60 return;
61 }
62
63 uint32 count = 0;
64 uint32 typeId = 0;
65
66 do
67 {
68 Field* fields = result->Fetch();
69
70 typeId = fields[0].GetUInt8();
71
73 continue;
74
75 if (typeId >= MAX_OUTDOORPVP_TYPES)
76 {
77 TC_LOG_ERROR("sql.sql", "Invalid OutdoorPvPTypes value {} in outdoorpvp_template; skipped.", typeId);
78 continue;
79 }
80
81 m_OutdoorPvPDatas[typeId] = sObjectMgr->GetScriptId(fields[1].GetString());
82
83 ++count;
84 }
85 while (result->NextRow());
86
87 TC_LOG_INFO("server.loading", ">> Loaded {} outdoor PvP definitions in {} ms", count, GetMSTimeDiffToNow(oldMSTime));
88}
89
91{
92 for (uint8 i = 1; i < MAX_OUTDOORPVP_TYPES; ++i)
93 {
94 if (map->GetId() != m_OutdoorMapIds[i])
95 continue;
96
97 if (!m_OutdoorPvPDatas[i])
98 {
99 TC_LOG_ERROR("sql.sql", "Could not initialize OutdoorPvP object for type ID {}; no entry in database.", uint32(i));
100 continue;
101 }
102
103 OutdoorPvP* pvp = sScriptMgr->CreateOutdoorPvP(m_OutdoorPvPDatas[i], map);
104 if (!pvp)
105 {
106 TC_LOG_ERROR("outdoorpvp", "Could not initialize OutdoorPvP object for type ID {}; got NULL pointer from script.", uint32(i));
107 continue;
108 }
109
110 if (!pvp->SetupOutdoorPvP())
111 {
112 TC_LOG_ERROR("outdoorpvp", "Could not initialize OutdoorPvP object for type ID {}; SetupOutdoorPvP failed.", uint32(i));
113 delete pvp;
114 continue;
115 }
116
117 m_OutdoorPvPByMap[map].emplace_back(pvp);
118 }
119}
120
122{
123 Trinity::Containers::EraseIf(m_OutdoorPvPMap, [map](OutdoorPvPMap::value_type const& pair)
124 {
125 return pair.first.first == map;
126 });
127 m_OutdoorPvPByMap.erase(map);
128}
129
131{
132 m_OutdoorPvPMap[{ handle->GetMap(), zoneid }] = handle;
133}
134
136{
137 OutdoorPvPMap::iterator itr = m_OutdoorPvPMap.find({ player->GetMap(), zoneid });
138 if (itr == m_OutdoorPvPMap.end())
139 return;
140
141 if (itr->second->HasPlayer(player))
142 return;
143
144 itr->second->HandlePlayerEnterZone(player, zoneid);
145 TC_LOG_DEBUG("outdoorpvp", "Player {} entered outdoorpvp id {}", player->GetGUID().ToString(), itr->second->GetTypeId());
146}
147
149{
150 OutdoorPvPMap::iterator itr = m_OutdoorPvPMap.find({ player->GetMap(), zoneid });
151 if (itr == m_OutdoorPvPMap.end())
152 return;
153
154 // teleport: remove once in removefromworld, once in updatezone
155 if (!itr->second->HasPlayer(player))
156 return;
157
158 itr->second->HandlePlayerLeaveZone(player, zoneid);
159 TC_LOG_DEBUG("outdoorpvp", "Player {} left outdoorpvp id {}", player->GetGUID().ToString(), itr->second->GetTypeId());
160}
161
163{
164 OutdoorPvPMap::iterator itr = m_OutdoorPvPMap.find({ map, zoneid });
165 if (itr == m_OutdoorPvPMap.end())
166 {
167 // no handle for this zone, return
168 return nullptr;
169 }
170 return itr->second;
171}
172
174{
175 m_UpdateTimer += diff;
177 {
178 for (auto mapItr = m_OutdoorPvPByMap.begin(); mapItr != m_OutdoorPvPByMap.end(); ++mapItr)
179 for (auto itr = mapItr->second.begin(); itr != mapItr->second.end(); ++itr)
180 (*itr)->Update(m_UpdateTimer);
181 m_UpdateTimer = 0;
182 }
183}
184
186{
187 if (OutdoorPvP* pvp = player->GetOutdoorPvP())
188 if (pvp->HasPlayer(player))
189 return pvp->HandleCustomSpell(player, spellId, go);
190
191 return false;
192}
193
195{
196 if (OutdoorPvP* pvp = player->GetOutdoorPvP())
197 if (pvp->HasPlayer(player))
198 return pvp->HandleOpenGo(player, go);
199
200 return false;
201}
202
204{
205 if (OutdoorPvP* pvp = player->GetOutdoorPvP())
206 if (pvp->HasPlayer(player))
207 pvp->HandleDropFlag(player, spellId);
208}
209
211{
212 if (OutdoorPvP* pvp = player->GetOutdoorPvP())
213 if (pvp->HasPlayer(player))
214 pvp->HandlePlayerResurrects(player, zoneid);
215}
216
218{
219 if (BroadcastTextEntry const* bct = sBroadcastTextStore.LookupEntry(id))
220 return DB2Manager::GetBroadcastTextValue(bct, locale);
221
222 TC_LOG_ERROR("outdoorpvp", "Can not find DefenseMessage (Zone: {}, Id: {}). BroadcastText (Id: {}) does not exist.", zoneId, id, id);
223 return "";
224}
LocaleConstant
Definition: Common.h:48
DB2Storage< BroadcastTextEntry > sBroadcastTextStore("BroadcastText.db2", &BroadcastTextLoadInfo::Instance)
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
@ DISABLE_TYPE_OUTDOORPVP
Definition: DisableMgr.h:32
#define TC_LOG_DEBUG(filterType__,...)
Definition: Log.h:156
#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 OUTDOORPVP_OBJECTIVE_UPDATE_INTERVAL
Definition: OutdoorPvPMgr.h:21
@ MAX_OUTDOORPVP_TYPES
Definition: OutdoorPvP.h:36
#define sScriptMgr
Definition: ScriptMgr.h:1418
uint32 GetMSTimeDiffToNow(uint32 oldMSTime)
Definition: Timer.h:57
uint32 getMSTime()
Definition: Timer.h:33
static char const * GetBroadcastTextValue(BroadcastTextEntry const *broadcastText, LocaleConstant locale=DEFAULT_LOCALE, uint8 gender=GENDER_MALE, bool forceGender=false)
Definition: DB2Stores.cpp:2008
Class used to access individual fields of database query result.
Definition: Field.h:90
uint8 GetUInt8() const
Definition: Field.cpp:30
Definition: Map.h:189
uint32 GetId() const
Definition: Map.cpp:3228
std::string ToString() const
Definition: ObjectGuid.cpp:554
static ObjectGuid GetGUID(Object const *o)
Definition: Object.h:159
bool HandleCustomSpell(Player *player, uint32 spellId, GameObject *go)
static OutdoorPvPMgr * instance()
OutdoorPvPMap m_OutdoorPvPMap
void CreateOutdoorPvPForMap(Map *map)
void AddZone(uint32 zoneid, OutdoorPvP *handle)
void HandlePlayerLeaveZone(Player *player, uint32 areaflag)
OutdoorPvPScriptIds m_OutdoorMapIds
bool HandleOpenGo(Player *player, GameObject *go)
void Update(uint32 diff)
void HandleDropFlag(Player *player, uint32 spellId)
OutdoorPvP * GetOutdoorPvPToZoneId(Map *map, uint32 zoneid)
void InitOutdoorPvP()
void DestroyOutdoorPvPForMap(Map *map)
std::unordered_map< Map *, std::vector< std::unique_ptr< OutdoorPvP > > > m_OutdoorPvPByMap
Definition: OutdoorPvPMgr.h:96
OutdoorPvPScriptIds m_OutdoorPvPDatas
void HandlePlayerEnterZone(Player *player, uint32 areaflag)
uint32 m_UpdateTimer
void HandlePlayerResurrects(Player *player, uint32 areaflag)
std::string GetDefenseMessage(uint32 zoneId, uint32 id, LocaleConstant locale) const
virtual bool SetupOutdoorPvP()
Definition: OutdoorPvP.h:156
Map * GetMap() const
Definition: OutdoorPvP.h:195
OutdoorPvP * GetOutdoorPvP() const
Definition: Player.cpp:25439
Map * GetMap() const
Definition: Object.h:624
bool IsDisabledFor(DisableType type, uint32 entry, WorldObject const *ref, uint8 flags)
Definition: DisableMgr.cpp:285
void EraseIf(Container &c, Predicate p)
Definition: Containers.h:279