www.pudn.com > GameEngine_src.rar > CItem.cpp


// CItem.cpp: implementation of the CItem class. 
// 
////////////////////////////////////////////////////////////////////// 
 
#include "CItem.h" 
#include "normal.h" 
#include "CEasyDraw.h" 
#include "CMessageManager.h" 
#include "CMap.h" 
#include "CItemFactory.h" 
#include "CDInput.h" 
#include "CHero.h" 
#include  
 
extern PEASYDRAW	g_pEasyDraw; 
extern CMap			theMap; 
extern CMessageManager theMsgMgr; 
extern CItemFactory	theItemFactory; 
extern CDIMouse		theMouse; 
extern CHero		theHero; 
 
 
const int BOX_CELL_WIDTH = 29; 
const int BOX_CELL_HEIGHT= 29; 
const BYTE BOX_HOLD_MASK = 0x01; 
 
 
 
////////////////////////////////////////////////////////////////////// 
// 
////////////////////////////////////////////////////////////////////// 
ITEM_INFO::ITEM_INFO() 
{ 
	m_ItemType = IT_ALL; 
	m_CmdNum = 0; 
	m_CmdBuf = NULL; 
 
	m_GroundSurface=0; 
	m_BoxSurface  = 0; 
	m_szInfoBuf = 0; 
} 
 
////////////////////////////////////////////////////////////////////// 
// 
////////////////////////////////////////////////////////////////////// 
ITEM_INFO::~ITEM_INFO() 
{ 
	SafeDeleteArray( m_CmdBuf ); 
	SafeDeleteArray( m_szInfoBuf ); 
	g_pEasyDraw->DeleteSurface( m_GroundSurface ); 
	g_pEasyDraw->DeleteSurface( m_BoxSurface ); 
 
	m_GroundSurface=0; 
	m_BoxSurface  = 0; 
} 
 
////////////////////////////////////////////////////////////////////// 
// 
////////////////////////////////////////////////////////////////////// 
CItem::CItem() 
{ 
	m_SpriteType = ST_ITEM; 
	m_Life = 0;					//生命大于0,表示在地面上,不在箱子里 
	m_NumAttribute = 0; 
} 
 
CItem::~CItem() 
{ 
 
} 
 
////////////////////////////////////////////////////////////////////// 
// 
////////////////////////////////////////////////////////////////////// 
bool CItem::Init( ITEM_INFO *pInfo ) 
{ 
	m_pItemInfo = pInfo; 
	m_UseTime = (short)pInfo->m_ItemUseTime; 
	m_Money = pInfo->m_ItemPrice; 
	m_CmdPool.AddCmd( pInfo->m_CmdBuf, pInfo->m_CmdNum ); 
 
	if ( pInfo->m_ItemType >= IT_ARMOR && pInfo->m_ItemType <= IT_SHIELD ) 
	{ 
		m_NumAttribute = m_CmdPool.GetCmdNum() - 2; 
	} 
 
	return true; 
} 
 
////////////////////////////////////////////////////////////////////// 
// 
////////////////////////////////////////////////////////////////////// 
bool CItem::Init( HERO_ITEM &heroItem ) 
{ 
	m_pItemInfo = NULL; 
	m_UseTime	= heroItem.useTime; 
	m_Money		= heroItem.money; 
 
	m_NumAttribute = heroItem.numCmd - 2; 
 
	OnPickUp( heroItem.rect ); 
	return true; 
} 
 
////////////////////////////////////////////////////////////////////// 
//更新在地面上的物品 
////////////////////////////////////////////////////////////////////// 
bool CItem::UpdateSprite() 
{ 
	unsigned int ThisTick = timeGetTime(); 
	if ( ThisTick - m_LastTick > 60000 ) 
	{ 
		//自我删除 
		theMap.NoHoldCell( m_BasePoint.y / CELL_HEIGHT, m_BasePoint.x /CELL_WIDTH, ITEM_MASK ); 
		theItemFactory.DeleteItem( this ); 
		return false; 
	} 
 
	if ( m_Life == 0 )			//如果生命为0,表示被捡起放到箱子里 
	{ 
		theMap.NoHoldCell( m_RenderPoint.y / CELL_HEIGHT, m_RenderPoint.x /CELL_WIDTH, ITEM_MASK ); 
		return false; 
	} 
 
	int w = m_pItemInfo->m_GroundSurface->GetWidth(); 
	int h = m_pItemInfo->m_GroundSurface->GetHeight(); 
 
	m_RenderPoint.x = m_BasePoint.x + (CELL_WIDTH/2) - (w/2); 
	m_RenderPoint.y = m_BasePoint.y + (CELL_HEIGHT/2)- (h/2); 
 
	m_Rect.left = m_RenderPoint.x; 
	m_Rect.top	= m_RenderPoint.y; 
	m_Rect.right= m_RenderPoint.x + w; 
	m_Rect.bottom=m_RenderPoint.y + h; 
 
	theMap.MapToClient( m_RenderPoint ); 
	return true; 
} 
 
