;casual menu

casual_menu_posY	EQU 2
casual_menu_posX	EQU 14

.casual_menu_callback

	bit 7,A
	jp nz,casual_menu_draw_callback
	bit 6,A
	jp nz,casual_menu_draw_callback
	
	cp 4
	jp z,casual_menu_start_howto

	cp 5
	jp z,casual_menu_exit_callback
	
	cp 0	;start ?
	ret nz
	
;increment game count
	ld A,(casual_played)
	cp #FF
	jr z,casual_count_skip
	inc A
.casual_count_skip
	ld (casual_played),A
	

;weapon respawn mode
	ld A,(casual_menu_weapon)
	ld (CASUAL_RespawnWeapon),A

;immediate respawn ...or not !	
	ld A,(casual_menu_rebirth)
	ld (CASUAL_RespawnImmediate),A

	;level try
	ld A,(casual_menu_try)
	inc A
	ld (current_lvl),A	
	pop AF
	ret
	
.casual_menu_start_howto
	ld A,10	;level 10!
;check if cybernoid level unlocked
	ld HL,level_list+1
	bit 7,(HL)
	jr z,casual_menu_start_howto_nocybernoid_level

;only for cybernoid level
	
	;weapon respawn mode
	ld A,(casual_menu_weapon)
	ld (CASUAL_RespawnWeapon),A

	;immediate respawn ...or not !	
	ld A,(casual_menu_rebirth)
	ld (CASUAL_RespawnImmediate),A
	
	ld A,11
	
.casual_menu_start_howto_nocybernoid_level	
	
	ld (current_lvl),A
	pop AF
	pop AF	;remove return address
	jp menu_title_startgame_nojingle	;jump directly to no jingle
	;ret
	
.casual_mode_enter
	;no music but only sfx
	;ld A,#FF
	;call MUSIC_Play

	ld A,(option_menu_game)	;option 1 = casual
	;or A
	cp 1
	jr nz,casual_not

	ld DE,casual_menu_howto_title
	ld HL,level_list+1
	bit 7,(HL)
	jr z,casual_normal_how2
	ld DE,casual_menu_howto_alternate
.casual_normal_how2
	ld (casual_menu_howto_title_strA),DE
	ld (casual_menu_howto_title_strB),DE
	
	ld HL,0
	ld BC,#162E
	call menu_cleanstrip
	
	ld HL,menu_int_table4
	ld (int_table),HL

;draw casual illustration	

	call casual_draw_image
	
	;draw casual title
	ld HL,#1100
	call GFX_GetCharAddress
	ex DE,HL	
	ld HL,casual_title
	ld BC,color_table_options_red
	call FONT_DrawStringEX

	ld HL,MENU_OPTION_SKIN
	call MENU_SetSkin
		
	ld HL,casual_menu
	call MENU_Init	
	jp MENU_Run
	

.casual_not
;cheat mode - selected level
;only if cheats are active
	ld L,1
	ld A,(cheat_active)
	or A
	jr z,casual_not_nocheats
	
	ld A,(cheat_level)
	ld L,A
	
.casual_not_nocheats	
	
	ld A,L
	ld (current_lvl),A
	
	xor A
	ld (CASUAL_RespawnWeapon),A
	ld (CASUAL_RespawnImmediate),A
	
	;increment game count
	ld A,(arcade_played)
	cp #FF
	jr z,arcade_count_skip
	inc A
.arcade_count_skip
	ld (arcade_played),A
	
	ret
	
.casual_menu_exit_callback

	;pop HL
	pop HL
	
	jp GAME_Enter

;***************************
;***** menus callbacks *****
;***************************

.option_menu_cheat_check
	;ld HL,level_list+1	;are cheats already unlocked ?
	;bit 5,(HL)
	;ret nz
	
	ld A,(option_cheat_ptr)
	cp 12	;at least 12 chars before fire !
	ret c
	
	ld E,A
	ld D,0
	ld HL,option_cheat_buffer
	add HL,DE
	ex DE,HL
	ld HL,option_cheat_sequence
	
	ld B,12
