summaryrefslogtreecommitdiff
path: root/src/dungeon_ai_item_weight.c
blob: ce79ad4673cce901ec00e76efd4b1c8a2fe74cf5 (plain)
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
#include "global.h"
#include "dungeon_ai_item_weight.h"

#include "constants/status.h"
#include "constants/targeting.h"
#include "dungeon_ai_targeting_2.h"
#include "dungeon_map_access.h"
#include "dungeon_pokemon_attributes.h"
#include "dungeon_util.h"
#include "moves.h"
#include "number_util.h"
#include "status_checks_1.h"

u32 EvaluateItem(struct DungeonEntity *targetPokemon, struct ItemSlot *item, u32 itemTargetFlags)
{
    struct DungeonEntityData *pokemonData = targetPokemon->entityData;
    s32 itemWeight = 0;
    bool8 targetOther = itemTargetFlags & 1;
    u16 targetAlly = (itemTargetFlags >> 1) & 1;
    s32 i;
    struct PokemonMove *move;
    struct PokemonMove *move2;
    switch (item->itemIndex)
    {
        case ITEM_ID_STICK:
        case ITEM_ID_IRON_THORN:
        case ITEM_ID_SILVER_SPIKE:
        case ITEM_ID_GOLD_FANG:
        case ITEM_ID_CACNEA_SPIKE:
        case ITEM_ID_CORSOLA_TWIG:
        case ITEM_ID_GRAVELEROCK:
        case ITEM_ID_GEO_PEBBLE:
            if (targetOther)
            {
                itemWeight = 70;
            }
            break;
        case ITEM_ID_DIET_RIBBON:
            if (targetOther && RoundUpFixedPoint(pokemonData->belly) > 0)
            {
                return 50;
            }
            else
            {
                itemWeight = 0;
            }
            break;
        case ITEM_ID_WHIFF_SPECS:
        case ITEM_ID_NO_AIM_SCOPE:
            if (targetOther)
            {
                return 50;
            }
            else
            {
                itemWeight = 0;
            }
            break;
        case ITEM_ID_ORAN_BERRY:
        case ITEM_ID_SITRUS_BERRY:
            if (pokemonData->HP < pokemonData->maxHP && pokemonData->HP <= pokemonData->maxHP / 4)
            {
                if (!targetOther)
                {
                    if (CanTargetAdjacentPokemon(targetPokemon))
                    {
                        itemWeight = 100;
                    }
                    else
                    {
                        itemWeight = 50;
                    }
                }
                else
                {
                    itemWeight = 50;
                }
            }
            break;
        case ITEM_ID_MAX_ELIXIR:
            itemWeight = 0;
            move = pokemonData->moves;
            move2 = move;
            for (i = 0; i < MAX_MON_MOVES; move++, move2++, i++)
            {
                if (move->moveFlags & MOVE_FLAG_EXISTS)
                {
                    if (move->PP == 0)
                    {
                        itemWeight += 30;
                    }
                    if (move->PP != GetMoveMaxPP(move2))
                    {
                        itemWeight += 6;
                    }
                }
            }
            if (itemWeight >= 99)
            {
                itemWeight = 99;
            }
            break;
        case ITEM_ID_HEAL_SEED:
            if (HasNegativeStatus(targetPokemon))
            {
                itemWeight = 80;
            }
            else
            {
                itemWeight = 0;
            }
            break;
        case ITEM_ID_PROTEIN:
            if (pokemonData->attack > 249)
            {
                itemWeight = 0;
            }
            else
            {
                itemWeight = 100;
            }
            break;
        case ITEM_ID_CALCIUM:
            if (pokemonData->specialAttack > 249)
            {
                itemWeight = 0;
            }
            else
            {
                itemWeight = 100;
            }
            break;
        case ITEM_ID_IRON:
            if (pokemonData->defense > 249)
            {
                itemWeight = 0;
            }
            else
            {
                itemWeight = 100;
            }
            break;
        case ITEM_ID_ZINC:
            if (pokemonData->specialDefense > 249)
            {
                itemWeight = 0;
            }
            else
            {
                itemWeight = 100;
            }
            break;
        case ITEM_ID_LIFE_SEED:
            if (!targetOther)
            {
                if (CanTargetAdjacentPokemon(targetPokemon))
                {
                    itemWeight = 10;
                }
                else
                {
                    itemWeight = 100;
                }
            }
            else
            {
                itemWeight = 0;
            }
            break;
        case ITEM_ID_EYEDROP_SEED:
            if (!CanSeeInvisible(targetPokemon))
            {
                if (CanTargetAdjacentPokemon(targetPokemon))
                {
                    itemWeight = 80;
                }
                else
                {
                    itemWeight = 5;
                }
            }
            else
            {
                return 0;
            }
            break;
        case ITEM_ID_QUICK_SEED:
            if (targetPokemon->entityData->movementSpeed < MAX_MOVEMENT_SPEED)
            {
                if (CanTargetAdjacentPokemon(targetPokemon))
                {
                    itemWeight = 80;
                }
                else
                {
                    itemWeight = 5;
                }
            }
            else
            {
                return 0;
            }
            break;
        case ITEM_ID_ALLURE_SEED:
            if (pokemonData->eyesightStatus != EYESIGHT_STATUS_CROSS_EYED)
            {
                if (CanTargetAdjacentPokemon(targetPokemon))
                {
                    itemWeight = 80;
                }
                else
                {
                    itemWeight = 5;
                }
            }
            else
            {
                return 0;
            }
            break;
        case ITEM_ID_CHERI_BERRY:
            if (pokemonData->nonVolatileStatus != NON_VOLATILE_STATUS_PARALYZED)
            {
                return 0;
            }
            else if (CanTargetAdjacentPokemon(targetPokemon))
            {
                itemWeight = 80;
            }
            else
            {
                itemWeight = 30;
            }
            break;
        case ITEM_ID_TOTTER_SEED:
            if (pokemonData->volatileStatus != VOLATILE_STATUS_CONFUSED)
            {
                if (CanTargetAdjacentPokemon(targetPokemon))
                {
                    itemWeight = 80;
                }
                else
                {
                    itemWeight = 15;
                }
            }
            else
            {
                return 0;
            }
            break;
        case ITEM_ID_PECHA_BERRY:
            if (pokemonData->nonVolatileStatus != NON_VOLATILE_STATUS_POISONED &&
                pokemonData->nonVolatileStatus != NON_VOLATILE_STATUS_BADLY_POISONED)
            {
                return 0;
            }
            else if (CanTargetAdjacentPokemon(targetPokemon))
            {
                itemWeight = 100;
            }
            else
            {
                itemWeight = 50;
            }
            break;
        case ITEM_ID_BLINKER_SEED:
            if (pokemonData->eyesightStatus != EYESIGHT_STATUS_BLINKER)
            {
                if (CanTargetAdjacentPokemon(targetPokemon))
                {
                    itemWeight = 80;
                }
                else
                {
                    itemWeight = 5;
                }
            }
            else
            {
                return 0;
            }
            break;
        case ITEM_ID_WARP_SEED:
            if (!targetAlly)
            {
                if (CanTargetAdjacentPokemon(targetPokemon))
                {
                    itemWeight = 40;
                }
                else
                {
                    itemWeight = 5;
                }
            }
            else if (pokemonData->HP < pokemonData->maxHP && pokemonData->HP < 20)
            {
                if (!targetOther)
                {
                    if (CanTargetAdjacentPokemon(targetPokemon))
                    {
                        itemWeight = 100;
                    }
                    else
                    {
                        itemWeight = 50;
                    }
                }
                else
                {
                    itemWeight = 50;
                }
            }
            break;
        case ITEM_ID_PATSY_BAND:
            itemWeight = 40;
            break;
        case ITEM_ID_SLEEP_SEED:
            if (pokemonData->sleepStatus != SLEEP_STATUS_SLEEP &&
                pokemonData->sleepStatus != SLEEP_STATUS_NAPPING &&
                pokemonData->sleepStatus != SLEEP_STATUS_NIGHTMARE)
            {
                if (CanTargetAdjacentPokemon(targetPokemon))
                {
                    itemWeight = 80;
                }
                else
                {
                    itemWeight = 5;
                }
            }
            else
            {
                return 0;
            }
            break;
        case ITEM_ID_CHESTO_BERRY:
            if (pokemonData->sleepStatus != SLEEP_STATUS_SLEEPLESS)
            {
                itemWeight = 5;
            }
            else
            {
                return 0;
            }
            break;
        case ITEM_ID_JOY_SEED:
            if (pokemonData->level < 99)
            {
                itemWeight = 80;
            }
            else
            {
                itemWeight = 0;
            }
            break;
        case ITEM_ID_GINSENG:
            itemWeight = 80;
            break;
        case ITEM_ID_RAWST_BERRY:
            if (pokemonData->nonVolatileStatus == NON_VOLATILE_STATUS_BURNED)
            {
                return 50;
            }
            else
            {
                itemWeight = 0;
            }
            break;
        case ITEM_ID_HUNGER_SEED:
            if (RoundUpFixedPoint(pokemonData->belly) > 0)
            {
                return 50;
            }
            else
            {
                itemWeight = 0;
            }
            break;
        case ITEM_ID_DOOM_SEED:
            if (pokemonData->level > 1)
            {
                itemWeight = 80;
            }
            else
            {
                itemWeight = 0;
            }
            break;
        case ITEM_ID_STUN_SEED:
            if (pokemonData->immobilizeStatus == IMMOBILIZE_STATUS_PETRIFIED)
            {
                return 0;
            }
            else if (CanTargetAdjacentPokemon(targetPokemon))
            {
                itemWeight = 80;
            }
            else
            {
                itemWeight = 5;
            }
            break;
        case ITEM_ID_BLAST_SEED:
            if (CanTargetAdjacentPokemon(targetPokemon))
            {
                itemWeight = 80;
            }
            else
            {
                itemWeight = 30;
            }
            break;
        case ITEM_ID_APPLE:
        case ITEM_ID_BIG_APPLE:
        case ITEM_ID_HUGE_APPLE:
            if (RoundUpFixedPoint(pokemonData->belly) < 10)
            {
                return 100;
            }
            else
            {
                itemWeight = 0;
            }
            break;
        case ITEM_ID_GRIMY_FOOD:
            itemWeight = 30;
            break;
        case ITEM_ID_ROLLCALL_ORB:
            move = pokemonData->moves; // Fixes a regswap.
            if (targetOther)
            {
                itemWeight = 0;
            }
            else
            {
                itemWeight = 20;
            }
            break;
        default:
            itemWeight = 0;
    }
    return itemWeight;
}

bool8 CanTargetAdjacentPokemon(struct DungeonEntity *pokemon)
{
    s32 facingDir;
    for (facingDir = 0; facingDir < NUM_DIRECTIONS; facingDir++)
    {
        struct MapTile *mapTile = GetMapTile_1(pokemon->posWorld.x + gAdjacentTileOffsets[facingDir].x, pokemon->posWorld.y + gAdjacentTileOffsets[facingDir].y);
        struct DungeonEntity *adjacentPokemon = mapTile->pokemon;
        if (adjacentPokemon != NULL && GetEntityType(adjacentPokemon) != ENTITY_NONE &&
            CanTarget(pokemon, adjacentPokemon, FALSE, TRUE) == TARGET_CAPABILITY_CAN_TARGET)
        {
            return TRUE;
        }
    }
    return FALSE;
}