mirror of
https://github.com/azerothcore/azerothcore-wotlk.git
synced 2025-11-10 12:24:22 +08:00
340 lines
11 KiB
C++
340 lines
11 KiB
C++
/*
|
|
* Copyright (C) 2016+ AzerothCore <www.azerothcore.org>
|
|
*
|
|
* 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 "VMapMgr2.h"
|
|
#include "Errors.h"
|
|
#include "Log.h"
|
|
#include "MapDefines.h"
|
|
#include "MapTree.h"
|
|
#include "ModelInstance.h"
|
|
#include "WorldModel.h"
|
|
#include <G3D/Vector3.h>
|
|
#include <iomanip>
|
|
#include <sstream>
|
|
#include <string>
|
|
|
|
using G3D::Vector3;
|
|
|
|
namespace VMAP
|
|
{
|
|
VMapMgr2::VMapMgr2()
|
|
{
|
|
GetLiquidFlagsPtr = &GetLiquidFlagsDummy;
|
|
IsVMAPDisabledForPtr = &IsVMAPDisabledForDummy;
|
|
thread_safe_environment = true;
|
|
}
|
|
|
|
VMapMgr2::~VMapMgr2()
|
|
{
|
|
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 VMapMgr2::InitializeThreadUnsafe(const std::vector<uint32>& mapIds)
|
|
{
|
|
// the caller must pass the list of all mapIds that will be used in the VMapMgr2 lifetime
|
|
for (const uint32& mapId : mapIds)
|
|
{
|
|
iInstanceMapTrees.emplace(mapId, nullptr);
|
|
}
|
|
|
|
thread_safe_environment = false;
|
|
}
|
|
|
|
Vector3 VMapMgr2::convertPositionToInternalRep(float x, float y, float z) const
|
|
{
|
|
Vector3 pos;
|
|
const float mid = 0.5f * MAX_NUMBER_OF_GRIDS * SIZE_OF_GRIDS;
|
|
pos.x = mid - x;
|
|
pos.y = mid - y;
|
|
pos.z = z;
|
|
|
|
return pos;
|
|
}
|
|
|
|
InstanceTreeMap::const_iterator VMapMgr2::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;
|
|
}
|
|
|
|
// move to MapTree too?
|
|
std::string VMapMgr2::getMapFileName(unsigned int mapId)
|
|
{
|
|
std::stringstream fname;
|
|
fname.width(3);
|
|
fname << std::setfill('0') << mapId << std::string(MAP_FILENAME_EXTENSION2);
|
|
|
|
return fname.str();
|
|
}
|
|
|
|
int VMapMgr2::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;
|
|
}
|
|
|
|
// load one tile (internal use only)
|
|
bool VMapMgr2::_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
|
|
ABORT("Invalid mapId {} tile [{}, {}] passed to VMapMgr2 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 VMapMgr2::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 VMapMgr2::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 VMapMgr2::isInLineOfSight(unsigned int mapId, float x1, float y1, float z1, float x2, float y2, float z2, ModelIgnoreFlags ignoreFlags)
|
|
{
|
|
#if defined(ENABLE_VMAP_CHECKS)
|
|
if (!isLineOfSightCalcEnabled() || IsVMAPDisabledForPtr(mapId, VMAP_DISABLE_LOS))
|
|
{
|
|
return true;
|
|
}
|
|
#endif
|
|
|
|
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, ignoreFlags);
|
|
}
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
get the hit position and return true if we hit something
|
|
otherwise the result pos will be the dest pos
|
|
*/
|
|
bool VMapMgr2::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 defined(ENABLE_VMAP_CHECKS)
|
|
if (isLineOfSightCalcEnabled() && !IsVMAPDisabledForPtr(mapId, VMAP_DISABLE_LOS))
|
|
#endif
|
|
{
|
|
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 VMapMgr2::getHeight(unsigned int mapId, float x, float y, float z, float maxSearchDist)
|
|
{
|
|
#if defined(ENABLE_VMAP_CHECKS)
|
|
if (isHeightCalcEnabled() && !IsVMAPDisabledForPtr(mapId, VMAP_DISABLE_HEIGHT))
|
|
#endif
|
|
{
|
|
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 VMapMgr2::GetAreaAndLiquidData(uint32 mapId, float x, float y, float z, Optional<uint8> reqLiquidType, AreaAndLiquidData& data) const
|
|
{
|
|
InstanceTreeMap::const_iterator instanceTree = GetMapTree(mapId);
|
|
if (instanceTree != iInstanceMapTrees.end())
|
|
{
|
|
LocationInfo info;
|
|
Vector3 pos = convertPositionToInternalRep(x, y, z);
|
|
if (instanceTree->second->GetLocationInfo(pos, info))
|
|
{
|
|
data.floorZ = info.ground_Z;
|
|
if (!IsVMAPDisabledForPtr(mapId, VMAP_DISABLE_LIQUIDSTATUS))
|
|
{
|
|
uint32 liquidType = info.hitModel->GetLiquidType(); // entry from LiquidType.dbc
|
|
float liquidLevel;
|
|
if (!reqLiquidType || (GetLiquidFlagsPtr(liquidType) & *reqLiquidType))
|
|
if (info.hitInstance->GetLiquidLevel(pos, info, liquidLevel))
|
|
data.liquidInfo.emplace(liquidType, liquidLevel);
|
|
}
|
|
|
|
if (!IsVMAPDisabledForPtr(mapId, VMAP_DISABLE_AREAFLAG))
|
|
data.areaInfo.emplace(info.hitModel->GetWmoID(), info.hitInstance->adtId, info.rootId, info.hitModel->GetMogpFlags(), info.hitInstance->ID);
|
|
return true;
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
WorldModel* VMapMgr2::acquireModelInstance(const std::string& basepath, const std::string& filename, uint32 flags/* Only used when creating the model */)
|
|
{
|
|
//! 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"))
|
|
{
|
|
LOG_ERROR("maps", "VMapMgr2: could not load '{}{}.vmo'", basepath, filename);
|
|
delete worldmodel;
|
|
return nullptr;
|
|
}
|
|
LOG_DEBUG("maps", "VMapMgr2: loading file '{}{}'", basepath, filename);
|
|
|
|
worldmodel->Flags = flags;
|
|
|
|
model = iLoadedModelFiles.insert(std::pair<std::string, ManagedModel>(filename, ManagedModel())).first;
|
|
model->second.setModel(worldmodel);
|
|
}
|
|
|
|
return model->second.getModel();
|
|
}
|
|
|
|
void VMapMgr2::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())
|
|
{
|
|
LOG_ERROR("maps", "VMapMgr2: trying to unload non-loaded file '{}'", filename);
|
|
return;
|
|
}
|
|
if (model->second.decRefCount() == 0)
|
|
{
|
|
LOG_DEBUG("maps", "VMapMgr2: unloading file '{}'", filename);
|
|
delete model->second.getModel();
|
|
iLoadedModelFiles.erase(model);
|
|
}
|
|
}
|
|
|
|
LoadResult VMapMgr2::existsMap(const char* basePath, unsigned int mapId, int x, int y)
|
|
{
|
|
return StaticMapTree::CanLoadMap(std::string(basePath), mapId, x, y);
|
|
}
|
|
|
|
void VMapMgr2::GetInstanceMapTree(InstanceTreeMap& instanceMapTree)
|
|
{
|
|
instanceMapTree = iInstanceMapTrees;
|
|
}
|
|
|
|
} // namespace VMAP
|