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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
|
Func_39680: ; 39680 (e:5680)
ld a, [H_WHOSETURN] ; $fff3
and a
ld a, [wd060]
ld hl, wBattleMonAttack + 1
jr z, .asm_39691
ld a, [wd065]
ld hl, wEnemyMonAttack + 1
.asm_39691
ld c, $4
ld b, a
.asm_39694
srl b
call c, Func_3969f
inc hl
inc hl
dec c
ret z
jr .asm_39694
Func_3969f: ; 3969f (e:569f)
ld a, [hl]
add a
ld [hld], a
ld a, [hl]
rl a
ld [hli], a
ret
Func_396a7: ; 396a7 (e:56a7)
ld a, [H_WHOSETURN] ; $fff3
and a
ld a, [wd061]
ld hl, wBattleMonAttack
jr z, .asm_396b8
ld a, [wd066]
ld hl, wEnemyMonAttack
.asm_396b8
ld c, $4
ld b, a
.asm_396bb
srl b
call c, Func_396c6
inc hl
inc hl
dec c
ret z
jr .asm_396bb
Func_396c6: ; 396c6 (e:56c6)
ld a, [hl]
srl a
ld [hli], a
rr [hl]
or [hl]
jr nz, .asm_396d1
ld [hl], $1
.asm_396d1
dec hl
ret
_ScrollTrainerPicAfterBattle: ; 396d3 (e:56d3)
; Load the enemy trainer's pic and scrolls it into
; the screen from the right.
xor a
ld [wEnemyMonSpecies2], a
ld b, $1
call GoPAL_SET
callab _LoadTrainerPic
hlCoord 19, 0
ld c, $0
.asm_396e9
inc c
ld a, c
cp $7
ret z
ld d, $0
push bc
push hl
.asm_396f2
call Func_39707
inc hl
ld a, $7
add d
ld d, a
dec c
jr nz, .asm_396f2
ld c, $4
call DelayFrames
pop hl
pop bc
dec hl
jr .asm_396e9
Func_39707: ; 39707 (e:5707)
push hl
push de
push bc
ld e, $7
.asm_3970c
ld [hl], d
ld bc, $14
add hl, bc
inc d
dec e
jr nz, .asm_3970c
pop bc
pop de
pop hl
ret
; creates a set of moves that may be used and returns its address in hl
; unused slots are filled with 0, all used slots may be chosen with equal probability
AIEnemyTrainerChooseMoves: ; 39719 (e:5719)
ld a, $a
ld hl, wHPBarMaxHP ; init temporary move selection array. Only the moves with the lowest numbers are chosen in the end
ld [hli], a ; move 1
ld [hli], a ; move 2
ld [hli], a ; move 3
ld [hl], a ; move 4
ld a, [W_ENEMYDISABLEDMOVE] ; forbid disabled move (if any)
swap a
and $f
jr z, .noMoveDisabled
ld hl, wHPBarMaxHP
dec a
ld c, a
ld b, $0
add hl, bc ; advance pointer to forbidden move
ld [hl], $50 ; forbid (highly discourage) disabled move
.noMoveDisabled
ld hl, TrainerClassMoveChoiceModifications ; 589B
ld a, [W_TRAINERCLASS]
ld b, a
.loopTrainerClasses
dec b
jr z, .readTrainerClassData
.loopTrainerClassData
ld a, [hli]
and a
jr nz, .loopTrainerClassData
jr .loopTrainerClasses
.readTrainerClassData
ld a, [hl]
and a
jp z, .useOriginalMoveSet
push hl
.nextMoveChoiceModification
pop hl
ld a, [hli]
and a
jr z, .loopFindMinimumEntries
push hl
ld hl, AIMoveChoiceModificationFunctionPointers ; $57a3
dec a
add a
ld c, a
ld b, $0
add hl, bc ; skip to pointer
ld a, [hli] ; read pointer into hl
ld h, [hl]
ld l, a
ld de, .nextMoveChoiceModification ; set return address
push de
jp [hl] ; execute modification function
.loopFindMinimumEntries ; all entries will be decremented sequentially until one of them is zero
ld hl, wHPBarMaxHP ; temp move selection array
ld de, wEnemyMonMoves ; enemy moves
ld c, $4
.loopDecrementEntries
ld a, [de]
inc de
and a
jr z, .loopFindMinimumEntries
dec [hl]
jr z, .minimumEntriesFound
inc hl
dec c
jr z, .loopFindMinimumEntries
jr .loopDecrementEntries
.minimumEntriesFound
ld a, c
.loopUndoPartialIteration ; undo last (partial) loop iteration
inc [hl]
dec hl
inc a
cp $5
jr nz, .loopUndoPartialIteration
ld hl, wHPBarMaxHP ; temp move selection array
ld de, wEnemyMonMoves ; enemy moves
ld c, $4
.filterMinimalEntries ; all minimal entries now have value 1. All other slots will be disabled (move set to 0)
ld a, [de]
and a
jr nz, .moveExisting ; 0x3978a $1
ld [hl], a
.moveExisting
ld a, [hl]
dec a
jr z, .slotWithMinimalValue
xor a
ld [hli], a ; disable move slot
jr .next
.slotWithMinimalValue
ld a, [de]
ld [hli], a ; enable move slot
.next
inc de
dec c
jr nz, .filterMinimalEntries
ld hl, wHPBarMaxHP ; use created temporary array as move set
ret
.useOriginalMoveSet
ld hl, wEnemyMonMoves ; use original move set
ret
AIMoveChoiceModificationFunctionPointers: ; 397a3 (e:57a3)
dw AIMoveChoiceModification1
dw AIMoveChoiceModification2
dw AIMoveChoiceModification3
dw AIMoveChoiceModification4 ; unused, does nothing
; discourages moves that cause no damage but only a status ailment if player's mon already has one
AIMoveChoiceModification1: ; 397ab (e:57ab)
ld a, [wBattleMonStatus]
and a
ret z ; return if no status ailment on player's mon
ld hl, wBuffer - 1 ; temp move selection array (-1 byte offest)
ld de, wEnemyMonMoves ; enemy moves
ld b, $5
.nextMove
dec b
ret z ; processed all 4 moves
inc hl
ld a, [de]
and a
ret z ; no more moves in move set
inc de
call ReadMove
ld a, [W_ENEMYMOVEPOWER]
and a
jr nz, .nextMove
ld a, [W_ENEMYMOVEEFFECT]
push hl
push de
push bc
ld hl, StatusAilmentMoveEffects
ld de, $0001
call IsInArray
pop bc
pop de
pop hl
jr nc, .nextMove
ld a, [hl]
add $5 ; discourage move
ld [hl], a
jr .nextMove
StatusAilmentMoveEffects ; 57e2
db $01 ; some sleep effect?
db SLEEP_EFFECT
db POISON_EFFECT
db PARALYZE_EFFECT
db $FF
; slightly encourage moves with specific effects
AIMoveChoiceModification2: ; 397e7 (e:57e7)
ld a, [wccd5]
cp $1
ret nz
ld hl, wBuffer - 1 ; temp move selection array (-1 byte offest)
ld de, wEnemyMonMoves ; enemy moves
ld b, $5
.nextMove
dec b
ret z ; processed all 4 moves
inc hl
ld a, [de]
and a
ret z ; no more moves in move set
inc de
call ReadMove
ld a, [W_ENEMYMOVEEFFECT]
cp ATTACK_UP1_EFFECT
jr c, .nextMove
cp BIDE_EFFECT
jr c, .preferMove
cp ATTACK_UP2_EFFECT
jr c, .nextMove
cp POISON_EFFECT
jr c, .preferMove
jr .nextMove
.preferMove
dec [hl] ; slighly encourage this move
jr .nextMove
; encourages moves that are effective against the player's mon
AIMoveChoiceModification3: ; 39817 (e:5817)
ld hl, wBuffer - 1 ; temp move selection array (-1 byte offest)
ld de, wEnemyMonMoves ; enemy moves
ld b, $5
.nextMove
dec b
ret z ; processed all 4 moves
inc hl
ld a, [de]
and a
ret z ; no more moves in move set
inc de
call ReadMove
push hl
push bc
push de
callab AIGetTypeEffectiveness
pop de
pop bc
pop hl
ld a, [wd11e]
cp $10
jr z, .nextMove
jr c, .notEffectiveMove
dec [hl] ; slighly encourage this move
jr .nextMove
.notEffectiveMove ; discourages non-effective moves if better moves are available
push hl
push de
push bc
ld a, [W_ENEMYMOVETYPE]
ld d, a
ld hl, wEnemyMonMoves ; enemy moves
ld b, $5
ld c, $0
.loopMoves
dec b
jr z, .done
ld a, [hli]
and a
jr z, .done
call ReadMove
ld a, [W_ENEMYMOVEEFFECT]
cp SUPER_FANG_EFFECT
jr z, .betterMoveFound ; Super Fang is considered to be a better move
cp SPECIAL_DAMAGE_EFFECT
jr z, .betterMoveFound ; any special damage moves are considered to be better moves
cp FLY_EFFECT
jr z, .betterMoveFound ; Fly is considered to be a better move
ld a, [W_ENEMYMOVETYPE]
cp d
jr z, .loopMoves
ld a, [W_ENEMYMOVEPOWER]
and a
jr nz, .betterMoveFound ; damaging moves of a different type are considered to be better moves
jr .loopMoves
.betterMoveFound
ld c, a
.done
ld a, c
pop bc
pop de
pop hl
and a
jr z, .nextMove
inc [hl] ; slighly discourage this move
jr .nextMove
AIMoveChoiceModification4: ; 39883 (e:5883)
ret
ReadMove: ; 39884 (e:5884)
push hl
push de
push bc
dec a
ld hl,Moves
ld bc,6
call AddNTimes
ld de,W_ENEMYMOVENUM
call CopyData
pop bc
pop de
pop hl
ret
; move choice modification methods that are applied for each trainer class
; 0 is sentinel value
TrainerClassMoveChoiceModifications: ; 3989b (e:589b)
db 0 ; YOUNGSTER
db 1,0 ; BUG CATCHER
db 1,0 ; LASS
db 1,3,0 ; SAILOR
db 1,0 ; JR__TRAINER_M
db 1,0 ; JR__TRAINER_F
db 1,2,3,0; POKEMANIAC
db 1,2,0 ; SUPER_NERD
db 1,0 ; HIKER
db 1,0 ; BIKER
db 1,3,0 ; BURGLAR
db 1,0 ; ENGINEER
db 1,2,0 ; JUGGLER_X
db 1,3,0 ; FISHER
db 1,3,0 ; SWIMMER
db 0 ; CUE_BALL
db 1,0 ; GAMBLER
db 1,3,0 ; BEAUTY
db 1,2,0 ; PSYCHIC_TR
db 1,3,0 ; ROCKER
db 1,0 ; JUGGLER
db 1,0 ; TAMER
db 1,0 ; BIRD_KEEPER
db 1,0 ; BLACKBELT
db 1,0 ; SONY1
db 1,3,0 ; PROF_OAK
db 1,2,0 ; CHIEF
db 1,2,0 ; SCIENTIST
db 1,3,0 ; GIOVANNI
db 1,0 ; ROCKET
db 1,3,0 ; COOLTRAINER_M
db 1,3,0 ; COOLTRAINER_F
db 1,0 ; BRUNO
db 1,0 ; BROCK
db 1,3,0 ; MISTY
db 1,3,0 ; LT__SURGE
db 1,3,0 ; ERIKA
db 1,3,0 ; KOGA
db 1,3,0 ; BLAINE
db 1,3,0 ; SABRINA
db 1,2,0 ; GENTLEMAN
db 1,3,0 ; SONY2
db 1,3,0 ; SONY3
db 1,2,3,0; LORELEI
db 1,0 ; CHANNELER
db 1,0 ; AGATHA
db 1,3,0 ; LANCE
TrainerPicAndMoneyPointers: ; 39914 (e:5914)
; trainer pic pointers and base money.
; money received after battle = base money × level of highest-level enemy mon
dw YoungsterPic
money 1500
dw BugCatcherPic
money 1000
dw LassPic
money 1500
dw SailorPic
money 3000
dw JrTrainerMPic
money 2000
dw JrTrainerFPic
money 2000
dw PokemaniacPic
money 5000
dw SuperNerdPic
money 2500
dw HikerPic
money 3500
dw BikerPic
money 2000
dw BurglarPic
money 9000
dw EngineerPic
money 5000
dw JugglerPic
money 3500
dw FisherPic
money 3500
dw SwimmerPic
money 500
dw CueBallPic
money 2500
dw GamblerPic
money 7000
dw BeautyPic
money 7000
dw PsychicPic
money 1000
dw RockerPic
money 2500
dw JugglerPic
money 3500
dw TamerPic
money 4000
dw BirdKeeperPic
money 2500
dw BlackbeltPic
money 2500
dw Rival1Pic
money 3500
dw ProfOakPic
money 9900
dw ChiefPic
money 3000
dw ScientistPic
money 5000
dw GiovanniPic
money 9900
dw RocketPic
money 3000
dw CooltrainerMPic
money 3500
dw CooltrainerFPic
money 3500
dw BrunoPic
money 9900
dw BrockPic
money 9900
dw MistyPic
money 9900
dw LtSurgePic
money 9900
dw ErikaPic
money 9900
dw KogaPic
money 9900
dw BlainePic
money 9900
dw SabrinaPic
money 9900
dw GentlemanPic
money 7000
dw Rival2Pic
money 6500
dw Rival3Pic
money 9900
dw LoreleiPic
money 9900
dw ChannelerPic
money 3000
dw AgathaPic
money 9900
dw LancePic
money 9900
INCLUDE "text/trainer_names.asm"
Func_39b87: ; 39b87 (e:5b87)
ld hl, wd0dc
ld de, wd0e1
ld b, $0
.asm_39b8f
ld a, [hli]
and a
jr z, .asm_39bc1
push hl
ld [wd0b5], a
ld a, BANK(MoveNames)
ld [wPredefBank], a
ld a, MOVE_NAME
ld [W_LISTTYPE], a
call GetName
ld hl, wcd6d
.asm_39ba7
ld a, [hli]
cp $50
jr z, .asm_39bb0
ld [de], a
inc de
jr .asm_39ba7
.asm_39bb0
ld a, b
ld [wcd6c], a
inc b
ld a, $4e
ld [de], a
inc de
pop hl
ld a, b
cp $4
jr z, .asm_39bd1
jr .asm_39b8f
.asm_39bc1
ld a, "-"
ld [de], a
inc de
inc b
ld a, b
cp $4
jr z, .asm_39bd1
ld a, $4e
ld [de], a
inc de
jr .asm_39bc1
.asm_39bd1
ld a, "@"
ld [de], a
ret
Func_39bd5: ; 39bd5 (e:5bd5)
ld a, [wd11b]
cp $1
jr nz, .asm_39be6
ld hl, wEnemyPartyCount ; wEnemyPartyCount
ld de, wEnemyMonOT ; wd9ac OT names of other player
ld a, $6
jr .asm_39c18
.asm_39be6
cp $4
jr nz, .calcAttackStat4
ld hl, wPartyCount ; wPartyCount
ld de, wPartyMonOT ; wd273
ld a, $5
jr .asm_39c18
.calcAttackStat4
cp $5
jr nz, .asm_39c02
ld hl, wStringBuffer2 + 11
ld de, MonsterNames ; $421e
ld a, $1
jr .asm_39c18
.asm_39c02
cp $2
jr nz, .asm_39c10
ld hl, wNumBagItems ; wNumBagItems
ld de, ItemNames ; $472b
ld a, $4
jr .asm_39c18
.asm_39c10
ld hl, wStringBuffer2 + 11
ld de, ItemNames ; $472b
ld a, ITEM_NAME
.asm_39c18
ld [W_LISTTYPE], a
ld a, l
ld [wList], a
ld a, h
ld [wList + 1], a
ld a, e
ld [wcf8d], a
ld a, d
ld [wcf8e], a
ld bc, ItemPrices
ld a, c
ld [wItemPrices], a
ld a, b
ld [wItemPrices + 1], a
ret
Func_39c37: ; 39c37 (e:5c37)
ld hl, wPartySpecies
ld a, [wcc49]
and a
jr z, .asm_39c4b
dec a
jr z, .asm_39c48
ld hl, wBoxSpecies
jr .asm_39c4b
.asm_39c48
ld hl, wEnemyPartyMons
.asm_39c4b
ld d, $0
add hl, de
ld a, [hl]
ld [wcf91], a
ret
ReadTrainer: ; 39c53 (e:5c53)
; don't change any moves in a link battle
ld a,[wLinkState]
and a
ret nz
; set [wEnemyPartyCount] to 0, [wEnemyPartyMons] to FF
; XXX first is total enemy pokemon?
; XXX second is species of first pokemon?
ld hl,wEnemyPartyCount
xor a
ld [hli],a
dec a
ld [hl],a
; get the pointer to trainer data for this class
ld a,[W_CUROPPONENT]
sub $C9 ; convert value from pokemon to trainer
add a,a
ld hl,TrainerDataPointers
ld c,a
ld b,0
add hl,bc ; hl points to trainer class
ld a,[hli]
ld h,[hl]
ld l,a
ld a,[W_TRAINERNO]
ld b,a
; At this point b contains the trainer number,
; and hl points to the trainer class.
; Our next task is to iterate through the trainers,
; decrementing b each time, until we get to the right one.
.outer
dec b
jr z,.IterateTrainer
.inner
ld a,[hli]
and a
jr nz,.inner
jr .outer
; if the first byte of trainer data is FF,
; - each pokemon has a specific level
; (as opposed to the whole team being of the same level)
; - if [W_LONEATTACKNO] != 0, one pokemon on the team has a special move
; else the first byte is the level of every pokemon on the team
.IterateTrainer
ld a,[hli]
cp $FF ; is the trainer special?
jr z,.SpecialTrainer ; if so, check for special moves
ld [W_CURENEMYLVL],a
.LoopTrainerData
ld a,[hli]
and a ; have we reached the end of the trainer data?
jr z,.FinishUp
ld [wcf91],a ; write species somewhere (XXX why?)
ld a,1
ld [wcc49],a
push hl
call AddPartyMon
pop hl
jr .LoopTrainerData
.SpecialTrainer
; if this code is being run:
; - each pokemon has a specific level
; (as opposed to the whole team being of the same level)
; - if [W_LONEATTACKNO] != 0, one pokemon on the team has a special move
ld a,[hli]
and a ; have we reached the end of the trainer data?
jr z,.AddLoneMove
ld [W_CURENEMYLVL],a
ld a,[hli]
ld [wcf91],a
ld a,1
ld [wcc49],a
push hl
call AddPartyMon
pop hl
jr .SpecialTrainer
.AddLoneMove
; does the trainer have a single monster with a different move
ld a,[W_LONEATTACKNO] ; Brock is 01, Misty is 02, Erika is 04, etc
and a
jr z,.AddTeamMove
dec a
add a,a
ld c,a
ld b,0
ld hl,LoneMoves
add hl,bc
ld a,[hli]
ld d,[hl]
ld hl,wEnemyMon1Moves + 2
ld bc,wEnemyMon2 - wEnemyMon1
call AddNTimes
ld [hl],d
jr .FinishUp
.AddTeamMove
; check if our trainer's team has special moves
; get trainer class number
ld a,[W_CUROPPONENT]
sub $C8
ld b,a
ld hl,TeamMoves
; iterate through entries in TeamMoves, checking each for our trainer class
.IterateTeamMoves
ld a,[hli]
cp b
jr z,.GiveTeamMoves ; is there a match?
inc hl ; if not, go to the next entry
inc a
jr nz,.IterateTeamMoves
; no matches found. is this trainer champion rival?
ld a,b
cp SONY3
jr z,.ChampionRival
jr .FinishUp ; nope
.GiveTeamMoves
ld a,[hl]
ld [wEnemyMon5Moves + 2],a
jr .FinishUp
.ChampionRival ; give moves to his team
; pidgeot
ld a,SKY_ATTACK
ld [wEnemyMon1Moves + 2],a
; starter
ld a,[W_RIVALSTARTER]
cp STARTER3
ld b,MEGA_DRAIN
jr z,.GiveStarterMove
cp STARTER1
ld b,FIRE_BLAST
jr z,.GiveStarterMove
ld b,BLIZZARD ; must be squirtle
.GiveStarterMove
ld a,b
ld [wEnemyMon6Moves + 2],a
.FinishUp ; XXX this needs documenting
xor a ; clear D079-D07B
ld de,wd079
ld [de],a
inc de
ld [de],a
inc de
ld [de],a
ld a,[W_CURENEMYLVL]
ld b,a
.LastLoop
ld hl,wd047
ld c,2
push bc
predef AddBCDPredef
pop bc
inc de
inc de
dec b
jr nz,.LastLoop
ret
INCLUDE "data/trainer_moves.asm"
INCLUDE "data/trainer_parties.asm"
TrainerAI: ; 3a52e (e:652e)
;XXX called at 34964, 3c342, 3c398
and a
ld a,[W_ISINBATTLE]
dec a
ret z ; if not a trainer, we're done here
ld a,[wLinkState]
cp LINK_STATE_BATTLING
ret z
ld a,[W_TRAINERCLASS] ; what trainer class is this?
dec a
ld c,a
ld b,0
ld hl,TrainerAIPointers
add hl,bc
add hl,bc
add hl,bc
ld a,[wAICount]
and a
ret z ; if no AI uses left, we're done here
inc hl
inc a
jr nz,.getpointer
dec hl
ld a,[hli]
ld [wAICount],a
.getpointer
ld a,[hli]
ld h,[hl]
ld l,a
call Random
jp [hl]
TrainerAIPointers: ; 3a55c (e:655c)
; one entry per trainer class
; first byte, number of times (per Pokémon) it can occur
; next two bytes, pointer to AI subroutine for trainer class
dbw 3,GenericAI
dbw 3,GenericAI
dbw 3,GenericAI
dbw 3,GenericAI
dbw 3,GenericAI
dbw 3,GenericAI
dbw 3,GenericAI
dbw 3,GenericAI
dbw 3,GenericAI
dbw 3,GenericAI
dbw 3,GenericAI
dbw 3,GenericAI
dbw 3,JugglerAI ; juggler_x
dbw 3,GenericAI
dbw 3,GenericAI
dbw 3,GenericAI
dbw 3,GenericAI
dbw 3,GenericAI
dbw 3,GenericAI
dbw 3,GenericAI
dbw 3,JugglerAI ; juggler
dbw 3,GenericAI
dbw 3,GenericAI
dbw 2,BlackbeltAI ; blackbelt
dbw 3,GenericAI
dbw 3,GenericAI
dbw 1,GenericAI ; chief
dbw 3,GenericAI
dbw 1,GiovanniAI ; giovanni
dbw 3,GenericAI
dbw 2,CooltrainerMAI ; cooltrainerm
dbw 1,CooltrainerFAI ; cooltrainerf
dbw 2,BrunoAI ; bruno
dbw 5,BrockAI ; brock
dbw 1,MistyAI ; misty
dbw 1,LtSurgeAI ; surge
dbw 1,ErikaAI ; erika
dbw 2,KogaAI ; koga
dbw 2,BlaineAI ; blaine
dbw 1,SabrinaAI ; sabrina
dbw 3,GenericAI
dbw 1,Sony2AI ; sony2
dbw 1,Sony3AI ; sony3
dbw 2,LoreleiAI ; lorelei
dbw 3,GenericAI
dbw 2,AgathaAI ; agatha
dbw 1,LanceAI ; lance
JugglerAI: ; 3a5e9 (e:65e9)
cp $40
ret nc
jp Func_3a72a
BlackbeltAI: ; 3a5ef (e:65ef)
cp $20
ret nc
jp AIUseXAttack
GiovanniAI: ; 3a5f5 (e:65f5)
cp $40
ret nc
jp AIUseGuardSpec
CooltrainerMAI: ; 3a5fb (e:65fb)
cp $40
ret nc
jp AIUseXAttack
CooltrainerFAI: ; 3a601 (e:6601)
cp $40
ld a,$A
call Func_3a7cf
jp c,AIUseHyperPotion
ld a,5
call Func_3a7cf
ret nc
jp Func_3a72a
BrockAI: ; 3a614 (e:6614)
; if his active monster has a status condition, use a full heal
ld a,[wEnemyMonStatus]
and a
ret z
jp AIUseFullHeal
MistyAI: ; 3a61c (e:661c)
cp $40
ret nc
jp AIUseXDefend
LtSurgeAI: ; 3a622 (e:6622)
cp $40
ret nc
jp AIUseXSpeed
ErikaAI: ; 3a628 (e:6628)
cp $80
ret nc
ld a,$A
call Func_3a7cf
ret nc
jp AIUseSuperPotion
KogaAI: ; 3a634 (e:6634)
cp $40
ret nc
jp AIUseXAttack
BlaineAI: ; 3a63a (e:663a)
cp $40
ret nc
jp AIUseSuperPotion
SabrinaAI: ; 3a640 (e:6640)
cp $40
ret nc
ld a,$A
call Func_3a7cf
ret nc
jp AIUseHyperPotion
Sony2AI: ; 3a64c (e:664c)
cp $20
ret nc
ld a,5
call Func_3a7cf
ret nc
jp AIUsePotion
Sony3AI: ; 3a658 (e:6658)
cp $20
ret nc
ld a,5
call Func_3a7cf
ret nc
jp AIUseFullRestore
LoreleiAI: ; 3a664 (e:6664)
cp $80
ret nc
ld a,5
call Func_3a7cf
ret nc
jp AIUseSuperPotion
BrunoAI: ; 3a670 (e:6670)
cp $40
ret nc
jp AIUseXDefend
AgathaAI: ; 3a676 (e:6676)
cp $14
jp c,Func_3a72a
cp $80
ret nc
ld a,4
call Func_3a7cf
ret nc
jp AIUseSuperPotion
LanceAI: ; 3a687 (e:6687)
cp $80
ret nc
ld a,5
call Func_3a7cf
ret nc
jp AIUseHyperPotion
GenericAI: ; 3a693 (e:6693)
and a ; clear carry
ret
; end of individual trainer AI routines
DecrementAICount: ; 3a695 (e:6695)
ld hl,wAICount
dec [hl]
scf
ret
Func_3a69b: ; 3a69b (e:669b)
ld a,(SFX_08_3e - SFX_Headers_08) / 3
jp PlaySoundWaitForCurrent
AIUseFullRestore: ; 3a6a0 (e:66a0)
call AICureStatus
ld a,FULL_RESTORE
ld [wcf05],a
ld de,wHPBarOldHP
ld hl,wEnemyMonHP + 1
ld a,[hld]
ld [de],a
inc de
ld a,[hl]
ld [de],a
inc de
ld hl,wEnemyMonMaxHP + 1
ld a,[hld]
ld [de],a
inc de
ld [wHPBarMaxHP],a
ld [wEnemyMonHP + 1],a
ld a,[hl]
ld [de],a
ld [wHPBarMaxHP+1],a
ld [wEnemyMonHP],a
jr Func_3a718
AIUsePotion: ; 3a6ca (e:66ca)
; enemy trainer heals his monster with a potion
ld a,POTION
ld b,20
jr AIRecoverHP
AIUseSuperPotion: ; 3a6d0 (e:66d0)
; enemy trainer heals his monster with a super potion
ld a,SUPER_POTION
ld b,50
jr AIRecoverHP
AIUseHyperPotion: ; 3a6d6 (e:66d6)
; enemy trainer heals his monster with a hyper potion
ld a,HYPER_POTION
ld b,200
; fallthrough
AIRecoverHP: ; 3a6da (e:66da)
; heal b HP and print "trainer used $(a) on pokemon!"
ld [wcf05],a
ld hl,wEnemyMonHP + 1
ld a,[hl]
ld [wHPBarOldHP],a
add b
ld [hld],a
ld [wHPBarNewHP],a
ld a,[hl]
ld [wHPBarOldHP+1],a
ld [wHPBarNewHP+1],a
jr nc,.next
inc a
ld [hl],a
ld [wHPBarNewHP+1],a
.next
inc hl
ld a,[hld]
ld b,a
ld de,wEnemyMonMaxHP + 1
ld a,[de]
dec de
ld [wHPBarMaxHP],a
sub b
ld a,[hli]
ld b,a
ld a,[de]
ld [wHPBarMaxHP+1],a
sbc b
jr nc,Func_3a718
inc de
ld a,[de]
dec de
ld [hld],a
ld [wHPBarNewHP],a
ld a,[de]
ld [hl],a
ld [wHPBarNewHP+1],a
; fallthrough
Func_3a718: ; 3a718 (e:6718)
call AIPrintItemUse_
hlCoord 2, 2
xor a
ld [wListMenuID],a
predef UpdateHPBar2
jp DecrementAICount
Func_3a72a: ; 3a72a (e:672a)
ld a,[wEnemyPartyCount]
ld c,a
ld hl,wEnemyMon1HP
ld d,0 ; keep count of unfainted monsters
; count how many monsters haven't fainted yet
.loop
ld a,[hli]
ld b,a
ld a,[hld]
or b
jr z,.Fainted ; has monster fainted?
inc d
.Fainted
push bc
ld bc,$2C
add hl,bc
pop bc
dec c
jr nz,.loop
ld a,d ; how many available monsters are there?
cp 2 ; don't bother if only 1 or 2
jp nc,SwitchEnemyMon
and a
ret
SwitchEnemyMon: ; 3a74b (e:674b)
; prepare to withdraw the active monster: copy hp, number, and status to roster
ld a,[wEnemyMonPartyPos]
ld hl,wEnemyMon1HP
ld bc,wEnemyMon2 - wEnemyMon1
call AddNTimes
ld d,h
ld e,l
ld hl,wEnemyMonHP
ld bc,4
call CopyData
ld hl, AIBattleWithdrawText
call PrintText
ld a,1
ld [wd11d],a
callab EnemySendOut
xor a
ld [wd11d],a
ld a,[wLinkState]
cp LINK_STATE_BATTLING
ret z
scf
ret
AIBattleWithdrawText: ; 3a781 (e:6781)
TX_FAR _AIBattleWithdrawText
db "@"
AIUseFullHeal: ; 3a786 (e:6786)
call Func_3a69b
call AICureStatus
ld a,FULL_HEAL
jp AIPrintItemUse
AICureStatus: ; 3a791 (e:6791)
; cures the status of enemy's active pokemon
ld a,[wEnemyMonPartyPos]
ld hl,wEnemyMon1Status
ld bc,wEnemyMon2 - wEnemyMon1
call AddNTimes
xor a
ld [hl],a ; clear status in enemy team roster
ld [wEnemyMonStatus],a ; clear status of active enemy
ld hl,W_ENEMYBATTSTATUS3
res 0,[hl]
ret
AIUseXAccuracy: ; 0x3a7a8 unused
call Func_3a69b
ld hl,W_ENEMYBATTSTATUS2
set 0,[hl]
ld a,X_ACCURACY
jp AIPrintItemUse
AIUseGuardSpec: ; 3a7b5 (e:67b5)
call Func_3a69b
ld hl,W_ENEMYBATTSTATUS2
set 1,[hl]
ld a,GUARD_SPEC_
jp AIPrintItemUse
AIUseDireHit: ; 0x3a7c2 unused
call Func_3a69b
ld hl,W_ENEMYBATTSTATUS2
set 2,[hl]
ld a,DIRE_HIT
jp AIPrintItemUse
Func_3a7cf: ; 3a7cf (e:67cf)
ld [H_DIVISOR],a
ld hl,wEnemyMonMaxHP
ld a,[hli]
ld [H_DIVIDEND],a
ld a,[hl]
ld [H_DIVIDEND + 1],a
ld b,2
call Divide
ld a,[H_QUOTIENT + 3]
ld c,a
ld a,[H_QUOTIENT + 2]
ld b,a
ld hl,wEnemyMonHP + 1
ld a,[hld]
ld e,a
ld a,[hl]
ld d,a
ld a,d
sub b
ret nz
ld a,e
sub c
ret
AIUseXAttack: ; 3a7f2 (e:67f2)
ld b,$A
ld a,X_ATTACK
jr AIIncreaseStat
AIUseXDefend: ; 3a7f8 (e:67f8)
ld b,$B
ld a,X_DEFEND
jr AIIncreaseStat
AIUseXSpeed: ; 3a7fe (e:67fe)
ld b,$C
ld a,X_SPEED
jr AIIncreaseStat
AIUseXSpecial: ; 3a804 (e:6804)
ld b,$D
ld a,X_SPECIAL
; fallthrough
AIIncreaseStat: ; 3a808 (e:6808)
ld [wcf05],a
push bc
call AIPrintItemUse_
pop bc
ld hl,W_ENEMYMOVEEFFECT
ld a,[hld]
push af
ld a,[hl]
push af
push hl
ld a,$AF
ld [hli],a
ld [hl],b
callab StatModifierUpEffect
pop hl
pop af
ld [hli],a
pop af
ld [hl],a
jp DecrementAICount
AIPrintItemUse: ; 3a82c (e:682c)
ld [wcf05],a
call AIPrintItemUse_
jp DecrementAICount
AIPrintItemUse_: ; 3a835 (e:6835)
; print "x used [wcf05] on z!"
ld a,[wcf05]
ld [wd11e],a
call GetItemName
ld hl, AIBattleUseItemText
jp PrintText
AIBattleUseItemText: ; 3a844 (e:6844)
TX_FAR _AIBattleUseItemText
db "@"
DrawAllPokeballs: ; 3a849 (e:6849)
call LoadPartyPokeballGfx
call SetupOwnPartyPokeballs
ld a, [W_ISINBATTLE] ; W_ISINBATTLE
dec a
ret z ; return if wild pokémon
jp SetupEnemyPartyPokeballs
DrawEnemyPokeballs: ; 0x3a857
call LoadPartyPokeballGfx
jp SetupEnemyPartyPokeballs
LoadPartyPokeballGfx: ; 3a85d (e:685d)
ld de, PokeballTileGraphics ; $697e
ld hl, vSprites + $310
ld bc, (BANK(PokeballTileGraphics) << 8) + $04
jp CopyVideoData
SetupOwnPartyPokeballs: ; 3a869 (e:6869)
call PlacePlayerHUDTiles
ld hl, wPartyMon1
ld de, wPartyCount ; wPartyCount
call SetupPokeballs
ld a, $60
ld hl, W_BASECOORDX ; wd081
ld [hli], a
ld [hl], a
ld a, $8
ld [wTrainerEngageDistance], a
ld hl, wOAMBuffer
jp Func_3a8e1
SetupEnemyPartyPokeballs: ; 3a887 (e:6887)
call PlaceEnemyHUDTiles
ld hl, wEnemyMons
ld de, wEnemyPartyCount ; wEnemyPartyCount
call SetupPokeballs
ld hl, W_BASECOORDX ; wd081
ld a, $48
ld [hli], a
ld [hl], $20
ld a, $f8
ld [wTrainerEngageDistance], a
ld hl, wOAMBuffer + PARTY_LENGTH * 4
jp Func_3a8e1
SetupPokeballs: ; 0x3a8a6
ld a, [de]
push af
ld de, wBuffer
ld c, PARTY_LENGTH
ld a, $34 ; empty pokeball
.emptyloop
ld [de], a
inc de
dec c
jr nz, .emptyloop ; 0x3a8b2 $fb
pop af
ld de, wBuffer
.monloop
push af
call PickPokeball
inc de
pop af
dec a
jr nz, .monloop
ret
PickPokeball: ; 3a8c2 (e:68c2)
inc hl
ld a, [hli]
and a
jr nz, .alive
ld a, [hl]
and a
ld b, $33 ; crossed ball (fainted)
jr z, .done_fainted
.alive
inc hl
inc hl
ld a, [hl] ; status
and a
ld b, $32 ; black ball (status)
jr nz, .done
dec b ; regular ball
jr .done
.done_fainted
inc hl
inc hl
.done
ld a, b
ld [de], a
ld bc, $0028 ; rest of mon struct
add hl, bc
ret
Func_3a8e1: ; 3a8e1 (e:68e1)
ld de, wHPBarMaxHP
ld c, PARTY_LENGTH
.asm_3a8e6
ld a, [W_BASECOORDY] ; wd082
ld [hli], a
ld a, [W_BASECOORDX] ; wd081
ld [hli], a
ld a, [de]
ld [hli], a
xor a
ld [hli], a
ld a, [W_BASECOORDX] ; wd081
ld b, a
ld a, [wTrainerEngageDistance]
add b
ld [W_BASECOORDX], a ; wd081
inc de
dec c
jr nz, .asm_3a8e6
ret
PlacePlayerHUDTiles: ; 3a902 (e:6902)
ld hl, PlayerBattleHUDGraphicsTiles ; $6916
ld de, wTrainerFacingDirection
ld bc, $3
call CopyData
hlCoord 18, 10
ld de, rIE ; $ffff
jr PlaceHUDTiles
PlayerBattleHUDGraphicsTiles: ; 3a916 (e:6916)
; The tile numbers for specific parts of the battle display for the player's pokemon
db $73 ; unused ($73 is hardcoded into the routine that uses these bytes)
db $77 ; lower-right corner tile of the HUD
db $6F ; lower-left triangle tile of the HUD
PlaceEnemyHUDTiles: ; 3a919 (e:6919)
ld hl, EnemyBattleHUDGraphicsTiles ; $692d
ld de, wTrainerFacingDirection
ld bc, $3
call CopyData
hlCoord 1, 2
ld de, $1
jr PlaceHUDTiles
EnemyBattleHUDGraphicsTiles: ; 3a92d (e:692d)
; The tile numbers for specific parts of the battle display for the enemy
db $73 ; unused ($73 is hardcoded in the routine that uses these bytes)
db $74 ; lower-left corner tile of the HUD
db $78 ; lower-right triangle tile of the HUD
PlaceHUDTiles: ; 3a930 (e:6930)
ld [hl], $73
ld bc, $14
add hl, bc
ld a, [wTrainerScreenY]
ld [hl], a
ld a, $8
.asm_3a93c
add hl, de
ld [hl], $76
dec a
jr nz, .asm_3a93c
add hl, de
ld a, [wTrainerScreenX]
ld [hl], a
ret
SetupPlayerAndEnemyPokeballs: ; 3a948 (e:6948)
call LoadPartyPokeballGfx
ld hl, wPartyMon1Species ; wPartyMon1Species (aliases: wPartyMon1)
ld de, wPartyCount ; wPartyCount
call SetupPokeballs
ld hl, W_BASECOORDX ; wd081
ld a, $50
ld [hli], a
ld [hl], $40
ld a, $8
ld [wTrainerEngageDistance], a
ld hl, wOAMBuffer
call Func_3a8e1
ld hl, wEnemyMons ; wEnemyMon1Species
ld de, wEnemyPartyCount ; wEnemyPartyCount
call SetupPokeballs
ld hl, W_BASECOORDX ; wd081
ld a, $50
ld [hli], a
ld [hl], $68
ld hl, wOAMBuffer + $18
jp Func_3a8e1
; four tiles: pokeball, black pokeball (status ailment), crossed out pokeball (faited) and pokeball slot (no mon)
PokeballTileGraphics:: ; 3a97e (e:697e)
INCBIN "gfx/pokeball.2bpp"
|