#include "game_types.h"

/* BG_SetTileAttr @ 0x080149E8 (real 100B) -- bit-loop tile-attr stamp (+21) with OR-3. Mirrors src/matched/BG_SetTileAttr.c. */
/* When word 13 holds the magic, stamps byte y at entry+21 and ORs 3 into
   entry[0] for each selected flagged entry (80-byte stride). */
void BG_SetTileAttr(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[21] = (char)y;
            base[0] = (char)(base[0] | 3);
        }
    }
    p[13] = 0x68736D53;
}
