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
|
## Stair Warps
Credit to ghoulslash. The easy implementation is to pull from the [repo](https://github.com/ghoulslash/pokeemerald/tree/stair_warps)
FRLG include a stair warp effect where the player walks up/down stairs at the beginning/end of the warp sequence. This ports that feature from FRLG to emerald:
<a href="https://imgur.com/q9SzXmW"><img src="https://i.imgur.com/q9SzXmW.gif" title="source: imgur.com" /></a>
### Add new metatile behaviors
**1.** First, open [include/constants/metatile_behaviors.h](../blob/master/include/constants/metatile_behaviors.h). Replace any 4 unused metatile behaviors (I chose MB_UNUSED_EB through MB_UNUSED_EE):
```c
#define MB_UP_RIGHT_STAIR_WARP 0xEB
#define MB_UP_LEFT_STAIR_WARP 0xEC
#define MB_DOWN_RIGHT_STAIR_WARP 0xED
#define MB_DOWN_LEFT_STAIR_WARP 0xEE
```
**2.** Next, open [src/metatile_behavior.c](../blob/master/src/metatile_behavior.c). Replace the elements in `sTileBitAttributes` for your metatile behaviours:
```c
[MB_UP_RIGHT_STAIR_WARP] = TILE_ATTRIBUTES(FALSE, FALSE, FALSE),
[MB_UP_LEFT_STAIR_WARP] = TILE_ATTRIBUTES(FALSE, FALSE, FALSE),
[MB_DOWN_RIGHT_STAIR_WARP] = TILE_ATTRIBUTES(FALSE, FALSE, FALSE),
[MB_DOWN_LEFT_STAIR_WARP] = TILE_ATTRIBUTES(FALSE, FALSE, FALSE),
```
**3.** At the bottom of the file, add the following 5 functions:
```c
bool8 MetatileBehavior_IsDirectionalUpRightStairWarp(u8 metatileBehavior)
{
if(metatileBehavior == MB_UP_RIGHT_STAIR_WARP)
return TRUE;
else
return FALSE;
}
bool8 MetatileBehavior_IsDirectionalUpLeftStairWarp(u8 metatileBehavior)
{
if (metatileBehavior == MB_UP_LEFT_STAIR_WARP)
return TRUE;
else
return FALSE;
}
bool8 MetatileBehavior_IsDirectionalDownRightStairWarp(u8 metatileBehavior)
{
if (metatileBehavior == MB_DOWN_RIGHT_STAIR_WARP)
return TRUE;
else
return FALSE;
}
bool8 MetatileBehavior_IsDirectionalDownLeftStairWarp(u8 metatileBehavior)
{
if (metatileBehavior == MB_DOWN_LEFT_STAIR_WARP)
return TRUE;
else
return FALSE;
}
bool8 MetatileBehavior_IsDirectionalStairWarp(u8 metatileBehavior)
{
if (metatileBehavior >= MB_UP_RIGHT_STAIR_WARP && metatileBehavior <= MB_DOWN_LEFT_STAIR_WARP)
return TRUE;
else
return FALSE;
}
```
### Globally define the metatile behavior functions
Open [include/metatile_behavior.h](../blob/master/include/metatile_behavior.h). Add the following to the bottom:
```c
bool8 MetatileBehavior_IsDirectionalUpRightStairWarp(u8 metatileBehavior);
bool8 MetatileBehavior_IsDirectionalUpLeftStairWarp(u8 metatileBehavior);
bool8 MetatileBehavior_IsDirectionalDownRightStairWarp(u8 metatileBehavior);
bool8 MetatileBehavior_IsDirectionalDownLeftStairWarp(u8 metatileBehavior);
bool8 MetatileBehavior_IsDirectionalStairWarp(u8 metatileBehavior);
```
### Add a new stair warp collision
This allows us to ignore the impassable tiles when we try to warp on the stairs. Open [include/global.fieldmap.h](../blob/master/include/global.fieldmap.h). Add `COLLISION_STAIR_WARP` after the enum define `COLLISION_HORIZONTAL_RAIL,`
### Add the collision exclusion
Open [src/field_player_avatar.c](../blob/master/src/field_player_avatar.c).
**1.** First, let's add `#include "field_screen_effect.h"` to the top of the file.
**2.** Next, find `static u8 CheckForPlayerAvatarCollision(u8 direction)`. Before the line, `MoveCoords(direction, &x, &y);`, add the following two lines:
```c
if (IsDirectionalStairWarpMetatileBehavior(MapGridGetMetatileBehaviorAt(x, y), direction))
return COLLISION_STAIR_WARP;
```
**3.** Finally, find `static void PlayerNotOnBikeMoving(u8 direction, u16 heldKeys)`. Replace the `if (collision)` code block with:
```c
if (collision)
{
if (collision == COLLISION_LEDGE_JUMP)
{
PlayerJumpLedge(direction);
return;
}
else if (collision == COLLISION_OBJECT_EVENT && IsPlayerCollidingWithFarawayIslandMew(direction))
{
PlayerNotOnBikeCollideWithFarawayIslandMew(direction);
return;
}
else if (collision == COLLISION_STAIR_WARP)
{
PlayerFaceDirection(direction);
}
else
{
u8 adjustedCollision = collision - COLLISION_STOP_SURFING;
if (adjustedCollision > 3)
PlayerNotOnBikeCollide(direction);
return;
}
}
```
### Add the warp arrow
Open [src/field_control_avatar.c](../blob/master/src/field_control_avatar.c) and find the function `TryArrowWarp`. Replace everything with the following:
```c
static bool8 TryArrowWarp(struct MapPosition *position, u16 metatileBehavior, u8 direction)
{
s8 warpEventId = GetWarpEventAtMapPosition(&gMapHeader, position);
u16 delay;
if (warpEventId != -1)
{
if (IsArrowWarpMetatileBehavior(metatileBehavior, direction) == TRUE)
{
StoreInitialPlayerAvatarState();
SetupWarp(&gMapHeader, warpEventId, position);
DoWarp();
return TRUE;
}
else if (IsDirectionalStairWarpMetatileBehavior(metatileBehavior, direction) == TRUE)
{
delay = 0;
if (gPlayerAvatar.flags & (PLAYER_AVATAR_FLAG_MACH_BIKE | PLAYER_AVATAR_FLAG_ACRO_BIKE))
{
SetPlayerAvatarTransitionFlags(PLAYER_AVATAR_FLAG_ON_FOOT);
delay = 12;
}
StoreInitialPlayerAvatarState();
SetupWarp(&gMapHeader, warpEventId, position);
DoStairWarp(metatileBehavior, delay);
return TRUE;
}
}
return FALSE;
}
```
### Add `DoStairWarp`
This is the meat of the code implementation. Let's start by opening [src/field_screen_effect.c](../blob/master/src/field_screen_effect.c).
**1.** Find the function `static void SetUpWarpExitTask(void)`. Before `else if (MetatileBehavior_IsNonAnimDoor(behavior) == TRUE)`, Add the following:
```c
else if (MetatileBehavior_IsDirectionalStairWarp(behavior) == TRUE)
func = Task_ExitStairs;
```
**2.** Also, add `static void Task_ExitStairs(u8 taskId);` to the top of the file underneath `static void Task_EnableScriptAfterMusicFade(u8 taskId);`.
**3.** At the bottom of the file, let's add a bunch of functions:
```c
static void GetStairsMovementDirection(u8 a0, s16 *a1, s16 *a2)
{
if (MetatileBehavior_IsDirectionalUpRightStairWarp(a0))
{
*a1 = 16;
*a2 = -10;
}
else if (MetatileBehavior_IsDirectionalUpLeftStairWarp(a0))
{
*a1 = -17;
*a2 = -10;
}
else if (MetatileBehavior_IsDirectionalDownRightStairWarp(a0))
{
*a1 = 17;
*a2 = 3;
}
else if (MetatileBehavior_IsDirectionalDownLeftStairWarp(a0))
{
*a1 = -17;
*a2 = 3;
}
else
{
*a1 = 0;
*a2 = 0;
}
}
static bool8 WaitStairExitMovementFinished(s16 *a0, s16 *a1, s16 *a2, s16 *a3, s16 *a4)
{
struct Sprite *sprite;
sprite = &gSprites[gPlayerAvatar.spriteId];
if (*a4 != 0)
{
*a2 += *a0;
*a3 += *a1;
sprite->pos2.x = *a2 >> 5;
sprite->pos2.y = *a3 >> 5;
(*a4)--;
return TRUE;
}
else
{
sprite->pos2.x = 0;
sprite->pos2.y = 0;
return FALSE;
}
}
static void ExitStairsMovement(s16 *a0, s16 *a1, s16 *a2, s16 *a3, s16 *a4)
{
s16 x, y;
u8 behavior;
s32 r1;
struct Sprite *sprite;
PlayerGetDestCoords(&x, &y);
behavior = MapGridGetMetatileBehaviorAt(x, y);
if (MetatileBehavior_IsDirectionalDownRightStairWarp(behavior) || MetatileBehavior_IsDirectionalUpRightStairWarp(behavior))
r1 = 3;
else
r1 = 4;
ObjectEventForceSetHeldMovement(&gObjectEvents[gPlayerAvatar.objectEventId], GetWalkInPlaceSlowMovementAction(r1));
GetStairsMovementDirection(behavior, a0, a1);
*a2 = *a0 * 16;
*a3 = *a1 * 16;
*a4 = 16;
sprite = &gSprites[gPlayerAvatar.spriteId];
sprite->pos2.x = *a2 >> 5;
sprite->pos2.y = *a3 >> 5;
*a0 *= -1;
*a1 *= -1;
}
static void Task_ExitStairs(u8 taskId)
{
s16 * data = gTasks[taskId].data;
switch (data[0])
{
default:
if (WaitForWeatherFadeIn() == TRUE)
{
CameraObjectReset1();
ScriptContext2_Disable();
DestroyTask(taskId);
}
break;
case 0:
Overworld_PlaySpecialMapMusic();
WarpFadeInScreen();
ScriptContext2_Enable();
ExitStairsMovement(&data[1], &data[2], &data[3], &data[4], &data[5]);
data[0]++;
break;
case 1:
if (!WaitStairExitMovementFinished(&data[1], &data[2], &data[3], &data[4], &data[5]))
data[0]++;
break;
}
}
bool8 IsDirectionalStairWarpMetatileBehavior(u16 metatileBehavior, u8 playerDirection)
{
switch (playerDirection)
{
case DIR_WEST:
if (MetatileBehavior_IsDirectionalUpLeftStairWarp(metatileBehavior))
return TRUE;
if (MetatileBehavior_IsDirectionalDownLeftStairWarp(metatileBehavior))
return TRUE;
break;
case DIR_EAST:
if (MetatileBehavior_IsDirectionalUpRightStairWarp(metatileBehavior))
return TRUE;
if (MetatileBehavior_IsDirectionalDownRightStairWarp(metatileBehavior))
return TRUE;
break;
}
return FALSE;
}
static void ForceStairsMovement(u16 a0, s16 *a1, s16 *a2)
{
ObjectEventForceSetHeldMovement(&gObjectEvents[gPlayerAvatar.objectEventId], GetWalkInPlaceNormalMovementAction(GetPlayerFacingDirection()));
GetStairsMovementDirection(a0, a1, a2);
}
static void UpdateStairsMovement(s16 a0, s16 a1, s16 *a2, s16 *a3, s16 *a4)
{
struct Sprite *playerSpr = &gSprites[gPlayerAvatar.spriteId];
struct ObjectEvent *playerObj = &gObjectEvents[gPlayerAvatar.objectEventId];
if (a1 > 0 || *a4 > 6)
*a3 += a1;
*a2 += a0;
(*a4)++;
playerSpr->pos2.x = *a2 >> 5;
playerSpr->pos2.y = *a3 >> 5;
if (playerObj->heldMovementFinished)
ObjectEventForceSetHeldMovement(playerObj, GetWalkInPlaceNormalMovementAction(GetPlayerFacingDirection()));
}
static void Task_StairWarp(u8 taskId)
{
s16 * data = gTasks[taskId].data;
struct ObjectEvent *playerObj = &gObjectEvents[gPlayerAvatar.objectEventId];
struct Sprite *playerSpr = &gSprites[gPlayerAvatar.spriteId];
switch (data[0])
{
case 0:
ScriptContext2_Enable();
FreezeObjectEvents();
CameraObjectReset2();
data[0]++;
break;
case 1:
if (!ObjectEventIsMovementOverridden(playerObj) || ObjectEventClearHeldMovementIfFinished(playerObj))
{
if (data[15] != 0)
data[15]--;
else
{
TryFadeOutOldMapMusic();
PlayRainStoppingSoundEffect();
playerSpr->oam.priority = 1;
ForceStairsMovement(data[1], &data[2], &data[3]);
PlaySE(SE_KAIDAN);
data[0]++;
}
}
break;
case 2:
UpdateStairsMovement(data[2], data[3], &data[4], &data[5], &data[6]);
data[15]++;
if (data[15] >= 12)
{
WarpFadeOutScreen();
data[0]++;
}
break;
case 3:
UpdateStairsMovement(data[2], data[3], &data[4], &data[5], &data[6]);
if (!PaletteFadeActive() && BGMusicStopped())
data[0]++;
break;
default:
gFieldCallback = FieldCB_DefaultWarpExit;
WarpIntoMap();
SetMainCallback2(CB2_LoadMap);
DestroyTask(taskId);
break;
}
}
void DoStairWarp(u16 metatileBehavior, u16 delay)
{
u8 taskId = CreateTask(Task_StairWarp, 10);
gTasks[taskId].data[1] = metatileBehavior;
gTasks[taskId].data[15] = delay;
Task_StairWarp(taskId);
}
```
### Globally define two stair warp functions
Open [include/field_screen_effect.h](../blob/master/include/field_screen_effect.h). At the bottom, add the following:
```c
void DoStairWarp(u16 metatileBehavior, u16 delay);
bool8 IsDirectionalStairWarpMetatileBehavior(u16 metatileBehavior, u8 playerDirection);
```
### Adjust the player's movement direction on stair warps
Open [src/overworld.c](../blob/master/src/overworld.c) and find the function `GetAdjustedInitialDirection`. Before the line `else if ((playerStruct->transitionFlags == PLAYER_AVATAR_FLAG_UNDERWATER && (etc...)`, add the following:
```c
else if (MetatileBehavior_IsDirectionalUpRightStairWarp(metatileBehavior) == TRUE || MetatileBehavior_IsDirectionalDownRightStairWarp(metatileBehavior) == TRUE)
return DIR_WEST;
else if (MetatileBehavior_IsDirectionalUpLeftStairWarp(metatileBehavior) == TRUE || MetatileBehavior_IsDirectionalDownLeftStairWarp(metatileBehavior) == TRUE)
return DIR_EAST;
```
|