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

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

--####################################################################################################
--################################### QUETE 3 : LE MARAIS ############################################
--####################################################################################################
------------------------------------------------------------------------------------------------------
---------------------------------Functions Part 1 : Nettoyer le marais -------------------------------
------------------------------------------------------------------------------------------------------
function F_swamp_first_click()
	PopElementTask("TASK_go_sage")
	if IsFXPlaying(Air, "fx/fx_boutique_4.lua", "Sage") then
		KillFX(Air, "fx/fx_boutique_4.lua", "Sage")
	end
	StartTalking( "Sage", 10 )
	DisplayInformation( "swamp_sage_start_text", "backgrounds/dialog/vignette_sage", "", 0, "", true )
	return true
end

function F_swamp_first_time()
	PopElementTask("TASK_go_sage")
	return true
end

function F_swamp_begin()
	PushTask("TASK_help_sage_ongoing","TASK_help_sage_ongoing",6.9)
end

function F_swamp_cancel()
	if GetNumberOfUsersUsingQuestElement("Q_swamp_sage_lookingafter") <= 0 then
		PopElementTask("TASK_help_sage_ongoing")
		MoveFairyToTaskBar()
	end
	return true
end

function F_swamp_sage_end()

	SetVisible( "LD_swamp_sage_weed1", true )
	SetVisible( "LD_swamp_sage_weed2", true )
	SetVisible( "LD_swamp_sage_weed3", true )
	SetVisible( "LD_swamp_sage_weed4", true )
	SetVisible( "LD_swamp_sage_weed5", true )
	
	SetCleanable( "SD_fleur1", true)
	SetCleanable( "SD_fleur2", true)
	SetCleanable( "SD_fleur3", true)
	PopElementTask("TASK_help_sage")
	PopElementTask("TASK_help_sage_ongoing")
	
	DisplayInformation( "swamp_sage_end_text", "backgrounds/dialog/vignette_sage", "", 0, "F_swamp_sage_end_new_task", true )
	DisplayQuestElementInTome( "Q_swamp_watering_can_fixing", true )
	StartTalking( "Sage", 10 )

	return true
end

function F_swamp_sage_end_new_task()
	if  not IsQuestElementCompleted("Q_swamp_watering_can_fixing") and not IsQuestElementBegin("Q_swamp_watering_can_fixing") then
		PushTask("TASK_find_watering_can","TASK_find_watering_can",8)
		PushTask("TASK_irrigate_swamp","TASK_irrigate_swamp",9)
		SetFirstClickable("LD_watering_can",true)
		PlayFX(Air, "fx/fx_boutique_4.lua", "LD_watering_can", 0, 0)
	end
end

------------------------------------------------------------------------------------------------------
---------------------------------Functions Part 2 : Irriguer le marais -------------------------------
------------------------------------------------------------------------------------------------------
function F_watering_can_fixing_first_click()
	PopElementTask("TASK_find_watering_can")
	if IsFXPlaying(Air, "fx/fx_boutique_4.lua", "LD_watering_can") then
		KillFX(Air, "fx/fx_boutique_4.lua", "LD_watering_can")
	end
	DisplayHelpInformation( "TIP_nb_character_on_quest", "backgrounds/dialog/vignette_fee", "", 0, "", true )
end

function F_watering_can_fixing_begin()
	PopElementTask("TASK_find_watering_can")
	PushTask("TASK_irrigate_swamp_ongoing","TASK_irrigate_swamp_ongoing",6.9)
end

function F_watering_can_cancel()
	if GetNumberOfUsersUsingQuestElement("Q_swamp_watering_can_fixing") <= 0 then
		PopElementTask("TASK_irrigate_swamp_ongoing")
		MoveFairyToTaskBar()
	end
	return true
end

function F_watering_can_fixing_inprogress( sOwnerName )
	PlayRepairSound( sOwnerName );
	return true
end

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

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

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

function F_watering_can_fixing_end()
	
	SetVisible("LD_flower_left", false);
	SetVisible("LD_flower_right", false);
	SetVisible("LD_watering_can_anim", true);
	SetVisible("LD_beeflower", true);
	SetAnim("LD_beeflower_anim", 1);
	SetVisible("LD_watering_can_cork",false);
	SetVisible("LD_dry_swamp_A",false);
	SetVisible("LD_dry_swamp_B",false);
	SetVisible("O_flower_left_B",true);
	SetVisible("O_flower_left_rotten",false);
	SetVisible("O_flower_right_A",true);
	SetVisible("O_flower_right_rotten",false);
	SetVisible("LD_waterploc_01",true);
	SetVisible("LD_waterploc_02",true);
	SetVisible("LD_waterploc_03",true);
	SetVisible("LD_waterploc_04",true);
	SetVisible("LD_waterploc_05",true);
	SetVisible("LD_waterploc_06",true);
	SetVisible("LD_waterploc_07",true);
	SetVisible("LD_waterploc_08",true);
	SetVisible("LD_waterploc_09",true);
	SetVisible("LD_waterploc_10",true);
	SetVisible("LD_waterreflect_watering_can_01",true);
	SetVisible("LD_waterreflect_watering_can_02",true);
	SetVisible("LD_waterreflect_watering_can_03",true);
	SetVisible("LD_waterreflect_watering_can_04",true);
	SetVisible("LD_waterreflect_watering_can_05",true);
	PopElementTask("TASK_irrigate_swamp")
	PopElementTask("TASK_irrigate_swamp_ongoing")
	DisplayQuestElementInTome( "Q_beeflower_fixing", true )
	DisplayQuestElementInTome( "Q_honey_spoon_fixing", true )
	DisplayQuestElementInTome( "Q_alambic_fixing", true )
	
	DisplayInformation( "watering_can_end", "backgrounds/dialog/vignette_fee", "", 0, "F_watering_can_end_new_task", true )
	
	return true
