ORG level_start

lvl_offset EQU 7


IF output_files
	write "bin/level4.lvl"
ENDIF

;********************
;*** LEVEL 4 DATA ***	
;********************
 START	DEFB 0
 SYNCHRO	DEFW 0000
;
START1	DEFB 0 ;test_pos
START2	DEFB 41
START3	DEFB 255
;
SYNCHRO1	DEFW SYNC1 ;test_sync
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,20,10,2,1,25,16,18,9,15,22,0
	db 0
	db 0,20,2,3,0,10,1,1,0,12,3,9,0,3,9,0
	db 0
	db 0,11,1,3,0,1,0,1,0,3,0,9,0,3,0,0
	db 0
	db 0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0
	db 0

.lvl_floating_color
	db 14
.lvl_music
	db 3 
.lvl_skip
	dw 0	;music to skip on music restart
.lvl_init
	dw	l4_init	;called after loading
.lvl_reinit
	dw	l4_reinit	;called each time player dies
	
.l4_init
	ret
.l4_reinit
;init array
	ld HL,init_array
	ld (BG_LevelEffect0_Init),HL
	ld HL,run_array
	ld (BG_LevelEffect0_Run),HL
	ld HL,end_array
	ld (BG_LevelEffect0_Stop),HL
	
;reset patterns to 0
	ld HL,egg_pat00
	ld (egg_current_col),HL
	
	ld HL,egg_pattern0
	ld (egg_current_pattern),HL
	
;init compiled sprites	
	call init_array_sprites

;init starfield
	call STARS_Init
	ret
	
;****************************************
;*	LEVEL 4 - 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)
;****************************************
.SYNC1	
	START_EFFECT 0,1,1	;start egg layer
	
	db	1,1,55,0,0
	
	DEFB 8-lvl_offset,1,99,4,2
 	DEFB 8-lvl_offset,24,16,30,8

 	DEFB 10-lvl_offset,0,15,32,9
	
 	DEFB 10-lvl_offset,24,31,30,6 ;7
	
	DEFB 11-lvl_offset,16,4,30,14
	
	
	DEFB 11-lvl_offset,24,4,30,6
	
	
 	DEFB 12-lvl_offset,16,15,32,1
	
	DEFB 13-lvl_offset,4,5,31,15
 	DEFB 13-lvl_offset,8,2,30,5
	
	DEFB 14-lvl_offset,3,2,30,8
	DEFB 15-lvl_offset,7,2,30,8
	DEFB 15-lvl_offset,8,5,30,15
	
	
 	DEFB 15-lvl_offset,8,12,16,254	;12,254
	
 	DEFB 15-lvl_offset,25,2,30,6
 	DEFB 16-lvl_offset,20,2,30,10
	DEFB 17-lvl_offset,16,32,30,5

	DEFB 18-lvl_offset,0,14,254,1
	DEFB 18-lvl_offset,18,2,30,4
 	DEFB 18-lvl_offset,24,17,26,254
	DEFB 19-lvl_offset,1,23,30,15
	DEFB 19-lvl_offset,4,2,30,10
 	DEFB 19-lvl_offset,8,15,32,9	;30
 	DEFB 19-lvl_offset,13,2,30,5
 	DEFB 19-lvl_offset,16,5,30,15



	
	DEFB 20-lvl_offset,24,33,30,2
	DEFB 22-lvl_offset,8,12,5,254
	DEFB 23-lvl_offset,1,26,10,254	;12
 	DEFB 23-lvl_offset,1,27,13,254	;15
 	DEFB 24-lvl_offset,0,4,30,17
	
 	DEFB 25-lvl_offset,15,45,20,254
	DEFB 25-lvl_offset,18,45,20,254
	DEFB 25-lvl_offset,21,45,20,254
	DEFB 25-lvl_offset,24,45,20,254
	DEFB 25-lvl_offset,27,45,20,254
	DEFB 25-lvl_offset,30,45,20,254
	DEFB 26-lvl_offset,1,45,20,254
 	DEFB 26-lvl_offset,4,45,20,254
 	DEFB 26-lvl_offset,7,45,20,254
 	DEFB 26-lvl_offset,10,45,20,254
	
	DEFB 27-lvl_offset,16,15,32,0 ;30
	DEFB 27-lvl_offset,16,13,16,20
 	DEFB 27-lvl_offset,24,12,17,254
	DEFB 27-lvl_offset,24,14,254,2
	
 	DEFB 28-lvl_offset,0,15,32,7 ;30
 	DEFB 28-lvl_offset,12,52,4,0
 	DEFB 28-lvl_offset,24,14,254,8



	
 	DEFB 30-lvl_offset,0,15,32,6 ;30
 	DEFB 30-lvl_offset,0,4,255,8
 	DEFB 30-lvl_offset,16,4,30,16
	
	DEFB 31-lvl_offset,0,15,32,7	;30
 	DEFB 32-lvl_offset,0,15,32,8	;30

 	;DEFB 32-lvl_offset,8,28,11,254
 	;DEFB 32-lvl_offset,16,28,10,254
	;DEFB 32-lvl_offset,24,28,9,254
 	;DEFB 33-lvl_offset,0,28,8,254
	
 	DEFB 32-lvl_offset,0,28,11,254
 	DEFB 32-lvl_offset,8,28,10,254
	DEFB 32-lvl_offset,16,28,9,254
 	DEFB 32-lvl_offset,24,28,8,254
	

	
	DEFB 33-lvl_offset,16,34,30,6
	
;db 0,0,0,0,0	
	
	DEFB 34-lvl_offset,8,15,32,2	;30
 	DEFB 34-lvl_offset,16,52,26,0
	
	
	DEFB 34-lvl_offset,23,35,30,11

		
	
 	DEFB 35-lvl_offset,0,29,10,254
 	DEFB 35-lvl_offset,8,14,254,1
	DEFB 35-lvl_offset,8,23,30,15
	DEFB 35-lvl_offset,25,23,30,15
 	DEFB 36-lvl_offset,4,5,30,15
 	DEFB 36-lvl_offset,8,25,254,15
 	DEFB 36-lvl_offset,24,24,254,15
	DEFB 37-lvl_offset,0,18,24,254
 	DEFB 38-lvl_offset,0,29,1,254
	DEFB 39-lvl_offset,0,29,4,254
	DEFB 40-lvl_offset,0,19,254,11

	CHECK_POINT	41-lvl_offset,0
.SYNC2
	DEFB 42-lvl_offset,8,12,14,254
 	DEFB 42-lvl_offset,16,12,9,254
 	DEFB 43-lvl_offset,0,4,30,2
	DEFB 43-lvl_offset,20,36,30,2
	DEFB 44-lvl_offset,8,13,16,20
	DEFB 45-lvl_offset,0,13,25,20
	
	DEFB 45-lvl_offset,0,15,32,5	;30
 	DEFB 45-lvl_offset,8,15,32,3
 	DEFB 45-lvl_offset,8,15,32,8
	
 	DEFB 45-lvl_offset,16,53,20,19
	
	DEFB 46-lvl_offset,0,37,30,6
	
 	DEFB 46-lvl_offset,0,26,9,254
	
	DEFB 46-lvl_offset,16,27,13,254
	;DEFB 46-lvl_offset,16,37,30,4

	
	
	
	
	DEFB 47-lvl_offset,0,4,255,11		;alien 		
	DEFB 47-lvl_offset,8,4,8,255		;alien 		
 	DEFB 47-lvl_offset,16,4,20,255		;alien


	;test_pos	EQU 47-lvl_offset
	;test_sync	EQU $
	;START_EFFECT test_pos,1,1	;start egg layer
	

 	DEFB 49-lvl_offset,16,19,254,11
	DEFB 50-lvl_offset,0,17,24,254
 ;
	;DEFB 50-lvl_offset,15,1,31,2
 	;DEFB 50-lvl_offset,23,1,31,2
 	;DEFB 50-lvl_offset,31,1,31,2
	
	DEFB 50-lvl_offset,31,56,0,0	;new egg layer
	
	
 	DEFB 51-lvl_offset,0,15,32,9	;30	egg ship
 	DEFB 51-lvl_offset,4,15,32,6	;egg ship
	DEFB 51-lvl_offset,8,15,32,15	;egg ship
	DEFB 51-lvl_offset,12,15,32,12	;egg ship
 	DEFB 51-lvl_offset,16,15,32,3	;egg ship
	
 	DEFB 55-lvl_offset,9,38,35,13	;pod 1 30,13

 	DEFB 56-lvl_offset,24,4,30,15	;alien 
	DEFB 56-lvl_offset,24,4,30,13	;alien
	DEFB 56-lvl_offset,24,4,30,9	;alien
 	DEFB 57-lvl_offset,0,52,16,0	;egg eater
	

	
 	DEFB 57-lvl_offset,16,39,34,7	;pod 2 & 3
 	DEFB 58-lvl_offset,16,40,32,13	;14

	DEFB 59-lvl_offset,0,4,29,255	;alien

 	DEFB 59-lvl_offset,8,4,30,7		;alien

	DEFB 61-lvl_offset,8,4,20,255	;alien
	

	DEFB 61-lvl_offset,8,53,26,19	;egg eater

 	DEFB 61-lvl_offset,24,53,12,19	;egg eater

		
 	DEFB 62-lvl_offset,0,41,32,253	;podder
	
 	DEFB 62-lvl_offset,0,51,0,4		;egg eater
	DEFB 62-lvl_offset,0,51,0,7		;egg eater
	DEFB 62-lvl_offset,0,51,0,11	;egg eater
 	DEFB 62-lvl_offset,0,51,0,15	;egg eater

 	DEFB 62-lvl_offset,16,52,15,0	;egg eater

 	DEFB 62-lvl_offset,24,51,0,9	;egg eater

 	DEFB 63-lvl_offset,0,4,8,255	;alien
 
 	DEFB 64-lvl_offset,0,52,23,0	;egg eater
 
 	DEFB 69-lvl_offset,24,21,30,5	
	DEFB 70-lvl_offset,24,15,32,5	;30
	DEFB 71-lvl_offset,24,22,254,6
	
	test_pos	EQU 73-lvl_offset
	test_sync	EQU $
	
	LVL_Music	74-lvl_offset,0,8
 ;stop scrolling
	DEFB 74-lvl_offset,0,99,1,0
	DEFB 74-lvl_offset,0,99,4,4
	
	STOP_EFFECT	 74-lvl_offset,1	;cleanup screen	;always on impair count, so static will be refresh next time
	

	
	;START_EFFECT 74-lvl_offset,1,0	;null effect	
	
	;db 0,0,0,0,0
	
	;**************
	;*** SHIP 3 ***
	;**************
