ORG level_start

lvl_offset EQU 10


IF output_files
	write "bin/level3.lvl"
ENDIF

CANON_RELEASE_POW	EQU 50
CANON_POW			EQU 50
REAR_CANON_POW		EQU 40
REAR_CANON_RATE		EQU 50
POKER_POW			EQU 60
CACHE_POW			EQU 60

;****************************************
; *	LEVEL 3 DATA	
;****************************************
 START	DEFB 0
 SYNCHRO	DEFW 0000
;
START1	DEFB 0
START2	DEFB 255
START3	DEFB 255
;
SYNCHRO1	DEFW SYNC1
SYNCHRO2	DEFW SYNC2
SYNCHRO3	DEFW SYNC3
;
TSPRADD		DEFW 0
TADD		DEFW 0
SEQTABLE	DEFW SQTABLE
SPEVENT1	DEFW SPEV1
SPEVENT2	DEFW SPEV2

.lvl_pal
	db 0,26,11,6,3,23,10,2,1,25,16,18,9,15,20,24
	db 0
	db 0,20,2,3,0,10,1,1,0,12,3,9,0,3,14,12
	db 0
	db 0,11,1,3,0,1,0,1,0,3,0,9,0,3,2,0
	db 0
	db 0,1,0,0,0,0,0,0,0,0,0,0,0,0,1,0
	db 0

.lvl_floating_color
	db 14
.lvl_music
	db 2 
.lvl_skip
	dw 0	;music to skip on music restart
.lvl_init
	dw	0	;called after loading
.lvl_reinit
	dw	lvl_reinit_proc	;called each time player dies
	
.lvl_reinit_proc
;get BS address
	ld A,2
	call TSPGADD_bank5
	inc HL
	ld (BS_Draw_StartAddress),HL
;clear zones	
	ld HL,BS_Erase
	ld IX,BS_InitTable
	call BS_Init
;re init used zones
	ld HL,BS_Add
	ld IX,BS_InitNormalTable
	call BS_Init
	
	ret
	
;init BS gfx here
.BS_Init
	ld (BS_InitFunc),HL
	
.BS_InitLoop
	ld A,(IX)
	or A
	ret z
	ld C,(IX+1)
	ld B,(IX+2)
	inc IX
	inc IX
	inc IX
	;push AF
	;push BC
BS_InitFunc	EQU $+1
	call BS_Erase
	;pop BC
	;pop AF
	;call BS_Add
	jr BS_InitLoop
;init table sprite,X,Y
.BS_InitTable
	db 3,cacheB_posX,cacheB_posY
	db 15,cacheA_posX,cacheA_posY
	db 19,4,8	;mouth
	db 19,30,7	;weakpoint , temporary sprite
	db 19,31,7
	db 70,16,1+2	;A1 ?
	db 71,38,0		;A3 -1+1
	db 50,2,5	;turrets group A 1 & 3
 	db 50,6,4
	db 80,8,0   ;poker A
	db 85,0,11	;poker C
	db 0
.BS_InitNormalTable
	db 104,21,12
	db 105,24,12
	db 106,27,12
	db 107,30,12
	db 0

;****************************************
;*	LEVEL 3 - ATTACK WAVES
;****************************************
;	1ST BYTE = TILENO
;	2ND BYTE
;		BITS 0-2 = TILESCROLL
;		BITS 3-4 = TILEPOS
;		BITS 5-7 = 
;	3RD BYTE = SEQUENCE NUMBER
;	4TH BYTE = INITIAL X POSITION
;	5TH BYTE = INITIAL Y POSITION
;	(TILESCROLL = FRAME OR BITPOS)
;****************************************

;ship composition for slots
slot_ship		EQU 1 ;1 big ship										(shot free slot)
slot_big_canon	EQU 2 ;2 big canon/big canon/reactor A					(shot free slot)
slot_hat		EQU 3 ;3 hat/hat/rearcanonF								(shot free slot)
slot_cacheB		EQU 4 ;4 cache under ship , hide a poker/rearcanon D	(shot free slot)
slot_pokerB		EQU 5 ;5 poker under cache B

slot_turretA1	EQU 6 ;6 turret 1 group A/big turret 3
slot_turretA2	EQU 7 ;7 turret 2 group A/big turret 4
slot_turretA3	EQU 8 ;8 turret 3 group A/turret 1 group C
slot_turretA4	EQU 9 ;9 turret 4 group A/turret 2 group C

slot_cacheA			EQU 10	;cache under ship that supports and hide 2 turrets	(shot free slot)/rearcanon B
slot_cacheA_turret1	EQU 11 ;turret 1 on the cache/turret 3 group C
slot_cacheA_turret2	EQU 12 ;turret 2 on the cache/turret 4 group C

slot_turretB1		EQU 13	;turret alone under

slot_cacheA_turret3	EQU 14 ;big turret hiden under cache A/rearcanon C

slot_pokerA			EQU 15	;poker close to hat/rearcanon E
slot_pokerC			EQU 16	;poker front down/reactor D

slot_big_turretA1	EQU 17 ;big turret 1
slot_big_turretA2	EQU 18 ;big turret 2

slot_pokerD			EQU 19 ;poker rear


slot_big_flamme	EQU 20 ;20 big canon flamme/hiden turret 1
slot_big_canonA	EQU 21 ;21 big canon shot 1/reactor B
slot_big_canonB	EQU 22 ;22 big canon shot 2/hiden turret 2
slot_big_canonC	EQU 23 ;23 big canon shot 3/reactor C

slot_teeth		EQU 24	;teeth on the front/rearcanonA
slot_weakpoint	EQU 25	;weak point

slot_extraA		EQU 26
slot_extraB		EQU 27
slot_extraC		EQU 28

slot_rear_canonF	EQU slot_hat				;rear canon group A/1
slot_rear_canonB	EQU slot_cacheA				;rear canon group A/2
slot_rear_canonC	EQU slot_cacheA_turret3		;rear canon group A/3

slot_rear_canonD	EQU slot_cacheB				;rear canon group B/1
slot_rear_canonE	EQU slot_pokerA				;rear canon group B/2
slot_rear_canonA	EQU slot_teeth				;rear canon group B/3

slot_big_canon_turret1	EQU slot_big_flamme
slot_big_canon_turret2	EQU slot_big_canonB
slot_big_turretA3		EQU slot_turretA1
slot_big_turretA4		EQU slot_turretA2
slot_turretC1			EQU slot_turretA3
slot_turretC2			EQU slot_turretA4
slot_turretC3			EQU slot_cacheA_turret1
slot_turretC4			EQU slot_cacheA_turret2

slot_reactorA			EQU slot_big_canon
slot_reactorB			EQU slot_big_canonA
slot_reactorC			EQU slot_big_canonC
slot_reactorD			EQU slot_pokerC	

cacheA_posX	EQU 5
cacheA_posY	EQU 12

cacheB_posX	EQU 10
cacheB_posY	EQU 9

pokerA_posX	EQU 8
pokerA_posY	EQU 0

pokerB_posX	EQU cacheB_posX
pokerB_posY	EQU cacheB_posY

pokerC_posX	EQU -1
pokerC_posY	EQU 11

pokerD_posX	EQU 25
pokerD_posY	EQU 4


.SYNC1
ship_start	EQU 1
	FORCE_Slot	slot_ship,ship_start,0,1,34,3 			;34,3	;Main ship
	;FORCE_Slot	slot_big_turretA3,ship_start+1,0,57,38,-1 ;18
	;FORCE_Slot	slot_weakpoint,ship_start,0,37,28+2,6		;weakpoint
	;FORCE_Slot	slot_extraA,ship_start,0,51,31,16
	;FORCE_Slot	slot_weakpoint,ship_start+1,0,37,28+2,6
;

;canon	
	FORCE_Slot	slot_big_canon,ship_start,0,2,-3,8	;ship canon	
	FORCE_Slot  slot_teeth,ship_start,0,36,4,8		;front teeth
;first poker
	FORCE_Slot slot_pokerC	,ship_start,4,23,pokerC_posX,pokerC_posY
;turrets upper , group A
	FORCE_Slot	slot_turretA1,ship_start,4,12,0,4	;no support
	FORCE_Slot	slot_turretA3,ship_start,8,16,2,3	;support
	FORCE_Slot	slot_turretA2,ship_start,12,12,4,3	;no support
	FORCE_Slot	slot_turretA4,ship_start,24,21,6,2	;support
;turret under , group B
	FORCE_Slot	slot_turretB1,ship_start,24,13,2,13 ;no support

;bonus 1 fire
	FORCE_Slot	slot_extraA,ship_start+2,0,52,31,3
	
;cache A	
	FORCE_Slot	slot_cacheA,ship_start+4,0,14,cacheA_posX,cacheA_posY ;cacheB
	FORCE_Slot	slot_cacheA_turret1,ship_start+4,0,13,cacheA_posX,cacheA_posY+2 ;turret 1 for cache A
	FORCE_Slot	slot_cacheA_turret2,ship_start+4,8,13,cacheA_posX+3,cacheA_posY+2 ;turret 1 for cache A
	
;bonus 2 fire
	FORCE_Slot	slot_extraA,ship_start+6,0,53,31,8
	
;poker A	
	FORCE_Slot slot_pokerA	,ship_start+8,0,20,pokerA_posX,pokerA_posY
;cache B
	FORCE_Slot	slot_cacheB,ship_start+9,0,10,cacheB_posX,cacheB_posY ;cacheB +9
;hat
	FORCE_Slot	slot_hat,ship_start+10,0,8,11,-1	;hat
;big turret part 1
	FORCE_Slot	slot_big_turretA1,ship_start+11,0,18,16,1
	FORCE_Slot	slot_big_turretA2,ship_start+12,0,17,19,1
	
;red twister	
	FORCE_Slot	slot_extraA,ship_start+19,0,50,31,14
	FORCE_Slot	slot_extraB,ship_start+19,8,50,31,16
	FORCE_Slot	slot_extraC,ship_start+19,16,50,31,12
	
