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

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

--####################################################################################################
--############################### QUETE 2 : LE ROYAUME DES LUCIOLES ##################################
--####################################################################################################

------------------------------------------------------------------------------------------------------
------------------------Functions Part 1 : Nourrir les lucioles de fruits-----------------------------
------------------------------------------------------------------------------------------------------

function F_firefly_kingdom_first_clic()
	PopElementTask("TASK_go_firefly")
	if IsFXPlaying(Air, "fx/fx_boutique_4.lua", "Luciole") then
		KillFX(Air, "fx/fx_boutique_4.lua", "Luciole")
	end
	
	DisplayInformation( "firefly_tree_fruit_start_text", "backgrounds/dialog/vignette_luciole", "", 0, "", true )
end

function F_firefly_tree_fruit_end()
	SetVisible("O_firefly_tree_sick_A",false)
	SetVisible("O_firefly_tree_sick_B",false)
	SetVisible("O_firefly_tree_healthy_A",true)
	SetVisible("O_firefly_tree_healthy_B",true)
	DisplayInformation( "firefly_tree_fruit_end_text", "backgrounds/dialog/vignette_luciole", "", 0, "F_firefly_tree_fruit_end_fairy_give_hint", true )
	DisplayQuestElementInTome( "Q_swamp_sage_lookingafter", true )
	SetFirstClickable("LD_mushroom_light",true)
	SetFirstClickable("LD_fairy_light_01",true)
	SetFirstClickable("LD_fairy_light_02",true)
	SetFirstClickable("LD_fairy_light_03",true)
	PopElementTask("TASK_feed_firefly")
	PopElementTask("TASK_feed_firefly_ongoing")
	
end

function F_firefly_tree_fruit_end_fairy_give_hint()

	if not IsQuestCompleted("Q_orchard_seeds") then	
		DisplayInformation( "firefly_tree_fruit_end_next", "backgrounds/dialog/vignette_fee", "", 0, "F_firefly_tree_new_task", true )
	else
		DisplayInformation( "firefly_tree_fruit_end_no_next", "backgrounds/dialog/vignette_fee", "", 0, "", true )
	end
end

function F_firefly_tree_new_task()
	if not IsQuestCompleted("Q_orchard_seeds") then	
		PushTask("TASK_grow_seeds","TASK_grow_seeds",5)
	end
end

function F_firefly_tree_fruit_finished()
	
	if GetFilledPercent("LD_firefly_tree_fruit") >= 100 then
		return true
	else
		return false
	end	
end

function F_firefly_tree_fruit_condition_begin()
	return not IsQuestElementCompleted( "Q_firefly_kingdom_fruit_feeding" )
end

function F_firefly_tree_fruit_begin()
	PushTask("TASK_feed_firefly_ongoing","TASK_feed_firefly_ongoing",3.9)	
end

function F_firefly_tree_fruit_cancel()
	if GetNumberOfUsersUsingQuestElement("Q_firefly_kingdom_fruit_feeding") <= 0 then
		PopElementTask("TASK_feed_firefly_ongoing")
		MoveFairyToTaskBar()
	end
	return true
end

------------------------------------------------------------------------------------------------------
--------------------Functions Part 2 : Nourrir les lucioles de sirop d'or-----------------------------
------------------------------------------------------------------------------------------------------

function F_firefly_last_crusade_condition_end()
	check = false
	if GetFilledPercent( "LD_firefly_tree_last_crusade" ) >= 100 then
		check = true
	end
	return check
end

function F_firefly_last_crusade_getprogress()
	return 100 - GetFilledPercent( "LD_firefly_tree_last_crusade" )
end

function F_firefly_last_crusade_first_time()
	SetTipToShow( "TIP_secondary_quests" )
end

function F_firefly_last_crusade_condition_begin()
	return not IsQuestElementCompleted( "Q_firefly_kingdom_golden_syrup_feeding" )
end

function F_firefly_last_crusad_feeding_end()
	SetTipToShow( "TIP_banquet" )
	DisplayInformation( "firefly_syrup_end", "backgrounds/dialog/vignette_luciole", "", 0, "F_firefly_last_crusad_feeding_end_new_task", true )
	PopElementTask("TASK_firefly_syrup");
end