;event controller	
	db 75-lvl_offset,0,99,3,20
	db 75-lvl_offset,0,60,5,5
	
	DEFB 75-lvl_offset,0,99,3,1	
	DEFB 75-lvl_offset,0,44,241,5	;SHIP 3

	DEFB 75-lvl_offset,0,99,3,5	
	DEFB 75-lvl_offset,0,59,243,13	;turret ship 3 low
	
	DEFB 75-lvl_offset,0,99,3,6	
	DEFB 75-lvl_offset,0,58,242,10	;turret ship 3 hi	
	
	DEFB 75-lvl_offset,0,99,3,10
 	DEFB 75-lvl_offset,0,50,248,8	;GLOWER 3


	;**************
	;*** SHIP 1 ***
	;**************
	
	DEFB 75-lvl_offset,0,99,3,3
	DEFB 75-lvl_offset,0,42,239,2	;SHIP 1
	
	DEFB 75-lvl_offset,0,99,3,8
	DEFB 75-lvl_offset,0,57,239,4	;turret ship 1
	
	DEFB 75-lvl_offset,0,99,3,4
	DEFB 75-lvl_offset,0,48,245,8	;GLOWER 1

	;**************
	;*** SHIP 2 ***
	;**************

	DEFB 75-lvl_offset,0,99,3,2
	DEFB 75-lvl_offset,0,43,245,2	;SHIP 2	no turret !
	
	DEFB 75-lvl_offset,0,99,3,7
	DEFB 75-lvl_offset,0,49,245,4	;GLOWER 2

;connect 1	
	LVL_SetSpec1	#4A,6,6			;connect 2+3
;connect 2 , test both possibilites
	LVL_SetSpec1	#4E,1,5			;connect 1+3
	LVL_SetSpec1	#4E,1,3			;connect 1+2
;connect 3
	LVL_SetSpec1	#55,11,6			;connect 3+2
;connect 4
	LVL_SetSpec1	#56,0,3		;connect 1+2
	LVL_SetSpec1	#56,0,5		;connect 3+1
;connect 5
	LVL_SetSpec1	#58,20,3			;connect 1+2
	LVL_SetSpec1	#58,20,5			;connect 3+1
;connect 6
	LVL_SetSpec1	#5D,4,3			;connect 1+2
	LVL_SetSpec1	#5D,4,5			;connect 3+1
	
	
	DEFB 103-lvl_offset,16,99,5,0
;
SYNC3	;DEFB 255,255,255,255,255	
;****************************************
 ;	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 
;****************************************
 ;FISH EGGS * 20
SQ1	DEFB 3+#20,63,12+16 ;12+16*1
 	DEFW SP1,0
;RED TWISTERS
SQ2	DEFB 0,1+64,2+16 ;*1
 	DEFW SP3,SP3,SP3,SP3,0
;
SQ3
;
;NODDING SNALIEN
 SQ4	DEFB 12+16,1+64,3+#20 ;2*16
 	DEFW SP14B,SP14,SP14C,SP14A,65535
 ;MISSILE WALKER
L4_MASTER0		EQU $+1
L4_MASTER0_val	EQU 48
SQ5	DEFB 14+16,16,5+32 ;1*16
	DEFW SP26A,SP26E,SP26C,SP26D
	DEFW SP26D,SP26C,SP26E,SP26A,SP26E,SP26C,SP26D
	DEFW SP26D,SP26C,SP26A,SP26EE,SP26CC,SP26DD,SP26DD
	DEFW SP26A,SP26A,0
;MISSILE 1 L
 SQ6	DEFB 15,1,#30 ;3*16
	DEFW SP27A,SP27A,SP27C,SP27B,SP27B,0
;MISSILE 2 L
 SQ7	DEFB 15,1,#30 ;3*16
	DEFW SP27A,SP27A,SP27CC,SP27B,SP27B,0
 ;MISSILE 1 R
 SQ8	DEFB 15,1,#30 ;3*16
	DEFW SP27A,SP27A,SP27D,SP27B,SP27B,0
;MISSILE 2 R
SQ9	DEFB 15,1,#30 ;3*16
	DEFW SP27A,SP27A,SP27DD,SP27B,SP27B,0
;FISH EGGS *3
SQ10	DEFB 4+#20,63,12+16 ;*1
		DEFW SP1A,0
 ;FISH EGGS *1
SQ11	;DEFB 5+#20,63,12+16 ;*1
		;DEFW SP1B,0
 ;UP/DOWN EGG LAYER
 SQ12	DEFB 7,1,4+#20 ;16*2
 	DEFW SP4B8,SP4B8,SP4B8,0
;DOWN/UP EGG LAYER
 SQ13	DEFB 8,1,4+#20 ;16*2
 	DEFW SP4A8,SP4A8,SP4A8,0
;LEFT/RIGHT EGG LAYER
SQ14	DEFB 6,1,4+#20 ;16*2
 	DEFW SP4D8,SP4D8,SP4D8,SP4D8,SP4D4,0
;RIGHT/LEFT EGG LAYER
SQ15	DEFB 6,1,4+#20 ;16*2
 	DEFW SP4C8,SP4C8,SP4C8,SP4C8,SP4C4,0
;EGG LAYER 1
 SQ16	DEFB 6,1,4+#20 ;16*2
 	DEFW SP4E8,SP4C4,SP4G2,SP4B2,SP4H2,SP4B2,SP4G2
	DEFW SP4B2,SP4H2,SP4B2,SP4G2,SP4C4,SP4E8,SP4E4,0
;EGG LAYER 5
 SQ17	DEFB 6,1,4+#20 ;16*2
 	DEFW SP4B8,SP4B5,SP4G5,SP4C5,SP4E5
	DEFW SP4A4,SP4F5,SP4D5,SP4H8,SP4H8,0
;EGG LAYER 18
SQ18	DEFB 6,1,4+#20 ;16*2
 	DEFW SP4B8,SP4B1,SP4G6,SP4C8,SP4C2,SP4E8,0
;EGG LAYER 19
SQ19	DEFB 6,1,4+#20 ;16*2
 	DEFW SP4H4,SP4D4,SP4F4,SP4D4,SP4H4,SP4D6,SP4F4
	DEFW SP4A6,SP4E4,SP4C6,SP4G4,SP4B6,SP4H8,0
;EGG LAYER 27
SQ20	DEFB 6,1,4+#20 ;16*2
 	DEFW SP4A8,SP4A8,SP4F4,SP4D4,SP4H4,SP4B4
	DEFW SP4G3,SP4C3,SP4E3,SP4A4,SP4F8,SP4F8,0
