//****************************************************************************
//Copyright (C) 2005-2006 Beijing BlueDJ Technology Co.,Ltd. All rights reserved.

//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 (in the file LICENSE.GPL); if not, write to the Free Software
//Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

//Please visit http://www.bluedj.com for more infomation about us.
//Contact us at ggwizard@gmail.com or darkdong@gmail.com.
//****************************************************************************/

#include "DJGameRoom.h"
#include "DJGamePanel.h"
#include "DJGameDesktop.h"
#include "DJClientRequest.h"
#include "DJGameMJDesktop.h"
#include "DJGameTraceManager.h"

#include "protocol.h"
#include "mjrule.h"

unsigned char g_chMJImageCardOrder[] = {
    MAHJONG_CARD_WAN|1,MAHJONG_CARD_WAN|2,MAHJONG_CARD_WAN|3,
    MAHJONG_CARD_WAN|4,MAHJONG_CARD_WAN|5,MAHJONG_CARD_WAN|6,
    MAHJONG_CARD_WAN|7,MAHJONG_CARD_WAN|8,MAHJONG_CARD_WAN|9,
    
    MAHJONG_CARD_TIAO|1,MAHJONG_CARD_TIAO|2,MAHJONG_CARD_TIAO|3,
    MAHJONG_CARD_TIAO|4,MAHJONG_CARD_TIAO|5,MAHJONG_CARD_TIAO|6,
    MAHJONG_CARD_TIAO|7,MAHJONG_CARD_TIAO|8,MAHJONG_CARD_TIAO|9,
    
    MAHJONG_CARD_TONG|1,MAHJONG_CARD_TONG|2,MAHJONG_CARD_TONG|3,
    MAHJONG_CARD_TONG|4,MAHJONG_CARD_TONG|5,MAHJONG_CARD_TONG|6,
    MAHJONG_CARD_TONG|7,MAHJONG_CARD_TONG|8,MAHJONG_CARD_TONG|9,
    
    MAHJONG_CARD_ZI|MAHJONG_CARD_ZI_DONG,MAHJONG_CARD_ZI|MAHJONG_CARD_ZI_NAN,
    MAHJONG_CARD_ZI|MAHJONG_CARD_ZI_XI,MAHJONG_CARD_ZI|MAHJONG_CARD_ZI_BEI,
    
    MAHJONG_CARD_ZI|MAHJONG_CARD_ZI_ZHONG,MAHJONG_CARD_ZI|MAHJONG_CARD_ZI_FA,
    MAHJONG_CARD_ZI|MAHJONG_CARD_ZI_BAI,
    
    MAHJONG_CARD_ZI|MAHJONG_CARD_ZI_PLUM,MAHJONG_CARD_ZI|MAHJONG_CARD_ZI_ORCHID,
    MAHJONG_CARD_ZI|MAHJONG_CARD_ZI_BAMBOO,MAHJONG_CARD_ZI|MAHJONG_CARD_ZI_MUM,
    
    MAHJONG_CARD_ZI|MAHJONG_CARD_ZI_SPRING,MAHJONG_CARD_ZI|MAHJONG_CARD_ZI_SUMMER,
    MAHJONG_CARD_ZI|MAHJONG_CARD_ZI_AUTUMN,MAHJONG_CARD_ZI|MAHJONG_CARD_ZI_WINTER,
    0
};

////////////////////////////////////////////////////////////////////////////////////////
DJGameMJDesktop::DJGameMJDesktop(QWidget* parent,DJGamePanel *panel,quint32 w,quint32 h)
		: DJGameDesktop(parent,panel,w,h)
{
	DJGameRoom *room = panel->gameRoom();
	c_chTotalSeats = room->numberOfSeats();
	c_Panel = panel;
	c_chHuPages = 14;
	c_shHuMask = 0xFFFF;//默认支持所有胡法，mjrule.h中定义了HUMASK
	c_Fx = 1; 
#ifdef DJ_OEM	
	m_scaleOfWall 	= 0.6;
	m_scaleOfPool 	= 0.8;
	
	m_scaleOfHand 	<< 0 << 1.2 << 1 << 1 << 1;
	m_scaleOfBack 	<< 0 << 1.2 << 1 << 1 << 1;
	m_scaleOfAnGang	<< 0 << 1.17<< 1 << 1 << 1;
	
	m_heightOfLyingCard  << 0 << 17 << 17 << 17 << 17;
	m_heightOfStandingCard  << 0 << 48 << 48 << 48 << 48;
	m_heightOfWallCard  << 0 << 18 << 18 << 18 << 18;
#else 
	m_scaleOfWall 	= 1;
	m_scaleOfPool 	= 1.2;
	
	m_scaleOfHand 	<< 0 << 1.5 << 1 << 1 << 1;
	m_scaleOfBack 	<< 0 << 1.5 << 1 << 1 << 1;
	m_scaleOfAnGang	<< 0 << 1.17<< 1 << 1 << 1;
	
	m_heightOfLyingCard  << 0 << 12 << 12 << 12 << 12;
	m_heightOfStandingCard  << 0 << 26 << 26 << 26 << 26;
	m_heightOfWallCard  << 0 << 15 << 15 << 15 << 15;
#endif	
	m_styleOfPoolCards 	= 0;
}
DJGameMJDesktop::~DJGameMJDesktop()
{
	djDebug() << "DJGameMJDesktop destructor";
}
quint16 DJGameMJDesktop::setSupportHuStyle(quint16 shMask)
{
	quint16 sh = c_shHuMask;
	c_shHuMask = shMask;
	return sh;
}

void  DJGameMJDesktop::AdjustPlayerCards(quint8 seat,quint8 *pcards)
{
	int i=0,j=0;
	//quint8 chCards = c_chHuPages;
	memset(c_chPlayerCards[seat],0,20);

	i = 0;
	
	while(pcards[i] != 0 && j<20)
	{
		if(MAHJONG_ISDIY(pcards[i]))
		{
			c_chPlayerCards[seat][j] = pcards[i];
			j++;
		}
		i++;
	}
}
void DJGameMJDesktop::SelectCard(DJGameImageItem *item)
{
	Q3CanvasItemList list = canvas()->allItems();
    Q3CanvasItemList::Iterator it = list.begin();
	DJGameImageItem *imgitem;
    for (; it != list.end(); ++it)
	{
		if(*it && (*it)->rtti() == djgame_imageRTTI)
		{
			imgitem = (DJGameImageItem *)*it;
			if(imgitem->type() == DJGAME_DESKTOP_TYPE_HAND && imgitem->sid() == c_Panel->selfSeatId())
			{
				DJGameMahjongItem *mjitem = (DJGameMahjongItem*)imgitem;
				if(imgitem == item)
				{
					if ( mjitem->selected() )
						mjitem->setSelected(false);
					else
						mjitem->setSelected(true);
				}else
				{
					mjitem->setSelected(false);
				}
			}
		}
	}
	canvas()->update();
}

void DJGameMJDesktop::SelectCard(quint8 card)
{
	DJGameImageItem *item = FindImageItem(card,c_Panel->selfSeatId(),DJGAME_DESKTOP_TYPE_HAND);
	SelectCard(item);
}

bool DJGameMJDesktop::handleItemClicked( Qt::MouseButton btn,const QPoint& pos, Q3CanvasItem *item )
{
	if(c_Panel->isLookingOn())
		return false;
	if( btn == Qt::RightButton && c_bThrowFlag)
	{
		Q3CanvasItemList list = canvas()->allItems();
    	Q3CanvasItemList::Iterator it = list.begin();
		DJGameImageItem *imgitem;
    	for (; it != list.end(); ++it)
		{
			if(*it && (*it)->rtti() == djgame_imageRTTI)
			{
				imgitem = (DJGameImageItem *)*it;
				if(imgitem->type() == DJGAME_DESKTOP_TYPE_HAND && imgitem->sid() == c_Panel->selfSeatId())
				{
					DJGameMahjongItem *mjitem = (DJGameMahjongItem*)imgitem;
					if(mjitem->selected())
					{
						char buf[2];
						buf[0] = mjitem->tid();
						quint8 traceid = GetMahjongThrowTraceID(buf[0]);
						if(traceid != 0)
							SendGameTrace(traceid,buf,1,NULL);
						c_bThrowFlag = false;
						mjitem->setSelected(false);
						return true;
					}
				}
			}
		}
		char buf[2];
		buf[0] = c_chPlayerCards[c_Panel->selfSeatId()][c_chHuPages-1];
		quint8 traceid = GetMahjongThrowTraceID(buf[0]);
		if(traceid != 0)
			SendGameTrace(traceid,buf,1,NULL);
		c_bThrowFlag = false;
		return true;
	}
	if( btn == Qt::LeftButton && item && item->rtti() == djgame_imageRTTI)
	{
		DJGameImageItem* imgitem = (DJGameImageItem *)(item);
		if(imgitem->type() == DJGAME_DESKTOP_TYPE_HAND)
		{
			SelectCard(imgitem);
			return true;
		}
	} 
	return false;
}
bool DJGameMJDesktop::handleItemDoubleClicked( Qt::MouseButton btn,const QPoint& pos, Q3CanvasItem *item )
{
	if(c_Panel->isLookingOn())
		return false;
	if ( c_bThrowFlag && btn == Qt::LeftButton && item && item->rtti() == djgame_imageRTTI) {
		DJGameImageItem *imgitem 	= (DJGameImageItem *)item;
		if(imgitem->type() == DJGAME_DESKTOP_TYPE_HAND && imgitem->sid() == c_Panel->selfSeatId())
		{
			DJGameMahjongItem *mjitem = (DJGameMahjongItem*)imgitem;
			char buf[2];
			buf[0] = mjitem->tid();
			quint8 traceid = GetMahjongThrowTraceID(buf[0]);
			if(traceid != 0)
				SendGameTrace(traceid,buf,1,NULL);
			c_bThrowFlag = false;
			mjitem->setSelected(false);
			canvas()->update();
			return true;
		}
	}
	return false;
}
/*
void DJGameMJDesktop::contentsMouseDoubleClickEvent ( QMouseEvent * e )
{
	QPoint pp;
	QMatrix m = GetCurrentMatrix();
	pp.setX( (e->pos().x()-m.dx())/m.m11());
	pp.setY( (e->pos().y()-m.dy())/m.m22());
	
	Q3CanvasItemList l=canvas()->collisions(e->pos());
    Q3CanvasItemList::Iterator it=l.begin();
	DJGameImageItem *imgitem;
    if (it != l.end())
	{
		if((*it)->rtti() == djgame_imageRTTI)
		{
			imgitem = (DJGameImageItem *)(*it);
			if(imgitem->type() == DJGAME_DESKTOP_TYPE_HAND)
			{
				SelectCard(imgitem);
				HitItem(Qt::RightButton,pp,NULL);
			}
		}
	}
}
*/
quint8 DJGameMJDesktop::GetMahjongThrowTraceID(quint8 card)
{
	return 0;
}
void  DJGameMJDesktop::SetThrowFlag(bool b)
{
	c_bThrowFlag = b;
}
bool  DJGameMJDesktop::GetThrowFlag()
{
	return c_bThrowFlag;
}

void DJGameMJDesktop::StaticInitDesktop()
{
	for(int i=1;i<=4;i++)
	{
		InitializeNormalWrap(&c_playerNormalHu[i]);
		memset(c_chPlayerCards[i],0,20);
		c_bPrivate[i] = false;
		c_bPlayerTing[i] = false;
		memset(c_chPlayerFlower[i],0,10);
		
	}
	c_chPoolPages = 0;
	c_chWallRemoves = 0;
	c_chWallEndRemoves = 0;
	c_chWallRemoved = 0;
	c_chWallEndRemoved = 0;
	SetWallStartPoint((1<<5)+3);

	c_bGang = false;
	c_bThrowFlag = false;
	c_bHUStatus = true;
}

