#include "game_types.h"

/* InitSubPositions @ 0x08000AE4 (real 190B) -- keyframe select walk with SWI map. Mirrors src/matched/InitSubPositions.c. */
/* Selects an animation frame: keys the (b) entry for a SWI-mapped target,
   then walks its counted sub-entries subtracting thresholds until one
   exceeds the remainder, publishing the resolved (ptr, LE32, LE32) triple.
   (The trailing zero block in the span is the next function's head.) */
extern int SWI_Call(int x);

void InitSubPositions(int *a, int b, int c)
{
    int *e = (int *)((char *)(int *)a[2] + b * 4);
    int key = (short)(*(unsigned char *)((char *)*e + 6) |
                      (*(unsigned char *)((char *)*e + 7) << 8));
    int r = c & 0x7FFF;
    int *e2;
    int count, i;
    unsigned char *r2;
    if (r >= key)
        r = SWI_Call(r);
    e2 = (int *)a[2];
    r2 = (unsigned char *)e2 + 8;
    count = (short)(*(unsigned char *)((char *)e2 + 4) |
                    (*(unsigned char *)((char *)e2 + 5) << 8));
    for (i = 0; i < count; i++, r2 += 4) {
        int v = (short)(r2[2] | (r2[3] << 8));
        if (r >= v) {
            r -= v;
            continue;
        }
        {
            unsigned w = (unsigned)r2[0] | ((unsigned)r2[1] << 8);
            int *dst = *(int **)(w * 4 + (unsigned)a[1]);
            a[3] = (int)dst;
            a[4] = (int)(*(unsigned char *)((char *)dst + 8) |
                         (*(unsigned char *)((char *)dst + 9) << 8) |
                         (*(unsigned char *)((char *)dst + 10) << 16) |
                         (*(unsigned char *)((char *)dst + 11) << 24));
            a[5] = (int)(r2[4] | (r2[5] << 8) |
                         (r2[6] << 16) | (r2[7] << 24));
        }
        return;
    }
}
