-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathcore.asm
9167 lines (8155 loc) · 143 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
; Core components of the battle engine.
DoBattle:
xor a
ld [wBattleParticipantsNotFainted], a
ld [wBattleParticipantsIncludingFainted], a
ld [wBattlePlayerAction], a
ld [wBattleEnded], a
inc a
ld [wBattleHasJustStarted], a
ld hl, wOTPartyMon1HP
ld bc, PARTYMON_STRUCT_LENGTH - 1
ld d, BATTLEACTION_SWITCH1 - 1
.loop
inc d
ld a, [hli]
or [hl]
jr nz, .alive
add hl, bc
jr .loop
.alive
ld a, d
ld [wBattleAction], a
ld a, [wLinkMode]
and a
jr z, .not_linked
ldh a, [hSerialConnectionStatus]
cp USING_INTERNAL_CLOCK
jr z, .player_2
.not_linked
ld a, [wBattleMode]
dec a
jr z, .wild
xor a
ld [wEnemySwitchMonIndex], a
call NewEnemyMonStatus
call ResetEnemyStatLevels
call BreakAttraction
call EnemySwitch
.wild
ld c, 40
call DelayFrames
.player_2
call LoadTileMapToTempTileMap
call CheckPlayerPartyForFitMon
ld a, d
and a
jp z, LostBattle
call Call_LoadTempTileMapToTileMap
ld a, [wBattleType]
cp BATTLETYPE_DEBUG
jp z, .tutorial_debug
cp BATTLETYPE_TUTORIAL
jp z, .tutorial_debug
xor a
ld [wCurPartyMon], a
.loop2
call CheckIfCurPartyMonIsFitToFight
jr nz, .alive2
ld hl, wCurPartyMon
inc [hl]
jr .loop2
.alive2
ld a, [wCurBattleMon]
ld [wLastPlayerMon], a
ld a, [wCurPartyMon]
ld [wCurBattleMon], a
inc a
ld hl, wPartySpecies - 1
ld c, a
ld b, 0
add hl, bc
ld a, [hl]
ld [wCurPartySpecies], a
ld [wTempBattleMonSpecies], a
hlcoord 1, 5
ld a, 9
call SlideBattlePicOut
call LoadTileMapToTempTileMap
call ResetBattleParticipants
call InitBattleMon
call ResetPlayerStatLevels
call SendOutMonText
call NewBattleMonStatus
call BreakAttraction
call SendOutPlayerMon
call EmptyBattleTextBox
call LoadTileMapToTempTileMap
call SetPlayerTurn
call SpikesDamage
ld a, [wLinkMode]
and a
jr z, .not_linked_2
ldh a, [hSerialConnectionStatus]
cp USING_INTERNAL_CLOCK
jr nz, .not_linked_2
xor a
ld [wEnemySwitchMonIndex], a
call NewEnemyMonStatus
call ResetEnemyStatLevels
call BreakAttraction
call EnemySwitch
call SetEnemyTurn
call SpikesDamage
.not_linked_2
jp BattleTurn
.tutorial_debug
jp BattleMenu
WildFled_EnemyFled_LinkBattleCanceled:
call Call_LoadTempTileMapToTileMap
ld a, [wBattleResult]
and BATTLERESULT_BITMASK
add DRAW
ld [wBattleResult], a
ld a, [wLinkMode]
and a
ld hl, BattleText_WildFled
jr z, .print_text
ld a, [wBattleResult]
and BATTLERESULT_BITMASK
ld [wBattleResult], a ; WIN
ld hl, BattleText_EnemyFled
call CheckMobileBattleError
jr nc, .print_text
ld hl, wcd2a
bit 4, [hl]
jr nz, .skip_text
ld hl, BattleText_LinkErrorBattleCanceled
.print_text
call StdBattleTextBox
.skip_text
call StopDangerSound
call CheckMobileBattleError
jr c, .skip_sfx
ld de, SFX_RUN
call PlaySFX
.skip_sfx
call SetPlayerTurn
ld a, 1
ld [wBattleEnded], a
ret
BattleTurn:
.loop
call Stubbed_Function3c1bf
call CheckContestBattleOver
jp c, .quit
xor a
ld [wPlayerIsSwitching], a
ld [wEnemyIsSwitching], a
ld [wBattleHasJustStarted], a
ld [wPlayerJustGotFrozen], a
ld [wEnemyJustGotFrozen], a
ld [wCurDamage], a
ld [wCurDamage + 1], a
call HandleBerserkGene
call UpdateBattleMonInParty
farcall AIChooseMove
call IsMobileBattle
jr nz, .not_disconnected
farcall Function100da5
farcall StartMobileInactivityTimer
farcall Function100dd8
jp c, .quit
.not_disconnected
call CheckPlayerLockedIn
jr c, .skip_iteration
.loop1
call BattleMenu
jr c, .quit
ld a, [wBattleEnded]
and a
jr nz, .quit
ld a, [wForcedSwitch] ; roared/whirlwinded/teleported
and a
jr nz, .quit
.skip_iteration
call ParsePlayerAction
jr nz, .loop1
call EnemyTriesToFlee
jr c, .quit
call DetermineMoveOrder
jr c, .false
call Battle_EnemyFirst
jr .proceed
.false
call Battle_PlayerFirst
.proceed
call CheckMobileBattleError
jr c, .quit
ld a, [wForcedSwitch]
and a
jr nz, .quit
ld a, [wBattleEnded]
and a
jr nz, .quit
call HandleBetweenTurnEffects
ld a, [wBattleEnded]
and a
jr nz, .quit
jp .loop
.quit
ret
Stubbed_Function3c1bf:
ret
ld a, 5 ; MBC30 bank used by JP Crystal; inaccessible by MBC3
call GetSRAMBank
ld hl, $a89b ; address of MBC30 bank
inc [hl]
jr nz, .finish
dec hl
inc [hl]
jr nz, .finish
dec [hl]
inc hl
dec [hl]
.finish
call CloseSRAM
ret
HandleBetweenTurnEffects:
ldh a, [hSerialConnectionStatus]
cp USING_EXTERNAL_CLOCK
jr z, .CheckEnemyFirst
call CheckFaint_PlayerThenEnemy
ret c
call HandleFutureSight
call CheckFaint_PlayerThenEnemy
ret c
call HandleWeather
call CheckFaint_PlayerThenEnemy
ret c
call HandleWrap
call CheckFaint_PlayerThenEnemy
ret c
call HandlePerishSong
call CheckFaint_PlayerThenEnemy
ret c
jr .NoMoreFaintingConditions
.CheckEnemyFirst:
call CheckFaint_EnemyThenPlayer
ret c
call HandleFutureSight
call CheckFaint_EnemyThenPlayer
ret c
call HandleWeather
call CheckFaint_EnemyThenPlayer
ret c
call HandleWrap
call CheckFaint_EnemyThenPlayer
ret c
call HandlePerishSong
call CheckFaint_EnemyThenPlayer
ret c
.NoMoreFaintingConditions:
call HandleLeftovers
call HandleMysteryberry
call HandleDefrost
call HandleSafeguard
call HandleScreens
call HandleStatBoostingHeldItems
call HandleHealingItems
call UpdateBattleMonInParty
call LoadTileMapToTempTileMap
jp HandleEncore
HasAnyoneFainted:
call HasPlayerFainted
jp nz, HasEnemyFainted
ret
CheckFaint_PlayerThenEnemy:
.faint_loop
call .Function
ret c
call HasAnyoneFainted
ret nz
jr .faint_loop
.Function:
call HasPlayerFainted
jr nz, .PlayerNotFainted
call HandlePlayerMonFaint
ld a, [wBattleEnded]
and a
jr nz, .BattleIsOver
.PlayerNotFainted:
call HasEnemyFainted
jr nz, .BattleContinues
call HandleEnemyMonFaint
ld a, [wBattleEnded]
and a
jr nz, .BattleIsOver
.BattleContinues:
and a
ret
.BattleIsOver:
scf
ret
CheckFaint_EnemyThenPlayer:
.faint_loop
call .Function
ret c
call HasAnyoneFainted
ret nz
jr .faint_loop
.Function:
call HasEnemyFainted
jr nz, .EnemyNotFainted
call HandleEnemyMonFaint
ld a, [wBattleEnded]
and a
jr nz, .BattleIsOver
.EnemyNotFainted:
call HasPlayerFainted
jr nz, .BattleContinues
call HandlePlayerMonFaint
ld a, [wBattleEnded]
and a
jr nz, .BattleIsOver
.BattleContinues:
and a
ret
.BattleIsOver:
scf
ret
HandleBerserkGene:
ldh a, [hSerialConnectionStatus]
cp USING_EXTERNAL_CLOCK
jr z, .reverse
call .player
jr .enemy
.reverse
call .enemy
; fallthrough
.player
call SetPlayerTurn
ld de, wPartyMon1Item
ld a, [wCurBattleMon]
ld b, a
jr .go
.enemy
call SetEnemyTurn
ld de, wOTPartyMon1Item
ld a, [wCurOTMon]
ld b, a
; fallthrough
.go
push de
push bc
callfar GetUserItem
ld a, [hl]
ld [wNamedObjectIndexBuffer], a
sub BERSERK_GENE
pop bc
pop de
ret nz
ld [hl], a
ld h, d
ld l, e
ld a, b
call GetPartyLocation
xor a
ld [hl], a
ld a, BATTLE_VARS_SUBSTATUS3
call GetBattleVarAddr
push af
set SUBSTATUS_CONFUSED, [hl]
ldh a, [hBattleTurn] ; wiki fix
and a
ld hl, wEnemyConfuseCount
jr z, .set_confuse_count
ld hl, wPlayerConfuseCount
.set_confuse_count
call BattleRandom
and %11
add 2
ld [hl], a
ld a, BATTLE_VARS_MOVE_ANIM
call GetBattleVarAddr
push hl
push af
xor a
ld [hl], a
ld [wAttackMissed], a
ld [wEffectFailed], a
farcall BattleCommand_AttackUp2
pop af
pop hl
ld [hl], a
call GetItemName
ld hl, BattleText_UsersStringBuffer1Activated
call StdBattleTextBox
callfar BattleCommand_StatUpMessage
pop af
bit SUBSTATUS_CONFUSED, a
ret nz
xor a
ld [wNumHits], a
ld de, ANIM_CONFUSED
call Call_PlayBattleAnim_OnlyIfVisible
call SwitchTurnCore
ld hl, BecameConfusedText
jp StdBattleTextBox
EnemyTriesToFlee:
ld a, [wLinkMode]
and a
jr z, .not_linked
ld a, [wBattleAction]
cp BATTLEACTION_FORFEIT
jr z, .forfeit
.not_linked
and a
ret
.forfeit
call WildFled_EnemyFled_LinkBattleCanceled
scf
ret
DetermineMoveOrder:
ld a, [wLinkMode]
and a
jr z, .use_move
ld a, [wBattleAction]
cp BATTLEACTION_STRUGGLE
jr z, .use_move
cp BATTLEACTION_SKIPTURN
jr z, .use_move
sub BATTLEACTION_SWITCH1
jr c, .use_move
ld a, [wBattlePlayerAction]
cp BATTLEPLAYERACTION_SWITCH
jr nz, .switch
ldh a, [hSerialConnectionStatus]
cp USING_INTERNAL_CLOCK
jr z, .player_2
call BattleRandom
cp 50 percent + 1
jp c, .player_first
jp .enemy_first
.player_2
call BattleRandom
cp 50 percent + 1
jp c, .enemy_first
jp .player_first
.switch
callfar AI_Switch
call SetEnemyTurn
call SpikesDamage
jp .enemy_first
.use_move
ld a, [wBattlePlayerAction]
and a ; BATTLEPLAYERACTION_USEMOVE?
jp nz, .player_first
call CompareMovePriority
jr z, .equal_priority
jp c, .player_first ; player goes first
jp .enemy_first
.equal_priority
call SetPlayerTurn
callfar GetUserItem
push bc
callfar GetOpponentItem
pop de
ld a, d
cp HELD_QUICK_CLAW
jr nz, .player_no_quick_claw
ld a, b
cp HELD_QUICK_CLAW
jr z, .both_have_quick_claw
call BattleRandom
cp e
jr nc, .speed_check
jp .player_first
.player_no_quick_claw
ld a, b
cp HELD_QUICK_CLAW
jr nz, .speed_check
call BattleRandom
cp c
jr nc, .speed_check
jp .enemy_first
.both_have_quick_claw
ldh a, [hSerialConnectionStatus]
cp USING_INTERNAL_CLOCK
jr z, .player_2b
call BattleRandom
cp c
jp c, .enemy_first
call BattleRandom
cp e
jp c, .player_first
jr .speed_check
.player_2b
call BattleRandom
cp e
jp c, .player_first
call BattleRandom
cp c
jp c, .enemy_first
jr .speed_check
.speed_check
ld de, wBattleMonSpeed
ld hl, wEnemyMonSpeed
ld c, 2
call CompareBytes
jr z, .speed_tie
jp nc, .player_first
jp .enemy_first
.speed_tie
ldh a, [hSerialConnectionStatus]
cp USING_INTERNAL_CLOCK
jr z, .player_2c
call BattleRandom
cp 50 percent + 1
jp c, .player_first
jp .enemy_first
.player_2c
call BattleRandom
cp 50 percent + 1
jp c, .enemy_first
.player_first
scf
ret
.enemy_first
and a
ret
CheckContestBattleOver:
ld a, [wBattleType]
cp BATTLETYPE_CONTEST
jr nz, .contest_not_over
ld a, [wParkBallsRemaining]
and a
jr nz, .contest_not_over
ld a, [wBattleResult]
and BATTLERESULT_BITMASK
add DRAW
ld [wBattleResult], a
scf
ret
.contest_not_over
and a
ret
CheckPlayerLockedIn:
ld a, [wPlayerSubStatus4]
and 1 << SUBSTATUS_RECHARGE
jp nz, .quit
ld hl, wEnemySubStatus3
res SUBSTATUS_FLINCHED, [hl]
ld hl, wPlayerSubStatus3
res SUBSTATUS_FLINCHED, [hl]
ld a, [hl]
and 1 << SUBSTATUS_CHARGED | 1 << SUBSTATUS_RAMPAGE
jp nz, .quit
ld hl, wPlayerSubStatus1
bit SUBSTATUS_ROLLOUT, [hl]
jp nz, .quit
and a
ret
.quit
scf
ret
ParsePlayerAction:
call CheckPlayerLockedIn
jp c, .locked_in
ld hl, wPlayerSubStatus5
bit SUBSTATUS_ENCORED, [hl]
jr z, .not_encored
ld a, [wLastPlayerMove]
ld [wCurPlayerMove], a
jr .encored
.not_encored
ld a, [wBattlePlayerAction]
cp BATTLEPLAYERACTION_SWITCH
jr z, .reset_rage
and a
jr nz, .reset_bide
ld a, [wPlayerSubStatus3]
and 1 << SUBSTATUS_BIDE
jr nz, .locked_in
xor a
ld [wMoveSelectionMenuType], a
inc a ; POUND
ld [wFXAnimID], a
call MoveSelectionScreen
push af
call Call_LoadTempTileMapToTileMap
call UpdateBattleHuds
ld a, [wCurPlayerMove]
cp STRUGGLE
jr z, .struggle
call PlayClickSFX
.struggle
ld a, $1
ldh [hBGMapMode], a
pop af
ret nz
.encored
call SetPlayerTurn
callfar UpdateMoveData
xor a
ld [wPlayerCharging], a
ld a, [wPlayerMoveStruct + MOVE_EFFECT]
cp EFFECT_FURY_CUTTER
jr z, .continue_fury_cutter
xor a
ld [wPlayerFuryCutterCount], a
.continue_fury_cutter
ld a, [wPlayerMoveStruct + MOVE_EFFECT]
cp EFFECT_RAGE
jr z, .continue_rage
ld hl, wPlayerSubStatus4
res SUBSTATUS_RAGE, [hl]
xor a
ld [wPlayerRageCounter], a
.continue_rage
ld a, [wPlayerMoveStruct + MOVE_EFFECT]
cp EFFECT_PROTECT
jr z, .continue_protect
cp EFFECT_ENDURE
jr z, .continue_protect
xor a
ld [wPlayerProtectCount], a
jr .continue_protect
.reset_bide
ld hl, wPlayerSubStatus3
res SUBSTATUS_BIDE, [hl]
.locked_in
xor a
ld [wPlayerFuryCutterCount], a
ld [wPlayerProtectCount], a
ld [wPlayerRageCounter], a
ld hl, wPlayerSubStatus4
res SUBSTATUS_RAGE, [hl]
.continue_protect
call ParseEnemyAction
xor a
ret
.reset_rage
xor a
ld [wPlayerFuryCutterCount], a
ld [wPlayerProtectCount], a
ld [wPlayerRageCounter], a
ld hl, wPlayerSubStatus4
res SUBSTATUS_RAGE, [hl]
xor a
ret
HandleEncore:
ldh a, [hSerialConnectionStatus]
cp USING_EXTERNAL_CLOCK
jr z, .player_1
call .do_player
jr .do_enemy
.player_1
call .do_enemy
.do_player
ld hl, wPlayerSubStatus5
bit SUBSTATUS_ENCORED, [hl]
ret z
ld a, [wPlayerEncoreCount]
dec a
ld [wPlayerEncoreCount], a
jr z, .end_player_encore
ld hl, wBattleMonPP
ld a, [wCurMoveNum]
ld c, a
ld b, 0
add hl, bc
ld a, [hl]
and PP_MASK
ret nz
.end_player_encore
ld hl, wPlayerSubStatus5
res SUBSTATUS_ENCORED, [hl]
call SetEnemyTurn
ld hl, BattleText_TargetsEncoreEnded
jp StdBattleTextBox
.do_enemy
ld hl, wEnemySubStatus5
bit SUBSTATUS_ENCORED, [hl]
ret z
ld a, [wEnemyEncoreCount]
dec a
ld [wEnemyEncoreCount], a
jr z, .end_enemy_encore
ld hl, wEnemyMonPP
ld a, [wCurEnemyMoveNum]
ld c, a
ld b, 0
add hl, bc
ld a, [hl]
and PP_MASK
ret nz
.end_enemy_encore
ld hl, wEnemySubStatus5
res SUBSTATUS_ENCORED, [hl]
call SetPlayerTurn
ld hl, BattleText_TargetsEncoreEnded
jp StdBattleTextBox
TryEnemyFlee:
ld a, [wBattleMode]
dec a
jr nz, .Stay
ld a, [wPlayerSubStatus5]
bit SUBSTATUS_CANT_RUN, a
jr nz, .Stay
ld a, [wEnemyWrapCount]
and a
jr nz, .Stay
ld a, [wEnemyMonStatus]
and 1 << FRZ | SLP
jr nz, .Stay
ld a, [wTempEnemyMonSpecies]
ld de, 1
ld hl, AlwaysFleeMons
call IsInArray
jr c, .Flee
call BattleRandom
ld b, a
cp 50 percent + 1
jr nc, .Stay
push bc
ld a, [wTempEnemyMonSpecies]
ld de, 1
ld hl, OftenFleeMons
call IsInArray
pop bc
jr c, .Flee
ld a, b
cp 10 percent + 1
jr nc, .Stay
ld a, [wTempEnemyMonSpecies]
ld de, 1
ld hl, SometimesFleeMons
call IsInArray
jr c, .Flee
.Stay:
and a
ret
.Flee:
scf
ret
INCLUDE "data/wild/flee_mons.asm"
CompareMovePriority:
; Compare the priority of the player and enemy's moves.
; Return carry if the player goes first, or z if they match.
ld a, [wCurPlayerMove]
call GetMovePriority
ld b, a
push bc
ld a, [wCurEnemyMove]
call GetMovePriority
pop bc
cp b
ret
GetMovePriority:
; Return the priority (0-3) of move a.
ld b, a
; Vital Throw goes last.
cp VITAL_THROW
ld a, 0
ret z
call GetMoveEffect
ld hl, MoveEffectPriorities
.loop
ld a, [hli]
cp b
jr z, .done
inc hl
cp -1
jr nz, .loop
ld a, BASE_PRIORITY
ret
.done
ld a, [hl]
ret
INCLUDE "data/moves/effects_priorities.asm"
GetMoveEffect:
ld a, b
dec a
ld hl, Moves + MOVE_EFFECT
ld bc, MOVE_LENGTH
call AddNTimes
ld a, BANK(Moves)
call GetFarByte
ld b, a
ret
Battle_EnemyFirst:
call LoadTileMapToTempTileMap
call TryEnemyFlee
jp c, WildFled_EnemyFled_LinkBattleCanceled
call SetEnemyTurn
ld a, $1
ld [wEnemyGoesFirst], a
callfar AI_SwitchOrTryItem
jr c, .switch_item
call EnemyTurn_EndOpponentProtectEndureDestinyBond
call CheckMobileBattleError
ret c
ld a, [wForcedSwitch]
and a
ret nz
call HasPlayerFainted
jp z, HandlePlayerMonFaint
call HasEnemyFainted
jp z, HandleEnemyMonFaint
.switch_item
call SetEnemyTurn
call ResidualDamage
jp z, HandleEnemyMonFaint
call RefreshBattleHuds
call PlayerTurn_EndOpponentProtectEndureDestinyBond
call CheckMobileBattleError
ret c
ld a, [wForcedSwitch]
and a
ret nz
call HasEnemyFainted
jp z, HandleEnemyMonFaint
call HasPlayerFainted
jp z, HandlePlayerMonFaint
call SetPlayerTurn
call ResidualDamage
jp z, HandlePlayerMonFaint
call RefreshBattleHuds
xor a ; BATTLEPLAYERACTION_USEMOVE
ld [wBattlePlayerAction], a
ret
Battle_PlayerFirst:
xor a
ld [wEnemyGoesFirst], a
call SetEnemyTurn
callfar AI_SwitchOrTryItem
push af
call PlayerTurn_EndOpponentProtectEndureDestinyBond
pop bc
ld a, [wForcedSwitch]
and a
ret nz
call CheckMobileBattleError
ret c
call HasEnemyFainted
jp z, HandleEnemyMonFaint
call HasPlayerFainted
jp z, HandlePlayerMonFaint
push bc
call SetPlayerTurn
call ResidualDamage
pop bc
jp z, HandlePlayerMonFaint
push bc
call RefreshBattleHuds
pop af
jr c, .switched_or_used_item
call LoadTileMapToTempTileMap
call TryEnemyFlee
jp c, WildFled_EnemyFled_LinkBattleCanceled
call EnemyTurn_EndOpponentProtectEndureDestinyBond
call CheckMobileBattleError
ret c
ld a, [wForcedSwitch]
and a
ret nz
call HasPlayerFainted
jp z, HandlePlayerMonFaint
call HasEnemyFainted
jp z, HandleEnemyMonFaint
.switched_or_used_item
call SetEnemyTurn
call ResidualDamage
jp z, HandleEnemyMonFaint
call RefreshBattleHuds
xor a ; BATTLEPLAYERACTION_USEMOVE
ld [wBattlePlayerAction], a
ret
PlayerTurn_EndOpponentProtectEndureDestinyBond:
call SetPlayerTurn
call EndUserDestinyBond
callfar DoPlayerTurn
jp EndOpponentProtectEndureDestinyBond
EnemyTurn_EndOpponentProtectEndureDestinyBond:
call SetEnemyTurn
call EndUserDestinyBond
callfar DoEnemyTurn
jp EndOpponentProtectEndureDestinyBond
EndOpponentProtectEndureDestinyBond:
ld a, BATTLE_VARS_SUBSTATUS1_OPP