;EGG LAYER 35
SQ21	DEFB 6,1,4+#20 ;16*2
 	DEFW SP4E5,SP4C6,SP4G8,SP4G8,SP4C8,0
;EGG LAYER 37
SQ22	DEFB 6,1,4+#20 ;16*2
 	DEFW SP4D8,SP4D8,SP4D2,SP4H4,SP4B4,SP4G8,0
;WALKER 1
SQ23	DEFB 1+16,1+64,2+32 ;*1
 	DEFW SP8MEG,SP8MEG,SP8MEG,SP8MEG,0
;WALKER 2
SQ24	DEFB 1+16,1+64,2+32 ;*1
 	DEFW SP80MEG,SP80MEG,SP80MEG,SP80MEG,0
;MISSILE WALKER LEFT
L4_MASTER1		EQU $+1
L4_MASTER1_val	EQU 48
 SQ25	DEFB 14+16,3,5+32 ;1*16
 	DEFW SP26B,SP26EE,SP26CC,SP26DD
 	DEFW SP26DD,SP26CC,SP26EE,SP26B,SP26EE,SP26CC,SP26DD
	DEFW SP26DD,SP26CC,SP26B,SP26E,SP26C,SP26D,SP26D
	DEFW SP26B,SP26B,0
;WALKER 3
SQ26	DEFB 1+16,1+64,2+32 ;16*1
 	DEFW SP8A,SP8A,SP8A,SP8AA,SP8B,SP8MEG,SP8MEG,0
;WALKER 4
SQ27	DEFB 1+16,1+64,2+32 ;16*1
 	DEFW SP8D_bis,SP8D_bis,SP8D,SP8D,SP8DD,SP8E,SP80,SP80	;+sp8D_bis*2
 	DEFW SP8F,SP8D,SP8D,SP8D,0
;WALKER 5
SQ28	DEFB 1+16,1+64,2+32 ;16*1
 	DEFW SP8D,SP8D,SP8DD,SP8E,SP802,SP8F,SP8DD
	DEFW SP8DD,SP8E,SP80MEG,SP80MEG,0
 ;WALKER 6
SQ29	DEFB 1+16,1+64,2+32 ;16*1
 	DEFW SP8D,SP8D,SP8DD,SP8DD,SP8DD,SP8E
 	DEFW SP80MEG,SP80MEG,SP80MEG,SP80MEG,0
;BIG SHIP LASERS
SQ30	DEFB 0,63,12+16 ;16*1
		DEFW SP5,SP5,SP5,0
;PODDER 1
SQ31	DEFB 2+#B0,1,2+32
		DEFW SP11A	;SP11A
		dw SP11G,SP11GG,SP11HH,SP11II,0
 ;PODDER 2
SQ32	DEFB 2+#E0,1,2+32
		DEFW SP11A,SP11B,SP11C,SP11BB,SP11A,SP11B,SP11C,SP11BB,0
;PODDER 3
SQ33	DEFB 2+#D0,1,2+32
 	DEFW #2020 ;32*256+32
;PODDER 4
SQ34	DEFB 2+#70,1,2+32
	DEFW #2020 ;32*256+32
;PODDER 5
SQ35	DEFB 2+#F0,1,2+32
 	DEFW SP11AA,SP11G,SP11G,SP11E,SP11E,SP11E,SP11E,0
 ;PODDER 6
SQ36	DEFB 2+#C0,1,2+32
 	DEFW #2020 ;32*256+32
;PODDER 7
SQ37	DEFB 2+#E0,1,2+32
		DEFW SP11A,SP11G,SP11GG,SP11HH,SP11II,0
 ;PODDER 8
SQ38	DEFB 2+#D0,1,2+32

	repeat 5
		dw SP11E,SP11EE
		dw SP11E,SP11EE
		dw SP11E,SP11EE
		dw SP11E,SP11EE
	rend

		;DEFW SP11J,SP11J,SP11J,SP11J,SP11J,SP11J,SP11J,SP11J,0
		dw 0
;PODDER 9
SQ39	DEFB 2+#D0,1,2+32
		DEFW #2626 ;38*256+38
;PODDER 10
 SQ40	DEFB 2+#B0,1,2+32
		DEFW #2626 ;38*256+38
;PODDER 11
 SQ41	DEFB 2+#80,1,2+32
		DEFW SP11AA,SP11G,SP11E,SP11GG,SP11HH,0
 ;
;ALIEN SHIP 1
SQ42	DEFB 10+#30,63,#40 ;DEFB 10+16*3,63,12+16*4
		DEFW SP68,SP68,SP68,SP66,SP56,SP55,SP44,SP36,SP68,SP63
		DEFW SP58,SP53,SP48,SP45,SP116,SP68,SP61
		DEFW SP58,SP51,SP38,SP34,SP112
		DEFW SP48,SP44,SP38,SP34,SP68,SP63,SP43
		DEFW SP42,SP58,SP53,SP85,SP66,SP95,SP85
		DEFW SP58,SP53,SP48,SP44,SP68,SP66,SP38,SP35,SP58,SP56
		DEFW SP118,SP118,SP118,SP116
		DEFW SP106,SP56,SP86,SP105,SP35,SP58,SP53,SP43,SP104,SP74
		DEFW SP33,SP118,SP68,SP66,SP48,SP45,SP116,SP78,SP75,SP51,SP118
		DEFW SP65,SP117,SP68,SP68,SP68,0
;
;ALIEN SHIP 2
SQ43	DEFB 10+#40,63,#40 ;DEFB 10+16*4,63,12+16*4
		DEFW SP68,SP68,SP68,SP66,SP56,SP55,SP44,SP36,SP68,SP63
		DEFW SP118,SP118,SP118,SP48,SP44,SP113
		DEFW SP58,SP51,SP38,SP34,SP112
		DEFW SP48,SP44,SP38,SP34,SP68,SP61,SP112,SP43
		DEFW SP42,SP58,SP53,SP85,SP66,SP95,SP85
		DEFW SP118,SP114,SP58,SP58,SP52,SP48,SP44,SP68,SP68,SP62
		DEFW SP38,SP34,SP58,SP53,SP118,SP14
		DEFW SP106,SP56,SP86,SP105,SP35,SP58,SP53,SP43,SP104,SP74
		DEFW SP104,SP67,SP58,SP56,SP37,SP68,SP66,SP118,SP58,SP53,SP111
		DEFW SP65,SP117,SP68,SP68,SP68,0
;
;ALIEN SHIP 3
SQ44	DEFB 10+#50,63,#40 ;DEFB 10+16*5,63,12+16*4
		DEFW SP68,SP68,SP68,SP66,SP56,SP55,SP44,SP36,SP68,SP63
		DEFW SP46,SP114,SP78,SP72,SP54,SP118,SP112
		DEFW SP68,SP68,SP62,SP48,SP42,SP38,SP32,SP112
		DEFW SP48,SP42,SP54,SP118,SP32,SP112
		DEFW SP42,SP58,SP53,SP85,SP66,SP95,SP85
		DEFW SP45,SP118,SP64,SP38,SP32,SP58,SP58,SP52,SP48,SP42
		DEFW SP68,SP68,SP62,SP58,SP57,SP36
		DEFW SP106,SP56,SP86,SP105,SP115,SP58,SP53,SP33,SP104,SP74
		DEFW SP104,SP67,SP58,SP56,SP118,SP118,SP118,SP118,SP82,SP35,SP112
		DEFW SP65,SP47,SP68,SP68,SP68,0
;YELLOW BUTTERFLY
SQ45	DEFB 0,1+64,3+#20
		DEFW SP7,SP7A,SP7A,SP7A,0
SQ46
SQ47
;
L4_MASTER2		EQU $+1
L4_MASTER2_val	EQU 60
;ALIEN GLOWER 1
SQ48	DEFB 11+#30,32,11+#20
		DEFW #2A2A,0
L4_MASTER3		EQU $+1
L4_MASTER3_val	EQU 60
;ALIEN GLOWER 2
SQ49	DEFB 11+#40,32,11+#20
		DEFW #2B2B,0
		
L4_MASTER4		EQU $+1
L4_MASTER4_val	EQU 60
;ALIEN GLOWER 3
SQ50	DEFB 11+#50,32,11+#20
		DEFW #2C2C,0
;EGG EATER LEFT/RIGHT
SQ51	DEFB 13,1,2+#20
		DEFW SP6B,65535
;EGG EATER UP/DOWN
 SQ52	DEFB 13,1,2+#20
		DEFW SP6,65535