void DJGameMJDesktop::SetSeatPrivate(quint8 seat)
{//是否接收到私有数据
	if(seat >0 && seat <=4)
		c_bPrivate[seat] = true;
}
bool DJGameMJDesktop::GetSeatPrivate(quint8 seat)
{
	if(seat >0 && seat <=4)
		return c_bPrivate[seat];
	return false;
}
void DJGameMJDesktop::GetDesktopCenterPoint(int *x,int *y,int *iSelfHeight)
{
	if(x != NULL)
		*x = (GetRealWidth()>>1);
	int h;
	QImage img = GetView1Card(0x01,false,false,&h);
	int is = img.height()+img.height()/2;
	if(iSelfHeight != NULL)
		*iSelfHeight = is;
 	if(y != NULL)
	{
		img = GetWallCard(2,&h);
		quint8 ch2WallLen = GetWallLength(2);
		int i2Wall = (img.width()-h)*ch2WallLen+h;
		img = GetWallCard(1,&h);
		int i13Wall = img.height()+h;
		int iy = DJGAME_MAHJONG_DESKTOP_AVATAR_MAXHEIGHT+60+i2Wall+i13Wall*2;
		int ispace = (static_cast<int>(GetRealHeight())-is-iy)/2;
		*y = DJGAME_MAHJONG_DESKTOP_AVATAR_MAXHEIGHT+60+ispace+i13Wall+i2Wall/2;
	}
}
QImage DJGameMJDesktop::GetWallCard(quint8 chView,int *h,qreal scale)
{
	QPixmap pix;
	QImage img;
	if ( 0 == scale )
		scale	= m_scaleOfWall;
	
	if( 1 == chView || 3 == chView )
	{
		pix = 	QPixmap(":/BaseRes/image/mahjong/an1.png");
		img = pix.convertToImage();
		
		img = img.smoothScale((int)(img.width()*scale),
								(int)(img.height()*scale));
	}else
	{
		pix = 	QPixmap(":/BaseRes/image/mahjong/an2.png");
		img = pix.convertToImage();
		img = img.smoothScale(static_cast<int>(img.width()*scale),
								static_cast<int>(img.height()*scale));
	}
	*h = (int)(m_heightOfWallCard.value(chView,0)*scale);
	return img;
}
//h is covered height
QImage DJGameMJDesktop::GetPoolCard(quint8 chView,quint8 chCard,int *h, qreal scale,bool isFlower)
{
	int index = GetCardIndex(chCard);
	if(index<0 || index>=44)
	{
		return GetWallCard(chView,h);
	}
	QPixmap pix;	
	if ( 0 == m_styleOfPoolCards || isFlower)
		pix	= QPixmap( QString(":/BaseRes/image/mahjong/mjcards%1.png").arg(chView) );
	else
		pix	= QPixmap(QString(":/BaseRes/image/mahjong/mjcards1.png"));
		
	int iw = (int)(pix.width()/44);
	*h = (int)(m_heightOfLyingCard.at(chView)*m_scaleOfPool);
	
	QImage img;
	QPixmap pix2 = QPixmap(iw,pix.height());
	pix2.fill();
	QPainter p(&pix2);
	p.drawPixmap(0,0,pix,index*iw,0,(index+1)*iw,-1);
	p.end();
	img = pix2.convertToImage();
	qreal fx	= m_scaleOfPool  * scale;
 	if( fx != 1 )
	{
 		img = img.smoothScale((int)(img.width()*fx),(int)(img.height()*fx));
	}
	return img;
}
QImage DJGameMJDesktop::GetView1Card(quint8 chCard,bool bStatic,bool bUnknown,int *h)
{
	QImage img;
	if( MAHJONG_ISERRORCARD(chCard) ) {
		if ( bStatic ) {
			img = GetWallCard(1,h,m_scaleOfAnGang.at(1));
		}else {
    		QPixmap pix = 	QPixmap(":/BaseRes/image/mahjong/mjcardsb.png");
#ifdef DJ_OEM	
    		int index	= 42;
#else
    		int index	= 43;
#endif		
    		int iw = (int)(pix.width()/44);
    		*h	= m_heightOfStandingCard.at(1);
    		QPixmap pix2 = pix.copy( index*iw, 0, iw, pix.height() );
    		img = pix2.convertToImage();
		}
	}else {
		if( bStatic ) {
			if ( bUnknown )  {
				img = GetWallCard(1,h,m_scaleOfAnGang.at(1));
			}else {
				img = GetPoolCard(1,chCard,h);
			}
		}else {
			QPixmap pix = 	QPixmap(":/BaseRes/image/mahjong/mjcardsb.png");
			
			int index;
			if ( bUnknown ) {
#ifdef DJ_OEM	
				index	= 42;
#else
				index	= 43;
#endif				
			}else {
				index	= GetCardIndex(chCard);
				if(index < 0 || index >=44) {
#ifdef DJ_OEM	
					index	= 42;
#else
					index	= 43;
#endif
				}
			}
			int iw = (int)(pix.width()/44);
			*h	= m_heightOfStandingCard.at(1);
			QPixmap pix2 = pix.copy( index*iw, 0, iw, pix.height() );
			img = pix2.convertToImage();
		}
	}
	qreal scale	= m_scaleOfHand.at(1);
	*h = static_cast<int>((*h)*scale);
	img = img.smoothScale(static_cast<int>(img.width()*scale),
							static_cast<int>(img.height()*scale));
	return img;
}

QImage DJGameMJDesktop::GetView2Card(quint8 chCard,bool bStatic,bool bUnknown,int *h)
{
	if(bUnknown && bStatic)
		return GetWallCard(2,h,m_scaleOfAnGang.at(2));
	QImage img;
	qreal scale;
	if(!bUnknown && !MAHJONG_ISERRORCARD(chCard))
	{
		int index = GetCardIndex(chCard);
		if(index < 0 || index >= 44)
			return GetWallCard(2,h);
		scale 	=  m_scaleOfHand.at(2);
		QPixmap pix = 	QPixmap(":/BaseRes/image/mahjong/mjcards2.png");
		int iw = (int)(pix.width()/44);
		*h = (int)(m_heightOfLyingCard.at(2)*scale);
		/*
		QPixmap pix2 = QPixmap(iw,pix.height());
		pix2.fill();
		QPainter p(&pix2);
		p.drawPixmap(0,0,pix,index*iw,0,(index+1)*iw,-1);
		p.end();
		*/
		QPixmap pix2 = pix.copy( index*iw, 0, iw, pix.height() );
		img = pix2.convertToImage();
		if(scale != 1)
		{
			img = img.smoothScale((int)(img.width()*scale),
									(int)(img.height()*scale));
		}
		return img;
	}
	QPixmap pix = 	QPixmap(":/BaseRes/image/mahjong/back2.png");
	img = pix.convertToImage();
	scale	= m_scaleOfBack.at(2);
	
	*h = (int)(m_heightOfStandingCard.at(2)*scale);
	if(scale != 1)
	{
		img = img.smoothScale((int)(img.width()*scale),
									(int)(img.height()*scale));
	}
	return img;
}

QImage DJGameMJDesktop::GetView3Card(quint8 chCard,bool bStatic,bool bUnknown,int *h)
{
	if(bUnknown && bStatic)
		return GetWallCard(3,h,m_scaleOfAnGang.at(3));
	QImage img;
	qreal scale;
	if(!bUnknown && !MAHJONG_ISERRORCARD(chCard))
	{
		int index = GetCardIndex(chCard);
		if(index < 0 || index >= 44)
			return GetWallCard(3,h);
		scale 	=  m_scaleOfHand.at(3);
		QPixmap pix = 	QPixmap(":/BaseRes/image/mahjong/mjcards3.png");
		int iw = (int)(pix.width()/44);
		*h = (int)(m_heightOfLyingCard.at(3)*scale);
		/*
		QPixmap pix2 = QPixmap(iw,pix.height());
		pix2.fill();
		QPainter p(&pix2);
		p.drawPixmap(0,0,pix,index*iw,0,(index+1)*iw,-1);
		p.end();
 		*/
 		QPixmap pix2 = pix.copy( index*iw, 0, iw, pix.height() );
 		img = pix2.convertToImage();
		if(scale != 1)
		{
			img = img.smoothScale((int)(img.width()*scale),
										(int)(img.height()*scale));
		}
		return img;
	}
	QPixmap pix = 	QPixmap(":/BaseRes/image/mahjong/back3.png");
	img = pix.convertToImage();
	scale	= m_scaleOfBack.at(3);
	*h = (int)(m_heightOfStandingCard.at(3)*scale);
	if(scale != 1)
	{
		img = img.smoothScale((int)(img.width()*scale),
								(int)(img.height()*scale));
	}
	return img;
}
QImage DJGameMJDesktop::GetView4Card(quint8 chCard,bool bStatic,bool bUnknown,int *h)
{
	if(bUnknown && bStatic)
		return GetWallCard(4,h,m_scaleOfAnGang.at(4));
	QImage img;
	qreal scale;
	if(!bUnknown && !MAHJONG_ISERRORCARD(chCard))
	{
		int index = GetCardIndex(chCard);
		if(index < 0 || index >= 44)
			return GetWallCard(4,h);
		scale 	=  m_scaleOfHand.at(4);
		QPixmap pix = 	QPixmap(":/BaseRes/image/mahjong/mjcards4.png");
		int iw = (int)(pix.width()/44);
		*h = (int)(m_heightOfLyingCard.at(4)*scale);
		/*
		QPixmap pix2 = QPixmap(iw,pix.height());
		pix2.fill();
		QPainter p(&pix2);
		p.drawPixmap(0,0,pix,index*iw,0,(index+1)*iw,-1);
		p.end();
 		*/
 		QPixmap pix2 = pix.copy( index*iw, 0, iw, pix.height() );
 		img = pix2.convertToImage();
		if(scale != 1)
		{
			img = img.smoothScale((int)(img.width()*scale),
									(int)(img.height()*scale));
		}
		return img;
	}
	QPixmap pix = 	QPixmap(":/BaseRes/image/mahjong/back4.png");
	img = pix.convertToImage();
	scale	= m_scaleOfBack.at(4);
	*h = (int)(m_heightOfStandingCard.at(4)*scale);
	if(scale != 1)
	{
		img = img.smoothScale((int)(img.width()*scale),
									(int)(img.height()*scale));
	}
	return img;
}

int DJGameMJDesktop::GetCardIndex(quint8 chCard)
{
	int i = 0;
    while(g_chMJImageCardOrder[i] != 0)
    {
        if(g_chMJImageCardOrder[i] == MAHJONG_CARD(chCard))
            break;
        i++;
    }
    if(g_chMJImageCardOrder[i] == 0)
    {
		return -1;
    }
	return i;
}

bool DJGameMJDesktop::playerTingStatus(quint8 chSeat)
{
	return true;
}

bool DJGameMJDesktop::playerHuStatus(quint8 chSeat,bool bDIY, quint8 lastMGangCard)
{
	if(!c_bPrivate[chSeat])
		return false;
	if(chSeat == 0 || chSeat > 4)
		return false;
	if(c_bHUStatus)
	{
		unsigned char chCards[20];
		memset(chCards,0,20);
		memcpy(chCards,c_chPlayerCards[chSeat],c_chHuPages);
		if(!bDIY)
		{//非自摸则把牌池中的最后一张加入
			if ( 0 == lastMGangCard ) {
    			if(c_chPoolPages == 0)
    				return false;
    			if(chSeat == c_PoolCards[c_chPoolPages-1].chSeat)
    				return false;
    			chCards[c_chHuPages-1] = MAHJONG_CARD(GetLastThrowCard());
			}else {
				chCards[c_chHuPages-1] =  lastMGangCard;
			}
		}
		//判断
		OrderCard(chCards,c_chHuPages);
		MahjongHUTypeWrap *pnormal = NULL,*presult,*p;
		for(int i=0;i<8;i++)
		{
			if(c_playerNormalHu[chSeat].cardwrap[i].chExist != 0)
			{
				pnormal = &c_playerNormalHu[chSeat];
				break;
			}
		}
		presult = MahjongRule_CheckHu(pnormal,chCards,c_shHuMask);
	    
    	if(presult != NULL)
    	{
	    	while(presult != NULL)
			{
				p=presult;
				presult = p->pnext;
				free(p);
	    	}
	    	return true;
		}
		return false;
	}
	//不用判断
	return false;
}

