diff options
| author | Carbenium <carbenium@outlook.com> | 2015-08-21 17:54:47 +0200 |
|---|---|---|
| committer | Carbenium <carbenium@outlook.com> | 2015-08-21 17:54:47 +0200 |
| commit | e4c97f66529ecfc2e9b3f675e5ebecd199c1d4dc (patch) | |
| tree | dc7332aaa75ad7c10d2bff9c35e5032b46eb3633 /src/common/Collision/Management | |
| parent | e9feddf862fd84eb106dd1d305e4a148ad1662bd (diff) | |
| parent | 1d2aafd39bcb79a67357d198ce9b2345642fdd39 (diff) | |
Merge pull request #15312 from StormBytePP/6.x_merge_common_and_move_database_out_of_shared
Core/Build: Merge common library and move database out of shared
Diffstat (limited to 'src/common/Collision/Management')
| -rw-r--r-- | src/common/Collision/Management/IVMapManager.h | 100 | ||||
| -rw-r--r-- | src/common/Collision/Management/MMapFactory.cpp | 44 | ||||
| -rw-r--r-- | src/common/Collision/Management/MMapFactory.h | 51 | ||||
| -rw-r--r-- | src/common/Collision/Management/MMapManager.cpp | 579 | ||||
| -rw-r--r-- | src/common/Collision/Management/MMapManager.h | 138 | ||||
| -rw-r--r-- | src/common/Collision/Management/VMapFactory.cpp | 42 | ||||
| -rw-r--r-- | src/common/Collision/Management/VMapFactory.h | 40 | ||||
| -rw-r--r-- | src/common/Collision/Management/VMapManager2.cpp | 329 | ||||
| -rw-r--r-- | src/common/Collision/Management/VMapManager2.h | 142 |
9 files changed, 1465 insertions, 0 deletions
diff --git a/src/common/Collision/Management/IVMapManager.h b/src/common/Collision/Management/IVMapManager.h new file mode 100644 index 00000000000..b890554257c --- /dev/null +++ b/src/common/Collision/Management/IVMapManager.h @@ -0,0 +1,100 @@ +/* + * Copyright (C) 2008-2015 TrinityCore <http://www.trinitycore.org/> + * Copyright (C) 2005-2010 MaNGOS <http://getmangos.com/> + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; either version 2 of the License, or (at your + * option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#ifndef _IVMAPMANAGER_H +#define _IVMAPMANAGER_H + +#include <string> +#include "Define.h" + +//=========================================================== + +/** +This is the minimum interface to the VMapMamager. +*/ + +namespace VMAP +{ + + enum VMAP_LOAD_RESULT + { + VMAP_LOAD_RESULT_ERROR, + VMAP_LOAD_RESULT_OK, + VMAP_LOAD_RESULT_IGNORED + }; + + #define VMAP_INVALID_HEIGHT -100000.0f // for check + #define VMAP_INVALID_HEIGHT_VALUE -200000.0f // real assigned value in unknown height case + + //=========================================================== + class IVMapManager + { + private: + bool iEnableLineOfSightCalc; + bool iEnableHeightCalc; + + public: + IVMapManager() : iEnableLineOfSightCalc(true), iEnableHeightCalc(true) { } + + virtual ~IVMapManager(void) { } + + virtual int loadMap(const char* pBasePath, unsigned int pMapId, int x, int y) = 0; + + virtual bool existsMap(const char* pBasePath, unsigned int pMapId, int x, int y) = 0; + + virtual void unloadMap(unsigned int pMapId, int x, int y) = 0; + virtual void unloadMap(unsigned int pMapId) = 0; + + virtual bool isInLineOfSight(unsigned int pMapId, float x1, float y1, float z1, float x2, float y2, float z2) = 0; + virtual float getHeight(unsigned int pMapId, float x, float y, float z, float maxSearchDist) = 0; + /** + test if we hit an object. return true if we hit one. rx, ry, rz will hold the hit position or the dest position, if no intersection was found + return a position, that is pReduceDist closer to the origin + */ + virtual bool getObjectHitPos(unsigned int pMapId, float x1, float y1, float z1, float x2, float y2, float z2, float& rx, float &ry, float& rz, float pModifyDist) = 0; + /** + send debug commands + */ + virtual bool processCommand(char *pCommand)= 0; + + /** + Enable/disable LOS calculation + It is enabled by default. If it is enabled in mid game the maps have to loaded manualy + */ + void setEnableLineOfSightCalc(bool pVal) { iEnableLineOfSightCalc = pVal; } + /** + Enable/disable model height calculation + It is enabled by default. If it is enabled in mid game the maps have to loaded manualy + */ + void setEnableHeightCalc(bool pVal) { iEnableHeightCalc = pVal; } + + bool isLineOfSightCalcEnabled() const { return(iEnableLineOfSightCalc); } + bool isHeightCalcEnabled() const { return(iEnableHeightCalc); } + bool isMapLoadingEnabled() const { return(iEnableLineOfSightCalc || iEnableHeightCalc ); } + + virtual std::string getDirFileName(unsigned int pMapId, int x, int y) const =0; + /** + Query world model area info. + \param z gets adjusted to the ground height for which this are info is valid + */ + 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; + }; + +} +#endif diff --git a/src/common/Collision/Management/MMapFactory.cpp b/src/common/Collision/Management/MMapFactory.cpp new file mode 100644 index 00000000000..667b8378c56 --- /dev/null +++ b/src/common/Collision/Management/MMapFactory.cpp @@ -0,0 +1,44 @@ +/* + * Copyright (C) 2008-2015 TrinityCore <http://www.trinitycore.org/> + * Copyright (C) 2005-2010 MaNGOS <http://getmangos.com/> + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; either version 2 of the License, or (at your + * option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#include "MMapFactory.h" +#include "Config.h" + +namespace MMAP +{ + // ######################## MMapFactory ######################## + // our global singleton copy + MMapManager* g_MMapManager = NULL; + + MMapManager* MMapFactory::createOrGetMMapManager() + { + if (g_MMapManager == NULL) + g_MMapManager = new MMapManager(); + + return g_MMapManager; + } + + void MMapFactory::clear() + { + if (g_MMapManager) + { + delete g_MMapManager; + g_MMapManager = NULL; + } + } +}
\ No newline at end of file diff --git a/src/common/Collision/Management/MMapFactory.h b/src/common/Collision/Management/MMapFactory.h new file mode 100644 index 00000000000..773983f81eb --- /dev/null +++ b/src/common/Collision/Management/MMapFactory.h @@ -0,0 +1,51 @@ +/* + * Copyright (C) 2008-2015 TrinityCore <http://www.trinitycore.org/> + * Copyright (C) 2005-2010 MaNGOS <http://getmangos.com/> + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; either version 2 of the License, or (at your + * option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#ifndef _MMAP_FACTORY_H +#define _MMAP_FACTORY_H + +#include "Define.h" +#include "MMapManager.h" +#include "DetourAlloc.h" +#include "DetourNavMesh.h" +#include "DetourNavMeshQuery.h" +#include <unordered_map> + +namespace MMAP +{ + enum MMAP_LOAD_RESULT + { + MMAP_LOAD_RESULT_ERROR, + MMAP_LOAD_RESULT_OK, + MMAP_LOAD_RESULT_IGNORED + }; + + // static class + // holds all mmap global data + // access point to MMapManager singleton + class MMapFactory + { + public: + static MMapManager* createOrGetMMapManager(); + static void clear(); + static bool IsPathfindingEnabled(uint32 mapId); + }; +} + +#endif + diff --git a/src/common/Collision/Management/MMapManager.cpp b/src/common/Collision/Management/MMapManager.cpp new file mode 100644 index 00000000000..f6ccfdf2720 --- /dev/null +++ b/src/common/Collision/Management/MMapManager.cpp @@ -0,0 +1,579 @@ +/* + * Copyright (C) 2008-2015 TrinityCore <http://www.trinitycore.org/> + * Copyright (C) 2005-2010 MaNGOS <http://getmangos.com/> + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; either version 2 of the License, or (at your + * option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#include "MMapManager.h" +#include "Log.h" +#include "Config.h" +#include "MMapFactory.h" + +namespace MMAP +{ + static char const* const MAP_FILE_NAME_FORMAT = "%smmaps/%04i.mmap"; + static char const* const TILE_FILE_NAME_FORMAT = "%smmaps/%04i%02i%02i.mmtile"; + + // ######################## MMapManager ######################## + MMapManager::~MMapManager() + { + for (MMapDataSet::iterator i = loadedMMaps.begin(); i != loadedMMaps.end(); ++i) + delete i->second; + + // by now we should not have maps loaded + // if we had, tiles in MMapData->mmapLoadedTiles, their actual data is lost! + } + + void MMapManager::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 MMapManager lifetime + for (auto const& p : mapData) + { + loadedMMaps.insert(MMapDataSet::value_type(p.first, nullptr)); + if (!p.second.empty()) + { + phaseMapData[p.first] = p.second; + for (uint32 phasedMapId : p.second) + _phaseTiles.insert(PhaseTileMap::value_type(phasedMapId, PhaseTileContainer())); + } + } + + thread_safe_environment = false; + } + + MMapDataSet::const_iterator MMapManager::GetMMapData(uint32 mapId) const + { + // return the iterator if found or end() if not found/NULL + MMapDataSet::const_iterator itr = loadedMMaps.find(mapId); + if (itr != loadedMMaps.cend() && !itr->second) + itr = loadedMMaps.cend(); + + return itr; + } + + bool MMapManager::loadMapData(uint32 mapId) + { + // we already have this map loaded? + MMapDataSet::iterator itr = loadedMMaps.find(mapId); + if (itr != loadedMMaps.end()) + { + if (itr->second) + return true; + } + else + { + if (thread_safe_environment) + itr = loadedMMaps.insert(MMapDataSet::value_type(mapId, nullptr)).first; + else + ASSERT(false, "Invalid mapId %u passed to MMapManager after startup in thread unsafe environment", mapId); + } + + // load and init dtNavMesh - read parameters from file + std::string fileName = Trinity::StringFormat(MAP_FILE_NAME_FORMAT, sConfigMgr->GetStringDefault("DataDir", "./").c_str(), mapId); + FILE* file = fopen(fileName.c_str(), "rb"); + if (!file) + { + TC_LOG_DEBUG("maps", "MMAP:loadMapData: Error: Could not open mmap file '%s'", fileName.c_str()); + return false; + } + + dtNavMeshParams params; + uint32 count = uint32(fread(¶ms, sizeof(dtNavMeshParams), 1, file)); + fclose(file); + if (count != 1) + { + TC_LOG_DEBUG("maps", "MMAP:loadMapData: Error: Could not read params from file '%s'", fileName.c_str()); + return false; + } + + dtNavMesh* mesh = dtAllocNavMesh(); + ASSERT(mesh); + if (dtStatusFailed(mesh->init(¶ms))) + { + dtFreeNavMesh(mesh); + TC_LOG_ERROR("maps", "MMAP:loadMapData: Failed to initialize dtNavMesh for mmap %04u from file %s", mapId, fileName.c_str()); + return false; + } + + TC_LOG_DEBUG("maps", "MMAP:loadMapData: Loaded %04i.mmap", mapId); + + // store inside our map list + MMapData* mmap_data = new MMapData(mesh, mapId); + + itr->second = mmap_data; + return true; + } + + uint32 MMapManager::packTileID(int32 x, int32 y) + { + return uint32(x << 16 | y); + } + + bool MMapManager::loadMap(const std::string& /*basePath*/, uint32 mapId, int32 x, int32 y) + { + // make sure the mmap is loaded and ready to load tiles + if (!loadMapData(mapId)) + return false; + + // get this mmap data + MMapData* mmap = loadedMMaps[mapId]; + ASSERT(mmap->navMesh); + + // check if we already have this tile loaded + uint32 packedGridPos = packTileID(x, y); + if (mmap->loadedTileRefs.find(packedGridPos) != mmap->loadedTileRefs.end()) + return false; + + // load this tile :: mmaps/MMMMXXYY.mmtile + std::string fileName = Trinity::StringFormat(TILE_FILE_NAME_FORMAT, sConfigMgr->GetStringDefault("DataDir", "./").c_str(), mapId, x, y); + FILE* file = fopen(fileName.c_str(), "rb"); + if (!file) + { + TC_LOG_DEBUG("maps", "MMAP:loadMap: Could not open mmtile file '%s'", fileName.c_str()); + return false; + } + + // read header + MmapTileHeader fileHeader; + if (fread(&fileHeader, sizeof(MmapTileHeader), 1, file) != 1 || fileHeader.mmapMagic != MMAP_MAGIC) + { + TC_LOG_ERROR("maps", "MMAP:loadMap: Bad header in mmap %04u%02i%02i.mmtile", mapId, x, y); + fclose(file); + return false; + } + + if (fileHeader.mmapVersion != MMAP_VERSION) + { + TC_LOG_ERROR("maps", "MMAP:loadMap: %04u%02i%02i.mmtile was built with generator v%i, expected v%i", + mapId, x, y, fileHeader.mmapVersion, MMAP_VERSION); + fclose(file); + return false; + } + + unsigned char* data = (unsigned char*)dtAlloc(fileHeader.size, DT_ALLOC_PERM); + ASSERT(data); + + size_t result = fread(data, fileHeader.size, 1, file); + if (!result) + { + TC_LOG_ERROR("maps", "MMAP:loadMap: Bad header or data in mmap %04u%02i%02i.mmtile", mapId, x, y); + fclose(file); + return false; + } + + fclose(file); + + dtMeshHeader* header = (dtMeshHeader*)data; + dtTileRef tileRef = 0; + + // memory allocated for data is now managed by detour, and will be deallocated when the tile is removed + if (dtStatusSucceed(mmap->navMesh->addTile(data, fileHeader.size, 0/*DT_TILE_FREE_DATA*/, 0, &tileRef))) + { + mmap->loadedTileRefs.insert(std::pair<uint32, dtTileRef>(packedGridPos, tileRef)); + ++loadedTiles; + TC_LOG_DEBUG("maps", "MMAP:loadMap: Loaded mmtile %04i[%02i, %02i] into %04i[%02i, %02i]", mapId, x, y, mapId, header->x, header->y); + + PhaseChildMapContainer::const_iterator phasedMaps = phaseMapData.find(mapId); + if (phasedMaps != phaseMapData.end()) + LoadPhaseTiles(phasedMaps, x, y); + + return true; + } + + TC_LOG_ERROR("maps", "MMAP:loadMap: Could not load %04u%02i%02i.mmtile into navmesh", mapId, x, y); + dtFree(data); + return false; + } + + PhasedTile* MMapManager::LoadTile(uint32 mapId, int32 x, int32 y) + { + // load this tile :: mmaps/MMMXXYY.mmtile + std::string fileName = Trinity::StringFormat(TILE_FILE_NAME_FORMAT, sConfigMgr->GetStringDefault("DataDir", "./").c_str(), mapId, x, y); + FILE* file = fopen(fileName.c_str(), "rb"); + if (!file) + { + // Not all tiles have phased versions, don't flood this msg + //TC_LOG_DEBUG("phase", "MMAP:LoadTile: Could not open mmtile file '%s'", fileName); + return NULL; + } + + PhasedTile* pTile = new PhasedTile(); + + // read header + if (fread(&pTile->fileHeader, sizeof(MmapTileHeader), 1, file) != 1 || pTile->fileHeader.mmapMagic != MMAP_MAGIC) + { + TC_LOG_ERROR("phase", "MMAP:LoadTile: Bad header in mmap %04u%02i%02i.mmtile", mapId, x, y); + fclose(file); + delete pTile; + return nullptr; + } + + if (pTile->fileHeader.mmapVersion != MMAP_VERSION) + { + TC_LOG_ERROR("phase", "MMAP:LoadTile: %04u%02i%02i.mmtile was built with generator v%i, expected v%i", + mapId, x, y, pTile->fileHeader.mmapVersion, MMAP_VERSION); + fclose(file); + delete pTile; + return nullptr; + } + + pTile->data = (unsigned char*)dtAlloc(pTile->fileHeader.size, DT_ALLOC_PERM); + ASSERT(pTile->data); + + size_t result = fread(pTile->data, pTile->fileHeader.size, 1, file); + if (!result) + { + TC_LOG_ERROR("phase", "MMAP:LoadTile: Bad header or data in mmap %04u%02i%02i.mmtile", mapId, x, y); + fclose(file); + delete pTile; + return nullptr; + } + + fclose(file); + + return pTile; + } + + void MMapManager::LoadPhaseTiles(PhaseChildMapContainer::const_iterator phasedMapData, int32 x, int32 y) + { + TC_LOG_DEBUG("phase", "MMAP:LoadPhaseTiles: Loading phased mmtiles for map %u, x: %i, y: %i", phasedMapData->first, x, y); + + uint32 packedGridPos = packTileID(x, y); + + for (uint32 phaseMapId : phasedMapData->second) + { + // only a few tiles have terrain swaps, do not write error for them + if (PhasedTile* data = LoadTile(phaseMapId, x, y)) + { + TC_LOG_DEBUG("phase", "MMAP:LoadPhaseTiles: Loaded phased %04u%02i%02i.mmtile for root phase map %u", phaseMapId, x, y, phasedMapData->first); + _phaseTiles[phaseMapId][packedGridPos] = data; + } + } + } + + void MMapManager::UnloadPhaseTile(PhaseChildMapContainer::const_iterator phasedMapData, int32 x, int32 y) + { + TC_LOG_DEBUG("phase", "MMAP:UnloadPhaseTile: Unloading phased mmtile for map %u, x: %i, y: %i", phasedMapData->first, x, y); + + uint32 packedGridPos = packTileID(x, y); + + for (uint32 phaseMapId : phasedMapData->second) + { + auto phasedTileItr = _phaseTiles.find(phaseMapId); + if (phasedTileItr == _phaseTiles.end()) + continue; + + auto dataItr = phasedTileItr->second.find(packedGridPos); + if (dataItr != phasedTileItr->second.end()) + { + TC_LOG_DEBUG("phase", "MMAP:UnloadPhaseTile: Unloaded phased %04u%02i%02i.mmtile for root phase map %u", phaseMapId, x, y, phasedMapData->first); + delete dataItr->second->data; + delete dataItr->second; + phasedTileItr->second.erase(dataItr); + } + } + } + + bool MMapManager::unloadMap(uint32 mapId, int32 x, int32 y) + { + // check if we have this map loaded + MMapDataSet::const_iterator itr = GetMMapData(mapId); + if (itr == loadedMMaps.end()) + { + // file may not exist, therefore not loaded + TC_LOG_DEBUG("maps", "MMAP:unloadMap: Asked to unload not loaded navmesh map. %04u%02i%02i.mmtile", mapId, x, y); + return false; + } + + MMapData* mmap = itr->second; + + // check if we have this tile loaded + uint32 packedGridPos = packTileID(x, y); + if (mmap->loadedTileRefs.find(packedGridPos) == mmap->loadedTileRefs.end()) + { + // file may not exist, therefore not loaded + TC_LOG_DEBUG("maps", "MMAP:unloadMap: Asked to unload not loaded navmesh tile. %04u%02i%02i.mmtile", mapId, x, y); + return false; + } + + dtTileRef tileRef = mmap->loadedTileRefs[packedGridPos]; + + // unload, and mark as non loaded + if (dtStatusFailed(mmap->navMesh->removeTile(tileRef, NULL, NULL))) + { + // this is technically a memory leak + // if the grid is later reloaded, dtNavMesh::addTile will return error but no extra memory is used + // we cannot recover from this error - assert out + TC_LOG_ERROR("maps", "MMAP:unloadMap: Could not unload %04u%02i%02i.mmtile from navmesh", mapId, x, y); + ASSERT(false); + } + else + { + mmap->loadedTileRefs.erase(packedGridPos); + --loadedTiles; + TC_LOG_DEBUG("maps", "MMAP:unloadMap: Unloaded mmtile %03i[%02i, %02i] from %04i", mapId, x, y, mapId); + + PhaseChildMapContainer::const_iterator phasedMaps = phaseMapData.find(mapId); + if (phasedMaps != phaseMapData.end()) + UnloadPhaseTile(phasedMaps, x, y); + return true; + } + + return false; + } + + bool MMapManager::unloadMap(uint32 mapId) + { + MMapDataSet::iterator itr = loadedMMaps.find(mapId); + if (itr == loadedMMaps.end() || !itr->second) + { + // file may not exist, therefore not loaded + TC_LOG_DEBUG("maps", "MMAP:unloadMap: Asked to unload not loaded navmesh map %04u", mapId); + return false; + } + + // unload all tiles from given map + MMapData* mmap = itr->second; + for (MMapTileSet::iterator i = mmap->loadedTileRefs.begin(); i != mmap->loadedTileRefs.end(); ++i) + { + uint32 x = (i->first >> 16); + uint32 y = (i->first & 0x0000FFFF); + if (dtStatusFailed(mmap->navMesh->removeTile(i->second, NULL, NULL))) + TC_LOG_ERROR("maps", "MMAP:unloadMap: Could not unload %04u%02i%02i.mmtile from navmesh", mapId, x, y); + else + { + PhaseChildMapContainer::const_iterator phasedMaps = phaseMapData.find(mapId); + if (phasedMaps != phaseMapData.end()) + UnloadPhaseTile(phasedMaps, x, y); + --loadedTiles; + TC_LOG_DEBUG("maps", "MMAP:unloadMap: Unloaded mmtile %04i[%02i, %02i] from %04i", mapId, x, y, mapId); + } + } + + delete mmap; + itr->second = nullptr; + TC_LOG_DEBUG("maps", "MMAP:unloadMap: Unloaded %04i.mmap", mapId); + + return true; + } + + bool MMapManager::unloadMapInstance(uint32 mapId, uint32 instanceId) + { + // check if we have this map loaded + MMapDataSet::const_iterator itr = GetMMapData(mapId); + if (itr == loadedMMaps.end()) + { + // file may not exist, therefore not loaded + TC_LOG_DEBUG("maps", "MMAP:unloadMapInstance: Asked to unload not loaded navmesh map %04u", mapId); + return false; + } + + MMapData* mmap = itr->second; + if (mmap->navMeshQueries.find(instanceId) == mmap->navMeshQueries.end()) + { + TC_LOG_DEBUG("maps", "MMAP:unloadMapInstance: Asked to unload not loaded dtNavMeshQuery mapId %04u instanceId %u", mapId, instanceId); + return false; + } + + dtNavMeshQuery* query = mmap->navMeshQueries[instanceId]; + + dtFreeNavMeshQuery(query); + mmap->navMeshQueries.erase(instanceId); + TC_LOG_DEBUG("maps", "MMAP:unloadMapInstance: Unloaded mapId %04u instanceId %u", mapId, instanceId); + + return true; + } + + dtNavMesh const* MMapManager::GetNavMesh(uint32 mapId, TerrainSet swaps) + { + MMapDataSet::const_iterator itr = GetMMapData(mapId); + if (itr == loadedMMaps.end()) + return NULL; + + return itr->second->GetNavMesh(swaps); + } + + dtNavMeshQuery const* MMapManager::GetNavMeshQuery(uint32 mapId, uint32 instanceId, TerrainSet swaps) + { + MMapDataSet::const_iterator itr = GetMMapData(mapId); + if (itr == loadedMMaps.end()) + return NULL; + + MMapData* mmap = itr->second; + if (mmap->navMeshQueries.find(instanceId) == mmap->navMeshQueries.end()) + { + // allocate mesh query + dtNavMeshQuery* query = dtAllocNavMeshQuery(); + ASSERT(query); + if (dtStatusFailed(query->init(mmap->GetNavMesh(swaps), 1024))) + { + dtFreeNavMeshQuery(query); + TC_LOG_ERROR("maps", "MMAP:GetNavMeshQuery: Failed to initialize dtNavMeshQuery for mapId %04u instanceId %u", mapId, instanceId); + return NULL; + } + + TC_LOG_DEBUG("maps", "MMAP:GetNavMeshQuery: created dtNavMeshQuery for mapId %04u instanceId %u", mapId, instanceId); + mmap->navMeshQueries.insert(std::pair<uint32, dtNavMeshQuery*>(instanceId, query)); + } + + return mmap->navMeshQueries[instanceId]; + } + + MMapData::MMapData(dtNavMesh* mesh, uint32 mapId) + { + navMesh = mesh; + _mapId = mapId; + } + + MMapData::~MMapData() + { + for (NavMeshQuerySet::iterator i = navMeshQueries.begin(); i != navMeshQueries.end(); ++i) + dtFreeNavMeshQuery(i->second); + + dtFreeNavMesh(navMesh); + + for (PhaseTileContainer::iterator i = _baseTiles.begin(); i != _baseTiles.end(); ++i) + { + delete (*i).second->data; + delete (*i).second; + } + } + + void MMapData::RemoveSwap(PhasedTile* ptile, uint32 swap, uint32 packedXY) + { + uint32 x = (packedXY >> 16); + uint32 y = (packedXY & 0x0000FFFF); + + if (loadedPhasedTiles[swap].find(packedXY) == loadedPhasedTiles[swap].end()) + { + TC_LOG_DEBUG("phase", "MMapData::RemoveSwap: mmtile %04u[%02i, %02i] unload skipped, due to not loaded", swap, x, y); + return; + } + dtMeshHeader* header = (dtMeshHeader*)ptile->data; + + // remove old tile + if (dtStatusFailed(navMesh->removeTile(loadedTileRefs[packedXY], NULL, NULL))) + TC_LOG_ERROR("phase", "MMapData::RemoveSwap: Could not unload phased %04u%02i%02i.mmtile from navmesh", swap, x, y); + else + { + TC_LOG_DEBUG("phase", "MMapData::RemoveSwap: Unloaded phased %04u%02i%02i.mmtile from navmesh", swap, x, y); + + // restore base tile + if (dtStatusSucceed(navMesh->addTile(_baseTiles[packedXY]->data, _baseTiles[packedXY]->dataSize, 0, 0, &loadedTileRefs[packedXY]))) + { + TC_LOG_DEBUG("phase", "MMapData::RemoveSwap: Loaded base mmtile %04u[%02i, %02i] into %04i[%02i, %02i]", _mapId, x, y, _mapId, header->x, header->y); + } + else + TC_LOG_ERROR("phase", "MMapData::RemoveSwap: Could not load base %04u%02i%02i.mmtile to navmesh", _mapId, x, y); + } + + loadedPhasedTiles[swap].erase(packedXY); + + if (loadedPhasedTiles[swap].empty()) + { + _activeSwaps.erase(swap); + TC_LOG_DEBUG("phase", "MMapData::RemoveSwap: Fully removed swap %u from map %u", swap, _mapId); + } + } + + void MMapData::AddSwap(PhasedTile* ptile, uint32 swap, uint32 packedXY) + { + + uint32 x = (packedXY >> 16); + uint32 y = (packedXY & 0x0000FFFF); + + if (loadedTileRefs.find(packedXY) == loadedTileRefs.end()) + { + TC_LOG_DEBUG("phase", "MMapData::AddSwap: phased mmtile %04u[%02i, %02i] load skipped, due to not loaded base tile on map %u", swap, x, y, _mapId); + return; + } + if (loadedPhasedTiles[swap].find(packedXY) != loadedPhasedTiles[swap].end()) + { + TC_LOG_DEBUG("phase", "MMapData::AddSwap: WARNING! phased mmtile %04u[%02i, %02i] load skipped, due to already loaded on map %u", swap, x, y, _mapId); + return; + } + + + dtMeshHeader* header = (dtMeshHeader*)ptile->data; + + const dtMeshTile* oldTile = navMesh->getTileByRef(loadedTileRefs[packedXY]); + + if (!oldTile) + { + TC_LOG_DEBUG("phase", "MMapData::AddSwap: phased mmtile %04u[%02i, %02i] load skipped, due to not loaded base tile ref on map %u", swap, x, y, _mapId); + return; + } + + // header xy is based on the swap map's tile set, wich doesn't have all the same tiles as root map, so copy the xy from the orignal header + header->x = oldTile->header->x; + header->y = oldTile->header->y; + + // the removed tile's data + PhasedTile* pt = new PhasedTile(); + // remove old tile + if (dtStatusFailed(navMesh->removeTile(loadedTileRefs[packedXY], &pt->data, &pt->dataSize))) + { + TC_LOG_ERROR("phase", "MMapData::AddSwap: Could not unload %04u%02i%02i.mmtile from navmesh", _mapId, x, y); + delete pt; + } + else + { + TC_LOG_DEBUG("phase", "MMapData::AddSwap: Unloaded %04u%02i%02i.mmtile from navmesh", _mapId, x, y); + + // store the removed data first time, this is the origonal, non-phased tile + if (_baseTiles.find(packedXY) == _baseTiles.end()) + _baseTiles[packedXY] = pt; + + _activeSwaps.insert(swap); + loadedPhasedTiles[swap].insert(packedXY); + + // add new swapped tile + if (dtStatusSucceed(navMesh->addTile(ptile->data, ptile->fileHeader.size, 0, 0, &loadedTileRefs[packedXY]))) + { + TC_LOG_DEBUG("phase", "MMapData::AddSwap: Loaded phased mmtile %04u[%02i, %02i] into %04i[%02i, %02i]", swap, x, y, _mapId, header->x, header->y); + } + else + TC_LOG_ERROR("phase", "MMapData::AddSwap: Could not load %04u%02i%02i.mmtile to navmesh", swap, x, y); + } + } + + dtNavMesh* MMapData::GetNavMesh(TerrainSet swaps) + { + std::set<uint32> activeSwaps = _activeSwaps; // _activeSwaps is modified inside RemoveSwap + for (uint32 swap : activeSwaps) + { + if (!swaps.count(swap)) // swap not active + { + if (PhaseTileContainer const* ptc = MMAP::MMapFactory::createOrGetMMapManager()->GetPhaseTileContainer(swap)) + for (PhaseTileContainer::const_iterator itr = ptc->begin(); itr != ptc->end(); ++itr) + RemoveSwap(itr->second, swap, itr->first); // remove swap + } + } + + // for each of the calling unit's terrain swaps + for (uint32 swap : swaps) + { + if (!_activeSwaps.count(swap)) // swap not active + { + // for each of the terrain swap's xy tiles + if (PhaseTileContainer const* ptc = MMAP::MMapFactory::createOrGetMMapManager()->GetPhaseTileContainer(swap)) + for (PhaseTileContainer::const_iterator itr = ptc->begin(); itr != ptc->end(); ++itr) + AddSwap(itr->second, swap, itr->first); // add swap + } + } + + return navMesh; + } +} diff --git a/src/common/Collision/Management/MMapManager.h b/src/common/Collision/Management/MMapManager.h new file mode 100644 index 00000000000..eaace584b0a --- /dev/null +++ b/src/common/Collision/Management/MMapManager.h @@ -0,0 +1,138 @@ +/* + * Copyright (C) 2008-2015 TrinityCore <http://www.trinitycore.org/> + * Copyright (C) 2005-2010 MaNGOS <http://getmangos.com/> + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; either version 2 of the License, or (at your + * option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#ifndef _MMAP_MANAGER_H +#define _MMAP_MANAGER_H + +#include "Define.h" +#include "DetourAlloc.h" +#include "DetourNavMesh.h" +#include "DetourNavMeshQuery.h" +#include "MapDefines.h" +#include <string> +#include <unordered_map> +#include <set> +#include <vector> + +// move map related classes +namespace MMAP +{ + typedef std::unordered_map<uint32, dtTileRef> MMapTileSet; + typedef std::unordered_map<uint32, dtNavMeshQuery*> NavMeshQuerySet; + + + typedef std::set<uint32> TerrainSet; + + struct NavMeshHolder + { + // Pre-built navMesh + dtNavMesh* navMesh; + + // List of terrain swap map ids used to build the navMesh + TerrainSet terrainIds; + + MMapTileSet loadedTileRefs; + }; + + struct PhasedTile + { + unsigned char* data; + MmapTileHeader fileHeader; + int32 dataSize; + }; + + typedef std::unordered_map<uint32, PhasedTile*> PhaseTileContainer; + typedef std::unordered_map<uint32, PhaseTileContainer> PhaseTileMap; + + + typedef std::unordered_map<uint32, TerrainSet> TerrainSetMap; + + class MMapData + { + public: + MMapData(dtNavMesh* mesh, uint32 mapId); + ~MMapData(); + + dtNavMesh* GetNavMesh(TerrainSet swaps); + + // we have to use single dtNavMeshQuery for every instance, since those are not thread safe + NavMeshQuerySet navMeshQueries; // instanceId to query + + dtNavMesh* navMesh; + MMapTileSet loadedTileRefs; + TerrainSetMap loadedPhasedTiles; + + private: + uint32 _mapId; + PhaseTileContainer _baseTiles; + std::set<uint32> _activeSwaps; + void RemoveSwap(PhasedTile* ptile, uint32 swap, uint32 packedXY); + void AddSwap(PhasedTile* tile, uint32 swap, uint32 packedXY); + }; + + + typedef std::unordered_map<uint32, MMapData*> MMapDataSet; + + // singleton class + // holds all all access to mmap loading unloading and meshes + class MMapManager + { + public: + MMapManager() : loadedTiles(0), thread_safe_environment(true) {} + ~MMapManager(); + + void InitializeThreadUnsafe(std::unordered_map<uint32, std::vector<uint32>> const& mapData); + bool loadMap(const std::string& basePath, uint32 mapId, int32 x, int32 y); + bool unloadMap(uint32 mapId, int32 x, int32 y); + bool unloadMap(uint32 mapId); + bool unloadMapInstance(uint32 mapId, uint32 instanceId); + + // the returned [dtNavMeshQuery const*] is NOT threadsafe + dtNavMeshQuery const* GetNavMeshQuery(uint32 mapId, uint32 instanceId, TerrainSet swaps); + dtNavMesh const* GetNavMesh(uint32 mapId, TerrainSet swaps); + + uint32 getLoadedTilesCount() const { return loadedTiles; } + uint32 getLoadedMapsCount() const { return loadedMMaps.size(); } + + typedef std::unordered_map<uint32, std::vector<uint32>> PhaseChildMapContainer; + void LoadPhaseTiles(PhaseChildMapContainer::const_iterator phasedMapData, int32 x, int32 y); + void UnloadPhaseTile(PhaseChildMapContainer::const_iterator phasedMapData, int32 x, int32 y); + PhaseTileContainer const* GetPhaseTileContainer(uint32 mapId) const + { + auto itr = _phaseTiles.find(mapId); + if (itr != _phaseTiles.end()) + return &itr->second; + return nullptr; + } + + private: + bool loadMapData(uint32 mapId); + uint32 packTileID(int32 x, int32 y); + + MMapDataSet::const_iterator GetMMapData(uint32 mapId) const; + MMapDataSet loadedMMaps; + PhaseChildMapContainer phaseMapData; + uint32 loadedTiles; + bool thread_safe_environment; + + PhasedTile* LoadTile(uint32 mapId, int32 x, int32 y); + PhaseTileMap _phaseTiles; + }; +} + +#endif
\ No newline at end of file diff --git a/src/common/Collision/Management/VMapFactory.cpp b/src/common/Collision/Management/VMapFactory.cpp new file mode 100644 index 00000000000..4c2750a9e5c --- /dev/null +++ b/src/common/Collision/Management/VMapFactory.cpp @@ -0,0 +1,42 @@ +/* + * Copyright (C) 2008-2015 TrinityCore <http://www.trinitycore.org/> + * Copyright (C) 2005-2010 MaNGOS <http://getmangos.com/> + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; either version 2 of the License, or (at your + * option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#include "VMapFactory.h" +#include "VMapManager2.h" + +namespace VMAP +{ + IVMapManager* gVMapManager = NULL; + + //=============================================== + // just return the instance + IVMapManager* VMapFactory::createOrGetVMapManager() + { + if (gVMapManager == nullptr) + gVMapManager= new VMapManager2(); // should be taken from config ... Please change if you like :-) + return gVMapManager; + } + + //=============================================== + // delete all internal data structures + void VMapFactory::clear() + { + delete gVMapManager; + gVMapManager = NULL; + } +} diff --git a/src/common/Collision/Management/VMapFactory.h b/src/common/Collision/Management/VMapFactory.h new file mode 100644 index 00000000000..3067c2919d5 --- /dev/null +++ b/src/common/Collision/Management/VMapFactory.h @@ -0,0 +1,40 @@ +/* + * Copyright (C) 2008-2015 TrinityCore <http://www.trinitycore.org/> + * Copyright (C) 2005-2010 MaNGOS <http://getmangos.com/> + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; either version 2 of the License, or (at your + * option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#ifndef _VMAPFACTORY_H +#define _VMAPFACTORY_H + +#include "IVMapManager.h" + +/** +This is the access point to the VMapManager. +*/ + +namespace VMAP +{ + //=========================================================== + + class VMapFactory + { + public: + static IVMapManager* createOrGetVMapManager(); + static void clear(); + }; + +} +#endif diff --git a/src/common/Collision/Management/VMapManager2.cpp b/src/common/Collision/Management/VMapManager2.cpp new file mode 100644 index 00000000000..9a31692593d --- /dev/null +++ b/src/common/Collision/Management/VMapManager2.cpp @@ -0,0 +1,329 @@ +/* + * Copyright (C) 2008-2015 TrinityCore <http://www.trinitycore.org/> + * Copyright (C) 2005-2010 MaNGOS <http://getmangos.com/> + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; either version 2 of the License, or (at your + * option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#include <iostream> +#include <iomanip> +#include <string> +#include <sstream> +#include "VMapManager2.h" +#include "MapTree.h" +#include "ModelInstance.h" +#include "WorldModel.h" +#include <G3D/Vector3.h> +#include "Log.h" +#include "VMapDefinitions.h" +#include "Errors.h" + +using G3D::Vector3; + +namespace VMAP +{ + VMapManager2::VMapManager2() + { + GetLiquidFlagsPtr = &GetLiquidFlagsDummy; + IsVMAPDisabledForPtr = &IsVMAPDisabledForDummy; + thread_safe_environment = true; + } + + VMapManager2::~VMapManager2(void) + { + for (InstanceTreeMap::iterator i = iInstanceMapTrees.begin(); i != iInstanceMapTrees.end(); ++i) + { + delete i->second; + } + for (ModelFileMap::iterator i = iLoadedModelFiles.begin(); i != iLoadedModelFiles.end(); ++i) + { + delete i->second.getModel(); + } + } + + 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 (auto const& p : mapData) + iInstanceMapTrees.insert(InstanceTreeMap::value_type(p.first, nullptr)); + + thread_safe_environment = false; + } + + Vector3 VMapManager2::convertPositionToInternalRep(float x, float y, float z) const + { + Vector3 pos; + const float mid = 0.5f * 64.0f * 533.33333333f; + pos.x = mid - x; + pos.y = mid - y; + pos.z = z; + + return pos; + } + + // move to MapTree too? + std::string VMapManager2::getMapFileName(unsigned int mapId) + { + std::stringstream fname; + fname.width(4); + fname << std::setfill('0') << mapId << std::string(MAP_FILENAME_EXTENSION2); + + return fname.str(); + } + + int VMapManager2::loadMap(const char* basePath, unsigned int mapId, int x, int y) + { + int result = VMAP_LOAD_RESULT_IGNORED; + if (isMapLoadingEnabled()) + { + if (_loadMap(mapId, basePath, x, y)) + result = VMAP_LOAD_RESULT_OK; + else + result = VMAP_LOAD_RESULT_ERROR; + } + + 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) + { + InstanceTreeMap::iterator 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); + } + + if (!instanceTree->second) + { + std::string mapFileName = getMapFileName(mapId); + StaticMapTree* newTree = new StaticMapTree(mapId, basePath); + if (!newTree->InitMap(mapFileName, this)) + { + delete newTree; + return false; + } + instanceTree->second = newTree; + } + + return instanceTree->second->LoadMapTile(tileX, tileY, this); + } + + void VMapManager2::unloadMap(unsigned int mapId) + { + InstanceTreeMap::iterator instanceTree = iInstanceMapTrees.find(mapId); + if (instanceTree != iInstanceMapTrees.end() && instanceTree->second) + { + instanceTree->second->UnloadMap(this); + if (instanceTree->second->numLoadedTiles() == 0) + { + delete instanceTree->second; + instanceTree->second = nullptr; + } + } + } + + void VMapManager2::unloadMap(unsigned int mapId, int x, int y) + { + InstanceTreeMap::iterator instanceTree = iInstanceMapTrees.find(mapId); + if (instanceTree != iInstanceMapTrees.end() && instanceTree->second) + { + instanceTree->second->UnloadMapTile(x, y, this); + if (instanceTree->second->numLoadedTiles() == 0) + { + delete instanceTree->second; + instanceTree->second = nullptr; + } + } + } + + bool VMapManager2::isInLineOfSight(unsigned int mapId, float x1, float y1, float z1, float x2, float y2, float z2) + { + if (!isLineOfSightCalcEnabled() || IsVMAPDisabledForPtr(mapId, VMAP_DISABLE_LOS)) + return true; + + InstanceTreeMap::const_iterator 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; + } + + /** + get the hit position and return true if we hit something + otherwise the result pos will be the dest pos + */ + bool VMapManager2::getObjectHitPos(unsigned int mapId, float x1, float y1, float z1, float x2, float y2, float z2, float& rx, float &ry, float& rz, float modifyDist) + { + if (isLineOfSightCalcEnabled() && !IsVMAPDisabledForPtr(mapId, VMAP_DISABLE_LOS)) + { + InstanceTreeMap::const_iterator instanceTree = GetMapTree(mapId); + if (instanceTree != iInstanceMapTrees.end()) + { + Vector3 pos1 = convertPositionToInternalRep(x1, y1, z1); + Vector3 pos2 = convertPositionToInternalRep(x2, y2, z2); + Vector3 resultPos; + bool result = instanceTree->second->getObjectHitPos(pos1, pos2, resultPos, modifyDist); + resultPos = convertPositionToInternalRep(resultPos.x, resultPos.y, resultPos.z); + rx = resultPos.x; + ry = resultPos.y; + rz = resultPos.z; + return result; + } + } + + rx = x2; + ry = y2; + rz = z2; + + return false; + } + + /** + get height or INVALID_HEIGHT if no height available + */ + + float VMapManager2::getHeight(unsigned int mapId, float x, float y, float z, float maxSearchDist) + { + if (isHeightCalcEnabled() && !IsVMAPDisabledForPtr(mapId, VMAP_DISABLE_HEIGHT)) + { + InstanceTreeMap::const_iterator instanceTree = GetMapTree(mapId); + if (instanceTree != iInstanceMapTrees.end()) + { + Vector3 pos = convertPositionToInternalRep(x, y, z); + float height = instanceTree->second->getHeight(pos, maxSearchDist); + if (!(height < G3D::finf())) + return height = VMAP_INVALID_HEIGHT_VALUE; // No height + + return height; + } + } + + return VMAP_INVALID_HEIGHT_VALUE; + } + + bool VMapManager2::getAreaInfo(unsigned int mapId, float x, float y, float& z, uint32& flags, int32& adtId, int32& rootId, int32& groupId) const + { + if (!IsVMAPDisabledForPtr(mapId, VMAP_DISABLE_AREAFLAG)) + { + InstanceTreeMap::const_iterator instanceTree = GetMapTree(mapId); + if (instanceTree != iInstanceMapTrees.end()) + { + Vector3 pos = convertPositionToInternalRep(x, y, z); + bool result = instanceTree->second->getAreaInfo(pos, flags, adtId, rootId, groupId); + // z is not touched by convertPositionToInternalRep(), so just copy + z = pos.z; + return result; + } + } + + return false; + } + + bool VMapManager2::GetLiquidLevel(uint32 mapId, float x, float y, float z, uint8 reqLiquidType, float& level, float& floor, uint32& type) const + { + if (!IsVMAPDisabledForPtr(mapId, VMAP_DISABLE_LIQUIDSTATUS)) + { + InstanceTreeMap::const_iterator instanceTree = GetMapTree(mapId); + if (instanceTree != iInstanceMapTrees.end()) + { + LocationInfo info; + Vector3 pos = convertPositionToInternalRep(x, y, z); + if (instanceTree->second->GetLocationInfo(pos, info)) + { + floor = info.ground_Z; + ASSERT(floor < std::numeric_limits<float>::max()); + ASSERT(info.hitModel); + type = info.hitModel->GetLiquidType(); // entry from LiquidType.dbc + if (reqLiquidType && !(GetLiquidFlagsPtr(type) & reqLiquidType)) + return false; + ASSERT(info.hitInstance); + if (info.hitInstance->GetLiquidLevel(pos, info, level)) + return true; + } + } + } + + return false; + } + + 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); + if (model == iLoadedModelFiles.end()) + { + WorldModel* worldmodel = new WorldModel(); + if (!worldmodel->readFile(basepath + filename + ".vmo")) + { + VMAP_ERROR_LOG("misc", "VMapManager2: could not load '%s%s.vmo'", basepath.c_str(), filename.c_str()); + delete worldmodel; + return NULL; + } + VMAP_DEBUG_LOG("maps", "VMapManager2: loading file '%s%s'", basepath.c_str(), filename.c_str()); + model = iLoadedModelFiles.insert(std::pair<std::string, ManagedModel>(filename, ManagedModel())).first; + model->second.setModel(worldmodel); + } + model->second.incRefCount(); + return model->second.getModel(); + } + + void VMapManager2::releaseModelInstance(const std::string &filename) + { + //! Critical section, thread safe access to iLoadedModelFiles + std::lock_guard<std::mutex> lock(LoadedModelFilesLock); + + ModelFileMap::iterator model = iLoadedModelFiles.find(filename); + if (model == iLoadedModelFiles.end()) + { + VMAP_ERROR_LOG("misc", "VMapManager2: trying to unload non-loaded file '%s'", filename.c_str()); + return; + } + if (model->second.decRefCount() == 0) + { + VMAP_DEBUG_LOG("maps", "VMapManager2: unloading file '%s'", filename.c_str()); + delete model->second.getModel(); + iLoadedModelFiles.erase(model); + } + } + + bool VMapManager2::existsMap(const char* basePath, unsigned int mapId, int x, int y) + { + return StaticMapTree::CanLoadMap(std::string(basePath), mapId, x, y); + } + +} // namespace VMAP diff --git a/src/common/Collision/Management/VMapManager2.h b/src/common/Collision/Management/VMapManager2.h new file mode 100644 index 00000000000..553145cda4b --- /dev/null +++ b/src/common/Collision/Management/VMapManager2.h @@ -0,0 +1,142 @@ +/* + * Copyright (C) 2008-2015 TrinityCore <http://www.trinitycore.org/> + * Copyright (C) 2005-2010 MaNGOS <http://getmangos.com/> + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; either version 2 of the License, or (at your + * option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#ifndef _VMAPMANAGER2_H +#define _VMAPMANAGER2_H + +#include <mutex> +#include <unordered_map> +#include <vector> +#include "Define.h" +#include "IVMapManager.h" + +//=========================================================== + +#define MAP_FILENAME_EXTENSION2 ".vmtree" + +#define FILENAMEBUFFER_SIZE 500 + +/** +This is the main Class to manage loading and unloading of maps, line of sight, height calculation and so on. +For each map or map tile to load it reads a directory file that contains the ModelContainer files used by this map or map tile. +Each global map or instance has its own dynamic BSP-Tree. +The loaded ModelContainers are included in one of these BSP-Trees. +Additionally a table to match map ids and map names is used. +*/ + +//=========================================================== + +namespace G3D +{ + class Vector3; +} + +namespace VMAP +{ + class StaticMapTree; + class WorldModel; + + class ManagedModel + { + public: + ManagedModel() : iModel(nullptr), iRefCount(0) { } + void setModel(WorldModel* model) { iModel = model; } + WorldModel* getModel() { return iModel; } + void incRefCount() { ++iRefCount; } + int decRefCount() { return --iRefCount; } + protected: + WorldModel* iModel; + int iRefCount; + }; + + typedef std::unordered_map<uint32, StaticMapTree*> InstanceTreeMap; + typedef std::unordered_map<std::string, ManagedModel> ModelFileMap; + + enum DisableTypes + { + VMAP_DISABLE_AREAFLAG = 0x1, + VMAP_DISABLE_HEIGHT = 0x2, + VMAP_DISABLE_LOS = 0x4, + VMAP_DISABLE_LIQUIDSTATUS = 0x8 + }; + + class VMapManager2 : public IVMapManager + { + protected: + // Tree to check collision + ModelFileMap iLoadedModelFiles; + InstanceTreeMap iInstanceMapTrees; + 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; } + + InstanceTreeMap::const_iterator GetMapTree(uint32 mapId) const; + + public: + // public for debug + G3D::Vector3 convertPositionToInternalRep(float x, float y, float z) const; + static std::string getMapFileName(unsigned int mapId); + + VMapManager2(); + ~VMapManager2(void); + + void InitializeThreadUnsafe(std::unordered_map<uint32, std::vector<uint32>> const& mapData); + int loadMap(const char* pBasePath, unsigned int mapId, int x, int y) override; + + void unloadMap(unsigned int mapId, int x, int y) override; + void unloadMap(unsigned int mapId) override; + + bool isInLineOfSight(unsigned int mapId, float x1, float y1, float z1, float x2, float y2, float z2) override ; + /** + fill the hit pos and return true, if an object was hit + */ + bool getObjectHitPos(unsigned int mapId, float x1, float y1, float z1, float x2, float y2, float z2, float& rx, float& ry, float& rz, float modifyDist) override; + float getHeight(unsigned int mapId, float x, float y, float z, float maxSearchDist) override; + + bool processCommand(char* /*command*/) override { return false; } // for debug and extensions + + 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; + + WorldModel* acquireModelInstance(const std::string& basepath, const std::string& filename); + void releaseModelInstance(const std::string& filename); + + // what's the use of this? o.O + virtual std::string getDirFileName(unsigned int mapId, int /*x*/, int /*y*/) const override + { + return getMapFileName(mapId); + } + virtual bool existsMap(const char* basePath, unsigned int mapId, int x, int y) override; + public: + void getInstanceMapTree(InstanceTreeMap &instanceMapTree); + + typedef uint32(*GetLiquidFlagsFn)(uint32 liquidType); + GetLiquidFlagsFn GetLiquidFlagsPtr; + + typedef bool(*IsVMAPDisabledForFn)(uint32 entry, uint8 flags); + IsVMAPDisabledForFn IsVMAPDisabledForPtr; + }; +} + +#endif |
