| A2 is a keeper after a native accepted catch; called only by cooperative
| goalie_ai.s. Restore all registers/CCR. Use native C0F8 assignment semantics
| to return a previously manual keeper to AI and his pad to a free skater.
| Explicit Y after this catch remains available; special modes stay native.
    .equ OBJ,0xFFFFB04A
    .text
    .globl _start
_start:
    move.w %sr,-(%sp)
    movem.l %d0-%d7/%a0-%a6,-(%sp)
    ori.w #0x700,%sr
    tst.w 0x34(%a2)
    bne.w done
    tst.w 0xFFFFD052
    bne.w done
    btst #3,0x62(%a2)
    beq.w done
    btst #0,0xFFFFC2FA
    bne.w done
    tst.w 0xFFFFD412
    bne.w done
    move.b 0xFFFFC2EA,%d0
    andi.b #0x11,%d0
    bne.w done
    move.w 0x52(%a2),%d7
    cmp.w 0xFFFFB7AA,%d7
    bne.w done
    moveq #1,%d4
    cmpi.w #5,%d7
    beq.s side_ready
    moveq #2,%d4
    cmpi.w #11,%d7
    bne.w done
side_ready:
    lea 0xFFFFC328,%a0
    moveq #3,%d0
    moveq #0,%d1
sides:
    move.w (%a0)+,%d2
    beq.s next_side
    cmp.w %d4,%d2
    bne.w done                     | opposing humans retain native control policy
    moveq #1,%d1
next_side:
    dbra %d0,sides
    tst.w %d1
    beq.w done
    lea 0xFFFFC320,%a4
    moveq #3,%d0
find_pad:
    cmp.w (%a4)+,%d7
    dbeq %d0,find_pad
    bne.w done
    lea OBJ,%a0
    cmpi.w #1,%d4
    beq.s objects
    adda.w #0x300,%a0
objects:
    moveq #-1,%d5                  | selected object
    moveq #-1,%d6                  | unsigned minimum squared distance
    moveq #5,%d3
candidate:
    tst.w 0x34(%a0)
    ble.s next_object
    btst #3,0x62(%a0)
    bne.s next_object              | never another human's player
    btst #2,0x63(%a0)
    bne.s next_object
    btst #5,0x62(%a0)
    bne.s next_object
    move.w (%a0),%d0
    sub.w (%a2),%d0
    muls.w %d0,%d0
    move.w 0x14(%a0),%d1
    sub.w 0x14(%a2),%d1
    muls.w %d1,%d1
    add.l %d1,%d0
    cmp.l %d6,%d0
    bhi.s next_object
    move.l %d0,%d6
    move.w 0x52(%a0),%d5
next_object:
    adda.w #0x80,%a0
    dbra %d3,candidate
    | With no free skater, native -1 detaches the pad safely; keeper still becomes AI.
    move.b 0x63(%a2),-(%sp)
    bclr #3,0x63(%a2)              | native assignment must not reject a passing/recovery keeper
    move.w %d7,%d1
    move.w %d5,%d0
    jsr 0xC0F8                     | clears keeper human flag, arms ordinary AI job, selects skater
    move.b (%sp)+,0x63(%a2)
    lea 0xFFFFC320,%a0
    lea 0xFFFFC328,%a1
    moveq #3,%d1
assign:
    move.w (%a1)+,%d2
    cmp.w (%a0),%d7
    bne.s next_pad
    cmp.w %d4,%d2
    bne.s next_pad
    move.w %d0,(%a0)               | also updates any temporary native P3/P4 mirror of this keeper
next_pad:
    addq.l #2,%a0
    dbra %d1,assign
done:
    movem.l (%sp)+,%d0-%d7/%a0-%a6
    move.w (%sp)+,%sr
    rts
