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
|
#include "global.h"
#include "pokemon.h"
#include "save_block_2.h"
#include "pokemon_storage_system.h"
#include "msgdata.h"
#include "string16.h"
#pragma thumb on
void PCStorage_init(struct PCStorage * pc)
{
PCStorage_InitializeBoxes(pc);
SaveSetDirtyBit();
}
u32 PCStorage_sizeof(void)
{
return sizeof(struct PCStorage);
}
void PCStorage_InitializeBoxes(struct PCStorage * pc)
{
int i, j;
for (i = 0; i < NUM_BOXES; i++)
{
for (j = 0; j < MONS_PER_BOX; j++)
{
ZeroBoxMonData(&pc->boxes[i][j]);
}
}
for (i = 0, j = 0; i < NUM_BOXES; i++)
{
pc->wallpapers[i] = (u8)j;
j++;
if (j >= NUM_DEFAULT_WALLPAPERS)
j = 0;
}
pc->unlockedWallpapers = 0;
struct MsgData * msgData = NewMsgDataFromNarc(1, NARC_MSGDATA_MSG, 14, 0);
if (msgData != NULL)
{
for (i = 0; i < NUM_BOXES; i++)
{
ReadMsgDataIntoU16Array(msgData, (u32)(i + 6), pc->names[i]);
}
DestroyMsgData(msgData);
}
pc->curBox = 0;
}
BOOL PCStorage_PlaceMonInFirstEmptySlotInAnyBox(struct PCStorage * pc, struct BoxPokemon * boxmon)
{
int i = (int)pc->curBox;
do
{
RestoreBoxMonPP(boxmon);
if (PCStorage_PlaceMonInBoxFirstEmptySlot(pc, i, boxmon))
{
SaveSetDirtyBit();
return TRUE;
}
i++;
if (i >= NUM_BOXES)
i = 0;
} while (i != pc->curBox);
return FALSE;
}
BOOL PCStorage_PlaceMonInBoxFirstEmptySlot(struct PCStorage * pc, int boxno, struct BoxPokemon * boxmon)
{
RestoreBoxMonPP(boxmon);
if (boxno == -1)
boxno = (int)pc->curBox;
for (int i = 0; i < MONS_PER_BOX; i++)
{
if (GetBoxMonData(&pc->boxes[boxno][i], MON_DATA_SPECIES, NULL) == SPECIES_NONE)
{
pc->boxes[boxno][i] = *boxmon;
SaveSetDirtyBit();
return TRUE;
}
}
return FALSE;
}
BOOL PCStorage_PlaceMonInBoxByIndexPair(struct PCStorage * pc, int boxno, int slotno, struct BoxPokemon * boxmon)
{
RestoreBoxMonPP(boxmon);
if (boxno == -1)
boxno = (int)pc->curBox;
if (boxno < NUM_BOXES && slotno < MONS_PER_BOX)
{
pc->boxes[boxno][slotno] = *boxmon;
SaveSetDirtyBit();
return TRUE;
}
GF_ASSERT(0);
return FALSE;
}
void PCStorage_DeleteBoxMonByIndexPair(struct PCStorage * pc, int boxno, int slotno)
{
if (boxno == -1)
boxno = (int)pc->curBox;
if (slotno < MONS_PER_BOX && boxno < NUM_BOXES)
{
ZeroBoxMonData(&pc->boxes[boxno][slotno]);
SaveSetDirtyBit();
return;
}
GF_ASSERT(0);
}
int PCStorage_GetActiveBox(struct PCStorage * pc)
{
return (int)pc->curBox;
}
int PCStorage_FindFirstBoxWithEmptySlot(struct PCStorage * pc)
{
int i, j;
i = (int)pc->curBox;
do
{
for (j = 0; j < (int)MONS_PER_BOX; j++)
{
if (!GetBoxMonData(&pc->boxes[i][j], MON_DATA_SPECIES_EXISTS, NULL))
return i;
}
i++;
if (i >= (int)NUM_BOXES)
i = 0;
} while (i != pc->curBox);
return (int)NUM_BOXES;
}
BOOL PCStorage_FindFirstEmptySlot(struct PCStorage * pc, int * boxno, int * slotno)
{
if (*boxno == -1)
*boxno = (int)pc->curBox;
int i = *boxno, j = *slotno;
do
{
while (j < (int)MONS_PER_BOX)
{
if (!GetBoxMonData(&pc->boxes[i][j], MON_DATA_SPECIES_EXISTS, NULL))
{
*boxno = i;
*slotno = j;
return TRUE;
}
j++;
}
i++;
if (i >= (int)NUM_BOXES)
i = 0;
if (i == *boxno)
break;
j = 0;
} while (1);
return (int)NUM_BOXES; // bug: should return FALSE if reached
}
int PCStorage_CountMonsAndEggsInAllBoxes(struct PCStorage * pc)
{
int i, j, count = 0;
for (i = 0; i < (int)NUM_BOXES; i++)
{
for (j = 0; j < (int)MONS_PER_BOX; j++)
{
if (GetBoxMonData(&pc->boxes[i][j], MON_DATA_SPECIES_EXISTS, NULL))
count++;
}
}
return count;
}
void PCStorage_SetActiveBox(struct PCStorage * pc, int boxno)
{
if (boxno < NUM_BOXES)
{
pc->curBox = (u32)boxno;
SaveSetDirtyBit();
return;
}
GF_ASSERT(0);
}
u8 PCStorage_GetBoxWallpaper(struct PCStorage * pc, int boxno)
{
if (boxno < NUM_BOXES)
return pc->wallpapers[boxno];
GF_ASSERT(0);
return 0;
}
void PCStorage_SetBoxWallpaper(struct PCStorage * pc, int boxno, u8 wallpaper)
{
if (boxno == -1)
boxno = (int)pc->curBox;
if (boxno < NUM_BOXES && wallpaper < NUM_WALLPAPERS)
{
pc->wallpapers[boxno] = wallpaper;
SaveSetDirtyBit();
return;
}
GF_ASSERT(0);
}
void PCStorage_GetBoxName(struct PCStorage * pc, int boxno, struct String * ret)
{
if (boxno == -1)
boxno = (int)pc->curBox;
if (boxno < NUM_BOXES)
{
CopyU16ArrayToString(ret, pc->names[boxno]);
return;
}
GF_ASSERT(0);
}
void PCStorage_SetBoxName(struct PCStorage * pc, int boxno, struct String * src)
{
if (boxno == -1)
boxno = (int)pc->curBox;
if (boxno < NUM_BOXES)
{
CopyStringToU16Array(src, pc->names[boxno], BOX_NAME_LENGTH);
SaveSetDirtyBit();
}
}
int PCStorage_CountMonsAndEggsInBox(struct PCStorage * pc, int boxno)
{
if (boxno == -1)
boxno = (int)pc->curBox;
if (boxno < NUM_BOXES)
{
int i, count = 0;
for (i = 0; i < (int)MONS_PER_BOX; i++)
{
if (GetBoxMonData(&pc->boxes[boxno][i], MON_DATA_SPECIES_EXISTS, NULL))
count++;
}
return count;
}
GF_ASSERT(0);
return 0;
}
int PCStorage_CountMonsInBox(struct PCStorage * pc, int boxno)
{
if (boxno == -1)
boxno = (int)pc->curBox;
if (boxno < NUM_BOXES)
{
int i, count = 0;
for (i = 0; i < (int)MONS_PER_BOX; i++)
{
if (GetBoxMonData(&pc->boxes[boxno][i], MON_DATA_SPECIES_EXISTS, NULL) && !GetBoxMonData(&pc->boxes[boxno][i], MON_DATA_IS_EGG, NULL))
count++;
}
return count;
}
GF_ASSERT(0);
return 0;
}
int PCStorage_CountMonsInAllBoxes(struct PCStorage * pc)
{
int count, i;
for (i = 0, count = 0; i < NUM_BOXES; i++)
{
count += PCStorage_CountMonsInBox(pc, i);
}
return count;
}
void PCStorage_SetBoxMonDataByIndexPair(struct PCStorage * pc, int boxno, int slotno, u32 attr, void * value)
{
if (boxno >= NUM_BOXES)
GF_ASSERT(boxno == -1);
GF_ASSERT(slotno < MONS_PER_BOX);
if (boxno == -1)
boxno = (int)pc->curBox;
SetBoxMonData(&pc->boxes[boxno][slotno], (int)attr, value);
SaveSetDirtyBit();
}
struct BoxPokemon * PCStorage_GetMonByIndexPair(struct PCStorage * pc, int boxno, int slotno)
{
if (boxno >= NUM_BOXES)
GF_ASSERT(boxno == -1);
GF_ASSERT(slotno < MONS_PER_BOX);
if (boxno == -1)
boxno = (int)pc->curBox;
return &pc->boxes[boxno][slotno];
}
void PCStorage_UnlockBonusWallpaper(struct PCStorage * pc, int wallpaper)
{
GF_ASSERT(wallpaper < NUM_BONUS_WALLPAPERS);
pc->unlockedWallpapers |= (1u << wallpaper);
SaveSetDirtyBit();
}
BOOL PCStorage_IsBonusWallpaperUnlocked(struct PCStorage * pc, int wallpaper)
{
GF_ASSERT(wallpaper < NUM_BONUS_WALLPAPERS);
return (pc->unlockedWallpapers & (1u << wallpaper)) != 0;
}
int PCStorage_CountUnlockedBonusWallpapers(struct PCStorage * pc)
{
int i, count;
for (i = 0, count = 0; i < NUM_BONUS_WALLPAPERS; i++)
{
if (PCStorage_IsBonusWallpaperUnlocked(pc, i))
count++;
}
return count;
}
|