;*******************
;*** KILL PLAYER ***
;*******************
.PLAYER_Die
	nop
	push AF
	
	;if immortal , goto end
	ld A,(PLAYER_Immortal)
	or A
	jr nz,PlayerDieDone
	
	
	;can not die anymore
	ld A,#C9
	ld (PLAYER_Die),A
	
	ld A,(CASUAL_RespawnImmediate)
	or A
	jr nz,PLAYER_DieRebirth

.PLAYER_DieNormal
	
	xor A
	ld (SHIP),A
	ld (BALL1),A
	ld (BALL2),A
	ld (BALL3),A
	ld (PILL1),A
	ld (PILL2),A
	ld (MISSILE),A
	ld (SHOOTON),A

;check if we erase weapons
	ld A,(CASUAL_RespawnWeapon)
	or A
	jr nz,PLAYER_Die_RespawnWeapons
	
	xor A
	ld (save_BALL1),A
	ld (save_BALL2),A
	ld (save_BALL3),A
	ld (save_PILL1),A
	ld (save_PILL2),A
	ld (save_MISSILE),A
	
.PLAYER_Die_RespawnWeapons
	
	push HL

	ld A,31
	ld B,20
	call PLAYER_DiePrepare	
	ld HL,GAME_Restart
	ld (END_JUMP),HL
	
	call SND_SfxStopAll
	
	;force all mode !
	xor A
	call SND_SetMode
	
	ld A,13
	call MUSIC_Play
	
	;remove a life
	ld HL,Current_Lives
	dec (HL)
	
	pop HL
	
.PlayerDieDone
	pop AF
	ret
.PLAYER_DieRebirth
;check first is we still have lives
	ld A,(Current_Lives)
	dec A
	jr z,PLAYER_DieNormal	;if last normal die
	ld (Current_Lives),A

;remove ship and shoot
	xor A
	ld (SHIP),A
	ld (SHOOTON),A
	
	;check if we erase weapons
	ld A,(CASUAL_RespawnWeapon)
	or A
	jr nz,PLAYER_DieRebirth_KeepWeapons
	
	xor A
	ld (BALL1),A
	ld (BALL2),A
	ld (BALL3),A
	ld (PILL1),A
	ld (PILL2),A
	ld (MISSILE),A
	
.PLAYER_DieRebirth_KeepWeapons

	push HL
	
	ld A,sfx_player_explode
	call SND_SfxPlay
	
	ld A,48+8
	ld B,10
	call PLAYER_DiePrepare
	ld HL,PLAYER_ImmediateRespawn
	ld (END_JUMP),HL
	
	pop HL
	pop AF
	ret
	
.PLAYER_ImmediateRespawn

	;adapt to selected mode 
	ld A,(option_menu_sound)
	call SND_SetMode

	ld A,1
	ld (SHOOTON),A
	
	call BRINGON1
	call LIVES_Show
	
	xor A
	ld (PLAYER_Die),A	;allow dying
	
	;player immortality
	ld A,immort_duration
	ld (PLAYER_Immortal),A
	
	jp GAME_LoopRetFromImmediate
	
.PLAYER_DiePrepare
	push BC
	push AF
	CALL ADDEXPLO0
	pop AF
	
	ld (HL),A ;type 1 , count 15
	ld BC,(SHIPX)
	inc L
	ld (HL),C
	inc L
	ld (HL),B
	
	pop BC
	ld A,B
	ld (END_COUNT),A
	ret
	
;************************************
;*** WAIT FOR RETURNING FROM GAME ***
;************************************
.PLAYER_End
	ld A,(END_COUNT)
	or A
	ret z
	

	ld B,5
.PLAYER_Wait1
	ld A,(int_counter)
	or A
	jr nz,PLAYER_Wait1
.PLAYER_Wait2
	ld A,(int_counter)
	cp 1
	jr nz,PLAYER_Wait2
	djnz PLAYER_Wait1	

	ld A,(END_COUNT)
	dec A
	ld (END_COUNT),A
	ret nz
	
	ld HL,(END_JUMP)
	ld SP,(Stack_Save)
	jp (HL)

;*********************************
;*** PLAYER CODE FROM ORIGINAL ***
;*********************************

;*******************
;*** init player ***
;*******************

