;refs
.title_seq_table

dw title_right_sequence_alt
dw title_left_sequence


;scroll sequences

;.title_right_sequence
;dw title_blank
;dw title_right_r
;dw title_blank
;dw title_right_o
;dw title_blank
;dw title_right_t
;dw title_blank
;dw title_right_y
;dw title_blank
;dw title_right_p
;dw title_blank
;dw title_right_e
;dw title_blank
;dw title_blank
;dw title_blank
;dw 0

.title_right_sequence_alt
dw title_blank
dw title_right_s
dw title_blank
dw title_blank
dw 0

.title_left_sequence
dw title_full_left
dw 0

;scroll data blocks info

.title_full_left
dw logo_e5_alt
dw logo_e4_alt
dw logo_e3
dw logo_e2
dw logo_e1
dw logo_e0
dw logo_blank
dw logo_p6
dw logo_p5
dw logo_p4
dw logo_p3
dw logo_p2
dw logo_p1
dw logo_p0
dw logo_blank
dw logo_y6
dw logo_y5
dw logo_y4
dw logo_y3
dw logo_y2
dw logo_y1
dw logo_y0
dw logo_blank
dw logo_t6
dw logo_t5
dw logo_t4
dw logo_t3
dw logo_t2
dw logo_t1
dw logo_t0
;dw logo_blank
dw logo_o3
dw logo_o2
dw logo_o1
dw logo_o0
dw logo_blank
dw logo_r6
dw logo_r5
dw logo_r4
dw logo_r3
dw logo_r2
dw logo_r1
dw logo_r0
dw logo_blank
dw 0

.title_blank
repeat 25
dw logo_blank
rend
dw 0

;.title_right_r
;dw logo_r0
;dw logo_r1
;dw logo_r2
;dw logo_r3
;dw logo_r4
;dw logo_r5
;dw logo_r6
;dw 0

;.title_right_o
;dw logo_o0
;dw logo_o1
;dw logo_o2
;dw logo_o3
;dw 0

;.title_right_t
;dw logo_t0
;dw logo_t1
;dw logo_t2
;dw logo_t3
;dw logo_t4
;dw logo_t5
;dw logo_t6
;dw 0

;.title_right_y
;dw logo_y0
;dw logo_y1
;dw logo_y2
;dw logo_y3
;dw logo_y4
;dw logo_y5
;dw logo_y6
;dw 0

;.title_right_p
;dw logo_p0
;dw logo_p1
;dw logo_p2
;dw logo_p3
;dw logo_p4
;dw logo_p5
;dw logo_p6
;dw 0

;.title_right_e
;dw logo_e0
;dw logo_e1
;dw logo_e2
;dw logo_e3
;dw logo_e4
;dw logo_e5
;dw 0

.title_right_s
dw logo_s0
dw logo_s1
dw logo_s2
dw logo_s3
dw logo_s4
dw logo_s5
dw logo_s6
dw 0



;title GFX

;blank
.logo_blank
incbin "gfx/logo_blank.pm1"

;R
.logo_r0
incbin "gfx/logo_r0.pm1"
.logo_r1
incbin "gfx/logo_r1.pm1"
.logo_r2
incbin "gfx/logo_r2.pm1"
.logo_r3
incbin "gfx/logo_r3.pm1"
.logo_r4
incbin "gfx/logo_r4.pm1"
.logo_r5
incbin "gfx/logo_r5.pm1"
.logo_r6
incbin "gfx/logo_r6.pm1"

;o
.logo_o0
incbin "gfx/logo_o0.pm1"
.logo_o1
incbin "gfx/logo_o1.pm1"
.logo_o2
incbin "gfx/logo_o2.pm1"
.logo_o3
incbin "gfx/logo_o3.pm1"

;T
.logo_t0
incbin "gfx/logo_t0.pm1"
.logo_t1
incbin "gfx/logo_t1.pm1"
.logo_t2
incbin "gfx/logo_t2.pm1"
.logo_t3
incbin "gfx/logo_t3.pm1"
.logo_t4
incbin "gfx/logo_t4.pm1"
.logo_t5
incbin "gfx/logo_t5.pm1"
.logo_t6
incbin "gfx/logo_t6.pm1"

;Y
.logo_y0
incbin "gfx/logo_y0.pm1"
.logo_y1
incbin "gfx/logo_y1.pm1"
.logo_y2
incbin "gfx/logo_y2.pm1"
.logo_y3
incbin "gfx/logo_y3.pm1"
.logo_y4
incbin "gfx/logo_y4.pm1"
.logo_y5
incbin "gfx/logo_y5.pm1"
.logo_y6
incbin "gfx/logo_y6.pm1"