////////////////////////////////////////////////////////////////////// 
//绘制地面上的物品 
////////////////////////////////////////////////////////////////////// 
void CItem::DrawSprite() 
{ 
	if ( m_bSelected ) 
	{ 
		m_pItemInfo->m_GroundSurface->DrawContour( m_RenderPoint.x, m_RenderPoint.y ); 
		theItemFactory.DrawName( m_pItemInfo, m_NumAttribute, m_RenderPoint.x, m_RenderPoint.y ); 
		m_bSelected = false; 
	} 
	else 
		m_pItemInfo->m_GroundSurface->DrawAutoClip( m_RenderPoint.x, m_RenderPoint.y ); 
} 
 
////////////////////////////////////////////////////////////////////// 
//绘制箱子里的物品, box_x, box_y 是物品栏左上角坐标 
////////////////////////////////////////////////////////////////////// 
void CItem::DrawInBox( int box_x, int box_y ) 
{ 
	int w = m_pItemInfo->m_BoxSurface->GetWidth(); 
	int h = m_pItemInfo->m_BoxSurface->GetHeight(); 
 
	m_RenderPoint.x = m_BasePoint.x - w / 2; 
	m_RenderPoint.y = m_BasePoint.y - h/ 2; 
	//从物品栏局部坐标转换为窗口客户区坐标 
	m_RenderPoint.x += box_x; 
	m_RenderPoint.y += box_y; 
 
	if ( m_bSelected ) 
	{ 
		m_pItemInfo->m_BoxSurface->DrawContour( m_RenderPoint.x, m_RenderPoint.y ); 
		//调用物品工厂函数绘制物品信息 
		theItemFactory.DrawInfo( this, m_CmdPool.GetCmdBuf(), m_CmdPool.GetCmdNum(), 
								m_RenderPoint.x+w/2, m_RenderPoint.y+h/2 ); 
		m_bSelected = false; 
	} 
	else 
		m_pItemInfo->m_BoxSurface->DrawSurface( m_RenderPoint.x, m_RenderPoint.y, 0 ); 
} 
 
////////////////////////////////////////////////////////////////////// 
//绘制在鼠标上的物品 
////////////////////////////////////////////////////////////////////// 
void CItem::DrawOnPick( int mx, int my ) 
{ 
	m_RenderPoint.x = mx - m_pItemInfo->m_BoxSurface->GetWidth() / 2; 
	m_RenderPoint.y = my - m_pItemInfo->m_BoxSurface->GetHeight() / 2; 
 
	m_pItemInfo->m_BoxSurface->DrawContour( m_RenderPoint.x, m_RenderPoint.y ); 
} 
 
 
////////////////////////////////////////////////////////////////////// 
//掉落到地面 
////////////////////////////////////////////////////////////////////// 
void CItem::OnDropDown( const POINT &base ) 
{ 
	m_LastTick = timeGetTime();					//掉落到地面时,开始计时 
	m_BasePoint = base; 
	m_Life = 100; 
	theMap.HoldCell( m_BasePoint.y / CELL_HEIGHT, m_BasePoint.x /CELL_WIDTH, ITEM_MASK ); 
	CalcPriority(); 
} 
 
////////////////////////////////////////////////////////////////////// 
//从地面上拾起到箱子, 
////////////////////////////////////////////////////////////////////// 
void CItem::OnPickUp( int x, int y ) 
{ 
	//m_RenderPoint = m_BasePoint; 
	m_Life = 0;						//捡入箱子后生命为0 
 
	//更新包围矩形为所占据的格子的矩形 
	m_Rect.left = x; 
	m_Rect.top  = y; 
	m_Rect.right= x + BOX_CELL_WIDTH; 
	m_Rect.bottom=y + BOX_CELL_HEIGHT; 
 
	if ( m_pItemInfo->m_HoldType == IHT_2x2 ) 
	{ 
		m_Rect.right += BOX_CELL_WIDTH; 
		m_Rect.bottom+= BOX_CELL_HEIGHT; 
	} 
	else if ( m_pItemInfo->m_HoldType == IHT_2x1 ) 
	{ 
		m_Rect.bottom+= BOX_CELL_HEIGHT; 
	} 
	else if ( m_pItemInfo->m_HoldType == IHT_1x2 ) 
	{ 
		m_Rect.right += BOX_CELL_WIDTH; 
	} 
	else if ( m_pItemInfo->m_HoldType == IHT_3x1 ) 
	{ 
		m_Rect.bottom += BOX_CELL_HEIGHT * 2; 
	} 
	else if ( m_pItemInfo->m_HoldType == IHT_3x2 ) 
	{ 
		m_Rect.bottom += BOX_CELL_HEIGHT * 2; 
		m_Rect.right += BOX_CELL_WIDTH; 
	} 
 
	//m_BasePoint为物品所在CELL的中心点 
	m_BasePoint.x = ( m_Rect.left + m_Rect.right ) / 2; 
	m_BasePoint.y = ( m_Rect.top + m_Rect.bottom ) / 2; 
} 
 
