diff --git a/src/common/Collision/Management/MMapManager.cpp b/src/common/Collision/Management/MMapManager.cpp index df4793a9014..c1f05becdd5 100644 --- a/src/common/Collision/Management/MMapManager.cpp +++ b/src/common/Collision/Management/MMapManager.cpp @@ -221,7 +221,7 @@ namespace MMAP } else { - TC_LOG_ERROR("maps", "MMAP:loadMap: Could not load %04u%02i%02i.mmtile into navmesh", mapId, x, y); + TC_LOG_ERROR("maps", "MMAP:loadMap: Could not load %03u%02i%02i.mmtile into navmesh", mapId, x, y); dtFree(data); return false; } @@ -257,11 +257,11 @@ namespace MMAP if (dtStatusFailed(query->init(mmap->navMesh, 1024))) { dtFreeNavMeshQuery(query); - TC_LOG_ERROR("maps", "MMAP:GetNavMeshQuery: Failed to initialize dtNavMeshQuery for mapId %04u instanceId %u", mapId, instanceId); + TC_LOG_ERROR("maps", "MMAP:GetNavMeshQuery: Failed to initialize dtNavMeshQuery for mapId %03u instanceId %u", mapId, instanceId); return false; } - TC_LOG_DEBUG("maps", "MMAP:GetNavMeshQuery: created dtNavMeshQuery for mapId %04u instanceId %u", mapId, instanceId); + TC_LOG_DEBUG("maps", "MMAP:GetNavMeshQuery: created dtNavMeshQuery for mapId %03u instanceId %u", mapId, instanceId); mmap->navMeshQueries.insert(std::pair(instanceId, query)); return true; } diff --git a/src/common/Collision/Management/VMapManager2.cpp b/src/common/Collision/Management/VMapManager2.cpp index 591be582593..1239a27d66c 100644 --- a/src/common/Collision/Management/VMapManager2.cpp +++ b/src/common/Collision/Management/VMapManager2.cpp @@ -40,16 +40,23 @@ namespace VMAP thread_safe_environment = true; } - VMapManager2::~VMapManager2(void) + VMapManager2::~VMapManager2() { - for (InstanceTreeMap::iterator i = iInstanceMapTrees.begin(); i != iInstanceMapTrees.end(); ++i) - { + for (auto i = iInstanceMapTrees.begin(); i != iInstanceMapTrees.end(); ++i) delete i->second; - } - for (ModelFileMap::iterator i = iLoadedModelFiles.begin(); i != iLoadedModelFiles.end(); ++i) - { + + for (auto i = iLoadedModelFiles.begin(); i != iLoadedModelFiles.end(); ++i) delete i->second.getModel(); - } + } + + InstanceTreeMap::const_iterator VMapManager2::GetMapTree(uint32 mapId) const + { + // return the iterator if found or end() if not found/NULL + auto itr = iInstanceMapTrees.find(mapId); + if (itr != iInstanceMapTrees.cend() && !itr->second) + itr = iInstanceMapTrees.cend(); + + return itr; } void VMapManager2::InitializeThreadUnsafe(std::unordered_map> const& mapData) @@ -63,7 +70,6 @@ namespace VMAP iParentMapData[childMapId] = mapId.first; } - thread_safe_environment = false; } @@ -93,13 +99,13 @@ namespace VMAP int result = VMAP_LOAD_RESULT_IGNORED; if (isMapLoadingEnabled()) { - if (_loadMap(mapId, basePath, x, y)) + if (loadSingleMap(mapId, basePath, x, y)) { result = VMAP_LOAD_RESULT_OK; auto childMaps = iChildMapData.find(mapId); if (childMaps != iChildMapData.end()) for (uint32 childMapId : childMaps->second) - if (!_loadMap(childMapId, basePath, x, y)) + if (!loadSingleMap(childMapId, basePath, x, y)) result = VMAP_LOAD_RESULT_ERROR; } else @@ -109,27 +115,17 @@ namespace VMAP return result; } - InstanceTreeMap::const_iterator VMapManager2::GetMapTree(uint32 mapId) const - { - // return the iterator if found or end() if not found/NULL - InstanceTreeMap::const_iterator itr = iInstanceMapTrees.find(mapId); - if (itr != iInstanceMapTrees.cend() && !itr->second) - itr = iInstanceMapTrees.cend(); - - return itr; - } - // load one tile (internal use only) - bool VMapManager2::_loadMap(uint32 mapId, const std::string& basePath, uint32 tileX, uint32 tileY) + bool VMapManager2::loadSingleMap(uint32 mapId, const std::string& basePath, uint32 tileX, uint32 tileY) { - InstanceTreeMap::iterator instanceTree = iInstanceMapTrees.find(mapId); + auto instanceTree = iInstanceMapTrees.find(mapId); if (instanceTree == iInstanceMapTrees.end()) { if (thread_safe_environment) instanceTree = iInstanceMapTrees.insert(InstanceTreeMap::value_type(mapId, nullptr)).first; else ASSERT(false, "Invalid mapId %u tile [%u, %u] passed to VMapManager2 after startup in thread unsafe environment", - mapId, tileX, tileY); + mapId, tileX, tileY); } if (!instanceTree->second) @@ -147,9 +143,19 @@ namespace VMAP return instanceTree->second->LoadMapTile(tileX, tileY, this); } - void VMapManager2::_unloadMap(uint32 mapId, int x, int y) + void VMapManager2::unloadMap(unsigned int mapId, int x, int y) { - InstanceTreeMap::iterator instanceTree = iInstanceMapTrees.find(mapId); + auto childMaps = iChildMapData.find(mapId); + if (childMaps != iChildMapData.end()) + for (uint32 childMapId : childMaps->second) + unloadSingleMap(childMapId, x, y); + + unloadSingleMap(mapId, x, y); + } + + void VMapManager2::unloadSingleMap(uint32 mapId, int x, int y) + { + auto instanceTree = iInstanceMapTrees.find(mapId); if (instanceTree != iInstanceMapTrees.end() && instanceTree->second) { instanceTree->second->UnloadMapTile(x, y, this); @@ -161,9 +167,19 @@ namespace VMAP } } - void VMapManager2::_unloadMap(uint32 mapId) + void VMapManager2::unloadMap(unsigned int mapId) { - InstanceTreeMap::iterator instanceTree = iInstanceMapTrees.find(mapId); + auto childMaps = iChildMapData.find(mapId); + if (childMaps != iChildMapData.end()) + for (uint32 childMapId : childMaps->second) + unloadSingleMap(childMapId); + + unloadSingleMap(mapId); + } + + void VMapManager2::unloadSingleMap(uint32 mapId) + { + auto instanceTree = iInstanceMapTrees.find(mapId); if (instanceTree != iInstanceMapTrees.end() && instanceTree->second) { instanceTree->second->UnloadMap(this); @@ -175,40 +191,18 @@ namespace VMAP } } - void VMapManager2::unloadMap(unsigned int mapId, int x, int y) - { - auto childMaps = iChildMapData.find(mapId); - if (childMaps != iChildMapData.end()) - for (uint32 childMapId : childMaps->second) - _unloadMap(childMapId, x, y); - - _unloadMap(mapId, x, y); - } - - void VMapManager2::unloadMap(unsigned int mapId) - { - auto childMaps = iChildMapData.find(mapId); - if (childMaps != iChildMapData.end()) - for (uint32 childMapId : childMaps->second) - _unloadMap(childMapId); - - _unloadMap(mapId); - } - bool VMapManager2::isInLineOfSight(unsigned int mapId, float x1, float y1, float z1, float x2, float y2, float z2, ModelIgnoreFlags ignoreFlags) { if (!isLineOfSightCalcEnabled() || IsVMAPDisabledForPtr(mapId, VMAP_DISABLE_LOS)) return true; - InstanceTreeMap::const_iterator instanceTree = GetMapTree(mapId); + auto instanceTree = GetMapTree(mapId); if (instanceTree != iInstanceMapTrees.end()) { Vector3 pos1 = convertPositionToInternalRep(x1, y1, z1); Vector3 pos2 = convertPositionToInternalRep(x2, y2, z2); if (pos1 != pos2) - { return instanceTree->second->isInLineOfSight(pos1, pos2, ignoreFlags); - } } return true; @@ -222,7 +216,7 @@ namespace VMAP { if (isLineOfSightCalcEnabled() && !IsVMAPDisabledForPtr(mapId, VMAP_DISABLE_LOS)) { - InstanceTreeMap::const_iterator instanceTree = GetMapTree(mapId); + auto instanceTree = GetMapTree(mapId); if (instanceTree != iInstanceMapTrees.end()) { Vector3 pos1 = convertPositionToInternalRep(x1, y1, z1); @@ -252,7 +246,7 @@ namespace VMAP { if (isHeightCalcEnabled() && !IsVMAPDisabledForPtr(mapId, VMAP_DISABLE_HEIGHT)) { - InstanceTreeMap::const_iterator instanceTree = GetMapTree(mapId); + auto instanceTree = GetMapTree(mapId); if (instanceTree != iInstanceMapTrees.end()) { Vector3 pos = convertPositionToInternalRep(x, y, z); @@ -271,7 +265,7 @@ namespace VMAP { if (!IsVMAPDisabledForPtr(mapId, VMAP_DISABLE_AREAFLAG)) { - InstanceTreeMap::const_iterator instanceTree = GetMapTree(mapId); + auto instanceTree = GetMapTree(mapId); if (instanceTree != iInstanceMapTrees.end()) { Vector3 pos = convertPositionToInternalRep(x, y, z); @@ -289,7 +283,7 @@ namespace VMAP { if (!IsVMAPDisabledForPtr(mapId, VMAP_DISABLE_LIQUIDSTATUS)) { - InstanceTreeMap::const_iterator instanceTree = GetMapTree(mapId); + auto instanceTree = GetMapTree(mapId); if (instanceTree != iInstanceMapTrees.end()) { LocationInfo info; @@ -323,7 +317,7 @@ namespace VMAP data.areaInfo = boost::in_place(adtId, rootId, groupId, flags); return; } - InstanceTreeMap::const_iterator instanceTree = GetMapTree(mapId); + auto instanceTree = GetMapTree(mapId); if (instanceTree != iInstanceMapTrees.end()) { LocationInfo info; @@ -350,7 +344,7 @@ namespace VMAP //! Critical section, thread safe access to iLoadedModelFiles std::lock_guard lock(LoadedModelFilesLock); - ModelFileMap::iterator model = iLoadedModelFiles.find(filename); + auto model = iLoadedModelFiles.find(filename); if (model == iLoadedModelFiles.end()) { WorldModel* worldmodel = new WorldModel(); @@ -376,7 +370,7 @@ namespace VMAP //! Critical section, thread safe access to iLoadedModelFiles std::lock_guard lock(LoadedModelFilesLock); - ModelFileMap::iterator model = iLoadedModelFiles.find(filename); + auto model = iLoadedModelFiles.find(filename); if (model == iLoadedModelFiles.end()) { VMAP_ERROR_LOG("misc", "VMapManager2: trying to unload non-loaded file '%s'", filename.c_str()); diff --git a/src/common/Collision/Management/VMapManager2.h b/src/common/Collision/Management/VMapManager2.h index 75e43d420d1..3782e76001e 100644 --- a/src/common/Collision/Management/VMapManager2.h +++ b/src/common/Collision/Management/VMapManager2.h @@ -87,10 +87,6 @@ namespace VMAP // Mutex for iLoadedModelFiles std::mutex LoadedModelFilesLock; - bool _loadMap(uint32 mapId, const std::string& basePath, uint32 tileX, uint32 tileY); - void _unloadMap(uint32 mapId, int x, int y); - void _unloadMap(uint32 mapId); - static uint32 GetLiquidFlagsDummy(uint32) { return 0; } static bool IsVMAPDisabledForDummy(uint32 /*entry*/, uint8 /*flags*/) { return false; } @@ -102,14 +98,18 @@ namespace VMAP static std::string getMapFileName(unsigned int mapId); VMapManager2(); - ~VMapManager2(void); + ~VMapManager2(); void InitializeThreadUnsafe(std::unordered_map> const& mapData); - int loadMap(const char* pBasePath, unsigned int mapId, int x, int y) override; + int loadMap(const char* pBasePath, unsigned int mapId, int x, int y) override; + bool loadSingleMap(uint32 mapId, const std::string& basePath, uint32 tileX, uint32 tileY); void unloadMap(unsigned int mapId, int x, int y) override; + void unloadSingleMap(uint32 mapId, int x, int y); + void unloadMap(unsigned int mapId) override; + void unloadSingleMap(uint32 mapId); bool isInLineOfSight(unsigned int mapId, float x1, float y1, float z1, float x2, float y2, float z2, ModelIgnoreFlags ignoreFlags) override ; /** diff --git a/src/tools/mmaps_generator/PathGenerator.cpp b/src/tools/mmaps_generator/PathGenerator.cpp index 83182a7f078..bd67850c98e 100644 --- a/src/tools/mmaps_generator/PathGenerator.cpp +++ b/src/tools/mmaps_generator/PathGenerator.cpp @@ -298,10 +298,10 @@ int main(int argc, char** argv) return silent ? -3 : finish("Press ENTER to close...", -3); std::string mapPath = (boost::filesystem::path("dbc") / "Map.dbc").string(); - DBCFileLoader* loader = new DBCFileLoader(); std::unordered_map> mapData; { + DBCFileLoader* loader = new DBCFileLoader(); if (!loader->Load(mapPath.c_str(), "x")) { delete loader; diff --git a/src/tools/mmaps_generator/TerrainBuilder.cpp b/src/tools/mmaps_generator/TerrainBuilder.cpp index 4787883a530..28ef56e8439 100644 --- a/src/tools/mmaps_generator/TerrainBuilder.cpp +++ b/src/tools/mmaps_generator/TerrainBuilder.cpp @@ -640,7 +640,7 @@ namespace MMAP bool TerrainBuilder::loadVMap(uint32 mapID, uint32 tileX, uint32 tileY, MeshData &meshData) { VMapManager2* vmapManager = static_cast(VMapFactory::createOrGetVMapManager()); - int result = vmapManager->loadMap("vmaps", mapID, tileX, tileY); + int result = vmapManager->loadSingleMap(mapID, "vmaps", tileX, tileY); bool retval = false; do diff --git a/src/tools/vmap4_extractor/vmapexport.cpp b/src/tools/vmap4_extractor/vmapexport.cpp index 99a6543f639..fbf6dff3230 100644 --- a/src/tools/vmap4_extractor/vmapexport.cpp +++ b/src/tools/vmap4_extractor/vmapexport.cpp @@ -398,11 +398,10 @@ void ParsMapFiles() if (itr == wdts.end()) { char fn[512]; - char id[10]; char* name = map_ids[mapId].name; sprintf(fn, "World\\Maps\\%s\\%s.wdt", name, name); itr = wdts.emplace(std::piecewise_construct, std::forward_as_tuple(mapId), std::forward_as_tuple(fn, name, maps_that_are_parents.count(mapId) > 0)).first; - if (!itr->second.init(id, mapId)) + if (!itr->second.init(mapId)) { wdts.erase(itr); return nullptr; @@ -418,9 +417,9 @@ void ParsMapFiles() { WDTFile* parentWDT = itr->second.parent_id >= 0 ? getWDT(itr->second.parent_id) : nullptr; printf("Processing Map %u\n[", itr->first); - for (int x = 0; x < 64; ++x) + for (int32 x = 0; x < 64; ++x) { - for (int y = 0; y < 64; ++y) + for (int32 y = 0; y < 64; ++y) { bool success = false; if (ADTFile* ADT = WDT->GetMap(x, y)) diff --git a/src/tools/vmap4_extractor/wdtfile.cpp b/src/tools/vmap4_extractor/wdtfile.cpp index c75005a41a7..e5856d9aafa 100644 --- a/src/tools/vmap4_extractor/wdtfile.cpp +++ b/src/tools/vmap4_extractor/wdtfile.cpp @@ -33,67 +33,68 @@ char * wdtGetPlainName(char * FileName) extern HANDLE WorldMpq; -WDTFile::WDTFile(char* file_name, char* file_name1, bool cache):WDT(WorldMpq, file_name) +WDTFile::WDTFile(char const* filePath, std::string mapName, bool cache) + : _file(WorldMpq, filePath), _mapName(std::move(mapName)) { - filename.append(file_name1,strlen(file_name1)); if (cache) { - adtCache = new ADTCache(); - memset(adtCache->file, 0, sizeof(adtCache->file)); + _adtCache = std::make_unique(); + memset(_adtCache->file, 0, sizeof(_adtCache->file)); } else - adtCache = nullptr; + _adtCache = nullptr; } -bool WDTFile::init(char* /*map_id*/, unsigned int mapID) +WDTFile::~WDTFile() = default; + +bool WDTFile::init(uint32 mapId) { - if (WDT.isEof()) - { - //printf("Can't find WDT file.\n"); + if (_file.isEof()) return false; - } char fourcc[5]; uint32 size; std::string dirname = std::string(szWorkDirWmo) + "/dir_bin"; - FILE *dirfile; - dirfile = fopen(dirname.c_str(), "ab"); + FILE* dirfile = fopen(dirname.c_str(), "ab"); if (!dirfile) { printf("Can't open dirfile!'%s'\n", dirname.c_str()); return false; } - while (!WDT.isEof()) + while (!_file.isEof()) { - WDT.read(fourcc,4); - WDT.read(&size, 4); + _file.read(fourcc, 4); + _file.read(&size, 4); flipcc(fourcc); fourcc[4] = 0; - size_t nextpos = WDT.getPos() + size; + size_t nextpos = _file.getPos() + size; - if (!strcmp(fourcc,"MAIN")) + if (!strcmp(fourcc, "MAIN")) { } - if (!strcmp(fourcc,"MWMO")) + if (!strcmp(fourcc, "MWMO")) { // global map objects if (size) { char *buf = new char[size]; - WDT.read(buf, size); - char *p=buf; - int q = 0; - gWmoInstansName = new std::string[size]; + _file.read(buf, size); + char *p = buf; while (p < buf + size) { - char* s=wdtGetPlainName(p); - FixNameCase(s,strlen(s)); - p=p+strlen(p)+1; - gWmoInstansName[q++] = s; + std::string path(p); + + char* s = wdtGetPlainName(p); + FixNameCase(s, strlen(s)); + FixNameSpaces(s, strlen(s)); + p = p + strlen(p) + 1; + _wmoNames.push_back(s); + + ExtractSingleWmo(path); } delete[] buf; } @@ -103,53 +104,44 @@ bool WDTFile::init(char* /*map_id*/, unsigned int mapID) // global wmo instance data if (size) { - gnWMO = (int)size / 64; + int32 gnWMO = (int)size / 64; for (int i = 0; i < gnWMO; ++i) { int id; - WDT.read(&id, 4); - WMOInstance inst(WDT,gWmoInstansName[id].c_str(), mapID, 65, 65, mapID, dirfile, nullptr); + _file.read(&id, 4); + WMOInstance inst(_file, _wmoNames[id].c_str(), mapId, 65, 65, mapId, dirfile, nullptr); } - - delete[] gWmoInstansName; - gWmoInstansName = NULL; } } - WDT.seek((int)nextpos); + _file.seek((int)nextpos); } - WDT.close(); + _file.close(); fclose(dirfile); return true; } -WDTFile::~WDTFile(void) -{ - WDT.close(); - delete adtCache; -} - -ADTFile* WDTFile::GetMap(int x, int y) +ADTFile* WDTFile::GetMap(int32 x, int32 y) { if (!(x >= 0 && y >= 0 && x < 64 && y < 64)) return nullptr; - if (adtCache && adtCache->file[x][y]) - return adtCache->file[x][y].get(); + if (_adtCache && _adtCache->file[x][y]) + return _adtCache->file[x][y].get(); char name[512]; - sprintf(name,"World\\Maps\\%s\\%s_%d_%d_obj0.adt", filename.c_str(), filename.c_str(), x, y); - ADTFile* adt = new ADTFile(name, adtCache != nullptr); - if (adtCache) - adtCache->file[x][y].reset(adt); + sprintf(name, "World\\Maps\\%s\\%s_%d_%d_obj0.adt", _mapName.c_str(), _mapName.c_str(), x, y); + ADTFile* adt = new ADTFile(name, _adtCache != nullptr); + if (_adtCache) + _adtCache->file[x][y].reset(adt); return adt; } void WDTFile::FreeADT(ADTFile* adt) { - if (adtCache) + if (_adtCache) return; delete adt; diff --git a/src/tools/vmap4_extractor/wdtfile.h b/src/tools/vmap4_extractor/wdtfile.h index 98f4fc59338..4456da1d1d4 100644 --- a/src/tools/vmap4_extractor/wdtfile.h +++ b/src/tools/vmap4_extractor/wdtfile.h @@ -28,23 +28,25 @@ class ADTFile; class WDTFile { public: - WDTFile(char* file_name, char* file_name1, bool cache); - ~WDTFile(void); + WDTFile(char const* file_name, std::string mapName, bool cache); + ~WDTFile(); + bool init(uint32 mapId); - bool init(char* map_id, unsigned int mapID); - ADTFile* GetMap(int x, int y); + ADTFile* GetMap(int32 x, int32 y); void FreeADT(ADTFile* adt); std::string* gWmoInstansName; int gnWMO; private: - MPQFile WDT; - std::string filename; + MPQFile _file; + std::string _mapName; + std::vector _wmoNames; struct ADTCache { std::unique_ptr file[64][64]; - } *adtCache; + }; + std::unique_ptr _adtCache; }; #endif