bool DJGameMJDesktop::playerPengStatus(quint8 chSeat)
{
	if(!c_bPrivate[chSeat])
		return false;
	if(c_chPoolPages == 0)
		return 0;
	if(chSeat == c_PoolCards[c_chPoolPages-1].chSeat)
		return 0;
	quint8 chCard = c_PoolCards[c_chPoolPages-1].chCard;
	
	if(MAHJONG_ISERRORCARD(MAHJONG_CARD(chCard)))
		return false;
	quint8 chPages = 0;
	for(int i=0;i<c_chHuPages-1;i++)
	{
		if(c_chPlayerCards[chSeat][i] == MAHJONG_CARD(chCard))
		{
			chPages ++;
			if(chPages == 2)
				return true;
		}
	}
	return false;
	
}
bool DJGameMJDesktop::hasPlayerTing( quint8 seatId ) const
{
	return c_bPlayerTing[seatId];
}
quint8 DJGameMJDesktop::GetLastThrowCard()
{
	if(c_chPoolPages == 0)
		return 0;
	return c_PoolCards[c_chPoolPages-1].chCard;
}

quint8 DJGameMJDesktop::GetLastThrowSeat()
{
	if(c_chPoolPages == 0)
		return 0;
	return c_PoolCards[c_chPoolPages-1].chSeat;
}

quint8 DJGameMJDesktop::playerGangStatus(quint8 chSeat,quint8 chCard,quint8 *pchGangs,quint8 chLen)
{
	if(!c_bPrivate[chSeat])
		return 0;
	quint8 chPages[0x40];
	quint8 chGangs = 0;
	int i;
	memset(chPages,0,0x40);
	for(i=0;i<c_chHuPages;i++)
	{
		if(!MAHJONG_ISERRORCARD(c_chPlayerCards[chSeat][i]))
		{
			chPages[MAHJONG_CARD(c_chPlayerCards[chSeat][i])]++;
			if(chCard == 0 && chPages[MAHJONG_CARD(c_chPlayerCards[chSeat][i])] == 4)
			{
				if(pchGangs != NULL && chGangs < chLen)
				{
					pchGangs[chGangs] = MAHJONG_CARD(c_chPlayerCards[chSeat][i]);
				}
	//			printf("gang1 %02x\n",MAHJONG_CARD(c_chPlayerCards[chSeat][i]));
				chGangs ++;
			}
			if(chCard != 0 && chPages[MAHJONG_CARD(c_chPlayerCards[chSeat][i])] == 3 && 
						MAHJONG_CARD(c_chPlayerCards[chSeat][i]) == MAHJONG_CARD(chCard))
			{
				if(pchGangs != NULL && chGangs < chLen)
				{
					pchGangs[chGangs] = c_chPlayerCards[chSeat][i];
				}
				chGangs ++;
		//		printf("gang2 %02x\n",MAHJONG_CARD(c_chPlayerCards[chSeat][i]));
			}
		}
	}
	if(chCard == 0)
	{
		for(i=0;i<8;i++)
		{
			if(c_playerNormalHu[chSeat].cardwrap[i].chExist != 0 && 
				c_playerNormalHu[chSeat].cardwrap[i].chCardWrapName == MAHJONG_CARDWRAP_NAME_KE)
			{
				if(c_playerNormalHu[chSeat].cardwrap[i].chWrap[3] == 0 &&
					chPages[MAHJONG_CARD(c_playerNormalHu[chSeat].cardwrap[i].chWrap[0])] == 1)
				{
					if(pchGangs != NULL && chGangs < chLen)
					{
						pchGangs[chGangs] = MAHJONG_CARD(c_playerNormalHu[chSeat].cardwrap[i].chWrap[0]);
					}
					chGangs ++;
					//printf("gang3 %02x\n",MAHJONG_CARD(c_playerNormalHu[chSeat].cardwrap[i].chWrap[0]));
				}
			}
		}
	}
	return chGangs;
	
}

quint8 DJGameMJDesktop::playerChiMask(quint8 chSeat)
{
	if(!c_bPrivate[chSeat])
		return 0;
	if(c_chPoolPages == 0)
		return 0;
	if(chSeat == c_PoolCards[c_chPoolPages-1].chSeat)
		return 0;
	quint8 chCard = c_PoolCards[c_chPoolPages-1].chCard;
	quint8 chChiSeat = chSeat+1;
	if(chChiSeat > 4)
		chChiSeat = 1;
	if(c_PoolCards[c_chPoolPages-1].chSeat != chChiSeat)
		return 0;
	if(MAHJONG_ISERRORCARD(MAHJONG_CARD(chCard)))
		return 0;
	if(MAHJONG_ISZI(MAHJONG_CARD(chCard)))
		return 0;
	quint8 chP1 = 0,chP2=0,chN1=0,chN2=0;
	quint8 chValue = GET_MAHJONG_CARDVALUE(chCard);
	for(int i=0;i<c_chHuPages-1;i++)
	{
		if(!MAHJONG_ISERRORCARD(c_chPlayerCards[chSeat][i]) && GET_MAHJONG_CARDCLASS(c_chPlayerCards[chSeat][i]) == GET_MAHJONG_CARDCLASS(chCard))
		{
			if(GET_MAHJONG_CARDVALUE(c_chPlayerCards[chSeat][i]) == chValue-1 && chValue>=2)
				chP1 ++;
			if(GET_MAHJONG_CARDVALUE(c_chPlayerCards[chSeat][i]) == chValue-2 && chValue >=3)
				chP2 ++;
			if(GET_MAHJONG_CARDVALUE(c_chPlayerCards[chSeat][i]) == chValue+1 && chValue <=8)
				chN1 ++;
			if(GET_MAHJONG_CARDVALUE(c_chPlayerCards[chSeat][i]) == chValue+2 && chValue <=7 )
				chN2 ++;
		}
	}
	quint8 chMask = 0;
	if(chP1 > 0 && chP2 > 0)
		chMask |= DJGAME_MAHJONG_CHIMASK_12X;
	if(chP1 > 0 && chN1 > 0)
		chMask |= DJGAME_MAHJONG_CHIMASK_1X3;
	if(chN1 > 0 && chN2 > 0)
		chMask |= DJGAME_MAHJONG_CHIMASK_X23;
		
	return chMask;
}

quint8 DJGameMJDesktop::GetPlayerCurrentCards(quint8 seat,quint8 *buf,quint8 chLen)
{
	if(seat == 0 || seat > 4)
		return 0;
	quint8 chRet = 0;
	for(int i=0;i<c_chHuPages;i++)
	{
		if(c_chPlayerCards[seat][i] != 0)
		{
			if(chRet < chLen )
				buf[chRet] = c_chPlayerCards[seat][i];
			chRet ++;
		}
	}
	return chRet;
}

void DJGameMJDesktop::ResetPlayerCards(quint8 chSeat,quint8 *pbuf,quint8 chPages)
{
	if(chSeat == 0 || chSeat > 4)
		return;
	memset(c_chPlayerCards[chSeat],0,20);
	if(pbuf != NULL && chPages > 0)
	{
		if(chPages > c_chHuPages)
			chPages = c_chHuPages;
		memcpy(c_chPlayerCards[chSeat],pbuf,chPages);
		OrderCard(c_chPlayerCards[chSeat],c_chHuPages);
	}
}
	
void DJGameMJDesktop::PlayerPickup(quint8 chSeat,quint8 card)
{
	djDebug()<<"DJGameMJDesktop::PlayerPickup"<<"seat"<<chSeat<<"card"<<hex<<card;
	if(chSeat == 0 || chSeat > 4)
		return;
	c_chPlayerCards[chSeat][c_chHuPages-1] = card;
	if(c_bGang)
	{
		RemoveWallCard(1,true);
	}else
		RemoveWallCard(1,false);
	c_bGang = false;
	c_bHUStatus = true;
}

void DJGameMJDesktop::PlayerTing(quint8 chSeat)
{
	djDebug() << "DJGameMJDesktop::PlayerTing" << chSeat;
	if(chSeat == 0 || chSeat > 4)
		return;
	c_bPlayerTing[chSeat] = true;
}
void DJGameMJDesktop::PlayerThrow(quint8 chSeat,quint8 card,bool b2Pool)
{
	djDebug() << "DJGameMJDesktop::PlayerThrow" << chSeat << card << b2Pool;
//	c_bGang = false;
	if(chSeat == 0 || chSeat > 4)
		return;
	int i;
	bool bFound=false;
	for(i=0;i<c_chHuPages;i++)
	{
		if(c_chPlayerCards[chSeat][i] == card)
		{
			c_chPlayerCards[chSeat][i] = 0;
			bFound = true;
			break;
		}
	}
	if(!bFound)
	{
		for(i=0;i<c_chHuPages;i++)
		{
			if(c_chPlayerCards[chSeat][i] == MAHJONG_CARD_BACKGROUND)
			{
				c_chPlayerCards[chSeat][i] = 0;
				bFound = true;
				break;
			}
		}
	}
	if(bFound)
		OrderCard(c_chPlayerCards[chSeat],c_chHuPages);
	if(b2Pool)
	{
		c_PoolCards[c_chPoolPages].chCard = card;
		c_PoolCards[c_chPoolPages].chSeat = chSeat;
		c_chPoolPages ++;
	}
	if(chSeat == c_Panel->selfSeatId())
	{
		c_bHUStatus = false;
		SelectCard((DJGameImageItem*)NULL);
	}else
		c_bHUStatus = true;
}

quint8 DJGameMJDesktop::GetTotalCardPages()
{
	quint8 chPages = 0;
	if(c_Rule.chChips & MAHJONG_RULE_CHIPS_FORBID19)
		chPages = 84;//7*4*3
	else
		chPages = 108; //9*4*3
	
	if(c_Rule.chChips & MAHJONG_RULE_CHIPS_FENG )
	{
		if(c_Rule.chChips & MAHJONG_RULE_CHIPS_SINGEFENG)
			chPages += 4;
		else
			chPages += 16;//4*4
	}
	if(c_Rule.chChips & MAHJONG_RULE_CHIPS_XI )
	{
		chPages += 12;//3*4
	}
	if(c_Rule.chChips & MAHJONG_RULE_CHIPS_4SEASON )
		chPages += 4;
	if(c_Rule.chChips & MAHJONG_RULE_CHIPS_4FLOWER )
		chPages += 4;
	return chPages;
	
}
void DJGameMJDesktop::OrderCard(quint8 *pcards,quint8 chPages)
{
	int i,j,ic;
	quint8 temp;
	do
	{
		j = 0;
		for(i=0;i<chPages-1;i++)
		{
			ic = Compare2Card(pcards[i],pcards[i+1]);
			if(ic == DJGAME_MJDESKTOP_COMPARE_RESUST_1 )
			{
				temp = pcards[i+1];
				pcards[i+1] = pcards[i];
				pcards[i] = temp;
				j ++;
			}
		}
				
	}while(j!=0);
}