;P
.logo_p0
incbin "gfx/logo_p0.pm1"
.logo_p1
incbin "gfx/logo_p1.pm1"
.logo_p2
incbin "gfx/logo_p2.pm1"
.logo_p3
incbin "gfx/logo_p3.pm1"
.logo_p4
incbin "gfx/logo_p4.pm1"
.logo_p5
incbin "gfx/logo_p5.pm1"
.logo_p6
incbin "gfx/logo_p6.pm1"

;E
.logo_e0
incbin "gfx/logo_e0.pm1"
.logo_e1
incbin "gfx/logo_e1.pm1"
.logo_e2
incbin "gfx/logo_e2.pm1"
.logo_e3
incbin "gfx/logo_e3.pm1"
;.logo_e4
;incbin "gfx/logo_e4.pm1"
;.logo_e5
;incbin "gfx/logo_e5.pm1"

.logo_e4_alt
incbin "gfx/logo_e4_alt.pm1"
.logo_e5_alt
incbin "gfx/logo_e5_alt.pm1"

;Stacked
.logo_s0
incbin "gfx/logo_s0.pm1"
.logo_s1
incbin "gfx/logo_s1.pm1"
.logo_s2
incbin "gfx/logo_s2.pm1"
.logo_s3
incbin "gfx/logo_s3.pm1"
.logo_s4
incbin "gfx/logo_s4.pm1"
.logo_s5
incbin "gfx/logo_s5.pm1"
.logo_s6
incbin "gfx/logo_s6.pm1"

;game enter loop
.TITLE_CheckEE

;check gold logo
	ld B,2
	ld DE,#5346 ;default color set
	
	ld HL,level_list+1
	bit 6,(HL)
	jr z,TITLE_CheckEE_NoGold
	
	ld B,3		;add master menu
	;ld A,B
	;dec A
	;ld (option_menu_game_mode-1),A	;force choice to master menu

	ld DE,#4A4E
	
	;ld A,3
	;ld (option_menu_game_mode),A	;add master menu !
	
.TITLE_CheckEE_NoGold

	ld A,B
	ld (option_menu_game_mode),A ;update master menu count
	
	ld HL,menu_color_set1
	ld (HL),D
	inc HL
	ld (HL),E
	

	ret


.TITLE_CheckMusic

sound_test_zik	EQU $+1
	ld A,0
	cp 16
	ret z
	
	call MUSIC_Stop
	ld E,25
	call DSK_Wait

	ld A,16
	ld (sound_test_zik),A
	
	ld HL,title_music
	ld DE,#9000
	ld BC,title_music_size
	ldir
	
	ld A,#FE
	ld HL,#9000
	
	call MUSIC_Play
	
	ret
	
.GAME_EnterDelay
GAME_EnterDoDelay EQU $+1
	ld A,0
	or A
	ret nz
	
	ld E,5
	call DSK_Wait
	ret
	

.GAME_Enter
	ld SP,#9000	;be sure stack is correct

	;full mode to be sure to have music and SFX in menus
	xor A
	call SND_SetMode
	
	call TITLE_CheckMusic 
	call TITLE_CheckEE

	;jp TITLE_Credits
	
	;sfx on channel A
	;break
	xor A
	call SND_SfxSetForcedChannel
	
	call SND_SfxStart
	
	ld A,(menu_force)
	or A
	jp nz,GAME_EnterMenuForce
	
.GAME_EnterLoop

	call TITLE_Run
	
	xor A
	ld (GAME_EnterDoDelay),A
	
	;from second run , alternative title
	;ld HL,title_right_sequence_alt
	;ld (title_seq_table),HL
	
	;SFX mode
	

.GAME_EnterNoTitle
	
	ld HL,MENU_TITLE_SKIN
	call MENU_SetSkin
	
	ld HL,menu_int_table
	ld (int_table),HL

	
	;draw dare sentences !
	
	ld HL,#1800
	call GFX_GetCharAddress
	ex DE,HL	
	ld HL,title_sentence1
	call FONT_DrawString
	
	call GAME_EnterDelay	
		
	ld HL,#1801
	call GFX_GetCharAddress
	ex DE,HL	
	ld HL,title_sentence2
	call FONT_DrawString
	
	call GAME_EnterDelay
	
	;draw credits
	ld HL,#2307
	call GFX_GetCharAddress
	ex DE,HL
		
	ld A,(option_menu_continue)
	cp 6
	jr z,main_continue_freeplay
	
	add 16+128
	ld (credit_str_pos),A
	ld HL,credit_str
	jr main_continue_done

