createArmyEnemyCastle = function()

ArmyEnemyCastle1 = UnlimitedArmy.New({
					Player = 5,
					Area = 3500,
					AutoDestroyIfEmpty = false,
					TransitAttackMove = true,
					Formation = UnlimitedArmy.Formations.Lines,
					LeaderFormation = 4,
					AIActive = true,
					AutoRotateRange = true,
					DoNotNormalizeSpeed = true,
					})

armyrespawncounting = 20
armychecktable = {}

	
		
		StartJob("ControlArmyEnemyCastle")
		StartSimpleJob("Check_ArmySoldiers")

	--	static one
	--	----------

	--	set up

		armyDefendEnemyCastle				= {}
	
		armyDefendEnemyCastle.player 		= 5
		armyDefendEnemyCastle.id			= 9
		armyDefendEnemyCastle.strength		= 8
		armyDefendEnemyCastle.position		= GetPosition("enemyCastle")
		armyDefendEnemyCastle.rodeLength	= 3000
		armyDefendEnemyCastle.control 		= {}
		armyDefendEnemyCastle.timer 		= 0
		
		SetupArmy(armyDefendEnemyCastle)

	--	create

		local troopDescription = {
		
			maxNumberOfSoldiers	= 8,
			minNumberOfSoldiers	= 2,
			experiencePoints 	= VERYHIGH_EXPERIENCE,
		}				


		troopDescription.leaderType = Entities.PV_Cannon3			
		EnlargeArmy(armyDefendEnemyCastle,troopDescription)
		EnlargeArmy(armyDefendEnemyCastle,troopDescription)
		
		troopDescription.leaderType = Entities.PU_LeaderHeavyCavalry2
		EnlargeArmy(armyDefendEnemyCastle,troopDescription)			
		EnlargeArmy(armyDefendEnemyCastle,troopDescription)
		
		troopDescription.leaderType = Entities.PU_LeaderSword4
		EnlargeArmy(armyDefendEnemyCastle,troopDescription)
		EnlargeArmy(armyDefendEnemyCastle,troopDescription)
		
		troopDescription.leaderType = Entities.PU_LeaderBow4
		EnlargeArmy(armyDefendEnemyCastle,troopDescription)
		EnlargeArmy(armyDefendEnemyCastle,troopDescription)
	end

