From a3d5f54b7588d6b5b3125c6c61356dcc0123b544 Mon Sep 17 00:00:00 2001 From: GriffinR Date: Mon, 1 Jul 2024 15:51:15 -0400 Subject: [PATCH] Fix type for offset in MapConnection --- include/global.fieldmap.h | 2 +- src/fieldmap.c | 2 +- src/item_use.c | 47 ++++++++++++++++++--------------------- 3 files changed, 24 insertions(+), 27 deletions(-) diff --git a/include/global.fieldmap.h b/include/global.fieldmap.h index 7461929c3a5c..c7a4133ea810 100644 --- a/include/global.fieldmap.h +++ b/include/global.fieldmap.h @@ -131,7 +131,7 @@ struct MapEvents struct MapConnection { u8 direction; - u32 offset; + s32 offset; u8 mapGroup; u8 mapNum; }; diff --git a/src/fieldmap.c b/src/fieldmap.c index d9c4fa371a29..5fdc4cbfb721 100644 --- a/src/fieldmap.c +++ b/src/fieldmap.c @@ -144,7 +144,7 @@ static void InitBackupMapLayoutConnections(struct MapHeader *mapHeader) for (i = 0; i < count; i++, connection++) { struct MapHeader const *cMap = GetMapHeaderFromConnection(connection); - u32 offset = connection->offset; + s32 offset = connection->offset; switch (connection->direction) { case CONNECTION_SOUTH: diff --git a/src/item_use.c b/src/item_use.c index 60338e437a0e..abd80e0eae3b 100755 --- a/src/item_use.c +++ b/src/item_use.c @@ -393,46 +393,43 @@ static bool8 IsHiddenItemPresentAtCoords(const struct MapEvents *events, s16 x, static bool8 IsHiddenItemPresentInConnection(const struct MapConnection *connection, int x, int y) { - - u16 localX, localY; - u32 localOffset; - s32 localLength; - - struct MapHeader const *const mapHeader = GetMapHeaderFromConnection(connection); - + s16 connectionX, connectionY; + struct MapHeader const *const connectionHeader = GetMapHeaderFromConnection(connection); + +// To convert our x/y into coordinates that are relative to the connected map, we must: +// - Subtract the virtual offset used for the border buffer (MAP_OFFSET). +// - Subtract the horizontal offset between North/South connections, or the vertical offset for East/West +// - Account for map size. (0,0) is in the NW corner of our map, so when looking North/West we have to add the height/width of the connected map, +// and when looking South/East we have to subtract the height/width of our current map. +#define localX (x - MAP_OFFSET) +#define localY (y - MAP_OFFSET) switch (connection->direction) { - // same weird temp variable behavior seen in IsHiddenItemPresentAtCoords case CONNECTION_NORTH: - localOffset = connection->offset + MAP_OFFSET; - localX = x - localOffset; - localLength = mapHeader->mapLayout->height - MAP_OFFSET; - localY = localLength + y; // additions are reversed for some reason + connectionX = localX - connection->offset; + connectionY = connectionHeader->mapLayout->height + localY; break; case CONNECTION_SOUTH: - localOffset = connection->offset + MAP_OFFSET; - localX = x - localOffset; - localLength = gMapHeader.mapLayout->height + MAP_OFFSET; - localY = y - localLength; + connectionX = localX - connection->offset; + connectionY = localY - gMapHeader.mapLayout->height; break; case CONNECTION_WEST: - localLength = mapHeader->mapLayout->width - MAP_OFFSET; - localX = localLength + x; // additions are reversed for some reason - localOffset = connection->offset + MAP_OFFSET; - localY = y - localOffset; + connectionX = connectionHeader->mapLayout->width + localX; + connectionY = localY - connection->offset; break; case CONNECTION_EAST: - localLength = gMapHeader.mapLayout->width + MAP_OFFSET; - localX = x - localLength; - localOffset = connection->offset + MAP_OFFSET; - localY = y - localOffset; + connectionX = localX - gMapHeader.mapLayout->width; + connectionY = localY - connection->offset; break; default: return FALSE; } - return IsHiddenItemPresentAtCoords(mapHeader->events, localX, localY); + return IsHiddenItemPresentAtCoords(connectionHeader->events, connectionX, connectionY); } +#undef localX +#undef localY + static void CheckForHiddenItemsInMapConnection(u8 taskId) { s16 playerX, playerY;