;start text - 12,8 (2) + 6,4 (1) , 6,4s = 320 vbl
;ships 2 - 5

;************************
;*** LEVEL 9 - ENDING ***
;************************

ORG level_start

IF output_files
write "bin/level9.lvl"
ENDIF

START	DEFB 0
SYNCHRO	DEFW 0000

START1	DEFB 0
START2	DEFB 0
START3	DEFB 0

SYNCHRO1	DEFW SYNC1 ;tst_sync ;seqX61 ;seq_boss ;seqX60 ;SYNC1 ;SYNC3 ;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,12,0
	db 0
	db 0,20,2,3,0,10,1,1,0,12,3,9,0,3,1,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 #FF 
.lvl_skip
	dw 0	;music to skip on music restart
.lvl_init
	dw	l9_init	;called after loading
.lvl_reinit
	dw	l9_reinit	;called each time player dies
	
.l9_init
	ld A,1
	ld (no_hud),A

	
	ret

.l9_reinit

;are we in arcade mode ?
	ld A,(option_menu_game)
	;or A
	;jr z,l9_reinit_gamemodeNormal
	cp 1
	jr nz,l9_reinit_gamemodeNormal

;did we start from level 1 ?
	ld A,(casual_menu_try)
	or A	;try 0 = level 1
	call nz,GAME_ForceOver
	
.l9_reinit_gamemodeNormal	
;are cheat active ?
	ld A,(cheat_active)
	or A
	jr z,l9_reinit_cheat_acceptable
	ld A,(cheat_level)
	;or A
	cp 1
	jr z,l9_reinit_cheat_acceptable
	
	call GAME_ForceOver
.l9_reinit_cheat_acceptable
;no weapon !
	xor A
	ld (BALL1),A
	ld (BALL2),A
	ld (BALL3),A
	ld (PILL2),A
	ld (PILL1),A
	ld (MISSILE),A
	ld (SHOOTON),A
	ld (writter_current_event_count),A
	ld HL,0
	ld (vbl_counter),A

;out of the screen	
	ld A,-30
	ld (SHIPX),A
;correct placement	
	ld A,9
	ld (SHIPY),A
;force player move	
	ld A,4
	ld (ForcePlayerMove),A
	ld A,45
	ld (INTRO_TIMER),A
	
	LD A,150 ;100
 	LD (EXBYTE),A
	
;registers BG effects
	ld HL,STARS_Init
	ld (BG_LevelEffect0_Init),HL
	
	ld HL,STARS_Run
	ld (BG_LevelEffect0_Run),HL

	ld HL,STARS_Stop
	ld (BG_LevelEffect0_Stop),HL 
	
;special colors for this hud	
	ld HL,HUD_EndColors
	ld (int_hud_color_setup),HL
	
;prevent fading at start	
	xor A
	ld (level_fade),A

;music + sfx	
	xor A
	call SND_SetMode
	
	;call l9_show_true_ending
	
	ld A,4
	ld (ForcePlayerMove),A
	call MMI
	xor A
	ld (MMIVAR),A

	ret

;	LEVEL	1 - 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
;explosions
	db 0,1,1,5,5

	SCROLL_Stop	7,0
	
;start writter	
	db 9,20,6,0,0

	
;start ships
	db 9,20,9,0,0
	
;ships	
	;db 16,16,2,-5,7
	;db 16,24,3,-5,15

	
	;db 17,0,3,-5,2
	;db 17,8,2,-5,16
	;db 17,16,5,-5,11
	;db 17,24,4,-5,5
	
	;db 18,0,2,-5,2
	;db 18,8,3,-5,12
	;db 18,16,4,-5,7
	;db 18,24,5,-5,5
	
	;db 19,0,4,-5,2
	;db 19,8,5,-5,12

;star	
	db 23,8,7,28,0
	
;fading and end	
	db 24,0,8,0,0	
	
	db 255,0,0,0,0
	
	
.SQTABLE	
	dw explosions_sq		;1
	dw r9_sq_red 			;2
	dw r9_sq_green			;3
	dw r9_sq_blue			;4
	dw r9_sq_orange			;5
	dw writter_sq			;6
	dw star_sq				;7
	dw fading_sq			;8
	dw ship_ctrl_sq			;9
	dw r9_sq_red2			;10
	dw r9_sq_blue2			;11
	dw r9_sq_green2			;12

	
	
	
	

	
