Hooks
static bool BeforeCvCity_IsCanPurchase(CvCity* self, bool bTestPurchaseCost, bool bTestTrainable, UnitTypes eUnitType, BuildingTypes eBuildingType, ProjectTypes eProjectType, YieldTypes ePurchaseYield, bool& result);static void AfterCvCity_IsCanPurchase(CvCity* self, bool bTestPurchaseCost, bool bTestTrainable, UnitTypes eUnitType, BuildingTypes eBuildingType, ProjectTypes eProjectType, YieldTypes ePurchaseYield, bool& result);static bool ReplaceCvCity_IsCanPurchase(bool (*original)(CvCity*, bool, bool, UnitTypes, BuildingTypes, ProjectTypes, YieldTypes), CvCity* self, bool bTestPurchaseCost, bool bTestTrainable, UnitTypes eUnitType, BuildingTypes eBuildingType, ProjectTypes eProjectType, YieldTypes ePurchaseYield);CivRegisterBefore<CivHooks::CvCity_IsCanPurchase>(host, &BeforeCvCity_IsCanPurchase);CivRegisterAfter<CivHooks::CvCity_IsCanPurchase>(host, &AfterCvCity_IsCanPurchase);CivRegisterReplace<CivHooks::CvCity_IsCanPurchase>(host, &ReplaceCvCity_IsCanPurchase);
Code
13848// Is this city allowed to purchase something right now?13849bool CvCity::IsCanPurchase(bool bTestPurchaseCost, bool bTestTrainable, UnitTypes eUnitType, BuildingTypes eBuildingType, ProjectTypes eProjectType, YieldTypes ePurchaseYield)13850{13851 CvAssertMsg(eUnitType >= 0 || eBuildingType >= 0 || eProjectType >= 0, "No valid passed in");13852 CvAssertMsg(!(eUnitType >= 0 && eBuildingType >= 0) && !(eUnitType >= 0 && eProjectType >= 0) && !(eBuildingType >= 0 && eProjectType >= 0), "Only one being passed");1385313854 // Can't purchase anything in a puppeted city13855 // slewis - The Venetian Exception13856 bool bIsPuppet = IsPuppet();13857 bool bVenetianException = false;13858 if (GET_PLAYER(m_eOwner).GetPlayerTraits()->IsNoAnnexing() && bIsPuppet)13859 {13860 bVenetianException = true;13861 }1386213863 if (bIsPuppet && !bVenetianException)13864 {13865 return false;13866 }1386713868 // Check situational reasons we can't purchase now (similar to not having enough gold or faith)13869 if(bTestPurchaseCost)13870 {13871 // Can't purchase things if the city is in resistance or is being razed13872 if(IsResistance() || IsRazing())13873 return false;1387413875 // if we're purchasing a unit13876 if(eUnitType >= 0)13877 {13878 // if we can't add this unit to this tile, then don't!13879 if(!CanPlaceUnitHere(eUnitType))13880 return false;13881 }13882 }1388313884 // What are we buying this with?13885 switch(ePurchaseYield)13886 {13887 case YIELD_GOLD:13888 {13889 int iGoldCost = -1;1389013891 // Unit13892 if(eUnitType != NO_UNIT)13893 {13894 if(!canTrain(eUnitType, false, !bTestTrainable, false /*bIgnoreCost*/, true /*bWillPurchase*/))13895 return false;1389613897 iGoldCost = GetPurchaseCost(eUnitType);13898 }13899 // Building13900 else if(eBuildingType != NO_BUILDING)13901 {13902#if defined(MOD_API_EXTENSIONS)13903 if(!canConstruct(eBuildingType, false, !bTestTrainable, false /*bIgnoreCost*/, true /*bWillPurchase*/))13904#else13905 if(!canConstruct(eBuildingType, false, !bTestTrainable))13906#endif13907 {13908 bool bAlreadyUnderConstruction = canConstruct(eBuildingType, true, !bTestTrainable) && getFirstBuildingOrder(eBuildingType) != -1;13909 if(!bAlreadyUnderConstruction)13910 {13911 return false;13912 }13913 }1391413915 iGoldCost = GetPurchaseCost(eBuildingType);13916 }13917 // Project13918 else if(eProjectType != NO_PROJECT)13919 {13920 if(/*1*/ GC.getPROJECT_PURCHASING_DISABLED() == 1)13921 return false;1392213923 if(!canCreate(eProjectType, false, !bTestTrainable))13924 return false;1392513926 iGoldCost = GetPurchaseCost(eProjectType);13927 }1392813929 if(iGoldCost == -1)13930 {13931 return false;13932 }13933 else13934 {13935 if(bTestPurchaseCost)13936 {13937 // Trying to buy something when you don't have enough money!!13938 if(iGoldCost > GET_PLAYER(getOwner()).GetTreasury()->GetGold())13939 return false;13940 }13941 }13942 }13943 break;1394413945 case YIELD_FAITH:13946 {13947 int iFaithCost = -1;1394813949 // Does this city have a majority religion?13950 ReligionTypes eReligion = GetCityReligions()->GetReligiousMajority();13951#if defined(MOD_BUGFIX_MINOR)13952 // Permit faith purchases from pantheon beliefs13953 if(eReligion < RELIGION_PANTHEON)13954#else13955 if(eReligion <= RELIGION_PANTHEON)13956#endif13957 {13958 return false;13959 }1396013961 // Unit13962 if(eUnitType != NO_UNIT)13963 {13964 iFaithCost = GetFaithPurchaseCost(eUnitType, true);13965 if(iFaithCost < 1)13966 {13967 return false;13968 }1396913970 CvUnitEntry* pkUnitInfo = GC.getUnitInfo(eUnitType);13971 if(pkUnitInfo)13972 {13973#if defined(MOD_BUGFIX_MINOR)13974 if (pkUnitInfo->IsRequiresEnhancedReligion() && !(GC.getGame().GetGameReligions()->GetReligion(eReligion, m_eOwner)->m_bEnhanced))13975#else13976 if (pkUnitInfo->IsRequiresEnhancedReligion() && !(GC.getGame().GetGameReligions()->GetReligion(eReligion, NO_PLAYER)->m_bEnhanced))13977#endif13978 {13979 return false;13980 }1398113982 13983 if (pkUnitInfo->IsRequiresFaithPurchaseEnabled())13984 {13985 TechTypes ePrereqTech = (TechTypes)pkUnitInfo->GetPrereqAndTech();13986 if (ePrereqTech == -1)13987 {13988 const CvReligion *pReligion = GC.getGame().GetGameReligions()->GetReligion(eReligion, m_eOwner);13989 if (!pReligion->m_Beliefs.IsFaithBuyingEnabled((EraTypes)0)) // Ed?13990 {13991 return false;13992 }13993 if(!canTrain(eUnitType, false, !bTestTrainable, false /*bIgnoreCost*/, true /*bWillPurchase*/))13994 {13995 return false;13996 }13997 }13998 else13999 {14000 CvTechEntry *pkTechInfo = GC.GetGameTechs()->GetEntry(ePrereqTech);14001 if (!pkTechInfo)14002 {14003 return false;14004 }14005 else14006 {14007 const CvReligion *pReligion = GC.getGame().GetGameReligions()->GetReligion(eReligion, m_eOwner);14008 if (!pReligion->m_Beliefs.IsFaithBuyingEnabled((EraTypes)pkTechInfo->GetEra()))14009 {14010 return false;14011 }14012 if(!canTrain(eUnitType, false, !bTestTrainable, false /*bIgnoreCost*/, true /*bWillPurchase*/))14013 {14014 return false;14015 }14016 }14017 }14018 }14019#if defined(MOD_BUGFIX_MINOR)14020 else14021 {14022 // Missionaries, Inquisitors and Prophets14023 // We need a full religion and not just a pantheon,14024 // and also to test that the player can build the unit, specifically the check for a civ specific version of the unit14025 if(eReligion <= RELIGION_PANTHEON || !canTrain(eUnitType, false, !bTestTrainable, true /*bIgnoreCost*/, true /*bWillPurchase*/))14026 {14027 return false;14028 }14029 }14030#endif14031 }14032 }14033 // Building14034 else if(eBuildingType != NO_BUILDING)14035 {14036 CvBuildingEntry* pkBuildingInfo = GC.GetGameBuildings()->GetEntry(eBuildingType);14037 14038 // Religion-enabled building14039 if(pkBuildingInfo && pkBuildingInfo->IsUnlockedByBelief())14040 {14041 ReligionTypes eMajority = GetCityReligions()->GetReligiousMajority();14042 if(eMajority <= RELIGION_PANTHEON)14043 {14044 return false;14045 }14046 const CvReligion* pReligion = GC.getGame().GetGameReligions()->GetReligion(eMajority, getOwner());14047 if(pReligion == NULL || !pReligion->m_Beliefs.IsBuildingClassEnabled((BuildingClassTypes)pkBuildingInfo->GetBuildingClassType()))14048 {14049 return false;14050 }14051#if defined(MOD_BUGFIX_MINOR)14052 }14053#endif1405414055#if defined(MOD_API_EXTENSIONS)14056 if(!canConstruct(eBuildingType, false, !bTestTrainable, true /*bIgnoreCost*/, true /*bWillPurchase*/))14057#else14058 if(!canConstruct(eBuildingType, false, !bTestTrainable, true /*bIgnoreCost*/))14059#endif14060 {14061 return false;14062 }1406314064 if(GetCityBuildings()->GetNumBuilding(eBuildingType) > 0)14065 {14066 return false;14067 }1406814069 TechTypes ePrereqTech = (TechTypes)pkBuildingInfo->GetPrereqAndTech();14070 if(ePrereqTech != NO_TECH)14071 {14072 CvTechEntry *pkTechInfo = GC.GetGameTechs()->GetEntry(ePrereqTech);14073 if (pkTechInfo && !GET_TEAM(GET_PLAYER(getOwner()).getTeam()).GetTeamTechs()->HasTech(ePrereqTech))14074 {14075 return false;14076 }14077 }1407814079 // Does this city have prereq buildings?14080 int iNumBuildingClassInfos = GC.getNumBuildingClassInfos();14081 BuildingTypes ePrereqBuilding;14082 for(int iI = 0; iI < iNumBuildingClassInfos; iI++)14083 {14084 CvBuildingClassInfo* pkBuildingClassInfo = GC.getBuildingClassInfo((BuildingClassTypes)iI);14085 if(!pkBuildingClassInfo)14086 {14087 continue;14088 }1408914090 if(pkBuildingInfo->IsBuildingClassNeededInCity(iI))14091 {14092 CvCivilizationInfo& thisCivInfo = getCivilizationInfo();14093 ePrereqBuilding = ((BuildingTypes)(thisCivInfo.getCivilizationBuildings(iI)));1409414095 if(ePrereqBuilding != NO_BUILDING)14096 {14097 if(0 == m_pCityBuildings->GetNumBuilding(ePrereqBuilding))14098 {14099 return false;14100 }14101 }14102 }14103 }14104#if !defined(MOD_BUGFIX_MINOR)14105 }14106#endif1410714108 iFaithCost = GetFaithPurchaseCost(eBuildingType);14109 if(iFaithCost < 1) return false;14110 }1411114112 if(iFaithCost > 0)14113 {14114 if(bTestPurchaseCost)14115 {14116 // Trying to buy something when you don't have enough faith!!14117 if(iFaithCost > GET_PLAYER(getOwner()).GetFaith())14118 {14119 return false;14120 }14121 }14122 }14123 }14124 break;14125 }1412614127 return true;14128}