
createQuestDestroyLionsDen = function()

	StartJob("QuestDestroyLionsDen")

	end


-----------------------------------------------------------------------------------------------------------------------	
--
--	JOB: "QuestDestroyLionsDen"
--
-----------------------------------------------------------------------------------------------------------------------	
	-------------------------------------------------------------------------------------------------------------------
	Condition_QuestDestroyLionsDen = function()
	-------------------------------------------------------------------------------------------------------------------
		
		local kerberosCastle = Logic.GetEntityIDByName("lionsDen")

		-- won if castle is burning or destroyed
		if	kerberosAppeared == nil and 
			Logic.GetEntityHealth(kerberosCastle) < (Logic.GetEntityMaxHealth(kerberosCastle) * 0.99) then

			DarkCastleDefUA:AddLeader(CreateEntity(5, Entities.CU_BlackKnight, GetPosition("defend1"), "Kerberos" ))
			
			kerberosAppeared = true
			
		end
			
		return IsDead("lionsDen") and IsDead("Kerberos")		
		
	end
		
	-------------------------------------------------------------------------------------------------------------------
	Action_QuestDestroyLionsDen = function()
	-------------------------------------------------------------------------------------------------------------------
		Logic.SetQuestType(1, 1, MAINQUEST_CLOSED, 1)
		
		endQuestDestroyLionsDen()
		
		return true
		
		end
-----------------------------------------------------------------------------------------------------------------------	

function createQuestDestroyLionsDenAdvanced()
	StartJob("QuestDestroyLionsDenAdvanced")
end

Condition_QuestDestroyLionsDenAdvanced = function()
	local kerberosCastle = Logic.GetEntityIDByName("lionsDen")
	if Dummyappeared == nil and Logic.GetEntityHealth(kerberosCastle) < (Logic.GetEntityMaxHealth(kerberosCastle) * 0.99) then
		DarkID = CreateEntity(5, Entities.CU_BlackKnight_LeaderMace1, GetPosition("defend1"), "DummyKerberos" )
		if HistoryFlag == 1 then
			Logic.SetEntityScriptingValue(DarkID, -29,Float2Int(1.5))
		else
			Logic.SetEntityScriptingValue(DarkID, -33,Float2Int(1.5))
		end
		Logic.SetSpeedFactor(DarkID, 1.2)
		Logic.SetEntityInvulnerabilityFlag(DarkID, 1)
		DarkCastleDefUA:AddLeader(DarkID)
		Dummyappeared = true
	end
	if IsDead("lionsDen") then
		if IsValid(DarkID) and lionsDenDown == nil then
			Logic.SetEntityInvulnerabilityFlag(DarkID, 0)
			lionsDenDown = true
			UAAttackPlayerFromLionsDen:Destroy()
		end
		if DarkCastleDefUA:IsDead() == -1 and UAAttackPlayerFromLionsDen:IsDead() == -1 then
			return true
		end
	end
end

Action_QuestDestroyLionsDenAdvanced = function()
	Logic.SetQuestType(1, 1, MAINQUEST_CLOSED, 1)
	RestoreStuff()
	endQuestDestroyLionsDenAdvanced()
	return true
end

