Skip to content

Dynamic overworld palette system

ExpoSeed edited this page Apr 20, 2021 · 9 revisions

NOTE: This tutorial is out of date: The most up to date code at time of writing can be found here. This also fixes some of the problems with the below system, like broken reflections and berry trees.

TODO: Update this tutorial.


For this tutorial we will be editing one function within src/overworld.c and three functions within src/event_object_movement.c to make it easier when adding new overworlds, with new palettes.

The current implementation breaks the reflection palette system, though for ease of adding new overworlds that should be adapted so it is dynamic as well, rather than requiring explicit reflection palettes.

Note: Function sub_808E894() has been decompiled so depending on your version it might be called LoadObjectEventPalette() instead.

overworld.c

sub_8086988()

static void sub_8086988(u32 a1)
{
    ResetTasks();
    ResetSpriteData();    ResetPaletteFade();
    ScanlineEffect_Clear();
    ResetAllPicSprites();
    ResetCameraUpdateInfo();
    InstallCameraPanAheadCallback();
+   FreeAllSpritePalettes();
-   if (!a1)
-       InitObjectEventPalettes(0);
-   else
-       InitObjectEventPalettes(1);

    FieldEffectActiveListClear();
    StartWeather();
    ResumePausedWeather();
    if (!a1)
        SetUpFieldTasks();
    RunOnResumeMapScript();
    TryStartMirageTowerPulseBlendEffect();
}

event_object_movement.c

TrySetupObjectEventSprite()

static u8 TrySetupObjectEventSprite(struct ObjectEventTemplate *objectEventTemplate, struct SpriteTemplate *spriteTemplate, u8 mapNum, u8 mapGroup, s16 cameraX, s16 cameraY)
{
    u8 spriteId;
-   u8 paletteSlot;
    u8 objectEventId;
    struct Sprite *sprite;
    struct ObjectEvent *objectEvent;
    const struct ObjectEventGraphicsInfo *graphicsInfo;

    objectEventId = InitObjectEventStateFromTemplate(objectEventTemplate, mapNum, mapGroup);
    if (objectEventId == OBJECT_EVENTS_COUNT)
        return OBJECT_EVENTS_COUNT;

    objectEvent = &gObjectEvents[objectEventId];
    graphicsInfo = GetObjectEventGraphicsInfo(objectEvent->graphicsId);
+   if (spriteTemplate->paletteTag != 0xffff)
+   {
+       LoadObjectEventPalette(spriteTemplate->paletteTag);
+   }
-   paletteSlot = graphicsInfo->paletteSlot;
-   if (paletteSlot == 0)
-   {
-       LoadPlayerObjectReflectionPalette(graphicsInfo->paletteTag1, 0);
-   }
-   else if (paletteSlot == 10)
-   {
-       LoadSpecialObjectReflectionPalette(graphicsInfo->paletteTag1, 10);
-   }
-   else if (paletteSlot >= 16)
-   {
-       paletteSlot -= 16;
-       sub_808EAB0(graphicsInfo->paletteTag1, paletteSlot);
-   }
    if (objectEvent->movementType == MOVEMENT_TYPE_INVISIBLE)
        objectEvent->invisible = TRUE;
    
-   *(u16 *)&spriteTemplate->paletteTag = 0xFFFF;
    spriteId = CreateSprite(spriteTemplate, 0, 0, 0);
    if (spriteId == MAX_SPRITES)
    {
        gObjectEvents[objectEventId].active = FALSE;
        return OBJECT_EVENTS_COUNT;
    }
    sprite = &gSprites[spriteId];
    sub_8092FF0(objectEvent->currentCoords.x + cameraX, objectEvent->currentCoords.y + cameraY, &sprite->pos1.x, &sprite->pos1.y);
    sprite->centerToCornerVecX = -(graphicsInfo->width >> 1);
    sprite->centerToCornerVecY = -(graphicsInfo->height >> 1);
    sprite->pos1.x += 8;
    sprite->pos1.y += 16 + sprite->centerToCornerVecY;
-   sprite->oam.paletteNum = paletteSlot;
    sprite->coordOffsetEnabled = TRUE;
    sprite->data[0] = objectEventId;
    objectEvent->spriteId = spriteId;
    objectEvent->inanimate = graphicsInfo->inanimate;
    if (!objectEvent->inanimate)
        StartSpriteAnim(sprite, GetFaceDirectionAnimNum(objectEvent->facingDirection));
    
    SetObjectSubpriorityByZCoord(objectEvent->previousElevation, sprite, 1);
    UpdateObjectEventVisibility(objectEvent, sprite);
    return objectEventId;
}

