;***********************************
;*** LEVEL B - CYBERNOID TRIBUTE ***
;***********************************

ORG level_start

IF output_files
write "bin/levelB.lvl"
ENDIF

START	DEFB 0
SYNCHRO	DEFW 0000

START1	DEFB 0; test_pos ;0
START2	DEFB 0
START3	DEFB 0

SYNCHRO1	DEFW SYNC1 ;test_sync ;SYNC1
SYNCHRO2	DEFW 0
SYNCHRO3	DEFW 0

TSPRADD		DEFW 0
TADD		DEFW 0

SEQTABLE	DEFW SQTABLE
SPEVENT1	DEFW SPEV1
SPEVENT2	DEFW SPEV2
;DEFB 0,0,0

;palette stuff 16 colors+border
.lvl_pal
	db 0,26,11,6,3,20,10,2,1,25,16,18,9,15,13,22
	db 0
	db 0,20,2,3,0,10,1,1,0,12,3,9,0,3,1,9
	db 0
	db 0,11,1,3,0,1,0,1,0,3,0,9,0,3,0,9
	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 #FF	;no music 
.lvl_skip
	dw 0	;music to skip on music restart
.lvl_init
	dw	lB_init	;called after loading
.lvl_reinit
	dw	lB_reinit	;called each time player dies
	
.lB_init
	;force to return directly to casual menu
	ld A,1
	ld (menu_force),A
	ret
.lB_reinit
	xor A
	ld (fading_count),A
	ld (fading_start),A

	;setup music
	ld A,#FE
	ld HL,levelb_music
	call MUSIC_Play 

	;be sure to have full volume
	xor A
	ld (LVL_NextMusik),A
	call MUSIC_SetVolume
	
	;default music
	ld HL,levelb_music
	ld (music_alt),HL


	ret
;enemies	
.SQTABLE
	dw en01_green_alien
	dw en02_green_alien_rev
	dw en03_green_obstacles
	dw en04_blue_up
	dw en05_blue_down
	dw en06_green_obstacle_bonus1
	dw en07_green_alien_bonus1
	dw en08_red_alien
	dw en09_dummy_shooter
	dw en10_red_up
	dw en11_green_obstacle_bonus2
	dw en12_red_alien_bonus
	dw en13_boss
	dw en14_boss_shoot
	dw en15_boss_shoot_trail
	dw en16_jimpow
	dw en17_green_obstacle_bonus3
	dw en18_green_obstacle_bonus4
	dw en19_endlevel
;action 1
.SPEV1
	dw action00_nop,action01_bonus,action02_green_obstacle,action03_boss,action04_boss_shoot,action5_fading
;action 2
.SPEV2
	dw actionx00_nop,actionx01_nop,actionx02_nop,actionx03_nop,actionx04_nop,actionx05_nop,actionx06_nop
	
;	LEVEL	A - 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

	;db 0,1,99,4,4
	
	;db 0,1,9,32,10
	;db 0,1,8,32,10
	;db 0,10,8,32,7
	;db 0,19,8,32,14

	;db 1,1,6,32,10
	
	;db 1,0,4,24,20
	;db 1,4,4,24,20
	;db 1,8,4,23,20
	;db 1,16,4,23,20
	
	;db 1,0,2,-2,8
	;db 1,0,2,-2,12
	
	;db 1,20,7,32,10
	;db 1,16,1,32,10
	;db 1,24,1,32,10

;first green barrier
	
	db 0,1,3,32,4
	db 0,1,3,32,6
	db 0,1,3,32,8
	db 0,1,3,32,10
	db 0,1,3,32,12
	db 0,1,3,32,14
	
	db 2,15,3,32,4
	db 2,15,3,32,6
	db 2,15,3,32,8
	db 2,15,3,32,10
	db 2,15,3,32,12
	db 2,15,6,32,14	;hidden bonus


	
;green barrier up and down

	db 8,1,3,32,5
	db 8,1,3,32,13
	
	db 10,15,3,32,5
	db 10,15,3,32,13
	
;4*blues up
	db 12,0,4,11,20
;barrier 3
	db 12,1,3,32,5
	db 12,1,3,32,13
;blues up second part
	db 12,3,4,11,20
	db 12,6,4,11,20
	db 12,9,4,11,20
	
;barrier 4	
	db 14,15,3,32,5
	db 14,15,3,32,13

;dummy shooter
	db 16,0,9,32,18
	db 16,0,9,32,1

;blues * 4 down	
	db 16,0,5,11,-2
	db 16,3,5,11,-2
	