function F_firefly_last_crusad_feeding_end_new_task()
	DisplayQuestElementInTome( "Q_firefly_kingdom_lamp_fixing", true )
	
	if not IsQuestElementCompleted("Q_legendary_melody_xylophone_cleaning") then
		SetFirstClickable( "LD_xylophone",true )
		PlayFX(Air, "fx/fx_boutique_4.lua", "LD_xylophone", 0, 0)
		PushTask("TASK_find_xylophone","TASK_find_xylophone",17)
		PushTask("TASK_clean_xylophone","TASK_clean_xylophone",18)
	end
end

------------------------------------------------------------------------------------------------------
-------------------------------Functions Part 3 : Réparer la lampe -----------------------------------
------------------------------------------------------------------------------------------------------
function F_firefly_lamp_fixing_begin( sOwnerName )
	PlayRepairSound( sOwnerName );
	return true
end

function F_firefly_lamp_fixing_retrieve( sOwnerName )
	PlayRepairFX( sOwnerName );
	return true
end

function F_firefly_lamp_fixing_stop( sOwnerName )
	KillRepairSound( sOwnerName );
	KillRepairFX( sOwnerName );
	return true
end

function F_firefly_lamp_fixing_end()
	SetVisible( "LD_firefly_lamp", false );
	SetVisible( "LD_firefly_lamp_repaired", true );
	DisplayQuestElementInTome( "Q_firefly_kingdom_guiding", true )
	PopElementTask("TASK_firefly_light_ongoing")
	return true
end

function F_firefly_lamp_begin()
	PushTask("TASK_firefly_light_ongoing","TASK_firefly_light_ongoing",18.9)	
end

function F_firefly_lamp_cancel()
	if GetNumberOfUsersUsingQuestElement("Q_firefly_kingdom_lamp_fixing") <= 0 then
		PopElementTask("TASK_firefly_light_ongoing")
		MoveFairyToTaskBar()
	end
	return true
end

------------------------------------------------------------------------------------------------------
-------------------------------Functions Part 4 : Guider les lucioles---------------------------------
------------------------------------------------------------------------------------------------------

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

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

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

function F_firefly_last_crusade_guide_end( sOwnerName )
	SetVisible("LD_firefly_lamp_repaired",false)
	SetVisible("LD_firefly_lamp_shiny",true)

	FireflyFollow( "" )
	SetPosition( "Luciole", 1834, 1566 )
	
	SetFireflyGatherStockName( "LD_firefly_tree_stock_after" );

	PopElementTask("TASK_firefly_light")
	if not IsQuestCompleted ("Q_mirrors") then
		PushTask("TASK_clean_mirrors","TASK_clean_mirrors",20)
	end
	
	if IsQuestElementCompleted("Q_mirror_a_fixing") then
		SetVisible( "LD_mirror_A_repaired", false )
	end
	
	if IsQuestElementCompleted("Q_mirror_b_fixing") then
		SetVisible( "LD_mirror_B_repaired", false )
	end
	
	if IsQuestElementCompleted("Q_mirror_c_fixing") then
		SetVisible( "LD_mirror_C_repaired", false )
	end
	
	if IsQuestElementCompleted("Q_mirror_d_fixing") then
		SetVisible( "LD_mirror_D_repaired", false )
	end
	
	if IsQuestElementCompleted("Q_mirror_e_fixing") then
		SetVisible( "LD_mirror_E_repaired", false )
	end
	
	if IsQuestElementCompleted("Q_mirror_f_fixing") then
		SetVisible( "LD_mirror_F_repaired", false )
	end

	return true
end

function F_firefly_last_crusade_guide_begin( sOwnerName )
	--PlayFX( Air, "fx/lucioles.lua", tGuiders[1], 0, 0 );
	--KillFX( Air, "fx/lucioles.lua", "LD_firefly_tree_fruit" );
	if( ( GetNumberOfUsersUsingQuestElement("Q_firefly_kingdom_guiding") >= G_firefly_last_crusade_guiding_min_users ) and ( not GetFireflyFollow() == "" ) ) then
		local tGuiders = GetQuestElementUsers( "Q_firefly_kingdom_guiding" );
		SetPosition( "Luciole", 1835, 112 );
		FireflyFollow( tGuiders[1] );
	end	
	return true;
end

