📄 mapdata.cpp
字号:
//-------------------------------------------------------------
bool CMapData::PlaceTerrainObj(CTerrainObj* pTerrainObj)
{
if(!pTerrainObj)
return false;
int nAmount = pTerrainObj->GetPartAmount();
for(int i = 0; i < nAmount; i++)
{
CTerrainObjPart* pPart = pTerrainObj->GetPartByIndex(i);
if(!pPart)
return false;
SIZE sizePart = pPart->GetBase();
for(int j = 0; j < sizePart.cy; j++)
{
for(int k = 0; k < sizePart.cx; k++)
{
CLayer* pLayer = pPart->GetLayer(k, j);
CLayer* pNewLayer = CLayer::CreateNew();
if(!pLayer || !pNewLayer)
return false;
POINT posCell = pPart->GetPos();
posCell.x -= k;
posCell.y -= j;
CCell* pCell = GetCell(posCell.x, posCell.y);
if(!pCell)
return false;
pNewLayer->dwMask = pLayer->dwMask;
pNewLayer->nTerrian = pLayer->nTerrian;
pNewLayer->nAltitude = pLayer->nAltitude + pCell->GetFloorAlt(m_set2Layer);
pCell->AddLayer(m_set2Layer, pNewLayer);
}
}
}
return true;
}
//-------------------------------------------------------------
bool CMapData::DisplaceTerrainObj(CTerrainObj* pTerrainObj)
{
CHECKF(pTerrainObj);
int nAmount = pTerrainObj->GetPartAmount();
for(int i = 0; i < nAmount; i++)
{
CTerrainObjPart* pPart = pTerrainObj->GetPartByIndex(i);
ASSERT(pPart);
if(!pPart)
return false;
SIZE sizePart = pPart->GetBase();
for(int j = 0; j < sizePart.cy; j++)
{
for(int k = 0; k < sizePart.cx; k++)
{
POINT posCell = pPart->GetPos();
posCell.x -= k;
posCell.y -= j;
CCell* pCell = GetCell(posCell.x, posCell.y);
ASSERT(pCell);
if(!pCell)
return false;
ASSERT(pCell->DelLayer(m_set2Layer));
}
}
}
return true;
}
//----------------------------------------------------//
void CMapData::ClearCell()
{
/* int nAmount = m_setCell.size();
for(int i = 0; i < nAmount; i++)
{
CCell* pCell = m_setCell[i];
SAFE_DELETE(pCell);
}
*/ m_setCell.clear();
}
//----------------------------------------------------//
void CMapData::ClearPassage()
{
int nAmount = m_setPassage.size();
for(int i = 0; i < nAmount; i++)
{
PassageInfo* pInfo = m_setPassage[i];
SAFE_DELETE(pInfo);
}
m_setPassage.clear();
}
//----------------------------------------------------//
void CMapData::ClearMapObj()
{
int nAmount = m_setMapObj.size();
for(int i = 0; i < nAmount; i++)
{
CTerrainObj* pInfo = m_setMapObj[i];
SAFE_DELETE(pInfo);
}
m_setMapObj.clear();
}
//----------------------------------------------------//
void CMapData::ClearLayerSet()
{
int nAmount = m_set2Layer.size();
for(int i = 0; i < nAmount; i++)
{
for(int j = 0; j < m_set2Layer[i].size(); j++)
{
if(m_set2Layer[i][j])
delete m_set2Layer[i][j];
}
}
m_set2Layer.clear();
}
//----------------------------------------------------//
CCell* CMapData::GetCell(int nIndex)
{
int nAmount = m_setCell.size();
if((nIndex < 0) || (nIndex >= nAmount))
return NULL;
return m_setCell[nIndex];
}
//----------------------------------------------------//
CCell* CMapData::GetCell(int nCellX, int nCellY)
{
return GetCell(POS2INDEX(nCellX, nCellY, m_sizeMap.cx, m_sizeMap.cy));
}
//----------------------------------------------------//
void CMapData::AddPassage(const PassageInfo* pInfo)
{
CHECK(pInfo);
POINT posCell;
posCell.x = pInfo->nPosX;
posCell.y = pInfo->nPosY;
AddPassage(posCell, pInfo->nIndex);
}
//----------------------------------------------------//
void CMapData::AddPassage(POINT posMap, int nIndex)
{
// 兼容于地图编辑器
{
int nAmount = m_setPassage.size();
for(int i = 0; i < nAmount; i++)
{
PassageInfo* pInfo = m_setPassage[i];
if(pInfo)
{
if((pInfo->nPosX == posMap.x) && (pInfo->nPosY == posMap.y))
{
pInfo->nIndex = nIndex;
return;
}
}
}
}
PassageInfo* pInfo = new PassageInfo;
if(pInfo)
{
pInfo->nPosX = posMap.x;
pInfo->nPosY = posMap.y;
pInfo->nIndex = nIndex;
m_setPassage.push_back(pInfo);
}
}
//----------------------------------------------------//
void CMapData::DelPassage(POINT posMap)
{
int nAmount = m_setPassage.size();
for(int i = 0; i < nAmount; i++)
{
PassageInfo* pInfo = m_setPassage[i];
if(pInfo)
{
if((pInfo->nPosX == posMap.x) && (pInfo->nPosY == posMap.y))
{
SAFE_DELETE(pInfo);
m_setPassage.erase(m_setPassage.begin()+i);
// 兼容于编辑器 return ;
}
}
}
}
//----------------------------------------------------//
int CMapData::GetExitIndex(POINT posCell) // return -1 : error
{
int nAmount = m_setPassage.size();
for(int i = 0; i < nAmount; i++)
{
PassageInfo* pInfo = m_setPassage[i];
if(pInfo)
{
if((pInfo->nPosX == posCell.x) && (pInfo->nPosY == posCell.y))
{
return pInfo->nIndex;
}
}
}
return -1;
}
//----------------------------------------------------//
bool CMapData::AddMapObj(CTerrainObj* pObj)
{
CHECKF(pObj);
m_setMapObj.push_back(pObj);
return PlaceTerrainObj(pObj);
}
bool CMapData::DelMapObj(int idx)
{
CHECKF(idx >= 0 && idx < m_setMapObj.size());
CTerrainObj* pObj = m_setMapObj[idx];
if(pObj && DisplaceTerrainObj(pObj))
{
SAFE_DELETE(pObj);
m_setMapObj.erase(m_setMapObj.begin() + idx);
return true;
}
return false;
}
///////////////////////////////////////////////////////////////////////////////////////
// interface
///////////////////////////////////////////////////////////////////////////////////////
bool CMapData::AddTerrainItem(OBJID idOwner, int nCellX, int nCellY, OBJID idTerrainItemType)
{
CHECKF(idTerrainItemType != ID_NONE);
char szKey[256];
sprintf(szKey, "NpcType%u", idTerrainItemType/10);
CIniFile ini("ini\\TerrainNpc.ini", szKey);
char szField[256];
sprintf(szField, "Dir%u", idTerrainItemType%10);
char szFileName[256];
if(!ini.GetString(szFileName, szField, 256))
{
LOGERROR("Can't find TerrainNpc[%d] in ini\\TerrainNpc.ini", idTerrainItemType);
return NULL;
}
CTerrainObj* pObj = CTerrainObj::CreateNew(szFileName, idOwner);
if(!pObj)
return false;
POINT posCell;
posCell.x = nCellX;
posCell.y = nCellY;
pObj->SetPos(posCell);
if(!AddMapObj(pObj))
{
SAFE_DELETE(pObj);
return false;
}
return true;
}
///////////////////////////////////////////////////////////////////////////////////////
bool CMapData::DelTerrainItem(OBJID idOwner)
{
for(int i = m_setMapObj.size()-1; i >= 0; i--)
{
CTerrainObj* pObj = m_setMapObj[i];
if(pObj && pObj->GetOwnerID() == idOwner)
{
if(!DelMapObj(i)) // 注意:如果一个OWNER有多个物件时,要先放关键物件,否则会部分删除。
return false;
}
}
return true;
}
///////////////////////////////////////////////////////////////////////////////////////
int CMapData::GetMapWidth()
{
return m_sizeMap.cx;
}
///////////////////////////////////////////////////////////////////////////////////////
int CMapData::GetMapHeight()
{
return m_sizeMap.cy;
}
///////////////////////////////////////////////////////////////////////////////////////
CCell* CMapData::QueryCell(int nCellX, int nCellY)
{
CHECKF(nCellX >= 0 && nCellX < m_sizeMap.cx);
CHECKF(nCellY >= 0 && nCellY < m_sizeMap.cy);
return GetCell(nCellX, nCellY);
}
///////////////////////////////////////////////////////////////////////////////////////
CCell* CMapData::QueryCell(POINT posCell)
{
return QueryCell(posCell.x, posCell.y);
}
///////////////////////////////////////////////////////////////////////////////////////
int CMapData::GetPassage(int x, int y)
{
for(int i = 0; i < m_setPassage.size(); i++)
{
if(m_setPassage[i]->nPosX == x && m_setPassage[i]->nPosY == y)
{
return m_setPassage[i]->nIndex;
}
}
return PASSAGE_NONE;
}
///////////////////////////////////////////////////////////////////////////////////////
// global entry
///////////////////////////////////////////////////////////////////////////////////////
IMapData* IMapData::CreateNew(int nMapDoc, LPCTSTR pszVersion/*=0*/)
{
CHECKF(nMapDoc != ID_NONE);
char szField[256];
sprintf(szField, "Map%d", nMapDoc);
CIniFile ini("ini\\gamemap.ini", szField);
char szFileName[256];
if(!ini.GetString(szFileName, "File", 256))
return NULL;
IMapData* pObj = CMapData::CreateNew(szFileName, pszVersion);
if(!pObj)
return false;
return pObj;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -