-
Notifications
You must be signed in to change notification settings - Fork 93
/
Copy pathcore.asm
2788 lines (2553 loc) · 52.9 KB
/
core.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 "engine/duel/ai/decks/unreferenced.asm"
; returns carry if damage dealt from any of
; a card's attacks KOs defending Pokémon
; outputs index of the attack that KOs
; input:
; [hTempPlayAreaLocation_ff9d] = location of attacking card to consider
; output:
; [wSelectedAttack] = attack index that KOs
CheckIfAnyAttackKnocksOutDefendingCard:
xor a ; FIRST_ATTACK_OR_PKMN_POWER
call .CheckAttack
ret c
ld a, SECOND_ATTACK
.CheckAttack:
call EstimateDamage_VersusDefendingCard
ld a, DUELVARS_ARENA_CARD_HP
call GetNonTurnDuelistVariable
ld hl, wDamage
sub [hl]
ret c
ret nz
scf
ret
; returns carry if any of the defending Pokémon's attacks
; brings card at hTempPlayAreaLocation_ff9d down
; to exactly 0 HP.
; outputs that attack index in wSelectedAttack.
CheckIfAnyDefendingPokemonAttackDealsSameDamageAsHP:
xor a ; FIRST_ATTACK_OR_PKMN_POWER
call .check_damage
ret c
ld a, SECOND_ATTACK
.check_damage
call EstimateDamage_FromDefendingPokemon
ldh a, [hTempPlayAreaLocation_ff9d]
add DUELVARS_ARENA_CARD_HP
call GetTurnDuelistVariable
ld hl, wDamage
sub [hl]
jr z, .true
ret
.true
scf
ret
; checks AI scores for all benched Pokémon
; returns the location of the card with highest score
; in a and [hTempPlayAreaLocation_ff9d]
FindHighestBenchScore:
ld a, DUELVARS_NUMBER_OF_POKEMON_IN_PLAY_AREA
call GetTurnDuelistVariable
ld b, a
ld c, 0
ld e, c
ld d, c
ld hl, wPlayAreaAIScore + 1
jp .next ; can be jr
.loop
ld a, [hli]
cp e
jr c, .next
ld e, a
ld d, c
.next
inc c
dec b
jr nz, .loop
ld a, d
ldh [hTempPlayAreaLocation_ff9d], a
or a
ret
; adds a to wAIScore
; if there's overflow, it's capped at 255
; output:
; a = a + wAIScore (capped at 255)
AIEncourage:
push hl
ld hl, wAIScore
add [hl]
jr nc, .no_cap
ld a, 255
.no_cap
ld [hl], a
pop hl
ret
; subs a from wAIScore
; if there's underflow, it's capped at 0
AIDiscourage:
push hl
push de
ld e, a
ld hl, wAIScore
ld a, [hl]
or a
jr z, .done
sub e
ld [hl], a
jr nc, .done
ld [hl], 0
.done
pop de
pop hl
ret
; loads defending Pokémon's weakness/resistance
; and the number of prize cards in both sides
LoadDefendingPokemonColorWRAndPrizeCards:
call SwapTurn
call GetArenaCardColor
call TranslateColorToWR
ld [wAIPlayerColor], a
call GetArenaCardWeakness
ld [wAIPlayerWeakness], a
call GetArenaCardResistance
ld [wAIPlayerResistance], a
call CountPrizes
ld [wAIPlayerPrizeCount], a
call SwapTurn
call CountPrizes
ld [wAIOpponentPrizeCount], a
ret
; called when AI has chosen its attack.
; executes all effects and damage.
; handles AI choosing parameters for certain attacks as well.
AITryUseAttack:
ld a, [wSelectedAttack]
ldh [hTemp_ffa0], a
ld e, a
ld a, DUELVARS_ARENA_CARD
call GetTurnDuelistVariable
ldh [hTempCardIndex_ff9f], a
ld d, a
call CopyAttackDataAndDamage_FromDeckIndex
ld a, OPPACTION_BEGIN_ATTACK
bank1call AIMakeDecision
ret c
call AISelectSpecialAttackParameters
jr c, .use_attack
ld a, EFFECTCMDTYPE_AI_SELECTION
call TryExecuteEffectCommandFunction
.use_attack
ld a, [wSelectedAttack]
ld e, a
ld a, DUELVARS_ARENA_CARD
call GetTurnDuelistVariable
ld d, a
call CopyAttackDataAndDamage_FromDeckIndex
ld a, OPPACTION_USE_ATTACK
bank1call AIMakeDecision
ret c
ld a, EFFECTCMDTYPE_AI_SWITCH_DEFENDING_PKMN
call TryExecuteEffectCommandFunction
ld a, OPPACTION_ATTACK_ANIM_AND_DAMAGE
bank1call AIMakeDecision
ret
; return carry if any of the following is satisfied:
; - deck index in a corresponds to a double colorless energy card;
; - card type in wTempCardType is colorless;
; - card ID in wTempCardID is a Pokémon card that has
; attacks that require energy other than its color and
; the deck index in a corresponds to that energy type;
; - card ID is Eevee and a corresponds to an energy type
; of water, fire or lightning;
; - type of card in register a is the same as wTempCardType.
; used for knowing if a given energy card can be discarded
; from a given Pokémon card
; input:
; a = energy card attached to Pokémon to check
; [wTempCardType] = TYPE_ENERGY_* of given Pokémon
; [wTempCardID] = card index of Pokémon card to check
CheckIfEnergyIsUseful:
push de
call GetCardIDFromDeckIndex
ld a, e
cp DOUBLE_COLORLESS_ENERGY
jr z, .set_carry
ld a, [wTempCardType]
cp TYPE_ENERGY_DOUBLE_COLORLESS
jr z, .set_carry
ld a, [wTempCardID]
ld d, PSYCHIC_ENERGY
cp EXEGGCUTE
jr z, .check_energy
cp EXEGGUTOR
jr z, .check_energy
cp PSYDUCK
jr z, .check_energy
cp GOLDUCK
jr z, .check_energy
ld d, WATER_ENERGY
cp SURFING_PIKACHU_LV13
jr z, .check_energy
cp SURFING_PIKACHU_ALT_LV13
jr z, .check_energy
cp EEVEE
jr nz, .check_type
ld a, e
cp WATER_ENERGY
jr z, .set_carry
cp FIRE_ENERGY
jr z, .set_carry
cp LIGHTNING_ENERGY
jr z, .set_carry
.check_type
ld d, $00 ; unnecessary?
call GetCardType
ld d, a
ld a, [wTempCardType]
cp d
jr z, .set_carry
pop de
or a
ret
.check_energy
ld a, d
cp e
jr nz, .check_type
.set_carry
pop de
scf
ret
; pick a random Pokemon in the bench.
; output:
; - a = PLAY_AREA_* of Bench Pokemon picked.
PickRandomBenchPokemon:
ld a, DUELVARS_NUMBER_OF_POKEMON_IN_PLAY_AREA
call GetTurnDuelistVariable
dec a
call Random
inc a
ret
AIPickPrizeCards:
ld a, [wNumberPrizeCardsToTake]
ld b, a
.loop
call .PickPrizeCard
ld a, DUELVARS_PRIZES
call GetTurnDuelistVariable
or a
jr z, .done
dec b
jr nz, .loop
.done
ret
; picks a prize card at random
; and adds it to the hand.
.PickPrizeCard:
ld a, DUELVARS_PRIZES
call GetTurnDuelistVariable
push hl
ld c, a
; choose a random prize card until
; one is found that isn't taken already.
.loop_pick_prize
ld a, 6
call Random
ld e, a
ld d, $00
ld hl, .prize_flags
add hl, de
ld a, [hl]
and c
jr z, .loop_pick_prize ; no prize
; prize card was found
; remove this prize from wOpponentPrizes
ld a, [hl]
pop hl
cpl
and [hl]
ld [hl], a
; add this prize card to the hand
ld a, e
add DUELVARS_PRIZE_CARDS
call GetTurnDuelistVariable
call AddCardToHand
ret
.prize_flags
db $1 << 0
db $1 << 1
db $1 << 2
db $1 << 3
db $1 << 4
db $1 << 5
db $1 << 6
db $1 << 7
; routine for AI to play all Basic cards from its hand
; in the beginning of the Duel.
AIPlayInitialBasicCards:
call CreateHandCardList
ld hl, wDuelTempList
.check_for_next_card
ld a, [hli]
ldh [hTempCardIndex_ff98], a
cp $ff
ret z ; return when done
call LoadCardDataToBuffer1_FromDeckIndex
ld a, [wLoadedCard1Type]
cp TYPE_ENERGY
jr nc, .check_for_next_card ; skip if not Pokemon card
ld a, [wLoadedCard1Stage]
or a
jr nz, .check_for_next_card ; skip if not Basic Stage
; play Basic card from hand
push hl
ldh a, [hTempCardIndex_ff98]
call PutHandPokemonCardInPlayArea
pop hl
jr .check_for_next_card
; returns carry if Pokémon at hTempPlayAreaLocation_ff9d
; can't use an attack or if that selected attack doesn't have enough energy
; input:
; [hTempPlayAreaLocation_ff9d] = location of Pokémon card
; [wSelectedAttack] = selected attack to examine
CheckIfSelectedAttackIsUnusable:
ldh a, [hTempPlayAreaLocation_ff9d]
or a
jr nz, .bench
bank1call HandleCantAttackSubstatus
ret c
bank1call CheckIfActiveCardParalyzedOrAsleep
ret c
ld a, DUELVARS_ARENA_CARD
call GetTurnDuelistVariable
ld d, a
ld a, [wSelectedAttack]
ld e, a
call CopyAttackDataAndDamage_FromDeckIndex
call HandleAmnesiaSubstatus
ret c
ld a, EFFECTCMDTYPE_INITIAL_EFFECT_1
call TryExecuteEffectCommandFunction
ret c
.bench
call CheckEnergyNeededForAttack
ret c ; can't be used
ld a, ATTACK_FLAG2_ADDRESS | FLAG_2_BIT_5_F
call CheckLoadedAttackFlag
ret
; load selected attack from Pokémon in hTempPlayAreaLocation_ff9d
; and checks if there is enough energy to execute the selected attack
; input:
; [hTempPlayAreaLocation_ff9d] = location of Pokémon card
; [wSelectedAttack] = selected attack to examine
; output:
; b = basic energy still needed
; c = colorless energy still needed
; e = output of ConvertColorToEnergyCardID, or $0 if not an attack
; carry set if no attack
; OR if it's a Pokémon Power
; OR if not enough energy for attack
CheckEnergyNeededForAttack:
ldh a, [hTempPlayAreaLocation_ff9d]
add DUELVARS_ARENA_CARD
call GetTurnDuelistVariable
ld d, a
ld a, [wSelectedAttack]
ld e, a
call CopyAttackDataAndDamage_FromDeckIndex
ld hl, wLoadedAttackName
ld a, [hli]
or [hl]
jr z, .no_attack
ld a, [wLoadedAttackCategory]
cp POKEMON_POWER
jr nz, .is_attack
.no_attack
lb bc, 0, 0
ld e, c
scf
ret
.is_attack
ldh a, [hTempPlayAreaLocation_ff9d]
ld e, a
call GetPlayAreaCardAttachedEnergies
bank1call HandleEnergyBurn
xor a
ld [wTempLoadedAttackEnergyCost], a
ld [wTempLoadedAttackEnergyNeededAmount], a
ld [wTempLoadedAttackEnergyNeededType], a
ld hl, wAttachedEnergies
ld de, wLoadedAttackEnergyCost
ld b, 0
ld c, (NUM_TYPES / 2) - 1
.loop
; check all basic energy cards except colorless
ld a, [de]
swap a
call CheckIfEnoughParticularAttachedEnergy
ld a, [de]
call CheckIfEnoughParticularAttachedEnergy
inc de
dec c
jr nz, .loop
; running CheckIfEnoughParticularAttachedEnergy back to back like this
; overwrites the results of a previous call of this function,
; however, no attack in the game has energy requirements for two
; different energy types (excluding colorless), so this routine
; will always just return the result for one type of basic energy,
; while all others will necessarily have an energy cost of 0
; if attacks are added to the game with energy requirements of
; two different basic energy types, then this routine only accounts
; for the type with the highest index
; colorless
ld a, [de]
swap a
and %00001111
ld b, a ; colorless energy still needed
ld a, [wTempLoadedAttackEnergyCost]
ld hl, wTempLoadedAttackEnergyNeededAmount
sub [hl]
ld c, a ; basic energy still needed
ld a, [wTotalAttachedEnergies]
sub c
sub b
jr c, .not_enough
ld a, [wTempLoadedAttackEnergyNeededAmount]
or a
ret z
; being here means the energy cost isn't satisfied,
; including with colorless energy
xor a
.not_enough
cpl
inc a
ld c, a ; colorless energy still needed
ld a, [wTempLoadedAttackEnergyNeededAmount]
ld b, a ; basic energy still needed
ld a, [wTempLoadedAttackEnergyNeededType]
call ConvertColorToEnergyCardID
ld e, a
ld d, 0
scf
ret
; takes as input the energy cost of an attack for a
; particular energy, stored in the lower nibble of a
; if the attack costs some amount of this energy, the lower nibble of a != 0,
; and this amount is stored in wTempLoadedAttackEnergyCost
; sets carry flag if not enough energy of this type attached
; input:
; a = this energy cost of attack (lower nibble)
; [hl] = attached energy
; output:
; carry set if not enough of this energy type attached
CheckIfEnoughParticularAttachedEnergy:
and %00001111
jr nz, .check
.has_enough
inc hl
inc b
or a
ret
.check
ld [wTempLoadedAttackEnergyCost], a
sub [hl]
jr z, .has_enough
jr c, .has_enough
; not enough energy
ld [wTempLoadedAttackEnergyNeededAmount], a
ld a, b
ld [wTempLoadedAttackEnergyNeededType], a
inc hl
inc b
scf
ret
; input:
; a = energy type
; output:
; a = energy card ID
ConvertColorToEnergyCardID:
push hl
push de
ld e, a
ld d, 0
ld hl, .card_id
add hl, de
ld a, [hl]
pop de
pop hl
ret
.card_id
db FIRE_ENERGY
db GRASS_ENERGY
db LIGHTNING_ENERGY
db WATER_ENERGY
db FIGHTING_ENERGY
db PSYCHIC_ENERGY
db DOUBLE_COLORLESS_ENERGY
; returns carry if loaded attack effect has
; an "initial effect 2" or "require selection" command type
; unreferenced
Func_14323:
ld hl, wLoadedAttackEffectCommands
ld a, [hli]
ld h, [hl]
ld l, a
ld a, EFFECTCMDTYPE_INITIAL_EFFECT_2
push hl
call CheckMatchingCommand
pop hl
jr nc, .set_carry
ld a, EFFECTCMDTYPE_REQUIRE_SELECTION
call CheckMatchingCommand
jr nc, .set_carry
or a
ret
.set_carry
scf
ret
; return carry depending on card index in a:
; - if energy card, return carry if no energy card has been played yet
; - if basic Pokémon card, return carry if there's space in bench
; - if evolution card, return carry if there's a Pokémon
; in Play Area it can evolve
; - if trainer card, return carry if it can be used
; input:
; a = card index to check
CheckIfCardCanBePlayed:
ldh [hTempCardIndex_ff9f], a
call LoadCardDataToBuffer1_FromDeckIndex
ld a, [wLoadedCard1Type]
cp TYPE_ENERGY
jr c, .pokemon_card
cp TYPE_TRAINER
jr z, .trainer_card
; energy card
ld a, [wAlreadyPlayedEnergy]
or a
ret z
scf
ret
.pokemon_card
ld a, [wLoadedCard1Stage]
or a
jr nz, .evolution_card
ld a, DUELVARS_NUMBER_OF_POKEMON_IN_PLAY_AREA
call GetTurnDuelistVariable
cp MAX_PLAY_AREA_POKEMON
ccf
ret
.evolution_card
bank1call IsPrehistoricPowerActive
ret c
ld a, DUELVARS_NUMBER_OF_POKEMON_IN_PLAY_AREA
call GetTurnDuelistVariable
ld c, a
ld b, 0
.loop
push bc
ld e, b
ldh a, [hTempCardIndex_ff9f]
ld d, a
call CheckIfCanEvolveInto
pop bc
ret nc
inc b
dec c
jr nz, .loop
scf
ret
.trainer_card
bank1call CheckCantUseTrainerDueToEffect
ret c
call LoadNonPokemonCardEffectCommands
ld a, EFFECTCMDTYPE_INITIAL_EFFECT_1
call TryExecuteEffectCommandFunction
ret
; loads all the energy cards
; in hand in wDuelTempList
; return carry if no energy cards found
CreateEnergyCardListFromHand:
push hl
push de
push bc
ld de, wDuelTempList
ld b, a
ld a, DUELVARS_NUMBER_OF_CARDS_IN_HAND
call GetTurnDuelistVariable
ld c, a
inc c
ld l, DUELVARS_HAND
jr .decrease
.loop
ld a, [hli]
push de
call GetCardIDFromDeckIndex
call GetCardType
pop de
and TYPE_ENERGY
jr z, .decrease
dec hl
ld a, [hli]
ld [de], a
inc de
.decrease
dec c
jr nz, .loop
ld a, $ff
ld [de], a
pop bc
pop de
pop hl
ld a, [wDuelTempList]
cp $ff
ccf
ret
; looks for card ID in hand and
; sets carry if a card wasn't found
; as opposed to LookForCardIDInHandList_Bank5
; this function doesn't create a list
; and preserves hl, de and bc
; input:
; a = card ID
; output:
; a = card deck index, if found
; carry set if NOT found
LookForCardIDInHand:
push hl
push de
push bc
ld b, a
ld a, DUELVARS_NUMBER_OF_CARDS_IN_HAND
call GetTurnDuelistVariable
ld c, a
inc c
ld l, DUELVARS_HAND
jr .next
.loop
ld a, [hli]
call GetCardIDFromDeckIndex
ld a, e
cp b
jr z, .no_carry
.next
dec c
jr nz, .loop
pop bc
pop de
pop hl
scf
ret
.no_carry
dec hl
ld a, [hl]
pop bc
pop de
pop hl
or a
ret
INCLUDE "engine/duel/ai/damage_calculation.asm"
AIProcessHandTrainerCards:
farcall _AIProcessHandTrainerCards
ret
INCLUDE "engine/duel/ai/deck_ai.asm"
; return carry if card ID loaded in a is found in hand
; and outputs in a the deck index of that card
; as opposed to LookForCardIDInHand, this function
; creates a list in wDuelTempList
; input:
; a = card ID
; output:
; a = card deck index, if found
; carry set if found
LookForCardIDInHandList_Bank5:
ld [wTempCardIDToLook], a
call CreateHandCardList
ld hl, wDuelTempList
.loop
ld a, [hli]
cp $ff
ret z
ldh [hTempCardIndex_ff98], a
call LoadCardDataToBuffer1_FromDeckIndex
ld b, a
ld a, [wTempCardIDToLook]
cp b
jr nz, .loop
ldh a, [hTempCardIndex_ff98]
scf
ret
; returns carry if card ID in a
; is found in Play Area, starting with
; location in b
; input:
; a = card ID
; b = PLAY_AREA_* to start with
; output:
; a = PLAY_AREA_* of found card
; carry set if found
LookForCardIDInPlayArea_Bank5:
ld [wTempCardIDToLook], a
.loop
ld a, DUELVARS_ARENA_CARD
add b
call GetTurnDuelistVariable
cp $ff
ret z
call LoadCardDataToBuffer1_FromDeckIndex
ld c, a
ld a, [wTempCardIDToLook]
cp c
jr z, .found
inc b
ld a, MAX_PLAY_AREA_POKEMON
cp b
jr nz, .loop
; not found
ld b, $ff
or a
ret
.found
ld a, b
scf
ret
; check if energy card ID in e is in AI hand and,
; if so, attaches it to card ID in d in Play Area.
; input:
; e = Energy card ID
; d = Pokemon card ID
AIAttachEnergyInHandToCardInPlayArea:
ld a, e
push de
call LookForCardIDInHandList_Bank5
pop de
ret nc ; not in hand
ld b, PLAY_AREA_ARENA
.attach
ld e, a
ld a, d
call LookForCardIDInPlayArea_Bank5
ldh [hTempPlayAreaLocation_ffa1], a
ld a, e
ldh [hTemp_ffa0], a
ld a, OPPACTION_PLAY_ENERGY
bank1call AIMakeDecision
ret
; same as AIAttachEnergyInHandToCardInPlayArea but
; only look for card ID in the Bench.
AIAttachEnergyInHandToCardInBench:
ld a, e
push de
call LookForCardIDInHandList_Bank5
pop de
ret nc
ld b, PLAY_AREA_BENCH_1
jr AIAttachEnergyInHandToCardInPlayArea.attach
INCLUDE "engine/duel/ai/init.asm"
; load selected attack from Pokémon in hTempPlayAreaLocation_ff9d,
; gets an energy card to discard and subsequently
; check if there is enough energy to execute the selected attack
; after removing that attached energy card.
; input:
; [hTempPlayAreaLocation_ff9d] = location of Pokémon card
; [wSelectedAttack] = selected attack to examine
; output:
; b = basic energy still needed
; c = colorless energy still needed
; e = output of ConvertColorToEnergyCardID, or $0 if not an attack
; carry set if no attack
; OR if it's a Pokémon Power
; OR if not enough energy for attack
CheckEnergyNeededForAttackAfterDiscard:
ldh a, [hTempPlayAreaLocation_ff9d]
add DUELVARS_ARENA_CARD
call GetTurnDuelistVariable
ld d, a
ld a, [wSelectedAttack]
ld e, a
call CopyAttackDataAndDamage_FromDeckIndex
ld hl, wLoadedAttackName
ld a, [hli]
or [hl]
jr z, .no_attack
ld a, [wLoadedAttackCategory]
cp POKEMON_POWER
jr nz, .is_attack
.no_attack
lb bc, 0, 0
ld e, c
scf
ret
.is_attack
ldh a, [hTempPlayAreaLocation_ff9d]
farcall AIPickEnergyCardToDiscard
call LoadCardDataToBuffer1_FromDeckIndex
cp DOUBLE_COLORLESS_ENERGY
jr z, .colorless
; color energy
; decrease respective attached energy by 1.
ld hl, wAttachedEnergies
dec a
ld c, a
ld b, $00
add hl, bc
dec [hl]
ld hl, wTotalAttachedEnergies
dec [hl]
jr .asm_1570c
; decrease attached colorless by 2.
.colorless
ld hl, wAttachedEnergies + COLORLESS
dec [hl]
dec [hl]
ld hl, wTotalAttachedEnergies
dec [hl]
dec [hl]
.asm_1570c
bank1call HandleEnergyBurn
xor a
ld [wTempLoadedAttackEnergyCost], a
ld [wTempLoadedAttackEnergyNeededAmount], a
ld [wTempLoadedAttackEnergyNeededType], a
ld hl, wAttachedEnergies
ld de, wLoadedAttackEnergyCost
ld b, 0
ld c, (NUM_TYPES / 2) - 1
.loop
; check all basic energy cards except colorless
ld a, [de]
swap a
call CheckIfEnoughParticularAttachedEnergy
ld a, [de]
call CheckIfEnoughParticularAttachedEnergy
inc de
dec c
jr nz, .loop
ld a, [de]
swap a
and $0f
ld b, a ; colorless energy still needed
ld a, [wTempLoadedAttackEnergyCost]
ld hl, wTempLoadedAttackEnergyNeededAmount
sub [hl]
ld c, a ; basic energy still needed
ld a, [wTotalAttachedEnergies]
sub c
sub b
jr c, .not_enough_energy
ld a, [wTempLoadedAttackEnergyNeededAmount]
or a
ret z
; being here means the energy cost isn't satisfied,
; including with colorless energy
xor a
.not_enough_energy
cpl
inc a
ld c, a ; colorless energy still needed
ld a, [wTempLoadedAttackEnergyNeededAmount]
ld b, a ; basic energy still needed
ld a, [wTempLoadedAttackEnergyNeededType]
call ConvertColorToEnergyCardID
ld e, a
ld d, 0
scf
ret
; zeroes a bytes starting from hl.
; this function is identical to 'ClearMemory_Bank2',
; 'ClearMemory_Bank6' and 'ClearMemory_Bank8'.
; preserves all registers
; input:
; a = number of bytes to clear
; hl = where to begin erasing
ClearMemory_Bank5:
push af
push bc
push hl
ld b, a
xor a
.clear_loop
ld [hli], a
dec b
jr nz, .clear_loop
pop hl
pop bc
pop af
ret
; converts an HP value or amount of damage to the number of equivalent damage counters
; preserves all registers except af
; input:
; a = HP value to convert
; output:
; a = number of damage counters
ConvertHPToDamageCounters_Bank5:
push bc
ld c, 0
.loop
sub 10
jr c, .done
inc c
jr .loop
.done
ld a, c
pop bc
ret
; returns in a the result of
; dividing b by a, rounded down
; input:
; a = divisor
; b = dividend
CalculateBDividedByA_Bank5:
push bc
ld c, a
ld a, b
ld b, c
ld c, 0
.loop
sub b
jr c, .done
inc c
jr .loop
.done
ld a, c
pop bc
ret
; returns in a the number of energy cards attached
; to Pokémon in location held by e
; this assumes that colorless are paired so
; that one colorless energy card provides 2 colorless energy