.BRINGON
	xor A
	ld (PLAYER_Die),A
	ld (uship2_valid_counter),A
	LD HL,BINGBONG
 	LD A,(HL)
 	AND A
 	JR NZ,BRINGON1
	INC (HL)
	
	call BRINGON1
	ld A,2
	ld (ForcePlayerMove),A
	
	LD A,255
 	LD (EXBYTE),A
	
	ld A,48
	ld (INTRO_TIMER),A
	
	ld A,-5
	ld (SHIPX),A

	RET
.BRINGON1	
	LD A,32
 	LD (EXBYTE),A
 	LD A,9 ;9
 	LD (SHIPY),A
;.BNM
	LD A,8
 	LD (SHIPX),A
	LD A,228 ;3
	LD (SHIP),A
 	RET

;*************
;*** INTRO ***
;*************

;.SHOOT	
;	LD A,(SHOOTON)
 ;	AND A
 ;	RET Z
 ;	LD BC,(SHIPX)
 ;	CP 1
	;JR NZ,SHOOT1
;	INC C
; 	LD A,C
;	CP 28
 ;	JR NZ,SHOOT2
;	LD A,2
;	LD (SHOOTON),A
;SHOOT2	
;	LD A,228
;SHOOT2A	
;	LD (SHIPX),BC
	;CALL USPN
 	;CALL EXHAUST
;	RET
; SHOOT1	DEC C
; 	LD A,228
;	CP C
;	JR NZ,SHOOT2A
; 	XOR A
 ;	LD (SHOOTON),A
;	LD (URSHIP1E),A
; 	LD A,228
;	LD (SHIP),A
; 	INC A
; 	JR SHOOT2A

;***********************
;*** player management ***
;***********************

.URSHIP_CallBack
	push AF
	push HL
	inc H	;bank
	inc H	;addr high
	inc H	;addr low
	inc H	;master
	ld A,(HL)
	cp NeutralMaster+1
	jr z,URSHIP_CallBackSpecialMaster	;special case Neutral+1 = no collision except player
	cp 33	;0 = BG 1-31 = enemy 32 = eggs
	jr nc,URSHIP_CallBack_NoCollision
	;break
	;player die here
	;stop drawing by removing arguments on stack
.URSHIP_CallBackSpecialMaster
	call PLAYER_Die
	ld HL,14
	add HL,SP
	ld SP,HL
	ret
.URSHIP_CallBack_NoCollision
	pop HL
	pop AF
	ret
	
;DE = x,y
;set carry if position occupied
.urship_check_pos
	ld B,2
.urship_check_pos_loopA
	ld A,E
	add A ;*2
	add ATTR_Addr_low
	ld L,A
	ld H,ATTR_Addr_High	;HL point on address for Y
	ld A,(HL)
	inc L
	ld H,(HL)
	add D ;do not forget to add X to array address
	ld L,A
	ld C,3
.urship_check_pos_loopB
	bit 7,(HL)	;is solid ?
	jr z,urship_check_pos_not_solid
	push HL
	inc H
	inc H
	inc H
	inc H
	ld A,(HL)
	or A
	jr z,urship_check_pos_solid	;if master = level so it is solid
	pop HL
	
.urship_check_pos_not_solid
	inc L
	dec C
	jr nz,urship_check_pos_loopB
	inc E
	djnz urship_check_pos_loopA
	xor A	;unset carry
	ret
.urship_check_pos_solid
	pop HL
	scf
	ret
	
URSHIP:
	SetMaster #FF				;player is #FF
	SetCallback URSHIP_CallBack ;collision callback	

	LD A,(SHIP)
 	AND A
 	RET Z
	
	ld A,(WAVE_COUNTER)
	or A
	jr z,URSHIP_WAVE_COUNTER
	dec A
	ld (WAVE_COUNTER),A
	
.URSHIP_WAVE_COUNTER

	LD HL,(SHIPX)	;L=X - H=Y
 	ld d,h
	ld e,l
	
	LD A,(MMIVAR)
 	LD B,A
	;AND 15 
	and %00000011
 	JR NZ,URSHIPB

	ld A,(SHIP)
	cp 228
	jr z,URSHIPB	
	
	jr c,URSHIP_CorrectSprUp
	dec A
	jr URSHIP_CorrectSprDone
.URSHIP_CorrectSprUp
	inc A