.option_menu_cheat_check_loop
	ld A,(DE)
	cp (HL)
	ret nz
	dec HL
	dec DE
	djnz option_menu_cheat_check_loop
	
	ld A,sfx_1up
	call SND_SfxPlay
	
	;set cheats
	ld HL,level_list+1
	;set 5,(HL)
	ld A,%00100000
	xor (HL)
	ld (HL),A
	
	;no more in buffer
	xor A
	ld (option_cheat_ptr),A

	pop AF	;saved
	pop AF  ;previous ret address
	ret

.option_menu_cheat_unlock
	and 64+128	;keep only direction
	push AF
	ld A,(option_cheat_ptr)
	inc A
	cp 16	;16 chars max
	jr nc,option_menu_cheat_exit
	ld (option_cheat_ptr),A
;goto end of location	
	ld E,A
	ld D,0
	ld HL,option_cheat_buffer
	add HL,DE
	
	pop AF
	ld (HL),A
	ret	
	
.option_menu_cheat_exit
	pop AF
	ret
.option_cheat_ptr
	db 0
.option_cheat_buffer
	ds 16,0
	
	db 64,64,128,128,64,64,128,128,64,128,64,128
	option_cheat_sequence EQU $-1

.option_menu_callback
	cp 64
	jr nc,option_menu_cheat_unlock
	
	push AF
	call option_menu_cheat_check
	pop AF
	
	cp 4
	jr z,option_menu_soundtest_callback	
	cp 5
	jr z,option_menu_reset_data
	cp 6
	jr z,option_menu_enter_credits
	cp 7
	jr z,option_menu_returnmain
	ret
.option_menu_reset_data
	ld DE,140
	ld A,(option_menu_reset)
	or A
	ret z
	;reset option
	;xor A
	;ld (option_menu_reset),A
	;jr z,option_menu_reset_data_reset_done
	ld BC,default_data_size-4	;no full data set , only ranking	
	cp 1
	jr z,option_menu_reset_data_continue
	ld BC,default_data_size
.option_menu_reset_data_continue
	ld HL,default_data
	ld DE,hi_score_table
	ldir
.option_menu_reset_data_reset_done
	xor A
	ld (option_menu_reset),A
	ld (option_menu_game_mode-1),A	;force choice to arcade mode	
	pop HL
	jp GAME_Enter
	
.option_menu_enter_credits
	;change_bankM	#C4 ;we are already in #C4 normally !
	pop HL	;remove ret address on stack
	jp TITLE_Credits
	;jp GAME_Enter
	
.option_menu_returnmain
	pop HL
	;call MUSIC_Stop
	
	ld HL,#0008
	ld BC,#0E2E
	call menu_cleanstrip
	

	ld HL,menu_int_table3
	ld (int_table),HL	
	
	;scroll up title
	xor A
	ld (TITLE_UpCount),A
	call TITLE_ScrollDown
	
	
	;disable cheat if casual active	
	ld A,(option_menu_game)
	or A
	jr z,casual_notactive
	xor A
	ld (cheat_active),A
.casual_notactive

	call TITLE_CheckMusic 
	;jp GAME_Enter
	jp GAME_EnterNoTitle
	
.option_menu_soundtest_callback

	call MUSIC_Stop
	ld A,(option_menu_soundtest)
	ld (sound_test_zik),A
	call MUSIC_Play

	ret
	
.menu_title_callback
	cp 2
	jp z,menu_enter_cheats
	cp 1
	jr z,menu_enter_options
	or A
	jr z,menu_title_startgame
	ret
.menu_title_startgame
	
	call casual_mode_enter
	
.menu_title_startgame_retfromcasual
	;default channel for jingle channel B for sfx
	ld A,1
	call SND_SfxSetForcedChannel
	;cut music first
	ld A,#FF
	call MUSIC_Play
	;play sound
	ld A,sfx_startGame
	call SND_SfxPlay

	
	ld B,100
.menu_title_run_loop
	push BC
	call GFX_WaitVbl
	call GFX_WaitVblEnd
	pop BC
	djnz menu_title_run_loop
	

	
.menu_title_startgame_nojingle	
	