.main_continue_freeplay
	ld HL,freeplay_str
	
.main_continue_done	

	call FONT_DrawString
	
	call GAME_EnterDelay	
	
	;copyrights
	
	ld HL,#1D13
	call GFX_GetCharAddress
	ex DE,HL	
	ld HL,irem_str
	call FONT_DrawString
	
	call GAME_EnterDelay
	
	ld HL,#1214
	call GFX_GetCharAddress
	ex DE,HL	
	ld HL,ees_str
	call FONT_DrawString
	
	call GAME_EnterDelay
	
;	ld HL,#0215
;	call GFX_GetCharAddress
;	ex DE,HL	
;	ld HL,rgc_str
;	ld BC,color_table_options_red
;	call FONT_DrawStringEX

	
;check for cheat menu
;edit for reset contest , 1 = only start option
	;ld A,1
	ld A,2
	ld HL,level_list+1
	bit 5,(HL)
	jr z,TITLE_RunNoCheat
	inc A	;add 1 option on title menu for cheats
	
.TITLE_RunNoCheat
	ld (title_menu+1),A	


;no more delay when returning
	ld A,1
	ld (GAME_EnterDoDelay),A
	
	ld HL,title_menu ;main menu
	call MENU_Init	
	
;run menu with timer
	ld HL,1500 ;30 secs
	call MENU_RunWithTimer
	
	;call MUSIC_Stop

;tst
	;ld HL,tst_score	
	;call hi_score_check
	;jr z,tst_score_nohi
	
	;push BC
	;push HL
	
	;call GFX_Cls
	;ld HL,menu_int_table5
	;ld (int_table),HL
	
	;ld HL,continue_pal
	;call GFX_SetColors
	
	;ld HL,crtc_config_game ;crtc_config_options
	;call GFX_SetCRTC
	
	;pop HL
	;pop BC
	;push BC
	;push HL
	
	
	;ld DE,tst_score
	;call hi_score_insert
	
	;pop HL
	;pop BC
	
	;call hi_score_enter
	
;.tst_score_nohi

;show hi scores	
	call GFX_Cls
	ld HL,menu_int_table5
	ld (int_table),HL
	
	ld HL,continue_pal
	call GFX_SetColors
	
	ld HL,crtc_config_game ;crtc_config_options
	call GFX_SetCRTC
	
	call hi_score_menu_enter
	
	call GFX_Cls
	
	ld HL,crtc_config_options
	call GFX_SetCRTC
	
	JP GAME_EnterLoop

;.tst_score
;	db 1,5,0,0,0,0

.title_sentence1
	str "BLAST OFF AND STRIKE"
.title_sentence2
	str "THE EVIL BYDO EMPIRE!"
.irem_str
	str "\1987 IREM CORP."
.ees_str
	str "128K VERSION BY EASTER EGG"
;.rgc_str
;	str "PNL 0106"
.freeplay_str
	str "FREE PLAY"
.credit_str
	str " CREDIT 0"
credit_str_pos	EQU $-1

;new place for title stuff

TITLE_Run:

	call GFX_cls
	
	;change_bankM	#C4

	;ld A,11
	;call MUSIC_Play
		
	ld HL,menu_int_table3
	ld (int_table),HL
	
	ld HL,default_pal
	call GFX_SetColors
	
	ei	;done


	xor A
	ld (TITLE_CurrentState),A
	ld (TITLE_UpCount),A
	
	LD HL,(title_seq_table)
	ld (TITLE_SequencePtr),HL
	ld E,(HL)
	inc HL
	ld D,(HL)
	ld (TITLE_CurrentBlock),DE

	
.TITLE_RunLoop
;synchro
	ld A,(int_counter)
	cp 5
	jr nz,TITLE_RunLoop
	
	ld A,(TITLE_CurrentState)
	or A
	jr nz,TITLE_RunNotScrollLeft

;update scroll
	ld HL,(crtc_R12_offset)
	inc HL
	call TITLE_UpdateCRTC

;get current block
	ld HL,(TITLE_CurrentBlock)
	ld E,(HL)
	inc HL
	ld D,(HL)
	inc HL
	ld (TITLE_CurrentBlock),HL