;reactors
	FORCE_Slot	slot_reactorA,ship_start+22,0,25,21,13		;reactor A ship_start+22
	FORCE_Slot	slot_reactorB,ship_start+22,4,47,24,13		;reactor B
	FORCE_Slot	slot_reactorC,ship_start+23,0,48,27,13		;reactor C
	FORCE_Slot	slot_reactorD,ship_start+23,0,49,30,13		;reactor D	
	
;bonus 3 circular
	FORCE_Slot	slot_extraA,ship_start+23,0,54,31,12	

;rear poker	
	FORCE_Slot slot_pokerD	,ship_start+24,0,24,pokerD_posX,pokerD_posY
	FORCE_Slot	slot_weakpoint,ship_start+24,0,46,28+2,6		;closed weakpoint
	
;walkers
	FORCE_Slot	slot_extraB,ship_start+25,0,51,31,16
	FORCE_Slot	slot_extraC,ship_start+25,8,51,31,16	
	
;small turret group C
	FORCE_Slot	slot_turretC1,ship_start+28,0,12,32,2
	FORCE_Slot	slot_turretC2,ship_start+28,4,16,34,1
	;FORCE_Slot	slot_turretC3,ship_start+28,8,12,36,1

;rear canons - group 1	

	FORCE_Slot slot_rear_canonA,ship_start+31,0,26,42,12
	FORCE_Slot slot_rear_canonB,ship_start+31,0,27,44,11
	FORCE_Slot slot_rear_canonC,ship_start+31,0,28,45,9	
	
;big turret
	FORCE_Slot	slot_big_turretA3,ship_start+31,0,57,38,-1 ;18
	FORCE_Slot	slot_big_turretA4,ship_start+31,4,17,42,-1
	
;last little turret	
	FORCE_Slot	slot_turretC4,ship_start+32,0,12,45,1
	
;rear canons - group 2
	FORCE_Slot slot_rear_canonD,ship_start+36,0,29,49,7
	FORCE_Slot slot_rear_canonE,ship_start+36,0,30,49,5
	FORCE_Slot slot_rear_canonF,ship_start+36,0,31,49,3	

	FORCE_Slot	slot_weakpoint,ship_start+40,0,37,28+2,6		;awake weakpoint

;bonus 4 laser
	FORCE_Slot	slot_extraA,ship_start+45,0,55,0,3	
	
	LVL_Music	50,0,8
	
.SYNC2
.SYNC3
	SCROLL_Stop	56,1
;bonus 5 laser
	FORCE_Slot	slot_extraA,ship_start+60,0,56,33,12
	
;end level
	DEFB ship_start+94,0,99,5,0

.SQTABLE
	dw SQ0_SHIP					;1
	dw SQ1_CANON				;2
	dw SQ2_CANON_RELEASE		;3
	dw SQ3_CANON_SHOTA			;4
	dw SQ4_CANON_SHOTB			;5
	dw SQ5_CANON_SHOTC			;6
	dw SQ6_CANON_FLAMME			;7
	dw SQ7_HATA					;8
	dw SQ8_HATB					;9
	dw SQ9_CACHEB1				;10
	dw SQ10_CACHEB2				;11
	dw SQ11_TURRET_SMALLA		;12
	dw SQ12_TURRET_SMALLB		;13
	dw SQ13_CACHEA1				;14
	dw SQ14_CACHEA2				;15
	dw SQ15_TURRETS_SMALL		;16
	dw SQ16_TURRET_BIG			;17
	dw SQ17_TURRETS_BIG			;18
	dw SQ18_TURRETR_BIG			;19
	dw SQ19_POKERA				;20
	dw SQ20_TURRETS_SMALLB		;21
	dw SQ21_POKERB				;22
	dw SQ22_POKERC				;23
	dw SQ23_POKERD				;24
	dw SQ24_REACTORA			;25
	dw SQ25_REARCANONA			;26
	dw SQ26_REARCANONB			;27
	dw SQ27_REARCANONC			;28
	dw SQ28_REARCANOND			;29
	dw SQ29_REARCANONE			;30
	dw SQ30_REARCANONF			;31
	dw SQ31_FIRECANONA			;32
	dw SQ32_FIRECANONB			;33
	dw SQ33_FIRECANONC			;34
	dw SQ34_FIRECANOND			;35
	dw SQ35_TEETH				;36
	dw SQ36_WEAKPOINT			;37
	dw SQ37_WEAKPOINTB0			;38
	dw SQ38_WEAKPOINTB1			;39
	dw SQ39_WEAKPOINTB2			;40
	dw SQ40_WEAKPOINTB3			;41
	dw SQ41_WEAKPOINTB4			;42
	dw SQ42_WEAKPOINTB5			;43
	dw SQ43_WEAKPOINTB6			;44
	dw SQ44_WEAKPOINTB7			;45
	dw SQ45_WEAKPOINTSTART		;46
	dw SQ46_REACTORB			;47
	dw SQ47_REACTORC			;48
	dw SQ48_REACTORD			;49
	dw SQ49_REDTWISTER			;50
	dw SQ50_WALKER				;51
	dw SQ51_PODDER1				;52
	dw SQ52_PODDER2				;53
	dw SQ53_PODDER3				;54
	dw SQ54_PODDER4				;55
	dw SQ55_PODDER5				;56
	dw SQ56_TURRETS_BIG2		;57
	
.SPEV1
	dw A0_null
	dw A1_half_speed
	dw A2_Bship
	dw A3_canon
	dw A4_fall
	dw A5_follow
	dw A6_hat
	dw A7_cacheB
	dw A8_turretA
	dw A9_cacheA
	dw A10_poker
	dw A11_reactor
	dw A12_rearcanon
	dw A13_teeth
	dw A14_weakpoint
	dw A15_podder
.SPEV2
	dw B0_null
	dw B1_half_speed
	dw B2_selectorA
	dw B3_selectorB
	dw B4_selectorC
	dw B5_selectorD
	dw B6_selectorE
	dw B7_selectorF
	dw B8
	dw B9
	dw BA
	dw BB
	dw BC
	dw BD_Laser
	dw BE_Circular
	dw BF_Fire
	
	

;****************************************
 ;	1ST BYTE
 ;		BITS 0-3 = SPECIAL EVENT 1
;		BITS 0-4 = SPECIAL EVENT 2
;	2ND BYTE = HIT POINTS
;	3RD BYTE
 ;		BITS 0-3 = POINT VALUE
;		BITS 4-7 = EXPLOSION TYPE
 ;	BYTES ARE NOW PAIRED TO MAKE UP
;	ADDRESS OF SPRITE PATTERNS
 ;	WORD 0 = END OF SEQUENCE 
;********** SPRITE SEQUENCE *************
 ;	1ST BYTE = SPRITE NUMBER
 ;
;	2ND BYTE =
 ;		1/2 BITS 0-1 = ORIENTATION
;		4	BIT 2 = UP
;		8	BIT 3 = DOWN
;		16	BIT 4 = LEFT
;		32	BIT 5 = RIGHT
 ;		64	BIT 6 = GENERAL PURPOSE
;		128	BIT 7 = END OF PATTERN
;****************************************

;second big turret W/ support

L3_MASTER0		EQU $+1
L3_MASTER0_val	EQU 10
.SQ56_TURRETS_BIG2
	db 8+#50,5,#40+3
	dw SP17_TURRETS_BIG
	dw #FFFF

;PODDER 1
SQ51_PODDER1	
	DEFB 15+#F0,1,2+32	;#C0
	DEFW SP11B,SP11A,SP11B,SP11C,SP11DDDD,SP11DDDD
	DEFW SP11DDDD,SP11D,SP11G,SP11G,SP11E,SP11E,SP11E,0
 ;PODDER 2
SQ52_PODDER2	
	DEFB 15+#F0,1,2+32	;#C0
	DEFW SP11BB,SP11A,SP11B,SP11C,SP11BB,SP11A,SP11B,SP11C,0
;PODDER 3
SQ53_PODDER3	
	DEFB 15+#E0,1,2+32	;#D0
	DEFW SP11AA,SP11G,SP11E,SP11E,SP11E
	DEFW SP11E,SP11E,SP11E,SP11E,0
;PODDER 4
SQ54_PODDER4	
	DEFB 15+#D0,1,2+32	;#B0
	DEFW SP11II,SP11JJ,SP11I,SP11HH,SP11K,SP11K,SP11K,SP11KK
	DEFW SP11GG,SP11F,SP11G,SP11C,SP11BB,SP11AA,SP11B,SP11CC,0
;PODDER 5
SQ55_PODDER5	
	DEFB 15+#D0,1,2+32	;#B0
	DEFW SP11AA,SP11G,SP11E,SP11GG,SP11HH,SP11II,0
	
;PODDERS
SP11A		DEFB 39,24,39,24							;FLY D/L 6
SP11AA		DEFB 39,24,39,24,39,24,39,24+128	;FLY D/L 4 
SP11B		DEFB 39,16,39,16							;FLY L 4
SP11BB		DEFB 39,16,39,16+128						;FLY L 2
SP11C		DEFB 39,20,39,20							;FLY U/L 6
SP11CC		DEFB 39,20,39,20,39,20,39,20+128	;FLY U/L 4
SP11DDDD	DEFB 39,8							;DROP 4
SP11DDD		DEFB 39,8							;DROP 3
SP11DD		DEFB 39,8							;DROP 2
SP11D		DEFB 39,8+128							;DROP 1
SP11E		DEFB 37,16,36,16,37,16,36,16+128	;WALK L
SP11F		DEFB 37,33,36,33,37,33,36,33+128	;WALK R
SP11G		DEFB 38,0,38,0+128						;LAND IMPACT L
SP11GG		DEFB 38,1,38,1+128						;LAND IMPACT R
SP11HH		DEFB 39,37,39,37,39,37,39,37+128	;FLY U/R 4
SP11I		DEFB 39,33,39,33							;FLY R 4
SP11II		DEFB 39,33,39,33+128						;FLY R 2
SP11JJ		DEFB 39,41,39,41,39,41,39,41+128	;FLY D/R 4
SP11K		DEFB 39,8+1,39,8+1,39,8+1						;DROP R/3
SP11KK		DEFB 39,8+1+128							;DROP R/1