end

function F_watering_can_end_new_task()
	PushTask("TASK_free_bee_queen","TASK_free_bee_queen",10)
end

------------------------------------------------------------------------------------------------------
------------------------------Functions Part 3 : Nénuphariser le marais ------------------------------
------------------------------------------------------------------------------------------------------

function F_tree_seed_end()
	SetVisible( "LD_swamp_sage_nenuphar1", true );
    SetVisible( "LD_swamp_sage_nenuphar2", true );
	SetVisible( "LD_swamp_sage_nenuphar3", true );
	SetVisible( "LD_swamp_sage_nenuphar4", true );
	SetVisible( "LD_swamp_sage_nenuphar5", true );
	
    SetVisible( "LD_swamp_sage_weed1", false );
	SetVisible( "LD_swamp_sage_weed2", false );
	SetVisible( "LD_swamp_sage_weed3", false );
	SetVisible( "LD_swamp_sage_weed4", false );
	SetVisible( "LD_swamp_sage_weed5", false );
	
	SetVisible( "LD_swamp_seed", false );
	
	PopElementTask("TASK_plant_swamp_seed")
	PopElementTask("TASK_plant_swamp_seed_ongoing")
	
	F_check_flower_gathering_ready()

	
	return true
end

function F_tree_seed_condition_end()
	check = false
	if GetFilledPercent( "LD_swamp_seed" ) == 0 then
		check = true
	end
	return check
end

function F_tree_seed_getprogressed()
	return GetFilledPercent( "LD_swamp_seed" )
end

function F_tree_seed_going_begin()
	local tGoers = GetQuestElementUsers( "Q_tree_seed_going" );
	SetTakenObject( tGoers[1], "LD_tree_seed" )
	F_nenuphar_begin()
	return true
end

function F_tree_seed_going_cancel( sOwnerName )
	SetPosition( "LD_tree_seed", 1050, -65 );
	SetTakenObject( sOwnerName, "" )
	F_nenuphar_cancel()
	return true
end

function F_tree_seed_going_end()
	-- if not IsQuestElementCompleted( "Q_tree_seed_going" ) then
		-- CompleteQuestElement( "Q_tree_seed_going", true )
	-- end
	local tGoers = GetQuestElementUsers( "Q_tree_seed_going" );
	SetVisible( "LD_swamp_seed", true )
	SetVisible( "LD_tree_seed", false );
	
	SetTakenObject( tGoers[1], "" )
	
	if IsQuestElementCompleted("Q_swamp_watering_can_fixing") then
		ForceQuestElement( "Q_tree_seed_planting", tGoers[1] )
	end
	
	DisplayQuestElementInTome( "Q_tree_seed_planting", true )
	return true
end

function F_tree_seed_going_getprogress()
	value = 100.00
	if( IsQuestElementCompleted( "Q_tree_seed_going" ) ) then
		value = 0.00
	end
	return value
end

function F_seedstree_growing_done(sOwnerName)
	KillCultivateSound( sOwnerName );
	KillCultivateFX( sOwnerName );
	return true
end

function F_nenuphar_begin()
	PushTask("TASK_plant_swamp_seed_ongoing","TASK_plant_swamp_seed_ongoing",14.4)
end

function F_nenuphar_cancel()
	if GetNumberOfUsersUsingQuestElement("Q_tree_seed_planting") + GetNumberOfUsersUsingQuestElement("Q_tree_seed_going") <= 0 then
		PopElementTask("TASK_plant_swamp_seed_ongoing")
		MoveFairyToTaskBar()
	end
	return true
end

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

