示例#1
0
 public override bool OnPreBattleBetween(ClientCard attacker, ClientCard defender)
 {
     if (attacker.Id == (int)CardId.Unifrog || attacker.Id == (int)CardId.DewdarkOfTheIceBarrier)
         return true;
     if (defender.IsMonsterInvincible() && !defender.IsMonsterDangerous() && attacker.Id == (int)CardId.SubmarineFrog)
         return true;
     return base.OnPreBattleBetween(attacker, defender);
 }
示例#2
0
 public ClientField()
 {
     Hand = new List<ClientCard>();
     MonsterZone = new ClientCard[5];
     SpellZone = new ClientCard[8];
     Graveyard = new List<ClientCard>();
     Banished = new List<ClientCard>();
     Deck = new List<ClientCard>();
     ExtraDeck = new List<ClientCard>();
 }
示例#3
0
文件: GameAI.cs 项目: Rayth/windbot
 public BattlePhaseAction Attack(ClientCard attacker, ClientCard defender)
 {
     if (defender != null)
     {
         string cardName = defender.Name ?? "monster";
         _dialogs.SendAttack(attacker.Name, cardName);
         SelectCard(defender);
     }
     else
         _dialogs.SendDirectAttack(attacker.Name);
     return new BattlePhaseAction(BattlePhaseAction.BattleAction.Attack, attacker.ActionIndex);
 }
示例#4
0
        public void AddCard(CardLocation loc, ClientCard card, int player, int seq, int pos, int id)
        {
            card.Location = loc;
            card.Sequence = seq;
            card.Position = pos;
            card.SetId(id);
            switch (loc)
            {
            case CardLocation.Hand:
                Fields[player].Hand.Add(card);
                break;

            case CardLocation.Grave:
                Fields[player].Graveyard.Add(card);
                break;

            case CardLocation.Removed:
                Fields[player].Banished.Add(card);
                break;

            case CardLocation.MonsterZone:
                Fields[player].MonsterZone[seq] = card;
                break;

            case CardLocation.SpellZone:
                Fields[player].SpellZone[seq] = card;
                break;

            case CardLocation.Deck:
                Fields[player].Deck.Add(card);
                break;

            case CardLocation.Extra:
                Fields[player].ExtraDeck.Add(card);
                break;
            }
        }
示例#5
0
        private void OnAttack(BinaryReader packet)
        {
            int ca = GetLocalPlayer(packet.ReadByte());
            int la = packet.ReadByte();
            int sa = packet.ReadByte();

            packet.ReadByte(); //
            int cd = GetLocalPlayer(packet.ReadByte());
            int ld = packet.ReadByte();
            int sd = packet.ReadByte();

            packet.ReadByte(); //

            ClientCard attackcard = _duel.GetCard(ca, (CardLocation)la, sa);
            ClientCard defendcard = _duel.GetCard(cd, (CardLocation)ld, sd);

            _duel.Fields[attackcard.Controller].BattlingMonster     = attackcard;
            _duel.Fields[1 - attackcard.Controller].BattlingMonster = defendcard;

            if (ld == 0 && (attackcard != null) && (ca != 0))
            {
                _ai.OnDirectAttack(attackcard);
            }
        }
示例#6
0
文件: Duel.cs 项目: Zayelion/windbot
 public void RemoveCard(CardLocation loc, ClientCard card, int player, int zone)
 {
     switch (loc)
     {
         case CardLocation.Hand:
             Fields[player].Hand.Remove(card);
             break;
         case CardLocation.Grave:
             Fields[player].Graveyard.Remove(card);
             break;
         case CardLocation.Removed:
             Fields[player].Banished.Remove(card);
             break;
         case CardLocation.MonsterZone:
             Fields[player].MonsterZone[zone] = null;
             break;
         case CardLocation.SpellZone:
             Fields[player].SpellZone[zone] = null;
             break;
         case CardLocation.Deck:
             Fields[player].Deck.Remove(card);
             break;
         case CardLocation.Extra:
             Fields[player].ExtraDeck.Remove(card);
             break;
     }
 }