int DJGameMJDesktop::Compare2Card(quint8 chCard1,quint8 chCard2)
{
	if(chCard1 == 0 && chCard2 == 0)
		return DJGAME_MJDESKTOP_COMPARE_RESUST_E;
	if(chCard1 == 0)
		return DJGAME_MJDESKTOP_COMPARE_RESUST_1;
	if(chCard2 == 0)
		return DJGAME_MJDESKTOP_COMPARE_RESUST_2;
	if(MAHJONG_ISPLUTUS(chCard1) && MAHJONG_ISPLUTUS(chCard2) )
		return DJGAME_MJDESKTOP_COMPARE_RESUST_E;
	if(MAHJONG_ISPLUTUS(chCard1))
		return DJGAME_MJDESKTOP_COMPARE_RESUST_2;
	if(MAHJONG_ISPLUTUS(chCard2))
		return DJGAME_MJDESKTOP_COMPARE_RESUST_1;
	
	if(chCard1 == MAHJONG_CARD_BACKGROUND && chCard2 == MAHJONG_CARD_BACKGROUND)
		return DJGAME_MJDESKTOP_COMPARE_RESUST_E;
	if(chCard1 == MAHJONG_CARD_BACKGROUND)
		return DJGAME_MJDESKTOP_COMPARE_RESUST_1;
	if(chCard2 == MAHJONG_CARD_BACKGROUND)
		return DJGAME_MJDESKTOP_COMPARE_RESUST_2;
	
	if(MAHJONG_CARD(chCard1) == MAHJONG_CARD(chCard2))
		return DJGAME_MJDESKTOP_COMPARE_RESUST_E;
	
	if(MAHJONG_ISZI(chCard1) && MAHJONG_NOTZI(chCard2))
		return DJGAME_MJDESKTOP_COMPARE_RESUST_1;
	if(MAHJONG_ISZI(chCard2) && MAHJONG_NOTZI(chCard1))
		return DJGAME_MJDESKTOP_COMPARE_RESUST_2;
	
	return (MAHJONG_CARD(chCard1)>MAHJONG_CARD(chCard2))?DJGAME_MJDESKTOP_COMPARE_RESUST_1:DJGAME_MJDESKTOP_COMPARE_RESUST_2;
	
}
	

void DJGameMJDesktop::PlayerCHI(quint8 chSeat,quint8 card1,quint8 card2,quint8 card3)
{
	djDebug() << "DJGameMJDesktop::PlayerCHI" << chSeat << card1 << card2 << card3;
	if(chSeat == 0 || chSeat > 4)
		return;
	PlayerThrow(chSeat,MAHJONG_CARD(card1),false);
	PlayerThrow(chSeat,MAHJONG_CARD(card2),false);
	if(c_chPoolPages > 0)
		c_chPoolPages --;
	for(int i=0;i<4;i++)
	{
		if(c_playerNormalHu[chSeat].cardwrap[i].chExist == 0)
		{
			c_playerNormalHu[chSeat].cardwrap[i].chWrap[0] = card1 | MAHJONG_CARD_STYLE_CHI;
			c_playerNormalHu[chSeat].cardwrap[i].chWrap[1] = card2 | MAHJONG_CARD_STYLE_CHI;
			c_playerNormalHu[chSeat].cardwrap[i].chWrap[2] = card3 | MAHJONG_CARD_STYLE_CHI;
			c_playerNormalHu[chSeat].cardwrap[i].chExist = 1;
			break;
		}
	}
	for(int i=c_chHuPages-2;i>=0;i--)
	{
		if(c_chPlayerCards[chSeat][i] != 0)
		{
			c_chPlayerCards[chSeat][c_chHuPages-1] = c_chPlayerCards[chSeat][i];
			c_chPlayerCards[chSeat][i] = 0;
			break;
		}
	}
	c_bHUStatus = false;
}
void DJGameMJDesktop::PlayerPENG(quint8 chSeat,quint8 card)
{
	djDebug() << "DJGameMJDesktop::PlayerPENG" << chSeat << card;
	if(chSeat == 0 || chSeat > 4)
		return;
	PlayerThrow(chSeat,MAHJONG_CARD(card),false);
	PlayerThrow(chSeat,MAHJONG_CARD(card),false);
	if(c_chPoolPages > 0)
		c_chPoolPages --;
	for(int i=4;i<8;i++)
	{
		if(c_playerNormalHu[chSeat].cardwrap[i].chExist == 0)
		{
			c_playerNormalHu[chSeat].cardwrap[i].chWrap[0] = card | MAHJONG_CARD_STYLE_CHI;
			c_playerNormalHu[chSeat].cardwrap[i].chWrap[1] = card | MAHJONG_CARD_STYLE_CHI;
			c_playerNormalHu[chSeat].cardwrap[i].chWrap[2] = card | MAHJONG_CARD_STYLE_CHI;
			c_playerNormalHu[chSeat].cardwrap[i].chWrap[3] = 0;
			c_playerNormalHu[chSeat].cardwrap[i].chExist = 1;
			break;;
		}
	}
	for(int i=c_chHuPages-2;i>=0;i--)
	{
		if(c_chPlayerCards[chSeat][i] != 0)
		{
			c_chPlayerCards[chSeat][c_chHuPages-1] = c_chPlayerCards[chSeat][i];
			c_chPlayerCards[chSeat][i] = 0;
			break;
		}
	}
	c_bHUStatus = false;
}
void DJGameMJDesktop::PlayerGANG(quint8 seat,quint8 card)
{
	if(seat == 0 || seat > 4)
		return;
	if(MAHJONG_ISFLOWERCARD(card))
	{
		for(int i=0;i<8;i++)
		{
			if(c_chPlayerFlower[seat][i] == 0)
			{
				c_chPlayerFlower[seat][i] = card;
				c_chPlayerFlower[seat][i+1] = 0;
				return;
			}
		}
		return;
	}
	c_bGang = true;
	int i;
	for(i=4;i<8;i++)
	{
		if(c_playerNormalHu[seat].cardwrap[i].chExist != 0 && MAHJONG_CARD(c_playerNormalHu[seat].cardwrap[i].chWrap[0]) == MAHJONG_CARD(card))
		{//已经碰过的明杠
			PlayerThrow(seat,MAHJONG_CARD(card),false);
			c_playerNormalHu[seat].cardwrap[i].chWrap[3] = card | MAHJONG_CARD_STYLE_CHI;
			return;
		}
	}
	if(MAHJONG_ISPLUTUS(card))
	{//混杠
		PlayerThrow(seat,card,false);
		PlayerThrow(seat,card,false);
		PlayerThrow(seat,card,false);
		PlayerThrow(seat,card,false);
	}else if(MAHJONG_ISDIY(card))
	{//暗杠
		PlayerThrow(seat,MAHJONG_CARD(card),false);
		PlayerThrow(seat,MAHJONG_CARD(card),false);
		PlayerThrow(seat,MAHJONG_CARD(card),false);
		PlayerThrow(seat,MAHJONG_CARD(card),false);
	}else 
	{//明杠
		PlayerThrow(seat,MAHJONG_CARD(card),false);
		PlayerThrow(seat,MAHJONG_CARD(card),false);
		PlayerThrow(seat,MAHJONG_CARD(card),false);
		card |= MAHJONG_CARD_STYLE_CHI;
		if(c_chPoolPages > 0)
			c_chPoolPages --;
	}
	for(i=4;i<8;i++)
	{
		if(c_playerNormalHu[seat].cardwrap[i].chExist == 0)
		{
			c_playerNormalHu[seat].cardwrap[i].chWrap[0] = card;
			c_playerNormalHu[seat].cardwrap[i].chWrap[1] = card;
			c_playerNormalHu[seat].cardwrap[i].chWrap[2] = card;
			c_playerNormalHu[seat].cardwrap[i].chWrap[3] = card;
			c_playerNormalHu[seat].cardwrap[i].chExist = 1;
			return;
		}
	}
}


void DJGameMJDesktop::SetRule(MahjongRule *prule)
{
	memcpy(&c_Rule,prule,sizeof(MahjongRule));
}

void DJGameMJDesktop::SetHuPages(quint8 pages)
{
	c_chHuPages = pages;
}

quint8 DJGameMJDesktop::GetWallLength(quint8 chView)
{
	quint8 chTotalPages = GetTotalCardPages();
	if(chView == 2 || chView == 4)
		return (chTotalPages>>3);
	quint8 chPages = chTotalPages%8;
	if(chView == 1)
		return (chTotalPages>>3)+(chPages>>1)+(chPages%2);
	else if(chView == 3)
		return (chTotalPages>>3)+(chPages>>1);
	return 0;
}
void DJGameMJDesktop::RepaintWallCards()
{
	ClearImage(0,0,DJGAME_DESKTOP_TYPE_WALL);
	
	RepaintView1WallCards();
	RepaintView2WallCards();
	RepaintView3WallCards();
	RepaintView4WallCards();
	c_chWallRemoved = 0;
	c_chWallEndRemoved = 0;
	SetWallStartPoint((c_chWallStartSeat<<5)+c_chWallStartPos);
	
	RenewWall();
}