Quest
{
	name = "Q_swamp",
	description = "Q_swamp_desc",
	callback_bestiary = {"bestiary_page11"},
	pnj = "Sage",
	
	
	---------------------------------Part 1 : Nettoyer le marais -------------------------------
	Quest_Element
	{
		name = "Q_swamp_sage_lookingafter",
		zone = "Z_swamp",
		
		description_done ="Q_swamp_sage_lookingafter_done",
		
		Check
		{
			value_type = "nature",
			level = 2 * G_check_factor,
		},

		Check
		{
			is_for_knowledge_tree = true,
			value_type = "nature",
			level = 1 * G_check_factor,
		},
		
		gain_feerie = G_swamp_clean_faerie_gain,

		
		interaction = "LD_swamp_sage",
		
		callback_firstclic = "F_swamp_first_click",
	
		callback_first_time = "F_swamp_first_time",
		callback_begin = "F_swamp_begin",
		callback_cancel = "F_swamp_cancel",
		callback_end = "F_swamp_sage_end",

		stay_where_dropped = false,
		max_users = 6,
		loop = true,

		Fix
		{
			stay_where_dropped = false,
			XpImproved = Nature,
			XpValue    = G_xp_standard_gain_per_action,
			interaction = "LD_swamp_sage",
			label = "fixing",
			fixTime = 5,
			fix_factor = 10,
			ratio_bonus_feerie = G_faerie_standard_gain_per_action,
		},
	},
	---------------------------------Part 2 : Irriguer le marais -------------------------------

	Quest_Element
	{
		name = "Q_swamp_watering_can_fixing",
		zone = "Z_honey_queen",
		
		description_done ="Q_swamp_watering_can_fixing_done",
		prerequisites = {"Q_swamp_sage_lookingafter"},

		Check
		{
			value_type = "knowledge",
			level = 1 * G_check_factor,
		},
		
		Check
		{
			is_for_knowledge_tree = true,
			value_type = "knowledge",
			level = 0 * G_check_factor,
		},
		
		gain_feerie = G_watering_can_faerie_gain,

		interaction = "LD_watering_can",
		callback_firstclic = "F_watering_can_fixing_first_click",
		callback_end = "F_watering_can_fixing_end",
		callback_begin = "F_watering_can_fixing_begin",
		callback_cancel = "F_watering_can_cancel",

		max_users = 3,
		loop = true,

		Fix
		{
			stay_where_dropped = false,
			interaction = "LD_watering_can",
			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_watering_can_fixing_inprogress",	  
			callback_done = "F_watering_can_fixing_done",
			callback_cancel = "F_watering_can_fixing_cancel",
			callback_retrieve_inprogress = "F_watering_can_fixing_retrieve_inprogress",
		},
	},
	---------------------------------Part 3 : Rapporter la graine au marais -------------------------------
	Quest_Element
	{
		name = "Q_tree_seed_going",
		
		prerequisites = {"Q_garden_seed_growing","Q_hermit_seed_growing","Q_swamp_sage_lookingafter"},
		
		Check
		{
		  value_type = "nature",
		  level = 3 * G_check_factor,
		},
		
		Check
		{
			is_for_knowledge_tree = true,
			value_type = "nature",
			level = 2 * G_check_factor,
		},
		
		gain_feerie = G_swamp_seed_deposit_faerie_gain,
	  
		
		interaction = "LD_tree_seed",
		
		percentage = false,
		
		max_users = 1,
		stay_where_dropped  = false,
		callback_begin = "F_tree_seed_going_begin",
		callback_cancel = "F_tree_seed_going_cancel", 
		callback_end = "F_tree_seed_going_end",
		callback_getprogress= "F_tree_seed_going_getprogress",
		

		
		object_to_see = "LD_swamp_seed",

		Fix
		{
			stay_where_dropped = false,
			interaction = "LD_swamp_seed",
			label = "gardening",
			XpImproved = Nature,
			XpValue    = G_xp_standard_gain_per_action,
			fixTime = 5,
			fix_factor = 0,
			ratio_bonus_feerie = G_faerie_standard_gain_per_action,
			
			callback_inprogress = "F_seedstree_growing_begin",
			callback_done = "F_seedstree_growing_done",
			callback_cancel = "F_seedstree_growing_stop",
			callback_retrieve_inprogress = "F_seedstree_growing_retrieve",
		},
	},
	---------------------------------Part 4 : Nénuphariser le marais -------------------------------
	Quest_Element
	{
	  
		name = "Q_tree_seed_planting",
		interaction = "LD_swamp_seed",
		
		zone = "Z_swamp",
		
		prerequisites = {"Q_tree_seed_going","Q_swamp_watering_can_fixing"},

		Check
		{
		  value_type = "nature",
		  level = 3 * G_check_factor,
		},
		
		Check
		{
			is_for_knowledge_tree = true,
			value_type = "nature",
			level = 2 * G_check_factor,
		},
		
		max_users = 3,
		stay_where_dropped  = false,
		condition_end       = "F_tree_seed_condition_end",
		callback_getprogress= "F_tree_seed_getprogressed",
		callback_end = "F_tree_seed_end",
		callback_begin = "F_nenuphar_begin",
		callback_cancel = "F_nenuphar_cancel",

		loop = true,

		gain_feerie = G_swamp_seed_growing_faerie_gain,

		Fix
		{
			stay_where_dropped = false,
			interaction = "LD_swamp_seed",
			label = "gardening",
			XpImproved = Nature,
			XpValue    = G_xp_standard_gain_per_action,
			fixTime = 5,
			fix_factor = 10,
			ratio_bonus_feerie = G_faerie_standard_gain_per_action,

			callback_inprogress = "F_seedstree_growing_begin",
			callback_done = "F_seedstree_growing_stop",
			callback_cancel = "F_seedstree_growing_stop",
			callback_retrieve_inprogress = "F_seedstree_growing_retrieve",
		},
	},
}