;EGG EATER DOWN/UP
 SQ53	DEFB 13,1,2+#20
	DEFW SP6A,65535
 ;EGG EATER RIGHT/LEFT
SQ54	DEFB 13,1,2+#20
	DEFW SP6C,65535
	
;SENDING PATTERNS FOR EGG LAYERS
SQ55	DEFB 4,63,16 ;*1
		DEFW egg_pattern1,0
		
SQ56	DEFB 4,63,16
		DEFW egg_pattern2,0
	
;turrets	
SQ57	DEFB 9+#30,62+64,12+16
		DEFW #2A2A
		
;turrets	
SQ58	DEFB 9+#40,62+64,12+16
		DEFW #2C2C
		
;turrets	
SQ59	DEFB 9+#50,62+64,12+16
		DEFW #2C2C
		
;event controller for boss
SQ60
		db 5,63,0
		dw controller_spr
		dw #FFFF
.controller_spr
		db 1,128
		

;****************************************
 SQTABLE	DEFW SQ1,SQ2,SQ3,SQ4,SQ5,SQ6,SQ7,SQ8,SQ9,SQ10
	DEFW SQ11,SQ12,SQ13,SQ14,SQ15,SQ16,SQ17,SQ18,SQ19,SQ20
	DEFW SQ21,SQ22,SQ23,SQ24,SQ25,SQ26,SQ27,SQ28,SQ29,SQ30
	DEFW SQ31,SQ32,SQ33,SQ34,SQ35,SQ36,SQ37,SQ38,SQ39,SQ40
	DEFW SQ41,SQ42,SQ43,SQ44,SQ45,SQ46,SQ47,SQ48,SQ49,SQ50
	DEFW SQ51,SQ52,SQ53,SQ54
	
	DW 	 SQ55,SQ56,SQ57,SQ58,SQ59,SQ60
;****************************************
 ;	1ST BYTE = SPRITE NUMBER
 ;
;	2ND BYTE =
 ;		BITS 0-1 = ORIENTATION
;		BIT 2 = UP
;		BIT 3 = DOWN
;		BIT 4 = LEFT
;		BIT 5 = RIGHT
 ;		BIT 6 = 
;		BIT 7 = END OF PATTERN
;****************************************	
;FISH EGGS
SP1		DEFB 54,0+128	;20 12
SP1A	DEFB 53,0+128	;3 11
SP1B	DEFB 52,0+128	;1 10
;
;RED TWISTERS
SP3	DEFB 55,16,56,24,57,16,56,20,55,20,56,16,57,24,56,16+128
 ;
;EGG LAYERS
SP4A8	DEFB 52,4	;UP
SP4A7	DEFB 52,4
SP4A6	DEFB 52,4
SP4A5	DEFB 52,4
SP4A4	DEFB 52,4
SP4A3	DEFB 52,4
SP4A2	DEFB 52,4
SP4A1	DEFB 52,4+128
;
SP4B8	DEFB 52,10	;DOWN
 SP4B7	DEFB 52,10
 SP4B6	DEFB 52,10
 SP4B5	DEFB 52,10
 SP4B4	DEFB 52,10
 SP4B3	DEFB 52,10
 SP4B2	DEFB 52,10
 SP4B1	DEFB 52,10+128
 ;
SP4C8	DEFB 54,17	;LEFT
 SP4C7	DEFB 54,17
 SP4C6	DEFB 54,17
 SP4C5	DEFB 54,17
 SP4C4	DEFB 54,17
 SP4C3	DEFB 54,17
 SP4C2	DEFB 54,17
 SP4C1	DEFB 54,17+128
 ;
SP4D8	DEFB 54,32	;RIGHT
SP4D7	DEFB 54,32
 SP4D6	DEFB 54,32
 SP4D5	DEFB 54,32
 SP4D4	DEFB 54,32
 SP4D3	DEFB 54,32
 SP4D2	DEFB 54,32
 SP4D1	DEFB 54,32+128
 ;
SP4E8	DEFB 53,21	;UP/LEFT
SP4E7	DEFB 53,21
 SP4E6	DEFB 53,21
 SP4E5	DEFB 53,21
 SP4E4	DEFB 53,21
 SP4E3	DEFB 53,21
 SP4E2	DEFB 53,21
 SP4E1	DEFB 53,21+128
 ;
SP4F8	DEFB 53,36	;UP/RIGHT
 SP4F7	DEFB 53,36
 SP4F6	DEFB 53,36
 SP4F5	DEFB 53,36
 SP4F4	DEFB 53,36
 SP4F3	DEFB 53,36
 SP4F2	DEFB 53,36
 SP4F1	DEFB 53,36+128
 ;
SP4G8	DEFB 53,27	;DOWN/LEFT
SP4G7	DEFB 53,27
 SP4G6	DEFB 53,27
 SP4G5	DEFB 53,27
 SP4G4	DEFB 53,27
 SP4G3	DEFB 53,27
 SP4G2	DEFB 53,27
 SP4G1	DEFB 53,27+128
 ;
SP4H8	DEFB 53,42	;DOWN/RIGHT
 SP4H7	DEFB 53,42
 SP4H6	DEFB 53,42
 SP4H5	DEFB 53,42
 SP4H4	DEFB 53,42
 SP4H3	DEFB 53,42
 SP4H2	DEFB 53,42
 SP4H1	DEFB 53,42+128
 ;BIG SHIP LASERS
SP5	DEFB 32,32,32,32,32,32,32,32
 	DEFB 32,32,32,32,32,32,32,32+128
;EGG EATERS
;SP6		DEFB 49,0,49,8,49,1,49,1+8+128			;UP/DOWN
SP6			;db 49,0,50,8,51,0,50,8+128
			db 49,0,49,8,50,0,50,8,51,0,51,8,50,0,50,8+128

;SP6A	DEFB 49,2,49,2+4,49,3,49,3+4+128	;DOWN/UP
SP6A		;db 49,2,50,4+2,51,2,50,4+2+128
		db 49,2,49,4+2,50,2,50,4+2,51,2,51,4+2,50,2,50,4+2+128

;SP6B	DEFB 46,0,46,32,46,2,46,2+32+128	;LEFT/RIGHT
SP6B	;db 46,0,47,32,48,0,47,32+128
		db 46,0,46,32,47,0,47,32,48,0,48,32,47,0,47,32+128

;SP6C	DEFB 46,1,46,1+16,46,3,46,3+16+128	;RIGHT/LEFT
SP6C	;db 46,1,47,1+16,48,1,47,1+16+128
		db 46,1,46,1+16,47,1,47,1+16,48,1,48,1+16,47,1,47,1+16+128

;YELLOW BUTTERFLIES
SP7		DEFB 33,8,33,8,33,8,33,8,33,8,33,8,33,8,33,8,34,24,34,24,34,24+128
SP7A	DEFB 35,16,35,16,35,16,35,16,35,16,35,16+128
;STRAIGHT WALKER R / L
 SP8MEG	DEFB 28,16,29,16
	DEFB 28,16,30,16			;WALK L 8
SP8	DEFB 28,16,29,16
 SP8M2	DEFB 28,16,30,16+128	 ;WALK L 4

 SP80MEG	DEFB 28,33,29,33,28,33,30,33
 SP80	DEFB 28,33,29,33
SP802	DEFB 28,33,30,33+128	 ;WALK R

 SP8A	DEFB 31,8,31,8,31,8
 SP8AA	DEFB 31,8,31,8+128	 ;FALL 5 L
SP8B	DEFB 30,0,30,0+128	 ;STAND STILL L
SP8C	DEFB 31,20,31,16,31,24+128	 ;ARC L
SP8D	DEFB 31,9
 SP8DD	DEFB 31,9,31,9,31,9+128 ;FALL 4 R
SP8E	DEFB 30,1,30,1+128	 ;STAND STILL R
SP8F	DEFB 31,37,31,33,31,41+128	 ;ARC R

SP8D_bis	db 31,9+128	;fall 1 alone
;
;PODDERS
SP11A	DEFB 9,24			;FLY D/L 5
SP11AA	DEFB 9,24,9,24,9,24,9,24+128	;FLY D/L 4 
SP11B	DEFB 9,16,9,16			;FLY L 4
SP11BB	DEFB 9,16,9,16+128		;FLY L 2
SP11C	DEFB 9,20			;FLY U/L 5
		DEFB 9,20,9,20,9,20,9,20+128	;FLY U/L 4
		
SP11E	DEFB 7,16,5,16,7,16,6,16
		DEFB 7,16,5,16,7,16,6,16+128	;WALK L
		
SP11EE	DEFB 7,33,5,1,7,33,6,1 ;33
		DEFB 7,33,5,1,7,33,6,1+128	;WALK R
		
