local ANTIUNITEDID = GameInfo.UnitPromotions["PROMOTION_ANTI_UNITE_D"].ID
local ANTIUNITECID = GameInfo.UnitPromotions["PROMOTION_ANTI_UNITE_C"].ID
local ANTIUNITEAID = GameInfo.UnitPromotions["PROMOTION_ANTI_UNITE_A"].ID
local ANTIUNITESID = GameInfo.UnitPromotions["PROMOTION_ANTI_UNITE_S"].ID
function AntiUnite(playerID)

local player = Players[playerID]

---------------------Run only when Qin player do turn-----------------------

if not player or not player:IsEverAlive() or player:GetCivilizationType()~=GameInfoTypes.CIVILIZATION_QIN then
	return
end
------------------Find out the number of enemy of QIN---------------
local pteam = Teams[player:GetTeam()]
local iNumWithWar = pteam:GetAtWarCount(true)

-----------------Give the Archer corresponding promotion according to the QIN's enemy number-------------------
	--[[ nothing for 1 enemy
		"PROMOTION_ANTI_UNITE_D" for 2 enemy;
		"PROMOTION_ANTI_UNITE_C" for 3 enemy;
		"PROMOTION_ANTI_UNITE_A" for 4 enemy;
		"PROMOTION_ANTI_UNITE_S" for 5 or more enemy]]
	if (iNumWithWar < 2 ) then
		for unit in player:Units() do
			if (unit:GetUnitCombatType() == GameInfoTypes.UNITCOMBAT_ARCHER) or (unit:GetUnitCombatType() == GameInfoTypes.UNITCOMBAT_SIEGE) then
				if unit:IsHasPromotion(ANTIUNITEDID) then
				unit:SetHasPromotion(ANTIUNITEDID,false)
				end
				if unit:IsHasPromotion(ANTIUNITEAID) then
				unit:SetHasPromotion(ANTIUNITEAID,false)
				end
				if unit:IsHasPromotion(ANTIUNITECID) then
				unit:SetHasPromotion(ANTIUNITECID,false)
				end
				if unit:IsHasPromotion(ANTIUNITESID) then
				unit:SetHasPromotion(ANTIUNITESID,false)
				end
			end
		end	
	return
	--[[if the number of enemy of QIN is 2,ARCHER will get PROMOTION_ANTI_UNITE_1]]
	elseif (iNumWithWar == 2) then 
		for unit in player:Units() do
			if (unit:GetUnitCombatType() == GameInfoTypes.UNITCOMBAT_ARCHER) or (unit:GetUnitCombatType() == GameInfoTypes.UNITCOMBAT_SIEGE) then
				if not unit:IsHasPromotion(ANTIUNITEDID) then
				unit:SetHasPromotion(ANTIUNITEDID,true)
				end
				if unit:IsHasPromotion(ANTIUNITEAID) then
				unit:SetHasPromotion(ANTIUNITEAID,false)
				end
				if unit:IsHasPromotion(ANTIUNITECID) then
				unit:SetHasPromotion(ANTIUNITECID,false)
				end
				if unit:IsHasPromotion(ANTIUNITESID) then
				unit:SetHasPromotion(ANTIUNITESID,false)
				end
			end
		end	
	return
	
	--[[if the number of enemy of QIN is 3,ARCHER will get PROMOTION_ANTI_UNITE_2]]
	elseif iNumWithWar == 3 then 
		for unit in player:Units() do
			if (unit:GetUnitCombatType() == GameInfoTypes.UNITCOMBAT_ARCHER) or (unit:GetUnitCombatType() == GameInfoTypes.UNITCOMBAT_SIEGE) then
				if not unit:IsHasPromotion(ANTIUNITECID) then
				unit:SetHasPromotion(ANTIUNITECID,true)
				end
				if unit:IsHasPromotion(ANTIUNITEAID) then
				unit:SetHasPromotion(ANTIUNITEAID,false)
				end
				if unit:IsHasPromotion(ANTIUNITESID) then
				unit:SetHasPromotion(ANTIUNITESID,false)
				end
				if unit:IsHasPromotion(ANTIUNITEDID) then
				unit:SetHasPromotion(ANTIUNITEDID,false)
				end
			end
		end	
	return
	--[[if the number of enemy of QIN is 4,ARCHER will get PROMOTION_ANTI_UNITE_3]]
	elseif iNumWithWar == 4 then 
		for unit in player:Units() do
			if (unit:GetUnitCombatType() == GameInfoTypes.UNITCOMBAT_ARCHER) or (unit:GetUnitCombatType() == GameInfoTypes.UNITCOMBAT_SIEGE) then
				if not unit:IsHasPromotion(ANTIUNITEAID) then
				unit:SetHasPromotion(ANTIUNITEAID,true)
				end
				if unit:IsHasPromotion(ANTIUNITESID) then
				unit:SetHasPromotion(ANTIUNITESID,false)
				end
				if unit:IsHasPromotion(ANTIUNITECID) then
				unit:SetHasPromotion(ANTIUNITECID,false)
				end
				if unit:IsHasPromotion(ANTIUNITEDID) then
				unit:SetHasPromotion(ANTIUNITEDID,false)
				end
			end
		end	
	return
	--[[if the number of enemy of QIN is up to 5,ARCHER will get PROMOTION_ANTI_UNITE_4]]
	elseif iNumWithWar == 5 or iNumWithWar > 5 then 
		for unit in player:Units() do
			if (unit:GetUnitCombatType() == GameInfoTypes.UNITCOMBAT_ARCHER) or (unit:GetUnitCombatType() == GameInfoTypes.UNITCOMBAT_SIEGE) then
				if not unit:IsHasPromotion(ANTIUNITESID) then
				unit:SetHasPromotion(ANTIUNITESID,true)
				end
				if unit:IsHasPromotion(ANTIUNITEAID) then
				unit:SetHasPromotion(ANTIUNITEAID,false)
				end
				if unit:IsHasPromotion(ANTIUNITECID) then
				unit:SetHasPromotion(ANTIUNITECID,false)
				end
				if unit:IsHasPromotion(ANTIUNITEDID) then
				unit:SetHasPromotion(ANTIUNITEDID,false)
				end
			end
		end
	end
end

GameEvents.PlayerDoTurn.Add(AntiUnite)