;add a wave
; HL = slot
;BC = coord
; D = direction
.WAVE_Add
	ld A,(HL)	;is first slot empty ?
	or A
	jr z,WAVE_Add_SlotOK
	ld A,4
	add L
	ld L,A
	ld A,(HL)
	or A
	ret nz		;if second slot is not empty ,return !
	
.WAVE_Add_SlotOK
	ld A,5
	ld (HL),A
	inc L
	ld (HL),D
	inc L
	ld (HL),C
	inc L
	ld (HL),B
	
	ld A,sfx_circular
	call SND_SfxPlay
	
	ret

;***********************************
;*** red/blue wave (red powerup low) ***
;***********************************

.WAVE_RunCallback

	bit 6,(HL)
	jr nz,WAVE_RunKillEgg

	push AF
	push HL
	push DE
	
	inc H	;get master
	inc H
	inc H
	inc H
	
	ld A,(HL)
	or A
	jr z,WAVE_RunCallbackKill
	cp 32
	;jr z,WAVE_RunCallbackDone2
	jr nc,WAVE_RunCallbackDone
	
.WAVE_RunCallbackHit
	push BC
	
	ld HL,(WAVE_Current)
	ld B,(HL)
	ex DE,HL
	call THEM_MakeDamage
	ex DE,HL
	
	pop BC
	
.WAVE_RunCallbackKill
	ld HL,(WAVE_Current)
	ld (HL),A
	
.WAVE_RunCallbackDone
	pop DE
	pop HL
	pop AF
	ret
;.WAVE_RunCallbackDone2
	;pop HL
	;pop AF
	;xor A
	;ret
.WAVE_RunKillEgg
	xor A
	ret
	
.WAVE_Current	dw 0

.WAVE_Run

	SetMaster NeutralMaster
	SetCallback WAVE_RunCallback

	ld HL,SPNS_Callback
	ld DE,SPMS_CallBack
	
	ld A,(FRAME)
	and 1
	jr z,WAVE_Run_Normal
	
	ld HL,SPFS_Callback
	ld DE,SPBS_CallBack

.WAVE_Run_Normal
	ld (WAVE_Draw),HL
	ld (WAVE_DrawRev),DE
	

	ld HL,WaveBufferB1
	ld A,50
	call WAVE_RunEntry
	
	ld HL,WaveBufferB2
	ld A,50
	call WAVE_RunEntry
	
	ld HL,WaveBufferR1
	ld A,51
	call WAVE_RunEntry
	
	ld HL,WaveBufferR2
	ld A,51
	call WAVE_RunEntry
		
	ret
	
.WAVE_RunEntry
	ld (WAVE_Current),HL
	ld (WAVE_Sprite),A	;write current sprite
	ld A,(HL)	;is empty ?
	or A
	ret z
	
	;break
	
	inc L	;get move
	ld A,(HL)
		
	push AF
	
	inc L
	ld C,(HL)
	inc L
	ld B,(HL)
	
	add C
	cp 32
	jr nc,WAVE_Kill
	
	ld C,A
	dec L
	ld (HL),A

	dec C
	
WAVE_Sprite EQU $+1
	ld A,0
	call USPGADD
	
	pop AF
	bit 7,A
	jr nz,WAVE_DrawRev-1
	
WAVE_Draw		EQU $+1
	jp SPNS

WAVE_DrawRev	EQU $+1
	jp SPNS

	
.WAVE_Kill
	pop AF
	dec L
	dec L
	dec L
	ld (HL),0
	ret