SP11G	DEFB 8,0,8,0+128			;LAND IMPACT L
SP11GG	DEFB 8,1,8,1+128			;LAND IMPACT R
SP11HH	DEFB 9,37,9,37,9,37,9,37+128 ;FLY U/R 4
SP11II	DEFB 9,33,9,33+128		;FLY R 2
SP11J	DEFB 8,0,8,0,8,0,8,16,8,0,8,0,8,0,8,16
		DEFB 8,0,8,0,8,0,8,16,8,0,8,0,8,0,8,16+128

 ;
;NODDING SNALIEN
 SP14	DEFB 19,0,20,0,20,0+128
 SP14A	DEFB 21,0,20,0,20,0+128
SP14B	DEFB 19,0+128
SP14C	DEFB 21,0+128
;
;MISSILE WALKERS
 SP26A	DEFB 14,16,15,16
 	DEFB 16,16,15,16
	DEFB 14,16,15,16,16,16,15,16+128	;WALK L 8
SP26B	DEFB 14,33,15,33,16,33,15,33
 	DEFB 14,33,15,33,16,33,15,33+128	;WALK R 4
SP26C	DEFB 17,0,17,0+128						;TILT UP L
 SP26CC	DEFB 17,1,17,1+128						;TILT UP R
SP26D	DEFB 18,0,18,0,18,0,18,0,18,0,18,0+128;WAIT UP L
 SP26DD	DEFB 18,1,18,1,18,1,18,1,18,1,18,1+128;WAIT UP R
SP26E	DEFB 14,0,14,0,14,0,14,0+128					;WAIT DOWN L
SP26EE	DEFB 14,1,14,1,14,1,14,1+128					;WAIT DOWN R
 ;
;MISSILES
SP27A	DEFB 42,4,42,4,42,4,42,4+128																	;UP
 SP27B	DEFB 42,10,42,10,42,10,42,10,42,10+128	 					;DOWN
 SP27C	DEFB 43,20,44,16,44,16,43,26+128							 					;TURN L 1
 SP27CC	DEFB 43,20,43,20,44,16,44,16,43,26,43,26+128	;TURN L 2
SP27D	DEFB 43,37,44,33,44,33,43,43+128													;TURN R 1
 SP27DD	DEFB 43,37,43,37,44,33,44,33,43,43,43,43+128	;TURN R 2
;
;ALIEN SHIPS
 SP38	DEFB 25,4	;UP
 SP37	DEFB 25,4
 SP36	DEFB 25,4
 SP35	DEFB 25,4
 SP34	DEFB 25,4
 SP33	DEFB 25,4
 SP32	DEFB 25,4
 SP31	DEFB 25,4+128
 ;
SP48	DEFB 25,8	;DOWN
 SP47	DEFB 25,8
 SP46	DEFB 25,8
 SP45	DEFB 25,8
 SP44	DEFB 25,8
 SP43	DEFB 25,8
 SP42	DEFB 25,8
 SP41	DEFB 25,8+128
 ;
SP58	DEFB 25,16	;LEFT
SP57	DEFB 25,16
SP56	DEFB 25,16
SP55	DEFB 25,16
SP54	DEFB 25,16
SP53	DEFB 25,16
SP52	DEFB 25,16
SP51	DEFB 25,16+128
;
SP68	DEFB 25,32	;RIGHT
 SP67	DEFB 25,32
SP66	DEFB 25,32
SP65	DEFB 25,32
SP64	DEFB 25,32
SP63	DEFB 25,32
SP62	DEFB 25,32
SP61	DEFB 25,32+128
;
SP78	DEFB 25,20	;UP/LEFT
 SP77	DEFB 25,20
SP76	DEFB 25,20
SP75	DEFB 25,20
SP74	DEFB 25,20
SP73	DEFB 25,20
SP72	DEFB 25,20
SP71	DEFB 25,20+128
;
SP88	DEFB 25,36	;UP/RIGHT
SP87	DEFB 25,36
SP86	DEFB 25,36
SP85	DEFB 25,36
SP84	DEFB 25,36
SP83	DEFB 25,36
SP82	DEFB 25,36
SP81	DEFB 25,36+128
;
SP98	DEFB 25,24	;DOWN/LEFT
 SP97	DEFB 25,24
SP96	DEFB 25,24
SP95	DEFB 25,24
SP94	DEFB 25,24
SP93	DEFB 25,24
SP92	DEFB 25,24
SP91	DEFB 25,24+128
;
SP108	DEFB 25,40	;DOWN/RIGHT
 SP107	DEFB 25,40
 SP106	DEFB 25,40
 SP105	DEFB 25,40
 SP104	DEFB 25,40
 SP103	DEFB 25,40
 SP102	DEFB 25,40
 SP101	DEFB 25,40+128
 ;
SP118	DEFB 25,0	;STAND STILL
 SP117	DEFB 25,0
SP116	DEFB 25,0
SP115	DEFB 25,0
SP114	DEFB 25,0
SP113	DEFB 25,0
SP112	DEFB 25,0
SP111	DEFB 25,0+128
;****************************************
.SPEV1	
	DEFW FX0,FX1,FX2,FX3,FX4,FX5,FX6,FX7,FX8
	DEFW FX9,FX10,FX11,FX12,FX13,FX14,FX15
.SPEV2	
	DEFW FXA0,FXA1,FXA2,FXA3,FXA4,FXA5
 	DEFW FXA6,FXA7,FXA8,FXA9,FXA10
	DEFW FXA11,FXA12,FXA13,FXA14,FXA15
;****************************************
 ;NO ACTION
 FX0	JP C,TV5
 FX0A	JP TV1
;
;HALF SPEED
FX1	JP C,TV5
FX1A	
	LD A,(FRAME)
	AND 1
 	JP NZ,TV1
 	JP TV2
;	
 ;PODDER EXPLOSION BYPASS
FX2	JR NC,FX1A
 	JP TV5
;
;FISH EGGS
FX3	;LD HL,20*256+1
 	;LD A,20
 	;SUB B
 	;LD H,A
FX3A
	;LD A,250
	;LD (SPRITE),A
 	;DEC C
 	;LD A,68
 	;CALL COLOUR
 	;INC C
 	;LD A,32
 	;CALL TILEDIP1
 	;CALL BUFFOFF
	xor A
	ld (IY+0),A
	ld (IY+8),A
	jp TV4
;
;FISH EGGS *3
FX4	
	ld L,(IY+1)
	ld H,(IY+2)
	ld (egg_current_pattern),HL

	;LD HL,3*256+1
	JR FX3A
 ;FISH EGGS *1
	
;EGGS	
FX6	
	jp C,TV5
	ld HL,#0101
	jr FX8_done
	
;UP/DOWN EGG LAYER
FX7	
	jp C,TV5
	ld HL,#0001
	jr FX8_done
	
;DOWN/UP EGG LAYER
FX8	
	jp C,TV5
	ld HL,#0001
	
.FX8_done
	ld A,(FRAME)
	and 7
	cp 1
	jr nz,FX8_done_nomove
	dec C
	
.FX8_done_nomove
	call array_put
 	JP TV1

;*******************
;*** GUN TURRETS ***
;*******************

.FX9
	jp C,TV5
	
;default sprite = bullet
	ex AF,AF'
	ld A,1
	ex AF,AF'

	push BC
	push DE
	
	;ld B,92
	ld (IY+0),62+64	 ;force full life
	
	ld A,(IY+5)
	and %11110000
	ld B,92			;#30
	cp #30
	jr z,FX9_SQ57_ok
	cp #40			
	jr z,FX9_SQ57_40
	ld B,98			;#50
	jr FX9_SQ57_ok
.FX9_SQ57_40	
	ld B,95	 		;#40
.FX9_SQ57_ok	

	ld E,(IY+4)	;Y turret
	ld A,(SHIPY)
	
	sub E	;r = shipx - y turret
	jr C,FX9_Yneg	;shipx<yturret
	
	cp 4
	jr nc,FX9_Ypos
	
	ld C,0
.FX9_Ydone

	ex AF,AF'
	ld A,B
	add C
	ex AF,AF'
	
	pop DE
	pop BC
	jp FX1A
.FX9_Ypos
	ld C,2
	jr FX9_Ydone
.FX9_Yneg
	ld C,1
	jr FX9_Ydone

;***********************	
;*** BIG ALIEN SHIPS ***
;***********************

.FX10	
	PUSH DE
 	LD A,(IY+5)
 	AND 240
 	CP #30 ;48
 	JR Z,FX10A
	CP #40 ;64
 	JR Z,FX10B
	
	LD HL,FXBYTE3
 	LD A,27
 	JR FX100