function F_firefly_last_crusade_guide_cancel( sOwnerName )
	if( GetNumberOfUsersUsingQuestElement("Q_firefly_kingdom_guiding") < G_firefly_last_crusade_guiding_min_users ) then
		FireflyFollow( "" );
		SetPosition( "Luciole", 1926, 59 );
	end
	return true;
end

function F_firefly_last_crusade_guiding_condition_begin()
	return not IsQuestElementCompleted( "Q_firefly_kingdom_guiding" )
end

function F_firefly_last_crusade_guiding_condition_end()
	check = false
	if GetFilledPercent( "LD_firefly_lamp_destination" ) <= 0 then
		check = true
	end
	return check
end

function F_firefly_last_crusade_guide_getprogress()
	return GetFilledPercent( "LD_firefly_lamp_destination" );
end

function F_firefly_last_crusade_guide_update()
	if( not IsCharacterUsingQuestElement( "Q_firefly_kingdom_guiding", GetFireflyFollow() ) ) then
		if( GetNumberOfUsersUsingQuestElement("Q_firefly_kingdom_guiding") >= G_firefly_last_crusade_guiding_min_users ) then
			local tGuiders = GetQuestElementUsers( "Q_firefly_kingdom_guiding" );
			FireflyFollow( tGuiders[1] );
		end
	end
	return true
end

function F_firefly_kingdom_end()
	EndGame()
end

--======================================================================================================
--=========================================Quest Description============================================
--======================================================================================================

