Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix type for offset in MapConnection #2011

Merged
merged 1 commit into from
Aug 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion include/global.fieldmap.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ struct MapEvents
struct MapConnection
{
u8 direction;
u32 offset;
s32 offset;
u8 mapGroup;
u8 mapNum;
};
Expand Down
2 changes: 1 addition & 1 deletion src/fieldmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
47 changes: 22 additions & 25 deletions src/item_use.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down