aboutsummaryrefslogtreecommitdiff
path: root/src/common/Collision/Maps
diff options
context:
space:
mode:
authorShauren <shauren.trinity@gmail.com>2018-02-24 21:07:08 +0100
committerShauren <shauren.trinity@gmail.com>2018-03-25 19:28:36 +0300
commit4798d9ce7abd86be381af086763d8dbc9ed67ef3 (patch)
tree01ff6ae3c36ca7c443d8cdfd105825262e29e92a /src/common/Collision/Maps
parent91be2332e249147ce3169c46a7da77f0f8c2211d (diff)
Core/VMaps: Implement loading phased tiles
Closes #15163
Diffstat (limited to 'src/common/Collision/Maps')
-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
4 files changed, 112 insertions, 43 deletions
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
{