//魔弹传递
void BackgroundEngine::missilePass(bool rightOrder,int dst,int src,bool* passed,int missilePoint)
{
    int i;
    PlayerEntity* srcPlayer = getPlayerByID(src);
    PlayerEntity* dstPlayer = getPlayerByID(dst);
    PlayerEntity* next = dstPlayer;
    passed[srcPlayer->getSeatNum()] = true;

    for(i = 0;i < this->playerList.size();i++)
    {
        if(!*(passed+i) && (i != dstPlayer->getSeatNum()))
            break;
    }
    if(i == this->playerList.size())
    {
        for(int j = 0;j < this->playerList.size();j++)
            *(passed + j) = false;
    }

    if(rightOrder)
    {   
        while((next->getColor() == dstPlayer->getColor()) || *(passed + next->getSeatNum()))
            next = next->getNext();

        coder.askForMissile(dst,src,missilePoint,next->getID());
    }
    else
    {
        while((next->getColor() == dstPlayer->getColor()) || passed[next->getSeatNum()])
            next = this->getFront(next);

        coder.askForMissile(dst,src,missilePoint,next->getID());
    }
}
//【充盈】
int MoQiang::ChongYing(Action* action)
{

   //检查客户端返回的是否为魔法牌&&雷系牌
	  int cardID = action->card_ids(0);
	  CardEntity* card = getCardByID(cardID);
		if( card->getType() != TYPE_MAGIC&&card->getElement()!=ELEMENT_THUNDER ){
			return GE_SUCCESS;
		}
		cardCount=0;
		used_ChongYing=true;
		SkillMsg skill_msg;
	   //丢弃魔法牌&&雷系牌
		Coder::skillNotice(id, id, CHONG_YING, skill_msg);
		engine->sendMessage(-1, MSG_SKILL, skill_msg);
		CardMsg show_card;
		Coder::showCardNotice(id, 1, cardID, show_card);
		engine->sendMessage(-1, MSG_CARD, show_card);

		//所有移牌操作都要用setStateMoveXXXX,ToHand的话要填好HARM,就算不是伤害
	PlayerEntity* it = this->getPre();
	HARM chongying;
	chongying.cause =CHONG_YING;
	chongying.point = 1;
	chongying.srcID = id;
	chongying.type = HARM_NONE;

	HARM chongying_discard;
	chongying_discard.cause =CHONG_YING_DISCARD;
	chongying_discard.point = 1;
	chongying_discard.srcID = id;
	chongying_discard.type = HARM_NONE;
	
	//先进后出,所以逆出牌顺序压,最后才是魔枪自己明弃法牌
	while(it != this){
		//bool isShown = false, bool canGiveUp = false
		if(it->getColor()!=color)
		engine->pushGameState(new StateRequestHand(it->getID(),chongying, -1, DECK_DISCARD, true, false)); //不能不弃牌
		else
         engine->pushGameState(new StateRequestHand(it->getID(),chongying_discard, -1, DECK_DISCARD, true, true));

		it = it->getPre();
	}
	engine->pushGameState(new StateRequestHand(this->getID(),chongying_discard, -1, DECK_DISCARD, true, true));
	engine->setStateMoveOneCardNotToHand(id, DECK_HAND, -1, DECK_DISCARD, cardID, id, CHONG_YING, true);

	addAction(ACTION_ATTACK,CHONG_YING);
	return GE_URGENT;
}
//测试用
void BackgroundEngine::showTest()
{
    PlayerEntity* player;
    char temp[500];
    QString tempStr;
    for(int i = 0;i < this->getPlayerNum();i++)
    {
        player = this->playerList.at(i);
        sprintf(temp,"player%d:\n",player->getID());
        tempStr.append(temp);

        for(int j = 0;j < player->getHandCardNum();j++)
        {

            tempStr.append(player->getHandCards().at(j)->getName());
            tempStr += QString::number(player->getHandCards().at(j)->getID());
            tempStr += " ";
        }
        tempStr += "\n";
    }

    sprintf(temp,"red team:%d\n",teamArea.getMorale(RED));
    tempStr.append(temp);
    sprintf(temp,"blue team:%d\n",teamArea.getMorale(BLUE));
    tempStr.append(temp);
    emit this->toInforDisplay(tempStr);
}
//查找对应ID的玩家
PlayerEntity* BackgroundEngine::getPlayerByID(int ID)
{
    PlayerEntity* one;
    //待修改,i应该小于实际玩家数
    for(int i = 0;i < this->playerNum;i++)
    {
        one = this->playerList.at(i);
        if(ID == one->getID())
            return one;      
    }
    return NULL;
}
int LingFu::BaiGuiYeXing_Effect()
{
	HARM baiGui;
	baiGui.cause = BAI_GUI_YE_XING;
	baiGui.point = using_LingLiBengJie ? 2 : 1;
	baiGui.srcID = id;
	baiGui.type = HARM_MAGIC;
	if(isExposed){
		PlayerEntity* it = this->getPre();		
		//先进后出,所以逆出牌顺序压
		do{
			if(it->getID() != dstIDs[0] && it->getID() != dstIDs[1]){
				engine->setStateTimeline3(it->getID(), baiGui);
			}
			it = it->getPre();
		}while(it != this->getPre());
	}
	else{
		engine->setStateTimeline3(dstIDs[0], baiGui);
	}
	return GE_URGENT;
}
int LingFu::LeiMing_Effect(Action *action)
{
	int dst1ID = action->dst_ids(0);
	int dst2ID = action->dst_ids(1);
	//填写伤害结构
	HARM leiMing;
	leiMing.cause = LEI_MING;
	leiMing.point = using_LingLiBengJie ? 2 : 1;
	leiMing.srcID = id;
	leiMing.type = HARM_MAGIC;
	//先进后出,所以逆出牌顺序压
	PlayerEntity* start = this->getPre();
	PlayerEntity* it = start;
	do{
		if(it->getID() == dst1ID || it->getID() == dst2ID){
			engine->setStateTimeline3(it->getID(), leiMing);
		}
		it = it->getPre();
	}while(it != start);
	
	//插入了新状态,请return GE_URGENT
	return GE_URGENT;
}
int LingFu::FengXing(Action *action)
{
	int cardID = action->card_ids(0);
	int dst1ID = action->dst_ids(0);
	int dst2ID = action->dst_ids(1);
	list<int> dstIDs;
	dstIDs.push_back(dst1ID);
	dstIDs.push_back(dst2ID);
	//宣告技能
	network::SkillMsg skill;
	Coder::skillNotice(id, dstIDs, FENG_XING, skill);
	engine->sendMessage(-1, MSG_SKILL, skill);
    //所有移牌操作都要用setStateMoveXXXX,ToHand的话要填好HARM,就算不是伤害
	
	HARM fengXing;
	fengXing.cause = FENG_XING;
	fengXing.point = 1;
	fengXing.srcID = id;
	fengXing.type = HARM_NONE;
	//先进后出,所以逆出牌顺序压,由自己开始明弃牌
	PlayerEntity* start = this->getPre();
	PlayerEntity* it = start;
	do{
		//没有手牌就不用弃
		if(it->getHandCardNum() > 0 && (it->getID() == dst1ID || it->getID() == dst2ID)){
			engine->pushGameState(new StateRequestHand(it->getID(), fengXing, -1, DECK_DISCARD, false, false));
		}
		it = it->getPre();
	}while(it != start);
	NianZhou();
	CardMsg show_card;
	Coder::showCardNotice(id, 1, cardID, show_card);
	engine->sendMessage(-1, MSG_CARD, show_card);
	engine->setStateMoveOneCardNotToHand(id, DECK_HAND, -1, DECK_DISCARD, cardID, id, FENG_XING, true);
	//插入了新状态,请return GE_URGENT
	return GE_URGENT;
}
//魔弹处理
void BackgroundEngine::missileProcess(CardEntity* card,int src,int dst)
{
    bool rightOrder;
    BatInfor reply;
    PlayerEntity* nextOpponent;

    //确定传递方向
    nextOpponent = this->getCurrentPlayer()->getNext();
    while(nextOpponent->getColor() == this->currentPlayer->getColor())
        nextOpponent = nextOpponent->getNext();
    if(nextOpponent->getID() == dst)
        rightOrder = true;
    else
        rightOrder = false;

    bool passed[6];
    for(int i = 0;i < this->playerList.size();i++)
    {
        passed[i] = false;
    }
    int missilePoint = 2;

    QList<CardEntity*> cards;


    do
    {
        cards.clear();
        //魔弹传递到下家
        missilePass(rightOrder,dst,src,passed,missilePoint);

        //读取用户回复
        reply = messageBuffer::readBatInfor();

        if(reply.reply == 0)
        {
            //继续传递
            src = dst;
            dst = reply.dstID;
            missilePoint++;
            cards << getCardByID(reply.CardID);
            this->useCard(cards,getPlayerByID(src),getPlayerByID(dst),false);
            continue;
        }
        else if(reply.reply == 1)
        {
            //圣光
            cards << getCardByID(reply.CardID);
            this->useCard(cards,getPlayerByID(dst));
            break;
        }
        else if(reply.reply == 2)
        {
            //无应对
            PlayerEntity* dstPlayer = getPlayerByID(dst);
            bool shieldBlocked = false;
            //检查圣盾
            for(int i = 0;i < dstPlayer->getBasicEffect().size();i++)
            {
                if(dstPlayer->getBasicEffect()[i]->getMagicName() == SHIELDCARD||dstPlayer->getBasicEffect().at(i)->getSpecialityList().contains(tr("天使之墙")))
                {
                    coder.shieldNotic(dst);
                    dstPlayer->removeBasicEffect(dstPlayer->getBasicEffect()[i]);
                    shieldBlocked = true;
                    break;
                }
            }
            if(shieldBlocked)
                break;
            Harm missileHurt;
            missileHurt.harmPoint = missilePoint;
            missileHurt.type = MAGIC;
            //无圣盾,造成伤害
            this->timeLine3(missileHurt,getPlayerByID(src),dstPlayer,"魔弹");
            break;
        }
        else if(reply.reply == 802)
        {
            //继续传递
            src = dst;
            dst = reply.dstID;
            missilePoint++;
            cards << getCardByID(reply.CardID);
            useCard(cards,getPlayerByID(src),getPlayerByID(dst));
            coder.notice("魔导师发动【魔弹融合】");
            continue;
        }

    }while(1);

}