;***************
;*** WALKERS ***
;***************

.SQ50_WALKER
	DEFB 1+16,1+64,2+16
 	DEFW SP50,SP50,SP50,SP50,0

.SP50
	DEFB 36,16,37,16,38,16,37,16
	DEFB 36,16,37,16,38,16,37,16+128
	
;********************
;*** RED TWISTERS ***
;********************
.SQ49_REDTWISTER	
	DEFB 0,1+64,2+16
 	DEFW SP49,SP49,SP49,SP49,0
	
.SP49
	DEFB 33,16,34,24,35,16,34,20,33,20,34,16,35,24,34,16+128

;***********************
;***WEAKPOINT BULLETS***
;***********************
.SQ37_WEAKPOINTB0
	db 0,1,#20
	dw SP37_WPB0
	dw 0
.SP37_WPB0
	repeat 4
	db 2,4
	rend
	db 2,0
	db 2,4
	db 2,0
	db 2,0
	db 2,4
	db 2,0
	db 2,0
	db 2,8
	db 2,0
	db 2,8
	db 2,8,2,8,2,8+128

	
.SQ38_WEAKPOINTB1
	db 0,1,#20
	dw SP38_WPB1
	dw 0
.SP38_WPB1
	db 2,4,2,4,2,4,2,4+16
	db 2,0,2,4
	db 2,0,2,0+16
	db 2,4
	db 2,0,2,0
	db 2,8+16
	db 2,0
	db 2,8,2,8,2,8+16,2,8+128
	
.SQ39_WEAKPOINTB2
	db 0,1,#20
	dw SP39_WPB2
	dw 0
.SP39_WPB2
	db 2,4,2,4,2,4,2,4+16
	db 2,4,2,4,2,4,2,4+16
	db 2,0,2,4
	db 2,0,2,0+16
	db 2,4
	db 2,0,2,0
	db 2,8+16
	db 2,0
	db 2,8,2,8,2,8,2,8
	db 2,8,2,8,2,8+16,2,8+128
	
.SQ40_WEAKPOINTB3
	db 0,1,#20
	dw SP40_WPB3
	dw 0
.SP40_WPB3
	db 2,4,2,4,2,4,2,4
	db 2,4,2,4,2,4,2,4+16
	db 2,0,2,4
	db 2,0,2,0
	db 2,4
	db 2,0,2,0
	db 2,8+16
	db 2,0
	db 2,8,2,8,2,8,2,8
	db 2,8,2,8,2,8+16,2,8+128

.SQ41_WEAKPOINTB4
	db 0,1,#20
	dw SP41_WPB4
	dw 0
.SP41_WPB4
	db 2,4,2,4,2,4,2,4+32
	db 2,0,2,4
	db 2,0,2,0+32
	db 2,4
	db 2,0,2,0
	db 2,8+32
	db 2,0
	db 2,8,2,8,2,8+32,2,8+128
	
.SQ42_WEAKPOINTB5
	db 0,1,#20
	dw SP42_WPB5
	dw 0
.SP42_WPB5
	db 2,4,2,4,2,4,2,4+32
	db 2,4,2,4,2,4,2,4+32
	db 2,0,2,4
	db 2,0,2,0+32
	db 2,4
	db 2,0,2,0
	db 2,8+32
	db 2,0
	db 2,8,2,8,2,8,2,8
	db 2,8,2,8,2,8+32,2,8+128
	
.SQ43_WEAKPOINTB6
	db 0,1,#20
	dw SP43_WPB6
	dw 0
.SP43_WPB6
	db 2,4,2,4,2,4,2,4
	db 2,4,2,4,2,4,2,4+32
	db 2,0,2,4
	db 2,0,2,0
	db 2,4
	db 2,0,2,0
	db 2,8+32
	db 2,0
	db 2,8,2,8,2,8,2,8
	db 2,8,2,8,2,8+32,2,8+128
	
.SQ44_WEAKPOINTB7
	db 0,1,#20
	dw SP44_WPB7
	dw 0
.SP44_WPB7
	repeat 4
	db 2,4
	rend
	db 2,0
	db 2,4
	db 2,0
	db 2,0
	db 2,4
	db 2,0
	db 2,0
	db 2,8
	db 2,0
	db 2,8
	db 2,8,2,8,2,8+128
	
;******************
;*** WEAK POINT ***
;******************
.SQ45_WEAKPOINTSTART
	db 5,63,0
	dw SP36_WEAKPOINT_WAIT
	dw #FFFF

.SQ36_WEAKPOINT
	db 14,60,0
	dw SP36_WEAKPOINT_WAIT
	dw SP36_WEAKPOINT_WAIT
	dw SP36_WEAKPOINT_EXIT
	dw SP36_WEAKPOINT_EXIT
	dw SP36_WEAKPOINT_OPEN
	dw SP36_WEAPOINT_ATTACK
	dw SP36_WEAPOINT_ATTACK
	dw SP36_WEAKPOINT_CLOSE
	dw SP36_WEAKPOINT_ENTER
	dw SP36_WEAKPOINT_ENTER
	dw #FFFF
	
.SP36_WEAKPOINT_WAIT
repeat 5
	db 23,0
rend
	db 23,128
.SP36_WEAKPOINT_EXIT
	db 23,0,23,0,23,0,23,0,23,128+16
.SP36_WEAKPOINT_ENTER
	db 23,0,23,0,23,0,23,0,23,128+32
.SP36_WEAKPOINT_OPEN
repeat 4
	db 25,0
rend
repeat 4
	db 27,0
rend
repeat 4
	db 29,0
rend
	db 29,128
.SP36_WEAPOINT_ATTACK
repeat 5
	db 31,0
	db 31,0
	db 31,64
rend
	db 31,128
.SP36_WEAKPOINT_CLOSE
repeat 4
	db 29,0
rend
repeat 4
	db 27,0
rend
repeat 4
	db 25,0
rend
	db 25,128

;**********************************
;*** TEETH ON FRONT OF THE SHIP ***
;**********************************
.SQ35_TEETH
	db 13,60,#30+6
	dw SP35_TEETH
	dw #FFFF
.SP35_TEETH
	db 19,128
	

;************************
;*** REAR CANONS FIRE ***
;************************
;down
.SQ31_FIRECANONA
	db 0,63,0
repeat 8
	dw SP31_FIRECANONA
rend
	dw 0
.SP31_FIRECANONA
repeat 3
	db 150,0
rend
	db 150,128+8
;down/right
.SQ32_FIRECANONB
	db 0,63,0
repeat 8
	dw SP32_FIRECANONB
rend
	dw 0
.SP32_FIRECANONB
repeat 3
	db 151,0
rend
	db 151,128+40	
;right
.SQ33_FIRECANONC
	db 0,63,0
repeat 8
	dw SP33_FIRECANONC
rend
	dw 0
.SP33_FIRECANONC
repeat 3
	db 152,0
rend
	db 152,128+32
;right/up
.SQ34_FIRECANOND
	db 0,63,0
repeat 8
	dw SP34_FIRECANOND
rend
	dw 0
.SP34_FIRECANOND
repeat 3
	db 153,0
rend
	db 153,128+36
;*******************
;*** REAR CANONS ***
;*******************
.SQ25_REARCANONA

L3_MASTER1	EQU $+1
L3_MASTER1_val	EQU REAR_CANON_POW+20

	db #20+12,REAR_CANON_POW,#20+4
	dw SP25_REARCANONA
	dw #FFFF	
.SP25_REARCANONA
	db 130,128
	
L3_MASTER2	EQU $+1
L3_MASTER2_val	EQU REAR_CANON_POW+20
	
.SQ26_REARCANONB
	db #30+12,REAR_CANON_POW,#20+4
	dw SP26_REARCANONB
	dw #FFFF	
.SP26_REARCANONB
	db 133,128

L3_MASTER3	EQU $+1
L3_MASTER3_val	EQU REAR_CANON_POW+20
	
.SQ27_REARCANONC
	db #40+12,REAR_CANON_POW,#20+4
	dw SP27_REARCANONC
	dw #FFFF 	
.SP27_REARCANONC
	db 136,128
	
L3_MASTER4	EQU $+1
L3_MASTER4_val	EQU REAR_CANON_POW+20
	
.SQ28_REARCANOND
	db #50+12,REAR_CANON_POW,#20+4
	dw SP28_REARCANOND
	dw #FFFF 	
.SP28_REARCANOND
	db 139,128

L3_MASTER5	EQU $+1
L3_MASTER5_val	EQU REAR_CANON_POW+20
	
.SQ29_REARCANONE
	db #60+12,REAR_CANON_POW,#20+4
	dw SP29_REARCANONE
	dw #FFFF 	
.SP29_REARCANONE
	db 142,128

L3_MASTER6	EQU $+1
L3_MASTER6_val	EQU REAR_CANON_POW+20
	
.SQ30_REARCANONF
	db #70+12,REAR_CANON_POW,#20+4
	dw SP30_REARCANONF
	dw #FFFF 	
.SP30_REARCANONF
	db 145,128

;****************
;*** REACTORS ***
;****************

L3_MASTER7		EQU $+1
L3_MASTER7_val	EQU 60

.SQ24_REACTORA
	db 11+#20,40,#40+6
	dw SP24_REACTOR
	dw #FFFF
	
.SP24_REACTOR
	db 100,128

L3_MASTER8		EQU $+1
L3_MASTER8_val	EQU 60
	
.SQ46_REACTORB
	db 11+#30,40,#40+6
	dw SP24_REACTOR
	dw #FFFF

L3_MASTER9		EQU $+1
L3_MASTER9_val	EQU 60
	
.SQ47_REACTORC
	db 11+#40,40,#40+6
	dw SP24_REACTOR
	dw #FFFF

L3_MASTERA		EQU $+1
L3_MASTERA_val	EQU 60
	
.SQ48_REACTORD
	db 11+#50,40,#40+6
	dw SP24_REACTOR
	dw #FFFF

