Skip to content

Commit

Permalink
MMAP: Add partial pre-loading on map init
Browse files Browse the repository at this point in the history
From TrinityCore/TrinityCore@16f555f

Still not fully safe
  • Loading branch information
killerwife committed Sep 2, 2024
1 parent fd1b862 commit e253788
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 21 deletions.
2 changes: 2 additions & 0 deletions src/game/Maps/Map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,8 @@ void Map::Initialize(bool loadInstanceData /*= true*/)

m_spawnManager.Initialize();

MMAP::MMapFactory::createOrGetMMapManager()->loadMapInstance(sWorld.GetDataPath(), GetId(), GetInstanceId());

sObjectMgr.LoadActiveEntities(this);

LoadTransports();
Expand Down
55 changes: 34 additions & 21 deletions src/game/MotionGenerators/MoveMap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,32 @@ namespace MMAP
return true;
}

bool MMapManager::loadMapInstance(std::string const& basePath, uint32 mapId, uint32 instanceId)
{
if (!loadMapData(basePath, mapId))
return false;

auto& mmap = loadedMMaps[mapId];
auto [queryItr, inserted] = mmap->navMeshQueries.try_emplace(instanceId, nullptr);
if (!inserted)
return true;

// allocate mesh query
dtNavMeshQuery* query = dtAllocNavMeshQuery();
MANGOS_ASSERT(query);
if (dtStatusFailed(query->init(mmap->navMesh, 1024)))
{
dtFreeNavMeshQuery(query);
mmap->navMeshQueries.erase(queryItr);
ERROR_DB_FILTER_LOG(LOG_FILTER_MAP_LOADING, "MMAP:GetNavMeshQuery: Failed to initialize dtNavMeshQuery for mapId %03u instanceId %u", mapId, instanceId);
return false;
}

DEBUG_FILTER_LOG(LOG_FILTER_MAP_LOADING, "MMAP:GetNavMeshQuery: created dtNavMeshQuery for mapId %03u instanceId %u", mapId, instanceId);
queryItr->second = query;
return true;
}

bool MMapManager::unloadMap(uint32 mapId, int32 x, int32 y)
{
// check if we have this map loaded
Expand Down Expand Up @@ -438,28 +464,15 @@ namespace MMAP

dtNavMeshQuery const* MMapManager::GetNavMeshQuery(uint32 mapId, uint32 instanceId)
{
if (loadedMMaps.find(mapId) == loadedMMaps.end())
auto itr = loadedMMaps.find(mapId);
if (itr == loadedMMaps.end())
return nullptr;

const auto& mmapData = loadedMMaps[mapId];
if (mmapData->navMeshQueries.find(instanceId) == mmapData->navMeshQueries.end())
{
// allocate mesh query
dtNavMeshQuery* query = dtAllocNavMeshQuery();
MANGOS_ASSERT(query);
dtStatus dtResult = query->init(mmapData->navMesh, 1024);
if (dtStatusFailed(dtResult))
{
dtFreeNavMeshQuery(query);
sLog.outError("MMAP:GetNavMeshQuery: Failed to initialize dtNavMeshQuery for mapId %03u instanceId %u", mapId, instanceId);
return nullptr;
}

DEBUG_FILTER_LOG(LOG_FILTER_MAP_LOADING, "MMAP:GetNavMeshQuery: created dtNavMeshQuery for mapId %03u instanceId %u", mapId, instanceId);
mmapData->navMeshQueries.insert(std::pair<uint32, dtNavMeshQuery*>(instanceId, query));
}
auto queryItr = itr->second->navMeshQueries.find(instanceId);
if (queryItr == itr->second->navMeshQueries.end())
return nullptr;

return mmapData->navMeshQueries[instanceId];
return queryItr->second;
}

dtNavMeshQuery const* MMapManager::GetModelNavMeshQuery(uint32 displayId)
Expand All @@ -482,11 +495,11 @@ namespace MMAP
if (dtStatusFailed(query->init(mmapGOData->navMesh, 2048)))
{
dtFreeNavMeshQuery(query);
sLog.outError("MMAP:GetNavMeshQuery: Failed to initialize dtNavMeshQuery for displayid %03u tid %s", displayId, ss.str().data());
sLog.outError("MMAP:GetModelNavMeshQuery: Failed to initialize dtNavMeshQuery for displayid %03u tid %s", displayId, ss.str().data());
return nullptr;
}

DETAIL_LOG("MMAP:GetNavMeshQuery: created dtNavMeshQuery for displayid %03u tid %s", displayId, ss.str().data());
DETAIL_LOG("MMAP:GetModelNavMeshQuery: created dtNavMeshQuery for displayid %03u tid %s", displayId, ss.str().data());
mmapGOData->navMeshGOQueries.insert(std::pair<std::thread::id, dtNavMeshQuery*>(threadId, query));
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/game/MotionGenerators/MoveMap.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ namespace MMAP
// we have to use single dtNavMeshQuery for every instance, since those are not thread safe
NavMeshQuerySet navMeshQueries; // instanceId to query
MMapTileSet mmapLoadedTiles; // maps [map grid coords] to [dtTile]

std::mutex mutex;
};

struct MMapGOData
Expand Down Expand Up @@ -97,6 +99,7 @@ namespace MMAP
bool loadMap(std::string const& basePath, uint32 mapId, int32 x, int32 y);
void loadAllGameObjectModels(std::string const& basePath, std::vector<uint32> const& displayIds);
bool loadGameObject(std::string const& basePath, uint32 displayId);
bool loadMapInstance(std::string const& basePath, uint32 mapId, uint32 instanceId);
bool unloadMap(uint32 mapId, int32 x, int32 y);
bool unloadMap(uint32 mapId);
bool unloadMapInstance(uint32 mapId, uint32 instanceId);
Expand Down

0 comments on commit e253788

Please sign in to comment.