OregonCore  revision fb2a440-git
Your Favourite TBC server
MMAP::MMapManager Class Reference

#include <MoveMap.h>

Public Member Functions

 MMapManager ()
 
 ~MMapManager ()
 
void InitializeThreadUnsafe (const std::vector< uint32 > &mapIds)
 
bool loadMap (uint32 mapId, int32 x, int32 y)
 
bool unloadMap (uint32 mapId, int32 x, int32 y)
 
bool unloadMap (uint32 mapId)
 
bool unloadMapInstance (uint32 mapId, uint32 instanceId)
 
dtNavMeshQuery const * GetNavMeshQuery (uint32 mapId, uint32 instanceId)
 
dtNavMesh const * GetNavMesh (uint32 mapId)
 
uint32 getLoadedTilesCount () const
 
uint32 getLoadedMapsCount () const
 

Private Member Functions

bool loadMapData (uint32 mapId)
 
uint32 packTileID (int32 x, int32 y)
 
MMapDataSet::const_iterator GetMMapData (uint32 mapId) const
 

Private Attributes

MMapDataSet loadedMMaps
 
uint32 loadedTiles
 
bool thread_safe_environment
 

Detailed Description

Definition at line 70 of file MoveMap.h.

Constructor & Destructor Documentation

MMAP::MMapManager::MMapManager ( )
inline

Definition at line 73 of file MoveMap.h.

bool thread_safe_environment
Definition: MoveMap.h:101
uint32 loadedTiles
Definition: MoveMap.h:100
MMAP::MMapManager::~MMapManager ( )

Definition at line 83 of file MoveMap.cpp.

84 {
85  for (MMapDataSet::iterator i = loadedMMaps.begin(); i != loadedMMaps.end(); ++i)
86  delete i->second;
87 
88  // by now we should not have maps loaded
89  // if we had, tiles in MMapData->mmapLoadedTiles, their actual data is lost!
90 }
MMapDataSet loadedMMaps
Definition: MoveMap.h:99

Member Function Documentation

uint32 MMAP::MMapManager::getLoadedMapsCount ( ) const
inline

Definition at line 90 of file MoveMap.h.

91  {
92  return loadedMMaps.size();
93  }
MMapDataSet loadedMMaps
Definition: MoveMap.h:99
uint32 MMAP::MMapManager::getLoadedTilesCount ( ) const
inline

Definition at line 86 of file MoveMap.h.

87  {
88  return loadedTiles;
89  }
uint32 loadedTiles
Definition: MoveMap.h:100
MMapDataSet::const_iterator MMAP::MMapManager::GetMMapData ( uint32  mapId) const
private

Definition at line 101 of file MoveMap.cpp.

102  {
103  // return the iterator if found or end() if not found/NULL
104  MMapDataSet::const_iterator itr = loadedMMaps.find(mapId);
105  if (itr != loadedMMaps.cend() && !itr->second)
106  itr = loadedMMaps.cend();
107 
108  return itr;
109  }
MMapDataSet loadedMMaps
Definition: MoveMap.h:99
dtNavMesh const * MMAP::MMapManager::GetNavMesh ( uint32  mapId)

Definition at line 358 of file MoveMap.cpp.

Referenced by PathInfo::PathInfo().

359 {
360  MMapDataSet::const_iterator itr = GetMMapData(mapId);
361  if (itr == loadedMMaps.end())
362  return NULL;
363 
364  return itr->second->navMesh;
365 }
MMapDataSet::const_iterator GetMMapData(uint32 mapId) const
Definition: MoveMap.cpp:101
MMapDataSet loadedMMaps
Definition: MoveMap.h:99
dtNavMeshQuery const * MMAP::MMapManager::GetNavMeshQuery ( uint32  mapId,
uint32  instanceId 
)

Definition at line 367 of file MoveMap.cpp.

References ASSERT, MMAP::MMapData::navMesh, MMAP::MMapData::navMeshQueries, and sLog.

Referenced by PathInfo::PathInfo().

