diff --git a/include/constants/metatile_behaviors.h b/include/constants/metatile_behaviors.h index a2f21d031..a78d169a0 100644 --- a/include/constants/metatile_behaviors.h +++ b/include/constants/metatile_behaviors.h @@ -242,4 +242,6 @@ #define MB_UNUSED_EE 0xEE #define MB_UNUSED_EF 0xEF +#define MB_INVALID 0xFF + #endif // GUARD_METATILE_BEHAVIORS diff --git a/include/event_object_movement.h b/include/event_object_movement.h index 8a28be8ae..3324437dc 100644 --- a/include/event_object_movement.h +++ b/include/event_object_movement.h @@ -378,7 +378,7 @@ u8 AddCameraObject(u8); void CameraObjectReset1(void); const u8 *GetObjectEventScriptPointerByObjectEventId(u8); u8 ObjectEventGetBerryTreeId(u8); -struct ObjectEventTemplate *GetObjectEventTemplateByLocalIdAndMap(u8, u8, u8); +const struct ObjectEventTemplate *GetObjectEventTemplateByLocalIdAndMap(u8, u8, u8); void OverrideTemplateCoordsForObjectEvent(struct ObjectEvent *pObject); void OverrideMovementTypeForObjectEvent(struct ObjectEvent *, u8); void TryOverrideTemplateCoordsForObjectEvent(u8, u8, u8); diff --git a/include/field_control_avatar.h b/include/field_control_avatar.h index bcb2b449e..e38836fa4 100644 --- a/include/field_control_avatar.h +++ b/include/field_control_avatar.h @@ -29,7 +29,7 @@ int ProcessPlayerFieldInput(struct FieldInput *pStruct); const u8 *GetInteractedLinkPlayerScript(struct MapPosition *, u8, u8); void ClearPoisonStepCounter(void); void RestartWildEncounterImmunitySteps(void); -u8 *GetCoordEventScriptAtMapPosition(struct MapPosition *); +const u8 *GetCoordEventScriptAtMapPosition(struct MapPosition *); const u8 *GetObjectEventScriptPointerPlayerFacing(void); u8 TrySetDiveWarp(void); bool8 dive_warp(struct MapPosition*, u16); diff --git a/include/fieldmap.h b/include/fieldmap.h index 0579547e8..ff713b55f 100644 --- a/include/fieldmap.h +++ b/include/fieldmap.h @@ -2,7 +2,7 @@ #define GUARD_FIELDMAP2_H #include "event_object_movement.h" -#define NUM_TILES_IN_PRIMARY 512 +#define NUM_TILES_IN_PRIMARY 512 // NOTE: CopyTilesetToVram assumes the number of primary and secondary tiles is the same #define NUM_TILES_TOTAL 1024 #define NUM_METATILES_IN_PRIMARY 512 #define NUM_METATILES_TOTAL 1024 @@ -10,43 +10,41 @@ #define NUM_PALS_TOTAL 12 #define MAX_MAP_DATA_SIZE 0x2800 -extern struct BackupMapLayout gBackupMapLayout; -struct MapHeader *mapconnection_get_mapheader(struct MapConnection *connection); -int GetMapBorderIdAt(int x, int y); -int CanCameraMoveInDirection(int direction); -u16 GetBehaviorByMetatileId(u16 metatile); -void MapGridSetMetatileEntryAt(int, int, u16); +#define NUM_TILES_PER_METATILE 8 -void not_trainer_hill_battle_pyramid(void); -void sub_8055FC0(void); -void mapheader_copy_mapdata_with_padding(struct MapHeader *mapHeader); -void map_copy_with_padding(u16 *map, u16 width, u16 height); -void sub_80560AC(struct MapHeader *); -void fillSouthConnection(struct MapHeader *, struct MapHeader *, s32); -void fillNorthConnection(struct MapHeader *, struct MapHeader *, s32); -void fillWestConnection(struct MapHeader *, struct MapHeader *, s32); -void fillEastConnection(struct MapHeader *, struct MapHeader *, s32); -u8 MapGridGetZCoordAt(int s, int y); -u8 MapGridIsImpassableAt(int, int); -u32 MapGridGetMetatileIdAt(int, int); -u32 MapGridGetMetatileBehaviorAt(int x, int y); // return: (u8|u16|int) args: (int|s16|s32) -u8 MapGridGetMetatileLayerTypeAt(int, int); -void MapGridSetMetatileIdAt(int, int, u16); -u16 GetBehaviorByMetatileId(u16 metatile); -void save_serialize_map(void); -void sub_8056670(); -bool8 CameraMove(int, int); -struct MapConnection *sub_8056A64(u8 direction, int x, int y); -bool8 sub_8056ABC(u8 direction, int x, int y, struct MapConnection *connection); -bool8 sub_8056B20(int x, int src_width, int dest_width, int offset); -struct MapConnection *sub_8056BA0(s16 x, s16 y); // fieldmap.c -void sub_8056C50(u16, u16); -void sav1_camera_get_focus_coords(u16 *x, u16 *y); -void GetCameraCoords(u16*, u16*); -void sub_8056D28(struct MapLayout *pData); -void sub_8056D38(struct MapLayout *pData); -void apply_map_tileset2_palette(struct MapLayout *pData); -void copy_map_tileset1_tileset2_to_vram(/*TODO: arg types*/); -void apply_map_tileset1_tileset2_palette(/*TODO: arg types*/); +// Map coordinates are offset by 7 when using the map +// buffer because it needs to load sufficient border +// metatiles to fill the player's view (the player has +// 7 metatiles of view horizontally in either direction). +#define MAP_OFFSET 7 +#define MAP_OFFSET_W (MAP_OFFSET * 2 + 1) +#define MAP_OFFSET_H (MAP_OFFSET * 2) + +extern struct BackupMapLayout gBackupMapLayout; + +u32 MapGridGetMetatileIdAt(int x, int y); +u32 MapGridGetMetatileBehaviorAt(int x, int y); +void MapGridSetMetatileIdAt(int x, int y, u16 metatile); +void MapGridSetMetatileEntryAt(int x, int y, u16 metatile); +void GetCameraCoords(u16 * x, u16 * y); +u8 MapGridGetCollisionAt(int x, int y); +int GetMapBorderIdAt(int x, int y); +bool32 CanCameraMoveInDirection(int direction); +u16 GetMetatileAttributesById(u16 metatile); +void GetCameraFocusCoords(u16 *x, u16 *y); +u8 MapGridGetMetatileLayerTypeAt(int x, int y); +u8 MapGridGetElevationAt(int x, int y); +bool8 CameraMove(int x, int y); +void SaveMapView(void); +void SetCameraFocusCoords(u16 x, u16 y); +void InitMap(void); +void InitMapFromSavedGame(void); +void CopyMapTilesetsToVram(struct MapLayout const *mapLayout); +void LoadMapTilesetPalettes(struct MapLayout const *mapLayout); +void LoadSecondaryTilesetPalette(struct MapLayout const *mapLayout); +void CopyPrimaryTilesetToVram(const struct MapLayout *); +void CopySecondaryTilesetToVram(const struct MapLayout *); +const struct MapHeader *const GetMapHeaderFromConnection(const struct MapConnection *connection); +const struct MapConnection *GetMapConnectionAtPos(s16 x, s16 y); #endif diff --git a/include/gba/defines.h b/include/gba/defines.h index 71de1275c..a5e2acdc8 100644 --- a/include/gba/defines.h +++ b/include/gba/defines.h @@ -58,6 +58,12 @@ #define TOTAL_OBJ_TILE_COUNT 1024 +#define PLTT_SIZEOF(n) ((n) * sizeof(u16)) +#define PLTT_SIZE_4BPP PLTT_SIZEOF(16) +#define PLTT_SIZE_8BPP PLTT_SIZEOF(256) + +#define PLTT_OFFSET_4BPP(n) ((n) * PLTT_SIZE_4BPP) + #define RGB(r, g, b) ((r) | ((g) << 5) | ((b) << 10)) #define RGB_BLACK RGB(0, 0, 0) diff --git a/include/global.fieldmap.h b/include/global.fieldmap.h index 11febc6d4..0a8341dad 100644 --- a/include/global.fieldmap.h +++ b/include/global.fieldmap.h @@ -1,31 +1,48 @@ #ifndef GUARD_GLOBAL_FIELDMAP_H #define GUARD_GLOBAL_FIELDMAP_H -#define COLLISION_DIR_SOUTH 0 -#define COLLISION_DIR_NORTH 1 -#define COLLISION_DIR_WEST 2 -#define COLLISION_DIR_EAST 3 -#define COLLISION_DIR_ALL (COLLISION_DIR_NORTH | COLLISION_DIR_SOUTH | COLLISION_DIR_EAST | COLLISION_DIR_WEST) +// Masks/shifts for blocks in the map grid +// Map grid blocks consist of a 10 bit metatile id, a 2 bit collision value, and a 4 bit elevation value +// This is the data stored in each data/layouts/*/map.bin file +#define MAPGRID_METATILE_ID_MASK 0x03FF // Bits 0-9 +#define MAPGRID_COLLISION_MASK 0x0C00 // Bits 10-11 +#define MAPGRID_ELEVATION_MASK 0xF000 // Bits 12-15 +#define MAPGRID_COLLISION_SHIFT 10 +#define MAPGRID_ELEVATION_SHIFT 12 -#define METATILE_COLLISION_MASK 0x0C00 -#define METATILE_ID_MASK 0x03FF -#define METATILE_ID_UNDEFINED 0x03FF -#define METATILE_ELEVATION_SHIFT 0x000C -#define METATILE_COLLISION_SHIFT 0x000A -#define METATILE_ELEVATION_MASK 0xF000 +// An undefined map grid block has all metatile id bits set and nothing else +#define MAPGRID_UNDEFINED MAPGRID_METATILE_ID_MASK + +// Masks/shifts for metatile attributes +// Metatile attributes consist of an 8 bit behavior value, 4 unused bits, and a 4 bit layer type value +// This is the data stored in each data/tilesets/*/*/metatile_attributes.bin file +#define METATILE_ATTR_BEHAVIOR_MASK 0x00FF // Bits 0-7 +#define METATILE_ATTR_LAYER_MASK 0xF000 // Bits 12-15 +#define METATILE_ATTR_LAYER_SHIFT 12 + +enum { + METATILE_LAYER_TYPE_NORMAL, // Metatile uses middle and top bg layers + METATILE_LAYER_TYPE_COVERED, // Metatile uses bottom and middle bg layers + METATILE_LAYER_TYPE_SPLIT, // Metatile uses bottom and top bg layers +}; #define METATILE_ID(tileset, name) (METATILE_##tileset##_##name) +// Rows of metatiles do not actually have a strict width. +// This constant is used for calculations for finding the next row of metatiles +// for constructing large tiles, such as the Battle Pike's curtain tile. +#define METATILE_ROW_WIDTH 8 + typedef void (*TilesetCB)(void); struct Tileset { /*0x00*/ bool8 isCompressed; /*0x01*/ bool8 isSecondary; - /*0x04*/ void *tiles; - /*0x08*/ void *palettes; - /*0x0c*/ void *metatiles; - /*0x10*/ void *metatileAttributes; + /*0x04*/ const u32 *tiles; + /*0x08*/ const u16 (*palettes)[16]; + /*0x0c*/ const u16 *metatiles; + /*0x10*/ const u16 *metatileAttributes; /*0x14*/ TilesetCB callback; }; @@ -33,10 +50,10 @@ struct MapLayout { /*0x00*/ s32 width; /*0x04*/ s32 height; - /*0x08*/ u16 *border; - /*0x0c*/ u16 *map; - /*0x10*/ struct Tileset *primaryTileset; - /*0x14*/ struct Tileset *secondaryTileset; + /*0x08*/ const u16 *border; + /*0x0c*/ const u16 *map; + /*0x10*/ const struct Tileset *primaryTileset; + /*0x14*/ const struct Tileset *secondaryTileset; }; struct BackupMapLayout @@ -50,7 +67,7 @@ struct ObjectEventTemplate { /*0x00*/ u8 localId; /*0x01*/ u8 graphicsId; - /*0x02*/ u8 unk2; + /*0x02*/ u8 kind; // Always OBJ_KIND_NORMAL in Ruby/Sapphire. /*0x04*/ s16 x; /*0x06*/ s16 y; /*0x08*/ u8 elevation; @@ -59,7 +76,7 @@ struct ObjectEventTemplate u8 movementRangeY:4; /*0x0C*/ u16 trainerType; /*0x0E*/ u16 trainerRange_berryTreeId; - /*0x10*/ u8 *script; + /*0x10*/ const u8 *script; /*0x14*/ u16 flagId; }; @@ -78,8 +95,7 @@ struct CoordEvent u8 elevation; u16 trigger; u16 index; - u8 filler_A[0x2]; - u8 *script; + const u8 *script; }; struct BgEvent @@ -88,7 +104,7 @@ struct BgEvent u8 elevation; u8 kind; // The "kind" field determines how to access bgUnion union below. union { - u8 *script; + const u8 *script; struct { u16 item; u16 hiddenItemId; @@ -104,10 +120,10 @@ struct MapEvents u8 coordEventCount; u8 bgEventCount; - struct ObjectEventTemplate *objectEvents; - struct WarpEvent *warps; - struct CoordEvent *coordEvents; - struct BgEvent *bgEvents; + const struct ObjectEventTemplate *objectEvents; + const struct WarpEvent *warps; + const struct CoordEvent *coordEvents; + const struct BgEvent *bgEvents; }; struct MapConnection @@ -121,15 +137,15 @@ struct MapConnection struct MapConnections { s32 count; - struct MapConnection *connections; + const struct MapConnection *connections; }; struct MapHeader { - /* 0x00 */ struct MapLayout *mapLayout; - /* 0x04 */ struct MapEvents *events; - /* 0x08 */ u8 *mapScripts; - /* 0x0C */ struct MapConnections *connections; + /* 0x00 */ const struct MapLayout *mapLayout; + /* 0x04 */ const struct MapEvents *events; + /* 0x08 */ const u8 *mapScripts; + /* 0x0C */ const struct MapConnections *connections; /* 0x10 */ u16 music; /* 0x12 */ u16 mapLayoutId; /* 0x14 */ u8 regionMapSectionId; diff --git a/include/item_use.h b/include/item_use.h index 1ba75a0ee..4b60f8558 100644 --- a/include/item_use.h +++ b/include/item_use.h @@ -13,7 +13,7 @@ void ItemUseOnFieldCB_Itemfinder(u8); void ItemUseOutOfBattle_Berry(u8); void RunItemfinderResults(u8); void ExitItemfinder(u8); -bool8 ItemfinderCheckForHiddenItems(struct MapEvents *, u8); +bool8 ItemfinderCheckForHiddenItems(const struct MapEvents *, u8); void sub_80C9720(u8); void sub_80C9838(u8, s16, s16); u8 GetPlayerDirectionTowardsHiddenItem(s16, s16); diff --git a/include/overworld.h b/include/overworld.h index f444a8405..4aed147bb 100644 --- a/include/overworld.h +++ b/include/overworld.h @@ -69,7 +69,7 @@ void gpu_sync_bg_hide(); // GetMapConnection bool8 SetDiveWarpEmerge(u16 x, u16 y); bool8 SetDiveWarpDive(u16 x, u16 y); -void sub_80538F0(u8 mapGroup, u8 mapNum); +void LoadMapFromCameraTransition(u8 mapGroup, u8 mapNum); // sub_8053994 void ResetInitialPlayerAvatarState(void); void StoreInitialPlayerAvatarState(void); @@ -177,7 +177,7 @@ u16 sub_805559C(void); void sub_80555B0(int linkPlayerId, int a2, struct UnkStruct_8054FF8 *a3); bool32 sub_8055618(struct UnkStruct_8054FF8 *); bool32 sub_8055630(struct UnkStruct_8054FF8 *); -u8 *sub_8055648(struct UnkStruct_8054FF8 *); +const u8 *sub_8055648(struct UnkStruct_8054FF8 *); bool32 sub_8055660(struct UnkStruct_8054FF8 *); const u8 *sub_805568C(struct UnkStruct_8054FF8 *); void sub_80557E8(void); diff --git a/include/palette.h b/include/palette.h index d4e33cce5..b9628728c 100644 --- a/include/palette.h +++ b/include/palette.h @@ -8,6 +8,17 @@ #define FADE_COLOR_WHITE (RGB(31, 31, 31) | 0x8000) +#define PALETTES_BG 0x0000FFFF +#define PALETTES_OBJECTS 0xFFFF0000 +#define PALETTES_ALL (PALETTES_BG | PALETTES_OBJECTS) + +#define PLTT_ID(n) ((n) * 16) +#define BG_PLTT_OFFSET 0x000 +#define OBJ_PLTT_OFFSET 0x100 +#define BG_PLTT_ID(n) (BG_PLTT_OFFSET + PLTT_ID(n)) +#define OBJ_PLTT_ID(n) (OBJ_PLTT_OFFSET + PLTT_ID(n)) +#define OBJ_PLTT_ID2(n) (PLTT_ID((n) + 16)) + enum { FAST_FADE_IN_FROM_WHITE, diff --git a/include/secret_base.h b/include/secret_base.h index 130c78ca3..3c8f6a77b 100644 --- a/include/secret_base.h +++ b/include/secret_base.h @@ -8,11 +8,11 @@ void ResetSecretBases(void); void SetCurrentSecretBaseVar(void); void CheckPlayerHasSecretBase(void); void SetOpenedSecretBaseMetatile(void); -void sub_80BB970(struct MapEvents *events); +void SetOccupiedSecretBaseEntranceMetatiles(const struct MapEvents *events); u8 sub_80BBB24(void); -void sub_80BBCCC(u8 flagIn); -void SetCurrentSecretBaseFromPosition(struct MapPosition *, struct MapEvents *); -void sub_80BC038(struct MapPosition *, struct MapEvents *); +void InitSecretBaseAppearance(u8 flagIn); +void SetCurrentSecretBaseFromPosition(struct MapPosition *, const struct MapEvents *); +void sub_80BC038(struct MapPosition *, const struct MapEvents *); u8 sub_80BC050(); u8 *GetSecretBaseMapName(u8 *dest); void SetPlayerSecretBaseRecordMixingParty(); diff --git a/src/braille_puzzles.c b/src/braille_puzzles.c index d00a86cd5..53c1ae8c7 100644 --- a/src/braille_puzzles.c +++ b/src/braille_puzzles.c @@ -46,9 +46,9 @@ void DoBrailleDigEffect(void) MapGridSetMetatileIdAt(16, 8, METATILE_ID(Cave, SealedChamberEntrance_TopLeft)); MapGridSetMetatileIdAt(17, 8, METATILE_ID(Cave, SealedChamberEntrance_TopMid)); MapGridSetMetatileIdAt(18, 8, METATILE_ID(Cave, SealedChamberEntrance_TopRight)); - MapGridSetMetatileIdAt(16, 9, METATILE_ID(Cave, SealedChamberEntrance_BottomLeft) | (COLLISION_DIR_ALL << METATILE_COLLISION_SHIFT)); + MapGridSetMetatileIdAt(16, 9, METATILE_ID(Cave, SealedChamberEntrance_BottomLeft) | MAPGRID_COLLISION_MASK); MapGridSetMetatileIdAt(17, 9, METATILE_ID(Cave, SealedChamberEntrance_BottomMid)); - MapGridSetMetatileIdAt(18, 9, METATILE_ID(Cave, SealedChamberEntrance_BottomRight) | (COLLISION_DIR_ALL << METATILE_COLLISION_SHIFT)); + MapGridSetMetatileIdAt(18, 9, METATILE_ID(Cave, SealedChamberEntrance_BottomRight) | MAPGRID_COLLISION_MASK); DrawWholeMapView(); PlaySE(SE_BANG); FlagSet(FLAG_SYS_BRAILLE_DIG); @@ -89,9 +89,9 @@ void DoBrailleStrengthEffect(void) MapGridSetMetatileIdAt(14, 26, METATILE_ID(Cave, SealedChamberEntrance_TopLeft)); MapGridSetMetatileIdAt(15, 26, METATILE_ID(Cave, SealedChamberEntrance_TopMid)); MapGridSetMetatileIdAt(16, 26, METATILE_ID(Cave, SealedChamberEntrance_TopRight)); - MapGridSetMetatileIdAt(14, 27, METATILE_ID(Cave, SealedChamberEntrance_BottomLeft) | (COLLISION_DIR_ALL << METATILE_COLLISION_SHIFT)); + MapGridSetMetatileIdAt(14, 27, METATILE_ID(Cave, SealedChamberEntrance_BottomLeft) | MAPGRID_COLLISION_MASK); MapGridSetMetatileIdAt(15, 27, METATILE_ID(Cave, SealedChamberEntrance_BottomMid)); - MapGridSetMetatileIdAt(16, 27, METATILE_ID(Cave, SealedChamberEntrance_BottomRight) | (COLLISION_DIR_ALL << METATILE_COLLISION_SHIFT)); + MapGridSetMetatileIdAt(16, 27, METATILE_ID(Cave, SealedChamberEntrance_BottomRight) | MAPGRID_COLLISION_MASK); DrawWholeMapView(); PlaySE(SE_BANG); FlagSet(FLAG_SYS_BRAILLE_STRENGTH); @@ -135,9 +135,9 @@ void UseFlyAncientTomb_Finish(void) MapGridSetMetatileIdAt(14, 26, METATILE_ID(Cave, SealedChamberEntrance_TopLeft)); MapGridSetMetatileIdAt(15, 26, METATILE_ID(Cave, SealedChamberEntrance_TopMid)); MapGridSetMetatileIdAt(16, 26, METATILE_ID(Cave, SealedChamberEntrance_TopRight)); - MapGridSetMetatileIdAt(14, 27, METATILE_ID(Cave, SealedChamberEntrance_BottomLeft) | (COLLISION_DIR_ALL << METATILE_COLLISION_SHIFT)); + MapGridSetMetatileIdAt(14, 27, METATILE_ID(Cave, SealedChamberEntrance_BottomLeft) | MAPGRID_COLLISION_MASK); MapGridSetMetatileIdAt(15, 27, METATILE_ID(Cave, SealedChamberEntrance_BottomMid)); - MapGridSetMetatileIdAt(16, 27, METATILE_ID(Cave, SealedChamberEntrance_BottomRight) | (COLLISION_DIR_ALL << METATILE_COLLISION_SHIFT)); + MapGridSetMetatileIdAt(16, 27, METATILE_ID(Cave, SealedChamberEntrance_BottomRight) | MAPGRID_COLLISION_MASK); DrawWholeMapView(); PlaySE(SE_BANG); FlagSet(FLAG_SYS_BRAILLE_FLY); diff --git a/src/debug/start_menu_debug.c b/src/debug/start_menu_debug.c index 370fe9145..ec1e6527a 100644 --- a/src/debug/start_menu_debug.c +++ b/src/debug/start_menu_debug.c @@ -842,7 +842,7 @@ void DebugMenu_8077238(void) u16 sp14 = 0; PlayerGetDestCoords(&x, &y); - z = MapGridGetZCoordAt(x, y); + z = MapGridGetElevationAt(x, y); x -= 7; y -= 7; @@ -2222,7 +2222,7 @@ void DebugMenu_80787EC(void) ConvertIntToHexStringN(gStringVar1, gSpecialVar_Result, STR_CONV_MODE_LEADING_ZEROS, 4); gSpecialVar_Result = MapGridGetMetatileIdAt(x, y); ConvertIntToHexStringN(gStringVar2, gSpecialVar_Result, STR_CONV_MODE_LEADING_ZEROS, 4); - gSpecialVar_Result = MapGridGetZCoordAt(x, y); + gSpecialVar_Result = MapGridGetElevationAt(x, y); ConvertIntToHexStringN(gStringVar3, gSpecialVar_Result, STR_CONV_MODE_LEADING_ZEROS, 4); StringExpandPlaceholders(gStringVar4, Str_839C414); } diff --git a/src/debug/watanabe_debug_menu.c b/src/debug/watanabe_debug_menu.c index 752c710d0..aefd85bb2 100644 --- a/src/debug/watanabe_debug_menu.c +++ b/src/debug/watanabe_debug_menu.c @@ -358,7 +358,7 @@ void InitWatanabeDebugMenu(void) { default: case 0: - save_serialize_map(); + SaveMapView(); sub_8125E2C(); gMain.state = 1; break; diff --git a/src/decoration.c b/src/decoration.c index da1ee8530..a957abf39 100644 --- a/src/decoration.c +++ b/src/decoration.c @@ -2089,7 +2089,7 @@ void sub_80FF1EC(u16 mapX, u16 mapY, u8 decWidth, u8 decHeight, u16 decIdx) for (j=0; j> 12) != 0)) collision = 0xc00; // impassable collision else @@ -2426,7 +2426,7 @@ bool8 sub_80FFC24(u8 taskId, const struct Decoration *decoration) { curX = gTasks[taskId].data[0] + j; behaviorAt = MapGridGetMetatileBehaviorAt(curX, curY); - layerType = GetBehaviorByMetatileId(0x200 + decoration->tiles[(mapY - 1 - i) * mapX + j]) & 0xf000; + layerType = GetMetatileAttributesById(0x200 + decoration->tiles[(mapY - 1 - i) * mapX + j]) & 0xf000; if (!sub_80FFBDC(behaviorAt, decoration)) { return FALSE; @@ -2451,7 +2451,7 @@ bool8 sub_80FFC24(u8 taskId, const struct Decoration *decoration) { curX = gTasks[taskId].data[0] + j; behaviorAt = MapGridGetMetatileBehaviorAt(curX, curY); - layerType = GetBehaviorByMetatileId(0x200 + decoration->tiles[(mapY - 1 - i) * mapX + j]) & 0xf000; + layerType = GetMetatileAttributesById(0x200 + decoration->tiles[(mapY - 1 - i) * mapX + j]) & 0xf000; if (!MetatileBehavior_IsNormal(behaviorAt) && !sub_80FFB6C(behaviorAt, layerType)) { return FALSE; @@ -2471,7 +2471,7 @@ bool8 sub_80FFC24(u8 taskId, const struct Decoration *decoration) { curX = gTasks[taskId].data[0] + j; behaviorAt = MapGridGetMetatileBehaviorAt(curX, curY); - layerType = GetBehaviorByMetatileId(0x200 + decoration->tiles[j]) & 0xf000; + layerType = GetMetatileAttributesById(0x200 + decoration->tiles[j]) & 0xf000; if (!MetatileBehavior_IsNormal(behaviorAt) && !MetatileBehavior_IsSecretBaseNorthWall(behaviorAt)) { return FALSE; diff --git a/src/event_object_movement.c b/src/event_object_movement.c index be3c8fceb..f168a191b 100644 --- a/src/event_object_movement.c +++ b/src/event_object_movement.c @@ -59,14 +59,14 @@ static void CameraObject_1(struct Sprite *); static void CameraObject_2(struct Sprite *); static void ObjectCB_CameraObject(struct Sprite *sprite); static bool8 ObjectEventDoesZCoordMatch(struct ObjectEvent *, u8); -static struct ObjectEventTemplate *FindObjectEventTemplateByLocalId(u8, struct ObjectEventTemplate*, u8); +static const struct ObjectEventTemplate *FindObjectEventTemplateByLocalId(u8, const struct ObjectEventTemplate*, u8); static void UpdateObjectEventSpriteSubpriorityAndVisibility(struct Sprite *); static void InitObjectPriorityByZCoord(struct Sprite *sprite, u8 z); static void CreateReflectionEffectSprites(void); static u8 GetObjectEventIdByLocalIdAndMapInternal(u8, u8, u8); static u8 GetObjectEventIdByLocalId(u8); static void RemoveObjectEventInternal(struct ObjectEvent *); -static void MakeObjectTemplateFromObjectEventTemplate(struct ObjectEventTemplate *objEventTemplate, struct SpriteTemplate *spriteTemplate, const struct SubspriteTable **subspriteTables); +static void MakeObjectTemplateFromObjectEventTemplate(const struct ObjectEventTemplate *objEventTemplate, struct SpriteTemplate *spriteTemplate, const struct SubspriteTable **subspriteTables); static void RemoveObjectEventIfOutsideView(struct ObjectEvent *objectEvent); static void SetPlayerAvatarObjectEventIdAndObjectId(u8, u8); static void sub_805B914(struct ObjectEvent *); @@ -1149,7 +1149,7 @@ static u8 GetObjectEventIdByLocalId(u8 localId) return OBJECT_EVENTS_COUNT; } -static u8 InitObjectEventStateFromTemplate(struct ObjectEventTemplate *template, u8 mapNum, u8 mapGroup) +static u8 InitObjectEventStateFromTemplate(const struct ObjectEventTemplate *template, u8 mapNum, u8 mapGroup) { struct ObjectEvent *objectEvent; u8 objectEventId; @@ -1280,7 +1280,7 @@ void RemoveAllObjectEventsExceptPlayer(void) } } -static u8 TrySetupObjectEventSprite(struct ObjectEventTemplate *objectEventTemplate, struct SpriteTemplate *spriteTemplate, u8 mapNum, u8 mapGroup, s16 cameraDeltaX, s16 cameraDeltaY) +static u8 TrySetupObjectEventSprite(const struct ObjectEventTemplate *objectEventTemplate, struct SpriteTemplate *spriteTemplate, u8 mapNum, u8 mapGroup, s16 cameraDeltaX, s16 cameraDeltaY) { u8 spriteId; u8 objectEventId; @@ -1336,7 +1336,7 @@ static u8 TrySetupObjectEventSprite(struct ObjectEventTemplate *objectEventTempl return objectEventId; } -static u8 TrySpawnObjectEvent(struct ObjectEventTemplate *objectEventTemplate, u8 mapNum, u8 mapGroup, s16 cameraDeltaX, s16 cameraDeltaY) +static u8 TrySpawnObjectEvent(const struct ObjectEventTemplate *objectEventTemplate, u8 mapNum, u8 mapGroup, s16 cameraDeltaX, s16 cameraDeltaY) { u8 objectEventId; struct SpriteTemplate spriteTemplate; @@ -1378,7 +1378,7 @@ u8 SpawnSpecialObjectEventParametrized(u8 graphicsId, u8 movementType, u8 localI y -= 7; objectEventTemplate.localId = localId; objectEventTemplate.graphicsId = graphicsId; - objectEventTemplate.unk2 = 0; + objectEventTemplate.kind = OBJ_KIND_NORMAL; objectEventTemplate.x = x; objectEventTemplate.y = y; objectEventTemplate.elevation = elevation; @@ -1392,7 +1392,7 @@ u8 SpawnSpecialObjectEventParametrized(u8 graphicsId, u8 movementType, u8 localI u8 show_sprite(u8 localId, u8 mapNum, u8 mapGroup) { - struct ObjectEventTemplate *objectEventTemplate; + const struct ObjectEventTemplate *objectEventTemplate; s16 x; s16 y; @@ -1424,7 +1424,7 @@ static void MakeObjectTemplateFromObjectEventGraphicsInfoWithCallbackIndex(u16 g MakeObjectTemplateFromObjectEventGraphicsInfo(graphicsId, sMovementTypeCallbacks[movementType], spriteTemplate, subspriteTables); } -static void MakeObjectTemplateFromObjectEventTemplate(struct ObjectEventTemplate *objEventTemplate, struct SpriteTemplate *spriteTemplate, const struct SubspriteTable **subspriteTables) +static void MakeObjectTemplateFromObjectEventTemplate(const struct ObjectEventTemplate *objEventTemplate, struct SpriteTemplate *spriteTemplate, const struct SubspriteTable **subspriteTables) { MakeObjectTemplateFromObjectEventGraphicsInfoWithCallbackIndex(objEventTemplate->graphicsId, objEventTemplate->movementType, spriteTemplate, subspriteTables); } @@ -2256,7 +2256,7 @@ u8 ObjectEventGetBerryTreeId(u8 objectEventId) return gObjectEvents[objectEventId].trainerRange_berryTreeId; } -struct ObjectEventTemplate *GetObjectEventTemplateByLocalIdAndMap(u8 localId, u8 mapNum, u8 mapGroup) +const struct ObjectEventTemplate *GetObjectEventTemplateByLocalIdAndMap(u8 localId, u8 mapNum, u8 mapGroup) { if (gSaveBlock1.location.mapNum == mapNum && gSaveBlock1.location.mapGroup == mapGroup) return FindObjectEventTemplateByLocalId(localId, gSaveBlock1.objectEventTemplates, gMapHeader.events->objectEventCount); @@ -2268,7 +2268,7 @@ struct ObjectEventTemplate *GetObjectEventTemplateByLocalIdAndMap(u8 localId, u8 } } -static struct ObjectEventTemplate *FindObjectEventTemplateByLocalId(u8 localId, struct ObjectEventTemplate *templates, u8 count) +static const struct ObjectEventTemplate *FindObjectEventTemplateByLocalId(u8 localId, const struct ObjectEventTemplate *templates, u8 count) { u8 i; @@ -4466,7 +4466,7 @@ u8 GetCollisionAtCoords(struct ObjectEvent *objectEvent, s16 x, s16 y, u32 dirn) direction = dirn; if (IsCoordOutsideObjectEventMovementRange(objectEvent, x, y)) return 1; - else if (MapGridIsImpassableAt(x, y) || GetMapBorderIdAt(x, y) == -1 || IsMetatileDirectionallyImpassable(objectEvent, x, y, direction)) + else if (MapGridGetCollisionAt(x, y) || GetMapBorderIdAt(x, y) == -1 || IsMetatileDirectionallyImpassable(objectEvent, x, y, direction)) return 2; else if (objectEvent->trackedByCamera && !CanCameraMoveInDirection(direction)) return 2; @@ -4483,7 +4483,7 @@ u8 GetCollisionFlagsAtCoords(struct ObjectEvent *objectEvent, s16 x, s16 y, u8 d if (IsCoordOutsideObjectEventMovementRange(objectEvent, x, y)) flags |= 1; - if (MapGridIsImpassableAt(x, y) || GetMapBorderIdAt(x, y) == -1 || IsMetatileDirectionallyImpassable(objectEvent, x, y, direction) || (objectEvent->trackedByCamera && !CanCameraMoveInDirection(direction))) + if (MapGridGetCollisionAt(x, y) || GetMapBorderIdAt(x, y) == -1 || IsMetatileDirectionallyImpassable(objectEvent, x, y, direction) || (objectEvent->trackedByCamera && !CanCameraMoveInDirection(direction))) flags |= 2; if (IsZCoordMismatchAt(objectEvent->currentElevation, x, y)) flags |= 4; @@ -7532,7 +7532,7 @@ bool8 IsZCoordMismatchAt(u8 z, s16 x, s16 y) if (z == 0) return FALSE; - mapZ = MapGridGetZCoordAt(x, y); + mapZ = MapGridGetElevationAt(x, y); if (mapZ == 0 || mapZ == 0xF) return FALSE; @@ -7585,8 +7585,8 @@ u8 ZCoordToPriority(u8 z) void ObjectEventUpdateZCoord(struct ObjectEvent *objEvent) { - u8 z = MapGridGetZCoordAt(objEvent->currentCoords.x, objEvent->currentCoords.y); - u8 z2 = MapGridGetZCoordAt(objEvent->previousCoords.x, objEvent->previousCoords.y); + u8 z = MapGridGetElevationAt(objEvent->currentCoords.x, objEvent->currentCoords.y); + u8 z2 = MapGridGetElevationAt(objEvent->previousCoords.x, objEvent->previousCoords.y); if (z == 0xF || z2 == 0xF) return; diff --git a/src/field_camera.c b/src/field_camera.c index 9c93a51f6..08abbd0e1 100644 --- a/src/field_camera.c +++ b/src/field_camera.c @@ -28,15 +28,15 @@ struct FieldCamera gFieldCamera; u16 gTotalCameraPixelOffsetY; u16 gTotalCameraPixelOffsetX; -static void RedrawMapSliceNorth(struct FieldCameraOffset*, struct MapLayout*); -static void RedrawMapSliceSouth(struct FieldCameraOffset*, struct MapLayout*); -static void RedrawMapSliceEast(struct FieldCameraOffset*, struct MapLayout*); -static void RedrawMapSliceWest(struct FieldCameraOffset*, struct MapLayout*); +static void RedrawMapSliceNorth(struct FieldCameraOffset*, const struct MapLayout*); +static void RedrawMapSliceSouth(struct FieldCameraOffset*, const struct MapLayout*); +static void RedrawMapSliceEast(struct FieldCameraOffset*, const struct MapLayout*); +static void RedrawMapSliceWest(struct FieldCameraOffset*, const struct MapLayout*); static s32 MapPosToBgTilemapOffset(struct FieldCameraOffset*, s32, s32); -static void DrawWholeMapViewInternal(int x, int y, struct MapLayout*); -static void DrawMetatileAt(struct MapLayout*, u16, int, int); -static void DrawMetatile(s32, u16*, u16); +static void DrawWholeMapViewInternal(int x, int y, const struct MapLayout*); +static void DrawMetatileAt(const struct MapLayout*, u16, int, int); +static void DrawMetatile(s32, const u16*, u16); static void CameraPanningCB_PanAhead(void); static void move_tilemap_camera_to_upper_left_corner_(struct FieldCameraOffset *cameraOffset) @@ -100,7 +100,7 @@ void DrawWholeMapView(void) sFieldCameraOffset.copyBGToVRAM = TRUE; } -static void DrawWholeMapViewInternal(int x, int y, struct MapLayout *mapLayout) +static void DrawWholeMapViewInternal(int x, int y, const struct MapLayout *mapLayout) { u8 i; u8 j; @@ -125,7 +125,7 @@ static void DrawWholeMapViewInternal(int x, int y, struct MapLayout *mapLayout) static void RedrawMapSlicesForCameraUpdate(struct FieldCameraOffset *cameraOffset, int x, int y) { - struct MapLayout *mapLayout = gMapHeader.mapLayout; + const struct MapLayout *mapLayout = gMapHeader.mapLayout; if (x > 0) RedrawMapSliceWest(cameraOffset, mapLayout); @@ -138,7 +138,7 @@ static void RedrawMapSlicesForCameraUpdate(struct FieldCameraOffset *cameraOffse cameraOffset->copyBGToVRAM = TRUE; } -static void RedrawMapSliceNorth(struct FieldCameraOffset *cameraOffset, struct MapLayout *mapLayout) +static void RedrawMapSliceNorth(struct FieldCameraOffset *cameraOffset, const struct MapLayout *mapLayout) { u8 i; u8 temp; @@ -157,7 +157,7 @@ static void RedrawMapSliceNorth(struct FieldCameraOffset *cameraOffset, struct M } } -static void RedrawMapSliceSouth(struct FieldCameraOffset *cameraOffset, struct MapLayout *mapLayout) +static void RedrawMapSliceSouth(struct FieldCameraOffset *cameraOffset, const struct MapLayout *mapLayout) { u8 i; u8 temp; @@ -172,7 +172,7 @@ static void RedrawMapSliceSouth(struct FieldCameraOffset *cameraOffset, struct M } } -static void RedrawMapSliceEast(struct FieldCameraOffset *cameraOffset, struct MapLayout *mapLayout) +static void RedrawMapSliceEast(struct FieldCameraOffset *cameraOffset, const struct MapLayout *mapLayout) { u8 i; u8 temp; @@ -187,7 +187,7 @@ static void RedrawMapSliceEast(struct FieldCameraOffset *cameraOffset, struct Ma } } -static void RedrawMapSliceWest(struct FieldCameraOffset *cameraOffset, struct MapLayout *mapLayout) +static void RedrawMapSliceWest(struct FieldCameraOffset *cameraOffset, const struct MapLayout *mapLayout) { u8 i; u8 temp; @@ -226,9 +226,9 @@ void DrawDoorMetatileAt(int x, int y, u16 *arr) } } -static void DrawMetatileAt(struct MapLayout *mapLayout, u16 offset, int x, int y) +static void DrawMetatileAt(const struct MapLayout *mapLayout, u16 offset, int x, int y) { - u16 *metatiles; + const u16 *metatiles; u16 metatileId = MapGridGetMetatileIdAt(x, y); if (metatileId > NUM_METATILES_TOTAL) @@ -247,7 +247,7 @@ static void DrawMetatileAt(struct MapLayout *mapLayout, u16 offset, int x, int y DrawMetatile(MapGridGetMetatileLayerTypeAt(x, y), metatiles + metatileId * 8, offset); } -static void DrawMetatile(s32 metatileLayerType, u16 *metatiles, u16 offset) +static void DrawMetatile(s32 metatileLayerType, const u16 *metatiles, u16 offset) { switch (metatileLayerType) { diff --git a/src/field_control_avatar.c b/src/field_control_avatar.c index 77b201d06..622c6f1be 100644 --- a/src/field_control_avatar.c +++ b/src/field_control_avatar.c @@ -88,9 +88,9 @@ static u16 GetPlayerCurMetatileBehavior(int); static bool8 TryStartInteractionScript(struct MapPosition*, u16, u8); static const u8 *GetInteractionScript(struct MapPosition*, u8, u8); static const u8 *GetInteractedObjectEventScript(struct MapPosition *, u8, u8); -static u8 *GetInteractedBackgroundEventScript(struct MapPosition *, u8, u8); -static u8 *GetInteractedMetatileScript(struct MapPosition *, u8, u8); -static u8 *GetInteractedWaterScript(struct MapPosition *, u8, u8); +static const u8 *GetInteractedBackgroundEventScript(struct MapPosition *, u8, u8); +static const u8 *GetInteractedMetatileScript(struct MapPosition *, u8, u8); +static const u8 *GetInteractedWaterScript(struct MapPosition *, u8, u8); static bool32 TrySetupDiveDownScript(void); static bool32 TrySetupDiveEmergeScript(void); static bool8 TryStartStepBasedScript(struct MapPosition *, u16, u16); @@ -104,8 +104,8 @@ static s8 GetWarpEventAtMapPosition(struct MapHeader *, struct MapPosition *); static void sub_8068C30(struct MapHeader *, s8, struct MapPosition *); static bool8 map_warp_consider_2_to_inside(struct MapPosition *, u16, u8); static s8 GetWarpEventAtPosition(struct MapHeader *, u16, u16, u8); -static u8 *GetCoordEventScriptAtPosition(struct MapHeader *, u16, u16, u8); -static struct BgEvent *GetBackgroundEventAtPosition(struct MapHeader *, u16, u16, u8); +static const u8 *GetCoordEventScriptAtPosition(struct MapHeader *, u16, u16, u8); +static const struct BgEvent *GetBackgroundEventAtPosition(struct MapHeader *, u16, u16, u8); static bool8 TryStartCoordEventScript(struct MapPosition *); static bool8 TryStartWarpEventScript(struct MapPosition *, u16); static bool8 TryStartCrackedFloorHoleScript(u16); @@ -295,7 +295,7 @@ static void GetInFrontOfPlayerPosition(struct MapPosition *position) GetXYCoordsOneStepInFrontOfPlayer(&position->x, &position->y); PlayerGetDestCoords(&x, &y); - if (MapGridGetZCoordAt(x, y) != 0) + if (MapGridGetElevationAt(x, y) != 0) position->height = PlayerGetZCoord(); else position->height = 0; @@ -398,9 +398,9 @@ static const u8 *GetInteractedObjectEventScript(struct MapPosition *position, u8 return script; } -static u8 *GetInteractedBackgroundEventScript(struct MapPosition *position, u8 metatileBehavior, u8 direction) +static const u8 *GetInteractedBackgroundEventScript(struct MapPosition *position, u8 metatileBehavior, u8 direction) { - struct BgEvent *bgEvent = GetBackgroundEventAtPosition(&gMapHeader, position->x - 7, position->y - 7, position->height); + const struct BgEvent *bgEvent = GetBackgroundEventAtPosition(&gMapHeader, position->x - 7, position->y - 7, position->height); if (bgEvent == NULL) return NULL; @@ -449,7 +449,7 @@ static u8 *GetInteractedBackgroundEventScript(struct MapPosition *position, u8 m return bgEvent->bgUnion.script; } -static u8 *GetInteractedMetatileScript(struct MapPosition *position, u8 metatileBehavior, u8 direction) +static const u8 *GetInteractedMetatileScript(struct MapPosition *position, u8 metatileBehavior, u8 direction) { s8 height; @@ -485,7 +485,7 @@ static u8 *GetInteractedMetatileScript(struct MapPosition *position, u8 metatile return EventScript_Blueprint; height = position->height; - if (height == MapGridGetZCoordAt(position->x, position->y)) + if (height == MapGridGetElevationAt(position->x, position->y)) { if (MetatileBehavior_IsSecretBasePC(metatileBehavior) == TRUE) return SecretBase_EventScript_PC; @@ -500,7 +500,7 @@ static u8 *GetInteractedMetatileScript(struct MapPosition *position, u8 metatile return NULL; } -static u8 *GetInteractedWaterScript(struct MapPosition *unused1, u8 metatileBehavior, u8 direction) +static const u8 *GetInteractedWaterScript(struct MapPosition *unused1, u8 metatileBehavior, u8 direction) { if (FlagGet(FLAG_BADGE05_GET) == TRUE && PartyHasMonWithSurf() == TRUE && IsPlayerFacingSurfableFishableWater() == TRUE) return EventScript_UseSurf; @@ -552,7 +552,7 @@ static bool8 TryStartStepBasedScript(struct MapPosition *position, u16 metatileB bool8 TryStartCoordEventScript(struct MapPosition *position) { - u8 *script = GetCoordEventScriptAtPosition(&gMapHeader, position->x - 7, position->y - 7, position->height); + const u8 *script = GetCoordEventScriptAtPosition(&gMapHeader, position->x - 7, position->y - 7, position->height); if (script == NULL) return FALSE; @@ -764,7 +764,7 @@ static s8 GetWarpEventAtMapPosition(struct MapHeader *mapHeader, struct MapPosit static void sub_8068C30(struct MapHeader *unused, s8 warpEventId, struct MapPosition *position) { - struct WarpEvent *warpEvent = &gMapHeader.events->warps[warpEventId]; + const struct WarpEvent *warpEvent = &gMapHeader.events->warps[warpEventId]; if (warpEvent->mapNum == MAP_NUM(DYNAMIC)) { @@ -811,7 +811,7 @@ static bool8 map_warp_consider_2_to_inside(struct MapPosition *position, u16 met static s8 GetWarpEventAtPosition(struct MapHeader *mapHeader, u16 x, u16 y, u8 elevation) { s32 i; - struct WarpEvent *warpEvent = mapHeader->events->warps; + const struct WarpEvent *warpEvent = mapHeader->events->warps; u8 warpCount = mapHeader->events->warpCount; for (i = 0; i < warpCount; i++, warpEvent++) @@ -825,7 +825,7 @@ static s8 GetWarpEventAtPosition(struct MapHeader *mapHeader, u16 x, u16 y, u8 e return -1; } -static u8 *TryRunCoordEventScript(struct CoordEvent *coordEvent) +static const u8 *TryRunCoordEventScript(const struct CoordEvent *coordEvent) { if (coordEvent != NULL) { @@ -845,10 +845,10 @@ static u8 *TryRunCoordEventScript(struct CoordEvent *coordEvent) return NULL; } -static u8 *GetCoordEventScriptAtPosition(struct MapHeader *mapHeader, u16 x, u16 y, u8 elevation) +static const u8 *GetCoordEventScriptAtPosition(struct MapHeader *mapHeader, u16 x, u16 y, u8 elevation) { s32 i; - struct CoordEvent *coordEvents = mapHeader->events->coordEvents; + const struct CoordEvent *coordEvents = mapHeader->events->coordEvents; u8 coordEventCount = mapHeader->events->coordEventCount; for (i = 0; i < coordEventCount; i++) @@ -857,7 +857,7 @@ static u8 *GetCoordEventScriptAtPosition(struct MapHeader *mapHeader, u16 x, u16 { if (coordEvents[i].elevation == elevation || coordEvents[i].elevation == 0) { - u8 *script = TryRunCoordEventScript(&coordEvents[i]); + const u8 *script = TryRunCoordEventScript(&coordEvents[i]); if (script != NULL) return script; } @@ -866,15 +866,15 @@ static u8 *GetCoordEventScriptAtPosition(struct MapHeader *mapHeader, u16 x, u16 return NULL; } -u8 *GetCoordEventScriptAtMapPosition(struct MapPosition *position) +const u8 *GetCoordEventScriptAtMapPosition(struct MapPosition *position) { return GetCoordEventScriptAtPosition(&gMapHeader, position->x - 7, position->y - 7, position->height); } -static struct BgEvent *GetBackgroundEventAtPosition(struct MapHeader *mapHeader, u16 x, u16 y, u8 elevation) +static const struct BgEvent *GetBackgroundEventAtPosition(struct MapHeader *mapHeader, u16 x, u16 y, u8 elevation) { u8 i; - struct BgEvent *bgEvents = mapHeader->events->bgEvents; + const struct BgEvent *bgEvents = mapHeader->events->bgEvents; u8 bgEventCount = mapHeader->events->bgEventCount; for (i = 0; i < bgEventCount; i++) diff --git a/src/field_effect_helpers.c b/src/field_effect_helpers.c index 4f3680a2f..ac43e8094 100644 --- a/src/field_effect_helpers.c +++ b/src/field_effect_helpers.c @@ -1024,7 +1024,7 @@ static void sub_812800C(struct ObjectEvent *objectEvent, struct Sprite *sprite) for (i = DIR_SOUTH; i <= DIR_EAST; i++, x = sprite->data[6], y = sprite->data[7]) { MoveCoords(i, &x, &y); - if (MapGridGetZCoordAt(x, y) == 3) + if (MapGridGetElevationAt(x, y) == 3) { sprite->data[5] ++; break; diff --git a/src/field_player_avatar.c b/src/field_player_avatar.c index d30717d65..97d38aa85 100644 --- a/src/field_player_avatar.c +++ b/src/field_player_avatar.c @@ -616,7 +616,7 @@ u8 CheckForObjectEventCollision(struct ObjectEvent *a, s16 x, s16 y, u8 directio static bool8 sub_8058EF0(s16 x, s16 y, u8 direction) { if ((gPlayerAvatar.flags & PLAYER_AVATAR_FLAG_SURFING) - && MapGridGetZCoordAt(x, y) == 3 + && MapGridGetElevationAt(x, y) == 3 && GetObjectEventIdByXYZ(x, y, 3) == 16) { sub_805A20C(direction); diff --git a/src/field_specials.c b/src/field_specials.c index 502542cc9..b607c445e 100644 --- a/src/field_specials.c +++ b/src/field_specials.c @@ -430,10 +430,10 @@ void MauvilleGymSpecial2(void) MapGridSetMetatileIdAt(x, y, METATILE_ID(MauvilleGym, GreenBeamH2_On)); break; case METATILE_ID(MauvilleGym, GreenBeamH3_Off): - MapGridSetMetatileIdAt(x, y, METATILE_ID(MauvilleGym, GreenBeamH3_On) | (COLLISION_DIR_ALL << METATILE_COLLISION_SHIFT)); + MapGridSetMetatileIdAt(x, y, METATILE_ID(MauvilleGym, GreenBeamH3_On) | MAPGRID_COLLISION_MASK); break; case METATILE_ID(MauvilleGym, GreenBeamH4_Off): - MapGridSetMetatileIdAt(x, y, METATILE_ID(MauvilleGym, GreenBeamH4_On) | (COLLISION_DIR_ALL << METATILE_COLLISION_SHIFT)); + MapGridSetMetatileIdAt(x, y, METATILE_ID(MauvilleGym, GreenBeamH4_On) | MAPGRID_COLLISION_MASK); break; case METATILE_ID(MauvilleGym, RedBeamH1_On): MapGridSetMetatileIdAt(x, y, METATILE_ID(MauvilleGym, RedBeamH1_Off)); @@ -454,41 +454,41 @@ void MauvilleGymSpecial2(void) MapGridSetMetatileIdAt(x, y, METATILE_ID(MauvilleGym, RedBeamH2_On)); break; case METATILE_ID(MauvilleGym, RedBeamH3_Off): - MapGridSetMetatileIdAt(x, y, METATILE_ID(MauvilleGym, RedBeamH3_On) | (COLLISION_DIR_ALL << METATILE_COLLISION_SHIFT)); + MapGridSetMetatileIdAt(x, y, METATILE_ID(MauvilleGym, RedBeamH3_On) | MAPGRID_COLLISION_MASK); break; case METATILE_ID(MauvilleGym, RedBeamH4_Off): - MapGridSetMetatileIdAt(x, y, METATILE_ID(MauvilleGym, RedBeamH4_On) | (COLLISION_DIR_ALL << METATILE_COLLISION_SHIFT)); + MapGridSetMetatileIdAt(x, y, METATILE_ID(MauvilleGym, RedBeamH4_On) | MAPGRID_COLLISION_MASK); break; case METATILE_ID(MauvilleGym, GreenBeamV1_On): - MapGridSetMetatileIdAt(x, y, METATILE_ID(MauvilleGym, PoleBottom_On) | (COLLISION_DIR_ALL << METATILE_COLLISION_SHIFT)); + MapGridSetMetatileIdAt(x, y, METATILE_ID(MauvilleGym, PoleBottom_On) | MAPGRID_COLLISION_MASK); break; case METATILE_ID(MauvilleGym, GreenBeamV2_On): MapGridSetMetatileIdAt(x, y, METATILE_ID(MauvilleGym, FloorTile)); break; case METATILE_ID(MauvilleGym, RedBeamV1_On): - MapGridSetMetatileIdAt(x, y, METATILE_ID(MauvilleGym, PoleBottom_Off) | (COLLISION_DIR_ALL << METATILE_COLLISION_SHIFT)); + MapGridSetMetatileIdAt(x, y, METATILE_ID(MauvilleGym, PoleBottom_Off) | MAPGRID_COLLISION_MASK); break; case METATILE_ID(MauvilleGym, RedBeamV2_On): MapGridSetMetatileIdAt(x, y, METATILE_ID(MauvilleGym, FloorTile)); break; case METATILE_ID(MauvilleGym, PoleBottom_On): - MapGridSetMetatileIdAt(x, y, METATILE_ID(MauvilleGym, GreenBeamV1_On) | (COLLISION_DIR_ALL << METATILE_COLLISION_SHIFT)); + MapGridSetMetatileIdAt(x, y, METATILE_ID(MauvilleGym, GreenBeamV1_On) | MAPGRID_COLLISION_MASK); break; case METATILE_ID(MauvilleGym, FloorTile): if (MapGridGetMetatileIdAt(x, y - 1) == METATILE_ID(MauvilleGym, GreenBeamV1_On)) { - MapGridSetMetatileIdAt(x, y, METATILE_ID(MauvilleGym, GreenBeamV2_On) | (COLLISION_DIR_ALL << METATILE_COLLISION_SHIFT)); + MapGridSetMetatileIdAt(x, y, METATILE_ID(MauvilleGym, GreenBeamV2_On) | MAPGRID_COLLISION_MASK); } else { - MapGridSetMetatileIdAt(x, y, METATILE_ID(MauvilleGym, RedBeamV2_On) | (COLLISION_DIR_ALL << METATILE_COLLISION_SHIFT)); + MapGridSetMetatileIdAt(x, y, METATILE_ID(MauvilleGym, RedBeamV2_On) | MAPGRID_COLLISION_MASK); } break; case METATILE_ID(MauvilleGym, PoleBottom_Off): - MapGridSetMetatileIdAt(x, y, METATILE_ID(MauvilleGym, RedBeamV1_On) | (COLLISION_DIR_ALL << METATILE_COLLISION_SHIFT)); + MapGridSetMetatileIdAt(x, y, METATILE_ID(MauvilleGym, RedBeamV1_On) | MAPGRID_COLLISION_MASK); break; case METATILE_ID(MauvilleGym, PoleTop_Off): - MapGridSetMetatileIdAt(x, y, METATILE_ID(MauvilleGym, PoleTop_On) | (COLLISION_DIR_ALL << METATILE_COLLISION_SHIFT)); + MapGridSetMetatileIdAt(x, y, METATILE_ID(MauvilleGym, PoleTop_On) | MAPGRID_COLLISION_MASK); break; case METATILE_ID(MauvilleGym, PoleTop_On): MapGridSetMetatileIdAt(x, y, METATILE_ID(MauvilleGym, PoleTop_Off)); @@ -538,10 +538,10 @@ void MauvilleGymSpecial3(void) MapGridSetMetatileIdAt(x, y, METATILE_ID(MauvilleGym, RedBeamH4_Off)); break; case METATILE_ID(MauvilleGym, GreenBeamV1_On): - MapGridSetMetatileIdAt(x, y, METATILE_ID(MauvilleGym, PoleBottom_On) | (COLLISION_DIR_ALL << METATILE_COLLISION_SHIFT)); + MapGridSetMetatileIdAt(x, y, METATILE_ID(MauvilleGym, PoleBottom_On) | MAPGRID_COLLISION_MASK); break; case METATILE_ID(MauvilleGym, RedBeamV1_On): - MapGridSetMetatileIdAt(x, y, METATILE_ID(MauvilleGym, PoleBottom_Off) | (COLLISION_DIR_ALL << METATILE_COLLISION_SHIFT)); + MapGridSetMetatileIdAt(x, y, METATILE_ID(MauvilleGym, PoleBottom_Off) | MAPGRID_COLLISION_MASK); break; case METATILE_ID(MauvilleGym, GreenBeamV2_On): case METATILE_ID(MauvilleGym, RedBeamV2_On): @@ -913,7 +913,7 @@ static void PCTurnOffEffect(void) { tileId = METATILE_ID(BrendansMaysHouse, MayPC_Off); } - MapGridSetMetatileIdAt(gSaveBlock1.pos.x + dx + 7, gSaveBlock1.pos.y + dy + 7, tileId | (COLLISION_DIR_ALL << METATILE_COLLISION_SHIFT)); + MapGridSetMetatileIdAt(gSaveBlock1.pos.x + dx + 7, gSaveBlock1.pos.y + dy + 7, tileId | MAPGRID_COLLISION_MASK); DrawWholeMapView(); } @@ -949,13 +949,13 @@ static void LotteryCornerComputerEffect(struct Task *task) task->data[3] = 0; if (task->data[4] != 0) { - MapGridSetMetatileIdAt(18, 8, METATILE_ID(Shop, Laptop1_Normal) | (COLLISION_DIR_ALL << METATILE_COLLISION_SHIFT)); - MapGridSetMetatileIdAt(18, 9, METATILE_ID(Shop, Laptop2_Normal) | (COLLISION_DIR_ALL << METATILE_COLLISION_SHIFT)); + MapGridSetMetatileIdAt(18, 8, METATILE_ID(Shop, Laptop1_Normal) | MAPGRID_COLLISION_MASK); + MapGridSetMetatileIdAt(18, 9, METATILE_ID(Shop, Laptop2_Normal) | MAPGRID_COLLISION_MASK); } else { - MapGridSetMetatileIdAt(18, 8, METATILE_ID(Shop, Laptop1_Flash) | (COLLISION_DIR_ALL << METATILE_COLLISION_SHIFT)); - MapGridSetMetatileIdAt(18, 9, METATILE_ID(Shop, Laptop2_Flash) | (COLLISION_DIR_ALL << METATILE_COLLISION_SHIFT)); + MapGridSetMetatileIdAt(18, 8, METATILE_ID(Shop, Laptop1_Flash) | MAPGRID_COLLISION_MASK); + MapGridSetMetatileIdAt(18, 9, METATILE_ID(Shop, Laptop2_Flash) | MAPGRID_COLLISION_MASK); } DrawWholeMapView(); task->data[4] ^= 1; @@ -969,8 +969,8 @@ static void LotteryCornerComputerEffect(struct Task *task) void EndLotteryCornerComputerEffect(void) { - MapGridSetMetatileIdAt(18, 8, METATILE_ID(Shop, Laptop1_Normal) | (COLLISION_DIR_ALL << METATILE_COLLISION_SHIFT)); - MapGridSetMetatileIdAt(18, 9, METATILE_ID(Shop, Laptop2_Normal) | (COLLISION_DIR_ALL << METATILE_COLLISION_SHIFT)); + MapGridSetMetatileIdAt(18, 8, METATILE_ID(Shop, Laptop1_Normal) | MAPGRID_COLLISION_MASK); + MapGridSetMetatileIdAt(18, 9, METATILE_ID(Shop, Laptop2_Normal) | MAPGRID_COLLISION_MASK); DrawWholeMapView(); } @@ -1302,33 +1302,33 @@ void sub_810ED60(struct Task *task) task->data[3] = 0; if (task->data[4] != 0) { - MapGridSetMetatileIdAt( 7, 7, METATILE_BattleTower_Elevator_Top0 | (COLLISION_DIR_ALL << METATILE_COLLISION_SHIFT)); - MapGridSetMetatileIdAt( 8, 7, METATILE_BattleTower_Elevator_Top1 | (COLLISION_DIR_ALL << METATILE_COLLISION_SHIFT)); - MapGridSetMetatileIdAt( 9, 7, METATILE_BattleTower_Elevator_Top1 | (COLLISION_DIR_ALL << METATILE_COLLISION_SHIFT)); - MapGridSetMetatileIdAt(10, 7, METATILE_BattleTower_Elevator_Top2 | (COLLISION_DIR_ALL << METATILE_COLLISION_SHIFT)); - MapGridSetMetatileIdAt( 7, 8, METATILE_BattleTower_Elevator_Mid0 | (COLLISION_DIR_ALL << METATILE_COLLISION_SHIFT)); - MapGridSetMetatileIdAt( 8, 8, METATILE_BattleTower_Elevator_Mid1 | (COLLISION_DIR_ALL << METATILE_COLLISION_SHIFT)); - MapGridSetMetatileIdAt( 9, 8, METATILE_BattleTower_Elevator_Mid1 | (COLLISION_DIR_ALL << METATILE_COLLISION_SHIFT)); - MapGridSetMetatileIdAt(10, 8, METATILE_BattleTower_Elevator_Mid2 | (COLLISION_DIR_ALL << METATILE_COLLISION_SHIFT)); - MapGridSetMetatileIdAt( 7, 9, METATILE_BattleTower_Elevator_Bottom0 | (COLLISION_DIR_ALL << METATILE_COLLISION_SHIFT)); - MapGridSetMetatileIdAt( 8, 9, METATILE_BattleTower_Elevator_Bottom1 | (COLLISION_DIR_ALL << METATILE_COLLISION_SHIFT)); - MapGridSetMetatileIdAt( 9, 9, METATILE_BattleTower_Elevator_Bottom1 | (COLLISION_DIR_ALL << METATILE_COLLISION_SHIFT)); - MapGridSetMetatileIdAt(10, 9, METATILE_BattleTower_Elevator_Bottom2 | (COLLISION_DIR_ALL << METATILE_COLLISION_SHIFT)); + MapGridSetMetatileIdAt( 7, 7, METATILE_BattleTower_Elevator_Top0 | MAPGRID_COLLISION_MASK); + MapGridSetMetatileIdAt( 8, 7, METATILE_BattleTower_Elevator_Top1 | MAPGRID_COLLISION_MASK); + MapGridSetMetatileIdAt( 9, 7, METATILE_BattleTower_Elevator_Top1 | MAPGRID_COLLISION_MASK); + MapGridSetMetatileIdAt(10, 7, METATILE_BattleTower_Elevator_Top2 | MAPGRID_COLLISION_MASK); + MapGridSetMetatileIdAt( 7, 8, METATILE_BattleTower_Elevator_Mid0 | MAPGRID_COLLISION_MASK); + MapGridSetMetatileIdAt( 8, 8, METATILE_BattleTower_Elevator_Mid1 | MAPGRID_COLLISION_MASK); + MapGridSetMetatileIdAt( 9, 8, METATILE_BattleTower_Elevator_Mid1 | MAPGRID_COLLISION_MASK); + MapGridSetMetatileIdAt(10, 8, METATILE_BattleTower_Elevator_Mid2 | MAPGRID_COLLISION_MASK); + MapGridSetMetatileIdAt( 7, 9, METATILE_BattleTower_Elevator_Bottom0 | MAPGRID_COLLISION_MASK); + MapGridSetMetatileIdAt( 8, 9, METATILE_BattleTower_Elevator_Bottom1 | MAPGRID_COLLISION_MASK); + MapGridSetMetatileIdAt( 9, 9, METATILE_BattleTower_Elevator_Bottom1 | MAPGRID_COLLISION_MASK); + MapGridSetMetatileIdAt(10, 9, METATILE_BattleTower_Elevator_Bottom2 | MAPGRID_COLLISION_MASK); } else { - MapGridSetMetatileIdAt( 7, 7, (METATILE_BattleTower_Elevator_Top0 + 3) | (COLLISION_DIR_ALL << METATILE_COLLISION_SHIFT)); - MapGridSetMetatileIdAt( 8, 7, (METATILE_BattleTower_Elevator_Top1 + 3) | (COLLISION_DIR_ALL << METATILE_COLLISION_SHIFT)); - MapGridSetMetatileIdAt( 9, 7, (METATILE_BattleTower_Elevator_Top1 + 3) | (COLLISION_DIR_ALL << METATILE_COLLISION_SHIFT)); - MapGridSetMetatileIdAt(10, 7, (METATILE_BattleTower_Elevator_Top2 + 3) | (COLLISION_DIR_ALL << METATILE_COLLISION_SHIFT)); - MapGridSetMetatileIdAt( 7, 8, (METATILE_BattleTower_Elevator_Mid0 + 3) | (COLLISION_DIR_ALL << METATILE_COLLISION_SHIFT)); - MapGridSetMetatileIdAt( 8, 8, (METATILE_BattleTower_Elevator_Mid1 + 3) | (COLLISION_DIR_ALL << METATILE_COLLISION_SHIFT)); - MapGridSetMetatileIdAt( 9, 8, (METATILE_BattleTower_Elevator_Mid1 + 3) | (COLLISION_DIR_ALL << METATILE_COLLISION_SHIFT)); - MapGridSetMetatileIdAt(10, 8, (METATILE_BattleTower_Elevator_Mid2 + 3) | (COLLISION_DIR_ALL << METATILE_COLLISION_SHIFT)); - MapGridSetMetatileIdAt( 7, 9, (METATILE_BattleTower_Elevator_Bottom0 + 3) | (COLLISION_DIR_ALL << METATILE_COLLISION_SHIFT)); - MapGridSetMetatileIdAt( 8, 9, (METATILE_BattleTower_Elevator_Bottom1 + 3) | (COLLISION_DIR_ALL << METATILE_COLLISION_SHIFT)); - MapGridSetMetatileIdAt( 9, 9, (METATILE_BattleTower_Elevator_Bottom1 + 3) | (COLLISION_DIR_ALL << METATILE_COLLISION_SHIFT)); - MapGridSetMetatileIdAt(10, 9, (METATILE_BattleTower_Elevator_Bottom2 + 3) | (COLLISION_DIR_ALL << METATILE_COLLISION_SHIFT)); + MapGridSetMetatileIdAt( 7, 7, (METATILE_BattleTower_Elevator_Top0 + 3) | MAPGRID_COLLISION_MASK); + MapGridSetMetatileIdAt( 8, 7, (METATILE_BattleTower_Elevator_Top1 + 3) | MAPGRID_COLLISION_MASK); + MapGridSetMetatileIdAt( 9, 7, (METATILE_BattleTower_Elevator_Top1 + 3) | MAPGRID_COLLISION_MASK); + MapGridSetMetatileIdAt(10, 7, (METATILE_BattleTower_Elevator_Top2 + 3) | MAPGRID_COLLISION_MASK); + MapGridSetMetatileIdAt( 7, 8, (METATILE_BattleTower_Elevator_Mid0 + 3) | MAPGRID_COLLISION_MASK); + MapGridSetMetatileIdAt( 8, 8, (METATILE_BattleTower_Elevator_Mid1 + 3) | MAPGRID_COLLISION_MASK); + MapGridSetMetatileIdAt( 9, 8, (METATILE_BattleTower_Elevator_Mid1 + 3) | MAPGRID_COLLISION_MASK); + MapGridSetMetatileIdAt(10, 8, (METATILE_BattleTower_Elevator_Mid2 + 3) | MAPGRID_COLLISION_MASK); + MapGridSetMetatileIdAt( 7, 9, (METATILE_BattleTower_Elevator_Bottom0 + 3) | MAPGRID_COLLISION_MASK); + MapGridSetMetatileIdAt( 8, 9, (METATILE_BattleTower_Elevator_Bottom1 + 3) | MAPGRID_COLLISION_MASK); + MapGridSetMetatileIdAt( 9, 9, (METATILE_BattleTower_Elevator_Bottom1 + 3) | MAPGRID_COLLISION_MASK); + MapGridSetMetatileIdAt(10, 9, (METATILE_BattleTower_Elevator_Bottom2 + 3) | MAPGRID_COLLISION_MASK); } DrawWholeMapView(); task->data[4] ^= 1; diff --git a/src/fieldmap.c b/src/fieldmap.c index f50bba140..74f1ee292 100644 --- a/src/fieldmap.c +++ b/src/fieldmap.c @@ -5,6 +5,7 @@ #include "script.h" #include "secret_base.h" #include "tv.h" +#include "constants/metatile_behaviors.h" struct ConnectionFlags { @@ -29,108 +30,135 @@ struct BackupMapLayout gBackupMapLayout; static const struct ConnectionFlags sDummyConnectionFlags = {0}; -struct MapHeader *mapconnection_get_mapheader(struct MapConnection *connection) +static void InitMapLayoutData(struct MapHeader *mapHeader); +static void InitBackupMapLayoutData(const u16 *map, u16 width, u16 height); +static void FillSouthConnection(struct MapHeader const *mapHeader, struct MapHeader const *connectedMapHeader, s32 offset); +static void FillNorthConnection(struct MapHeader const *mapHeader, struct MapHeader const *connectedMapHeader, s32 offset); +static void FillWestConnection(struct MapHeader const *mapHeader, struct MapHeader const *connectedMapHeader, s32 offset); +static void FillEastConnection(struct MapHeader const *mapHeader, struct MapHeader const *connectedMapHeader, s32 offset); +static void InitBackupMapLayoutConnections(struct MapHeader *mapHeader); +static void LoadSavedMapView(void); +static const struct MapConnection *GetIncomingConnection(u8 direction, int x, int y); +static bool8 IsPosInIncomingConnectingMap(u8 direction, int x, int y, const struct MapConnection *connection); +static bool8 IsCoordInIncomingConnectingMap(int coord, int srcMax, int destMax, int offset); + +#define GetBorderBlockAt(x, y)({ \ + u16 block; \ + int i; \ + UNUSED const u16 *border = gMapHeader.mapLayout->border; /* Read again below */ \ + \ + i = (x + 1) & 1; \ + i += ((y + 1) & 1) * 2; \ + \ + block = gMapHeader.mapLayout->border[i] | MAPGRID_COLLISION_MASK; \ +}) + +#define AreCoordsWithinMapGridBounds(x, y) (x >= 0 && x < gBackupMapLayout.width && y >= 0 && y < gBackupMapLayout.height) + +#define GetMapGridBlockAt(x, y) (AreCoordsWithinMapGridBounds(x, y) ? gBackupMapLayout.map[x + gBackupMapLayout.width * y] : GetBorderBlockAt(x, y)) + +const struct MapHeader *const GetMapHeaderFromConnection(const struct MapConnection *connection) { return Overworld_GetMapHeaderByGroupAndId(connection->mapGroup, connection->mapNum); } -void not_trainer_hill_battle_pyramid(void) +void InitMap(void) { - mapheader_copy_mapdata_with_padding(&gMapHeader); - sub_80BB970(gMapHeader.events); + InitMapLayoutData(&gMapHeader); + SetOccupiedSecretBaseEntranceMetatiles(gMapHeader.events); RunOnLoadMapScript(); } -void sub_8055FC0(void) +void InitMapFromSavedGame(void) { - mapheader_copy_mapdata_with_padding(&gMapHeader); - sub_80BBCCC(0); - sub_80BB970(gMapHeader.events); - sub_8056670(); + InitMapLayoutData(&gMapHeader); + InitSecretBaseAppearance(FALSE); + SetOccupiedSecretBaseEntranceMetatiles(gMapHeader.events); + LoadSavedMapView(); RunOnLoadMapScript(); UpdateTVScreensOnMap(gBackupMapLayout.width, gBackupMapLayout.height); } -void mapheader_copy_mapdata_with_padding(struct MapHeader *mapHeader) +static void InitMapLayoutData(struct MapHeader *mapHeader) { - struct MapLayout *mapLayout; + struct MapLayout const *mapLayout; int width; int height; mapLayout = mapHeader->mapLayout; - CpuFastFill16(0x03ff, gBackupMapData, sizeof(gBackupMapData)); + CpuFastFill16(MAPGRID_UNDEFINED, gBackupMapData, sizeof(gBackupMapData)); gBackupMapLayout.map = gBackupMapData; - width = mapLayout->width + 15; + width = mapLayout->width + MAP_OFFSET_W; gBackupMapLayout.width = width; - height = mapLayout->height + 14; + height = mapLayout->height + MAP_OFFSET_H; gBackupMapLayout.height = height; if (width * height <= MAX_MAP_DATA_SIZE) { - map_copy_with_padding(mapLayout->map, mapLayout->width, mapLayout->height); - sub_80560AC(mapHeader); + InitBackupMapLayoutData(mapLayout->map, mapLayout->width, mapLayout->height); + InitBackupMapLayoutConnections(mapHeader); } } -void map_copy_with_padding(u16 *map, u16 width, u16 height) +static void InitBackupMapLayoutData(const u16 *map, u16 width, u16 height) { u16 *dest; int y; dest = gBackupMapLayout.map; - dest += gBackupMapLayout.width * 7 + 7; + dest += gBackupMapLayout.width * 7 + MAP_OFFSET; for (y = 0; y < height; y++) { CpuCopy16(map, dest, width * 2); - dest += width + 0xf; + dest += width + MAP_OFFSET_W; map += width; } } -void sub_80560AC(struct MapHeader *mapHeader) +static void InitBackupMapLayoutConnections(struct MapHeader *mapHeader) { // BUG: This results in a null pointer dereference when mapHeader->connections // is NULL, causing count to be assigned a garbage value. This garbage value // just so happens to have the most significant bit set, so it is treated as // negative and the loop below thankfully never executes in this scenario. int count = mapHeader->connections->count; - struct MapConnection *connection = mapHeader->connections->connections; + const struct MapConnection *connection = mapHeader->connections->connections; int i; gMapConnectionFlags = sDummyConnectionFlags; for (i = 0; i < count; i++, connection++) { - struct MapHeader *cMap = mapconnection_get_mapheader(connection); + struct MapHeader const *cMap = GetMapHeaderFromConnection(connection); u32 offset = connection->offset; switch (connection->direction) { case CONNECTION_SOUTH: - fillSouthConnection(mapHeader, cMap, offset); - gMapConnectionFlags.south = 1; + FillSouthConnection(mapHeader, cMap, offset); + gMapConnectionFlags.south = TRUE; break; case CONNECTION_NORTH: - fillNorthConnection(mapHeader, cMap, offset); - gMapConnectionFlags.north = 1; + FillNorthConnection(mapHeader, cMap, offset); + gMapConnectionFlags.north = TRUE; break; case CONNECTION_WEST: - fillWestConnection(mapHeader, cMap, offset); - gMapConnectionFlags.west = 1; + FillWestConnection(mapHeader, cMap, offset); + gMapConnectionFlags.west = TRUE; break; case CONNECTION_EAST: - fillEastConnection(mapHeader, cMap, offset); - gMapConnectionFlags.east = 1; + FillEastConnection(mapHeader, cMap, offset); + gMapConnectionFlags.east = TRUE; break; } } } -void sub_8056134(int x, int y, struct MapHeader *mapHeader, 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; + const u16 *src; u16 *dest; int mapWidth; - mapWidth = mapHeader->mapLayout->width; - src = &mapHeader->mapLayout->map[mapWidth * y2 + x2]; + mapWidth = connectedMapHeader->mapLayout->width; + src = &connectedMapHeader->mapLayout->map[mapWidth * y2 + x2]; dest = &gBackupMapLayout.map[gBackupMapLayout.width * y + x]; for (i = 0; i < height; i++) @@ -141,7 +169,7 @@ void sub_8056134(int x, int y, struct MapHeader *mapHeader, int x2, int y2, int } } -void fillSouthConnection(struct MapHeader *mapHeader, struct MapHeader *connectedMapHeader, s32 offset) +static void FillSouthConnection(struct MapHeader const *mapHeader, struct MapHeader const *connectedMapHeader, s32 offset) { int x, y; int x2; @@ -151,43 +179,35 @@ void fillSouthConnection(struct MapHeader *mapHeader, struct MapHeader *connecte if (connectedMapHeader) { cWidth = connectedMapHeader->mapLayout->width; - x = offset + 7; - y = mapHeader->mapLayout->height + 7; + x = offset + MAP_OFFSET; + y = mapHeader->mapLayout->height + MAP_OFFSET; if (x < 0) { x2 = -x; x += cWidth; if (x < gBackupMapLayout.width) - { width = x; - } else - { width = gBackupMapLayout.width; - } x = 0; } else { x2 = 0; if (x + cWidth < gBackupMapLayout.width) - { width = cWidth; - } else - { width = gBackupMapLayout.width - x; - } } - sub_8056134( + FillConnection( x, y, connectedMapHeader, x2, /*y2*/ 0, - width, /*height*/ 7); + width, /*height*/ MAP_OFFSET); } } -void fillNorthConnection(struct MapHeader *mapHeader, struct MapHeader *connectedMapHeader, s32 offset) +static void FillNorthConnection(struct MapHeader const *mapHeader, struct MapHeader const *connectedMapHeader, s32 offset) { int x; int x2, y2; @@ -198,36 +218,28 @@ void fillNorthConnection(struct MapHeader *mapHeader, struct MapHeader *connecte { cWidth = connectedMapHeader->mapLayout->width; cHeight = connectedMapHeader->mapLayout->height; - x = offset + 7; - y2 = cHeight - 7; + x = offset + MAP_OFFSET; + y2 = cHeight - MAP_OFFSET; if (x < 0) { x2 = -x; x += cWidth; if (x < gBackupMapLayout.width) - { width = x; - } else - { width = gBackupMapLayout.width; - } x = 0; } else { x2 = 0; if (x + cWidth < gBackupMapLayout.width) - { width = cWidth; - } else - { width = gBackupMapLayout.width - x; - } } - sub_8056134( + FillConnection( x, /*y*/ 0, connectedMapHeader, x2, y2, @@ -237,7 +249,7 @@ void fillNorthConnection(struct MapHeader *mapHeader, struct MapHeader *connecte } -void fillWestConnection(struct MapHeader *mapHeader, struct MapHeader *connectedMapHeader, s32 offset) +static void FillWestConnection(struct MapHeader const *mapHeader, struct MapHeader const *connectedMapHeader, s32 offset) { int y; int x2, y2; @@ -247,43 +259,35 @@ void fillWestConnection(struct MapHeader *mapHeader, struct MapHeader *connected { cWidth = connectedMapHeader->mapLayout->width; cHeight = connectedMapHeader->mapLayout->height; - y = offset + 7; - x2 = cWidth - 7; + y = offset + MAP_OFFSET; + x2 = cWidth - MAP_OFFSET; if (y < 0) { y2 = -y; if (y + cHeight < gBackupMapLayout.height) - { height = y + cHeight; - } else - { height = gBackupMapLayout.height; - } y = 0; } else { y2 = 0; if (y + cHeight < gBackupMapLayout.height) - { height = cHeight; - } else - { height = gBackupMapLayout.height - y; - } } - sub_8056134( + FillConnection( /*x*/ 0, y, connectedMapHeader, x2, y2, - /*width*/ 7, height); + /*width*/ MAP_OFFSET, height); } } -void fillEastConnection(struct MapHeader *mapHeader, struct MapHeader *connectedMapHeader, s32 offset) +static void FillEastConnection(struct MapHeader const *mapHeader, struct MapHeader const *connectedMapHeader, s32 offset) { int x, y; int y2; @@ -292,177 +296,99 @@ void fillEastConnection(struct MapHeader *mapHeader, struct MapHeader *connected if (connectedMapHeader) { cHeight = connectedMapHeader->mapLayout->height; - x = mapHeader->mapLayout->width + 7; - y = offset + 7; + x = mapHeader->mapLayout->width + MAP_OFFSET; + y = offset + MAP_OFFSET; if (y < 0) { y2 = -y; if (y + cHeight < gBackupMapLayout.height) - { height = y + cHeight; - } else - { height = gBackupMapLayout.height; - } y = 0; } else { y2 = 0; if (y + cHeight < gBackupMapLayout.height) - { height = cHeight; - } else - { height = gBackupMapLayout.height - y; - } } - sub_8056134( + FillConnection( x, y, connectedMapHeader, /*x2*/ 0, y2, - /*width*/ 8, height); + /*width*/ MAP_OFFSET + 1, height); } } -union Block +u8 MapGridGetElevationAt(int x, int y) { - struct - { - u16 block:10; - u16 collision:2; - u16 elevation:4; - } block; - u16 value; -}; + u16 block = GetMapGridBlockAt(x, y); -u8 MapGridGetZCoordAt(int x, int y) -{ - u16 block; - int i; - u16 *border; - - if (x >= 0 && x < gBackupMapLayout.width - && y >= 0 && y < gBackupMapLayout.height) - { - block = gBackupMapLayout.map[x + gBackupMapLayout.width * y]; - } - else - { - border = gMapHeader.mapLayout->border; - i = (x + 1) & 1; - i += ((y + 1) & 1) * 2; - block = gMapHeader.mapLayout->border[i]; - block |= 0xc00; - } - if (block == 0x3ff) - { + if (block == MAPGRID_UNDEFINED) return 0; - } - return block >> 12; + + return block >> MAPGRID_ELEVATION_SHIFT; } -u8 MapGridIsImpassableAt(int x, int y) +u8 MapGridGetCollisionAt(int x, int y) { - u16 block; - int i; - u16 *border; + u16 block = GetMapGridBlockAt(x, y); - if (x >= 0 && x < gBackupMapLayout.width - && y >= 0 && y < gBackupMapLayout.height) - { - block = gBackupMapLayout.map[x + gBackupMapLayout.width * y]; - } - else - { - border = gMapHeader.mapLayout->border; - i = (x + 1) & 1; - i += ((y + 1) & 1) * 2; - block = gMapHeader.mapLayout->border[i]; - block |= METATILE_COLLISION_MASK; - } - if (block == METATILE_ID_UNDEFINED) - { - return 1; - } - return (block & METATILE_COLLISION_MASK) >> 10; + if (block == MAPGRID_UNDEFINED) + return TRUE; + + return (block & MAPGRID_COLLISION_MASK) >> MAPGRID_COLLISION_SHIFT; } u32 MapGridGetMetatileIdAt(int x, int y) { - u16 block; - int i; - int j; - struct MapLayout *mapLayout; - u16 *border; - u16 block2; + u16 block = GetMapGridBlockAt(x, y); - if (x >= 0 && x < gBackupMapLayout.width - && y >= 0 && y < gBackupMapLayout.height) - { - block = gBackupMapLayout.map[x + gBackupMapLayout.width * y]; - } - else - { - mapLayout = gMapHeader.mapLayout; - i = (x + 1) & 1; - i += ((y + 1) & 1) * 2; - block = mapLayout->border[i] | 0xc00; - } - if (block == 0x3ff) - { - border = gMapHeader.mapLayout->border; - j = (x + 1) & 1; - j += ((y + 1) & 1) * 2; - block2 = gMapHeader.mapLayout->border[j]; - block2 |= 0xc00; - return block2 & block; - } - return block & 0x3ff; + if (block == MAPGRID_UNDEFINED) + return GetBorderBlockAt(x, y) & MAPGRID_METATILE_ID_MASK; + + return block & MAPGRID_METATILE_ID_MASK; } u32 MapGridGetMetatileBehaviorAt(int x, int y) { - u16 metatile; - metatile = MapGridGetMetatileIdAt(x, y); - return GetBehaviorByMetatileId(metatile) & 0xff; + u16 metatile = MapGridGetMetatileIdAt(x, y); + return GetMetatileAttributesById(metatile) & METATILE_ATTR_BEHAVIOR_MASK; } u8 MapGridGetMetatileLayerTypeAt(int x, int y) { - u16 metatile; - metatile = MapGridGetMetatileIdAt(x, y); - return (GetBehaviorByMetatileId(metatile) & 0xf000) >> 12; + u16 metatile = MapGridGetMetatileIdAt(x, y); + return (GetMetatileAttributesById(metatile) & METATILE_ATTR_LAYER_MASK) >> METATILE_ATTR_LAYER_SHIFT; } void MapGridSetMetatileIdAt(int x, int y, u16 metatile) { int i; - if (x >= 0 && x < gBackupMapLayout.width - && y >= 0 && y < gBackupMapLayout.height) + if (AreCoordsWithinMapGridBounds(x, y)) { i = x + y * gBackupMapLayout.width; - gBackupMapLayout.map[i] = (gBackupMapLayout.map[i] & 0xf000) | (metatile & 0xfff); + gBackupMapLayout.map[i] = (gBackupMapLayout.map[i] & MAPGRID_ELEVATION_MASK) | (metatile & ~MAPGRID_ELEVATION_MASK); } } void MapGridSetMetatileEntryAt(int x, int y, u16 metatile) { int i; - if (x >= 0 && x < gBackupMapLayout.width - && y >= 0 && y < gBackupMapLayout.height) + if (AreCoordsWithinMapGridBounds(x, y)) { i = x + gBackupMapLayout.width * y; gBackupMapLayout.map[i] = metatile; } } -u16 GetBehaviorByMetatileId(u16 metatile) +u16 GetMetatileAttributesById(u16 metatile) { - u16 *attributes; + const u16 *attributes; if (metatile < NUM_METATILES_IN_PRIMARY) { attributes = gMapHeader.mapLayout->primaryTileset->metatileAttributes; @@ -475,11 +401,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; @@ -489,60 +415,59 @@ void save_serialize_map(void) width = gBackupMapLayout.width; x = gSaveBlock1.pos.x; y = gSaveBlock1.pos.y; - for (i = y; i < y + 14; i++) + for (i = y; i < y + MAP_OFFSET_H; i++) { - for (j = x; j < x + 15; j++) - { + for (j = x; j < x + MAP_OFFSET_W; j++) *mapView++ = gBackupMapData[width * i + j]; - } } } -int sub_8056618(void) +static bool32 SavedMapViewIsEmpty(void) { u16 i; - u32 r2; - r2 = 0; + u32 marker = 0; + +#ifndef UBFIX + // BUG: This loop extends past the bounds of the mapView array. Its size is only 0x100. for (i = 0; i < 0x200; i++) - { - r2 |= gSaveBlock1.mapView[i]; - } - if (r2 == 0) - { - return 1; - } - return 0; +#else + for (i = 0; i < ARRAY_COUNT(gSaveBlock1.mapView); i++) +#endif + marker |= gSaveBlock1.mapView[i]; + + if (marker == 0) + return TRUE; + + return FALSE; } -void sav2_mapdata_clear(void) +static void ClearSavedMapView(void) { CpuFill16(0, gSaveBlock1.mapView, sizeof(gSaveBlock1.mapView)); } -void sub_8056670(void) +static void LoadSavedMapView(void) { int i, j; int x, y; u16 *mapView; int width; mapView = gSaveBlock1.mapView; - if (!sub_8056618()) + if (!SavedMapViewIsEmpty()) { width = gBackupMapLayout.width; x = gSaveBlock1.pos.x; y = gSaveBlock1.pos.y; - for (i = y; i < y + 14; i++) + for (i = y; i < y + MAP_OFFSET_H; i++) { - for (j = x; j < x + 15; j++) - { + for (j = x; j < x + MAP_OFFSET_W; j++) gBackupMapData[width * i + j] = *mapView++; - } } - sav2_mapdata_clear(); + ClearSavedMapView(); } } -void sub_80566F0(u8 a1) +static void MoveMapViewToBackup(u8 direction) { u16 *mapView; int width; @@ -559,25 +484,25 @@ void sub_80566F0(u8 a1) r8 = 0; x0 = gSaveBlock1.pos.x; y0 = gSaveBlock1.pos.y; - x2 = 15; - y2 = 14; - switch (a1) + x2 = MAP_OFFSET_W; + y2 = MAP_OFFSET_H; + switch (direction) { case CONNECTION_NORTH: y0 += 1; - y2 = 13; + y2 = MAP_OFFSET_H - 1; break; case CONNECTION_SOUTH: r8 = 1; - y2 = 13; + y2 = MAP_OFFSET_H - 1; break; case CONNECTION_WEST: x0 += 1; - x2 = 14; + x2 = MAP_OFFSET_W - 1; break; case CONNECTION_EAST: r9 = 1; - x2 = 14; + x2 = MAP_OFFSET_W - 1; break; } for (y = 0; y < y2; y++) @@ -587,7 +512,7 @@ void sub_80566F0(u8 a1) for (x = 0; x < x2; x++) { desti = width * (y + y0); - srci = (y + r8) * 15 + r9; + srci = (y + r8) * MAP_OFFSET_W + r9; src = &mapView[srci + i]; dest = &gBackupMapData[x0 + desti + j]; *dest = *src; @@ -595,100 +520,69 @@ void sub_80566F0(u8 a1) j++; } } - sav2_mapdata_clear(); + ClearSavedMapView(); } int GetMapBorderIdAt(int x, int y) { - struct MapLayout *mapLayout; - u16 block, block2; - int i, j; - if (x >= 0 && x < gBackupMapLayout.width - && y >= 0 && y < gBackupMapLayout.height) - { - i = gBackupMapLayout.width; - i *= y; - block = gBackupMapLayout.map[x + i]; - if (block == 0x3ff) - { - goto fail; - } - } - else - { - mapLayout = gMapHeader.mapLayout; - j = (x + 1) & 1; - j += ((y + 1) & 1) * 2; - block2 = 0xc00 | mapLayout->border[j]; - if (block2 == 0x3ff) - { - goto fail; - } - } - goto success; -fail: - return -1; -success: + if (GetMapGridBlockAt(x, y) == MAPGRID_UNDEFINED) + return CONNECTION_INVALID; - if (x >= (gBackupMapLayout.width - 8)) + if (x >= (gBackupMapLayout.width - (MAP_OFFSET + 1))) { if (!gMapConnectionFlags.east) - { - return -1; - } + return CONNECTION_INVALID; + return CONNECTION_EAST; } - else if (x < 7) + else if (x < MAP_OFFSET) { if (!gMapConnectionFlags.west) - { - return -1; - } + return CONNECTION_INVALID; + return CONNECTION_WEST; } - else if (y >= (gBackupMapLayout.height - 7)) + else if (y >= (gBackupMapLayout.height - MAP_OFFSET)) { if (!gMapConnectionFlags.south) - { - return -1; - } + return CONNECTION_INVALID; + return CONNECTION_SOUTH; } - else if (y < 7) + else if (y < MAP_OFFSET) { if (!gMapConnectionFlags.north) - { - return -1; - } + return CONNECTION_INVALID; + return CONNECTION_NORTH; } else { - return 0; + return CONNECTION_NONE; } } int GetPostCameraMoveMapBorderId(int x, int y) { - return GetMapBorderIdAt(gSaveBlock1.pos.x + 7 + x, gSaveBlock1.pos.y + 7 + y); + return GetMapBorderIdAt(gSaveBlock1.pos.x + MAP_OFFSET + x, gSaveBlock1.pos.y + MAP_OFFSET + y); } -int CanCameraMoveInDirection(int direction) +bool32 CanCameraMoveInDirection(int direction) { int x, y; - x = gSaveBlock1.pos.x + 7 + gDirectionToVectors[direction].x; - y = gSaveBlock1.pos.y + 7 + gDirectionToVectors[direction].y; - if (GetMapBorderIdAt(x, y) == -1) - { - return 0; - } - return 1; + x = gSaveBlock1.pos.x + MAP_OFFSET + gDirectionToVectors[direction].x; + y = gSaveBlock1.pos.y + MAP_OFFSET + gDirectionToVectors[direction].y; + + if (GetMapBorderIdAt(x, y) == CONNECTION_INVALID) + return FALSE; + + return TRUE; } -void sub_8056918(struct MapConnection *connection, int direction, int x, int y) +static void SetPositionFromConnection(const struct MapConnection *connection, int direction, int x, int y) { - struct MapHeader *mapHeader; - mapHeader = mapconnection_get_mapheader(connection); + const struct MapHeader *mapHeader; + mapHeader = GetMapHeaderFromConnection(connection); switch (direction) { case CONNECTION_EAST: @@ -712,118 +606,116 @@ void sub_8056918(struct MapConnection *connection, int direction, int x, int y) bool8 CameraMove(int x, int y) { - unsigned int direction; - struct MapConnection *connection; + int direction; + const 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) { gSaveBlock1.pos.x += x; gSaveBlock1.pos.y += y; } else { - save_serialize_map(); + SaveMapView(); old_x = gSaveBlock1.pos.x; old_y = gSaveBlock1.pos.y; - connection = sub_8056A64(direction, gSaveBlock1.pos.x, gSaveBlock1.pos.y); - sub_8056918(connection, direction, x, y); - sub_80538F0(connection->mapGroup, connection->mapNum); + connection = GetIncomingConnection(direction, gSaveBlock1.pos.x, gSaveBlock1.pos.y); + SetPositionFromConnection(connection, direction, x, y); + LoadMapFromCameraTransition(connection->mapGroup, connection->mapNum); gCamera.active = TRUE; gCamera.x = old_x - gSaveBlock1.pos.x; gCamera.y = old_y - gSaveBlock1.pos.y; gSaveBlock1.pos.x += x; gSaveBlock1.pos.y += y; - sub_80566F0(direction); + MoveMapViewToBackup(direction); } return gCamera.active; } -struct MapConnection *sub_8056A64(u8 direction, int x, int y) +static const struct MapConnection *GetIncomingConnection(u8 direction, int x, int y) { int count; - struct MapConnection *connection; int i; - count = gMapHeader.connections->count; - connection = gMapHeader.connections->connections; + const struct MapConnection *connection; + const struct MapConnections *connections = gMapHeader.connections; + +#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) - { - if (sub_8056ABC(direction, x, y, connection) == TRUE) - { - return connection; - } - } + if (connection->direction == direction && IsPosInIncomingConnectingMap(direction, x, y, connection) == TRUE) + return connection; } return NULL; } -bool8 sub_8056ABC(u8 direction, int x, int y, struct MapConnection *connection) +static bool8 IsPosInIncomingConnectingMap(u8 direction, int x, int y, const struct MapConnection *connection) { - struct MapHeader *mapHeader; - mapHeader = mapconnection_get_mapheader(connection); + const struct MapHeader *mapHeader; + mapHeader = GetMapHeaderFromConnection(connection); switch (direction) { case CONNECTION_SOUTH: case CONNECTION_NORTH: - return sub_8056B20(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_8056B20(y, gMapHeader.mapLayout->height, mapHeader->mapLayout->height, connection->offset); + return IsCoordInIncomingConnectingMap(y, gMapHeader.mapLayout->height, mapHeader->mapLayout->height, connection->offset); } return FALSE; } -bool8 sub_8056B20(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; + if (offset2 < 0) - { offset2 = 0; - } - if (dest_width + offset < src_width) - { - src_width = dest_width + offset; - } - if (offset2 <= x && x <= src_width) - { + + if (destMax + offset < srcMax) + srcMax = destMax + offset; + + if (offset2 <= coord && coord <= srcMax) return TRUE; - } + return FALSE; } -int sub_8056B4C(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_8056B60(struct MapConnection *connection, int x, int y) +static int IsPosInConnectingMap(const struct MapConnection *connection, int x, int y) { - struct MapHeader *mapHeader; - mapHeader = mapconnection_get_mapheader(connection); + const struct MapHeader *mapHeader; + mapHeader = GetMapHeaderFromConnection(connection); switch (connection->direction) { case CONNECTION_SOUTH: case CONNECTION_NORTH: - return sub_8056B4C(x - connection->offset, mapHeader->mapLayout->width); + return IsCoordInConnectingMap(x - connection->offset, mapHeader->mapLayout->width); case CONNECTION_WEST: case CONNECTION_EAST: - return sub_8056B4C(y - connection->offset, mapHeader->mapLayout->height); + return IsCoordInConnectingMap(y - connection->offset, mapHeader->mapLayout->height); } return FALSE; } -struct MapConnection *sub_8056BA0(s16 x, s16 y) +const struct MapConnection *GetMapConnectionAtPos(s16 x, s16 y) { int count; - struct MapConnection *connection; + const struct MapConnection *connection; int i; u8 direction; if (!gMapHeader.connections) @@ -838,14 +730,14 @@ struct MapConnection *sub_8056BA0(s16 x, s16 y) { direction = connection->direction; if ((direction == CONNECTION_DIVE || direction == CONNECTION_EMERGE) - || (direction == CONNECTION_NORTH && y > 6) - || (direction == CONNECTION_SOUTH && y < gMapHeader.mapLayout->height + 7) - || (direction == CONNECTION_WEST && x > 6) - || (direction == CONNECTION_EAST && x < gMapHeader.mapLayout->width + 7)) + || (direction == CONNECTION_NORTH && y > MAP_OFFSET - 1) + || (direction == CONNECTION_SOUTH && y < gMapHeader.mapLayout->height + MAP_OFFSET) + || (direction == CONNECTION_WEST && x > MAP_OFFSET - 1) + || (direction == CONNECTION_EAST && x < gMapHeader.mapLayout->width + MAP_OFFSET)) { continue; } - if (sub_8056B60(connection, x - 7, y - 7) == TRUE) + if (IsPosInConnectingMap(connection, x - MAP_OFFSET, y - MAP_OFFSET) == TRUE) { return connection; } @@ -854,19 +746,19 @@ struct MapConnection *sub_8056BA0(s16 x, s16 y) return NULL; } -void sub_8056C50(u16 x, u16 y) +void SetCameraFocusCoords(u16 x, u16 y) { - gSaveBlock1.pos.x = x - 7; - gSaveBlock1.pos.y = y - 7; + gSaveBlock1.pos.x = x - MAP_OFFSET; + gSaveBlock1.pos.y = y - MAP_OFFSET; } -void sav1_camera_get_focus_coords(u16 *x, u16 *y) +void GetCameraFocusCoords(u16 *x, u16 *y) { - *x = gSaveBlock1.pos.x + 7; - *y = gSaveBlock1.pos.y + 7; + *x = gSaveBlock1.pos.x + MAP_OFFSET; + *y = gSaveBlock1.pos.y + MAP_OFFSET; } -void unref_sub_8056C7C(u16 x, u16 y) +static UNUSED void SetCameraCoords(u16 x, u16 y) { gSaveBlock1.pos.x = x; gSaveBlock1.pos.y = y; @@ -878,80 +770,75 @@ void GetCameraCoords(u16 *x, u16 *y) *y = gSaveBlock1.pos.y; } -void sub_8056C98(struct Tileset *tileset, void *dest) +static void CopyTilesetToVram(const struct Tileset *tileset, void *dest) { if (tileset) { if (!tileset->isCompressed) - { - CpuFastCopy(tileset->tiles, dest, NUM_TILES_IN_PRIMARY * 16 * 2); - } + CpuFastCopy(tileset->tiles, dest, 512 * TILE_SIZE_4BPP); // Number of primary and secondary tiles are assumed to be the same else - { LZ77UnCompVram(tileset->tiles, dest); - } } } -void sub_8056CBC(struct Tileset *tileset, int offset, int size) +static void LoadTilesetPalette(const struct Tileset *tileset, int destOffset, int size) { - u16 black; if (tileset) { if (tileset->isSecondary == FALSE) { - black = 0; - LoadPalette(&black, offset, 2); - LoadPalette(tileset->palettes + 2, offset + 1, size - 2); + u16 black = RGB_BLACK; + LoadPalette(&black, destOffset, PLTT_SIZEOF(1)); + LoadPalette(tileset->palettes[0] + 1, destOffset + 1, size - PLTT_SIZEOF(1)); } else if (tileset->isSecondary == TRUE) { - LoadPalette((u16*)tileset->palettes + (NUM_PALS_IN_PRIMARY * 16), offset, size); + LoadPalette(tileset->palettes[NUM_PALS_IN_PRIMARY], destOffset, size); } else { LZ77UnCompVram(tileset->palettes, (void*)EWRAM); - LoadPalette((void*)EWRAM, offset, size); + LoadPalette((void*)EWRAM, destOffset, size); } } } -void sub_8056D28(struct MapLayout *mapLayout) +void CopyPrimaryTilesetToVram(const struct MapLayout *mapLayout) { void *dest = (void*)(BG_VRAM); - sub_8056C98(mapLayout->primaryTileset, dest); + CopyTilesetToVram(mapLayout->primaryTileset, dest); } -void sub_8056D38(struct MapLayout *mapLayout) +void CopySecondaryTilesetToVram(const struct MapLayout *mapLayout) { - void *dest = (void*)(BG_VRAM + NUM_TILES_IN_PRIMARY * 16 * 2); - sub_8056C98(mapLayout->secondaryTileset, dest); + void *dest = (void*)(BG_VRAM + NUM_TILES_IN_PRIMARY * TILE_SIZE_4BPP); + CopyTilesetToVram(mapLayout->secondaryTileset, dest); } -void apply_map_tileset1_palette(struct MapLayout *mapLayout) +static void LoadPrimaryTilesetPalette(const struct MapLayout *mapLayout) { - sub_8056CBC(mapLayout->primaryTileset, 0, NUM_PALS_IN_PRIMARY * 16 * 2); + LoadTilesetPalette(mapLayout->primaryTileset, BG_PLTT_ID(0), NUM_PALS_IN_PRIMARY * PLTT_SIZE_4BPP); } -void apply_map_tileset2_palette(struct MapLayout *mapLayout) +void LoadSecondaryTilesetPalette(const struct MapLayout *mapLayout) { - sub_8056CBC(mapLayout->secondaryTileset, NUM_PALS_IN_PRIMARY * 16, (NUM_PALS_TOTAL - NUM_PALS_IN_PRIMARY) * 16 * 2); + LoadTilesetPalette(mapLayout->secondaryTileset, BG_PLTT_ID(NUM_PALS_IN_PRIMARY), (NUM_PALS_TOTAL - NUM_PALS_IN_PRIMARY) * PLTT_SIZE_4BPP); } -void copy_map_tileset1_tileset2_to_vram(struct MapLayout *mapLayout) +void CopyMapTilesetsToVram(const struct MapLayout *mapLayout) { if (mapLayout) { - sub_8056D28(mapLayout); - sub_8056D38(mapLayout); + CopyPrimaryTilesetToVram(mapLayout); + CopySecondaryTilesetToVram(mapLayout); } } -void apply_map_tileset1_tileset2_palette(struct MapLayout *mapLayout) +void LoadMapTilesetPalettes(const struct MapLayout *mapLayout) { if (mapLayout) { - apply_map_tileset1_palette(mapLayout); - apply_map_tileset2_palette(mapLayout); + LoadPrimaryTilesetPalette(mapLayout); + LoadSecondaryTilesetPalette(mapLayout); } } diff --git a/src/fldeff_cut.c b/src/fldeff_cut.c index 162dc4ff9..913b92aff 100644 --- a/src/fldeff_cut.c +++ b/src/fldeff_cut.c @@ -106,7 +106,7 @@ void Debug_SetUpFieldMove_Cut(void) for (j = 0; j < 3; j++) { x = j - 1 + gPlayerFacingPosition.x; - if (MapGridGetZCoordAt(x, y) == gPlayerFacingPosition.height) + if (MapGridGetElevationAt(x, y) == gPlayerFacingPosition.height) { metatileBehavior = MapGridGetMetatileBehaviorAt(x, y); if (MetatileBehavior_IsPokeGrass(metatileBehavior) == TRUE @@ -147,7 +147,7 @@ bool8 SetUpFieldMove_Cut(void) for (j = 0; j < 3; j++) { x = j - 1 + gPlayerFacingPosition.x; - if (MapGridGetZCoordAt(x, y) == gPlayerFacingPosition.height) + if (MapGridGetElevationAt(x, y) == gPlayerFacingPosition.height) { tileBehavior = MapGridGetMetatileBehaviorAt(x, y); if(MetatileBehavior_IsPokeGrass(tileBehavior) == TRUE @@ -219,7 +219,7 @@ bool8 FldEff_CutGrass(void) for (j = 0; j < 3; j++) { x = j - 1 + gPlayerFacingPosition.x; - if (MapGridGetZCoordAt(x, y) == (s8)gPlayerFacingPosition.height) + if (MapGridGetElevationAt(x, y) == (s8)gPlayerFacingPosition.height) { tileBehavior = MapGridGetMetatileBehaviorAt(x, y); if (MetatileBehavior_IsCuttableGrass(tileBehavior) == TRUE) diff --git a/src/fldeff_escalator.c b/src/fldeff_escalator.c index d2c9fe3c7..d0010e67c 100644 --- a/src/fldeff_escalator.c +++ b/src/fldeff_escalator.c @@ -110,13 +110,13 @@ void sub_80B4710(u8 taskId) sub_80B45B4(taskId, sEscalatorMetatiles_1F_1, 0); break; case 2: - sub_80B45B4(taskId, sEscalatorMetatiles_1F_2, (COLLISION_DIR_ALL << METATILE_COLLISION_SHIFT)); + sub_80B45B4(taskId, sEscalatorMetatiles_1F_2, MAPGRID_COLLISION_MASK); break; case 3: sub_80B45B4(taskId, sEscalatorMetatiles_1F_3, 0); break; case 4: - sub_80B45B4(taskId, sEscalatorMetatiles_2F_0, (COLLISION_DIR_ALL << METATILE_COLLISION_SHIFT)); + sub_80B45B4(taskId, sEscalatorMetatiles_2F_0, MAPGRID_COLLISION_MASK); break; case 5: sub_80B45B4(taskId, sEscalatorMetatiles_2F_1, 0); diff --git a/src/item_use.c b/src/item_use.c index 3920ae5bd..a6defcfee 100644 --- a/src/item_use.c +++ b/src/item_use.c @@ -239,7 +239,7 @@ bool32 CanFish(void) } else { - if (MetatileBehavior_IsSurfableWaterOrUnderwater(tileBehavior) && !MapGridIsImpassableAt(x, y)) + if (MetatileBehavior_IsSurfableWaterOrUnderwater(tileBehavior) && MapGridGetCollisionAt(x, y) == 0) return TRUE; if (MetatileBehavior_IsBridge(tileBehavior) == TRUE) return TRUE; @@ -326,7 +326,7 @@ void ExitItemfinder(u8 taskId) DestroyTask(taskId); } -bool8 ItemfinderCheckForHiddenItems(struct MapEvents *events, u8 taskId) +bool8 ItemfinderCheckForHiddenItems(const struct MapEvents *events, u8 taskId) { int distanceX, distanceY; u16 x, y; @@ -359,10 +359,10 @@ bool8 ItemfinderCheckForHiddenItems(struct MapEvents *events, u8 taskId) return FALSE; } -bool8 HiddenItemAtPos(struct MapEvents *events, s16 x, s16 y) +bool8 HiddenItemAtPos(const struct MapEvents *events, s16 x, s16 y) { u8 bgEventCount = events->bgEventCount; - struct BgEvent *bgEvent = events->bgEvents; + const struct BgEvent *bgEvent = events->bgEvents; int i; for (i = 0; i < bgEventCount; i++) @@ -378,14 +378,14 @@ bool8 HiddenItemAtPos(struct MapEvents *events, s16 x, s16 y) return FALSE; } -bool8 sub_80C9688(struct MapConnection *connection, int x, int y) +bool8 sub_80C9688(const struct MapConnection *connection, int x, int y) { - struct MapHeader *mapHeader; + const struct MapHeader *mapHeader; u16 localX, localY; u32 localOffset; s32 localLength; - mapHeader = mapconnection_get_mapheader(connection); + mapHeader = GetMapHeaderFromConnection(connection); switch (connection->direction) { @@ -441,7 +441,7 @@ void sub_80C9720(u8 taskId) || var2 > curY || curY >= height) { - struct MapConnection *conn = sub_8056BA0(curX, curY); + const struct MapConnection *conn = GetMapConnectionAtPos(curX, curY); if (conn && sub_80C9688(conn, curX, curY) == TRUE) sub_80C9838(taskId, curX - x, curY - y); } diff --git a/src/overworld.c b/src/overworld.c index 6185020ea..682de48b4 100644 --- a/src/overworld.c +++ b/src/overworld.c @@ -352,8 +352,8 @@ void Overworld_SetObjEventTemplateMovementType(u8 localId, u8 movementType) static void mapdata_load_assets_to_gpu_and_full_redraw(void) { move_tilemap_camera_to_upper_left_corner(); - copy_map_tileset1_tileset2_to_vram(gMapHeader.mapLayout); - apply_map_tileset1_tileset2_palette(gMapHeader.mapLayout); + CopyMapTilesetsToVram(gMapHeader.mapLayout); + LoadMapTilesetPalettes(gMapHeader.mapLayout); DrawWholeMapView(); cur_mapheader_run_tileset_funcs_after_some_cpuset(); } @@ -602,7 +602,7 @@ bool8 SetDiveWarpDive(u16 x, u16 y) return SetDiveWarp(CONNECTION_DIVE, x, y); } -void sub_80538F0(u8 mapGroup, u8 mapNum) +void LoadMapFromCameraTransition(u8 mapGroup, u8 mapNum) { s32 paletteIndex; @@ -621,9 +621,9 @@ void sub_80538F0(u8 mapGroup, u8 mapNum) SetDefaultFlashLevel(); Overworld_ClearSavedMusic(); RunOnTransitionMapScript(); - not_trainer_hill_battle_pyramid(); - sub_8056D38(gMapHeader.mapLayout); - apply_map_tileset2_palette(gMapHeader.mapLayout); + InitMap(); + CopySecondaryTilesetToVram(gMapHeader.mapLayout); + LoadSecondaryTilesetPalette(gMapHeader.mapLayout); for (paletteIndex = 6; paletteIndex < 12; paletteIndex++) ApplyWeatherGammaShiftToPal(paletteIndex); @@ -661,11 +661,11 @@ void sub_8053994(u32 a1) RunOnTransitionMapScript(); UpdateLocationHistoryForRoamer(); RoamerMoveToOtherLocationSet(); - not_trainer_hill_battle_pyramid(); + InitMap(); if (a1 != 1 && v3) { UpdateTVScreensOnMap(gBackupMapLayout.width, gBackupMapLayout.height); - sub_80BBCCC(1); + InitSecretBaseAppearance(TRUE); } } @@ -1476,7 +1476,7 @@ void CB2_ContinueSavedGame(void) UnfreezeObjectEvents(); DoTimeBasedEvents(); sub_805308C(); - sub_8055FC0(); + InitMapFromSavedGame(); PlayTimeCounter_Start(); ScriptContext1_Init(); ScriptContext2_Disable(); @@ -1570,15 +1570,15 @@ static bool32 sub_805483C(u8 *state) (*state)++; break; case 6: - sub_8056D28(gMapHeader.mapLayout); + CopyPrimaryTilesetToVram(gMapHeader.mapLayout); (*state)++; break; case 7: - sub_8056D38(gMapHeader.mapLayout); + CopySecondaryTilesetToVram(gMapHeader.mapLayout); (*state)++; break; case 8: - apply_map_tileset1_tileset2_palette(gMapHeader.mapLayout); + LoadMapTilesetPalettes(gMapHeader.mapLayout); (*state)++; break; case 9: @@ -1636,15 +1636,15 @@ bool32 sub_805493C(u8 *state, u32 a2) (*state)++; break; case 6: - sub_8056D28(gMapHeader.mapLayout); + CopyPrimaryTilesetToVram(gMapHeader.mapLayout); (*state)++; break; case 7: - sub_8056D38(gMapHeader.mapLayout); + CopySecondaryTilesetToVram(gMapHeader.mapLayout); (*state)++; break; case 8: - apply_map_tileset1_tileset2_palette(gMapHeader.mapLayout); + LoadMapTilesetPalettes(gMapHeader.mapLayout); (*state)++; break; case 9: @@ -1726,15 +1726,15 @@ bool32 sub_8054A9C(u8 *state) (*state)++; break; case 5: - sub_8056D28(gMapHeader.mapLayout); + CopyPrimaryTilesetToVram(gMapHeader.mapLayout); (*state)++; break; case 6: - sub_8056D38(gMapHeader.mapLayout); + CopySecondaryTilesetToVram(gMapHeader.mapLayout); (*state)++; break; case 7: - apply_map_tileset1_tileset2_palette(gMapHeader.mapLayout); + LoadMapTilesetPalettes(gMapHeader.mapLayout); (*state)++; break; case 8: @@ -1848,7 +1848,7 @@ void mli4_mapscripts_and_other(void) gTotalCameraPixelOffsetX = 0; gTotalCameraPixelOffsetY = 0; ResetObjectEvents(); - sav1_camera_get_focus_coords(&x, &y); + GetCameraFocusCoords(&x, &y); initialPlayerAvatarState = GetInitialPlayerAvatarState(); InitPlayerAvatar(x, y, initialPlayerAvatarState->direction, gSaveBlock2.playerGender); SetPlayerAvatarTransitionFlags(initialPlayerAvatarState->transitionFlags); @@ -1883,8 +1883,8 @@ void sub_8054E7C(void) void sub_8054E98(void) { u16 x, y; - sav1_camera_get_focus_coords(&x, &y); - sub_8056C50(x + gUnknown_03004860, y); + GetCameraFocusCoords(&x, &y); + SetCameraFocusCoords(x + gUnknown_03004860, y); } void sub_8054EC8(void) @@ -1892,7 +1892,7 @@ void sub_8054EC8(void) u16 i; u16 x, y; - sav1_camera_get_focus_coords(&x, &y); + GetCameraFocusCoords(&x, &y); x -= gUnknown_03004860; for (i = 0; i < gFieldLinkPlayerCount; i++) @@ -2312,7 +2312,7 @@ bool32 sub_8055630(struct UnkStruct_8054FF8 *a1) return FALSE; } -u8 *sub_8055648(struct UnkStruct_8054FF8 *a1) +const u8 *sub_8055648(struct UnkStruct_8054FF8 *a1) { if (a1->c != 2) return 0; @@ -2720,7 +2720,7 @@ static u8 LinkPlayerDetectCollision(u8 selfObjEventId, u8 a2, s16 x, s16 y) } } } - return MapGridIsImpassableAt(x, y); + return MapGridGetCollisionAt(x, y); } static void CreateLinkPlayerSprite(u8 linkPlayerId) diff --git a/src/rotating_gate.c b/src/rotating_gate.c index cf4b4daf4..934a7beb1 100644 --- a/src/rotating_gate.c +++ b/src/rotating_gate.c @@ -906,7 +906,7 @@ int RotatingGate_CanRotate(u8 gateId, int rotationDirection) if (sRotatingGate_ArmLayout[shape][i * 2 + j]) { - if (MapGridIsImpassableAt(x + armPos[armIndex].deltaX, y + armPos[armIndex].deltaY) == 1) + if (MapGridGetCollisionAt(x + armPos[armIndex].deltaX, y + armPos[armIndex].deltaY) == 1) return 0; } } diff --git a/src/scrcmd.c b/src/scrcmd.c index 1b6aea42f..0fb970642 100644 --- a/src/scrcmd.c +++ b/src/scrcmd.c @@ -1909,7 +1909,7 @@ bool8 ScrCmd_setmetatile(struct ScriptContext *ctx) if (!impassable) MapGridSetMetatileIdAt(x, y, metatileId); else - MapGridSetMetatileIdAt(x, y, metatileId | (COLLISION_DIR_ALL << METATILE_COLLISION_SHIFT)); + MapGridSetMetatileIdAt(x, y, metatileId | MAPGRID_COLLISION_MASK); return FALSE; } diff --git a/src/script.c b/src/script.c index cd4816a52..96cb86bf1 100644 --- a/src/script.c +++ b/src/script.c @@ -239,7 +239,7 @@ void ScriptContext2_RunNewScript(const u8 *ptr) static u8 *mapheader_get_tagged_pointer(u8 tag) { - u8 *mapScripts = gMapHeader.mapScripts; + const u8 *mapScripts = gMapHeader.mapScripts; if (mapScripts == NULL) return NULL; diff --git a/src/secret_base.c b/src/secret_base.c index 61af33926..6762e4e58 100644 --- a/src/secret_base.c +++ b/src/secret_base.c @@ -324,7 +324,7 @@ void sub_80BB8CC(void) VarSet(VAR_SECRET_BASE_MAP, gMapHeader.regionMapSectionId); } -void sub_80BB970(struct MapEvents *events) +void SetOccupiedSecretBaseEntranceMetatiles(const struct MapEvents *events) { u16 bgevidx, idx, jdx; s16 tile_id; @@ -450,7 +450,7 @@ bool8 CurrentMapIsSecretBase(void) return FALSE; } -void sub_80BBCCC(u8 flagIn) +void InitSecretBaseAppearance(u8 flagIn) { u16 curBaseId; u16 x, y; @@ -546,7 +546,7 @@ void SetSecretBaseOwnerGfxId(void) VarSet(VAR_OBJ_GFX_ID_F, sSecretBaseOwnerGfxIds[GetSecretBaseOwnerType(curBase)]); } -void SetCurrentSecretBaseFromPosition(struct MapPosition *position, struct MapEvents *events) +void SetCurrentSecretBaseFromPosition(struct MapPosition *position, const struct MapEvents *events) { s16 i; @@ -561,7 +561,7 @@ void SetCurrentSecretBaseFromPosition(struct MapPosition *position, struct MapEv } } -void sub_80BC038(struct MapPosition *position, struct MapEvents *events) +void sub_80BC038(struct MapPosition *position, const struct MapEvents *events) { SetCurrentSecretBaseFromPosition(position, events); SetCurrentSecretBaseVar(); @@ -726,7 +726,7 @@ void MoveOutOfSecretBase(void) void sub_80BC474(void) { u16 eventId; - struct MapEvents *mapEvents = gMapHeader.events; + const struct MapEvents *mapEvents = gMapHeader.events; for (eventId = 0; eventId < mapEvents->bgEventCount; eventId++) { diff --git a/src/start_menu.c b/src/start_menu.c index c87ad6330..6dc024c1b 100644 --- a/src/start_menu.c +++ b/src/start_menu.c @@ -597,7 +597,7 @@ static u8 SaveCallback2(void) static void sub_807160C(void) { - save_serialize_map(); + SaveMapView(); saveDialogCallback = SaveDialogCB_DisplayConfirmMessage; savingComplete = FALSE; } diff --git a/src/tv.c b/src/tv.c index 7c6c86418..19a2765d5 100644 --- a/src/tv.c +++ b/src/tv.c @@ -567,7 +567,7 @@ void SetTVMetatilesOnMap(int width, int height, u16 tileId) for (x=0; x