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

require( "package/quests/q_variables_define.lua" );
DebugOut( "generate infinite quests (file : package/quests/q_banquet.lua)" );

------------------------------------------------------------------------------------------------------------------
------------------------------------------------Fonctions de Check------------------------------------------------
------------------------------------------------------------------------------------------------------------------
-- vérifie que les préparatifs sont terminés
function F_banquet_dance_begin_condition()
	return IsQuestElementCompleted("Q_banquet_playing") and IsQuestElementCompleted("Q_banquet_cooking") and IsQuestElementCompleted("Q_banquet_brewing") == true
end

--Verifie que les 3 zones du banquet sont active en même temps
function F_banquet_activated_zone_checker()
	return GetNumberOfUsersUsingQuestElement("Q_banquet_playing") + GetNumberOfUsersUsingQuestElement("Q_banquet_cooking") + GetNumberOfUsersUsingQuestElement("Q_banquet_brewing")
end
------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------

------------------------------------------------------------------------------------------------------------------
------------------------------------------------Fonctions Preparation Banquet (partie 1)--------------------------
------------------------------------------------------------------------------------------------------------------
--Fonction d'update de la preparation => diminution d'un stock commun aux 3quest elements
function F_banquet_update( fDt, sOwnerName )
	StockDiminution( sOwnerName, "LD_banquet_dispatch", 10, fDt );
	return true
end

--Fonction de simulation de la preparation => diminution d'un stock commun aux 3quest elements
function F_banquet_simulate( fDt, sOwnerName )
	StockDiminution( sOwnerName, "LD_banquet_dispatch", 10, fDt );
	return true
end

--Progres de la preparation du banquet
function F_banquet_getprogress()
	return GetFilledPercent( "LD_banquet_dispatch" );
end

--Callback de fin -> lance la 2e partie de la quete (dance)
function F_banquet_danceparty()
  if G_preparation == 0 then
	G_preparation = 1;
	if IsGameInLinearTuto() then
		PopFrontTask();
		DisplayInformation( "banquet_tutorial_start_text", "backgrounds/dialog/vignette_fee", "", 0, "", true );
		DisableInput(true);
	else
		DisplayInformation( "banquet_danceparty_text", "backgrounds/dialog/vignette_fee", "", 0, "", false );
	end
	PlayMusic( "sounds/music_recruit.ogg", true )
	local tPlayers = GetQuestElementUsers( "Q_banquet_playing" );
	local tCookers = GetQuestElementUsers( "Q_banquet_cooking" );
	local tBrewers = GetQuestElementUsers( "Q_banquet_brewing" );
  
	--cache la zone active de la table et rend visible la zone de dance
	SetVisible( "LD_banquet_dispatch", false )
	SetVisible( "LD_banquet_dance", true )
	SetVisible( "LD_banquet_dance_drop", true )
  
	ResetSource( "LD_banquet_dispatch" )
	
	CompleteQuestElement( "Q_banquet_dancing", false )
	
	if IsFumeBarFXPlaying( tCookers[1] ) == true then
			KillFumeBarFX( tCookers[1] );
	end
	
	if not IsQuestElementCompleted("Q_banquet_playing") then
		CompleteQuestElement( "Q_banquet_playing", true )
	end
	if not IsQuestElementCompleted("Q_banquet_cooking") then
		CompleteQuestElement( "Q_banquet_cooking", true )
	end
	if not IsQuestElementCompleted("Q_banquet_brewing") then
		CompleteQuestElement( "Q_banquet_brewing", true )
	end
   
	ForceQuestElement( "Q_banquet_dancing", tPlayers[1] );
	ForceQuestElement( "Q_banquet_dancing", tCookers[1] );
	ForceQuestElement( "Q_banquet_dancing", tBrewers[1] );   
  end  
  
  return true
end

--Message de début du banquet
function F_banquet_start_dialogbox()	
	G_preparation = 0;
	return true
end

--Payement en bouffe du banquet
function F_banquet_pay()
	if (IsGameInLinearTuto() and IsFXPlaying(Air, G_tutorial_FX, "LD_banquet_dispatch")) then
		KillFX(Air, G_tutorial_FX, "LD_banquet_dispatch")
		DisableInput(true)
		DisplayInformation( "TUTO_banquet_start_text", "backgrounds/dialog/vignette_fee", "", 0,"", false )
		UnselectCurrent()
	end
	
	if G_banquet_payed == 0 then
		GainFood( -GetBanquetNeededFood() );
		G_banquet_payed = 1;
	end
	return true;
end

--Message de 1ere fois du banquet
function F_banquet_first_time_dialogbox()
    --DisplayInformation( "banquet_first_text", "backgrounds/dialog/vignette_fee", "", 0, "", false );
	G_banquet_ended = 0;
	return true
