31 for (std::pair<uint32 const, MMapData*>& loadedMMap :
loadedMMaps)
32 delete loadedMMap.second;
41 for (
uint32 const& mapId : mapIds)
42 loadedMMaps.insert(MMapDataSet::value_type(mapId,
nullptr));
50 MMapDataSet::const_iterator itr =
loadedMMaps.find(mapId);
60 MMapDataSet::iterator itr =
loadedMMaps.find(mapId);
69 itr =
loadedMMaps.insert(MMapDataSet::value_type(mapId,
nullptr)).first;
71 ABORT_MSG(
"Invalid mapId %u passed to MMapManager after startup in thread unsafe environment", mapId);
76 FILE* file = fopen(fileName.c_str(),
"rb");
79 TC_LOG_DEBUG(
"maps",
"MMAP:loadMapData: Error: Could not open mmap file '{}'", fileName);
88 TC_LOG_DEBUG(
"maps",
"MMAP:loadMapData: Error: Could not read params from file '{}'", fileName);
92 dtNavMesh* mesh = dtAllocNavMesh();
94 if (dtStatusFailed(mesh->init(&
params)))
97 TC_LOG_ERROR(
"maps",
"MMAP:loadMapData: Failed to initialize dtNavMesh for mmap {:03} from file {}", mapId, fileName);
101 TC_LOG_DEBUG(
"maps",
"MMAP:loadMapData: Loaded {:03}.mmap", mapId);
106 itr->second = mmap_data;
112 return uint32(x << 16 | y);
132 FILE* file = fopen(fileName.c_str(),
"rb");
135 TC_LOG_DEBUG(
"maps",
"MMAP:loadMap: Could not open mmtile file '{}'", fileName);
143 TC_LOG_ERROR(
"maps",
"MMAP:loadMap: Bad header in mmap {:03}{:02}{:02}.mmtile", mapId, x, y);
150 TC_LOG_ERROR(
"maps",
"MMAP:loadMap: {:03}{:02}{:02}.mmtile was built with generator v{}, expected v{}",
156 long pos = ftell(file);
157 fseek(file, 0, SEEK_END);
158 if (pos < 0 ||
static_cast<int32>(fileHeader.
size) > ftell(file) - pos)
160 TC_LOG_ERROR(
"maps",
"MMAP:loadMap: {:03}{:02}{:02}.mmtile has corrupted data size", mapId, x, y);
165 fseek(file, pos, SEEK_SET);
167 unsigned char* data = (
unsigned char*)dtAlloc(fileHeader.
size, DT_ALLOC_PERM);
170 size_t result = fread(data, fileHeader.
size, 1, file);
173 TC_LOG_ERROR(
"maps",
"MMAP:loadMap: Bad header or data in mmap {:03}{:02}{:02}.mmtile", mapId, x, y);
180 dtMeshHeader* header = (dtMeshHeader*)data;
181 dtTileRef tileRef = 0;
184 if (dtStatusSucceed(mmap->
navMesh->addTile(data, fileHeader.
size, DT_TILE_FREE_DATA, 0, &tileRef)))
186 mmap->
loadedTileRefs.insert(std::pair<uint32, dtTileRef>(packedGridPos, tileRef));
188 TC_LOG_DEBUG(
"maps",
"MMAP:loadMap: Loaded mmtile {:03}[{:02}, {:02}] into {:03}[{:02}, {:02}]", mapId, x, y, mapId, header->x, header->y);
193 TC_LOG_ERROR(
"maps",
"MMAP:loadMap: Could not load {:03}{:02}{:02}.mmtile into navmesh", mapId, x, y);
205 auto [queryItr, inserted] = mmap->
navMeshQueries.try_emplace(instanceId,
nullptr);
210 dtNavMeshQuery* query = dtAllocNavMeshQuery();
212 if (dtStatusFailed(query->init(mmap->
navMesh, 1024)))
214 dtFreeNavMeshQuery(query);
216 TC_LOG_ERROR(
"maps",
"MMAP:GetNavMeshQuery: Failed to initialize dtNavMeshQuery for mapId {:03} instanceId {}", mapId, instanceId);
220 TC_LOG_DEBUG(
"maps",
"MMAP:GetNavMeshQuery: created dtNavMeshQuery for mapId {:03} instanceId {}", mapId, instanceId);
221 queryItr->second = query;
228 MMapDataSet::const_iterator itr =
GetMMapData(mapId);
232 TC_LOG_DEBUG(
"maps",
"MMAP:unloadMap: Asked to unload not loaded navmesh map. {:03}{:02}{:02}.mmtile", mapId, x, y);
243 TC_LOG_DEBUG(
"maps",
"MMAP:unloadMap: Asked to unload not loaded navmesh tile. {:03}{:02}{:02}.mmtile", mapId, x, y);
250 if (dtStatusFailed(mmap->
navMesh->removeTile(tileRef,
nullptr,
nullptr)))
255 TC_LOG_ERROR(
"maps",
"MMAP:unloadMap: Could not unload {:03}{:02}{:02}.mmtile from navmesh", mapId, x, y);
262 TC_LOG_DEBUG(
"maps",
"MMAP:unloadMap: Unloaded mmtile {:03}[{:02}, {:02}] from {:03}", mapId, x, y, mapId);
271 MMapDataSet::iterator itr =
loadedMMaps.find(mapId);
275 TC_LOG_DEBUG(
"maps",
"MMAP:unloadMap: Asked to unload not loaded navmesh map {:03}", mapId);
283 uint32 x = (i->first >> 16);
284 uint32 y = (i->first & 0x0000FFFF);
285 if (dtStatusFailed(mmap->
navMesh->removeTile(i->second,
nullptr,
nullptr)))
286 TC_LOG_ERROR(
"maps",
"MMAP:unloadMap: Could not unload {:03}{:02}{:02}.mmtile from navmesh", mapId, x, y);
290 TC_LOG_DEBUG(
"maps",
"MMAP:unloadMap: Unloaded mmtile {:03}[{:02}, {:02}] from {:03}", mapId, x, y, mapId);
295 itr->second =
nullptr;
296 TC_LOG_DEBUG(
"maps",
"MMAP:unloadMap: Unloaded {:03}.mmap", mapId);
304 MMapDataSet::const_iterator itr =
GetMMapData(mapId);
308 TC_LOG_DEBUG(
"maps",
"MMAP:unloadMapInstance: Asked to unload not loaded navmesh map {:03}", mapId);
316 TC_LOG_DEBUG(
"maps",
"MMAP:unloadMapInstance: Asked to unload not loaded dtNavMeshQuery mapId {:03} instanceId {}", mapId, instanceId);
320 dtFreeNavMeshQuery(queryItr->second);
322 TC_LOG_DEBUG(
"maps",
"MMAP:unloadMapInstance: Unloaded mapId {:03} instanceId {}", mapId, instanceId);
329 MMapDataSet::const_iterator itr =
GetMMapData(mapId);
333 return itr->second->navMesh;
342 auto queryItr = itr->second->navMeshQueries.find(instanceId);
343 if (queryItr == itr->second->navMeshQueries.end())
346 return queryItr->second;
std::set< uint32 > params[2]
#define TC_LOG_DEBUG(filterType__,...)
#define TC_LOG_ERROR(filterType__,...)
dtNavMeshQuery const * GetNavMeshQuery(uint32 mapId, uint32 instanceId)
bool unloadMap(uint32 mapId, int32 x, int32 y)
void InitializeThreadUnsafe(const std::vector< uint32 > &mapIds)
bool loadMapData(std::string const &basePath, uint32 mapId)
bool loadMapInstance(std::string const &basePath, uint32 mapId, uint32 instanceId)
bool thread_safe_environment
dtNavMesh const * GetNavMesh(uint32 mapId)
MMapDataSet::const_iterator GetMMapData(uint32 mapId) const
bool loadMap(std::string const &basePath, uint32 mapId, int32 x, int32 y)
uint32 packTileID(int32 x, int32 y)
bool unloadMapInstance(uint32 mapId, uint32 instanceId)
constexpr char TILE_FILE_NAME_FORMAT[]
constexpr char MAP_FILE_NAME_FORMAT[]
std::string StringFormat(FormatString< Args... > fmt, Args &&... args)
Default TC string format function.
NavMeshQuerySet navMeshQueries
MMapTileSet loadedTileRefs