示例#7
0
        /// <summary>
        /// Called when the AI has to do something during the main phase.
        /// </summary>
        /// <param name="main">A lot of informations about the available actions.</param>
        /// <returns>A new MainPhaseAction containing the action to do.</returns>
        public MainPhaseAction OnSelectIdleCmd(MainPhase main)
        {
            if (Executor is RandomExecutorBase RandomExecutor)
            {
                RandomExecutor.SetMain(main);
                ChoiceWeight choice = new ChoiceWeight(RandomExecutor, Duel.Phase, Duel.Turn, _dialogs);
                //loop through setable monsters
                foreach (ClientCard card in main.MonsterSetableCards)
                {
                    choice.SetBest(MainPhaseAction.MainAction.SetMonster, card, card.ActionIndex[(int)MainPhaseAction.MainAction.SetMonster]);
                }
                //loop through cards that can change position
                foreach (ClientCard card in main.ReposableCards)
                {
                    choice.SetBest(MainPhaseAction.MainAction.Repos, card, card.ActionIndex[(int)MainPhaseAction.MainAction.Repos]);
                }
                //Loop through normal summonable monsters
                foreach (ClientCard card in main.SummonableCards)
                {
                    choice.SetBest(MainPhaseAction.MainAction.Summon, card, card.ActionIndex[(int)MainPhaseAction.MainAction.Summon]);
                }
                //loop through special summonable monsters
                foreach (ClientCard card in main.SpecialSummonableCards)
                {
                    choice.SetBest(MainPhaseAction.MainAction.SpSummon, card, card.ActionIndex[(int)MainPhaseAction.MainAction.SpSummon]);
                }
                //loop through activatable cards
                for (int i = 0; i < main.ActivableCards.Count; ++i)
                {
                    ClientCard card = main.ActivableCards[i];
                    choice.SetBest(MainPhaseAction.MainAction.Activate, card, card.ActionActivateIndex[main.ActivableDescs[i]]);
                }

                switch (choice.BestAction)
                {
                case MainPhaseAction.MainAction.Activate:
                    // _dialogs.SendActivate(choice.BestCard.Name);
                    break;

                case MainPhaseAction.MainAction.Repos:
                    break;

                case MainPhaseAction.MainAction.SetMonster:
                    // _dialogs.SendSetMonster();
                    break;

                case MainPhaseAction.MainAction.SetSpell:
                    break;

                case MainPhaseAction.MainAction.SpSummon:
                    //_dialogs.SendSummon(choice.BestCard.Name);
                    break;

                case MainPhaseAction.MainAction.Summon:
                    //_dialogs.SendSummon(choice.BestCard.Name);
                    break;

                default:
                    if (main.CanBattlePhase && Duel.Fields[0].HasAttackingMonster())
                    {
                        choice.RecordAction(MainPhaseAction.MainAction.ToBattlePhase, null, -1, 1);
                        return(new MainPhaseAction(MainPhaseAction.MainAction.ToBattlePhase));
                    }

                    _dialogs.SendEndTurn();
                    _dialogs.SendMessage("Card Advantage:" + RandomExecutor.GetCardAdvantageField().ToString());
                    choice.RecordAction(MainPhaseAction.MainAction.ToEndPhase, null, -1, 1);
                    return(new MainPhaseAction(MainPhaseAction.MainAction.ToEndPhase));
                }
                return(choice.ReturnBestAction());
            }
            else//the regular one
            {
                Executor.SetMain(main);
                foreach (CardExecutor exec in Executor.Executors)
                {
                    if (exec.Type == ExecutorType.GoToEndPhase && main.CanEndPhase && exec.Func()) // check if should enter end phase directly
                    {
                        _dialogs.SendEndTurn();
                        return(new MainPhaseAction(MainPhaseAction.MainAction.ToEndPhase));
                    }
                    if (exec.Type == ExecutorType.GoToBattlePhase && main.CanBattlePhase && exec.Func()) // check if should enter battle phase directly
                    {
                        return(new MainPhaseAction(MainPhaseAction.MainAction.ToBattlePhase));
                    }
                    // NOTICE: GoToBattlePhase and GoToEndPhase has no "card" can be accessed to ShouldExecute(), so instead use exec.Func() to check ...
                    // enter end phase and enter battle pahse is in higher priority.

                    for (int i = 0; i < main.ActivableCards.Count; ++i)
                    {
                        ClientCard card = main.ActivableCards[i];
                        if (ShouldExecute(exec, card, ExecutorType.Activate, main.ActivableDescs[i]))
                        {
                            _dialogs.SendActivate(card.Name);
                            Logger.WriteToFile($"{card.Name}]{card.Id}");
                            return(new MainPhaseAction(MainPhaseAction.MainAction.Activate, card.ActionActivateIndex[main.ActivableDescs[i]]));
                        }
                    }
                    foreach (ClientCard card in main.MonsterSetableCards)
                    {
                        if (ShouldExecute(exec, card, ExecutorType.MonsterSet))
                        {
                            _dialogs.SendSetMonster();
                            Logger.WriteToFile($"{card.Name}]{card.Id}");
                            return(new MainPhaseAction(MainPhaseAction.MainAction.SetMonster, card.ActionIndex));
                        }
                    }
                    foreach (ClientCard card in main.ReposableCards)
                    {
                        if (ShouldExecute(exec, card, ExecutorType.Repos))
                        {
                            return(new MainPhaseAction(MainPhaseAction.MainAction.Repos, card.ActionIndex));
                        }
                    }
                    foreach (ClientCard card in main.SpecialSummonableCards)
                    {
                        if (ShouldExecute(exec, card, ExecutorType.SpSummon))
                        {
                            _dialogs.SendSummon(card.Name);
                            Logger.WriteToFile($"{card.Name}]{card.Id}");
                            return(new MainPhaseAction(MainPhaseAction.MainAction.SpSummon, card.ActionIndex));
                        }
                    }
                    foreach (ClientCard card in main.SummonableCards)
                    {
                        if (ShouldExecute(exec, card, ExecutorType.Summon))
                        {
                            _dialogs.SendSummon(card.Name);
                            Logger.WriteToFile($"{card.Name}]{card.Id}");
                            return(new MainPhaseAction(MainPhaseAction.MainAction.Summon, card.ActionIndex));
                        }
                        if (ShouldExecute(exec, card, ExecutorType.SummonOrSet))
                        {
                            if (Executor.Util.IsAllEnemyBetter(true) && Executor.Util.IsAllEnemyBetterThanValue(card.Attack + 300, false) &&
                                main.MonsterSetableCards.Contains(card))
                            {
                                _dialogs.SendSetMonster();
                                Logger.WriteToFile($"{card.Name}]{card.Id}");
                                return(new MainPhaseAction(MainPhaseAction.MainAction.SetMonster, card.ActionIndex));
                            }
                            _dialogs.SendSummon(card.Name);
                            Logger.WriteToFile($"{card.Name}]{card.Id}");
                            return(new MainPhaseAction(MainPhaseAction.MainAction.Summon, card.ActionIndex));
                        }
                    }
                    foreach (ClientCard card in main.SpellSetableCards)
                    {
                        if (ShouldExecute(exec, card, ExecutorType.SpellSet))
                        {
                            return(new MainPhaseAction(MainPhaseAction.MainAction.SetSpell, card.ActionIndex));
                        }
                    }
                }

                if (main.CanBattlePhase && Duel.Fields[0].HasAttackingMonster())
                {
                    return(new MainPhaseAction(MainPhaseAction.MainAction.ToBattlePhase));
                }

                _dialogs.SendEndTurn();
                return(new MainPhaseAction(MainPhaseAction.MainAction.ToEndPhase));
            }
        }
