-
Notifications
You must be signed in to change notification settings - Fork 93
/
Copy pathtrainer_cards.asm
6058 lines (5441 loc) · 121 KB
/
trainer_cards.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
INCLUDE "data/duel/ai_trainer_card_logic.asm"
_AIProcessHandTrainerCards:
ld [wAITrainerCardPhase], a
; create hand list in wDuelTempList and wTempHandCardList.
call CreateHandCardList
ld hl, wDuelTempList
ld de, wTempHandCardList
call CopyListWithFFTerminatorFromHLToDE_Bank8
ld hl, wTempHandCardList
.loop_hand
ld a, [hli]
ld [wAITrainerCardToPlay], a
cp $ff
ret z
push hl
ld a, [wAITrainerCardPhase]
ld d, a
ld hl, AITrainerCardLogic
.loop_data
xor a
ld [wCurrentAIFlags], a
ld a, [hli]
cp $ff
jp z, .pop_hl
; compare input to first byte in data and continue if equal.
cp d
jp nz, .inc_hl_by_5
ld a, [hli]
ld [wAITrainerLogicCard], a
ld a, [wAITrainerCardToPlay]
call LoadCardDataToBuffer1_FromDeckIndex
cp SWITCH
jr nz, .skip_switch_check
ld b, a
ld a, [wPreviousAIFlags]
and AI_FLAG_USED_SWITCH
jr nz, .inc_hl_by_4
ld a, b
.skip_switch_check
; compare hand card to second byte in data and continue if equal.
ld b, a
ld a, [wAITrainerLogicCard]
cp b
jr nz, .inc_hl_by_4
; found Trainer card
push hl
push de
ld a, [wAITrainerCardToPlay]
ldh [hTempCardIndex_ff9f], a
; if Headache effects prevent playing card
; move on to the next item in list.
bank1call CheckCantUseTrainerDueToEffect
jp c, .next_in_data
call LoadNonPokemonCardEffectCommands
ld a, EFFECTCMDTYPE_INITIAL_EFFECT_1
call TryExecuteEffectCommandFunction
jp c, .next_in_data
; AI can randomly choose not to play card.
farcall AIChooseRandomlyNotToDoAction
jr c, .next_in_data
; call routine to decide whether to play Trainer card
pop de
pop hl
push hl
call CallIndirect
pop hl
jr nc, .inc_hl_by_4
; routine returned carry, which means
; this card should be played.
inc hl
inc hl
ld [wAITrainerCardParameter], a
; show Play Trainer Card screen
push de
push hl
ld a, [wAITrainerCardToPlay]
ldh [hTempCardIndex_ff9f], a
ld a, OPPACTION_PLAY_TRAINER
bank1call AIMakeDecision
pop hl
pop de
jr c, .inc_hl_by_2
; execute the effects of the Trainer card
push hl
call CallIndirect
pop hl
inc hl
inc hl
ld a, [wPreviousAIFlags]
ld b, a
ld a, [wCurrentAIFlags]
or b
ld [wPreviousAIFlags], a
pop hl
and AI_FLAG_MODIFIED_HAND
jp z, .loop_hand
; the hand was modified during the Trainer effect
; so it needs to be re-listed again and
; looped from the top.
call CreateHandCardList
ld hl, wDuelTempList
ld de, wTempHandCardList
call CopyListWithFFTerminatorFromHLToDE_Bank8
ld hl, wTempHandCardList
; clear the AI_FLAG_MODIFIED_HAND flag
ld a, [wPreviousAIFlags]
and ~AI_FLAG_MODIFIED_HAND
ld [wPreviousAIFlags], a
jp .loop_hand
.inc_hl_by_5
inc hl
.inc_hl_by_4
inc hl
inc hl
.inc_hl_by_2
inc hl
inc hl
jp .loop_data
.next_in_data
pop de
pop hl
inc hl
inc hl
inc hl
inc hl
jp .loop_data
.pop_hl
pop hl
jp .loop_hand
; makes AI use Potion card.
AIPlay_Potion:
ld a, [wAITrainerCardToPlay]
ldh [hTempCardIndex_ff9f], a
ld a, [wAITrainerCardParameter]
ldh [hTemp_ffa0], a
ld e, a
call GetCardDamageAndMaxHP
cp 20
jr c, .play_card
ld a, 20
.play_card
ldh [hTempPlayAreaLocation_ffa1], a
ld a, OPPACTION_EXECUTE_TRAINER_EFFECTS
bank1call AIMakeDecision
ret
; if AI doesn't decide to retreat this card,
; check if defending Pokémon can KO active card
; next turn after using Potion.
; if it cannot, return carry.
; also take into account whether attack is high recoil.
AIDecide_Potion_Phase07:
farcall AIDecideWhetherToRetreat
jr c, .no_carry
call AICheckIfAttackIsHighRecoil
jr c, .no_carry
xor a ; PLAY_AREA_ARENA
ldh [hTempPlayAreaLocation_ff9d], a
farcall CheckIfDefendingPokemonCanKnockOut
jr nc, .no_carry
ld d, a
ld a, DUELVARS_ARENA_CARD_HP
call GetTurnDuelistVariable
ld h, a
ld e, PLAY_AREA_ARENA
call GetCardDamageAndMaxHP
cp 20 + 1 ; if damage <= 20
jr c, .calculate_hp
ld a, 20 ; amount of Potion HP healing
; if damage done by defending Pokémon next turn will still
; KO this card after healing, return no carry.
.calculate_hp
ld l, a
ld a, h
add l
sub d
jr c, .no_carry
jr z, .no_carry
; return carry.
xor a
scf
ret
.no_carry
or a
ret
; finds a card in Play Area to use Potion on.
; output:
; a = card to use Potion on;
; carry set if Potion should be used.
AIDecide_Potion_Phase10:
xor a ; PLAY_AREA_ARENA
ldh [hTempPlayAreaLocation_ff9d], a
farcall CheckIfDefendingPokemonCanKnockOut
jr nc, .start_from_active
; can KO
ld d, a
ld a, DUELVARS_ARENA_CARD_HP
call GetTurnDuelistVariable
ld h, a
ld e, PLAY_AREA_ARENA
call GetCardDamageAndMaxHP
cp 20 + 1 ; if damage <= 20
jr c, .calculate_hp
ld a, 20
; return if using healing prevents KO.
.calculate_hp
ld l, a
ld a, h
add l
sub d
jr c, .count_prizes
jr z, .count_prizes
or a
ret
; using Potion on active card does not prevent a KO.
; if player is at last prize, start loop with active card.
; otherwise start loop at first bench Pokémon.
.count_prizes
call SwapTurn
call CountPrizes
call SwapTurn
dec a
jr z, .start_from_active
ld e, PLAY_AREA_BENCH_1
jr .loop
; find Play Area Pokémon with more than 10 damage.
; skip Pokémon if it has a BOOST_IF_TAKEN_DAMAGE attack.
.start_from_active
ld e, PLAY_AREA_ARENA
.loop
ld a, DUELVARS_ARENA_CARD
add e
call GetTurnDuelistVariable
cp $ff
ret z
call .check_boost_if_taken_damage
jr c, .has_boost_damage
call GetCardDamageAndMaxHP
cp 20 ; if damage >= 20
jr nc, .found
.has_boost_damage
inc e
jr .loop
; a card was found, now to check if it's active or benched.
.found
ld a, e
or a
jr z, .active_card
; bench card
push de
call SwapTurn
call CountPrizes
call SwapTurn
dec a
or a
jr z, .check_random
ld a, 10
call Random
cp 3
; 7/10 chance of returning carry.
.check_random
pop de
jr c, .no_carry
ld a, e
scf
ret
; return carry for active card if not High Recoil.
.active_card
push de
call AICheckIfAttackIsHighRecoil
pop de
jr c, .no_carry
ld a, e
scf
ret
.no_carry
or a
ret
; return carry if either of the attacks are usable
; and have the BOOST_IF_TAKEN_DAMAGE effect.
.check_boost_if_taken_damage
push de
xor a ; FIRST_ATTACK_OR_PKMN_POWER
ld [wSelectedAttack], a
farcall CheckIfSelectedAttackIsUnusable
jr c, .second_attack
ld a, ATTACK_FLAG3_ADDRESS | BOOST_IF_TAKEN_DAMAGE_F
call CheckLoadedAttackFlag
jr c, .set_carry
.second_attack
ld a, SECOND_ATTACK
ld [wSelectedAttack], a
farcall CheckIfSelectedAttackIsUnusable
jr c, .false
ld a, ATTACK_FLAG3_ADDRESS | BOOST_IF_TAKEN_DAMAGE_F
call CheckLoadedAttackFlag
jr c, .set_carry
.false
pop de
or a
ret
.set_carry
pop de
scf
ret
; makes AI use Super Potion card.
AIPlay_SuperPotion:
ld a, [wAITrainerCardToPlay]
ldh [hTempCardIndex_ff9f], a
ld a, [wAITrainerCardParameter]
ldh [hTempPlayAreaLocation_ffa1], a
call AIPickEnergyCardToDiscard
ldh [hTemp_ffa0], a
ld a, [wAITrainerCardParameter]
ld e, a
call GetCardDamageAndMaxHP
cp 40
jr c, .play_card
ld a, 40
.play_card
ldh [hTempRetreatCostCards], a
ld a, OPPACTION_EXECUTE_TRAINER_EFFECTS
bank1call AIMakeDecision
ret
; if AI doesn't decide to retreat this card and card has
; any energy cards attached, check if defending Pokémon can KO
; active card next turn after using Super Potion.
; if it cannot, return carry.
; also take into account whether attack is high recoil.
AIDecide_SuperPotion1:
farcall AIDecideWhetherToRetreat
jr c, .no_carry
call AICheckIfAttackIsHighRecoil
jr c, .no_carry
xor a ; PLAY_AREA_ARENA
ldh [hTempPlayAreaLocation_ff9d], a
ld e, a
call .check_attached_energy
ret nc
farcall CheckIfDefendingPokemonCanKnockOut
jr nc, .no_carry
ld d, a
ld d, a
ld a, DUELVARS_ARENA_CARD_HP
call GetTurnDuelistVariable
ld h, a
ld e, PLAY_AREA_ARENA
call GetCardDamageAndMaxHP
cp 40 + 1 ; if damage < 40
jr c, .calculate_hp
ld a, 40
.calculate_hp
ld l, a
ld a, h
add l
sub d
jr c, .no_carry
jr z, .no_carry
; return carry
ld a, e
scf
ret
.no_carry
or a
ret
; returns carry if card has energies attached.
.check_attached_energy
call GetPlayAreaCardAttachedEnergies
ld a, [wTotalAttachedEnergies]
or a
ret z
scf
ret
; finds a card in Play Area to use Super Potion on.
; output:
; a = card to use Super Potion on;
; carry set if Super Potion should be used.
AIDecide_SuperPotion2:
xor a ; PLAY_AREA_ARENA
ldh [hTempPlayAreaLocation_ff9d], a
farcall CheckIfDefendingPokemonCanKnockOut
jr nc, .start_from_active
; can KO
ld d, a
ld a, DUELVARS_ARENA_CARD_HP
call GetTurnDuelistVariable
ld h, a
ld e, PLAY_AREA_ARENA
call GetCardDamageAndMaxHP
cp 40 + 1 ; if damage < 40
jr c, .calculate_hp
ld a, 40
; return if using healing prevents KO.
.calculate_hp
ld l, a
ld a, h
add l
sub d
jr c, .count_prizes
jr z, .count_prizes
or a
ret
; using Super Potion on active card does not prevent a KO.
; if player is at last prize, start loop with active card.
; otherwise start loop at first bench Pokémon.
.count_prizes
call SwapTurn
call CountPrizes
call SwapTurn
dec a
jr z, .start_from_active
ld e, PLAY_AREA_BENCH_1
jr .loop
; find Play Area Pokémon with more than 30 damage.
; skip Pokémon if it doesn't have any energy attached,
; has a BOOST_IF_TAKEN_DAMAGE attack,
; or if discarding makes any attack of its attacks unusable.
.start_from_active
ld e, PLAY_AREA_ARENA
.loop
ld a, DUELVARS_ARENA_CARD
add e
call GetTurnDuelistVariable
cp $ff
ret z
ld d, a
call .check_attached_energy
jr nc, .next
call .check_boost_if_taken_damage
jr c, .next
call .check_energy_cost
jr c, .next
call GetCardDamageAndMaxHP
cp 40 ; if damage >= 40
jr nc, .found
.next
inc e
jr .loop
; a card was found, now to check if it's active or benched.
.found
ld a, e
or a
jr z, .active_card
; bench card
push de
call SwapTurn
call CountPrizes
call SwapTurn
dec a
or a
jr z, .check_random
ld a, 10
call Random
cp 3
; 7/10 chance of returning carry.
.check_random
pop de
jr c, .no_carry
ld a, e
scf
ret
; return carry for active card if not Hgh Recoil.
.active_card
push de
call AICheckIfAttackIsHighRecoil
pop de
jr c, .no_carry
ld a, e
scf
ret
.no_carry
or a
ret
; returns carry if card has energies attached.
.check_attached_energy
call GetPlayAreaCardAttachedEnergies
ld a, [wTotalAttachedEnergies]
or a
ret z
scf
ret
; return carry if either of the attacks are usable
; and have the BOOST_IF_TAKEN_DAMAGE effect.
.check_boost_if_taken_damage
push de
xor a ; FIRST_ATTACK_OR_PKMN_POWER
ld [wSelectedAttack], a
farcall CheckIfSelectedAttackIsUnusable
jr c, .second_attack_1
ld a, ATTACK_FLAG3_ADDRESS | BOOST_IF_TAKEN_DAMAGE_F
call CheckLoadedAttackFlag
jr c, .true_1
.second_attack_1
ld a, SECOND_ATTACK
ld [wSelectedAttack], a
farcall CheckIfSelectedAttackIsUnusable
jr c, .false_1
ld a, ATTACK_FLAG3_ADDRESS | BOOST_IF_TAKEN_DAMAGE_F
call CheckLoadedAttackFlag
jr c, .true_1
.false_1
pop de
or a
ret
.true_1
pop de
scf
ret
; returns carry if discarding energy card renders any attack unusable,
; given that they have enough energy to be used before discarding.
.check_energy_cost
push de
xor a ; FIRST_ATTACK_OR_PKMN_POWER
ld [wSelectedAttack], a
ld a, e
ldh [hTempPlayAreaLocation_ff9d], a
farcall CheckEnergyNeededForAttack
jr c, .second_attack_2
farcall CheckEnergyNeededForAttackAfterDiscard
jr c, .true_2
.second_attack_2
pop de
push de
ld a, SECOND_ATTACK
ld [wSelectedAttack], a
ld a, e
ldh [hTempPlayAreaLocation_ff9d], a
farcall CheckEnergyNeededForAttack
jr c, .false_2
farcall CheckEnergyNeededForAttackAfterDiscard
jr c, .true_2
.false_2
pop de
or a
ret
.true_2
pop de
scf
ret
; AI always attaches a Defender card to the Active Pokémon.
AIPlay_Defender:
ld a, [wAITrainerCardToPlay]
ldh [hTempCardIndex_ff9f], a
xor a ; PLAY_AREA_ARENA
ldh [hTemp_ffa0], a
ld a, OPPACTION_EXECUTE_TRAINER_EFFECTS
bank1call AIMakeDecision
ret
; returns carry if using Defender can prevent a KO
; by the defending Pokémon.
; this takes into account both attacks and whether they're useable.
AIDecide_Defender1:
xor a ; PLAY_AREA_ARENA
ldh [hTempPlayAreaLocation_ff9d], a
farcall CheckIfAnyAttackKnocksOutDefendingCard
jr nc, .cannot_ko
farcall CheckIfSelectedAttackIsUnusable
jr nc, .no_carry
farcall LookForEnergyNeededForAttackInHand
jr c, .no_carry
.cannot_ko
; check if any of the defending Pokémon's attacks deal
; damage exactly equal to current HP, and if so,
; only continue if that attack is useable.
farcall CheckIfAnyDefendingPokemonAttackDealsSameDamageAsHP
jr nc, .no_carry
call SwapTurn
farcall CheckIfSelectedAttackIsUnusable
call SwapTurn
jr c, .no_carry
ld a, [wSelectedAttack]
farcall EstimateDamage_FromDefendingPokemon
ld a, [wDamage]
ld [wce06], a
ld d, a
; load in a the attack that was not selected,
; and check if it is useable.
ld a, [wSelectedAttack]
ld b, a
ld a, SECOND_ATTACK
sub b
ld [wSelectedAttack], a
push de
call SwapTurn
farcall CheckIfSelectedAttackIsUnusable
call SwapTurn
pop de
jr c, .switch_back
; the other attack is useable.
; compare its damage to the selected attack.
ld a, [wSelectedAttack]
push de
farcall EstimateDamage_FromDefendingPokemon
pop de
ld a, [wDamage]
cp d
jr nc, .subtract
; in case the non-selected attack is useable
; and deals less damage than the selected attack,
; switch back to the other attack.
.switch_back
ld a, [wSelectedAttack]
ld b, a
ld a, SECOND_ATTACK
sub b
ld [wSelectedAttack], a
ld a, [wce06]
ld [wDamage], a
; now the selected attack is the one that deals
; the most damage of the two (and is useable).
; if subtracting damage by using Defender
; still prevents a KO, return carry.
.subtract
ld a, [wDamage]
sub 20
ld d, a
ld a, DUELVARS_ARENA_CARD_HP
call GetTurnDuelistVariable
sub d
jr c, .no_carry
jr z, .no_carry
scf
ret
.no_carry
or a
ret
; return carry if using Defender prevents Pokémon
; from being knocked out by an attack with recoil.
AIDecide_Defender2:
ld a, ATTACK_FLAG1_ADDRESS | HIGH_RECOIL_F
call CheckLoadedAttackFlag
jr c, .recoil
ld a, ATTACK_FLAG1_ADDRESS | LOW_RECOIL_F
call CheckLoadedAttackFlag
jr c, .recoil
or a
ret
.recoil
ld a, DUELVARS_ARENA_CARD
call GetTurnDuelistVariable
call LoadCardDataToBuffer2_FromDeckIndex
ld a, [wSelectedAttack]
or a
jr nz, .second_attack
; first attack
ld a, [wLoadedCard2Atk1EffectParam]
jr .check_weak
.second_attack
ld a, [wLoadedCard2Atk2EffectParam]
; double recoil damage if card is weak to its own color.
.check_weak
ld d, a
push de
call GetArenaCardColor
call TranslateColorToWR
ld b, a
call GetArenaCardWeakness
and b
pop de
jr z, .check_resist
sla d
; subtract 30 from recoil damage if card resists its own color.
; if this yields a negative number, return no carry.
.check_resist
push de
call GetArenaCardColor
call TranslateColorToWR
ld b, a
call GetArenaCardResistance
and b
pop de
jr z, .subtract
ld a, d
sub 30
jr c, .no_carry
ld d, a
; subtract damage prevented by Defender.
; if damage still knocks out card, return no carry.
; if damage does not knock out, return carry.
.subtract
ld a, d
or a
jr z, .no_carry
sub 20
ld d, a
ld a, DUELVARS_ARENA_CARD_HP
call GetTurnDuelistVariable
sub d
jr c, .no_carry
jr z, .no_carry
scf
ret
.no_carry
or a
ret
AIPlay_Pluspower:
ld a, [wCurrentAIFlags]
or AI_FLAG_USED_PLUSPOWER
ld [wCurrentAIFlags], a
ld a, [wAITrainerCardParameter]
ld [wAIPluspowerAttack], a
ld a, [wAITrainerCardToPlay]
ldh [hTempCardIndex_ff9f], a
ld a, OPPACTION_EXECUTE_TRAINER_EFFECTS
bank1call AIMakeDecision
ret
; returns carry if using a Pluspower can KO defending Pokémon
; if active card cannot KO without the boost.
; outputs in a the attack to use.
AIDecide_Pluspower1:
; this is mistakenly duplicated
xor a ; PLAY_AREA_ARENA
ldh [hTempPlayAreaLocation_ff9d], a
xor a ; PLAY_AREA_ARENA
ldh [hTempPlayAreaLocation_ff9d], a
; continue if no attack can knock out.
; if there's an attack that can, only continue
; if it's unusable and there's no card in hand
; to fulfill its energy cost.
farcall CheckIfAnyAttackKnocksOutDefendingCard
jr nc, .cannot_ko
farcall CheckIfSelectedAttackIsUnusable
jr nc, .no_carry
farcall LookForEnergyNeededForAttackInHand
jr c, .no_carry
; cannot use an attack that knocks out.
.cannot_ko
; get active Pokémon's info.
ld a, DUELVARS_ARENA_CARD
call GetTurnDuelistVariable
call GetCardIDFromDeckIndex
ld a, e
ld [wTempTurnDuelistCardID], a
; get defending Pokémon's info and check
; its No Damage or Effect substatus.
; if substatus is active, return.
call SwapTurn
ld a, DUELVARS_ARENA_CARD
call GetTurnDuelistVariable
call GetCardIDFromDeckIndex
ld a, e
ld [wTempNonTurnDuelistCardID], a
bank1call HandleNoDamageOrEffectSubstatus
call SwapTurn
jr c, .no_carry
; check both attacks and decide which one
; can KO with Pluspower boost.
; if neither can KO, return no carry.
xor a ; FIRST_ATTACK_OR_PKMN_POWER
ld [wSelectedAttack], a
call .check_ko_with_pluspower
jr c, .kos_with_pluspower_1
ld a, SECOND_ATTACK
ld [wSelectedAttack], a
call .check_ko_with_pluspower
jr c, .kos_with_pluspower_2
.no_carry
or a
ret
; first attack can KO with Pluspower.
.kos_with_pluspower_1
call .check_mr_mime
jr nc, .no_carry
xor a ; FIRST_ATTACK_OR_PKMN_POWER
scf
ret
; second attack can KO with Pluspower.
.kos_with_pluspower_2
call .check_mr_mime
jr nc, .no_carry
ld a, SECOND_ATTACK
scf
ret
; return carry if attack is useable and KOs
; defending Pokémon with Pluspower boost.
.check_ko_with_pluspower
farcall CheckIfSelectedAttackIsUnusable
jr c, .unusable
ld a, [wSelectedAttack]
farcall EstimateDamage_VersusDefendingCard
ld a, DUELVARS_ARENA_CARD_HP
call GetNonTurnDuelistVariable
ld b, a
ld hl, wDamage
sub [hl]
jr c, .no_carry
jr z, .no_carry
ld a, [hl]
add 10 ; add Pluspower boost
ld c, a
ld a, b
sub c
ret c ; return carry if damage > HP left
ret nz ; does not KO
scf
ret ; KOs with Pluspower boost
.unusable
or a
ret
; returns carry if Pluspower boost does
; not exceed 30 damage when facing Mr. Mime.
.check_mr_mime
ld a, [wDamage]
add 10 ; add Pluspower boost
cp 30 ; no danger in preventing damage
ret c
call SwapTurn
ld a, DUELVARS_ARENA_CARD
call GetTurnDuelistVariable
call GetCardIDFromDeckIndex
call SwapTurn
ld a, e
cp MR_MIME
ret z
; damage is >= 30 but not Mr. Mime
scf
ret
; returns carry 7/10 of the time
; if selected attack is useable, can't KO without Pluspower boost
; can damage Mr. Mime even with Pluspower boost
; and has a minimum damage > 0.
; outputs in a the attack to use.
AIDecide_Pluspower2:
xor a ; PLAY_AREA_ARENA
ldh [hTempPlayAreaLocation_ff9d], a
call .check_can_ko
jr nc, .no_carry
call .check_random
jr nc, .no_carry
call .check_mr_mime
jr nc, .no_carry
scf
ret
.no_carry
or a
ret
; returns carry if Pluspower boost does
; not exceed 30 damage when facing Mr. Mime.
.check_mr_mime
ld a, [wDamage]
add 10 ; add Pluspower boost
cp 30 ; no danger in preventing damage
ret c
call SwapTurn
ld a, DUELVARS_ARENA_CARD
call GetTurnDuelistVariable
call GetCardIDFromDeckIndex
call SwapTurn
ld a, e
cp MR_MIME
ret z
; damage is >= 30 but not Mr. Mime
scf
ret
; return carry if attack is useable but cannot KO.
.check_can_ko
farcall CheckIfSelectedAttackIsUnusable
jr c, .unusable
ld a, [wSelectedAttack]
farcall EstimateDamage_VersusDefendingCard
ld a, DUELVARS_ARENA_CARD_HP
call GetNonTurnDuelistVariable
ld b, a
ld hl, wDamage
sub [hl]
jr c, .no_carry
jr z, .no_carry
; can't KO.
scf
ret
.unusable
or a
ret
; return carry 7/10 of the time if
; attack is useable and minimum damage > 0.
.check_random
farcall CheckIfSelectedAttackIsUnusable
jr c, .unusable
ld a, [wSelectedAttack]
farcall EstimateDamage_VersusDefendingCard
ld a, [wAIMinDamage]
cp 10
jr c, .unusable
ld a, 10
call Random
cp 3
ret
AIPlay_Switch:
ld a, [wCurrentAIFlags]
or AI_FLAG_USED_SWITCH
ld [wCurrentAIFlags], a
ld a, [wAITrainerCardToPlay]
ldh [hTempCardIndex_ff9f], a
ld a, [wAITrainerCardParameter]
ldh [hTemp_ffa0], a
ld a, OPPACTION_EXECUTE_TRAINER_EFFECTS
bank1call AIMakeDecision
xor a
ld [wAIRetreatScore], a
ret
; returns carry if the active card has less energy cards
; than the retreat cost and if AI can't play an energy
; card from the hand to fulfill the cost
AIDecide_Switch:
; check if AI can already play an energy card from hand to retreat
ld a, [wAIPlayEnergyCardForRetreat]
or a
jr z, .check_cost_amount
; can't play energy card from hand to retreat
; compare number of energy cards attached to retreat cost
xor a ; PLAY_AREA_ARENA
ldh [hTempPlayAreaLocation_ff9d], a
call GetPlayAreaCardRetreatCost
push af
ld e, PLAY_AREA_ARENA
farcall CountNumberOfEnergyCardsAttached
ld b, a
pop af
sub b
; jump if cards attached > retreat cost
jr c, .check_cost_amount
cp 2