// This is our main menu. How jolly exciting, eh?

// Selector Defines
#define BASE_Y				passed_param_1
#define OPTION_HEIGHT			passed_param_2
#define MAXIMUM_OPTIONS			passed_param_3
#define OPTION_WIDTH			passed_param_4

#define OPTION_NUMBER_HIGHLIGHTED	v0
#define option_has_been_selected	v3

// Menu Defines

#define option_text			v0
#define option_y_co_ord			v1
#define selector_id			v2
#define playing_music_channel		v3

#define sound_volume			v4
#define music_volume			v5

#define playing_music_channel		v6

#define x_scale_counter			v7
#define y_scale_counter			v8
#define window_brightness		v9
#define continue_available		v10
#define menu_option_gap			v11
#define disabled_option			v12

	let signal = signal OR WAKE_CREDITS
	let signal = signal OR WAKE_LOGO
	let signal = signal OR SLEEP_TUNNEL_LIGHT
	let signal = signal OR WAKE_SCROLLY
	let signal = signal OR MENU_WIZ_AND_CAT

	let sound_volume = GET_GLOBAL_SOUND_VOLUME
	let music_volume = GET_GLOBAL_MUSIC_VOLUME

	let playing_music_channel = GET_GLOBAL_FLAG (playing_music_channel_flag)

	// Each of the menus has a wait 5 at the start to allow everything from the previous one to do it's thing.
	wait 5

	if (playing_music_channel = CONST_UNSET) then
		if (music_volume > 0) then
			let playing_music_channel = PLAY_MUSIC_STREAM_GET_HANDLE (WIZBALL_TITLE,255,CONST_MUSIC_FREQUENCY,128,-1)
			SET_GLOBAL_FLAG (playing_music_channel_flag, playing_music_channel)
			SET_GLOBAL_FLAG (playing_music_stream_flag, WIZBALL_TITLE)
			LET TEMP_1 = GET_GLOBAL_FLAG (played_tunes_flag)
			LET TEMP_1 = TEMP_1 OR MAIN_MENU_TUNE_BITFLAG
			SET_GLOBAL_FLAG (played_tunes_flag,TEMP_1)
		end_if
	end_if

	SET_GLOBAL_FLAG_AS_ENTITY_ID (menu_entity_identity_flag)

	// This variable isn't used to kill this thing, though. Just as a reminder, really.
	let ENTITY_TYPE = ENT_TYPE_PERMANENT_MENU_ENTITY

	let continue_available = GET_GLOBAL_FLAG (continue_game_available)
	if continue_available = true then
		let temp_1 = GET_SAVE_FILE_EXISTS (continue_game_save_tag)
		let continue_available = temp_1
		set_global_flag (continue_game_available, continue_available)
	end_if
	set_global_flag (continue_game_available, continue_available)
	let option_y_co_ord = 200
	let menu_option_gap = 23

	let selector_id = CONST_UNSET

	spawn_entity main_menu_selector,0,0,process_next
	let selector_id = last_child
	let selector_id.BASE_Y = option_y_co_ord
	let selector_id.OPTION_HEIGHT = menu_option_gap
	let selector_id.MAXIMUM_OPTIONS = 7
	if continue_available = true then
		let disabled_option = CONST_UNSET
	else
		let disabled_option = 2
	end_if
	let selector_id.OPTION_WIDTH = 384
	let selector_id.DISABLED_OPTION = disabled_option

	SPAWN_ENTITIES_FROM_TEXT (FALSE, 0, menu_option_text, MAIN_MENU_OPTION_1_NUMBER, process_next, NO_CENTER, 160, 200, 0, 0, font[arb])
	SPAWN_ENTITIES_FROM_TEXT (FALSE, 0, menu_option_text, MAIN_MENU_OPTION_1_LABEL, process_next, RIGHT_ALIGN, 480, 200, 0, 0, font[arb])
	SPAWN_ENTITIES_FROM_TEXT (FALSE, 0, menu_option_text, MAIN_MENU_OPTION_2_NUMBER, process_next, NO_CENTER, 160, 223, 0, 0, font[arb])
	SPAWN_ENTITIES_FROM_TEXT (FALSE, 0, menu_option_text, MAIN_MENU_OPTION_2_LABEL, process_next, RIGHT_ALIGN, 480, 223, 0, 0, font[arb])
	if continue_available = true then
		SPAWN_ENTITIES_FROM_TEXT (FALSE, 0, menu_option_text, MAIN_MENU_OPTION_3_NUMBER, process_next, NO_CENTER, 160, 246, 0, 0, font[arb])
		SPAWN_ENTITIES_FROM_TEXT (FALSE, 0, menu_option_text, MAIN_MENU_OPTION_3_LABEL, process_next, RIGHT_ALIGN, 480, 246, 0, 0, font[arb])
	else
		SPAWN_ENTITIES_FROM_TEXT (FALSE, 0, menu_option_text_disabled, MAIN_MENU_OPTION_3_NUMBER, process_next, NO_CENTER, 160, 246, 0, 0, font[arb])
		SPAWN_ENTITIES_FROM_TEXT (FALSE, 0, menu_option_text_disabled, MAIN_MENU_OPTION_3_LABEL, process_next, RIGHT_ALIGN, 480, 246, 0, 0, font[arb])
	end_if
	SPAWN_ENTITIES_FROM_TEXT (FALSE, 0, menu_option_text, MAIN_MENU_OPTION_4_NUMBER, process_next, NO_CENTER, 160, 269, 0, 0, font[arb])
	SPAWN_ENTITIES_FROM_TEXT (FALSE, 0, menu_option_text, MAIN_MENU_OPTION_4_LABEL, process_next, RIGHT_ALIGN, 480, 269, 0, 0, font[arb])
	SPAWN_ENTITIES_FROM_TEXT (FALSE, 0, menu_option_text, MAIN_MENU_OPTION_5_NUMBER, process_next, NO_CENTER, 160, 292, 0, 0, font[arb])
	SPAWN_ENTITIES_FROM_TEXT (FALSE, 0, menu_option_text, MAIN_MENU_OPTION_5_LABEL, process_next, RIGHT_ALIGN, 480, 292, 0, 0, font[arb])
	SPAWN_ENTITIES_FROM_TEXT (FALSE, 0, menu_option_text, MAIN_MENU_OPTION_6_NUMBER, process_next, NO_CENTER, 160, 315, 0, 0, font[arb])
	SPAWN_ENTITIES_FROM_TEXT (FALSE, 0, menu_option_text, MAIN_MENU_OPTION_6_LABEL, process_next, RIGHT_ALIGN, 480, 315, 0, 0, font[arb])
	SPAWN_ENTITIES_FROM_TEXT (FALSE, 0, menu_option_text, MAIN_MENU_CONTINUE_OPTION_7_NUMBER, process_next, NO_CENTER, 160, 338, 0, 0, font[arb])
	SPAWN_ENTITIES_FROM_TEXT (FALSE, 0, menu_option_text, MAIN_MENU_CONTINUE_OPTION_7_LABEL, process_next, RIGHT_ALIGN, 480, 338, 0, 0, font[arb])
	SPAWN_ENTITIES_FROM_TEXT (FALSE, 0, menu_version_stamp_text, MENU_VERSION_LABEL, process_next, RIGHT_ALIGN, 480, 372, 0, 0, font_small[arb])