示例#8
0
 private bool ShouldExecute(CardExecutor exec, ClientCard card, ExecutorType type, int desc = -1)
 {
     Executor.SetCard(type, card, desc);
     if (card != null &&
         exec.Type == type &&
         (exec.CardId == -1 || exec.CardId == card.Id) &&
         (exec.Func == null || exec.Func()))
         return true;
     return false;
 }
示例#9
0
 public void SelectCard(ClientCard card)
 {
     m_selector = new CardSelector(card);
 }
示例#10
0
        public ClientCard GetCard(int player, int loc, int index, int subindex)
        {
            if (player < 0 || player > 1)
            {
                return(null);
            }

            bool         isXyz    = (loc & 0x80) != 0;
            CardLocation location = (CardLocation)(loc & 0x7f);

            IList <ClientCard> cards = null;

            switch (location)
            {
            case CardLocation.Deck:
                cards = Fields[player].Deck;
                break;

            case CardLocation.Hand:
                cards = Fields[player].Hand;
                break;

            case CardLocation.MonsterZone:
                cards = Fields[player].MonsterZone;
                break;

            case CardLocation.SpellZone:
                cards = Fields[player].SpellZone;
                break;

            case CardLocation.Grave:
                cards = Fields[player].Graveyard;
                break;

            case CardLocation.Removed:
                cards = Fields[player].Banished;
                break;

            case CardLocation.Extra:
                cards = Fields[player].ExtraDeck;
                break;
            }
            if (cards == null)
            {
                return(null);
            }

            if (index >= cards.Count)
            {
                return(null);
            }

            if (isXyz)
            {
                ClientCard card = cards[index];
                if (card == null || subindex >= card.Overlays.Count)
                {
                    return(null);
                }
                return(null); // TODO card.Overlays[subindex]
            }

            return(cards[index]);
        }
