Bazaar developer reference

CvPlayerPolicies::CanAdoptPolicy(PolicyTypes, bool) const

Defined in
CvPolicyClasses.cpp, line 3476
Class
CvPlayerPolicies
Hook name
CivHooks::CvPlayerPolicies_CanAdoptPolicy
Lua events
GameEvents.PlayerCanAdoptTenet, GameEvents.PlayerCanAdoptPolicy

Hooks

static bool BeforeCvPlayerPolicies_CanAdoptPolicy(const CvPlayerPolicies* self, PolicyTypes eIndex, bool bIgnoreCost, bool& result);static void AfterCvPlayerPolicies_CanAdoptPolicy(const CvPlayerPolicies* self, PolicyTypes eIndex, bool bIgnoreCost, bool& result);static bool ReplaceCvPlayerPolicies_CanAdoptPolicy(bool (*original)(const CvPlayerPolicies*, PolicyTypes, bool), const CvPlayerPolicies* self, PolicyTypes eIndex, bool bIgnoreCost);CivRegisterBefore<CivHooks::CvPlayerPolicies_CanAdoptPolicy>(host, &BeforeCvPlayerPolicies_CanAdoptPolicy);CivRegisterAfter<CivHooks::CvPlayerPolicies_CanAdoptPolicy>(host, &AfterCvPlayerPolicies_CanAdoptPolicy);CivRegisterReplace<CivHooks::CvPlayerPolicies_CanAdoptPolicy>(host, &ReplaceCvPlayerPolicies_CanAdoptPolicy);

Code