;dummy shooter 2
	db 16,4,9,32,2
	db 16,4,9,32,17	

	db 16,6,5,11,-2
	db 16,9,5,11,-2
;blues * 4 up
	db 17,0,4,7,20
	db 17,3,4,7,20
	db 17,6,10,7,20
	db 17,9,4,7,20	
	
	;green barrier
	db 18,1,3,32,10
	db 18,1,3,32,8

	;blues * 4 up
	db 19,0,4,16,19
	db 19,3,4,16,19
	db 19,6,4,16,19
	db 19,9,4,16,19	
	
	;green barrier
	db 19,17,3,32,10
	db 19,17,11,32,8
	
	;blues * 4 down	
	db 20,0,5,12,-1
	db 20,3,5,12,-1
	db 20,6,5,12,-1
	db 20,9,5,12,-1


	db 26,8,8,32,8
	db 26,19,12,32,14
	db 26,27,8,32,10

	;jim powa !	
	db 27,0,16,34,8
	
	;green aliens
	db 27,8,1,32,4
	db 27,16,1,32,14
	db 27,24,1,32,10
	db 28,0,1,32,8
	db 28,8,1,32,12
	
	db 28,8,2,-1,12	
	db 28,8,8,32,10
	db 28,8,2,-1,6
	db 28,15,8,32,8
	db 28,16,2,-1,14
	db 28,27,8,32,14
	
	;wave 2
	db 29,0,1,32,4
	db 29,0,2,-1,6
	db 29,8,1,32,14
	db 29,8,2,-1,14
	db 29,16,1,32,10
	db 29,24,1,32,8
	db 30,0,1,32,12
	
	db 30,7,8,32,8
	db 30,19,8,32,14
	db 30,29,8,32,10
	
	;wave 3
	db 31,0,1,32,4
	db 31,0,2,-1,6
	db 31,8,1,32,14
	db 31,8,2,-1,14
	db 31,16,1,32,10
	db 31,24,1,32,8
	db 32,0,1,32,12
	
	db 32,9,8,32,8
	db 32,15,8,32,14
	db 32,29,8,32,10
	
	;wave 4
	db 33,0,1,32,4
	db 33,0,2,-1,6
	db 33,8,1,32,14
	db 33,8,2,-1,14
	db 33,16,1,32,10
	db 33,24,1,32,8
	db 34,0,1,32,12
	
	db 34,8,8,32,8
	db 34,19,8,32,14
	db 34,27,8,32,10
	
	
	CHECK_POINT	35,0

	;green barrier
	db 35,1,3,32,2
	db 35,1,3,32,4
	db 35,1,17,32,6
	db 35,1,3,32,8
	db 35,1,3,32,10
	db 35,1,18,32,12
	db 35,1,3,32,14
	db 35,1,3,32,16

	test_pos	EQU 43
	test_sync	EQU $		
	
	SCROLL_Stop 44,1
	
	db 45,0,13,20,12

	
	
	db 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
;
;green alien , move a bit , wait a bit and attack !
.en01_green_alien
	db 0,1+64,#20+2
	dw en01_green_alien_spr_move_slow
	dw en01_green_alien_wait
	dw en01_green_alien_attack
	dw 0

.en01_green_alien_spr_move_slow
	db 1,16,2,0,3,16,4,0
	db 1,16,2,0,3,16,4,128
.en01_green_alien_wait
repeat 3:db 1,0,2,0:rend
	db 1,0,2,128
.en01_green_alien_attack
repeat 15:db 1,16,2,16:rend
		 db 1,16,2,16+128
		 
;green alien reversed
.en02_green_alien_rev
	db 0,1+64,#20+2
	dw en02_green_alien_rev_spr_move_slow
	dw en01_green_alien_wait
	dw en02_green_alien_rev_attack
	dw 0
	
.en02_green_alien_rev_spr_move_slow
	db 3,32,4,0,1,32,2,0
	db 3,32,4,0,1,32,2,128
.en02_green_alien_rev_attack
repeat 15:db 1,32,2,32:rend
		 db 1,32,2,32+128
		 
;solid obstacles
.en03_green_obstacles
	db 2,30,#40+1
repeat 5
	dw en03_green_obstacles_spr
rend
	dw 0
.en03_green_obstacles_spr
repeat 7:db 16,16,16,0,17,0,17,0,18,0,18,0,19,0,19,0:rend
		 db 16,16,16,0,17,0,17,0,18,0,18,0,19,0,19,0+128
		 
;blue enemy up
.en04_blue_up
	db 0,1,#10+2
	dw en04_blue_up_spr
	dw 0
