forked from pret/pokeemerald
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathrotating_tile_puzzle.c
381 lines (339 loc) · 13.7 KB
/
rotating_tile_puzzle.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
#include "global.h"
#include "event_object_movement.h"
#include "fieldmap.h"
#include "malloc.h"
#include "rotating_tile_puzzle.h"
#include "script_movement.h"
#include "constants/event_object_movement.h"
#include "constants/event_objects.h"
#include "constants/metatile_labels.h"
#define ROTATE_COUNTERCLOCKWISE 0
#define ROTATE_CLOCKWISE 1
#define ROTATE_NONE 2
struct RotatingTileObject
{
u8 prevPuzzleTileNum;
u8 eventTemplateId;
};
struct RotatingTilePuzzle
{
struct RotatingTileObject objects[OBJECT_EVENTS_COUNT];
u8 numObjects;
bool8 isTrickHouse;
};
static const u8 sMovement_ShiftRight[] =
{
MOVEMENT_ACTION_LOCK_ANIM,
MOVEMENT_ACTION_WALK_NORMAL_RIGHT,
MOVEMENT_ACTION_UNLOCK_ANIM,
MOVEMENT_ACTION_STEP_END
};
static const u8 sMovement_ShiftDown[] =
{
MOVEMENT_ACTION_LOCK_ANIM,
MOVEMENT_ACTION_WALK_NORMAL_DOWN,
MOVEMENT_ACTION_UNLOCK_ANIM,
MOVEMENT_ACTION_STEP_END
};
static const u8 sMovement_ShiftLeft[] =
{
MOVEMENT_ACTION_LOCK_ANIM,
MOVEMENT_ACTION_WALK_NORMAL_LEFT,
MOVEMENT_ACTION_UNLOCK_ANIM,
MOVEMENT_ACTION_STEP_END
};
static const u8 sMovement_ShiftUp[] =
{
MOVEMENT_ACTION_LOCK_ANIM,
MOVEMENT_ACTION_WALK_NORMAL_UP,
MOVEMENT_ACTION_UNLOCK_ANIM,
MOVEMENT_ACTION_STEP_END
};
static const u8 sMovement_FaceRight[] =
{
MOVEMENT_ACTION_FACE_RIGHT,
MOVEMENT_ACTION_STEP_END
};
static const u8 sMovement_FaceDown[] =
{
MOVEMENT_ACTION_FACE_DOWN,
MOVEMENT_ACTION_STEP_END
};
static const u8 sMovement_FaceLeft[] =
{
MOVEMENT_ACTION_FACE_LEFT,
MOVEMENT_ACTION_STEP_END
};
static const u8 sMovement_FaceUp[] =
{
MOVEMENT_ACTION_FACE_UP,
MOVEMENT_ACTION_STEP_END
};
static void SaveRotatingTileObject(u8, u8);
static void TurnUnsavedRotatingTileObject(u8, u8);
EWRAM_DATA static struct RotatingTilePuzzle *sRotatingTilePuzzle = NULL;
void InitRotatingTilePuzzle(bool8 isTrickHouse)
{
if (sRotatingTilePuzzle == NULL)
sRotatingTilePuzzle = AllocZeroed(sizeof(*sRotatingTilePuzzle));
sRotatingTilePuzzle->isTrickHouse = isTrickHouse;
}
void FreeRotatingTilePuzzle(void)
{
u8 id;
TRY_FREE_AND_SET_NULL(sRotatingTilePuzzle);
id = GetObjectEventIdByLocalIdAndMap(OBJ_EVENT_ID_PLAYER, 0, 0);
ObjectEventClearHeldMovementIfFinished(&gObjectEvents[id]);
ScriptMovement_UnfreezeObjectEvents();
}
u16 MoveRotatingTileObjects(u8 puzzleNumber)
{
u8 i;
struct ObjectEventTemplate *objectEvents = gSaveBlock1Ptr->objectEventTemplates;
u16 localId = 0;
for (i = 0; i < OBJECT_EVENT_TEMPLATES_COUNT; i++)
{
s32 puzzleTileStart;
u8 puzzleTileNum;
s16 x = objectEvents[i].x + MAP_OFFSET;
s16 y = objectEvents[i].y + MAP_OFFSET;
u16 metatile = MapGridGetMetatileIdAt(x, y);
if (!sRotatingTilePuzzle->isTrickHouse)
puzzleTileStart = METATILE_MossdeepGym_YellowArrow_Right;
else
puzzleTileStart = METATILE_TrickHousePuzzle_Arrow_YellowOnWhite_Right;
// Object is on a metatile before the puzzle tile section
// UB: Because this is not if (metatile < puzzleTileStart), for the trick house (metatile - puzzleTileStart) below can result in casting a negative value to u8
if (metatile < METATILE_MossdeepGym_YellowArrow_Right)
continue;
// Object is on a metatile after the puzzle tile section (never occurs, in both cases the puzzle tiles are last)
if ((u8)((metatile - puzzleTileStart) / METATILE_ROW_WIDTH) >= 5)
continue;
// Object is on a metatile in puzzle tile section, but not one of the currently rotating color
if ((u8)((metatile - puzzleTileStart) / METATILE_ROW_WIDTH) != puzzleNumber)
continue;
puzzleTileNum = (u8)((metatile - puzzleTileStart) % METATILE_ROW_WIDTH);
// First 4 puzzle tiles are the colored arrows
if (puzzleTileNum < 4)
{
s8 x = 0;
s8 y = 0;
const u8 *movementScript;
switch (puzzleTileNum)
{
case 0: // Right Arrow
movementScript = sMovement_ShiftRight;
x = 1;
break;
case 1: // Down Arrow
movementScript = sMovement_ShiftDown;
y = 1;
break;
case 2: // Left Arrow
movementScript = sMovement_ShiftLeft;
x = -1;
break;
case 3: // Up Arrow
movementScript = sMovement_ShiftUp;
y = -1;
break;
default:
continue;
}
objectEvents[i].x += x;
objectEvents[i].y += y;
if (GetObjectEventIdByLocalIdAndMap(objectEvents[i].localId, gSaveBlock1Ptr->location.mapNum, gSaveBlock1Ptr->location.mapGroup) != OBJECT_EVENTS_COUNT)
{
SaveRotatingTileObject(i, puzzleTileNum);
localId = objectEvents[i].localId;
ScriptMovement_StartObjectMovementScript(localId, gSaveBlock1Ptr->location.mapNum, gSaveBlock1Ptr->location.mapGroup, movementScript);
}
// Never reached in normal gameplay
else
{
TurnUnsavedRotatingTileObject(i, puzzleTileNum);
}
}
}
return localId;
}
void TurnRotatingTileObjects(void)
{
u8 i;
s32 puzzleTileStart;
struct ObjectEventTemplate *objectEvents;
if (sRotatingTilePuzzle == NULL)
return;
if (!sRotatingTilePuzzle->isTrickHouse)
puzzleTileStart = METATILE_MossdeepGym_YellowArrow_Right;
else
puzzleTileStart = METATILE_TrickHousePuzzle_Arrow_YellowOnWhite_Right;
objectEvents = gSaveBlock1Ptr->objectEventTemplates;
for (i = 0; i < sRotatingTilePuzzle->numObjects; i++)
{
s32 rotation;
s8 tileDifference;
u8 objectEventId;
s16 x = objectEvents[sRotatingTilePuzzle->objects[i].eventTemplateId].x + MAP_OFFSET;
s16 y = objectEvents[sRotatingTilePuzzle->objects[i].eventTemplateId].y + MAP_OFFSET;
u16 metatile = MapGridGetMetatileIdAt(x, y);
// NOTE: The following 2 assignments and if else could all be replaced with rotation = ROTATE_COUNTERCLOCKWISE
// For an object to be saved in sRotatingTilePuzzle->objects, it must have been on a colored arrow tile
// After the first assignment, tileDifference will always be a number [0-3] representing which arrow tile the object is on now (0: right, 1: down, 2: left, 3: up)
// prevPuzzleTileNum will similarly be a number [0-3] representing the arrow tile the object just moved from
// All the puzzles are oriented counter-clockwise and can only move 1 step at a time, so the difference between the current tile and the previous tile will always either be -1 or 3 (0-1, 1-2, 2-3, 3-0)
// Which means tileDifference will always either be -1 or 3 after the below subtraction, and rotation will always be ROTATE_COUNTERCLOCKWISE after the following conditionals
tileDifference = (u8)((metatile - puzzleTileStart) % METATILE_ROW_WIDTH);
tileDifference -= (sRotatingTilePuzzle->objects[i].prevPuzzleTileNum);
// Always true, see above
if (tileDifference < 0 || tileDifference == 3)
{
// Always false, see above
if (tileDifference == -3)
rotation = ROTATE_CLOCKWISE;
else
rotation = ROTATE_COUNTERCLOCKWISE;
}
else
{
if (tileDifference > 0)
rotation = ROTATE_CLOCKWISE;
else
rotation = ROTATE_NONE;
}
objectEventId = GetObjectEventIdByLocalIdAndMap(objectEvents[sRotatingTilePuzzle->objects[i].eventTemplateId].localId, gSaveBlock1Ptr->location.mapNum, gSaveBlock1Ptr->location.mapGroup);
if (objectEventId != OBJECT_EVENTS_COUNT)
{
const u8 *movementScript;
u8 direction = gObjectEvents[objectEventId].facingDirection;
if (rotation == ROTATE_COUNTERCLOCKWISE)
{
switch (direction)
{
case DIR_EAST:
movementScript = sMovement_FaceUp;
objectEvents[sRotatingTilePuzzle->objects[i].eventTemplateId].movementType = MOVEMENT_TYPE_FACE_UP;
break;
case DIR_SOUTH:
movementScript = sMovement_FaceRight;
objectEvents[sRotatingTilePuzzle->objects[i].eventTemplateId].movementType = MOVEMENT_TYPE_FACE_RIGHT;
break;
case DIR_WEST:
movementScript = sMovement_FaceDown;
objectEvents[sRotatingTilePuzzle->objects[i].eventTemplateId].movementType = MOVEMENT_TYPE_FACE_DOWN;
break;
case DIR_NORTH:
movementScript = sMovement_FaceLeft;
objectEvents[sRotatingTilePuzzle->objects[i].eventTemplateId].movementType = MOVEMENT_TYPE_FACE_LEFT;
break;
default:
continue;
}
ScriptMovement_StartObjectMovementScript(objectEvents[sRotatingTilePuzzle->objects[i].eventTemplateId].localId,
gSaveBlock1Ptr->location.mapNum,
gSaveBlock1Ptr->location.mapGroup,
movementScript);
}
// Never reached
else if (rotation == ROTATE_CLOCKWISE)
{
switch (direction)
{
case DIR_EAST:
movementScript = sMovement_FaceDown;
objectEvents[sRotatingTilePuzzle->objects[i].eventTemplateId].movementType = MOVEMENT_TYPE_FACE_DOWN;
break;
case DIR_SOUTH:
movementScript = sMovement_FaceLeft;
objectEvents[sRotatingTilePuzzle->objects[i].eventTemplateId].movementType = MOVEMENT_TYPE_FACE_LEFT;
break;
case DIR_WEST:
movementScript = sMovement_FaceUp;
objectEvents[sRotatingTilePuzzle->objects[i].eventTemplateId].movementType = MOVEMENT_TYPE_FACE_UP;
break;
case DIR_NORTH:
movementScript = sMovement_FaceRight;
objectEvents[sRotatingTilePuzzle->objects[i].eventTemplateId].movementType = MOVEMENT_TYPE_FACE_RIGHT;
break;
default:
continue;
}
ScriptMovement_StartObjectMovementScript(objectEvents[sRotatingTilePuzzle->objects[i].eventTemplateId].localId,
gSaveBlock1Ptr->location.mapNum,
gSaveBlock1Ptr->location.mapGroup,
movementScript);
}
}
}
}
static void SaveRotatingTileObject(u8 eventTemplateId, u8 puzzleTileNum)
{
sRotatingTilePuzzle->objects[sRotatingTilePuzzle->numObjects].eventTemplateId = eventTemplateId;
sRotatingTilePuzzle->objects[sRotatingTilePuzzle->numObjects].prevPuzzleTileNum = puzzleTileNum;
sRotatingTilePuzzle->numObjects++;
}
// Functionally unused
static void TurnUnsavedRotatingTileObject(u8 eventTemplateId, u8 puzzleTileNum)
{
s8 tileDifference;
s32 rotation;
s32 puzzleTileStart;
u16 movementType;
struct ObjectEventTemplate *objectEvents = gSaveBlock1Ptr->objectEventTemplates;
s16 x = objectEvents[eventTemplateId].x + MAP_OFFSET;
s16 y = objectEvents[eventTemplateId].y + MAP_OFFSET;
u16 metatile = MapGridGetMetatileIdAt(x, y);
if (!sRotatingTilePuzzle->isTrickHouse)
puzzleTileStart = METATILE_MossdeepGym_YellowArrow_Right;
else
puzzleTileStart = METATILE_TrickHousePuzzle_Arrow_YellowOnWhite_Right;
tileDifference = (u8)((metatile - puzzleTileStart) % METATILE_ROW_WIDTH);
tileDifference -= puzzleTileNum;
if (tileDifference < 0 || tileDifference == 3)
rotation = ROTATE_COUNTERCLOCKWISE;
else if (tileDifference > 0 || tileDifference == -3)
rotation = ROTATE_CLOCKWISE;
else
rotation = ROTATE_NONE;
movementType = objectEvents[eventTemplateId].movementType;
if (rotation == ROTATE_COUNTERCLOCKWISE)
{
switch (movementType)
{
case MOVEMENT_TYPE_FACE_RIGHT:
objectEvents[eventTemplateId].movementType = MOVEMENT_TYPE_FACE_UP;
break;
case MOVEMENT_TYPE_FACE_DOWN:
objectEvents[eventTemplateId].movementType = MOVEMENT_TYPE_FACE_RIGHT;
break;
case MOVEMENT_TYPE_FACE_LEFT:
objectEvents[eventTemplateId].movementType = MOVEMENT_TYPE_FACE_DOWN;
break;
case MOVEMENT_TYPE_FACE_UP:
objectEvents[eventTemplateId].movementType = MOVEMENT_TYPE_FACE_LEFT;
break;
default:
break;
}
}
else if (rotation == ROTATE_CLOCKWISE)
{
switch (movementType)
{
case MOVEMENT_TYPE_FACE_RIGHT:
objectEvents[eventTemplateId].movementType = MOVEMENT_TYPE_FACE_DOWN;
break;
case MOVEMENT_TYPE_FACE_DOWN:
objectEvents[eventTemplateId].movementType = MOVEMENT_TYPE_FACE_LEFT;
break;
case MOVEMENT_TYPE_FACE_LEFT:
objectEvents[eventTemplateId].movementType = MOVEMENT_TYPE_FACE_UP;
break;
case MOVEMENT_TYPE_FACE_UP:
objectEvents[eventTemplateId].movementType = MOVEMENT_TYPE_FACE_RIGHT;
break;
default:
break;
}
}
}