📄 cell.cpp
字号:
// CELL.cpp: implementation of the CELL class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "server.h"
#include "CELL.h"
#include "Extern.h"
#include "USER.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
POINT g_add[8][5] =
{
{{-2, -2}, {-1, -2}, {0, -2}, {-2, -1}, {-2, 0}}, // 1
{{-1, -2}, {0, -2}, {1, -2}, {100, 0}, {100, 0}}, // 2
{{0, -2}, {1, -2}, {2, -2}, {2, -1}, {2, 0}}, // 3
{{-2, -1}, {-2, 0}, {-2, 1}, {100, 0}, {100, 0}}, // 4
{{2, -1}, {2, 0}, {2, 1}, {100, 0}, {100, 0}}, // 5
{{-2, 0}, {-2, 1}, {-2, 2}, {-1, 2}, {0, 2}}, // 6
{{-1, 2}, {0, 2}, {1, 2}, {100, 0}, {100, 0}}, // 7
{{2, 0}, {2, 1}, {2, 2}, {1, 2}, {0, 2}}, // 8
};
POINT g_del[8][5] =
{
{{1, -1}, {1, 0}, {1, 1}, {0, 1}, {-1, 1}}, // 1
{{-1, 1}, {0, 1}, {1, 1}, {100, 0}, {100, 0}}, // 2
{{-1, -1}, {-1, 0}, {-1, 1}, {0, 1}, {1, 1}}, // 3
{{1, -1}, {1, 0}, {1, 1}, {100, 0}, {100, 0}}, // 4
{{-1, -1}, {-1, 0}, {-1, 1}, {100, 0}, {100, 0}}, // 5
{{-1, -1}, {0, -1}, {1, -1}, {1, 0}, {1, 1}}, // 6
{{-1, -1}, {0, -1}, {1, -1}, {100, 0}, {100, 0}}, // 7
{{-1, -1}, {0, -1}, {1, -1}, {-1, 0}, {-1, 1}}, // 8
};
//////////////////////////////////////////////////////////////////////
// CellInfo
//////////////////////////////////////////////////////////////////////
CellInfo::CellInfo()
{
::InitializeCriticalSection(&m_cs);
::InitializeCriticalSection(&m_Itemcs);
//::InitializeCriticalSectionAndSpinCount(&m_cs, SPIN_COUNT);
//::InitializeCriticalSectionAndSpinCount(&m_Itemcs, SPIN_COUNT);
}
CellInfo::~CellInfo()
{
m_arCell.RemoveAll();
m_arItemCell.RemoveAll();
::DeleteCriticalSection(&m_cs);
::DeleteCriticalSection(&m_Itemcs);
}
/////////////////////////////////////////////////////////////////////
// Cell Array 俊 USER(NPC) ID 甫 持绰促.
//
void CellInfo::Add(int nUid)
{
::EnterCriticalSection(&m_cs);
m_arCell.Add(nUid);
::LeaveCriticalSection(&m_cs);
}
/////////////////////////////////////////////////////////////////////
// Cell Array 俊辑 USER(NPC) ID 甫 昏力茄促.
//
void CellInfo::Delete(int nUid)
{
::EnterCriticalSection(&m_cs);
for(int i = 0; i < m_arCell.GetSize(); i++)
{
if(m_arCell[i] == nUid)
{
m_arCell.RemoveAt(i); // 趣矫扼档 隔扼辑 break 巩阑 救逞
// break;
}
}
::LeaveCriticalSection(&m_cs);
}
/////////////////////////////////////////////////////////////////////
// Cell Array 甫 copy 秦 霖促.
//
void CellInfo::Get(CArray<int, int>& arCopy)
{
::EnterCriticalSection(&m_cs);
arCopy.RemoveAll();
for(int i = 0; i < m_arCell.GetSize(); i++) arCopy.Add(m_arCell.GetAt(i));
::LeaveCriticalSection(&m_cs);
}
void CellInfo::DeleteItem(CPoint nUid)
{
::EnterCriticalSection(&m_Itemcs);
for(int i = 0; i < m_arItemCell.GetSize(); i++)
{
if(m_arItemCell[i].x == nUid.x && m_arItemCell[i].y == nUid.y)
{
m_arItemCell.RemoveAt(i);
// break;
}
}
::LeaveCriticalSection(&m_Itemcs);
}
void CellInfo::AddItem(CPoint nUid)
{
::EnterCriticalSection(&m_Itemcs);
m_arItemCell.Add(nUid);
::LeaveCriticalSection(&m_Itemcs);
}
void CellInfo::GetItem(CArray<CPoint,CPoint>& arItemCopy)
{
::EnterCriticalSection(&m_Itemcs);
arItemCopy.RemoveAll();
for(int i = 0; i < m_arItemCell.GetSize(); i++) arItemCopy.Add(m_arItemCell.GetAt(i));
::LeaveCriticalSection(&m_Itemcs);
}
//////////////////////////////////////////////////////////////////////
// CELL
//////////////////////////////////////////////////////////////////////
CELL::CELL(CSize size, int nZone, COM *pCom)
{
m_pCom = pCom;
m_sizeMap = size;
int nX = size.cx / CELL_XSIZE;
int nY = size.cy / CELL_YSIZE;
if((nX * CELL_XSIZE) != size.cx) nX++;
if((nY * CELL_YSIZE) != size.cy) nY++;
m_sizeCell.cx = nX;
m_sizeCell.cy = nY;
m_Zone = nZone;
m_pCell = new CellInfo* [m_sizeCell.cx];
for(int i = 0; i < m_sizeCell.cx; i++)
{
m_pCell[i] = new CellInfo[m_sizeCell.cy];
}
}
CELL::~CELL()
{
RemoveCellData();
}
///////////////////////////////////////////////////////////////////////
// 皋葛府 沥府
//
void CELL::RemoveCellData()
{
if(m_pCell)
{
for(int i = 0; i < m_sizeCell.cx; i++)
{
delete[] m_pCell[i];
}
delete[] m_pCell;
}
}
////////////////////////////////////////////////////////////////////////
// 泅犁 伎狼 困摹甫 备茄促.
//
CPoint CELL::GetCellIndex(int xpos, int ypos)
{
if(xpos > m_sizeMap.cx || xpos < 0)
{
return CPoint(-1, -1);
}
if(ypos > m_sizeMap.cy || ypos < 0)
{
return CPoint(-1, -1);
}
int x, y;
int sx, sy, ex, ey;
CRect rect;
CPoint pt(xpos, ypos);
CPoint ptReturn;
for(y = 0; y < m_sizeCell.cy; y++)
{
sy = y * CELL_YSIZE;
ey = sy + CELL_YSIZE;
for(x = 0; x < m_sizeCell.cx; x++)
{
sx = x * CELL_XSIZE;
ex = sx + CELL_XSIZE;
rect.SetRect(sx, sy, ex, ey);
if(rect.PtInRect(pt))
{
ptReturn.x = x;
ptReturn.y = y;
return ptReturn;
}
}
}
return CPoint(-1, -1);
}
////////////////////////////////////////////////////////////////////////
// 蜡历啊 捞悼茄 Cell 规氢阑 备茄促.
//
// 1 2 3
// 4 5
// 6 7 8
int CELL::GetDirection(int sx, int sy, int ex, int ey)
{
int dx = ex - sx;
int dy = ey - sy;
if(dx == -1 && dy == -1) return 1;
if(dx == 0 && dy == -1) return 2;
if(dx == 1 && dy == -1) return 3;
if(dx == -1 && dy == 0) return 4;
if(dx == 1 && dy == 0) return 5;
if(dx == -1 && dy == 1) return 6;
if(dx == 0 && dy == 1) return 7;
if(dx == 1 && dy == 1) return 8;
return -1; // return ERROR
}
////////////////////////////////////////////////////////////////////////
// 蜡历啊 捞悼茄 Cell 规氢阑 备茄促.
//
int CELL::GetDirection(CPoint ptOld, CPoint ptNew)
{
int dx = ptNew.x - ptOld.x;
int dy = ptNew.y - ptOld.y;
if(dx == -1 && dy == -1) return 1;
if(dx == 0 && dy == -1) return 2;
if(dx == 1 && dy == -1) return 3;
if(dx == -1 && dy == 0) return 4;
if(dx == 1 && dy == 0) return 5;
if(dx == -1 && dy == 1) return 6;
if(dx == 0 && dy == 1) return 7;
if(dx == 1 && dy == 1) return 8;
return -1; // return ERROR
}
////////////////////////////////////////////////////////////////////////
// Cell 俊 货肺款 蜡历唱 NPC甫 眠啊茄促.
//
void CELL::AddCell(CPoint pt, int nUid)
{
// Add User ID to Cell and Copy Cell Array
CCellItemArray arCellItem;
CCellUidArray arUid;
m_pCell[pt.x][pt.y].Add(nUid);
ItemInfoArray arMapItem;
arMapItem.RemoveAll();
USER* pUser = NULL;
USER* pSendUser = NULL;
CNpc* pNpc = NULL;
int nSize = 0, iSize = 0;
int i, nX, nY;
int cx, cy;
DWORD dwDN = 0;
if(nUid >= USER_BAND && nUid < NPC_BAND)
{
CPoint ptCell;
pUser = m_pCom->GetUserUid(nUid - USER_BAND);
if(pUser == NULL || pUser->m_curz != m_Zone) return;
for(nX = -1; nX <= 1; nX++)
{
for(nY = -1; nY <= 1; nY++)
{
cx = pt.x + nX;
cy = pt.y + nY;
if(cx < 0 || cx >= m_sizeCell.cx) continue;
if(cy < 0 || cy >= m_sizeCell.cy) continue;
m_pCell[cx][cy].Get(arUid);
nSize = arUid.GetSize();
m_pCell[cx][cy].GetItem(arCellItem); // 甘惑俊辑狼 酒捞袍 困摹甫...
iSize = arCellItem.GetSize(); // 茄 伎俊辑狼 酒捞袍 困摹 荐
for(i = 0; i < iSize; i++) // 葛电 酒捞袍 困摹沥焊甫 焊辰促.
{
dwDN = 0;
ptCell = arCellItem[i];
if(!GetViewMapItemList(arMapItem, dwDN, ptCell, pUser->m_ZoneIndex)) continue;
if(dwDN > 0) pUser->GetMapMoneyInfo(dwDN, ptCell);
// if(arMapItem.GetSize() > 0) pUser->GetMapItemInfo(arMapItem, ptCell, -1, ITEM_INFO_MODIFY);
arMapItem.RemoveAll();
}
for(i = 0; i < nSize; i++)
{
nUid = arUid.GetAt(i);
if(nUid >= USER_BAND && nUid < NPC_BAND)
{
pSendUser = m_pCom->GetUserUid(nUid - USER_BAND);
if(pSendUser == NULL || pSendUser->m_state != STATE_GAMESTARTED || pSendUser->m_curz != m_Zone) continue;
pSendUser->SendUserInfo(pUser, INFO_MODIFY); // Other <- Me
pUser->SendUserInfo(pSendUser, INFO_MODIFY); // Other -> Me
}
if(nUid >= NPC_BAND)
{
pNpc = pUser->GetNpc(nUid - NPC_BAND);
if(!pNpc) continue;
pUser->SendNpcInfo(pNpc, INFO_MODIFY);
}
}
}
}
}
else if(nUid >= NPC_BAND)
{
pNpc = GetNpc(nUid - NPC_BAND);
if(pNpc == NULL) return;
for(nX = -1; nX <= 1; nX++)
{
for(nY = -1; nY <= 1; nY++)
{
cx = pt.x + nX;
cy = pt.y + nY;
if(cx < 0 || cx >= m_sizeCell.cx) continue;
if(cy < 0 || cy >= m_sizeCell.cy) continue;
m_pCell[cx][cy].Get(arUid);
nSize = arUid.GetSize();
for(i = 0; i < nSize; i++)
{
nUid = arUid.GetAt(i);
if(nUid >= USER_BAND && nUid < NPC_BAND)
{
pSendUser = m_pCom->GetUserUid(nUid - USER_BAND);
if(pSendUser == NULL || pSendUser->m_state != STATE_GAMESTARTED || pSendUser->m_curz != m_Zone) continue;
pSendUser->SendNpcInfo(pNpc, INFO_MODIFY);
// TRACE("Add Cell NPC %d\n", pNpc->m_sNid);
}
}
}
}
}
arMapItem.RemoveAll();
arUid.RemoveAll();
}
////////////////////////////////////////////////////////////////////////
// Cell 俊辑 蜡历唱 NPC甫 昏力茄促.
//
void CELL::DeleteCell(CPoint pt, int nUid)
{
// Add User ID to Cell and Copy Cell Array
CCellItemArray arCellItem;
ItemInfoArray arMapItem;
arMapItem.RemoveAll();
CCellUidArray arUid;
m_pCell[pt.x][pt.y].Delete(nUid);
USER* pUser = NULL;
USER* pSendUser = NULL;
CNpc* pNpc = NULL;
int nSize = 0, iSize = 0;
int i, nX, nY;
int cx, cy;
BOOL bZoneChanging = FALSE;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -