summaryrefslogtreecommitdiff
path: root/src/trainer_see.c
blob: 56085f9beb27c4cab387916c0a64d5dea1eca3f7 (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
#include "global.h"
#include "asm.h"
#include "asm_fieldmap.h"
#include "battle_setup.h"
#include "field_effect.h"
#include "field_map_obj.h"
#include "field_player_avatar.h"
#include "script.h"
#include "sprite.h"
#include "task.h"
#include "trainer_see.h"

extern bool8 (*gIsTrainerInRange[])(struct MapObject *, u16, s16, s16);
extern bool8 (*gTrainerSeeFuncList[])(u8, struct Task *, struct MapObject *);
extern bool8 (*gTrainerSeeFuncList2[])(u8, struct Task *, struct MapObject *);

extern u32 gUnknown_0202FF84[];

extern struct SpriteTemplate gSpriteTemplate_839B510;
extern struct SpriteTemplate gSpriteTemplate_839B528;

bool8 CheckTrainers(void)
{
    u8 i;

    for (i = 0; i < 16; i++)
    {
        if ( gMapObjects[i].active )
            if ( gMapObjects[i].trainerType == 1 ||  gMapObjects[i].trainerType == 3 )
                if ( CheckTrainer(i) )
                    return TRUE;
    }
    return FALSE;
}

bool8 CheckTrainer(u8 trainer)
{
   u8 *scriptPtr = GetFieldObjectScriptPointerByFieldObjectId(trainer);

   if (GetTrainerFlagFromScriptPointer(scriptPtr))
       return FALSE;
   else
   {
       struct MapObject *trainerObj = &gMapObjects[trainer];
       u8 canApproach = TrainerCanApproachPlayer(trainerObj);

       if (canApproach != 0)
        {
           TrainerWantsBattle(trainer, scriptPtr);
           sub_80842C8(trainerObj, (canApproach - 1));
           return TRUE;
        }
       else
       {
           return FALSE;
       }
   }
}

bool8 TrainerCanApproachPlayer(struct MapObject *trainerObj)
{
    s16 x, y;
    u8 i;
    u8 playerCoord;

    PlayerGetDestCoords(&x, &y);
    if ( trainerObj->trainerType == 1 ) // trainers that don't spin
    {
        playerCoord = gIsTrainerInRange[trainerObj->mapobj_unk_18 - 1](trainerObj, trainerObj->trainerRange_berryTreeId, x, y);
        return CheckPathBetweenTrainerAndPlayer((struct MapObject2 *)trainerObj, playerCoord, trainerObj->mapobj_unk_18);
    }
    else // spinners
    {
        for (i = 0; i < 4; i++)
        {
            playerCoord = gIsTrainerInRange[i](trainerObj, trainerObj->trainerRange_berryTreeId, x, y);
            if ( CheckPathBetweenTrainerAndPlayer((struct MapObject2 *)trainerObj, playerCoord, i + 1) ) // directions are 1-4 instead of 0-3. south north west east
                return playerCoord;
        }
        return FALSE;
    }
}

bool8 IsTrainerInRangeSouth(struct MapObject *trainerObj, s16 vision, s16 x, s16 y)
{
    if ( trainerObj->coords2.x == x
        && y > trainerObj->coords2.y
        && y <= trainerObj->coords2.y + vision )
        return (y - trainerObj->coords2.y);
    else
        return FALSE;
}

bool8 IsTrainerInRangeNorth(struct MapObject *trainerObj, s16 vision, s16 x, s16 y)
{
    if ( trainerObj->coords2.x == x
        && y < trainerObj->coords2.y
        && y >= trainerObj->coords2.y - vision )
        return (trainerObj->coords2.y - y);
    else
        return FALSE;
}

bool8 IsTrainerInRangeWest(struct MapObject *trainerObj, s16 vision, s16 x, s16 y)
{
    if ( trainerObj->coords2.y == y
        && x < trainerObj->coords2.x
        && x >= trainerObj->coords2.x - vision )
        return (trainerObj->coords2.x - x);
    else
        return FALSE;
}

bool8 IsTrainerInRangeEast(struct MapObject *trainerObj, s16 vision, s16 x, s16 y)
{
    if ( trainerObj->coords2.y == y
        && x > trainerObj->coords2.x
        && x <= trainerObj->coords2.x + vision )
        return (x - trainerObj->coords2.x);
    else
        return FALSE;
}

#ifdef BUGFIX_TRAINERAPPROACH
#define COLLISION_MASK ~1
#else
#define COLLISION_MASK 1
#endif

bool8 CheckPathBetweenTrainerAndPlayer(struct MapObject2 *trainerObj, u8 playerCoord, u8 direction)
{
    s16 x, y;
    u8 unk19_temp;
    u8 unk19b_temp;
    u8 i;
    u8 var;

    if (!playerCoord)
        return FALSE;

    x = trainerObj->coords2.x;
    y = trainerObj->coords2.y;

    for (i = 0; i <= playerCoord - 1; i++, MoveCoords(direction, &x, &y))
    {
        var = sub_8060024((struct MapObject *)trainerObj, x, y, direction);

        if (var && (var & COLLISION_MASK))
            return FALSE;
    }

    // preserve mapobj_unk_19 before clearing.
    unk19_temp = trainerObj->mapobj_unk_19;
    unk19b_temp = trainerObj->mapobj_unk_19b;
    trainerObj->mapobj_unk_19 = 0;
    trainerObj->mapobj_unk_19b = 0;

    var = npc_block_way((struct MapObject *)trainerObj, x, y, direction);

    trainerObj->mapobj_unk_19 = unk19_temp;
    trainerObj->mapobj_unk_19b = unk19b_temp;
    if (var == 4)
        return playerCoord;

    return FALSE;
}

void sub_80842C8(struct MapObject *trainerObj, u8 taskId)
{
    struct Task *task = &gTasks[CreateTask(RunTrainerSeeFuncList, 0x50)];

    task->data[1] = (u32)(trainerObj) >> 16;
    task->data[2] = (u32)(trainerObj);
    task->data[3] = taskId;
}

void sub_80842FC(TaskFunc func)
{
    TaskFunc func2 = RunTrainerSeeFuncList;
    u8 taskId = FindTaskIdByFunc(func2);

    SetTaskFuncWithFollowupFunc(taskId, RunTrainerSeeFuncList, func);
    gTasks[taskId].data[0] = 1;
    func2(taskId);
}

void RunTrainerSeeFuncList(u8 taskId)
{
    struct Task *task = &gTasks[taskId];
    struct MapObject *trainerObj = (struct MapObject *)((task->data[1] << 16) | (task->data[2]));

    if (!trainerObj->active)
        SwitchTaskToFollowupFunc(taskId);
    else
        while (gTrainerSeeFuncList[task->data[0]](taskId, task, trainerObj));
}

u8 sub_8084394() // cant be void because it is called with RunTrainerSeeFuncList with arguments.
{
    return 0;
}

s8 sub_8084398(u8 taskId, struct Task *task, struct MapObject *trainerObj)
{
    u8 direction;

    FieldObjectGetLocalIdAndMap(trainerObj, (u8 *)&gUnknown_0202FF84[0], (u8 *)&gUnknown_0202FF84[1], (u8 *)&gUnknown_0202FF84[2]);
    FieldEffectStart(0);

    direction = GetFaceDirectionAnimId(trainerObj->mapobj_unk_18);

    FieldObjectSetSpecialAnim(trainerObj, direction);
    task->data[0]++;
    return 1;
}

s8 sub_80843DC(u8 taskId, struct Task *task, struct MapObject *trainerObj)
{
    if (FieldEffectActiveListContains(0))
        return 0;
    else
    {
        task->data[0]++;
        if ((u8)(trainerObj->animPattern - 57) <= 1)
            task->data[0] = 6;
        if (trainerObj->animPattern == 63)
            task->data[0] = 8;
        return 1;
    }
}

s8 sub_808441C(u8 taskId, struct Task *task, struct MapObject *trainerObj)
{
    if (!(FieldObjectIsSpecialAnimOrDirectionSequenceAnimActive(trainerObj)) || FieldObjectClearAnimIfSpecialAnimFinished(trainerObj))
    {
            if (task->data[3])
            {
                FieldObjectSetSpecialAnim(trainerObj, GetGoSpeed0AnimId(trainerObj->mapobj_unk_18));
                task->data[3]--;
            }
            else
            {
                FieldObjectSetSpecialAnim(trainerObj, 0x3E);
                task->data[0]++;
            }
    }
    return 0;
}

s8 sub_8084478(u8 taskId, struct Task *task, struct MapObject *trainerObj)
{
    struct MapObject *playerObj;

    if (FieldObjectIsSpecialAnimOrDirectionSequenceAnimActive(trainerObj) && !FieldObjectClearAnimIfSpecialAnimFinished(trainerObj))
        return 0;

    npc_set_running_behaviour_etc(trainerObj, npc_running_behaviour_by_direction(trainerObj->mapobj_unk_18));
    sub_805C774(trainerObj, npc_running_behaviour_by_direction(trainerObj->mapobj_unk_18));
    sub_805C754(trainerObj);

    playerObj = &gMapObjects[gPlayerAvatar.mapObjectId];
    if (FieldObjectIsSpecialAnimOrDirectionSequenceAnimActive(playerObj)
     && !FieldObjectClearAnimIfSpecialAnimFinished(playerObj))
        return 0;

    sub_80597E8();
    FieldObjectSetSpecialAnim(&gMapObjects[gPlayerAvatar.mapObjectId], GetFaceDirectionAnimId(GetOppositeDirection(trainerObj->mapobj_unk_18)));
    task->data[0]++;
    return 0;
}

s8 sub_8084534(u8 taskId, struct Task *task, struct MapObject *trainerObj) // technically only 1 parameter, but needs all 3 for TrainerSeeFuncList call.
{
    struct MapObject *playerObj = &gMapObjects[gPlayerAvatar.mapObjectId];

    if ( !FieldObjectIsSpecialAnimOrDirectionSequenceAnimActive(playerObj)
        || FieldObjectClearAnimIfSpecialAnimFinished(playerObj) )
    SwitchTaskToFollowupFunc(taskId);

    return 0;
}

s8 sub_8084578(u8 taskId, struct Task *task, struct MapObject *trainerObj)
{
    if (!FieldObjectIsSpecialAnimOrDirectionSequenceAnimActive(trainerObj)
        || FieldObjectClearAnimIfSpecialAnimFinished(trainerObj))
    {
        FieldObjectSetSpecialAnim(trainerObj, 0x59);
        task->data[0]++;
    }
    return 0;
}

s8 sub_80845AC(u8 taskId, struct Task *task, struct MapObject *trainerObj)
{
    if ( FieldObjectClearAnimIfSpecialAnimFinished(trainerObj) )
        task->data[0] = 3;

    return 0;
}

s8 sub_80845C8(u8 taskId, struct Task *task, struct MapObject *trainerObj)
{
    if (!FieldObjectIsSpecialAnimOrDirectionSequenceAnimActive(trainerObj)
        || FieldObjectClearAnimIfSpecialAnimFinished(trainerObj))
    {
        FieldObjectSetSpecialAnim(trainerObj, 0x3E);
        task->data[0]++;
    }
    return 0;
}

s8 sub_80845FC(u8 taskId, struct Task *task, struct MapObject *trainerObj)
{
    if (FieldObjectCheckIfSpecialAnimFinishedOrInactive(trainerObj))
    {
        gUnknown_0202FF84[0] = trainerObj->coords2.x;
        gUnknown_0202FF84[1] = trainerObj->coords2.y;
        gUnknown_0202FF84[2] = gSprites[trainerObj->spriteId].subpriority - 1;
        gUnknown_0202FF84[3] = 2;
        task->data[4] = FieldEffectStart(49);
        task->data[0]++;
    }
    return 0;
}

s8 sub_8084654(u8 taskId, struct Task *task, struct MapObject *trainerObj)
{
    struct Sprite *sprite;

    if (gSprites[task->data[4]].animCmdIndex == 2)
    {
        trainerObj->mapobj_bit_26 = 0;
        trainerObj->mapobj_bit_2 = 1;

        sprite = &gSprites[trainerObj->spriteId];
        sprite->oam.priority = 2;
        FieldObjectClearAnimIfSpecialAnimFinished(trainerObj);
        FieldObjectSetSpecialAnim(trainerObj, sub_806084C(trainerObj->mapobj_unk_18));
        task->data[0]++;
    }
    return 0;
}

s8 sub_80846C8(u8 taskId, struct Task *task, struct MapObject *trainerObj)
{
    if (!FieldEffectActiveListContains(49))
        task->data[0] = 3;

    return 0;
}

void sub_80846E4(u8 taskId)
{
    struct Task *task = &gTasks[taskId];
    struct MapObject *mapObj;

    // another mapObj loaded into by loadword?
    LoadWordFromTwoHalfwords(&task->data[1], (u32 *)&mapObj);
    if (!task->data[7])
    {
        FieldObjectClearAnim(mapObj);
        task->data[7]++;
    }
    gTrainerSeeFuncList2[task->data[0]](taskId, task, mapObj);
    if (task->data[0] == 3 && !FieldEffectActiveListContains(49))
    {
        npc_set_running_behaviour_etc(mapObj, npc_running_behaviour_by_direction(mapObj->mapobj_unk_18));
        sub_805C774(mapObj, npc_running_behaviour_by_direction(mapObj->mapobj_unk_18));
        DestroyTask(taskId);
    }
    else
        mapObj->mapobj_bit_7 = 0;
}

void sub_8084794(u32 var)
{
    StoreWordInTwoHalfwords(&gTasks[CreateTask(sub_80846E4, 0)].data[1], var);
}

void sub_80847C8(void)
{
    sub_80842FC(sub_80847D8);
}

void sub_80847D8(u8 taskId)
{
    DestroyTask(taskId);
    EnableBothScriptContexts();
}

u8 FldEff_ExclamationMarkIcon1(void)
{
    u8 spriteId = CreateSpriteAtEnd(&gSpriteTemplate_839B510, 0, 0, 0x53);

    if (spriteId != 64)
        sub_8084894(&gSprites[spriteId], 0, 0);

    return 0;
}

u8 FldEff_ExclamationMarkIcon2(void)
{
    u8 spriteId = CreateSpriteAtEnd(&gSpriteTemplate_839B510, 0, 0, 0x52);

    if (spriteId != 64)
        sub_8084894(&gSprites[spriteId], 33, 1);

    return 0;
}

u8 FldEff_HeartIcon(void)
{
    u8 spriteId = CreateSpriteAtEnd(&gSpriteTemplate_839B528, 0, 0, 0x52);

    if (spriteId != 64)
        sub_8084894(&gSprites[spriteId], 46, 0);

    return 0;
}

void sub_8084894(struct Sprite *sprite, u16 a2, u8 a3)
{
    sprite->oam.priority = 1;
    sprite->coordOffsetEnabled = 1;

    sprite->data0 = gUnknown_0202FF84[0];
    sprite->data1 = gUnknown_0202FF84[1];
    sprite->data2 = gUnknown_0202FF84[2];
    sprite->data3 = -5;
    sprite->data7 = a2;

    StartSpriteAnim(sprite, a3);
}

void objc_exclamation_mark_probably(struct Sprite *sprite)
{
    u8 mapObjId;

    if (TryGetFieldObjectIdByLocalIdAndMap(sprite->data0, sprite->data1, sprite->data2, &mapObjId)
     || sprite->animEnded)
    {
        FieldEffectStop(sprite, (u8)sprite->data7);
    }
    else
    {
        struct Sprite *mapObjSprite = &gSprites[gMapObjects[mapObjId].spriteId];
        sprite->data4 += sprite->data3;
        sprite->pos1.x = mapObjSprite->pos1.x;
        sprite->pos1.y = mapObjSprite->pos1.y - 16;
        sprite->pos2.x = mapObjSprite->pos2.x;
        sprite->pos2.y = mapObjSprite->pos2.y + sprite->data4;
        if (sprite->data4)
            sprite->data3++;
        else
            sprite->data3 = 0;
    }
}