#include "game_types.h"

/* Sub_0800F2D8 @ 0x0800F2D8 (real 178B) -- emitter init twin with gap calls. Mirrors src/matched/Sub_0800F2D8.c. */
/* InitEmitters-shaped: blob 0x080CF4F8 unpack, gap DrawAt (120, 152), count
   word-14 select (4096 reads the short lane, else the count itself, else
   zeros), gap DrawAt (72, 80), gap UpdateSubPositions. Gap callees are raw
   bl bytes to unmapped spans; verify each target, never assume. */
extern void Sub_0800F2D8_LoadLevelData(int *buf); /* gap call, raw bl */
extern void Sub_0800F2D8_DrawAt(int *base, int *buf, int x, int y);
extern void InitSubPositions(int *buf, int x, int y);
extern void UpdateSubPositions(int *buf, int mode);

void Sub_0800F2D8(int *p)
{
    int *base = *(int **)0x02000134;
    unsigned char *blob = (unsigned char *)0x080CF4F8;
    int buf[7];
    int n = p[14];
    Sub_0800F2D8_LoadLevelData(buf);
    buf[1] = 0x080CF4F8;
    buf[2] = blob[4] | (blob[5] << 8) | (blob[6] << 16) | (blob[7] << 24);
    buf[3] = blob[8] | (blob[9] << 8) | (blob[10] << 16) | (blob[11] << 24);
    buf[0] = -1;
    Sub_0800F2D8_DrawAt(base, buf, 120, 152);
    if (n <= 0) {
        InitSubPositions(buf, 0, 0);
    } else if (n == 4096) {
        int *t = (int *)buf[3];
        int k = (short)(*(unsigned char *)((char *)t + 6) |
                        (*(unsigned char *)((char *)t + 7) << 8)) - 1;
        InitSubPositions(buf, 1, k);
    } else {
        InitSubPositions(buf, 1, n);
    }
    buf[0] = -1;
    Sub_0800F2D8_DrawAt(base, buf, 72, 80);
    UpdateSubPositions(buf, 2);
}
