summaryrefslogtreecommitdiff
path: root/src/battle_tent.c
blob: c7e70912ca860f9108172c586d33bcf32dd056eb (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
#include "global.h"
#include "battle_tent.h"
#include "overworld.h"
#include "event_data.h"
#include "region_map.h"
#include "battle.h"
#include "battle_setup.h"
#include "battle_tower.h"
#include "random.h"
#include "item.h"
#include "battle_factory_screen.h"
#include "frontier_util.h"
#include "string_util.h"
#include "constants/items.h"
#include "constants/region_map_sections.h"

extern const struct BattleFrontierTrainer gSlateportBattleTentTrainers[];
extern const struct FacilityMon gSlateportBattleTentMons[];
extern const u16 gBattleFrontierHeldItems[];

// This file's functions.
static void sub_81B99D4(void);
static void sub_81B9A28(void);
static void sub_81B9A44(void);
static void sub_81B9A60(void);
static void sub_81B9A90(void);
static void sub_81B9ABC(void);
static void sub_81B9B00(void);
static void sub_81B9B28(void);
static void sub_81B9BA0(void);
static void sub_81B9BF4(void);
static void sub_81B9C10(void);
static void sub_81B9C2C(void);
static void sub_81B9C70(void);
static void sub_81B9C98(void);
static void sub_81B9CF0(void);
static void sub_81B9D28(void);
static void sub_81B9D7C(void);
static void sub_81B9D98(void);
static void sub_81B9DB4(void);
static void sub_81B9DF8(void);
static void sub_81B9E20(void);
static void sub_81B9E78(void);
static void sub_81B9E88(void);
static void sub_81BA040(void);
static void sub_81B9EC0(void);

// IWRAM bss
static IWRAM_DATA u16 sRandMonSetId;

// const rom data
void static (*const gUnknown_086160B4[])(void) =
{
    sub_81B99D4,
    sub_81B9A28,
    sub_81B9A44,
    sub_81B9A60,
    sub_81B9A90,
    sub_81B9ABC,
    sub_81B9B00,
    sub_81B9B28
};

static const u16 sVerdanturfTentRewards[] = {ITEM_NEST_BALL};

void static (*const gUnknown_086160D8[])(void) =
{
    sub_81B9BA0,
    sub_81B9BF4,
    sub_81B9C10,
    sub_81B9C2C,
    sub_81B9C70,
    sub_81B9C98,
    sub_81B9CF0
};

static const u16 sFallarborTentRewards[] = {ITEM_HYPER_POTION};

void static (*const gUnknown_086160F8[])(void) =
{
    sub_81B9D28,
    sub_81B9D7C,
    sub_81B9D98,
    sub_81B9DB4,
    sub_81B9DF8,
    sub_81B9E20,
    sub_81B9E78,
    sub_81B9E88,
    sub_81BA040,
    sub_81B9EC0
};

static const u16 sSlateportTentRewards[] = {ITEM_FULL_HEAL};

// code
void sub_81B99B4(void)
{
    gUnknown_086160B4[gSpecialVar_0x8004]();
}

static void sub_81B99D4(void)
{
    gSaveBlock2Ptr->frontier.field_CA8 = 0;
    gSaveBlock2Ptr->frontier.curChallengeBattleNum = 0;
    gSaveBlock2Ptr->frontier.field_CA9_a = FALSE;
    saved_warp2_set(0, gSaveBlock1Ptr->location.mapGroup, gSaveBlock1Ptr->location.mapNum, -1);
}

static void sub_81B9A28(void)
{
    gSpecialVar_Result = gSaveBlock2Ptr->frontier.field_E6A;
}

static void sub_81B9A44(void)
{
    gSaveBlock2Ptr->frontier.field_E6A = gSpecialVar_0x8006;
}

static void sub_81B9A60(void)
{
    gTrainerBattleOpponent_A = (u32)((Random() % 255) * 5) / 64;
    SetBattleFacilityTrainerGfxId(gTrainerBattleOpponent_A, 0);
}

static void sub_81B9A90(void)
{
    if (gTrainerBattleOpponent_A < 300)
        FrontierSpeechToString(gFacilityTrainers[gTrainerBattleOpponent_A].speechBefore);
}

static void sub_81B9ABC(void)
{
    gSaveBlock2Ptr->frontier.field_CA8 = gSpecialVar_0x8005;
    VarSet(VAR_TEMP_0, 0);
    gSaveBlock2Ptr->frontier.field_CA9_a = TRUE;
    sub_81A4C30();
}

static void sub_81B9B00(void)
{
    gSaveBlock2Ptr->frontier.field_E6A = sVerdanturfTentRewards[Random() % ARRAY_COUNT(sVerdanturfTentRewards)];
}

static void sub_81B9B28(void)
{
    if (AddBagItem(gSaveBlock2Ptr->frontier.field_E6A, 1) == TRUE)
    {
        CopyItemName(gSaveBlock2Ptr->frontier.field_E6A, gStringVar1);
        gSaveBlock2Ptr->frontier.field_E6A = ITEM_NONE;
        gSpecialVar_Result = TRUE;
    }
    else
    {
        gSpecialVar_Result = FALSE;
    }
}

void sub_81B9B80(void)
{
    gUnknown_086160D8[gSpecialVar_0x8004]();
}

static void sub_81B9BA0(void)
{
    gSaveBlock2Ptr->frontier.field_CA8 = 0;
    gSaveBlock2Ptr->frontier.curChallengeBattleNum = 0;
    gSaveBlock2Ptr->frontier.field_CA9_a = FALSE;
    saved_warp2_set(0, gSaveBlock1Ptr->location.mapGroup, gSaveBlock1Ptr->location.mapNum, -1);
}

static void sub_81B9BF4(void)
{
    gSpecialVar_Result = gSaveBlock2Ptr->frontier.field_E6C;
}

static void sub_81B9C10(void)
{
    gSaveBlock2Ptr->frontier.field_E6C = gSpecialVar_0x8006;
}

static void sub_81B9C2C(void)
{
    gSaveBlock2Ptr->frontier.field_CA8 = gSpecialVar_0x8005;
    VarSet(VAR_TEMP_0, 0);
    gSaveBlock2Ptr->frontier.field_CA9_a = TRUE;
    sub_81A4C30();
}

static void sub_81B9C70(void)
{
    gSaveBlock2Ptr->frontier.field_E6C = sFallarborTentRewards[Random() % ARRAY_COUNT(sFallarborTentRewards)];
}

static void sub_81B9C98(void)
{
    if (AddBagItem(gSaveBlock2Ptr->frontier.field_E6C, 1) == TRUE)
    {
        CopyItemName(gSaveBlock2Ptr->frontier.field_E6C, gStringVar1);
        gSaveBlock2Ptr->frontier.field_E6C = ITEM_NONE;
        gSpecialVar_Result = TRUE;
    }
    else
    {
        gSpecialVar_Result = FALSE;
    }
}

static void sub_81B9CF0(void)
{
    GetFrontierTrainerName(gStringVar1, gTrainerBattleOpponent_A);
}

void sub_81B9D08(void)
{
    gUnknown_086160F8[gSpecialVar_0x8004]();
}

static void sub_81B9D28(void)
{
    gSaveBlock2Ptr->frontier.field_CA8 = 0;
    gSaveBlock2Ptr->frontier.curChallengeBattleNum = 0;
    gSaveBlock2Ptr->frontier.field_CA9_a = FALSE;
    saved_warp2_set(0, gSaveBlock1Ptr->location.mapGroup, gSaveBlock1Ptr->location.mapNum, -1);
}

static void sub_81B9D7C(void)
{
    gSpecialVar_Result = gSaveBlock2Ptr->frontier.field_E6E;
}

static void sub_81B9D98(void)
{
    gSaveBlock2Ptr->frontier.field_E6E = gSpecialVar_0x8006;
}

static void sub_81B9DB4(void)
{
    gSaveBlock2Ptr->frontier.field_CA8 = gSpecialVar_0x8005;
    VarSet(VAR_TEMP_0, 0);
    gSaveBlock2Ptr->frontier.field_CA9_a = TRUE;
    sub_81A4C30();
}

static void sub_81B9DF8(void)
{
    gSaveBlock2Ptr->frontier.field_E6E = sSlateportTentRewards[Random() % ARRAY_COUNT(sSlateportTentRewards)];
}

static void sub_81B9E20(void)
{
    if (AddBagItem(gSaveBlock2Ptr->frontier.field_E6E, 1) == TRUE)
    {
        CopyItemName(gSaveBlock2Ptr->frontier.field_E6E, gStringVar1);
        gSaveBlock2Ptr->frontier.field_E6E = ITEM_NONE;
        gSpecialVar_Result = TRUE;
    }
    else
    {
        gSpecialVar_Result = FALSE;
    }
}

static void sub_81B9E78(void)
{
    ZeroPlayerPartyMons();
    DoBattleFactorySelectScreen();
}

static void sub_81B9E88(void)
{
    DoBattleFactorySwapScreen();
}

bool8 sub_81B9E94(void)
{
    return (gMapHeader.regionMapSectionId == MAPSEC_SLATEPORT_CITY
            && ((gMapHeader.mapLayoutId == 385) | (gMapHeader.mapLayoutId == 386)));
}

static void sub_81B9EC0(void)
{
    s32 i, j;
    u8 firstMonId;
    u16 monSetId;
    u16 currSpecies;
    u16 species[PARTY_SIZE];
    u16 monIds[PARTY_SIZE];
    u16 heldItems[PARTY_SIZE];

    firstMonId = 0;
    gFacilityTrainers = gSlateportBattleTentTrainers;
    for (i = 0; i < PARTY_SIZE; i++)
    {
        species[i] = 0;
        monIds[i] = 0;
        heldItems[i] = 0;
    }
    gFacilityTrainerMons = gSlateportBattleTentMons;
    currSpecies = 0;
    i = 0;
    while (i != PARTY_SIZE)
    {
        // Cannot have two pokemon of the same species.
        monSetId = Random() % 70;
        for (j = firstMonId; j < firstMonId + i; j++)
        {
            u16 monId = monIds[j];
            if (monIds[j] == monSetId)
                break;
            if (species[j] == gFacilityTrainerMons[monSetId].species)
            {
                if (currSpecies == 0)
                    currSpecies = gFacilityTrainerMons[monSetId].species;
                else
                    break;
            }
        }
        if (j != i + firstMonId)
            continue;

        // Cannot have two same held items.
        for (j = firstMonId; j < i + firstMonId; j++)
        {
            if (heldItems[j] != 0 && heldItems[j] == gBattleFrontierHeldItems[gFacilityTrainerMons[monSetId].itemTableId])
            {
                if (gFacilityTrainerMons[monSetId].species == currSpecies)
                    currSpecies = 0;
                break;
            }
        }
        if (j != i + firstMonId)
            continue;

        gSaveBlock2Ptr->frontier.field_E70[i].monId = monSetId;
        species[i] = gFacilityTrainerMons[monSetId].species;
        heldItems[i] = gBattleFrontierHeldItems[gFacilityTrainerMons[monSetId].itemTableId];
        monIds[i] = monSetId;
        i++;
    }
}

static void sub_81BA040(void)
{
    u16 trainerId;
    s32 i, j, k;
    register const u16 *monSets asm("r9"); // Fix me. Compiler insists on moving that variable into stack.
    u16 species[3];
    u16 heldItems[3];
    s32 setsCount = 0;

    gFacilityTrainers = gSlateportBattleTentTrainers;
    gFacilityTrainerMons = gSlateportBattleTentMons;

    while (1)
    {
        do
        {
            trainerId = Random() % 30;
            for (i = 0; i < gSaveBlock2Ptr->frontier.curChallengeBattleNum; i++)
            {
                if (gSaveBlock2Ptr->frontier.field_CB4[i] == trainerId)
                    break;
            }
        } while (i != gSaveBlock2Ptr->frontier.curChallengeBattleNum);

        gTrainerBattleOpponent_A = trainerId;
        while (gFacilityTrainers[gTrainerBattleOpponent_A].monSets[setsCount] != 0xFFFF)
            setsCount++;
        if (setsCount > 8)
            break;
        setsCount = 0;
    }

    if (gSaveBlock2Ptr->frontier.curChallengeBattleNum < 2)
        gSaveBlock2Ptr->frontier.field_CB4[gSaveBlock2Ptr->frontier.curChallengeBattleNum] = gTrainerBattleOpponent_A;

    monSets = gFacilityTrainers[gTrainerBattleOpponent_A].monSets;
    i = 0;
    while (i != 3)
    {
        sRandMonSetId = monSets[Random() % setsCount];
        for (j = 0; j < 6; j++)
        {
            if (gFacilityTrainerMons[sRandMonSetId].species == gFacilityTrainerMons[gSaveBlock2Ptr->frontier.field_E70[j].monId].species)
                break;
        }
        if (j != 6)
            continue;

        for (k = 0; k < i; k++)
        {
            if (species[k] == gFacilityTrainerMons[sRandMonSetId].species)
                break;
        }
        if (k != i)
            continue;

        for (k = 0; k < i; k++)
        {
            if (heldItems[k] != 0 && heldItems[k] == gBattleFrontierHeldItems[gFacilityTrainerMons[sRandMonSetId].itemTableId])
                break;
        }
        if (k != i)
            continue;

        species[i] = gFacilityTrainerMons[sRandMonSetId].species;
        heldItems[i] = gBattleFrontierHeldItems[gFacilityTrainerMons[sRandMonSetId].itemTableId];
        gUnknown_03006298[i] = sRandMonSetId;
        i++;
    }
}