;****************************************
;	0 = MSB ADDRESS MISS DATA
;	1 = LSB ADDRESS MISS DATA
;	2 = X POS
;	3 = Y POS
;	4 = TARGET TRACKED
;	5 = CURRENT MOVEMENT PATTERN
;****************************************
.MOVEMISS	
	read "new_missiles.asm"

	LD IX,MISS1BUFF
 	LD IY,MISS2BUFF
 	CALL MISS1
	LD IX,MISS2BUFF
 	LD IY,MISS1BUFF
 	CALL MISS1
	LD A,(MISSCOUNT)
	AND A
 	RET Z
 	DEC A
 	LD (MISSCOUNT),A
	RET
;
MISS1	
	LD A,(IX+0)
	AND A
 	RET Z
;
;RETURN IF NOT ACTIVE
;
	LD H,A
	LD L,(IX+1)
 	LD C,(IX+2)
 	LD B,(IX+3)
 	LD A,(HL)
 	CP 255
	Jp Z,MISS2
;
;JUMP IF END OF CURRENT PATTERN
;
	LD A,C
	CP 31
 	JR NC,MISS3
 	LD A,B
	AND A
 	JR Z,MISS3A
 	CP 21
 	JR C,MISS3A
.MISS3	
	LD (IX+0),0
	RET
.MISS_HitEgg
	;ld (HL),64
	;dec H
	;dec H
	;dec H
	;dec H
	;ld (HL),4
	ld (IX+0),0
	ret
 ;
;IF MISSILE OUT OF BOUNDS THEN DESTROY IT
;
.MISS3A	
	LD D,(HL)
 	INC HL
	LD E,(HL)
 	INC HL
	LD A,E
	RRCA
	RRCA
	AND 15
	CALL ALIENF99
 	PUSH BC
	INC B
	call COLL_CheckPosition	
 	POP BC
 	jr nc,MISS_CollCheck2
	or A
	jr z,MISS3
	cp 32
	jr z,MISS_HitEgg
	jr nc,MISS_CollCheck2
	ld B,1	
	call THEM_MakeDamage
	jr MISS3
.MISS_CollCheck2
	push BC
	inc C
	inc B
	call COLL_CheckPosition	
 	POP BC
	jr nc,MISS_NoColl
	or A
	jr z,MISS3
	cp 32
	jr z,MISS_HitEgg
	jr nc,MISS_NoColl
	ld B,1	
	call THEM_MakeDamage
	jr MISS3
	
.MISS_NoColl	
 	LD (IX+0),H
 	LD (IX+1),L
 	LD (IX+2),C
 	LD (IX+3),B
 ;
	LD A,E
	AND 3
 	LD HL,SPNS
	AND A
 	JR Z,MISS1A
 	LD HL,SPMS
	CP 1
	JR Z,MISS1A
 	LD HL,SPFS
	CP 2
	JR Z,MISS1A
 	LD HL,SPBS
.MISS1A	
	LD (MISS1B+1),HL
	LD A,(FRAME)
	AND 1
 	ADD D
 	PUSH IX
	push IY
	CALL USPGADD
.MISS1B	
	CALL MISS_NONE
 	;CALL KILL
	pop IY
 	POP IX
	;JR Z,MISS3
.MISS_NONE
	RET
 ;
;END OF CURRENT SEQUENCE
 ;CHECK MISSILE STATUS
;	1 = RIGHT
;	2 = TURN UP
;	3 = UP
 ;	4 = TURN DOWN
;	5 = DOWN
.MISS2	LD A,(IX+5)
	CP 1
	JR Z,MISS21
 	CP 2
	JR Z,MISS22
 	CP 3 
 	JR Z,MISS23
 	CP 4
	JR Z,MISS24
 	CP 5
	JR Z,MISS25
.MISS26	
	LD HL,MISSL
 	JR MISS29
.MISS25	
	LD HL,MISSJ
 	JR MISS29
.MISS24	
	INC (IX+5)
	JR MISS25
.MISS23	
	LD HL,MISSI
 	JR MISS29
.MISS22	
	INC (IX+5)
	JR MISS23
 ;
;OK...ANYTHING TO ATTACK
 ;
.MISS21	
	LD DE,SEQBUFF
 	LD L,28
.MISS21Z	LD A,(DE)
	INC E
 	INC E
 	INC E
 	AND A
 	JR Z,MISS21X
	LD A,(DE)
 	DEC A
 	JR Z,MISS21X
	CP 32
 	JR NC,MISS21X
 	CP C
	JR Z,MISS21A
	DEC A
 	CP C
	JR Z,MISS21A
.MISS21X	
	LD A,E
 	ADD 6
 	LD E,A
	DEC L
 	JR NZ,MISS21Z
.MISS21Y	
	LD HL,MISSL
	JR MISS29
 ;
;TIME TO TURN....... UP OR DOWN
;
.MISS21A	LD A,(IY+4)
	CP 20
 	JR NC,MISS21X
 	CP L
	JR Z,MISS21X
	LD (IX+4),L
 	INC E
 	LD A,(DE)
 	DEC E
 	CP B
	JR Z,MISS21Y
	JR NC,MISS21B
 	LD A,2
	LD HL,MISSG
 	JR MISS21C
MISS21B	
	LD HL,MISSA
	LD A,4
MISS21C	
	LD (IX+5),A
	JR MISS29
 ;
MISS29	
	LD (IX+0),H
 	LD (IX+1),L
 	JP MISS1
	
 ;MISSILE DATA
MISSM	DEFB 69,4,69,0,69,0,69,0	;TAKE OFF UP
		DEFB 69,32,255		;
MISSN	DEFB 69,0,69,0,69,0,69,0	;TAKE OFF DOWN
		DEFB 69,32,255		;
MISSL	DEFB 69,32,255		;R
MISSG	DEFB 73,36+1,71,4		;R/U
MISSI	DEFB 71,4,71,4,71,4,255	;U
MISSA	DEFB 73,40+3,71,8+2	;R/D
MISSJ	DEFB 71,10,71,10,71,10,255	;D
 