Skip to content

Commit

Permalink
Fix type for offset in MapConnection
Browse files Browse the repository at this point in the history
  • Loading branch information
GriffinRichards authored and SBird1337 committed Aug 13, 2024
1 parent 312749d commit a3d5f54
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 27 deletions.
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

0 comments on commit a3d5f54

Please sign in to comment.