368 {
369  MMapDataSet::const_iterator itr = GetMMapData(mapId);
370  if (itr == loadedMMaps.end())
371  return NULL;
372 
373  MMapData* mmap = itr->second;
374  if (mmap->navMeshQueries.find(instanceId) == mmap->navMeshQueries.end())
375  {
376  // allocate mesh query
377  dtNavMeshQuery* query = dtAllocNavMeshQuery();
378  ASSERT(query);
379  if (dtStatusFailed(query->init(mmap->navMesh, 1024)))
380  {
381  dtFreeNavMeshQuery(query);
382  sLog.outError("MMAP:GetNavMeshQuery: Failed to initialize dtNavMeshQuery for mapId %03u instanceId %u", mapId, instanceId);
383  return NULL;
384  }
385 
386  sLog.outDetail("MMAP:GetNavMeshQuery: created dtNavMeshQuery for mapId %03u instanceId %u", mapId, instanceId);
387  mmap->navMeshQueries.insert(std::pair<uint32, dtNavMeshQuery*>(instanceId, query));
388  }
389 
390  return mmap->navMeshQueries[instanceId];
391 }
#define sLog
Log class singleton.
Definition: Log.h:187
MMapDataSet::const_iterator GetMMapData(uint32 mapId) const
Definition: MoveMap.cpp:101
MMapDataSet loadedMMaps
Definition: MoveMap.h:99
#define ASSERT
Definition: Errors.h:29
void MMAP::MMapManager::InitializeThreadUnsafe ( const std::vector< uint32 > &  mapIds)

Definition at line 92 of file MoveMap.cpp.

Referenced by World::SetInitialWorldSettings().

93  {
94  // the caller must pass the list of all mapIds that will be used in the VMapManager2 lifetime
95  for (const uint32& mapId : mapIds)
96  loadedMMaps.insert(MMapDataSet::value_type(mapId, nullptr));
97 
99  }
bool thread_safe_environment
Definition: MoveMap.h:101
MMapDataSet loadedMMaps
Definition: MoveMap.h:99
ACE_UINT32 uint32
Definition: Define.h:71
bool MMAP::MMapManager::loadMap ( uint32  mapId,
int32  x,
int32  y 
)

Definition at line 172 of file MoveMap.cpp.

References ASSERT, MMAP_MAGIC, MMAP_VERSION, MMAP::MMapData::mmapLoadedTiles, MmapTileHeader::mmapMagic, MmapTileHeader::mmapVersion, MMAP::MMapData::navMesh, MmapTileHeader::size, sLog, snprintf, and sWorld.

Referenced by Map::LoadMMap().