.URSHIP_CorrectSprDone
	LD (SHIP),A
	jr URSHIPB
	
	;LD A,(SHIP)
 	;CP 4
	;JR C,URSHIPA
	;DEC A
 	;LD (SHIP),A
 	;JR URSHIP1DA
	
;.URSHIPA	
	;CP 3
 	;JR Z,URSHIP1DA
	;INC A
 	;LD (SHIP),A
 	;JR URSHIP1DA
	
.URSHIPB	
	BIT 2,B				;left
	JR Z,URSHIP1A
 	DEC L

.URSHIP1A
	BIT 3,B				;right
 	JR Z,URSHIP1B
 	INC L

.URSHIP1B	

	BIT 1,B				;up
 	JR Z,URSHIP1C
 	INC H
 ;
	LD A,(SHIP)
 	;CP 2
	;cp 226
	cp 230
	JR Z,URSHIP1C
 	inc A
 	LD (SHIP),A
 ;
.URSHIP1C	
	BIT 0,B				;down
 	JR Z,URSHIP1D
 	DEC H
 ;
	LD A,(SHIP)
 	;CP 4
	;cp 230
	cp 226
	JR Z,URSHIP1D
 	dec A
 	LD (SHIP),A
	
.URSHIP1D	
	LD A,H
	CP 19	;20
 	JR C,uship1
 	and a
 	jr z,uship1
 	ld h,d
	
.uship1	
	LD A,L
	and a
 	jr z,uship3
 	CP 30 ;29 32 chars screens
 	JR C,uship2
	
.uship3
	ld l,e
.uship2	

	ld D,L
	ld E,H
	push HL
	call urship_check_pos
	pop HL
	jr nc,uship2_valid_pos
	
uship2_valid_counter	EQU $+1
	ld A,0
	cp 2
	jr nc,uship2_valid_pos
	inc A
	ld (uship2_valid_counter),A
	jr uship2_not_valid_pos
	
.uship2_valid_pos
	xor A
	ld (uship2_valid_counter),A

	LD (SHIPX),HL

.uship2_not_valid_pos
 ;
.URSHIP1DA	

	ld A,(ForcePlayerMove)
	or A
	jr nz,URSHIP1DA_DrawShip

	ld A,(PLAYER_Immortal)
	or A
	jr z,URSHIP1DA_DrawShip
	
	ld A,(FRAME)
	and 1
	jr z,EXHAUST
	
.URSHIP1DA_DrawShip

	LD BC,(SHIPX)
	;dec B ;new dimensions
	ld A,(FORCE_SPR)
	or A
	jr nz,URSHIP1DA_Force
	LD A,(SHIP)
.URSHIP1DA_Force
	;dec C	;added 32 chars screen
 	CALL USPN
 	;LD BC,(SHIPX)
 	;INC C
 	;INC C
 	;INC C
 	;CALL CONVERT
	;JP NC,DIE
 	;LD A,(COLLP)
	;AND A
 	;JP NZ,DIE

.EXHAUST	
	SetMaster NeutralMaster ;avoid force attach on boost
	LD A,(EXBYTE)
	AND A
 	JR Z,URSHIP1E
 	DEC A
 	LD (EXBYTE),A
 	LD BC,(SHIPX)
 	DEC C
 	DEC C
	inc B
 	LD A,98
 	CALL USPGADD
	LD A,(FRAME)
	AND 1
 	ADD 69
	;LD (CADD+1),A
 	CP 69
 	JR Z,EXHAUST1
 	CALL SPNS
 	JR EXHAUST0
.EXHAUST1	
	CALL SPFS
.EXHAUST0	
	;CALL RESETADD
	
.URSHIP1E	
	NOP
 	LD A,(MMIVAR)
 	BIT 4,A
	;break
 	JR NZ,URSHIP1E1
 	LD A,(PLASCOUNT)
	AND A
 	JR Z,URSHIP1F
.URSHIP1E1	

	CALL FIRE

.URSHIP1F	
	LD A,(MMIVAR)
 	BIT 5,A
 	RET Z
	
 ;
.URSHIP1F1	
	LD A,(BALLTYPE)
	AND A
 	RET Z
 	CALL JE1
	EX DE,HL
	