void DJGameMJDesktop::RenewWall()
{
	if(c_chWallRemoved < c_chWallRemoves)
	{
		//quint8 seat = c_chWallStartSeat;
		int pos = c_chWallStartPos;
		quint8 view = MapSeat2View(c_chWallStartSeat);
		quint8 chWallLen = GetWallLength(view)*2;
		int i=0;
		
		while(i<c_chWallRemoves)
		{
			ClearImage(pos,MapView2Seat(view),DJGAME_DESKTOP_TYPE_WALL);
			if(pos & 1)
				pos ++;
			else
				pos -= 3;
			if(pos < 1)
			{
				view = (view == 4)?1:view+1;
				chWallLen = GetWallLength(view)*2;
				pos = chWallLen-1;
			}
			i++;
		}
		c_chWallRemoved = c_chWallRemoves;
	}
	if(c_chWallEndRemoved < c_chWallEndRemoves)
	{
		//quint8 seat = c_chWallStartSeat;
		int pos = c_chWallStartPos+1;
		quint8 view = MapSeat2View(c_chWallStartSeat);
		quint8 chWallLen = GetWallLength(view)*2;
		int i=0;
		while(i<c_chWallEndRemoves)
		{
			pos ++;
			if(pos > chWallLen)
			{
				pos = 1;
				view = (view == 1)?4:view-1;
				chWallLen = GetWallLength(view)*2;
			}
			ClearImage(pos,MapView2Seat(view),DJGAME_DESKTOP_TYPE_WALL);
			i++;
		}
		c_chWallEndRemoved = c_chWallEndRemoves;
	}
}
void DJGameMJDesktop::AppendPlayerFlowerCard(quint8 seat,quint8 chCard)
{
	if(seat > 4 || !MAHJONG_ISFLOWERCARD(chCard))
		return;
	PlayerThrow(seat,chCard,false);//不放牌池中
	for(int i=0;i<16;i++)
	{
		if(!MAHJONG_ISFLOWERCARD(c_chPlayerFlower[seat][i]))
		{
			c_chPlayerFlower[seat][i] = chCard;
			return;
		}
	}
}
void DJGameMJDesktop::RepaintPlayerFlowers(quint8 seat)
{
	ClearFlowerCards(seat);
	
	quint8 chView = MapSeat2View(seat);
	switch(chView)
	{
		case 1 :
			RepaintView1Flowers(seat);
			break;
		case 2 :
			RepaintView2Flowers(seat);
			break;
		case 3 :
			RepaintView3Flowers(seat);
			break;
		case 4 :
			RepaintView4Flowers(seat);
			break;
	}
}
void DJGameMJDesktop::RepaintView1Flowers(quint8 seat)
{ 
 	if(c_chPlayerFlower[seat][0] == 0)
		return;
	QImage img;
	int x,y;
	QMatrix m = GetCurrentMatrix();
	
	int is;
	GetDesktopCenterPoint(&x,&y,&is);
  	x = 0;
	y = GetRealHeight() - is;
	DJGameImageItem *item;
	int h,i=0;
	while(c_chPlayerFlower[seat][i] != 0)
	{
		if(!MAHJONG_ISFLOWERCARD(c_chPlayerFlower[seat][i]))
			break;
		img = GetPoolCard(1,c_chPlayerFlower[seat][i],&h,1.0,true );
		item = new DJGameImageItem(img,canvas(),m,c_chPlayerFlower[seat][i],seat,DJGAME_DESKTOP_TYPE_FLOWER,false);
		item->setMatrix(m);
		item->setVAlignment(Qt::AlignBottom);
		item->move(x,y);
		item->setZ(100+i);
		item->show();
		
		x+= img.width();
		i++;
	}
}
void DJGameMJDesktop::RepaintView2Flowers(quint8 seat)
{
	if(c_chPlayerFlower[seat][0] == 0)
		return;
	QImage img;
	QMatrix m = GetCurrentMatrix();
	QRect rect	= idealRectOfShow( seat );
	int x	= rect.center().x();
	int y	= rect.top();
 	int h,i=0;
	DJGameImageItem *item;
	
	while(c_chPlayerFlower[seat][i] != 0)
	{
		if(!MAHJONG_ISFLOWERCARD(c_chPlayerFlower[seat][i]))
			break;
		img = GetPoolCard(2, c_chPlayerFlower[seat][i],&h,1.0,true);
		item = new DJGameImageItem(img,canvas(),m,c_chPlayerFlower[seat][i],seat,DJGAME_DESKTOP_TYPE_FLOWER,false);
		item->setMatrix(m);
		item->setHAlignment(Qt::AlignHCenter);
		item->setVAlignment(Qt::AlignBottom);
		item->move(x,y);
		item->setZ(100-i);
		item->show();
		
		y-= (img.height()-h);
		i++;
	}
}
void DJGameMJDesktop::RepaintView3Flowers(quint8 seat)
{
	if(c_chPlayerFlower[seat][0] == 0)
		return;
	QImage img;
	QMatrix m = GetCurrentMatrix();
	QRect rect	= idealRectOfShow( seat );
	int x	= rect.right();
	int y	= rect.center().y();
	int h,i=0;
	DJGameImageItem *item;
	
	while(c_chPlayerFlower[seat][i] != 0)
	{
		if(!MAHJONG_ISFLOWERCARD(c_chPlayerFlower[seat][i]))
			break;
		img = GetPoolCard(3,c_chPlayerFlower[seat][i],&h,1.0,true);
		item = new DJGameImageItem(img,canvas(),m,c_chPlayerFlower[seat][i],seat,DJGAME_DESKTOP_TYPE_FLOWER,false);
		item->setMatrix(m);
		item->setVAlignment(Qt::AlignVCenter);
		item->move(x,y);
		item->setZ(100+i);
		item->show();
		
		x+= img.width();
		i++;
	}
}
void DJGameMJDesktop::RepaintView4Flowers(quint8 seat)
{
	if(c_chPlayerFlower[seat][0] == 0)
		return;
	QImage img;
	QMatrix m = GetCurrentMatrix();
	QRect rect	= idealRectOfShow( seat );
	int x	= rect.center().x();
	int y	= rect.top();
	int h,i=0;
	DJGameImageItem *item;
	
	while(c_chPlayerFlower[seat][i] != 0)
	{
		if(!MAHJONG_ISFLOWERCARD(c_chPlayerFlower[seat][i]))
			break;
		img = GetPoolCard(4,c_chPlayerFlower[seat][i],&h,1.0,true);
		item = new DJGameImageItem(img,canvas(),m,c_chPlayerFlower[seat][i],seat,DJGAME_DESKTOP_TYPE_FLOWER,false);
		item->setMatrix(m);
		item->setHAlignment(Qt::AlignHCenter);
		item->setVAlignment(Qt::AlignBottom);
		item->move(x,y);
		item->setZ(100-i);
		item->show();
		
		y-= (img.height()-h);
		i++;
	}
}

void DJGameMJDesktop::RepaintPoolCards(quint8 seat)
{
	ClearImage(0,seat,DJGAME_DESKTOP_TYPE_POOL);
	
	quint8 chView = MapSeat2View(seat);
	switch(chView)
	{
		case 1 :
			RepaintView1PoolCards(seat);
			break;
		case 2 :
			RepaintView2PoolCards(seat);
			break;
		case 3 :
			RepaintView3PoolCards(seat);
			break;
		case 4 :
			RepaintView4PoolCards(seat);
			break;
	}
}

