.gba
.open "fusion.gba","MFFanBlocks.gba",0x8000000

.definelabel FanLeft,0x78
.definelabel FanRight,0x79
.definelabel FanUp,0x7A
.definelabel FanDown,0x7B
.definelabel Fan,0x7C

.definelabel DecompClipdata,0x2026000
.definelabel RoomWidth,0x30000A0

;This asm adds clipdata that functions like Samus Returns' Bombs and Fan system. Bombs 
;can be manipulated to move based on fan direction, allowing for puzzles and barriers.

;FanLeft sucks the bomb leftwards
;FanRight sucks the bomb rightwards
;FanUp sucks the bomb upwards
;FanDown sucks the bomb downwards
;Fan detonates bomb

; new code
.org 0x80F9A28   ; unused sound sample
CheckBombClip:
	push 	r2,r14
	ldrb	r0,[r6,12h]
	cmp 	r0,0		;skip if bomb is spawning
	beq	@@Return	;reset's accel timer for ram slot
	cmp	r0,3h		;skip if bomb detonated
	beq	@@Reset
	ldrh	r0,[r6,8h]	;Bomb Y
	ldrh	r1,[r6,0Ah]	;Bomb X
	lsr     r0,r0,6 
	lsr     r1,r1,6        
	ldr     r2,=RoomWidth
	ldrh    r2,[r2]        
	mul     r2,r0
	add     r2,r2,r1
	lsl     r2,r2,1
	ldr     r0,=DecompClipdata
	add     r1,r0,r2
	ldrh    r0,[r1]		;clipdata the bomb is on
	cmp	r0,FanLeft	;this takes up less space than setting up a loop, so while gross, it's better
	beq	@@FanLeft
	cmp	r0,FanRight
	beq	@@FanRight
	cmp	r0,FanUp
	beq	@@FanUp
	cmp	r0,FanDown
	beq	@@FanDown
	cmp	r0,Fan
	beq	@@Fan
	b	@@Return
.pool
@@FanLeft:
	ldrh	r0,[r6,0Ah]	;bomb X
	bl	FindSlot
	sub	r0,r0,r2
	strh	r0,[r6,0Ah]
	b	@@Return
@@FanRight:
	ldrh	r0,[r6,0Ah]	;bomb X
	bl	FindSlot
	add	r0,r0,r2
	strh	r0,[r6,0Ah]
	b	@@Return
@@FanUp:
	ldrh	r0,[r6,8h]	;bomb Y
	bl	FindSlot
	sub	r0,r0,r2
	strh	r0,[r6,8h]
	b	@@Return
@@FanDown:
	ldrh	r0,[r6,8h]	;bomb Y
	bl	FindSlot
	add	r0,r0,r2
	strh	r0,[r6,8h]
	b	@@Return
@@Fan:	
	mov	r0,1h
	strb	r0,[r6,1Eh]
	add	r0,1h
	strb	r0,[r6,12h]	;detonate bomb
@@Reset:
	bl	FindSlot
	mov	r0,0h
	strb	r0,[r1]
@@Return:
	pop	r2
	mov	r4,r6
	ldrb	r0,[r4,12h]
	pop	r1
	bx	r1


FindSlot:			;this routine is used to give each projectile slot an extra RAM slot for an acceleration timer
	push	r0
	mov	r1,20h
	mov	r2,0	
	ldr	r0,=3000960h
@@Continue:
	cmp	r0,r4
	bne	@@Loop
	ldr	r1,=3001750h	;extra ram base (not sure if these are unused in vanilla)
	add	r1,r1,r2	;extra ram address based on projectile slot
	ldrb	r2,[r1]
	cmp	r2,0Ch		;max accel speed (divides by 4)
	beq	@@Return
	add	r2,1h
	strb	r2,[r1]
	lsr	r2,r2,1h	;slows acceleration
@@Return:
	pop	r0
	bx	r14	

@@Loop:
	add 	r2,1h
	add	r0,r0,r1
	b	@@Continue
.pool


	


; hijack code at start of bomb processing code
.org 0x808575E
	bl      CheckBombCLip
	
; fix clipdata collision table
.org 0x83F0834 + FanLeft
	.byte 0,0,0,0		;blowing fan blocks
	.byte 1			;fan block

; fix clipdata behavior table
.org 0x83F11E8 + (FanLeft * 2)
	.halfword 0,0,0,0,0	;all blocks
	
.close
