From 86a9d94ce1eb7930cb9a59ad8f968388a627c58a Mon Sep 17 00:00:00 2001 From: GriffinR Date: Tue, 6 Apr 2021 16:05:43 -0400 Subject: [PATCH] Document fieldmap --- include/constants/metatile_behaviors.h | 1 + include/fieldmap.h | 7 +- include/global.fieldmap.h | 5 +- src/battle_pike.c | 2 +- src/battle_pyramid.c | 2 +- src/fieldmap.c | 118 ++++++++++++------------- src/save.c | 2 +- src/start_menu.c | 2 +- 8 files changed, 66 insertions(+), 73 deletions(-) diff --git a/include/constants/metatile_behaviors.h b/include/constants/metatile_behaviors.h index 94cb0ffca..5120bd9dc 100755 --- a/include/constants/metatile_behaviors.h +++ b/include/constants/metatile_behaviors.h @@ -241,5 +241,6 @@ #define MB_UNUSED_ED 0xED #define MB_UNUSED_EE 0xEE #define MB_UNUSED_EF 0xEF +#define MB_INVALID 0xFF #endif // GUARD_METATILE_BEHAVIORS diff --git a/include/fieldmap.h b/include/fieldmap.h index 438fb4787..5bcbe20ea 100644 --- a/include/fieldmap.h +++ b/include/fieldmap.h @@ -20,16 +20,13 @@ void MapGridSetMetatileEntryAt(int, int, u16); void GetCameraCoords(u16*, u16*); bool8 MapGridIsImpassableAt(int, int); int GetMapBorderIdAt(int x, int y); -int CanCameraMoveInDirection(int direction); +bool32 CanCameraMoveInDirection(int direction); u16 GetBehaviorByMetatileId(u16 metatileId); void GetCameraFocusCoords(u16 *x, u16 *y); u8 MapGridGetMetatileLayerTypeAt(int x, int y); u8 MapGridGetZCoordAt(int x, int y); bool8 CameraMove(int deltaX, int deltaY); -struct MapConnection *sub_8088950(u8 direction, int x, int y); -bool8 sub_80889A8(u8 direction, int x, int y, struct MapConnection *connection); -bool8 sub_8088A0C(int x, int src_width, int dest_width, int offset); -void save_serialize_map(void); +void SaveMapView(void); void SetCameraFocusCoords(u16 x, u16 y); void InitMap(void); void InitMapFromSavedGame(void); diff --git a/include/global.fieldmap.h b/include/global.fieldmap.h index c4d7be35d..a3d99ee21 100644 --- a/include/global.fieldmap.h +++ b/include/global.fieldmap.h @@ -1,6 +1,7 @@ #ifndef GUARD_GLOBAL_FIELDMAP_H #define GUARD_GLOBAL_FIELDMAP_H +#define METATILE_BEHAVIOR_MASK 0x00FF #define METATILE_COLLISION_MASK 0x0C00 #define METATILE_ID_MASK 0x03FF #define METATILE_ID_UNDEFINED 0x03FF @@ -12,7 +13,9 @@ enum { - CONNECTION_SOUTH = 1, + CONNECTION_INVALID = -1, + CONNECTION_NONE, + CONNECTION_SOUTH, CONNECTION_NORTH, CONNECTION_WEST, CONNECTION_EAST, diff --git a/src/battle_pike.c b/src/battle_pike.c index ed89f5ed9..d889d7cdd 100644 --- a/src/battle_pike.c +++ b/src/battle_pike.c @@ -711,7 +711,7 @@ static void SavePikeChallenge(void) gSaveBlock2Ptr->frontier.challengeStatus = gSpecialVar_0x8005; VarSet(VAR_TEMP_0, 0); gSaveBlock2Ptr->frontier.challengePaused = TRUE; - save_serialize_map(); + SaveMapView(); TrySavingData(SAVE_LINK); } diff --git a/src/battle_pyramid.c b/src/battle_pyramid.c index ac6d95338..a41a80bbb 100644 --- a/src/battle_pyramid.c +++ b/src/battle_pyramid.c @@ -933,7 +933,7 @@ static void SavePyramidChallenge(void) gSaveBlock2Ptr->frontier.challengeStatus = gSpecialVar_0x8005; VarSet(VAR_TEMP_0, 0); gSaveBlock2Ptr->frontier.challengePaused = TRUE; - save_serialize_map(); + SaveMapView(); TrySavingData(SAVE_LINK); } diff --git a/src/fieldmap.c b/src/fieldmap.c index 2961d2f49..82c085f9f 100644 --- a/src/fieldmap.c +++ b/src/fieldmap.c @@ -15,6 +15,7 @@ #include "trainer_hill.h" #include "tv.h" #include "constants/rgb.h" +#include "constants/metatile_behaviors.h" struct ConnectionFlags { @@ -43,6 +44,9 @@ static void FillEastConnection(struct MapHeader const *mapHeader, struct MapHead static void InitBackupMapLayoutConnections(struct MapHeader *mapHeader); static void LoadSavedMapView(void); static bool8 SkipCopyingMetatileFromSavedMap(u16* mapMetatilePtr, u16 mapWidth, u8 yMode); +static struct MapConnection *GetIncomingConnection(u8 direction, int x, int y); +static bool8 IsPosInIncomingConnectingMap(u8 direction, int x, int y, struct MapConnection *connection); +static bool8 IsCoordInIncomingConnectingMap(int coord, int srcMax, int destMax, int offset); struct MapHeader const *const GetMapHeaderFromConnection(struct MapConnection *connection) { @@ -149,7 +153,7 @@ static void InitBackupMapLayoutConnections(struct MapHeader *mapHeader) } } -static void sub_8087F54(int x, int y, struct MapHeader const *connectedMapHeader, int x2, int y2, int width, int height) +static void FillConnection(int x, int y, struct MapHeader const *connectedMapHeader, int x2, int y2, int width, int height) { int i; u16 *src; @@ -207,7 +211,7 @@ static void FillSouthConnection(struct MapHeader const *mapHeader, struct MapHea } } - sub_8087F54( + FillConnection( x, y, connectedMapHeader, x2, /*y2*/ 0, @@ -255,7 +259,7 @@ static void FillNorthConnection(struct MapHeader const *mapHeader, struct MapHea } } - sub_8087F54( + FillConnection( x, /*y*/ 0, connectedMapHeader, x2, y2, @@ -302,7 +306,7 @@ static void FillWestConnection(struct MapHeader const *mapHeader, struct MapHead } } - sub_8087F54( + FillConnection( /*x*/ 0, y, connectedMapHeader, x2, y2, @@ -347,7 +351,7 @@ static void FillEastConnection(struct MapHeader const *mapHeader, struct MapHead } } - sub_8087F54( + FillConnection( x, y, connectedMapHeader, /*x2*/ 0, y2, @@ -458,7 +462,7 @@ u32 MapGridGetMetatileBehaviorAt(int x, int y) { u16 metatile; metatile = MapGridGetMetatileIdAt(x, y); - return GetBehaviorByMetatileId(metatile) & 0xff; + return GetBehaviorByMetatileId(metatile) & METATILE_BEHAVIOR_MASK; } u8 MapGridGetMetatileLayerTypeAt(int x, int y) @@ -505,11 +509,11 @@ u16 GetBehaviorByMetatileId(u16 metatile) } else { - return 0xFF; + return MB_INVALID; } } -void save_serialize_map(void) +void SaveMapView(void) { int i, j; int x, y; @@ -595,7 +599,7 @@ static void LoadSavedMapView(void) } } -void sub_80885C4(u8 a1) +static void MoveMapViewToBackup(u8 direction) { int width; u16 *mapView; @@ -614,7 +618,7 @@ void sub_80885C4(u8 a1) y0 = gSaveBlock1Ptr->pos.y; x2 = 15; y2 = 14; - switch (a1) + switch (direction) { case CONNECTION_NORTH: y0 += 1; @@ -680,14 +684,14 @@ int GetMapBorderIdAt(int x, int y) } goto success; fail: - return -1; + return CONNECTION_INVALID; success: if (x >= (gBackupMapLayout.width - 8)) { if (!gMapConnectionFlags.east) { - return -1; + return CONNECTION_INVALID; } return CONNECTION_EAST; } @@ -695,7 +699,7 @@ success: { if (!gMapConnectionFlags.west) { - return -1; + return CONNECTION_INVALID; } return CONNECTION_WEST; } @@ -703,7 +707,7 @@ success: { if (!gMapConnectionFlags.south) { - return -1; + return CONNECTION_INVALID; } return CONNECTION_SOUTH; } @@ -711,13 +715,13 @@ success: { if (!gMapConnectionFlags.north) { - return -1; + return CONNECTION_INVALID; } return CONNECTION_NORTH; } else { - return 0; + return CONNECTION_NONE; } } @@ -726,19 +730,19 @@ int GetPostCameraMoveMapBorderId(int x, int y) return GetMapBorderIdAt(gSaveBlock1Ptr->pos.x + 7 + x, gSaveBlock1Ptr->pos.y + 7 + y); } -int CanCameraMoveInDirection(int direction) +bool32 CanCameraMoveInDirection(int direction) { int x, y; x = gSaveBlock1Ptr->pos.x + 7 + gDirectionToVectors[direction].x; y = gSaveBlock1Ptr->pos.y + 7 + gDirectionToVectors[direction].y; if (GetMapBorderIdAt(x, y) == -1) { - return 0; + return FALSE; } - return 1; + return TRUE; } -void sub_80887F8(struct MapConnection *connection, int direction, int x, int y) +static void SetPositionFromConnection(struct MapConnection *connection, int direction, int x, int y) { struct MapHeader const *mapHeader; mapHeader = GetMapHeaderFromConnection(connection); @@ -765,69 +769,57 @@ void sub_80887F8(struct MapConnection *connection, int direction, int x, int y) bool8 CameraMove(int x, int y) { - unsigned int direction; + int direction; struct MapConnection *connection; int old_x, old_y; gCamera.active = FALSE; direction = GetPostCameraMoveMapBorderId(x, y); - if (direction + 1 <= 1) + if (direction == CONNECTION_NONE || direction == CONNECTION_INVALID) { gSaveBlock1Ptr->pos.x += x; gSaveBlock1Ptr->pos.y += y; } else { - save_serialize_map(); + SaveMapView(); ClearMirageTowerPulseBlendEffect(); old_x = gSaveBlock1Ptr->pos.x; old_y = gSaveBlock1Ptr->pos.y; - connection = sub_8088950(direction, gSaveBlock1Ptr->pos.x, gSaveBlock1Ptr->pos.y); - sub_80887F8(connection, direction, x, y); + connection = GetIncomingConnection(direction, gSaveBlock1Ptr->pos.x, gSaveBlock1Ptr->pos.y); + SetPositionFromConnection(connection, direction, x, y); LoadMapFromCameraTransition(connection->mapGroup, connection->mapNum); gCamera.active = TRUE; gCamera.x = old_x - gSaveBlock1Ptr->pos.x; gCamera.y = old_y - gSaveBlock1Ptr->pos.y; gSaveBlock1Ptr->pos.x += x; gSaveBlock1Ptr->pos.y += y; - sub_80885C4(direction); + MoveMapViewToBackup(direction); } return gCamera.active; } -struct MapConnection *sub_8088950(u8 direction, int x, int y) +static struct MapConnection *GetIncomingConnection(u8 direction, int x, int y) { int count; int i; struct MapConnection *connection; const struct MapConnections *connections = gMapHeader.connections; - // UB: Multiple possible null dereferences -#ifdef UBFIX - if (connections != NULL) - { - count = connections->count; - connection = connections->connections; - if (connection != NULL) - { - for (i = 0; i < count; i++, connection++) - { - if (connection->direction == direction && sub_80889A8(direction, x, y, connection) == TRUE) - return connection; - } - } - } -#else + +#ifdef UBFIX // UB: Multiple possible null dereferences + if (connections == NULL || connections->connections == NULL) + return NULL; +#endif count = connections->count; connection = connections->connections; for (i = 0; i < count; i++, connection++) { - if (connection->direction == direction && sub_80889A8(direction, x, y, connection) == TRUE) + if (connection->direction == direction && IsPosInIncomingConnectingMap(direction, x, y, connection) == TRUE) return connection; } -#endif return NULL; } -bool8 sub_80889A8(u8 direction, int x, int y, struct MapConnection *connection) +static bool8 IsPosInIncomingConnectingMap(u8 direction, int x, int y, struct MapConnection *connection) { struct MapHeader const *mapHeader; mapHeader = GetMapHeaderFromConnection(connection); @@ -835,15 +827,15 @@ bool8 sub_80889A8(u8 direction, int x, int y, struct MapConnection *connection) { case CONNECTION_SOUTH: case CONNECTION_NORTH: - return sub_8088A0C(x, gMapHeader.mapLayout->width, mapHeader->mapLayout->width, connection->offset); + return IsCoordInIncomingConnectingMap(x, gMapHeader.mapLayout->width, mapHeader->mapLayout->width, connection->offset); case CONNECTION_WEST: case CONNECTION_EAST: - return sub_8088A0C(y, gMapHeader.mapLayout->height, mapHeader->mapLayout->height, connection->offset); + return IsCoordInIncomingConnectingMap(y, gMapHeader.mapLayout->height, mapHeader->mapLayout->height, connection->offset); } return FALSE; } -bool8 sub_8088A0C(int x, int src_width, int dest_width, int offset) +static bool8 IsCoordInIncomingConnectingMap(int coord, int srcMax, int destMax, int offset) { int offset2; offset2 = offset; @@ -851,24 +843,24 @@ bool8 sub_8088A0C(int x, int src_width, int dest_width, int offset) if (offset2 < 0) offset2 = 0; - if (dest_width + offset < src_width) - src_width = dest_width + offset; + if (destMax + offset < srcMax) + srcMax = destMax + offset; - if (offset2 <= x && x <= src_width) + if (offset2 <= coord && coord <= srcMax) return TRUE; return FALSE; } -int sub_8088A38(int x, int width) +static int IsCoordInConnectingMap(int coord, int max) { - if (x >= 0 && x < width) + if (coord >= 0 && coord < max) return TRUE; return FALSE; } -int sub_8088A4C(struct MapConnection *connection, int x, int y) +static int IsPosInConnectingMap(struct MapConnection *connection, int x, int y) { struct MapHeader const *mapHeader; mapHeader = GetMapHeaderFromConnection(connection); @@ -876,10 +868,10 @@ int sub_8088A4C(struct MapConnection *connection, int x, int y) { case CONNECTION_SOUTH: case CONNECTION_NORTH: - return sub_8088A38(x - connection->offset, mapHeader->mapLayout->width); + return IsCoordInConnectingMap(x - connection->offset, mapHeader->mapLayout->width); case CONNECTION_WEST: case CONNECTION_EAST: - return sub_8088A38(y - connection->offset, mapHeader->mapLayout->height); + return IsCoordInConnectingMap(y - connection->offset, mapHeader->mapLayout->height); } return FALSE; } @@ -909,7 +901,7 @@ struct MapConnection *GetConnectionAtCoords(s16 x, s16 y) { continue; } - if (sub_8088A4C(connection, x - 7, y - 7) == TRUE) + if (IsPosInConnectingMap(connection, x - 7, y - 7) == TRUE) { return connection; } @@ -991,12 +983,12 @@ static void CopyTilesetToVramUsingHeap(struct Tileset const *tileset, u16 numTil } } -void nullsub_3(u16 a0, u16 a1) +static void FieldmapPaletteDummy(u16 offset, u16 size) { } -void nullsub_90(void) +static void FieldmapUnkDummy(void) { } @@ -1011,17 +1003,17 @@ void LoadTilesetPalette(struct Tileset const *tileset, u16 destOffset, u16 size) { LoadPalette(&black, destOffset, 2); LoadPalette(((u16*)tileset->palettes) + 1, destOffset + 1, size - 2); - nullsub_3(destOffset + 1, (size - 2) >> 1); + FieldmapPaletteDummy(destOffset + 1, (size - 2) >> 1); } else if (tileset->isSecondary == TRUE) { LoadPalette(((u16*)tileset->palettes) + (NUM_PALS_IN_PRIMARY * 16), destOffset, size); - nullsub_3(destOffset, size >> 1); + FieldmapPaletteDummy(destOffset, size >> 1); } else { LoadCompressedPalette((u32*)tileset->palettes, destOffset, size); - nullsub_3(destOffset, size >> 1); + FieldmapPaletteDummy(destOffset, size >> 1); } } } diff --git a/src/save.c b/src/save.c index bbf4e7dbe..52301df03 100644 --- a/src/save.c +++ b/src/save.c @@ -913,7 +913,7 @@ void Task_LinkSave(u8 taskId) if (IsLinkTaskFinished()) { if (!tPartialSave) - save_serialize_map(); + SaveMapView(); tState = 3; } break; diff --git a/src/start_menu.c b/src/start_menu.c index 2316b85b6..275c6c032 100644 --- a/src/start_menu.c +++ b/src/start_menu.c @@ -850,7 +850,7 @@ static bool8 BattlePyramidRetireCallback(void) static void InitSave(void) { - save_serialize_map(); + SaveMapView(); sSaveDialogCallback = SaveConfirmSaveCallback; sSavingComplete = FALSE; }