void CvPolicyAI::DoChooseIdeology(CvPlayer *pPlayer)
{
    int iFreedomPriority = 0;
    int iAutocracyPriority = 0;
    int iOrderPriority = 0;
    int iFreedomMultiplier = 1;
    int iAutocracyMultiplier = 1;
    int iOrderMultiplier = 1;
    PolicyBranchTypes eFreedomBranch = (PolicyBranchTypes)GC.getPOLICY_BRANCH_FREEDOM();
    PolicyBranchTypes eAutocracyBranch = (PolicyBranchTypes)GC.getPOLICY_BRANCH_AUTOCRACY();
    PolicyBranchTypes eOrderBranch = (PolicyBranchTypes)GC.getPOLICY_BRANCH_ORDER();
    if (eFreedomBranch == NO_POLICY_BRANCH_TYPE || eAutocracyBranch == NO_POLICY_BRANCH_TYPE || eOrderBranch == NO_POLICY_BRANCH_TYPE)
    {
        return;
    }

    // First consideration is our victory type
    int iConquestPriority = max(0, pPlayer->GetGrandStrategyAI()->GetConquestPriority());
    int iDiploPriority = max(0, pPlayer->GetGrandStrategyAI()->GetUnitedNationsPriority());
    int iTechPriority = max(0, pPlayer->GetGrandStrategyAI()->GetSpaceshipPriority());
    int iCulturePriority = max(0, pPlayer->GetGrandStrategyAI()->GetCulturePriority());

    // Rule out one ideology if we are clearly (at least 25% more priority) going for the victory this ideology doesn't support
    int iClearPrefPercent = GC.getIDEOLOGY_PERCENT_CLEAR_VICTORY_PREF();
    if (iConquestPriority > (iDiploPriority   * (100 + iClearPrefPercent) / 100) &&
            iConquestPriority > (iTechPriority    * (100 + iClearPrefPercent) / 100) &&
            iConquestPriority > (iCulturePriority * (100 + iClearPrefPercent) / 100))
    {
        iFreedomMultiplier = 0;
    }
    else if (iDiploPriority > (iConquestPriority * (100 + iClearPrefPercent) / 100) &&
             iDiploPriority > (iTechPriority     * (100 + iClearPrefPercent) / 100) &&
             iDiploPriority > (iCulturePriority  * (100 + iClearPrefPercent) / 100))
    {
        iOrderMultiplier = 0;
    }
    else if (iTechPriority > (iConquestPriority * (100 + iClearPrefPercent) / 100) &&
             iTechPriority > (iDiploPriority    * (100 + iClearPrefPercent) / 100) &&
             iTechPriority > (iCulturePriority  * (100 + iClearPrefPercent) / 100))
    {
        iAutocracyMultiplier = 0;
    }

    int iFreedomTotal = iDiploPriority + iTechPriority + iCulturePriority;
    int iAutocracyTotal = iDiploPriority + iConquestPriority + iCulturePriority;
    int iOrderTotal = iTechPriority + iConquestPriority + iCulturePriority;
    int iGrandTotal = iFreedomTotal + iAutocracyTotal + iOrderTotal;

    if (iGrandTotal > 0)
    {
        int iPriorityToDivide = GC.getIDEOLOGY_SCORE_GRAND_STRATS();
        iFreedomPriority = (iFreedomTotal * iPriorityToDivide) / iGrandTotal;
        iAutocracyPriority = (iAutocracyTotal * iPriorityToDivide) / iGrandTotal;
        iOrderPriority = (iOrderTotal * iPriorityToDivide) / iGrandTotal;
    }

    CvString stage = "After Grand Strategies";
    LogIdeologyChoice(stage, iFreedomPriority, iAutocracyPriority, iOrderPriority);

    // Next look at free policies we can get
    iFreedomPriority += PolicyHelpers::GetNumFreePolicies(eFreedomBranch) * GC.getIDEOLOGY_SCORE_PER_FREE_TENET();
    iAutocracyPriority += PolicyHelpers::GetNumFreePolicies(eAutocracyBranch) * GC.getIDEOLOGY_SCORE_PER_FREE_TENET();
    iOrderPriority += PolicyHelpers::GetNumFreePolicies(eOrderBranch) * GC.getIDEOLOGY_SCORE_PER_FREE_TENET();;

    stage = "After Free Policies";
    LogIdeologyChoice(stage, iFreedomPriority, iAutocracyPriority, iOrderPriority);

    // Finally see what our friends (and enemies) have already chosen
    PlayerTypes eLoopPlayer;
    for (int iPlayerLoop = 0; iPlayerLoop < MAX_MAJOR_CIVS; iPlayerLoop++)
    {
        eLoopPlayer = (PlayerTypes) iPlayerLoop;
        if (eLoopPlayer != pPlayer->GetID() && pPlayer->GetDiplomacyAI()->IsPlayerValid(eLoopPlayer))
        {
            CvPlayer &kOtherPlayer = GET_PLAYER(eLoopPlayer);
            PolicyBranchTypes eOtherPlayerIdeology;
            eOtherPlayerIdeology = kOtherPlayer.GetPlayerPolicies()->GetLateGamePolicyTree();

            switch(pPlayer->GetDiplomacyAI()->GetMajorCivApproach(eLoopPlayer, /*bHideTrueFeelings*/ true))
            {
            case MAJOR_CIV_APPROACH_HOSTILE:
                if (eOtherPlayerIdeology == eFreedomBranch)
                {
                    iAutocracyPriority += GC.getIDEOLOGY_SCORE_HOSTILE();
                    iOrderPriority += GC.getIDEOLOGY_SCORE_HOSTILE();
                }
                else if (eOtherPlayerIdeology == eAutocracyBranch)
                {
                    iFreedomPriority += GC.getIDEOLOGY_SCORE_HOSTILE();
                    iOrderPriority += GC.getIDEOLOGY_SCORE_HOSTILE();
                }
                else if (eOtherPlayerIdeology == eOrderBranch)
                {
                    iAutocracyPriority += GC.getIDEOLOGY_SCORE_HOSTILE();;
                    iFreedomPriority += GC.getIDEOLOGY_SCORE_HOSTILE();
                }
                break;
            case MAJOR_CIV_APPROACH_GUARDED:
                if (eOtherPlayerIdeology == eFreedomBranch)
                {
                    iAutocracyPriority += GC.getIDEOLOGY_SCORE_GUARDED();
                    iOrderPriority += GC.getIDEOLOGY_SCORE_GUARDED();
                }
                else if (eOtherPlayerIdeology == eAutocracyBranch)
                {
                    iFreedomPriority += GC.getIDEOLOGY_SCORE_GUARDED();
                    iOrderPriority += GC.getIDEOLOGY_SCORE_GUARDED();
                }
                else if (eOtherPlayerIdeology == eOrderBranch)
                {
                    iAutocracyPriority += GC.getIDEOLOGY_SCORE_GUARDED();
                    iFreedomPriority += GC.getIDEOLOGY_SCORE_GUARDED();
                }
                break;
            case MAJOR_CIV_APPROACH_AFRAID:
                if (eOtherPlayerIdeology == eFreedomBranch)
                {
                    iFreedomPriority += GC.getIDEOLOGY_SCORE_AFRAID();
                }
                else if (eOtherPlayerIdeology == eAutocracyBranch)
                {
                    iAutocracyPriority += GC.getIDEOLOGY_SCORE_AFRAID();
                }
                else if (eOtherPlayerIdeology == eOrderBranch)
                {
                    iOrderPriority += GC.getIDEOLOGY_SCORE_AFRAID();
                }
                break;
            case MAJOR_CIV_APPROACH_FRIENDLY:
                if (eOtherPlayerIdeology == eFreedomBranch)
                {
                    iFreedomPriority += GC.getIDEOLOGY_SCORE_FRIENDLY();
                }
                else if (eOtherPlayerIdeology == eAutocracyBranch)
                {
                    iAutocracyPriority += GC.getIDEOLOGY_SCORE_FRIENDLY();
                }
                else if (eOtherPlayerIdeology == eOrderBranch)
                {
                    iOrderPriority += GC.getIDEOLOGY_SCORE_FRIENDLY();
                }
                break;
            case MAJOR_CIV_APPROACH_NEUTRAL:
                // No changes
                break;
            }
        }
    }

    stage = "After Relations";
    LogIdeologyChoice(stage, iFreedomPriority, iAutocracyPriority, iOrderPriority);

    // Look at Happiness impacts
    int iHappinessModifier = GC.getIDEOLOGY_SCORE_HAPPINESS();

    // -- Happiness we could add through tenets
    int iHappinessDelta;
    int iHappinessPoliciesInBranch;
    iHappinessDelta = GetBranchBuildingHappiness(pPlayer, eFreedomBranch);
    iHappinessPoliciesInBranch = GetNumHappinessPolicies(pPlayer, eFreedomBranch);
    if (iHappinessPoliciesInBranch > 0)
    {
        iFreedomPriority += iHappinessDelta * iHappinessModifier / iHappinessPoliciesInBranch;
    }
    iHappinessDelta = GetBranchBuildingHappiness(pPlayer, eAutocracyBranch);
    iHappinessPoliciesInBranch = GetNumHappinessPolicies(pPlayer, eAutocracyBranch);
    if (iHappinessPoliciesInBranch > 0)
    {
        iAutocracyPriority += iHappinessDelta * iHappinessModifier / iHappinessPoliciesInBranch;
    }
    iHappinessDelta = GetBranchBuildingHappiness(pPlayer, eOrderBranch);
    iHappinessPoliciesInBranch = GetNumHappinessPolicies(pPlayer, eOrderBranch);
    if (iHappinessPoliciesInBranch > 0)
    {
        iOrderPriority += iHappinessDelta * iHappinessModifier / iHappinessPoliciesInBranch;
    }

    stage = "After Tenet Happiness Boosts";
    LogIdeologyChoice(stage, iFreedomPriority, iAutocracyPriority, iOrderPriority);

    // -- Happiness we'd lose through Public Opinion
    iHappinessDelta = max (0, 100 - pPlayer->GetCulture()->ComputeHypotheticalPublicOpinionUnhappiness(eFreedomBranch));
    iFreedomPriority += iHappinessDelta * iHappinessModifier;
    iHappinessDelta = max (0, 100 - pPlayer->GetCulture()->ComputeHypotheticalPublicOpinionUnhappiness(eAutocracyBranch));
    iAutocracyPriority += iHappinessDelta * iHappinessModifier;
    iHappinessDelta = max (0, 100 - pPlayer->GetCulture()->ComputeHypotheticalPublicOpinionUnhappiness(eOrderBranch));
    iOrderPriority += iHappinessDelta * iHappinessModifier;

    stage = "After Public Opinion Happiness";
    LogIdeologyChoice(stage, iFreedomPriority, iAutocracyPriority, iOrderPriority);

    // Small random add-on
    iFreedomPriority += GC.getGame().getJonRandNum(10, "Freedom random priority bump");
    iAutocracyPriority += GC.getGame().getJonRandNum(10, "Autocracy random priority bump");
    iOrderPriority += GC.getGame().getJonRandNum(10, "Order random priority bump");

    stage = "After Random (1 to 10)";
    LogIdeologyChoice(stage, iFreedomPriority, iAutocracyPriority, iOrderPriority);

    // Rule out any branches that are totally out of consideration
    iFreedomPriority = iFreedomPriority * iFreedomMultiplier;
    iAutocracyPriority = iAutocracyPriority * iAutocracyMultiplier;
    iOrderPriority = iOrderPriority * iOrderMultiplier;

    stage = "Final (after Clear Victory Preference)";
    LogIdeologyChoice(stage, iFreedomPriority, iAutocracyPriority, iOrderPriority);

    // Pick the ideology
    PolicyBranchTypes eChosenBranch;
    if (iFreedomPriority >= iAutocracyPriority && iFreedomPriority >= iOrderPriority)
    {
        eChosenBranch = eFreedomBranch;
    }
    else if (iAutocracyPriority >= iFreedomPriority && iAutocracyPriority >= iOrderPriority)
    {
        eChosenBranch = eAutocracyBranch;
    }
    else
    {
        eChosenBranch = eOrderBranch;
    }
    pPlayer->GetPlayerPolicies()->SetPolicyBranchUnlocked(eChosenBranch, true, false);
    LogBranchChoice(eChosenBranch);
}
void CvPolicyAI::DoChooseIdeology(CvPlayer *pPlayer)
{
	int iFreedomPriority = 0;
	int iAutocracyPriority = 0;
	int iOrderPriority = 0;
	int iFreedomMultiplier = 1;
	int iAutocracyMultiplier = 1;
	int iOrderMultiplier = 1;
	PolicyBranchTypes eFreedomBranch = (PolicyBranchTypes)GC.getPOLICY_BRANCH_FREEDOM();
	PolicyBranchTypes eAutocracyBranch = (PolicyBranchTypes)GC.getPOLICY_BRANCH_AUTOCRACY();
	PolicyBranchTypes eOrderBranch = (PolicyBranchTypes)GC.getPOLICY_BRANCH_ORDER();
	if (eFreedomBranch == NO_POLICY_BRANCH_TYPE || eAutocracyBranch == NO_POLICY_BRANCH_TYPE || eOrderBranch == NO_POLICY_BRANCH_TYPE)
	{
		return;
	}
#if defined(MOD_DIPLOMACY_CIV4_FEATURES)
	if(MOD_DIPLOMACY_CIV4_FEATURES)
	{
		if(GET_TEAM(pPlayer->getTeam()).IsVassalOfSomeone())
		{
			TeamTypes eMasterTeam = GET_TEAM(pPlayer->getTeam()).GetMaster();
			if(eMasterTeam != NO_TEAM)
			{
				// Loop through all players to see if they're on our team
				for(int iPlayerLoop = 0; iPlayerLoop < MAX_MAJOR_CIVS; iPlayerLoop++)
				{
					PlayerTypes eMaster = (PlayerTypes) iPlayerLoop;

					// Assumes one player per team for master
					if(GET_PLAYER(eMaster).getTeam() == GET_TEAM(eMasterTeam).GetID())
					{
						if(GET_PLAYER(eMaster).GetPlayerPolicies()->GetLateGamePolicyTree() != NO_POLICY_BRANCH_TYPE)
						{
							pPlayer->GetPlayerPolicies()->SetPolicyBranchUnlocked(GET_PLAYER(eMaster).GetPlayerPolicies()->GetLateGamePolicyTree(), true, false);
							LogBranchChoice(GET_PLAYER(eMaster).GetPlayerPolicies()->GetLateGamePolicyTree());
							return;
						}
					}
				}
			}
		}
	}
#endif

	// First consideration is our victory type
	int iConquestPriority = max(0, pPlayer->GetGrandStrategyAI()->GetConquestPriority());
	int iDiploPriority = max(0, pPlayer->GetGrandStrategyAI()->GetUnitedNationsPriority());
	int iTechPriority = max(0, pPlayer->GetGrandStrategyAI()->GetSpaceshipPriority());
	int iCulturePriority = max(0, pPlayer->GetGrandStrategyAI()->GetCulturePriority());
	
#if defined(MOD_EVENTS_IDEOLOGIES)
	if (MOD_EVENTS_IDEOLOGIES) {
		CvPlayerPolicies* pPolicies = pPlayer->GetPlayerPolicies();

		// Just jump on the band-wagon and hard code for three ideologies!!!
		if (!pPolicies->CanAdoptIdeology(eFreedomBranch)) {
			iFreedomMultiplier = 0;
		}
		if (!pPolicies->CanAdoptIdeology(eAutocracyBranch)) {
			iAutocracyMultiplier = 0;
		}
		if (!pPolicies->CanAdoptIdeology(eOrderBranch)) {
			iOrderMultiplier = 0;
		}
	}
#endif

#if defined(MOD_EVENTS_IDEOLOGIES)
	if (iFreedomMultiplier != 0 && iAutocracyMultiplier != 0 && iOrderMultiplier != 0) {
#endif
		// Rule out one ideology if we are clearly (at least 25% more priority) going for the victory this ideology doesn't support
		int iClearPrefPercent = GC.getIDEOLOGY_PERCENT_CLEAR_VICTORY_PREF();
		if (iConquestPriority > (iDiploPriority   * (100 + iClearPrefPercent) / 100) &&
			iConquestPriority > (iTechPriority    * (100 + iClearPrefPercent) / 100) &&
			iConquestPriority > (iCulturePriority * (100 + iClearPrefPercent) / 100))
		{
			iFreedomMultiplier = 0;
		}
		else if (iDiploPriority > (iConquestPriority * (100 + iClearPrefPercent) / 100) &&
				 iDiploPriority > (iTechPriority     * (100 + iClearPrefPercent) / 100) &&
				 iDiploPriority > (iCulturePriority  * (100 + iClearPrefPercent) / 100))
		{
			iOrderMultiplier = 0;
		}
		else if (iTechPriority > (iConquestPriority * (100 + iClearPrefPercent) / 100) &&
				 iTechPriority > (iDiploPriority    * (100 + iClearPrefPercent) / 100) &&
				 iTechPriority > (iCulturePriority  * (100 + iClearPrefPercent) / 100))
		{
			iAutocracyMultiplier = 0;
		}
#if defined(MOD_EVENTS_IDEOLOGIES)
	}
#endif

	int iFreedomTotal = iDiploPriority + iTechPriority + iCulturePriority;
	int iAutocracyTotal = iDiploPriority + iConquestPriority + iCulturePriority;
	int iOrderTotal = iTechPriority + iConquestPriority + iCulturePriority;
	int iGrandTotal = iFreedomTotal + iAutocracyTotal + iOrderTotal;

	if (iGrandTotal > 0)
	{
		int iPriorityToDivide = GC.getIDEOLOGY_SCORE_GRAND_STRATS();
		iFreedomPriority = (iFreedomTotal * iPriorityToDivide) / iGrandTotal;
		iAutocracyPriority = (iAutocracyTotal * iPriorityToDivide) / iGrandTotal;
		iOrderPriority = (iOrderTotal * iPriorityToDivide) / iGrandTotal;
	}

	CvString stage = "After Grand Strategies";
	LogIdeologyChoice(stage, iFreedomPriority, iAutocracyPriority, iOrderPriority);

	// Next look at free policies we can get
	iFreedomPriority += PolicyHelpers::GetNumFreePolicies(eFreedomBranch) * GC.getIDEOLOGY_SCORE_PER_FREE_TENET();
	iAutocracyPriority += PolicyHelpers::GetNumFreePolicies(eAutocracyBranch) * GC.getIDEOLOGY_SCORE_PER_FREE_TENET();
	iOrderPriority += PolicyHelpers::GetNumFreePolicies(eOrderBranch) * GC.getIDEOLOGY_SCORE_PER_FREE_TENET();;

	stage = "After Free Policies";
	LogIdeologyChoice(stage, iFreedomPriority, iAutocracyPriority, iOrderPriority);

	// Finally see what our friends (and enemies) have already chosen
	PlayerTypes eLoopPlayer;
	for (int iPlayerLoop = 0; iPlayerLoop < MAX_MAJOR_CIVS; iPlayerLoop++)
	{
		eLoopPlayer = (PlayerTypes) iPlayerLoop;
		if (eLoopPlayer != pPlayer->GetID() && pPlayer->GetDiplomacyAI()->IsPlayerValid(eLoopPlayer))
		{
			CvPlayer &kOtherPlayer = GET_PLAYER(eLoopPlayer);
			PolicyBranchTypes eOtherPlayerIdeology;
			eOtherPlayerIdeology = kOtherPlayer.GetPlayerPolicies()->GetLateGamePolicyTree();

			switch(pPlayer->GetDiplomacyAI()->GetMajorCivApproach(eLoopPlayer, /*bHideTrueFeelings*/ true))
			{
			case MAJOR_CIV_APPROACH_HOSTILE:
				if (eOtherPlayerIdeology == eFreedomBranch)
				{
					iAutocracyPriority += GC.getIDEOLOGY_SCORE_HOSTILE();
					iOrderPriority += GC.getIDEOLOGY_SCORE_HOSTILE();
				}
				else if (eOtherPlayerIdeology == eAutocracyBranch)
				{
					iFreedomPriority += GC.getIDEOLOGY_SCORE_HOSTILE();
					iOrderPriority += GC.getIDEOLOGY_SCORE_HOSTILE();
				}
				else if (eOtherPlayerIdeology == eOrderBranch)
				{
					iAutocracyPriority += GC.getIDEOLOGY_SCORE_HOSTILE();;
					iFreedomPriority += GC.getIDEOLOGY_SCORE_HOSTILE();
				}
				break;
			case MAJOR_CIV_APPROACH_GUARDED:
				if (eOtherPlayerIdeology == eFreedomBranch)
				{
					iAutocracyPriority += GC.getIDEOLOGY_SCORE_GUARDED();
					iOrderPriority += GC.getIDEOLOGY_SCORE_GUARDED();
				}
				else if (eOtherPlayerIdeology == eAutocracyBranch)
				{
					iFreedomPriority += GC.getIDEOLOGY_SCORE_GUARDED();
					iOrderPriority += GC.getIDEOLOGY_SCORE_GUARDED();
				}
				else if (eOtherPlayerIdeology == eOrderBranch)
				{
					iAutocracyPriority += GC.getIDEOLOGY_SCORE_GUARDED();
					iFreedomPriority += GC.getIDEOLOGY_SCORE_GUARDED();
				}
				break;
			case MAJOR_CIV_APPROACH_AFRAID:
				if (eOtherPlayerIdeology == eFreedomBranch)
				{
					iFreedomPriority += GC.getIDEOLOGY_SCORE_AFRAID();
				}
				else if (eOtherPlayerIdeology == eAutocracyBranch)
				{
					iAutocracyPriority += GC.getIDEOLOGY_SCORE_AFRAID();
				}
				else if (eOtherPlayerIdeology == eOrderBranch)
				{
					iOrderPriority += GC.getIDEOLOGY_SCORE_AFRAID();
				}
				break;
			case MAJOR_CIV_APPROACH_FRIENDLY:
				if (eOtherPlayerIdeology == eFreedomBranch)
				{
					iFreedomPriority += GC.getIDEOLOGY_SCORE_FRIENDLY();
				}
				else if (eOtherPlayerIdeology == eAutocracyBranch)
				{
					iAutocracyPriority += GC.getIDEOLOGY_SCORE_FRIENDLY();
				}
				else if (eOtherPlayerIdeology == eOrderBranch)
				{
					iOrderPriority += GC.getIDEOLOGY_SCORE_FRIENDLY();
				}
				break;
			case MAJOR_CIV_APPROACH_NEUTRAL:
				// No changes
				break;
			}
		}
	}

	stage = "After Relations";
	LogIdeologyChoice(stage, iFreedomPriority, iAutocracyPriority, iOrderPriority);

	// Look at Happiness impacts
	int iHappinessModifier = GC.getIDEOLOGY_SCORE_HAPPINESS();

	// -- Happiness we could add through tenets
	int iHappinessDelta;
#if defined(MOD_BALANCE_CORE)
	//This was a dumb reason to weigh an ideology branch.
#else
	int iHappinessPoliciesInBranch;
	iHappinessDelta = GetBranchBuildingHappiness(pPlayer, eFreedomBranch);
	iHappinessPoliciesInBranch = GetNumHappinessPolicies(pPlayer, eFreedomBranch);
	if (iHappinessPoliciesInBranch > 0)
	{
		iFreedomPriority += iHappinessDelta * iHappinessModifier / iHappinessPoliciesInBranch;		
	}
	iHappinessDelta = GetBranchBuildingHappiness(pPlayer, eAutocracyBranch);
	iHappinessPoliciesInBranch = GetNumHappinessPolicies(pPlayer, eAutocracyBranch);
	if (iHappinessPoliciesInBranch > 0)
	{
		iAutocracyPriority += iHappinessDelta * iHappinessModifier / iHappinessPoliciesInBranch;		
	}
	iHappinessDelta = GetBranchBuildingHappiness(pPlayer, eOrderBranch);
	iHappinessPoliciesInBranch = GetNumHappinessPolicies(pPlayer, eOrderBranch);
	if (iHappinessPoliciesInBranch > 0)
	{
		iOrderPriority += iHappinessDelta * iHappinessModifier / iHappinessPoliciesInBranch;		
	}

	stage = "After Tenet Happiness Boosts";
	LogIdeologyChoice(stage, iFreedomPriority, iAutocracyPriority, iOrderPriority);
#endif
#if defined(MOD_BALANCE_CORE)
	bool bFirstIdeology = true;
	for (int iPlayerLoop = 0; iPlayerLoop < MAX_MAJOR_CIVS; iPlayerLoop++)
	{
		eLoopPlayer = (PlayerTypes) iPlayerLoop;
		if (eLoopPlayer != pPlayer->GetID() && pPlayer->GetDiplomacyAI()->IsPlayerValid(eLoopPlayer))
		{
			CvPlayer &kOtherPlayer = GET_PLAYER(eLoopPlayer);
			PolicyBranchTypes eOtherPlayerIdeology;
			eOtherPlayerIdeology = kOtherPlayer.GetPlayerPolicies()->GetLateGamePolicyTree();
			if(eOtherPlayerIdeology != NO_POLICY_BRANCH_TYPE)
			{
				bFirstIdeology = false;
				break;
			}
		}
	}
	if(!bFirstIdeology)
	{
#endif
	// -- Happiness we'd lose through Public Opinion
	iHappinessDelta = max (0, 100 - pPlayer->GetCulture()->ComputeHypotheticalPublicOpinionUnhappiness(eFreedomBranch));
	iFreedomPriority += iHappinessDelta * iHappinessModifier;
	iHappinessDelta = max (0, 100 - pPlayer->GetCulture()->ComputeHypotheticalPublicOpinionUnhappiness(eAutocracyBranch));
	iAutocracyPriority += iHappinessDelta * iHappinessModifier;
	iHappinessDelta = max (0, 100 - pPlayer->GetCulture()->ComputeHypotheticalPublicOpinionUnhappiness(eOrderBranch));
	iOrderPriority += iHappinessDelta * iHappinessModifier;

	stage = "After Public Opinion Happiness";
	LogIdeologyChoice(stage, iFreedomPriority, iAutocracyPriority, iOrderPriority);
#if defined(MOD_BALANCE_CORE)
	}
#endif
#if defined(MOD_BALANCE_CORE)
#ifdef AUI_BINOM_RNG
	// bigger random add-on
	iFreedomPriority += GC.getGame().getJonRandNumBinom(60, "Freedom random priority bump");
	iAutocracyPriority += GC.getGame().getJonRandNumBinom(60, "Autocracy random priority bump");
	iOrderPriority += GC.getGame().getJonRandNumBinom(60, "Order random priority bump");
#else
	// bigger random add-on
	iFreedomPriority += GC.getGame().getJonRandNum(75, "Freedom random priority bump");
	iAutocracyPriority += GC.getGame().getJonRandNum(75, "Autocracy random priority bump");
	iOrderPriority += GC.getGame().getJonRandNum(75, "Order random priority bump");
#endif
#else
	// Small random add-on
	iFreedomPriority += GC.getGame().getJonRandNum(10, "Freedom random priority bump");
	iAutocracyPriority += GC.getGame().getJonRandNum(10, "Autocracy random priority bump");
	iOrderPriority += GC.getGame().getJonRandNum(10, "Order random priority bump");
#endif
	stage = "After Random (1 to 10)";
	LogIdeologyChoice(stage, iFreedomPriority, iAutocracyPriority, iOrderPriority);

	// Rule out any branches that are totally out of consideration
	iFreedomPriority = iFreedomPriority * iFreedomMultiplier;
	iAutocracyPriority = iAutocracyPriority * iAutocracyMultiplier;
	iOrderPriority = iOrderPriority * iOrderMultiplier;

	stage = "Final (after Clear Victory Preference)";
	LogIdeologyChoice(stage, iFreedomPriority, iAutocracyPriority, iOrderPriority);

	// Pick the ideology
	PolicyBranchTypes eChosenBranch;
	if (iFreedomPriority >= iAutocracyPriority && iFreedomPriority >= iOrderPriority)
	{
		eChosenBranch = eFreedomBranch;
	}
	else if (iAutocracyPriority >= iFreedomPriority && iAutocracyPriority >= iOrderPriority)
	{
		eChosenBranch = eAutocracyBranch;
	}
	else
	{
		eChosenBranch = eOrderBranch;
	}
	pPlayer->GetPlayerPolicies()->SetPolicyBranchUnlocked(eChosenBranch, true, false);
	LogBranchChoice(eChosenBranch);

#if defined(MOD_BUGFIX_MISSING_POLICY_EVENTS)
	if (MOD_BUGFIX_MISSING_POLICY_EVENTS)
	{
		ICvEngineScriptSystem1* pkScriptSystem = gDLL->GetScriptSystem();
		if(pkScriptSystem)
		{
			CvLuaArgsHandle args;
			args->Push(pPlayer->GetID());
			args->Push(eChosenBranch);

			bool bResult = false;
			LuaSupport::CallHook(pkScriptSystem, "PlayerAdoptPolicyBranch", args.get(), bResult);
		}
	}
#endif
}
/// Choose a player's next policy purchase (could be opening a branch)
int CvPolicyAI::ChooseNextPolicy(CvPlayer* pPlayer)
{
    RandomNumberDelegate fcn;
    fcn = MakeDelegate(&GC.getGame(), &CvGame::getJonRandNum);
    int iRtnValue = (int)NO_POLICY;
    int iPolicyLoop;
    vector<int> aLevel3Tenets;

    bool bMustChooseTenet = (pPlayer->GetNumFreeTenets() > 0);

    // Create a new vector holding only policies we can currently adopt
    m_AdoptablePolicies.clear();

    // Loop through adding the adoptable policies
    for(iPolicyLoop = 0; iPolicyLoop < m_pCurrentPolicies->GetPolicies()->GetNumPolicies(); iPolicyLoop++)
    {
        if(m_pCurrentPolicies->CanAdoptPolicy((PolicyTypes) iPolicyLoop) && (!bMustChooseTenet || m_pCurrentPolicies->GetPolicies()->GetPolicyEntry(iPolicyLoop)->GetLevel() > 0))
        {
            int iWeight = 0;

            iWeight += m_PolicyAIWeights.GetWeight(iPolicyLoop);

            // Does this policy finish a branch for us?
            if(m_pCurrentPolicies->WillFinishBranchIfAdopted((PolicyTypes) iPolicyLoop))
            {
                int iPolicyBranch = m_pCurrentPolicies->GetPolicies()->GetPolicyEntry(iPolicyLoop)->GetPolicyBranchType();
                if(iPolicyBranch != NO_POLICY_BRANCH_TYPE)
                {
                    int iFinisherPolicy = m_pCurrentPolicies->GetPolicies()->GetPolicyBranchEntry(iPolicyBranch)->GetFreeFinishingPolicy();
                    if(iFinisherPolicy != NO_POLICY)
                    {
                        iWeight += m_PolicyAIWeights.GetWeight(iFinisherPolicy);
                    }
                }
            }
            m_AdoptablePolicies.push_back(iPolicyLoop + GC.getNumPolicyBranchInfos(), iWeight);

            if (m_pCurrentPolicies->GetPolicies()->GetPolicyEntry(iPolicyLoop)->GetLevel() == 3)
            {
                aLevel3Tenets.push_back(iPolicyLoop);
            }
        }
    }

    // Did we already start a branch in the set that is mutually exclusive?
    bool bStartedAMutuallyExclusiveBranch = false;
    for(int iBranchLoop = 0; iBranchLoop < GC.getNumPolicyBranchInfos(); iBranchLoop++)
    {
        const PolicyBranchTypes ePolicyBranch = static_cast<PolicyBranchTypes>(iBranchLoop);
        CvPolicyBranchEntry* pkPolicyBranchInfo = GC.getPolicyBranchInfo(ePolicyBranch);
        if(pkPolicyBranchInfo)
        {
            if(pPlayer->GetPlayerPolicies()->IsPolicyBranchUnlocked(ePolicyBranch))
            {
                if(pkPolicyBranchInfo->IsMutuallyExclusive())
                {
                    bStartedAMutuallyExclusiveBranch = true;
                }
            }
        }
    }

    AIGrandStrategyTypes eCultureGrandStrategy = (AIGrandStrategyTypes) GC.getInfoTypeForString("AIGRANDSTRATEGY_CULTURE");
    AIGrandStrategyTypes eCurrentGrandStrategy = pPlayer->GetGrandStrategyAI()->GetActiveGrandStrategy();

    // Loop though the branches adding each as another possibility
    if (!bMustChooseTenet)
    {
        for(int iBranchLoop = 0; iBranchLoop < GC.getNumPolicyBranchInfos(); iBranchLoop++)
        {
            const PolicyBranchTypes ePolicyBranch = static_cast<PolicyBranchTypes>(iBranchLoop);
            CvPolicyBranchEntry* pkPolicyBranchInfo = GC.getPolicyBranchInfo(ePolicyBranch);
            if(pkPolicyBranchInfo)
            {
                if(bStartedAMutuallyExclusiveBranch && pkPolicyBranchInfo->IsMutuallyExclusive())
                {
                    continue;
                }

#if defined (JRMOD_C23)
                // Don't open a branch disabling an already opened branch
                if (pPlayer->GetPlayerPolicies()->IsPolicyBranchBlocked(ePolicyBranch))
                    continue;
#endif

                if(pPlayer->GetPlayerPolicies()->CanUnlockPolicyBranch(ePolicyBranch) && !pPlayer->GetPlayerPolicies()->IsPolicyBranchUnlocked(ePolicyBranch))
                {
                    int iBranchWeight = 0;

                    // Does this branch actually help us, based on game options?
                    if(IsBranchEffectiveInGame(ePolicyBranch))
                    {
                        iBranchWeight += WeighBranch(ePolicyBranch);

                        iBranchWeight *= (100 - m_iPolicyWeightPercentDropNewBranch);
                        iBranchWeight /= 100;
#if defined (JRMOD_C23)
//Following condition no longer makes sense since Cultural victory has changed but i let it since it can help the AI to focus on a tree.
#endif
                        if(eCurrentGrandStrategy == eCultureGrandStrategy)
                        {
                            iBranchWeight /= 3;
                        }
                    }

                    m_AdoptablePolicies.push_back(iBranchLoop, iBranchWeight);
                }
            }
        }
    }

    m_AdoptablePolicies.SortItems();
    LogPossiblePolicies();

    // If there were any Level 3 tenets found, consider going for the one that matches our victory strategy
    if (aLevel3Tenets.size() > 0)
    {
        vector<int>::const_iterator it;
        for (it = aLevel3Tenets.begin(); it != aLevel3Tenets.end(); it++)
        {
            CvPolicyEntry *pEntry;
            pEntry = m_pCurrentPolicies->GetPolicies()->GetPolicyEntry(*it);
            if (pEntry)
            {
                AIGrandStrategyTypes eGrandStrategy = pPlayer->GetGrandStrategyAI()->GetActiveGrandStrategy();
                if (eGrandStrategy == GC.getInfoTypeForString("AIGRANDSTRATEGY_CONQUEST"))
                {
                    if (pEntry->GetFlavorValue((FlavorTypes)GC.getInfoTypeForString("FLAVOR_OFFENSE")) > 0)
                    {
                        LogPolicyChoice((PolicyTypes)*it);
                        return (*it) + GC.getNumPolicyBranchInfos();
                    }
                }
                else if(eGrandStrategy == GC.getInfoTypeForString("AIGRANDSTRATEGY_SPACESHIP"))
                {
                    if (pEntry->GetFlavorValue((FlavorTypes)GC.getInfoTypeForString("FLAVOR_SPACESHIP")) > 0)
                    {
                        LogPolicyChoice((PolicyTypes)*it);
                        return (*it) + GC.getNumPolicyBranchInfos();
                    }
                }
                else if(eGrandStrategy == GC.getInfoTypeForString("AIGRANDSTRATEGY_UNITED_NATIONS"))
                {
                    if (pEntry->GetFlavorValue((FlavorTypes)GC.getInfoTypeForString("FLAVOR_DIPLOMACY")) > 0)
                    {
                        LogPolicyChoice((PolicyTypes)*it);
                        return (*it) + GC.getNumPolicyBranchInfos();
                    }
                }
                else if(eGrandStrategy == GC.getInfoTypeForString("AIGRANDSTRATEGY_CULTURE"))
                {
                    if (pEntry->GetFlavorValue((FlavorTypes)GC.getInfoTypeForString("FLAVOR_CULTURE")) > 0)
                    {
                        LogPolicyChoice((PolicyTypes)*it);
                        return (*it) + GC.getNumPolicyBranchInfos();
                    }
                }
            }
        }
    }

    CvAssertMsg(m_AdoptablePolicies.GetTotalWeight() >= 0, "Total weights of considered policies should not be negative! Please send Anton your save file and version.");

    // If total weight is above 0, choose one above a threshold
    if(m_AdoptablePolicies.GetTotalWeight() > 0)
    {
        int iNumChoices = GC.getGame().getHandicapInfo().GetPolicyNumOptions();
        iRtnValue = m_AdoptablePolicies.ChooseFromTopChoices(iNumChoices, &fcn, "Choosing policy from Top Choices");
    }
    // Total weight may be 0 if the only branches and policies left are ones that are ineffective in our game, but we gotta pick something
    else if(m_AdoptablePolicies.GetTotalWeight() == 0 && m_AdoptablePolicies.size() > 0)
    {
        iRtnValue = m_AdoptablePolicies.ChooseAtRandom(&fcn, "Choosing policy at random (no good choices)");
    }

    // Log our choice
    if(iRtnValue != (int)NO_POLICY)
    {
        if(iRtnValue >= GC.getNumPolicyBranchInfos())
        {
            LogPolicyChoice((PolicyTypes)(iRtnValue - GC.getNumPolicyBranchInfos()));
        }
        else
        {
            LogBranchChoice((PolicyBranchTypes)iRtnValue);
        }
    }

    return iRtnValue;
}
/// Choose a player's next policy purchase (could be opening a branch)
int CvPolicyAI::ChooseNextPolicy(CvPlayer* pPlayer)
{
	RandomNumberDelegate fcn;
	fcn = MakeDelegate(&GC.getGame(), &CvGame::getJonRandNum);
	int iRtnValue = (int)NO_POLICY;
	int iPolicyLoop;
	vector<int> aLevel3Tenets;

	bool bMustChooseTenet = (pPlayer->GetNumFreeTenets() > 0);

	// Create a new vector holding only policies we can currently adopt
	m_AdoptablePolicies.clear();

	// Loop through adding the adoptable policies
	for(iPolicyLoop = 0; iPolicyLoop < m_pCurrentPolicies->GetPolicies()->GetNumPolicies(); iPolicyLoop++)
	{
		if(m_pCurrentPolicies->CanAdoptPolicy((PolicyTypes) iPolicyLoop) && (!bMustChooseTenet || m_pCurrentPolicies->GetPolicies()->GetPolicyEntry(iPolicyLoop)->GetLevel() > 0))
		{
			int iWeight = 0;

			iWeight += m_PolicyAIWeights.GetWeight(iPolicyLoop);
#if defined(MOD_BALANCE_CORE)
			//If this is an ideology policy, let's snap those up.
			if(m_pCurrentPolicies->GetPolicies()->GetPolicyEntry(iPolicyLoop)->GetLevel() > 0)
			{
				iWeight *= (m_pCurrentPolicies->GetPolicies()->GetPolicyEntry(iPolicyLoop)->GetLevel() + 1);
			}
			//Older branches should be slowly phased out.
			PolicyBranchTypes ePolicyBranch = (PolicyBranchTypes)m_pCurrentPolicies->GetPolicies()->GetPolicyEntry(iPolicyLoop)->GetPolicyBranchType();
			if(ePolicyBranch != NO_POLICY_BRANCH_TYPE)
			{
				CvPolicyBranchEntry* pkPolicyBranchInfo = GC.getPolicyBranchInfo(ePolicyBranch);
				if(pkPolicyBranchInfo)
				{
					//If we're already in this branch, let's get a bonus based on how many we have in it (this will push the AI to finish branches quickly.
					if(m_pCurrentPolicies->GetNumPoliciesOwnedInBranch(ePolicyBranch) > 0 || m_pCurrentPolicies->IsPolicyBranchUnlocked(ePolicyBranch))
					{
						iWeight *= (m_pCurrentPolicies->GetNumPoliciesOwnedInBranch(ePolicyBranch) + 1);
					}
					else
					{
						int iPolicyEra = pkPolicyBranchInfo->GetEraPrereq();
						int iPlayerEra = pPlayer->GetCurrentEra();
						if(iPolicyEra < iPlayerEra)
						{
							iWeight /= max(1, (iPlayerEra - iPolicyEra));
						}
					}
				}
				if(ePolicyBranch == (PolicyBranchTypes)GC.getInfoTypeForString("POLICY_BRANCH_PIETY", true))
				{
					EconomicAIStrategyTypes eStrategyBuildingReligion = (EconomicAIStrategyTypes) GC.getInfoTypeForString("ECONOMICAISTRATEGY_DEVELOPING_RELIGION", true);
					bool bBuildingReligion = false;
					if (eStrategyBuildingReligion != NO_ECONOMICAISTRATEGY)
					{
						bBuildingReligion = pPlayer->GetEconomicAI()->IsUsingStrategy(eStrategyBuildingReligion);
					}
					if(!bBuildingReligion)
					{
						iWeight = 0;
					}
					else
					{
						iWeight *= 5;
					}
				}
			}
			
#endif

			// Does this policy finish a branch for us?
			if(m_pCurrentPolicies->WillFinishBranchIfAdopted((PolicyTypes) iPolicyLoop))
			{
				int iPolicyBranch = m_pCurrentPolicies->GetPolicies()->GetPolicyEntry(iPolicyLoop)->GetPolicyBranchType();
				if(iPolicyBranch != NO_POLICY_BRANCH_TYPE)
				{
					int iFinisherPolicy = m_pCurrentPolicies->GetPolicies()->GetPolicyBranchEntry(iPolicyBranch)->GetFreeFinishingPolicy();
					if(iFinisherPolicy != NO_POLICY)
					{
						iWeight += m_PolicyAIWeights.GetWeight(iFinisherPolicy);
					}
				}
			}
			m_AdoptablePolicies.push_back(iPolicyLoop + GC.getNumPolicyBranchInfos(), iWeight);

			if (m_pCurrentPolicies->GetPolicies()->GetPolicyEntry(iPolicyLoop)->GetLevel() == 3)
			{
				aLevel3Tenets.push_back(iPolicyLoop);
			}
		}
	}

	// Did we already start a branch in the set that is mutually exclusive?
	bool bStartedAMutuallyExclusiveBranch = false;
	for(int iBranchLoop = 0; iBranchLoop < GC.getNumPolicyBranchInfos(); iBranchLoop++)
	{
		const PolicyBranchTypes ePolicyBranch = static_cast<PolicyBranchTypes>(iBranchLoop);
		CvPolicyBranchEntry* pkPolicyBranchInfo = GC.getPolicyBranchInfo(ePolicyBranch);
		if(pkPolicyBranchInfo)
		{
			if(pPlayer->GetPlayerPolicies()->IsPolicyBranchUnlocked(ePolicyBranch))
			{
				if(pkPolicyBranchInfo->IsMutuallyExclusive())
				{
					bStartedAMutuallyExclusiveBranch = true;
				}
			}
		}
	}
#if defined(MOD_BALANCE_CORE)
						//Leftover from Vanilla victory
#else
	AIGrandStrategyTypes eCultureGrandStrategy = (AIGrandStrategyTypes) GC.getInfoTypeForString("AIGRANDSTRATEGY_CULTURE");
	AIGrandStrategyTypes eCurrentGrandStrategy = pPlayer->GetGrandStrategyAI()->GetActiveGrandStrategy();
#endif
	// Loop though the branches adding each as another possibility
	if (!bMustChooseTenet)
	{
		for(int iBranchLoop = 0; iBranchLoop < GC.getNumPolicyBranchInfos(); iBranchLoop++)
		{
			const PolicyBranchTypes ePolicyBranch = static_cast<PolicyBranchTypes>(iBranchLoop);
			CvPolicyBranchEntry* pkPolicyBranchInfo = GC.getPolicyBranchInfo(ePolicyBranch);
			if(pkPolicyBranchInfo)
			{
				if(bStartedAMutuallyExclusiveBranch && pkPolicyBranchInfo->IsMutuallyExclusive())
				{
					continue;
				}

#if defined(MOD_BALANCE_CORE)
				bool bNeedToFinish = false;
				for(int iBranchLoop2 = 0; iBranchLoop2 < GC.getNumPolicyBranchInfos(); iBranchLoop2++)
				{
					const PolicyBranchTypes ePolicyBranch2 = static_cast<PolicyBranchTypes>(iBranchLoop2);
					CvPolicyBranchEntry* pkPolicyBranchInfo2 = GC.getPolicyBranchInfo(ePolicyBranch2);
					//Do we already have a different policy branch unlocked?
					if(pkPolicyBranchInfo2 && ePolicyBranch2 != ePolicyBranch && pPlayer->GetPlayerPolicies()->IsPolicyBranchUnlocked(ePolicyBranch2))
					{
						//Have we not finished it yet? If so, let's not open a new one.
						if(!pPlayer->GetPlayerPolicies()->HasPolicy((PolicyTypes)pkPolicyBranchInfo2->GetFreeFinishingPolicy()))
						{
							bNeedToFinish = true;
							break;
						}
					}
				}
				if(bNeedToFinish)
				{
					continue;
				}
#endif
				if(pPlayer->GetPlayerPolicies()->CanUnlockPolicyBranch(ePolicyBranch) && !pPlayer->GetPlayerPolicies()->IsPolicyBranchUnlocked(ePolicyBranch))
				{
					int iBranchWeight = 0;

					// Does this branch actually help us, based on game options?
					if(IsBranchEffectiveInGame(ePolicyBranch))
					{
						iBranchWeight += WeighBranch(ePolicyBranch);

						iBranchWeight *= (100 - m_iPolicyWeightPercentDropNewBranch);
						iBranchWeight /= 100;
#if defined(MOD_BALANCE_CORE)
						//Leftover from Vanilla victory
#else
						if(eCurrentGrandStrategy == eCultureGrandStrategy)
						{
							iBranchWeight /= 3;
						}
#endif
					}

					m_AdoptablePolicies.push_back(iBranchLoop, iBranchWeight);
				}
			}
		}
	}

	m_AdoptablePolicies.SortItems();
	LogPossiblePolicies();
#if defined(MOD_BALANCE_CORE)
#else
	// If there were any Level 3 tenets found, consider going for the one that matches our victory strategy
	if (aLevel3Tenets.size() > 0)
	{
		vector<int>::const_iterator it;
		for (it = aLevel3Tenets.begin(); it != aLevel3Tenets.end(); it++)
		{
			CvPolicyEntry *pEntry;
			pEntry = m_pCurrentPolicies->GetPolicies()->GetPolicyEntry(*it);
			if (pEntry)
			{
				AIGrandStrategyTypes eGrandStrategy = pPlayer->GetGrandStrategyAI()->GetActiveGrandStrategy();
				if (eGrandStrategy == GC.getInfoTypeForString("AIGRANDSTRATEGY_CONQUEST"))
				{
					if (pEntry->GetFlavorValue((FlavorTypes)GC.getInfoTypeForString("FLAVOR_OFFENSE")) > 0)
					{
						LogPolicyChoice((PolicyTypes)*it);
						return (*it) + GC.getNumPolicyBranchInfos();
					}
				}
				else if(eGrandStrategy == GC.getInfoTypeForString("AIGRANDSTRATEGY_SPACESHIP"))
				{
					if (pEntry->GetFlavorValue((FlavorTypes)GC.getInfoTypeForString("FLAVOR_SPACESHIP")) > 0)
					{
						LogPolicyChoice((PolicyTypes)*it);
						return (*it) + GC.getNumPolicyBranchInfos();
					}
				}
				else if(eGrandStrategy == GC.getInfoTypeForString("AIGRANDSTRATEGY_UNITED_NATIONS"))
				{
					if (pEntry->GetFlavorValue((FlavorTypes)GC.getInfoTypeForString("FLAVOR_DIPLOMACY")) > 0)
					{
						LogPolicyChoice((PolicyTypes)*it);
						return (*it) + GC.getNumPolicyBranchInfos();
					}
				}
				else if(eGrandStrategy == GC.getInfoTypeForString("AIGRANDSTRATEGY_CULTURE"))
				{
					if (pEntry->GetFlavorValue((FlavorTypes)GC.getInfoTypeForString("FLAVOR_CULTURE")) > 0)
					{
						LogPolicyChoice((PolicyTypes)*it);
						return (*it) + GC.getNumPolicyBranchInfos();
					}
				}

			}
		}
	}
#endif
	CvAssertMsg(m_AdoptablePolicies.GetTotalWeight() >= 0, "Total weights of considered policies should not be negative! Please send Anton your save file and version.");

	// If total weight is above 0, choose one above a threshold
	if(m_AdoptablePolicies.GetTotalWeight() > 0)
	{
		int iNumChoices = GC.getGame().getHandicapInfo().GetPolicyNumOptions();
		iRtnValue = m_AdoptablePolicies.ChooseFromTopChoices(iNumChoices, &fcn, "Choosing policy from Top Choices");
	}
	// Total weight may be 0 if the only branches and policies left are ones that are ineffective in our game, but we gotta pick something
	else if(m_AdoptablePolicies.GetTotalWeight() == 0 && m_AdoptablePolicies.size() > 0)
	{
		iRtnValue = m_AdoptablePolicies.ChooseAtRandom(&fcn, "Choosing policy at random (no good choices)");
	}

	// Log our choice
	if(iRtnValue != (int)NO_POLICY)
	{
		if(iRtnValue >= GC.getNumPolicyBranchInfos())
		{
			LogPolicyChoice((PolicyTypes)(iRtnValue - GC.getNumPolicyBranchInfos()));
		}
		else
		{
			LogBranchChoice((PolicyBranchTypes)iRtnValue);
		}
	}

	return iRtnValue;
}