;sequences	
 ;
;	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
;

.ship_ctrl_sq
	db 5,63,0
	dw writter_spr
	dw #FFFF
	


.fading_sq
	db 4,63,0
	dw writter_spr
	dw #FFFF

.star_sq
	db 0,60,0
repeat 10
	dw star_spr
rend
	dw 0
	
.star_spr
	db 5,24
	db 6,24+128

.writter_sq
	db 2,60,0
	dw writter_spr
	dw #FFFF
.writter_spr
	db 1,128

.explosions_sq
	db 1,63,0
repeat 16
	dw explosions_spr
rend
repeat 22
	dw explosions_spr3
rend
	dw explosions_spr2
	dw 0
.explosions_spr
	db 1,0,1,0
	db 1,128
.explosions_spr2
	db 2,128
.explosions_spr3
	db 3,0,3,128
	
;red R9	
.r9_sq_red
	db 3,63,0
repeat 10
	dw r9_spr_red
rend
	dw 0	
.r9_spr_red
	db 11,32,11,32,11,32,11,32+128
	
;red R9 half char move
.r9_sq_red2
	db 0,63,0
repeat 10
	dw r9_spr_red2
rend
	dw 0
.r9_spr_red2
	db 11,32,16,0,11,32,16,0,11,32,16,0,11,32,16,128
	
;blue r9 half char move
.r9_sq_blue2	
	db 0,63,0
repeat 10
	dw r9_spr_blue2	
rend
	dw 0
.r9_spr_blue2	
	db 14,32,19,0,14,32,19,0,14,32,19,0,14,32,19,128
	
;blue r9 half char move
.r9_sq_green2	
	db 0,63,0
repeat 10
	dw r9_spr_green2	
rend
	dw 0
.r9_spr_green2	
	db 13,32,18,0,13,32,18,0,13,32,18,0,13,32,18,128	
	
;green R9	
.r9_sq_green
	db 3,63,0
repeat 10
	dw r9_spr_green
rend
	dw 0	
.r9_spr_green
	db 13,32,13,32,13,32,13,32+128
	
;blue R9	
.r9_sq_blue
	db 3,63,0
repeat 10
	dw r9_spr_blue
rend
	dw 0	
.r9_spr_blue
	db 14,32,14,32,14,32,14,32+128
	
;orange R9	
.r9_sq_orange
	db 3,63,0
repeat 10
	dw r9_spr_orange
rend
	dw 0	
.r9_spr_orange
	db 15,32,15,32,15,32,15,32+128
	

.SPEV1
	dw noaction,explo_action,writter_action,r9_action,fading_action,ship_control	
.SPEV2
	dw nopostaction
	
.noaction
	jp TV1
	
.fading_action
	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

	ld A,(cheat_active)
	or A
	jr nz,l9_nogold
	
	;we won gold title !
	ld HL,level_list+1
	set 6,(HL)
	
.l9_nogold
	call l9_show_true_ending

	call GAME_ForceOver
	;xor A
	;ld (Current_Lives),A
	;inc A
	;ld (Current_Tries),A
	
	;ld A,1
	;ld (END_COUNT),A
	;ld HL,GAME_Restart
	;ld (END_JUMP),HL
	ld (IY+0),0
	jp TV5
	
	
.fading_count
	db 0
	
.r9_action
	push BC
	push DE
	
	ld A,10
	dec C
	dec C
	inc B
	call TSPGADD
	ld A,(FRAME)
	and 1
	jr z,r9_action_draw_rev
	call SPNS
	jr r9_action_done	
.r9_action_draw_rev
	call SPBS
.r9_action_done	
	pop DE
	pop BC
	jp TV1
	
.ship_control
	ld A,(writter_current_event_count)
	cp 2
	jp c,TV5
	cp 6
	jp nc,TV5
	
	cp 5
	jr nz,ship_control_not_last_pattern
	
	
	ld A,(vbl_counter)
	cp #20
	jr c,ship_control_not_last_pattern
	
	ld (IY+0),0
	jp TV5
	
