------------------------------------------------Variables------------------------------------------------

require( "package/quests/q_variables_define.lua" );
DebugOut( "generate main quests (file : package/quests/q_legendary_melody.lua)" );

--####################################################################################################
--############################# QUETE 4 : LA MELODIE LEGENDAIRE ######################################
--####################################################################################################

------------------------------------------------------------------------------------------------------
---------------------------------Functions Part 1 : Enchanter la muse --------------------------------
------------------------------------------------------------------------------------------------------

function F_muse_playing_end()
	SetVisible( "LD_muse", false );
	SetVisible( "LD_music_training", true );
	if IsGameInLinearTuto() then
		PopFrontTask()
	end	
	DisplayInformation( "muse_end_text", "backgrounds/dialog/vignette_muse", "", 0, "F_muse_playing_end_tuto_check", true );
	StartTalking( "Muse", 10 );
	--DisplayQuestElementInTome( "Q_legendary_melody_xylophone_cleaning", true )
end

function F_muse_playing_end_tuto_check()
	if IsGameInLinearTuto() then
		SetTutorialDrop( "", "" )
		DisplayTutorialCallback("TUTO_training_zone")
	end
end

function F_muse_playing_first_time()
	StartTalking( "Muse", 10 )
	if IsGameInLinearTuto() then
		PopFrontTask()
		SetTutorialDrop( "", "" )
	end
end

function F_muse_playing_stop( sOwnerName )
	KillMusicFX( sOwnerName )
	KillMusicSound( sOwnerName )
	return true
end

function F_muse_playing_inprogress( sOwnerName )
	PlayMusicSound( sOwnerName )
	return true
end

function F_muse_playing_retrieve( sOwnerName )
	PlayMusicFX( sOwnerName )
	return true
end

------------------------------------------------------------------------------------------------------
-----------------------------Functions Part 2 : nettoyer le Xylophone --------------------------------
------------------------------------------------------------------------------------------------------

 
 function F_xylophone_cleaning_first_click()
	if IsFXPlaying(Air, "fx/fx_boutique_4.lua", "LD_xylophone") then
		KillFX(Air, "fx/fx_boutique_4.lua", "LD_xylophone")
	end
	PopElementTask("TASK_find_xylophone")
 end
 
 -- Callback d'évaluation de l'avancement de la quete
 -- Doit renvoyer 100 pour une quete pas avancée et 0 pour une quete finie (GetFilledPercent fonctionne de meme)
function F_xylophone_getprogress()
	local progress;
	progress = GetFilledPercent( "LD_xylophone" );
	return ( progress );
end

function F_xylophone_end()
	SetVisible("O_xylo_a_dirty",false)
	SetVisible("O_xylo_b_dirty",false)
	SetVisible("O_xylo_c_dirty",false)
	SetVisible("O_xylo_d_dirty",false)
	SetVisible("O_xylo_e_dirty",false)
	SetVisible("O_xylo_f_dirty",false)
	
	SetVisible("O_xylo_a_clean",true)
	SetVisible("O_xylo_b_clean",true)
	SetVisible("O_xylo_c_clean",true)
	SetVisible("O_xylo_d_clean",true)
	SetVisible("O_xylo_e_clean",true)
	SetVisible("O_xylo_f_clean",true)
	
	SetVisible( "LD_xylophone", false )
	
	SetVisible( "LD_xylophone_learning", true )
	
	SetCleanable( "SD_instrument", true)
	DisplayQuestElementInTome( "Q_legendary_melody_learning", true )
	PopElementTask("TASK_clean_xylophone_ongoing")
	
	return true;
end

function F_legendary_melody_cleaning_begin()
	PushTask("TASK_clean_xylophone_ongoing","TASK_clean_xylophone_ongoing",17.8)
	if IsFXPlaying(Air, "fx/fx_boutique_4.lua", "LD_xylophone") then
		KillFX(Air, "fx/fx_boutique_4.lua", "LD_xylophone", 0, 0)
		end
	PopElementTask("TASK_find_xylophone")	
end

function F_legendary_melody_cleaning_cancel()
	if GetNumberOfUsersUsingQuestElement("Q_legendary_melody_xylophone_cleaning") <= 0 then
		PopElementTask("TASK_clean_xylophone_ongoing")
		MoveFairyToTaskBar()
	end
	return true
end

------------------------------------------------------------------------------------------------------
--------------------------Functions Part 3 : Apprendre la mélodie légendaire -------------------------
------------------------------------------------------------------------------------------------------
function F_legendary_melody_learning_end()
	SetVisible( "LD_xylophone_learning", false )
	DisplayQuestElementInTome( "Q_firefly_kingdom_guiding", true )
	if not IsQuestCompleted("Q_firefly_kingdom") then
		PushTask("TASK_firefly_light","TASK_firefly_light",19)
	end
	PopElementTask("TASK_clean_xylophone")
	PopElementTask("TASK_clean_xylophone_learning_ongoing")
end

function F_legendary_melody_learning_begin()
	PushTask("TASK_clean_xylophone_learning_ongoing","TASK_clean_xylophone_learning_ongoing",17.9)	
end

