31 for (std::pair<uint32 const, MMapData*>& loadedMMap :
loadedMMaps)
32 delete loadedMMap.second;
41 for (std::pair<
uint32 const, std::vector<uint32>>
const& mapId : mapData)
43 loadedMMaps.insert(MMapDataSet::value_type(mapId.first,
nullptr));
44 for (
uint32 childMapId : mapId.second)
54 MMapDataSet::const_iterator itr =
loadedMMaps.find(mapId);
64 MMapDataSet::iterator itr =
loadedMMaps.find(mapId);
73 itr =
loadedMMaps.insert(MMapDataSet::value_type(mapId,
nullptr)).first;
75 ABORT_MSG(
"Invalid mapId %u passed to MMapManager after startup in thread unsafe environment", mapId);
80 FILE* file = fopen(fileName.c_str(),
"rb");
83 TC_LOG_DEBUG(
"maps",
"MMAP:loadMapData: Error: Could not open mmap file '{}'", fileName);
92 TC_LOG_DEBUG(
"maps",
"MMAP:loadMapData: Error: Could not read params from file '{}'", fileName);
96 dtNavMesh* mesh = dtAllocNavMesh();
98 if (dtStatusFailed(mesh->init(&
params)))
101 TC_LOG_ERROR(
"maps",
"MMAP:loadMapData: Failed to initialize dtNavMesh for mmap {:04} from file {}", mapId, fileName);
105 TC_LOG_DEBUG(
"maps",
"MMAP:loadMapData: Loaded {:04}.mmap", mapId);
110 itr->second = mmap_data;
116 return uint32(x << 16 | y);
136 FILE* file = fopen(fileName.c_str(),
"rb");
143 file = fopen(fileName.c_str(),
"rb");
149 TC_LOG_DEBUG(
"maps",
"MMAP:loadMap: Could not open mmtile file '{}'", fileName);
157 TC_LOG_ERROR(
"maps",
"MMAP:loadMap: Bad header in mmap {:04}{:02}{:02}.mmtile", mapId, x, y);
164 TC_LOG_ERROR(
"maps",
"MMAP:loadMap: {:04}{:02}{:02}.mmtile was built with generator v{}, expected v{}",
170 long pos = ftell(file);
171 fseek(file, 0, SEEK_END);
172 if (pos < 0 ||
static_cast<int32>(fileHeader.
size) > ftell(file) - pos)
174 TC_LOG_ERROR(
"maps",
"MMAP:loadMap: {:04}{:02}{:02}.mmtile has corrupted data size", mapId, x, y);
179 fseek(file, pos, SEEK_SET);
181 unsigned char* data = (
unsigned char*)dtAlloc(fileHeader.
size, DT_ALLOC_PERM);
184 size_t result = fread(data, fileHeader.
size, 1, file);
187 TC_LOG_ERROR(
"maps",
"MMAP:loadMap: Bad header or data in mmap {:04}{:02}{:02}.mmtile", mapId, x, y);
194 dtMeshHeader* header = (dtMeshHeader*)data;
195 dtTileRef tileRef = 0;
198 if (dtStatusSucceed(mmap->
navMesh->addTile(data, fileHeader.
size, DT_TILE_FREE_DATA, 0, &tileRef)))
200 mmap->
loadedTileRefs.insert(std::pair<uint32, dtTileRef>(packedGridPos, tileRef));
202 TC_LOG_DEBUG(
"maps",
"MMAP:loadMap: Loaded mmtile {:04}[{:02}, {:02}] into {:04}[{:02}, {:02}]", mapId, x, y, mapId, header->x, header->y);
207 TC_LOG_ERROR(
"maps",
"MMAP:loadMap: Could not load {:04}{:02}{:02}.mmtile into navmesh", mapId, x, y);
219 auto [queryItr, inserted] = mmap->
navMeshQueries.try_emplace({ instanceMapId, instanceId },
nullptr);
224 dtNavMeshQuery* query = dtAllocNavMeshQuery();
226 if (dtStatusFailed(query->init(mmap->
navMesh, 1024)))
228 dtFreeNavMeshQuery(query);
230 TC_LOG_ERROR(
"maps",
"MMAP:GetNavMeshQuery: Failed to initialize dtNavMeshQuery for mapId {:04} instanceId {}", instanceMapId, instanceId);
234 TC_LOG_DEBUG(
"maps",
"MMAP:GetNavMeshQuery: created dtNavMeshQuery for mapId {:04} instanceId {}", instanceMapId, instanceId);
235 queryItr->second = query;
242 MMapDataSet::const_iterator itr =
GetMMapData(mapId);
246 TC_LOG_DEBUG(
"maps",
"MMAP:unloadMap: Asked to unload not loaded navmesh map. {:04}{:02}{:02}.mmtile", mapId, x, y);
258 TC_LOG_DEBUG(
"maps",
"MMAP:unloadMap: Asked to unload not loaded navmesh tile. {:04}{:02}{:02}.mmtile", mapId, x, y);
263 if (dtStatusFailed(mmap->
navMesh->removeTile(tileRefItr->second,
nullptr,
nullptr)))
268 TC_LOG_ERROR(
"maps",
"MMAP:unloadMap: Could not unload {:04}{:02}{:02}.mmtile from navmesh", mapId, x, y);
275 TC_LOG_DEBUG(
"maps",
"MMAP:unloadMap: Unloaded mmtile {:04}[{:02}, {:02}] from {:03}", mapId, x, y, mapId);
284 MMapDataSet::iterator itr =
loadedMMaps.find(mapId);
288 TC_LOG_DEBUG(
"maps",
"MMAP:unloadMap: Asked to unload not loaded navmesh map {:04}", mapId);
296 uint32 x = (i->first >> 16);
297 uint32 y = (i->first & 0x0000FFFF);
298 if (dtStatusFailed(mmap->
navMesh->removeTile(i->second,
nullptr,
nullptr)))
299 TC_LOG_ERROR(
"maps",
"MMAP:unloadMap: Could not unload {:04}{:02}{:02}.mmtile from navmesh", mapId, x, y);
303 TC_LOG_DEBUG(
"maps",
"MMAP:unloadMap: Unloaded mmtile {:04}[{:02}, {:02}] from {:04}", mapId, x, y, mapId);
308 itr->second =
nullptr;
309 TC_LOG_DEBUG(
"maps",
"MMAP:unloadMap: Unloaded {:04}.mmap", mapId);
317 MMapDataSet::const_iterator itr =
GetMMapData(meshMapId);
321 TC_LOG_DEBUG(
"maps",
"MMAP:unloadMapInstance: Asked to unload not loaded navmesh map {:04}", meshMapId);
326 auto queryItr = mmap->
navMeshQueries.find({ instanceMapId, instanceId });
329 TC_LOG_DEBUG(
"maps",
"MMAP:unloadMapInstance: Asked to unload not loaded dtNavMeshQuery mapId {:04} instanceId {}", instanceMapId, instanceId);
333 dtFreeNavMeshQuery(queryItr->second);
335 TC_LOG_DEBUG(
"maps",
"MMAP:unloadMapInstance: Unloaded mapId {:04} instanceId {}", instanceMapId, instanceId);
342 MMapDataSet::const_iterator itr =
GetMMapData(mapId);
346 return itr->second->navMesh;
355 auto queryItr = itr->second->navMeshQueries.find({ instanceMapId, instanceId });
356 if (queryItr == itr->second->navMeshQueries.end())
359 return queryItr->second;
std::unordered_set< uint32 > params[2]
#define TC_LOG_DEBUG(filterType__,...)
#define TC_LOG_ERROR(filterType__,...)
bool unloadMap(uint32 mapId, int32 x, int32 y)
bool loadMapData(std::string const &basePath, uint32 mapId)
bool loadMapInstance(std::string const &basePath, uint32 meshMapId, uint32 instanceMapId, uint32 instanceId)
std::unordered_map< uint32, uint32 > parentMapData
bool thread_safe_environment
dtNavMesh const * GetNavMesh(uint32 mapId)
MMapDataSet::const_iterator GetMMapData(uint32 mapId) const
void InitializeThreadUnsafe(std::unordered_map< uint32, std::vector< uint32 > > const &mapData)
dtNavMeshQuery const * GetNavMeshQuery(uint32 meshMapId, uint32 instanceMapId, uint32 instanceId)
bool loadMap(std::string const &basePath, uint32 mapId, int32 x, int32 y)
uint32 packTileID(int32 x, int32 y)
bool unloadMapInstance(uint32 meshMapId, uint32 instanceMapId, 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