;**************
;*** POKERS ***
;**************

.SQ23_POKERD
	db 10+#50,POKER_POW,#40+7
	dw SQ23_POKERD1_FIRE
	dw SQ23_POKERD1
	dw SQ23_POKERD2_FIRE
	dw SQ23_POKERD2
	dw SQ23_POKERD3_FIRE
	dw SQ23_POKERD3
	dw SQ23_POKERD4_FIRE
	dw SQ23_POKERD4
	dw SQ23_POKERD5_FIRE
	dw SQ23_POKERD5
	dw #FFFF
	
.SQ23_POKERD1_FIRE
	db 90,64
.SQ23_POKERD1	
	db 90,0,90,0,90,0,90,0,90,128
.SQ23_POKERD2_FIRE
	db 91,64
.SQ23_POKERD2
	db 91,0,91,0,91,0,91,0,91,128
.SQ23_POKERD3_FIRE
	db 92,64
.SQ23_POKERD3
	db 92,0,92,0,92,0,92,0,92,128
.SQ23_POKERD4_FIRE
	db 93,64
.SQ23_POKERD4
	db 93,0,93,0,93,0,93,0,93,128
.SQ23_POKERD5_FIRE
	db 94,64
.SQ23_POKERD5
	db 94,0,94,0,94,0,94,0,94,128

.SQ22_POKERC
	db 10+#40,POKER_POW,#40+7
	dw SQ22_POKERC1_FIRE
	dw SQ22_POKERC1
	dw SQ22_POKERC2_FIRE
	dw SQ22_POKERC2
	dw SQ22_POKERC3_FIRE
	dw SQ22_POKERC3
	dw SQ22_POKERC4_FIRE
	dw SQ22_POKERC4
	dw SQ22_POKERC5_FIRE
	dw SQ22_POKERC5
	dw #FFFF
	
.SQ22_POKERC1_FIRE
	db 85,64
.SQ22_POKERC1
	db 85,0,85,0,85,0,85,0,85,128
.SQ22_POKERC2_FIRE
	db 86,64
.SQ22_POKERC2
	db 86,0,86,0,86,0,86,0,86,128	
.SQ22_POKERC3_FIRE
	db 87,64
.SQ22_POKERC3
	db 87,0,87,0,87,0,87,0,87,128
.SQ22_POKERC4_FIRE
	db 88,64
.SQ22_POKERC4
	db 88,0,88,0,88,0,88,0,88,128 
.SQ22_POKERC5_FIRE
	db 89,64
.SQ22_POKERC5
	db 89,0,89,0,89,0,89,0,89,128

SQ21_POKERB
	db 10+#30,POKER_POW,#40+7
	dw SP21_POKERB1_FIRE
	dw SP21_POKERB1
	dw SP21_POKERB2_FIRE
	dw SP21_POKERB2
	dw SP21_POKERB3_FIRE
	dw SP21_POKERB3
	dw SP21_POKERB4_FIRE
	dw SP21_POKERB4
	dw SP21_POKERB5_FIRE
	dw SP21_POKERB5
	dw #FFFF
	
.SP21_POKERB1_FIRE
	db 95,64
.SP21_POKERB1
	db 95,0,95,0,95,0,95,0,95,128
.SP21_POKERB2_FIRE
	db 96,64
.SP21_POKERB2
	db 96,0,96,0,96,0,96,0,96,128
.SP21_POKERB3_FIRE
	db 97,64
.SP21_POKERB3
	db 97,0,97,0,97,0,97,0,97,128
.SP21_POKERB4_FIRE
	db 98,64
.SP21_POKERB4
	db 98,0,98,0,98,0,98,0,98,128
.SP21_POKERB5_FIRE
	db 99,64
.SP21_POKERB5
	db 99,0,99,0,99,0,99,0,99,128

SQ19_POKERA
	db 10+#20,POKER_POW,#40+7
	dw SP19_POKERA1_FIRE
	dw SP19_POKERA1
	dw SP19_POKERA2_FIRE
	dw SP19_POKERA2
	dw SP19_POKERA3_FIRE
	dw SP19_POKERA3
	dw SP19_POKERA4_FIRE
	dw SP19_POKERA4
	dw SP19_POKERA5_FIRE
	dw SP19_POKERA5
	dw #FFFF
	
.SP19_POKERA1_FIRE
	db 80,64
.SP19_POKERA1
	db 80,0,80,0,80,0,80,0,80,128
.SP19_POKERA2_FIRE
	db 81,64
.SP19_POKERA2
	db 81,0,81,0,81,0,81,0,81,128
.SP19_POKERA3_FIRE
	db 82,64
.SP19_POKERA3
	db 82,0,82,0,82,0,82,0,82,128
.SP19_POKERA4_FIRE
	db 83,64
.SP19_POKERA4
	db 83,0,83,0,83,0,83,0,83,128
.SP19_POKERA5_FIRE
	db 84,64
.SP19_POKERA5
	db 84,0,84,0,84,0,84,0,84,128

;*******************
;*** BIG_TURRETS ***
;*******************

L3_MASTERB		EQU $+1
L3_MASTERB_val	EQU 10
;W/O support
.SQ16_TURRET_BIG
	db 8,5,#40+3
	dw SP16_TURRET_BIG
	dw #FFFF
	
.SP16_TURRET_BIG
	db 60,128
	
L3_MASTERC		EQU $+1
L3_MASTERC_val	EQU 10	
;W/ support
.SQ17_TURRETS_BIG
	db 8+#30,5,#40+3
	dw SP17_TURRETS_BIG
	dw #FFFF
	
.SP17_TURRETS_BIG
	db 70,128
	
L3_MASTERD		EQU $+1
L3_MASTERD_val	EQU 10
	
;W/O support
.SQ18_TURRETR_BIG
	db 8,5,#40+3
	dw SP18_TURRETR_BIG
	dw #FFFF
	
.SP18_TURRETR_BIG
	db 60,128+2
;***************************
;*** TURRET WITH SUPPORT ***
;***************************

.SQ15_TURRETS_SMALL
	db 8+#20,1,#10+2
	dw SP15_TURRET
	dw #FFFF
	
.SP15_TURRET
	db 50,128
	
.SQ20_TURRETS_SMALLB
	db 8+#40,1,#10+2
	dw SP20_TURRET
	dw #FFFF
	
.SP20_TURRET	
	db 55,128

;***************
;*** CACHE A ***
;***************
.SQ13_CACHEA1
	db 9,CACHE_POW,0
	dw SP13_CACHEA
	dw #FFFF
.SP13_CACHEA
	db 15,128
	
.SQ14_CACHEA2	
	db 4,CACHE_POW,#40+6
	dw SP14_CACHEA,SP14_CACHEA,SP14_CACHEA,SP14_CACHEA,SP14_CACHEA
	dw SP14_CACHEAB
	dw #FFFF
.SP14_CACHEA
	db 17,0,17,0,17,0,17,0,17,128+8
.SP14_CACHEAB
	db 17,64,17,128

;*************************
;*** TURRET NO SUPPORT ***
;*************************

.SQ11_TURRET_SMALLA
	db 8,1,#10+2
	dw SP11_TURRET
	dw #FFFF
	
.SP11_TURRET
	db 40,128
	
.SQ12_TURRET_SMALLB
	db 8,1,#10+2
	dw SP12_TURRET
	dw #FFFF
	
.SP12_TURRET
	db 40,128+2

;**************************************
;*** CACHE B UNDER BS (hides poker) ***
;**************************************

;cache alone
.SQ10_CACHEB2
	db 4,CACHE_POW,#40+6
repeat 5
	dw SP_CACHEB2
rend
	dw SP_CACHEB2_2
	dw #FFFF
.SP_CACHEB2
	db 13,0,13,0,13,0,13,0,13,0,13,0,13,8+128
.SP_CACHEB2_2
	db 13,64,13,128

;cache attached to bship
SQ9_CACHEB1
	db 7,CACHE_POW,0
	dw SP_CACHEB1
	dw #FFFF
.SP_CACHEB1
	db 11,0,11,0,11,0,11,128

;***********
;*** HAT ***
;***********	
	
.SQ7_HATA
	db 6,40,#40+6
	dw SP_HATA
	dw #FFFF
.SP_HATA
	db 5,128
.SQ8_HATB
	db 5,63,0
	dw SP_HATB
	dw #FFFF
.SP_HATB
	db 6,128

;*****************	
;*** BIG CANON ***
;*****************

;flamme
	
.SQ6_CANON_FLAMME
	db 5,63,0
	dw SP_CANON_FLAMME,SP_CANON_FLAMME
	dw 0
.SP_CANON_FLAMME
	db 9,0,10,0,9,0,10,0,9,0,10,128

;shots

.SQ3_CANON_SHOTA
	db 0,62,0
repeat 8
	dw SP_CANON_SHOTA
rend
	dw 0
.SQ4_CANON_SHOTB
	db 0,62,0
repeat 8
	dw SP_CANON_SHOTB
rend
	dw 0
.SQ5_CANON_SHOTC
	db 0,62,0
repeat 8
	dw SP_CANON_SHOTC
rend
	dw 0
	
;left
.SP_CANON_SHOTA
	db 7,16,7,16,7,16,7,16+128
;left/up
.SP_CANON_SHOTB
	db 7,20,7,20,7,20,7,20+128
;left/down
.SP_CANON_SHOTC
	db 7,24,7,24,7,24,7,24+128

.SQ2_CANON_RELEASE
	db 4,CANON_RELEASE_POW,8+#40
repeat 15	
	dw SP_CANON_A
rend
repeat 5
	dw SP_CANON_B
rend
	dw SP_CANON_C
	dw #FFFF

.SP_CANON_A
	db 3,0,3,0,3,0,3,16+128
	db 3,0,3,0,3,0,3,16+128
.SP_CANON_B
	db 3,0,3,0,3,0,3,8+128
	db 3,0,3,0,3,0,3,8+128
	
.SP_CANON_C
	db 3,64,3,0
	
L3_MASTERE		EQU $+1
L3_MASTERE_val	EQU CANON_POW+10
.SQ1_CANON
	db 3,CANON_POW,0
	dw SP_CANON,#FFFF

