📄 vmapmanager.cpp
字号:
/*
* Copyright (C) 2005,2006,2007 MaNGOS <http://www.mangosproject.org/>
* Copyright (C) 2007-2008 Ascent Team <http://www.ascentemu.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, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "VMapManager.h"
#if defined(WIN32)
#define snprintf _snprintf
#endif
#define MAX_MAPS 600
namespace VMAP
{
inline bool IsTileMap(unsigned int mapid)
{
switch( mapid )
{
case 509:
case 469:
case 189:
case 30:
case 37:
case 33:
case 533:
case 209:
case 309:
case 560:
case 534:
case 532:
case 543:
case 568:
case 564:
case 0:
case 1:
case 530:
return true;
break;
default:
return false;
break;
}
}
static MapTree * m_maps[MAX_MAPS];
//=========================================================
VMapManager::VMapManager()
{
memset( m_maps, 0, sizeof(MapTree*) * MAX_MAPS );
}
//=========================================================
VMapManager::~VMapManager(void)
{
for(unsigned int i = 0; i < MAX_MAPS; ++i)
{
if( m_maps[i] != NULL )
delete m_maps[i];
m_maps[i] = NULL;
}
}
//=========================================================
inline Vector3 VMapManager::convertPositionToInternalRep(float x, float y, float z) const
{
float pos[3];
pos[0] = y;
pos[1] = z;
pos[2] = x;
double full = 64.0*533.33333333;
double mid = full/2.0;
pos[0] = full- (pos[0] + mid);
pos[2] = full- (pos[2] + mid);
return(Vector3(pos));
}
//=========================================================
inline Vector3 VMapManager::convertPositionToMangosRep(float x, float y, float z) const
{
float pos[3];
pos[0] = z;
pos[1] = x;
pos[2] = y;
double full = 64.0*533.33333333;
double mid = full/2.0;
pos[0] = -((mid+pos[0])-full);
pos[1] = -((mid+pos[1])-full);
return(Vector3(pos));
}
//=========================================================
//=========================================================
inline Vector3 VMapManager::convertPositionToInternalRep(LocationVector & vec) const
{
float pos[3];
pos[0] = vec.y;
pos[1] = vec.z;
pos[2] = vec.x;
double full = 64.0*533.33333333;
double mid = full/2.0;
pos[0] = full- (pos[0] + mid);
pos[2] = full- (pos[2] + mid);
return(Vector3(pos));
}
inline Vector3 VMapManager::convertPositionToInternalRepMod(LocationVector & vec) const
{
float pos[3];
pos[0] = vec.y;
pos[1] = vec.z + 2.0f;
pos[2] = vec.x;
double full = 64.0*533.33333333;
double mid = full/2.0;
pos[0] = full- (pos[0] + mid);
pos[2] = full- (pos[2] + mid);
return(Vector3(pos));
}
//=========================================================
inline Vector3 VMapManager::convertPositionToMangosRep(LocationVector & vec) const
{
float pos[3];
pos[0] = vec.y;
pos[1] = vec.z;
pos[2] = vec.x;
double full = 64.0*533.33333333;
double mid = full/2.0;
pos[0] = -((mid+pos[0])-full);
pos[1] = -((mid+pos[1])-full);
return(Vector3(pos));
}
inline LocationVector VMapManager::convertPositionToMangosRep(Vector3 & src) const
{
float pos[3];
pos[0] = src.z;
pos[1] = src.x;
pos[2] = src.y;
double full = 64.0*533.33333333;
double mid = full/2.0;
pos[0] = -((mid+pos[0])-full);
pos[1] = -((mid+pos[1])-full);
return LocationVector(pos[0], pos[1], pos[2]);
}
//=========================================================
std::string VMapManager::getDirFileName(unsigned int pMapId, int x, int y) const
{
char name[FILENAMEBUFFER_SIZE];
snprintf(name, FILENAMEBUFFER_SIZE, "%03u_%d_%d%s",pMapId, x, y, DIR_FILENAME_EXTENSION);
return(std::string(name));
}
//=========================================================
std::string VMapManager::getDirFileName(unsigned int pMapId) const
{
char name[FILENAMEBUFFER_SIZE];
snprintf(name, FILENAMEBUFFER_SIZE, "%03d%s",pMapId, DIR_FILENAME_EXTENSION);
return(std::string(name));
}
//=========================================================
// remote last return or LF
void chomp(std::string& str)
{
while(str.length() >0)
{
char lc = str[str.length()-1];
if(lc == '\r' || lc == '\n')
{
str = str.substr(0,str.length()-1);
}
else
{
break;
}
}
}
//=========================================================
void chompAndTrim(std::string& str)
{
while(str.length() >0)
{
char lc = str[str.length()-1];
if(lc == '\r' || lc == '\n' || lc == ' ' || lc == '"' || lc == '\'')
{
str = str.substr(0,str.length()-1);
}
else
{
break;
}
}
while(str.length() >0)
{
char lc = str[0];
if(lc == ' ' || lc == '"' || lc == '\'')
{
str = str.substr(1,str.length()-1);
}
else
{
break;
}
}
}
//=========================================================
int VMapManager::loadMap(const char* pBasePath, unsigned int pMapId, int x, int y)
{
bool result = false;
std::string dirFileName;
if( pMapId >= MAX_MAPS )
return false;
if( IsTileMap( pMapId ) )
dirFileName = getDirFileName( pMapId, x, y );
else
dirFileName = getDirFileName( pMapId );
MapTree* instanceTree = m_maps[pMapId];
if( instanceTree == NULL )
{
instanceTree = new MapTree( pBasePath );
m_maps[pMapId] = instanceTree;
}
unsigned int mapTileIdent = MAP_TILE_IDENT(x,y);
result = instanceTree->loadMap(dirFileName, mapTileIdent);
if(!result) // remove on fail
{
if(instanceTree->size() == 0)
{
m_maps[pMapId] = NULL;
delete instanceTree;
}
}
return(result);
}
//=========================================================
void VMapManager::unloadMap(unsigned int pMapId, int x, int y)
{
if( m_maps[pMapId] != NULL )
{
MapTree * instanceTree = m_maps[pMapId];
std::string dirFileName;
if( IsTileMap( pMapId ) )
dirFileName = getDirFileName( pMapId, x, y );
else
dirFileName = getDirFileName( pMapId );
unsigned int mapTileIdent = MAP_TILE_IDENT(x,y);
instanceTree->unloadMap(dirFileName, mapTileIdent);
if(instanceTree->size() == 0)
{
m_maps[ pMapId ] = NULL;
delete instanceTree;
}
}
}
//=========================================================
void VMapManager::unloadMap(unsigned int pMapId)
{
if( m_maps[pMapId] != NULL )
{
MapTree* instanceTree = m_maps[ pMapId ];
std::string dirFileName = getDirFileName(pMapId);
instanceTree->unloadMap(dirFileName, 0);
if(instanceTree->size() == 0)
{
m_maps[pMapId]=NULL;
delete instanceTree;
}
}
}
//==========================================================
bool VMapManager::isInLineOfSight(unsigned int pMapId, float x1, float y1, float z1, float x2, float y2, float z2)
{
bool result = true;
if( m_maps[pMapId] != NULL )
{
Vector3 pos1 = convertPositionToInternalRep(x1,y1,z1);
Vector3 pos2 = convertPositionToInternalRep(x2,y2,z2);
if(pos1 != pos2)
{
MapTree* mapTree = m_maps[pMapId];
result = mapTree->isInLineOfSight(pos1, pos2);
}
}
return(result);
}
bool VMapManager::isInLineOfSight(unsigned int pMapId, LocationVector & v1, LocationVector & v2)
{
bool result = true;
if( m_maps[pMapId] != NULL )
{
Vector3 pos1 = convertPositionToInternalRepMod(v1);
Vector3 pos2 = convertPositionToInternalRepMod(v2);
if(pos1 != pos2)
{
MapTree* mapTree = m_maps[pMapId];
result = mapTree->isInLineOfSight(pos1, pos2);
}
}
return(result);
}
bool VMapManager::isInDoors(unsigned int mapid, float x, float y, float z)
{
bool result = false;
if( m_maps[mapid] != NULL )
{
Vector3 pos = convertPositionToInternalRep(x,y,z);
MapTree* mapTree = m_maps[mapid];
result = mapTree->isInDoors(pos);
}
return(result);
}
bool VMapManager::isInDoors(unsigned int mapid, LocationVector & vec)
{
bool result = false;
if( m_maps[mapid] != NULL )
{
Vector3 pos = convertPositionToInternalRepMod(vec);
MapTree* mapTree = m_maps[mapid];
result = mapTree->isInDoors(pos);
}
return(result);
}
bool VMapManager::isOutDoors(unsigned int mapid, float x, float y, float z)
{
bool result = false;
if( m_maps[mapid] != NULL )
{
Vector3 pos = convertPositionToInternalRep(x,y,z);
MapTree* mapTree = m_maps[mapid];
result = mapTree->isOutDoors(pos);
}
return(result);
}
bool VMapManager::isOutDoors(unsigned int mapid, LocationVector & vec)
{
bool result = false;
if( m_maps[mapid] != NULL )
{
Vector3 pos = convertPositionToInternalRepMod(vec);
MapTree* mapTree = m_maps[mapid];
result = mapTree->isOutDoors(pos);
}
return(result);
}
//=========================================================
/**
get the hit position and return true if we hit something
otherwise the result pos will be the dest pos
*/
bool VMapManager::getObjectHitPos(unsigned int pMapId, float x1, float y1, float z1, float x2, float y2, float z2, float& rx, float &ry, float& rz, float pModifyDist)
{
bool result = false;
rx=x2;
ry=y2;
rz=z2;
if( m_maps[pMapId] != NULL )
{
Vector3 pos1 = convertPositionToInternalRep(x1,y1,z1);
Vector3 pos2 = convertPositionToInternalRep(x2,y2,z2);
Vector3 resultPos;
MapTree* mapTree = m_maps[pMapId];
result = mapTree->getObjectHitPos(pos1, pos2, resultPos, pModifyDist);
resultPos = convertPositionToMangosRep(resultPos.x,resultPos.y,resultPos.z);
rx = resultPos.x;
ry = resultPos.y;
rz = resultPos.z;
}
return result;
}
bool VMapManager::getObjectHitPos(unsigned int pMapId, LocationVector & v1, LocationVector & v2, LocationVector & vout, float pModifyDist)
{
bool result = false;
vout = v2;
if( m_maps[pMapId] != NULL )
{
Vector3 pos1 = convertPositionToInternalRepMod(v1);
Vector3 pos2 = convertPositionToInternalRepMod(v2);
Vector3 resultPos;
MapTree* mapTree = m_maps[pMapId];
result = mapTree->getObjectHitPos(pos1, pos2, resultPos, pModifyDist);
vout = convertPositionToMangosRep(resultPos);
}
return result;
}
//=========================================================
/**
get height or INVALID_HEIGHT if to hight was calculated
*/
//int gGetHeightCounter = 0;
float VMapManager::getHeight(unsigned int pMapId, float x, float y, float z)
{
float height = VMAP_INVALID_HEIGHT; //no height
if( m_maps[pMapId] != NULL )
{
Vector3 pos = convertPositionToInternalRep(x,y,z);
height = m_maps[pMapId]->getHeight(pos);
if(!(height < inf()))
{
return VMAP_INVALID_HEIGHT;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -