From 8f3a80c1cf39978056979a0bcbcb7321e510c101 Mon Sep 17 00:00:00 2001 From: Rat Date: Mon, 16 Mar 2015 07:28:49 +0100 Subject: Core/Phases: Ported new phasing system from 4.3.4 branch and fixed some map swap logic --- src/server/scripts/Commands/cs_debug.cpp | 42 ++++++++++++++++++++-------- src/server/scripts/Commands/cs_gobject.cpp | 3 +- src/server/scripts/Commands/cs_misc.cpp | 21 +++++++++++--- src/server/scripts/Commands/cs_modify.cpp | 11 +++++--- src/server/scripts/Commands/cs_npc.cpp | 45 ++++++++++++++++++++++++------ src/server/scripts/Commands/cs_reload.cpp | 6 ++-- src/server/scripts/Commands/cs_wp.cpp | 12 +++----- 7 files changed, 100 insertions(+), 40 deletions(-) (limited to 'src/server/scripts/Commands') diff --git a/src/server/scripts/Commands/cs_debug.cpp b/src/server/scripts/Commands/cs_debug.cpp index 5b60fc81229..e08557dd357 100644 --- a/src/server/scripts/Commands/cs_debug.cpp +++ b/src/server/scripts/Commands/cs_debug.cpp @@ -934,8 +934,7 @@ public: return false; } - for (auto phase : handler->GetSession()->GetPlayer()->GetPhases()) - v->SetInPhase(phase, false, true); + v->CopyPhaseFrom(handler->GetSession()->GetPlayer()); map->AddToMap(v->ToCreature()); @@ -959,6 +958,7 @@ public: char* t = strtok((char*)args, " "); char* p = strtok(NULL, " "); + char* m = strtok(NULL, " "); if (!t) return false; @@ -967,10 +967,16 @@ public: std::set phaseId; std::set worldMapSwap; - terrainswap.insert((uint32)atoi(t)); + if (uint32 ut = (uint32)atoi(t)) + terrainswap.insert(ut); if (p) - phaseId.insert((uint32)atoi(p)); + if (uint32 up = (uint32)atoi(p)) + phaseId.insert(up); + + if (m) + if (uint32 um = (uint32)atoi(m)) + worldMapSwap.insert(um); handler->GetSession()->SendSetPhaseShift(phaseId, terrainswap, worldMapSwap); return true; @@ -1390,14 +1396,28 @@ public: return true; } - static bool HandleDebugPhaseCommand(ChatHandler* /*handler*/, char const* /*args*/) + static bool HandleDebugPhaseCommand(ChatHandler* handler, char const* /*args*/) { - /*/ - Unit* unit = handler->getSelectedUnit(); - Player* player = handler->GetSession()->GetPlayer(); - if (unit && unit->GetTypeId() == TYPEID_PLAYER) - player = unit->ToPlayer(); - */ + Unit* target = handler->getSelectedUnit(); + + if (!target) + { + handler->SendSysMessage(LANG_SELECT_CREATURE); + handler->SetSentErrorMessage(true); + return false; + } + + std::stringstream phases; + + for (uint32 phase : target->GetPhases()) + { + phases << phase << " "; + } + + if (!phases.str().empty()) + handler->PSendSysMessage("Target's current phases: %s", phases.str().c_str()); + else + handler->SendSysMessage("Target is not phased"); return true; } }; diff --git a/src/server/scripts/Commands/cs_gobject.cpp b/src/server/scripts/Commands/cs_gobject.cpp index 59d32f9e418..36876a9be34 100644 --- a/src/server/scripts/Commands/cs_gobject.cpp +++ b/src/server/scripts/Commands/cs_gobject.cpp @@ -158,8 +158,7 @@ public: return false; } - for (auto phase : player->GetPhases()) - object->SetInPhase(phase, false, true); + object->CopyPhaseFrom(player); if (spawntimeSecs) { diff --git a/src/server/scripts/Commands/cs_misc.cpp b/src/server/scripts/Commands/cs_misc.cpp index bc8f7bad5fb..466c24eba5d 100644 --- a/src/server/scripts/Commands/cs_misc.cpp +++ b/src/server/scripts/Commands/cs_misc.cpp @@ -247,6 +247,21 @@ public: if (status) handler->PSendSysMessage(LANG_LIQUID_STATUS, liquidStatus.level, liquidStatus.depth_level, liquidStatus.entry, liquidStatus.type_flags, status); + + if (!object->GetTerrainSwaps().empty()) + { + std::stringstream ss; + for (uint32 swap : object->GetTerrainSwaps()) + ss << swap << " "; + handler->PSendSysMessage("Target's active terrain swaps: %s", ss.str().c_str()); + } + if (!object->GetWorldMapAreaSwaps().empty()) + { + std::stringstream ss; + for (uint32 swap : object->GetWorldMapAreaSwaps()) + ss << swap << " "; + handler->PSendSysMessage("Target's active world map area swaps: %s", ss.str().c_str()); + } return true; } @@ -407,8 +422,7 @@ public: target->GetContactPoint(_player, x, y, z); _player->TeleportTo(target->GetMapId(), x, y, z, _player->GetAngle(target), TELE_TO_GM_MODE); - for (auto phase : target->GetPhases()) - _player->SetInPhase(phase, true, true); + _player->CopyPhaseFrom(target, true); } else { @@ -532,8 +546,7 @@ public: float x, y, z; handler->GetSession()->GetPlayer()->GetClosePoint(x, y, z, target->GetObjectSize()); target->TeleportTo(handler->GetSession()->GetPlayer()->GetMapId(), x, y, z, target->GetOrientation()); - for (auto phase : handler->GetSession()->GetPlayer()->GetPhases()) - target->SetInPhase(phase, true, true); + target->CopyPhaseFrom(handler->GetSession()->GetPlayer(), true); } else { diff --git a/src/server/scripts/Commands/cs_modify.cpp b/src/server/scripts/Commands/cs_modify.cpp index c22edf52dd3..5af9b150572 100644 --- a/src/server/scripts/Commands/cs_modify.cpp +++ b/src/server/scripts/Commands/cs_modify.cpp @@ -1279,10 +1279,13 @@ public: uint32 phase = (uint32)atoi((char*)args); Unit* target = handler->getSelectedUnit(); - if (target) - target->SetInPhase(phase, true, !target->IsInPhase(phase)); - else - handler->GetSession()->GetPlayer()->SetInPhase(phase, true, !handler->GetSession()->GetPlayer()->IsInPhase(phase)); + if (!target) + target = handler->GetSession()->GetPlayer(); + + target->SetInPhase(phase, true, !target->IsInPhase(phase)); + + if (target->GetTypeId() == TYPEID_PLAYER) + target->ToPlayer()->SendUpdatePhasing(); return true; } diff --git a/src/server/scripts/Commands/cs_npc.cpp b/src/server/scripts/Commands/cs_npc.cpp index 70086c94988..9ee32d0c6e5 100644 --- a/src/server/scripts/Commands/cs_npc.cpp +++ b/src/server/scripts/Commands/cs_npc.cpp @@ -206,6 +206,7 @@ public: { "model", rbac::RBAC_PERM_COMMAND_NPC_SET_MODEL, false, &HandleNpcSetModelCommand, "", NULL }, { "movetype", rbac::RBAC_PERM_COMMAND_NPC_SET_MOVETYPE, false, &HandleNpcSetMoveTypeCommand, "", NULL }, { "phase", rbac::RBAC_PERM_COMMAND_NPC_SET_PHASE, false, &HandleNpcSetPhaseCommand, "", NULL }, + { "phasegroup", rbac::RBAC_PERM_COMMAND_NPC_SET_PHASE, false, &HandleNpcSetPhaseGroup, "", NULL }, { "spawndist", rbac::RBAC_PERM_COMMAND_NPC_SET_SPAWNDIST, false, &HandleNpcSetSpawnDistCommand, "", NULL }, { "spawntime", rbac::RBAC_PERM_COMMAND_NPC_SET_SPAWNTIME, false, &HandleNpcSetSpawnTimeCommand, "", NULL }, { "data", rbac::RBAC_PERM_COMMAND_NPC_SET_DATA, false, &HandleNpcSetDataCommand, "", NULL }, @@ -285,9 +286,6 @@ public: return false; } - for (auto phase : chr->GetPhases()) - creature->SetInPhase(phase, false, true); - creature->SaveToDB(map->GetId(), (1 << map->GetSpawnMode()), chr->GetPhaseMask()); ObjectGuid::LowType db_guid = creature->GetDBTableGUIDLow(); @@ -1100,7 +1098,37 @@ public: } //npc phase handling - //change phase of creature or pet + //change phase of creature + static bool HandleNpcSetPhaseGroup(ChatHandler* handler, char const* args) + { + if (!*args) + return false; + + uint32 phaseGroupId = (uint32)atoi((char*)args); + + Creature* creature = handler->getSelectedCreature(); + if (!creature || creature->IsPet()) + { + handler->SendSysMessage(LANG_SELECT_CREATURE); + handler->SetSentErrorMessage(true); + return false; + } + + creature->ClearPhases(); + + for (uint32 id : sDB2Manager.GetPhasesForGroup(phaseGroupId)) + creature->SetInPhase(id, false, true); // don't send update here for multiple phases, only send it once after adding all phases + + creature->UpdateObjectVisibility(); + creature->SetDBPhase(-int(phaseGroupId)); + + creature->SaveToDB(); + + return true; + } + + //npc phase handling + //change phase of creature static bool HandleNpcSetPhaseCommand(ChatHandler* handler, char const* args) { if (!*args) @@ -1109,17 +1137,18 @@ public: uint32 phase = (uint32) atoi((char*)args); Creature* creature = handler->getSelectedCreature(); - if (!creature) + if (!creature || creature->IsPet()) { handler->SendSysMessage(LANG_SELECT_CREATURE); handler->SetSentErrorMessage(true); return false; } - creature->SetInPhase(phase, true, !creature->IsInPhase(phase)); + creature->ClearPhases(); + creature->SetInPhase(phase, true, true); + creature->SetDBPhase(phase); - if (!creature->IsPet()) - creature->SaveToDB(); + creature->SaveToDB(); return true; } diff --git a/src/server/scripts/Commands/cs_reload.cpp b/src/server/scripts/Commands/cs_reload.cpp index 99cec7bed95..78f23ceab25 100644 --- a/src/server/scripts/Commands/cs_reload.cpp +++ b/src/server/scripts/Commands/cs_reload.cpp @@ -1104,10 +1104,10 @@ public: static bool HandleReloadPhaseDefinitionsCommand(ChatHandler* handler, const char* /*args*/) { - TC_LOG_INFO("misc", "Reloading phase_definitions table..."); - sObjectMgr->LoadPhaseDefinitions(); + TC_LOG_INFO("misc", "Reloading terrain_phase_info table..."); + sObjectMgr->LoadTerrainPhaseInfo(); sWorld->UpdatePhaseDefinitions(); - handler->SendGlobalGMSysMessage("Phase Definitions reloaded."); + handler->SendGlobalGMSysMessage("Terrain phase infos reloaded."); return true; } diff --git a/src/server/scripts/Commands/cs_wp.cpp b/src/server/scripts/Commands/cs_wp.cpp index 35b142e9e5e..a4e0ce3af61 100644 --- a/src/server/scripts/Commands/cs_wp.cpp +++ b/src/server/scripts/Commands/cs_wp.cpp @@ -682,8 +682,7 @@ public: return false; } - for (auto phase : chr->GetPhases()) - wpCreature2->SetInPhase(phase, false, true); + wpCreature2->CopyPhaseFrom(chr); wpCreature2->SaveToDB(map->GetId(), (1 << map->GetSpawnMode()), chr->GetPhaseMask()); // To call _LoadGoods(); _LoadQuests(); CreateTrainerSpells(); @@ -902,8 +901,7 @@ public: return false; } - for (auto phase : chr->GetPhases()) - wpCreature->SetInPhase(phase, false, true); + wpCreature->CopyPhaseFrom(chr); // Set "wpguid" column to the visual waypoint stmt = WorldDatabase.GetPreparedStatement(WORLD_UPD_WAYPOINT_DATA_WPGUID); @@ -967,8 +965,7 @@ public: return false; } - for (auto phase : chr->GetPhases()) - creature->SetInPhase(phase, false, true); + creature->CopyPhaseFrom(chr); creature->SaveToDB(map->GetId(), (1 << map->GetSpawnMode()), chr->GetPhaseMask()); if (!creature->LoadCreatureFromDB(creature->GetDBTableGUIDLow(), map)) @@ -1019,8 +1016,7 @@ public: return false; } - for (auto phase : chr->GetPhases()) - creature->SetInPhase(phase, false, true); + creature->CopyPhaseFrom(chr); creature->SaveToDB(map->GetId(), (1 << map->GetSpawnMode()), chr->GetPhaseMask()); if (!creature->LoadCreatureFromDB(creature->GetDBTableGUIDLow(), map)) -- cgit v1.2.3 From a68f19af5638f67778ab57b9ee9883c370362f2b Mon Sep 17 00:00:00 2001 From: Rat Date: Mon, 16 Mar 2015 16:54:47 +0100 Subject: Core/Phases: Mmaps now use the swapped terrain's mmtiles for all units if available. --- src/server/collision/Management/MMapManager.cpp | 280 ++++++++++++++++++++++-- src/server/collision/Management/MMapManager.h | 70 ++++-- src/server/game/Movement/PathGenerator.cpp | 4 +- src/server/scripts/Commands/cs_misc.cpp | 2 +- src/server/scripts/Commands/cs_mmaps.cpp | 12 +- 5 files changed, 329 insertions(+), 39 deletions(-) (limited to 'src/server/scripts/Commands') diff --git a/src/server/collision/Management/MMapManager.cpp b/src/server/collision/Management/MMapManager.cpp index 8c549017f12..6dcff9845f0 100644 --- a/src/server/collision/Management/MMapManager.cpp +++ b/src/server/collision/Management/MMapManager.cpp @@ -19,6 +19,8 @@ #include "MMapManager.h" #include "Log.h" #include "World.h" +#include "DBCStores.h" +#include "MMapFactory.h" namespace MMAP { @@ -76,8 +78,7 @@ namespace MMAP TC_LOG_INFO("maps", "MMAP:loadMapData: Loaded %03i.mmap", mapId); // store inside our map list - MMapData* mmap_data = new MMapData(mesh); - mmap_data->mmapLoadedTiles.clear(); + MMapData* mmap_data = new MMapData(mesh, mapId); loadedMMaps.insert(std::pair(mapId, mmap_data)); return true; @@ -100,7 +101,7 @@ namespace MMAP // check if we already have this tile loaded uint32 packedGridPos = packTileID(x, y); - if (mmap->mmapLoadedTiles.find(packedGridPos) != mmap->mmapLoadedTiles.end()) + if (mmap->loadedTileRefs.find(packedGridPos) != mmap->loadedTileRefs.end()) return false; // load this tile :: mmaps/MMMXXYY.mmtile @@ -152,19 +153,120 @@ namespace MMAP 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, DT_TILE_FREE_DATA, 0, &tileRef))) + if (dtStatusSucceed(mmap->navMesh->addTile(data, fileHeader.size, NULL/*DT_TILE_FREE_DATA*/, 0, &tileRef))) { - mmap->mmapLoadedTiles.insert(std::pair(packedGridPos, tileRef)); + mmap->loadedTileRefs.insert(std::pair(packedGridPos, tileRef)); ++loadedTiles; TC_LOG_INFO("maps", "MMAP:loadMap: Loaded mmtile %03i[%02i, %02i] into %03i[%02i, %02i]", mapId, x, y, mapId, header->x, header->y); + + LoadPhaseTiles(mapId, x, y); + return true; } + else + { + TC_LOG_ERROR("maps", "MMAP:loadMap: Could not load %03u%02i%02i.mmtile into navmesh", mapId, x, y); + dtFree(data); + return false; + } - TC_LOG_ERROR("maps", "MMAP:loadMap: Could not load %03u%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 + uint32 pathLen = sWorld->GetDataPath().length() + strlen("mmaps/%03i%02i%02i.mmtile") + 1; + char *fileName = new char[pathLen]; + + snprintf(fileName, pathLen, (sWorld->GetDataPath() + "mmaps/%03i%02i%02i.mmtile").c_str(), mapId, x, y); + + FILE* file = fopen(fileName, "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); + delete[] fileName; + return NULL; + } + delete[] fileName; + + 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 %03u%02i%02i.mmtile", mapId, x, y); + fclose(file); + return NULL; + } + + if (pTile->fileHeader.mmapVersion != MMAP_VERSION) + { + TC_LOG_ERROR("phase", "MMAP:LoadTile: %03u%02i%02i.mmtile was built with generator v%i, expected v%i", + mapId, x, y, pTile->fileHeader.mmapVersion, MMAP_VERSION); + fclose(file); + return NULL; + } + + 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 %03u%02i%02i.mmtile", mapId, x, y); + fclose(file); + return NULL; + } + + fclose(file); + + return pTile; + } + + void MMapManager::LoadPhaseTiles(uint32 mapId, int32 x, int32 y) + { + TC_LOG_DEBUG("phase", "MMAP:LoadPhaseTiles: Loading phased mmtiles for map %u, x: %i, y: %i", mapId, x, y); + + uint32 packedGridPos = packTileID(x, y); + + for (uint32 i = 0; i < sMapStore.GetNumRows(); ++i) + { + if (const MapEntry* const map = sMapStore.LookupEntry(i)) + { + if (map->ParentMapID == mapId) + { + PhasedTile* data = LoadTile(map->ID, x, y); + // only a few tiles have terrain swaps, do not write error for them + if (data) + { + TC_LOG_DEBUG("phase", "MMAP:LoadPhaseTiles: Loaded phased %03u%02i%02i.mmtile for root phase map %u", map->ID, x, y, mapId); + _phaseTiles[map->ID][packedGridPos] = data; + } + } + } + } + } + + void MMapManager::UnloadPhaseTile(uint32 mapId, int32 x, int32 y) + { + TC_LOG_DEBUG("phase", "MMAP:UnloadPhaseTile: Unloading phased mmtile for map %u, x: %i, y: %i", mapId, x, y); + + uint32 packedGridPos = packTileID(x, y); + + const MapEntry* const map = sMapStore.LookupEntry(mapId); // map existence already checked when loading + uint32 rootMapId = map->ParentMapID; + + if (_phaseTiles[mapId][packedGridPos]) + { + TC_LOG_DEBUG("phase", "MMAP:UnloadPhaseTile: Unloaded phased %03u%02i%02i.mmtile for root phase map %u", mapId, x, y, rootMapId); + delete _phaseTiles[mapId][packedGridPos]->data; + delete _phaseTiles[mapId][packedGridPos]; + _phaseTiles[mapId].erase(packedGridPos); + } + } + bool MMapManager::unloadMap(uint32 mapId, int32 x, int32 y) { // check if we have this map loaded @@ -179,14 +281,14 @@ namespace MMAP // check if we have this tile loaded uint32 packedGridPos = packTileID(x, y); - if (mmap->mmapLoadedTiles.find(packedGridPos) == mmap->mmapLoadedTiles.end()) + 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. %03u%02i%02i.mmtile", mapId, x, y); return false; } - dtTileRef tileRef = mmap->mmapLoadedTiles[packedGridPos]; + dtTileRef tileRef = mmap->loadedTileRefs[packedGridPos]; // unload, and mark as non loaded if (dtStatusFailed(mmap->navMesh->removeTile(tileRef, NULL, NULL))) @@ -199,9 +301,11 @@ namespace MMAP } else { - mmap->mmapLoadedTiles.erase(packedGridPos); + mmap->loadedTileRefs.erase(packedGridPos); --loadedTiles; TC_LOG_INFO("maps", "MMAP:unloadMap: Unloaded mmtile %03i[%02i, %02i] from %03i", mapId, x, y, mapId); + + UnloadPhaseTile(mapId, x, y); return true; } @@ -219,7 +323,7 @@ namespace MMAP // unload all tiles from given map MMapData* mmap = loadedMMaps[mapId]; - for (MMapTileSet::iterator i = mmap->mmapLoadedTiles.begin(); i != mmap->mmapLoadedTiles.end(); ++i) + for (MMapTileSet::iterator i = mmap->loadedTileRefs.begin(); i != mmap->loadedTileRefs.end(); ++i) { uint32 x = (i->first >> 16); uint32 y = (i->first & 0x0000FFFF); @@ -227,6 +331,7 @@ namespace MMAP TC_LOG_ERROR("maps", "MMAP:unloadMap: Could not unload %03u%02i%02i.mmtile from navmesh", mapId, x, y); else { + UnloadPhaseTile(mapId, x, y); --loadedTiles; TC_LOG_INFO("maps", "MMAP:unloadMap: Unloaded mmtile %03i[%02i, %02i] from %03i", mapId, x, y, mapId); } @@ -265,15 +370,15 @@ namespace MMAP return true; } - dtNavMesh const* MMapManager::GetNavMesh(uint32 mapId) + dtNavMesh const* MMapManager::GetNavMesh(uint32 mapId, TerrainSet swaps) { if (loadedMMaps.find(mapId) == loadedMMaps.end()) return NULL; - return loadedMMaps[mapId]->navMesh; + return loadedMMaps[mapId]->GetNavMesh(swaps); } - dtNavMeshQuery const* MMapManager::GetNavMeshQuery(uint32 mapId, uint32 instanceId) + dtNavMeshQuery const* MMapManager::GetNavMeshQuery(uint32 mapId, uint32 instanceId, TerrainSet swaps) { if (loadedMMaps.find(mapId) == loadedMMaps.end()) return NULL; @@ -284,7 +389,7 @@ namespace MMAP // allocate mesh query dtNavMeshQuery* query = dtAllocNavMeshQuery(); ASSERT(query); - if (dtStatusFailed(query->init(mmap->navMesh, 1024))) + if (dtStatusFailed(query->init(mmap->GetNavMesh(swaps), 1024))) { dtFreeNavMeshQuery(query); TC_LOG_ERROR("maps", "MMAP:GetNavMeshQuery: Failed to initialize dtNavMeshQuery for mapId %03u instanceId %u", mapId, instanceId); @@ -297,4 +402,149 @@ namespace MMAP 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 %03u[%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 %03u%02i%02i.mmtile from navmesh", swap, x, y); + else + { + TC_LOG_DEBUG("phase", "MMapData::RemoveSwap: Unloaded phased %03u%02i%02i.mmtile from navmesh", swap, x, y); + + // restore base tile + if (dtStatusSucceed(navMesh->addTile(_baseTiles[packedXY]->data, _baseTiles[packedXY]->dataSize, NULL, 0, &loadedTileRefs[packedXY]))) + { + TC_LOG_DEBUG("phase", "MMapData::RemoveSwap: Loaded base mmtile %03u[%02i, %02i] into %03i[%02i, %02i]", _mapId, x, y, _mapId, header->x, header->y); + } + else + TC_LOG_ERROR("phase", "MMapData::RemoveSwap: Could not load base %03u%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 %03u[%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 %03u[%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]); + + uint32 old_x = oldTile->header->x; + uint32 old_y = oldTile->header->y; + + // 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 + memcpy(ptile->data + 8, (char*)&old_x, 4); + memcpy(ptile->data + 12, (char*)&old_y, 4); + + // 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 %03u%02i%02i.mmtile from navmesh", _mapId, x, y); + else + { + TC_LOG_DEBUG("phase", "MMapData::AddSwap: Unloaded %03u%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, NULL, 0, &loadedTileRefs[packedXY]))) + { + TC_LOG_DEBUG("phase", "MMapData::AddSwap: Loaded phased mmtile %03u[%02i, %02i] into %03i[%02i, %02i]", swap, x, y, _mapId, header->x, header->y); + } + else + TC_LOG_ERROR("phase", "MMapData::AddSwap: Could not load %03u%02i%02i.mmtile to navmesh", swap, x, y); + } + } + + dtNavMesh* MMapData::GetNavMesh(TerrainSet swaps) + { + for (uint32 swap : _activeSwaps) + { + if (swaps.find(swap) == swaps.end()) // swap not active + { + PhaseTileContainer 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 + } + } + } + + if (!swaps.empty()) + { + // for each of the calling unit's terrain swaps + for (uint32 swap : swaps) + { + // for each of the terrain swap's xy tiles + PhaseTileContainer ptc = MMAP::MMapFactory::createOrGetMMapManager()->GetPhaseTileContainer(swap); + for (PhaseTileContainer::const_iterator itr = ptc.begin(); itr != ptc.end(); ++itr) + { + if (_activeSwaps.find(swap) == _activeSwaps.end()) // swap not active + { + AddSwap(itr->second, swap, itr->first); // add swap + } + } + } + } + return navMesh; + } } diff --git a/src/server/collision/Management/MMapManager.h b/src/server/collision/Management/MMapManager.h index 2169d82bb3e..b7356716a9d 100644 --- a/src/server/collision/Management/MMapManager.h +++ b/src/server/collision/Management/MMapManager.h @@ -23,8 +23,10 @@ #include "DetourAlloc.h" #include "DetourNavMesh.h" #include "DetourNavMeshQuery.h" +#include "World.h" #include #include +#include // move map related classes namespace MMAP @@ -32,24 +34,54 @@ namespace MMAP typedef std::unordered_map MMapTileSet; typedef std::unordered_map NavMeshQuerySet; - // dummy struct to hold map's mmap data - struct MMapData - { - MMapData(dtNavMesh* mesh) : navMesh(mesh) { } - ~MMapData() - { - for (NavMeshQuerySet::iterator i = navMeshQueries.begin(); i != navMeshQueries.end(); ++i) - dtFreeNavMeshQuery(i->second); - if (navMesh) - dtFreeNavMesh(navMesh); - } + typedef std::set 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 PhaseTileContainer; + typedef std::unordered_map PhaseTileMap; + + + typedef std::unordered_map 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 - MMapTileSet mmapLoadedTiles; // maps [map grid coords] to [dtTile] + + dtNavMesh* navMesh; + MMapTileSet loadedTileRefs; + TerrainSetMap loadedPhasedTiles; + + private: + uint32 _mapId; + PhaseTileContainer _baseTiles; + std::set _activeSwaps; + void RemoveSwap(PhasedTile* ptile, uint32 swap, uint32 packedXY); + void AddSwap(PhasedTile* tile, uint32 swap, uint32 packedXY); }; @@ -69,17 +101,25 @@ namespace MMAP bool unloadMapInstance(uint32 mapId, uint32 instanceId); // the returned [dtNavMeshQuery const*] is NOT threadsafe - dtNavMeshQuery const* GetNavMeshQuery(uint32 mapId, uint32 instanceId); - dtNavMesh const* GetNavMesh(uint32 mapId); + 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 uint32(loadedMMaps.size()); } + uint32 getLoadedMapsCount() const { return loadedMMaps.size(); } + + void LoadPhaseTiles(uint32 mapId, int32 x, int32 y); + void UnloadPhaseTile(uint32 mapId, int32 x, int32 y); + PhaseTileContainer GetPhaseTileContainer(uint32 mapId) { return _phaseTiles[mapId]; } + private: bool loadMapData(uint32 mapId); uint32 packTileID(int32 x, int32 y); MMapDataSet loadedMMaps; uint32 loadedTiles; + + PhasedTile* LoadTile(uint32 mapId, int32 x, int32 y); + PhaseTileMap _phaseTiles; }; } diff --git a/src/server/game/Movement/PathGenerator.cpp b/src/server/game/Movement/PathGenerator.cpp index e041ed9c6e6..0e436715622 100644 --- a/src/server/game/Movement/PathGenerator.cpp +++ b/src/server/game/Movement/PathGenerator.cpp @@ -42,8 +42,8 @@ PathGenerator::PathGenerator(const Unit* owner) : if (DisableMgr::IsPathfindingEnabled(mapId)) { MMAP::MMapManager* mmap = MMAP::MMapFactory::createOrGetMMapManager(); - _navMesh = mmap->GetNavMesh(mapId); - _navMeshQuery = mmap->GetNavMeshQuery(mapId, _sourceUnit->GetInstanceId()); + _navMesh = mmap->GetNavMesh(mapId, _sourceUnit->GetTerrainSwaps()); + _navMeshQuery = mmap->GetNavMeshQuery(mapId, _sourceUnit->GetInstanceId(), _sourceUnit->GetTerrainSwaps()); } CreateFilter(); diff --git a/src/server/scripts/Commands/cs_misc.cpp b/src/server/scripts/Commands/cs_misc.cpp index 466c24eba5d..d437e72b933 100644 --- a/src/server/scripts/Commands/cs_misc.cpp +++ b/src/server/scripts/Commands/cs_misc.cpp @@ -219,7 +219,7 @@ public: uint32 haveMap = Map::ExistMap(mapId, gridX, gridY) ? 1 : 0; uint32 haveVMap = Map::ExistVMap(mapId, gridX, gridY) ? 1 : 0; - uint32 haveMMap = (DisableMgr::IsPathfindingEnabled(mapId) && MMAP::MMapFactory::createOrGetMMapManager()->GetNavMesh(handler->GetSession()->GetPlayer()->GetMapId())) ? 1 : 0; + uint32 haveMMap = (DisableMgr::IsPathfindingEnabled(mapId) && MMAP::MMapFactory::createOrGetMMapManager()->GetNavMesh(handler->GetSession()->GetPlayer()->GetMapId(), handler->GetSession()->GetPlayer()->GetTerrainSwaps())) ? 1 : 0; if (haveVMap) { diff --git a/src/server/scripts/Commands/cs_mmaps.cpp b/src/server/scripts/Commands/cs_mmaps.cpp index 16edb91873f..166afec6b39 100644 --- a/src/server/scripts/Commands/cs_mmaps.cpp +++ b/src/server/scripts/Commands/cs_mmaps.cpp @@ -64,7 +64,7 @@ public: static bool HandleMmapPathCommand(ChatHandler* handler, char const* args) { - if (!MMAP::MMapFactory::createOrGetMMapManager()->GetNavMesh(handler->GetSession()->GetPlayer()->GetMapId())) + if (!MMAP::MMapFactory::createOrGetMMapManager()->GetNavMesh(handler->GetSession()->GetPlayer()->GetMapId(), handler->GetSession()->GetPlayer()->GetTerrainSwaps())) { handler->PSendSysMessage("NavMesh not loaded for current map."); return true; @@ -132,8 +132,8 @@ public: handler->PSendSysMessage("gridloc [%i, %i]", gy, gx); // calculate navmesh tile location - dtNavMesh const* navmesh = MMAP::MMapFactory::createOrGetMMapManager()->GetNavMesh(handler->GetSession()->GetPlayer()->GetMapId()); - dtNavMeshQuery const* navmeshquery = MMAP::MMapFactory::createOrGetMMapManager()->GetNavMeshQuery(handler->GetSession()->GetPlayer()->GetMapId(), player->GetInstanceId()); + dtNavMesh const* navmesh = MMAP::MMapFactory::createOrGetMMapManager()->GetNavMesh(handler->GetSession()->GetPlayer()->GetMapId(), handler->GetSession()->GetPlayer()->GetTerrainSwaps()); + dtNavMeshQuery const* navmeshquery = MMAP::MMapFactory::createOrGetMMapManager()->GetNavMeshQuery(handler->GetSession()->GetPlayer()->GetMapId(), player->GetInstanceId(), handler->GetSession()->GetPlayer()->GetTerrainSwaps()); if (!navmesh || !navmeshquery) { handler->PSendSysMessage("NavMesh not loaded for current map."); @@ -184,8 +184,8 @@ public: static bool HandleMmapLoadedTilesCommand(ChatHandler* handler, char const* /*args*/) { uint32 mapid = handler->GetSession()->GetPlayer()->GetMapId(); - dtNavMesh const* navmesh = MMAP::MMapFactory::createOrGetMMapManager()->GetNavMesh(mapid); - dtNavMeshQuery const* navmeshquery = MMAP::MMapFactory::createOrGetMMapManager()->GetNavMeshQuery(mapid, handler->GetSession()->GetPlayer()->GetInstanceId()); + dtNavMesh const* navmesh = MMAP::MMapFactory::createOrGetMMapManager()->GetNavMesh(mapid, handler->GetSession()->GetPlayer()->GetTerrainSwaps()); + dtNavMeshQuery const* navmeshquery = MMAP::MMapFactory::createOrGetMMapManager()->GetNavMeshQuery(mapid, handler->GetSession()->GetPlayer()->GetInstanceId(), handler->GetSession()->GetPlayer()->GetTerrainSwaps()); if (!navmesh || !navmeshquery) { handler->PSendSysMessage("NavMesh not loaded for current map."); @@ -215,7 +215,7 @@ public: MMAP::MMapManager* manager = MMAP::MMapFactory::createOrGetMMapManager(); handler->PSendSysMessage(" %u maps loaded with %u tiles overall", manager->getLoadedMapsCount(), manager->getLoadedTilesCount()); - dtNavMesh const* navmesh = manager->GetNavMesh(handler->GetSession()->GetPlayer()->GetMapId()); + dtNavMesh const* navmesh = manager->GetNavMesh(handler->GetSession()->GetPlayer()->GetMapId(), handler->GetSession()->GetPlayer()->GetTerrainSwaps()); if (!navmesh) { handler->PSendSysMessage("NavMesh not loaded for current map."); -- cgit v1.2.3