diff options
author | Shauren <shauren.trinity@gmail.com> | 2018-02-24 21:07:08 +0100 |
---|---|---|
committer | Shauren <shauren.trinity@gmail.com> | 2018-03-25 19:28:36 +0300 |
commit | 4798d9ce7abd86be381af086763d8dbc9ed67ef3 (patch) | |
tree | 01ff6ae3c36ca7c443d8cdfd105825262e29e92a | |
parent | 91be2332e249147ce3169c46a7da77f0f8c2211d (diff) |
Core/VMaps: Implement loading phased tiles
Closes #15163
21 files changed, 474 insertions, 195 deletions
diff --git a/src/common/Collision/Management/IVMapManager.h b/src/common/Collision/Management/IVMapManager.h index cec5657f5ed..922549f53da 100644 --- a/src/common/Collision/Management/IVMapManager.h +++ b/src/common/Collision/Management/IVMapManager.h @@ -94,6 +94,8 @@ namespace VMAP */ virtual bool getAreaInfo(unsigned int pMapId, float x, float y, float &z, uint32 &flags, int32 &adtId, int32 &rootId, int32 &groupId) const=0; virtual bool GetLiquidLevel(uint32 pMapId, float x, float y, float z, uint8 ReqLiquidType, float &level, float &floor, uint32 &type) const=0; + + virtual int32 GetDistanceToClosestPrimaryTile(uint32 mapId, int32 x, int32 y) const = 0; }; } diff --git a/src/common/Collision/Management/VMapManager2.cpp b/src/common/Collision/Management/VMapManager2.cpp index 354e3c03b87..9e5955b8794 100644 --- a/src/common/Collision/Management/VMapManager2.cpp +++ b/src/common/Collision/Management/VMapManager2.cpp @@ -40,23 +40,35 @@ 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(); - } } - void VMapManager2::InitializeThreadUnsafe(const std::vector<uint32>& mapIds) + 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<uint32, std::vector<uint32>> const& mapData) { // the caller must pass the list of all mapIds that will be used in the VMapManager2 lifetime - for (uint32 const& mapId : mapIds) - iInstanceMapTrees.insert(InstanceTreeMap::value_type(mapId, nullptr)); + iChildMapData = mapData; + for (std::pair<uint32 const, std::vector<uint32>> const& mapId : mapData) + { + iInstanceMapTrees.insert(InstanceTreeMap::value_type(mapId.first, nullptr)); + for (uint32 childMapId : mapId.second) + iParentMapData[childMapId] = mapId.first; + } thread_safe_environment = false; } @@ -87,8 +99,15 @@ 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 (!loadSingleMap(childMapId, basePath, x, y)) + result = VMAP_LOAD_RESULT_ERROR; + } else result = VMAP_LOAD_RESULT_ERROR; } @@ -96,20 +115,10 @@ 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) @@ -134,12 +143,22 @@ namespace VMAP return instanceTree->second->LoadMapTile(tileX, tileY, this); } - void VMapManager2::unloadMap(unsigned int mapId) + 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->UnloadMap(this); + instanceTree->second->UnloadMapTile(x, y, this); if (instanceTree->second->numLoadedTiles() == 0) { delete instanceTree->second; @@ -148,12 +167,22 @@ namespace VMAP } } - void VMapManager2::unloadMap(unsigned int mapId, int x, int y) + void VMapManager2::unloadMap(unsigned int mapId) + { + auto childMaps = iChildMapData.find(mapId); + if (childMaps != iChildMapData.end()) + for (uint32 childMapId : childMaps->second) + unloadSingleMap(childMapId); + + unloadSingleMap(mapId); + } + + void VMapManager2::unloadSingleMap(uint32 mapId) { - InstanceTreeMap::iterator instanceTree = iInstanceMapTrees.find(mapId); + auto instanceTree = iInstanceMapTrees.find(mapId); if (instanceTree != iInstanceMapTrees.end() && instanceTree->second) { - instanceTree->second->UnloadMapTile(x, y, this); + instanceTree->second->UnloadMap(this); if (instanceTree->second->numLoadedTiles() == 0) { delete instanceTree->second; @@ -167,15 +196,13 @@ namespace VMAP 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); - } } return true; @@ -189,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); @@ -219,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); @@ -238,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); @@ -256,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; @@ -279,12 +306,21 @@ namespace VMAP return false; } + int32 VMapManager2::GetDistanceToClosestPrimaryTile(uint32 mapId, int32 x, int32 y) const + { + auto instanceTree = GetMapTree(mapId); + if (instanceTree != iInstanceMapTrees.end()) + return instanceTree->second->GetDistanceToClosestPrimaryTile(x, y); + + return std::numeric_limits<int32>::max(); + } + WorldModel* VMapManager2::acquireModelInstance(const std::string& basepath, const std::string& filename) { //! Critical section, thread safe access to iLoadedModelFiles std::lock_guard<std::mutex> lock(LoadedModelFilesLock); - ModelFileMap::iterator model = iLoadedModelFiles.find(filename); + auto model = iLoadedModelFiles.find(filename); if (model == iLoadedModelFiles.end()) { WorldModel* worldmodel = new WorldModel(); @@ -307,7 +343,7 @@ namespace VMAP //! Critical section, thread safe access to iLoadedModelFiles std::lock_guard<std::mutex> 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()); @@ -323,7 +359,7 @@ namespace VMAP bool VMapManager2::existsMap(const char* basePath, unsigned int mapId, int x, int y) { - return StaticMapTree::CanLoadMap(std::string(basePath), mapId, x, y); + return StaticMapTree::CanLoadMap(std::string(basePath), mapId, x, y, this); } void VMapManager2::getInstanceMapTree(InstanceTreeMap &instanceMapTree) @@ -331,4 +367,13 @@ namespace VMAP instanceMapTree = iInstanceMapTrees; } + int32 VMapManager2::getParentMapId(uint32 mapId) const + { + auto itr = iParentMapData.find(mapId); + if (itr != iParentMapData.end()) + return int32(itr->second); + + return -1; + } + } // namespace VMAP diff --git a/src/common/Collision/Management/VMapManager2.h b/src/common/Collision/Management/VMapManager2.h index ba48c8cae34..fb23f6e239f 100644 --- a/src/common/Collision/Management/VMapManager2.h +++ b/src/common/Collision/Management/VMapManager2.h @@ -81,13 +81,12 @@ namespace VMAP // Tree to check collision ModelFileMap iLoadedModelFiles; InstanceTreeMap iInstanceMapTrees; + std::unordered_map<uint32, std::vector<uint32>> iChildMapData; + std::unordered_map<uint32, uint32> iParentMapData; bool thread_safe_environment; // Mutex for iLoadedModelFiles std::mutex LoadedModelFilesLock; - bool _loadMap(uint32 mapId, const std::string& basePath, uint32 tileX, uint32 tileY); - /* void _unloadMap(uint32 pMapId, uint32 x, uint32 y); */ - static uint32 GetLiquidFlagsDummy(uint32) { return 0; } static bool IsVMAPDisabledForDummy(uint32 /*entry*/, uint8 /*flags*/) { return false; } @@ -99,13 +98,18 @@ namespace VMAP static std::string getMapFileName(unsigned int mapId); VMapManager2(); - ~VMapManager2(void); + ~VMapManager2(); + + void InitializeThreadUnsafe(std::unordered_map<uint32, std::vector<uint32>> const& mapData); - void InitializeThreadUnsafe(const std::vector<uint32>& mapIds); 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) override ; /** @@ -119,6 +123,8 @@ namespace VMAP bool getAreaInfo(unsigned int pMapId, float x, float y, float& z, uint32& flags, int32& adtId, int32& rootId, int32& groupId) const override; bool GetLiquidLevel(uint32 pMapId, float x, float y, float z, uint8 reqLiquidType, float& level, float& floor, uint32& type) const override; + int32 GetDistanceToClosestPrimaryTile(uint32 mapId, int32 x, int32 y) const override; + WorldModel* acquireModelInstance(const std::string& basepath, const std::string& filename); void releaseModelInstance(const std::string& filename); @@ -131,6 +137,8 @@ namespace VMAP void getInstanceMapTree(InstanceTreeMap &instanceMapTree); + int32 getParentMapId(uint32 mapId) const; + typedef uint32(*GetLiquidFlagsFn)(uint32 liquidType); GetLiquidFlagsFn GetLiquidFlagsPtr; diff --git a/src/common/Collision/Maps/MapTree.cpp b/src/common/Collision/Maps/MapTree.cpp index 894705c56cc..55ff1711a54 100644 --- a/src/common/Collision/Maps/MapTree.cpp +++ b/src/common/Collision/Maps/MapTree.cpp @@ -237,9 +237,29 @@ namespace VMAP return(height); } + StaticMapTree::TileFileOpenResult StaticMapTree::OpenMapTileFile(std::string const& basePath, uint32 mapID, uint32 tileX, uint32 tileY, VMapManager2* vm) + { + TileFileOpenResult result; + result.Name = basePath + getTileFileName(mapID, tileX, tileY); + result.File = fopen(result.Name.c_str(), "rb"); + result.IsPrimary = true; + if (!result.File) + { + int32 parentMapId = vm->getParentMapId(mapID); + if (parentMapId != -1) + { + result.Name = basePath + getTileFileName(parentMapId, tileX, tileY); + result.File = fopen(result.Name.c_str(), "rb"); + result.IsPrimary = false; + } + } + + return result; + } + //========================================================= - bool StaticMapTree::CanLoadMap(const std::string &vmapPath, uint32 mapID, uint32 tileX, uint32 tileY) + bool StaticMapTree::CanLoadMap(const std::string &vmapPath, uint32 mapID, uint32 tileX, uint32 tileY, VMapManager2* vm) { std::string basePath = vmapPath; if (basePath.length() > 0 && basePath[basePath.length()-1] != '/' && basePath[basePath.length()-1] != '\\') @@ -259,8 +279,7 @@ namespace VMAP } if (tiled) { - std::string tilefile = basePath + getTileFileName(mapID, tileX, tileY); - FILE* tf = fopen(tilefile.c_str(), "rb"); + FILE* tf = OpenMapTileFile(basePath, mapID, tileX, tileY, vm).File; if (!tf) success = false; else @@ -321,6 +340,22 @@ namespace VMAP } } + if (success) + { + success = readChunk(rf, chunk, "SIDX", 4); + uint32 spawnIndicesSize = 0; + uint32 spawnId; + uint32 spawnIndex; + if (success && fread(&spawnIndicesSize, sizeof(uint32), 1, rf) != 1) success = false; + for (uint32 i = 0; i < spawnIndicesSize && success; ++i) + { + if (fread(&spawnId, sizeof(uint32), 1, rf) == 1 && fread(&spawnIndex, sizeof(uint32), 1, rf) == 1) + iSpawnIndices[spawnId] = spawnIndex; + else + success = false; + } + } + fclose(rf); return success; } @@ -337,6 +372,7 @@ namespace VMAP } iLoadedSpawns.clear(); iLoadedTiles.clear(); + iLoadedPrimaryTiles.clear(); } //========================================================= @@ -357,22 +393,21 @@ namespace VMAP } bool result = true; - std::string tilefile = iBasePath + getTileFileName(iMapID, tileX, tileY); - FILE* tf = fopen(tilefile.c_str(), "rb"); - if (tf) + TileFileOpenResult fileResult = OpenMapTileFile(iBasePath, iMapID, tileX, tileY, vm); + if (fileResult.File) { char chunk[8]; - if (!readChunk(tf, chunk, VMAP_MAGIC, 8)) + if (!readChunk(fileResult.File, chunk, VMAP_MAGIC, 8)) result = false; uint32 numSpawns = 0; - if (result && fread(&numSpawns, sizeof(uint32), 1, tf) != 1) + if (result && fread(&numSpawns, sizeof(uint32), 1, fileResult.File) != 1) result = false; for (uint32 i=0; i<numSpawns && result; ++i) { // read model spawns ModelSpawn spawn; - result = ModelSpawn::readFromFile(tf, spawn); + result = ModelSpawn::readFromFile(fileResult.File, spawn); if (result) { // acquire model instance @@ -381,15 +416,15 @@ namespace VMAP VMAP_ERROR_LOG("misc", "StaticMapTree::LoadMapTile() : could not acquire WorldModel pointer [%u, %u]", tileX, tileY); // update tree - uint32 referencedVal; - - if (fread(&referencedVal, sizeof(uint32), 1, tf) == 1) + auto spawnIndex = iSpawnIndices.find(spawn.ID); + if (spawnIndex != iSpawnIndices.end()) { + uint32 referencedVal = spawnIndex->second; if (!iLoadedSpawns.count(referencedVal)) { - if (referencedVal > iNTreeValues) + if (referencedVal >= iNTreeValues) { - VMAP_ERROR_LOG("maps", "StaticMapTree::LoadMapTile() : invalid tree element (%u/%u) referenced in tile %s", referencedVal, iNTreeValues, tilefile.c_str()); + VMAP_ERROR_LOG("maps", "StaticMapTree::LoadMapTile() : invalid tree element (%u/%u) referenced in tile %s", referencedVal, iNTreeValues, fileResult.Name.c_str()); continue; } @@ -412,7 +447,9 @@ namespace VMAP } } iLoadedTiles[packTileID(tileX, tileY)] = true; - fclose(tf); + if (fileResult.IsPrimary) + iLoadedPrimaryTiles.emplace_back(tileX, tileY); + fclose(fileResult.File); } else iLoadedTiles[packTileID(tileX, tileY)] = false; @@ -434,34 +471,33 @@ namespace VMAP } if (tile->second) // file associated with tile { - std::string tilefile = iBasePath + getTileFileName(iMapID, tileX, tileY); - FILE* tf = fopen(tilefile.c_str(), "rb"); - if (tf) + TileFileOpenResult fileResult = OpenMapTileFile(iBasePath, iMapID, tileX, tileY, vm); + if (fileResult.File) { bool result=true; char chunk[8]; - if (!readChunk(tf, chunk, VMAP_MAGIC, 8)) + if (!readChunk(fileResult.File, chunk, VMAP_MAGIC, 8)) result = false; uint32 numSpawns; - if (fread(&numSpawns, sizeof(uint32), 1, tf) != 1) + if (fread(&numSpawns, sizeof(uint32), 1, fileResult.File) != 1) result = false; for (uint32 i=0; i<numSpawns && result; ++i) { // read model spawns ModelSpawn spawn; - result = ModelSpawn::readFromFile(tf, spawn); + result = ModelSpawn::readFromFile(fileResult.File, spawn); if (result) { // release model instance vm->releaseModelInstance(spawn.name); // update tree - uint32 referencedNode; - - if (fread(&referencedNode, sizeof(uint32), 1, tf) != 1) + auto spawnIndex = iSpawnIndices.find(spawn.ID); + if (spawnIndex == iSpawnIndices.end()) result = false; else { + uint32 referencedNode = spawnIndex->second; if (!iLoadedSpawns.count(referencedNode)) VMAP_ERROR_LOG("misc", "StaticMapTree::UnloadMapTile() : trying to unload non-referenced model '%s' (ID:%u)", spawn.name.c_str(), spawn.ID); else if (--iLoadedSpawns[referencedNode] == 0) @@ -472,10 +508,12 @@ namespace VMAP } } } - fclose(tf); + fclose(fileResult.File); } } iLoadedTiles.erase(tile); + iLoadedPrimaryTiles.erase(std::remove_if(iLoadedPrimaryTiles.begin(), iLoadedPrimaryTiles.end(), + [tileX, tileY](std::pair<uint32, uint32> const& p) { return p.first == tileX && p.second == tileY; }), iLoadedPrimaryTiles.end()); TC_METRIC_EVENT("map_events", "UnloadMapTile", "Map: " + std::to_string(iMapID) + " TileX: " + std::to_string(tileX) + " TileY: " + std::to_string(tileY)); } @@ -485,4 +523,13 @@ namespace VMAP models = iTreeValues; count = iNTreeValues; } + + int32 StaticMapTree::GetDistanceToClosestPrimaryTile(int32 x, int32 y) const + { + int32 minDistance = std::numeric_limits<int32>::max(); + for (std::pair<int32, int32> const& primaryTile : iLoadedPrimaryTiles) + minDistance = std::min(minDistance, (primaryTile.first - x) * (primaryTile.first - x) + (primaryTile.second - y) * (primaryTile.second - y)); + + return minDistance; + } } diff --git a/src/common/Collision/Maps/MapTree.h b/src/common/Collision/Maps/MapTree.h index a15f328fc76..bf991c547c9 100644 --- a/src/common/Collision/Maps/MapTree.h +++ b/src/common/Collision/Maps/MapTree.h @@ -47,23 +47,33 @@ namespace VMAP BIH iTree; ModelInstance* iTreeValues; // the tree entries uint32 iNTreeValues; + std::unordered_map<uint32, uint32> iSpawnIndices; // Store all the map tile idents that are loaded for that map // some maps are not splitted into tiles and we have to make sure, not removing the map before all tiles are removed // empty tiles have no tile file, hence map with bool instead of just a set (consistency check) loadedTileMap iLoadedTiles; + std::vector<std::pair<int32, int32>> iLoadedPrimaryTiles; // stores <tree_index, reference_count> to invalidate tree values, unload map, and to be able to report errors loadedSpawnMap iLoadedSpawns; std::string iBasePath; + struct TileFileOpenResult + { + FILE* File; + std::string Name; + bool IsPrimary; + }; + private: + static TileFileOpenResult OpenMapTileFile(std::string const& basePath, uint32 mapID, uint32 tileX, uint32 tileY, VMapManager2* vm); bool getIntersectionTime(const G3D::Ray& pRay, float &pMaxDist, bool pStopAtFirstHit) const; //bool containsLoadedMapTile(unsigned int pTileIdent) const { return(iLoadedMapTiles.containsKey(pTileIdent)); } public: static std::string getTileFileName(uint32 mapID, uint32 tileX, uint32 tileY); static uint32 packTileID(uint32 tileX, uint32 tileY) { return tileX<<16 | tileY; } - static void unpackTileID(uint32 ID, uint32 &tileX, uint32 &tileY) { tileX = ID>>16; tileY = ID&0xFF; } - static bool CanLoadMap(const std::string &basePath, uint32 mapID, uint32 tileX, uint32 tileY); + static void unpackTileID(uint32 ID, uint32 &tileX, uint32 &tileY) { tileX = ID >> 16; tileY = ID & 0xFF; } + static bool CanLoadMap(const std::string &basePath, uint32 mapID, uint32 tileX, uint32 tileY, VMapManager2* vm); StaticMapTree(uint32 mapID, const std::string &basePath); ~StaticMapTree(); @@ -82,6 +92,8 @@ namespace VMAP uint32 numLoadedTiles() const { return uint32(iLoadedTiles.size()); } void getModelInstances(ModelInstance* &models, uint32 &count); + int32 GetDistanceToClosestPrimaryTile(int32 x, int32 y) const; + private: StaticMapTree(StaticMapTree const& right) = delete; StaticMapTree& operator=(StaticMapTree const& right) = delete; diff --git a/src/common/Collision/Maps/TileAssembler.cpp b/src/common/Collision/Maps/TileAssembler.cpp index dd096850d3f..d9133301223 100644 --- a/src/common/Collision/Maps/TileAssembler.cpp +++ b/src/common/Collision/Maps/TileAssembler.cpp @@ -84,7 +84,7 @@ namespace VMAP if (entry->second.flags & MOD_M2) { if (!calculateTransformedBound(entry->second)) - break; + continue; } else if (entry->second.flags & MOD_WORLDSPAWN) // WMO maps and terrain maps use different origin, so we need to adapt :/ { @@ -92,7 +92,7 @@ namespace VMAP //entry->second.iPos += Vector3(533.33333f*32, 533.33333f*32, 0.f); entry->second.iBound = entry->second.iBound + Vector3(533.33333f*32, 533.33333f*32, 0.f); } - mapSpawns.push_back(&(entry->second)); + mapSpawns.push_back(&entry->second); spawnedModelFiles.insert(entry->second.name); } @@ -110,9 +110,6 @@ namespace VMAP } // ===> possibly move this code to StaticMapTree class - std::map<uint32, uint32> modelNodeIdx; - for (uint32 i=0; i<mapSpawns.size(); ++i) - modelNodeIdx.insert(pair<uint32, uint32>(mapSpawns[i]->ID, i)); // write map tree file std::stringstream mapfilename; @@ -139,7 +136,17 @@ namespace VMAP for (TileMap::iterator glob=globalRange.first; glob != globalRange.second && success; ++glob) { - success = ModelSpawn::writeToFile(mapfile, map_iter->second->UniqueEntries[glob->second]); + success = ModelSpawn::writeToFile(mapfile, map_iter->second->UniqueEntries[glob->second.Id]); + } + + // spawn id to index map + if (success && fwrite("SIDX", 4, 1, mapfile) != 1) success = false; + uint32 mapSpawnsSize = mapSpawns.size(); + if (success && fwrite(&mapSpawnsSize, sizeof(uint32), 1, mapfile) != 1) success = false; + for (uint32 i = 0; i < mapSpawnsSize; ++i) + { + if (success && fwrite(&mapSpawns[i]->ID, sizeof(uint32), 1, mapfile) != 1) success = false; + if (success && fwrite(&i, sizeof(uint32), 1, mapfile) != 1) success = false; } fclose(mapfile); @@ -151,8 +158,9 @@ namespace VMAP TileMap::iterator tile; for (tile = tileEntries.begin(); tile != tileEntries.end(); ++tile) { - const ModelSpawn &spawn = map_iter->second->UniqueEntries[tile->second]; - if (spawn.flags & MOD_WORLDSPAWN) // WDT spawn, saved as tile 65/65 currently... + if (tile->second.Flags & MOD_WORLDSPAWN) // WDT spawn, saved as tile 65/65 currently... + continue; + if (tile->second.Flags & MOD_PARENT_SPAWN) // tile belongs to parent map continue; uint32 nSpawns = tileEntries.count(tile->first); std::stringstream tilefilename; @@ -172,16 +180,12 @@ namespace VMAP { if (s) ++tile; - const ModelSpawn &spawn2 = map_iter->second->UniqueEntries[tile->second]; + const ModelSpawn &spawn2 = map_iter->second->UniqueEntries[tile->second.Id]; success = success && ModelSpawn::writeToFile(tilefile, spawn2); - // MapTree nodes to update when loading tile: - std::map<uint32, uint32>::iterator nIdx = modelNodeIdx.find(spawn2.ID); - if (success && fwrite(&nIdx->second, sizeof(uint32), 1, tilefile) != 1) success = false; } fclose(tilefile); } } - // break; //test, extract only first map; TODO: remvoe this line } // add an object models, listed in temp_gameobject_models file @@ -239,9 +243,9 @@ namespace VMAP printf("spawning Map %u\n", mapID); mapData[mapID] = current = new MapSpawns(); } - else current = (*map_iter).second; + else current = map_iter->second; current->UniqueEntries.insert(pair<uint32, ModelSpawn>(spawn.ID, spawn)); - current->TileEntries.insert(pair<uint32, uint32>(StaticMapTree::packTileID(tileX, tileY), spawn.ID)); + current->TileEntries.insert(pair<uint32, TileSpawn>(StaticMapTree::packTileID(tileX, tileY), TileSpawn{ spawn.ID, spawn.flags })); } bool success = (ferror(dirf) == 0); fclose(dirf); diff --git a/src/common/Collision/Maps/TileAssembler.h b/src/common/Collision/Maps/TileAssembler.h index f370bb638ba..8c3b3f5b88d 100644 --- a/src/common/Collision/Maps/TileAssembler.h +++ b/src/common/Collision/Maps/TileAssembler.h @@ -52,8 +52,14 @@ namespace VMAP void moveToBasePos(const G3D::Vector3& pBasePos) { iPos -= pBasePos; } }; + struct TileSpawn + { + uint32 Id; + uint32 Flags; + }; + typedef std::map<uint32, ModelSpawn> UniqueEntryMap; - typedef std::multimap<uint32, uint32> TileMap; + typedef std::multimap<uint32, TileSpawn> TileMap; struct TC_COMMON_API MapSpawns { diff --git a/src/common/Collision/Models/ModelInstance.h b/src/common/Collision/Models/ModelInstance.h index 4160fd0c879..1edf3b0f442 100644 --- a/src/common/Collision/Models/ModelInstance.h +++ b/src/common/Collision/Models/ModelInstance.h @@ -35,8 +35,9 @@ namespace VMAP enum ModelFlags { MOD_M2 = 1, - MOD_WORLDSPAWN = 1<<1, - MOD_HAS_BOUND = 1<<2 + MOD_WORLDSPAWN = 1 << 1, + MOD_HAS_BOUND = 1 << 2, + MOD_PARENT_SPAWN = 1 << 3 }; class TC_COMMON_API ModelSpawn diff --git a/src/common/Collision/VMapDefinitions.h b/src/common/Collision/VMapDefinitions.h index 5410eb2130c..c8f3f2fdbe3 100644 --- a/src/common/Collision/VMapDefinitions.h +++ b/src/common/Collision/VMapDefinitions.h @@ -25,8 +25,8 @@ namespace VMAP { - const char VMAP_MAGIC[] = "VMAP_4.5"; - const char RAW_VMAP_MAGIC[] = "VMAP045"; // used in extracted vmap files with raw data + const char VMAP_MAGIC[] = "VMAP_4.6"; + const char RAW_VMAP_MAGIC[] = "VMAP046"; // used in extracted vmap files with raw data const char GAMEOBJECT_MODELS[] = "GameObjectModels.dtree"; // defined in TileAssembler.cpp currently... diff --git a/src/server/game/World/World.cpp b/src/server/game/World/World.cpp index 65f5f9bd59c..125e9036e64 100644 --- a/src/server/game/World/World.cpp +++ b/src/server/game/World/World.cpp @@ -1576,11 +1576,17 @@ void World::SetInitialWorldSettings() sTaxiPathGraph.Initialize(); std::vector<uint32> mapIds; + std::unordered_map<uint32, std::vector<uint32>> mapData; for (MapEntry const* mapEntry : sMapStore) + { mapIds.push_back(mapEntry->ID); + mapData.emplace(std::piecewise_construct, std::forward_as_tuple(mapEntry->ID), std::forward_as_tuple()); + if (mapEntry->ParentMapID != -1) + mapData[mapEntry->ParentMapID].push_back(mapEntry->ID); + } if (VMAP::VMapManager2* vmmgr2 = dynamic_cast<VMAP::VMapManager2*>(VMAP::VMapFactory::createOrGetVMapManager())) - vmmgr2->InitializeThreadUnsafe(mapIds); + vmmgr2->InitializeThreadUnsafe(mapData); MMAP::MMapManager* mmmgr = MMAP::MMapFactory::createOrGetMMapManager(); mmmgr->InitializeThreadUnsafe(mapIds); diff --git a/src/tools/mmaps_generator/MapBuilder.cpp b/src/tools/mmaps_generator/MapBuilder.cpp index a0f99ec6e5d..251df44dfa3 100644 --- a/src/tools/mmaps_generator/MapBuilder.cpp +++ b/src/tools/mmaps_generator/MapBuilder.cpp @@ -216,7 +216,7 @@ namespace MMAP _workerThreads.push_back(std::thread(&MapBuilder::WorkerThread, this)); } - m_tiles.sort([](MapTiles a, MapTiles b) + m_tiles.sort([](MapTiles const& a, MapTiles const& b) { return a.m_tiles->size() > b.m_tiles->size(); }); diff --git a/src/tools/vmap4_extractor/adtfile.cpp b/src/tools/vmap4_extractor/adtfile.cpp index e70619f8d48..76292c33a1f 100644 --- a/src/tools/vmap4_extractor/adtfile.cpp +++ b/src/tools/vmap4_extractor/adtfile.cpp @@ -79,26 +79,33 @@ char* GetExtension(char* FileName) extern CASC::StorageHandle CascStorage; -ADTFile::ADTFile(char* filename) : ADT(CascStorage, filename, false), nWMO(0), nMDX(0) +ADTFile::ADTFile(char* filename, bool cache) : ADT(CascStorage, filename, false), nWMO(0), nMDX(0) { Adtfilename.append(filename); + cacheable = cache; + dirfileCache = nullptr; } -bool ADTFile::init(uint32 map_num, uint32 tileX, uint32 tileY) +bool ADTFile::init(uint32 map_num, uint32 tileX, uint32 tileY, uint32 originalMapId) { + if (dirfileCache) + return initFromCache(map_num, tileX, tileY, originalMapId); + if (ADT.isEof()) return false; 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; } + if (cacheable) + dirfileCache = new std::vector<ADTOutputCache>(); + while (!ADT.isEof()) { char fourcc[5]; @@ -173,7 +180,7 @@ bool ADTFile::init(uint32 map_num, uint32 tileX, uint32 tileY) { uint32 id; ADT.read(&id, 4); - ModelInstance inst(ADT, ModelInstanceNames[id].c_str(), map_num, tileX, tileY, dirfile); + ModelInstance inst(ADT, ModelInstanceNames[id].c_str(), map_num, tileX, tileY, originalMapId, dirfile, dirfileCache); } ModelInstanceNames.clear(); @@ -188,7 +195,7 @@ bool ADTFile::init(uint32 map_num, uint32 tileX, uint32 tileY) { uint32 id; ADT.read(&id, 4); - WMOInstance inst(ADT, WmoInstanceNames[id].c_str(), map_num, tileX, tileY, dirfile); + WMOInstance inst(ADT, WmoInstanceNames[id].c_str(), map_num, tileX, tileY, originalMapId, dirfile, dirfileCache); } WmoInstanceNames.clear(); @@ -204,7 +211,37 @@ bool ADTFile::init(uint32 map_num, uint32 tileX, uint32 tileY) return true; } +bool ADTFile::initFromCache(uint32 map_num, uint32 tileX, uint32 tileY, uint32 originalMapId) +{ + if (dirfileCache->empty()) + return true; + + std::string dirname = std::string(szWorkDirWmo) + "/dir_bin"; + FILE* dirfile = fopen(dirname.c_str(), "ab"); + if (!dirfile) + { + printf("Can't open dirfile!'%s'\n", dirname.c_str()); + return false; + } + + for (ADTOutputCache const& cached : *dirfileCache) + { + fwrite(&map_num, sizeof(uint32), 1, dirfile); + fwrite(&tileX, sizeof(uint32), 1, dirfile); + fwrite(&tileY, sizeof(uint32), 1, dirfile); + uint32 flags = cached.Flags; + if (map_num != originalMapId) + flags |= MOD_PARENT_SPAWN; + fwrite(&flags, sizeof(uint32), 1, dirfile); + fwrite(cached.Data.data(), cached.Data.size(), 1, dirfile); + } + + fclose(dirfile); + return true; +} + ADTFile::~ADTFile() { ADT.close(); + delete dirfileCache; } diff --git a/src/tools/vmap4_extractor/adtfile.h b/src/tools/vmap4_extractor/adtfile.h index b388c8a581f..efe35647147 100644 --- a/src/tools/vmap4_extractor/adtfile.h +++ b/src/tools/vmap4_extractor/adtfile.h @@ -106,31 +106,28 @@ struct MapChunkHeader uint32 effectId; }; +struct ADTOutputCache +{ + uint32 Flags; + std::vector<uint8> Data; +}; class ADTFile { private: - //size_t mcnk_offsets[256], mcnk_sizes[256]; CASCFile ADT; - //mcell Mcell; std::string Adtfilename; + bool cacheable; + std::vector<ADTOutputCache>* dirfileCache; public: - ADTFile(char* filename); + ADTFile(char* filename, bool cache); ~ADTFile(); int nWMO; int nMDX; std::vector<std::string> WmoInstanceNames; std::vector<std::string> ModelInstanceNames; - bool init(uint32 map_num, uint32 tileX, uint32 tileY); - //void LoadMapChunks(); - - //uint32 wmo_count; -/* - const mcell& Getmcell() const - { - return Mcell; - } -*/ + bool init(uint32 map_num, uint32 tileX, uint32 tileY, uint32 originalMapId); + bool initFromCache(uint32 map_num, uint32 tileX, uint32 tileY, uint32 originalMapId); }; char const* GetPlainName(char const* FileName); diff --git a/src/tools/vmap4_extractor/model.cpp b/src/tools/vmap4_extractor/model.cpp index 12d7ee0ab2d..7ba041ef95e 100644 --- a/src/tools/vmap4_extractor/model.cpp +++ b/src/tools/vmap4_extractor/model.cpp @@ -19,6 +19,7 @@ #include "vmapexport.h" #include "model.h" #include "wmo.h" +#include "adtfile.h" #include "cascfile.h" #include <cassert> #include <algorithm> @@ -154,7 +155,7 @@ Vec3D fixCoordSystem2(Vec3D v) return Vec3D(v.x, v.z, v.y); } -ModelInstance::ModelInstance(CASCFile& f, char const* ModelInstName, uint32 mapID, uint32 tileX, uint32 tileY, FILE *pDirfile) +ModelInstance::ModelInstance(CASCFile& f, char const* ModelInstName, uint32 mapID, uint32 tileX, uint32 tileY, uint32 originalMapId, FILE* pDirfile, std::vector<ADTOutputCache>* dirfileCache) : id(0), scale(0), flags(0) { float ff[3]; @@ -190,6 +191,8 @@ ModelInstance::ModelInstance(CASCFile& f, char const* ModelInstName, uint32 mapI uint32 tcflags = MOD_M2; if (tileX == 65 && tileY == 65) tcflags |= MOD_WORLDSPAWN; + if (mapID != originalMapId) + tcflags |= MOD_PARENT_SPAWN; //write mapID, tileX, tileY, Flags, ID, Pos, Rot, Scale, name fwrite(&mapID, sizeof(uint32), 1, pDirfile); @@ -205,6 +208,34 @@ ModelInstance::ModelInstance(CASCFile& f, char const* ModelInstName, uint32 mapI fwrite(&nlen, sizeof(uint32), 1, pDirfile); fwrite(ModelInstName, sizeof(char), nlen, pDirfile); + if (dirfileCache) + { + dirfileCache->emplace_back(); + ADTOutputCache& cacheModelData = dirfileCache->back(); + cacheModelData.Flags = tcflags & ~MOD_PARENT_SPAWN; + cacheModelData.Data.resize( + sizeof(uint16) + // adtId + sizeof(uint32) + // id + sizeof(float) * 3 + // pos + sizeof(float) * 3 + // rot + sizeof(float) + // sc + sizeof(uint32) + // nlen + nlen); // ModelInstName + + uint8* cacheData = cacheModelData.Data.data(); +#define CACHE_WRITE(value, size, count, dest) memcpy(dest, value, size * count); dest += size * count; + + CACHE_WRITE(&adtId, sizeof(uint16), 1, cacheData); + CACHE_WRITE(&id, sizeof(uint32), 1, cacheData); + CACHE_WRITE(&pos, sizeof(float), 3, cacheData); + CACHE_WRITE(&rot, sizeof(float), 3, cacheData); + CACHE_WRITE(&sc, sizeof(float), 1, cacheData); + CACHE_WRITE(&nlen, sizeof(uint32), 1, cacheData); + CACHE_WRITE(ModelInstName, sizeof(char), nlen, cacheData); + +#undef CACHE_WRITE + } + /* int realx1 = (int) ((float) pos.x / 533.333333f); int realy1 = (int) ((float) pos.z / 533.333333f); int realx2 = (int) ((float) pos.x / 533.333333f); diff --git a/src/tools/vmap4_extractor/model.h b/src/tools/vmap4_extractor/model.h index 312c1c8cf12..8e8a8c899d6 100644 --- a/src/tools/vmap4_extractor/model.h +++ b/src/tools/vmap4_extractor/model.h @@ -24,6 +24,7 @@ #include <vector> class CASCFile; +struct ADTOutputCache; Vec3D fixCoordSystem(Vec3D v); @@ -59,7 +60,7 @@ public: float sc; ModelInstance() : id(0), scale(0), flags(0), sc(0.0f) {} - ModelInstance(CASCFile& f, char const* ModelInstName, uint32 mapID, uint32 tileX, uint32 tileY, FILE* pDirfile); + ModelInstance(CASCFile& f, char const* ModelInstName, uint32 mapID, uint32 tileX, uint32 tileY, uint32 originalMapId, FILE* pDirfile, std::vector<ADTOutputCache>* dirfileCache); }; diff --git a/src/tools/vmap4_extractor/vmapexport.cpp b/src/tools/vmap4_extractor/vmapexport.cpp index 3f68a0c9fe4..80f10fd2af3 100644 --- a/src/tools/vmap4_extractor/vmapexport.cpp +++ b/src/tools/vmap4_extractor/vmapexport.cpp @@ -33,6 +33,7 @@ #include <list> #include <map> #include <unordered_map> +#include <unordered_set> #include <vector> #include <cstdio> #include <cerrno> @@ -54,14 +55,14 @@ CASC::StorageHandle CascStorage; -typedef struct +struct map_info { char name[64]; - unsigned int id; -}map_id; + int32 parent_id; +}; -std::vector<map_id> map_ids; -uint32 map_count; +std::map<uint32, map_info> map_ids; +std::unordered_set<uint32> maps_that_are_parents; boost::filesystem::path input_path; bool preciseVectorData = false; @@ -108,7 +109,7 @@ struct MapLoadInfo //static const char * szWorkDirMaps = ".\\Maps"; const char* szWorkDirWmo = "./Buildings"; -const char* szRawVMAPMagic = "VMAP045"; +const char* szRawVMAPMagic = "VMAP046"; #define CASC_LOCALES_COUNT 17 char const* CascLocaleNames[CASC_LOCALES_COUNT] = @@ -274,26 +275,49 @@ bool ExtractSingleWmo(std::string& fname) void ParsMapFiles() { - char fn[512]; - //char id_filename[64]; - char id[10]; - for (unsigned int i = 0; i < map_ids.size(); ++i) + std::unordered_map<uint32, WDTFile> wdts; + auto getWDT = [&wdts](uint32 mapId) -> WDTFile* + { + auto itr = wdts.find(mapId); + if (itr == wdts.end()) + { + char fn[512]; + 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(mapId)) + { + wdts.erase(itr); + return nullptr; + } + } + + return &itr->second; + }; + + for (auto itr = map_ids.begin(); itr != map_ids.end(); ++itr) { - sprintf(id, "%04u", map_ids[i].id); - sprintf(fn,"World\\Maps\\%s\\%s.wdt", map_ids[i].name, map_ids[i].name); - WDTFile WDT(fn,map_ids[i].name); - if(WDT.init(id, map_ids[i].id)) + if (WDTFile* WDT = getWDT(itr->first)) { - printf("Processing Map %u\n[", map_ids[i].id); - for (int x=0; x<64; ++x) + WDTFile* parentWDT = itr->second.parent_id >= 0 ? getWDT(itr->second.parent_id) : nullptr; + printf("Processing Map %u\n[", itr->first); + for (int32 x = 0; x < 64; ++x) { - for (int y=0; y<64; ++y) + for (int32 y = 0; y < 64; ++y) { - if (ADTFile *ADT = WDT.GetMap(x,y)) + bool success = false; + if (ADTFile* ADT = WDT->GetMap(x, y)) + { + success = ADT->init(itr->first, x, y, itr->first); + WDT->FreeADT(ADT); + } + if (!success && parentWDT) { - //sprintf(id_filename,"%02u %02u %04u",x,y,map_ids[i].id);//!!!!!!!!! - ADT->init(map_ids[i].id, x, y); - delete ADT; + if (ADTFile* ADT = parentWDT->GetMap(x, y)) + { + ADT->init(itr->first, x, y, itr->second.parent_id); + parentWDT->FreeADT(ADT); + } } } printf("#"); @@ -398,7 +422,7 @@ int main(int argc, char ** argv) Trinity::Banner::Show("VMAP data extractor", [](char const* text) { printf("%s\n", text); }, nullptr); bool success = true; - const char *versionString = "V4.03 2015_05"; + const char *versionString = "V4.06 2018_02"; // Use command line arguments, when some if (!processArgv(argc, argv, versionString)) @@ -480,36 +504,35 @@ int main(int argc, char ** argv) exit(1); } - map_ids.resize(db2.GetRecordCount()); - std::unordered_map<uint32, uint32> idToIndex; for (uint32 x = 0; x < db2.GetRecordCount(); ++x) { DB2Record record = db2.GetRecord(x); - map_ids[x].id = record.GetId(); + map_info& m = map_ids[record.GetId()]; const char* map_name = record.GetString("Directory"); - size_t max_map_name_length = sizeof(map_ids[x].name); + size_t max_map_name_length = sizeof(m.name); if (strlen(map_name) >= max_map_name_length) { printf("Fatal error: Map name too long!\n"); exit(1); } - strncpy(map_ids[x].name, map_name, max_map_name_length); - map_ids[x].name[max_map_name_length - 1] = '\0'; - idToIndex[map_ids[x].id] = x; + strncpy(m.name, map_name, max_map_name_length); + m.name[max_map_name_length - 1] = '\0'; + m.parent_id = int16(record.GetUInt16("ParentMapID")); + if (m.parent_id >= 0) + maps_that_are_parents.insert(m.parent_id); } for (uint32 x = 0; x < db2.GetRecordCopyCount(); ++x) { DB2RecordCopy copy = db2.GetRecordCopy(x); - auto itr = idToIndex.find(copy.SourceRowId); - if (itr != idToIndex.end()) + auto itr = map_ids.find(copy.SourceRowId); + if (itr != map_ids.end()) { - map_id id; - id.id = copy.NewRowId; - strcpy(id.name, map_ids[itr->second].name); - map_ids.push_back(id); + map_info& id = map_ids[copy.NewRowId]; + strcpy(id.name, itr->second.name); + id.parent_id = itr->second.parent_id; } } diff --git a/src/tools/vmap4_extractor/vmapexport.h b/src/tools/vmap4_extractor/vmapexport.h index a73d259d52b..c798fb496ba 100644 --- a/src/tools/vmap4_extractor/vmapexport.h +++ b/src/tools/vmap4_extractor/vmapexport.h @@ -24,8 +24,9 @@ enum ModelFlags { MOD_M2 = 1, - MOD_WORLDSPAWN = 1<<1, - MOD_HAS_BOUND = 1<<2 + MOD_WORLDSPAWN = 1 << 1, + MOD_HAS_BOUND = 1 << 2, + MOD_PARENT_SPAWN = 1 << 3 }; extern const char * szWorkDirWmo; diff --git a/src/tools/vmap4_extractor/wdtfile.cpp b/src/tools/vmap4_extractor/wdtfile.cpp index c94775d7b0f..1f10e7883b0 100644 --- a/src/tools/vmap4_extractor/wdtfile.cpp +++ b/src/tools/vmap4_extractor/wdtfile.cpp @@ -19,6 +19,7 @@ #include "vmapexport.h" #include "wdtfile.h" #include "adtfile.h" +#include "Common.h" #include <cstdio> char * wdtGetPlainName(char * FileName) @@ -32,40 +33,45 @@ char * wdtGetPlainName(char * FileName) extern CASC::StorageHandle CascStorage; -WDTFile::WDTFile(char* file_name, char* file_name1):WDT(CascStorage, file_name), gnWMO(0) +WDTFile::WDTFile(char const* storagePath, std::string mapName, bool cache) + : _file(CascStorage, storagePath), _mapName(std::move(mapName)) { - filename.append(file_name1,strlen(file_name1)); + if (cache) + { + _adtCache = Trinity::make_unique<ADTCache>(); + memset(_adtCache->file, 0, sizeof(_adtCache->file)); + } + else + _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"); - if(!dirfile) + 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")) { @@ -76,7 +82,7 @@ bool WDTFile::init(char* /*map_id*/, unsigned int mapID) if (size) { char *buf = new char[size]; - WDT.read(buf, size); + _file.read(buf, size); char *p = buf; while (p < buf + size) { @@ -86,7 +92,7 @@ bool WDTFile::init(char* /*map_id*/, unsigned int mapID) FixNameCase(s, strlen(s)); FixNameSpaces(s, strlen(s)); p = p + strlen(p) + 1; - gWmoInstansName.push_back(s); + _wmoNames.push_back(s); ExtractSingleWmo(path); } @@ -98,36 +104,45 @@ 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, dirfile); + _file.read(&id, 4); + WMOInstance inst(_file, _wmoNames[id].c_str(), mapId, 65, 65, mapId, dirfile, nullptr); } } } - WDT.seek((int)nextpos); + _file.seek((int)nextpos); } - WDT.close(); + _file.close(); fclose(dirfile); return true; } -WDTFile::~WDTFile(void) +ADTFile* WDTFile::GetMap(int32 x, int32 y) { - WDT.close(); -} + if (!(x >= 0 && y >= 0 && x < 64 && y < 64)) + return nullptr; -ADTFile* WDTFile::GetMap(int x, int z) -{ - if(!(x>=0 && z >= 0 && x<64 && z<64)) - return NULL; + 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, z); - return new ADTFile(name); + 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) + return; + + delete adt; } diff --git a/src/tools/vmap4_extractor/wdtfile.h b/src/tools/vmap4_extractor/wdtfile.h index 89b71e9b99d..2fe09b94849 100644 --- a/src/tools/vmap4_extractor/wdtfile.h +++ b/src/tools/vmap4_extractor/wdtfile.h @@ -21,6 +21,7 @@ #include "cascfile.h" #include "wmo.h" +#include <memory> #include <string> #include <vector> @@ -28,18 +29,22 @@ class ADTFile; class WDTFile { -private: - CASCFile WDT; - std::string filename; public: - WDTFile(char* file_name, char* file_name1); - ~WDTFile(void); - bool init(char* map_id, unsigned int mapID); - - std::vector<std::string> gWmoInstansName; - int gnWMO; + WDTFile(char const* storagePath, std::string mapName, bool cache); + ~WDTFile(); + bool init(uint32 mapId); - ADTFile* GetMap(int x, int z); + ADTFile* GetMap(int32 x, int32 y); + void FreeADT(ADTFile* adt); +private: + CASCFile _file; + std::string _mapName; + std::vector<std::string> _wmoNames; + struct ADTCache + { + std::unique_ptr<ADTFile> file[64][64]; + }; + std::unique_ptr<ADTCache> _adtCache; }; #endif diff --git a/src/tools/vmap4_extractor/wmo.cpp b/src/tools/vmap4_extractor/wmo.cpp index 4607856d304..e24c95e6987 100644 --- a/src/tools/vmap4_extractor/wmo.cpp +++ b/src/tools/vmap4_extractor/wmo.cpp @@ -18,6 +18,7 @@ #include "vmapexport.h" #include "wmo.h" +#include "adtfile.h" #include "vec3d.h" #include <cstdio> #include <cstdlib> @@ -514,7 +515,7 @@ WMOGroup::~WMOGroup() delete [] LiquBytes; } -WMOInstance::WMOInstance(CASCFile& f, char const* WmoInstName, uint32 mapID, uint32 tileX, uint32 tileY, FILE* pDirfile) +WMOInstance::WMOInstance(CASCFile& f, char const* WmoInstName, uint32 mapID, uint32 tileX, uint32 tileY, uint32 originalMapId, FILE* pDirfile, std::vector<ADTOutputCache>* dirfileCache) : currx(0), curry(0), wmo(NULL), doodadset(0), pos(), indx(0), id(0) { float ff[3]; @@ -578,7 +579,11 @@ WMOInstance::WMOInstance(CASCFile& f, char const* WmoInstName, uint32 mapID, uin float scale = 1.0f; uint32 flags = MOD_HAS_BOUND; - if(tileX == 65 && tileY == 65) flags |= MOD_WORLDSPAWN; + if (tileX == 65 && tileY == 65) + flags |= MOD_WORLDSPAWN; + if (mapID != originalMapId) + flags |= MOD_PARENT_SPAWN; + //write mapID, tileX, tileY, Flags, ID, Pos, Rot, Scale, Bound_lo, Bound_hi, name fwrite(&mapID, sizeof(uint32), 1, pDirfile); fwrite(&tileX, sizeof(uint32), 1, pDirfile); @@ -591,10 +596,42 @@ WMOInstance::WMOInstance(CASCFile& f, char const* WmoInstName, uint32 mapID, uin fwrite(&scale, sizeof(float), 1, pDirfile); fwrite(&pos2, sizeof(float), 3, pDirfile); fwrite(&pos3, sizeof(float), 3, pDirfile); - uint32 nlen=strlen(WmoInstName); + uint32 nlen = strlen(WmoInstName); fwrite(&nlen, sizeof(uint32), 1, pDirfile); fwrite(WmoInstName, sizeof(char), nlen, pDirfile); + if (dirfileCache) + { + dirfileCache->emplace_back(); + ADTOutputCache& cacheModelData = dirfileCache->back(); + cacheModelData.Flags = flags & ~MOD_PARENT_SPAWN; + cacheModelData.Data.resize( + sizeof(uint16) + // adtId + sizeof(uint32) + // id + sizeof(float) * 3 + // pos + sizeof(float) * 3 + // rot + sizeof(float) + // scale + sizeof(float) * 3 + // pos2 + sizeof(float) * 3 + // pos3 + sizeof(uint32) + // nlen + nlen); // WmoInstName + + uint8* cacheData = cacheModelData.Data.data(); +#define CACHE_WRITE(value, size, count, dest) memcpy(dest, value, size * count); dest += size * count; + + CACHE_WRITE(&adtId, sizeof(uint16), 1, cacheData); + CACHE_WRITE(&id, sizeof(uint32), 1, cacheData); + CACHE_WRITE(&pos, sizeof(float), 3, cacheData); + CACHE_WRITE(&rot, sizeof(float), 3, cacheData); + CACHE_WRITE(&scale, sizeof(float), 1, cacheData); + CACHE_WRITE(&pos2, sizeof(float), 3, cacheData); + CACHE_WRITE(&pos3, sizeof(float), 3, cacheData); + CACHE_WRITE(&nlen, sizeof(uint32), 1, cacheData); + CACHE_WRITE(WmoInstName, sizeof(char), nlen, cacheData); + +#undef CACHE_WRITE + } + /* fprintf(pDirfile,"%s/%s %f,%f,%f_%f,%f,%f 1.0 %d %d %d,%d %d\n", MapName, WmoInstName, diff --git a/src/tools/vmap4_extractor/wmo.h b/src/tools/vmap4_extractor/wmo.h index e9482f0d8d1..8bb147e986f 100644 --- a/src/tools/vmap4_extractor/wmo.h +++ b/src/tools/vmap4_extractor/wmo.h @@ -43,6 +43,7 @@ enum MopyFlags class WMOInstance; class WMOManager; class CASCFile; +struct ADTOutputCache; /* for whatever reason a certain company just can't stick to one coordinate system... */ static inline Vec3D fixCoords(const Vec3D &v){ return Vec3D(v.z, v.x, v.y); } @@ -138,7 +139,7 @@ public: Vec3D pos2, pos3, rot; uint32 indx, id; - WMOInstance(CASCFile&f , char const* WmoInstName, uint32 mapID, uint32 tileX, uint32 tileY, FILE* pDirfile); + WMOInstance(CASCFile&f , char const* WmoInstName, uint32 mapID, uint32 tileX, uint32 tileY, uint32 originalMapId, FILE* pDirfile, std::vector<ADTOutputCache>* dirfileCache); }; #endif |