pokeemerald/src/fieldmap.c

1045 lines
26 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"
2018-05-09 12:07:56 +02:00
struct ConnectionFlags
{
u8 south:1;
u8 north:1;
u8 west:1;
u8 east:1;
};
2018-12-28 18:18:23 +01:00
EWRAM_DATA static u16 gBackupMapData[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};
2018-12-28 18:18:23 +01:00
EWRAM_DATA static struct ConnectionFlags gMapConnectionFlags = {0};
2018-05-09 12:07:56 +02:00
EWRAM_DATA static u32 sFiller_02037344 = 0; // without this, the next file won't align properly
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);
static void InitBackupMapLayoutData(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 bool8 SkipCopyingMetatileFromSavedMap(u16* mapMetatilePtr, u16 mapWidth, u8 yMode);
2018-05-09 12:07:56 +02:00
struct MapHeader const *const mapconnection_get_mapheader(struct MapConnection *connection)
{
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
{
2018-12-28 18:18:23 +01:00
CpuFastFill(0x03ff03ff, gBackupMapData, sizeof(gBackupMapData));
2019-02-07 03:01:29 +01:00
GenerateBattlePyramidFloorLayout(gBackupMapData, 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
{
2018-12-28 18:18:23 +01:00
CpuFastFill(0x03ff03ff, gBackupMapData, sizeof(gBackupMapData));
2019-11-13 22:10:05 +01:00
GenerateTrainerHillFloorLayout(gBackupMapData);
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;
2018-12-28 18:18:23 +01:00
CpuFastFill16(0x03ff, gBackupMapData, sizeof(gBackupMapData));
gBackupMapLayout.map = gBackupMapData;
2018-06-21 00:41:51 +02:00
width = mapLayout->width + 15;
2018-12-28 18:18:23 +01:00
gBackupMapLayout.width = width;
2018-06-21 00:41:51 +02:00
height = mapLayout->height + 14;
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
}
}
2018-12-28 18:18:23 +01:00
static void InitBackupMapLayoutData(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;
dest += gBackupMapLayout.width * 7 + 7;
2018-05-09 12:07:56 +02:00
for (y = 0; y < height; y++)
{
CpuCopy16(map, dest, width * 2);
2018-12-28 18:18:23 +01:00
dest += width + 15;
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;
struct MapConnection *connection;
int i;
if (mapHeader->connections)
{
count = mapHeader->connections->count;
connection = mapHeader->connections->connections;
2018-12-28 18:18:23 +01:00
gMapConnectionFlags = sDummyConnectionFlags;
2018-05-09 12:07:56 +02:00
for (i = 0; i < count; i++, connection++)
{
struct MapHeader const *cMap = mapconnection_get_mapheader(connection);
u32 offset = connection->offset;
switch (connection->direction)
{
case CONNECTION_SOUTH:
2018-12-28 18:18:23 +01:00
FillSouthConnection(mapHeader, cMap, offset);
gMapConnectionFlags.south = 1;
2018-05-09 12:07:56 +02:00
break;
case CONNECTION_NORTH:
2018-12-28 18:18:23 +01:00
FillNorthConnection(mapHeader, cMap, offset);
gMapConnectionFlags.north = 1;
2018-05-09 12:07:56 +02:00
break;
case CONNECTION_WEST:
2018-12-28 18:18:23 +01:00
FillWestConnection(mapHeader, cMap, offset);
gMapConnectionFlags.west = 1;
2018-05-09 12:07:56 +02:00
break;
case CONNECTION_EAST:
2018-12-28 18:18:23 +01:00
FillEastConnection(mapHeader, cMap, offset);
gMapConnectionFlags.east = 1;
2018-05-09 12:07:56 +02:00
break;
}
}
}
}
2018-12-28 18:18:23 +01:00
static void sub_8087F54(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;
u16 *src;
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;
2018-05-09 12:07:56 +02:00
x = offset + 7;
2018-06-21 00:41:51 +02:00
y = mapHeader->mapLayout->height + 7;
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
}
}
sub_8087F54(
x, y,
connectedMapHeader,
x2, /*y2*/ 0,
width, /*height*/ 7);
}
}
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;
2018-05-09 12:07:56 +02:00
x = offset + 7;
y2 = cHeight - 7;
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
}
}
sub_8087F54(
x, /*y*/ 0,
connectedMapHeader,
x2, y2,
width, /*height*/ 7);
}
}
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;
2018-05-09 12:07:56 +02:00
y = offset + 7;
x2 = cWidth - 7;
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
}
}
sub_8087F54(
/*x*/ 0, y,
connectedMapHeader,
x2, y2,
/*width*/ 7, height);
}
}
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;
x = mapHeader->mapLayout->width + 7;
2018-05-09 12:07:56 +02:00
y = offset + 7;
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
}
}
sub_8087F54(
x, y,
connectedMapHeader,
/*x2*/ 0, y2,
/*width*/ 8, height);
}
}
union Block
{
struct
{
u16 block:10;
u16 collision:2;
u16 elevation:4;
} block;
u16 value;
};
u8 MapGridGetZCoordAt(int x, int y)
{
u16 block;
int i;
u16 *border;
2018-12-28 18:18:23 +01:00
if (x >= 0 && x < gBackupMapLayout.width
&& y >= 0 && y < gBackupMapLayout.height)
2018-05-09 12:07:56 +02:00
{
2018-12-28 18:18:23 +01:00
block = gBackupMapLayout.map[x + gBackupMapLayout.width * y];
2018-05-09 12:07:56 +02:00
}
else
{
2018-06-21 00:41:51 +02:00
border = gMapHeader.mapLayout->border;
2018-05-09 12:07:56 +02:00
i = (x + 1) & 1;
i += ((y + 1) & 1) * 2;
2018-06-21 00:41:51 +02:00
block = gMapHeader.mapLayout->border[i];
2019-02-27 04:56:22 +01:00
block |= METATILE_COLLISION_MASK;
2018-05-09 12:07:56 +02:00
}
2019-02-27 04:56:22 +01:00
if (block == METATILE_ID_UNDEFINED)
2018-05-09 12:07:56 +02:00
{
return 0;
}
2018-07-07 14:24:19 +02:00
2019-02-27 04:56:22 +01:00
return block >> METATILE_ELEVATION_SHIFT;
2018-05-09 12:07:56 +02:00
}
u8 MapGridIsImpassableAt(int x, int y)
{
u16 block;
int i;
u16 *border;
2018-12-28 18:18:23 +01:00
if (x >= 0 && x < gBackupMapLayout.width
&& y >= 0 && y < gBackupMapLayout.height)
2018-05-09 12:07:56 +02:00
{
2018-12-28 18:18:23 +01:00
block = gBackupMapLayout.map[x + gBackupMapLayout.width * y];
2018-05-09 12:07:56 +02:00
}
else
{
2018-06-21 00:41:51 +02:00
border = gMapHeader.mapLayout->border;
2018-05-09 12:07:56 +02:00
i = (x + 1) & 1;
i += ((y + 1) & 1) * 2;
2018-06-21 00:41:51 +02:00
block = gMapHeader.mapLayout->border[i];
2019-02-27 04:56:22 +01:00
block |= METATILE_COLLISION_MASK;
2018-05-09 12:07:56 +02:00
}
2019-02-27 04:56:22 +01:00
if (block == METATILE_ID_UNDEFINED)
2018-05-09 12:07:56 +02:00
{
return 1;
}
2019-02-27 04:56:22 +01:00
return (block & METATILE_COLLISION_MASK) >> METATILE_COLLISION_SHIFT;
2018-05-09 12:07:56 +02:00
}
u32 MapGridGetMetatileIdAt(int x, int y)
{
u16 block;
int i;
int j;
2018-06-21 00:41:51 +02:00
struct MapLayout const *mapLayout;
2018-05-09 12:07:56 +02:00
u16 *border;
u16 block2;
2018-12-28 18:18:23 +01:00
if (x >= 0 && x < gBackupMapLayout.width
&& y >= 0 && y < gBackupMapLayout.height)
2018-05-09 12:07:56 +02:00
{
2018-12-28 18:18:23 +01:00
block = gBackupMapLayout.map[x + gBackupMapLayout.width * y];
2018-05-09 12:07:56 +02:00
}
else
{
2018-06-21 00:41:51 +02:00
mapLayout = gMapHeader.mapLayout;
2018-05-09 12:07:56 +02:00
i = (x + 1) & 1;
i += ((y + 1) & 1) * 2;
2019-02-27 04:56:22 +01:00
block = mapLayout->border[i] | METATILE_COLLISION_MASK;
2018-05-09 12:07:56 +02:00
}
2019-02-27 04:56:22 +01:00
if (block == METATILE_ID_UNDEFINED)
2018-05-09 12:07:56 +02:00
{
2018-06-21 00:41:51 +02:00
border = gMapHeader.mapLayout->border;
2018-05-09 12:07:56 +02:00
j = (x + 1) & 1;
j += ((y + 1) & 1) * 2;
2018-06-21 00:41:51 +02:00
block2 = gMapHeader.mapLayout->border[j];
2019-02-22 09:08:48 +01:00
// This OR is completely pointless.
2019-02-27 04:56:22 +01:00
block2 |= METATILE_COLLISION_MASK;
return block2 & METATILE_ID_MASK;
2018-05-09 12:07:56 +02:00
}
2019-02-27 04:56:22 +01:00
return block & METATILE_ID_MASK;
2018-05-09 12:07:56 +02:00
}
u32 MapGridGetMetatileBehaviorAt(int x, int y)
{
u16 metatile;
metatile = MapGridGetMetatileIdAt(x, y);
return GetBehaviorByMetatileId(metatile) & 0xff;
}
u8 MapGridGetMetatileLayerTypeAt(int x, int y)
{
u16 metatile;
metatile = MapGridGetMetatileIdAt(x, y);
2019-02-27 04:56:22 +01:00
return (GetBehaviorByMetatileId(metatile) & METATILE_ELEVATION_MASK) >> METATILE_ELEVATION_SHIFT;
2018-05-09 12:07:56 +02:00
}
void MapGridSetMetatileIdAt(int x, int y, u16 metatile)
{
int i;
2018-12-28 18:18:23 +01:00
if (x >= 0 && x < gBackupMapLayout.width
&& y >= 0 && y < gBackupMapLayout.height)
2018-05-09 12:07:56 +02:00
{
2018-12-28 18:18:23 +01:00
i = x + y * gBackupMapLayout.width;
2019-02-27 04:56:22 +01:00
gBackupMapLayout.map[i] = (gBackupMapLayout.map[i] & METATILE_ELEVATION_MASK) | (metatile & ~METATILE_ELEVATION_MASK);
2018-05-09 12:07:56 +02:00
}
}
void MapGridSetMetatileEntryAt(int x, int y, u16 metatile)
{
int i;
2018-12-28 18:18:23 +01:00
if (x >= 0 && x < gBackupMapLayout.width
&& y >= 0 && y < gBackupMapLayout.height)
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
}
}
u16 GetBehaviorByMetatileId(u16 metatile)
{
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
{
return 0xFF;
2018-05-09 12:07:56 +02:00
}
}
void save_serialize_map(void)
{
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;
for (i = y; i < y + 14; i++)
{
for (j = x; j < x + 15; j++)
{
2018-12-28 18:18:23 +01:00
*mapView++ = gBackupMapData[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;
// 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];
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;
for (i = y; i < y + 14; i++)
{
if (i == y && i != 0)
2018-12-28 18:18:23 +01:00
yMode = 0;
2018-06-21 00:41:51 +02:00
else if (i == y + 13 && 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
2018-05-09 12:07:56 +02:00
for (j = x; j < x + 15; j++)
{
2018-12-28 18:18:23 +01:00
if (!SkipCopyingMetatileFromSavedMap(&gBackupMapData[j + width * i], width, yMode))
gBackupMapData[j + width * i] = *mapView;
2018-05-09 12:07:56 +02:00
mapView++;
}
}
for (j = x; j < x + 15; j++)
{
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)
2018-12-28 18:18:23 +01:00
FixLongGrassMetatilesWindowBottom(j, y + 13);
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
}
}
void sub_80885C4(u8 a1)
{
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;
x2 = 15;
y2 = 14;
switch (a1)
{
case CONNECTION_NORTH:
y0 += 1;
y2 = 13;
break;
case CONNECTION_SOUTH:
r8 = 1;
y2 = 13;
break;
case CONNECTION_WEST:
x0 += 1;
x2 = 14;
break;
case CONNECTION_EAST:
r9 = 1;
x2 = 14;
break;
}
for (y = 0; y < y2; y++)
{
i = 0;
j = 0;
for (x = 0; x < x2; x++)
{
desti = width * (y + y0);
srci = (y + r8) * 15 + r9;
src = &mapView[srci + i];
2018-12-28 18:18:23 +01:00
dest = &gBackupMapData[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)
{
2018-06-21 00:41:51 +02:00
struct MapLayout const *mapLayout;
2018-05-09 12:07:56 +02:00
u16 block, block2;
int i, j;
2018-12-28 18:18:23 +01:00
if (x >= 0 && x < gBackupMapLayout.width
&& y >= 0 && y < gBackupMapLayout.height)
2018-05-09 12:07:56 +02:00
{
2018-12-28 18:18:23 +01:00
i = gBackupMapLayout.width;
2018-05-09 12:07:56 +02:00
i *= y;
2018-12-28 18:18:23 +01:00
block = gBackupMapLayout.map[x + i];
2019-02-27 04:56:22 +01:00
if (block == METATILE_ID_UNDEFINED)
2018-05-09 12:07:56 +02:00
{
goto fail;
}
}
else
{
2018-06-21 00:41:51 +02:00
mapLayout = gMapHeader.mapLayout;
2018-05-09 12:07:56 +02:00
j = (x + 1) & 1;
j += ((y + 1) & 1) * 2;
2019-02-27 04:56:22 +01:00
block2 = METATILE_COLLISION_MASK | mapLayout->border[j];
if (block2 == METATILE_ID_UNDEFINED)
2018-05-09 12:07:56 +02:00
{
goto fail;
}
}
goto success;
fail:
return -1;
success:
2018-12-28 18:18:23 +01:00
if (x >= (gBackupMapLayout.width - 8))
2018-05-09 12:07:56 +02:00
{
2018-12-28 18:18:23 +01:00
if (!gMapConnectionFlags.east)
2018-05-09 12:07:56 +02:00
{
return -1;
}
return CONNECTION_EAST;
}
else if (x < 7)
{
2018-12-28 18:18:23 +01:00
if (!gMapConnectionFlags.west)
2018-05-09 12:07:56 +02:00
{
return -1;
}
return CONNECTION_WEST;
}
2018-12-28 18:18:23 +01:00
else if (y >= (gBackupMapLayout.height - 7))
2018-05-09 12:07:56 +02:00
{
2018-12-28 18:18:23 +01:00
if (!gMapConnectionFlags.south)
2018-05-09 12:07:56 +02:00
{
return -1;
}
return CONNECTION_SOUTH;
}
else if (y < 7)
{
2018-12-28 18:18:23 +01:00
if (!gMapConnectionFlags.north)
2018-05-09 12:07:56 +02:00
{
return -1;
}
return CONNECTION_NORTH;
}
else
{
return 0;
}
}
int GetPostCameraMoveMapBorderId(int x, int y)
{
return GetMapBorderIdAt(gSaveBlock1Ptr->pos.x + 7 + x, gSaveBlock1Ptr->pos.y + 7 + y);
}
int CanCameraMoveInDirection(int direction)
{
int x, y;
x = gSaveBlock1Ptr->pos.x + 7 + gDirectionToVectors[direction].x;
y = gSaveBlock1Ptr->pos.y + 7 + gDirectionToVectors[direction].y;
2018-05-09 12:07:56 +02:00
if (GetMapBorderIdAt(x, y) == -1)
{
return 0;
}
return 1;
}
void sub_80887F8(struct MapConnection *connection, int direction, int x, int y)
{
struct MapHeader const *mapHeader;
mapHeader = mapconnection_get_mapheader(connection);
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)
{
unsigned int direction;
struct MapConnection *connection;
int old_x, old_y;
gCamera.active = FALSE;
direction = GetPostCameraMoveMapBorderId(x, y);
if (direction + 1 <= 1)
{
gSaveBlock1Ptr->pos.x += x;
gSaveBlock1Ptr->pos.y += y;
}
else
{
save_serialize_map();
ClearMirageTowerPulseBlendEffect();
2018-05-09 12:07:56 +02:00
old_x = gSaveBlock1Ptr->pos.x;
old_y = gSaveBlock1Ptr->pos.y;
connection = sub_8088950(direction, gSaveBlock1Ptr->pos.x, gSaveBlock1Ptr->pos.y);
sub_80887F8(connection, direction, x, y);
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;
sub_80885C4(direction);
}
return gCamera.active;
}
struct MapConnection *sub_8088950(u8 direction, int x, int y)
{
int count;
struct MapConnection *connection;
int i;
count = gMapHeader.connections->count;
connection = gMapHeader.connections->connections;
for (i = 0; i < count; i++, connection++)
{
if (connection->direction == direction && sub_80889A8(direction, x, y, connection) == TRUE)
return connection;
}
return NULL;
}
bool8 sub_80889A8(u8 direction, int x, int y, struct MapConnection *connection)
{
struct MapHeader const *mapHeader;
mapHeader = mapconnection_get_mapheader(connection);
switch (direction)
{
case CONNECTION_SOUTH:
case CONNECTION_NORTH:
2018-06-21 00:41:51 +02:00
return sub_8088A0C(x, gMapHeader.mapLayout->width, mapHeader->mapLayout->width, connection->offset);
2018-05-09 12:07:56 +02:00
case CONNECTION_WEST:
case CONNECTION_EAST:
2018-06-21 00:41:51 +02:00
return sub_8088A0C(y, gMapHeader.mapLayout->height, mapHeader->mapLayout->height, connection->offset);
2018-05-09 12:07:56 +02:00
}
return FALSE;
}
bool8 sub_8088A0C(int x, int src_width, int dest_width, 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)
return TRUE;
return FALSE;
}
int sub_8088A38(int x, int width)
{
if (x >= 0 && x < width)
return TRUE;
return FALSE;
}
int sub_8088A4C(struct MapConnection *connection, int x, int y)
{
struct MapHeader const *mapHeader;
mapHeader = mapconnection_get_mapheader(connection);
switch (connection->direction)
{
case CONNECTION_SOUTH:
case CONNECTION_NORTH:
2018-06-21 00:41:51 +02:00
return sub_8088A38(x - connection->offset, mapHeader->mapLayout->width);
2018-05-09 12:07:56 +02:00
case CONNECTION_WEST:
case CONNECTION_EAST:
2018-06-21 00:41:51 +02:00
return sub_8088A38(y - connection->offset, mapHeader->mapLayout->height);
2018-05-09 12:07:56 +02:00
}
return FALSE;
}
struct MapConnection *sub_8088A8C(s16 x, s16 y)
{
int count;
struct MapConnection *connection;
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)
|| (direction == CONNECTION_NORTH && y > 6)
2018-06-21 00:41:51 +02:00
|| (direction == CONNECTION_SOUTH && y < gMapHeader.mapLayout->height + 7)
2018-05-09 12:07:56 +02:00
|| (direction == CONNECTION_WEST && x > 6)
2018-06-21 00:41:51 +02:00
|| (direction == CONNECTION_EAST && x < gMapHeader.mapLayout->width + 7))
2018-05-09 12:07:56 +02:00
{
continue;
}
if (sub_8088A4C(connection, x - 7, y - 7) == TRUE)
{
return connection;
}
}
}
return NULL;
}
void sub_8088B3C(u16 x, u16 y)
{
gSaveBlock1Ptr->pos.x = x - 7;
gSaveBlock1Ptr->pos.y = y - 7;
}
2018-12-28 18:18:23 +01:00
void GetCameraFocusCoords(u16 *x, u16 *y)
2018-05-09 12:07:56 +02:00
{
*x = gSaveBlock1Ptr->pos.x + 7;
*y = gSaveBlock1Ptr->pos.y + 7;
}
2018-12-28 18:18:23 +01:00
void SetPlayerCoords(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;
}
void sub_8088B94(int x, int y, int a2)
{
2018-12-28 18:18:23 +01:00
if (x >= 0 && x < gBackupMapLayout.width && y >= 0 && y < gBackupMapLayout.height)
2018-05-09 12:07:56 +02:00
{
if (a2 != 0)
2019-02-27 04:56:22 +01:00
gBackupMapLayout.map[x + gBackupMapLayout.width * y] |= METATILE_COLLISION_MASK;
2018-05-09 12:07:56 +02:00
else
2019-02-27 04:56:22 +01:00
gBackupMapLayout.map[x + gBackupMapLayout.width * y] &= ~METATILE_COLLISION_MASK;
2018-05-09 12:07:56 +02:00
}
}
2018-12-28 18:18:23 +01:00
static bool8 SkipCopyingMetatileFromSavedMap(u16* mapMetatilePtr, 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)
mapMetatilePtr -= mapWidth;
2018-05-09 12:07:56 +02:00
else
2018-12-28 18:18:23 +01:00
mapMetatilePtr += mapWidth;
2018-05-09 12:07:56 +02:00
2019-02-27 04:56:22 +01:00
if (sub_80FADE4(*mapMetatilePtr & METATILE_ID_MASK, yMode) == 1)
2018-05-09 12:07:56 +02:00
return TRUE;
return FALSE;
}
void copy_tileset_patterns_to_vram(struct Tileset const *tileset, u16 numTiles, u16 offset)
{
if (tileset)
{
if (!tileset->isCompressed)
LoadBgTiles(2, tileset->tiles, numTiles * 32, offset);
else
decompress_and_copy_tile_data_to_vram(2, tileset->tiles, numTiles * 32, offset, 0);
}
}
void copy_tileset_patterns_to_vram2(struct Tileset const *tileset, u16 numTiles, u16 offset)
{
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
}
}
void nullsub_3(u16 a0, u16 a1)
{
}
void nullsub_90(void)
{
}
void apply_map_tileset_palette(struct Tileset const *tileset, u16 destOffset, u16 size)
{
u16 black = RGB_BLACK;
if (tileset)
{
if (tileset->isSecondary == FALSE)
{
LoadPalette(&black, destOffset, 2);
LoadPalette(((u16*)tileset->palettes) + 1, destOffset + 1, size - 2);
nullsub_3(destOffset + 1, (size - 2) >> 1);
}
else if (tileset->isSecondary == TRUE)
{
LoadPalette(((u16*)tileset->palettes) + (NUM_PALS_IN_PRIMARY * 16), destOffset, size);
2018-05-09 12:07:56 +02:00
nullsub_3(destOffset, size >> 1);
}
else
{
2018-12-19 03:15:59 +01:00
LoadCompressedPalette((u32*)tileset->palettes, destOffset, size);
2018-05-09 12:07:56 +02:00
nullsub_3(destOffset, size >> 1);
}
}
}
2018-06-21 00:41:51 +02:00
void copy_map_tileset1_to_vram(struct MapLayout const *mapLayout)
2018-05-09 12:07:56 +02:00
{
copy_tileset_patterns_to_vram(mapLayout->primaryTileset, NUM_TILES_IN_PRIMARY, 0);
2018-05-09 12:07:56 +02:00
}
2018-06-21 00:41:51 +02:00
void copy_map_tileset2_to_vram(struct MapLayout const *mapLayout)
2018-05-09 12:07:56 +02:00
{
copy_tileset_patterns_to_vram(mapLayout->secondaryTileset, NUM_TILES_TOTAL - NUM_TILES_IN_PRIMARY, NUM_TILES_IN_PRIMARY);
2018-05-09 12:07:56 +02:00
}
2018-06-21 00:41:51 +02:00
void copy_map_tileset2_to_vram_2(struct MapLayout const *mapLayout)
2018-05-09 12:07:56 +02:00
{
copy_tileset_patterns_to_vram2(mapLayout->secondaryTileset, NUM_TILES_TOTAL - NUM_TILES_IN_PRIMARY, NUM_TILES_IN_PRIMARY);
2018-05-09 12:07:56 +02:00
}
2018-06-21 00:41:51 +02:00
void apply_map_tileset1_palette(struct MapLayout const *mapLayout)
2018-05-09 12:07:56 +02:00
{
apply_map_tileset_palette(mapLayout->primaryTileset, 0, NUM_PALS_IN_PRIMARY * 16 * 2);
2018-05-09 12:07:56 +02:00
}
2018-06-21 00:41:51 +02:00
void apply_map_tileset2_palette(struct MapLayout const *mapLayout)
2018-05-09 12:07:56 +02:00
{
apply_map_tileset_palette(mapLayout->secondaryTileset, NUM_PALS_IN_PRIMARY * 16, (NUM_PALS_TOTAL - NUM_PALS_IN_PRIMARY) * 16 * 2);
2018-05-09 12:07:56 +02:00
}
2018-06-21 00:41:51 +02:00
void copy_map_tileset1_tileset2_to_vram(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
{
copy_tileset_patterns_to_vram2(mapLayout->primaryTileset, NUM_TILES_IN_PRIMARY, 0);
copy_tileset_patterns_to_vram2(mapLayout->secondaryTileset, NUM_TILES_TOTAL - NUM_TILES_IN_PRIMARY, NUM_TILES_IN_PRIMARY);
2018-05-09 12:07:56 +02:00
}
}
2018-06-21 00:41:51 +02:00
void apply_map_tileset1_tileset2_palette(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
{
2018-06-21 00:41:51 +02:00
apply_map_tileset1_palette(mapLayout);
apply_map_tileset2_palette(mapLayout);
2018-05-09 12:07:56 +02:00
}
}