summaryrefslogtreecommitdiff
path: root/arm9/src/map_matrix.c
blob: 1808d82cb52e2da2eafa14815e11e5b5ecf19544 (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
#include "global.h"
#include "map_matrix.h"
#include "MI_memory.h"
#include "filesystem.h"
#include "heap.h"
#include "map_header.h"

THUMB_FUNC static void MapMatrix_MapMatrixData_Load(
    struct MapMatrixData *map_matrix, u16 file_id, u32 map_id)
{
    map_matrix->width = 0;
    map_matrix->height = 0;

    s32 i;
    for (i = 0; i < MAP_MATRIX_MAX_SIZE; i++)
    {
        map_matrix->headers[i] = 0;
        map_matrix->altitudes[i] = 0;
        map_matrix->maps.data[i] = 0;
    }

    for (i = 0; i < MAP_MATRIX_MAX_NAME_LENGTH; i++)
    {
        map_matrix->name[i] = 0;
    }

    void *buffer =
        AllocAtEndAndReadWholeNarcMemberByIdPair(NARC_FIELDDATA_MAPMATRIX_MAP_MATRIX, file_id, 11);
    u8 *cursor = (u8 *)buffer;

    map_matrix->width = *(cursor++);
    map_matrix->height = *(cursor++);
    u8 has_headers_section = *(cursor++);
    u8 has_altitudes_section = *(cursor++);
    u8 name_length = *(cursor++);

    GF_ASSERT(name_length <= MAP_MATRIX_MAX_NAME_LENGTH);

    MI_CpuCopy8(cursor, &map_matrix->name, name_length);
    cursor += name_length;

    if (has_headers_section != 0)
    {
        MI_CpuCopy8(
            cursor, &map_matrix->headers, map_matrix->width * map_matrix->height * sizeof(u16));
        cursor += map_matrix->width * map_matrix->height * sizeof(u16);
    }
    else
    {
        MIi_CpuClear16((u16)map_id,
            &map_matrix->headers,
            map_matrix->width * map_matrix->height * sizeof(u16));
    }

    if (has_altitudes_section != 0)
    {
        MI_CpuCopy8(
            cursor, &map_matrix->altitudes, map_matrix->width * map_matrix->height * sizeof(u8));
        cursor += map_matrix->width * map_matrix->height * sizeof(u8);
    }

    MI_CpuCopy8(
        cursor, map_matrix->maps.data, map_matrix->width * map_matrix->height * sizeof(u16));
    FreeToHeap(buffer);
}

THUMB_FUNC struct MapMatrix *MapMatrix_New(void)
{
    struct MapMatrix *map_matrix = AllocFromHeap(11, sizeof(struct MapMatrix));

    map_matrix->width = 0;
    map_matrix->height = 0;
    map_matrix->matrix_id = 0;

    return map_matrix;
}

THUMB_FUNC void MapMatrix_Load(u16 map_id, struct MapMatrix *map_matrix)
{
    u16 matrix_id = MapHeader_GetMatrixId(map_id);

    MapMatrix_MapMatrixData_Load(&map_matrix->data, matrix_id, map_id);

    map_matrix->matrix_id = (u8)matrix_id;
    map_matrix->height = map_matrix->data.height;
    map_matrix->width = map_matrix->data.width;
}

THUMB_FUNC void MapMatrix_Free(struct MapMatrix *map_matrix)
{
    FreeToHeap(map_matrix);
}

THUMB_FUNC u16 MapMatrix_GetMapData(s32 map_id, struct MapMatrix *map_matrix)
{
    GF_ASSERT(map_id < map_matrix->width * map_matrix->height);

    return map_matrix->data.maps.data[map_id];
}

THUMB_FUNC u8 MapMatrix_GetWidth(struct MapMatrix *map_matrix)
{
    GF_ASSERT(map_matrix != NULL);

    return map_matrix->width;
}

THUMB_FUNC u8 MapMatrix_GetHeight(struct MapMatrix *map_matrix)
{
    GF_ASSERT(map_matrix != NULL);

    return map_matrix->height;
}

THUMB_FUNC u16 MapMatrix_GetMapHeader(struct MapMatrix *map_matrix, s32 x, s32 y)
{
    s32 width = map_matrix->width;
    s32 height = map_matrix->height;

    GF_ASSERT(x >= 0 && x < width);
    GF_ASSERT(y >= 0 && y < height);

    return map_matrix->data.headers[y * width + x];
}

THUMB_FUNC u16 MapMatrix_GetMapHeaderFromID(struct MapMatrix *map_matrix, s32 map_id)
{
    s32 max_map_id = map_matrix->width * map_matrix->height;

    GF_ASSERT(0 <= map_id && map_id < max_map_id);

    return map_matrix->data.headers[map_id];
}

THUMB_FUNC u8 MapMatrix_GetMatrixID(struct MapMatrix *map_matrix)
{
    return map_matrix->matrix_id;
}

THUMB_FUNC u32 MapMatrix_GetMapAltitude(
    struct MapMatrix *map_matrix, u8 param1, s16 x, s16 y, int matrix_width)
{
#pragma unused(param1)
    GF_ASSERT(x < matrix_width);
    GF_ASSERT(y * matrix_width + x < MAP_MATRIX_MAX_SIZE);

    return map_matrix->data.altitudes[y * matrix_width + x];
}

THUMB_FUNC struct MapData *MapMatrix_MapData_New(u32 heap_id)
{
    struct MapData *map_data = AllocFromHeap(heap_id, sizeof(struct MapData));
    void *buffer =
        AllocAtEndAndReadWholeNarcMemberByIdPair(NARC_FIELDDATA_MAPMATRIX_MAP_MATRIX, 0, heap_id);
    u8 *cursor = (u8 *)buffer;

    cursor += 4;
    u8 name_length = *cursor;
    cursor++;
    cursor += name_length;

    MI_CpuCopy8(cursor, map_data, sizeof(struct MapData));
    FreeToHeap(buffer);

    return map_data;
}

THUMB_FUNC void MapMatrix_MapData_Free(struct MapData *map_data)
{
    GF_ASSERT(map_data != NULL);
    FreeToHeap(map_data);
}

THUMB_FUNC u16 MapMatrix_MapData_GetData(struct MapData *map_data, s32 x, s32 y)
{
    return map_data->data[MAP_MATRIX_MAX_WIDTH * y + x];
}

THUMB_FUNC u16 GetMapHeader(u32 file_id, u16 x, u16 y)
{
    struct MapMatrixData map_matrix;

    MapMatrix_MapMatrixData_Load(&map_matrix, (u16)file_id, 0);
    GF_ASSERT(x < map_matrix.width);
    GF_ASSERT(y < map_matrix.height);

    return map_matrix.headers[y * map_matrix.width + x];
}

THUMB_FUNC u16 GetMapData(s32 map_id, struct MapMatrix *map_matrix)
{
    GF_ASSERT(map_matrix != NULL);
    return MapMatrix_GetMapData(map_id, map_matrix);
}

THUMB_FUNC void FUN_02034678(struct MapMatrix *map_matrix)
{
    u16 *maps = map_matrix->data.maps.data;
    u8 *altitudes = map_matrix->data.altitudes;
    u8 width = map_matrix->width;

    if (map_matrix->matrix_id != 0)
    {
        return;
    }

    maps[width * 0x15 + 0x17] = 0xB0;
    maps[width * 0x15 + 0x18] = 0xB0;
    maps[width * 0x16 + 0x17] = 0xB0;
    maps[width * 0x16 + 0x18] = 0xB0;
    altitudes[width * 0x15 + 0x17] = 2;
    altitudes[width * 0x15 + 0x18] = 2;
    altitudes[width * 0x16 + 0x17] = 2;
    altitudes[width * 0x16 + 0x18] = 2;
}

THUMB_FUNC void FUN_020346CC(struct MapMatrix *map_matrix)
{
    u16 *maps = map_matrix->data.maps.data;
    u8 width = map_matrix->width;

    if (map_matrix->matrix_id != 0)
    {
        return;
    }

    maps[width * 0x0F + 0x1C] = 0x77;
    maps[width * 0x10 + 0x1B] = 0x78;
    maps[width * 0x10 + 0x1C] = 0x79;
    maps[width * 0x11 + 0x1B] = 0x7A;
}