////////////////////////////////////////////////////////////////////// 
//拾起到装备栏中 
////////////////////////////////////////////////////////////////////// 
void CItem::OnPickUp( const RECT &rc ) 
{ 
	m_Rect.left		= rc.left; 
	m_Rect.right	= rc.right; 
	m_Rect.top		= rc.top; 
	m_Rect.bottom	= rc.bottom; 
 
	m_BasePoint.x = ( m_Rect.left + m_Rect.right ) / 2; 
	m_BasePoint.y = ( m_Rect.top + m_Rect.bottom ) / 2; 
 
	m_Life = 0; 
} 
 
////////////////////////////////////////////////////////////////////// 
//被使用 
////////////////////////////////////////////////////////////////////// 
bool CItem::OnUsed() 
{ 
	m_CmdPool.SetObj( this ); 
	m_CmdPool.RunCmdAll(); 
 
	if ( m_UseTime < 1 ) 
		return false; 
 
	return true; 
} 
 
////////////////////////////////////////////////////////////////////// 
//增加附加属性 
////////////////////////////////////////////////////////////////////// 
void CItem::AddAttribute( const ES_CMD &cmd ) 
{ 
	m_CmdPool.AddCmd( cmd ); 
	m_NumAttribute++; 
} 
 
////////////////////////////////////////////////////////////////////// 
// 
////////////////////////////////////////////////////////////////////// 
void CItem::SetItemInfo( ITEM_INFO *pInfo ) 
{ 
	m_pItemInfo = pInfo; 
	if ( pInfo != NULL ) 
	{ 
		if ( pInfo->m_ItemType >= IT_ARMOR && pInfo->m_ItemType <= IT_SHIELD ) 
			m_NumAttribute = m_CmdPool.GetCmdNum() - 2; 
		else 
			m_NumAttribute = 0; 
	} 
} 
 
//**********************************CItemBox实现**************************************************** 
CItem * CItemBox::s_pCurPickItem; 
bool	CItemBox::s_bMouseMsg; 
 
//装备栏的全部格子 
CELL_INFO CItemBox::s_CellInfoArray[18] = {			{{94,18,153,78}, IT_HAT, 0},  
{{63,82,92,112}, IT_EARRING, 0}, {{109,82,138,112}, IT_NECKLACE, 0}, {{155,82,184,112}, IT_EARRING, 0},  
{{22,120,81,207}, IT_WEAPON, 0}, {{95,120,154,207}, IT_ARMOR, 0}, {{167,120,226,207}, IT_SHIELD, 0},  
{{22,214,51,243}, IT_BANGLE, 0}, {{196,213,225,243}, IT_BANGLE, 0}, {{61,214,90,244}, IT_RING, 0}, 
{{94,214,123,244}, IT_RING, 0}, {{126,214,155,244}, IT_RING, 0}, {{159,214,188,244}, IT_RING, 0}, 
{{62,249,91,279}, IT_AMULET, 0}, {{95,249,124,279}, IT_AMULET, 0}, {{127,249,156,279}, IT_AMULET, 0}, 
{{160,249,189,279}, IT_AMULET, 0}, {{94,285,153,345}, IT_SHOES, 0}		}; 
 
 
////////////////////////////////////////////////////////////////////// 
// 
////////////////////////////////////////////////////////////////////// 
CItemBox::CItemBox() 
{ 
	m_Width = 0; 
	m_Height= 0; 
	m_CellArray = NULL; 
	m_BoxSurface = NULL; 
	m_IsOpen = false; 
	m_pCurSelItem = NULL; 
	m_MoneySurface = NULL; 
 
	m_CloseButtonRect.left	= 0; 
	m_CloseButtonRect.top	= 0; 
	m_CloseButtonRect.right	= 0; 
	m_CloseButtonRect.bottom= 0; 
} 
 
 
////////////////////////////////////////////////////////////////////// 
// 
////////////////////////////////////////////////////////////////////// 
CItemBox::~CItemBox() 
{ 
} 
 
