TrinityCore
DisableMgr.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 "DisableMgr.h"
19#include "CriteriaHandler.h"
20#include "DatabaseEnv.h"
21#include "DB2Stores.h"
22#include "Log.h"
23#include "Map.h"
24#include "ObjectMgr.h"
25#include "OutdoorPvP.h"
26#include "Player.h"
27#include "SpellMgr.h"
28#include "StringConvert.h"
29#include "VMapManager2.h"
30#include "World.h"
31
32namespace DisableMgr
33{
34
35char const* MapTypeNames[] =
36{
37 "World",
38 "Dungeon",
39 "Raid",
40 "Battleground",
41 "Arena",
42 "Scenario"
43};
44
45namespace
46{
47 struct DisableData
48 {
50 std::unordered_set<uint32> params[2]; // params0, params1
51 };
52
53 // single disables here with optional data
54 typedef std::unordered_map<uint32, DisableData> DisableTypeMap;
55
56 std::array<DisableTypeMap, MAX_DISABLE_TYPES> m_DisableMap;
57}
58
60{
61 uint32 oldMSTime = getMSTime();
62
63 // reload case
64 for (std::size_t i = 0; i < m_DisableMap.size(); ++i)
65 m_DisableMap[i].clear();
66
67 QueryResult result = WorldDatabase.Query("SELECT sourceType, entry, flags, params_0, params_1 FROM disables");
68
69 uint32 total_count = 0;
70
71 if (!result)
72 {
73 TC_LOG_INFO("server.loading", ">> Loaded 0 disables. DB table `disables` is empty!");
74 return;
75 }
76
77 Field* fields;
78 do
79 {
80 fields = result->Fetch();
81 uint32 type = fields[0].GetUInt32();
82 if (type >= MAX_DISABLE_TYPES)
83 {
84 TC_LOG_ERROR("sql.sql", "Invalid type {} specified in `disables` table, skipped.", type);
85 continue;
86 }
87
88 uint32 entry = fields[1].GetUInt32();
89 uint16 flags = fields[2].GetUInt16();
90 std::string params_0 = fields[3].GetString();
91 std::string params_1 = fields[4].GetString();
92
93 DisableData data;
94 data.flags = flags;
95
96 switch (type)
97 {
99 if (!(sSpellMgr->GetSpellInfo(entry, DIFFICULTY_NONE) || flags & SPELL_DISABLE_DEPRECATED_SPELL))
100 {
101 TC_LOG_ERROR("sql.sql", "Spell entry {} from `disables` doesn't exist in dbc, skipped.", entry);
102 continue;
103 }
104
106 {
107 TC_LOG_ERROR("sql.sql", "Disable flags for spell {} are invalid, skipped.", entry);
108 continue;
109 }
110
112 {
113 for (std::string_view mapStr : Trinity::Tokenize(params_0, ',', true))
114 {
115 if (Optional<uint32> mapId = Trinity::StringTo<uint32>(mapStr))
116 data.params[0].insert(*mapId);
117 else
118 TC_LOG_ERROR("sql.sql", "Disable map '{}' for spell {} is invalid, skipped.", mapStr, entry);
119 }
120 }
121
123 {
124 for (std::string_view areaStr : Trinity::Tokenize(params_1, ',', true))
125 {
126 if (Optional<uint32> areaId = Trinity::StringTo<uint32>(areaStr))
127 data.params[1].insert(*areaId);
128 else
129 TC_LOG_ERROR("sql.sql", "Disable area '{}' for spell {} is invalid, skipped.", areaStr, entry);
130 }
131 }
132
133 break;
134 // checked later
136 break;
137 case DISABLE_TYPE_MAP:
139 {
140 MapEntry const* mapEntry = sMapStore.LookupEntry(entry);
141 if (!mapEntry)
142 {
143 TC_LOG_ERROR("sql.sql", "Map entry {} from `disables` doesn't exist in dbc, skipped.", entry);
144 continue;
145 }
146 bool isFlagInvalid = false;
147 switch (mapEntry->InstanceType)
148 {
149 case MAP_COMMON:
150 if (flags)
151 isFlagInvalid = true;
152 break;
153 case MAP_INSTANCE:
154 case MAP_RAID:
155 if (flags & DUNGEON_STATUSFLAG_HEROIC && !sDB2Manager.GetMapDifficultyData(entry, DIFFICULTY_HEROIC))
157 if (flags & RAID_STATUSFLAG_10MAN_HEROIC && !sDB2Manager.GetMapDifficultyData(entry, DIFFICULTY_10_HC))
159 if (flags & RAID_STATUSFLAG_25MAN_HEROIC && !sDB2Manager.GetMapDifficultyData(entry, DIFFICULTY_25_HC))
161 if (!flags)
162 isFlagInvalid = true;
163 break;
164 case MAP_BATTLEGROUND:
165 case MAP_ARENA:
166 TC_LOG_ERROR("sql.sql", "Battleground map {} specified to be disabled in map case, skipped.", entry);
167 continue;
168 }
169 if (isFlagInvalid)
170 {
171 TC_LOG_ERROR("sql.sql", "Disable flags for map {} are invalid, skipped.", entry);
172 continue;
173 }
174 break;
175 }
177 if (!sBattlemasterListStore.LookupEntry(entry))
178 {
179 TC_LOG_ERROR("sql.sql", "Battleground entry {} from `disables` doesn't exist in dbc, skipped.", entry);
180 continue;
181 }
182 if (flags)
183 TC_LOG_ERROR("sql.sql", "Disable flags specified for battleground {}, useless data.", entry);
184 break;
186 if (entry > MAX_OUTDOORPVP_TYPES)
187 {
188 TC_LOG_ERROR("sql.sql", "OutdoorPvPTypes value {} from `disables` is invalid, skipped.", entry);
189 continue;
190 }
191 if (flags)
192 TC_LOG_ERROR("sql.sql", "Disable flags specified for outdoor PvP {}, useless data.", entry);
193 break;
195 if (!sCriteriaStore.LookupEntry(entry))
196 {
197 TC_LOG_ERROR("sql.sql", "Criteria entry {} from `disables` doesn't exist in dbc, skipped.", entry);
198 continue;
199 }
200 if (flags)
201 TC_LOG_ERROR("sql.sql", "Disable flags specified for Criteria {}, useless data.", entry);
202 break;
204 {
205 MapEntry const* mapEntry = sMapStore.LookupEntry(entry);
206 if (!mapEntry)
207 {
208 TC_LOG_ERROR("sql.sql", "Map entry {} from `disables` doesn't exist in dbc, skipped.", entry);
209 continue;
210 }
211 switch (mapEntry->InstanceType)
212 {
213 case MAP_COMMON:
215 TC_LOG_INFO("misc", "Areaflag disabled for {} map {}.", MapTypeNames[mapEntry->InstanceType], entry);
217 TC_LOG_INFO("misc", "Liquid status disabled for {} map {}.", MapTypeNames[mapEntry->InstanceType], entry);
218 break;
219 case MAP_INSTANCE:
220 case MAP_RAID:
221 case MAP_BATTLEGROUND:
222 case MAP_ARENA:
223 case MAP_SCENARIO:
225 TC_LOG_INFO("misc", "Height disabled for {} map {}.", MapTypeNames[mapEntry->InstanceType], entry);
227 TC_LOG_INFO("misc", "LoS disabled for {} map {}.", MapTypeNames[mapEntry->InstanceType], entry);
228 break;
229 }
230 break;
231 }
233 {
234 MapEntry const* mapEntry = sMapStore.LookupEntry(entry);
235 if (!mapEntry)
236 {
237 TC_LOG_ERROR("sql.sql", "Map entry {} from `disables` doesn't exist in dbc, skipped.", entry);
238 continue;
239 }
240 if (mapEntry->InstanceType <= MAP_SCENARIO)
241 TC_LOG_INFO("misc", "Pathfinding disabled for {} map {}.", MapTypeNames[mapEntry->InstanceType], entry);
242 break;
243 }
245 {
246 if (!sPhaseStore.LookupEntry(entry))
247 {
248 TC_LOG_ERROR("sql.sql", "Phase entry {} from `disables` doesn't exist in dbc, skipped.", entry);
249 continue;
250 }
251 if (flags)
252 TC_LOG_ERROR("sql.sql", "Disable flags specified for phase {}, useless data.", entry);
253 break;
254 }
255 default:
256 break;
257 }
258
259 m_DisableMap[type][entry] = std::move(data);
260 ++total_count;
261 }
262 while (result->NextRow());
263
264 TC_LOG_INFO("server.loading", ">> Loaded {} disables in {} ms", total_count, GetMSTimeDiffToNow(oldMSTime));
265}
266
268{
269 uint32 oldMSTime = getMSTime();
270
271 std::size_t count = m_DisableMap[DISABLE_TYPE_QUEST].size();
272 if (!count)
273 {
274 TC_LOG_INFO("server.loading", ">> Checked 0 quest disables.");
275 return;
276 }
277
278 // check only quests, rest already done at startup
279 for (auto itr = m_DisableMap[DISABLE_TYPE_QUEST].begin(); itr != m_DisableMap[DISABLE_TYPE_QUEST].end();)
280 {
281 uint32 entry = itr->first;
282 if (!sObjectMgr->GetQuestTemplate(entry))
283 {
284 TC_LOG_ERROR("sql.sql", "Quest entry {} from `disables` doesn't exist, skipped.", entry);
285 m_DisableMap[DISABLE_TYPE_QUEST].erase(itr++);
286 continue;
287 }
288 if (itr->second.flags)
289 TC_LOG_ERROR("sql.sql", "Disable flags specified for quest {}, useless data.", entry);
290 ++itr;
291 }
292
293 TC_LOG_INFO("server.loading", ">> Checked {} quest disables in {} ms", count, GetMSTimeDiffToNow(oldMSTime));
294}
295
296bool IsDisabledFor(DisableType type, uint32 entry, WorldObject const* ref, uint8 flags /*= 0*/)
297{
299 if (m_DisableMap[type].empty())
300 return false;
301
302 DisableTypeMap::iterator itr = m_DisableMap[type].find(entry);
303 if (itr == m_DisableMap[type].end()) // not disabled
304 return false;
305
306 switch (type)
307 {
309 {
310 uint16 spellFlags = itr->second.flags;
311 if (ref)
312 {
313 if ((ref->GetTypeId() == TYPEID_PLAYER && (spellFlags & SPELL_DISABLE_PLAYER)) ||
314 (ref->GetTypeId() == TYPEID_UNIT && ((spellFlags & SPELL_DISABLE_CREATURE) || (ref->ToUnit()->IsPet() && (spellFlags & SPELL_DISABLE_PET)))) ||
315 (ref->GetTypeId() == TYPEID_GAMEOBJECT && (spellFlags & SPELL_DISABLE_GAMEOBJECT)))
316 {
318 {
319 if (Map const* map = ref->FindMap())
320 {
321 if (spellFlags & SPELL_DISABLE_ARENAS && map->IsBattleArena())
322 return true; // Current map is Arena and this spell is disabled here
323
324 if (spellFlags & SPELL_DISABLE_BATTLEGROUNDS && map->IsBattleground())
325 return true; // Current map is a Battleground and this spell is disabled here
326 }
327 }
328
329 if (spellFlags & SPELL_DISABLE_MAP)
330 {
331 std::unordered_set<uint32> const& mapIds = itr->second.params[0];
332 if (mapIds.find(ref->GetMapId()) != mapIds.end())
333 return true; // Spell is disabled on current map
334
335 if (!(spellFlags & SPELL_DISABLE_AREA))
336 return false; // Spell is disabled on another map, but not this one, return false
337
338 // Spell is disabled in an area, but not explicitly our current mapId. Continue processing.
339 }
340
341 if (spellFlags & SPELL_DISABLE_AREA)
342 {
343 std::unordered_set<uint32> const& areaIds = itr->second.params[1];
344 if (areaIds.find(ref->GetAreaId()) != areaIds.end())
345 return true; // Spell is disabled in this area
346 return false; // Spell is disabled in another area, but not this one, return false
347 }
348 else
349 return true; // Spell disabled for all maps
350 }
351
352 return false;
353 }
354 else if (spellFlags & SPELL_DISABLE_DEPRECATED_SPELL) // call not from spellcast
355 return true;
356 else if (flags & SPELL_DISABLE_LOS)
357 return (spellFlags & SPELL_DISABLE_LOS) != 0;
358
359 break;
360 }
361 case DISABLE_TYPE_MAP:
363 if (!ref)
364 return true;
365 if (Player const* player = ref->ToPlayer())
366 {
367 MapEntry const* mapEntry = sMapStore.LookupEntry(entry);
368 if (mapEntry->IsDungeon())
369 {
370 uint8 disabledModes = itr->second.flags;
371 Difficulty targetDifficulty = player->GetDifficultyID(mapEntry);
372 sDB2Manager.GetDownscaledMapDifficultyData(entry, targetDifficulty);
373 switch (targetDifficulty)
374 {
376 return (disabledModes & DUNGEON_STATUSFLAG_NORMAL) != 0;
378 return (disabledModes & DUNGEON_STATUSFLAG_HEROIC) != 0;
379 case DIFFICULTY_10_HC:
380 return (disabledModes & RAID_STATUSFLAG_10MAN_HEROIC) != 0;
381 case DIFFICULTY_25_HC:
382 return (disabledModes & RAID_STATUSFLAG_25MAN_HEROIC) != 0;
383 default:
384 return false;
385 }
386 }
387 else if (mapEntry->InstanceType == MAP_COMMON)
388 return true;
389 }
390 return false;
392 return true;
398 return true;
400 return (flags & itr->second.flags) != 0;
401 default:
402 break;
403 }
404
405 return false;
406}
407
409{
410 return IsDisabledFor(DISABLE_TYPE_VMAP, entry, nullptr, flags);
411}
412
414{
415 return sWorld->getBoolConfig(CONFIG_ENABLE_MMAPS)
417}
418
419} // Namespace
DB2Storage< PhaseEntry > sPhaseStore("Phase.db2", &PhaseLoadInfo::Instance)
DB2Storage< BattlemasterListEntry > sBattlemasterListStore("BattlemasterList.db2", &BattlemasterListLoadInfo::Instance)
DB2Storage< MapEntry > sMapStore("Map.db2", &MapLoadInfo::Instance)
DB2Storage< CriteriaEntry > sCriteriaStore("Criteria.db2", &CriteriaLoadInfo::Instance)
#define sDB2Manager
Definition: DB2Stores.h:554
@ MAP_COMMON
Definition: DBCEnums.h:1337
@ MAP_SCENARIO
Definition: DBCEnums.h:1342
@ MAP_BATTLEGROUND
Definition: DBCEnums.h:1340
@ MAP_ARENA
Definition: DBCEnums.h:1341
@ MAP_INSTANCE
Definition: DBCEnums.h:1338
@ MAP_RAID
Definition: DBCEnums.h:1339
Difficulty
Definition: DBCEnums.h:918
@ DIFFICULTY_NONE
Definition: DBCEnums.h:919
@ DIFFICULTY_25_HC
Definition: DBCEnums.h:925
@ DIFFICULTY_NORMAL
Definition: DBCEnums.h:920
@ DIFFICULTY_HEROIC
Definition: DBCEnums.h:921
@ DIFFICULTY_10_HC
Definition: DBCEnums.h:924
std::shared_ptr< ResultSet > QueryResult
DatabaseWorkerPool< WorldDatabaseConnection > WorldDatabase
Accessor to the world database.
Definition: DatabaseEnv.cpp:20
uint8_t uint8
Definition: Define.h:144
uint16_t uint16
Definition: Define.h:143
uint32_t uint32
Definition: Define.h:142
std::unordered_set< uint32 > params[2]
Definition: DisableMgr.cpp:50
uint16 flags
Definition: DisableMgr.cpp:49
@ MMAP_DISABLE_PATHFINDING
Definition: DisableMgr.h:61
@ SPELL_DISABLE_LOS
Definition: DisableMgr.h:49
@ SPELL_DISABLE_CREATURE
Definition: DisableMgr.h:44
@ SPELL_DISABLE_PET
Definition: DisableMgr.h:45
@ SPELL_DISABLE_ARENAS
Definition: DisableMgr.h:51
@ SPELL_DISABLE_MAP
Definition: DisableMgr.h:47
@ SPELL_DISABLE_PLAYER
Definition: DisableMgr.h:43
@ SPELL_DISABLE_DEPRECATED_SPELL
Definition: DisableMgr.h:46
@ MAX_SPELL_DISABLE_TYPE
Definition: DisableMgr.h:53
@ SPELL_DISABLE_BATTLEGROUNDS
Definition: DisableMgr.h:52
@ SPELL_DISABLE_GAMEOBJECT
Definition: DisableMgr.h:50
@ SPELL_DISABLE_AREA
Definition: DisableMgr.h:48
DisableType
Definition: DisableMgr.h:26
@ DISABLE_TYPE_SPELL
Definition: DisableMgr.h:27
@ DISABLE_TYPE_CRITERIA
Definition: DisableMgr.h:31
@ DISABLE_TYPE_MMAP
Definition: DisableMgr.h:34
@ DISABLE_TYPE_PHASE_AREA
Definition: DisableMgr.h:36
@ MAX_DISABLE_TYPES
Definition: DisableMgr.h:38
@ DISABLE_TYPE_VMAP
Definition: DisableMgr.h:33
@ DISABLE_TYPE_QUEST
Definition: DisableMgr.h:28
@ DISABLE_TYPE_MAP
Definition: DisableMgr.h:29
@ DISABLE_TYPE_BATTLEGROUND
Definition: DisableMgr.h:30
@ DISABLE_TYPE_LFG_MAP
Definition: DisableMgr.h:35
@ DISABLE_TYPE_OUTDOORPVP
Definition: DisableMgr.h:32
#define ASSERT
Definition: Errors.h:68
#define TC_LOG_ERROR(filterType__, message__,...)
Definition: Log.h:188
#define TC_LOG_INFO(filterType__, message__,...)
Definition: Log.h:182
@ TYPEID_GAMEOBJECT
Definition: ObjectGuid.h:44
@ TYPEID_UNIT
Definition: ObjectGuid.h:41
@ TYPEID_PLAYER
Definition: ObjectGuid.h:42
#define sObjectMgr
Definition: ObjectMgr.h:1979
std::optional< T > Optional
Optional helper class to wrap optional values within.
Definition: Optional.h:25
@ MAX_OUTDOORPVP_TYPES
Definition: OutdoorPvP.h:36
@ DUNGEON_STATUSFLAG_NORMAL
@ RAID_STATUSFLAG_10MAN_HEROIC
@ RAID_STATUSFLAG_25MAN_HEROIC
@ DUNGEON_STATUSFLAG_HEROIC
#define sSpellMgr
Definition: SpellMgr.h:856
uint32 GetMSTimeDiffToNow(uint32 oldMSTime)
Definition: Timer.h:57
uint32 getMSTime()
Definition: Timer.h:33
Class used to access individual fields of database query result.
Definition: Field.h:92
std::string GetString() const
Definition: Field.cpp:125
uint16 GetUInt16() const
Definition: Field.cpp:45
uint32 GetUInt32() const
Definition: Field.cpp:61
Definition: Map.h:190
static Unit * ToUnit(Object *o)
Definition: Object.h:226
TypeID GetTypeId() const
Definition: Object.h:174
static Player * ToPlayer(Object *o)
Definition: Object.h:214
bool IsPet() const
Definition: Unit.h:744
constexpr uint32 GetMapId() const
Definition: Position.h:204
Map * FindMap() const
Definition: Object.h:626
uint32 GetAreaId() const
Definition: Object.h:547
#define sWorld
Definition: World.h:929
@ CONFIG_ENABLE_MMAPS
Definition: World.h:167
void CheckQuestDisables()
Definition: DisableMgr.cpp:267
bool IsDisabledFor(DisableType type, uint32 entry, WorldObject const *ref, uint8 flags)
Definition: DisableMgr.cpp:296
char const * MapTypeNames[]
Definition: DisableMgr.cpp:35
bool IsVMAPDisabledFor(uint32 entry, uint8 flags)
Definition: DisableMgr.cpp:408
bool IsPathfindingEnabled(uint32 mapId)
Definition: DisableMgr.cpp:413
void LoadDisables()
Definition: DisableMgr.cpp:59
TC_COMMON_API std::vector< std::string_view > Tokenize(std::string_view str, char sep, bool keepEmpty)
Definition: Util.cpp:54
@ VMAP_DISABLE_LIQUIDSTATUS
Definition: VMapManager2.h:59
@ VMAP_DISABLE_LOS
Definition: VMapManager2.h:58
@ VMAP_DISABLE_HEIGHT
Definition: VMapManager2.h:57
@ VMAP_DISABLE_AREAFLAG
Definition: VMapManager2.h:56
int8 InstanceType
bool IsDungeon() const