forked from danenders/pkedx
-
Notifications
You must be signed in to change notification settings - Fork 1
/
battle_ai_script_commands.c
2276 lines (1963 loc) · 65.5 KB
/
battle_ai_script_commands.c
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
#include "global.h"
#include "battle.h"
#include "battle_anim.h"
#include "battle_ai_script_commands.h"
#include "battle_factory.h"
#include "battle_setup.h"
#include "data.h"
#include "item.h"
#include "pokemon.h"
#include "random.h"
#include "recorded_battle.h"
#include "util.h"
#include "constants/abilities.h"
#include "constants/battle_ai.h"
#include "constants/battle_move_effects.h"
#include "constants/moves.h"
#define AI_ACTION_DONE 0x0001
#define AI_ACTION_FLEE 0x0002
#define AI_ACTION_WATCH 0x0004
#define AI_ACTION_DO_NOT_ATTACK 0x0008
#define AI_ACTION_UNK5 0x0010
#define AI_ACTION_UNK6 0x0020
#define AI_ACTION_UNK7 0x0040
#define AI_ACTION_UNK8 0x0080
#define AI_THINKING_STRUCT ((struct AI_ThinkingStruct *)(gBattleResources->ai))
#define BATTLE_HISTORY ((struct BattleHistory *)(gBattleResources->battleHistory))
// AI states
enum
{
AIState_SettingUp,
AIState_Processing,
AIState_FinishedProcessing,
AIState_DoNotProcess
};
/*
gAIScriptPtr is a pointer to the next battle AI cmd command to read.
when a command finishes processing, gAIScriptPtr is incremented by
the number of bytes that the current command had reserved for arguments
in order to read the next command correctly. refer to battle_ai_scripts.s for the
AI scripts.
*/
extern const u8 *const gBattleAI_ScriptsTable[];
static u8 ChooseMoveOrAction_Singles(void);
static u8 ChooseMoveOrAction_Doubles(void);
static void RecordLastUsedMoveByTarget(void);
static void BattleAI_DoAIProcessing(void);
static void AIStackPushVar(const u8 *);
static bool8 AIStackPop(void);
static void Cmd_if_random_less_than(void);
static void Cmd_if_random_greater_than(void);
static void Cmd_if_random_equal(void);
static void Cmd_if_random_not_equal(void);
static void Cmd_score(void);
static void Cmd_if_hp_less_than(void);
static void Cmd_if_hp_more_than(void);
static void Cmd_if_hp_equal(void);
static void Cmd_if_hp_not_equal(void);
static void Cmd_if_status(void);
static void Cmd_if_not_status(void);
static void Cmd_if_status2(void);
static void Cmd_if_not_status2(void);
static void Cmd_if_status3(void);
static void Cmd_if_not_status3(void);
static void Cmd_if_side_affecting(void);
static void Cmd_if_not_side_affecting(void);
static void Cmd_if_less_than(void);
static void Cmd_if_more_than(void);
static void Cmd_if_equal(void);
static void Cmd_if_not_equal(void);
static void Cmd_if_less_than_ptr(void);
static void Cmd_if_more_than_ptr(void);
static void Cmd_if_equal_ptr(void);
static void Cmd_if_not_equal_ptr(void);
static void Cmd_if_move(void);
static void Cmd_if_not_move(void);
static void Cmd_if_in_bytes(void);
static void Cmd_if_not_in_bytes(void);
static void Cmd_if_in_hwords(void);
static void Cmd_if_not_in_hwords(void);
static void Cmd_if_user_has_attacking_move(void);
static void Cmd_if_user_has_no_attacking_moves(void);
static void Cmd_get_turn_count(void);
static void Cmd_get_type(void);
static void Cmd_get_considered_move_power(void);
static void Cmd_get_how_powerful_move_is(void);
static void Cmd_get_last_used_battler_move(void);
static void Cmd_if_equal_(void);
static void Cmd_if_not_equal_(void);
static void Cmd_if_user_goes(void);
static void Cmd_if_user_doesnt_go(void);
static void Cmd_nop_2A(void);
static void Cmd_nop_2B(void);
static void Cmd_count_usable_party_mons(void);
static void Cmd_get_considered_move(void);
static void Cmd_get_considered_move_effect(void);
static void Cmd_get_ability(void);
static void Cmd_get_highest_type_effectiveness(void);
static void Cmd_if_type_effectiveness(void);
static void Cmd_nop_32(void);
static void Cmd_nop_33(void);
static void Cmd_if_status_in_party(void);
static void Cmd_if_status_not_in_party(void);
static void Cmd_get_weather(void);
static void Cmd_if_effect(void);
static void Cmd_if_not_effect(void);
static void Cmd_if_stat_level_less_than(void);
static void Cmd_if_stat_level_more_than(void);
static void Cmd_if_stat_level_equal(void);
static void Cmd_if_stat_level_not_equal(void);
static void Cmd_if_can_faint(void);
static void Cmd_if_cant_faint(void);
static void Cmd_if_has_move(void);
static void Cmd_if_doesnt_have_move(void);
static void Cmd_if_has_move_with_effect(void);
static void Cmd_if_doesnt_have_move_with_effect(void);
static void Cmd_if_any_move_disabled_or_encored(void);
static void Cmd_if_curr_move_disabled_or_encored(void);
static void Cmd_flee(void);
static void Cmd_if_random_safari_flee(void);
static void Cmd_watch(void);
static void Cmd_get_hold_effect(void);
static void Cmd_get_gender(void);
static void Cmd_is_first_turn_for(void);
static void Cmd_get_stockpile_count(void);
static void Cmd_is_double_battle(void);
static void Cmd_get_used_held_item(void);
static void Cmd_get_move_type_from_result(void);
static void Cmd_get_move_power_from_result(void);
static void Cmd_get_move_effect_from_result(void);
static void Cmd_get_protect_count(void);
static void Cmd_nop_52(void);
static void Cmd_nop_53(void);
static void Cmd_nop_54(void);
static void Cmd_nop_55(void);
static void Cmd_nop_56(void);
static void Cmd_nop_57(void);
static void Cmd_call(void);
static void Cmd_goto(void);
static void Cmd_end(void);
static void Cmd_if_level_cond(void);
static void Cmd_if_target_taunted(void);
static void Cmd_if_target_not_taunted(void);
static void Cmd_check_ability(void);
static void Cmd_is_of_type(void);
static void Cmd_if_target_is_ally(void);
static void Cmd_if_flash_fired(void);
static void Cmd_if_holds_item(void);
// ewram
EWRAM_DATA const u8 *gAIScriptPtr = NULL;
EWRAM_DATA static u8 sBattler_AI = 0;
// const rom data
typedef void (*BattleAICmdFunc)(void);
static const BattleAICmdFunc sBattleAICmdTable[] =
{
Cmd_if_random_less_than, // 0x0
Cmd_if_random_greater_than, // 0x1
Cmd_if_random_equal, // 0x2
Cmd_if_random_not_equal, // 0x3
Cmd_score, // 0x4
Cmd_if_hp_less_than, // 0x5
Cmd_if_hp_more_than, // 0x6
Cmd_if_hp_equal, // 0x7
Cmd_if_hp_not_equal, // 0x8
Cmd_if_status, // 0x9
Cmd_if_not_status, // 0xA
Cmd_if_status2, // 0xB
Cmd_if_not_status2, // 0xC
Cmd_if_status3, // 0xD
Cmd_if_not_status3, // 0xE
Cmd_if_side_affecting, // 0xF
Cmd_if_not_side_affecting, // 0x10
Cmd_if_less_than, // 0x11
Cmd_if_more_than, // 0x12
Cmd_if_equal, // 0x13
Cmd_if_not_equal, // 0x14
Cmd_if_less_than_ptr, // 0x15
Cmd_if_more_than_ptr, // 0x16
Cmd_if_equal_ptr, // 0x17
Cmd_if_not_equal_ptr, // 0x18
Cmd_if_move, // 0x19
Cmd_if_not_move, // 0x1A
Cmd_if_in_bytes, // 0x1B
Cmd_if_not_in_bytes, // 0x1C
Cmd_if_in_hwords, // 0x1D
Cmd_if_not_in_hwords, // 0x1E
Cmd_if_user_has_attacking_move, // 0x1F
Cmd_if_user_has_no_attacking_moves, // 0x20
Cmd_get_turn_count, // 0x21
Cmd_get_type, // 0x22
Cmd_get_considered_move_power, // 0x23
Cmd_get_how_powerful_move_is, // 0x24
Cmd_get_last_used_battler_move, // 0x25
Cmd_if_equal_, // 0x26
Cmd_if_not_equal_, // 0x27
Cmd_if_user_goes, // 0x28
Cmd_if_user_doesnt_go, // 0x29
Cmd_nop_2A, // 0x2A
Cmd_nop_2B, // 0x2B
Cmd_count_usable_party_mons, // 0x2C
Cmd_get_considered_move, // 0x2D
Cmd_get_considered_move_effect, // 0x2E
Cmd_get_ability, // 0x2F
Cmd_get_highest_type_effectiveness, // 0x30
Cmd_if_type_effectiveness, // 0x31
Cmd_nop_32, // 0x32
Cmd_nop_33, // 0x33
Cmd_if_status_in_party, // 0x34
Cmd_if_status_not_in_party, // 0x35
Cmd_get_weather, // 0x36
Cmd_if_effect, // 0x37
Cmd_if_not_effect, // 0x38
Cmd_if_stat_level_less_than, // 0x39
Cmd_if_stat_level_more_than, // 0x3A
Cmd_if_stat_level_equal, // 0x3B
Cmd_if_stat_level_not_equal, // 0x3C
Cmd_if_can_faint, // 0x3D
Cmd_if_cant_faint, // 0x3E
Cmd_if_has_move, // 0x3F
Cmd_if_doesnt_have_move, // 0x40
Cmd_if_has_move_with_effect, // 0x41
Cmd_if_doesnt_have_move_with_effect, // 0x42
Cmd_if_any_move_disabled_or_encored, // 0x43
Cmd_if_curr_move_disabled_or_encored, // 0x44
Cmd_flee, // 0x45
Cmd_if_random_safari_flee, // 0x46
Cmd_watch, // 0x47
Cmd_get_hold_effect, // 0x48
Cmd_get_gender, // 0x49
Cmd_is_first_turn_for, // 0x4A
Cmd_get_stockpile_count, // 0x4B
Cmd_is_double_battle, // 0x4C
Cmd_get_used_held_item, // 0x4D
Cmd_get_move_type_from_result, // 0x4E
Cmd_get_move_power_from_result, // 0x4F
Cmd_get_move_effect_from_result, // 0x50
Cmd_get_protect_count, // 0x51
Cmd_nop_52, // 0x52
Cmd_nop_53, // 0x53
Cmd_nop_54, // 0x54
Cmd_nop_55, // 0x55
Cmd_nop_56, // 0x56
Cmd_nop_57, // 0x57
Cmd_call, // 0x58
Cmd_goto, // 0x59
Cmd_end, // 0x5A
Cmd_if_level_cond, // 0x5B
Cmd_if_target_taunted, // 0x5C
Cmd_if_target_not_taunted, // 0x5D
Cmd_if_target_is_ally, // 0x5E
Cmd_is_of_type, // 0x5F
Cmd_check_ability, // 0x60
Cmd_if_flash_fired, // 0x61
Cmd_if_holds_item, // 0x62
};
// For the purposes of determining the most powerful move in a moveset, these
// moves are treated the same as having a power of 0 or 1
#define IGNORED_MOVES_END 0xFFFF
static const u16 sIgnoredPowerfulMoveEffects[] =
{
EFFECT_EXPLOSION,
EFFECT_DREAM_EATER,
EFFECT_RAZOR_WIND,
EFFECT_SKY_ATTACK,
EFFECT_RECHARGE,
EFFECT_SKULL_BASH,
EFFECT_SOLAR_BEAM,
EFFECT_SPIT_UP,
EFFECT_FOCUS_PUNCH,
EFFECT_SUPERPOWER,
EFFECT_ERUPTION,
EFFECT_OVERHEAT,
IGNORED_MOVES_END
};
// code
void BattleAI_HandleItemUseBeforeAISetup(u8 defaultScoreMoves)
{
s32 i;
u8 *data = (u8 *)BATTLE_HISTORY;
for (i = 0; i < sizeof(struct BattleHistory); i++)
data[i] = 0;
// Items are allowed to use in ONLY trainer battles.
if ((gBattleTypeFlags & BATTLE_TYPE_TRAINER)
&& !(gBattleTypeFlags & (BATTLE_TYPE_LINK | BATTLE_TYPE_SAFARI | BATTLE_TYPE_BATTLE_TOWER
| BATTLE_TYPE_EREADER_TRAINER | BATTLE_TYPE_SECRET_BASE | BATTLE_TYPE_FRONTIER
| BATTLE_TYPE_INGAME_PARTNER | BATTLE_TYPE_RECORDED_LINK)
)
)
{
for (i = 0; i < MAX_TRAINER_ITEMS; i++)
{
if (gTrainers[gTrainerBattleOpponent_A].items[i] != 0)
{
BATTLE_HISTORY->trainerItems[BATTLE_HISTORY->itemsNo] = gTrainers[gTrainerBattleOpponent_A].items[i];
BATTLE_HISTORY->itemsNo++;
}
}
}
BattleAI_SetupAIData(defaultScoreMoves);
}
void BattleAI_SetupAIData(u8 defaultScoreMoves)
{
s32 i;
u8 *data = (u8 *)AI_THINKING_STRUCT;
u8 moveLimitations;
// Clear AI data.
for (i = 0; i < sizeof(struct AI_ThinkingStruct); i++)
data[i] = 0;
// Conditional score reset, unlike Ruby.
for (i = 0; i < MAX_MON_MOVES; i++)
{
if (defaultScoreMoves & 1)
AI_THINKING_STRUCT->score[i] = 100;
else
AI_THINKING_STRUCT->score[i] = 0;
defaultScoreMoves >>= 1;
}
moveLimitations = CheckMoveLimitations(gActiveBattler, 0, MOVE_LIMITATIONS_ALL);
// Ignore moves that aren't possible to use.
for (i = 0; i < MAX_MON_MOVES; i++)
{
if (gBitTable[i] & moveLimitations)
AI_THINKING_STRUCT->score[i] = 0;
AI_THINKING_STRUCT->simulatedRNG[i] = 100 - (Random() % 16);
}
gBattleResources->AI_ScriptsStack->size = 0;
sBattler_AI = gActiveBattler;
// Decide a random target battlerId in doubles.
if (gBattleTypeFlags & BATTLE_TYPE_DOUBLE)
{
gBattlerTarget = (Random() & BIT_FLANK) + (GetBattlerSide(gActiveBattler) ^ BIT_SIDE);
if (gAbsentBattlerFlags & gBitTable[gBattlerTarget])
gBattlerTarget ^= BIT_FLANK;
}
// There's only one choice in single battles.
else
{
gBattlerTarget = sBattler_AI ^ BIT_SIDE;
}
// Choose proper trainer ai scripts.
if (gBattleTypeFlags & BATTLE_TYPE_RECORDED)
AI_THINKING_STRUCT->aiFlags = GetAiScriptsInRecordedBattle();
else if (gBattleTypeFlags & BATTLE_TYPE_SAFARI)
AI_THINKING_STRUCT->aiFlags = AI_SCRIPT_SAFARI;
else if (gBattleTypeFlags & BATTLE_TYPE_ROAMER)
AI_THINKING_STRUCT->aiFlags = AI_SCRIPT_ROAMING;
else if (gBattleTypeFlags & BATTLE_TYPE_FIRST_BATTLE)
AI_THINKING_STRUCT->aiFlags = AI_SCRIPT_FIRST_BATTLE;
else if (gBattleTypeFlags & BATTLE_TYPE_FACTORY)
AI_THINKING_STRUCT->aiFlags = GetAiScriptsInBattleFactory();
else if (gBattleTypeFlags & (BATTLE_TYPE_FRONTIER | BATTLE_TYPE_EREADER_TRAINER | BATTLE_TYPE_TRAINER_HILL | BATTLE_TYPE_SECRET_BASE))
AI_THINKING_STRUCT->aiFlags = AI_SCRIPT_CHECK_BAD_MOVE | AI_SCRIPT_CHECK_VIABILITY | AI_SCRIPT_TRY_TO_FAINT;
else if (gBattleTypeFlags & BATTLE_TYPE_TWO_OPPONENTS)
AI_THINKING_STRUCT->aiFlags = gTrainers[gTrainerBattleOpponent_A].aiFlags | gTrainers[gTrainerBattleOpponent_B].aiFlags;
else
AI_THINKING_STRUCT->aiFlags = gTrainers[gTrainerBattleOpponent_A].aiFlags;
if (gBattleTypeFlags & BATTLE_TYPE_DOUBLE)
AI_THINKING_STRUCT->aiFlags |= AI_SCRIPT_DOUBLE_BATTLE; // act smart in doubles and don't attack your partner
}
u8 BattleAI_ChooseMoveOrAction(void)
{
u16 savedCurrentMove = gCurrentMove;
u8 ret;
if (!(gBattleTypeFlags & BATTLE_TYPE_DOUBLE))
ret = ChooseMoveOrAction_Singles();
else
ret = ChooseMoveOrAction_Doubles();
gCurrentMove = savedCurrentMove;
return ret;
}
static u8 ChooseMoveOrAction_Singles(void)
{
u8 currentMoveArray[MAX_MON_MOVES];
u8 consideredMoveArray[MAX_MON_MOVES];
u8 numOfBestMoves;
s32 i;
RecordLastUsedMoveByTarget();
while (AI_THINKING_STRUCT->aiFlags != 0)
{
if (AI_THINKING_STRUCT->aiFlags & 1)
{
AI_THINKING_STRUCT->aiState = AIState_SettingUp;
BattleAI_DoAIProcessing();
}
AI_THINKING_STRUCT->aiFlags >>= 1;
AI_THINKING_STRUCT->aiLogicId++;
AI_THINKING_STRUCT->movesetIndex = 0;
}
// Check special AI actions.
if (AI_THINKING_STRUCT->aiAction & AI_ACTION_FLEE)
return AI_CHOICE_FLEE;
if (AI_THINKING_STRUCT->aiAction & AI_ACTION_WATCH)
return AI_CHOICE_WATCH;
numOfBestMoves = 1;
currentMoveArray[0] = AI_THINKING_STRUCT->score[0];
consideredMoveArray[0] = 0;
for (i = 1; i < MAX_MON_MOVES; i++)
{
if (gBattleMons[sBattler_AI].moves[i] != MOVE_NONE)
{
// In ruby, the order of these if statements is reversed.
if (currentMoveArray[0] == AI_THINKING_STRUCT->score[i])
{
currentMoveArray[numOfBestMoves] = AI_THINKING_STRUCT->score[i];
consideredMoveArray[numOfBestMoves++] = i;
}
if (currentMoveArray[0] < AI_THINKING_STRUCT->score[i])
{
numOfBestMoves = 1;
currentMoveArray[0] = AI_THINKING_STRUCT->score[i];
consideredMoveArray[0] = i;
}
}
}
return consideredMoveArray[Random() % numOfBestMoves];
}
static u8 ChooseMoveOrAction_Doubles(void)
{
s32 i;
s32 j;
#ifndef BUGFIX
s32 scriptsToRun;
#else
// the value assigned to this is a u32 (aiFlags)
// this becomes relevant because aiFlags can have bit 31 set
// and scriptsToRun is shifted
// this never happens in the vanilla game because bit 31 is
// only set when it's the first battle
u32 scriptsToRun;
#endif
s16 bestMovePointsForTarget[MAX_BATTLERS_COUNT];
s8 mostViableTargetsArray[MAX_BATTLERS_COUNT];
u8 actionOrMoveIndex[MAX_BATTLERS_COUNT];
u8 mostViableMovesScores[MAX_MON_MOVES];
u8 mostViableMovesIndices[MAX_MON_MOVES];
s32 mostViableTargetsNo;
s32 mostViableMovesNo;
s16 mostMovePoints;
for (i = 0; i < MAX_BATTLERS_COUNT; i++)
{
if (i == sBattler_AI || gBattleMons[i].hp == 0)
{
actionOrMoveIndex[i] = 0xFF;
bestMovePointsForTarget[i] = -1;
}
else
{
if (gBattleTypeFlags & BATTLE_TYPE_PALACE)
BattleAI_SetupAIData(gBattleStruct->palaceFlags >> 4);
else
BattleAI_SetupAIData((1 << MAX_MON_MOVES) - 1);
gBattlerTarget = i;
if ((i & BIT_SIDE) != (sBattler_AI & BIT_SIDE))
RecordLastUsedMoveByTarget();
AI_THINKING_STRUCT->aiLogicId = 0;
AI_THINKING_STRUCT->movesetIndex = 0;
scriptsToRun = AI_THINKING_STRUCT->aiFlags;
while (scriptsToRun != 0)
{
if (scriptsToRun & 1)
{
AI_THINKING_STRUCT->aiState = AIState_SettingUp;
BattleAI_DoAIProcessing();
}
scriptsToRun >>= 1;
AI_THINKING_STRUCT->aiLogicId++;
AI_THINKING_STRUCT->movesetIndex = 0;
}
if (AI_THINKING_STRUCT->aiAction & AI_ACTION_FLEE)
{
actionOrMoveIndex[i] = AI_CHOICE_FLEE;
}
else if (AI_THINKING_STRUCT->aiAction & AI_ACTION_WATCH)
{
actionOrMoveIndex[i] = AI_CHOICE_WATCH;
}
else
{
mostViableMovesScores[0] = AI_THINKING_STRUCT->score[0];
mostViableMovesIndices[0] = 0;
mostViableMovesNo = 1;
for (j = 1; j < MAX_MON_MOVES; j++)
{
if (gBattleMons[sBattler_AI].moves[j] != 0)
{
if (mostViableMovesScores[0] == AI_THINKING_STRUCT->score[j])
{
mostViableMovesScores[mostViableMovesNo] = AI_THINKING_STRUCT->score[j];
mostViableMovesIndices[mostViableMovesNo] = j;
mostViableMovesNo++;
}
if (mostViableMovesScores[0] < AI_THINKING_STRUCT->score[j])
{
mostViableMovesScores[0] = AI_THINKING_STRUCT->score[j];
mostViableMovesIndices[0] = j;
mostViableMovesNo = 1;
}
}
}
actionOrMoveIndex[i] = mostViableMovesIndices[Random() % mostViableMovesNo];
bestMovePointsForTarget[i] = mostViableMovesScores[0];
// Don't use a move against ally if it has less than 100 points.
if (i == (sBattler_AI ^ BIT_FLANK) && bestMovePointsForTarget[i] < 100)
{
bestMovePointsForTarget[i] = -1;
mostViableMovesScores[0] = mostViableMovesScores[0]; // Needed to match.
}
}
}
}
mostMovePoints = bestMovePointsForTarget[0];
mostViableTargetsArray[0] = 0;
mostViableTargetsNo = 1;
for (i = 1; i < MAX_BATTLERS_COUNT; i++)
{
if (mostMovePoints == bestMovePointsForTarget[i])
{
mostViableTargetsArray[mostViableTargetsNo] = i;
mostViableTargetsNo++;
}
if (mostMovePoints < bestMovePointsForTarget[i])
{
mostMovePoints = bestMovePointsForTarget[i];
mostViableTargetsArray[0] = i;
mostViableTargetsNo = 1;
}
}
gBattlerTarget = mostViableTargetsArray[Random() % mostViableTargetsNo];
return actionOrMoveIndex[gBattlerTarget];
}
static void BattleAI_DoAIProcessing(void)
{
while (AI_THINKING_STRUCT->aiState != AIState_FinishedProcessing)
{
switch (AI_THINKING_STRUCT->aiState)
{
case AIState_DoNotProcess: // Needed to match.
break;
case AIState_SettingUp:
gAIScriptPtr = gBattleAI_ScriptsTable[AI_THINKING_STRUCT->aiLogicId]; // set AI ptr to logic ID.
if (gBattleMons[sBattler_AI].pp[AI_THINKING_STRUCT->movesetIndex] == 0)
{
AI_THINKING_STRUCT->moveConsidered = 0;
}
else
{
AI_THINKING_STRUCT->moveConsidered = gBattleMons[sBattler_AI].moves[AI_THINKING_STRUCT->movesetIndex];
}
AI_THINKING_STRUCT->aiState++;
break;
case AIState_Processing:
if (AI_THINKING_STRUCT->moveConsidered != 0)
{
sBattleAICmdTable[*gAIScriptPtr](); // Run AI command.
}
else
{
AI_THINKING_STRUCT->score[AI_THINKING_STRUCT->movesetIndex] = 0;
AI_THINKING_STRUCT->aiAction |= AI_ACTION_DONE;
}
if (AI_THINKING_STRUCT->aiAction & AI_ACTION_DONE)
{
AI_THINKING_STRUCT->movesetIndex++;
if (AI_THINKING_STRUCT->movesetIndex < MAX_MON_MOVES && !(AI_THINKING_STRUCT->aiAction & AI_ACTION_DO_NOT_ATTACK))
AI_THINKING_STRUCT->aiState = AIState_SettingUp;
else
AI_THINKING_STRUCT->aiState++;
AI_THINKING_STRUCT->aiAction &= ~(AI_ACTION_DONE);
}
break;
}
}
}
static void RecordLastUsedMoveByTarget(void)
{
s32 i;
for (i = 0; i < MAX_MON_MOVES; i++)
{
if (BATTLE_HISTORY->usedMoves[gBattlerTarget].moves[i] == gLastMoves[gBattlerTarget])
break;
if (BATTLE_HISTORY->usedMoves[gBattlerTarget].moves[i] == MOVE_NONE)
{
BATTLE_HISTORY->usedMoves[gBattlerTarget].moves[i] = gLastMoves[gBattlerTarget];
break;
}
}
}
void ClearBattlerMoveHistory(u8 battlerId)
{
s32 i;
for (i = 0; i < MAX_MON_MOVES; i++)
BATTLE_HISTORY->usedMoves[battlerId].moves[i] = MOVE_NONE;
}
void RecordAbilityBattle(u8 battlerId, u8 abilityId)
{
BATTLE_HISTORY->abilities[battlerId] = abilityId;
}
void ClearBattlerAbilityHistory(u8 battlerId)
{
BATTLE_HISTORY->abilities[battlerId] = ABILITY_NONE;
}
void RecordItemEffectBattle(u8 battlerId, u8 itemEffect)
{
BATTLE_HISTORY->itemEffects[battlerId] = itemEffect;
}
void ClearBattlerItemEffectHistory(u8 battlerId)
{
BATTLE_HISTORY->itemEffects[battlerId] = 0;
}
static void Cmd_if_random_less_than(void)
{
u16 random = Random();
if (random % 256 < gAIScriptPtr[1])
gAIScriptPtr = T1_READ_PTR(gAIScriptPtr + 2);
else
gAIScriptPtr += 6;
}
static void Cmd_if_random_greater_than(void)
{
u16 random = Random();
if (random % 256 > gAIScriptPtr[1])
gAIScriptPtr = T1_READ_PTR(gAIScriptPtr + 2);
else
gAIScriptPtr += 6;
}
static void Cmd_if_random_equal(void)
{
u16 random = Random();
if (random % 256 == gAIScriptPtr[1])
gAIScriptPtr = T1_READ_PTR(gAIScriptPtr + 2);
else
gAIScriptPtr += 6;
}
static void Cmd_if_random_not_equal(void)
{
u16 random = Random();
if (random % 256 != gAIScriptPtr[1])
gAIScriptPtr = T1_READ_PTR(gAIScriptPtr + 2);
else
gAIScriptPtr += 6;
}
static void Cmd_score(void)
{
AI_THINKING_STRUCT->score[AI_THINKING_STRUCT->movesetIndex] += gAIScriptPtr[1]; // Add the result to the array of the move consider's score.
if (AI_THINKING_STRUCT->score[AI_THINKING_STRUCT->movesetIndex] < 0) // If the score is negative, flatten it to 0.
AI_THINKING_STRUCT->score[AI_THINKING_STRUCT->movesetIndex] = 0;
gAIScriptPtr += 2; // AI return.
}
static void Cmd_if_hp_less_than(void)
{
u16 battlerId;
if (gAIScriptPtr[1] == AI_USER)
battlerId = sBattler_AI;
else
battlerId = gBattlerTarget;
if ((u32)(100 * gBattleMons[battlerId].hp / gBattleMons[battlerId].maxHP) < gAIScriptPtr[2])
gAIScriptPtr = T1_READ_PTR(gAIScriptPtr + 3);
else
gAIScriptPtr += 7;
}
static void Cmd_if_hp_more_than(void)
{
u16 battlerId;
if (gAIScriptPtr[1] == AI_USER)
battlerId = sBattler_AI;
else
battlerId = gBattlerTarget;
if ((u32)(100 * gBattleMons[battlerId].hp / gBattleMons[battlerId].maxHP) > gAIScriptPtr[2])
gAIScriptPtr = T1_READ_PTR(gAIScriptPtr + 3);
else
gAIScriptPtr += 7;
}
static void Cmd_if_hp_equal(void)
{
u16 battlerId;
if (gAIScriptPtr[1] == AI_USER)
battlerId = sBattler_AI;
else
battlerId = gBattlerTarget;
if ((u32)(100 * gBattleMons[battlerId].hp / gBattleMons[battlerId].maxHP) == gAIScriptPtr[2])
gAIScriptPtr = T1_READ_PTR(gAIScriptPtr + 3);
else
gAIScriptPtr += 7;
}
static void Cmd_if_hp_not_equal(void)
{
u16 battlerId;
if (gAIScriptPtr[1] == AI_USER)
battlerId = sBattler_AI;
else
battlerId = gBattlerTarget;
if ((u32)(100 * gBattleMons[battlerId].hp / gBattleMons[battlerId].maxHP) != gAIScriptPtr[2])
gAIScriptPtr = T1_READ_PTR(gAIScriptPtr + 3);
else
gAIScriptPtr += 7;
}
static void Cmd_if_status(void)
{
u16 battlerId;
u32 status;
if (gAIScriptPtr[1] == AI_USER)
battlerId = sBattler_AI;
else
battlerId = gBattlerTarget;
status = T1_READ_32(gAIScriptPtr + 2);
if (gBattleMons[battlerId].status1 & status)
gAIScriptPtr = T1_READ_PTR(gAIScriptPtr + 6);
else
gAIScriptPtr += 10;
}
static void Cmd_if_not_status(void)
{
u16 battlerId;
u32 status;
if (gAIScriptPtr[1] == AI_USER)
battlerId = sBattler_AI;
else
battlerId = gBattlerTarget;
status = T1_READ_32(gAIScriptPtr + 2);
if (!(gBattleMons[battlerId].status1 & status))
gAIScriptPtr = T1_READ_PTR(gAIScriptPtr + 6);
else
gAIScriptPtr += 10;
}
static void Cmd_if_status2(void)
{
u16 battlerId;
u32 status;
if (gAIScriptPtr[1] == AI_USER)
battlerId = sBattler_AI;
else
battlerId = gBattlerTarget;
status = T1_READ_32(gAIScriptPtr + 2);
if ((gBattleMons[battlerId].status2 & status))
gAIScriptPtr = T1_READ_PTR(gAIScriptPtr + 6);
else
gAIScriptPtr += 10;
}
static void Cmd_if_not_status2(void)
{
u16 battlerId;
u32 status;
if (gAIScriptPtr[1] == AI_USER)
battlerId = sBattler_AI;
else
battlerId = gBattlerTarget;
status = T1_READ_32(gAIScriptPtr + 2);
if (!(gBattleMons[battlerId].status2 & status))
gAIScriptPtr = T1_READ_PTR(gAIScriptPtr + 6);
else
gAIScriptPtr += 10;
}
static void Cmd_if_status3(void)
{
u16 battlerId;
u32 status;
if (gAIScriptPtr[1] == AI_USER)
battlerId = sBattler_AI;
else
battlerId = gBattlerTarget;
status = T1_READ_32(gAIScriptPtr + 2);
if (gStatuses3[battlerId] & status)
gAIScriptPtr = T1_READ_PTR(gAIScriptPtr + 6);
else
gAIScriptPtr += 10;
}
static void Cmd_if_not_status3(void)
{
u16 battlerId;
u32 status;
if (gAIScriptPtr[1] == AI_USER)
battlerId = sBattler_AI;
else
battlerId = gBattlerTarget;
status = T1_READ_32(gAIScriptPtr + 2);
if (!(gStatuses3[battlerId] & status))
gAIScriptPtr = T1_READ_PTR(gAIScriptPtr + 6);
else
gAIScriptPtr += 10;
}
static void Cmd_if_side_affecting(void)
{
u16 battlerId;
u32 side, status;
if (gAIScriptPtr[1] == AI_USER)
battlerId = sBattler_AI;
else
battlerId = gBattlerTarget;
side = GET_BATTLER_SIDE(battlerId);
status = T1_READ_32(gAIScriptPtr + 2);
if (gSideStatuses[side] & status)
gAIScriptPtr = T1_READ_PTR(gAIScriptPtr + 6);
else
gAIScriptPtr += 10;
}
static void Cmd_if_not_side_affecting(void)
{
u16 battlerId;
u32 side, status;
if (gAIScriptPtr[1] == AI_USER)
battlerId = sBattler_AI;
else
battlerId = gBattlerTarget;
side = GET_BATTLER_SIDE(battlerId);
status = T1_READ_32(gAIScriptPtr + 2);
if (!(gSideStatuses[side] & status))
gAIScriptPtr = T1_READ_PTR(gAIScriptPtr + 6);
else
gAIScriptPtr += 10;
}
static void Cmd_if_less_than(void)
{
if (AI_THINKING_STRUCT->funcResult < gAIScriptPtr[1])
gAIScriptPtr = T1_READ_PTR(gAIScriptPtr + 2);
else
gAIScriptPtr += 6;
}
static void Cmd_if_more_than(void)
{
if (AI_THINKING_STRUCT->funcResult > gAIScriptPtr[1])
gAIScriptPtr = T1_READ_PTR(gAIScriptPtr + 2);
else
gAIScriptPtr += 6;
}
static void Cmd_if_equal(void)
{
if (AI_THINKING_STRUCT->funcResult == gAIScriptPtr[1])
gAIScriptPtr = T1_READ_PTR(gAIScriptPtr + 2);
else
gAIScriptPtr += 6;
}
static void Cmd_if_not_equal(void)
{
if (AI_THINKING_STRUCT->funcResult != gAIScriptPtr[1])
gAIScriptPtr = T1_READ_PTR(gAIScriptPtr + 2);
else
gAIScriptPtr += 6;
}
static void Cmd_if_less_than_ptr(void)
{
const u8 *value = T1_READ_PTR(gAIScriptPtr + 1);
if (AI_THINKING_STRUCT->funcResult < *value)
gAIScriptPtr = T1_READ_PTR(gAIScriptPtr + 5);
else
gAIScriptPtr += 9;
}
static void Cmd_if_more_than_ptr(void)
{
const u8 *value = T1_READ_PTR(gAIScriptPtr + 1);
if (AI_THINKING_STRUCT->funcResult > *value)
gAIScriptPtr = T1_READ_PTR(gAIScriptPtr + 5);
else
gAIScriptPtr += 9;
}
static void Cmd_if_equal_ptr(void)
{
const u8 *value = T1_READ_PTR(gAIScriptPtr + 1);
if (AI_THINKING_STRUCT->funcResult == *value)
gAIScriptPtr = T1_READ_PTR(gAIScriptPtr + 5);
else
gAIScriptPtr += 9;
}
static void Cmd_if_not_equal_ptr(void)
{
const u8 *value = T1_READ_PTR(gAIScriptPtr + 1);
if (AI_THINKING_STRUCT->funcResult != *value)
gAIScriptPtr = T1_READ_PTR(gAIScriptPtr + 5);
else
gAIScriptPtr += 9;
}
static void Cmd_if_move(void)
{
u16 move = T1_READ_16(gAIScriptPtr + 1);
if (AI_THINKING_STRUCT->moveConsidered == move)
gAIScriptPtr = T1_READ_PTR(gAIScriptPtr + 3);