;OK..BALL IN PLAY
;IS IT CONNECTED
.UR0	
	EX AF,AF'
	LD A,(ATTATCH)
	AND A
 	JR Z,UR2
;....YES !
.UR1
	CP 2
 	LD A,(SHIPX)
	JR Z,UR1A
 	CP 26
 	RET NC
	LD A,4
	JR UR1B
.UR1A
	CP 4
	RET C
 	LD A,5
.UR1B
	LD (HL),A
 	XOR A
 	LD (ATTATCH),A
	;LD HL,20*256+FULL+1
	;CALL BEEP
	ld A,sfx_release
	call SND_SfxPlay
 	RET
 ;....NO (AAAAHHHHHH !!!)
 .UR2
	LD A,(HL)
	CP 3
	JR Z,UR2A
 	CP 7
	RET NZ
.UR2A
	LD (HL),6
 	RET
	
;**********************************************
;*** RENDERING ROUTINE FOR PLAYER STUFF ***
;**********************************************
.USPN	;CALL USPALL
		CALL USPGADD
		CALL SPNS_CallBack
		;CALL SPBS_CallBack
		;JR USPALL1
		ret

.USPF	
		;CALL USPALL
		CALL USPGADD
		CALL SPFS_CallBack
		;JR USPALL1
		ret

.USPM	
		;CALL USPALL
		CALL USPGADD
		CALL SPMS_CallBack
		;JR USPALL1
		ret
;
.USPB	;CALL USPALL
		CALL USPGADD
		CALL SPBS_CallBack
		;JR USPALL1
		ret
;
USPALL	;CALL USPGADD
		;XOR A
		;LD (SCREEN4),A
		;RET
 USPALL1
		;LD A,201
		;LD (SCREEN4),A
		;RET

;******************
;*** FIRE STUFF ***
;******************
		
.FIRE	

	;break
	LD A,(MMIVAR)
 	BIT 4,A
 	JR  NZ,FIREA
	LD A,(PLASCOUNT)
	CP 12
 	Jp NC,FIREC
 	LD DE,(SHIPX)
 	INC E
	inc D
 	CALL ADDBULL
	
	ld A,(BALL1)
	or A
	jr nz,FIRE_PlaySound
	
	ld A,(ATTATCH)
	or A
	jp nz,FIREE1

.FIRE_PlaySound
	ld A,sfx_baseShoot
	call SND_SfxPlay
	
	
	JP FIREE1
 ;
.FIREA
	ld A,(PLASCOUNT)
	cp 10
	jr nz,FIREA_NoSnd
	
	ld A,sfx_beamload
	call SND_SfxPlay
	
.FIREA_NoSnd	
;missile
	;LD A,(MISSILE)
 	;AND A
 	;JR Z,FIREA_MissDone
	;LD A,(MISSCOUNT)
	;AND A
 	;JR NZ,FIREA_MissDone
 	;LD A,(MISS1BUFF)
	;AND A
 	;JR NZ,FIREA_MissDone
 	;LD A,(MISS2BUFF)
	;AND A
 	;JR NZ,FIREA_MissDone
 	;LD A,32
 	;LD (MISSCOUNT),A
	;LD IX,MISS1BUFF
 	;LD BC,(SHIPX)
 	;LD HL,MISSM
 	;DEC B
 	;LD (IX+0),H
 	;LD (IX+1),L
 	;LD (IX+2),C
 	;LD (IX+3),B 
	;XOR A
 	;LD (IX+4),A
 	;LD (IX+10),A
	;INC A
 	;INC B
	;inc B
 	;LD (IX+5),A
 	;LD (IX+11),A
	;LD (IX+9),B
 	;LD (IX+8),C
 	;LD HL,MISSN
 	;LD (IX+7),L
 	;LD (IX+6),H
	
	;launch missile
	LD A,(MISSILE)
	or A
	call nz,MISSLE_Spawn
	
.FIREA_MissDone
	
  ;LD A,(PLASCOUNT)
 ;CP 12
  ;JR NZ,FIREAX
 ;LD HL,2*256+FULL+3
 ;CALL BEEP
.FIREAX
 	LD A,(PLASP)
	ADD 2
 	AND 15
	LD (PLASP),A
	SRL A
 	ADD 99
	EX  AF,AF'
	LD A,(PLASCOUNT)
	INC A
 	INC A
 	CP 66
 	JR Z,FIREAA
 	LD (PLASCOUNT),A
