Skip to content

Commit

Permalink
Sync fieldmap
Browse files Browse the repository at this point in the history
  • Loading branch information
GriffinRichards committed May 9, 2023
1 parent af019d6 commit 28b53dc
Show file tree
Hide file tree
Showing 31 changed files with 515 additions and 595 deletions.
2 changes: 2 additions & 0 deletions include/constants/metatile_behaviors.h
Original file line number Diff line number Diff line change
Expand Up @@ -242,4 +242,6 @@
#define MB_UNUSED_EE 0xEE
#define MB_UNUSED_EF 0xEF

#define MB_INVALID 0xFF

#endif // GUARD_METATILE_BEHAVIORS
2 changes: 1 addition & 1 deletion include/event_object_movement.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion include/field_control_avatar.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
72 changes: 35 additions & 37 deletions include/fieldmap.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,51 +2,49 @@
#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
#define NUM_PALS_IN_PRIMARY 6
#define NUM_PALS_TOTAL 12
#define MAX_MAP_DATA_SIZE 0x2800

#define NUM_TILES_PER_METATILE 8

// 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;
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);

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*/);
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
6 changes: 6 additions & 0 deletions include/gba/defines.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
82 changes: 49 additions & 33 deletions include/global.fieldmap.h
Original file line number Diff line number Diff line change
@@ -1,42 +1,59 @@
#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;
};

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
Expand All @@ -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;
Expand All @@ -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;
};

Expand All @@ -78,8 +95,7 @@ struct CoordEvent
u8 elevation;
u16 trigger;
u16 index;
u8 filler_A[0x2];
u8 *script;
const u8 *script;
};

struct BgEvent
Expand All @@ -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;
Expand All @@ -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
Expand All @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion include/item_use.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions include/overworld.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
11 changes: 11 additions & 0 deletions include/palette.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
8 changes: 4 additions & 4 deletions include/secret_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
12 changes: 6 additions & 6 deletions src/braille_puzzles.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions src/debug/start_menu_debug.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
}
Expand Down
Loading

0 comments on commit 28b53dc

Please sign in to comment.