
-- UA that defends Kerberos' main castle; = End Boss
-- attacks player from Lion's den (Main castle)

createArmyAttack3 = function()
	UAAttackPlayerFromLionsDen = UnlimitedArmy:New({
					Player = 5,
					Area = 4200,
					TransitAttackMove = true,
					Formation = UnlimitedArmy.Formations.Chaotic,
					LeaderFormation = FormationFunktion,
					AIActive = true,
					AutoRotateRange = true,
					DoNotNormalizeSpeed = true,
					})
	--[[local spawnpos = {}
	spawnpos[1] = GetPosition("innerFestungSpawn1")
	spawnpos[1].Generator = "innerFestungSpawnTurm1"
	spawnpos[2] = GetPosition("innerFestungSpawn2")
	spawnpos[2].Generator = "innerFestungSpawnTurm2"
	spawnpos[3] = GetPosition("innerFestungSpawn3")
	spawnpos[3].Generator = "innerFestungSpawnTurm3"
	spawnpos[4] = GetPosition("innerFestungSpawn4")
	spawnpos[4].Generator = "innerFestungSpawnTurm4"
	spawnpos[5] = GetPosition("innerFestungSpawn5")
	spawnpos[5].Generator = "innerFestungSpawnTurm5"
	spawnpos[6] = GetPosition("defend1")
	spawnpos[6].Generator = "lionsDen"
	UAAttackPlayerFromLionsDenSpawnGenerator = UnlimitedArmySpawnGenerator:New(UAAttackPlayerFromLionsDen,{
										Position = spawnpos
										ArmySize = 20,
										SpawnCounter = 60,
										SpawnLeaders = 20,
										LeaderDesc = {
										{LeaderType = Entities.PU_LeaderSword4, SoldierNum = 8, SpawnNum = 4, Looped = true, Experience = 3},
										{LeaderType = Entities.PU_LeaderHeavyCavalry2, SoldierNum = 3, SpawnNum = 4, Looped = true, Experience = 3},
										{LeaderType = Entities.PU_LeaderBow4, SoldierNum = 8, SpawnNum = 6, Looped = true, Experience = 3},
										{LeaderType = Entities.PV_Cannon4, SoldierNum = 0, SpawnNum = 4, Looped = true, Experience = 3},
										{LeaderType = Entities.PU_LeaderPoleArm4, SoldierNum = 8, SpawnNum = 5, Looped = true, Experience = 3},
										{LeaderType = Entities.PU_LeaderCavalry2, SoldierNum = 3, SpawnNum = 5, Looped = true, Experience = 3}
										}
										})]]
	--UAAttackPlayerFromLionsDen:AddCommandMove(GetPosition("castle"),true)
	--UAAttackPlayerFromLionsDen:AddCommandWaitForIdle(true)
	--UAAttackPlayerFromLionsDen:AddCommandMove(GetPosition("castle1"),true)
	--UAAttackPlayerFromLionsDen:AddCommandWaitForIdle(true)
	StartSimpleJob("BombAttacker")
	UAAttackPlayerFromLionsDen.SpawnerCountDown = 300
	UAAttackPlayerFromLionsDen.CurrentTroopAmount = 0
	UAAttackPlayerFromLionsDen:AddCommandLuaFunc(LionsDenAttackerFunc,true)
end

function LionsDenAttackerFunc(self)
	if not DarkCastleDefUA.Spawner or DarkCastleDefUA.Spawner:IsDead() then
		return true
	end
	if self:GetSize(true,false) < 10 then
		self.SpawnerCountDown = self.SpawnerCountDown - 1
		if self.SpawnerCountDown <= 0 then
			for i = 1,10,1 do
				if IsDead("bomb"..i) then
					local troopID = AI.Entity_CreateFormation(5, Entities.PU_LeaderHeavyCavalry2, nil, 3, DarkCastleDefUA.Spawner.Pos[1].X,DarkCastleDefUA.Spawner.Pos[1].Y, nil, nil, 3,0)
					SetEntityName(troopID,"bomb"..i)
					Logic.SetSpeedFactor(troopID, 2.0)
					self:AddLeader("bomb"..i)
				end
			end
			--mehr spawnen?
			self.SpawnerCountDown = 150
		end
	end
	if self:IsIdle() then
		return GetNextDZ(self)
	end
	return false
end

function GetNextDZ(self)
	local worldsize = Logic.WorldGetSize()
	local tempdistancevariable
	local DZanzahl = {Logic.GetPlayerEntitiesInArea(1, Entities.PB_VillageCenter1, self:GetPosition().X, self:GetPosition().Y, worldsize, 1)}
	local GZanzahl = {Logic.GetPlayerEntitiesInArea(1, Entities.PB_VillageCenter2, self:GetPosition().X, self:GetPosition().Y, worldsize, 1)}
	local SZanzahl = {Logic.GetPlayerEntitiesInArea(1, Entities.PB_VillageCenter3, self:GetPosition().X, self:GetPosition().Y, worldsize, 1)}
	if DZanzahl[1] ~= 0 then
		tempdistancevariable = DZanzahl[2]
	end
	if GZanzahl[1] ~= 0 then
		if tempdistancevariable == nil then
			tempdistancevariable = GZanzahl[2]
		elseif GetDistance(tempdistancevariable,self:GetPosition()) <= GetDistance(GZanzahl[2],self:GetPosition()) then
			tempdistancevariable = GZanzahl[2]
		end
	end
	if SZanzahl[1] ~= 0 then
		if tempdistancevariable == nil then
			tempdistancevariable = SZanzahl[2]
		elseif GetDistance(tempdistancevariable,self:GetPosition()) <= GetDistance(SZanzahl[2],self:GetPosition()) then
			tempdistancevariable = SZanzahl[2]
		end
	end
	if tempdistancevariable == nil then
		return Militarybasedown(self)
	end
	return true, UnlimitedArmy.CreateCommandMove(GetPosition(tempdistancevariable))
end

function BombAttacker()
	for i = 1,10,1 do
		local bombname = "bomb"..i
		if IsAlive(bombname) then
			if GetRandom(100) > 80 then
				CreateEntity(5,Entities.XD_Bomb1,GetPosition(bombname))
			end
		end
	end
	return false
end