示例#11
0
        private void InternalOnSelectCard(GamePacketReader packet, Func<IList<ClientCard>, int, int, bool, IList<ClientCard>> func)
        {
            packet.ReadByte(); // player
            bool cancelable = packet.ReadByte() != 0;
            int min = packet.ReadByte();
            int max = packet.ReadByte();

            IList<ClientCard> cards = new List<ClientCard>();
            int count = packet.ReadByte();
            for (int i = 0; i < count; ++i)
            {
                int id = packet.ReadInt32();
                int player = GetLocalPlayer(packet.ReadByte());
                CardLocation loc = (CardLocation)packet.ReadByte();
                int seq = packet.ReadByte();
                packet.ReadByte(); // pos
                ClientCard card;
                if (((int)loc & (int)CardLocation.Overlay) != 0)
                    card = new ClientCard(id, CardLocation.Overlay);
                else
                    card = _duel.GetCard(player, loc, seq);
                if (card == null) continue;
                if (card.Id == 0)
                    card.SetId(id);
                cards.Add(card);
            }

            IList<ClientCard> selected = func(cards, min, max, cancelable);

            if (selected.Count == 0 && cancelable)
            {
                Connection.Send(CtosMessage.Response, -1);
                return;
            }

            byte[] result = new byte[selected.Count + 1];
            result[0] = (byte)selected.Count;
            for (int i = 0; i < selected.Count; ++i)
            {
                int id = 0;
                for (int j = 0; j < count; ++j)
                {
                    if (cards[j] == null) continue;
                    if (cards[j].Equals(selected[i]))
                    {
                        id = j;
                        break;
                    }
                }
                result[i + 1] = (byte)id;
            }

            GamePacketWriter reply = new GamePacketWriter(CtosMessage.Response);
            reply.Write(result);
            Connection.Send(reply);
        }
示例#12
0
 public void SelectCard(ClientCard card)
 {
     m_selector_pointer = m_selector.Count();
     m_selector.Add(new CardSelector(card));
 }
示例#13
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());
        }
示例#14
0
 /// <summary>
 /// Called when the AI got attack directly.
 /// </summary>
 public void OnDirectAttack(ClientCard card)
 {
     _dialogs.SendOnDirectAttack(card.Name);
 }
示例#15
0
 public bool Equals(ClientCard card)
 {
     return(ReferenceEquals(this, card));
 }
示例#16
0
 /// <summary>
 /// Called when a chain is executed.
 /// </summary>
 /// <param name="card">Card who is chained.</param>
 /// <param name="player">Player who is currently chaining.</param>
 public void OnChaining(ClientCard card, int player)
 {
     Duel.LastSummonPlayer = -1;
     Executor.OnChaining(player, card);
 }
示例#17
0
 public void SelectCard(ClientCard card)
 {
     this.selector_pointer = this.selector.Count();
     this.selector.Add(new CardSelector(card));
 }
示例#18
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());
        }
