forked from pret/pokeemerald
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathdecompress.c
411 lines (353 loc) · 12.2 KB
/
decompress.c
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 "malloc.h"
#include "data.h"
#include "decompress.h"
#include "pokemon.h"
#include "text.h"
EWRAM_DATA ALIGNED(4) u8 gDecompressionBuffer[0x4000] = {0};
static void DuplicateDeoxysTiles(void *pointer, s32 species);
void LZDecompressWram(const u32 *src, void *dest)
{
LZ77UnCompWram(src, dest);
}
void LZDecompressVram(const u32 *src, void *dest)
{
LZ77UnCompVram(src, dest);
}
u16 LoadCompressedSpriteSheet(const struct CompressedSpriteSheet *src)
{
struct SpriteSheet dest;
LZ77UnCompWram(src->data, gDecompressionBuffer);
dest.data = gDecompressionBuffer;
dest.size = src->size;
dest.tag = src->tag;
return LoadSpriteSheet(&dest);
}
void LoadCompressedSpriteSheetOverrideBuffer(const struct CompressedSpriteSheet *src, void *buffer)
{
struct SpriteSheet dest;
LZ77UnCompWram(src->data, buffer);
dest.data = buffer;
dest.size = src->size;
dest.tag = src->tag;
LoadSpriteSheet(&dest);
}
void LoadCompressedSpritePalette(const struct CompressedSpritePalette *src)
{
struct SpritePalette dest;
LZ77UnCompWram(src->data, gDecompressionBuffer);
dest.data = (void *) gDecompressionBuffer;
dest.tag = src->tag;
LoadSpritePalette(&dest);
}
void LoadCompressedSpritePaletteOverrideBuffer(const struct CompressedSpritePalette *src, void *buffer)
{
struct SpritePalette dest;
LZ77UnCompWram(src->data, buffer);
dest.data = buffer;
dest.tag = src->tag;
LoadSpritePalette(&dest);
}
void DecompressPicFromTable(const struct CompressedSpriteSheet *src, void *buffer, s32 species)
{
if (species > NUM_SPECIES)
LZ77UnCompWram(gMonFrontPicTable[0].data, buffer);
else
LZ77UnCompWram(src->data, buffer);
DuplicateDeoxysTiles(buffer, species);
}
void HandleLoadSpecialPokePic(const struct CompressedSpriteSheet *src, void *dest, s32 species, u32 personality)
{
bool8 isFrontPic;
if (src == &gMonFrontPicTable[species])
isFrontPic = TRUE; // frontPic
else
isFrontPic = FALSE; // backPic
LoadSpecialPokePic_2(src, dest, species, personality, isFrontPic);
}
void LoadSpecialPokePic(const struct CompressedSpriteSheet *src, void *dest, s32 species, u32 personality, bool8 isFrontPic)
{
if (species == SPECIES_UNOWN)
{
u16 i = GET_UNOWN_LETTER(personality);
// The other Unowns are separate from Unown A.
if (i == 0)
i = SPECIES_UNOWN;
else
i += SPECIES_UNOWN_B - 1;
if (!isFrontPic)
LZ77UnCompWram(gMonBackPicTable[i].data, dest);
else
LZ77UnCompWram(gMonFrontPicTable[i].data, dest);
}
else if (species > NUM_SPECIES) // is species unknown? draw the ? icon
{
LZ77UnCompWram(gMonFrontPicTable[0].data, dest);
}
else
{
LZ77UnCompWram(src->data, dest);
}
DuplicateDeoxysTiles(dest, species);
DrawSpindaSpots(species, personality, dest, isFrontPic);
}
void Unused_LZDecompressWramIndirect(const void **src, void *dest)
{
LZ77UnCompWram(*src, dest);
}
static void UNUSED StitchObjectsOn8x8Canvas(s32 object_size, s32 object_count, u8 *src_tiles, u8 *dest_tiles)
{
/*
This function appears to emulate behaviour found in the GB(C) versions regarding how the Pokemon images
are stitched together to be displayed on the battle screen.
Given "compacted" tiles, an object count and a bounding box/object size, place the tiles in such a way
that the result will have each object centered in a 8x8 tile canvas.
*/
s32 i, j, k, l;
u8 *src = src_tiles, *dest = dest_tiles;
u8 bottom_off;
if (object_size & 1)
{
// Object size is odd
bottom_off = (object_size >> 1) + 4;
for (l = 0; l < object_count; l++)
{
// Clear all unused rows of tiles plus the half-tile required due to centering
for (j = 0; j < 8-object_size; j++)
{
for (k = 0; k < 8; k++)
{
for (i = 0; i < 16; i++)
{
if (j % 2 == 0)
{
// Clear top half of top tile and bottom half of bottom tile when on even j
((dest+i) + (k << 5))[((j >> 1) << 8)] = 0;
((bottom_off << 8) + (dest+i) + (k << 5) + 16)[((j >> 1) << 8)] = 0;
}
else
{
// Clear bottom half of top tile and top half of tile following bottom tile when on odd j
((dest+i) + (k << 5) + 16)[((j >> 1) << 8)] = 0;
((bottom_off << 8) + (dest+i) + (k << 5) + 256)[((j >> 1) << 8)] = 0;
}
}
}
}
// Clear the columns to the left and right that wont be used completely
// Unlike the previous loops, this will clear the later used space as well
for (j = 0; j < 2; j++)
{
for (i = 0; i < 8; i++)
{
for (k = 0; k < 32; k++)
{
// Left side
((dest+k) + (i << 8))[(j << 5)] = 0;
// Right side
((dest+k) + (i << 8))[(j << 5)+192] = 0;
}
}
}
// Skip the top row and first tile on the second row for objects of size 5
if (object_size == 5) dest += 0x120;
// Copy tile data
for (j = 0; j < object_size; j++)
{
for (k = 0; k < object_size; k++)
{
for (i = 0; i < 4; i++)
{
// Offset the tile by +4px in both x and y directions
(dest + (i << 2))[18] = (src + (i << 2))[0];
(dest + (i << 2))[19] = (src + (i << 2))[1];
(dest + (i << 2))[48] = (src + (i << 2))[2];
(dest + (i << 2))[49] = (src + (i << 2))[3];
(dest + (i << 2))[258] = (src + (i << 2))[16];
(dest + (i << 2))[259] = (src + (i << 2))[17];
(dest + (i << 2))[288] = (src + (i << 2))[18];
(dest + (i << 2))[289] = (src + (i << 2))[19];
}
src += 32;
dest += 32;
}
// At the end of a row, skip enough tiles to get to the beginning of the next row
if (object_size == 7) dest += 0x20;
else if (object_size == 5) dest += 0x60;
}
// Skip remaining unused space to go to the beginning of the next object
if (object_size == 7) dest += 0x100;
else if (object_size == 5) dest += 0x1e0;
}
}
else
{
// Object size is even
for (i = 0; i < object_count; i++)
{
// For objects of size 6, the first and last row and column will be cleared
// While the remaining space will be filled with actual data
if (object_size == 6)
{
for (k = 0; k < 256; k++)
{
*dest = 0;
dest++;
}
}
for (j = 0; j < object_size; j++)
{
if (object_size == 6)
{
for (k = 0; k < 32; k++)
{
*dest = 0;
dest++;
}
}
// Copy tile data
for (k = 0; k < 32 * object_size; k++)
{
*dest = *src;
src++;
dest++;
}
if (object_size == 6)
{
for (k = 0; k < 32; k++)
{
*dest = 0;
dest++;
}
}
}
if (object_size == 6)
{
for (k = 0; k < 256; k++)
{
*dest = 0;
dest++;
}
}
}
}
}
u32 GetDecompressedDataSize(const u32 *ptr)
{
const u8 *ptr8 = (const u8 *)ptr;
return (ptr8[3] << 16) | (ptr8[2] << 8) | (ptr8[1]);
}
bool8 LoadCompressedSpriteSheetUsingHeap(const struct CompressedSpriteSheet *src)
{
struct SpriteSheet dest;
void *buffer;
buffer = AllocZeroed(src->data[0] >> 8);
LZ77UnCompWram(src->data, buffer);
dest.data = buffer;
dest.size = src->size;
dest.tag = src->tag;
LoadSpriteSheet(&dest);
Free(buffer);
return FALSE;
}
bool8 LoadCompressedSpritePaletteUsingHeap(const struct CompressedSpritePalette *src)
{
struct SpritePalette dest;
void *buffer;
buffer = AllocZeroed(src->data[0] >> 8);
LZ77UnCompWram(src->data, buffer);
dest.data = buffer;
dest.tag = src->tag;
LoadSpritePalette(&dest);
Free(buffer);
return FALSE;
}
void DecompressPicFromTable_2(const struct CompressedSpriteSheet *src, void *buffer, s32 species) // a copy of DecompressPicFromTable
{
if (species > NUM_SPECIES)
LZ77UnCompWram(gMonFrontPicTable[0].data, buffer);
else
LZ77UnCompWram(src->data, buffer);
DuplicateDeoxysTiles(buffer, species);
}
void LoadSpecialPokePic_2(const struct CompressedSpriteSheet *src, void *dest, s32 species, u32 personality, bool8 isFrontPic) // a copy of LoadSpecialPokePic
{
if (species == SPECIES_UNOWN)
{
u16 i = GET_UNOWN_LETTER(personality);
// The other Unowns are separate from Unown A.
if (i == 0)
i = SPECIES_UNOWN;
else
i += SPECIES_UNOWN_B - 1;
if (!isFrontPic)
LZ77UnCompWram(gMonBackPicTable[i].data, dest);
else
LZ77UnCompWram(gMonFrontPicTable[i].data, dest);
}
else if (species > NUM_SPECIES) // is species unknown? draw the ? icon
{
LZ77UnCompWram(gMonFrontPicTable[0].data, dest);
}
else
{
LZ77UnCompWram(src->data, dest);
}
DuplicateDeoxysTiles(dest, species);
DrawSpindaSpots(species, personality, dest, isFrontPic);
}
void HandleLoadSpecialPokePic_2(const struct CompressedSpriteSheet *src, void *dest, s32 species, u32 personality) // a copy of HandleLoadSpecialPokePic
{
bool8 isFrontPic;
if (src == &gMonFrontPicTable[species])
isFrontPic = TRUE; // frontPic
else
isFrontPic = FALSE; // backPic
LoadSpecialPokePic_2(src, dest, species, personality, isFrontPic);
}
void DecompressPicFromTable_DontHandleDeoxys(const struct CompressedSpriteSheet *src, void *buffer, s32 species)
{
if (species > NUM_SPECIES)
LZ77UnCompWram(gMonFrontPicTable[0].data, buffer);
else
LZ77UnCompWram(src->data, buffer);
}
void HandleLoadSpecialPokePic_DontHandleDeoxys(const struct CompressedSpriteSheet *src, void *dest, s32 species, u32 personality)
{
bool8 isFrontPic;
if (src == &gMonFrontPicTable[species])
isFrontPic = TRUE; // frontPic
else
isFrontPic = FALSE; // backPic
LoadSpecialPokePic_DontHandleDeoxys(src, dest, species, personality, isFrontPic);
}
void LoadSpecialPokePic_DontHandleDeoxys(const struct CompressedSpriteSheet *src, void *dest, s32 species, u32 personality, bool8 isFrontPic)
{
if (species == SPECIES_UNOWN)
{
u16 i = GET_UNOWN_LETTER(personality);
// The other Unowns are separate from Unown A.
if (i == 0)
i = SPECIES_UNOWN;
else
i += SPECIES_UNOWN_B - 1;
if (!isFrontPic)
LZ77UnCompWram(gMonBackPicTable[i].data, dest);
else
LZ77UnCompWram(gMonFrontPicTable[i].data, dest);
}
else if (species > NUM_SPECIES) // is species unknown? draw the ? icon
{
LZ77UnCompWram(gMonFrontPicTable[0].data, dest);
}
else
{
LZ77UnCompWram(src->data, dest);
}
DrawSpindaSpots(species, personality, dest, isFrontPic);
}
static void DuplicateDeoxysTiles(void *pointer, s32 species)
{
if (species == SPECIES_DEOXYS)
CpuCopy32(pointer + MON_PIC_SIZE, pointer, MON_PIC_SIZE);
}