#include "game_types.h"

/* Sub_0800F8EC @ 0x0800F8EC (real 132B) -- latch/counter gate into gap call and publish. Mirrors src/matched/Sub_0800F8EC.c. */
/* First-run latch at +0x1C22 (via the mailbox halfword), then a counter at
   +0x1C24 that increments only on mailbox value 15 and otherwise clears;
   past 60 it resets both, runs the gap call, publishes [p+0x988] to word 1,
   and zeroes the 0x144 mailbox halfword. */
extern void Sub_0800F8EC_GapCall(int *a, int b, int c, int d);

void Sub_0800F8EC(int *p)
{
    int *latch = (int *)((char *)p + 0x1C22);
    if (*latch == 0) {
        if (*(unsigned short *)*(int **)0x02000150 != 0)
            return;
        *(unsigned char *)latch = 1;
        return;
    }
    {
        int *ctr = (int *)((char *)p + 0x1C24);
        if (*(unsigned short *)*(int **)0x02000150 == 15)
            *ctr = *ctr + 1;
        else
            *ctr = 0;
        if (*ctr <= 60)
            return;
        *(unsigned char *)latch = 0;
        *ctr = 0;
    }
    Sub_0800F8EC_GapCall((int *)((char *)p + 2464), 2, 0, 30);
    p[1] = *(int *)((char *)p + 0x988);
    *(unsigned short *)*(int **)0x02000144 = 0;
}