;
.FIREAA	
	ld HL,GAUGE_POS
	ld A,(PLASCOUNT)
	call GAUGE_Display


	;CP 40
 	;JR NC,FIREA2
	;LD HL,PLASPRINT
 	;LD DE,PLT1
	;AND 3
 	;JR NZ,FIREA1
	;INC (HL)
	;INC E
 ;FIREA1	LD A,(HL)
 	;ADD 11
	;LD C,A
	;LD B,20
 	;EX DE,HL
	;CALL SMALLTEXT
;
FIREA2	
	LD BC,(SHIPX)
 	INC C
 	INC C
 	INC C
  	LD A,(ATTATCH)
	CP 1
	JR NZ,FIREB
 	INC C
 	INC C
	
	ld A,(BALL1)
	or A
	jr nz,FIREB		;module 1 is smaller
 	INC C
.FIREB	
	ld A,(PLASCOUNT)	;minimal plasma count to show charge
	cp 12
	jr c,FIRE0
	
	SetMaster NeutralMaster ;not solid !
	
	EX AF,AF'
	CALL USPGADD
	CALL SPNS
 	JR FIRE0
;
.FIREC	
	LD DE,6174 ;24*256+30
	LD A,(PLASCOUNT)
	CP 36
 	JR NC,FIRED
 	LD DE,5908 ;23*256+20
 	CP 20
 	JR NC,FIRED
 	LD DE,5642 ;22*256+10
.FIRED

	;beam sound
	ld A,sfx_beam
	call SND_SfxPlay
	
	LD HL,PLASBUFF
 	LD BC,(SHIPX)
 	LD A,(ATTATCH)
	AND A
 	JR Z,FIREE
	INC C
 	INC C
 	INC C
.FIREE	
	LD (HL),D
	INC L
 	LD (HL),B
 	INC L
 	LD (HL),C
 	LD A,E
	LD (PLASPOWER),A

.FIREE1	
	ld HL,GAUGE_POS
	xor A
	LD (PLASCOUNT),A
	LD (PLASP),A
	LD (PLASPRINT),A
	call GAUGE_Display
.FIRE0	

	LD A,(BALL1)
 	AND A
 	JR Z,FIRE1
;*** module type 1 ***
	LD A,(ATTATCH)
	AND A
 	JR NZ,FIRE1
;*** not attached ***
 	LD A,(MMIVAR)
 	BIT 4,A
 	JP NZ,FIRE4
 	LD DE,(BALL1X)
	INC D
 	CALL ADDBULL
	JP FIRE4
	
.FIRE1	
	LD A,(BALL2)
 	AND A
 	JR Z,FIRE2
;*** module type 2 ***
	LD A,(MMIVAR)
 	BIT 4,A
 	JP NZ,FIRE4
 	LD A,(ATTATCH)
	AND A
 	JR NZ,FIRE1A
;*** not attached ***
	LD DE,(BALL2X)
	INC D
 	INC C
 	CALL ADDBULLM1
	JP FIRE4
;*** module 2 , attached***
.FIRE1A	
	ld A,5+128
	ld (fire_life),A
	ld A,3 ;4
	ld (laser_life),A	;set laser length
	
	
	LD A,(BALLPOW)
	CP 3
	JR Z,FIRE2B ;FIRE1A1
	CP 2
	;JR Z,FIRE1A2
	jr Z,FIRE1A1

	
	JP FIRE2F
.FIRE1A2	
	LD A,3
 	JP FIRE2JJ
;PILL BULLETS HERE
.FIRE1A1

	;XOR A
	;LD (FIRE4DIP+1),A
 	;LD BC,BALL2
 	;LD DE,BALL2X
	;CALL FIRE4A
 	;LD A,3
	;LD (FIRE4DIP+1),A
 	;LD BC,BALL2
 	;LD DE,BALL2X
	;CALL FIRE4AA
	
	ld A,(WAVE_COUNTER)
	or A
	jr nz,FIRE1A1_Done
	
	ld D,2
	
 	LD A,(ATTATCH)
	CP 2
	jr nz,FIRE1A1_Direction

	ld D,-2
	