start

.start_main_loop

	// Now we just wait...

	IF_INPUT_KEYBOARD_HIT key_1 then
		set_global_flag (game_mode_single_player,TRUE)
		goto start_game
	end_if

	IF_INPUT_KEYBOARD_HIT key_2 then
		set_global_flag (game_mode_single_player,FALSE)
		goto start_game
	end_if

	IF_INPUT_KEYBOARD_HIT key_3 then
		if continue_available = true then
			goto continue_game
		end_if
	end_if

	IF_INPUT_KEYBOARD_HIT key_4 then
		goto select_options
	end_if

	IF_INPUT_KEYBOARD_HIT key_5 then
		goto show_hiscores
	end_if

	IF_INPUT_KEYBOARD_HIT key_6 then
		goto show_wiztips
	end_if

	IF_INPUT_KEYBOARD_HIT key_7 then
		goto quit_game
	end_if

	IF_INPUT_KEYBOARD_HIT key_esc then
		goto quit_game
	end_if

	IF_DEBUG_MODE then
		IF_INPUT_KEYBOARD_HIT key_F1 then
			let temp_1 = get_global_flag (record_games)

			if (temp_1 = true) then
				set_global_flag (record_games, false)
			else
				set_global_flag (record_games, true)
			end_if
		END_IF
	END_IF

	IF (selector_id != CONST_UNSET) then
		IF (selector_id.option_has_been_selected = TRUE) then
			switch selector_id.OPTION_NUMBER_HIGHLIGHTED
				case 0
					set_global_flag (game_mode_single_player,TRUE)
					goto start_game
				break

				case 1
					set_global_flag (game_mode_single_player,FALSE)
					goto start_game
				break

				case 2
					if continue_available = true then
						goto continue_game
					end_if
				break

				case 3
					goto select_options
				break

				case 4
					goto show_hiscores
				break

				case 5
					goto show_wiztips
				break

				case 6
					goto quit_game
				break
			end_switch
		END_IF
	END_IF

end



.continue_game

	if_flag (continue_game_available = false) then
		PLAY_SOUND menu_select_back 255 CONST_SOUND_FREQUENCY 128
		goto start_main_loop
	end_if

	PLAY_SOUND menu_select 255 CONST_SOUND_FREQUENCY 128

		let temp_1 = GET_GLOBAL_FLAG (save_load_spinner_entity_id)
		if temp_1 = CONST_UNSET then
			spawn_entity (save_load_spinner,0,0,process_last)
		end_if

		enable_window SCORE_OVERLAY_WINDOW
		wait 6

		spawn_entity continue_game,0,0,process_next

	if_flag (playing_music_channel_flag) != CONST_UNSET then
		spawn_entity (fade_music,0,0,process_next)
		let last_child.passed_param_1 = GET_GLOBAL_FLAG (playing_music_channel_flag)
		let last_child.passed_param_2 = GET_GLOBAL_FLAG (playing_music_stream_flag)
		let last_child.passed_param_3 = 15
		let last_child.passed_param_4 = 255

		SET_GLOBAL_FLAG (playing_music_channel_flag, CONST_UNSET)
		SET_GLOBAL_FLAG (playing_music_stream_flag, CONST_UNSET)
	end_if

	alter_entities ( ALTERATION_KILL , ENTITY_TYPE & ENT_TYPE_SPECIFIC_MENU_ENTITY )
	alter_entities ( ALTERATION_KILL , ENTITY_TYPE & ENT_TYPE_PERMANENT_MENU_ENTITY )

	end



