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
|
#include "global.h"
#include "dungeon_ai_attack.h"
#include "constants/direction.h"
#include "constants/dungeon_action.h"
#include "constants/iq_skill.h"
#include "constants/move_id.h"
#include "constants/status.h"
#include "constants/tactic.h"
#include "constants/type.h"
#include "charge_move.h"
#include "dungeon_action.h"
#include "dungeon_ai_targeting.h"
#include "dungeon_ai_targeting_1.h"
#include "dungeon_ai_attack_1.h"
#include "dungeon_ai_attack_2.h"
#include "dungeon_capabilities_1.h"
#include "dungeon_global_data.h"
#include "dungeon_map_access.h"
#include "dungeon_pokemon_attributes.h"
#include "dungeon_random.h"
#include "dungeon_random_1.h"
#include "dungeon_util.h"
#include "dungeon_visibility.h"
#include "moves.h"
#include "position_util.h"
#include "status_checker.h"
#include "status_checks.h"
#include "targeting.h"
#include "targeting_flags.h"
#define REGULAR_ATTACK_INDEX 4
const s16 gRegularAttackWeights[] = {100, 20, 30, 40, 50};
extern bool8 gCanAttackInDirection[NUM_DIRECTIONS];
extern s32 gNumPotentialTargets;
extern s32 gPotentialAttackTargetWeights[NUM_DIRECTIONS];
extern u8 gPotentialAttackTargetDirections[NUM_DIRECTIONS];
extern struct DungeonEntity *gPotentialTargets[NUM_DIRECTIONS];
extern bool8 IsMoveUsable_1(struct DungeonEntity*, s32, bool8);
extern bool8 TargetRegularAttack(struct DungeonEntity*, u32*, bool8);
extern bool8 IsTargetInLineRange(struct DungeonEntity*, struct DungeonEntity*, s32);
void DecideAttack(struct DungeonEntity *pokemon)
{
struct DungeonEntityData *pokemonData = pokemon->entityData;
s32 i;
s32 chargeStatus = CHARGING_STATUS_CHARGE;
struct MoveTargetResults moveTargetResults[MAX_MON_MOVES + 1];
bool8 willNotUnlinkMove[MAX_MON_MOVES];
s32 randomWeight;
bool8 hasPPChecker;
s32 numUsableMoves;
s32 total;
s32 weightCounter;
bool8 hasWeakTypePicker;
s32 regularAttackTargetDir;
bool8 canTargetRegularAttack;
s32 maxWeight;
if (CannotAttack(pokemon, FALSE) ||
ShouldAvoidEnemiesAndShowEffect(pokemon, TRUE) ||
HasTactic(pokemon, TACTIC_KEEP_YOUR_DISTANCE) ||
(pokemonData->volatileStatus == VOLATILE_STATUS_CONFUSED && RollPercentChance(gConfusedAttackChance)))
{
return;
}
if (pokemonData->chargingStatus != CHARGING_STATUS_NONE)
{
for (i = 0; i < MAX_MON_MOVES; i++)
{
if (pokemonData->moves[i].moveFlags & MOVE_FLAG_EXISTS &&
MoveMatchesChargingStatus(pokemon, &pokemonData->moves[i]) &&
pokemonData->chargingStatusMoveIndex == i)
{
s32 chosenMoveIndex;
SetAction(&pokemonData->action, DUNGEON_ACTION_USE_MOVE_AI);
chosenMoveIndex = i;
if (i > 0 && pokemonData->moves[i].moveFlags & MOVE_FLAG_LINKED)
{
while (--chosenMoveIndex > 0)
{
if (!(pokemonData->moves[chosenMoveIndex].moveFlags & MOVE_FLAG_LINKED))
{
break;
}
}
}
pokemonData->action.actionUseIndex = chosenMoveIndex;
TargetTileInFront(pokemon);
return;
}
}
}
total = 0;
numUsableMoves = 0;
for (i = 0; i < MAX_MON_MOVES; i++)
{
struct PokemonMove *move = &pokemonData->moves[i];
if (pokemonData->moves[i].moveFlags & MOVE_FLAG_EXISTS)
{
if (pokemonData->moves[i].moveFlags & MOVE_FLAG_ENABLED)
{
numUsableMoves++;
}
total += move->PP;
}
}
if (total == 0)
{
struct PokemonMove struggle;
InitPokemonMove(&struggle, MOVE_STRUGGLE);
FindMoveTarget(&moveTargetResults[0], pokemon, &struggle);
if (moveTargetResults[0].moveUsable)
{
SetAction(&pokemonData->action, DUNGEON_ACTION_STRUGGLE);
pokemonData->action.facingDir = moveTargetResults[0].targetDir & DIRECTION_MASK;
TargetTileInFront(pokemon);
}
return;
}
hasWeakTypePicker = HasIQSkill(pokemon, IQ_SKILL_WEAK_TYPE_PICKER);
hasPPChecker = HasIQSkill(pokemon, IQ_SKILL_PP_CHECKER) != FALSE;
total = 0;
for (i = 0; i < MAX_MON_MOVES; i++)
{
willNotUnlinkMove[i] = TRUE;
}
if (hasPPChecker)
{
s32 minPP = 99;
s32 linkedMoveStartIndex = 0;
// Linked moves unlink if any move in the chain runs out of PP.
// With PP Checker, the AI avoids this by not using linked moves if any move in the chain has 1 PP left.
// This requires a separate check from the 0-PP check used for unlinked moves.
for (i = 0; i < MAX_MON_MOVES; i++)
{
struct PokemonMove *move = &pokemonData->moves[i];
if (!(move->moveFlags & MOVE_FLAG_EXISTS))
{
break;
}
if (i != 0 && !(move->moveFlags & MOVE_FLAG_LINKED))
{
if (linkedMoveStartIndex + 1 < i && minPP <= 1 && linkedMoveStartIndex + 1 <= i)
{
s32 j;
for (j = linkedMoveStartIndex; j < i; j++)
{
willNotUnlinkMove[j] = FALSE;
}
}
minPP = move->PP;
linkedMoveStartIndex = i;
}
else
{
s32 newMinPP = move->PP;
if (newMinPP > minPP)
{
newMinPP = minPP;
}
minPP = newMinPP;
}
}
if (linkedMoveStartIndex + 1 < i && minPP <= 1 && linkedMoveStartIndex + 1 <= i)
{
s32 j;
for (j = linkedMoveStartIndex; j < i; j++)
{
willNotUnlinkMove[j] = FALSE;
}
}
}
for (i = 0; i < MAX_MON_MOVES; i++)
{
struct PokemonMove *move;
moveTargetResults[i].moveUsable = FALSE;
move = &pokemonData->moves[i];
if (move->moveFlags & MOVE_FLAG_EXISTS &&
willNotUnlinkMove[i] &&
IsMoveUsable_1(pokemon, i, hasPPChecker) &&
move->moveFlags & MOVE_FLAG_ENABLED)
{
moveTargetResults[i].moveUsable = TRUE;
if (pokemonData->chargingStatus == chargeStatus)
{
if (move->moveID == MOVE_CHARGE)
{
moveTargetResults[i].moveWeight = 0;
continue;
}
else if (GetMoveTypeForPokemon(pokemon, move) == TYPE_ELECTRIC)
{
moveTargetResults[i].moveWeight = GetMoveWeight(move);
}
else
{
moveTargetResults[i].moveWeight = 1;
}
}
else if (hasWeakTypePicker)
{
struct MoveTargetResults *results = &moveTargetResults[i];
results->moveWeight = FindMoveTarget(results, pokemon, move);
}
else
{
moveTargetResults[i].moveWeight = GetMoveWeight(move);
}
total += moveTargetResults[i].moveWeight;
}
}
moveTargetResults[REGULAR_ATTACK_INDEX].moveWeight = 0;
if (!HasIQSkill(pokemon, IQ_SKILL_EXCLUSIVE_MOVE_USER) && pokemonData->chargingStatus != chargeStatus)
{
moveTargetResults[REGULAR_ATTACK_INDEX].moveUsable = TRUE;
if (pokemonData->chargingStatus == chargeStatus)
{
// Never reached? Charge already skips the regular attack in the outer block.
moveTargetResults[REGULAR_ATTACK_INDEX].moveWeight = 1;
}
else if (hasWeakTypePicker)
{
moveTargetResults[REGULAR_ATTACK_INDEX].moveWeight = 2;
}
else
{
moveTargetResults[REGULAR_ATTACK_INDEX].moveWeight = gRegularAttackWeights[numUsableMoves];
}
total += moveTargetResults[REGULAR_ATTACK_INDEX].moveWeight;
}
if (hasWeakTypePicker)
{
s32 i;
maxWeight = 0;
total = 0;
for (i = 0; i < MAX_MON_MOVES + 1; i++)
{
if (!moveTargetResults[i].moveUsable)
{
moveTargetResults[i].moveWeight = 0;
}
else if (maxWeight < moveTargetResults[i].moveWeight)
{
maxWeight = moveTargetResults[i].moveWeight;
}
}
for (i = 0; i < MAX_MON_MOVES + 1; i++)
{
if (moveTargetResults[i].moveUsable)
{
if (maxWeight != moveTargetResults[i].moveWeight)
{
moveTargetResults[i].moveWeight = 0;
}
total += moveTargetResults[i].moveWeight;
}
}
}
if (total == 0)
{
return;
}
randomWeight = DungeonRandomCapped(total);
weightCounter = 0;
if (!HasIQSkill(pokemon, IQ_SKILL_EXCLUSIVE_MOVE_USER))
{
canTargetRegularAttack = TargetRegularAttack(pokemon, ®ularAttackTargetDir, TRUE);
}
else
{
canTargetRegularAttack = FALSE;
regularAttackTargetDir = DIRECTION_SOUTH;
}
for (i = 0; i <= REGULAR_ATTACK_INDEX; i++)
{
if (moveTargetResults[i].moveUsable && moveTargetResults[i].moveWeight != 0)
{
weightCounter += moveTargetResults[i].moveWeight;
if (weightCounter >= randomWeight)
{
if (i == REGULAR_ATTACK_INDEX)
{
if (canTargetRegularAttack)
{
SetAction(&pokemonData->action, DUNGEON_ACTION_REGULAR_ATTACK);
pokemonData->action.facingDir = regularAttackTargetDir & DIRECTION_MASK;
TargetTileInFront(pokemon);
}
}
else
{
FindMoveTarget(&moveTargetResults[i], pokemon, &pokemonData->moves[i]);
if (moveTargetResults[i].moveUsable)
{
s32 chosenMoveIndex;
SetAction(&pokemonData->action, DUNGEON_ACTION_USE_MOVE_AI);
chosenMoveIndex = i;
if (i > 0 && pokemonData->moves[i].moveFlags & MOVE_FLAG_LINKED)
{
while (--chosenMoveIndex > 0)
{
if (!(pokemonData->moves[chosenMoveIndex].moveFlags & MOVE_FLAG_LINKED))
{
break;
}
}
}
pokemonData->action.facingDir = moveTargetResults[i].targetDir & DIRECTION_MASK;
pokemonData->action.actionUseIndex = chosenMoveIndex;
TargetTileInFront(pokemon);
}
else
{
break;
}
}
return;
}
}
}
if (canTargetRegularAttack)
{
SetAction(&pokemonData->action, DUNGEON_ACTION_REGULAR_ATTACK);
pokemonData->action.facingDir = regularAttackTargetDir & DIRECTION_MASK;
TargetTileInFront(pokemon);
}
}
s32 FindMoveTarget(struct MoveTargetResults *moveTargetResults, struct DungeonEntity *pokemon, struct PokemonMove *move)
{
s32 targetingFlags;
s32 moveWeight = 1;
struct DungeonEntityData *pokemonData = pokemon->entityData;
s32 numPotentialTargets = 0;
s32 i;
bool8 hasStatusChecker;
s32 rangeTargetingFlags;
s32 rangeTargetingFlags2;
for (i = 0; i < NUM_DIRECTIONS; i++)
{
gCanAttackInDirection[i] = FALSE;
}
targetingFlags = GetMoveTargetingFlagsForPokemon(pokemon, move, TRUE);
hasStatusChecker = HasIQSkill(pokemon, IQ_SKILL_STATUS_CHECKER);
moveTargetResults->moveUsable = FALSE;
if ((pokemonData->volatileStatus == VOLATILE_STATUS_TAUNTED && !MoveDealsDirectDamage(move)) ||
(hasStatusChecker && !CanUseOnSelfWithStatusChecker(pokemon, move)))
{
return 1;
}
rangeTargetingFlags = targetingFlags & 0xF0;
if (rangeTargetingFlags == TARGETING_FLAG_TARGET_OTHER ||
rangeTargetingFlags == TARGETING_FLAG_TARGET_FRONTAL_CONE ||
rangeTargetingFlags == TARGETING_FLAG_TARGET_AROUND)
{
if (pokemonData->eyesightStatus == EYESIGHT_STATUS_BLINKER)
{
u8 facingDir = pokemonData->action.facingDir;
i = facingDir; // Fixes a regswap.
if (!gCanAttackInDirection[i])
{
gCanAttackInDirection[i] = TRUE;
gPotentialAttackTargetDirections[numPotentialTargets] = i;
gPotentialAttackTargetWeights[numPotentialTargets] = 99;
gPotentialTargets[numPotentialTargets] = NULL;
numPotentialTargets++;
}
}
else
{
for (i = 0; i < NUM_DIRECTIONS; i++)
{
// Double assignment to fix a regswap.
s16 rangeTargetingFlags = rangeTargetingFlags2 = targetingFlags & 0xF0;
struct MapTile *adjacentTile = GetMapTile_1(pokemon->posWorld.x + gAdjacentTileOffsets[i].x,
pokemon->posWorld.y + gAdjacentTileOffsets[i].y);
struct DungeonEntity *adjacentPokemon = adjacentTile->pokemon;
if (adjacentPokemon != NULL && GetEntityType(adjacentPokemon) == ENTITY_POKEMON)
{
if (rangeTargetingFlags != TARGETING_FLAG_TARGET_FRONTAL_CONE &&
rangeTargetingFlags != TARGETING_FLAG_TARGET_AROUND)
{
if (!CanAttackInFront(pokemon, i))
{
continue;
}
}
numPotentialTargets = WeightMoveIfUsable(numPotentialTargets, targetingFlags, pokemon, adjacentPokemon, move, hasStatusChecker);
}
}
}
}
else if (rangeTargetingFlags == TARGETING_FLAG_TARGET_ROOM)
{
s32 i;
for (i = 0; i < DUNGEON_MAX_POKEMON; i++)
{
struct DungeonEntity *target = gDungeonGlobalData->allPokemon[i];
if (EntityExists(target) && CanSee(pokemon, target))
{
numPotentialTargets = WeightMoveIfUsable(numPotentialTargets, targetingFlags, pokemon, target, move, hasStatusChecker);
}
}
}
else if (rangeTargetingFlags == TARGETING_FLAG_TARGET_2_TILES_AHEAD)
{
for (i = 0; i < NUM_DIRECTIONS; i++)
{
struct MapTile *targetTile = GetMapTile_1(pokemon->posWorld.x + gAdjacentTileOffsets[i].x,
pokemon->posWorld.y + gAdjacentTileOffsets[i].y);
if (CanAttackInFront(pokemon, i))
{
struct DungeonEntity *targetPokemon = targetTile->pokemon;
if (targetPokemon != NULL && GetEntityType(targetPokemon) == ENTITY_POKEMON)
{
s32 prevNumPotentialTargets = numPotentialTargets;
numPotentialTargets = WeightMoveIfUsable(numPotentialTargets, targetingFlags, pokemon, targetPokemon, move, hasStatusChecker);
if (prevNumPotentialTargets != numPotentialTargets)
{
continue;
}
}
targetTile = GetMapTile_1(pokemon->posWorld.x + gAdjacentTileOffsets[i].x * 2,
pokemon->posWorld.y + gAdjacentTileOffsets[i].y * 2);
targetPokemon = targetTile->pokemon;
if (targetPokemon != NULL && GetEntityType(targetPokemon) == ENTITY_POKEMON)
{
numPotentialTargets = WeightMoveIfUsable(numPotentialTargets, targetingFlags, pokemon, targetPokemon, move, hasStatusChecker);
}
}
}
}
else if (rangeTargetingFlags == TARGETING_FLAG_TARGET_LINE || rangeTargetingFlags == TARGETING_FLAG_CUT_CORNERS)
{
s32 maxRange = 1;
s32 i;
if (rangeTargetingFlags == TARGETING_FLAG_TARGET_LINE)
{
maxRange = 10;
}
for (i = 0; i < DUNGEON_MAX_POKEMON; i++)
{
struct DungeonEntity *target = gDungeonGlobalData->allPokemon[i];
if (EntityExists(target) && pokemon != target)
{
s32 facingDir = CalculateFacingDir(&pokemon->posWorld, &target->posWorld);
if (!gCanAttackInDirection[facingDir] &&
CanSee(pokemon, target) &&
IsTargetInLineRange(pokemon, target, maxRange) &&
CanUseStatusMove(targetingFlags, pokemon, target, move, hasStatusChecker) &&
IsTargetStraightAhead(pokemon, target, facingDir, maxRange))
{
gCanAttackInDirection[facingDir] = TRUE;
gPotentialAttackTargetDirections[numPotentialTargets] = facingDir;
gPotentialAttackTargetWeights[numPotentialTargets] = WeightMove(pokemon, targetingFlags, target, GetMoveTypeForPokemon(pokemon, move));
gPotentialTargets[numPotentialTargets] = target;
numPotentialTargets++;
}
}
}
}
else if (rangeTargetingFlags == TARGETING_FLAG_TARGET_FLOOR)
{
s32 i;
for (i = 0; i < DUNGEON_MAX_POKEMON; i++)
{
struct DungeonEntity *target = gDungeonGlobalData->allPokemon[i];
if (EntityExists(target))
{
numPotentialTargets = WeightMoveIfUsable(numPotentialTargets, targetingFlags, pokemon, target, move, hasStatusChecker);
}
}
}
else if (rangeTargetingFlags == TARGETING_FLAG_TARGET_SELF)
{
numPotentialTargets = WeightMoveIfUsable(numPotentialTargets, targetingFlags, pokemon, pokemon, move, hasStatusChecker);
}
if (numPotentialTargets == 0)
{
moveTargetResults->moveUsable = FALSE;
}
else
{
s32 totalWeight = 0;
s32 maxWeight = 0;
s32 weightCounter;
s32 i;
for (i = 0; i < numPotentialTargets; i++)
{
if (maxWeight < gPotentialAttackTargetWeights[i])
{
maxWeight = gPotentialAttackTargetWeights[i];
}
}
for (i = 0; i < numPotentialTargets; i++)
{
if (maxWeight != gPotentialAttackTargetWeights[i])
{
gPotentialAttackTargetWeights[i] = 0;
}
}
moveWeight = maxWeight;
for (i = 0; i < numPotentialTargets; i++)
{
totalWeight += gPotentialAttackTargetWeights[i];
}
weightCounter = DungeonRandomCapped(totalWeight);
for (i = 0; i < numPotentialTargets; i++)
{
weightCounter -= gPotentialAttackTargetWeights[i];
if (weightCounter < 0)
{
break;
}
}
moveTargetResults->moveUsable = TRUE;
moveTargetResults->targetDir = gPotentialAttackTargetDirections[i];
moveTargetResults->moveWeight = 8;
}
return moveWeight;
}
|