void DJGameMJDesktop::RepaintView1PoolCards(quint8 seat)
{
	if ( 0 == m_styleOfPoolCards ) {
    	int sx,sy;
    	QImage poolimg;
    	int cx,cy,iSelfHeight,h;
    	GetDesktopCenterPoint(&cx,&cy,&iSelfHeight);
    
    	quint8 chWallLen = GetWallLength(1);
    	QImage imgwall = GetWallCard(1,&h);
    	sx = cx - (chWallLen*imgwall.width())/2;
    	
    	chWallLen = GetWallLength(2);
    	imgwall = GetWallCard(2,&h);
    	sy = cy + (chWallLen*(imgwall.height()-h))/2 + 2 * h;
    	
    	QMatrix m = GetCurrentMatrix();
    	int x = sx,y=sy;
    	DJGameImageItem *item;
    	int z = 300;
    	for(int i= 0;i<c_chPoolPages;i++)
    	{
    		if(c_PoolCards[i].chSeat == seat)
    		{
    			if( i == c_chPoolPages  - 1 ) {
    				poolimg = GetPoolCard(1,c_PoolCards[i].chCard,&h,1.5);
    				item = new DJGameImageItem(poolimg,canvas(),m,c_PoolCards[i].chCard,seat,DJGAME_DESKTOP_TYPE_POOL,false);
    				item->setMark( true );
    				item->setHAlignment(Qt::AlignHCenter);
    				//item->setVAlignment(Qt::AlignBottom);
    				item->move( cx, sy );
    				item->setZ(z);
    				item->setMatrix(m); 
    				item->show();
    				return;
     			}else {
     				poolimg = GetPoolCard(1,c_PoolCards[i].chCard,&h);
     				item = new DJGameImageItem(poolimg,canvas(),m,c_PoolCards[i].chCard,seat,DJGAME_DESKTOP_TYPE_POOL,false);
     				item->setVAlignment(Qt::AlignBottom);
     				item->setMark( false );
     			}
    			item->move(x,y);
    			item->setZ(z);
    			z--;
    			item->show();
    			item->setMatrix(m);
    			
    			x += poolimg.width();
    			if( x > cx )
    			{
    				x = sx;
    				y -= poolimg.height()-h;
    				if( y < cy )
    				{
    					x = sx;
    					y = sy;
    				}
    			}
    		}
    	}
	}else if ( 1 == m_styleOfPoolCards ) {
		int sx,sy,ex,ey;
    	int cx,cy,iSelfHeight,h;
    	GetDesktopCenterPoint(&cx,&cy,&iSelfHeight);
    	
    	QImage poolimg = GetPoolCard(1,1,&h);
    	sx = cx - (poolimg.width()/2);
    	sy = cy + ((poolimg.height()-h)/2);
    	
    	quint8 chWallLen = GetWallLength(1);
    	QImage imgwall = GetWallCard(1,&h);
    	ex = cx + (chWallLen*imgwall.width())/2;
    	
    	chWallLen = GetWallLength(2);
    	imgwall = GetWallCard(2,&h);
    	ey = cy + (chWallLen*(imgwall.height()-h))/2;
    	
    	QMatrix m = GetCurrentMatrix();
    	int x = sx,y=sy;
    	DJGameImageItem *item;
    	int z = 100;
    	for(int i= 0;i<c_chPoolPages;i++)
    	{
    		if(c_PoolCards[i].chSeat == seat)
    		{
    			if( i == c_chPoolPages  - 1 ) {
    				poolimg = GetPoolCard(1,c_PoolCards[i].chCard,&h,1.5);
					item = new DJGameImageItem(poolimg,canvas(),m,c_PoolCards[i].chCard,seat,DJGAME_DESKTOP_TYPE_POOL,false);
					item->setMark( true );
     			}else {
     				poolimg = GetPoolCard(1,c_PoolCards[i].chCard,&h);
     				item = new DJGameImageItem(poolimg,canvas(),m,c_PoolCards[i].chCard,seat,DJGAME_DESKTOP_TYPE_POOL,false);
     				item->setMark( false );
     			}
    			item->move(x,y);
    			item->setZ(z);
    			z++;
    			item->show();
    			item->setMatrix(m);
    			
    			y += (poolimg.height()-h);
    			if(y+(poolimg.height()-h) > ey)
    			{
    				y = sy;
    				x += poolimg.width();
    				if(x+poolimg.width() > ex)
    				{
    					x = sx;
    					y = sy;
    				}
    			}
    		}
    	}
	}
}
void DJGameMJDesktop::RepaintView2PoolCards(quint8 seat)
{
	if ( 0 == m_styleOfPoolCards ) {
    	int sx,sy;
    	QImage poolimg;
    	int cx,cy,iSelfHeight,h;
    	GetDesktopCenterPoint(&cx,&cy,&iSelfHeight);
    	
    	quint8 chWallLen = GetWallLength(1);
    	QImage imgwall = GetWallCard(1,&h);
    	sx = cx - (chWallLen*imgwall.width())/2;
    	
    	chWallLen = GetWallLength(2);
    	imgwall = GetWallCard(2,&h);
    	sy = cy - (chWallLen*(imgwall.height()-h))/2;
    	
    	QMatrix m = GetCurrentMatrix();
    	int x = sx,y=sy;
    	DJGameImageItem *item;
    	int z = 300;
    	for(int i= 0;i<c_chPoolPages;i++)
    	{
    		if(c_PoolCards[i].chSeat == seat)
    		{
    			if( i == c_chPoolPages  - 1 ) {
    				poolimg = GetPoolCard(2,c_PoolCards[i].chCard,&h,1.5);
    				item = new DJGameImageItem(poolimg,canvas(),m,c_PoolCards[i].chCard,seat,DJGAME_DESKTOP_TYPE_POOL,false);
    				item->setMark( true );
    				item->setHAlignment(Qt::AlignRight);
    				item->setVAlignment(Qt::AlignVCenter);
    				item->move( sx ,cy );
    				item->setZ(z);
    				item->setMatrix(m); 
    				item->show();
    				return;
     			}else {
     				poolimg = GetPoolCard(2,c_PoolCards[i].chCard,&h);
     				item = new DJGameImageItem(poolimg,canvas(),m,c_PoolCards[i].chCard,seat,DJGAME_DESKTOP_TYPE_POOL,false);
     				item->setMark( false );
     			}
    			item->move(x,y);
    			item->setZ(z);
    			z++;
    			item->show();
    			item->setMatrix(m);
    			
    			y += poolimg.height() - h;
    			if( y > cy )
    			{
    				y = sy;
    				x += poolimg.width();
    				if( x > cx )
    				{
    					x = sx;
    					y = sy;
    				}
    			}
    		}
    	}
	}else if ( 1 == m_styleOfPoolCards ) {
		int sx,sy,ex,ey;
    	int cx,cy,iSelfHeight,h;
    	GetDesktopCenterPoint(&cx,&cy,&iSelfHeight);
    	
    	QImage poolimg = GetPoolCard(2,1,&h);
    	sx = cx - (poolimg.width()/2);
    	sy = cy - ((poolimg.height()-h)/2);
    	
    	quint8 chWallLen = GetWallLength(1);
    	QImage imgwall = GetWallCard(1,&h);
    	ex = cx - (chWallLen*imgwall.width())/2;
    	
    	chWallLen = GetWallLength(2);
    	imgwall = GetWallCard(2,&h);
    	ey = cy + (chWallLen*(imgwall.height()-h))/2;
    	
    	QMatrix m = GetCurrentMatrix();
    	int x = sx,y=sy;
    	DJGameImageItem *item;
    	int z = 100;
    	for(int i= 0;i<c_chPoolPages;i++)
    	{
    		if(c_PoolCards[i].chSeat == seat)
    		{
    			if( i == c_chPoolPages  - 1 ) {
    				poolimg = GetPoolCard(2,c_PoolCards[i].chCard,&h,1.5);
					item = new DJGameImageItem(poolimg,canvas(),m,c_PoolCards[i].chCard,seat,DJGAME_DESKTOP_TYPE_POOL,false);
					item->setMark( true );
     			}else {
     				poolimg = GetPoolCard(2,c_PoolCards[i].chCard,&h);
     				item = new DJGameImageItem(poolimg,canvas(),m,c_PoolCards[i].chCard,seat,DJGAME_DESKTOP_TYPE_POOL,false);
     				item->setMark( false );
     			}
    			item->setHAlignment(Qt::AlignRight);
    			item->move(x,y);
    			item->setZ(z);
    			z++;
    			item->show();
    			item->setMatrix(m);
    			
    			x -= poolimg.width();
    			if(x-poolimg.width() <ex)
    			{
    				x = sx;
    				y += (poolimg.height()-h);
    				if(y+(poolimg.height()-h) > ey)
    				{
    					sy -= h;
    					x = sx;
    					y = sy;
    				}
    			}
    		}
    	}
	}
}
void DJGameMJDesktop::RepaintView3PoolCards(quint8 seat)
{
	if ( 0 == m_styleOfPoolCards ) {
    	int sx,sy;
    	QImage poolimg;
    	int cx,cy,iSelfHeight,h;
    	GetDesktopCenterPoint(&cx,&cy,&iSelfHeight);
    	
    	quint8 chWallLen = GetWallLength(1);
    	QImage imgwall = GetWallCard(1,&h);
    	sx = cx + (chWallLen*imgwall.width())/2;
    	
    	chWallLen = GetWallLength(2);
    	imgwall = GetWallCard(2,&h);
    	sy = cy - (chWallLen*(imgwall.height()-h))/2;
    	
    	QMatrix m = GetCurrentMatrix();
    	int x = sx,y=sy;
    	DJGameImageItem *item;
    	int z=300;
    	for(int i= 0;i<c_chPoolPages;i++)
    	{
    		if(c_PoolCards[i].chSeat == seat)
    		{
    			if( i == c_chPoolPages  - 1 ) {
    				poolimg = GetPoolCard(3,c_PoolCards[i].chCard,&h,1.5);
    				item = new DJGameImageItem(poolimg,canvas(),m,c_PoolCards[i].chCard,seat,DJGAME_DESKTOP_TYPE_POOL,false);
    				item->setMark( true );
    				item->setHAlignment(Qt::AlignHCenter);
    				item->setVAlignment(Qt::AlignBottom);
    				item->move( cx, sy );
    				item->setZ(z);
    				item->setMatrix(m); 
    				item->show(); 
    				return;
     			}else {
     				poolimg = GetPoolCard(3,c_PoolCards[i].chCard,&h);
     				item = new DJGameImageItem(poolimg,canvas(),m,c_PoolCards[i].chCard,seat,DJGAME_DESKTOP_TYPE_POOL,false);
     				item->setMark( false );
     			}
    			item->setHAlignment(Qt::AlignRight);
    			item->move(x,y);
    			item->setZ(z);
    			z++;
    			item->show();
    			item->setMatrix(m);
    			
    			x -= poolimg.width();
    			if( x < cx )
    			{
    				x = sx;
    				y += poolimg.height()-h;
    				if( y > cy )
    				{
    					x = sx;
    					y = sy;
    				}
    			}
    		}
    	}
	}else if ( 1 == m_styleOfPoolCards ) {
		int sx,sy,ex,ey;
    	int cx,cy,iSelfHeight,h;
    	GetDesktopCenterPoint(&cx,&cy,&iSelfHeight);
    	
    	QImage poolimg = GetPoolCard(3,1,&h);
    	sx = cx + (poolimg.width()/2);
    	sy = cy - ((poolimg.height()-h)/2)+h;
    	
    	quint8 chWallLen = GetWallLength(1);
    	QImage imgwall = GetWallCard(1,&h);
    	ex = cx - (chWallLen*imgwall.width())/2;
    	
    	chWallLen = GetWallLength(2);
    	imgwall = GetWallCard(2,&h);
    	ey = cy - (chWallLen*(imgwall.height()-h))/2;
    	
    	QMatrix m = GetCurrentMatrix();
    	int x = sx,y=sy;
    	DJGameImageItem *item;
    	int z=100;
    	for(int i= 0;i<c_chPoolPages;i++)
    	{
    		if(c_PoolCards[i].chSeat == seat)
    		{
    			if( i == c_chPoolPages  - 1 ) {
    				poolimg = GetPoolCard(3,c_PoolCards[i].chCard,&h,1.5);
					item = new DJGameImageItem(poolimg,canvas(),m,c_PoolCards[i].chCard,seat,DJGAME_DESKTOP_TYPE_POOL,false);
					item->setMark( true );
     			}else {
     				poolimg = GetPoolCard(3,c_PoolCards[i].chCard,&h);
     				item = new DJGameImageItem(poolimg,canvas(),m,c_PoolCards[i].chCard,seat,DJGAME_DESKTOP_TYPE_POOL,false);
     				item->setMark( false );
     			}
    			item->setHAlignment(Qt::AlignRight);
    			item->setVAlignment(Qt::AlignBottom);
    			item->move(x,y);
    			item->setZ(z);
    			z --;
    			item->show();
    			item->setMatrix(m);
    			
    			y -= (poolimg.height()-h);
    			if(y-(poolimg.height()) < ey)
    			{
    				y = sy;
    				x -= poolimg.width();
    				if(x-poolimg.width()<ex)
    				{
    					sy -= h;
    					x = sx;
    					y = sy;
    				}
    			}
    		}
    	}
	}
}
void DJGameMJDesktop::RepaintView4PoolCards(quint8 seat)
{
	if ( 0 == m_styleOfPoolCards ) {
    	int sx,sy;
    	QImage poolimg;
    	int cx,cy,iSelfHeight,h;
    	GetDesktopCenterPoint(&cx,&cy,&iSelfHeight);
    	
    	quint8 chWallLen = GetWallLength(1);
    	QImage imgwall = GetWallCard(1,&h);
    	sx = cx + (chWallLen*imgwall.width())/2;
    	
    	chWallLen = GetWallLength(2);
    	imgwall = GetWallCard(2,&h);
    	sy = cy + (chWallLen*(imgwall.height()-h))/2 + 2 * h;
    	
    	QMatrix m = GetCurrentMatrix();
    	int x = sx,y=sy;
    	DJGameImageItem *item;
    	int z= 300;
    	for(int i= 0;i<c_chPoolPages;i++)
    	{
    		if(c_PoolCards[i].chSeat == seat)
    		{
    			if( i == c_chPoolPages  - 1 ) {
    				poolimg = GetPoolCard(4,c_PoolCards[i].chCard,&h,1.5);
    				item = new DJGameImageItem(poolimg,canvas(),m,c_PoolCards[i].chCard,seat,DJGAME_DESKTOP_TYPE_POOL,false);
    				item->setMark( true );
    				//item->setHAlignment(Qt::AlignRight);
    				item->setVAlignment(Qt::AlignVCenter);
    				item->move( sx, cy );
    				item->setZ(z);
    				item->setMatrix(m); 
    				item->show(); 
    				return;
     			}else {
     				poolimg = GetPoolCard(4,c_PoolCards[i].chCard,&h);
     				item = new DJGameImageItem(poolimg,canvas(),m,c_PoolCards[i].chCard,seat,DJGAME_DESKTOP_TYPE_POOL,false);
     				item->setMark( false );
     			}
     			item->setHAlignment(Qt::AlignRight);
    			item->setVAlignment(Qt::AlignBottom);
    			item->move(x,y);
    			item->setZ(z);
    			z--;
    			item->show();
    			item->setMatrix(m);
    			
    			y -= poolimg.height()-h;
    			if( y < cy )
    			{
    				y = sy;
    				x -= poolimg.width();
    				if( x < cx )
    				{
    					x = sx;
    					y = sy;
    				}
    			}
    		}
    	}
	}else if ( 1 == m_styleOfPoolCards ) {
		int sx,sy,ex,ey;
    	int cx,cy,iSelfHeight,h;
    	GetDesktopCenterPoint(&cx,&cy,&iSelfHeight);
    	
    	QImage poolimg = GetPoolCard(2,1,&h);
    	sx = cx + (poolimg.width()/2);
    	sy = cy + ((poolimg.height()-h)/2)+h;
    	
    	quint8 chWallLen = GetWallLength(1);
    	QImage imgwall = GetWallCard(1,&h);
    	ex = cx + (chWallLen*imgwall.width())/2;
    	
    	chWallLen = GetWallLength(2);
    	imgwall = GetWallCard(2,&h);
    	ey = cy - (chWallLen*(imgwall.height()-h))/2;
    	
    	QMatrix m = GetCurrentMatrix();
    	int x = sx,y=sy;
    	DJGameImageItem *item;
    	int z= 100;
    	for(int i= 0;i<c_chPoolPages;i++)
    	{
    		if(c_PoolCards[i].chSeat == seat)
    		{
    			if( i == c_chPoolPages  - 1 ) {
    				poolimg = GetPoolCard(4,c_PoolCards[i].chCard,&h,1.5);
					item = new DJGameImageItem(poolimg,canvas(),m,c_PoolCards[i].chCard,seat,DJGAME_DESKTOP_TYPE_POOL,false);
					item->setMark( true );
     			}else {
     				poolimg = GetPoolCard(4,c_PoolCards[i].chCard,&h);
     				item = new DJGameImageItem(poolimg,canvas(),m,c_PoolCards[i].chCard,seat,DJGAME_DESKTOP_TYPE_POOL,false);
     				item->setMark( false );
     			}
    			item->setVAlignment(Qt::AlignBottom);
    			item->move(x,y);
    			item->setZ(z);
    			z --;
    			item->show();
    			item->setMatrix(m);
    			
    			x += poolimg.width();
    			if(x+poolimg.width() >ex)
    			{
    				x = sx;
    				y -= (poolimg.height()-h);
    				if(y-(poolimg.height()-h) <ey)
    				{
    					sy -= h;
    					x = sx;
    					y = sy;
    				}
    			}
    		}
    	}
	}
}
void DJGameMJDesktop::RepaintPlayerCards(quint8 seat)
{
	quint8 chView = MapSeat2View(seat);
	ClearImage(0,seat,DJGAME_DESKTOP_TYPE_HAND);
	ClearImage(0,seat,DJGAME_DESKTOP_TYPE_STATIC);
	switch(chView)
	{
		case 1 :
			RepaintView1Cards(&(c_playerNormalHu[seat]),c_chPlayerCards[seat],c_chPlayerFlower[seat]);
			break;
		case 2 :
			RepaintView2Cards(&(c_playerNormalHu[seat]),c_chPlayerCards[seat],c_chPlayerFlower[seat]);
			break;
		case 3 :
			RepaintView3Cards(&(c_playerNormalHu[seat]),c_chPlayerCards[seat],c_chPlayerFlower[seat]);
			break;
		case 4 :
			RepaintView4Cards(&(c_playerNormalHu[seat]),c_chPlayerCards[seat],c_chPlayerFlower[seat]);
			break;
	}
}

