Esempio n. 1
0
/// Should the AI look at switching ideology branches?
void CvPolicyAI::DoConsiderIdeologySwitch(CvPlayer* pPlayer)
{
    // Gather basic Ideology info
    int iCurrentHappiness = pPlayer->GetExcessHappiness();
    int iPublicOpinionUnhappiness = pPlayer->GetCulture()->GetPublicOpinionUnhappiness();
    PolicyBranchTypes ePreferredIdeology = pPlayer->GetCulture()->GetPublicOpinionPreferredIdeology();
    PolicyBranchTypes eCurrentIdeology = pPlayer->GetPlayerPolicies()->GetLateGamePolicyTree();
    PlayerTypes eMostPressure = pPlayer->GetCulture()->GetPublicOpinionBiggestInfluence();

    // Possible enough that we need to look at this in detail?
    if (iCurrentHappiness <= GC.getSUPER_UNHAPPY_THRESHOLD() && iPublicOpinionUnhappiness >= 10)
    {
        // How much Happiness could we gain from a switch?
        int iHappinessCurrentIdeology = GetBranchBuildingHappiness(pPlayer, eCurrentIdeology);
        int iHappinessPreferredIdeology = GetBranchBuildingHappiness(pPlayer, ePreferredIdeology);

        // Does the switch fight against our clearly preferred victory path?
        bool bDontSwitchFreedom = false;
        bool bDontSwitchOrder = false;
        bool bDontSwitchAutocracy = false;
        int iConquestPriority = pPlayer->GetGrandStrategyAI()->GetConquestPriority();
        int iDiploPriority = pPlayer->GetGrandStrategyAI()->GetUnitedNationsPriority();
        int iTechPriority = pPlayer->GetGrandStrategyAI()->GetSpaceshipPriority();
        int iCulturePriority = pPlayer->GetGrandStrategyAI()->GetCulturePriority();
        int iClearPrefPercent = GC.getIDEOLOGY_PERCENT_CLEAR_VICTORY_PREF();
        if (iConquestPriority > (iDiploPriority   * (100 + iClearPrefPercent) / 100) &&
                iConquestPriority > (iTechPriority    * (100 + iClearPrefPercent) / 100) &&
                iConquestPriority > (iCulturePriority * (100 + iClearPrefPercent) / 100))
        {
            bDontSwitchFreedom = true;
        }
        else if (iDiploPriority > (iConquestPriority * (100 + iClearPrefPercent) / 100) &&
                 iDiploPriority > (iTechPriority     * (100 + iClearPrefPercent) / 100) &&
                 iDiploPriority > (iCulturePriority  * (100 + iClearPrefPercent) / 100))
        {
            bDontSwitchOrder = true;
        }
        else if (iTechPriority > (iConquestPriority * (100 + iClearPrefPercent) / 100) &&
                 iTechPriority > (iDiploPriority    * (100 + iClearPrefPercent) / 100) &&
                 iTechPriority > (iCulturePriority  * (100 + iClearPrefPercent) / 100))
        {
            bDontSwitchAutocracy = true;
        }

        int iTotalHappinessImprovement = iPublicOpinionUnhappiness + iHappinessPreferredIdeology - iHappinessCurrentIdeology;
        if (iTotalHappinessImprovement >= 10)
        {
            if (bDontSwitchFreedom && ePreferredIdeology == GC.getPOLICY_BRANCH_FREEDOM())
            {
                return;
            }
            if (bDontSwitchAutocracy && ePreferredIdeology == GC.getPOLICY_BRANCH_AUTOCRACY())
            {
                return;
            }
            if (bDontSwitchOrder && ePreferredIdeology == GC.getPOLICY_BRANCH_ORDER())
            {
                return;
            }

            // Cleared all obstacles -- REVOLUTION!
            pPlayer->SetAnarchyNumTurns(GC.getSWITCH_POLICY_BRANCHES_ANARCHY_TURNS());
            pPlayer->GetPlayerPolicies()->DoSwitchIdeologies(ePreferredIdeology);

            if (ePreferredIdeology == GC.getPOLICY_BRANCH_FREEDOM() && eCurrentIdeology == GC.getPOLICY_BRANCH_ORDER())
            {
                if (GET_PLAYER(eMostPressure).GetID() == GC.getGame().getActivePlayer())
                {
                    gDLL->UnlockAchievement(ACHIEVEMENT_XP2_39);
                }
            }
        }
    }
}
/// Should the AI look at switching ideology branches?
void CvPolicyAI::DoConsiderIdeologySwitch(CvPlayer* pPlayer)
{
	// Gather basic Ideology info
	int iCurrentHappiness = pPlayer->GetExcessHappiness();
	int iPublicOpinionUnhappiness = pPlayer->GetCulture()->GetPublicOpinionUnhappiness();
	PolicyBranchTypes ePreferredIdeology = pPlayer->GetCulture()->GetPublicOpinionPreferredIdeology();
	PolicyBranchTypes eCurrentIdeology = pPlayer->GetPlayerPolicies()->GetLateGamePolicyTree();
#if !defined(NO_ACHIEVEMENTS)
	PlayerTypes eMostPressure = pPlayer->GetCulture()->GetPublicOpinionBiggestInfluence();
#endif
#if defined(MOD_DIPLOMACY_CIV4_FEATURES)
	if(MOD_DIPLOMACY_CIV4_FEATURES)
	{
		if(GET_TEAM(pPlayer->getTeam()).IsVassalOfSomeone() && pPlayer->GetPlayerPolicies()->GetLateGamePolicyTree() != NO_POLICY_BRANCH_TYPE)
		{
			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 && GET_PLAYER(eMaster).GetPlayerPolicies()->GetLateGamePolicyTree() != pPlayer->GetPlayerPolicies()->GetLateGamePolicyTree())
						{
							// Cleared all obstacles -- REVOLUTION!
							pPlayer->SetAnarchyNumTurns(GC.getSWITCH_POLICY_BRANCHES_ANARCHY_TURNS());
							pPlayer->GetPlayerPolicies()->DoSwitchIdeologies(GET_PLAYER(eMaster).GetPlayerPolicies()->GetLateGamePolicyTree());	
							Localization::String strSummary = Localization::Lookup("TXT_KEY_ANARCHY_BEGINS_SUMMARY");
							Localization::String strMessage = Localization::Lookup("TXT_KEY_ANARCHY_BEGINS");
							pPlayer->GetNotifications()->Add(NOTIFICATION_GENERIC, strMessage.toUTF8(), strSummary.toUTF8(), pPlayer->GetID(), GC.getSWITCH_POLICY_BRANCHES_ANARCHY_TURNS(), -1);
							return;
						}
					}
				}
			}
		}
	}
