#include "game_types.h"

/* BG_SetMultiAttr @ 0x08014AE8 (real 110B) -- bit-loop tile-attr stamp (+25) over flagged entries. Mirrors src/matched/BG_SetMultiAttr.c. */
/* When word 13 holds the magic, bumps it and stamps byte y at entry+25 for
   each of the n = p[8] entries (80-byte stride) selected by bitmask x whose
   flag byte has bit 128; entries with y == 0 get ResetTileFlags instead.
   Restores the magic on exit. */
extern void BG_ResetTileFlags(char *base);

void BG_SetMultiAttr(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[25] = (char)y;
            if (y == 0)
                BG_ResetTileFlags(base);
        }
    }
    p[13] = 0x68736D53;
}
