.gba
.open "ZM_U.gba","LedgeVault.gba",0x8000000

.definelabel FreeSpace,filesize("ZM_U.gba") + 0x8000000

.definelabel SamusDataCopy,0x30013F4
.definelabel ProcessClipdataForSamus,0x8057df8
.definelabel CheckMorphed,0x80104a8

.org 0x80058fc ; Side block detected
    ldr r1,=FreeSpace
    mov pc,r1
    .pool

.org FreeSpace
    push r2,r5 ; Save r2 and r5
    add r0,sp,#0xC ; 0xC instead of vanilla 0x4 because we pushed to stack
    ldrh r0,[r0,#0x0]
    ldrh r5,[r6,#0x12] ; Need pevious position
    strh r0,[r6,#0x12]
    mov r1,r7
    add r1,#0x58
    ldrb r0,[r1,#0x0]
    add r0,#0x1
    strb r0,[r1,#0x0] ; Touching side block flag
    
    ldr r0,=CheckMorphed
    bl CallR0
    cmp r0,#0x0
    bne @@Return

    ldrh r0,[r6,#0x14] ; r6 = Samus data
    add r0,#0x40
    ldrh r1,[r6,#0x12]

    ldr r2,=ProcessClipdataForSamus
    bl CallR2 ; Block below samus
    cmp r0,#0x0
    beq @@Return ; If not solid

    ; r4 has the hitbox offset
    ldrh r0,[r6,#0x14] ; r6 = Samus data
    add r1,r5,r4 ; r1 = X + Offset

    ldr r2,=ProcessClipdataForSamus
    bl CallR2 ; Bottom block
    mov r1,#0x80
    lsl r1,#0x11
    and r0,r1
    beq @@Return ; If not solid

    ldrh r0,[r6,#0x14] ; r6 = Samus data
    sub r0,#0x40 ; Block above
    add r1,r5,r4 ; r1 = X + Offset

    ldr r2,=ProcessClipdataForSamus
    bl CallR2 ; Bottom block
    mov r1,#0x80
    lsl r1,#0x11
    and r0,r1
    bne @@Return ; If solid

    ldrh r0,[r6,#0x14] ; r6 = Samus data
    sub r0,#0x80 ; 2 Blocks above
    add r1,r5,r4 ; r1 = X + Offset

    ldr r2,=ProcessClipdataForSamus
    bl CallR2
    mov r1,#0x80
    lsl r1,#0x11
    and r0,r1
    bne @@Return ; If not solid
    mov r0,#0x1B ; Pulling youself forward

    pop r2,r5
    ldr r1,=SamusDataCopy
    strb r2,[r1,#0xA]
    ldrh r1,[r6,#0x14]
    sub r1,#0x40
    strh r1,[r6,#0x14]
    ldr r1,=0x8005ac4
    mov pc,r1

    @@Return:
    pop r2,r5
    ldr r0,=0x800590c
    mov pc,r0
    .pool

CallR0:
    mov pc,r0

CallR2:
    mov pc,r2

.close