Example #1
0
        /// <summary>
        /// Called when the AI has to do something during the battle phase.
        /// </summary>
        /// <param name="battle">Informations about usable cards.</param>
        /// <returns>A new BattlePhaseAction containing the action to do.</returns>
        public BattlePhaseAction OnSelectBattleCmd(BattlePhase battle)
        {
            Executor.SetBattle(battle);
            foreach (CardExecutor exec in Executor.Executors)
            {
                for (int i = 0; i < battle.ActivableCards.Count; ++i)
                {
                    ClientCard card = battle.ActivableCards[i];
                    if (ShouldExecute(exec, card, ExecutorType.Activate, battle.ActivableDescs[i]))
                    {
                        _dialogs.SendChaining(card.Name);
                        return(new BattlePhaseAction(BattlePhaseAction.BattleAction.Activate, card.ActionIndex));
                    }
                }
            }

            List <ClientCard> attackers = new List <ClientCard>(battle.AttackableCards);

            attackers.Sort(AIFunctions.CompareCardAttack);

            List <ClientCard> defenders = new List <ClientCard>(Duel.Fields[1].GetMonsters());

            defenders.Sort(AIFunctions.CompareDefensePower);

            return(Executor.OnBattle(attackers, defenders));
        }
        private void OnSelectBattleCmd(GameServerPacket packet)
        {
            packet.ReadByte();             // player
            _duel.BattlePhase = new BattlePhase();
            BattlePhase battle = _duel.BattlePhase;

            int count = packet.ReadByte();

            for (int i = 0; i < count; ++i)
            {
                packet.ReadInt32();                 // card id
                int          con  = GetLocalPlayer(packet.ReadByte());
                CardLocation loc  = (CardLocation)packet.ReadByte();
                int          seq  = packet.ReadByte();
                int          desc = packet.ReadInt32();

                ClientCard card = _duel.GetCard(con, loc, seq);
                if (card != null)
                {
                    card.ActionIndex[0] = i;
                    battle.ActivableCards.Add(card);
                    battle.ActivableDescs.Add(desc);
                }
            }

            count = packet.ReadByte();
            for (int i = 0; i < count; ++i)
            {
                packet.ReadInt32();                 // card id
                int          con = GetLocalPlayer(packet.ReadByte());
                CardLocation loc = (CardLocation)packet.ReadByte();
                int          seq = packet.ReadByte();
                packet.ReadByte();                 // diratt

                ClientCard card = _duel.GetCard(con, loc, seq);
                if (card != null)
                {
                    card.ActionIndex[1] = i;
                    battle.AttackableCards.Add(_duel.GetCard(con, loc, seq));
                }
            }

            battle.CanMainPhaseTwo = packet.ReadByte() != 0;
            battle.CanEndPhase     = packet.ReadByte() != 0;

            Connection.Send(CtosMessage.Response, _ai.OnSelectBattleCmd(battle).ToValue());
        }
Example #3
0
        /// <summary>
        /// Called when the AI has to do something during the battle phase.
        /// </summary>
        /// <param name="battle">Informations about usable cards.</param>
        /// <returns>A new BattlePhaseAction containing the action to do.</returns>
        public BattlePhaseAction OnSelectBattleCmd(BattlePhase battle)
        {
            Executor.SetBattle(battle);
            foreach (CardExecutor exec in Executor.Executors)
            {
                for (int i = 0; i < battle.ActivableCards.Count; ++i)
                {
                    ClientCard card = battle.ActivableCards[i];
                    if (ShouldExecute(exec, card, ExecutorType.Activate, battle.ActivableDescs[i]))
                    {
                        _dialogs.SendChaining(card.Name);
                        return new BattlePhaseAction(BattlePhaseAction.BattleAction.Activate, card.ActionIndex);
                    }
                }
            }

            List<ClientCard> attackers = new List<ClientCard>(battle.AttackableCards);
            attackers.Sort(AIFunctions.CompareCardAttack);

            List<ClientCard> defenders = new List<ClientCard>(Duel.Fields[1].GetMonsters());
            defenders.Sort(AIFunctions.CompareDefensePower);

            return Executor.OnBattle(attackers, defenders);
        }
