pokeemerald/src/fieldmap.c

941 lines
27 KiB
C
Raw Normal View History

2018-05-09 12:07:56 +02:00
#include "global.h"
2019-02-07 03:01:29 +01:00
#include "battle_pyramid.h"
2018-05-09 12:07:56 +02:00
#include "bg.h"
#include "fieldmap.h"
2018-12-24 22:28:33 +01:00
#include "fldeff.h"
2018-12-20 04:19:54 +01:00
#include "fldeff_misc.h"
2018-11-14 01:01:50 +01:00
#include "frontier_util.h"
2018-05-09 12:07:56 +02:00
#include "menu.h"
#include "mirage_tower.h"
2018-11-14 01:01:50 +01:00
#include "overworld.h"
2018-05-09 12:07:56 +02:00
#include "palette.h"
#include "pokenav.h"
#include "script.h"
#include "secret_base.h"
2019-01-13 20:50:08 +01:00
#include "trainer_hill.h"
2018-05-09 12:07:56 +02:00
#include "tv.h"
2018-11-14 01:01:50 +01:00
#include "constants/rgb.h"
2021-04-06 22:05:43 +02:00
#include "constants/metatile_behaviors.h"
2018-05-09 12:07:56 +02:00
struct ConnectionFlags
{
u8 south:1;
u8 north:1;
u8 west:1;
u8 east:1;
};
2022-05-17 19:51:54 +02:00
EWRAM_DATA static u16 sBackupMapData[MAX_MAP_DATA_SIZE] = {0};
2018-05-09 12:07:56 +02:00
EWRAM_DATA struct MapHeader gMapHeader = {0};
EWRAM_DATA struct Camera gCamera = {0};
2022-05-17 19:51:54 +02:00
EWRAM_DATA static struct ConnectionFlags sMapConnectionFlags = {0};
EWRAM_DATA static u32 sFiller = 0; // without this, the next file won't align properly
2018-05-09 12:07:56 +02:00
2018-12-28 18:18:23 +01:00
struct BackupMapLayout gBackupMapLayout;
2018-05-09 12:07:56 +02:00
static const struct ConnectionFlags sDummyConnectionFlags = {0};
2018-12-28 18:18:23 +01:00
static void InitMapLayoutData(struct MapHeader *mapHeader);
2023-03-08 17:07:44 +01:00
static void InitBackupMapLayoutData(const u16 *map, u16 width, u16 height);
2018-12-28 18:18:23 +01:00
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 bool8 SkipCopyingMetatileFromSavedMap(u16 *mapBlock, u16 mapWidth, u8 yMode);
2023-03-08 17:07:44 +01:00
static const struct MapConnection *GetIncomingConnection(u8 direction, int x, int y);
static bool8 IsPosInIncomingConnectingMap(u8 direction, int x, int y, const struct MapConnection *connection);
2021-04-06 22:05:43 +02:00
static bool8 IsCoordInIncomingConnectingMap(int coord, int srcMax, int destMax, int offset);
2018-12-28 18:18:23 +01:00
2022-01-19 16:15:32 +01:00
#define GetBorderBlockAt(x, y)({ \
2021-04-07 19:26:02 +02:00
u16 block; \
int i; \
2023-03-08 17:07:44 +01:00
const u16 *border = gMapHeader.mapLayout->border; /* Unused, they read it again below */ \
2021-04-07 19:26:02 +02:00
\
i = (x + 1) & 1; \
i += ((y + 1) & 1) * 2; \
\
2022-01-19 16:15:32 +01:00
block = gMapHeader.mapLayout->border[i] | MAPGRID_COLLISION_MASK; \
2021-04-07 19:26:02 +02:00
})
#define AreCoordsWithinMapGridBounds(x, y) (x >= 0 && x < gBackupMapLayout.width && y >= 0 && y < gBackupMapLayout.height)
2022-01-19 16:15:32 +01:00
#define GetMapGridBlockAt(x, y) (AreCoordsWithinMapGridBounds(x, y) ? gBackupMapLayout.map[x + gBackupMapLayout.width * y] : GetBorderBlockAt(x, y))
2021-04-07 19:26:02 +02:00
2023-03-08 17:07:44 +01:00
const struct MapHeader *const GetMapHeaderFromConnection(const struct MapConnection *connection)
2018-05-09 12:07:56 +02:00
{
return Overworld_GetMapHeaderByGroupAndId(connection->mapGroup, connection->mapNum);
}
2018-12-28 18:18:23 +01:00
void InitMap(void)
2018-05-09 12:07:56 +02:00
{
2018-12-28 18:18:23 +01:00
InitMapLayoutData(&gMapHeader);
2019-04-05 23:11:24 +02:00
SetOccupiedSecretBaseEntranceMetatiles(gMapHeader.events);
RunOnLoadMapScript();
2018-05-09 12:07:56 +02:00
}
2018-12-28 18:18:23 +01:00
void InitMapFromSavedGame(void)
2018-05-09 12:07:56 +02:00
{
2018-12-28 18:18:23 +01:00
InitMapLayoutData(&gMapHeader);
2019-04-05 23:11:24 +02:00
InitSecretBaseAppearance(FALSE);
SetOccupiedSecretBaseEntranceMetatiles(gMapHeader.events);
2018-12-28 18:18:23 +01:00
LoadSavedMapView();
RunOnLoadMapScript();
2018-12-28 18:18:23 +01:00
UpdateTVScreensOnMap(gBackupMapLayout.width, gBackupMapLayout.height);
2018-05-09 12:07:56 +02:00
}
2019-02-07 03:01:29 +01:00
void InitBattlePyramidMap(bool8 setPlayerPosition)
2018-05-09 12:07:56 +02:00
{
CpuFastFill16(MAPGRID_UNDEFINED, sBackupMapData, sizeof(sBackupMapData));
2022-05-17 19:51:54 +02:00
GenerateBattlePyramidFloorLayout(sBackupMapData, setPlayerPosition);
2018-05-09 12:07:56 +02:00
}
2018-12-28 18:18:23 +01:00
void InitTrainerHillMap(void)
2018-05-09 12:07:56 +02:00
{
CpuFastFill16(MAPGRID_UNDEFINED, sBackupMapData, sizeof(sBackupMapData));
2022-05-17 19:51:54 +02:00
GenerateTrainerHillFloorLayout(sBackupMapData);
2018-05-09 12:07:56 +02:00
}
2018-12-28 18:18:23 +01:00
static void InitMapLayoutData(struct MapHeader *mapHeader)
2018-05-09 12:07:56 +02:00
{
2018-06-21 00:41:51 +02:00
struct MapLayout const *mapLayout;
2018-05-09 12:07:56 +02:00
int width;
int height;
2018-06-21 00:41:51 +02:00
mapLayout = mapHeader->mapLayout;
2022-05-17 19:51:54 +02:00
CpuFastFill16(MAPGRID_UNDEFINED, sBackupMapData, sizeof(sBackupMapData));
gBackupMapLayout.map = sBackupMapData;
2021-10-09 18:12:18 +02:00
width = mapLayout->width + MAP_OFFSET_W;
2018-12-28 18:18:23 +01:00
gBackupMapLayout.width = width;
2021-10-09 18:12:18 +02:00
height = mapLayout->height + MAP_OFFSET_H;
2018-12-28 18:18:23 +01:00
gBackupMapLayout.height = height;
if (width * height <= MAX_MAP_DATA_SIZE)
2018-05-09 12:07:56 +02:00
{
2018-12-28 18:18:23 +01:00
InitBackupMapLayoutData(mapLayout->map, mapLayout->width, mapLayout->height);
InitBackupMapLayoutConnections(mapHeader);
2018-05-09 12:07:56 +02:00
}
}
2023-03-08 17:07:44 +01:00
static void InitBackupMapLayoutData(const u16 *map, u16 width, u16 height)
2018-05-09 12:07:56 +02:00
{
u16 *dest;
int y;
2018-12-28 18:18:23 +01:00
dest = gBackupMapLayout.map;
2021-10-09 18:12:18 +02:00
dest += gBackupMapLayout.width * 7 + MAP_OFFSET;
2018-05-09 12:07:56 +02:00
for (y = 0; y < height; y++)
{
CpuCopy16(map, dest, width * 2);
2021-10-09 18:12:18 +02:00
dest += width + MAP_OFFSET_W;
2018-05-09 12:07:56 +02:00
map += width;
}
}
2018-12-28 18:18:23 +01:00
static void InitBackupMapLayoutConnections(struct MapHeader *mapHeader)
2018-05-09 12:07:56 +02:00
{
int count;
2023-03-08 17:07:44 +01:00
const struct MapConnection *connection;
2018-05-09 12:07:56 +02:00
int i;
if (mapHeader->connections)
{
count = mapHeader->connections->count;
connection = mapHeader->connections->connections;
2022-05-17 19:51:54 +02:00
sMapConnectionFlags = sDummyConnectionFlags;
2018-05-09 12:07:56 +02:00
for (i = 0; i < count; i++, connection++)
{
struct MapHeader const *cMap = GetMapHeaderFromConnection(connection);
2018-05-09 12:07:56 +02:00
u32 offset = connection->offset;
switch (connection->direction)
{
case CONNECTION_SOUTH:
2018-12-28 18:18:23 +01:00
FillSouthConnection(mapHeader, cMap, offset);
2022-05-17 19:51:54 +02:00
sMapConnectionFlags.south = TRUE;
2018-05-09 12:07:56 +02:00
break;
case CONNECTION_NORTH:
2018-12-28 18:18:23 +01:00
FillNorthConnection(mapHeader, cMap, offset);
2022-05-17 19:51:54 +02:00
sMapConnectionFlags.north = TRUE;
2018-05-09 12:07:56 +02:00
break;
case CONNECTION_WEST:
2018-12-28 18:18:23 +01:00
FillWestConnection(mapHeader, cMap, offset);
2022-05-17 19:51:54 +02:00
sMapConnectionFlags.west = TRUE;
2018-05-09 12:07:56 +02:00
break;
case CONNECTION_EAST:
2018-12-28 18:18:23 +01:00
FillEastConnection(mapHeader, cMap, offset);
2022-05-17 19:51:54 +02:00
sMapConnectionFlags.east = TRUE;
2018-05-09 12:07:56 +02:00
break;
}
}
}
}
2021-04-06 22:05:43 +02:00
static void FillConnection(int x, int y, struct MapHeader const *connectedMapHeader, int x2, int y2, int width, int height)
2018-05-09 12:07:56 +02:00
{
int i;
2023-03-08 17:07:44 +01:00
const u16 *src;
2018-05-09 12:07:56 +02:00
u16 *dest;
int mapWidth;
2018-12-28 18:18:23 +01:00
mapWidth = connectedMapHeader->mapLayout->width;
src = &connectedMapHeader->mapLayout->map[mapWidth * y2 + x2];
dest = &gBackupMapLayout.map[gBackupMapLayout.width * y + x];
2018-05-09 12:07:56 +02:00
for (i = 0; i < height; i++)
{
CpuCopy16(src, dest, width * 2);
2018-12-28 18:18:23 +01:00
dest += gBackupMapLayout.width;
2018-05-09 12:07:56 +02:00
src += mapWidth;
}
}
2018-12-28 18:18:23 +01:00
static void FillSouthConnection(struct MapHeader const *mapHeader, struct MapHeader const *connectedMapHeader, s32 offset)
2018-05-09 12:07:56 +02:00
{
int x, y;
int x2;
int width;
int cWidth;
if (connectedMapHeader)
{
2018-06-21 00:41:51 +02:00
cWidth = connectedMapHeader->mapLayout->width;
2021-10-09 18:12:18 +02:00
x = offset + MAP_OFFSET;
y = mapHeader->mapLayout->height + MAP_OFFSET;
2018-05-09 12:07:56 +02:00
if (x < 0)
{
x2 = -x;
x += cWidth;
2018-12-28 18:18:23 +01:00
if (x < gBackupMapLayout.width)
2018-05-09 12:07:56 +02:00
width = x;
else
2018-12-28 18:18:23 +01:00
width = gBackupMapLayout.width;
2018-05-09 12:07:56 +02:00
x = 0;
}
else
{
x2 = 0;
2018-12-28 18:18:23 +01:00
if (x + cWidth < gBackupMapLayout.width)
2018-05-09 12:07:56 +02:00
width = cWidth;
else
2018-12-28 18:18:23 +01:00
width = gBackupMapLayout.width - x;
2018-05-09 12:07:56 +02:00
}
2021-04-06 22:05:43 +02:00
FillConnection(
2018-05-09 12:07:56 +02:00
x, y,
connectedMapHeader,
x2, /*y2*/ 0,
2021-10-09 18:12:18 +02:00
width, /*height*/ MAP_OFFSET);
2018-05-09 12:07:56 +02:00
}
}
2018-12-28 18:18:23 +01:00
static void FillNorthConnection(struct MapHeader const *mapHeader, struct MapHeader const *connectedMapHeader, s32 offset)
2018-05-09 12:07:56 +02:00
{
int x;
int x2, y2;
int width;
int cWidth, cHeight;
if (connectedMapHeader)
{
2018-06-21 00:41:51 +02:00
cWidth = connectedMapHeader->mapLayout->width;
cHeight = connectedMapHeader->mapLayout->height;
2021-10-09 18:12:18 +02:00
x = offset + MAP_OFFSET;
y2 = cHeight - MAP_OFFSET;
2018-05-09 12:07:56 +02:00
if (x < 0)
{
x2 = -x;
x += cWidth;
2018-12-28 18:18:23 +01:00
if (x < gBackupMapLayout.width)
2018-05-09 12:07:56 +02:00
width = x;
else
2018-12-28 18:18:23 +01:00
width = gBackupMapLayout.width;
2018-05-09 12:07:56 +02:00
x = 0;
}
else
{
x2 = 0;
2018-12-28 18:18:23 +01:00
if (x + cWidth < gBackupMapLayout.width)
2018-05-09 12:07:56 +02:00
width = cWidth;
else
2018-12-28 18:18:23 +01:00
width = gBackupMapLayout.width - x;
2018-05-09 12:07:56 +02:00
}
2021-04-06 22:05:43 +02:00
FillConnection(
2018-05-09 12:07:56 +02:00
x, /*y*/ 0,
connectedMapHeader,
x2, y2,
2021-10-09 18:12:18 +02:00
width, /*height*/ MAP_OFFSET);
2018-05-09 12:07:56 +02:00
}
}
2018-12-28 18:18:23 +01:00
static void FillWestConnection(struct MapHeader const *mapHeader, struct MapHeader const *connectedMapHeader, s32 offset)
2018-05-09 12:07:56 +02:00
{
int y;
int x2, y2;
int height;
int cWidth, cHeight;
if (connectedMapHeader)
{
2018-06-21 00:41:51 +02:00
cWidth = connectedMapHeader->mapLayout->width;
cHeight = connectedMapHeader->mapLayout->height;
2021-10-09 18:12:18 +02:00
y = offset + MAP_OFFSET;
x2 = cWidth - MAP_OFFSET;
2018-05-09 12:07:56 +02:00
if (y < 0)
{
y2 = -y;
2018-12-28 18:18:23 +01:00
if (y + cHeight < gBackupMapLayout.height)
2018-05-09 12:07:56 +02:00
height = y + cHeight;
else
2018-12-28 18:18:23 +01:00
height = gBackupMapLayout.height;
2018-05-09 12:07:56 +02:00
y = 0;
}
else
{
y2 = 0;
2018-12-28 18:18:23 +01:00
if (y + cHeight < gBackupMapLayout.height)
2018-05-09 12:07:56 +02:00
height = cHeight;
else
2018-12-28 18:18:23 +01:00
height = gBackupMapLayout.height - y;
2018-05-09 12:07:56 +02:00
}
2021-04-06 22:05:43 +02:00
FillConnection(
2018-05-09 12:07:56 +02:00
/*x*/ 0, y,
connectedMapHeader,
x2, y2,
2021-10-09 18:12:18 +02:00
/*width*/ MAP_OFFSET, height);
2018-05-09 12:07:56 +02:00
}
}
2018-12-28 18:18:23 +01:00
static void FillEastConnection(struct MapHeader const *mapHeader, struct MapHeader const *connectedMapHeader, s32 offset)
2018-05-09 12:07:56 +02:00
{
int x, y;
int y2;
int height;
int cHeight;
if (connectedMapHeader)
{
2018-06-21 00:41:51 +02:00
cHeight = connectedMapHeader->mapLayout->height;
2021-10-09 18:12:18 +02:00
x = mapHeader->mapLayout->width + MAP_OFFSET;
y = offset + MAP_OFFSET;
2018-05-09 12:07:56 +02:00
if (y < 0)
{
y2 = -y;
2018-12-28 18:18:23 +01:00
if (y + cHeight < gBackupMapLayout.height)
2018-05-09 12:07:56 +02:00
height = y + cHeight;
else
2018-12-28 18:18:23 +01:00
height = gBackupMapLayout.height;
2018-05-09 12:07:56 +02:00
y = 0;
}
else
{
y2 = 0;
2018-12-28 18:18:23 +01:00
if (y + cHeight < gBackupMapLayout.height)
2018-05-09 12:07:56 +02:00
height = cHeight;
else
2018-12-28 18:18:23 +01:00
height = gBackupMapLayout.height - y;
2018-05-09 12:07:56 +02:00
}
2021-04-06 22:05:43 +02:00
FillConnection(
2018-05-09 12:07:56 +02:00
x, y,
connectedMapHeader,
/*x2*/ 0, y2,
2021-10-09 18:12:18 +02:00
/*width*/ MAP_OFFSET + 1, height);
2018-05-09 12:07:56 +02:00
}
}
2022-01-21 18:48:19 +01:00
u8 MapGridGetElevationAt(int x, int y)
2018-05-09 12:07:56 +02:00
{
2022-01-19 16:15:32 +01:00
u16 block = GetMapGridBlockAt(x, y);
2018-05-09 12:07:56 +02:00
2022-01-19 16:15:32 +01:00
if (block == MAPGRID_UNDEFINED)
2018-05-09 12:07:56 +02:00
return 0;
2018-07-07 14:24:19 +02:00
2022-01-19 16:15:32 +01:00
return block >> MAPGRID_ELEVATION_SHIFT;
2018-05-09 12:07:56 +02:00
}
u8 MapGridGetCollisionAt(int x, int y)
2018-05-09 12:07:56 +02:00
{
2022-01-19 16:15:32 +01:00
u16 block = GetMapGridBlockAt(x, y);
2018-05-09 12:07:56 +02:00
2022-01-19 16:15:32 +01:00
if (block == MAPGRID_UNDEFINED)
2021-04-07 19:26:02 +02:00
return TRUE;
2022-01-19 16:15:32 +01:00
return (block & MAPGRID_COLLISION_MASK) >> MAPGRID_COLLISION_SHIFT;
2018-05-09 12:07:56 +02:00
}
u32 MapGridGetMetatileIdAt(int x, int y)
{
2022-01-19 16:15:32 +01:00
u16 block = GetMapGridBlockAt(x, y);
2018-05-09 12:07:56 +02:00
2022-01-19 16:15:32 +01:00
if (block == MAPGRID_UNDEFINED)
return GetBorderBlockAt(x, y) & MAPGRID_METATILE_ID_MASK;
2021-04-07 19:26:02 +02:00
2022-01-19 16:15:32 +01:00
return block & MAPGRID_METATILE_ID_MASK;
2018-05-09 12:07:56 +02:00
}
u32 MapGridGetMetatileBehaviorAt(int x, int y)
{
2021-04-07 19:26:02 +02:00
u16 metatile = MapGridGetMetatileIdAt(x, y);
2022-01-19 16:15:32 +01:00
return GetMetatileAttributesById(metatile) & METATILE_ATTR_BEHAVIOR_MASK;
2018-05-09 12:07:56 +02:00
}
u8 MapGridGetMetatileLayerTypeAt(int x, int y)
{
2021-04-07 19:26:02 +02:00
u16 metatile = MapGridGetMetatileIdAt(x, y);
2022-01-19 16:15:32 +01:00
return (GetMetatileAttributesById(metatile) & METATILE_ATTR_LAYER_MASK) >> METATILE_ATTR_LAYER_SHIFT;
2018-05-09 12:07:56 +02:00
}
void MapGridSetMetatileIdAt(int x, int y, u16 metatile)
{
int i;
2021-04-07 19:26:02 +02:00
if (AreCoordsWithinMapGridBounds(x, y))
2018-05-09 12:07:56 +02:00
{
2018-12-28 18:18:23 +01:00
i = x + y * gBackupMapLayout.width;
2022-01-19 16:15:32 +01:00
gBackupMapLayout.map[i] = (gBackupMapLayout.map[i] & MAPGRID_ELEVATION_MASK) | (metatile & ~MAPGRID_ELEVATION_MASK);
2018-05-09 12:07:56 +02:00
}
}
void MapGridSetMetatileEntryAt(int x, int y, u16 metatile)
{
int i;
2021-04-07 19:26:02 +02:00
if (AreCoordsWithinMapGridBounds(x, y))
2018-05-09 12:07:56 +02:00
{
2018-12-28 18:18:23 +01:00
i = x + gBackupMapLayout.width * y;
gBackupMapLayout.map[i] = metatile;
2018-05-09 12:07:56 +02:00
}
}
2022-01-19 16:15:32 +01:00
u16 GetMetatileAttributesById(u16 metatile)
2018-05-09 12:07:56 +02:00
{
2022-09-27 21:15:32 +02:00
const u16 *attributes;
if (metatile < NUM_METATILES_IN_PRIMARY)
2018-05-09 12:07:56 +02:00
{
2018-06-21 00:41:51 +02:00
attributes = gMapHeader.mapLayout->primaryTileset->metatileAttributes;
2018-05-09 12:07:56 +02:00
return attributes[metatile];
}
else if (metatile < NUM_METATILES_TOTAL)
2018-05-09 12:07:56 +02:00
{
2018-06-21 00:41:51 +02:00
attributes = gMapHeader.mapLayout->secondaryTileset->metatileAttributes;
return attributes[metatile - NUM_METATILES_IN_PRIMARY];
2018-05-09 12:07:56 +02:00
}
else
{
2021-04-06 22:05:43 +02:00
return MB_INVALID;
2018-05-09 12:07:56 +02:00
}
}
2021-04-06 22:05:43 +02:00
void SaveMapView(void)
2018-05-09 12:07:56 +02:00
{
int i, j;
int x, y;
u16 *mapView;
int width;
mapView = gSaveBlock1Ptr->mapView;
2018-12-28 18:18:23 +01:00
width = gBackupMapLayout.width;
2018-05-09 12:07:56 +02:00
x = gSaveBlock1Ptr->pos.x;
y = gSaveBlock1Ptr->pos.y;
2021-10-09 18:12:18 +02:00
for (i = y; i < y + MAP_OFFSET_H; i++)
2018-05-09 12:07:56 +02:00
{
2021-10-09 18:12:18 +02:00
for (j = x; j < x + MAP_OFFSET_W; j++)
2022-05-17 19:51:54 +02:00
*mapView++ = sBackupMapData[width * i + j];
2018-05-09 12:07:56 +02:00
}
}
2018-12-28 18:18:23 +01:00
static bool32 SavedMapViewIsEmpty(void)
2018-05-09 12:07:56 +02:00
{
u16 i;
2018-12-28 18:18:23 +01:00
u32 marker = 0;
#ifndef UBFIX
2018-12-28 18:18:23 +01:00
// BUG: This loop extends past the bounds of the mapView array. Its size is only 0x100.
2018-05-09 12:07:56 +02:00
for (i = 0; i < 0x200; i++)
2018-12-28 18:18:23 +01:00
marker |= gSaveBlock1Ptr->mapView[i];
#else
// UBFIX: Only iterate over 0x100
for (i = 0; i < ARRAY_COUNT(gSaveBlock1Ptr->mapView); i++)
marker |= gSaveBlock1Ptr->mapView[i];
#endif
2018-12-28 18:18:23 +01:00
if (marker == 0)
return TRUE;
else
return FALSE;
2018-05-09 12:07:56 +02:00
}
2018-12-28 18:18:23 +01:00
static void ClearSavedMapView(void)
2018-05-09 12:07:56 +02:00
{
CpuFill16(0, gSaveBlock1Ptr->mapView, sizeof(gSaveBlock1Ptr->mapView));
}
2018-12-28 18:18:23 +01:00
static void LoadSavedMapView(void)
2018-05-09 12:07:56 +02:00
{
2018-12-28 18:18:23 +01:00
u8 yMode;
2018-05-09 12:07:56 +02:00
int i, j;
int x, y;
u16 *mapView;
int width;
mapView = gSaveBlock1Ptr->mapView;
2018-12-28 18:18:23 +01:00
if (!SavedMapViewIsEmpty())
2018-05-09 12:07:56 +02:00
{
2018-12-28 18:18:23 +01:00
width = gBackupMapLayout.width;
2018-05-09 12:07:56 +02:00
x = gSaveBlock1Ptr->pos.x;
y = gSaveBlock1Ptr->pos.y;
2021-10-09 18:12:18 +02:00
for (i = y; i < y + MAP_OFFSET_H; i++)
2018-05-09 12:07:56 +02:00
{
if (i == y && i != 0)
2018-12-28 18:18:23 +01:00
yMode = 0;
2021-10-09 18:12:18 +02:00
else if (i == y + MAP_OFFSET_H - 1 && i != gMapHeader.mapLayout->height - 1)
2018-12-28 18:18:23 +01:00
yMode = 1;
2018-05-09 12:07:56 +02:00
else
2018-12-28 18:18:23 +01:00
yMode = 0xFF;
2018-07-07 14:24:19 +02:00
2021-10-09 18:12:18 +02:00
for (j = x; j < x + MAP_OFFSET_W; j++)
2018-05-09 12:07:56 +02:00
{
2022-05-17 19:51:54 +02:00
if (!SkipCopyingMetatileFromSavedMap(&sBackupMapData[j + width * i], width, yMode))
sBackupMapData[j + width * i] = *mapView;
2018-05-09 12:07:56 +02:00
mapView++;
}
}
2021-10-09 18:12:18 +02:00
for (j = x; j < x + MAP_OFFSET_W; j++)
2018-05-09 12:07:56 +02:00
{
if (y != 0)
2018-12-28 18:18:23 +01:00
FixLongGrassMetatilesWindowTop(j, y - 1);
2018-06-21 00:41:51 +02:00
if (i < gMapHeader.mapLayout->height - 1)
2021-10-09 18:12:18 +02:00
FixLongGrassMetatilesWindowBottom(j, y + MAP_OFFSET_H - 1);
2018-05-09 12:07:56 +02:00
}
2018-12-28 18:18:23 +01:00
ClearSavedMapView();
2018-05-09 12:07:56 +02:00
}
}
2021-04-06 22:05:43 +02:00
static void MoveMapViewToBackup(u8 direction)
2018-05-09 12:07:56 +02:00
{
int width;
u16 *mapView;
int x0, y0;
int x2, y2;
u16 *src, *dest;
int srci, desti;
int r9, r8;
int x, y;
int i, j;
mapView = gSaveBlock1Ptr->mapView;
2018-12-28 18:18:23 +01:00
width = gBackupMapLayout.width;
2018-05-09 12:07:56 +02:00
r9 = 0;
r8 = 0;
x0 = gSaveBlock1Ptr->pos.x;
y0 = gSaveBlock1Ptr->pos.y;
2021-10-09 18:12:18 +02:00
x2 = MAP_OFFSET_W;
y2 = MAP_OFFSET_H;
2021-04-06 22:05:43 +02:00
switch (direction)
2018-05-09 12:07:56 +02:00
{
case CONNECTION_NORTH:
y0 += 1;
2021-10-09 18:12:18 +02:00
y2 = MAP_OFFSET_H - 1;
2018-05-09 12:07:56 +02:00
break;
case CONNECTION_SOUTH:
r8 = 1;
2021-10-09 18:12:18 +02:00
y2 = MAP_OFFSET_H - 1;
2018-05-09 12:07:56 +02:00
break;
case CONNECTION_WEST:
x0 += 1;
2021-10-09 18:12:18 +02:00
x2 = MAP_OFFSET_W - 1;
2018-05-09 12:07:56 +02:00
break;
case CONNECTION_EAST:
r9 = 1;
2021-10-09 18:12:18 +02:00
x2 = MAP_OFFSET_W - 1;
2018-05-09 12:07:56 +02:00
break;
}
for (y = 0; y < y2; y++)
{
i = 0;
j = 0;
for (x = 0; x < x2; x++)
{
desti = width * (y + y0);
2021-10-09 18:12:18 +02:00
srci = (y + r8) * MAP_OFFSET_W + r9;
2018-05-09 12:07:56 +02:00
src = &mapView[srci + i];
2022-05-17 19:51:54 +02:00
dest = &sBackupMapData[x0 + desti + j];
2018-05-09 12:07:56 +02:00
*dest = *src;
i++;
j++;
}
}
2018-12-28 18:18:23 +01:00
ClearSavedMapView();
2018-05-09 12:07:56 +02:00
}
int GetMapBorderIdAt(int x, int y)
{
2022-01-19 16:15:32 +01:00
if (GetMapGridBlockAt(x, y) == MAPGRID_UNDEFINED)
2021-04-07 19:26:02 +02:00
return CONNECTION_INVALID;
2018-05-09 12:07:56 +02:00
2021-10-09 18:12:18 +02:00
if (x >= (gBackupMapLayout.width - (MAP_OFFSET + 1)))
2018-05-09 12:07:56 +02:00
{
2022-05-17 19:51:54 +02:00
if (!sMapConnectionFlags.east)
2021-04-06 22:05:43 +02:00
return CONNECTION_INVALID;
2021-04-07 19:26:02 +02:00
2018-05-09 12:07:56 +02:00
return CONNECTION_EAST;
}
2021-10-09 18:12:18 +02:00
else if (x < MAP_OFFSET)
2018-05-09 12:07:56 +02:00
{
2022-05-17 19:51:54 +02:00
if (!sMapConnectionFlags.west)
2021-04-06 22:05:43 +02:00
return CONNECTION_INVALID;
2021-04-07 19:26:02 +02:00
2018-05-09 12:07:56 +02:00
return CONNECTION_WEST;
}
2021-10-09 18:12:18 +02:00
else if (y >= (gBackupMapLayout.height - MAP_OFFSET))
2018-05-09 12:07:56 +02:00
{
2022-05-17 19:51:54 +02:00
if (!sMapConnectionFlags.south)
2021-04-06 22:05:43 +02:00
return CONNECTION_INVALID;
2021-04-07 19:26:02 +02:00
2018-05-09 12:07:56 +02:00
return CONNECTION_SOUTH;
}
2021-10-09 18:12:18 +02:00
else if (y < MAP_OFFSET)
2018-05-09 12:07:56 +02:00
{
2022-05-17 19:51:54 +02:00
if (!sMapConnectionFlags.north)
2021-04-06 22:05:43 +02:00
return CONNECTION_INVALID;
2021-04-07 19:26:02 +02:00
2018-05-09 12:07:56 +02:00
return CONNECTION_NORTH;
}
else
{
2021-04-06 22:05:43 +02:00
return CONNECTION_NONE;
2018-05-09 12:07:56 +02:00
}
}
int GetPostCameraMoveMapBorderId(int x, int y)
{
2021-10-09 18:12:18 +02:00
return GetMapBorderIdAt(gSaveBlock1Ptr->pos.x + MAP_OFFSET + x, gSaveBlock1Ptr->pos.y + MAP_OFFSET + y);
2018-05-09 12:07:56 +02:00
}
2021-04-06 22:05:43 +02:00
bool32 CanCameraMoveInDirection(int direction)
2018-05-09 12:07:56 +02:00
{
int x, y;
2021-10-09 18:12:18 +02:00
x = gSaveBlock1Ptr->pos.x + MAP_OFFSET + gDirectionToVectors[direction].x;
y = gSaveBlock1Ptr->pos.y + MAP_OFFSET + gDirectionToVectors[direction].y;
2021-04-07 19:26:02 +02:00
if (GetMapBorderIdAt(x, y) == CONNECTION_INVALID)
2021-04-06 22:05:43 +02:00
return FALSE;
2021-04-07 19:26:02 +02:00
2021-04-06 22:05:43 +02:00
return TRUE;
2018-05-09 12:07:56 +02:00
}
2023-03-08 17:07:44 +01:00
static void SetPositionFromConnection(const struct MapConnection *connection, int direction, int x, int y)
2018-05-09 12:07:56 +02:00
{
struct MapHeader const *mapHeader;
mapHeader = GetMapHeaderFromConnection(connection);
2018-05-09 12:07:56 +02:00
switch (direction)
{
case CONNECTION_EAST:
gSaveBlock1Ptr->pos.x = -x;
gSaveBlock1Ptr->pos.y -= connection->offset;
break;
case CONNECTION_WEST:
2018-06-21 00:41:51 +02:00
gSaveBlock1Ptr->pos.x = mapHeader->mapLayout->width;
2018-05-09 12:07:56 +02:00
gSaveBlock1Ptr->pos.y -= connection->offset;
break;
case CONNECTION_SOUTH:
gSaveBlock1Ptr->pos.x -= connection->offset;
gSaveBlock1Ptr->pos.y = -y;
break;
case CONNECTION_NORTH:
gSaveBlock1Ptr->pos.x -= connection->offset;
2018-06-21 00:41:51 +02:00
gSaveBlock1Ptr->pos.y = mapHeader->mapLayout->height;
2018-05-09 12:07:56 +02:00
break;
}
}
bool8 CameraMove(int x, int y)
{
2021-04-06 22:05:43 +02:00
int direction;
2023-03-08 17:07:44 +01:00
const struct MapConnection *connection;
2018-05-09 12:07:56 +02:00
int old_x, old_y;
gCamera.active = FALSE;
direction = GetPostCameraMoveMapBorderId(x, y);
2021-04-06 22:05:43 +02:00
if (direction == CONNECTION_NONE || direction == CONNECTION_INVALID)
2018-05-09 12:07:56 +02:00
{
gSaveBlock1Ptr->pos.x += x;
gSaveBlock1Ptr->pos.y += y;
}
else
{
2021-04-06 22:05:43 +02:00
SaveMapView();
ClearMirageTowerPulseBlendEffect();
2018-05-09 12:07:56 +02:00
old_x = gSaveBlock1Ptr->pos.x;
old_y = gSaveBlock1Ptr->pos.y;
2021-04-06 22:05:43 +02:00
connection = GetIncomingConnection(direction, gSaveBlock1Ptr->pos.x, gSaveBlock1Ptr->pos.y);
SetPositionFromConnection(connection, direction, x, y);
LoadMapFromCameraTransition(connection->mapGroup, connection->mapNum);
2018-05-09 12:07:56 +02:00
gCamera.active = TRUE;
gCamera.x = old_x - gSaveBlock1Ptr->pos.x;
gCamera.y = old_y - gSaveBlock1Ptr->pos.y;
gSaveBlock1Ptr->pos.x += x;
gSaveBlock1Ptr->pos.y += y;
2021-04-06 22:05:43 +02:00
MoveMapViewToBackup(direction);
2018-05-09 12:07:56 +02:00
}
return gCamera.active;
}
2023-03-08 17:07:44 +01:00
static const struct MapConnection *GetIncomingConnection(u8 direction, int x, int y)
2018-05-09 12:07:56 +02:00
{
int count;
int i;
2023-03-08 17:07:44 +01:00
const struct MapConnection *connection;
const struct MapConnections *connections = gMapHeader.connections;
2021-04-06 22:05:43 +02:00
#ifdef UBFIX // UB: Multiple possible null dereferences
if (connections == NULL || connections->connections == NULL)
return NULL;
#endif
count = connections->count;
connection = connections->connections;
2018-05-09 12:07:56 +02:00
for (i = 0; i < count; i++, connection++)
{
2021-04-06 22:05:43 +02:00
if (connection->direction == direction && IsPosInIncomingConnectingMap(direction, x, y, connection) == TRUE)
2018-05-09 12:07:56 +02:00
return connection;
}
return NULL;
}
2023-03-08 17:07:44 +01:00
static bool8 IsPosInIncomingConnectingMap(u8 direction, int x, int y, const struct MapConnection *connection)
2018-05-09 12:07:56 +02:00
{
struct MapHeader const *mapHeader;
mapHeader = GetMapHeaderFromConnection(connection);
2018-05-09 12:07:56 +02:00
switch (direction)
{
case CONNECTION_SOUTH:
case CONNECTION_NORTH:
2021-04-06 22:05:43 +02:00
return IsCoordInIncomingConnectingMap(x, gMapHeader.mapLayout->width, mapHeader->mapLayout->width, connection->offset);
2018-05-09 12:07:56 +02:00
case CONNECTION_WEST:
case CONNECTION_EAST:
2021-04-06 22:05:43 +02:00
return IsCoordInIncomingConnectingMap(y, gMapHeader.mapLayout->height, mapHeader->mapLayout->height, connection->offset);
2018-05-09 12:07:56 +02:00
}
return FALSE;
}
2021-04-06 22:05:43 +02:00
static bool8 IsCoordInIncomingConnectingMap(int coord, int srcMax, int destMax, int offset)
2018-05-09 12:07:56 +02:00
{
int offset2;
offset2 = offset;
if (offset2 < 0)
offset2 = 0;
2021-04-06 22:05:43 +02:00
if (destMax + offset < srcMax)
srcMax = destMax + offset;
2018-05-09 12:07:56 +02:00
2021-04-06 22:05:43 +02:00
if (offset2 <= coord && coord <= srcMax)
2018-05-09 12:07:56 +02:00
return TRUE;
return FALSE;
}
2021-04-06 22:05:43 +02:00
static int IsCoordInConnectingMap(int coord, int max)
2018-05-09 12:07:56 +02:00
{
2021-04-06 22:05:43 +02:00
if (coord >= 0 && coord < max)
2018-05-09 12:07:56 +02:00
return TRUE;
return FALSE;
}
2023-03-08 17:07:44 +01:00
static int IsPosInConnectingMap(const struct MapConnection *connection, int x, int y)
2018-05-09 12:07:56 +02:00
{
struct MapHeader const *mapHeader;
mapHeader = GetMapHeaderFromConnection(connection);
2018-05-09 12:07:56 +02:00
switch (connection->direction)
{
case CONNECTION_SOUTH:
case CONNECTION_NORTH:
2021-04-06 22:05:43 +02:00
return IsCoordInConnectingMap(x - connection->offset, mapHeader->mapLayout->width);
2018-05-09 12:07:56 +02:00
case CONNECTION_WEST:
case CONNECTION_EAST:
2021-04-06 22:05:43 +02:00
return IsCoordInConnectingMap(y - connection->offset, mapHeader->mapLayout->height);
2018-05-09 12:07:56 +02:00
}
return FALSE;
}
2023-03-08 17:07:44 +01:00
const struct MapConnection *GetMapConnectionAtPos(s16 x, s16 y)
2018-05-09 12:07:56 +02:00
{
int count;
2023-03-08 17:07:44 +01:00
const struct MapConnection *connection;
2018-05-09 12:07:56 +02:00
int i;
u8 direction;
if (!gMapHeader.connections)
{
return NULL;
}
else
{
count = gMapHeader.connections->count;
connection = gMapHeader.connections->connections;
for (i = 0; i < count; i++, connection++)
{
direction = connection->direction;
if ((direction == CONNECTION_DIVE || direction == CONNECTION_EMERGE)
2021-10-09 18:12:18 +02:00
|| (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))
2018-05-09 12:07:56 +02:00
{
continue;
}
2021-10-09 18:12:18 +02:00
if (IsPosInConnectingMap(connection, x - MAP_OFFSET, y - MAP_OFFSET) == TRUE)
2018-05-09 12:07:56 +02:00
{
return connection;
}
}
}
return NULL;
}
void SetCameraFocusCoords(u16 x, u16 y)
2018-05-09 12:07:56 +02:00
{
2021-10-09 18:12:18 +02:00
gSaveBlock1Ptr->pos.x = x - MAP_OFFSET;
gSaveBlock1Ptr->pos.y = y - MAP_OFFSET;
2018-05-09 12:07:56 +02:00
}
2018-12-28 18:18:23 +01:00
void GetCameraFocusCoords(u16 *x, u16 *y)
2018-05-09 12:07:56 +02:00
{
2021-10-09 18:12:18 +02:00
*x = gSaveBlock1Ptr->pos.x + MAP_OFFSET;
*y = gSaveBlock1Ptr->pos.y + MAP_OFFSET;
2018-05-09 12:07:56 +02:00
}
// Unused
static void SetCameraCoords(u16 x, u16 y)
2018-05-09 12:07:56 +02:00
{
gSaveBlock1Ptr->pos.x = x;
gSaveBlock1Ptr->pos.y = y;
}
void GetCameraCoords(u16 *x, u16 *y)
{
*x = gSaveBlock1Ptr->pos.x;
*y = gSaveBlock1Ptr->pos.y;
}
2020-06-01 16:23:12 +02:00
void MapGridSetMetatileImpassabilityAt(int x, int y, bool32 impassable)
2018-05-09 12:07:56 +02:00
{
2021-04-07 19:26:02 +02:00
if (AreCoordsWithinMapGridBounds(x, y))
2018-05-09 12:07:56 +02:00
{
2020-06-01 16:23:12 +02:00
if (impassable)
2022-01-19 16:15:32 +01:00
gBackupMapLayout.map[x + gBackupMapLayout.width * y] |= MAPGRID_COLLISION_MASK;
2018-05-09 12:07:56 +02:00
else
2022-01-19 16:15:32 +01:00
gBackupMapLayout.map[x + gBackupMapLayout.width * y] &= ~MAPGRID_COLLISION_MASK;
2018-05-09 12:07:56 +02:00
}
}
static bool8 SkipCopyingMetatileFromSavedMap(u16 *mapBlock, u16 mapWidth, u8 yMode)
2018-05-09 12:07:56 +02:00
{
2018-12-28 18:18:23 +01:00
if (yMode == 0xFF)
2018-05-09 12:07:56 +02:00
return FALSE;
2018-07-07 14:24:19 +02:00
2018-12-28 18:18:23 +01:00
if (yMode == 0)
2022-01-19 16:15:32 +01:00
mapBlock -= mapWidth;
2018-05-09 12:07:56 +02:00
else
2022-01-19 16:15:32 +01:00
mapBlock += mapWidth;
2018-05-09 12:07:56 +02:00
2022-01-19 16:15:32 +01:00
if (IsLargeBreakableDecoration(*mapBlock & MAPGRID_METATILE_ID_MASK, yMode) == TRUE)
2018-05-09 12:07:56 +02:00
return TRUE;
return FALSE;
}
static void CopyTilesetToVram(struct Tileset const *tileset, u16 numTiles, u16 offset)
2018-05-09 12:07:56 +02:00
{
if (tileset)
{
if (!tileset->isCompressed)
LoadBgTiles(2, tileset->tiles, numTiles * 32, offset);
else
2020-05-14 10:37:09 +02:00
DecompressAndCopyTileDataToVram(2, tileset->tiles, numTiles * 32, offset, 0);
2018-05-09 12:07:56 +02:00
}
}
static void CopyTilesetToVramUsingHeap(struct Tileset const *tileset, u16 numTiles, u16 offset)
2018-05-09 12:07:56 +02:00
{
if (tileset)
{
if (!tileset->isCompressed)
LoadBgTiles(2, tileset->tiles, numTiles * 32, offset);
else
2018-08-18 00:54:18 +02:00
DecompressAndLoadBgGfxUsingHeap(2, tileset->tiles, numTiles * 32, offset, 0);
2018-05-09 12:07:56 +02:00
}
}
2022-09-03 01:29:35 +02:00
// Below two are dummied functions from FRLG, used to tint the overworld palettes for the Quest Log
static void ApplyGlobalTintToPaletteEntries(u16 offset, u16 size)
2018-05-09 12:07:56 +02:00
{
}
static void ApplyGlobalTintToPaletteSlot(u8 slot, u8 count)
2018-05-09 12:07:56 +02:00
{
}
void LoadTilesetPalette(struct Tileset const *tileset, u16 destOffset, u16 size)
2018-05-09 12:07:56 +02:00
{
u16 black = RGB_BLACK;
if (tileset)
{
if (tileset->isSecondary == FALSE)
{
2022-08-19 16:29:35 +02:00
LoadPalette(&black, destOffset, PLTT_SIZEOF(1));
LoadPalette(tileset->palettes[0] + 1, destOffset + 1, size - PLTT_SIZEOF(1));
ApplyGlobalTintToPaletteEntries(destOffset + 1, (size - PLTT_SIZEOF(1)) >> 1);
2018-05-09 12:07:56 +02:00
}
else if (tileset->isSecondary == TRUE)
{
LoadPalette(tileset->palettes[NUM_PALS_IN_PRIMARY], destOffset, size);
2022-09-03 01:29:35 +02:00
ApplyGlobalTintToPaletteEntries(destOffset, size >> 1);
2018-05-09 12:07:56 +02:00
}
else
{
LoadCompressedPalette((const u32 *)tileset->palettes, destOffset, size);
2022-09-03 01:29:35 +02:00
ApplyGlobalTintToPaletteEntries(destOffset, size >> 1);
2018-05-09 12:07:56 +02:00
}
}
}
void CopyPrimaryTilesetToVram(struct MapLayout const *mapLayout)
2018-05-09 12:07:56 +02:00
{
CopyTilesetToVram(mapLayout->primaryTileset, NUM_TILES_IN_PRIMARY, 0);
2018-05-09 12:07:56 +02:00
}
void CopySecondaryTilesetToVram(struct MapLayout const *mapLayout)
2018-05-09 12:07:56 +02:00
{
CopyTilesetToVram(mapLayout->secondaryTileset, NUM_TILES_TOTAL - NUM_TILES_IN_PRIMARY, NUM_TILES_IN_PRIMARY);
2018-05-09 12:07:56 +02:00
}
void CopySecondaryTilesetToVramUsingHeap(struct MapLayout const *mapLayout)
2018-05-09 12:07:56 +02:00
{
CopyTilesetToVramUsingHeap(mapLayout->secondaryTileset, NUM_TILES_TOTAL - NUM_TILES_IN_PRIMARY, NUM_TILES_IN_PRIMARY);
2018-05-09 12:07:56 +02:00
}
static void LoadPrimaryTilesetPalette(struct MapLayout const *mapLayout)
2018-05-09 12:07:56 +02:00
{
2022-08-19 17:32:00 +02:00
LoadTilesetPalette(mapLayout->primaryTileset, BG_PLTT_ID(0), NUM_PALS_IN_PRIMARY * PLTT_SIZE_4BPP);
2018-05-09 12:07:56 +02:00
}
void LoadSecondaryTilesetPalette(struct MapLayout const *mapLayout)
2018-05-09 12:07:56 +02:00
{
2022-08-19 16:29:35 +02:00
LoadTilesetPalette(mapLayout->secondaryTileset, BG_PLTT_ID(NUM_PALS_IN_PRIMARY), (NUM_PALS_TOTAL - NUM_PALS_IN_PRIMARY) * PLTT_SIZE_4BPP);
2018-05-09 12:07:56 +02:00
}
void CopyMapTilesetsToVram(struct MapLayout const *mapLayout)
2018-05-09 12:07:56 +02:00
{
2018-06-21 00:41:51 +02:00
if (mapLayout)
2018-05-09 12:07:56 +02:00
{
CopyTilesetToVramUsingHeap(mapLayout->primaryTileset, NUM_TILES_IN_PRIMARY, 0);
CopyTilesetToVramUsingHeap(mapLayout->secondaryTileset, NUM_TILES_TOTAL - NUM_TILES_IN_PRIMARY, NUM_TILES_IN_PRIMARY);
2018-05-09 12:07:56 +02:00
}
}
void LoadMapTilesetPalettes(struct MapLayout const *mapLayout)
2018-05-09 12:07:56 +02:00
{
2018-06-21 00:41:51 +02:00
if (mapLayout)
2018-05-09 12:07:56 +02:00
{
LoadPrimaryTilesetPalette(mapLayout);
LoadSecondaryTilesetPalette(mapLayout);
2018-05-09 12:07:56 +02:00
}
}