;check if empty
	ld A,E
	or D
	call z,TITLE_NextBlock
	jr z,TITLE_State0End
	
	push DE
	ld HL,#2D00
	call GFX_GetCharAddress
	ld B,9
	call TITLE_CLeanStrip
	
	;ld HL,#2D08
	ld HL,#2D09
	call GFX_GetCharAddress
	pop DE
	call TITLE_DrawStrip
	
.TITLE_State0End

.TITLE_RunNotScrollLeft
	cp 1
	jr nz,TITLE_RunNotScrollRight

;update scroll
	ld HL,(crtc_R12_offset)
	dec HL
	call TITLE_UpdateCRTC

;get current block
	ld HL,(TITLE_CurrentBlock)
	ld E,(HL)
	inc HL
	ld D,(HL)
	inc HL
	ld (TITLE_CurrentBlock),HL
;check if empty
	ld A,E
	or D
	call z,TITLE_NextBlock
	jr z,TITLE_State1End
	push DE
	
	ld HL,#0009 ;8
	call GFX_GetCharAddress
	pop DE
	call TITLE_DrawStrip
	
.TITLE_State1End	
	

.TITLE_RunNotScrollRight
	cp 2
	jr nz,TITLE_RunDone
	ret
;edited old code ,this is now a local lentry
.TITLE_ScrollUp	
;counter synchro
	ld A,(int_counter)
	cp 5
	jr nz,TITLE_ScrollUp	
	
	ld A,(TITLE_UpCount)
	inc A
	ld (TITLE_UpCount),A
	cp 10
	jr nz,TITLE_RunNotScrollUpEnd
	ret
.TITLE_RunNotScrollUpEnd	
	;update scroll
	ld HL,(crtc_R12_offset)
	ld DE,46
	add HL,DE
	call TITLE_UpdateCRTC
.TITLE_ScrollUpCounter
	ld A,(int_counter)
	cp 5
	jr z,TITLE_ScrollUpCounter
	jr TITLE_ScrollUp	
	
.TITLE_ScrollDown	
;counter synchro
	ld A,(int_counter)
	cp 5
	jr nz,TITLE_ScrollDown	
	
	ld A,(TITLE_UpCount)
	inc A
	ld (TITLE_UpCount),A
	cp 10
	jr nz,TITLE_RunNotScrollDownEnd
	ret
.TITLE_RunNotScrollDownEnd	
	;update scroll
	ld HL,(crtc_R12_offset)
	ld DE,-46
	add HL,DE
	call TITLE_UpdateCRTC
.TITLE_ScrollDownCounter
	ld A,(int_counter)
	cp 5
	jr z,TITLE_ScrollDownCounter
	jr TITLE_ScrollDown	

	
.TITLE_RunDone
	
.TITLE_RunCounterLoop
;counter synchro
	ld A,(int_counter)
	cp 5
	jr z,TITLE_RunCounterLoop
	
	jp TITLE_RunLoop
	
.TITLE_UpdateCRTC
	ld A,H
	and %00000011
	ld H,A
	ld (crtc_R12_offset),HL
	
	ld A,#30
	or H
	ld H,A
;update R12 & R13	
	di
	ld BC,#BC00+12
	out (C),C
	inc B
	out (C),H
	dec B
	inc C
	out (C),C
	inc B
	out (C),L
	ei
	ret

;cleanup strip
;HL = address
;B = n chars	
.TITLE_CleanStrip
	ld C,0
		
.TITLE_CleanStripLoop
;repeat 3
	push BC
	ld B,3
	
.TITLE_CleanStripLoop_inloop	
	ld (HL),C
	inc L
	ld (HL),C
	dec L
	set 3,H
	ld (HL),C
	inc L
	ld (HL),C
	dec L
	ld A,8
	add H
	ld H,A
	
	djnz TITLE_CleanStripLoop_inloop
	pop BC
;rend
	ld (HL),C
	inc L
	ld (HL),C
	dec L
	set 3,H
	ld (HL),C
	inc L
	ld (HL),C
	dec L
	
	ld A,H
	and %11000111
	ld H,A
	
	ld A,92
	add L
	ld L,A
	
	jr nc,TITLE_CleanStrip_CharDone
	inc H	
	ld A,H
	and %11000111
	ld H,A	
	
.TITLE_CleanStrip_CharDone

	djnz TITLE_CleanStripLoop
	ret

;Draw strip
;HL = address
;DE = strip address

.TITLE_DrawStrip
	;break
	ld BC,#0808
