.gba
.open "ZM_U.gba","LedgeVault.gba",0x8000000

.definelabel FreeSpace,filesize("ZM_U.gba") + 0x8000000

.definelabel SamusDataCopy,0x30013F4
.definelabel SamusEquipment,0x3001530
.definelabel ProcessClipdataForSamus,0x8057df8
.definelabel CheckMorphed,0x80104a8

.org 0x8005900 ; Side block detected
;    ldr r1,=FreeSpace
;    mov pc,r1
;    .pool
    ldrh r3,[r6,#0x12] ; Need pevious position
    strh r0,[r6,#0x12]
		ldr r0,=FreeSpace
    mov pc,r0
    .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]
		mov r1,r7
    add r1,#0x58
		mov r5,r3
    ldrb r0,[r1,#0x0]
    add r0,#0x1
    strb r0,[r1,#0x0] ; Touching side block flag

;Check for power grip. Comment out the lines below to make ledge vault possible without power grip    
		ldr r0,=SamusEquipment
		ldrb r0,[r0,#0xF]
		mov r1,80h
		and r0,r1
		cmp r0,0h
		bne @@Continue1
		b @@Return
;End of check for power grip

@@Continue1:
    ldr r0,=CheckMorphed
    bl CallR0
    cmp r0,#0x0
		beq @@Continue2
    b @@Return

@@Continue2:		
		ldrh r0,[r6,#0x14] ; r6 = Samus data
    add r0,#0x40
    ldrh r1,[r6,#0x12]
    ldr r2,=ProcessClipdataForSamus ; Block below samus
    bl CallR2
		mov r1,#0x80
    lsl r1,#0x11
    and r0,r1
		cmp r0,#0x0
		bne @@Continue3
    b @@Return ; If not solid

@@Continue3:
		; r4 has the hitbox offset		
    ldrh r0,[r6,#0x14] ; r6 = Samus data
    sub r0,#0x40 ; Block above
    add r1,r5,r4 ; r1 = X + Offset
    ldr r2,=ProcessClipdataForSamus ; Bottom block
    bl CallR2
		mov r1,#0x80
    lsl r1,#0x11
    and r0,r1
    cmp r0,#0x0
		beq @@OneTileLedge; If not 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
    cmp r0,#0x0
		bne @@Return ; If solid
		
		ldrh r0,[r6,#0x14] ; r6 = Samus data
    sub r0,#0x80 ; 2 Blocks above
    ldrh r1,[r6,#0x12]
		ldr r2,=ProcessClipdataForSamus
    bl CallR2
		mov r1,#0x80
    lsl r1,#0x11
    and r0,r1
    cmp r0,#0x0
		bne @@Return ; If solid

		ldrh r0,[r6,#0x14] ; r6 = Samus data
    sub r0,#0x0C0 ; 3 Blocks above
    add r1,r5,r4 ; r1 = X + Offset
    ldr r2,=ProcessClipdataForSamus
    bl CallR2
		mov r1,#0x80
    lsl r1,#0x11
    and r0,r1
    cmp r0,#0x0
		bne @@TwoTilePullInMorphTunnel ; If solid
		
		ldrh r0,[r6,#0x14] ; r6 = Samus data
    sub r0,#0x0C0 ; 3 Blocks above
    ldrh r1,[r6,#0x12]
		ldr r2,=ProcessClipdataForSamus
    bl CallR2
		mov r1,#0x80
    lsl r1,#0x11
    and r0,r1
    cmp r0,#0x0
		bne @@TwoTilePullInMorphTunnel ; If solid

		b @@TwoTilePullForward

@@OneTileLedge:
		ldrh r0,[r6,#0x14] ; r6 = Samus data
    add r1,r5,r4 ; r1 = X + Offset
    ldr r2,=ProcessClipdataForSamus ; Bottom block
    bl CallR2
		mov r1,#0x80
    lsl r1,#0x11
    and r0,r1
    cmp r0,#0x0
		beq @@Return ; If not 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
    cmp r0,#0x0
		bne @@OneTilePullInMorphTunnel ; If solid
		
		ldrh r0,[r6,#0x14] ; r6 = Samus data
    sub r0,#0x80 ; 2 Blocks above
    ldrh r1,[r6,#0x12]
		ldr r2,=ProcessClipdataForSamus
		bl CallR2
    mov r1,#0x80
    lsl r1,#0x11
    and r0,r1
    cmp r0,#0x0
		bne @@OneTilePullInMorphTunnel ; If solid
    
@@OneTilePullForward:
		mov r0,#0x1B ; Pulling youself forward
		ldrh r1,[r6,#0x14]
    sub r1,#0x40
    strh r1,[r6,#0x14]
		b @@LedgeReturn
		
@@OneTilePullInMorphTunnel:
		ldr r0,=SamusEquipment
		ldrb r0,[r0,#0xF]
		mov r1,40h
		and r0,r1
		cmp r0,0h
		beq @@Return
		mov r0,#0x1C ; Pulling youself into morphball tunnel
		ldrh r1,[r6,#0x14]
    add r1,#0x40
    strh r1,[r6,#0x14]
		b @@LedgeReturn

@@TwoTilePullForward:
		mov r0,#0x1B ; Pulling youself forward
		ldrh r1,[r6,#0x14]
    sub r1,#0x80
    strh r1,[r6,#0x14]
		b @@LedgeReturn
		
@@TwoTilePullInMorphTunnel:
		ldr r0,=SamusEquipment
		ldrb r0,[r0,#0xF]
		mov r1,40h
		and r0,r1
		cmp r0,0h
		beq @@Return
		mov r0,#0x1C ; Pulling youself into morphball tunnel

@@LedgeReturn:
		pop r2,r5
    ldr r1,=SamusDataCopy
    strb r2,[r1,#0xA]
    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