function BurningBuildingHandler()
	for _,buildingID1 in ipairs(InvisBuildingsPart1) do
		local gebaeudeID = Logic.GetEntityIDByName(buildingID1)
		if IsAlive(gebaeudeID) then
			local eTtype = Logic.GetEntityTypeName(Logic.GetEntityType(gebaeudeID))
			if eTtype ~= "CB_Bastille1" and eTtype ~= "PB_DarkTower3" and eTtype ~= "CB_DarkCastle" then
				if Logic.GetEntityMaxHealth(gebaeudeID) *0.25 >= Logic.GetEntityHealth(gebaeudeID) then
					if Logic.EntityGetPlayer(gebaeudeID) ~= 3 then
						Logic.ChangeEntityPlayerID(gebaeudeID, 3)
					end
				end
			end
		end
	end
	for _,buildingID1 in ipairs(InvisBuildingsPart2) do
		local gebaeudeID = Logic.GetEntityIDByName(buildingID1)
		if IsAlive(gebaeudeID) then
			local eTtype = Logic.GetEntityTypeName(Logic.GetEntityType(gebaeudeID))
			if eTtype ~= "CB_Bastille1" and eTtype ~= "PB_DarkTower3" and eTtype ~= "CB_DarkCastle" then
				if Logic.GetEntityMaxHealth(gebaeudeID) *0.25 >= Logic.GetEntityHealth(gebaeudeID) then
					if Logic.EntityGetPlayer(gebaeudeID) ~= 3 then
						Logic.ChangeEntityPlayerID(gebaeudeID, 3)
					end
				end
			end
		end
	end
	for _,buildingID1 in ipairs(InvisBuildingsPart3) do
		local gebaeudeID = Logic.GetEntityIDByName(buildingID1)
		if IsAlive(gebaeudeID) then
			local eTtype = Logic.GetEntityTypeName(Logic.GetEntityType(gebaeudeID))
			if eTtype ~= "CB_Bastille1" and eTtype ~= "PB_DarkTower3" and eTtype ~= "CB_DarkCastle" then
				if Logic.GetEntityMaxHealth(gebaeudeID) *0.25 >= Logic.GetEntityHealth(gebaeudeID) then
					if Logic.EntityGetPlayer(gebaeudeID) ~= 3 then
						Logic.ChangeEntityPlayerID(gebaeudeID, 3)
					end
				end
			end
		end
	end
	for _,buildingID1 in ipairs(InvisBuildingsPart4) do
		local gebaeudeID = Logic.GetEntityIDByName(buildingID1)
		if IsAlive(gebaeudeID) then
			local eTtype = Logic.GetEntityTypeName(Logic.GetEntityType(gebaeudeID))
			if eTtype ~= "CB_Bastille1" and eTtype ~= "PB_DarkTower3" and eTtype ~= "CB_DarkCastle" then
				if Logic.GetEntityMaxHealth(gebaeudeID) *0.25 >= Logic.GetEntityHealth(gebaeudeID) then
					if Logic.EntityGetPlayer(gebaeudeID) ~= 3 then
						Logic.ChangeEntityPlayerID(gebaeudeID, 3)
					end
				end
			end
		end
	end
	for _,buildingID1 in ipairs(InvisBuildingsPart5) do
		local gebaeudeID = Logic.GetEntityIDByName(buildingID1)
		if IsAlive(gebaeudeID) then
			local eTtype = Logic.GetEntityTypeName(Logic.GetEntityType(gebaeudeID))
			if eTtype ~= "CB_Bastille1" and eTtype ~= "PB_DarkTower3" and eTtype ~= "CB_DarkCastle" then
				if Logic.GetEntityMaxHealth(gebaeudeID) *0.25 >= Logic.GetEntityHealth(gebaeudeID) then
					if Logic.EntityGetPlayer(gebaeudeID) ~= 3 then
						Logic.ChangeEntityPlayerID(gebaeudeID, 3)
					end
				end
			end
		end
	end
	for _,buildingID1 in ipairs(InvisBuildingsPart6) do
		local gebaeudeID = Logic.GetEntityIDByName(buildingID1)
		if IsAlive(gebaeudeID) then
			local eTtype = Logic.GetEntityTypeName(Logic.GetEntityType(gebaeudeID))
			if eTtype ~= "CB_Bastille1" and eTtype ~= "PB_DarkTower3" and eTtype ~= "CB_DarkCastle" then
				if Logic.GetEntityMaxHealth(gebaeudeID) *0.25 >= Logic.GetEntityHealth(gebaeudeID) then
					if Logic.EntityGetPlayer(gebaeudeID) ~= 3 then
						Logic.ChangeEntityPlayerID(gebaeudeID, 3)
					end
				end
			end
		end
	end
	return false
end

function RestoreStuff()
	--Restore Old Music-func
	function LocalMusic_UpdateMusic()

	if LocalMusic.BattlesOnTheMap == 0 then
		--Music is playing?
		if  LocalMusic.SongLength  > Logic.GetTime() then	 
			return
		end	
	end

	--get current Weather
	local Weather = Logic.GetWeatherState()
	
	if Weather == 1 then
		--normal
		Weather = "summer"
	elseif Weather == 2 then
		--rain
		Weather = "snow"
	elseif Weather == 3 then
		--snow
		Weather = "snow"
	end
	
	local SetToUse
	
	if LocalMusic.BattlesOnTheMap == 1 then
		SetToUse = LocalMusic.SetBattle
	else
		SetToUse = LocalMusic.UseSet[Weather]
	end
	
	--is briefing running?
 	if IsBriefingActive ~= nil and IsBriefingActive() == true then
		SetToUse = LocalMusic.SetBriefing
	end
	
	
	local SongAmount = table.getn(SetToUse)
	local Random = 1 + XGUIEng.GetRandom(SongAmount-1)
	local SongToPlay = Folders.Music .. SetToUse[Random][1]	
	
	
	Sound.StartMusic( SongToPlay, 127)
	LocalMusic.SongLength =  Logic.GetTime() + SetToUse[Random][2] + 1
	
	LocalMusic.BattlesOnTheMap = 0
	
	--GUI.AddNote(SetToUse[Random][1] .. " is currently playing")
	

end

function LocalMusic.CallbackSettlerKilled(_HurterPlayerID, _HurtPlayerID)
	
	local PlayerID = GUI.GetPlayerID()
	
	if _HurterPlayerID ~= _HurtPlayerID 
	and PlayerID == _HurtPlayerID  then		
		if LocalMusic.LastBattleMusicStarted < Logic.GetTime() then		
			LocalMusic.BattlesOnTheMap = 1
			LocalMusic.LastBattleMusicStarted = Logic.GetTime() + 127
			LocalMusic.SongLength = 0
		end
	end
end

LocalMusic.SongLength = 0
	
end