// 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 music_vu_bar_entity		v4
#define sound_vu_bar_entity		v5
#define gfx_indicator_entity		v6
#define particle_indicator_entity	v7

#define sound_volume			v8
#define music_volume			v9
#define gfx_mode			v10
#define particle_count			v11

#define restart_warning_visible		v12

//#define gfx_depth			v14
//#define gfx_window			v15
//#define gfx_indicator_entity_depth	v16
//#define gfx_indicator_entity_window	v17

	let world_x = 0
	let world_y = 0
	set_private_co_ords

	let restart_warning_visible = false

	let gfx_mode = get_global_flag (display_mode_flag)
	let particle_count = get_global_flag (particle_commonness_multiplier)

	let sound_volume = GET_GLOBAL_SOUND_VOLUME
	let music_volume = GET_GLOBAL_MUSIC_VOLUME

	let signal = signal OR SLEEP_CREDITS
	let signal = signal OR WAKE_SCROLLY
	let signal = signal OR WAKE_LOGO
	let signal = signal OR SLEEP_TUNNEL_LIGHT
	let signal = signal OR SLEEP_WIZ_AND_CAT

	// 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

	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 option_y_co_ord = OPTION_MENU_OPTION_TOP_Y_CO_ORD

	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_HEIGHT
	let selector_id.MAXIMUM_OPTIONS = 8
	let selector_id.OPTION_WIDTH = 528

	let music_vu_bar_entity = CONST_UNSET
	let sound_vu_bar_entity = CONST_UNSET

	let option_text = SUB_OPTION_1

	for counter = 0 to 8
		SPAWN_ENTITIES_FROM_TEXT (FALSE, 0, menu_option_text, option_text, process_next, NO_CENTER, 88, option_y_co_ord, 0, 0, font[arb])
		let option_text = option_text + 1
		SPAWN_ENTITIES_FROM_TEXT (FALSE, 0, menu_option_text, option_text, process_next, RIGHT_ALIGN, 552, option_y_co_ord, 0, 0, font[arb])
		let option_text = option_text + 1

		if counter = 2 then
			spawn_entity menu_option_indicator,523,option_y_co_ord,process_next
			let music_vu_bar_entity = last_child
			let music_vu_bar_entity.current_frame = music_volume / 1000
			let music_vu_bar_entity.current_frame = music_vu_bar_entity.current_frame + 3
		else_if counter = 3 then
			spawn_entity menu_option_indicator,523,option_y_co_ord,process_next
			let sound_vu_bar_entity = last_child
			let sound_vu_bar_entity.current_frame = sound_volume / 1000
			let sound_vu_bar_entity.current_frame = sound_vu_bar_entity.current_frame + 3
		else_if counter = 4 then
			spawn_entity menu_option_indicator,523,option_y_co_ord,process_next
			let gfx_indicator_entity = last_child
			let gfx_indicator_entity.current_frame = 14 + gfx_mode
		else_if counter = 5 then
			spawn_entity menu_option_indicator,523,option_y_co_ord,process_next
			let particle_indicator_entity = last_child
			let particle_indicator_entity.current_frame = 17 + particle_count
		end_if

		let option_y_co_ord = option_y_co_ord + MENU_OPTION_HEIGHT
	next

	let music_vu_bar_entity.INTERPOLATION_X_PERCENTAGE = music_volume
	let sound_vu_bar_entity.INTERPOLATION_X_PERCENTAGE = sound_volume

start

	// Now we just wait...

	IF_INPUT_KEYBOARD_HIT key_1 then
		goto define_keys
	end_if

	IF_INPUT_KEYBOARD_HIT key_2 then
		goto define_nifta_keys
	end_if

	IF_INPUT_KEYBOARD_HIT key_3 then
		gosub alter_music_volume
	end_if

	IF_INPUT_KEYBOARD_HIT key_4 then
		gosub alter_sfx_volume
	end_if

	IF_INPUT_KEYBOARD_HIT key_5 then
		gosub alter_gfx_mode
	end_if

	IF_INPUT_KEYBOARD_HIT key_6 then
		gosub alter_particle_count
	end_if

	IF_INPUT_KEYBOARD_HIT key_7 then
		gosub music_test
	end_if

	IF_INPUT_KEYBOARD_HIT key_8 then
		goto main_menu
	end_if

	IF_INPUT_KEYBOARD_HIT key_esc then
		goto main_menu
	end_if



	IF (selector_id.option_has_been_selected = TRUE) then
		switch selector_id.OPTION_NUMBER_HIGHLIGHTED
			case 0
				goto define_keys
			break

			case 1
				goto define_nifta_keys
			break

			case 2
				gosub alter_music_volume
			break

			case 3
				gosub alter_sfx_volume
			break

			case 4
				gosub alter_gfx_mode
			break

			case 5
				gosub alter_particle_count
			break

			case 6
				gosub music_test
			break

			case 7
				goto main_menu
			break
		end_switch
	END_IF