.SP_CANON
	db 3,128
;BIG SHIP
.SQ0_SHIP
	DEFB 2,63,2+32
	DEFW SP3,SP3,SP3,SP3,SP3B,SP7
 	DEFW SP7,SP7,SP7,SP3,SP5,SP5,SP5,SP5,SP1
	DEFW SP1,SP1,SP1,SP5,SP5,SP3,SP3,SP3
	DEFW SP3B,SP7,SP7,SP3A,SP3A,SP3A,SP3A
 	DEFW SP3A,SP3A
	DEFW SP3A,SP3A,SP3A,SP3A,SP3A,SP7A,SP7A,SP7A,SP7A,SP7A
	DEFW SP3A,SP5,SP5,SP1,SP1,SP3,SP3B,SP3B,SP3B,SP3B,SP3B,SP7 ;added SP3B*4
	DEFW SP7,SP4,SP5,SP5
	
	dw SP2,SP2,SP2,SP2
	
	DEFW SP1,SP1,SP8,SP8,SP8,SP7,SP3B,SP5,SP3B
	DEFW SP1,SP6,SP6,SP2,SP8,SP8,SP3B,SP5,SP6,SP6
 	DEFW SP4,SP7,SP7,SP7,SP7,SP3B,SP4,SP4,SP5,SP1
 	DEFW SP1,SP1,SP8,SP8,SP8,SP7,SP3,SP5,SP3B
 	DEFW SP1,SP6,SP6,SP2,SP8,SP8,SP3B,SP5,SP6,SP6
 	DEFW SP4,SP7,SP7,SP7,SP7,SP4,SP4
	DEFW SP2A,SP2A,SP2A,SP2A,SP2A,SP2A,SP2A,SP2A,SP2A,0
	
;BIG SHIPS
 SP1	DEFB 2,0,2,0,2,0,2,0	;UP
 	DEFB 2,0,2,0,2,0,2,0 
 	DEFB 2,0,2,0,2,0,2,0
	DEFB 2,0,2,0,2,0,2,0
	DEFB 2,0,2,0,2,0,2,0
	DEFB 2,0,2,0,2,0,2,4+128
;
SP2	DEFB 2,0,2,0,2,0,2,0	;DOWN
 	DEFB 2,0,2,0,2,0,2,0
	DEFB 2,0,2,0,2,0,2,0
	DEFB 2,0,2,0,2,0,2,0
	DEFB 2,0,2,0,2,0,2,0
	DEFB 2,0,2,0,2,0,2,8+128
SP2A	
	DEFB 2,0,2,0,2,0,2,0
	DEFB 2,0,2,0,2,0,2,8
	DEFB 2,0,2,0,2,0,2,0
	DEFB 2,0,2,0,2,0,2,8+128
;
SP3	DEFB 2,0,2,0,2,0,2,0	;LEFT
 	DEFB 2,0,2,0,2,0,2,0
	DEFB 2,0,2,0,2,0,2,0
	DEFB 2,0,2,0,2,0,2,0
	DEFB 2,0,2,0,2,0,2,0
	DEFB 2,0,2,0,2,0,2,16
SP3B	
	DEFB 2,0,2,0,2,0,2,0	;LEFT
	DEFB 2,0,2,0,2,0,2,0
SP3A	DEFB 2,0,2,0,2,0,2,0
	DEFB 2,0,2,0,2,0,2,0
	DEFB 2,0,2,0,2,0,2,0
	DEFB 2,0,2,0,2,0,2,16+128
 ;
SP4	DEFB 2,0,2,0,2,0,2,0	;RIGHT
	DEFB 2,0,2,0,2,0,2,0
	DEFB 2,0,2,0,2,0,2,0
	DEFB 2,0,2,0,2,0,2,0
	DEFB 2,0,2,0,2,0,2,0
	DEFB 2,0,2,0,2,0,2,32+128
 ;
SP5	DEFB 2,0,2,0,2,0,2,0	;UP/LEFT
	DEFB 2,0,2,0,2,0,2,0
	DEFB 2,0,2,0,2,0,2,0
	DEFB 2,0,2,0,2,0,2,0
	DEFB 2,0,2,0,2,0,2,0
	DEFB 2,0,2,0,2,0,2,20+128
 ;
SP6	DEFB 2,0,2,0,2,0,2,0	;UP/RIGHT
 	DEFB 2,0,2,0,2,0,2,0
	DEFB 2,0,2,0,2,0,2,0
	DEFB 2,0,2,0,2,0,2,0
	DEFB 2,0,2,0,2,0,2,0
	DEFB 2,0,2,0,2,0,2,36+128
 ;
SP7	DEFB 2,0,2,0,2,0,2,0	;DOWN/LEFT
	DEFB 2,0,2,0,2,0,2,0
SP7A	DEFB 2,0,2,0,2,0,2,0
	DEFB 2,0,2,0,2,0,2,0
	DEFB 2,0,2,0,2,0,2,0
	DEFB 2,0,2,0,2,0,2,24+128
 ;
SP8	DEFB 2,0,2,0,2,0,2,0	;DOWN/RIGHT
 	DEFB 2,0,2,0,2,0,2,0
	DEFB 2,0,2,0,2,0,2,0
	DEFB 2,0,2,0,2,0,2,0
	DEFB 2,0,2,0,2,0,2,0
	DEFB 2,0,2,0,2,0,2,40+128
 ;
SP9	DEFB 2,0,2,0,2,0,2,0	;STOP STILL
 	DEFB 2,0,2,0,2,0,2,0
	DEFB 2,0,2,0,2,0,2,0
	DEFB 2,0,2,0,2,0,2,0
	DEFB 2,0,2,0,2,0,2,0
	DEFB 2,0,2,0,2,0,2,0+128
	


;**********************************
;*** SPECIAL ROUTINES FOR LEVEL ***
;**********************************

;replace bit of ship
;HL = table , A=selector 
;in table 1 = sprite , 2 = x offset , 3 = y offset
.UpdateShipPart_Once
	ld A,#20
	ld B,(IY+4)
	ld C,(IY+3)
.UpdateShipPart

	and %11110000
	
	ret z	;return if no selector
	
	push BC
	push DE
	
	sub #20	;selector start at 2
	
	rrca
	rrca
	rrca	;so here * 2
	ld E,A
	rrca
	add A,E	;*2 + *1 = *3

;compute on table	
	ld E,A
	ld D,0
	add HL,DE
	
;get sprite and save
	ld E,(HL)
	inc HL
;get X offset
	ld A,(HL)
	add C
	ld C,A
	inc HL
;get Y offset
	ld A,(HL)
	add B
	ld B,A

;restore sprite	
	ld A,E
	call BS_Add
	
;force updating
	ld A,1
	ld (ForceBSUpdate),A
	
	pop DE
	pop BC
	
	ret
	

;cause autodestruction of and objet
; A = slot
.L3_AutoDestruct
	push HL
	AND 127
 	DEC A
 	LD L,A
	ADD A
 	ADD A
 	ADD A
 	ADD L
 	LD H,SEQBUFF_high
	LD L,A
	
	ld A,(HL)
	or A
	jr z,L3_AutoDestruct_Done
	
	ld (HL),0
	ld A,8
	add L	
	ld L,A           
	ld (HL),128
	
.L3_AutoDestruct_Done	
	pop HL
	ret
	
;self destruct an object
;IY = slot
.L3_SelfDestruct
	ld (IY+0),0
	ld (IY+8),128
	ret
	
;spawn an object forcing a slot
;A = slot
;HL points on class,x,y
.L3_SpawnOnSlot
	ld (FORCESEQ),A
	jp FORCESYNC

;check damage for an object
;if yes , make sound + inc sprite for color keys
.L3_CheckDamage
	ret Z ;NZ = damaged
	
	ld A,(FRAME)
	and 1
	ret z
	
	ex AF,AF'
	inc A
	ex AF,AF'
	
	xor A
	call SND_SfxPlay
	ret
	
	;special SPR find in bank #C5
.TSPGADD_bank5
	push BC
	ld C,#C5
	jp USPGADD1

;****************
;*** A events ***
;****************

.A0_null
	jp c,TV5
	jp TV1
	
.A1_half_speed
	jp c,TV5
	ld A,(FRAME)
	and 1
 	jp nz,TV1
 	jp TV2

;*******************
;*** BATTLE SHIP ***
;*******************	
.A2_Bship
;break
	LD (IY+3),C
	LD (IY+4),B
	
	call L3_CheatCheckPlayer
	
	ld C,(IY+4)
	ld B,(IY+3)
	
	ld A,(DE)
	bit 7,A
	call nz,A2_Bship_ChangeReactorSpr

ForceBSUpdate EQU $+1	
	ld A,0
	or A
	jr nz,A2_ForceUpdate
	
	ld A,(DE)
	and 127
	jr z,A2_Bship_Skip	
	
.A2_ForceUpdate
	push BC
	push DE
	push HL
	push IX
	
	call BS_Draw	
	
	xor A
	ld (ForceBSUpdate),A

	pop IX
	pop HL
	pop DE
	pop BC
	
.A2_Bship_Skip	
	jp TV5
	
.A2_Bship_ChangeReactorSpr
	push BC
	ld B,0
	
	ld L,A
	bit 3,L
	jr nz,A2_Bship_ChangeReactorSpr_Done
	
	ld A,110
	ld H,0
	bit 4,L
	jr z,A2_Bship_ChangeReactorSpr_NotLeft
	add 10
	ld H,1
.A2_Bship_ChangeReactorSpr_NotLeft
	bit 5,L
	jr z,A2_Bship_ChangeReactorSpr_NotRight
	sub 10
	ld H,-1
.A2_Bship_ChangeReactorSpr_NotRight	

	inc B
	bit 2,L
	jr z,A2_Bship_ChangeReactorSpr_NotUp
	inc B
	
.A2_Bship_ChangeReactorSpr_NotUp
	ld (A2_reactorDir),A
	ld A,H
	ld (A2_reactorOffset),A