3475/// Can we adopt this policy?3476bool CvPlayerPolicies::CanAdoptPolicy(PolicyTypes eIndex, bool bIgnoreCost) const3477{3478	CvAssertMsg(eIndex >= 0, "eIndex is expected to be non-negative (invalid Index)");3479	CvAssertMsg(eIndex < GC.getNumPolicyInfos(), "eIndex is expected to be within maximum bounds (invalid Index)");34803481	CvPolicyEntry* pkPolicyEntry = GC.getPolicyInfo(eIndex);3482	if(pkPolicyEntry == NULL)3483		return false;34843485	// Already has Policy?3486	if(HasPolicy(eIndex))3487	{3488		return false;3489	}34903491	// Has enough culture to spend?3492	if((!bIgnoreCost) && m_pPlayer->getNextPolicyCost() > 0)3493	{3494		if(m_pPlayer->getJONSCulture() < m_pPlayer->getNextPolicyCost())3495		{3496			bool bTenet = pkPolicyEntry->GetLevel() > 0;3497			if (m_pPlayer->GetNumFreePolicies() == 0)3498			{3499				if (!bTenet || m_pPlayer->GetNumFreeTenets() == 0)3500				{3501					return false;3502				}3503			}3504		}3505	}35063507	PolicyBranchTypes eBranch = (PolicyBranchTypes) pkPolicyEntry->GetPolicyBranchType();35083509	// If it doesn't have a branch, it's a freebie that comes WITH the branch, so we can't pick it manually3510	if(eBranch == NO_POLICY_BRANCH_TYPE)3511	{3512		return false;3513	}35143515	if(!IsPolicyBranchUnlocked(eBranch))3516	{3517		return false;3518	}35193520	// Is it from a branch with Levels?3521	CvPolicyBranchEntry* pkPolicyBranchInfo = GC.getPolicyBranchInfo(eBranch);3522	if(pkPolicyBranchInfo == NULL)3523	{3524		return false;3525	}3526	else3527	{3528		if (pkPolicyBranchInfo->IsPurchaseByLevel())3529		{3530			// If below level 1, can't have as many of this level as of the previous one3531			int iLevel = pkPolicyEntry->GetLevel();3532			if (iLevel > 1)3533			{3534				int iPoliciesOfThisLevel = GetNumTenetsOfLevel(eBranch, iLevel) + 1 /* For the policy we're adding here */;3535				int iPoliciesOfPreviousLevel = GetNumTenetsOfLevel(eBranch, iLevel - 1);3536				if (iPoliciesOfThisLevel >= iPoliciesOfPreviousLevel)3537				{3538					return false;3539				}3540			}3541		}3542	}35433544	// Other Policies as Prereqs35453546	bool bFoundPossible = false;3547	bool bFoundValid = false;35483549	for(int iI = 0; iI < GC.getNUM_OR_TECH_PREREQS(); iI++)3550	{3551		PolicyTypes ePrereq = (PolicyTypes)pkPolicyEntry->GetPrereqOrPolicies(iI);3552		if(ePrereq != NO_POLICY)3553		{3554			bFoundPossible = true;35553556			if(HasPolicy(ePrereq))3557			{3558				bFoundValid = true;3559				break;3560			}3561		}3562	}35633564	if(bFoundPossible && !bFoundValid)3565	{3566		return false;3567	}35683569	for(int iI = 0; iI < GC.getNUM_AND_TECH_PREREQS(); iI++)3570	{3571		const PolicyTypes ePrereq = static_cast<PolicyTypes>(pkPolicyEntry->GetPrereqAndPolicies(iI));35723573		if(ePrereq == NO_POLICY)3574			continue;35753576		CvPolicyEntry* pkPrereqPolicyInfo = GC.getPolicyInfo(ePrereq);3577		if(pkPrereqPolicyInfo)3578		{3579			if(!HasPolicy(ePrereq))3580			{3581				return false;3582			}3583		}3584	}35853586	// Disabled by another Policy?3587	for(int iPolicyLoop = 0; iPolicyLoop < GetPolicies()->GetNumPolicies(); iPolicyLoop++)3588	{3589		const PolicyTypes eDisablePolicy =static_cast<PolicyTypes>(iPolicyLoop);35903591		CvPolicyEntry* pkDisablePolicyInfo = GC.getPolicyInfo(eDisablePolicy);3592		if(pkDisablePolicyInfo)3593		{3594			if(HasPolicy(eDisablePolicy))3595			{3596				for(int iI = 0; iI < GC.getNUM_AND_TECH_PREREQS(); iI++)3597				{3598					if(pkDisablePolicyInfo->GetPolicyDisables(iI) == eIndex)3599					{3600						return false;3601					}3602				}3603			}3604		}3605	}36063607	// Has tech prereq? (no policies have one by default)3608	if(pkPolicyEntry->GetTechPrereq() != NO_TECH)3609	{3610		if(!GET_TEAM(m_pPlayer->getTeam()).GetTeamTechs()->HasTech((TechTypes) pkPolicyEntry->GetTechPrereq()))3611		{3612			return false;3613		}3614	}36153616	ICvEngineScriptSystem1* pkScriptSystem = gDLL->GetScriptSystem();3617	if(pkScriptSystem)3618	{3619		CvLuaArgsHandle args;3620		args->Push(m_pPlayer->GetID());3621		args->Push(eIndex);36223623		// Attempt to execute the game events.3624		// Will return false if there are no registered listeners.3625		bool bResult = false;3626		if(LuaSupport::CallTestAll(pkScriptSystem, "PlayerCanAdoptPolicy", args.get(), bResult))3627		{3628			// Check the result.3629			if(bResult == false)3630			{3631				return false;3632			}3633		}3634	}3635	3636#if defined(MOD_EVENTS_IDEOLOGIES)3637	if (MOD_EVENTS_IDEOLOGIES && pkPolicyEntry->GetLevel() > 0) {3638		if (GAMEEVENTINVOKE_TESTALL(GAMEEVENT_PlayerCanAdoptTenet, m_pPlayer->GetID(), eIndex) == GAMEEVENTRETURN_FALSE) {3639			return false;3640		}3641	}3642#endif36433644	return true;3645}

Calls

Called by