end





.define_keys

	PLAY_SOUND menu_select 255 CONST_SOUND_FREQUENCY 128

	spawn_entity menu_define_keys,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

.define_nifta_keys

	PLAY_SOUND menu_select 255 CONST_SOUND_FREQUENCY 128

	spawn_entity menu_define_nifta_keys,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



.music_test

	// The Music Test option is inaccessable if you have the music level set to 0. Booyah!

	if music_volume = 0 then
		PLAY_SOUND menu_select_bad 255 CONST_SOUND_FREQUENCY 128
		return
	else
		PLAY_SOUND menu_select 255 CONST_SOUND_FREQUENCY 128
		spawn_entity menu_music_test,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_if



.alter_music_volume

	PLAY_SOUND menu_select 255 CONST_SOUND_FREQUENCY 128

	let music_volume = music_volume + 1000 !< 0 !> 10999

	let music_vu_bar_entity.current_frame = music_volume / 1000
	let music_vu_bar_entity.current_frame = music_vu_bar_entity.current_frame + 3

	sET_GLOBAL_MUSIC_VOLUME music_volume

	let playing_music_channel = get_global_flag (playing_music_channel_flag)

	if playing_music_channel != const_unset then
		ADJUST_MUSIC_STREAM playing_music_channel 255 CONST_MUSIC_FREQUENCY 128
	else
		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

	return



.alter_sfx_volume

	PLAY_SOUND menu_select 255 CONST_SOUND_FREQUENCY 128

	let sound_volume = sound_volume + 1000 !< 0 !> 10999

	let sound_vu_bar_entity.current_frame = sound_volume / 1000
	let sound_vu_bar_entity.current_frame = sound_vu_bar_entity.current_frame + 3

	sET_GLOBAL_SOUND_VOLUME sound_volume

	PLAY_SOUND menu_select 255 CONST_SOUND_FREQUENCY 128

	return



.alter_gfx_mode

	PLAY_SOUND menu_select 255 CONST_SOUND_FREQUENCY 128

	gosub activate_restart_warning

	let gfx_mode = gfx_mode + 1 !< 0 !> 3

	let gfx_indicator_entity.current_frame = 14 + gfx_mode

	set_global_flag (display_mode_flag,gfx_mode)

	return



//.alter_gfx_mode_depth
//
//	PLAY_SOUND menu_select 255 CONST_SOUND_FREQUENCY 128
//
//	gosub activate_restart_warning
//
//	let gfx_mode = gfx_mode + 1 !< 0 !> 3
//
//	let gfx_indicator_entity.current_frame = 14 + gfx_mode
//
//	set_global_flag (display_mode_flag,gfx_mode)
//
//	return



//.alter_gfx_mode_window
//
//	PLAY_SOUND menu_select 255 CONST_SOUND_FREQUENCY 128
//
//	gosub activate_restart_warning
//
//	let gfx_mode = gfx_mode + 1 !< 0 !> 3
//
//	let gfx_indicator_entity.current_frame = 14 + gfx_mode
//
//	set_global_flag (display_mode_flag,gfx_mode)
//
//	return



.activate_restart_warning

	if (restart_warning_visible = false) then
		SPAWN_ENTITIES_FROM_TEXT (FALSE, 0, menu_option_text, GFX_MODE_MESSAGE, process_next, CENTER_X, 320, GFX_MESSAGE_Y_CO_ORD, -1, 0, font[arb])
		let restart_warning_visible = true
		let signal = signal AND !WAKE_SCROLLY
		let signal = signal OR SLEEP_SCROLLY
	end_if

	return




.alter_particle_count

	PLAY_SOUND menu_select 255 CONST_SOUND_FREQUENCY 128

	let particle_count = particle_count + 1 !< 1 !> 3

	let particle_indicator_entity.current_frame = 17 + particle_count

	set_global_flag (particle_commonness_multiplier,particle_count)
	let temp_1 = 4 - particle_count
	set_global_flag (particle_rareness_multiplier,temp_1)

	return



.main_menu

	PLAY_SOUND menu_select_back 255 CONST_SOUND_FREQUENCY 128

	if music_volume = 0 then
		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
	end_if

	spawn_entity menu_main,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