end

--begin condition musique
function F_banquet_music_begin_condition()
	return GetNumberOfUsersUsingQuestElement("Q_banquet_brewing") >= 1
end
--begin condition cook
function F_banquet_cook_begin_condition()
	return GetNumberOfUsersUsingQuestElement("Q_banquet_brewing") >= 1
end
-- begin condition brew == begin condition preparation
function F_banquet_prepare_begin_condition()
	return G_banquet_payed == 1 or GetFoodReserveValue() >= GetBanquetNeededFood()
end


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

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

--function F_banquet_music_end( sOwnerName )
function F_banquet_music_playing_stop( sOwnerName )
	if IsMusicFXPlaying( sOwnerName ) == true then
		KillMusicFX( sOwnerName )
		KillMusicSound( sOwnerName )
	end
	return true
end

function F_cooking_sounds_begin( sOwnerName )
	PlayCookSound( sOwnerName )
	return true
end

function F_banquet_cooking_end( sOwnerName )
	if IsFumeBarFXPlaying( sOwnerName ) == true then
		KillFumeBarFX( sOwnerName );
	end
	return true
end

function F_banquet_cooking_retrieve( sOwnerName )
	PlayFumeBarFX( sOwnerName );
	return true
end

function F_banquet_cooking_stop( sOwnerName )
	KillCookSound( sOwnerName );
	return true
end

--Test pour vérifier que des perso travaillent sur les 3 quest element
function F_banquet_work_condition()
	check = false
	if F_banquet_activated_zone_checker() >= 3 then
		check = true
	end
	return check
end


function F_banquet_deactivated_zone_music( sOwnerName )
	if IsMusicFXPlaying( sOwnerName ) == true then
		KillMusicFX( sOwnerName )
		KillMusicSound( sOwnerName )
	end 
	F_banquet_prepare_test_failed()
	return true
end


function F_banquet_deactivated_zone_food( sOwnerName )
	if IsFumeBarFXPlaying( sOwnerName ) == true then
		KillFumeBarFX( sOwnerName );
	end
	F_banquet_prepare_test_failed()
	return true
end


function F_banquet_deactivated_zone_drink( sOwnerName )
	F_banquet_prepare_test_failed()
	return true
end

--Test effectué à chaque drag de perso hors de la preparation => si plus personne, reset du banquet
function F_banquet_prepare_test_failed()
	if F_banquet_activated_zone_checker() <= 0 then
		ResetSource( "LD_banquet_dispatch" )
		F_banquet_failed()
	elseif F_banquet_activated_zone_checker() == 1 then
		DisplayHelpInformation( "banquet_cancel_warning", "backgrounds/dialog/vignette_fee", "", 0,"", false )
	end
	return true
end
------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------

------------------------------------------------------------------------------------------------------------------
------------------------------------------------Fonctions Fete Banquet (partie 2)---------------------------------
------------------------------------------------------------------------------------------------------------------
--Fonction appelée à chaque drag de perso hors de la quete => si plus personne, reset
function F_banquet_dance_drag_dancer()
	if GetNumberOfUsersUsingQuestElement("Q_banquet_dancing") <= 0 then
		PlayMusic( "", false )
		ResetSource( "LD_banquet_dance" )
		F_banquet_failed()
	elseif GetNumberOfUsersUsingQuestElement("Q_banquet_dancing") == 1 then
		DisplayHelpInformation( "banquet_cancel_warning", "backgrounds/dialog/vignette_fee", "", 0,"", false )
	end  
  return true;
end

