
;********************
;*** ADD A BULLET ***
;********************

;normal bullet
.ADDBULL
	
	CALL ADDBULLT
	LD (HL),24 ;8+16*1
	INC L
 	LD (HL),E
 	INC L
 	LD (HL),D
.ADDBULL1
  ;LD HL,1*256+QUART+1
  ;CALL BEEP
  ;break

 	RET
 ;
.ADDBULLM	
	CALL ADDBULLM2
;
ADDBULLM1	
	CALL ADDBULLT
	LD A,25 ;9+16*1
 	CALL ADDBULLM3
	CALL ADDBULLT
 	LD A,26 ;10+16*1
	CALL ADDBULLM3
	JR ADDBULL1
 ;
.ADDBULLM2	
	CALL ADDBULLT
	INC E
 	LD A,33 ;1+16*2
 	CALL ADDBULLM3
	CALL ADDBULLT
 	INC D
 	LD A,50 ;2+16*3
 ;
.ADDBULLM3	
	LD (HL),A
	INC L
 	LD (HL),E
 	INC L
 	LD (HL),D
 	RET
 ;
ADDBULLT	
	LD HL,BULLMTABLE
	LD B,16
 ADDBULLT1	
	LD A,(HL)
	AND A
 	RET Z
 	INC L
 	INC L
 	INC L
 	DJNZ ADDBULLT1
	;POP HL
	ld HL,BULLMTABLE
	RET
	
;***********************************
;***PLAYER BULLET MANAGEMENT ***
;***********************************

;new version !
.BULLETS_Move
	ld HL,BULLMTABLE
	ld B,16
.BULLETS_Loop
	ld A,(HL)
	or A
	jr nz,BULLET_Valid
	inc L
	inc L
	inc L
.BULLETS_MoveDone
	djnz BULLETS_Loop
	ret
.BULLET_Valid

	push BC
	push HL
	
	and 15
	ld DE,BULLETS_Table
	add A
	add E
	ld E,A	
	
	ld A,(DE)
	ld IXL,A
	inc E
	ld A,(DE)
	ld IXH,A

	
	inc L
	ld C,(HL)
	inc L
	ld B,(HL)
;break	
	call BULLET_DoMove
	ld DE,4
	add IX,DE	
	call BULLET_DoMove
	
	pop HL
	inc L
	ld (HL),C
	inc L
	ld (HL),B
	inc L
	pop BC
	jr BULLETS_MoveDone
;do move , we can alter all except BC
.BULLET_DoMove
	;first of all , are we on screen ?
	ld A,C
	cp 32
	jr nc,BULLET_Kill
	ld A,B
	cp 20
	jr nc,BULLET_Kill
	;now , compute address in array
	add A
	add ATTR_Addr_low
	ld L,A
	ld H,ATTR_Addr_High
	;get it !
	ld E,(HL)
	inc L
	ld D,(HL)
	;add X
	ld A,C
	add E
	ld E,A
	;so we are , now check if we owns something here
	ld A,(DE)
	bit 6,A
	jr nz,BULLET_KillOnEgg
	and %10000001
	jr z,BULLET_Draw
	inc D	;bank
	inc D	;low addr
	inc D	;high addr
	inc D	;master !
	ld A,(DE)
	or A
	jr z,BULLET_Kill
	cp 32		
	jr nc,BULLET_FinishMove
	ld B,1
	call THEM_MakeDamage
	jr BULLET_Kill
	
.BULLET_Draw
;break
	ex DE,HL
	ld (HL),63 ;63+128 ; compiled sprite (old = 1)
	inc H
	;ld (HL),#C0	;bank #C0
	inc H
	ld A,(IX+0)	;sprite graphix
	ld (HL),A
	inc H
	ld A,(IX+1)
	ld (HL),A
	inc H
	ld (HL),NeutralMaster	;neutral master
	
.BULLET_FinishMove
	;do the move
	ld A,(IX+2)
	add C
	ld C,A
	ld A,(IX+3)
	add B
	ld B,A
	ret

.BULLET_KillOnEgg
	ex DE,HL
	ld (HL),4	;auto erase
	inc H
	inc H
	inc H
	inc H
	ld (HL),64	;neutral
.BULLET_Kill
	pop HL ;get ret address on stack
	pop HL
	ld (HL),0	;kill bullet !
	inc L
	inc L
	inc L
	pop BC
	jp BULLETS_MoveDone
	
;*********************************
;*** PRECOMPILE BULLET SPRITES ***
;*********************************

.BULLET_InitSprites

	ld B,8
	ld DE,4
	