////////////////////////////////////////////////////////////////////// 
//x,y:箱子初始化位置(屏幕坐标) 
//w,h:箱子的格子数 
////////////////////////////////////////////////////////////////////// 
bool CItemBox::Init( int x, int y, int w, int h, PSURFACE boxSurface, BOX_TYPE type ) 
{ 
	m_CellArray = new BYTE[w*h]; 
	m_BoxType = type; 
	if ( m_CellArray == NULL ) 
		assert(false); 
	ZeroMemory( m_CellArray, w*h ); 
 
	m_BoxSurface = boxSurface; 
	if ( m_BoxSurface == NULL ) 
		assert(false); 
 
	if ( type == ITEM_BAG )				//如果是背包,还要初始化一个金钱页面 
	{ 
		int ch = g_pEasyDraw->g_CharHeight; 
		int cw = g_pEasyDraw->g_CharWidth * 10; 
		m_MoneySurface = g_pEasyDraw->CreateSurfaceEx( NULL, cw, ch, true, true, 0 ); 
		debug_assert( m_MoneySurface != NULL ); 
	} 
 
	m_Rect.left = x; 
	m_Rect.top = y; 
	m_Rect.right= x+m_BoxSurface->GetWidth(); 
	m_Rect.bottom=y+m_BoxSurface->GetHeight(); 
 
	m_Width = w; 
	m_Height= h; 
 
	return m_ItemPointerPool.Init( 128 ); 
} 
 
////////////////////////////////////////////////////////////////////// 
// 
////////////////////////////////////////////////////////////////////// 
void CItemBox::Free() 
{ 
	SafeDeleteArray( m_CellArray ); 
	if ( m_BoxType != ITEM_SHOP )				//商店与保管箱共用一个页面 
		g_pEasyDraw->DeleteSurface( m_BoxSurface ); 
	m_ItemPointerPool.Free(); 
 
	if ( m_MoneySurface != NULL ) 
	{ 
		g_pEasyDraw->DeleteSurface( m_MoneySurface ); 
		m_MoneySurface = NULL; 
	} 
} 
 