--Fin de la quête => creation nouveau perso
function F_banquet_end()
	if  IsGameInLinearTuto() and G_banquet_ended == 0 then
		DisplayTutorialCallback( "TUTO_recruit" );
	else
		if( GetNumberOfCharacter() < G_max_population ) then
			DisplayInformation( "banquet_end_text", "backgrounds/dialog/vignette_fee", "", 0,"", false );
		else
			DisplayInformation( "banquet_end_text_max_pop", "backgrounds/dialog/vignette_fee", "", 0,"", false );
		end
	end

  --rend visible la zone active de la table
	SetVisible( "LD_banquet_dispatch", true );
	SetVisible( "LD_banquet_dance", false );
	SetVisible( "LD_banquet_dance_drop", false );
  
	CompleteQuestElement( "Q_banquet_playing", false );
	CompleteQuestElement( "Q_banquet_cooking", false );
	CompleteQuestElement( "Q_banquet_brewing", false );

	if G_banquet_ended == 0 then
		G_banquet_ended = 1;		
	end  
	
	local tDancers = GetQuestElementUsers( G_banquet_dance );
	local nbUsers = GetNumberOfUsersUsingQuestElement( G_banquet_dance );
	local i = 1;
	while i <= nbUsers do
		GainOneCharacterHunger( tDancers[i], G_banquet_hunger_gain );
		i = i+1;
	end

	PlayMusic( "", false );
  
	if( GetNumberOfCharacter() < G_max_population ) then
		RecruitNewCharacter  
		{
			positionX = 149.000000,
			positionY = 160.000000,
			animal_positionX = 149.000000,
			animal_positionY = 160.000000,
			moral = 100,
			hunger_start = 100,
			hunger_limit = 20,
	
			-- Metiers
			metiers =
			{
				nature = {},
				exploration = {},
				knowledge = {},
				music = {},
			},
		
			-- Feedings Parameters --
			feed_collectTime = G_feed_collectTime, -- en sec.
			feed_gather_factor = G_feed_gather_factor,  
			feed_eat_factor = G_feed_eat_factor
		}
		
		G_lastCharaterRecruitedName = GetLastCharacterName();
		SetPositionToMove( G_lastCharaterRecruitedName, 762, 447, { scallbackretrieve = "F_recruitmove_retrieve", scallbackcancel = "F_recruitmove_cancel", scallbackend = "F_recruitmove_end" }  );
		
		SetStockMaxValue( "LD_banquet_dispatch", GetBanquetNewStock() );
        SetStockMaxValue( "LD_banquet_dance", 5 * GetBanquetNewStock() / 3 );
		
		if  IsGameInLinearTuto() then
			SelectObject( G_lastCharaterRecruitedName );
		end
		
		if( GetNumberOfCharacter() >= G_max_population) then
			CompleteQuest( "Q_population", true );			
		end
	end	
	
	ResetSource( "LD_banquet_dance" )
	ResetSource( "LD_banquet_dispatch" )
  
	CompleteQuest( "Q_banquet", false );
	G_preparation = 0;
	G_banquet_payed = 0;

	return true
end

function F_banquet_dance_end_condition()
	return (GetFilledPercent("LD_banquet_dance") <= 0);
end

function F_banquet_dance_get_progress()
	return GetFilledPercent("LD_banquet_dance");
end

function F_recruitmove_retrieve( sOwnerName )
	PlayFX( Air, "fx/fx_fee_fuite.lua", sOwnerName, 0, 0 );
	return true
end

function F_recruitmove_cancel( sOwnerName )
	KillFX( Air, "fx/fx_fee_fuite.lua", sOwnerName );
	return true
end

function F_recruitmove_end( sOwnerName )
	KillFX( Air, "fx/fx_fee_fuite.lua", sOwnerName );
	return true
end

function F_banquet_dancing_update( fDt, sOwnerName )
	GainOneCharacterMoral( sOwnerName, G_banquet_moral_gain*fDt*0.001 );
	return true
end

------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------


------------------------------------------------------------------------------------------------------------------
------------------------------------------------Fonctions Anulation Banquet (commune aux 2 parties)---------------
------------------------------------------------------------------------------------------------------------------
function F_banquet_failed()
DebugOut( "CANCELBANQUET" );
  StopQuestElement("Q_banquet_playing")
  StopQuestElement("Q_banquet_cooking")
  StopQuestElement("Q_banquet_brewing")
  StopQuestElement("Q_banquet_dancing")
  
  CompleteQuestElement( "Q_banquet_dancing", false )
  CompleteQuestElement( "Q_banquet_playing", false )
  CompleteQuestElement( "Q_banquet_cooking", false )
  CompleteQuestElement( "Q_banquet_brewing", false )
  
  SetVisible( "LD_banquet_dispatch", true )
  SetVisible( "LD_banquet_dance", false )
  SetVisible( "LD_banquet_dance_drop", false )
  
  ResetSource( "LD_banquet_dance" )
  ResetSource( "LD_banquet_dispatch" )
  
  G_banquet_payed = 0;
  
  if not IsGameInLinearTuto() then
	DisplayInformation( "banquet_failed_text", "backgrounds/dialog/vignette_fee", "", 0,"", true );  
  end
  return true
end
------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------

