aboutsummaryrefslogtreecommitdiff
path: root/src/common/Collision
diff options
context:
space:
mode:
Diffstat (limited to 'src/common/Collision')
-rw-r--r--src/common/Collision/Management/IVMapManager.h2
-rw-r--r--src/common/Collision/Management/VMapManager2.cpp123
-rw-r--r--src/common/Collision/Management/VMapManager2.h18
-rw-r--r--src/common/Collision/Maps/MapTree.cpp97
-rw-r--r--src/common/Collision/Maps/MapTree.h16
-rw-r--r--src/common/Collision/Maps/TileAssembler.cpp34
-rw-r--r--src/common/Collision/Maps/TileAssembler.h8
-rw-r--r--src/common/Collision/Models/ModelInstance.h5
-rw-r--r--src/common/Collision/VMapDefinitions.h4
9 files changed, 216 insertions, 91 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...