////////////////////////////////////////////////////////////////////// 
// 
////////////////////////////////////////////////////////////////////// 
int CItemBox::HandleMouseMsg() 
{ 
	if ( !m_IsOpen || !s_bMouseMsg )	return 0; 
 
	int mx = theMouse.GetX(); 
	int my = theMouse.GetY(); 
	m_pCurSelItem = NULL; 
 
	if ( mx > m_Rect.left && mx < m_Rect.right ) 
	{ 
		if ( my > m_Rect.top && my < m_Rect.bottom ) 
		{ 
			mx -= m_Rect.left; 
			my -= m_Rect.top; 
			for ( m_ItemPointerPool.Begin(); m_ItemPointerPool.IsNotNull(); m_ItemPointerPool.MoveNext() ) 
			{ 
				if ( (*m_ItemPointerPool.GetCur())->IsSelected( mx, my ) ) 
				{ 
					m_pCurSelItem = (*m_ItemPointerPool.GetCur());		//决定当前指针指向的物品 
					break; 
				} 
			} 
 
			if ( theMouse.IsLRelease() ) 
			{ 
				if ( m_pCurSelItem != NULL && s_pCurPickItem == NULL )	//从物品栏中拿起一件物品	 
				{ 
					if ( m_BoxType == ITEM_SHOP )				//如果是商店,拿起一件物品就要扣钱 
					{ 
						if ( theHero.ChangeMoney( -(m_pCurSelItem->GetMoney()) ) == false ) 
						{ 
							theMsgMgr.AddMessage( "金钱不足" ); 
							s_bMouseMsg = false; 
							return 1; 
						} 
					} 
					s_pCurPickItem = m_pCurSelItem; 
					DeleteItemHelp( m_pCurSelItem ); 
				} 
				else if ( s_pCurPickItem != NULL )		//放进一件物品 
				{ 
					if ( m_BoxType == ITEM_EQUIPMENT )	//如果是装备栏则一个个格子判断 
					{ 
						for ( int i = 0; i < 18; ++i ) 
						{ 
							RECT &rc = s_CellInfoArray[i].m_Rect;  
							if ( mx > rc.left && mx < rc.right && my > rc.top && my < rc.bottom ) 
							{ 
								if ( s_CellInfoArray[i].m_Type == s_pCurPickItem->GetItemType()  
									&& s_CellInfoArray[i].m_Hold == 0 ) 
								{ 
									if ( s_pCurPickItem->GetHeroClass() == theHero.GetHeroClass() || 
										 s_pCurPickItem->GetHeroClass() == SC_ALL ) 
									{ 
										if ( s_pCurPickItem->GetItemLevel() <= theHero.GetHeroLevel() ) 
										{ 
											s_pCurPickItem->OnPickUp( rc );				//把物品放入装备栏中 
											m_ItemPointerPool.Add( s_pCurPickItem ); 
											s_CellInfoArray[i].m_Hold = 1; 
											s_pCurPickItem = NULL; 
										} 
										else 
											theMsgMgr.AddMessage( "等级不够" ); 
									} 
									else 
										theMsgMgr.AddMessage( "职业不符" ); 
								} 
								break; 
							} 
						} 
					} 
					else 
					{ 
						if ( m_BoxType == ITEM_TROUGH )			//如果是快捷使用栏,必须是药品或技能卡才能放入 
						{ 
							ITEM_TYPE type = s_pCurPickItem->GetItemType(); 
							if ( type != IT_DRUG && type != IT_MAGE_CARD ) 
							{ 
								theMsgMgr.AddMessage( "无法操作" ); 
								s_bMouseMsg = false; 
								return 1; 
							} 
						} 
 
						int row = (my - s_pCurPickItem->GetHalfHeight()) / BOX_CELL_HEIGHT; 
						int col = (mx - s_pCurPickItem->GetHalfWidth()) / BOX_CELL_WIDTH; 
						if ( IsCellHold( row, col, s_pCurPickItem->GetHoldType() ) == false ) 
						{ 
							s_pCurPickItem->OnPickUp( col * BOX_CELL_WIDTH + 2, row * BOX_CELL_HEIGHT + 10 ); 
							m_ItemPointerPool.Add( s_pCurPickItem ); 
							HoldCell( row, col, s_pCurPickItem ->GetHoldType(), BOX_HOLD_MASK ); 
 
							if ( m_BoxType == ITEM_SHOP )	//如果是商店,放入物品就会加钱 
								theHero.ChangeMoney( s_pCurPickItem->GetMoney() ); 
							s_pCurPickItem = NULL; 
						} 
						else  
						{ 
							if ( s_pCurPickItem->GetItemType() == IT_STONE ) 
							{ 
								if ( s_pCurPickItem->OnUsed() == false ) 
								{ 
									theItemFactory.DeleteItem( s_pCurPickItem ); 
									s_pCurPickItem = NULL; 
								} 
							} 
						} 
					} 
				} 
				else						//关闭按钮 
				{ 
					if ( mx >= m_CloseButtonRect.left && mx < m_CloseButtonRect.right ) 
					{ 
						if ( my >= m_CloseButtonRect.top && my < m_CloseButtonRect.bottom ) 
							CloseBox(); 
					} 
				} 
			} 
			else if ( theMouse.IsRPress() && (int)m_BoxType < 2 ) 
			{ 
				UseItemHelp( m_pCurSelItem ); 
			} 
			else if ( theMouse.IsLPress() ) 
			{ 
				m_MouseOffset.x = theMouse.GetX() - m_Rect.left; 
				m_MouseOffset.y = theMouse.GetY() - m_Rect.top; 
			} 
			else if ( theMouse.IsLDown() ) 
			{ 
				MoveBox( theMouse.GetX()-m_MouseOffset.x, theMouse.GetY()-m_MouseOffset.y ); 
			} 
			 
			s_bMouseMsg = false; 
			return 1; 
		} 
	} 
 
	if ( s_pCurPickItem != NULL && m_BoxType == ITEM_BAG ) 
	{ 
		if ( theMouse.IsLRelease() ) 
		{ 
			POINT p = {theMouse.GetX(),theMouse.GetY()}; 
			theMap.ClientToMap( p ); 
			p.x = p.x / CELL_WIDTH * CELL_WIDTH; 
			p.y = p.y / CELL_HEIGHT* CELL_HEIGHT; 
 
			s_pCurPickItem->OnDropDown( p ); 
			if ( theMap.AddItem( s_pCurPickItem ) == false ) 
				theItemFactory.DeleteItem( s_pCurPickItem ); 
 
			s_pCurPickItem = NULL; 
		} 
		return 1; 
	} 
 
	return 0; 
} 
 
 
////////////////////////////////////////////////////////////////////// 
// 
////////////////////////////////////////////////////////////////////// 
void CItemBox::Draw() 
{ 
	if ( m_IsOpen ) 
	{ 
		m_BoxSurface->DrawAutoClip( m_Rect.left, m_Rect.top ); 
		//绘制金钱数量,只用在背包中 
		if ( m_MoneySurface != NULL ) 
		{ 
			char sz[16]; 
			wsprintf( sz, "%d", theHero.GetMoney() ); 
			m_MoneySurface->ClearSurface( 0, 0 ); 
			m_MoneySurface->BeginDrawText( 0, 0 ); 
			m_MoneySurface->DrawText( sz, 0xffffff, false ); 
			m_MoneySurface->EndDrawText(); 
			m_MoneySurface->DrawSurface( m_Rect.left + 101, m_Rect.top + 198, 0 ); 
		} 
 
		for ( m_ItemPointerPool.Begin(); m_ItemPointerPool.IsNotNull(); m_ItemPointerPool.MoveNext() ) 
		{ 
			(*m_ItemPointerPool.GetCur())->DrawInBox( m_Rect.left, m_Rect.top ); 
		} 
 
		if ( m_pCurSelItem ) 
		{ 
			m_pCurSelItem->EnableSelected(); 
			m_pCurSelItem->DrawInBox( m_Rect.left, m_Rect.top ); 
		} 
		if ( s_pCurPickItem ) 
			s_pCurPickItem->DrawOnPick( theMouse.GetX(), theMouse.GetY() ); 
	} 
} 
 
