Skip to content

Set Metatile IDs From Another Map

ghoulslash edited this page Apr 28, 2023 · 4 revisions

Set Metatile IDs from Another Map

credit to ghoulslash

Step 1

Open fieldmap.c and add this function somewhere:

extern void DrawWholeMapView(void);
void CopyMetatileIdsFromMapLayout(u16 mapGroup, u16 mapNum, const struct UCoords8 *pos)
{
    u32 i, block, x, y;
    struct MapLayout const *layout = Overworld_GetMapHeaderByGroupAndId(mapGroup, mapNum)->mapLayout;
    
    i = 0;
    do {
        x = pos[i].x;
        y = pos[i].y;
        block = layout->map[x + layout->width * y];
        gBackupMapLayout.map[(x + MAP_OFFSET) + (y + MAP_OFFSET) * gBackupMapLayout.width] = block;
        i++;
    } while (pos[i].x != 0xFF);
    
    DrawWholeMapView();
}

It works by taking a map group and number and array of (x,y) positions and copies them into the current map from the given layout. An example of the pos argument:

  • If I want to copy the first 2 columns of a map from y positions 0 through 7:
static const struct UCoords8 sTestPositions[] = {
    {0, 0}, {0, 1}, {0, 2}, {0, 3}, {0, 4}, {0, 5}, {0, 6}, {0, 7},
    {1, 0}, {1, 1}, {1, 2}, {1, 3}, {1, 4}, {1, 5}, {1, 6}, {1, 7},
    {0xFF, 0xFF}, // to signify the end of the array
};
  • So we would call the above with CopyMetatileIdsFromMapLayout(<map map group>, <my map num>, sTestPositions);

Another option - copy rectangular portion

void CopyMetatileIdsFromMapLayoutWithinBounds(u16 mapGroup, u16 mapNum, u8 xmin, u8 ymin, u8 xmax, u8 ymax)
{
    u32 i, j, block;
    struct MapLayout const *layout = Overworld_GetMapHeaderByGroupAndId(mapGroup, mapNum)->mapLayout;

    for (i = xmin; i < xmax; i++) {
        for (j = ymin; j < ymax; j++) {
            block = layout->map[i + layout->width * j];
            gBackupMapLayout.map[(i + MAP_OFFSET) + (j + MAP_OFFSET) * gBackupMapLayout.width] = block;
        }
    }
    DrawWholeMapView();
}
Clone this wiki locally