#include "game_types.h"

/* BG_SetTilePalette @ 0x08014974 (real 112B) -- bit-loop palette stamp (+11/+13) with OR-12. Mirrors src/matched/BG_SetTilePalette.c. */
/* When word 13 holds the magic, stamps the signed byte of y at entry+11
   and y's low byte at entry+13, ORing 12 into entry[0], per selected flagged
   entry (80-byte stride). */
void BG_SetTilePalette(int *p, unsigned x, unsigned y)
{
    char *base;
    int n, i;
    if (p[13] != 0x68736D53)
        return;
    p[13]++;
    n = ((unsigned char *)p)[8];
    base = (char *)p[11];
    for (i = 0; i < n; i++, base += 80) {
        if ((x & (1u << i)) != 0 && (base[0] & 128) != 0) {
            base[11] = (char)((short)y >> 8);
            base[13] = (char)y;
            base[0] = (char)(base[0] | 12);
        }
    }
    p[13] = 0x68736D53;
}