Quest
{
	name = G_banquet_quest_name,
	description = "Q_banquet_desc",
    callback_bestiary = {"bestiary_page12"},
	primary_quest = false,
	display_in_tome = false,

	--Music
	Quest_Element
	{
		Check
		{
			value_type = "music",
			level = 0 * G_check_factor,
		},

		name = G_banquet_play,
		interaction = "LD_banquet_dispatch",

		callback_cancel = "F_banquet_deactivated_zone_music",	
		workbegin_condition = "F_banquet_work_condition",
		callback_getprogress= "F_banquet_getprogress",
		callback_end = "F_banquet_danceparty",
		condition_begin = "F_banquet_music_begin_condition",
		callback_begin = "F_banquet_pay";

		stay_where_dropped = false,

		loop = true,

		is_interruptible = false,

		must_be_seen_to_be_completed = false,
		
		include_in_labrador_intentions = false,

		max_users = 1,

		Musique
		{
			--stay_where_dropped = false,
			interaction = "LD_banquet_music",
			label = "playing",
			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_banquet_music_playing_stop",
			callback_cancel = "F_banquet_music_playing_stop",
			callback_retrieve_inprogress = "F_banquet_music_playing_retrieve",
			callback_inprogress = "F_banquet_music_playing_inprogress",
			callback_update = "F_banquet_update",
			callback_simulate = "F_banquet_simulate",
		},
	},
  
	--Food
	Quest_Element
	{
	  
		Check
		{
			value_type = "nature",
			level = 0 * G_check_factor,
		},
		   
		name = G_banquet_cook,
		interaction = "LD_banquet_dispatch",
		callback_cancel = "F_banquet_deactivated_zone_food",
		callback_end = "F_banquet_cooking_end",	
		workbegin_condition = "F_banquet_work_condition",
		callback_getprogress= "F_banquet_getprogress",
		callback_end = "F_banquet_danceparty",

		condition_begin = "F_banquet_cook_begin_condition",

		loop = true,
		stay_where_dropped = false,

		is_interruptible = false,

		must_be_seen_to_be_completed = false,
		
		include_in_labrador_intentions = false,

		emoticone = "emoticon_cook",

		max_users = 1,

		Gather
		{
			source = "LD_reservoir",
			collectTime = 5, 
			gather_factor  = 0,
			label = "collecting",
			ratio_bonus_feerie = G_faerie_standard_gain_per_action,
		},

		Fix
		{
			stay_where_dropped = false,
			interaction = "LD_banquet_food",
			label = "cooking",
			XpImproved = Nature,
			XpValue    = G_xp_standard_gain_per_infinite_action,
			fixTime = 5,
			fix_factor = 10,
			ratio_bonus_feerie = G_faerie_standard_gain_per_infinite_action,
			callback_done = "F_banquet_cooking_stop",
			callback_cancel = "F_banquet_cooking_stop",
			callback_inprogress = "F_cooking_sounds_begin",
			callback_retrieve_inprogress = "F_banquet_cooking_retrieve",
			callback_update = "F_banquet_update",
			callback_simulate = "F_banquet_simulate",
		},	
	},
  
	--Drink
	Quest_Element
	{

		Check
		{
			value_type = "nature",
			level = 0 * G_check_factor,
		},

		name = G_banquet_brew,
		interaction = "LD_banquet_dispatch",

		callback_first_time = "F_banquet_first_time_dialogbox",

		callback_begin = "F_banquet_start_dialogbox",

		callback_cancel = "F_banquet_deactivated_zone_drink",	
		workbegin_condition = "F_banquet_work_condition",

		callback_getprogress= "F_banquet_getprogress",
		callback_end = "F_banquet_danceparty",

		condition_begin = "F_banquet_prepare_begin_condition",

		loop = true,
		stay_where_dropped = false,

		is_interruptible = false,

		must_be_seen_to_be_completed = false,
		
		include_in_labrador_intentions = false,

		emoticone = "emoticon_brew",

		max_users = 1,

		Fix
		{
			stay_where_dropped = false,
			interaction = "LD_banquet_drink",
			label = "brewing",
			XpImproved = Nature,
			XpValue    = G_xp_standard_gain_per_infinite_action,
			fixTime = 5,
			fix_factor = 10,
			ratio_bonus_feerie = G_faerie_standard_gain_per_infinite_action,
			callback_update = "F_banquet_update",
			callback_simulate = "F_banquet_simulate",
		},
	},
  
	--Dance
	Quest_Element
	{

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

		name = G_banquet_dance,
		interaction = "LD_banquet_dance_drop",
		condition_begin = "F_banquet_dance_begin_condition",
		condition_end = "F_banquet_dance_end_condition",
		callback_getprogress = "F_banquet_dance_get_progress",
		callback_cancel = "F_banquet_dance_drag_dancer",

		callback_end = "F_banquet_end",

		max_users = 200,
		-- min_users = 3,
		loop = true,

		--must_be_seen_to_be_completed = false,
		
		include_in_labrador_intentions = false,

		is_interruptible = false,

		Dance
		{
			interaction = "LD_banquet_dance",
			label = "dancing",
			XpImproved = Music,
			XpValue    = G_xp_standard_gain_per_infinite_action,
			danceTime = 5,
			dance_factor = 10,
			ratio_bonus_feerie = G_faerie_standard_gain_per_infinite_action,
			callback_update = "F_banquet_dancing_update",
		},
	},
}
