summaryrefslogtreecommitdiff
path: root/src/trainer_see.c
blob: b30f2734782880fb15d113f69a8609b49b0686ab (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
#include "global.h"
#include "trainer_see.h"
#include "battle_setup.h"

extern u8 gApproachingTrainerId;
extern u8 gNoOfApproachingTrainers;
extern u8 gUnknown_030060AC;
extern u16 gUnknown_03006080;

// this file's functions
u8 CheckIfTrainerWantsBattle(u8 mapObjectId);

bool8 CheckForTrainersWantingBattle(void)
{
    u8 i;

    gNoOfApproachingTrainers = 0;
    gApproachingTrainerId = 0;

    for (i = 0; i < MAP_OBJECTS_COUNT; i++)
    {
        u8 retVal;

        if (!gMapObjects[i].active)
            continue;
        if (gMapObjects[i].trainerType != 1 && gMapObjects[i].trainerType != 3)
            continue;

        retVal = CheckIfTrainerWantsBattle(i);
        if (retVal == 2)
            break; // two trainers have been found

        if (retVal == 0) // no trainers
            continue;

        if (gNoOfApproachingTrainers > 1)
            break;
        if (GetMonsStateToDoubles_2() != 0) // one trainer found and cant have a double battle
            break;
    }

    if (gNoOfApproachingTrainers == 1)
    {
        ResetTrainerOpponentIds();
        ConfigureAndSetUpOneTrainerBattle(gApproachingTrainers[gNoOfApproachingTrainers - 1].mapObjectId,
                                          gApproachingTrainers[gNoOfApproachingTrainers - 1].trainerScriptPtr);
        gUnknown_030060AC = 1;
        return TRUE;
    }
    else if (gNoOfApproachingTrainers == 2)
    {
        ResetTrainerOpponentIds();
        for (i = 0; i < gNoOfApproachingTrainers; i++, gApproachingTrainerId++)
        {
            ConfigureTwoTrainersBattle(gApproachingTrainers[i].mapObjectId,
                                       gApproachingTrainers[i].trainerScriptPtr);
        }
        SetUpTwoTrainersBattle();
        gApproachingTrainerId = 0;
        gUnknown_030060AC = 1;
        return TRUE;
    }
    else
    {
        gUnknown_030060AC = 0;
        return FALSE;
    }
}