////////////////////////////////////////////////////////////////////// 
// 
////////////////////////////////////////////////////////////////////// 
BYTE CItemBox::GetCell( int row, int col ) 
{ 
	if ( row >= 0 && row < m_Height ) 
	{ 
		if ( col >= 0 && col < m_Width ) 
		{ 
			return m_CellArray[row * m_Width + col]; 
		} 
	} 
 
	return BOX_HOLD_MASK; 
} 
 
////////////////////////////////////////////////////////////////////// 
// 
////////////////////////////////////////////////////////////////////// 
bool CItemBox::IsCellHold( int row, int col, enum ITEM_HOLD_TYPE HoldType ) 
{ 
	//CELL c[3] = { CELL(row,col+1), CELL(row+1,col), CELL(row+1,col+1) }; 
	BYTE mask = GetCell( row, col ); 
 
	switch ( HoldType ) 
	{ 
	case IHT_2x2: 
		mask |= GetCell( row, col+1 ); 
		mask |= GetCell( row+1, col ); 
		mask |= GetCell( row+1, col+1 ); 
		break; 
 
	case IHT_2x1: 
		mask |= GetCell( row+1, col ); 
		break; 
 
	case IHT_1x2: 
		mask |= GetCell( row, col+1 ); 
		break; 
 
	case IHT_3x1: 
		mask |= GetCell( row+1, col ); 
		mask |= GetCell( row+2, col ); 
		break; 
 
	case IHT_3x2: 
		mask |= GetCell( row, col+1 ); 
		mask |= GetCell( row+1, col ); 
		mask |= GetCell( row+1, col+1 ); 
		mask |= GetCell( row+2, col ); 
		mask |= GetCell( row+2, col+1 ); 
		break; 
	} 
 
	if ( mask & BOX_HOLD_MASK ) 
		return true; 
	return false; 
} 
 
////////////////////////////////////////////////////////////////////// 
// 
////////////////////////////////////////////////////////////////////// 
void CItemBox::HoldCell( int row, int col, ITEM_HOLD_TYPE HoldType, BYTE mask ) 
{ 
	if ( m_BoxType == ITEM_EQUIPMENT )	return; 
 
	int index[3] = { row*m_Width+(col+1), (row+1)*m_Width+col, (row+1)*m_Width+(col+1) }; 
 
	m_CellArray[ row * m_Width + col ] = mask; 
 
	switch ( HoldType ) 
	{ 
	case IHT_2x2: 
		m_CellArray[ index[0] ] = mask; 
		m_CellArray[ index[1] ] = mask; 
		m_CellArray[ index[2] ] = mask; 
		break; 
 
	case IHT_2x1: 
		m_CellArray[ index[1] ] = mask; 
		break; 
 
	case IHT_1x2: 
		m_CellArray[ index[0] ] = mask; 
		break; 
 
	case IHT_3x1: 
		m_CellArray[ index[1] ] = mask; 
		m_CellArray[ (row+2)*m_Width+col ] = mask; 
		break; 
 
	case IHT_3x2: 
		m_CellArray[ index[0] ] = mask; 
		m_CellArray[ index[1] ] = mask; 
		m_CellArray[ index[2] ] = mask; 
		m_CellArray[ (row+2)*m_Width+col ] = mask; 
		m_CellArray[ (row+2)*m_Width+col+1 ] = mask; 
		break; 
	} 
} 
 
