function CreateArmyP8Defense()	
	UACounter = UACounter + 1
	P8UADef1 = LazyUnlimitedArmy:New({
		Player = 8,
		Area = 3500,
		AutoDestroyIfEmpty = false,
		TransitAttackMove = true,
		Formation = UnlimitedArmy.Formations.Chaotic,
		PrepDefense = false,
		DestroyBridges = false,
		LeaderFormation = 4,
		AIActive = true,
		DefendDoNotHelpHeroes = false,
		DoNotNormalizeSpeed = false,
		IgnoreFleeing = trze,
		HiResJob = true
	},
	UACounter,
	UAMaxCounter
	)
	UAP8Def1Recuiter = UnlimitedArmyRecruiter:New(P8UADef1,{
		Buildings = BuildingTable,
		ArmySize = 10,
		UCats = {
			{UCat = UpgradeCategories.LeaderSword, SpawnNum = 1, Looped = true},
			{UCat = UpgradeCategories.LeaderPoleArm, SpawnNum = 1, Looped = true},
			{UCat = UpgradeCategories.LeaderBow, SpawnNum = 1, Looped = true},
			{UCat = UpgradeCategories.LeaderHeavyCavalry, SpawnNum = 1, Looped = true},
			{UCat = UpgradeCategories.LeaderCavalry, SpawnNum = 1, Looped = true},
			--{UCat = UpgradeCategories.Cannon4, SpawnNum = 1, Looped = true},
			{UCat = UpgradeCategories.LeaderRifle, SpawnNum = 1, Looped = true},
		},
		ResCheat = true,
		ReorderAllowed = true,
		RemoveUnavailable = true,
		RandomizeSpawn = false,
		DoNotRemoveIfDeadOrEmpty = true,
	})
	
	UACounter = UACounter + 1
	P8UADef2 = LazyUnlimitedArmy:New({
		Player = 8,
		Area = 3500,
		AutoDestroyIfEmpty = false,
		TransitAttackMove = true,
		Formation = UnlimitedArmy.Formations.Chaotic,
		PrepDefense = false,
		DestroyBridges = false,
		LeaderFormation = 4,
		AIActive = true,
		DefendDoNotHelpHeroes = false,
		DoNotNormalizeSpeed = false,
		IgnoreFleeing = true,
		HiResJob = true
	},
	UACounter,
	UAMaxCounter
	)
	UAP8Def2Recuiter = UnlimitedArmyRecruiter:New(P8UADef2,{
		Buildings = BuildingTable,
		ArmySize = 10,
		UCats = {
			{UCat = UpgradeCategories.LeaderSword, SpawnNum = 1, Looped = true},
			{UCat = UpgradeCategories.LeaderPoleArm, SpawnNum = 1, Looped = true},
			{UCat = UpgradeCategories.LeaderBow, SpawnNum = 1, Looped = true},
			{UCat = UpgradeCategories.LeaderHeavyCavalry, SpawnNum = 1, Looped = true},
			{UCat = UpgradeCategories.LeaderCavalry, SpawnNum = 1, Looped = true},
			--{UCat = UpgradeCategories.Cannon4, SpawnNum = 1, Looped = true},
			{UCat = UpgradeCategories.LeaderRifle, SpawnNum = 1, Looped = true},
		},
		ResCheat = true,
		ReorderAllowed = false,
		RemoveUnavailable = true,
		RandomizeSpawn = false,
		DoNotRemoveIfDeadOrEmpty = true,
	})
	
	P8UADef1.CommandDelayer = 5
	P8UADef2.CommandDelayer = 5
	P8UADef2.DefPos = GetPosition("P8_DefensePoint2")
	P8UADef1.DefPos = GetPosition("P8_DefensePoint1")
	
	P8UADef1:AddCommandLuaFunc(BetterDefBehavior,false)
	P8UADef2:AddCommandLuaFunc(BetterDefBehavior,false)
	
	StartSimpleJob("DefBackPoints")
end

function DefBackPoints()
	local _number, _, _ = Logic.GetPlayerEntitiesInArea(8, Entities.PB_Tower2, GetPosition("P8_DefensePoint1").X, GetPosition("P8_DefensePoint1").Y, 2000, 1, 8)
	if (_number < 2) and not P8UADef1.switched then
		P8UADef1.switched =  true
		P8UADef1.DefPos = GetPosition("backerDef1")
	end
	local _number, _, _ = Logic.GetPlayerEntitiesInArea(8, Entities.PB_Tower2, GetPosition("P8_DefensePoint2").X, GetPosition("P8_DefensePoint2").Y, 2000, 1, 8)
	if (_number < 2) and not P8UADef2.switched then
		P8UADef2.switched =  true
		P8UADef2.DefPos = GetPosition("backerDef2")
	end
	if P8UADef2.switched and P8UADef1.switched then
		return true
	end
	return false
end

function BetterDefBehavior(self)
	if self.CommandDelayer >= 0 then
		self.CommandDelayer = self.CommandDelayer -1
		return false
	else
		self.CommandDelayer = 5
	end
	if GetDistance(self:GetPosition(),self.DefPos) > 4000  then
		return false, UnlimitedArmy.CreateCommandFlee(self.DefPos)
	else
		if self:IsIdle() then
			return false, UnlimitedArmy.CreateCommandMove(self.DefPos)
		end
	end
	return false
end