summaryrefslogtreecommitdiff
path: root/src/use_pokeblock.c
blob: 12f475c93b5bef25580f1161acc7c4becf80220e (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
#include "global.h"
#include "main.h"
#include "pokeblock.h"
#include "malloc.h"
#include "palette.h"
#include "pokenav.h"
#include "unknown_task.h"
#include "text.h"
#include "bg.h"
#include "window.h"
#include "text_window.h"

struct UsePokeblockSubStruct
{
    void (*field_0)(void);
    void (*callback)(void);
    struct Pokeblock *pokeblock;
    struct Pokemon *pokemon;
    u8 stringBuffer[0x40];
    u8 field_50;
    u8 field_51;
    u8 field_52;
    u8 field_53;
    u8 field_54;
    u8 field_55;
    u8 field_56;
    u8 field_57[5];
    u8 field_5c[5];
    u8 field_61[5];
    s16 field_66[5];
};

struct UsePokeblockStruct
{
    u8 field_0[0x7C58];
    u8 field_7C58[0x378];
    struct UsePokeblockSubStruct info;
};

extern u16 gKeyRepeatStartDelay;

// this file's functions
void sub_816636C(void (*func)(void));
void sub_8166380(void);
void sub_816631C(void);
void sub_81662C0(void);
void sub_8166564(void);
void sub_8166304(void);
void sub_81668F8(void);
void sub_8167420(void);
void sub_8167760(void);
u8 sub_81672E4(u8 arg0);
bool8 sub_8168328(void);
bool8 sub_8167930(void);
void sub_8167608(u8 arg0);
void sub_8167BA0(u16 arg0, u8 copyToVramMode);

extern const struct BgTemplate gUnknown_085DFCCC[4];
extern const struct WindowTemplate gUnknown_085DFCDC[];

// ram variables
EWRAM_DATA struct UsePokeblockSubStruct *gUnknown_0203BC90 = NULL;
EWRAM_DATA void (*gUnknown_0203BC94)(void) = NULL;
EWRAM_DATA struct Pokeblock *gUnknown_0203BC98 = NULL;
EWRAM_DATA u8 gPokeblockMonId = 0;
EWRAM_DATA s16 gPokeblockGain = 0;
EWRAM_DATA void *gUnknown_0203BCA0 = NULL;
EWRAM_DATA void *gUnknown_0203BCA4 = NULL;
EWRAM_DATA void *gUnknown_0203BCA8 = NULL;
EWRAM_DATA struct UsePokeblockStruct *gUnknown_0203BCAC = NULL;

// const rom data
// todo: make it static once the file is decompiled

// code
void ChooseMonToGivePokeblock(struct Pokeblock *pokeblock, void (*callback)(void))
{
    gUnknown_0203BCAC = AllocZeroed(0x806C);
    gUnknown_0203BC90 = &gUnknown_0203BCAC->info;
    gUnknown_0203BC90->pokeblock = pokeblock;
    gUnknown_0203BC90->callback = callback;
    sub_816636C(sub_8166380);
    SetMainCallback2(sub_816631C);
}

void CB2_ReturnAndChooseMonToGivePokeblock(void)
{
    gUnknown_0203BCAC = AllocZeroed(0x806C);
    gUnknown_0203BC90 = &gUnknown_0203BCAC->info;
    gUnknown_0203BC90->pokeblock = gUnknown_0203BC98;
    gUnknown_0203BC90->callback = gUnknown_0203BC94;
    gPokeblockMonId = sub_81672E4(gPokeblockMonId);
    gUnknown_0203BC90->field_56 = gPokeblockMonId < 4 ? 0 : 1;
    sub_816636C(sub_8166380);
    SetMainCallback2(sub_81662C0);
}

void sub_81662C0(void)
{
    gUnknown_0203BC90->field_0();
    AnimateSprites();
    BuildOamBuffer();
    UpdatePaletteFade();
    if (gUnknown_0203BC90->field_0 == sub_8166564)
    {
        gUnknown_0203BC90->field_50 = 0;
        SetMainCallback2(sub_8166304);
    }
}

void sub_8166304(void)
{
    sub_81668F8();
    AnimateSprites();
    BuildOamBuffer();
    UpdatePaletteFade();
}

void sub_816631C(void)
{
    gUnknown_0203BC90->field_0();
    AnimateSprites();
    BuildOamBuffer();
    RunTextPrinters();
    UpdatePaletteFade();
}

void sub_8166340(void)
{
    LoadOam();
    ProcessSpriteCopyRequests();
    TransferPlttBuffer();
    sub_81D2108(gUnknown_0203BCAC->field_7C58);
    sub_80BA0A8();
}

void sub_816636C(void (*func)(void))
{
    gUnknown_0203BC90->field_0 = func;
    gUnknown_0203BC90->field_50 = 0;
}

void sub_8166380(void)
{
    switch (gUnknown_0203BC90->field_50)
    {
        case 0:
            gUnknown_0203BCAC->field_0[0x7B10] = 0xFF;
            sub_81D1ED4(gUnknown_0203BCAC->field_7C58);
            gUnknown_0203BC90->field_50++;
            break;
        case 1:
            ResetSpriteData();
            FreeAllSpritePalettes();
            gUnknown_0203BC90->field_50++;
            break;
        case 2:
            SetVBlankCallback(NULL);
            CpuFill32(0, (void*)(VRAM), VRAM_SIZE);
            gUnknown_0203BC90->field_50++;
            break;
        case 3:
            ResetBgsAndClearDma3BusyFlags(0);
            InitBgsFromTemplates(0, gUnknown_085DFCCC, ARRAY_COUNT(gUnknown_085DFCCC));
            InitWindows(gUnknown_085DFCDC);
            DeactivateAllTextPrinters();
            sub_809882C(0, 0x97, 0xE0);
            gUnknown_0203BC90->field_50++;
            break;
        case 4:
            gUnknown_0203BC90->field_50++;
            break;
        case 5:
            if (!sub_8168328())
            {
                gUnknown_0203BC90->field_50++;
            }
            break;
        case 6:
            gKeyRepeatStartDelay = 20;
            sub_8167420();
            gUnknown_0203BC90->field_50++;
            break;
        case 7:
            if (!sub_8167930())
            {
                gUnknown_0203BC90->field_50++;
            }
            break;
        case 8:
            sub_8167608(0);
            sub_8167760();
            gUnknown_0203BC90->field_50++;
            break;
        case 9:
            if (!sub_81D312C(&gUnknown_0203BCAC->field_0[0x7B0E]))
            {
                gUnknown_0203BC90->field_50++;
            }
            break;
        case 10:
            gUnknown_0203BC90->field_50++;
            break;
        case 11:
            sub_81D2754(gUnknown_0203BCAC->field_7C58, &gUnknown_0203BCAC->field_0[0x7C6C]);
            sub_81D20AC(gUnknown_0203BCAC->field_7C58);
            gUnknown_0203BC90->field_50++;
            break;
        case 12:
            if (!sub_81D20BC(gUnknown_0203BCAC->field_7C58))
            {
                sub_81D1F84(gUnknown_0203BCAC->field_7C58, &gUnknown_0203BCAC->field_7C58[0x14], &gUnknown_0203BCAC->field_7C58[0x14]);
                gUnknown_0203BC90->field_50++;
            }
            break;
        case 13:
            sub_81D2230(gUnknown_0203BCAC->field_7C58);
            gUnknown_0203BC90->field_50++;
            break;
        case 14:
            PutWindowTilemap(0);
            PutWindowTilemap(1);
            sub_8167BA0(0, 1);
            gUnknown_0203BC90->field_50++;
            break;
        case 15:
            sub_816636C(sub_8166564);
            break;
    }
}