.BULLET_InitSprites_loop

	push AF
	push BC
	push DE
	
	;write compiled sprite adress
	ex AF,AF'
	ld A,IXL
	ld (IY+0),A
	ld A,IXH
	ld (IY+1),A
	add IY,DE
	ex AF,AF'

	;get pixels
BULLET_InitSprites_BankSelect	EQU	$+1
	call USPGADD
	
	;get first tile
	inc HL	;global flag
	inc HL	;tile flags
	ld E,(HL)	;low addr
	inc HL	
	ld D,(HL)	;high addr
	
	;compile sprite

	call AUTO_Generate_Sprite

	pop DE
	pop BC
	pop AF
	
	inc A
	djnz BULLET_InitSprites_loop
	ret
	
;bullet table , for each direction from 0 to 7 clockwise storing
; first part address , x move , y move , second part address , second direction
.BULLETS_Table_Dir0	;x=0 - y=-1
dw #4000:db 0,-1:dw #4000:db 0,-1
.BULLETS_Table_Dir1	;x=1 - y=-1
dw #4000:db 1,-1:dw #4000:db 1,-1
.BULLETS_Table_Dir2	;x=1 - y=0
dw #4000:db 1,0:dw #4000:db 1,0
.BULLETS_Table_Dir3	;x=1 - y=1
dw #4000:db 1,1:dw #4000:db 1,1
.BULLETS_Table_Dir4	;x=0 - y=1
dw #4000:db 0,1:dw #4000:db 0,1
.BULLETS_Table_Dir5	;x=-1 - y=1
dw #4000:db -1,1:dw #4000:db -1,1
.BULLETS_Table_Dir6	;x=1 - y=0
dw #4000:db -1,0:dw #4000:db -1,0
.BULLETS_Table_Dir7	;x=-1 - y=-1
dw #4000:db -1,-1:dw #4000:db -1,-1


;*********************
;*** PLASMA STUFF ***
;*********************

.PLASMA_CallBack
	push BC
	push AF
	push HL
	inc H
	inc H
	inc H
	inc H
	ld A,(HL)
	or A
	jr z,PLASMA_CallBack_Kill
	cp 32
	jr z,PLASMA_L4	
	jr nc,PLASMA_CallBack_Done	

	;break
	
	ld A,(PLASPOWER)
	ld B,A
	ld A,(HL)
	call THEM_MakeDamage
	ld (PLASPOWER),A
.PLASMA_CallBack_Done	
	;break
	pop HL
	pop AF
	pop BC
	ret
.PLASMA_L4
	pop HL
	pop AF
	pop BC
	xor A
	ret
.PLASMA_CallBack_Kill
	xor A
	ld (PLASPOWER),A
	jr PLASMA_CallBack_Done	

.PLASMA	
	
	SetMaster #FE	;#FE = plasma master
	SetCallBack PLASMA_CallBack

	LD HL,PLASBUFF
	LD A,(HL)
 	AND A
 	RET Z
 	EX AF,AF'
 	INC L
 	LD  B,(HL)
	INC L
 	LD  A,(HL)
	ADD 2
 	LD (HL),A
 	CP 35
 	JR NC,PLASMA1
 	LD C,A
	;CALL CONVERT
	;JR NC,PLASMA1
 ;
	;INC DE
	;LD A,(DE)
 	;CP 251
	;JR NC,PLASMA1
 	;INC DE
	;LD A,(DE)
 	;CP 251
	;JR NC,PLASMA1
 	;INC DE
	;LD A,(DE)
 	;CP 251
	;JR NC,PLASMA1
 	;INC DE
	;LD A,(DE)
 	;CP 251
	;JR NC,PLASMA1
 ;
	;EX AF,AF'
 	;PUSH AF
 	;EX AF,AF'
 	PUSH BC
 	CALL PLASMA2
	POP BC
	;POP DE
	LD A,(PLASPOWER)
	AND A
 	JR Z,PLASMA1
	;LD A,D
	;EX AF,AF'
 	;CALL PLASMA2
	;LD A,(PLASPOWER)
	;AND A
 	;JR Z,PLASMA1
	RET
 ;
.PLASMA2	
	LD A,(FRAME)
 	AND 1
 	JR Z,PLASMA3
	EX AF,AF'
	;break
 	CALL USPF
 	;CALL KILL_1
 	RET
.PLASMA3	
	EX AF,AF'
	;break
	CALL USPN
 	;CALL KILL_1
 	RET
 ;
.PLASMA1	
	XOR A
	LD (PLASBUFF),A
 	LD (PLASPOWER),A
	RET