-
Notifications
You must be signed in to change notification settings - Fork 95
/
Copy pathretreat.asm
1012 lines (937 loc) · 20.8 KB
/
retreat.asm
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
; determine AI score for retreating
; return carry if AI decides to retreat
AIDecideWhetherToRetreat:
ld a, [wConfusionRetreatCheckWasUnsuccessful]
or a
jp nz, .no_carry
xor a
ld [wAIPlayEnergyCardForRetreat], a
call LoadDefendingPokemonColorWRAndPrizeCards
ld a, $80 ; initial retreat score
ld [wAIScore], a
ld a, [wAIRetreatScore]
or a
jr z, .check_status
; add wAIRetreatScore * 8 to score
srl a
srl a
sla a ; *8
call AIEncourage
.check_status
ld a, DUELVARS_ARENA_CARD_STATUS
call GetTurnDuelistVariable
or a
jr z, .skip_status_check ; no status
and DOUBLE_POISONED
jr z, .check_cnf ; no poison
ld a, 2
call AIEncourage
.check_cnf
ld a, [hl]
and CNF_SLP_PRZ
cp CONFUSED
jr nz, .skip_status_check
ld a, 1
call AIEncourage
.skip_status_check
xor a ; PLAY_AREA_ARENA
ldh [hTempPlayAreaLocation_ff9d], a
call CheckIfAnyAttackKnocksOutDefendingCard
jr nc, .active_cant_ko_1
call CheckIfSelectedAttackIsUnusable
jp nc, .active_cant_use_atk
call LookForEnergyNeededForAttackInHand
jr nc, .active_cant_ko_1
.active_cant_use_atk
ld a, 5
call AIDiscourage
ld a, [wAIOpponentPrizeCount]
cp 2
jr nc, .active_cant_ko_1
ld a, 35
call AIDiscourage
.active_cant_ko_1
call CheckIfDefendingPokemonCanKnockOut
jr nc, .defending_cant_ko
ld a, 2
call AIEncourage
call CheckIfNotABossDeckID
jr c, .check_resistance_1
ld a, [wAIPlayerPrizeCount]
cp 2
jr nc, .check_prize_count
ld a, TRUE
ld [wAIPlayEnergyCardForRetreat], a
.defending_cant_ko
call CheckIfNotABossDeckID
jr c, .check_resistance_1
ld a, [wAIPlayerPrizeCount]
cp 2
jr nc, .check_prize_count
ld a, 2
call AIEncourage
.check_prize_count
ld a, [wAIOpponentPrizeCount]
cp 2
jr nc, .check_resistance_1
ld a, 2
call AIDiscourage
.check_resistance_1
call GetArenaCardColor
call TranslateColorToWR
ld b, a
ld a, [wAIPlayerResistance]
and b
jr z, .check_weakness_1
ld a, 1
call AIEncourage
; check bench for Pokémon that
; the defending card is not resistant to
; if one is found, skip AIDiscourage
ld a, [wAIPlayerResistance]
ld b, a
ld a, DUELVARS_BENCH
call GetTurnDuelistVariable
.loop_resistance_1
ld a, [hli]
cp $ff
jr z, .exit_loop_resistance_1
call LoadCardDataToBuffer1_FromDeckIndex
ld a, [wLoadedCard1Type]
call TranslateColorToWR
and b
jr nz, .loop_resistance_1
jr .check_weakness_1
.exit_loop_resistance_1
ld a, 2
call AIDiscourage
.check_weakness_1
ld a, [wAIPlayerColor]
ld b, a
call GetArenaCardWeakness
and b
jr z, .check_resistance_2
ld a, 2
call AIEncourage
; check bench for Pokémon that
; is not weak to defending Pokémon
; if one is found, skip AIDiscourage
ld a, [wAIPlayerColor]
ld b, a
ld a, DUELVARS_BENCH
call GetTurnDuelistVariable
.loop_weakness_1
ld a, [hli]
cp $ff
jr z, .exit_loop_weakness_1
call LoadCardDataToBuffer1_FromDeckIndex
ld a, [wLoadedCard1Weakness]
and b
jr nz, .loop_weakness_1
jr .check_resistance_2
.exit_loop_weakness_1
ld a, 3
call AIDiscourage
.check_resistance_2
ld a, [wAIPlayerColor]
ld b, a
call GetArenaCardResistance
and b
jr z, .check_weakness_2
ld a, 3
call AIDiscourage
; check bench for Pokémon that
; is the defending Pokémon's weakness
; if none is found, skip AIEncourage
.check_weakness_2
ld a, [wAIPlayerWeakness]
ld b, a
ld a, DUELVARS_BENCH
call GetTurnDuelistVariable
ld e, PLAY_AREA_BENCH_1 - 1
.loop_weakness_2
inc e
ld a, [hli]
cp $ff
jr z, .check_resistance_3
push de
call LoadCardDataToBuffer1_FromDeckIndex
ld a, [wLoadedCard1Type]
call TranslateColorToWR
pop de
and b
jr z, .loop_weakness_2
ld a, 2
call AIEncourage
push de
ld a, DUELVARS_ARENA_CARD
call GetTurnDuelistVariable
call GetCardIDFromDeckIndex
ld a, e
pop de
cp PORYGON
jr nz, .check_weakness_3
; handle Porygon
ld a, e
call CheckIfCanDamageDefendingPokemon
jr nc, .check_weakness_3
ld a, 10
call AIEncourage
jr .check_resistance_3
.check_weakness_3
call GetArenaCardColor
call TranslateColorToWR
ld b, a
ld a, [wAIPlayerWeakness]
and b
jr z, .check_resistance_3
ld a, 3
call AIDiscourage
; check bench for Pokémon that
; is resistant to defending Pokémon
; if none is found, skip AIEncourage
.check_resistance_3
ld a, [wAIPlayerColor]
ld b, a
ld a, DUELVARS_BENCH
call GetTurnDuelistVariable
.loop_resistance_2
ld a, [hli]
cp $ff
jr z, .check_ko_2
call LoadCardDataToBuffer1_FromDeckIndex
ld a, [wLoadedCard1Resistance]
and b
jr z, .loop_resistance_2
ld a, 1
call AIEncourage
; check bench for Pokémon that
; can KO defending Pokémon
; if none is found, skip AIEncourage
.check_ko_2
ld a, DUELVARS_BENCH
call GetTurnDuelistVariable
ld c, 0
.loop_ko_1
inc c
ld a, [hli]
cp $ff
jr z, .check_defending_id
ld a, c
ldh [hTempPlayAreaLocation_ff9d], a
push hl
push bc
call CheckIfAnyAttackKnocksOutDefendingCard
jr nc, .no_ko
call CheckIfSelectedAttackIsUnusable
jr nc, .success
call LookForEnergyNeededForAttackInHand
jr c, .success
.no_ko
pop bc
pop hl
jr .loop_ko_1
.success
pop bc
pop hl
ld a, 2
call AIEncourage
; a bench Pokémon was found that can KO
; if this is a boss deck and it's at last prize card
; if arena Pokémon cannot KO, add to AI score
; and set wAIPlayEnergyCardForRetreat to $01
ld a, [wAIOpponentPrizeCount]
cp 2
jr nc, .check_defending_id
call CheckIfNotABossDeckID
jr c, .check_defending_id
xor a ; PLAY_AREA_ARENA
ldh [hTempPlayAreaLocation_ff9d], a
call CheckIfAnyAttackKnocksOutDefendingCard
jr nc, .active_cant_ko_2
call CheckIfSelectedAttackIsUnusable
jp nc, .check_defending_id
.active_cant_ko_2
ld a, 40
call AIEncourage
ld a, TRUE
ld [wAIPlayEnergyCardForRetreat], a
.check_defending_id
ld a, DUELVARS_ARENA_CARD
call GetNonTurnDuelistVariable
call SwapTurn
call GetCardIDFromDeckIndex
call SwapTurn
ld a, e
cp MR_MIME
jr z, .mr_mime_or_hitmonlee
cp HITMONLEE
jr nz, .check_retreat_cost
; check bench if there's any Pokémon
; that can damage defending Pokémon
; this is done because of Mr. Mime's PKMN PWR
; and Hitmonlee's ability to attack Bench
.mr_mime_or_hitmonlee
xor a ; PLAY_AREA_ARENA
call CheckIfCanDamageDefendingPokemon
jr c, .check_retreat_cost
ld a, DUELVARS_BENCH
call GetTurnDuelistVariable
ld c, 0
.loop_damage
inc c
ld a, [hli]
cp $ff
jr z, .check_retreat_cost
ld a, c
push hl
push bc
call CheckIfCanDamageDefendingPokemon
jr c, .can_damage
pop bc
pop hl
jr .loop_damage
.can_damage
pop bc
pop hl
ld a, 5
call AIEncourage
ld a, TRUE
ld [wAIPlayEnergyCardForRetreat], a
; subtract from wAIScore if retreat cost is larger than 1
; then check if any cards have at least half HP,
; are final evolutions and can use second attack in the bench
; and adds to wAIScore if the active Pokémon doesn't meet
; these conditions
.check_retreat_cost
xor a ; PLAY_AREA_ARENA
ldh [hTempPlayAreaLocation_ff9d], a
call GetPlayAreaCardRetreatCost
cp 2
jr c, .one_or_none
cp 3
jr nc, .three_or_more
; exactly two
ld a, 1
call AIDiscourage
jr .one_or_none
.three_or_more
ld a, 2
call AIDiscourage
.one_or_none
call CheckIfArenaCardIsFullyPowered
jr c, .check_defending_can_ko
call CountNumberOfSetUpBenchPokemon
cp 2
jr c, .check_defending_can_ko
call AIEncourage
; check bench for Pokémon that
; the defending Pokémon can't knock out
; if none is found, skip AIDiscourage
.check_defending_can_ko
ld a, DUELVARS_BENCH
call GetTurnDuelistVariable
ld e, 0
.loop_ko_2
inc e
ld a, [hli]
cp $ff
jr z, .exit_loop_ko
push de
push hl
call LoadCardDataToBuffer2_FromDeckIndex
ld a, [wLoadedCard2ID]
pop hl
pop de
cp MYSTERIOUS_FOSSIL
jr z, .loop_ko_2
cp CLEFAIRY_DOLL
jr z, .loop_ko_2
ld a, e
ldh [hTempPlayAreaLocation_ff9d], a
push de
push hl
call CheckIfDefendingPokemonCanKnockOut
pop hl
pop de
jr c, .loop_ko_2
jr .check_active_id
.exit_loop_ko
ld a, 20
call AIDiscourage
.check_active_id
ld a, DUELVARS_ARENA_CARD
call GetTurnDuelistVariable
call GetCardIDFromDeckIndex
ld a, e
cp MYSTERIOUS_FOSSIL
jr z, .mysterious_fossil_or_clefairy_doll
cp CLEFAIRY_DOLL
jr z, .mysterious_fossil_or_clefairy_doll
; if wAIScore is at least 131, set carry
ld a, [wAIScore]
cp 131
jr nc, .set_carry
.no_carry
or a
ret
.set_carry
scf
ret
; set carry regardless if active card is
; either Mysterious Fossil or Clefairy Doll
; and there's a bench Pokémon who is not KO'd
; by defending Pokémon and can damage it
.mysterious_fossil_or_clefairy_doll
ld e, 0
.loop_ko_3
inc e
ld a, e
add DUELVARS_ARENA_CARD
call GetTurnDuelistVariable
cp $ff
jr z, .no_carry
ld a, e
ldh [hTempPlayAreaLocation_ff9d], a
push de
call CheckIfDefendingPokemonCanKnockOut
pop de
jr c, .loop_ko_3
ld a, e
push de
call CheckIfCanDamageDefendingPokemon
pop de
jr nc, .loop_ko_3
jr .set_carry
; if player's turn and loaded attack is not a Pokémon Power OR
; if opponent's turn and wAITriedAttack == 0
; set wAIRetreatFlags's bit 7 flag
SetAIRetreatFlags:
xor a
ld [wAIRetreatFlags], a
ld a, [wWhoseTurn]
cp OPPONENT_TURN
jr z, .opponent
; player
ld a, [wLoadedAttackCategory]
cp POKEMON_POWER
ret z
jr .set_flag
.opponent
ld a, [wAITriedAttack]
or a
ret nz
.set_flag
ld a, %10000000
ld [wAIRetreatFlags], a
ret
; calculates AI score for bench Pokémon
; returns in a and [hTempPlayAreaLocation_ff9d] the
; Play Area location of best card to switch to.
; returns carry if no Bench Pokemon.
AIDecideBenchPokemonToSwitchTo:
xor a ; PLAY_AREA_ARENA
ldh [hTempPlayAreaLocation_ff9d], a
ld a, DUELVARS_NUMBER_OF_POKEMON_IN_PLAY_AREA
call GetTurnDuelistVariable
cp 2
ret c
; has at least 2 Pokémon in Play Area
call SetAIRetreatFlags
call LoadDefendingPokemonColorWRAndPrizeCards
ld a, 50
ld [wAIScore], a
ld a, DUELVARS_NUMBER_OF_POKEMON_IN_PLAY_AREA
call GetTurnDuelistVariable
ld b, a
ld c, PLAY_AREA_ARENA
push bc
jp .store_score
.loop_play_area
push bc
ld a, c
ldh [hTempPlayAreaLocation_ff9d], a
ld a, 50
ld [wAIScore], a
; check if card can KO defending Pokémon
; if it can, raise AI score
; if on last prize card, raise AI score again
call CheckIfAnyAttackKnocksOutDefendingCard
jr nc, .check_can_use_atks
call CheckIfSelectedAttackIsUnusable
jr c, .check_can_use_atks
ld a, 10
call AIEncourage
ld a, [wAIRetreatFlags]
or %00000001
ld [wAIRetreatFlags], a
call CountPrizes
cp 2
jp nc, .check_defending_weak
ld a, 10
call AIEncourage
; calculates damage of both attacks
; to raise AI score accordingly
.check_can_use_atks
xor a ; FIRST_ATTACK_OR_PKMN_POWER
ld [wSelectedAttack], a
call CheckIfSelectedAttackIsUnusable
call nc, .HandleAttackDamageScore
ld a, SECOND_ATTACK
ld [wSelectedAttack], a
call CheckIfSelectedAttackIsUnusable
call nc, .HandleAttackDamageScore
jr .check_energy_card
; adds to AI score depending on amount of damage
; it can inflict to the defending Pokémon
; AI score += floor(Damage / 10) + 1
.HandleAttackDamageScore
ld a, [wSelectedAttack]
call EstimateDamage_VersusDefendingCard
ld a, [wDamage]
call ConvertHPToDamageCounters_Bank5
inc a
call AIEncourage
ret
; if an energy card that is needed is found in hand
; calculate damage of the move and raise AI score
; AI score += floor(Damage / 20)
.check_energy_card
call LookForEnergyNeededInHand
jr nc, .check_attached_energy
ld a, [wSelectedAttack]
call EstimateDamage_VersusDefendingCard
ld a, [wDamage]
call ConvertHPToDamageCounters_Bank5
srl a
call AIEncourage
; if no energies attached to card, lower AI score
.check_attached_energy
ldh a, [hTempPlayAreaLocation_ff9d]
ld e, a
call GetPlayAreaCardAttachedEnergies
ld a, [wTotalAttachedEnergies]
or a
jr nz, .check_mr_mime
ld a, 1
call AIDiscourage
; if can damage Mr Mime, raise AI score
.check_mr_mime
ld a, DUELVARS_ARENA_CARD
call GetNonTurnDuelistVariable
call SwapTurn
call LoadCardDataToBuffer2_FromDeckIndex
call SwapTurn
cp MR_MIME
jr nz, .check_defending_weak
xor a ; FIRST_ATTACK_OR_PKMN_POWER
call EstimateDamage_VersusDefendingCard
ld a, [wDamage]
or a
jr nz, .can_damage
ld a, SECOND_ATTACK
call EstimateDamage_VersusDefendingCard
ld a, [wDamage]
or a
jr z, .check_defending_weak
.can_damage
ld a, 5
call AIEncourage
; if defending card is weak to this card, raise AI score
.check_defending_weak
ldh a, [hTempPlayAreaLocation_ff9d]
add DUELVARS_ARENA_CARD
call GetTurnDuelistVariable
call LoadCardDataToBuffer1_FromDeckIndex
ld a, [wLoadedCard1Type]
call TranslateColorToWR
ld c, a
ld hl, wAIPlayerWeakness
and [hl]
jr z, .check_defending_resist
ld a, 3
call AIEncourage
; if defending card is resistant to this card, lower AI score
.check_defending_resist
ld a, c
ld hl, wAIPlayerResistance
and [hl]
jr z, .check_resistance
ld a, 2
call AIDiscourage
; if this card is resistant to defending Pokémon, raise AI score
.check_resistance
ld a, [wAIPlayerColor]
ld hl, wLoadedCard1Resistance
and [hl]
jr z, .check_weakness
ld a, 2
call AIEncourage
; if this card is weak to defending Pokémon, lower AI score
.check_weakness
ld a, [wAIPlayerColor]
ld hl, wLoadedCard1Weakness
and [hl]
jr z, .check_retreat_cost
ld a, 3
call AIDiscourage
; if this card's retreat cost < 2, raise AI score
; if this card's retreat cost > 2, lower AI score
.check_retreat_cost
call GetPlayAreaCardRetreatCost
cp 2
jr c, .one_or_none
jr z, .check_player_prize_count
ld a, 1
call AIDiscourage
jr .check_player_prize_count
.one_or_none
ld a, 1
call AIEncourage
; if wAIRetreatFlags != $81
; if defending Pokémon can KO this card
; if player is not at last prize card, lower 3 from AI score
; if player is at last prize card, lower 10 from AI score
.check_player_prize_count
ld a, [wAIRetreatFlags]
cp %10000000 | %00000001
jr z, .check_hp
call CheckIfDefendingPokemonCanKnockOut
jr nc, .check_hp
ld e, 3
ld a, [wAIPlayerPrizeCount]
cp 1
jr nz, .lower_score_1
ld e, 10
.lower_score_1
ld a, e
call AIDiscourage
; if this card's HP is 0, make AI score 0
.check_hp
ldh a, [hTempPlayAreaLocation_ff9d]
add DUELVARS_ARENA_CARD_HP
call GetTurnDuelistVariable
or a
jr nz, .add_hp_score
ld [wAIScore], a
jr .store_score
; AI score += floor(HP/40)
.add_hp_score
ld b, a
ld a, 4
call CalculateBDividedByA_Bank5
call ConvertHPToDamageCounters_Bank5
call AIEncourage
; raise AI score if
; - is a Mr Mime OR
; - is a MewLv8 and defending card is not basic stage
ldh a, [hTempPlayAreaLocation_ff9d]
add DUELVARS_ARENA_CARD
call GetTurnDuelistVariable
call LoadCardDataToBuffer1_FromDeckIndex
cp MR_MIME
jr z, .raise_score
cp MEW_LV8
jr nz, .check_if_has_bench_utility
ld a, DUELVARS_ARENA_CARD
call GetNonTurnDuelistVariable
call LoadCardDataToBuffer2_FromDeckIndex
ld a, [wLoadedCard2Stage]
or a
jr z, .check_if_has_bench_utility
.raise_score
ld a, 5
call AIEncourage
; if wLoadedCard1AIInfo == AI_INFO_BENCH_UTILITY,
; lower AI score
.check_if_has_bench_utility
ld a, [wLoadedCard1AIInfo]
; bug, should mask out HAS_EVOLUTION flag first
cp AI_INFO_BENCH_UTILITY
jr nz, .mysterious_fossil_or_clefairy_doll
ld a, 2
call AIDiscourage
; if card is Mysterious Fossil or Clefairy Doll,
; lower AI score
.mysterious_fossil_or_clefairy_doll
ld a, [wLoadedCard1ID]
cp MYSTERIOUS_FOSSIL
jr z, .lower_score_2
cp CLEFAIRY_DOLL
jr nz, .ai_score_bonus
.lower_score_2
ld a, 10
call AIDiscourage
.ai_score_bonus
ld b, a
ld a, [wAICardListRetreatBonus + 1]
or a
jr z, .store_score
ld h, a
ld a, [wAICardListRetreatBonus + 0]
ld l, a
.loop_ids
ld a, [hli]
or a
jr z, .store_score ; list is over
cp b
jr nz, .next_id
ld a, [hl]
cp $80
jr c, .subtract_score
sub $80
call AIEncourage
jr .next_id
.subtract_score
ld c, a
ld a, $80
sub c
call AIDiscourage
.next_id
inc hl
jr .loop_ids
.store_score
ldh a, [hTempPlayAreaLocation_ff9d]
ld c, a
ld b, $00
ld hl, wPlayAreaAIScore
add hl, bc
ld a, [wAIScore]
ld [hl], a
pop bc
inc c
dec b
jp nz, .loop_play_area
; done
xor a
ld [wAIRetreatScore], a
jp FindHighestBenchScore
; handles AI action of retreating Arena Pokémon
; and chooses which energy cards to discard.
; if card can't discard, return carry.
; in case it's Clefairy Doll or Mysterious Fossil,
; handle its effect to discard itself instead of retreating.
; input:
; - a = Play Area location (PLAY_AREA_*) of card to retreat to.
AITryToRetreat:
push af
ld a, [wAIPlayEnergyCardForRetreat]
or a
jr z, .check_id
; AI is allowed to play an energy card
; from the hand in order to provide
; the necessary energy for retreat cost
; check status
ld a, DUELVARS_ARENA_CARD_STATUS
call GetTurnDuelistVariable
and CNF_SLP_PRZ
cp ASLEEP
jp z, .check_id
cp PARALYZED
jp z, .check_id
; if an energy card hasn't been played yet,
; checks if the Pokémon needs just one more energy to retreat
; if it does, check if there are any energy cards in hand
; and if there are, play that energy card
ld a, [wAlreadyPlayedEnergy]
or a
jr nz, .check_id
ld e, PLAY_AREA_ARENA
call CountNumberOfEnergyCardsAttached
push af
xor a ; PLAY_AREA_ARENA
ldh [hTempPlayAreaLocation_ff9d], a
call GetPlayAreaCardRetreatCost
pop bc
cp b
jr c, .check_id
jr z, .check_id
; energy attached < retreat cost
sub b
cp 1
jr nz, .check_id
call CreateEnergyCardListFromHand
jr c, .check_id
ld a, [wDuelTempList]
ldh [hTemp_ffa0], a
xor a ; PLAY_AREA_ARENA
ldh [hTempPlayAreaLocation_ffa1], a
ld a, OPPACTION_PLAY_ENERGY
bank1call AIMakeDecision
.check_id
ld a, DUELVARS_ARENA_CARD
call GetTurnDuelistVariable
call GetCardIDFromDeckIndex
ld a, e
cp MYSTERIOUS_FOSSIL
jp z, .mysterious_fossil_or_clefairy_doll
cp CLEFAIRY_DOLL
jp z, .mysterious_fossil_or_clefairy_doll
; if card is Asleep or Paralyzed, set carry and exit
; else, load the status in hTemp_ffa0
pop af
ldh [hTempPlayAreaLocation_ffa1], a
ld a, DUELVARS_ARENA_CARD_STATUS
call GetTurnDuelistVariable
ld b, a
and CNF_SLP_PRZ
cp ASLEEP
jp z, .set_carry
cp PARALYZED
jp z, .set_carry
ld a, b
ldh [hTemp_ffa0], a
ld a, $ff
ldh [hTempRetreatCostCards], a
; check energy required to retreat
; if the cost is 0, retreat right away
xor a ; PLAY_AREA_ARENA
ldh [hTempPlayAreaLocation_ff9d], a
call GetPlayAreaCardRetreatCost
ld [wTempCardRetreatCost], a
or a
jp z, .retreat
; if cost > 0 and number of energy cards attached == cost
; discard them all
xor a ; PLAY_AREA_ARENA
call CreateArenaOrBenchEnergyCardList
ld e, PLAY_AREA_ARENA
call GetPlayAreaCardAttachedEnergies
ld a, [wTotalAttachedEnergies]
ld c, a
ld a, [wTempCardRetreatCost]
cp c
jr nz, .choose_energy_discard
ld hl, hTempRetreatCostCards
ld de, wDuelTempList
.loop_1
ld a, [de]
inc de
ld [hli], a
cp $ff
jr nz, .loop_1
jp .retreat
; if cost > 0 and number of energy cards attached > cost
; choose energy cards to discard according to color
.choose_energy_discard
ld a, DUELVARS_ARENA_CARD
call GetTurnDuelistVariable
call GetCardIDFromDeckIndex
ld a, e
ld [wTempCardID], a
call LoadCardDataToBuffer1_FromCardID
ld a, [wLoadedCard1Type]
or TYPE_ENERGY
ld [wTempCardType], a
ld a, [wTempCardRetreatCost]
ld c, a
; first, look for and discard double colorless energy
; if retreat cost is >= 2
ld hl, wDuelTempList
ld de, hTempRetreatCostCards
.loop_2
ld a, c
cp 2
jr c, .energy_not_same_color
ld a, [hli]
cp $ff
jr z, .energy_not_same_color
ld [de], a
push de
call GetCardIDFromDeckIndex
ld a, e
pop de
cp DOUBLE_COLORLESS_ENERGY
jr nz, .loop_2
ld a, [de]
call RemoveCardFromDuelTempList
dec hl
inc de
dec c
dec c
jr nz, .loop_2
jr .end_retreat_list
; second, shuffle attached cards and discard energy cards
; that are not of the same type as the Pokémon
; the exception for this are cards that are needed for
; some attacks but are not of the same color as the Pokémon
; (i.e. Psyduck's Headache attack)
; and energy cards attached to Eevee corresponding to a
; color of any of its evolutions (water, fire, lightning)
.energy_not_same_color
ld hl, wDuelTempList
call CountCardsInDuelTempList
call ShuffleCards
.loop_3
ld a, [hli]
cp $ff
jr z, .any_energy
ld [de], a
call CheckIfEnergyIsUseful
jr c, .loop_3
ld a, [de]
call RemoveCardFromDuelTempList
dec hl
inc de
dec c
jr nz, .loop_3
jr .end_retreat_list
; third, discard any card until
; cost requirement is met
.any_energy
ld hl, wDuelTempList
.loop_4
ld a, [hli]
cp $ff
jr z, .set_carry
ld [de], a
inc de
push de
call GetCardIDFromDeckIndex
ld a, e
pop de
cp DOUBLE_COLORLESS_ENERGY
jr nz, .not_double_colorless
dec c
jr z, .end_retreat_list
.not_double_colorless
dec c
jr nz, .loop_4
.end_retreat_list
ld a, $ff
ld [de], a
.retreat
ld a, OPPACTION_ATTEMPT_RETREAT
bank1call AIMakeDecision
or a
ret
.set_carry
scf
ret
; handle Mysterious Fossil and Clefairy Doll
; if there are bench Pokémon, use effect to discard card
; this is equivalent to using its Pokémon Power
.mysterious_fossil_or_clefairy_doll
ld a, DUELVARS_NUMBER_OF_POKEMON_IN_PLAY_AREA
call GetTurnDuelistVariable
cp 2
jr nc, .has_bench
; doesn't have any bench
pop af
jr .set_carry
.has_bench
ld a, DUELVARS_ARENA_CARD
call GetTurnDuelistVariable
ldh [hTempCardIndex_ff9f], a