TrinityCore
BattlefieldMgr.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 "BattlefieldMgr.h"
19#include "Containers.h"
20#include "DatabaseEnv.h"
21#include "Log.h"
22#include "Map.h"
23#include "ObjectMgr.h"
24#include "Player.h"
25#include "ScriptMgr.h"
26
27namespace
28{
29constexpr std::array<uint32, BATTLEFIELD_MAX> BattlefieldIdToMapId = { 0, 571, 732 };
30constexpr std::array<uint32, BATTLEFIELD_MAX> BattlefieldIdToZoneId = { 0, 4197, 5095 }; // imitate World_PVP_Area.db2
31std::array<uint32, BATTLEFIELD_MAX> BattlefieldIdToScriptId = { 0, 0, 0 };
32}
33
35{
36 _updateTimer = 0;
37}
38
40
42{
44 return &instance;
45}
46
48{
49 uint32 oldMSTime = getMSTime();
50
51 uint32 count = 0;
52
53 if (QueryResult result = WorldDatabase.Query("SELECT TypeId, ScriptName FROM battlefield_template"))
54 {
55 do
56 {
57 Field* fields = result->Fetch();
58
59 uint32 typeId = fields[0].GetUInt8();
60
61 if (typeId >= BATTLEFIELD_MAX)
62 {
63 TC_LOG_ERROR("sql.sql", "BattlefieldMgr::InitBattlefield: Invalid TypeId value {} in battlefield_template, skipped.", typeId);
64 continue;
65 }
66
67 BattlefieldIdToScriptId[typeId] = sObjectMgr->GetScriptId(fields[1].GetString());
68
69 ++count;
70 } while (result->NextRow());
71 }
72
73 TC_LOG_INFO("server.loading", ">> Loaded {} battlefields in {} ms", count, GetMSTimeDiffToNow(oldMSTime));
74}
75
77{
78 for (uint32 i = 0; i < BATTLEFIELD_MAX; ++i)
79 {
80 if (!BattlefieldIdToScriptId[i])
81 continue;
82
83 if (BattlefieldIdToMapId[i] != map->GetId())
84 continue;
85
86 Battlefield* bf = sScriptMgr->CreateBattlefield(BattlefieldIdToScriptId[i], map);
87 if (!bf)
88 continue;
89
90 if (!bf->SetupBattlefield())
91 {
92 TC_LOG_INFO("bg.battlefield", "Setting up battlefield with TypeId {} on map {} instance id {} failed.", i, map->GetId(), map->GetInstanceId());
93 delete bf;
94 }
95
96 _battlefieldsByMap[map].emplace_back(bf);
97 TC_LOG_INFO("bg.battlefield", "Setting up battlefield with TypeId {} on map {} instance id {} succeeded.", i, map->GetId(), map->GetInstanceId());
98 }
99}
100
102{
103 _battlefieldsByMap.erase(map);
104}
105
107{
108 _battlefieldsByZone[{ bf->GetMap(), zoneId }] = bf;
109}
110
112{
113 auto itr = _battlefieldsByZone.find({ player->GetMap(), zoneId });
114 if (itr == _battlefieldsByZone.end())
115 return;
116
117 Battlefield* bf = itr->second;
118 if (!bf->IsEnabled() || bf->HasPlayer(player))
119 return;
120
121 bf->HandlePlayerEnterZone(player, zoneId);
122 TC_LOG_DEBUG("bg.battlefield", "Player {} entered battlefield id {}", player->GetGUID().ToString(), bf->GetTypeId());
123}
124
126{
127 auto itr = _battlefieldsByZone.find({ player->GetMap(), zoneId });
128 if (itr == _battlefieldsByZone.end())
129 return;
130
131 // teleport: remove once in removefromworld, once in updatezone
132 if (!itr->second->HasPlayer(player))
133 return;
134
135 itr->second->HandlePlayerLeaveZone(player, zoneId);
136 TC_LOG_DEBUG("bg.battlefield", "Player {} left battlefield id {}", player->GetGUID().ToString(), itr->second->GetTypeId());
137}
138
140{
141 return std::find(BattlefieldIdToZoneId.begin(), BattlefieldIdToZoneId.end(), zoneId) != BattlefieldIdToZoneId.end();
142}
143
145{
146 auto itr = _battlefieldsByZone.find({ map, zoneId });
147 if (itr == _battlefieldsByZone.end())
148 {
149 // no handle for this zone, return
150 return nullptr;
151 }
152
153 if (!itr->second->IsEnabled())
154 return nullptr;
155
156 return itr->second;
157}
158
160{
161 if (BattlefieldsMapByMap::mapped_type const* battlefields = Trinity::Containers::MapGetValuePtr(_battlefieldsByMap, map))
162 for (std::unique_ptr<Battlefield> const& battlefield : *battlefields)
163 if (battlefield->GetBattleId() == battleId)
164 return battlefield.get();
165
166 return nullptr;
167}
168
170{
171 _updateTimer += diff;
173 {
174 for (auto const& [map, battlefields] : _battlefieldsByMap)
175 for (std::unique_ptr<Battlefield> const& bfItr : battlefields)
176 if (bfItr->IsEnabled())
177 bfItr->Update(_updateTimer);
178
179 _updateTimer = 0;
180 }
181}
@ BATTLEFIELD_MAX
Definition: Battlefield.h:31
@ BATTLEFIELD_OBJECTIVE_UPDATE_INTERVAL
Definition: Battlefield.h:68
std::shared_ptr< ResultSet > QueryResult
DatabaseWorkerPool< WorldDatabaseConnection > WorldDatabase
Accessor to the world database.
Definition: DatabaseEnv.cpp:20
uint32_t uint32
Definition: Define.h:142
#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 sScriptMgr
Definition: ScriptMgr.h:1418
uint32 GetMSTimeDiffToNow(uint32 oldMSTime)
Definition: Timer.h:57
uint32 getMSTime()
Definition: Timer.h:33
BattlefieldMapByZone _battlefieldsByZone
void Update(uint32 diff)
void DestroyBattlefieldsForMap(Map const *map)
void CreateBattlefieldsForMap(Map *map)
void HandlePlayerLeaveZone(Player *player, uint32 zoneId)
bool IsWorldPvpArea(uint32 zoneId) const
BattlefieldsMapByMap _battlefieldsByMap
Battlefield * GetBattlefieldToZoneId(Map const *map, uint32 zoneId)
void HandlePlayerEnterZone(Player *player, uint32 zoneId)
Battlefield * GetBattlefieldByBattleId(Map const *map, uint32 battleId)
static BattlefieldMgr * instance()
void AddZone(uint32 zoneId, Battlefield *bf)
Map * GetMap() const
Definition: Battlefield.h:183
virtual bool SetupBattlefield()
Call this to init the Battlefield.
Definition: Battlefield.h:159
bool HasPlayer(Player *player) const
bool IsEnabled() const
Return if battlefield is enable.
Definition: Battlefield.h:196
void HandlePlayerEnterZone(Player *player, uint32 zone)
Called when player (player) enter in zone.
Definition: Battlefield.cpp:70
uint32 GetTypeId() const
Definition: Battlefield.h:180
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
uint32 GetInstanceId() const
Definition: Map.h:314
std::string ToString() const
Definition: ObjectGuid.cpp:554
static ObjectGuid GetGUID(Object const *o)
Definition: Object.h:159
Map * GetMap() const
Definition: Object.h:624
auto MapGetValuePtr(M &map, typename M::key_type const &key)
Definition: MapUtils.h:29