-
Notifications
You must be signed in to change notification settings - Fork 93
/
Copy pathenergy.asm
1047 lines (946 loc) · 23.3 KB
/
energy.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
; processes AI energy card playing logic
; with AI_ENERGY_FLAG_DONT_PLAY flag on
; unreferenced
Func_16488:
ld a, AI_ENERGY_FLAG_DONT_PLAY
ld [wAIEnergyAttachLogicFlags], a
ld de, wTempPlayAreaAIScore
ld hl, wPlayAreaAIScore
ld b, MAX_PLAY_AREA_POKEMON
.loop
ld a, [hli]
ld [de], a
inc de
dec b
jr nz, .loop
ld a, [wAIScore]
ld [de], a
jr AIProcessAndTryToPlayEnergy.has_logic_flags
; have AI choose an energy card to play, but do not play it.
; does not consider whether the cards have evolutions to be played.
; return carry if an energy card is chosen to use in any Play Area card,
; and if so, return its Play Area location in hTempPlayAreaLocation_ff9d.
AIProcessButDontPlayEnergy_SkipEvolution:
ld a, AI_ENERGY_FLAG_DONT_PLAY | AI_ENERGY_FLAG_SKIP_EVOLUTION
ld [wAIEnergyAttachLogicFlags], a
; backup wPlayAreaAIScore in wTempPlayAreaAIScore.
ld de, wTempPlayAreaAIScore
ld hl, wPlayAreaAIScore
ld b, MAX_PLAY_AREA_POKEMON
.loop
ld a, [hli]
ld [de], a
inc de
dec b
jr nz, .loop
ld a, [wAIScore]
ld [de], a
jr AIProcessEnergyCards
; have AI choose an energy card to play, but do not play it.
; does not consider whether the cards have evolutions to be played.
; return carry if an energy card is chosen to use in any Bench card,
; and if so, return its Play Area location in hTempPlayAreaLocation_ff9d.
AIProcessButDontPlayEnergy_SkipEvolutionAndArena:
ld a, AI_ENERGY_FLAG_DONT_PLAY | AI_ENERGY_FLAG_SKIP_EVOLUTION | AI_ENERGY_FLAG_SKIP_ARENA_CARD
ld [wAIEnergyAttachLogicFlags], a
; backup wPlayAreaAIScore in wTempPlayAreaAIScore.
ld de, wTempPlayAreaAIScore
ld hl, wPlayAreaAIScore
ld b, MAX_PLAY_AREA_POKEMON
.loop
ld a, [hli]
ld [de], a
inc de
dec b
jr nz, .loop
ld a, [wAIScore]
ld [de], a
jr AIProcessEnergyCards
; copies wTempPlayAreaAIScore to wPlayAreaAIScore
; and loads wAIScore with value in wTempAIScore.
; identical to RetrievePlayAreaAIScoreFromBackup2.
RetrievePlayAreaAIScoreFromBackup1:
push af
ld de, wPlayAreaAIScore
ld hl, wTempPlayAreaAIScore
ld b, MAX_PLAY_AREA_POKEMON
.loop
ld a, [hli]
ld [de], a
inc de
dec b
jr nz, .loop
ld a, [hl]
ld [wAIScore], a
pop af
ret
; have AI decide whether to play energy card from hand
; and determine which card is best to attach it.
AIProcessAndTryToPlayEnergy:
xor a
ld [wAIEnergyAttachLogicFlags], a
.has_logic_flags
call CreateEnergyCardListFromHand
jr nc, AIProcessEnergyCards
; no energy
ld a, [wAIEnergyAttachLogicFlags]
or a
jr z, .exit
jp RetrievePlayAreaAIScoreFromBackup1
.exit
or a
ret
; have AI decide whether to play energy card
; and determine which card is best to attach it.
AIProcessEnergyCards:
; initialize Play Area AI score
ld a, $80
ld b, MAX_PLAY_AREA_POKEMON
ld hl, wPlayAreaEnergyAIScore
.loop
ld [hli], a
dec b
jr nz, .loop
; Legendary Articuno Deck has its own energy card logic
call HandleLegendaryArticunoEnergyScoring
; start the main Play Area loop
ld b, PLAY_AREA_ARENA
ld a, DUELVARS_NUMBER_OF_POKEMON_IN_PLAY_AREA
call GetTurnDuelistVariable
ld c, a
.loop_play_area
push bc
ld a, b
ldh [hTempPlayAreaLocation_ff9d], a
ld a, $80
ld [wAIScore], a
ld a, $ff
ld [wTempAI], a
ld a, [wAIEnergyAttachLogicFlags]
and AI_ENERGY_FLAG_SKIP_EVOLUTION
jr nz, .check_venusaur
; check if energy needed is found in hand
; and if there's an evolution in hand or deck
; and if so, add to AI score
call CreateHandCardList
ldh a, [hTempPlayAreaLocation_ff9d]
add DUELVARS_ARENA_CARD
call GetTurnDuelistVariable
ld [wCurCardCanAttack], a
call GetAttacksEnergyCostBits
ld hl, wDuelTempList
call CheckEnergyFlagsNeededInList
jp nc, .store_score
ld a, [wCurCardCanAttack]
call CheckForEvolutionInList
jr nc, .no_evolution_in_hand
ld [wTempAI], a ; store evolution card found
ld a, 2
call AIEncourage
jr .check_venusaur
.no_evolution_in_hand
ld a, [wCurCardCanAttack]
call CheckForEvolutionInDeck
jr nc, .check_venusaur
ld a, 1
call AIEncourage
; if there's no Muk in any Play Area
; and there's VenusaurLv67 in own Play Area,
; add to AI score
.check_venusaur
ld a, MUK
call CountPokemonWithActivePkmnPowerInBothPlayAreas
jr c, .check_if_active
ld a, VENUSAUR_LV67
call CountTurnDuelistPokemonWithActivePkmnPower
jr nc, .check_if_active
ld a, 1
call AIEncourage
.check_if_active
ldh a, [hTempPlayAreaLocation_ff9d]
or a
jr nz, .bench
; arena
ld a, [wAIBarrierFlagCounter]
bit AI_MEWTWO_MILL_F, a
jr z, .add_to_score
; subtract from score instead
; if Player is running MewtwoLv53 mill deck.
ld a, 5
call AIDiscourage
jr .check_defending_can_ko
.add_to_score
ld a, 4
call AIEncourage
; lower AI score if poison/double poison
; will KO Pokémon between turns
; or if the defending Pokémon can KO
ld a, DUELVARS_ARENA_CARD_HP
call GetTurnDuelistVariable
call ConvertHPToDamageCounters_Bank5
cp 3
jr nc, .check_defending_can_ko
; hp < 30
cp 2
jr z, .has_20_hp
; hp = 10
ld a, DUELVARS_ARENA_CARD_STATUS
call GetTurnDuelistVariable
and POISONED
jr z, .check_defending_can_ko
jr .poison_will_ko
.has_20_hp
ld a, DUELVARS_ARENA_CARD_STATUS
call GetTurnDuelistVariable
and DOUBLE_POISONED
jr z, .check_defending_can_ko
.poison_will_ko
ld a, 10
call AIDiscourage
jr .check_bench
.check_defending_can_ko
call CheckIfDefendingPokemonCanKnockOut
jr nc, .ai_score_bonus
ld a, 10
call AIDiscourage
; if either poison will KO or defending Pokémon can KO,
; check if there are bench Pokémon,
; if there are not, add AI score
.check_bench
ld a, DUELVARS_NUMBER_OF_POKEMON_IN_PLAY_AREA
call GetTurnDuelistVariable
dec a
jr nz, .ai_score_bonus
ld a, 6
call AIEncourage
jr .ai_score_bonus
; lower AI score by 3 - (bench HP)/10
; if bench HP < 30
.bench
add DUELVARS_ARENA_CARD_HP
call GetTurnDuelistVariable
call ConvertHPToDamageCounters_Bank5
cp 3
jr nc, .ai_score_bonus
; hp < 30
ld b, a
ld a, 3
sub b
call AIDiscourage
; check list in wAICardListEnergyBonus
.ai_score_bonus
ld a, [wAICardListEnergyBonus + 1]
or a
jr z, .check_boss_deck ; is null
ld h, a
ld a, [wAICardListEnergyBonus]
ld l, a
push hl
ldh a, [hTempPlayAreaLocation_ff9d]
add DUELVARS_ARENA_CARD
call GetTurnDuelistVariable
call GetCardIDFromDeckIndex
pop hl
.loop_id_list
ld a, [hli]
or a
jr z, .check_boss_deck
cp e
jr nz, .next_id
; number of attached energy cards
ld a, [hli]
ld d, a
push de
ldh a, [hTempPlayAreaLocation_ff9d]
ld e, a
call GetPlayAreaCardAttachedEnergies
ld a, [wTotalAttachedEnergies]
pop de
cp d
jr c, .check_id_score
; already reached target number of energy cards
ld a, 10
call AIDiscourage
jr .store_score ; bug, should be jr .check_boss_deck
.check_id_score
ld a, [hli]
cp $80
jr c, .decrease_score_1
sub $80
call AIEncourage
jr .check_boss_deck
.decrease_score_1
ld d, a
ld a, $80
sub d
call AIDiscourage
jr .check_boss_deck
.next_id
inc hl
inc hl
jr .loop_id_list
; if it's a boss deck, call HandleAIEnergyScoringForRepeatedBenchPokemon
; and apply to the AI score the values
; determined for this card
.check_boss_deck
call CheckIfNotABossDeckID
jr c, .skip_boss_deck
call HandleAIEnergyScoringForRepeatedBenchPokemon
; applies wPlayAreaEnergyAIScore
ldh a, [hTempPlayAreaLocation_ff9d]
ld c, a
ld b, $00
ld hl, wPlayAreaEnergyAIScore
add hl, bc
ld a, [hl]
cp $80
jr c, .decrease_score_2
sub $80
call AIEncourage
jr .skip_boss_deck
.decrease_score_2
ld b, a
ld a, $80
sub b
call AIDiscourage
.skip_boss_deck
ld a, 1
call AIEncourage
; add AI score for both attacks,
; according to their energy requirements.
xor a ; FIRST_ATTACK_OR_PKMN_POWER
call DetermineAIScoreOfAttackEnergyRequirement
ld a, SECOND_ATTACK
call DetermineAIScoreOfAttackEnergyRequirement
; store bench score for this card.
.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 b
dec c
jp nz, .loop_play_area
; the Play Area loop is over and the score
; for each card has been calculated.
; now to determine the highest score.
call FindPlayAreaCardWithHighestAIScore
jp nc, .not_found
ld a, [wAIEnergyAttachLogicFlags]
or a
jr z, .play_card
scf
jp RetrievePlayAreaAIScoreFromBackup1
.play_card
call CreateEnergyCardListFromHand
jp AITryToPlayEnergyCard
.not_found:
ld a, [wAIEnergyAttachLogicFlags]
or a
jr z, .no_carry
jp RetrievePlayAreaAIScoreFromBackup1
.no_carry
or a
ret
; checks score related to selected attack,
; in order to determine whether to play energy card.
; the AI score is increased/decreased accordingly.
; input:
; [wSelectedAttack] = attack to check.
DetermineAIScoreOfAttackEnergyRequirement:
ld [wSelectedAttack], a
call CheckEnergyNeededForAttack
jp c, .not_enough_energy
ld a, ATTACK_FLAG2_ADDRESS | ATTACHED_ENERGY_BOOST_F
call CheckLoadedAttackFlag
jr c, .attached_energy_boost
ld a, ATTACK_FLAG2_ADDRESS | DISCARD_ENERGY_F
call CheckLoadedAttackFlag
jr c, .discard_energy
jp .check_evolution
.attached_energy_boost
ld a, [wLoadedAttackEffectParam]
cp MAX_ENERGY_BOOST_IS_LIMITED
jr z, .check_surplus_energy
; is MAX_ENERGY_BOOST_IS_NOT_LIMITED,
; which is equal to 3, add to score.
call AIEncourage
jp .check_evolution
.check_surplus_energy
call CheckIfNoSurplusEnergyForAttack
jr c, .asm_166cd
cp 3 ; check how much surplus energy
jr c, .asm_166cd
.asm_166c5
ld a, 5
call AIDiscourage
jp .check_evolution
.asm_166cd
ld a, 2
call AIEncourage
; check whether attack has ATTACHED_ENERGY_BOOST flag
; and add to AI score if attaching another energy
; will KO defending Pokémon.
; add more to score if this is currently active Pokémon.
ld a, ATTACK_FLAG2_ADDRESS | ATTACHED_ENERGY_BOOST_F
call CheckLoadedAttackFlag
jp nc, .check_evolution
ld a, [wSelectedAttack]
call EstimateDamage_VersusDefendingCard
ld a, DUELVARS_ARENA_CARD_HP
call GetNonTurnDuelistVariable
ld hl, wDamage
sub [hl]
jp c, .check_evolution
jp z, .check_evolution
ld a, [wDamage]
add 10 ; boost gained by attaching another energy card
ld b, a
ld a, DUELVARS_ARENA_CARD_HP
call GetNonTurnDuelistVariable
sub b
jr c, .attaching_kos_player
jr nz, .check_evolution
.attaching_kos_player
ld a, 20
call AIEncourage
ldh a, [hTempPlayAreaLocation_ff9d]
or a
jr nz, .check_evolution
ld a, 10
call AIEncourage
jr .check_evolution
; checks if there is surplus energy for attack
; that discards attached energy card.
; if current card is ZapdosLv64, don't add to score.
; if there is no surplus energy, encourage playing energy.
.discard_energy
ld a, [wLoadedCard1ID]
cp ZAPDOS_LV64
jr z, .check_evolution
call CheckIfNoSurplusEnergyForAttack
jr c, .asm_166cd
jr .asm_166c5
.not_enough_energy
ld a, ATTACK_FLAG2_ADDRESS | FLAG_2_BIT_5_F
call CheckLoadedAttackFlag
jr nc, .check_color_needed
ld a, 5
call AIDiscourage
; if the energy card color needed is in hand, increase AI score.
; if a colorless card is needed, increase AI score.
.check_color_needed
ld a, b
or a
jr z, .check_colorless_needed
ld a, e
call LookForCardIDInHand
jr c, .check_colorless_needed
ld a, 4
call AIEncourage
jr .check_total_needed
.check_colorless_needed
ld a, c
or a
jr z, .check_evolution
ld a, 3
call AIEncourage
; if only one energy card is needed for attack,
; encourage playing energy card.
.check_total_needed
ld a, b
add c
dec a
jr nz, .check_evolution
ld a, 3
call AIEncourage
; if the attack KOs player and this is the active card, add to AI score.
ldh a, [hTempPlayAreaLocation_ff9d]
or a
jr nz, .check_evolution
ld a, [wSelectedAttack]
call EstimateDamage_VersusDefendingCard
ld a, DUELVARS_ARENA_CARD_HP
call GetNonTurnDuelistVariable
ld hl, wDamage
sub [hl]
jr z, .atk_kos_defending
jr nc, .check_evolution
.atk_kos_defending
ld a, 20
call AIEncourage
; this is possibly a bug.
; this is an identical check as above to test whether this card is active.
; in case it is active, the score gets added 10 more points,
ldh a, [hTempPlayAreaLocation_ff9d]
or a
jr nz, .check_evolution
ld a, 10
call AIEncourage
.check_evolution
ld a, [wTempAI] ; evolution in hand
cp $ff
ret z
; temporarily replace this card with evolution in hand.
ld b, a
ldh a, [hTempPlayAreaLocation_ff9d]
add DUELVARS_ARENA_CARD
call GetTurnDuelistVariable
push af
ld [hl], b
; check for energy still needed for evolution to attack.
; if FLAG_2_BIT_5 is not set, check what color is needed.
; if the energy card color needed is in hand, increase AI score.
; if a colorless card is needed, increase AI score.
call CheckEnergyNeededForAttack
jr nc, .done
ld a, ATTACK_FLAG2_ADDRESS | FLAG_2_BIT_5_F
call CheckLoadedAttackFlag
jr c, .done
ld a, b
or a
jr z, .check_colorless_needed_evo
ld a, e
call LookForCardIDInHand
jr c, .check_colorless_needed_evo
ld a, 2
call AIEncourage
jr .done
.check_colorless_needed_evo
ld a, c
or a
jr z, .done
ld a, 1
call AIEncourage
; recover the original card in the Play Area location.
.done
ldh a, [hTempPlayAreaLocation_ff9d]
add DUELVARS_ARENA_CARD
call GetTurnDuelistVariable
pop af
ld [hl], a
ret
; returns in hTempPlayAreaLocation_ff9d the Play Area location
; of the card with the highest Play Area AI score, unless
; the highest score is below $85.
; if it succeeds in return a card location, set carry.
; if AI_ENERGY_FLAG_SKIP_ARENA_CARD is set in wAIEnergyAttachLogicFlags
; doesn't include the Arena card and there's no minimum score.
FindPlayAreaCardWithHighestAIScore:
ld a, [wAIEnergyAttachLogicFlags]
and AI_ENERGY_FLAG_SKIP_ARENA_CARD
jr nz, .only_bench
ld a, DUELVARS_NUMBER_OF_POKEMON_IN_PLAY_AREA
call GetTurnDuelistVariable
ld b, a
ld c, PLAY_AREA_ARENA
ld e, c
ld d, c
ld hl, wPlayAreaAIScore
; find highest Play Area AI score.
.loop_1
ld a, [hli]
cp e
jr c, .next_1
jr z, .next_1
ld e, a ; overwrite highest score found
ld d, c ; overwrite Play Area of highest score
.next_1
inc c
dec b
jr nz, .loop_1
; if highest AI score is below $85, return no carry.
; else, store Play Area location and return carry.
ld a, e
cp $85
jr c, .not_enough_score
ld a, d
ldh [hTempPlayAreaLocation_ff9d], a
scf
ret
.not_enough_score
or a
ret
; same as above but only check bench Pokémon scores.
.only_bench
ld a, DUELVARS_NUMBER_OF_POKEMON_IN_PLAY_AREA
call GetTurnDuelistVariable
dec a
jr z, .no_carry
ld b, a
ld e, 0
ld c, PLAY_AREA_BENCH_1
ld d, c
ld hl, wPlayAreaAIScore + 1
.loop_2
ld a, [hli]
cp e
jr c, .next_2
jr z, .next_2
ld e, a ; overwrite highest score found
ld d, c ; overwrite Play Area of highest score
.next_2
inc c
dec b
jr nz, .loop_2
; in this case, there is no minimum threshold AI score.
ld a, d
ldh [hTempPlayAreaLocation_ff9d], a
scf
ret
.no_carry
or a
ret
; returns carry if there's an evolution card
; that can evolve card in hTempPlayAreaLocation_ff9d,
; and that card needs energy to use wSelectedAttack.
CheckIfEvolutionNeedsEnergyForAttack:
call CreateHandCardList
ldh a, [hTempPlayAreaLocation_ff9d]
add DUELVARS_ARENA_CARD
call GetTurnDuelistVariable
call CheckCardEvolutionInHandOrDeck
jr c, .has_evolution
or a
ret
.has_evolution
ld b, a
ldh a, [hTempPlayAreaLocation_ff9d]
add DUELVARS_ARENA_CARD
call GetTurnDuelistVariable
push af
ld [hl], b
call CheckEnergyNeededForAttack
jr c, .not_enough_energy
ldh a, [hTempPlayAreaLocation_ff9d]
add DUELVARS_ARENA_CARD
call GetTurnDuelistVariable
pop af
ld [hl], a
or a
ret
.not_enough_energy
ldh a, [hTempPlayAreaLocation_ff9d]
add DUELVARS_ARENA_CARD
call GetTurnDuelistVariable
pop af
ld [hl], a
scf
ret
; returns in e the card ID of the energy required for
; the Discard/Energy Boost attack loaded in wSelectedAttack.
; if it's ZapdosLv64's Thunderbolt attack, return no carry.
; if it's Charizard's Fire Spin or Exeggutor's Big Eggsplosion
; attack, don't return energy card ID, but set carry.
; output:
; b = TRUE if needs color energy;
; c = TRUE if only needs colorless energy;
; carry set if not ZapdosLv64's Thunderbolt attack.
GetEnergyCardForDiscardOrEnergyBoostAttack:
; load card ID and check selected attack index.
ldh a, [hTempPlayAreaLocation_ff9d]
add DUELVARS_ARENA_CARD
call GetTurnDuelistVariable
call LoadCardDataToBuffer2_FromDeckIndex
ld b, a
ld a, [wSelectedAttack]
or a
jr z, .first_attack
; check if second attack is ZapdosLv64's Thunderbolt,
; Charizard's Fire Spin or Exeggutor's Big Eggsplosion,
; for these to be treated differently.
; for both attacks, load its energy cost.
ld a, b
cp ZAPDOS_LV64
jr z, .zapdos
cp CHARIZARD
jr z, .charizard_or_exeggutor
cp EXEGGUTOR
jr z, .charizard_or_exeggutor
ld hl, wLoadedCard2Atk2EnergyCost
jr .fire
.first_attack
ld hl, wLoadedCard2Atk1EnergyCost
; check which energy color the attack requires,
; and load in e the card ID of corresponding energy card,
; then return carry flag set.
.fire
ld a, [hli]
ld b, a
and $f0
jr z, .grass
ld e, FIRE_ENERGY
jr .set_carry
.grass
ld a, b
and $0f
jr z, .lightning
ld e, GRASS_ENERGY
jr .set_carry
.lightning
ld a, [hli]
ld b, a
and $f0
jr z, .water
ld e, LIGHTNING_ENERGY
jr .set_carry
.water
ld a, b
and $0f
jr z, .fighting
ld e, WATER_ENERGY
jr .set_carry
.fighting
ld a, [hli]
ld b, a
and $f0
jr z, .psychic
ld e, FIGHTING_ENERGY
jr .set_carry
.psychic
ld e, PSYCHIC_ENERGY
.set_carry
lb bc, TRUE, FALSE
scf
ret
; for ZapdosLv64's Thunderbolt attack, return with no carry.
.zapdos
or a
ret
; Charizard's Fire Spin and Exeggutor's Big Eggsplosion,
; return carry.
.charizard_or_exeggutor
lb bc, FALSE, TRUE
scf
ret
; called after the AI has decided which card to attach
; energy from hand. AI does checks to determine whether
; this card needs more energy or not, and chooses the
; right energy card to play. If the card is played,
; return with carry flag set.
AITryToPlayEnergyCard:
; check if energy cards are still needed for attacks.
; if first attack doesn't need, test for the second attack.
xor a
ld [wTempAI], a
ld [wSelectedAttack], a ; FIRST_ATTACK_OR_PKMN_POWER
call CheckEnergyNeededForAttack
jr nc, .second_attack
ld a, b
or a
jr nz, .check_deck
ld a, c
or a
jr nz, .check_deck
.second_attack
ld a, SECOND_ATTACK
ld [wSelectedAttack], a
call CheckEnergyNeededForAttack
jr nc, .check_discard_or_energy_boost
ld a, b
or a
jr nz, .check_deck
ld a, c
or a
jr nz, .check_deck
; neither attack needs energy cards to be used.
; check whether these attacks can be given
; extra energy cards for their effects.
.check_discard_or_energy_boost
ld a, $01
ld [wTempAI], a
; for both attacks, check if it has the effect of
; discarding energy cards or attached energy boost.
xor a ; FIRST_ATTACK_OR_PKMN_POWER
ld [wSelectedAttack], a
call CheckEnergyNeededForAttack
ld a, ATTACK_FLAG2_ADDRESS | ATTACHED_ENERGY_BOOST_F
call CheckLoadedAttackFlag
jr c, .energy_boost_or_discard_energy
ld a, ATTACK_FLAG2_ADDRESS | DISCARD_ENERGY_F
call CheckLoadedAttackFlag
jr c, .energy_boost_or_discard_energy
ld a, SECOND_ATTACK
ld [wSelectedAttack], a
call CheckEnergyNeededForAttack
ld a, ATTACK_FLAG2_ADDRESS | ATTACHED_ENERGY_BOOST_F
call CheckLoadedAttackFlag
jr c, .energy_boost_or_discard_energy
ld a, ATTACK_FLAG2_ADDRESS | DISCARD_ENERGY_F
call CheckLoadedAttackFlag
jr c, .energy_boost_or_discard_energy
; if none of the attacks have those flags, do an additional
; check to ascertain whether evolution card needs energy
; to use second attack. Return if all these checks fail.
call CheckIfEvolutionNeedsEnergyForAttack
ret nc
call CreateEnergyCardListFromHand
jr .check_deck
; for attacks that discard energy or get boost for
; additional energy cards, get the energy card ID required by attack.
; if it's ZapdosLv64's Thunderbolt attack, return.
.energy_boost_or_discard_energy
call GetEnergyCardForDiscardOrEnergyBoostAttack
ret nc
; some decks allow basic Pokémon to be given double colorless
; in anticipation for evolution, so play card if that is the case.
.check_deck
call CheckSpecificDecksToAttachDoubleColorless
jr c, .play_energy_card
ld a, b
or a
jr z, .colorless_energy
; in this case, Pokémon needs a specific basic energy card.
; look for basic energy card needed in hand and play it.
ld a, e
call LookForCardIDInHand
ldh [hTemp_ffa0], a
jr nc, .play_energy_card
; in this case Pokémon just needs colorless (any basic energy card).
; if active card, check if it needs 2 colorless.
; if it does (and also doesn't additionally need a color energy),
; look for double colorless card in hand and play it if found.
.colorless_energy
ldh a, [hTempPlayAreaLocation_ff9d]
or a
jr nz, .look_for_any_energy
ld a, c
or a
jr z, .check_if_done
cp 2
jr nz, .look_for_any_energy
; needs two colorless
ld hl, wDuelTempList
.loop_1
ld a, [hli]
cp $ff
jr z, .look_for_any_energy
ldh [hTemp_ffa0], a
call GetCardIDFromDeckIndex
ld a, e
cp DOUBLE_COLORLESS_ENERGY
jr nz, .loop_1
jr .play_energy_card
; otherwise, look for any card and play it.
; if it's a boss deck, only play double colorless in this situation.
.look_for_any_energy
ld hl, wDuelTempList
call CountCardsInDuelTempList
call ShuffleCards
.loop_2
ld a, [hli]
cp $ff
jr z, .check_if_done
call CheckIfOpponentHasBossDeckID
jr nc, .load_card
push af
call GetCardIDFromDeckIndex
ld a, e
cp DOUBLE_COLORLESS_ENERGY
pop bc
jr z, .loop_2
ld a, b
.load_card
ldh [hTemp_ffa0], a
; plays energy card loaded in hTemp_ffa0 and sets carry flag.
.play_energy_card
ldh a, [hTempPlayAreaLocation_ff9d]
ldh [hTempPlayAreaLocation_ffa1], a
ld a, OPPACTION_PLAY_ENERGY
bank1call AIMakeDecision
scf
ret
; wTempAI is 1 if the attack had a Discard/Energy Boost effect,
; and 0 otherwise. If 1, then return. If not one, check if
; there is still a second attack to check.
.check_if_done
ld a, [wTempAI]
or a
jr z, .check_first_attack
ret
.check_first_attack
ld a, [wSelectedAttack]
or a
jp z, .second_attack
ret
; check if playing certain decks so that AI can decide whether to play
; double colorless to some specific cards.
; these are cards that do not need double colorless to any of their attacks
; but are required by their evolutions.
; return carry if there's a double colorless in hand to attach
; and it's one of the card IDs from these decks.
; output:
; [hTemp_ffa0] = card index of double colorless in hand;
; carry set if can play energy card.
CheckSpecificDecksToAttachDoubleColorless:
push bc
push de
push hl
; check if AI is playing any of the applicable decks.
ld a, [wOpponentDeckID]
cp LEGENDARY_DRAGONITE_DECK_ID
jr z, .legendary_dragonite_deck
cp FIRE_CHARGE_DECK_ID
jr z, .fire_charge_deck
cp LEGENDARY_RONALD_DECK_ID
jr z, .legendary_ronald_deck
.no_carry
pop hl
pop de
pop bc
or a
ret
; if playing Legendary Dragonite deck,
; check for Charmander and Dratini.
.legendary_dragonite_deck
call .get_id
cp CHARMANDER
jr z, .check_colorless_attached
cp DRATINI
jr z, .check_colorless_attached