summaryrefslogtreecommitdiff
path: root/src/wild_pokemon_area.c
blob: c93ae36b62381646c014db3234df9d4455ca5e80 (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
#include "global.h"
#include "field_specials.h"
#include "event_data.h"
#include "wild_encounter.h"
#include "roamer.h"
#include "overworld.h"
#include "pokedex_area_markers.h"
#include "constants/region_map_sections.h"
#include "constants/maps.h"

struct SeviiDexArea
{
    const u16 (*lut)[2];
    s32 count;
};

struct RoamerPair
{
    u16 roamer;
    u16 starter;
};

static s32 GetRoamerIndex(u16 species);
static s32 CountRoamerNests(u16 species, struct Subsprite * subsprites);
static bool32 PokemonInAnyEncounterTableInMap(const struct WildPokemonHeader * data, s32 species);
static bool32 PokemonInEncounterTable(const struct WildPokemonInfo * pokemon, s32 species, s32 count);
static u16 GetMapSecIdFromWildMonHeader(const struct WildPokemonHeader * header);
static bool32 TryGetMapSecPokedexAreaEntry(u16 mapSecId, const u16 (*lut)[2], s32 count, s32 * lutIdx_p, u16 * tableIdx_p);

static const u16 sDexAreas_Kanto[][2] = {
    { MAPSEC_PALLET_TOWN,          1 },
    { MAPSEC_VIRIDIAN_CITY,        2 },
    { MAPSEC_PEWTER_CITY,          3 },
    { MAPSEC_CERULEAN_CITY,        4 },
    { MAPSEC_LAVENDER_TOWN,        5 },
    { MAPSEC_VERMILION_CITY,       6 },
    { MAPSEC_CELADON_CITY,         7 },
    { MAPSEC_FUCHSIA_CITY,         8 },
    { MAPSEC_CINNABAR_ISLAND,      9 },
    { MAPSEC_INDIGO_PLATEAU,      10 },
    { MAPSEC_SAFFRON_CITY,        11 },
    { MAPSEC_ROUTE_4_POKECENTER,  15 },
    { MAPSEC_ROUTE_10_POKECENTER, 21 },
    { MAPSEC_ROUTE_1,             12 },
    { MAPSEC_ROUTE_2,             13 },
    { MAPSEC_ROUTE_3,             14 },
    { MAPSEC_ROUTE_4,             15 },
    { MAPSEC_ROUTE_5,             16 },
    { MAPSEC_ROUTE_6,             17 },
    { MAPSEC_ROUTE_7,             18 },
    { MAPSEC_ROUTE_8,             19 },
    { MAPSEC_ROUTE_9,             20 },
    { MAPSEC_ROUTE_10,            21 },
    { MAPSEC_ROUTE_11,            22 },
    { MAPSEC_ROUTE_12,            23 },
    { MAPSEC_ROUTE_13,            24 },
    { MAPSEC_ROUTE_14,            25 },
    { MAPSEC_ROUTE_15,            26 },
    { MAPSEC_ROUTE_16,            27 },
    { MAPSEC_ROUTE_17,            28 },
    { MAPSEC_ROUTE_18,            29 },
    { MAPSEC_ROUTE_19,            30 },
    { MAPSEC_ROUTE_20,            31 },
    { MAPSEC_ROUTE_21,            32 },
    { MAPSEC_ROUTE_22,            33 },
    { MAPSEC_ROUTE_23,            34 },
    { MAPSEC_ROUTE_24,            35 },
    { MAPSEC_ROUTE_25,            36 },
    { MAPSEC_VIRIDIAN_FOREST,     37 },
    { MAPSEC_MT_MOON,             39 },
    { MAPSEC_S_S_ANNE,             6 },
    { MAPSEC_UNDERGROUND_PATH,    11 },
    { MAPSEC_UNDERGROUND_PATH_2,  11 },
    { MAPSEC_DIGLETTS_CAVE,       38 },
    { MAPSEC_KANTO_VICTORY_ROAD,  47 },
    { MAPSEC_ROCKET_HIDEOUT,       7 },
    { MAPSEC_SILPH_CO,            11 },
    { MAPSEC_POKEMON_MANSION,     46 },
    { MAPSEC_KANTO_SAFARI_ZONE,   44 },
    { MAPSEC_POKEMON_LEAGUE,      47 },
    { MAPSEC_ROCK_TUNNEL,         41 },
    { MAPSEC_SEAFOAM_ISLANDS,     45 },
    { MAPSEC_POKEMON_TOWER,       43 },
    { MAPSEC_CERULEAN_CAVE,       40 },
    { MAPSEC_POWER_PLANT,         42 }
};

static const u16 sDexAreas_Sevii1[][2] = {
	{ MAPSEC_KINDLE_ROAD,    55 },
	{ MAPSEC_TREASURE_BEACH, 56 },
	{ MAPSEC_ONE_ISLAND,     48 },
	{ MAPSEC_MT_EMBER,       72 }    
};

static const u16 sDexAreas_Sevii2[][2] = {
	{ MAPSEC_CAPE_BRINK, 57 },
	{ MAPSEC_TWO_ISLAND, 49 }    
};

static const u16 sDexAreas_Sevii3[][2] = {
	{ MAPSEC_BOND_BRIDGE,     58 },
	{ MAPSEC_THREE_ISLE_PORT, 59 },
	{ MAPSEC_THREE_ISLAND,    50 },
	{ MAPSEC_BERRY_FOREST,    73 },
	{ MAPSEC_THREE_ISLE_PATH, 59 }    
};

static const u16 sDexAreas_Sevii4[][2] = {
	{ MAPSEC_FOUR_ISLAND,  51 },
	{ MAPSEC_ICEFALL_CAVE, 74 }    
};

static const u16 sDexAreas_Sevii5[][2] = {
	{ MAPSEC_RESORT_GORGEOUS,  60 },
	{ MAPSEC_WATER_LABYRINTH,  61 },
	{ MAPSEC_FIVE_ISLE_MEADOW, 62 },
	{ MAPSEC_MEMORIAL_PILLAR,  63 },
	{ MAPSEC_FIVE_ISLAND,      52 },
	{ MAPSEC_ROCKET_WAREHOUSE, 62 },
	{ MAPSEC_LOST_CAVE,        75 }    
};

static const u16 sDexAreas_Sevii6[][2] = {
	{ MAPSEC_OUTCAST_ISLAND, 64 },
	{ MAPSEC_GREEN_PATH,     65 },
	{ MAPSEC_WATER_PATH,     66 },
	{ MAPSEC_RUIN_VALLEY,    67 },
	{ MAPSEC_DOTTED_HOLE,    78 },
	{ MAPSEC_PATTERN_BUSH,   77 },
	{ MAPSEC_ALTERING_CAVE,  76 }    
};

static const u16 sDexAreas_Sevii7[][2] = {
	{ MAPSEC_TRAINER_TOWER,   68 },
	{ MAPSEC_CANYON_ENTRANCE, 69 },
	{ MAPSEC_SEVAULT_CANYON,  70 },
	{ MAPSEC_TANOBY_RUINS,    71 },
	{ MAPSEC_MONEAN_CHAMBER,  79 },
	{ MAPSEC_LIPTOO_CHAMBER,  79 },
	{ MAPSEC_WEEPTH_CHAMBER,  79 },
	{ MAPSEC_DILFORD_CHAMBER, 79 },
	{ MAPSEC_SCUFIB_CHAMBER,  79 },
	{ MAPSEC_RIXY_CHAMBER,    79 },
	{ MAPSEC_VIAPOIS_CHAMBER, 79 }    
};

static const struct SeviiDexArea sSeviiDexAreas[] = {
    { sDexAreas_Sevii1,  4 },
    { sDexAreas_Sevii2,  2 },
    { sDexAreas_Sevii3,  5 },
    { sDexAreas_Sevii4,  2 },
    { sDexAreas_Sevii5,  7 },
    { sDexAreas_Sevii6,  7 },
    { sDexAreas_Sevii7, 11 }
};

static const struct RoamerPair sRoamerPairs[] = {
    { SPECIES_ENTEI,   SPECIES_BULBASAUR  },
    { SPECIES_SUICUNE, SPECIES_CHARMANDER },
    { SPECIES_RAIKOU,  SPECIES_SQUIRTLE   }
};

s32 BuildPokedexAreaSubspriteBuffer(u16 species, struct Subsprite * subsprites)
{
    s32 areaCount;
    s32 j;
    s32 mapSecId;
    u16 dexAreaSubspriteIdx;
    s32 dexAreaEntryLUTidx;
    s32 seviiAreas;
    s32 alteringCaveCount;
    s32 alteringCaveNum;
    s32 i;

    if (GetRoamerIndex(species) >= SPECIES_NONE)
    {
        return CountRoamerNests(species, subsprites);
    }

    seviiAreas = GetUnlockedSeviiAreas();
    alteringCaveCount = 0;
    alteringCaveNum = VarGet(VAR_ALTERING_CAVE_WILD_SET);
    if (alteringCaveNum > 8)
        alteringCaveNum = 0;
    for (i = 0, areaCount = 0; gWildMonHeaders[i].mapGroup != MAP_GROUP(UNDEFINED); i++)
    {
        mapSecId = GetMapSecIdFromWildMonHeader(&gWildMonHeaders[i]);
        if (mapSecId == MAPSEC_ALTERING_CAVE)
        {
            alteringCaveCount++;
            if (alteringCaveNum != alteringCaveCount - 1)
                continue;
        }
        if (PokemonInAnyEncounterTableInMap(&gWildMonHeaders[i], species))
        {
            dexAreaEntryLUTidx = 0;
            while (TryGetMapSecPokedexAreaEntry(mapSecId, sDexAreas_Kanto, 55, &dexAreaEntryLUTidx, &dexAreaSubspriteIdx))
            {
                if (dexAreaSubspriteIdx != 0)
                {
                    SetAreaSubsprite(areaCount++, dexAreaSubspriteIdx, subsprites);
                }
            }
            for (j = 0; j < NELEMS(sSeviiDexAreas); j++)
            {
                if ((seviiAreas >> j) & 1)
                {
                    dexAreaEntryLUTidx = 0;
                    while (TryGetMapSecPokedexAreaEntry(mapSecId, sSeviiDexAreas[j].lut, sSeviiDexAreas[j].count, &dexAreaEntryLUTidx, &dexAreaSubspriteIdx))
                    {
                        if (dexAreaSubspriteIdx != 0)
                        {
                            SetAreaSubsprite(areaCount++, dexAreaSubspriteIdx, subsprites);
                        }
                    }
                }
            }
        }
    }

    return areaCount;
}

static s32 GetRoamerIndex(u16 species)
{
    s32 i;
    for (i = 0; i < NELEMS(sRoamerPairs); i++)
    {
        if (sRoamerPairs[i].roamer == species)
            return i;
    }

    return -1;
}

static s32 CountRoamerNests(u16 species, struct Subsprite * subsprites)
{
    u16 roamerLocation;
    s32 roamerIdx;
    u16 dexAreaSubspriteIdx;
    s32 dexAreaEntryLUTidx;

    roamerIdx = GetRoamerIndex(species);
    if (roamerIdx < 0)
        return 0;
    if (sRoamerPairs[roamerIdx].starter != GetStarterSpecies())
        return 0;
    roamerLocation = GetRoamerLocationMapSectionId();
    dexAreaEntryLUTidx = 0;
    if (TryGetMapSecPokedexAreaEntry(roamerLocation, sDexAreas_Kanto, 55, &dexAreaEntryLUTidx, &dexAreaSubspriteIdx))
    {
        if (dexAreaSubspriteIdx != 0)
        {
            SetAreaSubsprite(0, dexAreaSubspriteIdx, subsprites);
            return 1;
        }
    }
    return 0;
}

static bool32 PokemonInAnyEncounterTableInMap(const struct WildPokemonHeader * data, s32 species)
{
    if (PokemonInEncounterTable(data->landMonsInfo, species, 12))
        return TRUE;
    if (PokemonInEncounterTable(data->waterMonsInfo, species, 5))
        return TRUE;
    if (PokemonInEncounterTable(data->fishingMonsInfo, species, 12)) // 10
        return TRUE;
    if (PokemonInEncounterTable(data->rockSmashMonsInfo, species, 5))
        return TRUE;

    return FALSE;
}

static bool32 PokemonInEncounterTable(const struct WildPokemonInfo * info, s32 species, s32 count)
{
    s32 i;
    if (info != NULL)
    {
        for (i = 0; i < count; i++)
        {
            if (info->wildPokemon[i].species == species)
                return TRUE;
        }
    }
    return FALSE;
}

static u16 GetMapSecIdFromWildMonHeader(const struct WildPokemonHeader * header)
{
    return Overworld_GetMapHeaderByGroupAndId(header->mapGroup, header->mapNum)->regionMapSectionId;
}

static bool32 TryGetMapSecPokedexAreaEntry(u16 mapSecId, const u16 (*lut)[2], s32 count, s32 * lutIdx_p, u16 * tableIdx_p)
{
    s32 i;
    for (i = *lutIdx_p; i < count; i++)
    {
        if (lut[i][0] == mapSecId)
        {
            *tableIdx_p = lut[i][1];
            *lutIdx_p = i + 1;
            return TRUE;
        }
    }
    return FALSE;
}