173 {
174  // make sure the mmap is loaded and ready to load tiles
175  if (!loadMapData(mapId))
176  return false;
177 
178  // get this mmap data
179  MMapData* mmap = loadedMMaps[mapId];
180  ASSERT(mmap->navMesh);
181 
182  // check if we already have this tile loaded
183  uint32 packedGridPos = packTileID(x, y);
184  if (mmap->mmapLoadedTiles.find(packedGridPos) != mmap->mmapLoadedTiles.end())
185  {
186  sLog.outError("MMAP:loadMap: Asked to load already loaded navmesh tile. %03u%02i%02i.mmtile", mapId, x, y);
187  return false;
188  }
189 
190  // load this tile :: mmaps/MMMXXYY.mmtile
191  uint32 pathLen = sWorld.GetDataPath().length() + strlen("mmaps/%03i%02i%02i.mmtile") + 1;
192  char* fileName = new char[pathLen];
193  snprintf(fileName, pathLen, (sWorld.GetDataPath() + "mmaps/%03i%02i%02i.mmtile").c_str(), mapId, x, y);
194 
195  FILE* file = fopen(fileName, "rb");
196  if (!file)
197  {
198  sLog.outMMap("MMAP:loadMap: Could not open mmtile file '%s'", fileName);
199  delete [] fileName;
200  return false;
201  }
202  delete [] fileName;
203 
204  // read header
205  MmapTileHeader fileHeader;
206  fread(&fileHeader, sizeof(MmapTileHeader), 1, file);
207 
208  if (fileHeader.mmapMagic != MMAP_MAGIC)
209  {
210  sLog.outError("MMAP:loadMap: Bad header in mmap %03u%02i%02i.mmtile", mapId, x, y);
211  fclose(file);
212  return false;
213  }
214 
215  if (fileHeader.mmapVersion != MMAP_VERSION)
216  {
217  sLog.outError("MMAP:loadMap: %03u%02i%02i.mmtile was built with generator v%i, expected v%i",
218  mapId, x, y, fileHeader.mmapVersion, MMAP_VERSION);
219  fclose(file);
220  return false;
221  }
222 
223  unsigned char* data = (unsigned char*)dtAlloc(fileHeader.size, DT_ALLOC_PERM);
224  ASSERT(data);
225 
226  size_t result = fread(data, fileHeader.size, 1, file);
227  if (!result)
228  {
229  sLog.outError("MMAP:loadMap: Bad header or data in mmap %03u%02i%02i.mmtile", mapId, x, y);
230  fclose(file);
231  return false;
232  }
233 
234  fclose(file);
235 
236  dtMeshHeader* header = (dtMeshHeader*)data;
237  dtTileRef tileRef = 0;
238 
239  // memory allocated for data is now managed by detour, and will be deallocated when the tile is removed
240  if (dtStatusSucceed(mmap->navMesh->addTile(data, fileHeader.size, DT_TILE_FREE_DATA, 0, &tileRef)))
241  {
242  mmap->mmapLoadedTiles.insert(std::pair<uint32, dtTileRef>(packedGridPos, tileRef));
243  ++loadedTiles;
244  sLog.outDetail("MMAP:loadMap: Loaded mmtile %03i[%02i,%02i] into %03i[%02i,%02i]", mapId, x, y, mapId, header->x, header->y);
245  return true;
246  }
247  else
248  {
249  sLog.outError("MMAP:loadMap: Could not load %03u%02i%02i.mmtile into navmesh", mapId, x, y);
250  dtFree(data);
251  return false;
252  }
253 }
#define MMAP_MAGIC
#define snprintf
Definition: Common.h:129
bool loadMapData(uint32 mapId)
Definition: MoveMap.cpp:111
uint32 packTileID(int32 x, int32 y)
Definition: MoveMap.cpp:167
#define sLog
Log class singleton.
Definition: Log.h:187
uint32 loadedTiles
Definition: MoveMap.h:100
MMapDataSet loadedMMaps
Definition: MoveMap.h:99
#define ASSERT
Definition: Errors.h:29
#define sWorld
Definition: World.h:860
#define MMAP_VERSION
ACE_UINT32 uint32
Definition: Define.h:71
bool MMAP::MMapManager::loadMapData ( uint32  mapId)
private

Definition at line 111 of file MoveMap.cpp.

References ASSERT, MMAP::MMapData::mmapLoadedTiles, sLog, snprintf, and sWorld.