示例#19
0
        private void OnSelectSum(BinaryReader packet)
        {
            bool mode = packet.ReadByte() == 0;

            packet.ReadByte(); // player
            int sumval = packet.ReadInt32();
            int min    = packet.ReadByte();
            int max    = packet.ReadByte();

            IList <ClientCard> mandatoryCards = new List <ClientCard>();
            IList <ClientCard> cards          = new List <ClientCard>();

            for (int j = 0; j < 2; ++j)
            {
                int count = packet.ReadByte();
                for (int i = 0; i < count; ++i)
                {
                    int          cardId = packet.ReadInt32();
                    int          player = GetLocalPlayer(packet.ReadByte());
                    CardLocation loc    = (CardLocation)packet.ReadByte();
                    int          seq    = packet.ReadByte();
                    ClientCard   card   = _duel.GetCard(player, loc, seq);
                    if (cardId != 0 && card.Id != cardId)
                    {
                        card.SetId(cardId);
                    }
                    card.SelectSeq = i;
                    int OpParam  = packet.ReadInt32();
                    int OpParam1 = OpParam & 0xffff;
                    int OpParam2 = OpParam >> 16;
                    if (OpParam2 > 0 && OpParam1 > OpParam2)
                    {
                        card.OpParam1 = OpParam2;
                        card.OpParam2 = OpParam1;
                    }
                    else
                    {
                        card.OpParam1 = OpParam1;
                        card.OpParam2 = OpParam2;
                    }
                    if (j == 0)
                    {
                        mandatoryCards.Add(card);
                    }
                    else
                    {
                        cards.Add(card);
                    }
                }
            }

            for (int k = 0; k < mandatoryCards.Count; ++k)
            {
                sumval -= mandatoryCards[k].OpParam1;
            }

            IList <ClientCard> selected = _ai.OnSelectSum(cards, sumval, min, max, mode);

            byte[] result = new byte[mandatoryCards.Count + selected.Count + 1];
            int    index  = 0;

            result[index++] = (byte)(mandatoryCards.Count + selected.Count);
            while (index <= mandatoryCards.Count)
            {
                result[index++] = 0;
            }
            for (int i = 0; i < selected.Count; ++i)
            {
                result[index++] = (byte)selected[i].SelectSeq;
            }

            BinaryWriter reply = GamePacketFactory.Create(CtosMessage.Response);

            reply.Write(result);
            Connection.Send(reply);
        }
示例#20
0
 public void SelectMaterials(ClientCard card)
 {
     m_materialSelector = new CardSelector(card);
 }
        private void InternalOnSelectCard(GameServerPacket packet, Func <IList <ClientCard>, int, int, bool, IList <ClientCard> > func)
        {
            packet.ReadByte();             // player
            bool cancelable = packet.ReadByte() != 0;
            int  min        = packet.ReadByte();
            int  max        = packet.ReadByte();

            IList <ClientCard> cards = new List <ClientCard>();
            int count = packet.ReadByte();

            for (int i = 0; i < count; ++i)
            {
                int          id     = packet.ReadInt32();
                int          player = GetLocalPlayer(packet.ReadByte());
                CardLocation loc    = (CardLocation)packet.ReadByte();
                int          seq    = packet.ReadByte();
                packet.ReadByte();                 // pos
                ClientCard card;
                if (((int)loc & (int)CardLocation.Overlay) != 0)
                {
                    card = new ClientCard(id, CardLocation.Overlay);
                }
                else
                {
                    card = _duel.GetCard(player, loc, seq);
                }
                if (card == null)
                {
                    continue;
                }
                if (card.Id == 0)
                {
                    card.SetId(id);
                }
                cards.Add(card);
            }

            IList <ClientCard> selected = func(cards, min, max, cancelable);

            if (selected.Count == 0 && cancelable)
            {
                Connection.Send(CtosMessage.Response, -1);
                return;
            }

            byte[] result = new byte[selected.Count + 1];
            result[0] = (byte)selected.Count;
            for (int i = 0; i < selected.Count; ++i)
            {
                int id = 0;
                for (int j = 0; j < count; ++j)
                {
                    if (cards[j] == null)
                    {
                        continue;
                    }
                    if (cards[j].Equals(selected[i]))
                    {
                        id = j;
                        break;
                    }
                }
                result[i + 1] = (byte)id;
            }

            GameClientPacket reply = new GameClientPacket(CtosMessage.Response);

            reply.Write(result);
            Connection.Send(reply);
        }
