summaryrefslogtreecommitdiff
path: root/src/battle_party_menu.c
blob: 79c926e0d075827f79d90b9b24e4d93aa39ea2c3 (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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
#include "global.h"
#include "asm.h"
#include "main.h"
#include "menu.h"
#include "palette.h"
#include "pokemon.h"
#include "songs.h"
#include "sound.h"
#include "string_util.h"
#include "task.h"
#include "text.h"
#include "battle_party_menu.h"

extern u8 IsLinkDoubleBattle(void);
extern void TryCreatePartyMenuMonIcon(u8, u8, struct Pokemon *);
extern void LoadHeldItemIconGraphics(void);
extern void CreateHeldItemIcons_806DC34();
extern u8 sub_806BD58(u8, u8);
extern void PartyMenuPrintMonsLevelOrStatus(void);
extern void PrintPartyMenuMonNicknames(void);
extern void PartyMenuTryPrintMonsHP(void);
extern void nullsub_13(void);
extern void PartyMenuDrawHPBars(void);
extern u8 sub_806B58C(u8);
extern u8 GetItemEffectType();
extern void sub_806E750(u8, const struct PartyPopupMenu *, const struct PartyMenuItem *, int);
extern u16 sub_806BD80();
extern u8 sub_806CA38();
extern void sub_806D5A4(void);
extern void sub_802E414(void);
extern void sub_8094D60(void);
extern void sub_80A6DCC(void);
extern void sub_806AF4C();
extern u8 sub_80F9344(void);
extern u8 sub_806B124(void);
extern void sub_806C994();
extern void sub_806BF74();
extern void sub_806AEDC(void);
extern TaskFunc PartyMenuGetPopupMenuFunc(u8, const struct PartyPopupMenu *, const struct PartyMenuItem *, u8);
extern void ShowPokemonSummaryScreen(struct Pokemon *, u8, u8, void (*)(u8), int);
extern void sub_806E7D0(u8, const struct PartyPopupMenu *);
extern u8 *sub_8040D08();
extern void sub_806E834();
extern u8 sub_8094C20();
extern void sub_8040B8C(void);
extern u8 pokemon_order_func(u8);
extern void sub_8094C98(u8, u8);
extern void sub_806E6F0();
extern void sub_806D538();

extern u16 gScriptItemId;
extern u8 gPlayerPartyCount;
extern u8 gUnknown_02024A68;
extern u16 gUnknown_02024A6A[];
extern u8 gUnknown_02024E6C;
extern u8 gUnknown_0202E8F4;
extern u8 gUnknown_0202E8F5;
extern u8 gUnknown_0202E8F6;
extern u8 gUnknown_02038473;
extern u8 gUnknown_020384F0;
extern void (*gUnknown_03004AE4)();  //don't know types yet
extern const u8 gOtherText_CantSwitchPokeWithYours[];
extern const u8 gOtherText_NoEnergyLeft[];
extern const u8 gOtherText_EGGCantBattle[];
extern const u8 gOtherText_AlreadySelected[];
extern const u8 gOtherText_CantBeSwitched[];
extern const u8 gOtherText_AlreadyBattle[];
extern const u8 OtherText_Summary[];
extern const u8 gOtherText_CancelNoTerminator[];
extern const u8 OtherText_Shift[];
extern const u8 OtherText_SendOut[];

static void Task_809527C(u8);
static void Task_80952B4(u8);
static void Task_80952E4(u8);
static void Task_8095330(u8);
static void Task_809538C(void);
static void Task_HandlePopupMenuInput(u8);
static void Task_BattlePartyMenuSummary(u8 taskId);
static void Task_BattlePartyMenuShift(u8 taskId);
static void Task_BattlePartyMenuCancel(u8 taskId);

static const struct PartyMenuItem sBattlePartyMenuActions[] =
{
    {OtherText_Summary,             Task_BattlePartyMenuSummary},
    {gOtherText_CancelNoTerminator, Task_BattlePartyMenuCancel},
    {OtherText_Shift,               Task_BattlePartyMenuShift},
    {OtherText_SendOut,             Task_BattlePartyMenuShift},
};
static const u8 Unknown_83B5FEC[] = {2, 0, 1};  //SHIFT, SUMMARY, CANCEL
static const u8 Unknown_83B5FEF[] = {3, 0, 1};  //SEND OUT, SUMMARY, CANCEL
static const u8 Unknown_83B5FF2[] = {0, 1};     //SUMMARY, CANCEL
static const struct PartyPopupMenu sBattlePartyPopupMenus[] =
{
    {ARRAY_COUNT(Unknown_83B5FEC), 9, Unknown_83B5FEC},
    {ARRAY_COUNT(Unknown_83B5FEF), 9, Unknown_83B5FEF},
    {ARRAY_COUNT(Unknown_83B5FF2), 9, Unknown_83B5FF2},
};

// rom_8094928 is part of this file, has yet to be decompiled.

int SetUpBattlePartyMenu(void)
{
    switch (EWRAM_1B000.unk264)
    {
    case 0:
        //TODO: try to get rid of this duplicate code
        if (IsLinkDoubleBattle() == TRUE)
        {
            if (EWRAM_1B000.unk266 != 6)
            {
                TryCreatePartyMenuMonIcon(EWRAM_1B000.unk260, EWRAM_1B000.unk266, &gPlayerParty[EWRAM_1B000.unk266]);
                EWRAM_1B000.unk266++;
            }
            else
            {
                EWRAM_1B000.unk266 = 0;
                EWRAM_1B000.unk264++;
            }
        }
        else
        {
            if (EWRAM_1B000.unk266 < 6)
            {
                TryCreatePartyMenuMonIcon(EWRAM_1B000.unk260, EWRAM_1B000.unk266, &gPlayerParty[EWRAM_1B000.unk266]);
                EWRAM_1B000.unk266++;
            }
            else
            {
                EWRAM_1B000.unk266 = 0;
                EWRAM_1B000.unk264++;
            }                
        }
        break;
    case 1:
        LoadHeldItemIconGraphics();
        EWRAM_1B000.unk264++;
        break;
    case 2:
        CreateHeldItemIcons_806DC34(EWRAM_1B000.unk260);
        EWRAM_1B000.unk264++;
        break;
    case 3:
        if (sub_806BD58(EWRAM_1B000.unk260, EWRAM_1B000.unk266) == 1)
        {
            EWRAM_1B000.unk266 = 0;
            EWRAM_1B000.unk264++;
        }
        else
            EWRAM_1B000.unk266++;
        break;
    case 4:
        PartyMenuPrintMonsLevelOrStatus();
        EWRAM_1B000.unk264++;
        break;
    case 5:
        PrintPartyMenuMonNicknames();
        EWRAM_1B000.unk264++;
        break;
    case 6:
        PartyMenuTryPrintMonsHP();
        EWRAM_1B000.unk264++;
        break;
    case 7:
        nullsub_13();
        EWRAM_1B000.unk264++;
        break;
    case 8:
        PartyMenuDrawHPBars();
        EWRAM_1B000.unk264++;
        break;
    case 9:
        if (sub_806B58C(EWRAM_1B000.unk266) == 1)
        {
            EWRAM_1B000.unk266 = 0;
            EWRAM_1B000.unk264++;
        }
        else
            EWRAM_1B000.unk266++;
        break;
    case 10:
        if (gUnknown_02038473 == 3)
        {
            if (GetItemEffectType(gScriptItemId) == 10)
                EWRAM_1B000.unk259 = 0xFF;
            else
                EWRAM_1B000.unk259 = 3;
        }
        return TRUE;
    }
    return FALSE;
}

static void sub_8095050(u8 a, u8 b)
{
    if (!GetMonData(&gPlayerParty[b], MON_DATA_IS_EGG))
    {
        if (gUnknown_02038473 == 1)
        {
            gTasks[EWRAM_1B000.unk260].data[4] = 1;
            gTasks[EWRAM_1B000.unk260].data[5] = 1;
        }
        else
        {
            gTasks[EWRAM_1B000.unk260].data[4] = 0;
            gTasks[EWRAM_1B000.unk260].data[5] = 0;
        }
    }
    else
    {
        gTasks[EWRAM_1B000.unk260].data[4] = 2;
        gTasks[EWRAM_1B000.unk260].data[5] = 2;
    }
    sub_806E750(gTasks[a].data[4], sBattlePartyPopupMenus, sBattlePartyMenuActions, 0);
}

void SetUpBattlePokemonMenu(u8 a)
{
    if (!gPaletteFade.active)
    {
        if (gUnknown_02038473 == 3 && GetItemEffectType(gScriptItemId) == 10)
        {
            gUnknown_03004AE4(a, gScriptItemId, Task_80952E4);
            return;
        }
        
        switch (sub_806BD80(a))
        {
        case 1:
            if (gUnknown_02038473 == 3)
            {
                if (GetMonData(&gPlayerParty[sub_806CA38(a)], MON_DATA_IS_EGG))
                    PlaySE(SE_HAZURE);
                else
                {
                    sub_806D5A4();
                    gUnknown_03004AE4(a, gScriptItemId, Task_80952E4);
                }
            }
            else
            {
                PlaySE(SE_SELECT);
                GetMonNickname(&gPlayerParty[sub_806CA38(a)], gStringVar1);
                sub_8095050(a, sub_806CA38(a));
                SetTaskFuncWithFollowupFunc(a, Task_HandlePopupMenuInput, SetUpBattlePokemonMenu);
            }
            break;
        case 2:
            if (gUnknown_02038473 == 1)
                PlaySE(SE_HAZURE);
            else
            {
                PlaySE(SE_SELECT);
                if (gUnknown_02038473 == 3)
                {
                    gUnknown_0202E8F4 = 0;
                    gTasks[a].func = Task_80952E4;
                }
                else
                {
                    gUnknown_0202E8F4 = 0;
                    gTasks[a].func = Task_809527C;
                }
            }
            break;
        }
    }
}

static void Task_809527C(u8 taskId)
{
    BeginNormalPaletteFade(0xFFFFFFFF, 0, 0, 16, 0);
    gTasks[taskId].func = Task_80952B4;
}

static void Task_80952B4(u8 taskId)
{
    if (!gPaletteFade.active)
    {
        sub_8094D60();
        DestroyTask(taskId);
        SetMainCallback2(sub_802E414);
    }
}

static void Task_80952E4(u8 taskId)
{
    if (gUnknown_0202E8F4 != 0)
        Task_809527C(taskId);
    else
    {
        BeginNormalPaletteFade(0xFFFFFFFF, 0, 0, 16, 0);
        gTasks[taskId].func = Task_8095330;
    }
}

static void Task_8095330(u8 taskId)
{
    if (!gPaletteFade.active)
    {
        sub_8094D60();
        DestroyTask(taskId);
        sub_80A6DCC();
    }
}

static void Task_809535C(u8 taskId)
{
    gPaletteFade.bufferTransferDisabled = TRUE;
    sub_806AF4C(1, 0xFF, SetUpBattlePokemonMenu, 5);
    SetMainCallback2(Task_809538C);
}

static void Task_809538C(void)
{
    do
    {
        if (sub_806B124() == 1)
        {
            sub_806C994(EWRAM_1B000.unk260, gUnknown_020384F0);
            sub_806BF74(EWRAM_1B000.unk260, 0);
            GetMonNickname(&gPlayerParty[gUnknown_020384F0], gStringVar1);
            sub_8095050(EWRAM_1B000.unk260, gUnknown_020384F0);
            SetTaskFuncWithFollowupFunc(EWRAM_1B000.unk260, Task_HandlePopupMenuInput, SetUpBattlePokemonMenu);
            SetMainCallback2(sub_806AEDC);
            return;
        }
    } while (sub_80F9344() != 1);
}

static void Task_HandlePopupMenuInput(u8 taskId)
{
    TaskFunc func;
    
    if (!gPaletteFade.active)
    {
        if (gMain.newAndRepeatedKeys & DPAD_UP)
        {
            PlaySE(SE_SELECT);
            MoveMenuCursor(-1);
            return;
        }
        if (gMain.newAndRepeatedKeys & DPAD_DOWN)
        {
            PlaySE(SE_SELECT);
            MoveMenuCursor(1);
            return;
        }
        if (gMain.newKeys & A_BUTTON)
        {            
            PlaySE(SE_SELECT);
            func = PartyMenuGetPopupMenuFunc(gTasks[taskId].data[4],
                               sBattlePartyPopupMenus,
                               sBattlePartyMenuActions,
                               GetMenuCursorPos());
            func(taskId);
            return;
        }
        if (gMain.newKeys & B_BUTTON)
        {
            PlaySE(SE_SELECT);
            Task_BattlePartyMenuCancel(taskId);
            return;
        }
    }
}

static void Task_80954C0(u8 taskId)
{
    if (gUnknown_0202E8F6 == 0)
        Task_BattlePartyMenuCancel(taskId);
}

static void Task_ShowSummaryScreen(u8 taskId)
{
    u8 partySelection = sub_806CA38(taskId);
    
    if (!gPaletteFade.active)
    {
        DestroyTask(taskId);
        EWRAM_1B000.unk262 = 1;
        ShowPokemonSummaryScreen(gPlayerParty, partySelection, gPlayerPartyCount - 1, Task_809535C, 4);
    }
}

static void Task_BattlePartyMenuSummary(u8 taskId)
{
    sub_806CA38(taskId);  //an unused variable was probably set with this.
    gTasks[taskId].func = Task_ShowSummaryScreen;
    BeginNormalPaletteFade(0xFFFFFFFF, 0, 0, 16, 0);
}

static void Task_BattlePartyMenuShift(u8 taskId)
{
    u8 partySelection;
    u8 i;
    u8 r4;
    
    sub_806E7D0(gTasks[taskId].data[4], sBattlePartyPopupMenus);
    partySelection = sub_806CA38(taskId);
    if (IsLinkDoubleBattle() == TRUE && (partySelection == 1 || partySelection == 4 || partySelection == 5))
    {
        sub_806D5A4();
        StringCopy(gStringVar1, sub_8040D08());
        StringExpandPlaceholders(gStringVar4, gOtherText_CantSwitchPokeWithYours);
        sub_806E834(gStringVar4, 0);
        gTasks[taskId].func = Task_80954C0;
        return;
    }
    if (GetMonData(&gPlayerParty[partySelection], MON_DATA_HP) == 0)
    {
        sub_806D5A4();
        GetMonNickname(&gPlayerParty[partySelection], gStringVar1);
        StringExpandPlaceholders(gStringVar4, gOtherText_NoEnergyLeft);
        sub_806E834(gStringVar4, 0);
        gTasks[taskId].func = Task_80954C0;
        return;
    }
    for (i = 0; i < gUnknown_02024A68; i++)
    {
        if (battle_side_get_owner(i) == 0
         && sub_8094C20(partySelection) == gUnknown_02024A6A[i])
        {
            sub_806D5A4();
            GetMonNickname(&gPlayerParty[partySelection], gStringVar1);
            StringExpandPlaceholders(gStringVar4, gOtherText_AlreadyBattle);
            sub_806E834(gStringVar4, 0);
            gTasks[taskId].func = Task_80954C0;
            return;
        }
    }
    if (GetMonData(&gPlayerParty[partySelection], MON_DATA_IS_EGG))
    {
        sub_806D5A4();
        StringExpandPlaceholders(gStringVar4, gOtherText_EGGCantBattle);
        sub_806E834(gStringVar4, 0);
        gTasks[taskId].func = Task_80954C0;
        return;
    }
    if (sub_8094C20(partySelection) == EWRAM_1609D)
    {
        sub_806D5A4();
        GetMonNickname(&gPlayerParty[partySelection], gStringVar1);
        StringExpandPlaceholders(gStringVar4, gOtherText_AlreadySelected);
        sub_806E834(gStringVar4, 0);
        gTasks[taskId].func = Task_80954C0;
        return;
    }
    if (gUnknown_02038473 == 4)
    {
        sub_806D5A4();
        sub_8040B8C();
        sub_806E834(gStringVar4, 0);
        gTasks[taskId].func = Task_80954C0;
        return;
    }
    if (gUnknown_02038473 == 2)
    {
        u8 r0;
        u8 r4 = gUnknown_02024E6C;
        
        sub_806D5A4();
        r0 = pokemon_order_func(gUnknown_02024A6A[r4]);
        GetMonNickname(&gPlayerParty[r0], gStringVar1);
        StringExpandPlaceholders(gStringVar4, gOtherText_CantBeSwitched);
        sub_806E834(gStringVar4, 0);
        gTasks[taskId].func = Task_80954C0;
        return;
    }
    gUnknown_0202E8F5 = sub_8094C20(partySelection);
    gUnknown_0202E8F4 = 1;
    r4 = pokemon_order_func(gUnknown_02024A6A[gUnknown_02024E6C]);
    sub_8094C98(r4, partySelection);
    sub_806E6F0(&gPlayerParty[r4], &gPlayerParty[partySelection]);
    gTasks[taskId].func = Task_809527C;
}

static void Task_BattlePartyMenuCancel(u8 taskId)
{
    sub_8072DEC();
    sub_806E7D0(gTasks[taskId].data[4], sBattlePartyPopupMenus);
    gTasks[taskId].data[4] = gTasks[taskId].data[5];
    sub_806D538(0, 0);
    SwitchTaskToFollowupFunc(taskId);
}