.FIRE1A1_Direction

	ld A,10
	ld (WAVE_COUNTER),A
	
	ld HL,WaveBufferB1
	ld BC,(SHIPX)
	call WAVE_Add
	
	ld HL,WaveBufferR1
	ld BC,(SHIPX)
	inc B
	call WAVE_Add
	
.FIRE1A1_Done

	jp FIRE3
;
FIRE2	
	LD A,(BALL3)
 	AND A
 	JP Z,FIRE3
;*** module 3 ***
	LD A,(MMIVAR)
 	BIT 4,A
 	JP NZ,FIRE3
	
	ld A,10+128
	ld (fire_life),A
	ld A,5 ;8
	ld (laser_life),A	;set laser length

	
 	LD A,(ATTATCH)
	AND A
 	JR NZ,FIRE2A
;***not attached***
	LD DE,(BALL3X)
	CALL ADDBULLM
 	JP FIRE3
;
.FIRE2A	
	LD A,(BALLPOW)
	AND 127
 	CP 1
	JR Z,FIRE2F0
	CP 2
	JP Z,FIRE2J
 	CP 3
	JP NZ,FIRE3
	
;******************
;*** fire laser ***
;******************
.FIRE2B	
	ld BC,(SHIPX)
	inc B
 	LD A,(ATTATCH)
	CP 2
	jr z,FIRE2D	
	
	ld A,3
	add C
	ld C,A
	
	ld HL,FIRE_A
	ld DE,FIRE_UpTable
	call FIRE_Add

	ld HL,FIRE_B
	ld DE,FIRE_DnTable
	call FIRE_Add
	 JP FIRE3
;.FIRE2C
	
.FIRE2D	
	ld A,-2
	add C
	ld C,A
	
	ld HL,FIRE_A
	ld DE,FIRE_UpTable_rev
	call FIRE_Add

	ld HL,FIRE_B
	ld DE,FIRE_DnTable_rev
	call FIRE_Add

 	JP FIRE3
;******************
;*** blue laser ***
;******************
.FIRE2F0	
	
.FIRE2F	
 	LD BC,(SHIPX)
	inc B
	
 	LD A,(ATTATCH)
	CP 2
	JR Z,FIRE2G
 ;FRONT
	ld A,5
	add C
	ld C,A
 
	ld A,1
	ld HL,laserA
	call LASER_Fire
	
	ld A,2
	ld HL,laserB
	call LASER_Fire
	
	ld A,3
	ld HL,laserC
	call LASER_Fire
	
.FIRE2I	
 
 	jp FIRE3
;BACK
.FIRE2G	

	ld A,-3
	add C
	ld C,A
 
	ld A,7
	ld HL,laserA
	call LASER_Fire
	
	ld A,6
	ld HL,laserB
	call LASER_Fire
	
	ld A,5
	ld HL,laserC
	call LASER_Fire

	jp FIRE3
	
;**********************
;*** circular laser *** 
;**********************

.FIRE2J
	LD A,(ATTATCH)
	cp 2
	jr z,FIRE2JJ

	LD BC,(SHIPX)
	dec B
	inc C
	inc C
	inc C
	ld A,1
	call CIRC_Fire
	
	ld A,(WAVE_COUNTER)
	or A
	jr nz,FIRE2J_Done
	
	ld A,10
	ld (WAVE_COUNTER),A
	
	
	ld A,(PILL1)
	or A
	jr z,FIRE2J_NoPill1
	
	ld HL,WaveBufferB1
	ld BC,(PILL1X)
	dec C
	ld D,2
	call WAVE_Add

.FIRE2J_NoPill1

	ld A,(PILL2)
	or A
	jr z,FIRE2J_NoPill2
	
	ld HL,WaveBufferR1
	ld BC,(PILL2X)
	dec C
	inc B
	ld D,2
	call WAVE_Add
	
.FIRE2J_NoPill2
	
.FIRE2J_Done	
	jr FIRE3
	
.FIRE2JJ	
	LD BC,(SHIPX)
	dec B
	dec C
	ld A,-1
	call CIRC_Fire
	
	ld A,(WAVE_COUNTER)
	or A
	jr nz,FIRE2JJ_Done
	
	ld A,10
	ld (WAVE_COUNTER),A
	
	
	ld A,(PILL1)
	or A
	jr z,FIRE2JJ_NoPill1
	
	ld HL,WaveBufferB1
	ld BC,(PILL1X)
	inc C
	ld D,-2
	call WAVE_Add