////////////////////////////////////////////////////////////////////// 
// 
////////////////////////////////////////////////////////////////////// 
bool CItemBox::AddItem( CItem *pItem ) 
{ 
	if ( pItem->GetItemType() == IT_GOLD ) 
	{ 
		theHero.ChangeMoney( pItem->GetMoney() ); 
		pItem->OnPickUp( 0, 0 ); 
		return true; 
	} 
		 
	for ( int i = 0; i < m_Height; ++i ) 
	{ 
		for ( int j = 0; j < m_Width; ++j ) 
		{ 
			if ( IsCellHold( i, j, pItem->GetHoldType() ) == false ) 
			{ 
				pItem->OnPickUp( j * BOX_CELL_WIDTH + 2, i * BOX_CELL_HEIGHT + 10 ); 
				m_ItemPointerPool.Add( pItem ); 
				HoldCell( i, j, pItem->GetHoldType(), BOX_HOLD_MASK ); 
				return true; 
			} 
		} 
	} 
 
	theMsgMgr.AddMessage( "背包已满" ); 
	return false; 
} 
 
////////////////////////////////////////////////////////////////////// 
// 
////////////////////////////////////////////////////////////////////// 
void CItemBox::AddItem( CItem *pItem, int x, int y, ITEM_HOLD_TYPE holdType ) 
{ 
	if ( m_BoxType != ITEM_EQUIPMENT ) 
	{ 
		int col = (x - 2) / BOX_CELL_WIDTH; 
		int row = (y - 10)/ BOX_CELL_HEIGHT; 
 
		m_ItemPointerPool.Add( pItem ); 
		HoldCell( row, col, holdType, BOX_HOLD_MASK ); 
	} 
	else 
	{ 
		for ( int i = 0; i < 18; ++i ) 
		{ 
			if ( s_CellInfoArray[i].m_Rect.left == x && s_CellInfoArray[i].m_Rect.top == y ) 
			{ 
				s_CellInfoArray[i].m_Hold = 1; 
				m_ItemPointerPool.Add( pItem ); 
				break; 
			} 
		} 
	} 
} 
 
////////////////////////////////////////////////////////////////////// 
// 
////////////////////////////////////////////////////////////////////// 
void CItemBox::MoveBox( int x, int y ) 
{ 
	RECT rect; 
	rect.left	= x; 
	rect.top	= y; 
	rect.right	= x+m_BoxSurface->GetWidth(); 
	rect.bottom	= y+m_BoxSurface->GetHeight(); 
	if ( rect.left >= 0 && rect.right < g_pEasyDraw->GetDeviceWidth() ) 
	{ 
		if ( rect.top >= 0 && rect.bottom < g_pEasyDraw->GetDeviceHeight() ) 
			m_Rect = rect; 
	} 
} 
 
////////////////////////////////////////////////////////////////////// 
// 
////////////////////////////////////////////////////////////////////// 
void CItemBox::DeleteItemHelp( CItem *pItem ) 
{ 
	int row = pItem->GetRect().top; 
	int col = pItem->GetRect().left;	 
	if ( m_BoxType == ITEM_EQUIPMENT ) 
	{ 
		for ( int i = 0; i < 18; ++i ) 
		{ 
			CELL_INFO &ci = s_CellInfoArray[i]; 
			if ( ci.m_Rect.left == col && ci.m_Rect.top == row ) 
			{ 
				ci.m_Hold = 0; 
				break; 
			} 
		} 
	} 
	else 
	{ 
		row = row / BOX_CELL_HEIGHT; 
		col = col / BOX_CELL_WIDTH;	 
		HoldCell( row, col, pItem->GetHoldType(), 0x00 ); 
	} 
	m_ItemPointerPool.Delete( pItem ); 
} 
 
////////////////////////////////////////////////////////////////////// 
// 
////////////////////////////////////////////////////////////////////// 
void CItemBox::UseItemHelp( CItem *pItem ) 
{ 
	if ( pItem != NULL ) 
	{ 
		if ( pItem->GetItemType() >= IT_DRUG && pItem->GetItemType() <= IT_STONE ) 
		{ 
			if ( pItem->GetHeroClass() == SC_ALL || pItem->GetHeroClass() == theHero.GetHeroClass() ) 
			{ 
				if ( pItem->GetItemLevel() <= theHero.GetHeroLevel() ) 
				{ 
					if ( pItem->OnUsed() == false ) 
					{ 
						DeleteItemHelp( pItem ); 
						theItemFactory.DeleteItem( pItem ); 
					} 
				} 
				else 
					theMsgMgr.AddMessage( "等级不够" ); 
			} 
			else 
				theMsgMgr.AddMessage( "职业不符" ); 
		} 
	} 
} 
 
