#****************************************************************************
#**
#**  File     :  /lua/editor/UnitCountBuildConditions.lua
#**  Author(s): Dru Staltman, John Comes
#**
#**  Summary  : Generic AI Platoon Build Conditions
#**             Build conditions always return true or false
#**
#**  Copyright © 2005 Gas Powered Games, Inc.  All rights reserved.
#****************************************************************************
local AIUtils = import('/lua/ai/aiutilities.lua')

function CanUpgrade(aiBrain, upgrade)
    local units = aiBrain:GetListOfUnits( categories.COMMAND, false )
    for k,v in units do
       	
       	if v:HasEnhancement( upgrade ) then
    			return false
    		end
    		
    		local bp = v:GetBlueprint()
       	if bp == nil then
       		return false
       	end
       	
       	local enh = bp.Enhancements[upgrade]
       	local econ = AIUtils.AIGetEconomyNumbers(aiBrain)
    		
    		if enh == nil then
    			return false
    			
    		elseif not v:HasEnhancement( enh.Prerequisite ) and enh.Prerequisite != nil then
    			return false
    		
    		elseif (econ.MassTrend*3 > enh.BuildCostMass/enh.BuildTime
    		and econ.EnergyTrend*3 > enh.BuildCostEnergy/enh.BuildTime ) then
        		#LOG('TRUE:  ' .. upgrade .. '  ' .. aiBrain.Nickname .. '  ' .. aiBrain.Name)
        		return true
    		elseif (econ.MassStorage >= enh.BuildCostMass/2
    		and econ.EnergyStorage >= enh.BuildCostEnergy/2 ) then
        		#LOG('TRUE2:  '.. upgrade .. '  ' .. aiBrain.Nickname .. '  ' .. aiBrain.Name)
        		return true
        elseif aiBrain:GetCurrentUnits( categories.TECH2 ) > 20 or aiBrain:GetCurrentUnits( categories.TECH3 ) > 5 then
        		#LOG('Tech:  '.. upgrade .. '  ' .. aiBrain.Nickname .. '  ' .. aiBrain.Name)
        		#LOG(aiBrain:GetCurrentUnits( categories.TECH2 ))
        		#LOG(aiBrain:GetCurrentUnits( categories.TECH3 ))
        		return true
        end
    end
    return false
end