.en04_blue_up_spr
repeat 2
	db 11,4,11,4,11,4,11,4
	db 11,4,11,4,11,4,11,4
rend
	db 11,4,11,4,11,4,11,4+128
	
;blue_enemy_down
.en05_blue_down
	db 0,1,#10+2
	dw en05_blue_down_spr
	dw 0
.en05_blue_down_spr
repeat 2
	db 11,8,11,8,11,8,11,8
	db 11,8,11,8,11,8,11,8
rend
	db 11,8,11,8,11,8,11,8+128
	
.en06_green_obstacle_bonus1
	db 1+#10,1,#40+1
	dw #0303
	
.en07_green_alien_bonus1
	db 1+#10,1,#20+2
	dw #0101
	
.en08_red_alien
	db 0,1+64,#20+2
repeat 7
	dw en08_red_alien_spr
rend
	dw 0
.en08_red_alien_spr
	;db 5,0,7,16				;left,left
	db 5,0,7+1,16+4			;left , left+up
	db 7,4,7+1,4				;up,up
	db 5,4,7+1,16				;left+up , left
	db 5,0,7+1,16				;left,left
	;db 5,0,7,16				;left,left
	db 5,0,7+1,16+8			;left,left+dn
	db 7,8,7+1,8				;dn,dn
	db 5,8,7+1,16				;left+dn,left
	db 5,0,7+1,16+128				;left,left
	
.en09_dummy_shooter
	db 0,62+64,0
repeat 8
	dw en09_dummy_shooter_spr
rend
	dw 0
.en09_dummy_shooter_spr
repeat 3
	db 20,0,20,0,20,0,20,0,20,0,20,0,20,0,20,16
rend
	db 20,0,20,0,20,0,20,0,20,0,20,0,20,0,20,16+128
	
;red enemy up (bonus)
.en10_red_up
	db 1+#10,1,#10+2
	dw en10_red_up_spr
	dw 0
.en10_red_up_spr
repeat 2
	db 12,4,12,4,12,4,12,4
	db 12,4,12,4,12,4,12,4
rend
	db 12,4,12,4,12,4,12,4+128
	
;green obstacle give missles	
.en11_green_obstacle_bonus2
	db 1+#40,1,#40+1
	dw #0303
	
.en12_red_alien_bonus
	db 1+#50,1,#20+2
	dw #0808
	
.en13_boss
	db 3,48,#40+10 ;(5*1000)
	dw en13_bossA
	dw en13_bossB
	dw en13_bossC
	dw en13_bossB
	dw en13_bossA
	dw #FFFF
	
.en13_bossA
	repeat 15:db 32,0:rend:db 32,128
.en13_bossB
	repeat 7:db 31,0:rend:db 31,128
.en13_bossC
	repeat 3:db 30,64:rend
	db 30,64
	repeat 3:db 30,64:rend	
	db 30,128
	
	
.en14_boss_shoot
	db 4,20,#20
repeat 10
	dw en_14_sp_wait
	dw en_14_sp_move	
rend
	dw 0
.en_14_sp_move
	repeat 2:db 40,64,41,64:rend:db 40,64,41,128
.en_14_sp_wait
	db 40,0,41,0,40,0,41,128

.en15_boss_shoot_trail	
	db 0,1,0
	dw en15_sp
	dw 0
.en15_sp
	db 40,0,41,0,42,0,43,0,44,128
	
.en16_jimpow
	db 0,16,#20
repeat 5	
	dw en16_spr
rend
	dw 0
.en16_spr
repeat 10 : db 60,16,60,0,61,0,61,0,62,0,62,0,63,0,63,0 :rend
	db 60,16,60,0,61,0,61,0,62,0,62,0,63,0,63,128
	
	;green obstacle give blue bonus
.en17_green_obstacle_bonus3
	db 1+#10,1,#40
	dw #0303
	
	;green obstacle give bit bonus
.en18_green_obstacle_bonus4
	db 1+#50,1,#40
	dw #0303	
	
.en19_endlevel
	db 5,1,0
	dw spr_endlevel
	dw #FFFF
.spr_endlevel
	db 1,128
	
	
;	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
;

	
.action00_nop
	jp c,TV5
	jp TV1
.action01_bonus	
	jp nc,TV1
	
	;get selector
	ld A,(IY+5)
	and %11110000
	rrca
	rrca
	rrca
	rrca
	
	ld L,A
	
	call JEW_Spawn
	
	jp TV5
	
.action02_green_obstacle
	jp c,TV5
	jp z,TV1
	ex AF,AF'
	add 10
	ex AF,AF'
	xor A
	call SND_SfxPlay
	SetMaster 0
	jp TV1
	
