function UniteBonus(PlayerID)
local pPlayer = Players[PlayerID]
local UnityBonus1ID = GameInfo.UnitPromotions["PROMOTION_UNITE_BONUS_1"].ID
local UnityBonus2ID = GameInfo.UnitPromotions["PROMOTION_UNITE_BONUS_2"].ID
local UnityBonus3ID = GameInfo.UnitPromotions["PROMOTION_UNITE_BONUS_3"].ID
local UnityBonus4ID = GameInfo.UnitPromotions["PROMOTION_UNITE_BONUS_4"].ID
---------------------Run only when YAN player do turn-----------------------
--print ("UnityBonus is loaded")
if not pPlayer or not pPlayer:IsEverAlive() or (pPlayer:GetCivilizationType()~=GameInfoTypes.CIVILIZATION_YAN) then
	return
end
--print ("UnityBonus has found YAN")
local myTeamID = pPlayer:GetTeam()
local maxNumWar=0
------------------Find out the max number of enemy's enemy---------------
for _, player in pairs(Players) do
--print (pPlayer:GetWorstEnemyName())
	if player and player:IsEverAlive() and not player:IsBarbarian() then
		local pTeam = Teams[player:GetTeam()]
		if pTeam:IsAtWar(myTeamID) then
			iNumWithWar = pTeam:GetAtWarCount(true)
			if iNumWithWar>maxNumWar then
				maxNumWar = iNumWithWar
			end
		end
	end
end
--print ("maxNumWar is "..maxNumWar)
-----------------Give the Melee /Gun and Mountied/Armor units corresponding promotion according to the maxNumWar-------------------
	--[[ nothing for 1 
		"PROMOTION_UNITE_BONUS_1" for 2 ;
		"PROMOTION_UNITE_BONUS_2" for 3 ;
		"PROMOTION_UNITE_BONUS_3" for 4 ;
		"PROMOTION_UNITE_BONUS_4" for 5 or more ]]
if (maxNumWar < 2 ) then
		for unit in pPlayer:Units() do
			if (unit:GetUnitCombatType() == GameInfoTypes.UNITCOMBAT_MELEE) or (unit:GetUnitCombatType() == GameInfoTypes.UNITCOMBAT_MOUNTED) or (unit:GetUnitCombatType() == GameInfoTypes.UNITCOMBAT_GUN) or (unit:GetUnitCombatType() == GameInfoTypes.UNITCOMBAT_ARMOR) then
				if unit:IsHasPromotion(UnityBonus1ID) then
				unit:SetHasPromotion(UnityBonus1ID,false)
				end
				if unit:IsHasPromotion(UnityBonus2ID) then
				unit:SetHasPromotion(UnityBonus2ID,false)
				end
				if unit:IsHasPromotion(UnityBonus3ID) then
				unit:SetHasPromotion(UnityBonus3ID,false)
				end
				if unit:IsHasPromotion(UnityBonus4ID) then
				unit:SetHasPromotion(UnityBonus4ID,false)
				end
			end
		end
		return
elseif (maxNumWar == 2) then 
		for unit in pPlayer:Units() do
			if (unit:GetUnitCombatType() == GameInfoTypes.UNITCOMBAT_MELEE) or (unit:GetUnitCombatType() == GameInfoTypes.UNITCOMBAT_MOUNTED) or (unit:GetUnitCombatType() == GameInfoTypes.UNITCOMBAT_GUN) or (unit:GetUnitCombatType() == GameInfoTypes.UNITCOMBAT_ARMOR) then
				if  not unit:IsHasPromotion(UnityBonus1ID) then
				unit:SetHasPromotion(UnityBonus1ID,true)
				end
				if unit:IsHasPromotion(UnityBonus2ID) then
				unit:SetHasPromotion(UnityBonus2ID,false)
				end
				if unit:IsHasPromotion(UnityBonus3ID) then
				unit:SetHasPromotion(UnityBonus3ID,false)
				end
				if unit:IsHasPromotion(UnityBonus4ID) then
				unit:SetHasPromotion(UnityBonus4ID,false)
				end
			end
		end
		return
elseif (maxNumWar == 3) then 
		for unit in pPlayer:Units() do
			if (unit:GetUnitCombatType() == GameInfoTypes.UNITCOMBAT_MELEE) or (unit:GetUnitCombatType() == GameInfoTypes.UNITCOMBAT_MOUNTED) or (unit:GetUnitCombatType() == GameInfoTypes.UNITCOMBAT_GUN) or (unit:GetUnitCombatType() == GameInfoTypes.UNITCOMBAT_ARMOR) then
				if unit:IsHasPromotion(UnityBonus1ID) then
				unit:SetHasPromotion(UnityBonus1ID,false)
				end
				if  not unit:IsHasPromotion(UnityBonus2ID) then
				unit:SetHasPromotion(UnityBonus2ID,true)
				end
				if unit:IsHasPromotion(UnityBonus3ID) then
				unit:SetHasPromotion(UnityBonus3ID,false)
				end
				if unit:IsHasPromotion(UnityBonus4ID) then
				unit:SetHasPromotion(UnityBonus4ID,false)
				end
			end
		end
		return
elseif maxNumWar == 4 then 
		for unit in pPlayer:Units() do
			if (unit:GetUnitCombatType() == GameInfoTypes.UNITCOMBAT_MELEE) or (unit:GetUnitCombatType() == GameInfoTypes.UNITCOMBAT_MOUNTED) or (unit:GetUnitCombatType() == GameInfoTypes.UNITCOMBAT_GUN) or (unit:GetUnitCombatType() == GameInfoTypes.UNITCOMBAT_ARMOR) then
				if unit:IsHasPromotion(UnityBonus1ID) then
				unit:SetHasPromotion(UnityBonus1ID,false)
				end
				if unit:IsHasPromotion(UnityBonus2ID) then
				unit:SetHasPromotion(UnityBonus2ID,false)
				end
				if  not unit:IsHasPromotion(UnityBonus3ID) then
				unit:SetHasPromotion(UnityBonus3ID,true)
				end
				if unit:IsHasPromotion(UnityBonus4ID) then
				unit:SetHasPromotion(UnityBonus4ID,false)
				end
			end
		end
		return
elseif maxNumWar == 5 or maxNumWar > 5 then 
		for unit in pPlayer:Units() do
			if (unit:GetUnitCombatType() == GameInfoTypes.UNITCOMBAT_MELEE) or (unit:GetUnitCombatType() == GameInfoTypes.UNITCOMBAT_MOUNTED) or (unit:GetUnitCombatType() == GameInfoTypes.UNITCOMBAT_GUN) or (unit:GetUnitCombatType() == GameInfoTypes.UNITCOMBAT_ARMOR) then
				if unit:IsHasPromotion(UnityBonus1ID) then
				unit:SetHasPromotion(UnityBonus1ID,false)
				end
				if unit:IsHasPromotion(UnityBonus2ID) then
				unit:SetHasPromotion(UnityBonus2ID,false)
				end
				if unit:IsHasPromotion(UnityBonus3ID) then
				unit:SetHasPromotion(UnityBonus3ID,false)
				end
				if  not unit:IsHasPromotion(UnityBonus4ID) then
				unit:SetHasPromotion(UnityBonus4ID,true)
				end
			end
		end
end
	
end
GameEvents.PlayerDoTurn.Add(UniteBonus)