;*********************************
	call MUSIC_Stop ;stop music first !
	;channel B for sfx
	ld A,1
	call SND_SfxSetForcedChannel
	pop HL
	jp GAME_Run
	
.menu_enter_options_screen
	ld HL,0
	ld BC,#082E
	call menu_cleanstrip
	
	ld HL,#0013
	ld BC,#032E
	call menu_cleanstrip
	

	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

	ret

	
.menu_enter_options
	;no need for ret address
	pop HL
	pop HL
	;reset sequence ptr
	xor A
	ld (option_cheat_ptr),A
	
	;change screen config
	call menu_enter_options_screen
.menu_reenter_options	
	;draw option title
	;ld HL,#1109
	ld HL,#1409
	call GFX_GetCharAddress
	ex DE,HL	
	ld HL,options_title
	ld BC,color_table_options_red
	call FONT_DrawStringEX
	
	ld HL,option_menu
	call MENU_Init	
	jp MENU_Run
	
;cleanup a strip
;HL = x,y
;B = height , C = width
.menu_cleanstrip

.menu_cleanstrip_loopX
	push HL
	push BC
	
	call GFX_GetCharAddress
	pop BC
	push BC
	call TITLE_CLeanStrip
	
	pop BC
	pop HL
	inc H
	dec C
	jr nz,menu_cleanstrip_loopX
	
	ret
	
	
	
	
.menu_cleanup
	ld HL,#2D0A
	
.menu_cleanup_loop

	push HL
	call GFX_GetCharAddress
	ld B,12
	call TITLE_CLeanStrip
	pop HL
	
	dec H
	jr nz,menu_cleanup_loop
	ret
	
	
;***********************
;*** CHEATS CALLBACK ***
;***********************
	
.menu_enter_cheats
	;no need for ret address
	pop HL
	pop HL
	
	;change screen config
	call menu_enter_options_screen
	call cheat_convert_to_menu
	
	ld HL,#1409
	call GFX_GetCharAddress
	ex DE,HL	
	ld HL,title_menu_cheat_title
	ld BC,color_table_options_red
	call FONT_DrawStringEX

	ld HL,cheat_menu
	call MENU_Init	
	jp MENU_Run
	
.cheat_menu_callback
	cp 7
	ret nz
	;jp z,option_menu_returnmain
	;ret
	;break
;convert menu info to cheat
	ld BC,0
	ld E,0
	ld A,(cheat_force)
	cp 1
	jr nz,cheat_convert_not_force1
	inc E
.cheat_convert_not_force1
	cp 2
	jr nz,cheat_convert_not_force2
	inc C
.cheat_convert_not_force2
	cp 3
	jr nz,cheat_convert_not_force3
	inc B
.cheat_convert_not_force3
	ld A,E
	ld (cheat_mod1),A
	ld A,C
	ld (cheat_mod2),A
	ld A,B
	ld (cheat_mod3),A
	
	ld BC,0
	ld A,(cheat_bit)
	or A
	jr z,cheat_convert_notbit
	inc C
	dec A
	jr z,cheat_convert_notbit
	inc B

.cheat_convert_notbit
	ld A,C
	ld (cheat_pill1),A
	ld A,B
	ld (cheat_pill2),A
;disable casual mode is cheat active	
	ld A,(cheat_active)
	or A
	jr z,cheat_notactive
	xor A
	ld (option_menu_game),A
.cheat_notactive
	jp option_menu_returnmain	
	
;convert cheat info to menu info
.cheat_convert_to_menu	
	ld C,3
	ld A,(cheat_mod3)
	or A
	jr nz,cheat_convert_to_menu_force_found
	dec C
	ld A,(cheat_mod2)
	or A
	jr nz,cheat_convert_to_menu_force_found
	dec C
	ld A,(cheat_mod1)
	or A
	jr nz,cheat_convert_to_menu_force_found
	dec C
.cheat_convert_to_menu_force_found
	ld A,C
	ld (cheat_force),A
;pill stuff	
	ld A,(cheat_pill1)
	ld C,A
	ld A,(cheat_pill2)
	add C
	ld (cheat_bit),A
	
	ret
	