TrinityCore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
MapManager.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2008-2016 TrinityCore <http://www.trinitycore.org/>
3  * Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License as published by the
7  * Free Software Foundation; either version 2 of the License, or (at your
8  * option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13  * more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #ifndef TRINITY_MAPMANAGER_H
20 #define TRINITY_MAPMANAGER_H
21 
22 #include "Object.h"
23 #include "Map.h"
24 #include "MapInstanced.h"
25 #include "GridStates.h"
26 #include "MapUpdater.h"
27 
28 class Transport;
29 struct TransportCreatureProto;
30 
32 {
33  public:
34  static MapManager* instance();
35 
36  Map* CreateBaseMap(uint32 mapId);
37  Map* FindBaseNonInstanceMap(uint32 mapId) const;
38  Map* CreateMap(uint32 mapId, Player* player, uint32 loginInstanceId=0);
39  Map* FindMap(uint32 mapId, uint32 instanceId) const;
40 
41  uint32 GetAreaId(uint32 mapid, float x, float y, float z) const
42  {
43  Map const* m = const_cast<MapManager*>(this)->CreateBaseMap(mapid);
44  return m->GetAreaId(x, y, z);
45  }
46  uint32 GetZoneId(uint32 mapid, float x, float y, float z) const
47  {
48  Map const* m = const_cast<MapManager*>(this)->CreateBaseMap(mapid);
49  return m->GetZoneId(x, y, z);
50  }
51  void GetZoneAndAreaId(uint32& zoneid, uint32& areaid, uint32 mapid, float x, float y, float z)
52  {
53  Map const* m = const_cast<MapManager*>(this)->CreateBaseMap(mapid);
54  m->GetZoneAndAreaId(zoneid, areaid, x, y, z);
55  }
56 
57  void Initialize(void);
58  void Update(uint32);
59 
61  {
62  if (t < MIN_GRID_DELAY)
63  i_gridCleanUpDelay = MIN_GRID_DELAY;
64  else
65  i_gridCleanUpDelay = t;
66  }
67 
69  {
70  if (t < MIN_MAP_UPDATE_DELAY)
72 
73  i_timer.SetInterval(t);
74  i_timer.Reset();
75  }
76 
77  //void LoadGrid(int mapid, int instId, float x, float y, const WorldObject* obj, bool no_unload = false);
78  void UnloadAll();
79 
80  static bool ExistMapAndVMap(uint32 mapid, float x, float y);
81  static bool IsValidMAP(uint32 mapid, bool startUp);
82 
83  static bool IsValidMapCoord(uint32 mapid, float x, float y)
84  {
85  return IsValidMAP(mapid, false) && Trinity::IsValidMapCoord(x, y);
86  }
87 
88  static bool IsValidMapCoord(uint32 mapid, float x, float y, float z)
89  {
90  return IsValidMAP(mapid, false) && 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, false) && Trinity::IsValidMapCoord(x, y, z, o);
96  }
97 
98  static bool IsValidMapCoord(WorldLocation const& loc)
99  {
100  return IsValidMapCoord(loc.GetMapId(), loc.GetPositionX(), loc.GetPositionY(), loc.GetPositionZ(), loc.GetOrientation());
101  }
102 
103  void DoDelayedMovesAndRemoves();
104 
105  Map::EnterState PlayerCannotEnter(uint32 mapid, Player* player, bool loginCheck = false);
106  void InitializeVisibilityDistanceInfo();
107 
108  /* statistics */
109  uint32 GetNumInstances();
110  uint32 GetNumPlayersInInstances();
111 
112  // Instance ID management
113  void InitInstanceIds();
114  uint32 GenerateInstanceId();
115  void RegisterInstanceId(uint32 instanceId);
116  void FreeInstanceId(uint32 instanceId);
117 
118  uint32 GetNextInstanceId() const { return _nextInstanceId; };
119  void SetNextInstanceId(uint32 nextInstanceId) { _nextInstanceId = nextInstanceId; };
120 
121  MapUpdater * GetMapUpdater() { return &m_updater; }
122 
123  template<typename Worker>
124  void DoForAllMaps(Worker&& worker);
125 
126  template<typename Worker>
127  void DoForAllMapsWithMapId(uint32 mapId, Worker&& worker);
128 
129 private:
130  typedef std::unordered_map<uint32, Map*> MapMapType;
131  typedef std::vector<bool> InstanceIds;
132 
133  MapManager();
134  ~MapManager();
135 
136  Map* FindBaseMap(uint32 mapId) const
137  {
138  MapMapType::const_iterator iter = i_maps.find(mapId);
139  return (iter == i_maps.end() ? NULL : iter->second);
140  }
141 
142  MapManager(const MapManager &);
143  MapManager& operator=(const MapManager &);
144 
145  std::mutex _mapsLock;
147  MapMapType i_maps;
149 
150  InstanceIds _instanceIds;
153 };
154 
155 template<typename Worker>
156 void MapManager::DoForAllMaps(Worker&& worker)
157 {
158  std::lock_guard<std::mutex> lock(_mapsLock);
159 
160  for (auto& mapPair : i_maps)
161  {
162  Map* map = mapPair.second;
163  if (MapInstanced* mapInstanced = map->ToMapInstanced())
164  {
165  MapInstanced::InstancedMaps& instances = mapInstanced->GetInstancedMaps();
166  for (auto& instancePair : instances)
167  worker(instancePair.second);
168  }
169  else
170  worker(map);
171  }
172 }
173 
174 template<typename Worker>
175 inline void MapManager::DoForAllMapsWithMapId(uint32 mapId, Worker&& worker)
176 {
177  std::lock_guard<std::mutex> lock(_mapsLock);
178 
179  auto itr = i_maps.find(mapId);
180  if (itr != i_maps.end())
181  {
182  Map* map = itr->second;
183  if (MapInstanced* mapInstanced = map->ToMapInstanced())
184  {
185  MapInstanced::InstancedMaps& instances = mapInstanced->GetInstancedMaps();
186  for (auto& p : instances)
187  worker(p.second);
188  }
189  else
190  worker(map);
191  }
192 }
193 
194 #define sMapMgr MapManager::instance()
195 #endif
uint32 GetAreaId(float x, float y, float z, bool *isOutdoors) const
Definition: Map.cpp:2426
Definition: MapInstanced.h:28
std::vector< bool > InstanceIds
Definition: MapManager.h:131
Map * FindBaseMap(uint32 mapId) const
Definition: MapManager.h:136
#define MIN_GRID_DELAY
Definition: GridDefines.h:44
Definition: Position.h:228
void GetZoneAndAreaId(uint32 &zoneid, uint32 &areaid, float x, float y, float z) const
Definition: Map.cpp:2481
static bool IsValidMapCoord(uint32 mapid, float x, float y, float z, float o)
Definition: MapManager.h:93
uint32 _nextInstanceId
Definition: MapManager.h:151
uint32 GetAreaId(uint32 mapid, float x, float y, float z) const
Definition: MapManager.h:41
arena_t NULL
Definition: jemalloc_internal.h:624
Definition: Transport.h:28
std::unordered_map< uint32, Map * > MapMapType
Definition: MapManager.h:130
uint32 i_gridCleanUpDelay
Definition: MapManager.h:146
std::unordered_map< uint32, Map * > InstancedMaps
Definition: MapInstanced.h:32
MapMapType i_maps
Definition: MapManager.h:147
void DoForAllMapsWithMapId(uint32 mapId, Worker &&worker)
Definition: MapManager.h:175
static bool IsValidMapCoord(WorldLocation const &loc)
Definition: MapManager.h:98
MapUpdater m_updater
Definition: MapManager.h:152
float GetOrientation() const
Definition: Position.h:107
uint32 GetNextInstanceId() const
Definition: MapManager.h:118
bool IsValidMapCoord(float c)
Definition: GridDefines.h:218
float GetPositionY() const
Definition: Position.h:105
G3D::int16 z
Definition: Vector3int16.h:46
void SetNextInstanceId(uint32 nextInstanceId)
Definition: MapManager.h:119
InstanceIds _instanceIds
Definition: MapManager.h:150
Vector2int16 & operator=(const Any &a)
float GetPositionZ() const
Definition: Position.h:106
uint32_t uint32
Definition: Define.h:150
static void Initialize(PCASC_SALSA20 pState, LPBYTE pbKey, DWORD cbKeyLength, LPBYTE pbVector)
Definition: CascDecrypt.cpp:81
G3D::int16 y
Definition: Vector2int16.h:38
#define MIN_MAP_UPDATE_DELAY
Definition: GridDefines.h:45
uint32 GetMapId() const
Definition: Position.h:254
Definition: Map.h:259
Definition: MapUpdater.h:31
uint32 GetZoneId(float x, float y, float z) const
Definition: Map.cpp:2471
void DoForAllMaps(Worker &&worker)
Definition: MapManager.h:156
Definition: MapManager.h:31
static bool IsValidMapCoord(uint32 mapid, float x, float y)
Definition: MapManager.h:83
static bool IsValidMapCoord(uint32 mapid, float x, float y, float z)
Definition: MapManager.h:88
#define TC_GAME_API
Definition: Define.h:134
void SetMapUpdateInterval(uint32 t)
Definition: MapManager.h:68
void GetZoneAndAreaId(uint32 &zoneid, uint32 &areaid, uint32 mapid, float x, float y, float z)
Definition: MapManager.h:51
EnterState
Definition: Map.h:371
void SetGridCleanUpDelay(uint32 t)
Definition: MapManager.h:60
MapUpdater * GetMapUpdater()
Definition: MapManager.h:121
MapInstanced * ToMapInstanced()
Definition: Map.h:492
G3D::int16 x
Definition: Vector2int16.h:37
float GetPositionX() const
Definition: Position.h:104
uint32 GetZoneId(uint32 mapid, float x, float y, float z) const
Definition: MapManager.h:46
void Update(uint32 diff)
Definition: WeatherMgr.cpp:150
IntervalTimer i_timer
Definition: MapManager.h:148
std::mutex _mapsLock
Definition: MapManager.h:145
Definition: Timer.h:47