示例#22
0
 /// <summary>
 /// Called when a chain is executed.
 /// </summary>
 /// <param name="card">Card who is chained.</param>
 /// <param name="player">Player who is currently chaining.</param>
 public void OnChaining(ClientCard card, int player)
 {
     Executor.OnChaining(player, card);
 }
        private void OnSelectIdleCmd(GameServerPacket packet)
        {
            packet.ReadByte();             // player

            _duel.MainPhase = new MainPhase();
            MainPhase main = _duel.MainPhase;
            int       count;

            for (int k = 0; k < 5; k++)
            {
                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();
                    ClientCard   card = _duel.GetCard(con, loc, seq);
                    if (card == null)
                    {
                        continue;
                    }
                    card.ActionIndex[k] = i;
                    switch (k)
                    {
                    case 0:
                        main.SummonableCards.Add(card);
                        break;

                    case 1:
                        main.SpecialSummonableCards.Add(card);
                        break;

                    case 2:
                        main.ReposableCards.Add(card);
                        break;

                    case 3:
                        main.MonsterSetableCards.Add(card);
                        break;

                    case 4:
                        main.SpellSetableCards.Add(card);
                        break;
                    }
                }
            }
            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)
                {
                    continue;
                }
                card.ActionIndex[5] = i;
                if (card.ActionActivateIndex.ContainsKey(desc))
                {
                    card.ActionActivateIndex.Remove(desc);
                }
                card.ActionActivateIndex.Add(desc, i);
                main.ActivableCards.Add(card);
                main.ActivableDescs.Add(desc);
            }

            main.CanBattlePhase = packet.ReadByte() != 0;
            main.CanEndPhase    = packet.ReadByte() != 0;
            packet.ReadByte();             // CanShuffle

            Connection.Send(CtosMessage.Response, _ai.OnSelectIdleCmd(main).ToValue());
        }
示例#24
0
        /// <summary>
        /// Called when the AI has to do something during the main phase.
        /// </summary>
        /// <param name="main">A lot of informations about the available actions.</param>
        /// <returns>A new MainPhaseAction containing the action to do.</returns>
        public MainPhaseAction OnSelectIdleCmd(MainPhase main)
        {
            Executor.SetMain(main);
            foreach (CardExecutor exec in Executor.Executors)
            {
                if (exec.Type == ExecutorType.GoToEndPhase && main.CanEndPhase && exec.Func()) // check if should enter end phase directly
                {
                    _dialogs.SendEndTurn();
                    return(new MainPhaseAction(MainPhaseAction.MainAction.ToEndPhase));
                }
                if (exec.Type == ExecutorType.GoToBattlePhase && main.CanBattlePhase && exec.Func()) // check if should enter battle phase directly
                {
                    return(new MainPhaseAction(MainPhaseAction.MainAction.ToBattlePhase));
                }
                // NOTICE: GoToBattlePhase and GoToEndPhase has no "card" can be accessed to ShouldExecute(), so instead use exec.Func() to check ...
                // enter end phase and enter battle pahse is in higher priority.

                for (int i = 0; i < main.ActivableCards.Count; ++i)
                {
                    ClientCard card = main.ActivableCards[i];
                    if (ShouldExecute(exec, card, ExecutorType.Activate, main.ActivableDescs[i]))
                    {
                        _dialogs.SendActivate(card.Name);
                        return(new MainPhaseAction(MainPhaseAction.MainAction.Activate, card.ActionActivateIndex[main.ActivableDescs[i]]));
                    }
                }
                foreach (ClientCard card in main.MonsterSetableCards)
                {
                    if (ShouldExecute(exec, card, ExecutorType.MonsterSet))
                    {
                        _dialogs.SendSetMonster();
                        return(new MainPhaseAction(MainPhaseAction.MainAction.SetMonster, card.ActionIndex));
                    }
                }
                foreach (ClientCard card in main.ReposableCards)
                {
                    if (ShouldExecute(exec, card, ExecutorType.Repos))
                    {
                        return(new MainPhaseAction(MainPhaseAction.MainAction.Repos, card.ActionIndex));
                    }
                }
                foreach (ClientCard card in main.SpecialSummonableCards)
                {
                    if (ShouldExecute(exec, card, ExecutorType.SpSummon))
                    {
                        _dialogs.SendSummon(card.Name);
                        return(new MainPhaseAction(MainPhaseAction.MainAction.SpSummon, card.ActionIndex));
                    }
                }
                foreach (ClientCard card in main.SummonableCards)
                {
                    if (ShouldExecute(exec, card, ExecutorType.Summon))
                    {
                        _dialogs.SendSummon(card.Name);
                        return(new MainPhaseAction(MainPhaseAction.MainAction.Summon, card.ActionIndex));
                    }
                    if (ShouldExecute(exec, card, ExecutorType.SummonOrSet))
                    {
                        if (Executor.Util.IsAllEnemyBetter(true) && Executor.Util.IsAllEnemyBetterThanValue(card.Attack + 300, false) &&
                            main.MonsterSetableCards.Contains(card))
                        {
                            _dialogs.SendSetMonster();
                            return(new MainPhaseAction(MainPhaseAction.MainAction.SetMonster, card.ActionIndex));
                        }
                        _dialogs.SendSummon(card.Name);
                        return(new MainPhaseAction(MainPhaseAction.MainAction.Summon, card.ActionIndex));
                    }
                }
                foreach (ClientCard card in main.SpellSetableCards)
                {
                    if (ShouldExecute(exec, card, ExecutorType.SpellSet))
                    {
                        return(new MainPhaseAction(MainPhaseAction.MainAction.SetSpell, card.ActionIndex));
                    }
                }
            }

            if (main.CanBattlePhase && Duel.Fields[0].HasAttackingMonster())
            {
                return(new MainPhaseAction(MainPhaseAction.MainAction.ToBattlePhase));
            }

            _dialogs.SendEndTurn();
            return(new MainPhaseAction(MainPhaseAction.MainAction.ToEndPhase));
        }