.ship_control_not_last_pattern	
	ld A,(FRAME)
	and 7
	jp nz,TV5
	
	push DE
	push BC
	
	ld HL,(ship_control_ptr)
	ld A,(HL)
	cp #FF	
	jr nz,ship_control_list_notend
		
	ld HL,ship_control_list
.ship_control_list_notend	
	push HL
	ld DE,ship_control_spawn
	ld BC,3
	ldir
	ld (ship_control_ptr),HL
	pop HL
	call FORCESYNC
	
	
	
	
	pop BC
	pop DE

	jp TV5	
	
.ship_control_spawn
	db 0,0,0
	
.ship_control_ptr
	dw ship_control_list
	
.ship_control_list
	db 2,-5,7
	db 3,-5,15
	db 10,-5,2
	db 2,-5,16
	;db 5,-5,11
	db 12,-5,11
	db 4,-5,5	
	;db 2,-5,2
	db 3,-5,13
	db 4,-5,7
	;db 5,-5,5	
	db 11,-5,5
	db 4,-5,2
	db 5,-5,13
	db #FF
	
.writter_action

;if we reached 6,2 seconds (320 vbl)
	ld HL,vbl_counter+1
	ld A,(HL)	;high first
	cp 1
	jp c,TV5
	dec HL
	ld A,(HL)
	cp #40
	jp c,TV5 ;writter_action_erase_text

;vbl = 320 , write text	
	ld A,(writter_current_event_count)
	inc A
	ld (writter_current_event_count),A
	cp 2
	jp c,TV5

;reset vbl count	
	xor A
	ld (HL),A
	inc HL
	ld (HL),A
	
	push DE
	push BC
	
	ld A,20
	call GFX_SendR6
	call writter_action_erase_text
	
.writter_action_loop
	
;get address
	ld HL,(writter_address)
	ld E,(HL)
	inc HL
	ld D,(HL)
	inc HL
;no more to write ?	
	ld A,D
	or E
	jr z,writter_action_done
	

	
;get text address	
	ld C,(HL)
	inc HL
	ld B,(HL)
	inc HL
	
	ld A,(HL)
	inc HL
	
	ld (writter_timer),A

;save writter new address
	ld (writter_address),HL
	
	ld H,B
	ld L,C
	
;do write
	ld BC,color_table_options_normal
	call FONT_DrawStringEX
	
	ld A,(writter_timer)
	or A
	jr z,writter_action_loop


.writter_action_done
	ld A,24
	call GFX_SendR6

	pop BC
	pop DE
	
	jp TV5
	
.writter_action_erase_text
	;dec HL
	;ld A,(HL)
	;cp #30
	;jp c,TV5
	
	;push BC
	;push DE
	
	ld HL,txt9
	ld DE,writter_base	
	ld BC,color_table_options_normal
	call FONT_DrawStringEX
	
	ld HL,txt9
	ld DE,writter_base+128	
	ld BC,color_table_options_normal
	call FONT_DrawStringEX
	
	;pop DE
	;pop BC
	;jp tv5
	ret
	
.writter_address
	dw writer_action_list
.writter_timer
	db 1
.writter_current_event_count
	db 0
	
writter_base	EQU #C540
.writer_action_list
	dw writter_base+3,txt1:db 0			;3
	dw writter_base+128+3,txt2:db 110	;6
	
	dw writter_base+1,txt3:db 0			;1
	dw writter_base+128+1,txt4:db 110	;4
	
	dw writter_base+4,txt5:db 0			;4
	dw writter_base+128+4,txt6:db 110	;14
	
	
	dw writter_base+1,txt7:db 0			;1
	dw writter_base+128+1,txt8:db 90	;22
	
	dw 0
	
.txt1
	str "THANKS TO YOUR BRAVE FIGHTING"
.txt2
	str "THE COSMOS RESTORED PEACE."
.txt3
	str "THE BYDO EMPIRE WAS ANNIHILATED"
.txt4
	str "TO NEVER SCARE PEOPLE AGAIN."
.txt5
	str "YOUR NAME WILL REMAIN IN THE"
.txt6
	str "UNIVERSE FOREVER."
.txt7
	str "THANK YOU FOR PLAYING THE GAME"
.txt8
	str "TO THE END."