;
.FX10B	
	LD A,(FXBYTE4)
 	AND A
 	JR Z,FX10E
	DEC A
 	LD (FXBYTE4),A
	JR FX10D
;
.FX10E	
	LD A,(SHIPY)
 	LD L,A
	DEC L
 	LD A,B
	CP L
 	JR NC,FX10D
	ADD 5
 	CP L
	JR C,FX10D
	LD A,(SHIPX)
	LD L,A
	LD A,C
	ADD 10
	CP L
	JR NC,FX10D
 	LD HL,FX10Z+1
 	LD (HL),A
 	INC HL
	LD A,(SHIPY)
	LD (HL),A
 	DEC HL
	DEC HL
	CALL FORCESYNC
	LD A,6
	LD (FXBYTE4),A
	ld A,sfx_boss4shot
	call SND_SfxPlay
;
FX10D	
	LD A,26
	LD HL,FXBYTE2
 	JR FX100
FX10A	
	LD A,25
	LD HL,FXBYTE1
FX100	
	EX AF,AF'
	LD A,(HL)
 	AND A
 	JR Z,FX101
	DEC A
 	LD (HL),A
 	LD A,71
 	;LD (CADD+1),A
FX101	
	POP DE
 	JP FX1A
FX10Z	DEFB 30,0,0

;*********************
;*** ALIEN GLOWERS ***
;*********************

.FX11	
	PUSH BC:PUSH DE
	EX AF,AF'
 	LD A,(IY+5)
 	AND 240
 	CP 48
 	JR Z,FX11A
	CP 64
 	JR Z,FX11B

;******************************
;*** #50 SHIP 3 - 2 turrets ***
;******************************

 	;LD A,62
	ld IX,FX11_table3
	call FX11_ComputeSprite
	
	LD HL,SEQBUFF+45 ;(9*9)	turret hi
 	LD DE,SEQBUFF+0  ;(9*0)	;ship
 	LD BC,FXBYTE3

 	JR FX111
	
;******************************
;*** compute current sprite ***
;******************************	
.FX11_ComputeSprite
	ld A,(FRAME)
	AND 1
	jr nz,FX11_NotDamaged_Done

	ex AF,AF'
	jr z,FX11_NotDamaged
	
	ex AF,AF'
	
	ld A,sfx_touchboss
	call SND_SfxPlay
	
	ld A,(IX+4)
	ret
	
.FX11_NotDamaged
	ex AF,AF'
.FX11_NotDamaged_Done
	ld A,(FRAME)
	and 7
	srl A
	ld (FX1_offset),A
.FX1_offset	EQU $+2
	ld A,(IX+0)
	ret
	
	
.FX11_table1
	db 64,58,64,67,59
.FX11_table2
	db 65,60,65,68,61
.FX11_table3
	db 66,62,66,69,63
	
;******************************
;*** #40 SHIP 1 - no turret ***
;******************************
	
.FX11B	
	;LD A,60
	ld IX,FX11_table2
	call FX11_ComputeSprite
	
	LD HL,SEQBUFF+9 ;(9*6)
 	LD DE,SEQBUFF+9  ;(9*1)
 	LD BC,FXBYTE2
 	JR FX111
	
;*************************
;*** SHIP 1 - 1 turret ***
;*************************
; S = 3 - T =8 - W = 4
.FX11A	
	LD A,58
	ld IX,FX11_table1
	call FX11_ComputeSprite
	
	
	LD HL,SEQBUFF+18 ;(9*3)
 	LD DE,SEQBUFF+63 ;(9*8)
 	LD BC,FXBYTE1
	
;******************
;*** DO DESTROY ***
;******************

.FX111	
	EX AF,AF'
	JP NC,FX11C

	LD A,(IY+5)
	and 240
	cp #50
	jr nz,FX111_NotShip3
	
;exception for SHIP 3
	xor A
	ld (SEQBUFF+36),A	;turret low
	ld (SEQBUFF+36+8),A	;not touched !
	
.FX111_NotShip3	
	
 	PUSH DE
 	LD DE,(CIRCHOLD2)
 	XOR A
 	;LD (DE),A
 	POP DE
	PUSH HL
 	POP IX
	;LD (CADD+1),A
 	LD (IX+0),A
	ld (IX+8),A
 	;LD (IX+9),A
 	;LD (IX+18),A
	PUSH DE
 	POP IX
	LD (IX+0),A
	ld (IX+8),A
 	LD C,(IX+3)
 	LD B,(IX+4)
 	INC C
 	PUSH BC
 	INC C
 	INC C
 	INC B
 	INC B
 	LD (CIRCHOLD1),BC
 	CALL ADDEXPLO
 	POP BC
	PUSH BC
 	LD (CIRCHOLD1),BC
 	CALL ADDEXPLO
 	POP BC
	LD A,C
	ADD 4
 	LD C,A
	PUSH BC
 	LD (CIRCHOLD1),BC
 	CALL ADDEXPLO
 	POP BC
	LD A,B
	ADD 4
 	LD B,A
	PUSH BC
 	LD (CIRCHOLD1),BC
 	CALL ADDEXPLO
 	POP BC
	LD A,C
	SUB 4
 	LD C,A
	LD (CIRCHOLD1),BC
 	CALL ADDEXPLO
 	;LD DE,5*256+4
 	;CALL ATS
	ld A,12-1
	call SCORE_Add
	POP DE:POP BC
	
;***********************
;*** ARE ALL ALIVE ? ***
;***********************

	LD A,(SEQBUFF)
	LD L,A
	LD A,(SEQBUFF+9)
	LD H,A
	LD A,(SEQBUFF+18)
 	ADD H
 	ADD L
 	JP NZ,TV4
 	;LD A,101-lvl_offset
	;LD (TILENO),A
	call SOFT_END_Start
 	JP TV4
	
;***********
;*** END ***
;***********

FX11C	
	;JR Z,FX11D
 	;LD A,1
	;LD (BC),A
FX11D	
	;LD A,(FRAME)
 	;RRCA
	;RRCA
	;AND 64
	;ADD 4
 	;LD (CADD+1),A
 	POP DE:POP BC
 	JP FX1A
	
;controller for boss events
.FX5	;LD HL,1*256+1
;is counter loaded ?
;break
	ld A,(FX5_counter)
	or A
	jr z,FX5_notLoaded
	
	cp 7
	jr nz,FX5_NotPlaySound
	push AF
	ld A,sfx_boss4crush
	call SND_SfxPlay
	pop AF
.FX5_NotPlaySound
	dec A
	ld (FX5_counter),A
	or A
	jr z,FX5_Finish
	
	ld A,(FRAME)
	and 1
	ld BC,#BC05
	out (C),C
	inc B
	out (C),A
	
	jp TV5
.FX5_Finish
	xor A
	ld (SCR_SPEC1),A	;be sure there is no pending event
	ld BC,#BC05
	out (C),C
	inc B
	out (C),A
	jp TV5

;check for event , bit 0,1,2 is a ship
.FX5_notLoaded
	ld A,(SCR_SPEC1)
	or A
	jp z,TV5	;no event to dispatch
	

	;break
	
	ld C,0
	LD A,(SEQBUFF)
	or A
	jr z,FX5_Boss1Dead
	ld C,1
.FX5_Boss1Dead
	ld A,(SEQBUFF+9)
	or A
	jr z,FX5_Boss2Dead
	ld A,2
	or C
	ld C,A
.FX5_Boss2Dead
	ld A,(SEQBUFF+18)
	or A
	jr z,FX5_Boss3Dead
	ld A,4
	or C
	ld C,A
.FX5_Boss3Dead

	ld A,C
	push AF
	ld A,(SCR_SPEC1)
	ld C,A
	pop AF
	and C		;mask before
	cp C
	jp nz,TV5	;must match with event
	
	ld A,7	;start event
	ld (FX5_counter),A
	
	xor A
	ld (SCR_SPEC1),A	;acquit event
	jp TV5
	
	

.FX5_counter
	db 0	
 ;
;***********************
;*** NODDING SNALIEN ***
;***********************

read "FX12_nodding_alien.asm"

;EGG EATERS
FX13
	jp c,FX13X  ;if dead , remove !
	LD A,B
	CP 20
 	JP NC,FX13X
 	LD A,C
	CP 31
 	JP NC,FX13X
 	LD A,(SHIPX)
	INC A
 	LD L,A
	LD A,(SHIPY)
	LD H,A
;
	LD A,(DE)
 	AND 60
	CP 4
	JR Z,FX13B
	CP 8
	JR Z,FX13B
