
function UpdateUnitCap()
    # If we are asked to share out unit cap for the defeated army, do the following...
#    if not ScenarioInfo.Options.DoNotShareUnitCap then
        local aliveCount = 0
		local deadCount = 0
        for k,brain in ArmyBrains do
			if not ArmyIsCivilian(brain:GetArmyIndex()) then
				deadCount = deadCount + 1
			end
            if not brain:IsDefeated() and not ArmyIsCivilian(brain:GetArmyIndex()) then
                aliveCount = aliveCount + 1
            end
        end
        if aliveCount > 0 then
            local initialCap = tonumber(ScenarioInfo.Options.UnitCap)
            local totalCap = deadCount * initialCap
            for k,brain in ArmyBrains do
                if not brain:IsDefeated() then
                    SetArmyUnitCap(brain:GetArmyIndex(),math.floor(totalCap / aliveCount))
                end
            end
        end
#    end
end

