TrinityCore
Loading...
Searching...
No Matches
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 "DatabaseEnv.h"
20#include "ObjectMgr.h"
21#include "Log.h"
22#include "Player.h"
23#include "ScriptMgr.h"
24
29
31{
32 for (BattlefieldSet::iterator itr = _battlefieldSet.begin(); itr != _battlefieldSet.end(); ++itr)
33 delete *itr;
34
35 _battlefieldMap.clear();
36}
37
43
45{
46 uint32 oldMSTime = getMSTime();
47
48 uint32 count = 0;
49
50 if (QueryResult result = WorldDatabase.Query("SELECT TypeId, ScriptName FROM battlefield_template"))
51 {
52 do
53 {
54 Field* fields = result->Fetch();
55
56 uint32 typeId = fields[0].GetUInt8();
57
58 if (typeId >= BATTLEFIELD_MAX)
59 {
60 TC_LOG_ERROR("sql.sql", "BattlefieldMgr::InitBattlefield: Invalid TypeId value {} in battlefield_template, skipped.", typeId);
61 continue;
62 }
63
64 uint32 scriptId = sObjectMgr->GetScriptId(fields[1].GetString());
65
66 Battlefield* bf = sScriptMgr->CreateBattlefield(scriptId);
67 if (!bf)
68 continue;
69
70 if (!bf->SetupBattlefield())
71 {
72 TC_LOG_INFO("bg.battlefield", "Setting up battlefield with TypeId {} failed.", typeId);
73 delete bf;
74 }
75 else
76 {
77 _battlefieldSet.push_back(bf);
78 TC_LOG_INFO("bg.battlefield", "Setting up battlefield with TypeId {} succeeded.", typeId);
79 }
80
81 ++count;
82 } while (result->NextRow());
83 }
84
85 TC_LOG_INFO("server.loading", ">> Loaded {} battlefields in {} ms", count, GetMSTimeDiffToNow(oldMSTime));
86}
87
89{
90 _battlefieldMap[zoneId] = bf;
91}
92
94{
95 BattlefieldMap::iterator itr = _battlefieldMap.find(zoneId);
96 if (itr == _battlefieldMap.end())
97 return;
98
99 Battlefield* bf = itr->second;
100 if (!bf->IsEnabled() || bf->HasPlayer(player))
101 return;
102
103 bf->HandlePlayerEnterZone(player, zoneId);
104 TC_LOG_DEBUG("bg.battlefield", "Player {} entered battlefield id {}", player->GetGUID().ToString(), bf->GetTypeId());
105}
106
108{
109 BattlefieldMap::iterator itr = _battlefieldMap.find(zoneId);
110 if (itr == _battlefieldMap.end())
111 return;
112
113 // teleport: remove once in removefromworld, once in updatezone
114 if (!itr->second->HasPlayer(player))
115 return;
116
117 itr->second->HandlePlayerLeaveZone(player, zoneId);
118 TC_LOG_DEBUG("bg.battlefield", "Player {} left battlefield id {}", player->GetGUID().ToString(), itr->second->GetTypeId());
119}
120
122{
123 BattlefieldMap::iterator itr = _battlefieldMap.find(zoneId);
124 if (itr == _battlefieldMap.end())
125 {
126 // no handle for this zone, return
127 return nullptr;
128 }
129
130 if (!itr->second->IsEnabled())
131 return nullptr;
132
133 return itr->second;
134}
135
137{
138 for (BattlefieldSet::iterator itr = _battlefieldSet.begin(); itr != _battlefieldSet.end(); ++itr)
139 {
140 if ((*itr)->GetBattleId() == battleId)
141 return *itr;
142 }
143 return nullptr;
144}
145
147{
148 BattlefieldMap::iterator itr = _battlefieldMap.find(zoneId);
149 if (itr != _battlefieldMap.end())
150 return itr->second;
151
152 return nullptr;
153}
154
156{
157 _updateTimer += diff;
159 {
160 for (BattlefieldSet::iterator itr = _battlefieldSet.begin(); itr != _battlefieldSet.end(); ++itr)
161 if ((*itr)->IsEnabled())
162 (*itr)->Update(_updateTimer);
163 _updateTimer = 0;
164 }
165}
@ BATTLEFIELD_MAX
Definition Battlefield.h:29
@ BATTLEFIELD_OBJECTIVE_UPDATE_INTERVAL
Definition Battlefield.h:57
std::shared_ptr< ResultSet > QueryResult
DatabaseWorkerPool< WorldDatabaseConnection > WorldDatabase
Accessor to the world database.
uint32_t uint32
Definition Define.h:133
#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:1721
#define sScriptMgr
Definition ScriptMgr.h:1168
uint32 GetMSTimeDiffToNow(uint32 oldMSTime)
Definition Timer.h:57
uint32 getMSTime()
Definition Timer.h:33
BattlefieldMap _battlefieldMap
void Update(uint32 diff)
Battlefield * GetBattlefieldToZoneId(uint32 zoneId)
void HandlePlayerLeaveZone(Player *player, uint32 zoneId)
BattlefieldSet _battlefieldSet
Battlefield * GetBattlefieldByBattleId(uint32 battleId)
void HandlePlayerEnterZone(Player *player, uint32 zoneId)
ZoneScript * GetZoneScript(uint32 zoneId)
static BattlefieldMgr * instance()
void AddZone(uint32 zoneId, Battlefield *bf)
virtual bool SetupBattlefield()
Call this to init the Battlefield.
bool HasPlayer(Player *player) const
bool IsEnabled() const
Return if battlefield is enable.
void HandlePlayerEnterZone(Player *player, uint32 zone)
Called when player (player) enter in zone.
uint32 GetTypeId() const
Class used to access individual fields of database query result.
Definition Field.h:92
uint8 GetUInt8() const
Definition Field.cpp:29
std::string ToString() const
static ObjectGuid GetGUID(Object const *o)
Definition Object.h:78