;
FX13A	LD A,C
 	CP L
	JR Z,FX13A1
 	INC A
 	CP L
	JR Z,FX13A1
 	INC A
 	CP L
	JR NZ,FX13YY
FX13A1	LD A,B
	CP H
	LD HL,SQ53
	JR NC,FX13Y
 	LD HL,SQ52
	JR FX13Y
;
FX13B	LD A,B
 	CP H
	JR NZ,FX13YY
FX13B1	LD A,C
	CP L
	LD HL,SQ54
	JR NC,FX13Y
 	LD HL,SQ51
;
FX13Y	
	LD (IY+6),L
	LD (IY+7),H
;****************************************
FX13YY	
	LD (IY+3),C
 	LD (IY+4),B
	
	SetCallBack FX13_CallBack
	
	LD A,(DE) 

	PUSH DE
	PUSH AF
 	EX AF,AF'
 	CALL TSPGADD
	POP AF
 	AND 3
	JR NZ,L4_THEM6
 	CALL SPNS_CallBack
 	JR L4_THEM9
L4_THEM6	
	CP 1
 	JR NZ,L4_THEM7
 	CALL SPMS_CallBack
 	JR L4_THEM9
L4_THEM7	
	CP 2
 	JR NZ,L4_THEM8
 	CALL SPFS_CallBack
 	JR L4_THEM9
L4_THEM8	
	CALL SPBS_CallBack
L4_THEM9	
	POP DE
	
	jp TV5
	
;**************************
;*** COLLISION CALLBACK ***
;**************************
	
.FX13_CallBack
	bit 6,(HL)
	jr z,FX13_Damage
	
	xor A
	ret
	
.FX13_Damage
	push AF
	push HL
	push BC

	inc H	;bank
	inc H	;lo
	inc H	;hi
	inc H	;master !
	
	ld A,(HL)
	or A		;is 
	jr z,FX13_DamageDone
	cp 32
	jr nc,FX13_DamageDone

	
	ld B,10
	call THEM_MakeDamage

.FX13_DamageDone
	pop BC
	pop HL
	pop AF
	ret
	
 	;LD HL,TSPGADD
 	;LD (USPALL+1),HL
	;LD A,1
	;LD (WEAPON),A
 	;PUSH DE
 	;LD A,(DE)
 	;AND 3
 	;CP 1
	;JR Z,FXO1
 	;CP 2
	;JR Z,FXO2
 	;CP 3
	;JR Z,FXO3
 	;EX AF,AF'
 	;CALL USPN
 	;JR FXO9
;FXO1	
	;EX AF,AF'
 	;CALL USPM
 	;JR FXO9
;FXO2	
	;EX AF,AF'
 	;CALL USPF
 	;JR FXO9
;FXO3	
	;EX AF,AF'
 	;CALL USPB
;FXO9	
	;CALL KILL
 	;POP DE
	;LD HL,USPGADD
 	;LD (USPALL+1),HL
	;XOR A
 	;LD (WEAPON),A
 	;JP TV3
;****************************************
FX13X	
	LD (IY+0),0
	JP TV4

;***********************
;*** MISSILE WALKERS ***
;***********************
read "FX14_missile_walker.asm"	

	

 ;MISSILE IMPACT
FX15
	jp c,TV5
	PUSH DE
 	;CALL CONVERT
	call COLL_CheckPosition
	POP DE
	JP nc,TV1
	LD (IY+0),0
 	LD (CIRCHOLD1),BC
 	CALL ADDEXPLO
 	JP TV4
;****************************************
 ;NO ACTION
FXA0	JP TV3
;
;HALF SPEED
FXA1	
	LD A,(FRAME)
	AND 1
 	JP NZ,TV3
 	JP TV4
;
;FISH EGGS CLEAR UP
FXA2	;CALL BUFFON
 	;LD A,31
 	;CALL TILEDIP1
 	JP TV3
FXA3	
		JR FXA1
 ;
FXA4	
		JR FXA3
 ;
FXA5	JR FXA3
 ;
FXA6
 ;
;PODDER TOP PILL
 FXA7	LD L,5
	JR FXA15A
 ;
;PODDER BOTTOM PILL
FXA8	LD L,5
	JR FXA15A
 ;
;PODDER BULLETS (1)
;FXA9	LD L,1
 ;	JR FXA15A
;
;PODDER MINI LASER (2)
 ;FXA10	LD L,2
;	JR FXA15A
;
;PODDER LASER (3)
FXA10
FXA11	LD L,1
 	JR FXA15A
 ;
;PODDER BLOB (3)
 FXA9
 FXA12	LD L,3
 	JR FXA15A
 ;
;PODDER CIRCLE (3)
 FXA13	LD L,2
 	JR FXA15A
 ;
;PODDER BONUS
FXA14	LD L,6
 	JR FXA15A
 ;
;PODDER MISSILE
FXA15	LD L,4
 FXA15A	CALL FXA99
	;LD A,(JEW1)
 	;AND A
 	;JR NZ,FXA15B
;
	;LD (JEW1X),BC
 	;LD A,L
	;LD (JEW1),A
 	;LD A,(FRAME)
	;DEC A
 	;AND 7
 	;LD (JEW1P),A
	;JP FXA1
 ;
;FXA15B	LD (JEW2X),BC
 	;LD A,L
	;LD (JEW2),A
 	;LD A,(FRAME)
	;DEC A
 	;AND 7
 	;LD (JEW2P),A
	call JEW_Spawn
	JP FXA1
;****************************************
 FXA99	LD A,(IY+0)
	AND A
 	JR NZ,FXA99A
	RET
 FXA99A	POP HL
	JP FXA1
;****************************************

;*********************************
;*** put a single egg in array ***
;*********************************
; BC = yx
.array_put
	push BC
	add HL,BC	;add to HL offset and transfert to BC	
	ld C,L		
	ld B,H
;in screen ?
	ld A,C
	cp 32
	jr nc,array_put_done
	
	ld A,B
	cp 20
	jr nc,array_put_done

;get address on array attribute
	ld A,B
	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 C ;do not forget to add X to array address
	ld L,A
	
	bit 7,(HL)	;is solid ?
	jr z,array_put_ignore_master	;if not , ignore master !

	ld C,H	;save H
	;get master
	inc H		;bank
	inc H		;low addr
	inc H		;hi addr
	inc H		;master
	ld A,(HL)
	or A
	jr z,array_put_done	;if NULL it is level so do not mark
	ld H,C
.array_put_ignore_master
	ld (HL),64+128+3	;mark to be sure it will be not erased
	
.array_put_done
	pop BC
	ret

;******************
;*** init array ***
;******************

.init_array
	;call init_array_empty
	call init_array_sprites
	ret
	
;init sprites
.init_array_sprites

	;save sprite found routine
	ld HL,(BULLET_InitSprites_BankSelect)
	push HL
	ld HL,TSPGADD
	ld (BULLET_InitSprites_BankSelect),HL

	;8 first sprites
	ld A,80
	ld B,8
	ld IY,array_spr
	ld IX,#F600
	ld DE,2
	
	call BULLET_InitSprites_loop
	
	ld B,4
	ld IX,#FE00
	ld DE,2
	call BULLET_InitSprites_loop
	
	pop HL
	ld (BULLET_InitSprites_BankSelect),HL
	
	ret
	
.array_spr	ds 24,0	;12 sprites

;fill array w/ zero
.init_array_empty
	;ld HL,egg_array
	;ld BC,660
;.init_array_loop
	;ld (HL),0	;remove for debug stuff
	;inc HL
	;dec BC
	;ld A,C
	;or B
	;jr nz,init_array_loop
	;ret
	ret
	
;********************
;*** update array ***
;********************
.run_array

	;update starfield
	ld A,(STARS_Counter)
	inc A
	ld (STARS_Counter),A
	
	call STARS_Run

	ld A,(FRAME)
	and 7
	call z,run_array_scroll

	ld A,(FRAME)
	and 1
	ret nz
	
	ld A,(FRAME)
	and 6

	ld C,A
	ld B,0
	
	ld IX,array_spr
	add IX,BC
	
	ld A,(IX+0)
	ld (run_array_case01_lo),A
	ld A,(IX+1)
	ld (run_array_case01_hi),A
	
	ld A,(IX+8)
	ld (run_array_case10_lo),A
	ld A,(IX+9)
	ld (run_array_case10_hi),A
	
	ld A,(IX+16)
	ld (run_array_case11_lo),A
	ld A,(IX+17)
	ld (run_array_case11_hi),A

	call run_array_buffer_render
	
	ret