112 {
113  // we already have this map loaded?
114  MMapDataSet::iterator itr = loadedMMaps.find(mapId);
115  if (itr != loadedMMaps.end())
116  {
117  if (itr->second)
118  return true;
119  }
120  else
121  {
123  itr = loadedMMaps.insert(MMapDataSet::value_type(mapId, nullptr)).first;
124  else
125  ASSERT(false && "Invalid mapId passed to MMapManager after startup in thread unsafe environment");
126  }
127 
128  // load and init dtNavMesh - read parameters from file
129  uint32 pathLen = sWorld.GetDataPath().length() + strlen("mmaps/%03i.mmap") + 1;
130  char* fileName = new char[pathLen];
131  snprintf(fileName, pathLen, (sWorld.GetDataPath() + "mmaps/%03i.mmap").c_str(), mapId);
132 
133  FILE* file = fopen(fileName, "rb");
134  if (!file)
135  {
136  sLog.outMMap("MMAP:loadMapData: Error: Could not open mmap file '%s'", fileName);
137  delete [] fileName;
138  return false;
139  }
140 
141  dtNavMeshParams params;
142  fread(&params, sizeof(dtNavMeshParams), 1, file);
143  fclose(file);
144 
145  dtNavMesh* mesh = dtAllocNavMesh();
146  ASSERT(mesh);
147  if (dtStatusFailed(mesh->init(&params)))
148  {
149  dtFreeNavMesh(mesh);
150  sLog.outError("MMAP:loadMapData: Failed to initialize dtNavMesh for mmap %03u from file %s", mapId, fileName);
151  delete [] fileName;
152  return false;
153  }
154 
155  delete [] fileName;
156 
157  sLog.outDetail("MMAP:loadMapData: Loaded %03i.mmap", mapId);
158 
159  // store inside our map list
160  MMapData* mmap_data = new MMapData(mesh);
161  mmap_data->mmapLoadedTiles.clear();
162 
163  itr->second = mmap_data;
164  return true;
165 }
#define snprintf
Definition: Common.h:129
#define sLog
Log class singleton.
Definition: Log.h:187
bool thread_safe_environment
Definition: MoveMap.h:101
MMapDataSet loadedMMaps
Definition: MoveMap.h:99
#define ASSERT
Definition: Errors.h:29
#define sWorld
Definition: World.h:860
ACE_UINT32 uint32
Definition: Define.h:71
uint32 MMAP::MMapManager::packTileID ( int32  x,
int32  y 
)
private

Definition at line 167 of file MoveMap.cpp.

168 {
169  return uint32(x << 16 | y);
170 }
ACE_UINT32 uint32
Definition: Define.h:71
bool MMAP::MMapManager::unloadMap ( uint32  mapId,
int32  x,
int32  y 
)

Definition at line 255 of file MoveMap.cpp.

References ASSERT, MMAP::MMapData::mmapLoadedTiles, MMAP::MMapData::navMesh, and sLog.

Referenced by Map::UnloadGrid().

256 {
257  // check if we have this map loaded
258  MMapDataSet::const_iterator itr = GetMMapData(mapId);
259  if (itr == loadedMMaps.end())
260  {
261  // file may not exist, therefore not loaded
262  sLog.outMMap("MMAP:unloadMap: Asked to unload not loaded navmesh map. %03u%02i%02i.mmtile", mapId, x, y);
263  return false;
264  }
265 
266  MMapData* mmap = itr->second;
267 
268  // check if we have this tile loaded
269  uint32 packedGridPos = packTileID(x, y);
270  if (mmap->mmapLoadedTiles.find(packedGridPos) == mmap->mmapLoadedTiles.end())
271  {
272  // file may not exist, therefore not loaded
273  sLog.outMMap("MMAP:unloadMap: Asked to unload not loaded navmesh tile. %03u%02i%02i.mmtile", mapId, x, y);
274  return false;
275  }
276 
277  dtTileRef tileRef = mmap->mmapLoadedTiles[packedGridPos];
278 
279  // unload, and mark as non loaded
280  if (dtStatusFailed(mmap->navMesh->removeTile(tileRef, NULL, NULL)))
281  {
282  // this is technically a memory leak
283  // if the grid is later reloaded, dtNavMesh::addTile will return error but no extra memory is used
284  // we cannot recover from this error - assert out
285  sLog.outError("MMAP:unloadMap: Could not unload %03u%02i%02i.mmtile from navmesh", mapId, x, y);
286  ASSERT(false);
287  }
288  else
289  {
290  mmap->mmapLoadedTiles.erase(packedGridPos);
291  --loadedTiles;
292  sLog.outDetail("MMAP:unloadMap: Unloaded mmtile %03i[%02i,%02i] from %03i", mapId, x, y, mapId);
293  return true;
294  }
295 
296  return false;
297 }
uint32 packTileID(int32 x, int32 y)
Definition: MoveMap.cpp:167
#define sLog
Log class singleton.
Definition: Log.h:187
MMapDataSet::const_iterator GetMMapData(uint32 mapId) const
Definition: MoveMap.cpp:101
uint32 loadedTiles
Definition: MoveMap.h:100
MMapDataSet loadedMMaps
Definition: MoveMap.h:99
#define ASSERT
Definition: Errors.h:29
ACE_UINT32 uint32
Definition: Define.h:71
bool MMAP::MMapManager::unloadMap ( uint32  mapId)