void DJGameMJDesktop::RepaintView1Cards(MahjongHUTypeWrap *pstatic ,quint8 *pcards,quint8 *pflowers)
{
	djDebug()<<"DJGameMJDesktop::RepaintView1Cards";
	QImage img;
	int i,j,h;
	QMatrix m = GetCurrentMatrix();
	quint8 seat = MapView2Seat(1);
	if(c_bPrivate[seat])
		img = GetView1Card(1,false,false,&h);
	else 
		img = GetView1Card(1,false,true,&h);
	qreal scale	= 1;
	quint16 shWidth = static_cast<int>(img.width()*c_chHuPages+(15*scale));
	int cx,cy,iSelf;
	GetDesktopCenterPoint(&cx,&cy,&iSelf);
	int x = cx + shWidth/2;
	int y = GetRealHeight()-img.height()-20;
	
	DJGameMahjongItem *mjitem;
	int z = 100;
	for(i=c_chHuPages-1;i>=0;i--)
	{
		if(pcards[i] != 0)
		{
			img = GetView1Card(pcards[i],false,false,&h);
			mjitem = new DJGameMahjongItem(img,canvas(),m,pcards[i],seat,DJGAME_DESKTOP_TYPE_HAND,false);
			mjitem->setHAlignment(Qt::AlignRight);
			mjitem->setdy(15);
			mjitem->setZ(z);
			z--;
			mjitem->move(x,y);
			mjitem->show();
			mjitem->setMatrix(m);
			x -= img.width();
			if(i==c_chHuPages-1)
				x-=static_cast<int>(10*scale);
		}else if(i==c_chHuPages-1)
		{
			x -= static_cast<int>(img.width()+10*scale);
		}
	}
	DJGameImageItem *item;
	x -= static_cast<int>(10*scale);
	for(i=0;i<8;i++)
	{
		if(pstatic->cardwrap[i].chExist != 0)
		{
			for(j=2;j>=0;j--)
			{
				if( pstatic->cardwrap[i].chCardWrapName == MAHJONG_CARDWRAP_NAME_KE
					&& pstatic->cardwrap[i].chWrap[3] != 0
					&& MAHJONG_ISDIY(pstatic->cardwrap[i].chWrap[0]) )
				{
					img = GetView1Card(pstatic->cardwrap[i].chWrap[j],true,true,&h);
				}else
				{
					img = GetView1Card(pstatic->cardwrap[i].chWrap[j],true,false,&h);
				}
				item = new DJGameImageItem(img,canvas(),m,pstatic->cardwrap[i].chWrap[j],seat,DJGAME_DESKTOP_TYPE_STATIC,false);
				item->setHAlignment(Qt::AlignRight);
				item->setZ(z);
				z--;
				item->move(x,y);
				item->show();
				item->setMatrix(m);
				if( pstatic->cardwrap[i].chCardWrapName == MAHJONG_CARDWRAP_NAME_KE
					&& pstatic->cardwrap[i].chWrap[3] != 0
					&& j == 1 )
				{
					int th;
					QImage imgGang = GetView1Card(pstatic->cardwrap[i].chWrap[0],true,false,&th);
					item = new DJGameImageItem(imgGang,canvas(),m,pstatic->cardwrap[i].chWrap[j],seat,DJGAME_DESKTOP_TYPE_STATIC,false);
					item->setHAlignment(Qt::AlignRight);
					item->setVAlignment(Qt::AlignBottom);
					item->move(x,y+img.height()-h);
					item->setZ(z+5);
					item->show();
					item->setMatrix(m);
				}
				x -= img.width();
			}//for j
			x -= static_cast<int>(10*scale);
		}//if != 0
	}//for i
	if(c_bPlayerTing[seat])
	{
		djDebug()<<"view 1 is tinging"<<seat;
		x = 0;
		y = GetRealHeight()-iSelf;
		QPixmap pix = 	QPixmap(":/BaseRes/image/mahjong/ting.png");
		img = pix.convertToImage();
		item = new DJGameImageItem(img,canvas(),m,0xFF,seat,DJGAME_DESKTOP_TYPE_STATIC,false);
		item->move(x,y);
		item->show();
		item->setMatrix(m);
	}
}
void DJGameMJDesktop::RepaintView2Cards(MahjongHUTypeWrap *pstatic ,quint8 *pcards,quint8 *pflowers)
{
	QImage img;
	int i,j,h;
	QMatrix m = GetCurrentMatrix();
	quint8 seat = MapView2Seat(2);
	if(c_bPrivate[seat])
		img = GetView2Card(2,false,false,&h);
	else 
		img = GetView2Card(2,false,true,&h);
	qreal scale	= 1;
	quint16 shHeight = static_cast<int>((img.height()-h)*c_chHuPages+(15*scale));
	int cx,cy,iSelf;
	GetDesktopCenterPoint(&cx,&cy,&iSelf);
	int x = DJGAME_MAHJONG_DESKTOP_AVATAR_MAXWIDTH+15;
	int y = cy + shHeight/2-(img.height()-h);
	DJGameImageItem *item;
	int z = 100;
	for(i=c_chHuPages-1;i>=0;i--)
	{
		if(pcards[i] != 0)
		{
			img = GetView2Card(pcards[i],false,false,&h);
			item = new DJGameImageItem(img,canvas(),m,pcards[i],seat,DJGAME_DESKTOP_TYPE_STATIC,false);
			item->move(x,y);
			item->setZ(z);
			z --;
			item->show();
			item->setMatrix(m);
			y -= (img.height()-h);
			if(i==c_chHuPages-1)
				y-=static_cast<int>(10*scale);
		}else if(i==c_chHuPages-1)
		{
			y -= static_cast<int>((img.height()-h)+10*scale);
		}
	}
	y -= static_cast<int>(10*scale);
	for(i=0;i<8;i++)
	{
		if(pstatic->cardwrap[i].chExist != 0)
		{
			for(j=2;j>=0;j--)
			{
				if( pstatic->cardwrap[i].chCardWrapName == MAHJONG_CARDWRAP_NAME_KE
					&& pstatic->cardwrap[i].chWrap[3] != 0
					&& MAHJONG_ISDIY(pstatic->cardwrap[i].chWrap[0]))
				{
					img = GetView2Card(pstatic->cardwrap[i].chWrap[j],true,true,&h);
				}else
				{
					img = GetView2Card(pstatic->cardwrap[i].chWrap[j],true,false,&h);
				}
				item = new DJGameImageItem(img,canvas(),m,pstatic->cardwrap[i].chWrap[j],seat,DJGAME_DESKTOP_TYPE_STATIC,false);
				item->move(x,y);
				item->setZ(z);
				z --;
				item->show();
				item->setMatrix(m);
				if( pstatic->cardwrap[i].chCardWrapName == MAHJONG_CARDWRAP_NAME_KE
					&& pstatic->cardwrap[i].chWrap[3] != 0
					&& j == 1 ) 
				{
					int th;
					QImage imgGang = GetView2Card(pstatic->cardwrap[i].chWrap[0],true,false,&th);
					item = new DJGameImageItem(imgGang,canvas(),m,pstatic->cardwrap[i].chWrap[j],seat,DJGAME_DESKTOP_TYPE_STATIC,false);
					item->setVAlignment(Qt::AlignBottom);
					item->move(x,y+img.height()-h);
					item->setZ(z+5);
					item->show();
					item->setMatrix(m);
				}
				y -= (img.height()-h);
			}//for j
			y -= static_cast<int>(10*scale);
		}//if != 0
	}//for i
	if(c_bPlayerTing[seat])
	{
		djDebug()<<"view 2 is tinging"<<seat;
		x = DJGAME_MAHJONG_DESKTOP_AVATAR_MAXWIDTH;
		y = cy;
		QPixmap pix = 	QPixmap(":/BaseRes/image/mahjong/ting2.png");
		img = pix.convertToImage();
		item = new DJGameImageItem(img,canvas(),m,0xFF,seat,DJGAME_DESKTOP_TYPE_STATIC,false);
		item->setVAlignment(Qt::AlignVCenter);
		item->move(x,y);
		item->show();
		item->setMatrix(m);
	}
}
void DJGameMJDesktop::RepaintView3Cards(MahjongHUTypeWrap *pstatic ,quint8 *pcards,quint8 *pflowers)
{
	QImage img;
	int i,j,h;
	QMatrix m = GetCurrentMatrix();
	quint8 seat = MapView2Seat(3);
	if(c_bPrivate[seat])
		img = GetView3Card(3,false,false,&h);
	else 
		img = GetView3Card(3,false,true,&h);
	qreal scale	= 1;
	quint16 shWidth = static_cast<int>(img.width()*c_chHuPages+(15*scale));
	int cx,cy,iSelf;
	GetDesktopCenterPoint(&cx,&cy,&iSelf);
	int y = DJGAME_MAHJONG_DESKTOP_AVATAR_MAXHEIGHT+15;
	int x = cx - shWidth/2;
	
	DJGameImageItem *item;
	int z= 100;
	for(i=c_chHuPages-1;i>=0;i--)
	{
		if(pcards[i] != 0)
		{
			img = GetView3Card(pcards[i],false,false,&h);
			item = new DJGameImageItem(img,canvas(),m,pcards[i],seat,DJGAME_DESKTOP_TYPE_STATIC,false);
			item->move(x,y);
			item->setZ(z);
			item->show();
			item->setMatrix(m);
			x += img.width()-1;
			if(i==c_chHuPages-1)
				x +=static_cast<int>(10*scale);
		}else if(i==c_chHuPages-1)
		{
			x += static_cast<int>(img.width()+10*scale);
		}
	}
	x += static_cast<int>(10*scale);
	for(i=0;i<8;i++)
	{
		if(pstatic->cardwrap[i].chExist != 0)
		{
			for(j=2;j>=0;j--)
			{
				if( pstatic->cardwrap[i].chCardWrapName == MAHJONG_CARDWRAP_NAME_KE
					&& pstatic->cardwrap[i].chWrap[3] != 0
					&& MAHJONG_ISDIY(pstatic->cardwrap[i].chWrap[0]) )
				{
					img = GetView3Card(pstatic->cardwrap[i].chWrap[j],true,true,&h);
				}else
				{
					img = GetView3Card(pstatic->cardwrap[i].chWrap[j],true,false,&h);
				}
				item = new DJGameImageItem(img,canvas(),m,pstatic->cardwrap[i].chWrap[j],seat,DJGAME_DESKTOP_TYPE_STATIC,false);
				item->move(x,y);
				item->setZ(z);
				item->show();
				item->setMatrix(m);
				if( pstatic->cardwrap[i].chCardWrapName == MAHJONG_CARDWRAP_NAME_KE
					&& pstatic->cardwrap[i].chWrap[3] != 0
					&& j == 1 )
				{
					int th;
					QImage imgGang = GetView3Card(pstatic->cardwrap[i].chWrap[0],true,false,&th);
					item = new DJGameImageItem(imgGang,canvas(),m,pstatic->cardwrap[i].chWrap[j],seat,DJGAME_DESKTOP_TYPE_STATIC,false);
					item->setVAlignment(Qt::AlignBottom);
					item->move(x,y+img.height()-h);
					item->setZ(z+5);
					item->show();
					item->setMatrix(m);
				}
				x += img.width();
			}//for j
			x += static_cast<int>(10*scale);
		}//if != 0
	}//for i
	if(c_bPlayerTing[seat])
	{
		djDebug()<<"view 3 is tinging"<<seat;
		x = cx;
		y = DJGAME_MAHJONG_DESKTOP_AVATAR_MAXHEIGHT;
		QPixmap pix = 	QPixmap(":/BaseRes/image/mahjong/ting.png");
		img = pix.convertToImage();
		item = new DJGameImageItem(img,canvas(),m,0xFF,seat,DJGAME_DESKTOP_TYPE_STATIC,false);
		item->setHAlignment(Qt::AlignHCenter);
		item->move(x,y);
		item->show();
		item->setMatrix(m);
	}
}
void DJGameMJDesktop::RepaintView4Cards(MahjongHUTypeWrap *pstatic ,quint8 *pcards,quint8 *pflowers)
{
	QImage img;
	int i,j,h;
	QMatrix m = GetCurrentMatrix();
	quint8 seat = MapView2Seat(4);
	if(c_bPrivate[seat])
		img = GetView4Card(4,false,false,&h);
	else 
		img = GetView4Card(2,false,true,&h);
	qreal scale	= 1;
	quint16 shHeight = static_cast<int>((img.height()-h)*c_chHuPages+(15*scale));
	int cx,cy,iSelf;
	GetDesktopCenterPoint(&cx,&cy,&iSelf);
	int x = GetRealWidth()-(DJGAME_MAHJONG_DESKTOP_AVATAR_MAXWIDTH+15);
	int y = cy - shHeight/2;
	
	DJGameImageItem *item;
	int z=100;
	for(i=c_chHuPages-1;i>=0;i--)
	{
		if(pcards[i] != 0)
		{
			img = GetView4Card(pcards[i],false,false,&h);
			item = new DJGameImageItem(img,canvas(),m,pcards[i],seat,DJGAME_DESKTOP_TYPE_STATIC,false);
			item->setHAlignment(Qt::AlignRight);
			item->move(x,y);
			item->setZ(z);
			z ++;
			item->show();
			item->setMatrix(m);
			y += (img.height()-h);
			if(i==c_chHuPages-1)
				y+=static_cast<int>(10*scale);
		}else if(i==c_chHuPages-1)
		{
			y += static_cast<int>((img.height()-h)+10*scale);
		}
	}
	y += static_cast<int>(10*scale);
	for(i=0;i<8;i++)
	{
		if(pstatic->cardwrap[i].chExist != 0)
		{
			for(j=2;j>=0;j--)
			{
				if( pstatic->cardwrap[i].chCardWrapName == MAHJONG_CARDWRAP_NAME_KE
					&& pstatic->cardwrap[i].chWrap[3] != 0
					&& MAHJONG_ISDIY(pstatic->cardwrap[i].chWrap[0]) )
				{
					img = GetView4Card(pstatic->cardwrap[i].chWrap[j],true,true,&h);
				}else
				{
					img = GetView4Card(pstatic->cardwrap[i].chWrap[j],true,false,&h);
				}
				item = new DJGameImageItem(img,canvas(),m,pstatic->cardwrap[i].chWrap[j],seat,DJGAME_DESKTOP_TYPE_STATIC,false);
				item->setHAlignment(Qt::AlignRight);
				item->move(x,y);
				item->setZ(z);
				z++;
				item->show();
				item->setMatrix(m);
				if( pstatic->cardwrap[i].chCardWrapName == MAHJONG_CARDWRAP_NAME_KE
					&& pstatic->cardwrap[i].chWrap[3] != 0
					&& j == 1 )
				{
					int th;
					QImage imgGang = GetView4Card(pstatic->cardwrap[i].chWrap[0],true,false,&th);
					item = new DJGameImageItem(imgGang,canvas(),m,pstatic->cardwrap[i].chWrap[j],seat,DJGAME_DESKTOP_TYPE_STATIC,false);
					item->setHAlignment(Qt::AlignRight);
					item->setVAlignment(Qt::AlignBottom);
					item->move(x,y+img.height()-h);
					item->setZ(z+5);
					item->show();
					item->setMatrix(m);
				}
				y += (img.height()-h);
			}//for j
			y += static_cast<int>(10*scale);
		}//if != 0
	}//for i
	if(c_bPlayerTing[seat])
	{ 
		djDebug()<<"view 4 is tinging"<<seat;
		x = GetRealWidth()-DJGAME_MAHJONG_DESKTOP_AVATAR_MAXWIDTH;
		y = cy;
		QPixmap pix = 	QPixmap(":/BaseRes/image/mahjong/ting2.png");
		img = pix.convertToImage();
		item = new DJGameImageItem(img,canvas(),m,0xFF,seat,DJGAME_DESKTOP_TYPE_STATIC,false);
		item->setHAlignment(Qt::AlignRight);
		item->setVAlignment(Qt::AlignVCenter);
		item->move(x,y);
		item->show();
		item->setMatrix(m);
	}
}

