-
Notifications
You must be signed in to change notification settings - Fork 817
/
scoring.asm
3267 lines (2639 loc) · 52.5 KB
/
scoring.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
AIScoring: ; used only for BANK(AIScoring)
AI_Basic:
; Don't do anything redundant:
; -Using status-only moves if the player can't be statused
; -Using moves that fail if they've already been used
ld hl, wEnemyAIMoveScores - 1
ld de, wEnemyMonMoves
ld b, NUM_MOVES + 1
.checkmove
dec b
ret z
inc hl
ld a, [de]
and a
ret z
inc de
call AIGetEnemyMove
ld a, [wEnemyMoveStruct + MOVE_EFFECT]
ld c, a
; Dismiss moves with special effects if they are
; useless or not a good choice right now.
; For example, healing moves, weather moves, Dream Eater...
push hl
push de
push bc
farcall AI_Redundant
pop bc
pop de
pop hl
jr nz, .discourage
; Dismiss status-only moves if the player can't be statused.
ld a, [wEnemyMoveStruct + MOVE_EFFECT]
push hl
push de
push bc
ld hl, StatusOnlyEffects
ld de, 1
call IsInArray
pop bc
pop de
pop hl
jr nc, .checkmove
ld a, [wBattleMonStatus]
and a
jr nz, .discourage
; Dismiss Safeguard if it's already active.
ld a, [wPlayerScreens]
bit SCREENS_SAFEGUARD, a
jr z, .checkmove
.discourage
call AIDiscourageMove
jr .checkmove
INCLUDE "data/battle/ai/status_only_effects.asm"
AI_Setup:
; Use stat-modifying moves on turn 1.
; 50% chance to greatly encourage stat-up moves during the first turn of enemy's Pokemon.
; 50% chance to greatly encourage stat-down moves during the first turn of player's Pokemon.
; Almost 90% chance to greatly discourage stat-modifying moves otherwise.
ld hl, wEnemyAIMoveScores - 1
ld de, wEnemyMonMoves
ld b, NUM_MOVES + 1
.checkmove
dec b
ret z
inc hl
ld a, [de]
and a
ret z
inc de
call AIGetEnemyMove
ld a, [wEnemyMoveStruct + MOVE_EFFECT]
cp EFFECT_ATTACK_UP
jr c, .checkmove
cp EFFECT_EVASION_UP + 1
jr c, .statup
; cp EFFECT_ATTACK_DOWN - 1
jr z, .checkmove
cp EFFECT_EVASION_DOWN + 1
jr c, .statdown
cp EFFECT_ATTACK_UP_2
jr c, .checkmove
cp EFFECT_EVASION_UP_2 + 1
jr c, .statup
; cp EFFECT_ATTACK_DOWN_2 - 1
jr z, .checkmove
cp EFFECT_EVASION_DOWN_2 + 1
jr c, .statdown
jr .checkmove
.statup
ld a, [wEnemyTurnsTaken]
and a
jr nz, .discourage
jr .encourage
.statdown
ld a, [wPlayerTurnsTaken]
and a
jr nz, .discourage
.encourage
call AI_50_50
jr c, .checkmove
dec [hl]
dec [hl]
jr .checkmove
.discourage
call Random
cp 12 percent
jr c, .checkmove
inc [hl]
inc [hl]
jr .checkmove
AI_Types:
; Dismiss any move that the player is immune to.
; Encourage super-effective moves.
; Discourage not very effective moves unless
; all damaging moves are of the same type.
ld hl, wEnemyAIMoveScores - 1
ld de, wEnemyMonMoves
ld b, NUM_MOVES + 1
.checkmove
dec b
ret z
inc hl
ld a, [de]
and a
ret z
inc de
call AIGetEnemyMove
push hl
push bc
push de
ld a, 1
ldh [hBattleTurn], a
callfar BattleCheckTypeMatchup
pop de
pop bc
pop hl
ld a, [wTypeMatchup]
and a
jr z, .immune
cp EFFECTIVE
jr z, .checkmove
jr c, .noteffective
; effective
ld a, [wEnemyMoveStruct + MOVE_POWER]
and a
jr z, .checkmove
dec [hl]
jr .checkmove
.noteffective
; Discourage this move if there are any moves
; that do damage of a different type.
push hl
push de
push bc
ld a, [wEnemyMoveStruct + MOVE_TYPE]
ld d, a
ld hl, wEnemyMonMoves
ld b, NUM_MOVES + 1
ld c, 0
.checkmove2
dec b
jr z, .movesdone
ld a, [hli]
and a
jr z, .movesdone
call AIGetEnemyMove
ld a, [wEnemyMoveStruct + MOVE_TYPE]
cp d
jr z, .checkmove2
ld a, [wEnemyMoveStruct + MOVE_POWER]
and a
jr nz, .damaging
jr .checkmove2
.damaging
ld c, a
.movesdone
ld a, c
pop bc
pop de
pop hl
and a
jr z, .checkmove
inc [hl]
jr .checkmove
.immune
call AIDiscourageMove
jr .checkmove
AI_Offensive:
; Greatly discourage non-damaging moves.
ld hl, wEnemyAIMoveScores - 1
ld de, wEnemyMonMoves
ld b, NUM_MOVES + 1
.checkmove
dec b
ret z
inc hl
ld a, [de]
and a
ret z
inc de
call AIGetEnemyMove
ld a, [wEnemyMoveStruct + MOVE_POWER]
and a
jr nz, .checkmove
inc [hl]
inc [hl]
jr .checkmove
AI_Smart:
; Context-specific scoring.
ld hl, wEnemyAIMoveScores
ld de, wEnemyMonMoves
ld b, NUM_MOVES + 1
.checkmove
dec b
ret z
ld a, [de]
inc de
and a
ret z
push de
push bc
push hl
call AIGetEnemyMove
ld a, [wEnemyMoveStruct + MOVE_EFFECT]
ld hl, AI_Smart_EffectHandlers
ld de, 3
call IsInArray
inc hl
jr nc, .nextmove
ld a, [hli]
ld e, a
ld d, [hl]
pop hl
push hl
ld bc, .nextmove
push bc
push de
ret
.nextmove
pop hl
pop bc
pop de
inc hl
jr .checkmove
AI_Smart_EffectHandlers:
dbw EFFECT_SLEEP, AI_Smart_Sleep
dbw EFFECT_LEECH_HIT, AI_Smart_LeechHit
dbw EFFECT_SELFDESTRUCT, AI_Smart_Selfdestruct
dbw EFFECT_DREAM_EATER, AI_Smart_DreamEater
dbw EFFECT_MIRROR_MOVE, AI_Smart_MirrorMove
dbw EFFECT_EVASION_UP, AI_Smart_EvasionUp
dbw EFFECT_ALWAYS_HIT, AI_Smart_AlwaysHit
dbw EFFECT_ACCURACY_DOWN, AI_Smart_AccuracyDown
dbw EFFECT_RESET_STATS, AI_Smart_ResetStats
dbw EFFECT_BIDE, AI_Smart_Bide
dbw EFFECT_FORCE_SWITCH, AI_Smart_ForceSwitch
dbw EFFECT_HEAL, AI_Smart_Heal
dbw EFFECT_TOXIC, AI_Smart_Toxic
dbw EFFECT_LIGHT_SCREEN, AI_Smart_LightScreen
dbw EFFECT_OHKO, AI_Smart_Ohko
dbw EFFECT_RAZOR_WIND, AI_Smart_RazorWind
dbw EFFECT_SUPER_FANG, AI_Smart_SuperFang
dbw EFFECT_TRAP_TARGET, AI_Smart_TrapTarget
dbw EFFECT_UNUSED_2B, AI_Smart_Unused2B
dbw EFFECT_CONFUSE, AI_Smart_Confuse
dbw EFFECT_SP_DEF_UP_2, AI_Smart_SpDefenseUp2
dbw EFFECT_REFLECT, AI_Smart_Reflect
dbw EFFECT_PARALYZE, AI_Smart_Paralyze
dbw EFFECT_SPEED_DOWN_HIT, AI_Smart_SpeedDownHit
dbw EFFECT_SUBSTITUTE, AI_Smart_Substitute
dbw EFFECT_HYPER_BEAM, AI_Smart_HyperBeam
dbw EFFECT_RAGE, AI_Smart_Rage
dbw EFFECT_MIMIC, AI_Smart_Mimic
dbw EFFECT_LEECH_SEED, AI_Smart_LeechSeed
dbw EFFECT_DISABLE, AI_Smart_Disable
dbw EFFECT_COUNTER, AI_Smart_Counter
dbw EFFECT_ENCORE, AI_Smart_Encore
dbw EFFECT_PAIN_SPLIT, AI_Smart_PainSplit
dbw EFFECT_SNORE, AI_Smart_Snore
dbw EFFECT_CONVERSION2, AI_Smart_Conversion2
dbw EFFECT_LOCK_ON, AI_Smart_LockOn
dbw EFFECT_DEFROST_OPPONENT, AI_Smart_DefrostOpponent
dbw EFFECT_SLEEP_TALK, AI_Smart_SleepTalk
dbw EFFECT_DESTINY_BOND, AI_Smart_DestinyBond
dbw EFFECT_REVERSAL, AI_Smart_Reversal
dbw EFFECT_SPITE, AI_Smart_Spite
dbw EFFECT_HEAL_BELL, AI_Smart_HealBell
dbw EFFECT_PRIORITY_HIT, AI_Smart_PriorityHit
dbw EFFECT_THIEF, AI_Smart_Thief
dbw EFFECT_MEAN_LOOK, AI_Smart_MeanLook
dbw EFFECT_NIGHTMARE, AI_Smart_Nightmare
dbw EFFECT_FLAME_WHEEL, AI_Smart_FlameWheel
dbw EFFECT_CURSE, AI_Smart_Curse
dbw EFFECT_PROTECT, AI_Smart_Protect
dbw EFFECT_FORESIGHT, AI_Smart_Foresight
dbw EFFECT_PERISH_SONG, AI_Smart_PerishSong
dbw EFFECT_SANDSTORM, AI_Smart_Sandstorm
dbw EFFECT_ENDURE, AI_Smart_Endure
dbw EFFECT_ROLLOUT, AI_Smart_Rollout
dbw EFFECT_SWAGGER, AI_Smart_Swagger
dbw EFFECT_FURY_CUTTER, AI_Smart_FuryCutter
dbw EFFECT_ATTRACT, AI_Smart_Attract
dbw EFFECT_SAFEGUARD, AI_Smart_Safeguard
dbw EFFECT_MAGNITUDE, AI_Smart_Magnitude
dbw EFFECT_BATON_PASS, AI_Smart_BatonPass
dbw EFFECT_PURSUIT, AI_Smart_Pursuit
dbw EFFECT_RAPID_SPIN, AI_Smart_RapidSpin
dbw EFFECT_MORNING_SUN, AI_Smart_MorningSun
dbw EFFECT_SYNTHESIS, AI_Smart_Synthesis
dbw EFFECT_MOONLIGHT, AI_Smart_Moonlight
dbw EFFECT_HIDDEN_POWER, AI_Smart_HiddenPower
dbw EFFECT_RAIN_DANCE, AI_Smart_RainDance
dbw EFFECT_SUNNY_DAY, AI_Smart_SunnyDay
dbw EFFECT_BELLY_DRUM, AI_Smart_BellyDrum
dbw EFFECT_PSYCH_UP, AI_Smart_PsychUp
dbw EFFECT_MIRROR_COAT, AI_Smart_MirrorCoat
dbw EFFECT_SKULL_BASH, AI_Smart_SkullBash
dbw EFFECT_TWISTER, AI_Smart_Twister
dbw EFFECT_EARTHQUAKE, AI_Smart_Earthquake
dbw EFFECT_FUTURE_SIGHT, AI_Smart_FutureSight
dbw EFFECT_GUST, AI_Smart_Gust
dbw EFFECT_STOMP, AI_Smart_Stomp
dbw EFFECT_SOLARBEAM, AI_Smart_Solarbeam
dbw EFFECT_THUNDER, AI_Smart_Thunder
dbw EFFECT_FLY, AI_Smart_Fly
db -1 ; end
AI_Smart_Sleep:
; Greatly encourage sleep inducing moves if the enemy has either Dream Eater or Nightmare.
; 50% chance to greatly encourage sleep inducing moves otherwise.
ld b, EFFECT_DREAM_EATER
call AIHasMoveEffect
jr c, .encourage
ld b, EFFECT_NIGHTMARE
call AIHasMoveEffect
ret nc
.encourage
call AI_50_50
ret c
dec [hl]
dec [hl]
ret
AI_Smart_LeechHit:
push hl
ld a, 1
ldh [hBattleTurn], a
callfar BattleCheckTypeMatchup
pop hl
; 60% chance to discourage this move if not very effective.
ld a, [wTypeMatchup]
cp EFFECTIVE
jr c, .discourage
; Do nothing if effectiveness is neutral.
ret z
; Do nothing if enemy's HP is full.
call AICheckEnemyMaxHP
ret c
; 80% chance to encourage this move otherwise.
call AI_80_20
ret c
dec [hl]
ret
.discourage
call Random
cp 39 percent + 1
ret c
inc [hl]
ret
AI_Smart_LockOn:
ld a, [wPlayerSubStatus5]
bit SUBSTATUS_LOCK_ON, a
jr nz, .player_locked_on
push hl
call AICheckEnemyQuarterHP
jr nc, .discourage
call AICheckEnemyHalfHP
jr c, .skip_speed_check
call AICompareSpeed
jr nc, .discourage
.skip_speed_check
ld a, [wPlayerEvaLevel]
cp BASE_STAT_LEVEL + 3
jr nc, .maybe_encourage
cp BASE_STAT_LEVEL + 1
jr nc, .do_nothing
ld a, [wEnemyAccLevel]
cp BASE_STAT_LEVEL - 2
jr c, .maybe_encourage
cp BASE_STAT_LEVEL
jr c, .do_nothing
ld hl, wEnemyMonMoves
ld c, NUM_MOVES + 1
.checkmove
dec c
jr z, .discourage
ld a, [hli]
and a
jr z, .discourage
call AIGetEnemyMove
ld a, [wEnemyMoveStruct + MOVE_ACC]
cp 71 percent - 1
jr nc, .checkmove
ld a, 1
ldh [hBattleTurn], a
push hl
push bc
farcall BattleCheckTypeMatchup
ld a, [wTypeMatchup]
cp EFFECTIVE
pop bc
pop hl
jr c, .checkmove
.do_nothing
pop hl
ret
.discourage
pop hl
inc [hl]
ret
.maybe_encourage
pop hl
call AI_50_50
ret c
dec [hl]
dec [hl]
ret
.player_locked_on
push hl
ld hl, wEnemyAIMoveScores - 1
ld de, wEnemyMonMoves
ld c, NUM_MOVES + 1
.checkmove2
inc hl
dec c
jr z, .dismiss
ld a, [de]
and a
jr z, .dismiss
inc de
call AIGetEnemyMove
ld a, [wEnemyMoveStruct + MOVE_ACC]
cp 71 percent - 1
jr nc, .checkmove2
dec [hl]
dec [hl]
jr .checkmove2
.dismiss
pop hl
jp AIDiscourageMove
AI_Smart_Selfdestruct:
; Selfdestruct, Explosion
; Unless this is the enemy's last Pokemon...
push hl
farcall FindAliveEnemyMons
pop hl
jr nc, .notlastmon
; ...greatly discourage this move unless this is the player's last Pokemon too.
push hl
call AICheckLastPlayerMon
pop hl
jr nz, .discourage
.notlastmon
; Greatly discourage this move if enemy's HP is above 50%.
call AICheckEnemyHalfHP
jr c, .discourage
; Do nothing if enemy's HP is below 25%.
call AICheckEnemyQuarterHP
ret nc
; If enemy's HP is between 25% and 50%,
; over 90% chance to greatly discourage this move.
call Random
cp 8 percent
ret c
.discourage
inc [hl]
inc [hl]
inc [hl]
ret
AI_Smart_DreamEater:
; 90% chance to greatly encourage this move.
; The AI_Basic layer will make sure that
; Dream Eater is only used against sleeping targets.
call Random
cp 10 percent
ret c
dec [hl]
dec [hl]
dec [hl]
ret
AI_Smart_EvasionUp:
; Dismiss this move if enemy's evasion can't raise anymore.
ld a, [wEnemyEvaLevel]
cp MAX_STAT_LEVEL
jp nc, AIDiscourageMove
; If enemy's HP is full...
call AICheckEnemyMaxHP
jr nc, .hp_mismatch_1
; ...greatly encourage this move if player is badly poisoned.
ld a, [wPlayerSubStatus5]
bit SUBSTATUS_TOXIC, a
jr nz, .greatly_encourage
; ...70% chance to greatly encourage this move if player is not badly poisoned.
call Random
cp 70 percent
jr nc, .not_encouraged
.greatly_encourage
dec [hl]
dec [hl]
ret
.hp_mismatch_1
; Greatly discourage this move if enemy's HP is below 25%.
call AICheckEnemyQuarterHP
jr nc, .hp_mismatch_2
; If enemy's HP is above 25% but not full, 4% chance to greatly encourage this move.
call Random
cp 4 percent
jr c, .greatly_encourage
; If enemy's HP is between 25% and 50%,...
call AICheckEnemyHalfHP
jr nc, .hp_mismatch_3
; If enemy's HP is above 50% but not full, 20% chance to greatly encourage this move.
call AI_80_20
jr c, .greatly_encourage
jr .not_encouraged
.hp_mismatch_3
; ...50% chance to greatly discourage this move.
call AI_50_50
jr c, .not_encouraged
.hp_mismatch_2
inc [hl]
inc [hl]
; 30% chance to end up here if enemy's HP is full and player is not badly poisoned.
; 77% chance to end up here if enemy's HP is above 50% but not full.
; 96% chance to end up here if enemy's HP is between 25% and 50%.
; 100% chance to end up here if enemy's HP is below 25%.
; In other words, we only end up here if the move has not been encouraged or dismissed.
.not_encouraged
ld a, [wPlayerSubStatus5]
bit SUBSTATUS_TOXIC, a
jr nz, .maybe_greatly_encourage
ld a, [wPlayerSubStatus4]
bit SUBSTATUS_LEECH_SEED, a
jr nz, .maybe_encourage
; Discourage this move if enemy's evasion level is higher than player's accuracy level.
ld a, [wEnemyEvaLevel]
ld b, a
ld a, [wPlayerAccLevel]
cp b
jr c, .discourage
; Greatly encourage this move if the player is in the middle of Fury Cutter or Rollout.
ld a, [wPlayerFuryCutterCount]
and a
jr nz, .greatly_encourage
ld a, [wPlayerSubStatus1]
bit SUBSTATUS_ROLLOUT, a
jr nz, .greatly_encourage
.discourage
inc [hl]
ret
; Player is badly poisoned.
; 70% chance to greatly encourage this move.
; This would counter any previous discouragement.
.maybe_greatly_encourage
call Random
cp 31 percent + 1
ret c
dec [hl]
dec [hl]
ret
; Player is seeded.
; 50% chance to encourage this move.
; This would partly counter any previous discouragement.
.maybe_encourage
call AI_50_50
ret c
dec [hl]
ret
AI_Smart_AlwaysHit:
; 80% chance to greatly encourage this move if either...
; ...enemy's accuracy level has been lowered three or more stages
ld a, [wEnemyAccLevel]
cp BASE_STAT_LEVEL - 2
jr c, .encourage
; ...or player's evasion level has been raised three or more stages.
ld a, [wPlayerEvaLevel]
cp BASE_STAT_LEVEL + 3
ret c
.encourage
call AI_80_20
ret c
dec [hl]
dec [hl]
ret
AI_Smart_MirrorMove:
; If the player did not use any move last turn...
ld a, [wLastPlayerCounterMove]
and a
jr nz, .usedmove
; ...do nothing if enemy is slower than player
call AICompareSpeed
ret nc
; ...or dismiss this move if enemy is faster than player.
jp AIDiscourageMove
; If the player did use a move last turn...
.usedmove
push hl
ld hl, UsefulMoves
ld de, 1
call IsInArray
pop hl
; ...do nothing if he didn't use a useful move.
ret nc
; If he did, 50% chance to encourage this move...
call AI_50_50
ret c
dec [hl]
; ...and 90% chance to encourage this move again if the enemy is faster.
call AICompareSpeed
ret nc
call Random
cp 10 percent
ret c
dec [hl]
ret
AI_Smart_AccuracyDown:
; If player's HP is full...
call AICheckPlayerMaxHP
jr nc, .hp_mismatch_1
; ...and enemy's HP is above 50%...
call AICheckEnemyHalfHP
jr nc, .hp_mismatch_1
; ...greatly encourage this move if player is badly poisoned.
ld a, [wPlayerSubStatus5]
bit SUBSTATUS_TOXIC, a
jr nz, .greatly_encourage
; ...70% chance to greatly encourage this move if player is not badly poisoned.
call Random
cp 70 percent
jr nc, .not_encouraged
.greatly_encourage
dec [hl]
dec [hl]
ret
.hp_mismatch_1
; Greatly discourage this move if player's HP is below 25%.
call AICheckPlayerQuarterHP
jr nc, .hp_mismatch_2
; If player's HP is above 25% but not full, 4% chance to greatly encourage this move.
call Random
cp 4 percent
jr c, .greatly_encourage
; If player's HP is between 25% and 50%,...
call AICheckPlayerHalfHP
jr nc, .hp_mismatch_3
; If player's HP is above 50% but not full, 20% chance to greatly encourage this move.
call AI_80_20
jr c, .greatly_encourage
jr .not_encouraged
; ...50% chance to greatly discourage this move.
.hp_mismatch_3
call AI_50_50
jr c, .not_encouraged
.hp_mismatch_2
inc [hl]
inc [hl]
; We only end up here if the move has not been already encouraged.
.not_encouraged
ld a, [wPlayerSubStatus5]
bit SUBSTATUS_TOXIC, a
jr nz, .maybe_greatly_encourage
ld a, [wPlayerSubStatus4]
bit SUBSTATUS_LEECH_SEED, a
jr nz, .encourage
; Discourage this move if enemy's evasion level is higher than player's accuracy level.
ld a, [wEnemyEvaLevel]
ld b, a
ld a, [wPlayerAccLevel]
cp b
jr c, .discourage
; Greatly encourage this move if the player is in the middle of Fury Cutter or Rollout.
ld a, [wPlayerFuryCutterCount]
and a
jr nz, .greatly_encourage
ld a, [wPlayerSubStatus1]
bit SUBSTATUS_ROLLOUT, a
jr nz, .greatly_encourage
.discourage
inc [hl]
ret
; Player is badly poisoned.
; 70% chance to greatly encourage this move.
; This would counter any previous discouragement.
.maybe_greatly_encourage
call Random
cp 31 percent + 1
ret c
dec [hl]
dec [hl]
ret
; Player is seeded.
; 50% chance to encourage this move.
; This would partly counter any previous discouragement.
.encourage
call AI_50_50
ret c
dec [hl]
ret
AI_Smart_ResetStats:
; 85% chance to encourage this move if any of enemy's stat levels is lower than -2.
push hl
ld hl, wEnemyAtkLevel
ld c, NUM_LEVEL_STATS
.enemystatsloop
dec c
jr z, .enemystatsdone
ld a, [hli]
cp BASE_STAT_LEVEL - 2
jr c, .encourage
jr .enemystatsloop
; 85% chance to encourage this move if any of player's stat levels is higher than +2.
.enemystatsdone
ld hl, wPlayerAtkLevel
ld c, NUM_LEVEL_STATS
.playerstatsloop
dec c
jr z, .discourage
ld a, [hli]
cp BASE_STAT_LEVEL + 3
jr c, .playerstatsloop
.encourage
pop hl
call Random
cp 16 percent
ret c
dec [hl]
ret
; Discourage this move if neither:
; Any of enemy's stat levels is lower than -2.
; Any of player's stat levels is higher than +2.
.discourage
pop hl
inc [hl]
ret
AI_Smart_Bide:
; 90% chance to discourage this move unless enemy's HP is full.
call AICheckEnemyMaxHP
ret c
call Random
cp 10 percent
ret c
inc [hl]
ret
AI_Smart_ForceSwitch:
; Whirlwind, Roar.
; Discourage this move if the player has not shown
; a super-effective move against the enemy.
; Consider player's type(s) if its moves are unknown.
push hl
callfar CheckPlayerMoveTypeMatchups
ld a, [wEnemyAISwitchScore]
cp BASE_AI_SWITCH_SCORE
pop hl
ret c
inc [hl]
ret
AI_Smart_Heal:
AI_Smart_MorningSun:
AI_Smart_Synthesis:
AI_Smart_Moonlight:
; 90% chance to greatly encourage this move if enemy's HP is below 25%.
; Discourage this move if enemy's HP is higher than 50%.
; Do nothing otherwise.
call AICheckEnemyQuarterHP
jr nc, .encourage
call AICheckEnemyHalfHP
ret nc
inc [hl]
ret
.encourage
call Random
cp 10 percent
ret c
dec [hl]
dec [hl]
ret
AI_Smart_Toxic:
AI_Smart_LeechSeed:
; Discourage this move if player's HP is below 50%.
call AICheckPlayerHalfHP
ret c
inc [hl]
ret
AI_Smart_LightScreen:
AI_Smart_Reflect:
; Over 90% chance to discourage this move unless enemy's HP is full.
call AICheckEnemyMaxHP
ret c
call Random
cp 8 percent
ret c
inc [hl]
ret
AI_Smart_Ohko:
; Dismiss this move if player's level is higher than enemy's level.
; Else, discourage this move is player's HP is below 50%.
ld a, [wBattleMonLevel]
ld b, a
ld a, [wEnemyMonLevel]
cp b
jp c, AIDiscourageMove
call AICheckPlayerHalfHP
ret c
inc [hl]
ret
AI_Smart_TrapTarget:
; Bind, Wrap, Fire Spin, Clamp