-
Notifications
You must be signed in to change notification settings - Fork 95
/
Copy pathduel.asm
2410 lines (2307 loc) · 53.2 KB
/
duel.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
; save duel state to SRAM
; called between each two-player turn, just after player draws card (ROM bank 1 loaded)
SaveDuelStateToSRAM::
ld a, $2
call BankswitchSRAM
; save duel data to sCurrentDuel
call SaveDuelData
xor a
call BankswitchSRAM
call EnableSRAM
ld hl, s0a008
ld a, [hl]
inc [hl]
call DisableSRAM
; select hl = SRAM3:(a000 + $400 * [s0a008] & $3)
; save wDuelTurns, non-turn holder's arena card ID, turn holder's arena card ID
and $3
add HIGH($a000) / 4
ld l, $0
ld h, a
add hl, hl
add hl, hl
ld a, $3
call BankswitchSRAM
push hl
ld a, DUELVARS_ARENA_CARD
call GetTurnDuelistVariable
call GetCardIDFromDeckIndex
ld a, e
ld [wTempTurnDuelistCardID], a
call SwapTurn
ld a, DUELVARS_ARENA_CARD
call GetTurnDuelistVariable
call GetCardIDFromDeckIndex
ld a, e
ld [wTempNonTurnDuelistCardID], a
call SwapTurn
pop hl
push hl
call EnableSRAM
ld a, [wDuelTurns]
ld [hli], a
ld a, [wTempNonTurnDuelistCardID]
ld [hli], a
ld a, [wTempTurnDuelistCardID]
ld [hli], a
; save duel data to SRAM3:(a000 + $400 * [s0a008] & $3) + $0010
pop hl
ld de, $0010
add hl, de
ld e, l
ld d, h
call DisableSRAM
bank1call SaveDuelDataToDE
xor a
call BankswitchSRAM
ret
; copies the deck pointed to by de to wPlayerDeck or wOpponentDeck (depending on whose turn it is)
CopyDeckData::
ld hl, wPlayerDeck
ldh a, [hWhoseTurn]
cp PLAYER_TURN
jr z, .copy_deck_data
ld hl, wOpponentDeck
.copy_deck_data
; start by putting a terminator at the end of the deck
push hl
ld bc, DECK_SIZE - 1
add hl, bc
ld [hl], $0
pop hl
push hl
.next_card
ld a, [de]
inc de
ld b, a
or a
jr z, .done
ld a, [de]
inc de
ld c, a
.card_quantity_loop
ld [hl], c
inc hl
dec b
jr nz, .card_quantity_loop
jr .next_card
.done
ld hl, wDeckName
ld a, [de]
inc de
ld [hli], a
ld a, [de]
ld [hl], a
pop hl
ld bc, DECK_SIZE - 1
add hl, bc
ld a, [hl]
or a
ret nz
debug_nop
scf
ret
; return, in register a, the amount of prizes that the turn holder has not yet drawn
CountPrizes::
push hl
ld a, DUELVARS_PRIZES
call GetTurnDuelistVariable
ld l, a
xor a
.count_loop
rr l
adc $00
inc l
dec l
jr nz, .count_loop
pop hl
ret
; shuffles the turn holder's deck
; if less than 60 cards remain in the deck, it makes sure that the rest are ignored
ShuffleDeck::
ldh a, [hWhoseTurn]
ld h, a
ld d, a
ld a, DECK_SIZE
ld l, DUELVARS_NUMBER_OF_CARDS_NOT_IN_DECK
sub [hl]
ld b, a
ld a, DUELVARS_DECK_CARDS
add [hl]
ld l, a ; hl = DUELVARS_DECK_CARDS + [DUELVARS_NUMBER_OF_CARDS_NOT_IN_DECK]
ld a, b ; a = number of cards in the deck
call ShuffleCards
ret
; draw a card from the turn holder's deck, saving its location as CARD_LOCATION_JUST_DRAWN.
; returns carry if deck is empty, nc if a card was successfully drawn.
; AddCardToHand is meant to be called next (unless this function returned carry).
DrawCardFromDeck::
push hl
ld a, DUELVARS_NUMBER_OF_CARDS_NOT_IN_DECK
call GetTurnDuelistVariable
cp DECK_SIZE
jr nc, .empty_deck
inc a
ld [hl], a ; increment number of cards not in deck
add DUELVARS_DECK_CARDS - 1 ; point to top card in the deck
ld l, a
ld a, [hl] ; grab card number (0-59) from wPlayerDeckCards or wOpponentDeckCards array
ld l, a
ld [hl], CARD_LOCATION_JUST_DRAWN ; temporarily write to corresponding card location variable
pop hl
or a
ret
.empty_deck
pop hl
scf
ret
; add a card to the top of the turn holder's deck
; the card is identified by register a, which contains the deck index (0-59) of the card
ReturnCardToDeck::
push hl
push af
ld a, DUELVARS_NUMBER_OF_CARDS_NOT_IN_DECK
call GetTurnDuelistVariable
dec a
ld [hl], a ; decrement number of cards not in deck
add DUELVARS_DECK_CARDS
ld l, a ; point to top deck card
pop af
ld [hl], a ; set top deck card
ld l, a
ld [hl], CARD_LOCATION_DECK
ld a, l
pop hl
ret
; search a card in the turn holder's deck, extract it, and set its location to
; CARD_LOCATION_JUST_DRAWN. AddCardToHand is meant to be called next.
; the card is identified by register a, which contains the deck index (0-59) of the card.
SearchCardInDeckAndAddToHand::
push af
push hl
push de
push bc
ld c, a
ld a, DUELVARS_NUMBER_OF_CARDS_NOT_IN_DECK
call GetTurnDuelistVariable
ld a, DECK_SIZE
sub [hl]
inc [hl] ; increment number of cards not in deck
ld b, a ; DECK_SIZE - [DUELVARS_NUMBER_OF_CARDS_NOT_IN_DECK] (number of cards in deck)
ld l, c
set CARD_LOCATION_JUST_DRAWN_F, [hl]
ld l, DUELVARS_DECK_CARDS + DECK_SIZE - 1
ld e, l
ld d, h ; hl = de = DUELVARS_DECK_CARDS + DECK_SIZE - 1 (last card)
inc b
jr .match
.loop
ld a, [hld]
cp c
jr z, .match
ld [de], a
dec de
.match
dec b
jr nz, .loop
pop bc
pop de
pop hl
pop af
ret
; adds a card to the turn holder's hand and increments the number of cards in the hand
; the card is identified by register a, which contains the deck index (0-59) of the card
AddCardToHand::
push af
push hl
push de
ld e, a
ld l, a
ldh a, [hWhoseTurn]
ld h, a
; write CARD_LOCATION_HAND into the location of this card
ld [hl], CARD_LOCATION_HAND
; increment number of cards in hand
ld l, DUELVARS_NUMBER_OF_CARDS_IN_HAND
inc [hl]
; add card to hand
ld a, DUELVARS_HAND - 1
add [hl]
ld l, a
ld [hl], e
pop de
pop hl
pop af
ret
; removes a card from the turn holder's hand and decrements the number of cards in the hand
; the card is identified by register a, which contains the deck index (0-59) of the card
RemoveCardFromHand::
push af
push hl
push bc
push de
ld c, a
ld a, DUELVARS_NUMBER_OF_CARDS_IN_HAND
call GetTurnDuelistVariable
or a
jr z, .done ; done if no cards in hand
ld b, a ; number of cards in hand
ld l, DUELVARS_HAND
ld e, l
ld d, h
.next_card
ld a, [hli]
cp c
jr nz, .no_match
push hl
ld l, DUELVARS_NUMBER_OF_CARDS_IN_HAND
dec [hl]
pop hl
jr .done_card
.no_match
ld [de], a ; keep any card that doesn't match in the player's hand
inc de
.done_card
dec b
jr nz, .next_card
.done
pop de
pop bc
pop hl
pop af
ret
; moves a card to the turn holder's discard pile, as long as it is in the hand
; the card is identified by register a, which contains the deck index (0-59) of the card
MoveHandCardToDiscardPile::
call GetTurnDuelistVariable
ld a, [hl]
and $ff ^ CARD_LOCATION_JUST_DRAWN
cp CARD_LOCATION_HAND
ret nz ; return if card not in hand
ld a, l
call RemoveCardFromHand
; fallthrough
; puts the turn holder's card with the deck index (0-59) given in a into the discard pile
PutCardInDiscardPile::
push af
push hl
push de
call GetTurnDuelistVariable
ld [hl], CARD_LOCATION_DISCARD_PILE
ld e, l
ld l, DUELVARS_NUMBER_OF_CARDS_IN_DISCARD_PILE
inc [hl]
ld a, DUELVARS_DECK_CARDS - 1
add [hl]
ld l, a
ld [hl], e ; save card to DUELVARS_DECK_CARDS + [DUELVARS_NUMBER_OF_CARDS_IN_DISCARD_PILE]
pop de
pop hl
pop af
ret
; search a card in the turn holder's discard pile, extract it, and set its location to
; CARD_LOCATION_JUST_DRAWN. AddCardToHand is meant to be called next.
; the card is identified by register a, which contains the deck index (0-59) of the card
MoveDiscardPileCardToHand::
push hl
push de
push bc
call GetTurnDuelistVariable
set CARD_LOCATION_JUST_DRAWN_F, [hl]
ld b, l
ld l, DUELVARS_NUMBER_OF_CARDS_IN_DISCARD_PILE
ld a, [hl]
or a
jr z, .done ; done if no cards in discard pile
ld c, a
dec [hl] ; decrement number of cards in discard pile
ld l, DUELVARS_DECK_CARDS
ld e, l
ld d, h ; de = hl = DUELVARS_DECK_CARDS
.next_card
ld a, [hli]
cp b
jr z, .match
ld [de], a
inc de
.match
dec c
jr nz, .next_card
ld a, b
.done
pop bc
pop de
pop hl
ret
; return in the z flag whether turn holder's prize a (0-7) has been drawn or not
; z: drawn, nz: not drawn
CheckPrizeTaken::
ld e, a
ld d, 0
ld hl, PowersOf2
add hl, de
ld a, [hl]
ld e, a
cpl
ld d, a
ld a, DUELVARS_PRIZES
call GetTurnDuelistVariable
and e
ret
PowersOf2::
db $01, $02, $04, $08, $10, $20, $40, $80
; fill wDuelTempList with the turn holder's discard pile cards (their 0-59 deck indexes)
; return carry if the turn holder has no cards in the discard pile
CreateDiscardPileCardList::
ldh a, [hWhoseTurn]
ld h, a
ld l, DUELVARS_NUMBER_OF_CARDS_IN_DISCARD_PILE
ld b, [hl]
ld a, DUELVARS_DECK_CARDS - 1
add [hl] ; point to last card in discard pile
ld l, a
ld de, wDuelTempList
inc b
jr .begin_loop
.next_card_loop
ld a, [hld]
ld [de], a
inc de
.begin_loop
dec b
jr nz, .next_card_loop
ld a, $ff ; $ff-terminated
ld [de], a
ld l, DUELVARS_NUMBER_OF_CARDS_IN_DISCARD_PILE
ld a, [hl]
or a
ret nz
scf
ret
; fill wDuelTempList with the turn holder's remaining deck cards (their 0-59 deck indexes)
; return carry if the turn holder has no cards left in the deck
CreateDeckCardList::
ld a, DUELVARS_NUMBER_OF_CARDS_NOT_IN_DECK
call GetTurnDuelistVariable
cp DECK_SIZE
jr nc, .no_cards_left_in_deck
ld a, DECK_SIZE
sub [hl]
ld c, a
ld b, a ; c = b = DECK_SIZE - [DUELVARS_NUMBER_OF_CARDS_NOT_IN_DECK]
ld a, [hl]
add DUELVARS_DECK_CARDS
ld l, a ; l = DUELVARS_DECK_CARDS + [DUELVARS_NUMBER_OF_CARDS_NOT_IN_DECK]
inc b
ld de, wDuelTempList
jr .begin_loop
.next_card
ld a, [hli]
ld [de], a
inc de
.begin_loop
dec b
jr nz, .next_card
ld a, $ff ; $ff-terminated
ld [de], a
ld a, c
or a
ret
.no_cards_left_in_deck
ld a, $ff
ld [wDuelTempList], a
scf
ret
; fill wDuelTempList with the turn holder's energy cards
; in the arena or in a bench slot (their 0-59 deck indexes).
; if a == 0: search in CARD_LOCATION_ARENA
; if a != 0: search in CARD_LOCATION_BENCH_[A]
; return carry if no energy cards were found
CreateArenaOrBenchEnergyCardList::
or CARD_LOCATION_PLAY_AREA
ld c, a
ld de, wDuelTempList
ld a, DUELVARS_CARD_LOCATIONS
call GetTurnDuelistVariable
.next_card_loop
ld a, [hl]
cp c
jr nz, .skip_card ; jump if not in specified play area location
ld a, l
call LoadCardDataToBuffer2_FromDeckIndex
ld a, [wLoadedCard2Type]
and 1 << TYPE_ENERGY_F
jr z, .skip_card ; jump if Pokemon or trainer card
ld a, l
ld [de], a ; add to wDuelTempList
inc de
.skip_card
inc l
ld a, l
cp DECK_SIZE
jr c, .next_card_loop
; all cards checked
ld a, $ff
ld [de], a
ld a, [wDuelTempList]
cp $ff
jr z, .no_energies_found
or a
ret
.no_energies_found
scf
ret
; fill wDuelTempList with the turn holder's hand cards (their 0-59 deck indexes)
; return carry if the turn holder has no cards in hand
; and outputs in a number of cards.
CreateHandCardList::
call FindLastCardInHand
inc b
jr .skip_card
.check_next_card_loop
ld a, [hld]
push hl
ld l, a
bit CARD_LOCATION_JUST_DRAWN_F, [hl]
pop hl
jr nz, .skip_card
ld [de], a
inc de
.skip_card
dec b
jr nz, .check_next_card_loop
ld a, $ff ; $ff-terminated
ld [de], a
ld l, DUELVARS_NUMBER_OF_CARDS_IN_HAND
ld a, [hl]
or a
ret nz
scf
ret
; sort the turn holder's hand cards by ID (highest to lowest ID)
; makes use of wDuelTempList
SortHandCardsByID::
call FindLastCardInHand
.loop
ld a, [hld]
ld [de], a
inc de
dec b
jr nz, .loop
ld a, $ff
ld [de], a
call SortCardsInDuelTempListByID
call FindLastCardInHand
.loop2
ld a, [de]
inc de
ld [hld], a
dec b
jr nz, .loop2
ret
; returns:
; b = turn holder's number of cards in hand (DUELVARS_NUMBER_OF_CARDS_IN_HAND)
; hl = pointer to turn holder's last (newest) card in DUELVARS_HAND
; de = wDuelTempList
FindLastCardInHand::
ldh a, [hWhoseTurn]
ld h, a
ld l, DUELVARS_NUMBER_OF_CARDS_IN_HAND
ld b, [hl]
ld a, DUELVARS_HAND - 1
add [hl]
ld l, a
ld de, wDuelTempList
ret
; shuffles the deck by swapping the position of each card with the position of another random card
; input:
; a = how many cards to shuffle
; hl = DUELVARS_DECK_CARDS + [DUELVARS_NUMBER_OF_CARDS_NOT_IN_DECK]
ShuffleCards::
or a
ret z ; return if deck is empty
push hl
push de
push bc
ld c, a
ld b, a
ld e, l
ld d, h
.shuffle_next_card_loop
push bc
push de
ld a, c
call Random
add e
ld e, a
ld a, $0
adc d
ld d, a
ld a, [de]
ld b, [hl]
ld [hl], a
ld a, b
ld [de], a
pop de
pop bc
inc hl
dec b
jr nz, .shuffle_next_card_loop
pop bc
pop de
pop hl
ret
; sort a $ff-terminated list of deck index cards by ID (lowest to highest ID).
; the list is wDuelTempList.
SortCardsInDuelTempListByID::
ld hl, hTempListPtr_ff99
ld [hl], LOW(wDuelTempList)
inc hl
ld [hl], HIGH(wDuelTempList)
jr SortCardsInListByID_CheckForListTerminator
; sort a $ff-terminated list of deck index cards by ID (lowest to highest ID).
; the pointer to the list is given in hTempListPtr_ff99.
; sorting by ID rather than deck index means that the order of equal (same ID) cards does not matter,
; even if they have a different deck index.
SortCardsInListByID::
; load [hTempListPtr_ff99] into hl and de
ld hl, hTempListPtr_ff99
ld a, [hli]
ld h, [hl]
ld l, a
ld e, l
ld d, h
; get ID of card with deck index at [de]
ld a, [de]
call GetCardIDFromDeckIndex_bc
ld a, c
ldh [hTempCardID_ff9b], a
ld a, b
ldh [hTempCardID_ff9b + 1], a ; 0
; hl = [hTempListPtr_ff99] + 1
inc hl
jr .check_list_end
.next_card_in_list
ld a, [hl]
call GetCardIDFromDeckIndex_bc
ldh a, [hTempCardID_ff9b + 1]
cp b
jr nz, .go
ldh a, [hTempCardID_ff9b]
cp c
.go
jr c, .not_lower_id
; this card has the lowest ID of those checked so far
ld e, l
ld d, h
ld a, c
ldh [hTempCardID_ff9b], a
ld a, b
ldh [hTempCardID_ff9b + 1], a
.not_lower_id
inc hl
.check_list_end
bit 7, [hl] ; $ff is the list terminator
jr z, .next_card_in_list
; reached list terminator
ld hl, hTempListPtr_ff99
push hl
ld a, [hli]
ld h, [hl]
ld l, a
; swap the lowest ID card found with the card in the current list position
ld c, [hl]
ld a, [de]
ld [hl], a
ld a, c
ld [de], a
pop hl
; [hTempListPtr_ff99] += 1 (point hl to next card in list)
inc [hl]
jr nz, SortCardsInListByID_CheckForListTerminator
inc hl
inc [hl]
; fallthrough
SortCardsInListByID_CheckForListTerminator::
ld hl, hTempListPtr_ff99
ld a, [hli]
ld h, [hl]
ld l, a
bit 7, [hl] ; $ff is the list terminator
jr z, SortCardsInListByID
ret
; returns, in register bc, the id of the card with the deck index specified in register a
; preserves hl
GetCardIDFromDeckIndex_bc::
push hl
call _GetCardIDFromDeckIndex
ld c, a
ld b, $0
pop hl
ret
; return [wDuelTempList + a] in a and in hTempCardIndex_ff98
GetCardInDuelTempList_OnlyDeckIndex::
push hl
push de
ld e, a
ld d, $0
ld hl, wDuelTempList
add hl, de
ld a, [hl]
ldh [hTempCardIndex_ff98], a
pop de
pop hl
ret
; given the deck index (0-59) of a card in [wDuelTempList + a], return:
; - the id of the card with that deck index in register de
; - [wDuelTempList + a] in hTempCardIndex_ff98 and in register a
GetCardInDuelTempList::
push hl
ld e, a
ld d, $0
ld hl, wDuelTempList
add hl, de
ld a, [hl]
ldh [hTempCardIndex_ff98], a
call GetCardIDFromDeckIndex
pop hl
ldh a, [hTempCardIndex_ff98]
ret
; returns, in register de, the id of the card with the deck index (0-59) specified by register a
; preserves af and hl
GetCardIDFromDeckIndex::
push af
push hl
call _GetCardIDFromDeckIndex
ld e, a
ld d, $0
pop hl
pop af
ret
; remove card c from wDuelTempList (it contains a $ff-terminated list of deck indexes)
; returns carry if no matches were found.
RemoveCardFromDuelTempList::
push hl
push de
push bc
ld hl, wDuelTempList
ld e, l
ld d, h
ld c, a
ld b, $00
.next
ld a, [hli]
cp $ff
jr z, .end_of_list
cp c
jr z, .match
ld [de], a
inc de
inc b
.match
jr .next
.end_of_list
ld [de], a
ld a, b
or a
jr nz, .done
scf
.done
pop bc
pop de
pop hl
ret
; return the number of cards in wDuelTempList in a
CountCardsInDuelTempList::
push hl
push bc
ld hl, wDuelTempList
ld b, -1
.loop
inc b
ld a, [hli]
cp $ff
jr nz, .loop
ld a, b
pop bc
pop hl
ret
; returns, in register a, the id of the card with the deck index (0-59) specified in register a
_GetCardIDFromDeckIndex::
push de
ld e, a
ld d, $0
ld hl, wPlayerDeck
ldh a, [hWhoseTurn]
cp PLAYER_TURN
jr z, .load_card_from_deck
ld hl, wOpponentDeck
.load_card_from_deck
add hl, de
ld a, [hl]
pop de
ret
; load data of card with deck index a (0-59) to wLoadedCard1
LoadCardDataToBuffer1_FromDeckIndex::
push hl
push de
push bc
push af
call GetCardIDFromDeckIndex
call LoadCardDataToBuffer1_FromCardID
pop af
ld hl, wLoadedCard1
bank1call ConvertSpecialTrainerCardToPokemon
ld a, e
pop bc
pop de
pop hl
ret
; load data of card with deck index a (0-59) to wLoadedCard2
LoadCardDataToBuffer2_FromDeckIndex::
push hl
push de
push bc
push af
call GetCardIDFromDeckIndex
call LoadCardDataToBuffer2_FromCardID
pop af
ld hl, wLoadedCard2
bank1call ConvertSpecialTrainerCardToPokemon
ld a, e
pop bc
pop de
pop hl
ret
; evolve a turn holder's Pokemon card in the play area slot determined by hTempPlayAreaLocation_ff9d
; into another turn holder's Pokemon card identifier by its deck index (0-59) in hTempCardIndex_ff98.
; return nc if evolution was successful.
EvolvePokemonCardIfPossible::
; first make sure the attempted evolution is viable
ldh a, [hTempCardIndex_ff98]
ld d, a
ldh a, [hTempPlayAreaLocation_ff9d]
ld e, a
call CheckIfCanEvolveInto
ret c ; return if it's not capable of evolving into the selected Pokemon
; fallthrough
; evolve a turn holder's Pokemon card in the play area slot determined by hTempPlayAreaLocation_ff9d
; into another turn holder's Pokemon card identifier by its deck index (0-59) in hTempCardIndex_ff98.
EvolvePokemonCard::
; place the evolved Pokemon card in the play area location of the pre-evolved Pokemon card
ldh a, [hTempPlayAreaLocation_ff9d]
ld e, a
add DUELVARS_ARENA_CARD
call GetTurnDuelistVariable
ld [wPreEvolutionPokemonCard], a ; save pre-evolved Pokemon card into wPreEvolutionPokemonCard
call LoadCardDataToBuffer2_FromDeckIndex
ldh a, [hTempCardIndex_ff98]
ld [hl], a
call LoadCardDataToBuffer1_FromDeckIndex
ldh a, [hTempCardIndex_ff98]
call PutHandCardInPlayArea
; update the Pokemon's HP with the difference
ldh a, [hTempPlayAreaLocation_ff9d] ; derp
ld a, e
add DUELVARS_ARENA_CARD_HP
call GetTurnDuelistVariable
ld a, [wLoadedCard2HP]
ld c, a
ld a, [wLoadedCard1HP]
sub c
add [hl]
ld [hl], a
; reset status (if in arena) and set the flag that prevents it from evolving again this turn
ld a, e
add DUELVARS_ARENA_CARD_FLAGS
ld l, a
ld [hl], $00
ld a, e
add DUELVARS_ARENA_CARD_CHANGED_TYPE
ld l, a
ld [hl], $00
ld a, e
or a
call z, ClearAllStatusConditions
; set the new evolution stage of the card
ldh a, [hTempPlayAreaLocation_ff9d]
add DUELVARS_ARENA_CARD_STAGE
call GetTurnDuelistVariable
ld a, [wLoadedCard1Stage]
ld [hl], a
or a
ret
; never executed
scf
ret
; check if the turn holder's Pokemon card at e can evolve into the turn holder's Pokemon card d.
; e is the play area location offset (PLAY_AREA_*) of the Pokemon trying to evolve.
; d is the deck index (0-59) of the Pokemon card that was selected to be the evolution target.
; return carry if can't evolve, plus nz if the reason for it is the card was played this turn.
CheckIfCanEvolveInto::
push de
ld a, e
add DUELVARS_ARENA_CARD
call GetTurnDuelistVariable
call LoadCardDataToBuffer2_FromDeckIndex
ld a, d
call LoadCardDataToBuffer1_FromDeckIndex
ld hl, wLoadedCard2Name
ld de, wLoadedCard1PreEvoName
ld a, [de]
cp [hl]
jr nz, .cant_evolve ; jump if they are incompatible to evolve
inc de
inc hl
ld a, [de]
cp [hl]
jr nz, .cant_evolve ; jump if they are incompatible to evolve
pop de
ld a, e
add DUELVARS_ARENA_CARD_FLAGS
call GetTurnDuelistVariable
and CAN_EVOLVE_THIS_TURN
jr nz, .can_evolve
; if the card trying to evolve was played this turn, it can't evolve
ld a, $01
or a
scf
ret
.can_evolve
or a
ret
.cant_evolve
pop de
xor a
scf
ret
; check if the turn holder's Pokemon card at e can evolve this turn, and is a basic
; Pokemon card that whose second stage evolution is the turn holder's Pokemon card d.
; e is the play area location offset (PLAY_AREA_*) of the Pokemon trying to evolve.
; d is the deck index (0-59) of the Pokemon card that was selected to be the evolution target.
; return carry if not basic to stage 2 evolution, or if evolution not possible this turn.
CheckIfCanEvolveInto_BasicToStage2::
ld a, e
add DUELVARS_ARENA_CARD_FLAGS
call GetTurnDuelistVariable
and CAN_EVOLVE_THIS_TURN
jr nz, .can_evolve
jr .cant_evolve
.can_evolve
ld a, e
add DUELVARS_ARENA_CARD
ld l, a
ld a, [hl]
call LoadCardDataToBuffer2_FromDeckIndex
ld a, d
call LoadCardDataToBuffer1_FromDeckIndex
ld hl, wLoadedCard1PreEvoName
ld e, [hl]
inc hl
ld d, [hl]
call LoadCardDataToBuffer1_FromName
ld hl, wLoadedCard2Name
ld de, wLoadedCard1PreEvoName
ld a, [de]
cp [hl]
jr nz, .cant_evolve
inc de
inc hl
ld a, [de]
cp [hl]
jr nz, .cant_evolve
or a
ret
.cant_evolve
xor a
scf
ret
; clear the status, all substatuses, and temporary duelvars of the turn holder's
; arena Pokemon. called when sending a new Pokemon into the arena.
; does not reset Headache, since it targets a player rather than a Pokemon.
ClearAllStatusConditions::
push hl
ldh a, [hWhoseTurn]
ld h, a
xor a
ld l, DUELVARS_ARENA_CARD_STATUS
ld [hl], a ; NO_STATUS
ld l, DUELVARS_ARENA_CARD_SUBSTATUS1
ld [hl], a
ld l, DUELVARS_ARENA_CARD_SUBSTATUS2
ld [hl], a
ld l, DUELVARS_ARENA_CARD_CHANGED_WEAKNESS
ld [hl], a
ld l, DUELVARS_ARENA_CARD_CHANGED_RESISTANCE
ld [hl], a
ld l, DUELVARS_ARENA_CARD_SUBSTATUS3
res SUBSTATUS3_THIS_TURN_DOUBLE_DAMAGE_F, [hl]
ld l, DUELVARS_ARENA_CARD_DISABLED_ATTACK_INDEX
ld [hli], a
ld [hli], a
ld [hli], a
ld [hli], a
ld [hli], a
ld [hli], a
ld [hli], a
ld [hl], a
pop hl
ret
; Removes a Pokemon card from the hand and places it in the arena or first available bench slot.
; If the Pokemon is placed in the arena, the status conditions of the player's arena card are zeroed.
; input:
; a = deck index of the card
; return carry if there is no room for more Pokemon
PutHandPokemonCardInPlayArea::
push af
ld a, DUELVARS_NUMBER_OF_POKEMON_IN_PLAY_AREA
call GetTurnDuelistVariable