-- UtilityFunctions
-- Author: linco
-- DateCreated: 2/13/2016 11:16:01 AM
--------------------------------------------------------------



------------------------------------------------Players Functions------------------------------------------------------

function GetPlayerByCivilization(civilizationType)--------------Get Player by civilizationType
	for _, pPlayer in pairs(Players) do
		if pPlayer:GetCivilizationType() == civilizationType then 
			return pPlayer
		end
	end
end




-------------If two players are AT WAR?

function PlayersAtWar(iPlayer,ePlayer)---------This function return a blooen.
	local iTeam = Teams[iPlayer:GetTeam()];
	local eTeamIndex = ePlayer:GetTeam();
	if iTeam:IsAtWar(eTeamIndex) then
	   return true;
	else
	   return false;
	end
end


---------If the AI player is at war with Human?

function PlayerAtWarWithHuman(player) ---------This function return a blooen.
	local CurrentPlayerTeam = Teams[player:GetTeam()]
	
	for playerID,HumanPlayer in pairs(Players) do
		if HumanPlayer:IsHuman() then
			
			
			local HumanPlayerTeamIndex = HumanPlayer:GetTeam()
			
			if CurrentPlayerTeam:IsAtWar(HumanPlayerTeamIndex) then
				print ("Human is at war with this AI!")
			   return true
			else
			   return false
			end
			
			break
		end	
	end	
end



---------If the AI has the chance to become the Boss?
function AICanBeBoss (player)----------This function return a blooen.

local WorldCityTotal = Game.GetNumCities() 
local WorldPopTotal = Game.GetTotalPopulation()

local AICityCount = player:GetNumCities()
local AIPopCount = player:GetTotalPopulation()
local AITourismOutput = player:GetTourism()

--print ("World Cities Count:"..WorldCityTotal)
--print ("World Population Count:"..WorldPopTotal)
--

	if player:IsHuman() then
		print ("Human is always boss!")
		return true
	end
	
	if player:IsBarbarian() or player:IsMinorCiv() then
		print ("Minors are Not available!")
    	return
	end	
		
		
	if AICityCount <= 1 or AIPopCount <= 1 then  
		return false
	end
	
	

	if AICityCount >= 10 or AICityCount >= WorldCityTotal/10 or AIPopCount >= WorldPopTotal/10 or AITourismOutput > 500 then
		print ("This AI can become a Boss!")
		return true
	else	
		return false
	end
end



-- SharedFunctions
-- Author: shao@shao
-- DateCreated: 7/23/2017 11:16:01 AM
--------------------------------------------------------------



----------Evalue player acoording to city-nums,population and unit-num.
function EvaluePower(player)--------------return a int between 0 and 1 .and a int.
local WorldPlayerTotal = 0
local WorldCityTotal = 0
local WorldPopTotal = 0
local WorldMiliMightTotal = 0

for _, pPlayer in pairs(Players) do
	if not pPlayer:IsMinorCiv() and not pPlayer:IsBarbarian() then
	 WorldPlayerTotal =  WorldPlayerTotal + 1
	 WorldCityTotal = WorldCityTotal + pPlayer:GetNumCities()
	 WorldPopTotal = WorldPopTotal + pPlayer:GetTotalPopulation()
	 WorldMiliMightTotal = pPlayer:GetMilitaryMight() + WorldMiliMightTotal
	end
end

local CityCount = player:GetNumCities()
local PopCount = player:GetTotalPopulation()
local MilitaryMight = player:GetMilitaryMight()
if WorldMiliMightTotal > MilitaryMight then
local ValueStrong = (CityCount/WorldCityTotal)*0.4 + (PopCount/WorldPopTotal)*0.4 +( MilitaryMight/WorldMiliMightTotal)*0.2
local ValuePower = ValueStrong * WorldPlayerTotal
	return ValueStrong,ValuePower
else
local ValueStrong = (CityCount/WorldCityTotal)*0.6 + (PopCount/WorldPopTotal)*0.4
local ValuePower = ValueStrong * WorldPlayerTotal
	return ValueStrong,ValuePower
end
end



-------Get player Name
function GetPlayerName(player)	
 	if player:IsMinorCiv() then
 		local minorCivType = player:GetMinorCivType();
 		local minorCivInfo = GameInfo.MinorCivilizations[minorCivType];
 		return Locale.ConvertTextKey(minorCivInfo.Description);
 	end
 	local iCivType = player:GetCivilizationType();
 	local civInfo = GameInfo.Civilizations[iCivType];			
 	return Locale.ConvertTextKey(civInfo.Description);	
 end