From f03c5fb067d31d29dd44e63e569c0c71cc006087 Mon Sep 17 00:00:00 2001 From: Kuruyia Date: Tue, 25 Mar 2025 20:40:00 +0100 Subject: [PATCH 1/2] Document terrain attributes module This documents the `unk_02054BD0.c` file, which is in charge of loading the terrain attributes from the land data NARC for some map load modes, separately from the land data manager. Signed-off-by: Kuruyia --- include/constants/field/map.h | 9 ++ include/field/field_system.h | 4 +- include/struct_decls/struct_02054C18_decl.h | 6 -- include/terrain_attributes.h | 22 +++++ include/unk_02054BD0.h | 12 --- platinum.us/main.lsf | 2 +- src/field_map_change.c | 6 +- src/meson.build | 2 +- src/overlay005/land_data.c | 7 -- src/terrain_attributes.c | 89 +++++++++++++++++ src/terrain_collision_manager.c | 10 +- src/unk_02054BD0.c | 102 -------------------- 12 files changed, 132 insertions(+), 139 deletions(-) delete mode 100644 include/struct_decls/struct_02054C18_decl.h create mode 100644 include/terrain_attributes.h delete mode 100644 include/unk_02054BD0.h create mode 100644 src/terrain_attributes.c delete mode 100644 src/unk_02054BD0.c diff --git a/include/constants/field/map.h b/include/constants/field/map.h index 11fc37ccc1..3b4780ec28 100644 --- a/include/constants/field/map.h +++ b/include/constants/field/map.h @@ -2,10 +2,19 @@ #define POKEPLATINUM_CONSTANTS_FIELD_MAP_H #include +#include #define MAP_OBJECT_TILE_SIZE (16 * FX32_ONE) #define MAP_TILES_COUNT_X 32 #define MAP_TILES_COUNT_Y 32 +#define INVALID_LAND_DATA_ID 0xFFFF +#define INVALID_TERRAIN_ATTRIBUTES -1 +#define INVALID_MAP_MATRIX_INDEX -1 + +#define TERRAIN_ATTRIBUTES_OFFSET 0x10 +#define TERRAIN_ATTRIBUTES_SIZE 0x800 +#define TERRAIN_ATTRIBUTES_COUNT (TERRAIN_ATTRIBUTES_SIZE / sizeof(u16)) + #endif // POKEPLATINUM_CONSTANTS_FIELD_MAP_H diff --git a/include/field/field_system.h b/include/field/field_system.h index 80eb39d38a..f0709fe702 100644 --- a/include/field/field_system.h +++ b/include/field/field_system.h @@ -1,7 +1,6 @@ #ifndef POKEPLATINUM_FIELD_SYSTEM_STRUCT_H #define POKEPLATINUM_FIELD_SYSTEM_STRUCT_H -#include "struct_decls/struct_02054C18_decl.h" #include "struct_decls/struct_0205B43C_decl.h" #include "struct_decls/struct_0205C22C_decl.h" #include "struct_decls/struct_0205E884_decl.h" @@ -40,6 +39,7 @@ #include "pokedex_memory.h" #include "pokeradar.h" #include "savedata.h" +#include "terrain_attributes.h" #include "terrain_collision_manager.h" #define NUM_HONEY_TREES 21 @@ -89,7 +89,7 @@ typedef struct FieldSystem_t { AreaLightManager *areaLightMan; MapPropAnimationManager *mapPropAnimMan; MapPropOneShotAnimationManager *mapPropOneShotAnimMan; - UnkStruct_02054C18 *unk_58; + TerrainAttributes *terrainAttributes; const TerrainCollisionManager *terrainCollisionMan; BOOL skipMapAttributes; Signpost *signpost; diff --git a/include/struct_decls/struct_02054C18_decl.h b/include/struct_decls/struct_02054C18_decl.h deleted file mode 100644 index 64e87dd888..0000000000 --- a/include/struct_decls/struct_02054C18_decl.h +++ /dev/null @@ -1,6 +0,0 @@ -#ifndef POKEPLATINUM_STRUCT_02054C18_DECL_H -#define POKEPLATINUM_STRUCT_02054C18_DECL_H - -typedef struct UnkStruct_02054C18_t UnkStruct_02054C18; - -#endif // POKEPLATINUM_STRUCT_02054C18_DECL_H diff --git a/include/terrain_attributes.h b/include/terrain_attributes.h new file mode 100644 index 0000000000..afd2a8c56e --- /dev/null +++ b/include/terrain_attributes.h @@ -0,0 +1,22 @@ +#ifndef POKEPLATINUM_TERRAIN_ATTRIBUTES_H +#define POKEPLATINUM_TERRAIN_ATTRIBUTES_H + +#include + +#include "constants/field/map.h" + +#include "field/field_system_decl.h" + +#define TERRAIN_ATTRIBUTES_MAP_MATRIX_COUNT 225 +#define TERRAIN_ATTRIBUTES_MAX_BLOCK_COUNT 16 + +typedef struct TerrainAttributes { + u8 mapMatrixIndexToBlockIndex[TERRAIN_ATTRIBUTES_MAP_MATRIX_COUNT]; + u16 terrainAttributes[TERRAIN_ATTRIBUTES_COUNT * TERRAIN_ATTRIBUTES_MAX_BLOCK_COUNT]; +} TerrainAttributes; + +void TerrainAttributes_New(FieldSystem *fieldSystem, const u8 blockCount); +void TerrainAttributes_Free(FieldSystem *fieldSystem); +const u16 *TerrainAttributes_Get(const u32 mapMatrixIndex, const TerrainAttributes *terrainAttributes); + +#endif // POKEPLATINUM_TERRAIN_ATTRIBUTES_H diff --git a/include/unk_02054BD0.h b/include/unk_02054BD0.h deleted file mode 100644 index 7b0e01eecd..0000000000 --- a/include/unk_02054BD0.h +++ /dev/null @@ -1,12 +0,0 @@ -#ifndef POKEPLATINUM_UNK_02054BD0_H -#define POKEPLATINUM_UNK_02054BD0_H - -#include "struct_decls/struct_02054C18_decl.h" - -#include "field/field_system_decl.h" - -void sub_02054BD0(FieldSystem *fieldSystem, const u8 param1); -void sub_02054BF8(FieldSystem *fieldSystem); -u16 const *sub_02054C0C(const u32 param0, const UnkStruct_02054C18 *param1); - -#endif // POKEPLATINUM_UNK_02054BD0_H diff --git a/platinum.us/main.lsf b/platinum.us/main.lsf index d6a0e5e191..f96a318fc8 100644 --- a/platinum.us/main.lsf +++ b/platinum.us/main.lsf @@ -226,7 +226,7 @@ Static main Object main.nef.p/src_unk_02052C6C.c.o Object main.nef.p/src_field_map_change.c.o Object main.nef.p/src_unk_02054884.c.o - Object main.nef.p/src_unk_02054BD0.c.o + Object main.nef.p/src_terrain_attributes.c.o Object main.nef.p/src_terrain_collision_manager.c.o Object main.nef.p/src_unk_020553DC.c.o Object main.nef.p/src_unk_020556C4.c.o diff --git a/src/field_map_change.c b/src/field_map_change.c index bf3bad2b2a..9b32e4de9d 100644 --- a/src/field_map_change.c +++ b/src/field_map_change.c @@ -54,6 +54,7 @@ #include "system.h" #include "system_flags.h" #include "system_vars.h" +#include "terrain_attributes.h" #include "terrain_collision_manager.h" #include "trainer_info.h" #include "unk_020041CC.h" @@ -63,7 +64,6 @@ #include "unk_0203A7D8.h" #include "unk_0203A944.h" #include "unk_0203D1B8.h" -#include "unk_02054BD0.h" #include "unk_020553DC.h" #include "unk_020559DC.h" #include "unk_0205B33C.h" @@ -389,7 +389,7 @@ static void FieldMapChange_InitTerrainCollisionManager(FieldSystem *fieldSystem) TerrainCollisionManager_Init(&fieldSystem->terrainCollisionMan, fieldSystem->mapLoadMode->useSimpleTerrainCollisions); if (fieldSystem->mapLoadMode->unk_00_16) { - sub_02054BD0(fieldSystem, fieldSystem->mapLoadMode->unk_00_24); + TerrainAttributes_New(fieldSystem, fieldSystem->mapLoadMode->unk_00_24); } } @@ -401,7 +401,7 @@ static void FieldMapChange_RemoveTerrainCollisionManager(FieldSystem *fieldSyste fieldSystem->bottomScreen = 5; if (fieldSystem->mapLoadMode->unk_00_16) { - sub_02054BF8(fieldSystem); + TerrainAttributes_Free(fieldSystem); } fieldSystem->mapLoadMode = NULL; diff --git a/src/meson.build b/src/meson.build index 124f9c791c..78601f0777 100644 --- a/src/meson.build +++ b/src/meson.build @@ -214,7 +214,7 @@ pokeplatinum_c = files( 'unk_02052C6C.c', 'field_map_change.c', 'unk_02054884.c', - 'unk_02054BD0.c', + 'terrain_attributes.c', 'terrain_collision_manager.c', 'unk_020553DC.c', 'unk_020556C4.c', diff --git a/src/overlay005/land_data.c b/src/overlay005/land_data.c index 9cf6b80a09..f069ac5d0d 100644 --- a/src/overlay005/land_data.c +++ b/src/overlay005/land_data.c @@ -26,13 +26,6 @@ #include "system.h" #include "unk_020366A0.h" -#define INVALID_LAND_DATA_ID 0xFFFF -#define INVALID_TERRAIN_ATTRIBUTES -1 -#define INVALID_MAP_MATRIX_INDEX -1 - -#define TERRAIN_ATTRIBUTES_SIZE 0x800 -#define TERRAIN_ATTRIBUTES_COUNT 1024 - #define MAP_LAZY_LOADER_SLOT_COUNT 2 #define MAP_LAZY_LOADER_MANAGER_COUNT 2 #define MAP_MODEL_LAZY_LOADER_DATA_CHUNK_SIZE 0xE000 diff --git a/src/terrain_attributes.c b/src/terrain_attributes.c new file mode 100644 index 0000000000..5628951d9f --- /dev/null +++ b/src/terrain_attributes.c @@ -0,0 +1,89 @@ +#include "terrain_attributes.h" + +#include +#include + +#include "constants/field/map.h" + +#include "field/field_system.h" + +#include "heap.h" +#include "map_matrix.h" +#include "narc.h" + +static void TerrainAttributes_Load(MapMatrix *mapMatrix, TerrainAttributes *terrainAttributes, const u8 blockCount); +static const u8 TerrainAttributes_GetOrPushLandDataID(const u16 landDataID, u16 *blockIndexToLandDataID, u8 *blockCount); + +void TerrainAttributes_New(FieldSystem *fieldSystem, const u8 blockCount) +{ + if (fieldSystem->terrainAttributes == NULL) { + fieldSystem->terrainAttributes = Heap_AllocFromHeap(HEAP_ID_FIELDMAP, sizeof(TerrainAttributes)); + + TerrainAttributes_Load(fieldSystem->mapMatrix, fieldSystem->terrainAttributes, blockCount); + } +} + +void TerrainAttributes_Free(FieldSystem *fieldSystem) +{ + if (fieldSystem->terrainAttributes != NULL) { + Heap_FreeToHeap(fieldSystem->terrainAttributes); + fieldSystem->terrainAttributes = NULL; + } +} + +const u16 *TerrainAttributes_Get(const u32 mapMatrixIndex, const TerrainAttributes *terrainAttributes) +{ + u8 blockIndex = terrainAttributes->mapMatrixIndexToBlockIndex[mapMatrixIndex]; + return &terrainAttributes->terrainAttributes[MAP_TILES_COUNT_X * MAP_TILES_COUNT_Y * blockIndex]; +} + +static void TerrainAttributes_Load(MapMatrix *mapMatrix, TerrainAttributes *terrainAttributes, const u8 blockCount) +{ + u16 mapMatrixIndex; + u16 landDataID; + int i, j; + u16 blockIndexToLandDataID[TERRAIN_ATTRIBUTES_MAX_BLOCK_COUNT]; + + u8 realBlockCount = 0; + int mapMatrixWidth = MapMatrix_GetWidth(mapMatrix); + int mapMatrixHeight = MapMatrix_GetHeight(mapMatrix); + + for (j = 0; j < mapMatrixHeight; j++) { + for (i = 0; i < mapMatrixWidth; i++) { + mapMatrixIndex = j * mapMatrixWidth + i; + landDataID = MapMatrix_GetLandDataIDByIndex2(mapMatrixIndex, mapMatrix); + + u8 blockIndex = TerrainAttributes_GetOrPushLandDataID(landDataID, blockIndexToLandDataID, &realBlockCount); + terrainAttributes->mapMatrixIndexToBlockIndex[mapMatrixIndex] = blockIndex; + } + } + + NARC *landDataNARC = NARC_ctor(NARC_INDEX_FIELDDATA__LAND_DATA__LAND_DATA, HEAP_ID_FIELDMAP); + + for (i = 0; i < blockCount; i++) { + landDataID = blockIndexToLandDataID[i]; + void *terrainAttributesBlock = &terrainAttributes->terrainAttributes[MAP_TILES_COUNT_X * MAP_TILES_COUNT_Y * i]; + + NARC_ReadFromMember(landDataNARC, landDataID, TERRAIN_ATTRIBUTES_OFFSET, TERRAIN_ATTRIBUTES_SIZE, terrainAttributesBlock); + } + + NARC_dtor(landDataNARC); +} + +static const u8 TerrainAttributes_GetOrPushLandDataID(const u16 landDataID, u16 *blockIndexToLandDataID, u8 *blockCount) +{ + u8 i; + + for (i = 0; i < *blockCount; i++) { + if (blockIndexToLandDataID[i] == landDataID) { + return i; + } + } + + GF_ASSERT(i < TERRAIN_ATTRIBUTES_MAX_BLOCK_COUNT); + + blockIndexToLandDataID[i] = landDataID; + (*blockCount)++; + + return i; +} diff --git a/src/terrain_collision_manager.c b/src/terrain_collision_manager.c index ab4a2555b2..6624d090f3 100644 --- a/src/terrain_collision_manager.c +++ b/src/terrain_collision_manager.c @@ -16,10 +16,10 @@ #include "dynamic_map_features.h" #include "map_matrix.h" #include "map_tile_behavior.h" -#include "unk_02054BD0.h" +#include "terrain_attributes.h" -#define INVALID_TERRAIN_ATTRIBUTES 0xFF -#define INVALID_TILE_BEHAVIOR 0xFF +#define INVALID_TILE_ATTRIBUTES 0xFF +#define INVALID_TILE_BEHAVIOR 0xFF #define TERRAIN_ATTRIBUTES_COLLISION_SHIFT 15 #define TERRAIN_ATTRIBUTES_COLLISION_MASK 0x01 @@ -180,7 +180,7 @@ static BOOL GetTileAttributes(const FieldSystem *fieldSystem, const int tileX, c BOOL loadedMapIndexValid = LandDataManager_GetRelativeLoadedMapsQuadrantOfTile(landDataMan, tileX, tileY, &loadedMapIndex); if (loadedMapIndexValid == FALSE) { - (*attributes) = INVALID_TERRAIN_ATTRIBUTES; + (*attributes) = INVALID_TILE_ATTRIBUTES; return FALSE; } @@ -203,7 +203,7 @@ static BOOL GetSimpleTileAttributes(const FieldSystem *fieldSystem, const int ti u32 mapMatrixIndex = mapMatrixX + mapMatrixY * mapMatrixWidth; u32 tileIndex = (tileY % MAP_TILES_COUNT_Y) * MAP_TILES_COUNT_X + (tileX % MAP_TILES_COUNT_X); - u16 const *terrainAttributes = sub_02054C0C(mapMatrixIndex, fieldSystem->unk_58); + u16 const *terrainAttributes = TerrainAttributes_Get(mapMatrixIndex, fieldSystem->terrainAttributes); *attributes = terrainAttributes[tileIndex]; return TRUE; diff --git a/src/unk_02054BD0.c b/src/unk_02054BD0.c deleted file mode 100644 index f230811c55..0000000000 --- a/src/unk_02054BD0.c +++ /dev/null @@ -1,102 +0,0 @@ -#include "unk_02054BD0.h" - -#include -#include - -#include "field/field_system.h" - -#include "heap.h" -#include "map_matrix.h" -#include "narc.h" - -typedef struct UnkStruct_02054C18_t { - u8 unk_00[225]; - u16 unk_E2[16384]; -} UnkStruct_02054C18; - -static void sub_02054C18(MapMatrix *param0, UnkStruct_02054C18 *param1, const u8 param2); -static const u8 sub_02054CC4(const u16 param0, u16 *param1, u8 *param2); - -void sub_02054BD0(FieldSystem *fieldSystem, const u8 param1) -{ - int v0; - void *v1; - - if (fieldSystem->unk_58 == NULL) { - fieldSystem->unk_58 = Heap_AllocFromHeap(HEAP_ID_FIELDMAP, sizeof(UnkStruct_02054C18)); - - sub_02054C18(fieldSystem->mapMatrix, fieldSystem->unk_58, param1); - } -} - -void sub_02054BF8(FieldSystem *fieldSystem) -{ - if (fieldSystem->unk_58 != NULL) { - Heap_FreeToHeap(fieldSystem->unk_58); - fieldSystem->unk_58 = NULL; - } -} - -u16 const *sub_02054C0C(const u32 param0, const UnkStruct_02054C18 *param1) -{ - u16 const *v0; - u8 v1 = param1->unk_00[param0]; - v0 = (u16 const *)&(param1->unk_E2[(32 * 32) * v1]); - - return v0; -} - -static void sub_02054C18(MapMatrix *param0, UnkStruct_02054C18 *param1, const u8 param2) -{ - u8 v0; - u16 v1; - u8 v2; - u16 v3; - int v4, v5; - int v6, v7; - u16 v8[16]; - void *v9; - NARC *v10; - - v0 = 0; - v7 = MapMatrix_GetWidth(param0); - v6 = MapMatrix_GetHeight(param0); - - for (v5 = 0; v5 < v6; v5++) { - for (v4 = 0; v4 < v7; v4++) { - v3 = v5 * v7 + v4; - v1 = MapMatrix_GetLandDataIDByIndex2(v3, param0); - v2 = sub_02054CC4(v1, v8, &v0); - param1->unk_00[v3] = v2; - } - } - - v10 = NARC_ctor(NARC_INDEX_FIELDDATA__LAND_DATA__LAND_DATA, HEAP_ID_FIELDMAP); - - for (v4 = 0; v4 < param2; v4++) { - v1 = v8[v4]; - v9 = &(param1->unk_E2[(32 * 32) * v4]); - - NARC_ReadFromMember(v10, v1, 0x10, 0x800, v9); - } - - NARC_dtor(v10); -} - -static const u8 sub_02054CC4(const u16 param0, u16 *param1, u8 *param2) -{ - u8 v0; - - for (v0 = 0; v0 < (*param2); v0++) { - if (param1[v0] == param0) { - return v0; - } - } - - GF_ASSERT(v0 < 16); - - param1[v0] = param0; - (*param2)++; - - return v0; -} From 45c3c1a000c26d80500b767eb39ac352a0825d21 Mon Sep 17 00:00:00 2001 From: Kuruyia Date: Wed, 26 Mar 2025 07:47:39 +0100 Subject: [PATCH 2/2] Document terrain attributes module-related map load mode fields This documents the following fields in the map load mode struct: - `useSeparateTerrainAttributes`: whether to load terrain attributes using the separate terrain attributes module. - `separateTerrainAttributesBlockCount`: the number of terrain attributes block to load when using the separate terrain attributes module. Each block corresponds to the terrain attributes contained in a land data file. Note: when using the simple terrain collisions in a map load mode, it is mandatory to also use the separate terrain attributes module, as this is where the terrain collision manager will get terrain attributes. Signed-off-by: Kuruyia --- include/struct_defs/map_load_mode.h | 4 ++-- src/field_map_change.c | 16 ++++++++-------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/include/struct_defs/map_load_mode.h b/include/struct_defs/map_load_mode.h index 72ebb3ab26..09620992a3 100644 --- a/include/struct_defs/map_load_mode.h +++ b/include/struct_defs/map_load_mode.h @@ -6,9 +6,9 @@ typedef struct MapLoadMode { u32 skipMapAttributes : 4; u32 useSimpleTerrainCollisions : 4; u32 unk_00_12 : 4; - u32 unk_00_16 : 4; + u32 useSeparateTerrainAttributes : 4; u32 unk_00_20 : 4; - u32 unk_00_24 : 8; + u32 separateTerrainAttributesBlockCount : 8; u32 unk_04; } MapLoadMode; diff --git a/src/field_map_change.c b/src/field_map_change.c index 9b32e4de9d..8f87b10988 100644 --- a/src/field_map_change.c +++ b/src/field_map_change.c @@ -156,11 +156,11 @@ static void sub_02053E5C(FieldTask *task); static BOOL sub_0205444C(FieldTask *task, int param1); static const MapLoadMode sMapLoadMode[] = { - { 0x1, FALSE, FALSE, 0x0, 0x0, 0x1, 0x0, 0xC4000 }, - { 0x2, TRUE, TRUE, 0x1, 0x1, 0x0, 0x10, 0xC4000 }, - { 0x3, FALSE, FALSE, 0x0, 0x0, 0x1, 0x0, 0xC4000 }, - { 0x4, TRUE, TRUE, 0x0, 0x1, 0x1, 0x1, 0xC4000 }, - { 0x1, TRUE, TRUE, 0x0, 0x1, 0x1, 0x1, 0xA0000 } + { 0x1, FALSE, FALSE, 0x0, FALSE, 0x1, 0x0, 0xC4000 }, + { 0x2, TRUE, TRUE, 0x1, TRUE, 0x0, 0x10, 0xC4000 }, + { 0x3, FALSE, FALSE, 0x0, FALSE, 0x1, 0x0, 0xC4000 }, + { 0x4, TRUE, TRUE, 0x0, TRUE, 0x1, 0x1, 0xC4000 }, + { 0x1, TRUE, TRUE, 0x0, TRUE, 0x1, 0x1, 0xA0000 } }; static const WindowTemplate Unk_020EC3A0 = { @@ -388,8 +388,8 @@ static void FieldMapChange_InitTerrainCollisionManager(FieldSystem *fieldSystem) TerrainCollisionManager_Init(&fieldSystem->terrainCollisionMan, fieldSystem->mapLoadMode->useSimpleTerrainCollisions); - if (fieldSystem->mapLoadMode->unk_00_16) { - TerrainAttributes_New(fieldSystem, fieldSystem->mapLoadMode->unk_00_24); + if (fieldSystem->mapLoadMode->useSeparateTerrainAttributes) { + TerrainAttributes_New(fieldSystem, fieldSystem->mapLoadMode->separateTerrainAttributesBlockCount); } } @@ -400,7 +400,7 @@ static void FieldMapChange_RemoveTerrainCollisionManager(FieldSystem *fieldSyste fieldSystem->terrainCollisionMan = NULL; fieldSystem->bottomScreen = 5; - if (fieldSystem->mapLoadMode->unk_00_16) { + if (fieldSystem->mapLoadMode->useSeparateTerrainAttributes) { TerrainAttributes_Free(fieldSystem); }