.start_game

	PLAY_SOUND menu_select 255 CONST_SOUND_FREQUENCY 128

	spawn_entity start_game,0,0,process_next

	if_flag (playing_music_channel_flag) != CONST_UNSET then
		spawn_entity (fade_music,0,0,process_next)
		let last_child.passed_param_1 = GET_GLOBAL_FLAG (playing_music_channel_flag)
		let last_child.passed_param_2 = GET_GLOBAL_FLAG (playing_music_stream_flag)
		let last_child.passed_param_3 = 15
		let last_child.passed_param_4 = 255

		SET_GLOBAL_FLAG (playing_music_channel_flag, CONST_UNSET)
		SET_GLOBAL_FLAG (playing_music_stream_flag, CONST_UNSET)
	end_if

	alter_entities ( ALTERATION_KILL , ENTITY_TYPE & ENT_TYPE_SPECIFIC_MENU_ENTITY )
	alter_entities ( ALTERATION_KILL , ENTITY_TYPE & ENT_TYPE_PERMANENT_MENU_ENTITY )

	end



.select_options

	PLAY_SOUND menu_select 255 CONST_SOUND_FREQUENCY 128

	spawn_entity menu_options,0,0,process_next

	alter_entities ( ALTERATION_KILL , ENTITY_TYPE & ENT_TYPE_SPECIFIC_MENU_ENTITY )

	let signal = signal OR DESTROY_OPTION_TEXT
	let signal = signal OR SLEEP_CREDITS
	let signal = signal AND !WAKE_CREDITS

	kill_entity own_id

	end



.show_hiscores

	PLAY_SOUND menu_select 255 CONST_SOUND_FREQUENCY 128

	spawn_entity menu_hiscore_reading,0,0,process_next

	alter_entities ( ALTERATION_KILL , ENTITY_TYPE & ENT_TYPE_SPECIFIC_MENU_ENTITY )

	let signal = signal OR DESTROY_OPTION_TEXT

	kill_entity own_id

	end



.show_wiztips

	spawn_entity menu_wiztips,0,0,process_next

	alter_entities ( ALTERATION_KILL , ENTITY_TYPE & ENT_TYPE_SPECIFIC_MENU_ENTITY )

	let signal = signal OR DESTROY_OPTION_TEXT

	kill_entity own_id

	end



.quit_game

	PLAY_SOUND menu_select_back 255 CONST_SOUND_FREQUENCY 128

	SAVE_HISCORE_TABLE (MAIN_HISCORE_TABLE,HISCORE_FILENAME_TAG)

	if_flag (playing_music_channel_flag) != CONST_UNSET then
		spawn_entity (fade_music,0,0,process_next)
		let last_child.passed_param_1 = GET_GLOBAL_FLAG (playing_music_channel_flag)
		let last_child.passed_param_2 = GET_GLOBAL_FLAG (playing_music_stream_flag)
		let last_child.passed_param_3 = 15
		let last_child.passed_param_4 = 255

		SET_GLOBAL_FLAG (playing_music_channel_flag, CONST_UNSET)
		SET_GLOBAL_FLAG (playing_music_stream_flag, CONST_UNSET)
	end_if

//	let signal = signal OR DESTROY_OPTION_TEXT
//	let signal = signal OR SLEEP_CREDITS
//	let signal = signal OR SLEEP_SCROLLY
//	let signal = signal AND !WAKE_SCROLLY
//	let signal = signal AND !WAKE_CREDITS

	spawn_entity (save_config)
	spawn_entity (save_unlocked_tunes)

	ENABLE_WINDOW_VERTEX_COLOUR

	let x_scale_counter = 10000
	let y_scale_counter = 10000
	let window_brightness = 0

	start

		SET_WINDOW_VERTEX_COLOUR window_brightness window_brightness window_brightness

		set_window_scale window_number x_scale_counter y_scale_counter 5000 5000

		let window_brightness = window_brightness + 5 !> 255

		let y_scale_counter = y_scale_counter % 8000 !< 45

		if y_scale_counter = 45 then
			let x_scale_counter = x_scale_counter % 8000 !< 35
			if x_scale_counter = 35 then
				if window_brightness = 255 then
					gosub finish_and_quit
				end_if
			end_if
		end_if
	end



.finish_and_quit

	wait 15

	CLOSE_DOWN_SYSTEM

	start

	end