function F_legendary_melody_learning_cancel()
	if GetNumberOfUsersUsingQuestElement("Q_legendary_melody_learning") <= 0 then
		PopElementTask("TASK_clean_xylophone_learning_ongoing")
		MoveFairyToTaskBar()
	end
	return true
end

function F_xylophone_music_playing_retrieve( sOwnerName )
	PlayMusicFX( sOwnerName )
	return true
end

function F_xylophone_music_playing_inprogress( sOwnerName )
	PlayMusicSound( sOwnerName )
	return true
end

function F_xylophone_music_playing_stop( sOwnerName )
	if IsMusicFXPlaying( sOwnerName ) == true then
		KillMusicFX( sOwnerName )
		KillMusicSound( sOwnerName )
	end
	return true
end
--======================================================================================================
--=========================================Quest Description============================================
--======================================================================================================

Quest
{
	name = "Q_legendary_melody",
	description = "Q_legendary_melody_desc",
	
	callback_bestiary = {"bestiary_page8"},
	
	---------------------------Part 1 : Enchanter la muse ---------------------
	Quest_Element
	{
		name = "Q_legendary_melody_muse_playing", 
		description_done = "Q_legendary_melody_muse_playing_done",

		Check
		{
		  value_type = "music",
		  level = 0 * G_check_factor,
		},
		
		gain_feerie = G_muse_faerie_gain,

		interaction = "LD_muse",     -- name of the source or character that launches this quest
		callback_end = "F_muse_playing_end",
		callback_first_time = "F_muse_playing_first_time",
		must_be_seen_to_be_completed = false,

		max_users = 3,
		loop = true,

		Musique
		{
		  stay_where_dropped = false,
		  interaction = "LD_muse",
		  label = "playing",
		  
		  XpImproved = Music,
		  XpValue    = G_xp_standard_gain_per_action,
		  fixTime = 30,
		  fix_factor = 10,
		  ratio_bonus_feerie = G_faerie_standard_gain_per_action,
		  callback_done = "F_muse_playing_stop",
		  callback_cancel = "F_muse_playing_stop",
		  callback_retrieve_inprogress = "F_muse_playing_retrieve",
		  callback_inprogress = "F_muse_playing_inprogress"
		},
	},
	---------------------------Part 2 : Nettoyer le Xylophone ---------------------

	Quest_Element
	{
		name = "Q_legendary_melody_xylophone_cleaning",
		zone = "Z_xylophone",

		prerequisites = {"Q_legendary_melody_muse_playing",},

		Check
		{
			value_type = "exploration",
			level = 2 * G_check_factor,
		},
	
		Check
		{
			is_for_knowledge_tree = true,
			value_type = "exploration",
			level = 1 * G_check_factor,
		},
		
		gain_feerie = G_xylo_clean_faerie_gain,

		interaction = "LD_xylophone",
		max_users = 6,
		min_users = 2 * G_check_factor,
		loop = true,
	
		callback_firstclic = "F_xylophone_cleaning_first_click",
		callback_getprogress = "F_xylophone_getprogress",
		callback_begin = "F_legendary_melody_cleaning_begin",
		callback_cancel = "F_legendary_melody_cleaning_cancel",
		callback_end = "F_xylophone_end",
	
		Clean
		{
			stay_where_dropped = false,
			interaction = "LD_xylophone",
			XpImproved = Exploration,
			XpValue = G_xp_standard_gain_per_action,
			fixTime = 5,
			fix_factor = 10,
			ratio_bonus_feerie = G_faerie_standard_gain_per_action,
		},
	},
	---------------------------Part 3 : Apprendre la mélodie légendaire ---------------------
	Quest_Element
	{
		name = "Q_legendary_melody_learning",
		zone = "Z_xylophone",
		description_done = "Q_legendary_melody_learning_done",
		
		prerequisites = {"Q_legendary_melody_xylophone_cleaning"},

		Check
		{
			value_type = "music",
			level = 3 * G_check_factor,
		},
	
		Check
		{
			is_for_knowledge_tree = true,
			value_type = "music",
			level = 1 * G_check_factor,
		},
		
		interaction = "LD_xylophone_learning",
		max_users = 6,
		min_users = 2 * G_check_factor,
		loop = true,
		
		gain_feerie = G_xylo_learn_faerie_gain,
		
		callback_begin = "F_legendary_melody_learning_begin",
		callback_cancel = "F_legendary_melody_learning_cancel",
		callback_end = "F_legendary_melody_learning_end",
		
		Musique
		{
			stay_where_dropped = false,
			interaction = "LD_xylophone_learning",
			XpImproved = Music,
			XpValue    = G_xp_standard_gain_per_infinite_action,
			danceTime = 60,
			fixTime = 10,
			fix_factor = 10,
			ratio_bonus_feerie = G_faerie_standard_gain_per_infinite_action,
			callback_done = "F_xylophone_music_playing_stop",
			callback_cancel = "F_xylophone_music_playing_stop",
			callback_retrieve_inprogress = "F_xylophone_music_playing_retrieve",
			callback_inprogress = "F_xylophone_music_playing_inprogress",
		},
	}
}