;********************
;*** scroll array ***
;********************
.run_array_scroll
	ld BC,#1F14
	ld IX,ATTR_Addr
	ld IY,(egg_current_col)
	
.run_array_scroll_loopY
	push BC
	ld L,(IX+0)
	ld E,L
	inc E
	ld H,(IX+1)
	ld D,H
	
	inc IXL
	inc IXL
	
	bit 6,(HL)
	jr z,run_array_scroll_loopX
	
	ld (HL),4	
	
.run_array_scroll_loopX
	ld A,(DE)
	bit 6,A
	
	jr z,run_array_scroll_Empty
	ld (HL),A
	ld A,4
	ld (DE),A

.run_array_scroll_Empty
	inc HL
	inc DE
	djnz run_array_scroll_loopX
	
	bit 6,(IY)
	jr z,run_array_nonext_col
	ld (HL),64
	jr run_array_next_coldone
.run_array_nonext_col
	bit 6,(HL)
	jr z,run_array_next_coldone
	ld (HL),4
.run_array_next_coldone
	inc IY
	pop BC
	dec C
	jr nz,run_array_scroll_loopY
	
	call run_array_nextpattern
	
	ret
	
;*******************************************************
;*** SCAN ATTRIBUTES ARRAY TO CHECK FOR DESTRUCTIBLE ***
;******************************************************* 	
;update array into rendering buffer

.run_array_buffer_render
	ld IX,ATTR_Addr
	ld BC,#2014		;32*20
	ld DE,(egg_current_col)
	
.run_array_buffer_render_loopY
	push BC	;save BC first !
	;get work address in HL
	ld C,(IX+0)
	ld H,(IX+1)
	
	ld A,32		;get byte at end of the line + 1
	add C	
	ld L,A
	ld A,(HL)
	push AF		;save it !
	ld A,(DE)
	ld (HL),A	;and replace with scroll next column
	
	ld L,C
	
	inc IXL
	inc IXL

.run_array_buffer_render_loopX	

	bit 6,(HL)		;is a destructible thingy ?
	jr nz,run_array_buffer_case1X
	
	inc L
	bit 6,(HL)
	jr nz,run_array_buffer_case01
	
;***************
;*** case 00 ***
;***************

	dec L			;return to first cell
	
	;get master to see if 32
	ld C,H			;save H in C
	inc H	;bank
	inc H 	;low addr
	inc H	;high addr
	inc H	;master
	
	ld A,32
	cp (HL)
	jr nz,run_array_buffer_render_skip
	
	;if yes, erase 
	ld (HL),64	;neutral master !
	ld H,C
	ld (HL),4	;auto erase feature
	
	;we 'll loose 1 nop but this will be faster than a jr
	
.run_array_buffer_render_skip
	ld H,C

.run_array_buffer_render_done
	inc L
	djnz run_array_buffer_render_loopX	
	
	pop AF
	ld (HL),A	;restore data as we are at HL+33
	inc DE		;next element in column
	
	pop BC
	dec C
	jr nz,run_array_buffer_render_loopY
	
	ret

.run_array_buffer_case1X
	;ld (HL),128+3
	;jr run_array_buffer_render_done
	
	inc L
	bit 6,(HL)	
	jr nz,run_array_buffer_case11
	
	
;***************
;*** case 10 ***
;***************

.run_array_buffer_case10
	dec L	;return to previous cell
	ld A,H
	ld (HL),55+128+64	;solid block destructible
	inc H
	;ld (HL),#C6			;bank
	inc H
run_array_case10_lo		EQU $+1
	ld (HL),0
	inc H
run_array_case10_hi		EQU $+1
	ld (HL),0
	inc H
	ld (HL),32
	ld H,A
	jr run_array_buffer_render_done
	
;***************
;*** case 11 ***
;***************

.run_array_buffer_case11
	dec L ;previous cell	
	ld A,H
	ld (HL),55+128+64	;solid block destructible
	inc H
	;ld (HL),#C6			;bank
	inc H
run_array_case11_lo		EQU $+1
	ld (HL),0
	inc H
run_array_case11_hi		EQU $+1
	ld (HL),0
	inc H
	ld (HL),32
	ld H,A
	jr run_array_buffer_render_done
	
;***************
;*** case 01 ***
;***************

.run_array_buffer_case01
	dec L ;return to previous cell
	ld A,H
	ld (HL),55	;solid block not solid not destructible
	inc H
	;ld (HL),#C6			;bank
	inc H
run_array_case01_lo		EQU $+1
	ld (HL),0
	inc H
run_array_case01_hi		EQU $+1
	ld (HL),0
	inc H
	ld (HL),32
	ld H,A
	jr run_array_buffer_render_done
	
;****************************
;*** COMPUTE NEXT PATTERN ***
;****************************
.run_array_nextpattern

	ld HL,(egg_current_pattern)
	ld E,(HL)
	inc HL
	ld D,(HL)
	inc HL
	
	;is next pattern null ?
	ld A,E
	or D
	ret z	;if yes , do not change pattern
	
	ld (egg_current_pattern),HL
	ld (egg_current_col),DE
	ret
	
;*******************
;*** END PATTERN ***
;*******************
;cleanup screen
.end_array

	;stop stars
	xor A
	ld (STARS_ForceNoMove),A
	
	;force to starfield !
	ld HL,STARS_Run
	ld (BG_Effect_Addr),HL

	ld IX,ATTR_Addr
	ld BC,#2014		;32*20
	ld DE,(array_spr)
	
.end_array_loopY
	ld L,(IX+0)
	ld H,(IX+1)
	inc IXL
	inc IXL
	push BC
.end_array_loopX
	bit 6,(HL)
	jr nz,end_array_setup
	ld A,H
	ld (HL),4
	inc H
	inc H
	inc H
	inc H
	ld (HL),64	;setup a neutral master !
	ld H,A
.end_array_done
	inc L
	djnz end_array_loopX
	pop BC
	dec C
	jr nz,end_array_loopY
	
	ret
.end_array_setup
	ld A,H
	ld (HL),55+128+64	;solid block destructible
	inc H
	;ld (HL),#C6			;bank
	inc H
	ld (HL),E
	inc H
	ld (HL),D
	inc H
	ld (HL),32			;destructible 
	ld H,A
	jr end_array_done
	
.egg_current_col
	dw egg_pat00

.egg_current_pattern
	dw egg_pattern0

.egg_pattern0
	dw 0
	
.egg_pattern1
	dw egg_pat01
	dw egg_pat01
	dw egg_pat00
	dw 0
	
.egg_pattern2
	dw egg_pat03
	dw egg_pat03
	dw egg_pat03
	dw egg_pat03
	
	dw egg_pat03
	dw egg_pat03
	dw egg_pat03
	dw egg_pat03
	
	dw egg_pat03
	dw egg_pat03
	dw egg_pat03
	dw egg_pat03
	
	dw egg_pat03
	dw egg_pat03

;bonus 1	
	dw egg_pat04_bis
	dw egg_pat04_bis
	dw egg_pat04_bis
	dw egg_pat04_bis
	dw egg_pat04_bis
	dw egg_pat04
	dw egg_pat04
	dw egg_pat04
	dw egg_pat04

;bonus *2	
	dw egg_pat04
	dw egg_pat04
	dw egg_pat04
	
	dw egg_pat04
	dw egg_pat04
	dw egg_pat04
	dw egg_pat04
	dw egg_pat04
	dw egg_pat04
	dw egg_pat04
	dw egg_pat04
	
	dw egg_pat03_bis
	dw egg_pat03_bis
	dw egg_pat03_bis
	dw egg_pat03_bis
	dw egg_pat03_bis
	dw egg_pat03_bis
	dw egg_pat03_bis
	dw egg_pat03_bis
	dw egg_pat03_bis
	dw egg_pat03_bis
	dw egg_pat03_bis
	dw egg_pat03_bis
	
	dw egg_pat00
	dw 0

.egg_pat00
	ds 20,0
.egg_pat01
	ds 4,0
	ds 2,64
	ds 8,64
	ds 6,0
.egg_pat03
	ds 3,0
	ds 14,64
	ds 3,0
.egg_pat03_bis
	ds 4,0
	ds 2,64
	ds 11,64
	ds 3,0	

;.egg_pat04
;	ds 4,0
;	ds 2,64
;	ds 4,0
;	ds 2,64
;	ds 3,0
;	db 0,64
;	ds 3,0

.egg_pat04	
.egg_pat04_bis
	ds 4,0
	ds 2,64
	ds 4,64
	ds 2,64
	ds 3,0
	db 0,64
	ds 3,0
	
;starfield
read "l4_starfield.asm"

;master data
	read "l4_master_data.asm"
	