.FIRE2JJ_NoPill1

	ld A,(PILL2)
	or A
	jr z,FIRE2JJ_NoPill2
	
	ld HL,WaveBufferR1
	ld BC,(PILL2X)
	inc C
	inc B
	ld D,-2
	call WAVE_Add
	
.FIRE2JJ_NoPill2
	
.FIRE2JJ_Done
	
	
	
	;LD (BLOBDIP1+1),A
	;LD (BLOBDIP2+1),A
 	;LD (BLOBDIP3+1),A
 	;INC A
 	;LD (BLOBDIP4A+1),A
	;LD HL,BLOBBUFF1
 	;LD A,(HL)
 	;LD HL,BLOBBUFF2
 	;ADD (HL)
	;AND A
 	;JR NZ,FIRE3
 	;LD DE,(SHIPX)
 	;LD A,(ATTATCH)
	;CP 2
	;JR NZ,FIRE2K
	;DEC E
 ;	LD HL,MB75
	;JR FIRE2L
 ;
FIRE2K	
	;LD A,E
	;ADD 5
 	;LD E,A
	;LD HL,MB76
FIRE2L	
	;LD (MB1DIP+1),HL
	;LD (MB2DIP+1),HL
	;XOR A
 	;LD (BLOBBYTE),A
 	;LD (BLOBBYTE1),A
	;LD (BLOBBYTE2),A
	;DEC D
 	;LD HL,BLOBBUFF1
 	;LD A,1
	;CALL FIRE2HH
	;LD HL,BLOBBUFF2
 	;LD A,2
	;CALL FIRE2HH
 ;LD HL,28*256+HALF+1
  ;CALL BEEP
 ;
.FIRE3

  ;LD HL,6*256+FULL+1
 ;CALL BEEP
 ;
.FIRE4	
	ret ;added
	;LD A,(PILL1)
 	AND A
 	JR Z,FIRE5
;PILL1 NOT PRESENT
 	LD A,(MMIVAR)
 	BIT 4,A
 	JR NZ,FIRE5
 ;FIRE BUTTON PRESSED
 	XOR A
 	LD (FIRE4DIP+1),A
 ;PUT 0 Y OFFSET INTO ADD SKEWER DATA
 	LD BC,BALL3
 	LD DE,PILL1X
;SET UP RIGHT PILL AND DO IT !!
	CALL FIRE4A
 	JR FIRE5
;
FIRE4A	
	;LD HL,PILL1BUFF
 	JR FIRE4AB
FIRE4AA	
	;LD HL,PILL1BUFF+6
FIRE4AB	LD A,(BC)
	AND A
 	RET Z
 ;IF BALL NOT ACTIVE OR
 	CP 3
	RET NC
;NOT TYPE 3 OR
 	LD A,(ATTATCH)
	AND A
 	RET Z
 ;NOT ATTACHED OR
 	LD A,(BALLPOW)
	CP 3
	RET NZ
;POWER NOT 3 THEN .....
;TRY AND FIND ROOM
 	LD B,2
FIRE4B	LD A,(HL)
 	AND A
 	JR Z,FIRE4C
 	INC L
 	INC L
 	INC L
 	DJNZ FIRE4B
 	RET
 ;
FIRE4C	LD A,(ATTATCH)
	RRCA
	RRCA
	LD (HL),A
 ;SET DIRECTION OF SKEWER
 	INC L
 	EX DE,HL
	LDI
 ;GET X POS OF SKEWER
 	LD A,(HL)
 FIRE4DIP	ADD 0
 	LD (DE),A
 ;GET Y POS + OFFSET OF SKEWER
	RET
 ;
FIRE5	
	LD A,(PILL2)
 	AND A
 	RET Z
 ;PILL2 NOT PRESENT
 	LD A,(MMIVAR)
 	BIT 4,A
 	RET NZ
;FIRE BUTTON PRESSED
 	LD A,1
	LD (FIRE4DIP+1),A
 ;SET UP +1 Y OFFSET IN ADD SKEWER DATA
 	LD BC,BALL3
 	LD DE,PILL2X
	CALL FIRE4AA
;SET UP PILL AND DO IT !!
	RET