Quest
{
	name = "Q_firefly_kingdom",
	description = "Q_firefly_kingdom_desc",
	
	pnj = "Luciole",
	
	callback_bestiary = {"bestiary_page4"},
  callback_end = "F_firefly_kingdom_end",
	
	---------------------------Part 1 : Nourrir les lucioles de fruits ---------------------
	Quest_Element
	{
		name = "Q_firefly_kingdom_fruit_feeding",
		zone = "Z_firefly_tree",
		
		description_done = "Q_firefly_kingdom_fruit_feeding_done",
		
		prerequisites  = {"Q_world_history_ruins_learning"},
		
		Check
		{
		  value_type = "nature",
		  level = 1 * G_check_factor,
		},
		
		
		
		gain_feerie = G_firefly_fruit_feed_faerie_gain,
	  
		
		interaction = "LD_firefly_tree_fruit",
		max_users = 4,
		
		callback_firstclic = "F_firefly_kingdom_first_clic",
		condition_begin = "F_firefly_tree_fruit_condition_begin",
		condition_end = "F_firefly_tree_fruit_finished",
		callback_begin = "F_firefly_tree_fruit_begin",
		callback_cancel = "F_firefly_tree_fruit_cancel",
		callback_end = "F_firefly_tree_fruit_end",
		--callback_getprogress = "F_firefly_tree_fruit_getprogress",
		
		loop = true,
		stay_where_dropped = false,
		
		see_x = 1756,
		see_y = 59,
		
		Gather
		{
			stay_where_dropped = false,
			source = "id_stock_nourriture_gather",
			CompetenceUsed = Nature,
			collectTime = 5, 
			gather_factor  = 5,
			ratio_bonus_feerie = G_faerie_standard_gain_per_action,
		},

		Deposite
		{
			label = "feeding",
			stock = "LD_firefly_tree_fruit",
			fixTime = 5,
		},
	},
	
	--------------------------- Part 2 : Nourrir les lucioles de sirop d'or ---------------------
	Quest_Element
	{
		name = "Q_firefly_kingdom_golden_syrup_feeding",
		description_done = "Q_firefly_kingdom_golden_syrup_feeding_done",
		
		prerequisites  = {"Q_firefly_kingdom_fruit_feeding","Q_golden_syrup_distilling"},
		zone = "Z_firefly_tree",

		Check
		{
			value_type = "nature",
			level = 4 * G_check_factor,
		},
	
		Check
		{
			is_for_knowledge_tree = true,
			value_type = "nature",
			level = 2 * G_check_factor,
		},
		
		gain_feerie = G_firefly_syrup_feed_faerie_gain,

		
		interaction = "LD_firefly_tree_fruit",
		max_users = 4,
		min_users = 2 * G_check_factor,
	
		condition_end = "F_firefly_last_crusade_condition_end",
		condition_begin = "F_firefly_last_crusade_condition_begin",
		callback_begin = "F_firefly_last_crusade_first_time",
		callback_end = "F_firefly_last_crusad_feeding_end",
		callback_getprogress = "F_firefly_last_crusade_getprogress",
	
		loop = true,
		stay_where_dropped = false,
	
		see_x = 1756,
		see_y = 59,
	
		Gather
		{
			stay_where_dropped = false,
			source = "LD_golden_syrup",
			CompetenceUsed = Nature,
			collectTime = 5, 
			gather_factor  = 10,
			ratio_bonus_feerie = G_faerie_standard_gain_per_action,
		},

	
		Deposite
		{
			label = "Q_firefly_last_crusade_feeding",
			stock = "LD_firefly_tree_last_crusade",
			fixTime = 5,
		},
	},
	-------------------------------Part 3 : Réparer la lampe -----------------------------------

	Quest_Element
	{
		name = "Q_firefly_kingdom_lamp_fixing",
		description_done = "Q_firefly_kingdom_lamp_fixing_done",
		zone = "Z_firefly_kingdom",
		
		Check
		{
			value_type = "knowledge",
			level = 4 * G_check_factor,
		},

		Check
		{
			is_for_knowledge_tree = true,
			value_type = "knowledge",
			level = 2 * G_check_factor,
		},
		
		gain_feerie = G_firefly_lamp_fix_faerie_gain,
  
		
		interaction = "LD_firefly_lamp",

		callback_end = "F_firefly_lamp_fixing_end",
		callback_begin = "F_firefly_lamp_begin",
		callback_cancel = "F_firefly_lamp_cancel",

		max_users = 6,
		min_users = 2 * G_check_factor,
		loop = true,

		Fix
		{
			stay_where_dropped = false,
			interaction = "LD_firefly_lamp",
			label = "fixing",
			XpImproved = Knowledge,
			XpValue = G_xp_standard_gain_per_action,
			fixTime = 5,
			fix_factor = 10,
			ratio_bonus_feerie = G_faerie_standard_gain_per_action,
			callback_inprogress = "F_firefly_lamp_fixing_begin",
			callback_done = "F_firefly_lamp_fixing_stop",
			callback_cancel = "F_firefly_lamp_fixing_stop",
			callback_retrieve_inprogress = "F_firefly_lamp_fixing_retrieve",
		},
	},
	-------------------------------Part 4 : Guider les lucioles---------------------------------
	Quest_Element
	{
		name = "Q_firefly_kingdom_guiding",
		description_done = "Q_firefly_kingdom_guiding_done",
		zone = "Z_firefly_kingdom",
		
		prerequisites = {"Q_firefly_kingdom_lamp_fixing","Q_firefly_kingdom_golden_syrup_feeding","Q_legendary_melody_xylophone_cleaning","Q_legendary_melody_learning"},
		
		Check
		{
			value_type = "music",
			level = 4 * G_check_factor,
		},
	
		Check
		{
			is_for_knowledge_tree = true,
			value_type = "music",
			level = 2 * G_check_factor,
		},
		
		gain_feerie = G_firefly_guiding_faerie_gain,

		
		interaction = "LD_firefly_tree_fruit",
		max_users = 4,
		min_users = G_firefly_last_crusade_guiding_min_users,

		condition_begin = "F_firefly_last_crusade_guiding_condition_begin",
		condition_end = "F_firefly_last_crusade_guiding_condition_end",
		-- callback_first_time = "F_firefly_tree_fruit_start",
		--callback_getprogress = "F_firefly_last_crusade_getprogress",
		callback_getprogress = "F_firefly_last_crusade_guide_getprogress",
		callback_begin = "F_firefly_last_crusade_guide_begin",
		callback_cancel = "F_firefly_last_crusade_guide_cancel",
		callback_end = "F_firefly_last_crusade_guide_end",

		callback_update = "F_firefly_last_crusade_guide_update",

		stay_where_dropped = false,

		object_to_see = "LD_firefly_lamp_destination",

		Musique
		{
			stay_where_dropped = false,
			interaction = "LD_firefly_lamp_destination",
			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_music_firefly_stop",
			callback_cancel = "F_music_firefly_stop",
			callback_retrieve_inprogress = "F_music_firefly_retrieve",
			callback_inprogress = "F_music_firefly_inprogress"
		},
	},
}