示例#25
0
 public void SelectMaterials(ClientCard card, int hint = 0)
 {
     m_materialSelector     = new CardSelector(card);
     m_materialSelectorHint = hint;
 }
示例#26
0
        public void AddCard(CardLocation loc, int cardId, int player, int seq, int pos)
        {
            ClientCard card = new ClientCard(cardId, loc, seq, pos);

            AddCard(loc, card, player, seq, pos, cardId);
        }
示例#27
0
        /// <summary>
        /// Called when the AI has to do something during the main phase.
        /// </summary>
        /// <param name="main">A lot of informations about the available actions.</param>
        /// <returns>A new MainPhaseAction containing the action to do.</returns>
        public MainPhaseAction OnSelectIdleCmd(MainPhase main)
        {
            Executor.SetMain(main);
            foreach (CardExecutor exec in Executor.Executors)
            {
                for (int i = 0; i < main.ActivableCards.Count; ++i)
                {
                    ClientCard card = main.ActivableCards[i];
                    if (ShouldExecute(exec, card, ExecutorType.Activate, main.ActivableDescs[i]))
                    {
                        _dialogs.SendActivate(card.Name);
                        return(new MainPhaseAction(MainPhaseAction.MainAction.Activate, card.ActionActivateIndex[main.ActivableDescs[i]]));
                    }
                }
                foreach (ClientCard card in main.MonsterSetableCards)
                {
                    if (ShouldExecute(exec, card, ExecutorType.MonsterSet))
                    {
                        _dialogs.SendSetMonster();
                        return(new MainPhaseAction(MainPhaseAction.MainAction.SetMonster, card.ActionIndex));
                    }
                }
                foreach (ClientCard card in main.ReposableCards)
                {
                    if (ShouldExecute(exec, card, ExecutorType.Repos))
                    {
                        return(new MainPhaseAction(MainPhaseAction.MainAction.Repos, card.ActionIndex));
                    }
                }
                foreach (ClientCard card in main.SpecialSummonableCards)
                {
                    if (ShouldExecute(exec, card, ExecutorType.SpSummon))
                    {
                        _dialogs.SendSummon(card.Name);
                        Duel.LastSummonPlayer = 0;
                        return(new MainPhaseAction(MainPhaseAction.MainAction.SpSummon, card.ActionIndex));
                    }
                }
                foreach (ClientCard card in main.SummonableCards)
                {
                    if (ShouldExecute(exec, card, ExecutorType.Summon))
                    {
                        _dialogs.SendSummon(card.Name);
                        Duel.LastSummonPlayer = 0;
                        return(new MainPhaseAction(MainPhaseAction.MainAction.Summon, card.ActionIndex));
                    }
                    if (ShouldExecute(exec, card, ExecutorType.SummonOrSet))
                    {
                        if (Utils.IsAllEnemyBetter(true) && Utils.IsAllEnemyBetterThanValue(card.Attack + 300, false) &&
                            main.MonsterSetableCards.Contains(card))
                        {
                            _dialogs.SendSetMonster();
                            return(new MainPhaseAction(MainPhaseAction.MainAction.SetMonster, card.ActionIndex));
                        }
                        _dialogs.SendSummon(card.Name);
                        Duel.LastSummonPlayer = 0;
                        return(new MainPhaseAction(MainPhaseAction.MainAction.Summon, card.ActionIndex));
                    }
                }
                foreach (ClientCard card in main.SpellSetableCards)
                {
                    if (ShouldExecute(exec, card, ExecutorType.SpellSet))
                    {
                        return(new MainPhaseAction(MainPhaseAction.MainAction.SetSpell, card.ActionIndex));
                    }
                }
            }

            if (main.CanBattlePhase && Duel.Fields[0].HasAttackingMonster())
            {
                return(new MainPhaseAction(MainPhaseAction.MainAction.ToBattlePhase));
            }

            _dialogs.SendEndTurn();
            return(new MainPhaseAction(MainPhaseAction.MainAction.ToEndPhase));
        }
