-
Notifications
You must be signed in to change notification settings - Fork 115
/
Copy pathGameAI.cs
1204 lines (1074 loc) · 43.8 KB
/
GameAI.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System.Linq;
using System.Collections.Generic;
using System.Threading;
using WindBot.Game.AI;
using YGOSharp.OCGWrapper.Enums;
namespace WindBot.Game
{
public class GameAI
{
public GameClient Game { get; private set; }
public Duel Duel { get; private set; }
public Executor Executor { get; set; }
private Dialogs _dialogs;
// record activated count to prevent infinite actions
private Dictionary<int, int> _activatedCards;
public GameAI(GameClient game, Duel duel)
{
Game = game;
Duel = duel;
_dialogs = new Dialogs(game);
_activatedCards = new Dictionary<int, int>();
}
private void CheckSurrender()
{
foreach (CardExecutor exec in Executor.Executors)
{
if (exec.Type == ExecutorType.Surrender && exec.Func())
{
_dialogs.SendSurrender();
Game.Surrender();
}
}
}
/// <summary>
/// Called when the AI got the error message.
/// </summary>
public void OnRetry()
{
_dialogs.SendSorry();
}
public void OnDeckError(string card)
{
_dialogs.SendDeckSorry(card);
Thread.Sleep(1000);
_dialogs.SendSurrender();
Game.Connection.Close();
}
/// <summary>
/// Called when the AI join the game.
/// </summary>
public void OnJoinGame()
{
_dialogs.SendWelcome();
}
/// <summary>
/// Called when the duel starts.
/// </summary>
public void OnStart()
{
_dialogs.SendDuelStart();
}
/// <summary>
/// Customized called when the AI do something in a duel.
/// </summary>
public void SendCustomChat(int index, params object[] opts)
{
_dialogs.SendCustomChat(index, opts);
}
/// <summary>
/// Called when the AI do the rock-paper-scissors.
/// </summary>
/// <returns>1 for Scissors, 2 for Rock, 3 for Paper.</returns>
public int OnRockPaperScissors()
{
return Executor.OnRockPaperScissors();
}
/// <summary>
/// Called when the AI won the rock-paper-scissors.
/// </summary>
/// <returns>True if the AI should begin first, false otherwise.</returns>
public bool OnSelectHand()
{
return Executor.OnSelectHand();
}
/// <summary>
/// Called when any player draw card.
/// </summary>
public void OnDraw(int player)
{
Executor.OnDraw(player);
}
/// <summary>
/// Called when it's a new turn.
/// </summary>
public void OnNewTurn()
{
_activatedCards.Clear();
Executor.OnNewTurn();
}
/// <summary>
/// Called when it's a new phase.
/// </summary>
public void OnNewPhase()
{
m_selector.Clear();
m_position.Clear();
m_selector_pointer = -1;
m_materialSelector = null;
m_materialSelectorHint = 0;
m_option = -1;
m_yesno = -1;
m_announce = 0;
m_place = 0;
if (Duel.Player == 0 && Duel.Phase == DuelPhase.Draw)
{
_dialogs.SendNewTurn();
}
Executor.OnNewPhase();
CheckSurrender();
}
public void OnMove(ClientCard card, int previousControler, int previousLocation, int currentControler, int currentLocation)
{
Executor.OnMove(card, previousControler, previousLocation, currentControler, currentLocation);
}
/// <summary>
/// Called when the AI got attack directly.
/// </summary>
public void OnDirectAttack(ClientCard card)
{
_dialogs.SendOnDirectAttack(card.Name);
CheckSurrender();
}
/// <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);
}
public void OnChainSolved(int chainIndex)
{
Executor.OnChainSolved(chainIndex);
}
/// <summary>
/// Called when card is successfully special summoned.
/// Used on monsters that can only special summoned once per turn.
/// </summary>
public void OnSpSummoned()
{
Executor.OnSpSummoned();
}
/// <summary>
/// Called when a chain has been solved.
/// </summary>
public void OnChainEnd()
{
m_selector.Clear();
m_selector_pointer = -1;
Executor.OnChainEnd();
CheckSurrender();
}
/// <summary>
/// Called when receiving annouce
/// </summary>
/// <param name="player">Player who announce.</param>
/// <param name="data">Annouced info.</param>
public void OnReceivingAnnouce(int player, int data)
{
Executor.OnReceivingAnnouce(player, data);
}
/// <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)
return ToMainPhase2();
if (defenders.Count == 0)
{
// Attack with the monster with the lowest attack first
ClientCard attacker = attackers[attackers.Count - 1];
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)
return Attack(attackers[0], (defenders.Count == 0) ? null : defenders[0]);
return ToMainPhase2();
}
/// <summary>
/// Called when the AI has to select one or more cards.
/// </summary>
/// <param name="cards">List of available cards.</param>
/// <param name="min">Minimal quantity.</param>
/// <param name="max">Maximal quantity.</param>
/// <param name="hint">The hint message of the select.</param>
/// <param name="cancelable">True if you can return an empty list.</param>
/// <returns>A new list containing the selected cards.</returns>
public IList<ClientCard> OnSelectCard(IList<ClientCard> cards, int min, int max, int hint, bool cancelable)
{
// Check for the executor.
IList<ClientCard> result = Executor.OnSelectCard(cards, min, max, hint, cancelable);
if (result != null)
return result;
if (hint == HintMsg.SpSummon && min == 1 && max > min) // pendulum summon
{
result = Executor.OnSelectPendulumSummon(cards, max);
if (result != null)
return result;
}
CardSelector selector = null;
if (hint == HintMsg.FusionMaterial || hint == HintMsg.SynchroMaterial || hint == HintMsg.XyzMaterial || hint == HintMsg.LinkMaterial)
{
if (m_materialSelector != null)
{
//Logger.DebugWriteLine("m_materialSelector");
selector = m_materialSelector;
}
else
{
if (hint == HintMsg.FusionMaterial)
result = Executor.OnSelectFusionMaterial(cards, min, max);
if (hint == HintMsg.SynchroMaterial)
result = Executor.OnSelectSynchroMaterial(cards, 0, min, max);
if (hint == HintMsg.XyzMaterial)
result = Executor.OnSelectXyzMaterial(cards, min, max);
if (hint == HintMsg.LinkMaterial)
result = Executor.OnSelectLinkMaterial(cards, min, max);
if (result != null)
return result;
// Update the next selector.
selector = GetSelectedCards();
}
}
else
{
if (m_materialSelector != null && hint == m_materialSelectorHint)
{
//Logger.DebugWriteLine("m_materialSelector hint match");
selector = m_materialSelector;
}
else
{
// Update the next selector.
selector = GetSelectedCards();
}
}
// If we selected a card, use this card.
if (selector != null)
return selector.Select(cards, min, max);
// Always select the first available cards and choose the minimum.
IList<ClientCard> selected = new List<ClientCard>();
if (hint == HintMsg.AttackTarget && cancelable) return selected;
if (cards.Count >= min)
{
for (int i = 0; i < min; ++i)
selected.Add(cards[i]);
}
return selected;
}
/// <summary>
/// Called when the AI can chain (activate) a card.
/// </summary>
/// <param name="cards">List of activable cards.</param>
/// <param name="descs">List of effect descriptions.</param>
/// <param name="forced">You can't return -1 if this param is true.</param>
/// <param name="timing">Current hint timing</param>
/// <returns>Index of the activated card or -1.</returns>
public int OnSelectChain(IList<ClientCard> cards, IList<int> descs, bool forced, int timing = -1)
{
Executor.OnSelectChain(cards);
foreach (CardExecutor exec in Executor.Executors)
{
for (int i = 0; i < cards.Count; ++i)
{
ClientCard card = cards[i];
if (ShouldExecute(exec, card, ExecutorType.Activate, descs[i], timing))
{
_dialogs.SendChaining(card.Name);
return i;
}
}
}
// If we're forced to chain, we chain the first card. However don't do anything.
return forced ? 0 : -1;
}
/// <summary>
/// Called when the AI has to use one or more counters.
/// </summary>
/// <param name="type">Type of counter to use.</param>
/// <param name="quantity">Quantity of counter to select.</param>
/// <param name="cards">List of available cards.</param>
/// <param name="counters">List of available counters.</param>
/// <returns>List of used counters.</returns>
public IList<int> OnSelectCounter(int type, int quantity, IList<ClientCard> cards, IList<int> counters)
{
// Always select the first available counters.
int[] used = new int[counters.Count];
int i = 0;
while (quantity > 0)
{
if (counters[i] >= quantity)
{
used[i] = quantity;
quantity = 0;
}
else
{
used[i] = counters[i];
quantity -= counters[i];
}
i++;
}
return used;
}
/// <summary>
/// Called when the AI has to sort cards.
/// </summary>
/// <param name="cards">Cards to sort.</param>
/// <returns>List of sorted cards.</returns>
public IList<ClientCard> OnCardSorting(IList<ClientCard> cards)
{
IList<ClientCard> result = Executor.OnCardSorting(cards);
if (result != null)
return result;
result = new List<ClientCard>();
// TODO: use selector
result = cards.ToList();
return result;
}
/// <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, int desc)
{
foreach (CardExecutor exec in Executor.Executors)
{
if (ShouldExecute(exec, card, ExecutorType.Activate, desc))
return true;
}
return false;
}
/// <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);
CheckSurrender();
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 (main.MonsterSetableCards.Contains(card) && Executor.OnSelectMonsterSummonOrSet(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);
}
/// <summary>
/// Called when the AI has to select an option.
/// </summary>
/// <param name="options">List of available options.</param>
/// <returns>Index of the selected option.</returns>
public int OnSelectOption(IList<int> options)
{
int result = Executor.OnSelectOption(options);
if (result != -1)
return result;
if (m_option != -1 && m_option < options.Count)
return m_option;
return 0; // Always select the first option.
}
public int OnSelectPlace(int cardId, int player, CardLocation location, int available)
{
int selector_selected = m_place;
m_place = 0;
int executor_selected = Executor.OnSelectPlace(cardId, player, location, available);
if ((executor_selected & available) > 0)
return executor_selected & available;
if ((selector_selected & available) > 0)
return selector_selected & available;
// TODO: LinkedZones
return 0;
}
/// <summary>
/// Called when the AI has to select a card position.
/// </summary>
/// <param name="cardId">Id of the card to position on the field.</param>
/// <param name="positions">List of available positions.</param>
/// <returns>Selected position.</returns>
public CardPosition OnSelectPosition(int cardId, IList<CardPosition> positions)
{
CardPosition selector_selected = GetSelectedPosition();
CardPosition executor_selected = Executor.OnSelectPosition(cardId, positions);
// Selects the selected position if available, the first available otherwise.
if (positions.Contains(executor_selected))
return executor_selected;
if (positions.Contains(selector_selected))
return selector_selected;
return positions[0];
}
/// <summary>
/// Called when the AI has to tribute for a synchro monster or ritual monster.
/// </summary>
/// <param name="cards">Available cards.</param>
/// <param name="sum">Result of the operation.</param>
/// <param name="min">Minimum cards.</param>
/// <param name="max">Maximum cards.</param>
/// <param name="mode">True for exact equal.</param>
/// <returns></returns>
public IList<ClientCard> OnSelectSum(IList<ClientCard> cards, int sum, int min, int max, int hint, bool mode)
{
IList<ClientCard> selected = Executor.OnSelectSum(cards, sum, min, max, hint, mode);
if (selected != null)
{
return selected;
}
if (hint == HintMsg.Release || hint == HintMsg.SynchroMaterial)
{
if (m_materialSelector != null)
{
selected = m_materialSelector.Select(cards, min, max);
}
else
{
switch (hint)
{
case HintMsg.SynchroMaterial:
selected = Executor.OnSelectSynchroMaterial(cards, sum, min, max);
break;
case HintMsg.Release:
selected = Executor.OnSelectRitualTribute(cards, sum, min, max);
break;
}
}
if (selected != null)
{
int s1 = 0, s2 = 0;
foreach (ClientCard card in selected)
{
s1 += card.OpParam1;
s2 += (card.OpParam2 != 0) ? card.OpParam2 : card.OpParam1;
}
if ((mode && (s1 == sum || s2 == sum)) || (!mode && (s1 >= sum || s2 >= sum)))
{
return selected;
}
}
}
if (mode)
{
// equal
if (sum == 0 && min == 0)
{
return new List<ClientCard>();
}
if (min <= 1)
{
// try special level first
foreach (ClientCard card in cards)
{
if (card.OpParam2 == sum)
{
return new[] { card };
}
}
// try level equal
foreach (ClientCard card in cards)
{
if (card.OpParam1 == sum)
{
return new[] { card };
}
}
}
// try all
int s1 = 0, s2 = 0;
foreach (ClientCard card in cards)
{
s1 += card.OpParam1;
s2 += (card.OpParam2 != 0) ? card.OpParam2 : card.OpParam1;
}
if (s1 == sum || s2 == sum)
{
return cards;
}
// try all combinations
int i = (min <= 1) ? 2 : min;
while (i <= max && i <= cards.Count)
{
IEnumerable<IEnumerable<ClientCard>> combos = CardContainer.GetCombinations(cards, i);
foreach (IEnumerable<ClientCard> combo in combos)
{
Logger.DebugWriteLine("--");
s1 = 0;
s2 = 0;
foreach (ClientCard card in combo)
{
s1 += card.OpParam1;
s2 += (card.OpParam2 != 0) ? card.OpParam2 : card.OpParam1;
}
if (s1 == sum || s2 == sum)
{
return combo.ToList();
}
}
i++;
}
}
else
{
// larger
if (min <= 1)
{
// try special level first
foreach (ClientCard card in cards)
{
if (card.OpParam2 >= sum)
{
return new[] { card };
}
}
// try level equal
foreach (ClientCard card in cards)
{
if (card.OpParam1 >= sum)
{
return new[] { card };
}
}
}
// try all combinations
int i = (min <= 1) ? 2 : min;
while (i <= max && i <= cards.Count)
{
IEnumerable<IEnumerable<ClientCard>> combos = CardContainer.GetCombinations(cards, i);
foreach (IEnumerable<ClientCard> combo in combos)
{
Logger.DebugWriteLine("----");
int s1 = 0, s2 = 0;
foreach (ClientCard card in combo)
{
s1 += card.OpParam1;
s2 += (card.OpParam2 != 0) ? card.OpParam2 : card.OpParam1;
}
if (s1 >= sum || s2 >= sum)
{
return combo.ToList();
}
}
i++;
}
}
Logger.WriteErrorLine("Fail to select sum.");
return new List<ClientCard>();
}
/// <summary>
/// Called when the AI has to tribute one or more cards.
/// </summary>
/// <param name="cards">List of available cards.</param>
/// <param name="min">Minimal quantity.</param>
/// <param name="max">Maximal quantity.</param>
/// <param name="hint">The hint message of the select.</param>
/// <param name="cancelable">True if you can return an empty list.</param>
/// <returns>A new list containing the tributed cards.</returns>
public IList<ClientCard> OnSelectTribute(IList<ClientCard> cards, int min, int max, int hint, bool cancelable)
{
// Always choose the minimum and lowest atk.
List<ClientCard> sorted = new List<ClientCard>();
sorted.AddRange(cards);
sorted.Sort(CardContainer.CompareCardAttack);
IList<ClientCard> selected = new List<ClientCard>();
for (int i = 0; i < min && i < sorted.Count; ++i)
selected.Add(sorted[i]);
return selected;
}
/// <summary>
/// Called when the AI has to select yes or no.
/// </summary>
/// <param name="desc">Id of the question.</param>
/// <returns>True for yes, false for no.</returns>
public bool OnSelectYesNo(int desc)
{
if (m_yesno != -1)
return m_yesno > 0;
return Executor.OnSelectYesNo(desc);
}
/// <summary>
/// Called when the AI has to select if to continue attacking when replay.
/// </summary>
/// <returns>True for yes, false for no.</returns>
public bool OnSelectBattleReplay()
{
return Executor.OnSelectBattleReplay();
}
/// <summary>
/// Called when the AI has to declare a card.
/// </summary>
/// <param name="avail">Available card's ids.</param>
/// <returns>Id of the selected card.</returns>
public int OnAnnounceCard(IList<int> avail)
{
int selected = Executor.OnAnnounceCard(avail);
if (avail.Contains(selected))
return selected;
if (avail.Contains(m_announce))
return m_announce;
else if (m_announce > 0)
Logger.WriteErrorLine("Pre-announced card cant be used: " + m_announce);
return avail[0];
}
// _ Others functions _
// Those functions are used by the AI behavior.
private CardSelector m_materialSelector;
private int m_materialSelectorHint;
private int m_place;
private int m_option;
private int m_number;
private int m_announce;
private int m_yesno;
private IList<CardAttribute> m_attributes = new List<CardAttribute>();
private IList<CardSelector> m_selector = new List<CardSelector>();
private IList<CardPosition> m_position = new List<CardPosition>();
private int m_selector_pointer = -1;
private IList<CardRace> m_races = new List<CardRace>();
public void SelectCard(ClientCard card)
{
m_selector_pointer = m_selector.Count();
m_selector.Add(new CardSelector(card));
}
public void SelectCard(IList<ClientCard> cards)
{
m_selector_pointer = m_selector.Count();
m_selector.Add(new CardSelector(cards));
}
public void SelectCard(int cardId)
{
m_selector_pointer = m_selector.Count();
m_selector.Add(new CardSelector(cardId));
}
public void SelectCard(IList<int> ids)
{
m_selector_pointer = m_selector.Count();
m_selector.Add(new CardSelector(ids));
}
public void SelectCard(params int[] ids)
{
m_selector_pointer = m_selector.Count();
m_selector.Add(new CardSelector(ids));
}
public void SelectCard(CardLocation loc)
{
m_selector_pointer = m_selector.Count();
m_selector.Add(new CardSelector(loc));
}
public void SelectNextCard(ClientCard card)
{
if (m_selector_pointer == -1)
{
Logger.WriteErrorLine("Error: Call SelectNextCard() before SelectCard()");
m_selector_pointer = 0;
}
m_selector.Insert(m_selector_pointer, new CardSelector(card));
}
public void SelectNextCard(IList<ClientCard> cards)
{
if (m_selector_pointer == -1)
{
Logger.WriteErrorLine("Error: Call SelectNextCard() before SelectCard()");
m_selector_pointer = 0;
}
m_selector.Insert(m_selector_pointer, new CardSelector(cards));
}
public void SelectNextCard(int cardId)
{
if (m_selector_pointer == -1)
{
Logger.WriteErrorLine("Error: Call SelectNextCard() before SelectCard()");
m_selector_pointer = 0;
}
m_selector.Insert(m_selector_pointer, new CardSelector(cardId));
}
public void SelectNextCard(IList<int> ids)
{
if (m_selector_pointer == -1)
{
Logger.WriteErrorLine("Error: Call SelectNextCard() before SelectCard()");
m_selector_pointer = 0;
}
m_selector.Insert(m_selector_pointer, new CardSelector(ids));
}
public void SelectNextCard(params int[] ids)
{
if (m_selector_pointer == -1)
{
Logger.WriteErrorLine("Error: Call SelectNextCard() before SelectCard()");
m_selector_pointer = 0;
}
m_selector.Insert(m_selector_pointer, new CardSelector(ids));
}
public void SelectNextCard(CardLocation loc)
{
if (m_selector_pointer == -1)
{
Logger.WriteErrorLine("Error: Call SelectNextCard() before SelectCard()");
m_selector_pointer = 0;
}
m_selector.Insert(m_selector_pointer, new CardSelector(loc));
}
public void SelectThirdCard(ClientCard card)
{
if (m_selector_pointer == -1)
{
Logger.WriteErrorLine("Error: Call SelectThirdCard() before SelectCard()");
m_selector_pointer = 0;
}
m_selector.Insert(m_selector_pointer, new CardSelector(card));
}
public void SelectThirdCard(IList<ClientCard> cards)
{
if (m_selector_pointer == -1)
{
Logger.WriteErrorLine("Error: Call SelectThirdCard() before SelectCard()");
m_selector_pointer = 0;
}
m_selector.Insert(m_selector_pointer, new CardSelector(cards));
}
public void SelectThirdCard(int cardId)
{
if (m_selector_pointer == -1)
{
Logger.WriteErrorLine("Error: Call SelectThirdCard() before SelectCard()");
m_selector_pointer = 0;
}
m_selector.Insert(m_selector_pointer, new CardSelector(cardId));
}
public void SelectThirdCard(IList<int> ids)
{
if (m_selector_pointer == -1)
{
Logger.WriteErrorLine("Error: Call SelectThirdCard() before SelectCard()");
m_selector_pointer = 0;
}
m_selector.Insert(m_selector_pointer, new CardSelector(ids));
}
public void SelectThirdCard(params int[] ids)
{
if (m_selector_pointer == -1)
{
Logger.WriteErrorLine("Error: Call SelectThirdCard() before SelectCard()");
m_selector_pointer = 0;
}
m_selector.Insert(m_selector_pointer, new CardSelector(ids));
}
public void SelectThirdCard(CardLocation loc)
{
if (m_selector_pointer == -1)
{
Logger.WriteErrorLine("Error: Call SelectThirdCard() before SelectCard()");
m_selector_pointer = 0;
}
m_selector.Insert(m_selector_pointer, new CardSelector(loc));
}
public void SelectMaterials(ClientCard card, int hint = 0)
{
m_materialSelector = new CardSelector(card);
m_materialSelectorHint = hint;
}
public void SelectMaterials(IList<ClientCard> cards, int hint = 0)
{
m_materialSelector = new CardSelector(cards);
m_materialSelectorHint = hint;
}
public void SelectMaterials(int cardId, int hint = 0)
{
m_materialSelector = new CardSelector(cardId);