.A2_Bship_ChangeReactorSpr_Done
	ld A,B
	ld (A2_reactor_Fire),A
	pop BC
	ret
.A2_reactorDir
	db 110
.A2_reactorOffset
	db 0
.A2_reactor_Fire
	db 0

;*****************
;*** BIG CANON ***
;*****************
	
.A3_canon
	jp c,A3_canon_changemode	;if killed by player , change mode
	
	call L3_CheckDamage ;color keys ?
	
	call L3_ComputePosFromShip	;compute new pos
;force canon to detach	
	cp 12
	jr nc,A3_canonOK
	
	;ld (IY+0),0
	;ld (IY+8),128
	call L3_SelfDestruct
.A3_canonOK

;check if still on screen	
	add 10
	cp 4
	jr c,A3_canon_normal_end

		ld A,(FRAME)
		and 31
		push AF
		call z,A3_canon_spawnbullets
		pop AF
		cp 16
		call z,A3_canon_spawnflamme
	
	jp TV2
;is out of screen ?
.A3_canon_normal_end
	ld (IY+0),0	;kill it quietly
	jp TV5
;spawn flamme
.A3_canon_spawnflamme
	push BC
	ld A,slot_big_flamme
	ld HL,A3_FlammeObject
	call L3_SpawnOnSlot

	pop BC
	push BC
	
	ld (A3_canonbulletA+1),BC	
	;spawn A
	ld A,slot_big_canonA
	ld HL,A3_canonbulletA
	call L3_SpawnOnSlot
	
	pop BC
	ret
.A3_FlammeObject
	db 7,-8,8
;spawn bullets
.A3_canon_spawnbullets
	push BC

	ld (A3_canonbulletB+1),BC
	ld (A3_canonbulletC+1),BC	
	;spawn B
	ld A,slot_big_canonB
	ld HL,A3_canonbulletB
	call L3_SpawnOnSlot
	;spawn C
	ld A,slot_big_canonC
	ld HL,A3_canonbulletC
	call L3_SpawnOnSlot
	
	pop BC
	ret
.A3_canonbulletA
	db 4,0,0
.A3_canonbulletB
	db 5,0,0
.A3_canonbulletC
	db 6,0,0
;respawn a new canon object that falls
.A3_canon_changemode
	call L3_ComputePosFromShip ;current pos
	ld (A3_canon_respawn+1),BC
	ld A,slot_big_canon ;our slot
	ld HL,A3_canon_respawn
	call L3_SpawnOnSlot
	SetNullMaster
;spawn 2 hiden turrets	
	push BC
	push DE
	ld A,slot_big_canon_turret1
	ld HL,A3_canon_turret1
	call L3_SpawnOnSlot
	
	ld A,slot_big_canon_turret2
	ld HL,A3_canon_turret2
	call L3_SpawnOnSlot
	pop DE
	pop BC
	jp TV2
.A3_canon_respawn
	db 3,0,0
.A3_canon_turret1
	db 12,0,8
.A3_canon_turret2
	db 12,2,8

;**********************
;*** OBJECT FALLING ***
;**********************
;destroy if flag 6 of pattern is set
.A4_fall
	jp c,TV5
	
	call L3_CheckDamage ;color keys ?
	
	ld A,(DE)
	bit 6,A
	jp z,TV1
	
	;ld (IY+0),0
	;ld (IY+8),128
	call L3_SelfDestruct
	jp TV1
	
;***************************
;*** OBJECT FOLLOWING BS ***
;***************************	
.A5_follow
	jp c,A5_follow_kill
	
	;write new position
	;LD (IY+3),C
	;LD (IY+4),B
	
	;call L3_ComputePosFromShip
	call L3_ValidateAndComputePosFromShip
	jp TV2
.A5_follow_kill
	;call L3_ComputePosFromShip
	;ld HL,(last_explo)
	;inc HL
	;ld (HL),C
	;inc HL
	;ld (HL),B
	call L3_FixExplosion
	jp TV5

	
;***********
;*** HAT ***
;***********
.A6_hat
	jp c,A6_hatkill	
	
	call L3_ComputePosFromShip
	jp TV2
	
.A6_hatkill
	ld (A6_newhat+1),BC
	push BC	
	ld A,slot_hat
	ld HL,A6_newhat
	call L3_SpawnOnSlot
	pop BC
	call L3_ComputePosFromShip
;fix explo position
	push BC
	;ld HL,(last_explo)
	;inc HL
	;dec C
	;ld (HL),C
	;inc HL
	;dec B
	;ld (HL),B
	call L3_FixExplosion
	pop BC
	
	SetNullMaster
	jp TV2
.A6_newhat
	db 9,0,0

;*******************************
;*** CACHE B , HIDES A POKER ***
;*******************************
	
.A7_cacheB
	jr c,A7_cacheBkill	
	
	call L3_CheckDamage ;color keys ?
	
	call L3_ComputePosFromShip

;if X<14 force detach	
	ld A,C
	cp 14
	jp nc,TV2
	
	ld (IY+0),0
	ld (IY+8),128
	
	jp TV2	
.A7_cacheBkill
	push BC
	push DE
	
	pop DE
	pop BC
	push BC
	
	ld (A7_pokerB+1),BC	;write new pos for poker B
	call L3_ComputePosFromShip

	inc C
	inc B
	ld (A7_cacheB_new+1),BC

;spawn new cache	
	ld A,slot_cacheB
	ld HL,A7_cacheB_new
	call L3_SpawnOnSlot
;spawn poker B
	ld A,slot_pokerB
	ld HL,A7_pokerB
	call L3_SpawnOnSlot
	
	pop BC
	SetNullMaster
	call L3_ComputePosFromShip
	jp TV2 ;TV5
.A7_cacheB_new
	db 11,0,0
.A7_pokerB
	db 22,0,0
;**********************************
;*** TURRET W/ or W/OUT SUPPORT ***
;**********************************
.A8_turretA
	jp c,A8_turretA_kill
	
;write new position
	LD (IY+3),C
	LD (IY+4),B
	
	call L3_ComputePosFromShip

;check to shot	
	ld A,(FRAME)
	and 63
	ld L,A
	ld A,(MASTER)
	add A
	cp L
	call z,A8_turretA_shot

;compute sprite
	ld A,(DE)
	bit 1,A
	jr z,A8_turretA_NormalWay

;in reverse way , turret follow only if ship is under	
	ld A,(SHIPY)
	cp B
	jr c,A8_computeSpriteNoMove
	jr A8_ComputeSpriteMove	
;normal way , turret follow only if ship upper
.A8_turretA_NormalWay
	ld A,(SHIPY)
	cp B
	jr nc,A8_computeSpriteNoMove
;no move , d=ship_x-turret_x
.A8_computeSpriteMove
	ld A,(SHIPX)
	sub C
	jr c,A8_computeSpriteMoveNeg
	
	cp 3
	jr c,A8_computeSpriteMoveUpper

	ex AF,AF'
	add 3
	ex AF,AF'
	jp TV2
.A8_computeSpriteMoveNeg
	ex AF,AF'
	inc A
	ex AF,AF'
	jp TV2
.A8_computeSpriteMoveUpper
	ex AF,AF'
	add 2
	ex AF,AF'
	jp TV2
;no move , only +0 or +4
.A8_computeSpriteNoMove
	ld A,(SHIPX)
	cp C
	jp c,TV2
	ex AF,AF'
	add 4
	ex AF,AF'
	jp TV2
.A8_turretA_shot
	push BC	
	ld A,(MASTER)
	dec A
	call ALIENPOW_Fire	
	pop BC
	ret

.A8_turretA_kill

;get selector and setup table for updating ship
	ld A,(IY+5)
	ld HL,A8_turretTable
	call UpdateShipPart
	
	
	;call L3_ComputePosFromShip
	;ld HL,(last_explo)
	;inc HL
	;ld (HL),C
	;inc HL
	;ld (HL),B
	call L3_FixExplosion
	SetNullMaster
	jp TV2
	
.A8_turretTable	
;selector 1 - small turret with support
	db 50,0,2
;selector 2 - big turret with support
	db 70,0,2
;selector 3 - small turret with support B
	db 55,0,2
;selector 4 - big turret with support #2
	db 71,0,2


;***************
;*** CACHE A ***
;***************	
.A9_cacheA
	jr c,A9_cacheA_kill
	
	call L3_CheckDamage ;color keys ?
	
	;write new position
	;LD (IY+3),C
	;LD (IY+4),B	
	;call L3_ComputePosFromShip
	call L3_ValidateAndComputePosFromShip
	
	;if X<14 force detach	
	ld A,C
	cp 14
	jp nc,TV2
	
	;ld (IY+0),0
	;ld (IY+8),128
	call L3_SelfDestruct
	
	jp TV2
.A9_cacheA_kill
	
;update ship

	;push DE
	;push BC
	;ld A,15
	;ld B,cacheA_posY
	;ld C,cacheA_posX
	;call BS_Add
	;ld A,1
	;ld (ForceBSUpdate),A
	;pop BC
	;pop DE
	ld HL,A9_cacheA_kill_table
	call UpdateShipPart_Once

;slot_cacheA_turret3
	call L3_computePosFromShip
	inc B
	ld (A9_cacheA_new+1),BC
	dec B
	
	push BC

	
;spawn new turret
	ld A,slot_cacheA_turret3
	ld HL,A9_cacheA_turret1
	call L3_SpawnOnSlot
	
;destroy turrets on it
	ld A,slot_cacheA_turret1
	call L3_AutoDestruct
	ld A,slot_cacheA_turret2
	call L3_AutoDestruct
	
;spawn new cache
	ld A,slot_cacheA
	ld HL,A9_cacheA_new
	call L3_SpawnOnSlot
	pop BC
	SetNullMaster
	jp TV2	
	
.A9_cacheA_new
	db 15,0,0
.A9_cacheA_turret1
	db 19,cacheA_posX+2,cacheA_posY
.A9_cacheA_kill_table
	db 15,0,0

;*************
;*** POKER ***
;*************	
.A10_poker
	jp c,A10_poker_kill ;TV5

