-
Notifications
You must be signed in to change notification settings - Fork 93
/
Copy pathcore.asm
8455 lines (7906 loc) · 193 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
; try to resume a saved duel from the main menu
TryContinueDuel::
call SetupDuel
call LoadAndValidateDuelSaveData
ldtx hl, BackUpIsBrokenText
jr c, HandleFailedToContinueDuel
; fallthrough
_ContinueDuel::
ld hl, sp+$00
ld a, l
ld [wDuelReturnAddress], a
ld a, h
ld [wDuelReturnAddress + 1], a
call ClearJoypad
ld a, [wDuelTheme]
call PlaySong
xor a
ld [wDuelFinished], a
call DuelMainInterface
jp MainDuelLoop.between_turns
HandleFailedToContinueDuel:
call DrawWideTextBox_WaitForInput
call ResetSerial
scf
ret
; this function begins the duel after the opponent's graphics, name and deck have been introduced
; loads both player's decks and sets up the variables and resources required to begin a duel.
StartDuel_VSAIOpp::
ld a, PLAYER_TURN
ldh [hWhoseTurn], a
ld a, DUELIST_TYPE_PLAYER
ld [wPlayerDuelistType], a
ld a, [wNPCDuelDeckID]
ld [wOpponentDeckID], a
call LoadPlayerDeck
call SwapTurn
call LoadOpponentDeck
call SwapTurn
jr StartDuel
StartDuel_VSLinkOpp:
ld a, MUSIC_DUEL_THEME_1
ld [wDuelTheme], a
ld hl, wOpponentName
xor a
ld [hli], a
ld [hl], a
ld [wIsPracticeDuel], a
; fallthrough
StartDuel:
ld hl, sp+$0
ld a, l
ld [wDuelReturnAddress], a
ld a, h
ld [wDuelReturnAddress + 1], a
xor a
ld [wCurrentDuelMenuItem], a
call SetupDuel
ld a, [wNPCDuelPrizes]
ld [wDuelInitialPrizes], a
call InitVariablesToBeginDuel
ld a, [wDuelTheme]
call PlaySong
call HandleDuelSetup
ret c
; fallthrough
; the loop returns here after every turn switch
MainDuelLoop:
xor a
ld [wCurrentDuelMenuItem], a
call UpdateSubstatusConditions_StartOfTurn
call DisplayDuelistTurnScreen
call HandleTurn
.between_turns
call ExchangeRNG
ld a, [wDuelFinished]
or a
jr nz, .duel_finished
call UpdateSubstatusConditions_EndOfTurn
call HandleBetweenTurnsEvents
call FinishQueuedAnimations
call ExchangeRNG
ld a, [wDuelFinished]
or a
jr nz, .duel_finished
ld hl, wDuelTurns
inc [hl]
ld a, [wDuelType]
cp DUELTYPE_PRACTICE
jr z, .practice_duel
.next_turn
call SwapTurn
jr MainDuelLoop
.practice_duel
ld a, [wIsPracticeDuel]
or a
jr z, .next_turn
ld a, [hl]
cp 15 ; the practice duel lasts 15 turns (8 player turns and 7 opponent turns)
jr c, .next_turn
xor a ; DUEL_WIN
ld [wDuelResult], a
ret
.duel_finished
call ZeroObjectPositionsAndToggleOAMCopy
call EmptyScreen
ld a, BOXMSG_DECISION
call DrawDuelBoxMessage
ldtx hl, DecisionText
call DrawWideTextBox_WaitForInput
call EmptyScreen
ldh a, [hWhoseTurn]
push af
ld a, PLAYER_TURN
ldh [hWhoseTurn], a
call DrawDuelistPortraitsAndNames
call PrintDuelResultStats
pop af
ldh [hWhoseTurn], a
; animate the duel result screen
; load the correct music and animation depending on result
call ResetAnimationQueue
ld a, [wDuelFinished]
cp TURN_PLAYER_WON
jr z, .active_duelist_won_duel
cp TURN_PLAYER_LOST
jr z, .active_duelist_lost_duel
ld a, DUEL_ANIM_DUEL_DRAW
ld c, MUSIC_MATCH_DRAW
ldtx hl, DuelWasADrawText
jr .handle_duel_finished
.active_duelist_won_duel
ldh a, [hWhoseTurn]
cp PLAYER_TURN
jr nz, .opponent_won_duel
.player_won_duel
xor a ; DUEL_WIN
ld [wDuelResult], a
ld a, DUEL_ANIM_DUEL_WIN
ld c, MUSIC_MATCH_VICTORY
ldtx hl, WonDuelText
jr .handle_duel_finished
.active_duelist_lost_duel
ldh a, [hWhoseTurn]
cp PLAYER_TURN
jr nz, .player_won_duel
.opponent_won_duel
ld a, DUEL_LOSS
ld [wDuelResult], a
ld a, DUEL_ANIM_DUEL_LOSS
ld c, MUSIC_MATCH_LOSS
ldtx hl, LostDuelText
.handle_duel_finished
call PlayDuelAnimation
ld a, c
call PlaySong
ld a, OPPONENT_TURN
ldh [hWhoseTurn], a
call DrawWideTextBox_PrintText
call EnableLCD
.wait_song
call DoFrame
call AssertSongFinished
or a
jr nz, .wait_song
ld a, [wDuelFinished]
cp TURN_PLAYER_TIED
jr z, .tied_duel
call PlayDefaultSong
call WaitForWideTextBoxInput
call FinishQueuedAnimations
call ResetSerial
ld a, PLAYER_TURN
ldh [hWhoseTurn], a
ret
.tied_duel
call WaitForWideTextBoxInput
call FinishQueuedAnimations
ld a, [wDuelTheme]
call PlaySong
ldtx hl, StartSuddenDeathMatchText
call DrawWideTextBox_WaitForInput
ld a, 1
ld [wDuelInitialPrizes], a
call InitVariablesToBeginDuel
ld a, [wDuelType]
cp DUELTYPE_LINK
jr z, .link_duel
ld a, PLAYER_TURN
ldh [hWhoseTurn], a
call HandleDuelSetup
jp MainDuelLoop
.link_duel
call ExchangeRNG
ld h, PLAYER_TURN
ld a, [wSerialOp]
cp $29
jr z, .got_turn
ld h, OPPONENT_TURN
.got_turn
ld a, h
ldh [hWhoseTurn], a
call HandleDuelSetup
jp nc, MainDuelLoop
ret
; empty the screen, and setup text and graphics for a duel
SetupDuel:
xor a ; SYM_SPACE
ld [wTileMapFill], a
call ZeroObjectPositionsAndToggleOAMCopy
call EmptyScreen
call LoadSymbolsFont
call SetDefaultConsolePalettes
lb de, $38, $9f
call SetupText
call EnableLCD
ret
; handle the turn of the duelist identified by hWhoseTurn.
; if player's turn, display the animation of the player drawing the card at
; hTempCardIndex_ff98, and save the duel state to SRAM.
HandleTurn:
ld a, DUELVARS_DUELIST_TYPE
call GetTurnDuelistVariable
ld [wDuelistType], a
ld a, [wDuelTurns]
cp 2
jr c, .skip_let_evolve ; jump if it's the turn holder's first turn
call SetAllPlayAreaPokemonCanEvolve
.skip_let_evolve
call InitVariablesToBeginTurn
call DisplayDrawOneCardScreen
call DrawCardFromDeck
jr nc, .deck_not_empty
ld a, TURN_PLAYER_LOST
ld [wDuelFinished], a
ret
.deck_not_empty
ldh [hTempCardIndex_ff98], a
call AddCardToHand
ld a, [wDuelistType]
cp DUELIST_TYPE_PLAYER
jr z, .player_turn
; opponent's turn
call SwapTurn
call IsClairvoyanceActive
call SwapTurn
call c, DisplayPlayerDrawCardScreen
jr DuelMainInterface
; player's turn
.player_turn
call DisplayPlayerDrawCardScreen
call SaveDuelStateToSRAM
; fallthrough
; when a practice duel turn needs to be restarted because the player did not
; follow the instructions correctly, the game loops back here
RestartPracticeDuelTurn:
ld a, PRACTICEDUEL_PRINT_TURN_INSTRUCTIONS
call DoPracticeDuelAction
; fallthrough
; print the main interface during a duel, including background, Pokemon, HUDs and a text box.
; the bottom text box changes depending on whether the turn belongs to the player (show the duel menu),
; an AI opponent (print "Waiting..." and a reduced menu) or a link opponent (print "<Duelist> is thinking").
DuelMainInterface:
call DrawDuelMainScene
ld a, [wDuelistType]
cp DUELIST_TYPE_PLAYER
jr z, PrintDuelMenuAndHandleInput
cp DUELIST_TYPE_LINK_OPP
jp z, DoLinkOpponentTurn
; DUELIST_TYPE_AI_OPP
xor a
ld [wVBlankCounter], a
ld [wSkipDuelistIsThinkingDelay], a
ldtx hl, DuelistIsThinkingText
call DrawWideTextBox_PrintTextNoDelay
call AIDoAction_Turn
ld a, $ff
ld [wPlayerAttackingCardIndex], a
ld [wPlayerAttackingAttackIndex], a
ret
PrintDuelMenuAndHandleInput:
call DrawWideTextBox
ld hl, DuelMenuData
call PlaceTextItems
.menu_items_printed
call SaveDuelData
ld a, [wDuelFinished]
or a
ret nz
ld a, [wCurrentDuelMenuItem]
call SetMenuItem
.handle_input
call DoFrame
ldh a, [hKeysHeld]
and B_BUTTON
jr z, .b_not_held
ldh a, [hKeysPressed]
bit D_UP_F, a
jr nz, DuelMenuShortcut_OpponentPlayArea
bit D_DOWN_F, a
jr nz, DuelMenuShortcut_PlayerPlayArea
bit D_LEFT_F, a
jr nz, DuelMenuShortcut_PlayerDiscardPile
bit D_RIGHT_F, a
jr nz, DuelMenuShortcut_OpponentDiscardPile
bit START_F, a
jp nz, DuelMenuShortcut_OpponentActivePokemon
.b_not_held
ldh a, [hKeysPressed]
and START
jp nz, DuelMenuShortcut_PlayerActivePokemon
ldh a, [hKeysPressed]
bit SELECT_F, a
jp nz, DuelMenuShortcut_BothActivePokemon
ld a, [wDebugSkipDuelMenuInput]
or a
jr nz, .handle_input
call HandleDuelMenuInput
ld a, e
ld [wCurrentDuelMenuItem], a
jr nc, .handle_input
ldh a, [hCurMenuItem]
ld hl, DuelMenuFunctionTable
jp JumpToFunctionInTable
DuelMenuFunctionTable:
dw DuelMenu_Hand
dw DuelMenu_Attack
dw DuelMenu_Check
dw DuelMenu_PkmnPower
dw DuelMenu_Retreat
dw DuelMenu_Done
; unreferenced
UnreferencedDrawCardFromDeckToHand:
call DrawCardFromDeck
call nc, AddCardToHand
ld a, OPPACTION_DRAW_CARD
call SetOppAction_SerialSendDuelData
jp PrintDuelMenuAndHandleInput.menu_items_printed
; triggered by pressing B + UP in the duel menu
DuelMenuShortcut_OpponentPlayArea:
call OpenNonTurnHolderPlayAreaScreen
jp DuelMainInterface
; triggered by pressing B + DOWN in the duel menu
DuelMenuShortcut_PlayerPlayArea:
call OpenTurnHolderPlayAreaScreen
jp DuelMainInterface
; triggered by pressing B + RIGHT in the duel menu
DuelMenuShortcut_OpponentDiscardPile:
call OpenNonTurnHolderDiscardPileScreen
jp c, PrintDuelMenuAndHandleInput
jp DuelMainInterface
; triggered by pressing B + LEFT in the duel menu
DuelMenuShortcut_PlayerDiscardPile:
call OpenTurnHolderDiscardPileScreen
jp c, PrintDuelMenuAndHandleInput
jp DuelMainInterface
; draw the non-turn holder's play area screen
OpenNonTurnHolderPlayAreaScreen:
call SwapTurn
call OpenTurnHolderPlayAreaScreen
call SwapTurn
ret
; draw the turn holder's play area screen
OpenTurnHolderPlayAreaScreen:
call HasAlivePokemonInPlayArea
jp OpenPlayAreaScreenForViewing
; draw the non-turn holder's discard pile screen
OpenNonTurnHolderDiscardPileScreen:
call SwapTurn
call OpenDiscardPileScreen
jp SwapTurn
; draw the turn holder's discard pile screen
OpenTurnHolderDiscardPileScreen:
jp OpenDiscardPileScreen
; draw the non-turn holder's hand screen. simpler version of OpenPlayerHandScreen
; used only for checking the cards rather than for playing them.
OpenNonTurnHolderHandScreen_Simple:
call SwapTurn
call OpenTurnHolderHandScreen_Simple
jp SwapTurn
; draw the turn holder's hand screen. simpler version of OpenPlayerHandScreen
; used only for checking the cards rather than for playing them.
; used for example in the "Your Play Area" screen of the Check menu
OpenTurnHolderHandScreen_Simple:
call CreateHandCardList
jr c, .no_cards_in_hand
call InitAndDrawCardListScreenLayout
ld a, START + A_BUTTON
ld [wNoItemSelectionMenuKeys], a
jp DisplayCardList
.no_cards_in_hand
ldtx hl, NoCardsInHandText
jp DrawWideTextBox_WaitForInput
; triggered by pressing B + START in the duel menu
DuelMenuShortcut_OpponentActivePokemon:
call SwapTurn
call OpenActivePokemonScreen
call SwapTurn
jp DuelMainInterface
; triggered by pressing START in the duel menu
DuelMenuShortcut_PlayerActivePokemon:
call OpenActivePokemonScreen
jp DuelMainInterface
; draw the turn holder's active Pokemon screen if it exists
OpenActivePokemonScreen:
ld a, DUELVARS_ARENA_CARD
call GetTurnDuelistVariable
cp -1
ret z
call GetCardIDFromDeckIndex
call LoadCardDataToBuffer1_FromCardID
ld hl, wCurPlayAreaSlot
xor a
ld [hli], a
ld [hl], a ; wCurPlayAreaY
call OpenCardPage_FromCheckPlayArea
ret
; triggered by selecting the "Pkmn Power" item in the duel menu
DuelMenu_PkmnPower:
call DisplayPlayAreaScreenToUsePkmnPower
jp c, DuelMainInterface
call UseAttackOrPokemonPower
jp DuelMainInterface
; triggered by selecting the "Done" item in the duel menu
DuelMenu_Done:
ld a, PRACTICEDUEL_REPEAT_INSTRUCTIONS
call DoPracticeDuelAction
; always jumps on practice duel (no action requires player to select Done)
jp c, RestartPracticeDuelTurn
ld a, OPPACTION_FINISH_NO_ATTACK
call SetOppAction_SerialSendDuelData
call ClearNonTurnTemporaryDuelvars
ret
; triggered by selecting the "Retreat" item in the duel menu
DuelMenu_Retreat:
ld a, DUELVARS_ARENA_CARD_STATUS
call GetTurnDuelistVariable
and CNF_SLP_PRZ
cp CONFUSED
ldh [hTemp_ffa0], a
jr nz, .not_confused
ld a, [wConfusionRetreatCheckWasUnsuccessful]
or a
jr nz, .unable_due_to_confusion
call CheckAbleToRetreat
jr c, .unable_to_retreat
call DisplayRetreatScreen
jr c, .done
ldtx hl, SelectPkmnOnBenchToSwitchWithActiveText
call DrawWideTextBox_WaitForInput
call OpenPlayAreaScreenForSelection
jr c, .done
ld [wBenchSelectedPokemon], a
ld a, [wBenchSelectedPokemon] ; unnecessary
ldh [hTempPlayAreaLocation_ffa1], a
ld a, OPPACTION_ATTEMPT_RETREAT
call SetOppAction_SerialSendDuelData
call AttemptRetreat
jr nc, .done
call DrawDuelMainScene
.unable_due_to_confusion
ldtx hl, UnableToRetreatText
call DrawWideTextBox_WaitForInput
jp PrintDuelMenuAndHandleInput
.not_confused
; note that the energy cards are discarded (DiscardRetreatCostCards), then returned
; (ReturnRetreatCostCardsToArena), then discarded again for good (AttemptRetreat).
; It's done this way so that the retreating Pokemon is listed with its energies updated
; when the Play Area screen is shown to select the Pokemon to switch to. The reason why
; AttemptRetreat is responsible for discarding the energy cards is because, if the
; Pokemon is confused, it may not be able to retreat, so they cannot be discarded earlier.
call CheckAbleToRetreat
jr c, .unable_to_retreat
call DisplayRetreatScreen
jr c, .done
call DiscardRetreatCostCards
ldtx hl, SelectPkmnOnBenchToSwitchWithActiveText
call DrawWideTextBox_WaitForInput
call OpenPlayAreaScreenForSelection
ld [wBenchSelectedPokemon], a
ldh [hTempPlayAreaLocation_ffa1], a
push af
call ReturnRetreatCostCardsToArena
pop af
jp c, DuelMainInterface
ld a, OPPACTION_ATTEMPT_RETREAT
call SetOppAction_SerialSendDuelData
call AttemptRetreat
.done
jp DuelMainInterface
.unable_to_retreat
call DrawWideTextBox_WaitForInput
jp PrintDuelMenuAndHandleInput
; triggered by selecting the "Hand" item in the duel menu
DuelMenu_Hand:
ld a, DUELVARS_NUMBER_OF_CARDS_IN_HAND
call GetTurnDuelistVariable
or a
jr nz, OpenPlayerHandScreen
ldtx hl, NoCardsInHandText
call DrawWideTextBox_WaitForInput
jp PrintDuelMenuAndHandleInput
; draw the screen for the player's hand and handle user input to for example check
; a card or attempt to use a card, playing the card if possible in that case.
OpenPlayerHandScreen:
call CreateHandCardList
call InitAndDrawCardListScreenLayout
ldtx hl, PleaseSelectHandText
call SetCardListInfoBoxText
ld a, PLAY_CHECK
ld [wCardListItemSelectionMenuType], a
.handle_input
call DisplayCardList
push af
ld a, [wSortCardListByID]
or a
call nz, SortHandCardsByID
pop af
jp c, DuelMainInterface
ldh a, [hTempCardIndex_ff98]
call LoadCardDataToBuffer1_FromDeckIndex
ld a, [wLoadedCard1Type]
ld c, a
bit TYPE_TRAINER_F, c
jr nz, .trainer_card
bit TYPE_ENERGY_F, c
jr nz, PlayEnergyCard
call PlayPokemonCard
jr c, ReloadCardListScreen ; jump if card not played
jp DuelMainInterface
.trainer_card
call PlayTrainerCard
jr c, ReloadCardListScreen ; jump if card not played
jp DuelMainInterface
; play the energy card with deck index at hTempCardIndex_ff98
; c contains the type of energy card being played
PlayEnergyCard:
ld a, c
cp TYPE_ENERGY_WATER
jr nz, .not_water_energy
call IsRainDanceActive
jr c, .rain_dance_active
.not_water_energy
ld a, [wAlreadyPlayedEnergy]
or a
jr nz, .already_played_energy
call HasAlivePokemonInPlayArea
call OpenPlayAreaScreenForSelection ; choose card to play energy card on
jp c, DuelMainInterface ; exit if no card was chosen
.play_energy_set_played
ld a, TRUE
ld [wAlreadyPlayedEnergy], a
.play_energy
ldh a, [hTempPlayAreaLocation_ff9d]
ldh [hTempPlayAreaLocation_ffa1], a
ld e, a
ldh a, [hTempCardIndex_ff98]
ldh [hTemp_ffa0], a
call PutHandCardInPlayArea
call PrintPlayAreaCardList_EnableLCD
ld a, OPPACTION_PLAY_ENERGY
call SetOppAction_SerialSendDuelData
call PrintAttachedEnergyToPokemon
jp DuelMainInterface
.rain_dance_active
call HasAlivePokemonInPlayArea
call OpenPlayAreaScreenForSelection ; choose card to play energy card on
jp c, DuelMainInterface ; exit if no card was chosen
call CheckRainDanceScenario
jr c, .play_energy
ld a, [wAlreadyPlayedEnergy]
or a
jr z, .play_energy_set_played
ldtx hl, MayOnlyAttachOneEnergyCardText
call DrawWideTextBox_WaitForInput
jp OpenPlayerHandScreen
.already_played_energy
ldtx hl, MayOnlyAttachOneEnergyCardText
call DrawWideTextBox_WaitForInput
; fallthrough
; reload the card list screen after the card trying to play couldn't be played
ReloadCardListScreen:
call CreateHandCardList
; skip doing the things that have already been done when initially opened
call DrawCardListScreenLayout
jp OpenPlayerHandScreen.handle_input
; place a basic Pokemon card on the arena or bench, or place an stage 1 or 2
; Pokemon card over a Pokemon card already in play to evolve it.
; the card to use is loaded in wLoadedCard1 and its deck index is at hTempCardIndex_ff98.
; return nc if the card was played, carry if it wasn't.
PlayPokemonCard:
ld a, [wLoadedCard1Stage]
or a ; BASIC
jr nz, .try_evolve ; jump if the card being played is a Stage 1 or 2 Pokemon
ld a, DUELVARS_NUMBER_OF_POKEMON_IN_PLAY_AREA
call GetTurnDuelistVariable
cp MAX_PLAY_AREA_POKEMON
jr nc, .no_space
ldh a, [hTempCardIndex_ff98]
ldh [hTemp_ffa0], a
call PutHandPokemonCardInPlayArea
ldh [hTempPlayAreaLocation_ff9d], a
add DUELVARS_ARENA_CARD_STAGE
call GetTurnDuelistVariable
ld [hl], BASIC
ld a, OPPACTION_PLAY_BASIC_PKMN
call SetOppAction_SerialSendDuelData
ldh a, [hTempCardIndex_ff98]
call LoadCardDataToBuffer1_FromDeckIndex
ld a, 20
call CopyCardNameAndLevel
ld [hl], $00
ld hl, $0000
call LoadTxRam2
ldtx hl, PlacedOnTheBenchText
call DrawWideTextBox_WaitForInput
call ProcessPlayedPokemonCard
or a
ret
.no_space
ldtx hl, NoSpaceOnTheBenchText
call DrawWideTextBox_WaitForInput
scf
ret
.try_evolve
ld a, DUELVARS_NUMBER_OF_POKEMON_IN_PLAY_AREA
call GetTurnDuelistVariable
ld c, a
ldh a, [hTempCardIndex_ff98]
ld d, a
ld e, PLAY_AREA_ARENA
push de
push bc
.next_play_area_pkmn
push de
call CheckIfCanEvolveInto
pop de
jr nc, .can_evolve
inc e
dec c
jr nz, .next_play_area_pkmn
pop bc
pop de
.find_cant_evolve_reason_loop
push de
call CheckIfCanEvolveInto
pop de
ldtx hl, CantEvolvePokemonInSameTurnItsPlacedText
jr nz, .cant_same_turn
inc e
dec c
jr nz, .find_cant_evolve_reason_loop
ldtx hl, NoPokemonCapableOfEvolvingText
.cant_same_turn
; don't bother opening the selection screen if there are no pokemon capable of evolving
call DrawWideTextBox_WaitForInput
scf
ret
.can_evolve
pop bc
pop de
call IsPrehistoricPowerActive
jr c, .prehistoric_power
call HasAlivePokemonInPlayArea
.try_evolve_loop
call OpenPlayAreaScreenForSelection
jr c, .done
ldh a, [hTempCardIndex_ff98]
ldh [hTemp_ffa0], a
ldh a, [hTempPlayAreaLocation_ff9d]
ldh [hTempPlayAreaLocation_ffa1], a
call EvolvePokemonCardIfPossible
jr c, .try_evolve_loop ; jump if evolution wasn't successful somehow
ld a, OPPACTION_EVOLVE_PKMN
call SetOppAction_SerialSendDuelData
call PrintPlayAreaCardList_EnableLCD
call PrintPokemonEvolvedIntoPokemon
call ProcessPlayedPokemonCard
.done
or a
ret
.prehistoric_power
call DrawWideTextBox_WaitForInput
scf
ret
; triggered by selecting the "Check" item in the duel menu
DuelMenu_Check:
call FinishQueuedAnimations
call OpenDuelCheckMenu
jp DuelMainInterface
; triggered by pressing SELECT in the duel menu
DuelMenuShortcut_BothActivePokemon:
call FinishQueuedAnimations
call OpenVariousPlayAreaScreens_FromSelectPresses
jp DuelMainInterface
OpenVariousPlayAreaScreens_FromSelectPresses:
call OpenInPlayAreaScreen_FromSelectButton
ret c
call .Func_45a9
ret c
call SwapTurn
call .Func_45a9
call SwapTurn
ret
.Func_45a9
call HasAlivePokemonInPlayArea
ld a, $02
ld [wPlayAreaSelectAction], a
call OpenPlayAreaScreenForViewing
ldh a, [hKeysPressed]
and B_BUTTON
ret z
scf
ret
; check if the turn holder's arena Pokemon is unable to retreat due to
; some status condition or due the bench containing no alive Pokemon.
; return carry if unable, nc if able.
CheckAbleToRetreat:
call CheckUnableToRetreatDueToEffect
ret c
call CheckIfActiveCardParalyzedOrAsleep
ret c
call HasAlivePokemonInBench
jr c, .unable_to_retreat
ld a, DUELVARS_ARENA_CARD
call GetTurnDuelistVariable
call GetCardIDFromDeckIndex
call LoadCardDataToBuffer1_FromCardID
ld a, [wLoadedCard1Type]
cp TYPE_TRAINER
jr z, .unable_to_retreat
call CheckIfEnoughEnergiesToRetreat
jr c, .not_enough_energies
or a
ret
.not_enough_energies
ld a, [wEnergyCardsRequiredToRetreat]
ld l, a
ld h, $00
call LoadTxRam3
ldtx hl, EnergyCardsRequiredToRetreatText
jr .done
.unable_to_retreat
ldtx hl, UnableToRetreatText
.done
scf
ret
; check if the turn holder's arena Pokemon has enough energies attached to it
; in order to retreat. Return carry if it doesn't.
; load amount of energies required to wEnergyCardsRequiredToRetreat.
CheckIfEnoughEnergiesToRetreat:
ld e, PLAY_AREA_ARENA
call GetPlayAreaCardAttachedEnergies
xor a ; PLAY_AREA_ARENA
ldh [hTempPlayAreaLocation_ff9d], a
call GetPlayAreaCardRetreatCost
ld [wEnergyCardsRequiredToRetreat], a
ld c, a
ld a, [wTotalAttachedEnergies]
cp c
ret c
ld [wNumRetreatEnergiesSelected], a
ld a, c
ld [wEnergyCardsRequiredToRetreat], a
or a
ret
; display the screen that prompts the player to select energy cards to discard
; in order to retreat a Pokemon card. also handle input in order to display
; the amount of energy cards already selected, and return whenever enough
; energy cards have been selected or if the player declines to retreat.
DisplayRetreatScreen:
ld a, $ff
ldh [hTempRetreatCostCards], a
ld a, [wEnergyCardsRequiredToRetreat]
or a
ret z ; return if no energy cards are required at all
xor a
ld [wNumRetreatEnergiesSelected], a
call CreateArenaOrBenchEnergyCardList
call SortCardsInDuelTempListByID
ld a, LOW(hTempRetreatCostCards)
ld [wTempRetreatCostCardsPos], a
xor a ; PLAY_AREA_ARENA
call DisplayEnergyDiscardScreen
ld a, [wEnergyCardsRequiredToRetreat]
ld [wEnergyDiscardMenuDenominator], a
.select_energies_loop
ld a, [wNumRetreatEnergiesSelected]
ld [wEnergyDiscardMenuNumerator], a
call HandleEnergyDiscardMenuInput
ret c
ldh a, [hTempCardIndex_ff98]
call LoadCardDataToBuffer2_FromDeckIndex
; append selected energy card to hTempRetreatCostCards
ld hl, wTempRetreatCostCardsPos
ld c, [hl]
inc [hl]
ldh a, [hTempCardIndex_ff98]
ld [$ff00+c], a
; accumulate selected energy card
ld c, 1
ld a, [wLoadedCard2Type]
cp TYPE_ENERGY_DOUBLE_COLORLESS
jr nz, .not_double
inc c
.not_double
ld hl, wNumRetreatEnergiesSelected
ld a, [hl]
add c
ld [hl], a
ld hl, wEnergyCardsRequiredToRetreat
cp [hl]
jr nc, .enough
; not enough energies selected yet
ldh a, [hTempCardIndex_ff98]
call RemoveCardFromDuelTempList
call DisplayEnergyDiscardMenu
jr .select_energies_loop
.enough
; terminate hTempRetreatCostCards array with $ff
ld a, [wTempRetreatCostCardsPos]
ld c, a
ld a, $ff
ld [$ff00+c], a
or a
ret
; display the screen that prompts the player to select energy cards to discard
; in order to retreat a Pokemon card or use an attack like Ember. includes the
; card's information and a menu to select the attached energy cards to discard.
; input: a = PLAY_AREA_* of the Pokemon trying to discard energies from.
DisplayEnergyDiscardScreen:
ld [wEnergyDiscardPlayAreaLocation], a
call EmptyScreen
call LoadDuelCardSymbolTiles
call LoadDuelFaceDownCardTiles
ld a, [wEnergyDiscardPlayAreaLocation]
ld hl, wCurPlayAreaSlot
ld [hli], a
ld [hl], 0 ; wCurPlayAreaY
call PrintPlayAreaCardInformation
xor a
ld [wEnergyDiscardMenuNumerator], a
inc a
ld [wEnergyDiscardMenuDenominator], a
; fallthrough
; display the menu that belongs to the energy discard screen that lets the player
; select energy cards attached to a Pokemon card in order to retreat it or use
; an attack like Ember, Flamethrower...
DisplayEnergyDiscardMenu:
lb de, 0, 3
lb bc, 20, 10
call DrawRegularTextBox
ldtx hl, ChooseEnergyCardToDiscardText
call DrawWideTextBox_PrintTextNoDelay
call EnableLCD
call CountCardsInDuelTempList
ld hl, EnergyDiscardCardListParameters
lb de, 0, 0 ; initial page scroll offset, initial item (in the visible page)
call PrintCardListItems
ld a, 4
ld [wCardListIndicatorYPosition], a
ret
; if [wEnergyDiscardMenuDenominator] non-0:
; prints "[wEnergyDiscardMenuNumerator]/[wEnergyDiscardMenuDenominator]" at 16,16
; where [wEnergyDiscardMenuNumerator] is the number of energy cards already selected to discard
; and [wEnergyDiscardMenuDenominator] is the total number of energies that are required to discard.
; if [wEnergyDiscardMenuDenominator] == 0:
; prints only "[wEnergyDiscardMenuNumerator]"
HandleEnergyDiscardMenuInput:
lb bc, 16, 16
ld a, [wEnergyDiscardMenuDenominator]
or a
jr z, .print_single_number
ld a, [wEnergyDiscardMenuNumerator]
add SYM_0
call WriteByteToBGMap0
inc b
ld a, SYM_SLASH
call WriteByteToBGMap0
inc b
ld a, [wEnergyDiscardMenuDenominator]
add SYM_0
call WriteByteToBGMap0
jr .wait_input
.print_single_number
ld a, [wEnergyDiscardMenuNumerator]
inc b
call WriteTwoDigitNumberInTxSymbolFormat
.wait_input
call DoFrame
call HandleCardListInput
jr nc, .wait_input
cp $ff ; B pressed?
jr z, .return_carry
call GetCardInDuelTempList_OnlyDeckIndex
or a
ret
.return_carry
scf
ret
EnergyDiscardCardListParameters:
db 1, 5 ; cursor x, cursor y
db 4 ; item x
db 14 ; maximum length, in tiles, occupied by the name and level string of each card in the list
db 4 ; number of items selectable without scrolling
db SYM_CURSOR_R ; cursor tile number
db SYM_SPACE ; tile behind cursor
dw NULL ; function pointer if non-0
; triggered by selecting the "Attack" item in the duel menu
DuelMenu_Attack:
call HandleCantAttackSubstatus
jr c, .alert_cant_attack_and_cancel_menu
call CheckIfActiveCardParalyzedOrAsleep
jr nc, .can_attack
.alert_cant_attack_and_cancel_menu
call DrawWideTextBox_WaitForInput
jp PrintDuelMenuAndHandleInput
.can_attack
xor a
ld [wSelectedDuelSubMenuItem], a
.try_open_attack_menu
call PrintAndLoadAttacksToDuelTempList
or a
jr nz, .open_attack_menu
ldtx hl, NoSelectableAttackText
call DrawWideTextBox_WaitForInput
jp PrintDuelMenuAndHandleInput
.open_attack_menu
push af
ld a, [wSelectedDuelSubMenuItem]