示例#28
0
 public bool Equals(ClientCard card)
 {
     return ReferenceEquals(this, card);
 }
示例#29
0
 public void SelectCard(ClientCard card)
 {
     m_selector = new CardSelector(card);
 }
示例#30
0
 /// <summary>
 /// Called when the AI has to choose to activate or not an effect.
 /// </summary>
 /// <param name="card">Card to activate.</param>
 /// <returns>True for yes, false for no.</returns>
 public bool OnSelectEffectYn(ClientCard card)
 {
     foreach (CardExecutor exec in Executor.Executors)
     {
         if (ShouldExecute(exec, card, ExecutorType.Activate))
             return true;
     }
     return false;
 }
示例#31
0
 public void SelectNextCard(ClientCard card)
 {
     m_nextSelector = new CardSelector(card);
 }
示例#32
0
 public void SelectNextCard(ClientCard card)
 {
     m_nextSelector = new CardSelector(card);
 }
示例#33
0
 public void SelectThirdCard(ClientCard card)
 {
     m_thirdSelector = new CardSelector(card);
 }
示例#34
0
 /// <summary>
 /// Called when a chain is executed.
 /// </summary>
 /// <param name="card">Card who is chained.</param>
 /// <param name="player">Player who is currently chaining.</param>
 public void OnChaining(ClientCard card, int player)
 {
     Executor.OnChaining(player,card);
 }
示例#35
0
        private void OnSelectSum(GameServerPacket packet)
        {
            packet.ReadByte(); // mode
            packet.ReadByte(); // player
            int sumval = packet.ReadInt32();
            int min    = packet.ReadByte();
            int max    = packet.ReadByte();

            IList <ClientCard> mandatoryCards = new List <ClientCard>();
            IList <ClientCard> cards          = new List <ClientCard>();

            for (int j = 0; j < 2; ++j)
            {
                int count = packet.ReadByte();
                for (int i = 0; i < count; ++i)
                {
                    int          cardId = packet.ReadInt32();
                    int          player = GetLocalPlayer(packet.ReadByte());
                    CardLocation loc    = (CardLocation)packet.ReadByte();
                    int          seq    = packet.ReadByte();
                    ClientCard   card   = _duel.GetCard(player, loc, seq);
                    if (card != null)
                    {
                        if (cardId != 0 && card.Id != cardId)
                        {
                            card.SetId(cardId);
                        }
                    }
                    if (j == 0)
                    {
                        mandatoryCards.Add(card);
                    }
                    else
                    {
                        cards.Add(card);
                    }
                    packet.ReadInt32();
                }
            }

            IList <ClientCard> selected = _ai.OnSelectSum(cards, sumval, min, max);

            byte[] result = new byte[mandatoryCards.Count + selected.Count + 1];
            int    index  = 0;

            result[index++] = (byte)(mandatoryCards.Count + selected.Count);
            while (index < mandatoryCards.Count)
            {
                result[index++] = 0;
            }
            for (int i = 0; i < selected.Count; ++i)
            {
                int id = 0;
                for (int j = 0; j < cards.Count; ++j)
                {
                    if (cards[j] == null)
                    {
                        continue;
                    }
                    if (cards[j].Equals(selected[i]))
                    {
                        id = j;
                        break;
                    }
                }
                result[index++] = (byte)id;
            }

            GameClientPacket reply = new GameClientPacket(CtosMessage.Response);

            reply.Write(result);
            Connection.Send(reply);
        }