////////////////////////////////////////////////////////////////////// 
// 
////////////////////////////////////////////////////////////////////// 
void CItemBox::UseItem( int row, int col ) 
{ 
	POINT p = { col * BOX_CELL_WIDTH + 2, row * BOX_CELL_HEIGHT + 10 };		//加2,10是箱子页面偏移 
 
	for ( m_ItemPointerPool.Begin(); m_ItemPointerPool.IsNotNull(); m_ItemPointerPool.MoveNext() ) 
	{ 
		CItem *pItem = (*m_ItemPointerPool.GetCur()); 
		if ( p.x == pItem->GetRect().left && p.y == pItem->GetRect().top ) 
		{ 
			UseItemHelp( pItem ); 
			break; 
		} 
	} 
} 
 
////////////////////////////////////////////////////////////////////// 
// 
////////////////////////////////////////////////////////////////////// 
void CItemBox::OnCloseEquipment() 
{ 
	if ( m_BoxType == ITEM_EQUIPMENT )	//关闭装备栏时,重新计算英雄属性 
	{ 
		theHero.ClearItemInfo(); 
		for ( m_ItemPointerPool.Begin(); m_ItemPointerPool.IsNotNull(); m_ItemPointerPool.MoveNext() ) 
		{ 
			CItem *pItem = (*m_ItemPointerPool.GetCur()); 
			pItem->OnUsed(); 
		} 
		theHero.CalcHeroInfo(); 
	} 
} 
 
////////////////////////////////////////////////////////////////////// 
// 
////////////////////////////////////////////////////////////////////// 
void CItemBox::CloseBox() 
{ 
 
	OnCloseEquipment(); 
	if ( m_BoxType == ITEM_SHOP )	//关闭商店时,删除里面所有的商品 
	{ 
		for ( m_ItemPointerPool.Begin(); m_ItemPointerPool.IsNotNull(); m_ItemPointerPool.MoveNext() ) 
		{ 
			CItem *pItem = (*m_ItemPointerPool.GetCur()); 
			theItemFactory.DeleteItem( pItem ); 
			m_ItemPointerPool.FreeCur(); 
		} 
		ZeroMemory( m_CellArray, m_Width*m_Height ); 
	} 
	m_IsOpen = false; 
} 
 
////////////////////////////////////////////////////////////////////// 
// 
////////////////////////////////////////////////////////////////////// 
bool CItemBox::UpdateEquipment( int rate ) 
{ 
	bool re = false; 
	if ( m_pCurSelItem != NULL && 
		m_pCurSelItem->GetItemType() >= IT_ARMOR && 
		m_pCurSelItem->GetItemType() <= IT_SHIELD && 
		m_pCurSelItem->GetNumAttribute() > 0 ) 
	{ 
		srand( timeGetTime() ); 
		int rand_num = rand() % ( 128 + m_pCurSelItem->GetItemLevel() ); 
		CES_CmdPool &cmdPool = m_pCurSelItem->GetCmdPool(); 
		ES_CMD *cmdBuf = cmdPool.GetCmdBuf(); 
		int &n = cmdBuf[cmdPool.GetCmdNum() - 1].param_2; 
 
		if ( n >= 10 )  
			return false; 
 
		rate -= n * 4; 
		if ( rate < 0 ) rate = 0; 
 
		if ( rand_num <= rate ) 
		{ 
			++n;						//升级成功 
			theMsgMgr.AddMessage( "升级成功" ); 
		} 
		else 
		{	 
			theMsgMgr.AddMessage( "升级失败" ); 
			if ( n > 4 ) 
			{ 
				rand_num %= 128; 
				if ( rand_num > 64 ) 
					--n; 
			} 
		} 
 
		re = true; 
	} 
	return re; 
} 
 
////////////////////////////////////////////////////////////////////// 
//给物品增加附加属性 
////////////////////////////////////////////////////////////////////// 
bool CItemBox::AddItemAttribute( int index ) 
{ 
	bool re = false; 
	if ( m_pCurSelItem != NULL && 
		m_pCurSelItem->GetItemType() >= IT_ARMOR && 
		m_pCurSelItem->GetItemType() <= IT_SHIELD && 
		m_pCurSelItem->GetNumAttribute() < 3 ) 
	{ 
		srand( timeGetTime() ); 
		int rand_num = rand() % ( 128 + m_pCurSelItem->GetItemLevel() ); 
		int rate = m_pCurSelItem->GetNumAttribute() * 32; 
 
		if ( rand_num >= rate ) 
		{ 
			ES_CMD cmd; 
			cmd.cmd_id = ADD_HERO_INFO; 
			cmd.param_1 = index; 
			cmd.param_2 = 1; 
			m_pCurSelItem->AddAttribute( cmd ); 
			theMsgMgr.AddMessage( "合成成功" ); 
		} 
		else 
			theMsgMgr.AddMessage( "合成失败" ); 
		re = true; 
	} 
	return re; 
}