TrinityCore
Loading...
Searching...
No Matches
MapManager.h
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#ifndef TRINITY_MAPMANAGER_H
19#define TRINITY_MAPMANAGER_H
20
21#include "GridDefines.h"
22#include "IteratorPair.h"
23#include "MapUpdater.h"
24#include "Optional.h"
25#include "Position.h"
26#include "SharedDefines.h"
27#include "UniqueTrackablePtr.h"
28#include <boost/dynamic_bitset_fwd.hpp>
29#include <map>
30#include <shared_mutex>
31
32class Battleground;
33class BattlegroundMap;
34class GarrisonMap;
35class Group;
36class InstanceLock;
37class InstanceMap;
38class Map;
39class Player;
40enum Difficulty : int16;
41
43{
44 MapManager();
46
47 public:
48 MapManager(MapManager const&) = delete;
50 MapManager& operator=(MapManager const&) = delete;
52
53 static MapManager* instance();
54
55 Map* CreateMap(uint32 mapId, Player* player, Optional<uint32> lfgDungeonsId = {});
56 Map* FindMap(uint32 mapId, uint32 instanceId) const;
57 uint32 FindInstanceIdForPlayer(uint32 mapId, Player const* player) const;
58
59 void Initialize();
60 void Update(uint32 diff);
61
63 {
64 if (t < MIN_GRID_DELAY)
65 i_gridCleanUpDelay = MIN_GRID_DELAY;
66 else
67 i_gridCleanUpDelay = t;
68 }
69
71 {
74
75 i_timer.SetInterval(t);
76 i_timer.Reset();
77 }
78
79 void UnloadAll();
80
81 static bool IsValidMAP(uint32 mapId);
82
83 static bool IsValidMapCoord(uint32 mapid, float x, float y)
84 {
85 return IsValidMAP(mapid) && Trinity::IsValidMapCoord(x, y);
86 }
87
88 static bool IsValidMapCoord(uint32 mapid, float x, float y, float z)
89 {
90 return IsValidMAP(mapid) && Trinity::IsValidMapCoord(x, y, z);
91 }
92
93 static bool IsValidMapCoord(uint32 mapid, float x, float y, float z, float o)
94 {
95 return IsValidMAP(mapid) && Trinity::IsValidMapCoord(x, y, z, o);
96 }
97
98 static bool IsValidMapCoord(uint32 mapid, Position const& pos)
99 {
100 return IsValidMapCoord(mapid, pos.GetPositionX(), pos.GetPositionY(), pos.GetPositionZ(), pos.GetOrientation());
101 }
102
103 static bool IsValidMapCoord(WorldLocation const& loc)
104 {
105 return IsValidMapCoord(loc.GetMapId(), loc);
106 }
107
108 void InitializeVisibilityDistanceInfo();
109
110 /* statistics */
111 uint32 GetNumInstances() const;
112 uint32 GetNumPlayersInInstances() const;
113
114 // Instance ID management
115 void InitInstanceIds();
116 uint32 GenerateInstanceId();
117 void RegisterInstanceId(uint32 instanceId);
118 void FreeInstanceId(uint32 instanceId);
119
120 MapUpdater * GetMapUpdater() { return &m_updater; }
121
122 template<typename Worker>
123 void DoForAllMaps(Worker&& worker);
124
125 template<typename Worker>
126 void DoForAllMapsWithMapId(uint32 mapId, Worker&& worker);
127
128 void IncreaseScheduledScriptsCount() { ++_scheduledScripts; }
129 void DecreaseScheduledScriptCount() { --_scheduledScripts; }
130 void DecreaseScheduledScriptCount(std::size_t count) { _scheduledScripts -= count; }
131 bool IsScriptScheduled() const { return _scheduledScripts > 0; }
132
133 void AddSC_BuiltInScripts();
134
135 private:
136 using MapKey = std::pair<uint32, uint32>;
137 typedef std::map<MapKey, Trinity::unique_trackable_ptr<Map>> MapMapType;
138 typedef boost::dynamic_bitset<size_t> InstanceIds;
139
140 Map* FindMap_i(uint32 mapId, uint32 instanceId) const;
141
142 Map* CreateWorldMap(uint32 mapId, uint32 instanceId);
143 InstanceMap* CreateInstance(uint32 mapId, uint32 instanceId, InstanceLock* instanceLock, Difficulty difficulty, TeamId team, Group* group,
144 Optional<uint32> lfgDungeonsId);
145 BattlegroundMap* CreateBattleground(uint32 mapId, uint32 instanceId, Battleground* bg);
146 GarrisonMap* CreateGarrison(uint32 mapId, uint32 instanceId, Player* owner);
147
148 bool DestroyMap(Map* map);
149
150 mutable std::shared_mutex _mapsLock;
154
155 std::unique_ptr<InstanceIds> _freeInstanceIds;
158
159 // atomic op counter for active scripts amount
160 std::atomic<std::size_t> _scheduledScripts;
161};
162
163template<typename Worker>
164void MapManager::DoForAllMaps(Worker&& worker)
165{
166 std::shared_lock<std::shared_mutex> lock(_mapsLock);
167
168 for (auto const& [key, map] : i_maps)
169 worker(map.get());
170}
171
172template<typename Worker>
173void MapManager::DoForAllMapsWithMapId(uint32 mapId, Worker&& worker)
174{
175 std::shared_lock<std::shared_mutex> lock(_mapsLock);
176
178 i_maps.lower_bound({ mapId, 0 }),
179 i_maps.upper_bound({ mapId, std::numeric_limits<uint32>::max() })
180 );
181
182 for (auto const& [key, map] : range)
183 worker(map.get());
184}
185
186#define sMapMgr MapManager::instance()
187
188#endif
Difficulty
Definition DBCEnums.h:932
#define TC_GAME_API
Definition Define.h:129
int16_t int16
Definition Define.h:151
uint32_t uint32
Definition Define.h:154
#define MIN_GRID_DELAY
Definition GridDefines.h:45
#define MIN_MAP_UPDATE_DELAY
Definition GridDefines.h:46
std::optional< T > Optional
Optional helper class to wrap optional values within.
Definition Optional.h:25
Definition Group.h:205
std::unique_ptr< InstanceIds > _freeInstanceIds
Definition MapManager.h:155
std::shared_mutex _mapsLock
Definition MapManager.h:150
static bool IsValidMapCoord(uint32 mapid, float x, float y, float z, float o)
Definition MapManager.h:93
void DecreaseScheduledScriptCount(std::size_t count)
Definition MapManager.h:130
void DoForAllMapsWithMapId(uint32 mapId, Worker &&worker)
Definition MapManager.h:173
static bool IsValidMapCoord(uint32 mapid, Position const &pos)
Definition MapManager.h:98
void SetMapUpdateInterval(uint32 t)
Definition MapManager.h:70
MapUpdater m_updater
Definition MapManager.h:157
void DecreaseScheduledScriptCount()
Definition MapManager.h:129
boost::dynamic_bitset< size_t > InstanceIds
Definition MapManager.h:138
static bool IsValidMapCoord(uint32 mapid, float x, float y)
Definition MapManager.h:83
MapManager & operator=(MapManager const &)=delete
uint32 i_gridCleanUpDelay
Definition MapManager.h:151
void IncreaseScheduledScriptsCount()
Definition MapManager.h:128
MapManager(MapManager &&)=delete
MapManager(MapManager const &)=delete
std::atomic< std::size_t > _scheduledScripts
Definition MapManager.h:160
void DoForAllMaps(Worker &&worker)
Definition MapManager.h:164
void SetGridCleanUpDelay(uint32 t)
Definition MapManager.h:62
static bool IsValidMapCoord(WorldLocation const &loc)
Definition MapManager.h:103
MapUpdater * GetMapUpdater()
Definition MapManager.h:120
std::pair< uint32, uint32 > MapKey
Definition MapManager.h:136
uint32 _nextInstanceId
Definition MapManager.h:156
MapMapType i_maps
Definition MapManager.h:152
std::map< MapKey, Trinity::unique_trackable_ptr< Map > > MapMapType
Definition MapManager.h:137
bool IsScriptScheduled() const
Definition MapManager.h:131
IntervalTimer i_timer
Definition MapManager.h:153
MapManager & operator=(MapManager &&)=delete
static bool IsValidMapCoord(uint32 mapid, float x, float y, float z)
Definition MapManager.h:88
Definition Map.h:225
constexpr uint32 GetMapId() const
Definition Position.h:216
constexpr IteratorPair< iterator, end_iterator > MakeIteratorPair(iterator first, end_iterator second)
bool IsValidMapCoord(float c)
constexpr float GetPositionX() const
Definition Position.h:87
constexpr float GetPositionY() const
Definition Position.h:88
constexpr float GetOrientation() const
Definition Position.h:90
constexpr float GetPositionZ() const
Definition Position.h:89