RemoveObjectEventInternal()

static void RemoveObjectEventInternal(struct ObjectEvent *objectEvent)
{
+   u8 paletteNum;

    struct SpriteFrameImage image;
    image.size = GetObjectEventGraphicsInfo(objectEvent->graphicsId)->size;
    gSprites[objectEvent->spriteId].images = ℑ
+   paletteNum = gSprites[objectEvent->spriteId].oam.paletteNum;
    DestroySprite(&gSprites[objectEvent->spriteId]);
+   FieldEffectFreePaletteIfUnused(paletteNum);

}

sub_808E1B8()

static void sub_808E1B8(u8 objectEventId, s16 x, s16 y)
{
    u8 spriteId;
-   u8 paletteSlot;
    struct Sprite *sprite;
    struct ObjectEvent *objectEvent;
    struct SpriteTemplate spriteTemplate;
    struct SpriteFrameImage spriteFrameImage;
    const struct SubspriteTable *subspriteTables;
    const struct ObjectEventGraphicsInfo *graphicsInfo;

#define i spriteId
    for (i = 0; i < ARRAY_COUNT(gLinkPlayerObjectEvents); i++)
    {
        if (gLinkPlayerObjectEvents[i].active && objectEventId == gLinkPlayerObjectEvents[i].objEventId)
        {
            return;
        }
    }
#undef i

    objectEvent = &gObjectEvents[objectEventId];
    subspriteTables = NULL;
    graphicsInfo = GetObjectEventGraphicsInfo(objectEvent->graphicsId);
    spriteFrameImage.size = graphicsInfo->size;
    MakeObjectTemplateFromObjectEventGraphicsInfoWithCallbackIndex(objectEvent->graphicsId, objectEvent->movementType, &spriteTemplate, &subspriteTables);
    spriteTemplate.images = &spriteFrameImage;
+   if (spriteTemplate.paletteTag != 0xffff)
+   {
+       LoadObjectEventPalette(spriteTemplate.paletteTag);
+   }
-   *(u16 *)&spriteTemplate.paletteTag = 0xffff;
-   paletteSlot = graphicsInfo->paletteSlot;
-   if (paletteSlot == 0)
-   {
-       LoadPlayerObjectReflectionPalette(graphicsInfo->paletteTag1, graphicsInfo->paletteSlot);
-   }
-   else if (paletteSlot == 10)
-   {
-       LoadSpecialObjectReflectionPalette(graphicsInfo->paletteTag1, graphicsInfo->paletteSlot);
-   }
-   else if (paletteSlot >= 16)
-   {
-       paletteSlot -= 16;
-       sub_808EAB0(graphicsInfo->paletteTag1, paletteSlot);
-   }
-   *(u16 *)&spriteTemplate.paletteTag = 0xffff;
    spriteId = CreateSprite(&spriteTemplate, 0, 0, 0);
    if (spriteId != MAX_SPRITES)
    {
        sprite = &gSprites[spriteId];
        sub_8092FF0(x + objectEvent->currentCoords.x, y + objectEvent->currentCoords.y, &sprite->pos1.x, &sprite->pos1.y);
        sprite->centerToCornerVecX = -(graphicsInfo->width >> 1);
        sprite->centerToCornerVecY = -(graphicsInfo->height >> 1);
        sprite->pos1.x += 8;
        sprite->pos1.y += 16 + sprite->centerToCornerVecY;
        sprite->images = graphicsInfo->images;
        if (objectEvent->movementType == MOVEMENT_TYPE_PLAYER)
        {
            SetPlayerAvatarObjectEventIdAndObjectId(objectEventId, spriteId);
            objectEvent->warpArrowSpriteId = CreateWarpArrowSprite();
        }
        if (subspriteTables != NULL)
        {
            SetSubspriteTables(sprite, subspriteTables);
        }
-       sprite->oam.paletteNum = paletteSlot;
        sprite->coordOffsetEnabled = TRUE;
        sprite->data[0] = objectEventId;
        objectEvent->spriteId = spriteId;
        if (!objectEvent->inanimate && objectEvent->movementType != MOVEMENT_TYPE_PLAYER)
        {
            StartSpriteAnim(sprite, GetFaceDirectionAnimNum(objectEvent->facingDirection));
        }
        sub_808E38C(objectEvent);
        SetObjectSubpriorityByZCoord(objectEvent->previousElevation, sprite, 1);
    }
}
Clone this wiki locally