void DJGameMJDesktop::RepaintView1WallCards()
{
	int x,y,iSelfHeight;
	int i,h;
	GetDesktopCenterPoint(&x,&y,&iSelfHeight);
	quint8 ch2WallLen = GetWallLength(2);
	QImage img = GetWallCard(2,&h);
	y += (img.height()-h)*ch2WallLen/2 + h*2;
	quint8 chWallLen = GetWallLength(1);
	quint8 seat = MapView2Seat(1);
	
	img = GetWallCard(1,&h);
	x -= (chWallLen*img.width())/2;
	DJGameImageItem *item;
	QMatrix m = GetCurrentMatrix();
	int cx = x;
	for(i=1;i<=chWallLen;i++)
	{
		item = new DJGameImageItem(img,canvas(),m,(i-1)*2+1,seat,DJGAME_DESKTOP_TYPE_WALL,false);
		item->move( cx,y);
		item->setZ(130+i*2);
		item->show();
		item->setMatrix(m);
		cx += img.width();
	}
	cx = x;
	y += h;
	for(i=1;i<=chWallLen;i++)
	{
		item = new DJGameImageItem(img,canvas(),m,i*2,seat,DJGAME_DESKTOP_TYPE_WALL,false);
		item->move( cx,y);
		item->setZ(100+i*2-1);
		item->show();
		item->setMatrix(m);
		cx += img.width();
	}
	
}
void DJGameMJDesktop::RepaintView2WallCards()
{
	int x,y,iSelfHeight;
	int i,h;
	GetDesktopCenterPoint(&x,&y,&iSelfHeight);
	quint8 ch1WallLen = GetWallLength(1);
	QImage img = GetWallCard(1,&h);
	x -= (img.width())*ch1WallLen/2;
	quint8 chWallLen = GetWallLength(2);
	quint8 seat = MapView2Seat(2);
	
	img = GetWallCard(2,&h);
	y -= (chWallLen*(img.height()-h))/2;
	DJGameImageItem *item;
	QMatrix m = GetCurrentMatrix();
	int cy = y;
	for(i=1;i<=chWallLen;i++)
	{
		item = new DJGameImageItem(img,canvas(),m,(i-1)*2+1,seat,DJGAME_DESKTOP_TYPE_WALL,false);
		item->setHAlignment(Qt::AlignRight);
		item->move( x,cy);
		item->setZ(130+i*2);
		item->show();
		item->setMatrix(m);
		cy += (img.height()-h);
	}
	y += h;
	cy = y;
	for(i=1;i<=chWallLen;i++)
	{
		item = new DJGameImageItem(img,canvas(),m,i*2,seat,DJGAME_DESKTOP_TYPE_WALL,false);
		item->setHAlignment(Qt::AlignRight);
		item->move( x,cy);
		item->setZ(100+i*2-1);
		item->show();
		item->setMatrix(m);
		cy += (img.height()-h);
	}
}
void DJGameMJDesktop::RepaintView3WallCards()
{
	int x,y,iSelfHeight;
	int i,h;
	GetDesktopCenterPoint(&x,&y,&iSelfHeight);
	quint8 ch2WallLen = GetWallLength(2);
	QImage img = GetWallCard(2,&h);
	y -= (img.height()-h)*ch2WallLen/2;
	quint8 chWallLen = GetWallLength(3);
	quint8 seat = MapView2Seat(3);
	
	img = GetWallCard(3,&h);
	x += (chWallLen*img.width())/2;
	y -= (img.height()+h);
	DJGameImageItem *item;
	QMatrix m = GetCurrentMatrix();
	int cx = x;
	for(i=1;i<=chWallLen;i++)
	{
		item = new DJGameImageItem(img,canvas(),m,(i-1)*2+1,seat,DJGAME_DESKTOP_TYPE_WALL,false);
		item->setHAlignment(Qt::AlignRight);
		item->move( cx,y);
		item->setZ(130+i*2);
		item->show();
		item->setMatrix(m);
		cx -= img.width();
	}
	cx = x;
	y += h;
	for(i=1;i<=chWallLen;i++)
	{
		item = new DJGameImageItem(img,canvas(),m,i*2,seat,DJGAME_DESKTOP_TYPE_WALL,false);
		item->setHAlignment(Qt::AlignRight);
		item->move( cx,y);
		item->setZ(100+i*2-1);
		item->show();
		item->setMatrix(m);
		cx -= img.width();
	}
}
void DJGameMJDesktop::RepaintView4WallCards()
{
	int x,y,iSelfHeight;
	int i,h;
	GetDesktopCenterPoint(&x,&y,&iSelfHeight);
	quint8 ch1WallLen = GetWallLength(1);
	QImage img = GetWallCard(1,&h);
	x += (img.width())*ch1WallLen/2;
	quint8 chWallLen = GetWallLength(2);
	quint8 seat = MapView2Seat(4);
	
	img = GetWallCard(2,&h);
	y += (chWallLen*(img.height()-h))/2;
	y += h;
	DJGameImageItem *item;
	QMatrix m = GetCurrentMatrix();
	int cy = y;
	for(i=1;i<=chWallLen;i++)
	{
		item = new DJGameImageItem(img,canvas(),m,(i-1)*2+1,seat,DJGAME_DESKTOP_TYPE_WALL,false);
		item->setVAlignment(Qt::AlignBottom);
		item->move( x,cy);
		item->setZ(130-i*2);
		item->show();
		item->setMatrix(m);
		cy -= (img.height()-h);
	}
	y += h;
	cy = y;
	for(i=1;i<=chWallLen;i++)
	{
		item = new DJGameImageItem(img,canvas(),m,i*2,seat,DJGAME_DESKTOP_TYPE_WALL,false);
		item->setVAlignment(Qt::AlignBottom);
		item->move( x,cy);
		item->setZ(100-(i*2-1));
		item->show();
		item->setMatrix(m);
		cy -= (img.height()-h);
	}
}

void DJGameMJDesktop::SetWallStartPoint(quint8 chStart)
{
	c_chWallStartSeat = (chStart>>5);
	if(c_chWallStartSeat == 0)
		c_chWallStartSeat = 1;
	c_chWallStartPos = (chStart&0x1F);
	quint8 chWallLen = GetWallLength(MapSeat2View(c_chWallStartSeat));
	if(c_chWallStartPos > chWallLen || c_chWallStartPos < 1)
		c_chWallStartPos = 1;
	c_chWallStartPos = c_chWallStartPos*2-1;
}
void DJGameMJDesktop::RemoveWallCard(quint8 chPages,bool bFormEnd)
{
	if(bFormEnd)
		c_chWallEndRemoves += chPages;
	else
		c_chWallRemoves += chPages;
}

void DJGameMJDesktop::ClearPoolCards(quint8 seat)
{
	ClearImage(0,seat,DJGAME_DESKTOP_TYPE_POOL);
}
void DJGameMJDesktop::ClearHandCards(quint8 seat)
{
	ClearImage(0,seat,DJGAME_DESKTOP_TYPE_HAND);
	ClearImage(0,seat,DJGAME_DESKTOP_TYPE_STATIC);
}
void DJGameMJDesktop::ClearFlowerCards(quint8 seat)
{
	ClearImage(0,seat,DJGAME_DESKTOP_TYPE_FLOWER);
}
void DJGameMJDesktop::SetFx(qreal fx)
{
	c_Fx = fx;
}

////////////////////////////////////////////////////////////////////////////
DJGameMahjongItem::DJGameMahjongItem( QImage img, Q3Canvas *cv,QMatrix &m,unsigned short tid,unsigned short sid,unsigned short type,bool bMovable)
					:DJGameImageItem(img,cv,m,tid,sid,type,bMovable)
{
	c_x = 0;
	c_y = 0;
	c_dy = 15;
	c_bSelected = false;
	
	QPen pen( Qt::red );
	pen.setWidth( 2 );
	setPen( pen );
}

DJGameMahjongItem::~DJGameMahjongItem()
{
}
void DJGameMahjongItem::move(double x, double y)
{
	c_x = x;
	c_y = y;

	if(c_bSelected)
	{
		y -= c_dy;
	}
	DJGameImageItem::move(x,y);
}
void DJGameMahjongItem::setSelected(bool b)
{
	c_bSelected = b;
	setMark(c_bSelected);
	move(c_x,c_y);
}
void DJGameMahjongItem::setdy(int d)
{
	c_dy = d;
}