#endif	
	// Possible enough that we need to look at this in detail?
	if (iCurrentHappiness <= GC.getSUPER_UNHAPPY_THRESHOLD() && iPublicOpinionUnhappiness >= 10)
	{
		// How much Happiness could we gain from a switch?
		int iHappinessCurrentIdeology = GetBranchBuildingHappiness(pPlayer, eCurrentIdeology);
		int iHappinessPreferredIdeology = GetBranchBuildingHappiness(pPlayer, ePreferredIdeology);

		// Does the switch fight against our clearly preferred victory path?
		bool bDontSwitchFreedom = false;
		bool bDontSwitchOrder = false;
		bool bDontSwitchAutocracy = false;
		int iConquestPriority = pPlayer->GetGrandStrategyAI()->GetConquestPriority();
		int iDiploPriority = pPlayer->GetGrandStrategyAI()->GetUnitedNationsPriority();
		int iTechPriority = pPlayer->GetGrandStrategyAI()->GetSpaceshipPriority();
		int iCulturePriority = pPlayer->GetGrandStrategyAI()->GetCulturePriority();
		int iClearPrefPercent = GC.getIDEOLOGY_PERCENT_CLEAR_VICTORY_PREF();
		if (iConquestPriority > (iDiploPriority   * (100 + iClearPrefPercent) / 100) &&
			iConquestPriority > (iTechPriority    * (100 + iClearPrefPercent) / 100) &&
			iConquestPriority > (iCulturePriority * (100 + iClearPrefPercent) / 100))
		{
			bDontSwitchFreedom = true;
		}
		else if (iDiploPriority > (iConquestPriority * (100 + iClearPrefPercent) / 100) &&
			iDiploPriority > (iTechPriority     * (100 + iClearPrefPercent) / 100) &&
			iDiploPriority > (iCulturePriority  * (100 + iClearPrefPercent) / 100))
		{
			bDontSwitchOrder = true;
		}
		else if (iTechPriority > (iConquestPriority * (100 + iClearPrefPercent) / 100) &&
			iTechPriority > (iDiploPriority    * (100 + iClearPrefPercent) / 100) &&
			iTechPriority > (iCulturePriority  * (100 + iClearPrefPercent) / 100))
		{
			bDontSwitchAutocracy = true;
		}
#if defined(MOD_BALANCE_CORE)
		//Sanity check - would a change to this branch simply make us unhappy in another way? If so, don't do it.
		if(ePreferredIdeology != NO_POLICY_BRANCH_TYPE)
		{
			int iUnhappiness = pPlayer->GetCulture()->ComputeHypotheticalPublicOpinionUnhappiness(ePreferredIdeology);
			if(iUnhappiness >= iPublicOpinionUnhappiness)
			{
				return;
			}
		}
#endif
		int iTotalHappinessImprovement = iPublicOpinionUnhappiness + iHappinessPreferredIdeology - iHappinessCurrentIdeology;
		if (iTotalHappinessImprovement >= 10)
		{
			if (bDontSwitchFreedom && ePreferredIdeology == GC.getPOLICY_BRANCH_FREEDOM())
			{
				return;
			}
			if (bDontSwitchAutocracy && ePreferredIdeology == GC.getPOLICY_BRANCH_AUTOCRACY())
			{
				return;
			}
			if (bDontSwitchOrder && ePreferredIdeology == GC.getPOLICY_BRANCH_ORDER())
			{
				return;
			}
#if defined(MOD_BALANCE_CORE)
			//Final sanity check - are we flip-flopping?
			if(GC.getGame().getGameTurn() - pPlayer->GetCulture()->GetTurnIdeologySwitch() <= 30)
			{
				return;
			}
#endif

			// Cleared all obstacles -- REVOLUTION!
			pPlayer->SetAnarchyNumTurns(GC.getSWITCH_POLICY_BRANCHES_ANARCHY_TURNS());
			pPlayer->GetPlayerPolicies()->DoSwitchIdeologies(ePreferredIdeology);	
#if defined(MOD_BALANCE_CORE)
			Localization::String strSummary = Localization::Lookup("TXT_KEY_ANARCHY_BEGINS_SUMMARY");
			Localization::String strMessage = Localization::Lookup("TXT_KEY_ANARCHY_BEGINS");
			pPlayer->GetNotifications()->Add(NOTIFICATION_GENERIC, strMessage.toUTF8(), strSummary.toUTF8(), pPlayer->GetID(), GC.getSWITCH_POLICY_BRANCHES_ANARCHY_TURNS(), -1);
#endif
#if !defined(NO_ACHIEVEMENTS)
			if (ePreferredIdeology == GC.getPOLICY_BRANCH_FREEDOM() && eCurrentIdeology == GC.getPOLICY_BRANCH_ORDER())
			{
				if (GET_PLAYER(eMostPressure).GetID() == GC.getGame().getActivePlayer())
				{
					gDLL->UnlockAchievement(ACHIEVEMENT_XP2_39);
				}
			}
#endif
		}
	}
}
Esempio n. 3
0
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
}