-----------------------------------------------------------------------------------------------------------------------	
--
--	JOB: "ControlArmyEnemyCastle"
--
-----------------------------------------------------------------------------------------------------------------------	
	-------------------------------------------------------------------------------------------------------------------
	Condition_ControlArmyEnemyCastle = function()
	-------------------------------------------------------------------------------------------------------------------
		
		return true
		
		end
		
	-------------------------------------------------------------------------------------------------------------------
	Action_ControlArmyEnemyCastle = function()
	-------------------------------------------------------------------------------------------------------------------
		if ArmyEnemyCastle1:GetSize(true) <= 5 then
			armyrespawncounting = armyrespawncounting - 1
			if armyrespawncounting == 0 then
				ArmyEnemyCastle1:ClearCommandQueue()
				for ij = 1,10,1 do
					local randomgennum = math.random(100)
					if randomgennum >= 89  then
						ArmyEnemyCastle1:CreateLeaderForArmy(Entities.PV_Cannon4, 0, GetPosition("enemyCastle"), 0)
					elseif randomgennum >= 67 then
						ArmyEnemyCastle1:CreateLeaderForArmy(Entities.PU_LeaderSword4, 8, GetPosition("enemyCastle"), 3)
					elseif randomgennum >= 45 then
						ArmyEnemyCastle1:CreateLeaderForArmy(Entities.PU_LeaderBow4, 8, GetPosition("enemyCastle"), 3)
					elseif randomgennum >= 23 then
						ArmyEnemyCastle1:CreateLeaderForArmy(Entities.PU_LeaderPoleArm4, 8, GetPosition("enemyCastle"), 3)
					elseif randomgennum >= 1 then
						ArmyEnemyCastle1:CreateLeaderForArmy(Entities.PU_LeaderHeavyCavalry2, 3, GetPosition("enemyCastle"), 3)
					end
				end
				ArmyEnemyCastle1:AddCommandMove(GetPosition("HQ_Crawford"))
				ArmyEnemyCastle1:AddCommandWaitForIdle(false)
				armyrespawncounting = 20
			end
		end
		
		if ArmyEnemyCastle1:IsDead() and not IsDead("enemyCastle") then
		
		ArmyEnemyCastle1:ClearCommandQueue()
		for ii = 1,3,1 do
		ArmyEnemyCastle1:CreateLeaderForArmy(Entities.PU_LeaderSword4, 8, GetPosition("enemyCastle"), 3)
		end
		for ii = 1,3,1 do
		ArmyEnemyCastle1:CreateLeaderForArmy(Entities.PU_LeaderBow4, 8, GetPosition("enemyCastle"), 3)
		end
		for ii = 1,2,1 do
		ArmyEnemyCastle1:CreateLeaderForArmy(Entities.PV_Cannon4, 0, GetPosition("enemyCastle"), 0)
		end
		for ii = 1,4,1 do
		ArmyEnemyCastle1:CreateLeaderForArmy(Entities.PU_LeaderPoleArm4, 8, GetPosition("enemyCastle"), 3)
		end
		for ii = 1,4,1 do
		ArmyEnemyCastle1:CreateLeaderForArmy(Entities.PU_LeaderHeavyCavalry2, 3, GetPosition("enemyCastle"), 3)
		end
		ArmyEnemyCastle1:AddCommandMove(GetPosition("HQ_Crawford"))
		ArmyEnemyCastle1:AddCommandWaitForIdle(false)
		armyrespawncounting = 20
		end
		
		
		if IsDead(armyDefendEnemyCastle) == true
		and IsDead("enemyCastle") == false
		then
		
		local troopDescription = {
		
			maxNumberOfSoldiers	= 9,
			minNumberOfSoldiers	= 2,
			experiencePoints 	= VERYHIGH_EXPERIENCE,
		}				

		troopDescription.leaderType = Entities.PV_Cannon3			
		EnlargeArmy(armyDefendEnemyCastle,troopDescription)
		EnlargeArmy(armyDefendEnemyCastle,troopDescription)
		
		troopDescription.leaderType = Entities.PU_LeaderHeavyCavalry2
		EnlargeArmy(armyDefendEnemyCastle,troopDescription)			
		EnlargeArmy(armyDefendEnemyCastle,troopDescription)
		
		troopDescription.leaderType = Entities.PU_LeaderSword4
		EnlargeArmy(armyDefendEnemyCastle,troopDescription)
		EnlargeArmy(armyDefendEnemyCastle,troopDescription)
		
		troopDescription.leaderType = Entities.PU_LeaderBow4
		EnlargeArmy(armyDefendEnemyCastle,troopDescription)
		EnlargeArmy(armyDefendEnemyCastle,troopDescription)
		
		end
		

		return false
		
		end
		
Check_ArmySoldiers = function ()
    for hochzahl,id in ipairs(ArmyEnemyCastle1.Leaders) do
			if Logic.LeaderGetNumberOfSoldiers(id)<1 and Logic.LeaderGetMaxNumberOfSoldiers(id)>0 and not Table_Check(armychecktable, id) then
				table.insert(armychecktable, 1, {id,15})
				--armychecktable[hochzahl] = {id, 20}
			end
    end
	if table.getn(armychecktable) < 1 then
		return false
	else
		for ccounter = table.getn(armychecktable), 1,-1 do
			if armychecktable[ccounter][2] == 0 then
				if IsValid(armychecktable[ccounter][1]) then
				SetHealth(armychecktable[ccounter][1],0)
				end 
				table.remove(armychecktable, ccounter)
			else
				armychecktable[ccounter][2] = armychecktable[ccounter][2] - 1
			end
		end
	end
	return false
end

Table_Check = function(tableinsert,ida)
for k,tableinsertis in ipairs(tableinsert) do
    if tableinsertis[1] == ida then
      return true
    end
  end
  return false
end