;*** FIRE add ***
; HL = slot
; BC = position
; DE = table
.FIRE_Add
	ld A,(HL)
	or A
	ret nz
	
	ld (HL),15	;max direction changes
	inc HL
	ld (HL),C
	inc HL
	ld (HL),B
	inc HL
	ld (HL),E
	inc HL
	ld (HL),D
	
	ld A,sfx_fire
	call SND_SfxPlay
	
	ret


;*** FIRE WEAPON ***

.FIRE_Run
	ld IX,FIRE_A
	call FIRE_RunEntry
	ld IX,FIRE_B
	
.FIRE_RunEntry
	ld A,(IX)
	or A
	ret z
	
	ld L,(IX+3)
	ld H,(IX+4)
	
.FIRE_RunSolution
	ld C,(IX+1)
	ld B,(IX+2)
	
	ld E,(HL)
	inc HL
	ld D,(HL)
	inc HL
	;check if not last
	ld A,D
	or E
	jr nz,FIRE_RunStillSolution
	;no more solution ,do not update !
.FIRE_RunKill
	ld (IX),0
	ret
	
.FIRE_RunStillSolution
	;add to new pos and check bounds
	ld A,B	;y
	add D
	cp 20
	jr nc,FIRE_RunKill
	ld B,A
	ld A,C
	add E
	cp 32
	jr nc,FIRE_RunKill
	ld C,A
	call COLL_CheckPositionDE	;check if we have something
	jr nc,FIRE_RunNoCollision
	or A						;is null master ?
	jr z,FIRE_Collision		
	cp 32						;is egg ?
	jr nz,FIRE_RunNoCollision
.FIRE_Collision
	;so there is something solid, try next solution
	inc HL
	inc HL	;skip solution address
	
;remove a solution in count
	ld A,(IX+0)
	or A
	jr z,FIRE_RunSolution 	;already dead !
	dec (IX+0)				
	jr FIRE_RunSolution 
	
.FIRE_RunNoCollision
	ld A,(HL)	;get new solution address
	inc HL		;and set as new solution table
	ld (IX+3),A
	ld A,(HL)
	ld (IX+4),A
	
	;write x & y
	ld (IX+1),C
	ld (IX+2),B
	
	;now compute current laser slot
	;ld A,(current_laser) ;get segment
	;inc A
	;and 31
	;ld (current_laser),A
	LaserNextSegment
	
	ld L,A
	add A	;2
	add A	;4
	add L	;5
	ld L,A
	ld H,LaserBuffer_High ;current laser in HL

fire_life	EQU $+1	
	ld (HL),10	;life
	inc L
	ld (HL),E	;cell address low
	inc L
	ld (HL),D	;high
	inc L
fire_spriteA	EQU $+1
	ld (HL),Fire_lopow_sprite AND 255
	inc L
fire_spriteB	EQU $+1	
	ld (HL),Fire_lopow_sprite / 256
	
	ret

.FIRE_A
	db	0	;current state
	dw	0	;w,y
	dw	FIRE_UpTable	;current move
	
.FIRE_B
	db	0	;current state
	dw	0	;w,y
	dw	FIRE_DnTable	;current move

;solution table
; db movx,movy solution to try
; dw nextaction next table to use for this solution
; finishes with db 0,0

.FIRE_UpTable
	db	0,-1	;up
	dw	FIRE_UpTable
	db	1,0		;right
	dw	FIRE_UpTable
	dw	0,1		;continue down
	dw	FIRE_UpTable	;change next action
	db	0,0	;end
.FIRE_UpTableB
	db	1,0		;right first this time
	dw	FIRE_UpTable	;return to table A
	db	0,1		;continue down
	dw	FIRE_UpTableB
	db	0,0		;no more solution
	
.FIRE_DnTable
	db	0,1
	dw	FIRE_DnTable
	db	1,0
	dw	FIRE_DnTable
	db	0,-1
	dw	FIRE_DnTableB
	db 0,0
.FIRE_DnTableB
	db	1,0
	dw	FIRE_DnTable
	db	0,-1
	dw	FIRE_DnTableB
	db 0,0

;*** reversed ***
	
.FIRE_UpTable_rev
	db	0,-1	;up
	dw	FIRE_UpTable_rev
	db	-1,0		;left
	dw	FIRE_UpTable_rev
	dw	0,1		;continue down
	dw	FIRE_UpTableB_rev	;change next action
	db	0,0	;end
.FIRE_UpTableB_rev
	db	-1,0		;left first this time
	dw	FIRE_UpTable_rev	;return to table A
	db	0,1		;continue down
	dw	FIRE_UpTableB_rev
	db	0,0		;no more solution
	
.FIRE_DnTable_rev
	db	0,1
	dw	FIRE_DnTable_rev
	db	-1,0
	dw	FIRE_DnTable_rev
	db	0,-1
	dw	FIRE_DnTableB_rev
	db 0,0
.FIRE_DnTableB_rev
	db	-1,0
	dw	FIRE_DnTable_rev
	db	0,-1
	dw	FIRE_DnTableB_rev
	db 0,0