;write new position
	;LD (IY+3),C
	;LD (IY+4),B

;compute new position relative to ship	
	call L3_ComputePosFromShip

;must we shot ?
	ld A,(DE)
	bit 6,A
	jp z,TV2

	push DE
	push BC
	
;so here , check from B event and table
	ld A,(IY+5)
	and %11110000
	rrca
	rrca
	rrca
	sub 4					;selector A is on actionB #2
	ld E,A
	ld D,0
	ld HL,A10_pokerTable
	add HL,DE				;points on address of shot info
	
	ld E,(HL)
	inc HL
	ld D,(HL)				;get poker info in DE
	ex DE,HL				;finish in HL

;increment slot counter	
	ld A,(HL)
	xor 1
	ld (HL),A
	inc HL
	
	push HL
	ld E,A
	ld D,0
	add HL,DE
	ld A,(HL)	;so we got slot to use in A
	pop HL
	inc HL	;skip slot info
	inc HL
	push AF ;save slot	
;we are on dir counter
;increment and mod to 4
	ld A,(HL)
	inc A
	cp 5
	jr c,A10_ValidCanonCounter
	xor A
.A10_ValidCanonCounter
	ld (HL),A
	inc HL
	ld E,A

;compute shot offset
	ld A,(HL)
	add C
	ld C,A
	inc HL
	ld A,(HL)
	add B
	ld B,A
	inc HL
	
	
	ld D,0
	add HL,DE	;points to canon info
	ld D,(HL)
	pop AF		;restore slot
	push DE		;save info

;compute slot address	
 	DEC A
 	AND 127
 	LD E,A
	ADD A
 	ADD E
 	LD D,0
	LD E,A
	LD HL,ALIENFIRE
 	ADD HL,DE
;restore info in A , F will be corrupted
	pop AF
	or 16
	ld (HL),A
	inc HL
	
	ld (HL),C
	inc HL
	ld (HL),B
	
	pop BC	
	pop DE

	jp TV2	
.A10_poker_kill
	;push BC
	;push DE
	ld A,(IY+5)
	ld HL,A10_pokerkill_Table
	call UpdateShipPart
	;and %11110000
	;cp #30
	;jr z,A10_poker_killB
;.A10_poker_kill_finish
	;ld A,1
	;ld (ForceBSUpdate),A
	;pop DE
	;pop BC
	
	;call L3_ComputePosFromShip
	;ld HL,(last_explo)
	;inc HL
	;ld (HL),C
	;inc HL
	;ld (HL),B
	call L3_FixExplosion
	SetNullMaster
	jp TV2
;.A10_poker_killB
	;ld A,3
	;ld B,cacheB_posY
	;ld C,cacheB_posX
	;call BS_Add
	;jr A10_poker_kill_finish
	
.A10_pokerkill_Table
	db 80,0,0
	db 95,0,0
	db 85,1,0
	db 90,0,0
	
.A10_pokerTable
	dw poker_A ;close to hat
	dw poker_B ;under cache B
	dw poker_C ;front down
	dw poker_D ;rear
	
;poker structure
; byte current slot , 0-1
; byte (slot)*2
; byte counter , 0-5 inc for each canon
; byte (shot_offset)*2
; byte direction %0000RLDU
.poker_A
;slot info
	db 0
	db slot_pokerA,1
;canon info
	db -1
	db 1,1
	db %00000100
	db %00000001
	db %00000110
	db %00000101
	db %00001001
	
.poker_B
;slot info
	db 0
	db slot_pokerB,2
;canon info
	db -1
	db 2,2
	db %00001000
	db %00000010
	db %00001001
	db %00001010
	db %00000110

.poker_C
;slot info
	db 0
	db slot_pokerC,3
;canon info RLDU
	db -1
	db 1,1
	db %00000100
	db %00000010
	db %00000101
	db %00000110
	db %00001010
	
.poker_D
;slot info
	db 0
	db slot_pokerD,3
;canon info RLDU
	db -1
	db 1,1
	db %00001000
	db %00000001
	db %00001010
	db %00001001
	db %00000101
;****************
;*** REACTORS ***
;****************
.A11_reactor
	jp c,A11_reactor_kill
	
	call L3_ComputePosFromShip
	
	ld A,(A2_reactorOffset)
	add C
	ld C,A
	
	ex AF,AF'
	ld A,(A2_reactorDir)
	ex AF,AF'
	
	ld A,(A2_reactor_Fire)
	or A
	jp Z,TV2
	
	ex AF,AF'
	inc A
	ld L,A
	ex AF,AF'
	
	SaveMaster
	
	SetMaster NeutralMaster+1 ;very special master , not solid except with player
	ld A,(A2_reactor_Fire)
	add L
	ld L,A
	ld A,(FRAME)
	and 1
	add L
	ld L,A
	
	push BC
	push DE
	inc B
	inc B
	call TSPGADD
	call SPNS
	pop DE
	pop BC
	
	RestoreMaster
	
	jp TV2
.A11_reactor_kill
	ld A,(IY+5)
	and %11110000
	rrca
	rrca
	rrca
	rrca
	sub 2
	add 100
	

	push DE
	push BC
	
	dec B
	call BS_Add
	
	pop BC
	pop DE

	;call L3_ComputePosFromShip
	;ld HL,(last_explo)
	;inc HL
	;ld (HL),C
	;inc HL
	;ld (HL),B
	call L3_FixExplosion
	
	ld A,1
	ld (ForceBSUpdate),A
	
	jp TV5
	
;*****************
;***Rear canons***
;*****************
.A12_rearcanon
	jp C,A12_rearcanon_kill
	
	push AF
	call A12_ComputeSlot
	ld A,(HL)
	or A
	jr nz,A12_rearcanon_waitreborn
	inc HL
	ld A,(HL)
	dec A
	ld (HL),A
	call z,A12_rearcanon_shot
	pop AF
	
	jr z,A12_rearcanon_notDamaged
	
	ld A,(FRAME)
	and 1
	jr z,A12_rearcanon_notDamaged
	
	ex AF,AF'
	inc A
	ex AF,AF'
.A12_rearcanon_notDamaged
	
	call L3_ComputePosFromShip
	
	jp TV2
.A12_rearcanon_waitreborn
	pop AF
;until reborn = 0	
	dec (HL)
	
	ld A,(HL)
	cp 20
	jr c,A12_rearcanon_waitreborn_flickering
	
	ex AF,AF'
	add 2
	ex AF,AF'
	jr A12_rearcanon_waitreborn_done
	
.A12_rearcanon_waitreborn_flickering	
	ld A,(FRAME)
	and 1
	add A
	ld L,A
	ex AF,AF'
	add L
	ex AF,AF'

.A12_rearcanon_waitreborn_done

	call L3_ComputePosFromShip
	SetNullMaster
	jp TV2
	
	
.A12_rearcanon_kill
;correct explosion pos
	;call L3_ComputePosFromShip
	;ld HL,(last_explo)
	;inc HL
	;ld (HL),C
	;inc HL
	;ld (HL),B
	call L3_FixExplosion
;get slot and set reborn counter	
	call A12_ComputeSlot
L3_MASTERF		EQU $+1
L3_MASTERF_val	EQU	150	;faster respawn !
	ld (HL),250
;full health
	ld A,REAR_CANON_POW
	ld (IY+0),A
	
	SetNullMaster
	
	jp TV2
	
.A12_ComputeSlot
	push DE
	ld A,(IY+5)
	and %11110000
	rrca
	rrca
	sub 8					;selector A is on actionB #2
	ld E,A
	ld D,0
	ld HL,A12_rearcanon_slot_table
	add HL,DE				;points on address of rearcanon info
	pop DE
	ret
	
.A12_rearcanon_shot
;reset shot counter
L3_MASTERG		EQU	$+1
L3_MASTERG_val	EQU REAR_CANON_RATE-10

	ld (HL),REAR_CANON_RATE
	push BC
	inc HL
;get class
	ld A,(HL)
	ld (A12_rearcanon_projectile),A
	call L3_ComputePosFromShip
;get coords
	inc HL
	ld A,(HL)
	and %11110000
	rrca
	rrca
	rrca
	rrca
	add C
	ld C,A
	ld A,(HL)
	and %00001111
	sub 8
	add B
	ld B,A
	ld (A12_rearcanon_projectile+1),BC
	ld HL,A12_rearcanon_projectile
;spawn
	call FORCESYNC
	pop BC
	ret
	
.A12_rearcanon_projectile
	db 0,0,0
;individual table for canon
;0 - byte reborn counter
;1 - shot counter
;2 - shot class
;3 - offset 0bXXXXYYYY Y is signed, 8 = 0
.A12_rearcanon_slot_table
	db 0,10,32,#1A
	db 0,15,33,#2A
	db 0,20,34,#29
	db 0,25,33,#2A
	db 0,30,34,#28
	db 0,35,35,#26
	
;*******************
;*** FRONT TEETH ***
;*******************
.A13_Teeth
	jr c,A13_Teeth_kill
	
	;write new position
	;LD (IY+3),C
	;LD (IY+4),B
	
	call L3_ComputePosFromShip
	jp TV2
.A13_Teeth_kill
;break
;first , replace new sprite
	push BC
	push DE
	
	ex AF,AF'

	;ld A,19 ;quick and dirty bugfix
	push AF
	call BS_Add
	ld A,1
	ld (ForceBSUpdate),A
	pop AF
	ex AF,AF'
	
	pop DE
	pop BC
	
	;call L3_ComputePosFromShip
	;ld HL,(last_explo)
	;inc HL
	;ld (HL),C
	;inc HL
	;ld (HL),B
	call L3_FixExplosion
	SetNullMaster
	jp TV2

;*****************
;*** WEAKPOINT ***
;*****************	
.A14_weakpoint
	jr nc,A14_NotEndLvl
	ld A,1
	ld (NOSCROLL),A
	ld A,14-1	;10K
	call SCORE_Add
	call BIG_EXPLO_Start
	jp TV5
	