.txt9
	ds 31,0
	db 128
	
.explo_action

	ex AF,AF'
	cp 3
	jp z,TV5
	cp 2
	jr nz,explo_action_nomusic

	
	push IY
	push DE
	;setup music
	ld A,#FE
	ld HL,end_music
	call MUSIC_Play 
	pop DE
	pop IY
	
	ld HL,0
	ld (vbl_counter),HL
	
	jp TV5
	
.explo_action_nomusic
	ex AF,AF'

	;ld A,(FRAME)
	;and 1
	;jp nz,TV5
	
;compute explosion
	ld A,sfx_explodeC
	call SND_SfxPlay
	CALL ADDEXPLO0
	
	ld (HL),72
	inc L
	
	ld A,R
	and 7
	sub 2
	ld (HL),A

	inc L
	
	ld A,R
	and 15
	add 2
	ld (HL),A
	
	jp TV5

	
.nopostaction
	jp TV3
	
.l9_show_true_ending
;only in arcade mode !
;correct here for MASTER mode ?
	ld A,(option_menu_game)
	;or A
	;ret nz
	cp 1
	ret z	;if casual return
	
;cheats ? no true ending	
	ld A,(cheat_active)
	or A
	ret nz
	
;force master mode
	ld A,2
	ld (option_menu_game_mode-1),A	;force choice to master menu	
	
	call GFX_HideScreen
	
;wait a bit
	ld E,100
	call DSK_Wait
	
	SetIntTable int_l9_table
	
	ld HL,end_crtc_config
	call GFX_SetCRTC
	
	ld HL,true_end_color_pal
	call GFX_SetColors
	
	ld BC,#7FC5
	out (C),C
	
	ld HL,#4000
	ld DE,#C000
	
	ld B,32
	
.l9_true_ending_draw_image_loop_chars
	push BC
	push DE
	ld B,8
.l9_true_ending_draw_image_loop_lines
	push BC
	push DE

	ld BC,64
	ldir
	
	pop DE
	
	ld A,8
	add D
	ld D,A
	
	pop BC
	djnz l9_true_ending_draw_image_loop_lines
	
	pop DE
	ld BC,64
	ex DE,HL
	add HL,BC
	ex DE,HL
	
	pop BC
	djnz l9_true_ending_draw_image_loop_chars
	
	change_mode 0
	
	xor A
	ld (#C000),A	;erase bank mark
	
	ld HL,current_score
	ld DE,end_score_score
	ld B,6
	
.l9_ending_make_score
	ld A,(HL)
	add 16
	ld (DE),A
	inc HL
	inc DE
	djnz l9_ending_make_score
	
	ld HL,end_score
	ld DE,#C000+1984+14
	call FONT_DrawString
	
	ld A,32
	call GFX_SendR6

	xor A
	ld (ForcePlayerMove),A
	
	call KEY_Update
	call KEY_UpdateState

.l9_wait_key
	call GFX_WaitVbl
	
	;call MMI
	;bit 4,A
	call KEY_Update
	call KEY_UpdateState
	ld A,(keys+4)
	cp 1
	jr nz,l9_wait_key
	
	call GFX_Cls
	
	ld HL,crtc_config_game
	call GFX_SetCRTC
	
	xor A
	ld HL,lvl_pal	
	call GFX_SetupLevelPalette
	
	call GFX_HideScreen

	
	ret

	
;interrupt table - 8 vectors
.int_l9_table
dw int_first
dw int_SND_Player
dw int_done
dw int_done
dw int_done
dw int_last
dw int_done
dw int_done

.int_last
	repeat 48
	call int_wait_line
	rend
	change_mode 1
	jp int_done
	
.int_first
	change_mode 0
	jp int_done
	
.int_wait_line
	db 64-7,0
	ret
	
.end_crtc_config
	db #01,#20,#02,#2A,4,38,5,0,#06,0,#07,38-3,#0C,#30,#0D,#00,9,7,#FF
.true_end_color_pal
	db 0,20,20,11,3,23,10,2,1,7,4,8,17,15,24,26,0
	
.end_score
	db "YOUR SCORE: "
.end_score_score	
	str "000000 "
	
.end_music
	incbin "snd/rtypeend.raw"
	

read "starfield.asm"