Skip to content

Commit 4de15d8

Browse files
authored
PLAYER_IN_MAP_CHUNK macro for Map_WorldObjectsUpdate (#394)
1 parent 8b2ffd6 commit 4de15d8

3 files changed

Lines changed: 22 additions & 24 deletions

File tree

include/maps/map1/map1_s06.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#ifndef _MAPS_MAP1_S06_H
22
#define _MAPS_MAP1_S06_H
33

4-
extern s8 D_800D7761;
4+
extern u8 D_800D7761;
55

66
extern s16 D_800DAF78;
77

@@ -15,7 +15,7 @@ extern s_WorldObjectDesc D_800DB024;
1515

1616
extern s_WorldObjectDesc D_800DB064;
1717

18-
extern s8 D_800DAFE4;
18+
extern u8 D_800DAFE4;
1919

2020
void func_800D63B0(void);
2121

include/maps/shared.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2831,4 +2831,17 @@ static inline void WorldObjectPositionSet(s_WorldObjectPos* eventPos, q19_12 pos
28312831
eventPos->rotation_C.vz = rotZ;
28322832
}
28332833

2834+
#define MAP_CHUNK_CHECK_VARIABLE_DECL() s32 __chunkIdx
2835+
/** @hack This macro requires a variable `s32 __chunkIdx` to be declared before using it.
2836+
* Macro `MAP_CHUNK_CHECK_VARIABLE_DECL` declares that variable, call it before calling this macro!
2837+
* First argument is `vx` or `vz` which is used as the component name in `VECTOR3`.
2838+
* Some maps appear to have a bug, where the negative position check will never be true, because they check
2839+
* if the chunk index will be some positive number. Seems like they forgot to do an ABS ?
2840+
*/
2841+
#define PLAYER_IN_MAP_CHUNK(crd, x1, x2, x3, x4) \
2842+
(__chunkIdx = g_SysWork.player_4C.chara_0.position_18.crd / FP_METER(40.0f), \
2843+
((g_SysWork.player_4C.chara_0.position_18.crd > 0 && (__chunkIdx + x1) == x2) ||\
2844+
(g_SysWork.player_4C.chara_0.position_18.crd <= 0 && (__chunkIdx + x3) == x4)))
2845+
2846+
28342847
#endif

src/maps/map0_s01/map0_s01_events.c

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -969,8 +969,7 @@ void Map_WorldObjectsInit() // 0x800DC9C8
969969

970970
void Map_WorldObjectsUpdate(void) // 0x800DCCF4
971971
{
972-
s32 stepX;
973-
s32 stepZ;
972+
MAP_CHUNK_CHECK_VARIABLE_DECL();
974973
s32 temp_a1;
975974
s32 var_a2;
976975
s32* ptr;
@@ -1086,33 +1085,19 @@ void Map_WorldObjectsUpdate(void) // 0x800DCCF4
10861085
Savegame_EventFlagSet(EventFlag_52);
10871086
}
10881087

1089-
stepX = g_SysWork.player_4C.chara_0.position_18.vx / FP_METER(40.0f);
1090-
if ((g_SysWork.player_4C.chara_0.position_18.vx > FP_METER(0.0f) && stepX == 0) ||
1091-
(g_SysWork.player_4C.chara_0.position_18.vx <= FP_METER(0.0f) && (stepX - 1) == 1))
1088+
if (PLAYER_IN_MAP_CHUNK(vx, 0, 0, -1, 1) && PLAYER_IN_MAP_CHUNK(vz, 1, 7, -1, 7))
10921089
{
1093-
stepZ = g_SysWork.player_4C.chara_0.position_18.vz / FP_METER(40.0f);
1094-
if ((g_SysWork.player_4C.chara_0.position_18.vz > FP_METER(0.0f) && (stepZ + 1) == 7) ||
1095-
(g_SysWork.player_4C.chara_0.position_18.vz <= FP_METER(0.0f) && (stepZ - 1) == 7))
1090+
if (!Savegame_EventFlagGet(EventFlag_M0S01_HealthDrink0))
10961091
{
1097-
if (!Savegame_EventFlagGet(EventFlag_M0S01_HealthDrink0))
1098-
{
1099-
g_WorldGfx_ObjectAdd(&g_CommonWorldObjects[1], &D_800DE12C.position_0, &D_800DE12C.rotation_C);
1100-
}
1092+
g_WorldGfx_ObjectAdd(&g_CommonWorldObjects[1], &D_800DE12C.position_0, &D_800DE12C.rotation_C);
11011093
}
11021094
}
11031095

1104-
stepX = g_SysWork.player_4C.chara_0.position_18.vx / FP_METER(40.0f);
1105-
if ((g_SysWork.player_4C.chara_0.position_18.vx > FP_METER(0.0f) && stepX == 0) ||
1106-
(g_SysWork.player_4C.chara_0.position_18.vx <= FP_METER(0.0f) && (stepX - 1) == 1))
1096+
if (PLAYER_IN_MAP_CHUNK(vx, 0, 0, -1, 1) && PLAYER_IN_MAP_CHUNK(vz, 1, 7, -1, 7))
11071097
{
1108-
stepZ = g_SysWork.player_4C.chara_0.position_18.vz / FP_METER(40.0f);
1109-
if ((g_SysWork.player_4C.chara_0.position_18.vz > FP_METER(0.0f) && (stepZ + 1) == 7) ||
1110-
(g_SysWork.player_4C.chara_0.position_18.vz <= FP_METER(0.0f) && (stepZ - 1) == 7))
1098+
if (!Savegame_EventFlagGet(EventFlag_M0S01_HealthDrink1))
11111099
{
1112-
if (!Savegame_EventFlagGet(EventFlag_M0S01_HealthDrink1))
1113-
{
1114-
g_WorldGfx_ObjectAdd(&g_CommonWorldObjects[1], &D_800DE140.position_0, &D_800DE140.rotation_C);
1115-
}
1100+
g_WorldGfx_ObjectAdd(&g_CommonWorldObjects[1], &D_800DE140.position_0, &D_800DE140.rotation_C);
11161101
}
11171102
}
11181103
}

0 commit comments

Comments
 (0)