.A14_NotEndLvl	

	push AF
	call L3_CheckDamage
	pop AF
	
	ld A,21	
	jr z,A14_Nodamage
	ld L,A
	ld A,(FRAME)
	and 1
	add L	
.A14_Nodamage
	ld (A14_Body),A



	;write new position
	;LD (IY+3),C
	;LD (IY+4),B

	call L3_ValidateAndComputePosFromShip
	
	ld A,(DE)
	bit 6,A
	call nz,A14_weakpoint_spawnbullet
	
	push BC
	push DE
	
	inc C
	inc C
	inc C
	
	SaveMaster
	SetNullMaster
A14_Body	equ $+1	
	ld A,21
	call TSPGADD
	call SPNS
	
	RestoreMaster
	
	pop DE
	pop BC

	jp TV2
.A14_weakpoint_spawnbullet
	push BC
	push DE
	
	ld A,R
	and 7
	add 38
	ld (A14_bullet),A
	
	inc C
	inc C
	
	ld (A14_bullet+1),BC
	ld HL,A14_bullet
	call FORCESYNC
	
	pop DE
	pop BC
	ret
.A14_bullet
	db 39,0,0

	
;**************
;*** PODDER ***
;**************
pod_offset	EQU 124	;added 'cause podders are now in BASE
 ;PODDER EXPLOSION BYPASS
.A15_podder
 	JP C,TV5
	
	;half speed
	LD A,(FRAME)
	AND 1
	jr z,A15_NoMove
	
	LD (IY+3),C
	LD (IY+4),B
	
.A15_NoMove

	LD A,(DE)

	PUSH DE
	PUSH AF
 	EX AF,AF'
	add pod_offset	;base
 	CALL USPGADD
	POP AF
 	AND 3
	jp NZ,THEM6
 	CALL SPNS
 	jp THEM9	

;*** B events ***
.B0_null
	jp TV3

	
.B1_half_speed
	ld A,(FRAME)
	and 1
 	jp nz,TV3
 	jp TV4

;to allow selection in A funcs
.B2_selectorA
.B3_selectorB
.B4_selectorC
.B5_selectorD
.B6_selectorE
.B7_selectorF
	jp TV3
.B8
.B9
.BA
.BB
.BC
.BD_Laser
	ld L,1
	jr L3_BonusGive	
.BE_Circular
	ld L,2
	jr L3_BonusGive	
.BF_Fire
	LD L,3
;podder subroutine
.L3_BonusGive	
	
	CALL FXA99
	call JEW_Spawn
	jp TV3

;check podder death
.FXA99	
	LD A,(IY+0)
	and 63
 	jr NZ,FXA99A
	ret
.FXA99A	
	pop HL
	jr B1_half_speed
	

	
;special routine to render battleship
; B = X , C = Y
BS_Width	EQU 51
.BS_Draw

	push BC
	ld BC,#7FC5
	out (C),C
	pop BC

	xor A
	ld (BS_Draw_StartXoffset),A

	ld D,BS_Width	
	ld A,B
	cp 32
	call nc,Xneg_clip
	ld A,B
	add D
	sub 32
	call nc,Xpos_clip
	
	push BC		;save X,Y
	ld IXH,ATTR_Addr_high	
	
BS_Draw_StartAddress EQU $+1
	ld HL,0
BS_Draw_StartXoffset EQU $+1
	ld BC,0
	
	add HL,BC	;now we have sprite address in HL
	
	pop BC

;exchange BC and DE so BC=W/H , DE=X/Y
	ld A,B
	ld B,D	;B= W
	ld D,A	;D = X	
	ld E,C	;E = Y
	ld C,14	;C = H
	
	ex DE,HL ;now HL = X/Y , DE = sprite address

.BS_DrawYloop

	push HL		;save X/Y
	push BC
	push DE

	ld A,L	;get Y
	cp 20
	jr nc,BS_DrawSkipLine
	
	add A	;compute line in table
	add ATTR_Addr_low
	ld IXL,A
	
	ld A,(IX+0)	;get low addr
	add H		;add X

	ld L,A
	ld H,(IX+1) ;finish addr computation
	
;here DE = Sprite , HL = screen address	, B = W , C = H
	
.BS_DrawXloop
	ld A,(HL)
	and 3
	cp 3
	jr z,BS_DoNotDraw	;if it is a block , do not draw

	ld A,(DE)
	or A				;is this empty ?
	jr z,BS_CheckOld	;check if we need to erase old block
	
	inc DE
	ld A,H
	ex AF,AF'			;save high byte of scr address			
	
	ld (HL),5+128 		;magic static flag
	inc H
	ld (HL),#C5			;bank
	inc H
;low byte of tile	
	ld A,(DE)
	ld (HL),A
	inc DE
	inc H
;high byte of tile
	ld A,(DE)
	ld (HL),A
	inc DE	
	inc H
;master
	ld (HL),0
;restore H	
	ex AF,AF'
	ld H,A

.BS_DrawTileDone
	inc L
	djnz BS_DrawXloop

.BS_DrawSkipLine	
	pop HL
	ld DE,BS_Width*3
	add HL,DE
	ex DE,HL
	
	pop BC
	pop HL
	inc L
	dec C
	jr nz,BS_DrawYloop
	
	ret
	
.BS_CheckOld
	ld A,(HL)
	and 63
	cp 13
	jr nz,BS_DoNotDraw	
	ld (HL),4 ;so auto erase !
.BS_DoNotDraw	
	inc DE
	inc DE
	inc DE
	jr BS_DrawTileDone

	
;do negative clip X<0 or X>31
.Xneg_clip
	bit 7,B		;check if X>31 and >0
	jr z,Xneg_clip_clean_exit ; if yes , do not draw it
	
	ld A,BS_Width
	add B
;if X+W<0 (>127) the do not draw
	bit 7,A
	jr nz,Xneg_clip_clean_exit
;now compute the new width
	ld A,B
	add D	;D = -x + W so W = W - X 
;check if new W is <0 (or >127)
	bit 7,A
	jr nz,Xneg_clip_clean_exit
	ld D,A	;so D = W - X
;compute sprite start offset
	ld A,B 
	neg ;offset = - (-x) *3 
	ld B,A 
	add A
	add B
	ld (BS_Draw_StartXoffset),A

;X = 0
	ld B,0
	ret
	
.Xneg_clip_clean_exit
	pop BC ;get ret address to keep clean
	ret
	
.Xpos_clip
	ld E,A
	ld A,D
	sub E
	ld D,A
	ret
	
;erase an object to battleship sprite
;A = sprite , BC = YX
.BS_Erase
	push BC
	push AF
;first save bank
	ld A,(#4000)
	ld (BS_EraseBank),A
	ld BC,#7FC5
	out (C),C
	
;get sprite
	pop AF
	call BS_ComputeAddSprite	
;HL = sprite
;BC = W,H	
;DE = BS address

.BS_EraseYloop
	push BC
	push DE
.BS_EraseXloop
	ld A,(HL)
	or A
	jr z,BS_EraseXloopSkip
	xor A
	ld (DE),A
.BS_EraseXloopSkip
	inc HL
	inc HL
	inc HL
	inc DE
	inc DE
	inc DE
	djnz BS_EraseXloop
	pop DE
	ex DE,HL	;exchange to do addition
	ld BC,153	;line width
	add HL,BC
	ex DE,HL
	
	pop BC
	dec C
	jr nz,BS_EraseYloop
	
;restore bank
BS_EraseBank EQU $+1
	ld BC,#7F00
	out (C),C
	ret

;add an object to battleship sprite
;A = sprite , BC = YX
.BS_Add
	push BC
	push AF
;first save bank
	ld A,(#4000)
	ld (BS_AddBank),A
	ld BC,#7FC5
	out (C),C
	
;get sprite
	pop AF
	call BS_ComputeAddSprite	
;HL = sprite
;BC = W,H	
;DE = BS address

.BS_AddYloop
	push BC
	push DE
.BS_AddXloop
	ld A,(HL)
	or A
	jr z,BS_AddXloopSkip
	ld (DE),A
	inc DE
	inc HL
	ld A,(HL)
	ld (DE),A
	inc DE
	inc HL
	ld A,(HL)
	ld (DE),A
	inc DE
	inc HL
	djnz BS_AddXloop
	jr BS_AddXloop_End
.BS_AddXloopSkip
	inc HL
	inc HL
	inc HL
	inc DE
	inc DE
	inc DE
	djnz BS_AddXloop
.BS_AddXloop_End	
	pop DE
	ex DE,HL	;exchange to do addition
	ld BC,153	;line width
	add HL,BC
	ex DE,HL
	
	pop BC
	dec C
	jr nz,BS_AddYloop
	
;restore bank
BS_AddBank EQU $+1
	ld BC,#7F00
	out (C),C
	ret

;compute sprite and address in BS	
.BS_ComputeAddSprite
	call TSPGADD_bank5
	inc HL
;HL = sprite
;DE = W,H
;set W/H in BC
	ld C,E
	ld B,D
;compute address , works on secondary register set
	exx 
	pop HL		;save ret address
	pop BC
	push HL		;restore ret address
;compute Y	, 3*51 = 153 = 128+16+8+2+1
	ld L,B
	ld H,0
	push HL			;save *1
	add HL,HL		;*2
	add HL,HL		;*4
	add HL,HL		;*8
	push HL			;save *8
	add HL,HL		;*16
	push HL			;save *16
	add HL,HL		;*32
	add HL,HL		;*64
	add HL,HL		;*128
	pop DE
	add HL,DE		;+*16
	pop DE
	add HL,DE		;+*8
	pop DE
	add HL,DE		;+*1	
	ex DE,HL		;save in DE
	
;compute X as * 3
	ld B,0
	ld L,C
	ld H,0
	
	add HL,HL ;*2
	add HL,BC ;+*1
	
;add (x*3)+(y*153)
	add HL,DE
	ex DE,HL	;save in DE
	ld HL,(BS_Draw_StartAddress)
	add HL,DE	;add to BS start address

;restore in DE in main reg set
	push HL
	exx
	pop DE
	ret
	
read "l3_master_data.asm"	
	