.action03_boss
	jp c,action03_endlevel
	
	ld A,45
	
	jr z,action03_boss_nottouched
	
	ex AF,AF'
	add 20
	ex AF,AF'
	
	SetNullMaster
	call SND_SfxPlay
	
	ld A,46
	
.action03_boss_nottouched	
	ld (action03_boss_sprite),A
	
	
	ld A,(DE)
	bit 6,A
	jr z,action03_nolook
	
	ld HL,(SHIPX)
	ld (action3_ship_pos),HL
	
	jr action03_look_done
	
.action03_nolook	
	SetNullMaster	;when not looking unvulnerable
.action03_look_done

	ld A,(FRAME)
	and 63
	jr nz,action03_noshoot
	
	push BC
	
	dec C
	inc B
	inc B
	inc B
	inc B
	ld (action3_spawn+1),BC
	ld HL,action3_spawn
	call FORCESYNC
	
	pop BC
	
	
.action03_noshoot	

	ld A,(FRAME)
	and 31
	jr nz,action03_notshoot2
	
	ld HL,action3_spawn2
	call FORCESYNC
	
.action03_notshoot2	

	SaveMaster

	SetNullMaster
	;ex AF,AF'
	;push AF
action03_boss_sprite	EQU $+1	
	ld A,45
	
	push AF
	push DE
	push BC
	call TSPGADD

	call REND_EraseZone
	
	pop BC
	pop DE
	pop AF
	
	push BC
	push DE
	call TSPGADD
	call SPNS
	pop DE
	pop BC
	
	;pop AF
	;ex AF,AF'
	
	RestoreMaster
	
	
	jp TV1
.action03_endlevel
	;explosion ! (what ? i am lazy and copied from boss L5 ?)
	PUSH BC
 	LD (CIRCHOLD1),BC
 	CALL ADDEXPLO
 	POP BC
	INC C
 	INC C
 	INC C
 	PUSH BC
 	LD (CIRCHOLD1),BC
 	CALL ADDEXPLO
 	POP BC
	INC B
 	INC B
 	INC B
 	PUSH BC
 	LD (CIRCHOLD1),BC
 	CALL ADDEXPLO
 	POP BC
	DEC C
 	DEC C
 	DEC C
 	LD (CIRCHOLD1),BC
 	CALL ADDEXPLO
	ld HL,action4_spawn3
	call FORCESYNC
	;immortal
	ld A,#C9
	ld (PLAYER_Die),A

	jp TV5


.action3_ship_pos
	dw 0
.action3_spawn
	db 14,0,0
.action3_spawn2
	db 1,32,10
.action4_spawn3
	db 19,0,0
	
.action04_boss_shoot
	ld A,(DE)
	bit 6,A
	jr z,action04_boss_shoot_wait

;spawn trail	
	push BC
	ld (action4_spawn+1),BC
	ld HL,action4_spawn
	call FORCESYNC	
	pop BC
;find player	
	ld HL,action3_ship_pos
	ld A,(HL)
	cp C
	jr z,action4_nochangeX
	jr nc,action4_right
	dec C
	jr action4_nochangeX
.action4_right
	inc C	
.action4_nochangeX
	inc HL
	ld A,(HL)
	cp B
	jr z,action4_nochangeY
	jr nc,action4_down
	dec B
	jr action4_nochangeY
.action4_down
	inc B
.action4_nochangeY	

	call COLL_CheckPosition
	jr nc,action4_nocollision
	or A
	jr nz,action4_nocollision
;auto explode	
	ld (IY+0),0
	ld (IY+8),128
	
	
.action4_nocollision	
	
.action04_boss_shoot_wait
	jp TV1
.action4_spawn
	db 15,0,0
	
.action5_fading
	ld A,(fading_start)
	inc A
	cp 64
	jr nc,action5_fading_run
	
	ld (fading_start),A
	jp TV5
	
.action5_fading_run
		
	ld A,(FRAME)
	and 7
	jp nz,TV5
	
	ld A,(fading_count)
	inc A
	ld (fading_count),A
	
	cp 4
	jr nc,fading_action_end
	
	ld HL,lvl_pal	
	call GFX_SetupLevelPalette
	
	jp TV5
.fading_action_end
	call GFX_HideScreen
	jp GAME_FullRestart
	
.fading_count
	db 0	
.fading_start
	db 0
	
	
	
.actionx00_nop
.actionx01_nop
.actionx02_nop
.actionx03_nop
.actionx04_nop
.actionx05_nop
.actionx06_nop
	jp TV3
	
.levelb_music
		incbin "snd/rtypecy2.raw"