TrinityCore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
MMapManager.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2008-2016 TrinityCore <http://www.trinitycore.org/>
3  * Copyright (C) 2005-2010 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 _MMAP_MANAGER_H
20 #define _MMAP_MANAGER_H
21 
22 #include "Define.h"
23 #include "DetourAlloc.h"
24 #include "DetourNavMesh.h"
25 #include "DetourNavMeshQuery.h"
26 #include "MapDefines.h"
27 #include <string>
28 #include <unordered_map>
29 #include <set>
30 #include <vector>
31 
32 // move map related classes
33 namespace MMAP
34 {
35  typedef std::unordered_map<uint32, dtTileRef> MMapTileSet;
36  typedef std::unordered_map<uint32, dtNavMeshQuery*> NavMeshQuerySet;
37 
38 
39  typedef std::set<uint32> TerrainSet;
40 
42  {
43  // Pre-built navMesh
45 
46  // List of terrain swap map ids used to build the navMesh
47  TerrainSet terrainIds;
48 
49  MMapTileSet loadedTileRefs;
50  };
51 
53  {
54  unsigned char* data;
57  };
58 
59  typedef std::unordered_map<uint32, PhasedTile*> PhaseTileContainer;
60  typedef std::unordered_map<uint32, PhaseTileContainer> PhaseTileMap;
61 
62 
63  typedef std::unordered_map<uint32, TerrainSet> TerrainSetMap;
64 
66  {
67  public:
68  MMapData(dtNavMesh* mesh, uint32 mapId);
69  ~MMapData();
70 
71  dtNavMesh* GetNavMesh(TerrainSet swaps);
72 
73  // we have to use single dtNavMeshQuery for every instance, since those are not thread safe
74  NavMeshQuerySet navMeshQueries; // instanceId to query
75 
77  MMapTileSet loadedTileRefs;
78  TerrainSetMap loadedPhasedTiles;
79 
80  private:
82  PhaseTileContainer _baseTiles;
83  std::set<uint32> _activeSwaps;
84  void RemoveSwap(PhasedTile* ptile, uint32 swap, uint32 packedXY);
85  void AddSwap(PhasedTile* tile, uint32 swap, uint32 packedXY);
86  };
87 
88 
89  typedef std::unordered_map<uint32, MMapData*> MMapDataSet;
90 
91  // singleton class
92  // holds all all access to mmap loading unloading and meshes
94  {
95  public:
96  MMapManager() : loadedTiles(0), thread_safe_environment(true) {}
97  ~MMapManager();
98 
99  void InitializeThreadUnsafe(std::unordered_map<uint32, std::vector<uint32>> const& mapData);
100  bool loadMap(const std::string& basePath, uint32 mapId, int32 x, int32 y);
101  bool unloadMap(uint32 mapId, int32 x, int32 y);
102  bool unloadMap(uint32 mapId);
103  bool unloadMapInstance(uint32 mapId, uint32 instanceId);
104 
105  // the returned [dtNavMeshQuery const*] is NOT threadsafe
106  dtNavMeshQuery const* GetNavMeshQuery(uint32 mapId, uint32 instanceId, TerrainSet swaps);
107  dtNavMesh const* GetNavMesh(uint32 mapId, TerrainSet swaps);
108 
109  uint32 getLoadedTilesCount() const { return loadedTiles; }
110  uint32 getLoadedMapsCount() const { return uint32(loadedMMaps.size()); }
111 
112  typedef std::unordered_map<uint32, std::vector<uint32>> PhaseChildMapContainer;
113  void LoadPhaseTiles(PhaseChildMapContainer::const_iterator phasedMapData, int32 x, int32 y);
114  void UnloadPhaseTile(PhaseChildMapContainer::const_iterator phasedMapData, int32 x, int32 y);
115  PhaseTileContainer const* GetPhaseTileContainer(uint32 mapId) const
116  {
117  auto itr = _phaseTiles.find(mapId);
118  if (itr != _phaseTiles.end())
119  return &itr->second;
120  return nullptr;
121  }
122 
123  private:
124  bool loadMapData(uint32 mapId);
125  uint32 packTileID(int32 x, int32 y);
126 
127  MMapDataSet::const_iterator GetMMapData(uint32 mapId) const;
128  MMapDataSet loadedMMaps;
129  PhaseChildMapContainer phaseMapData;
132 
133  PhasedTile* LoadTile(uint32 mapId, int32 x, int32 y);
134  PhaseTileMap _phaseTiles;
135  };
136 }
137 
138 #endif
std::set< uint32 > _activeSwaps
Definition: MMapManager.h:83
std::unordered_map< uint32, MMapData * > MMapDataSet
Definition: MMapManager.h:89
PhaseTileMap _phaseTiles
Definition: MMapManager.h:134
uint32 _mapId
Definition: MMapManager.h:81
int32 dataSize
Definition: MMapManager.h:56
std::unordered_map< uint32, dtNavMeshQuery * > NavMeshQuerySet
Definition: MMapManager.h:36
dtNavMesh * navMesh
Definition: MMapManager.h:44
Definition: MMapFactory.cpp:22
uint32 getLoadedMapsCount() const
Definition: MMapManager.h:110
PhaseChildMapContainer phaseMapData
Definition: MMapManager.h:129
MMapManager()
Definition: MMapManager.h:96
MMapTileSet loadedTileRefs
Definition: MMapManager.h:49
dtNavMesh * navMesh
Definition: MMapManager.h:76
Definition: DetourNavMesh.h:323
#define true
Definition: CascPort.h:17
MMapTileSet loadedTileRefs
Definition: MMapManager.h:77
Definition: MMapManager.h:41
bool thread_safe_environment
Definition: MMapManager.h:131
std::unordered_map< uint32, PhaseTileContainer > PhaseTileMap
Definition: MMapManager.h:60
uint32 loadedTiles
Definition: MMapManager.h:130
std::unordered_map< uint32, PhasedTile * > PhaseTileContainer
Definition: MMapManager.h:59
std::unordered_map< uint32, dtTileRef > MMapTileSet
Definition: MMapManager.h:35
MmapTileHeader fileHeader
Definition: MMapManager.h:55
int32_t int32
Definition: Define.h:146
uint32_t uint32
Definition: Define.h:150
G3D::int16 y
Definition: Vector2int16.h:38
std::unordered_map< uint32, TerrainSet > TerrainSetMap
Definition: MMapManager.h:63
MMapDataSet loadedMMaps
Definition: MMapManager.h:128
#define TC_COMMON_API
Definition: Define.h:116
TerrainSetMap loadedPhasedTiles
Definition: MMapManager.h:78
uint32 getLoadedTilesCount() const
Definition: MMapManager.h:109
Definition: MMapManager.h:65
std::unordered_map< uint32, std::vector< uint32 > > PhaseChildMapContainer
Definition: MMapManager.h:112
Definition: MapDefines.h:10
PhaseTileContainer _baseTiles
Definition: MMapManager.h:82
Definition: MMapManager.h:93
unsigned char * data
Definition: MMapManager.h:54
std::set< uint32 > TerrainSet
Definition: MMapManager.h:39
TerrainSet terrainIds
Definition: MMapManager.h:47
NavMeshQuerySet navMeshQueries
Definition: MMapManager.h:74
uint32_t uint32
Definition: g3dmath.h:168
G3D::int16 x
Definition: Vector2int16.h:37
Definition: DetourNavMeshQuery.h:153
Definition: MMapManager.h:52
PhaseTileContainer const * GetPhaseTileContainer(uint32 mapId) const
Definition: MMapManager.h:115