#include "game_types.h"

/* Physics_ApplyDamping @ 0x08019EC0 (real 128B) -- mode-dispatched damping accumulate. Mirrors src/matched/Physics_ApplyDamping.c. */
/* Damps a1 by the mode: mode 1 accumulates the slot table (0x02001518,
   missing slot returns -1); mode 2 accumulates SVC 12 over the bounce
   result; the tail runs SVC 10 over (bounce, acc), stores acc back unless
   the slot is missing or the service is nonzero, and returns -1/acc. */
extern int Physics_BounceReflect(int x);
extern int Object_Find(int x);
extern int SWI_AB(int service, void *arg); /* swi #0xab */

int Physics_ApplyDamping(int a0, int a1, int mode)
{
    int t = Physics_BounceReflect(a0);
    int slot = Object_Find(t);
    int acc = a1;
    int *tab = (int *)0x02001518;
    int out[2];
    int r;
    if (mode == 1) {
        if (slot == 20)
            return -1;
        acc += *(int *)((char *)tab + slot * 8 + 4);
        mode = 0;
    }
    if (mode == 2)
        acc += SWI_AB(12, &t);
    out[0] = Physics_BounceReflect(a0);
    out[1] = acc;
    r = SWI_AB(10, out);
    if (slot != 20 && r == 0)
        *(int *)((char *)tab + slot * 8 + 4) = acc;
    return r != 0 ? -1 : acc;
}