Example #4
0
        /// <summary>
        /// Called when the AI has to do something during the battle phase.
        /// </summary>
        /// <param name="battle">Informations about usable cards.</param>
        /// <returns>A new BattlePhaseAction containing the action to do.</returns>
        public BattlePhaseAction OnSelectBattleCmd(BattlePhase battle)
        {
            Executor.SetBattle(battle);
            foreach (CardExecutor exec in Executor.Executors)
            {
                if (exec.Type == ExecutorType.GoToMainPhase2 && battle.CanMainPhaseTwo && exec.Func()) // check if should enter main phase 2 directly
                {
                    return(ToMainPhase2());
                }
                if (exec.Type == ExecutorType.GoToEndPhase && battle.CanEndPhase && exec.Func()) // check if should enter end phase directly
                {
                    return(ToEndPhase());
                }
                for (int i = 0; i < battle.ActivableCards.Count; ++i)
                {
                    ClientCard card = battle.ActivableCards[i];
                    if (ShouldExecute(exec, card, ExecutorType.Activate, battle.ActivableDescs[i]))
                    {
                        _dialogs.SendChaining(card.Name);
                        return(new BattlePhaseAction(BattlePhaseAction.BattleAction.Activate, card.ActionIndex));
                    }
                }
            }

            // Sort the attackers and defenders, make monster with higher attack go first.
            List <ClientCard> attackers = new List <ClientCard>(battle.AttackableCards);

            attackers.Sort(CardContainer.CompareCardAttack);
            attackers.Reverse();

            List <ClientCard> defenders = new List <ClientCard>(Duel.Fields[1].GetMonsters());

            defenders.Sort(CardContainer.CompareDefensePower);
            defenders.Reverse();

            // Let executor decide which card should attack first.
            ClientCard selected = Executor.OnSelectAttacker(attackers, defenders);

            if (selected != null && attackers.Contains(selected))
            {
                attackers.Remove(selected);
                attackers.Insert(0, selected);
            }

            // Check for the executor.
            BattlePhaseAction result = Executor.OnBattle(attackers, defenders);

            if (result != null)
            {
                return(result);
            }

            if (attackers.Count == 0)
            {
                if (battle.CanMainPhaseTwo)
                {
                    return(ToMainPhase2());
                }
                else if (battle.CanEndPhase)
                {
                    return(ToEndPhase());
                }
            }

            if (defenders.Count == 0)
            {
                // Attack with the monster with the lowest attack first
                for (int i = attackers.Count - 1; i >= 0; --i)
                {
                    ClientCard attacker = attackers[i];
                    if (attacker.Attack > 0)
                    {
                        return(Attack(attacker, null));
                    }
                }
            }
            else
            {
                for (int k = 0; k < attackers.Count; ++k)
                {
                    ClientCard attacker = attackers[k];
                    attacker.IsLastAttacker = (k == attackers.Count - 1);
                    result = Executor.OnSelectAttackTarget(attacker, defenders);
                    if (result != null)
                    {
                        return(result);
                    }
                }
            }

            if (!battle.CanMainPhaseTwo && !battle.CanEndPhase)
            {
                return(Attack(attackers[0], (defenders.Count == 0) ? null : defenders[0]));
            }

            return(battle.CanMainPhaseTwo ? ToMainPhase2() : ToEndPhase());
        }
Example #5
0
        private void OnSelectBattleCmd(BinaryReader packet)
        {
            packet.ReadByte(); // player
            _duel.BattlePhase = new BattlePhase();
            BattlePhase battle = _duel.BattlePhase;

            int count = packet.ReadByte();

            for (int i = 0; i < count; ++i)
            {
                packet.ReadInt32(); // card id
                int          con  = GetLocalPlayer(packet.ReadByte());
                CardLocation loc  = (CardLocation)packet.ReadByte();
                int          seq  = packet.ReadByte();
                int          desc = packet.ReadInt32();

                ClientCard card = _duel.GetCard(con, loc, seq);
                if (card != null)
                {
                    card.ActionIndex[0] = i;
                    battle.ActivableCards.Add(card);
                    battle.ActivableDescs.Add(desc);
                }
            }

            count = packet.ReadByte();
            for (int i = 0; i < count; ++i)
            {
                packet.ReadInt32(); // card id
                int          con    = GetLocalPlayer(packet.ReadByte());
                CardLocation loc    = (CardLocation)packet.ReadByte();
                int          seq    = packet.ReadByte();
                int          diratt = packet.ReadByte();

                ClientCard card = _duel.GetCard(con, loc, seq);
                if (card != null)
                {
                    card.ActionIndex[1] = i;
                    if (diratt > 0)
                    {
                        card.CanDirectAttack = true;
                    }
                    else
                    {
                        card.CanDirectAttack = false;
                    }
                    battle.AttackableCards.Add(card);
                    card.Attacked = false;
                }
            }
            List <ClientCard> monsters = _duel.Fields[0].GetMonsters();

            foreach (ClientCard monster in monsters)
            {
                if (!battle.AttackableCards.Contains(monster))
                {
                    monster.Attacked = true;
                }
            }

            battle.CanMainPhaseTwo = packet.ReadByte() != 0;
            battle.CanEndPhase     = packet.ReadByte() != 0;

            Connection.Send(CtosMessage.Response, _ai.OnSelectBattleCmd(battle).ToValue());
        }