.TITLE_DrawStripLoop
;repeat 3
	push BC
	ld B,3
	
.TITLE_DrawStripLoopRepeat

	ld A,(DE)
	ld (HL),A
	inc DE
	inc L
	ld A,(DE)
	ld (HL),A
	inc DE
	dec L
	set 3,H
	ld A,(DE)
	ld (HL),A
	inc DE
	inc L
	ld A,(DE)
	ld (HL),A
	inc DE
	dec L
	ld A,C
	add H
	ld H,A
	
	djnz TITLE_DrawStripLoopRepeat
	pop BC
;rend
	ld A,(DE)
	ld (HL),A
	inc DE
	inc L
	ld A,(DE)
	ld (HL),A
	inc DE
	dec L
	set 3,H
	ld A,(DE)
	ld (HL),A
	inc DE
	inc L
	ld A,(DE)
	ld (HL),A
	inc DE
	dec L

	ld A,H
	and %11000111
	ld H,A
	
	ld A,92
	add L
	ld L,A
	
	jr nc,TITLE_DrawStrip_CharDone
	inc H	
	ld A,H
	and %11000111
	ld H,A
	
.TITLE_DrawStrip_CharDone
	djnz TITLE_DrawStripLoop
	
	ret
	
	
.TITLE_NextBlock
	ld HL,(TITLE_SequencePtr)
	inc HL
	inc HL
	ld (TITLE_SequencePtr),HL
	ld E,(HL)
	inc HL
	ld D,(HL)
	
	ex DE,HL
	ld A,H
	or L
	jr z,TITLE_NextState	

	ld E,(HL)
	inc HL
	ld D,(HL)
	inc HL
	ld (TITLE_CurrentBlock ),HL
	
	ret
.TITLE_NextState
	ld A,(TITLE_CurrentState)
	inc A
	ld (TITLE_CurrentState),A
	
	cp 1
	jr nz,TITLE_NotChangeForState1

	LD HL,(title_seq_table+2)
	ld (TITLE_SequencePtr),HL
	ld E,(HL)
	inc HL
	ld D,(HL)
	ld (TITLE_CurrentBlock),DE	
	
	
.TITLE_NotChangeForState1	
	xor A
	ret

;jump to menu callback using choice = 0 so start game	
.GAME_EnterMenuForceCasual
	xor A
	call menu_title_callback

;adapt code to enter force mode
; 1 = return to casual
; 2 = return to options	
.GAME_EnterMenuForce

	ei
	push AF
	xor A
	ld (menu_force),A	;reset flag
	;reset CRTC offset
	ld HL,crtc_R12_offset
	ld (HL),A
	inc HL
	ld (HL),A
	;we must be in mode 1
	change_mode 1
	;standard ints !
	ld HL,menu_int_table3
	ld (int_table),HL
	
	pop AF
	cp 1
	jr z,GAME_EnterMenuForceCasual
	
;else we go to options
;hide screen
	ld BC,#BC01
	xor A
	out (C),C
	inc B
	out (C),A

	ld B,43
	ld HL,#2B00
	ld IX,title_full_left

.GAME_EnterMenuForce_OptionDrawLoop	
	push BC
	push HL
	
	
	call GFX_GetCharAddress
	ld E,(IX+0)
	ld D,(IX+1)
	inc IX
	inc IX
	call TITLE_DrawStrip	
	pop HL
	dec H
	pop BC
	djnz GAME_EnterMenuForce_OptionDrawLoop
	
	ld HL,menu_int_table3
	ld (int_table),HL	
	
	;scroll up title
	;xor A
	;ld (TITLE_UpCount),A
	;call TITLE_ScrollUp	

	ld HL,MENU_OPTION_SKIN
	call MENU_SetSkin
	
	;call MUSIC_Stop	;stop music
	;change music to #FF to keep sfx
	;ld A,#FF
	;call MUSIC_Play

	ld HL,menu_int_table2
	ld (int_table),HL
;show screen	
	ld BC,#BC01
	ld A,#2E
	out (C),C
	inc B
	out (C),A

	jp menu_reenter_options
	


	
.TITLE_CurrentState	db 0
.TITLE_UpCount		db 0
.TITLE_SequencePtr	dw 0
.TITLE_CurrentBlock	dw 0

.title_music
incbin "snd/rtypetit.raw"
title_music_size	EQU $-title_music

.default_data
	incbin "bin/facea/hiscore.dat"
default_data_size EQU $-default_data
.default_cat_art
	incbin "bin/cat_art.raw"