Definition at line 299 of file MoveMap.cpp.

References MMAP::MMapData::mmapLoadedTiles, MMAP::MMapData::navMesh, and sLog.

300 {
301  MMapDataSet::iterator itr = loadedMMaps.find(mapId);
302  if (itr == loadedMMaps.end() || !itr->second)
303  {
304  // file may not exist, therefore not loaded
305  sLog.outMMap("MMAP:unloadMap: Asked to unload not loaded navmesh map %03u", mapId);
306  return false;
307  }
308 
309  // unload all tiles from given map
310  MMapData* mmap = itr->second;
311  for (MMapTileSet::iterator i = mmap->mmapLoadedTiles.begin(); i != mmap->mmapLoadedTiles.end(); ++i)
312  {
313  uint32 x = (i->first >> 16);
314  uint32 y = (i->first & 0x0000FFFF);
315  if (dtStatusFailed(mmap->navMesh->removeTile(i->second, NULL, NULL)))
316  sLog.outError("MMAP:unloadMap: Could not unload %03u%02i%02i.mmtile from navmesh", mapId, x, y);
317  else
318  {
319  --loadedTiles;
320  sLog.outDetail("MMAP:unloadMap: Unloaded mmtile %03i[%02i,%02i] from %03i", mapId, x, y, mapId);
321  }
322  }
323 
324  delete mmap;
325  itr->second = nullptr;
326  sLog.outDetail("MMAP:unloadMap: Unloaded %03i.mmap", mapId);
327 
328  return true;
329 }
#define sLog
Log class singleton.
Definition: Log.h:187
uint32 loadedTiles
Definition: MoveMap.h:100
MMapDataSet loadedMMaps
Definition: MoveMap.h:99
ACE_UINT32 uint32
Definition: Define.h:71
bool MMAP::MMapManager::unloadMapInstance ( uint32  mapId,
uint32  instanceId 
)

Definition at line 331 of file MoveMap.cpp.

References MMAP::MMapData::navMeshQueries, and sLog.

332 {
333  // check if we have this map loaded
334  MMapDataSet::const_iterator itr = GetMMapData(mapId);
335  if (itr == loadedMMaps.end())
336  {
337  // file may not exist, therefore not loaded
338  sLog.outMMap("MMAP:unloadMapInstance: Asked to unload not loaded navmesh map %03u", mapId);
339  return false;
340  }
341 
342  MMapData* mmap = itr->second;
343  if (mmap->navMeshQueries.find(instanceId) == mmap->navMeshQueries.end())
344  {
345  sLog.outMMap("MMAP:unloadMapInstance: Asked to unload not loaded dtNavMeshQuery mapId %03u instanceId %u", mapId, instanceId);
346  return false;
347  }
348 
349  dtNavMeshQuery* query = mmap->navMeshQueries[instanceId];
350 
351  dtFreeNavMeshQuery(query);
352  mmap->navMeshQueries.erase(instanceId);
353  sLog.outDetail("MMAP:unloadMapInstance: Unloaded mapId %03u instanceId %u", mapId, instanceId);
354 
355  return true;
356 }
#define sLog
Log class singleton.
Definition: Log.h:187
MMapDataSet::const_iterator GetMMapData(uint32 mapId) const
Definition: MoveMap.cpp:101
MMapDataSet loadedMMaps
Definition: MoveMap.h:99

Member Data Documentation

MMapDataSet MMAP::MMapManager::loadedMMaps
private

Definition at line 99 of file MoveMap.h.

uint32 MMAP::MMapManager::loadedTiles
private

Definition at line 100 of file MoveMap.h.

bool MMAP::MMapManager::thread_safe_environment
private

Definition at line 101 of file MoveMap.h.


The documentation for this class was generated from the following files: