Bazaar developer reference

CvTradeAI::PrioritizeTradeRoutes(TradeConnectionList &)

Defined in
CvTradeClasses.cpp, line 4746
Class
CvTradeAI
Hook name
CivHooks::CvTradeAI_PrioritizeTradeRoutes

Hooks

static bool BeforeCvTradeAI_PrioritizeTradeRoutes(CvTradeAI* self, TradeConnectionList& aTradeConnectionList);static void AfterCvTradeAI_PrioritizeTradeRoutes(CvTradeAI* self, TradeConnectionList& aTradeConnectionList);static void ReplaceCvTradeAI_PrioritizeTradeRoutes(void (*original)(CvTradeAI*, TradeConnectionList&), CvTradeAI* self, TradeConnectionList& aTradeConnectionList);CivRegisterBefore<CivHooks::CvTradeAI_PrioritizeTradeRoutes>(host, &BeforeCvTradeAI_PrioritizeTradeRoutes);CivRegisterAfter<CivHooks::CvTradeAI_PrioritizeTradeRoutes>(host, &AfterCvTradeAI_PrioritizeTradeRoutes);CivRegisterReplace<CivHooks::CvTradeAI_PrioritizeTradeRoutes>(host, &ReplaceCvTradeAI_PrioritizeTradeRoutes);

Code

4745/// Prioritize TRs4746void CvTradeAI::PrioritizeTradeRoutes(TradeConnectionList& aTradeConnectionList)4747{	4748	//CvPlayerTrade* pPlayerTrade = m_pPlayer->GetTrade();4749	GetAvailableTR(aTradeConnectionList);47504751	// if the list is empty, bail4752	if (aTradeConnectionList.size() == 0)4753	{4754		return;4755	}47564757	// score TR4758	std::vector<TRSortElement> aProductionSortedTR;4759	std::vector<TRSortElement> aFoodSortedTR;4760	std::vector<TRSortElement> aGoldSortedTR;4761#if defined(MOD_TRADE_WONDER_RESOURCE_ROUTES)4762	std::vector<TRSortElement> aWonderSortedTR;4763#endif47644765	// FOOD FOOD FOOD FOOD4766	if (m_pPlayer->GetHappiness() >= 0)4767	{4768		// - Find smallest city4769		int iSmallestCitySize = MAX_INT;4770		CvCity* pSmallestCity = NULL;4771		int iCityLoop;4772		CvCity* pCity = NULL;4773		for (pCity = m_pPlayer->firstCity(&iCityLoop); pCity != NULL; pCity = m_pPlayer->nextCity(&iCityLoop))4774		{4775			int iCitySize = pCity->getPopulation();4776			if (iCitySize < iSmallestCitySize)4777			{4778				pSmallestCity = pCity;4779				iSmallestCitySize = iCitySize;4780			}4781		}47824783		// - Send tr there4784		// if there is a smallest city, score according to that4785		if (pSmallestCity)4786		{4787			aFoodSortedTR.clear();4788			for (uint ui = 0; ui < aTradeConnectionList.size(); ui++)4789			{4790				TRSortElement kElement;4791				kElement.m_kTradeConnection = aTradeConnectionList[ui];4792				kElement.m_iScore = ScoreFoodTR(aTradeConnectionList[ui], pSmallestCity);4793				if (kElement.m_iScore > 0)4794				{4795					aFoodSortedTR.push_back(kElement);4796				}4797			}47984799			std::stable_sort(aFoodSortedTR.begin(), aFoodSortedTR.end(), SortTR());4800		}4801	}48024803	// PRODUCTION PRODUCTION PRODUCTION PRODUCTION4804	// - Search for wonder city4805	// - Search for spaceship city4806	std::vector<CvCity*> apProductionTargetCities;4807	CvCity* pWonderCity = m_pPlayer->GetCitySpecializationAI()->GetWonderBuildCity();4808	if (pWonderCity)4809	{4810		apProductionTargetCities.push_back(pWonderCity);4811	}4812	CvCity* pCity = NULL;4813	int iCityLoop;4814	for (pCity = m_pPlayer->firstCity(&iCityLoop); pCity != NULL; pCity = m_pPlayer->nextCity(&iCityLoop))4815	{4816		UnitTypes eUnit = pCity->getProductionUnit();4817		if (eUnit != -1)4818		{4819			CvUnitEntry *pkUnit = GC.GetGameUnits()->GetEntry(eUnit);4820			if (pkUnit)4821			{4822				if (pkUnit->GetSpaceshipProject() != NO_PROJECT)4823				{4824					apProductionTargetCities.push_back(pCity);4825				}4826			}4827		}4828	}4829	if (apProductionTargetCities.size() > 0)4830	{4831		aProductionSortedTR.clear();4832		for (uint ui = 0; ui < aTradeConnectionList.size(); ui++)4833		{4834			TRSortElement kElement;4835			kElement.m_kTradeConnection = aTradeConnectionList[ui];4836			kElement.m_iScore = ScoreProductionTR(aTradeConnectionList[ui], apProductionTargetCities);4837			if (kElement.m_iScore > 0)4838			{4839				aProductionSortedTR.push_back(kElement);4840			}4841		}4842		std::stable_sort(aProductionSortedTR.begin(), aProductionSortedTR.end(), SortTR());4843	}48444845#if defined(MOD_TRADE_WONDER_RESOURCE_ROUTES)4846	if (MOD_TRADE_WONDER_RESOURCE_ROUTES) {4847		// WONDER WONDER WONDER WONDER4848		ResourceTypes eWonderResource = ::getWonderResource();48494850		if (eWonderResource != NO_RESOURCE) {4851			std::vector<CvCity*> apWonderTargetCities;4852			CvCity* pCity = NULL;4853			int iCityLoop;4854			for (pCity = m_pPlayer->firstCity(&iCityLoop); pCity != NULL; pCity = m_pPlayer->nextCity(&iCityLoop)) {4855				// Only interested in cities that don't have the wonder resource locally and are actually building a wonder!4856				if (!pCity->IsHasResourceLocal(eWonderResource, true))4857				{4858					BuildingTypes eBuilding = pCity->getProductionBuilding();4859					if (eBuilding != -1)4860					{4861						CvBuildingEntry *pkBuilding = GC.GetGameBuildings()->GetEntry(eBuilding);4862						if (pkBuilding)4863						{4864							if (GET_PLAYER(pCity->getOwner()).GetWonderProductionAI()->IsWonder(*pkBuilding))4865							{4866								bool bAddCity = true;4867								int iDestX = pCity->getX();4868								int iDestY = pCity->getY();48694870								// Only add if no existing wonder resource trade route to here4871								CvGameTrade* pTrade = GC.getGame().GetGameTrade();4872								for (uint ui = 0; ui < pTrade->m_aTradeConnections.size(); ui++)4873								{4874									if (pTrade->IsTradeRouteIndexEmpty(ui))4875									{4876										continue;4877									}48784879									TradeConnection* pTradeConnection = &(pTrade->m_aTradeConnections[ui]);4880									if (pTradeConnection->m_eConnectionType == TRADE_CONNECTION_WONDER_RESOURCE && pTradeConnection->m_iDestX == iDestX && pTradeConnection->m_iDestY == iDestY)4881									{4882										bAddCity = false;4883										break;4884									}4885								}4886							4887								if (bAddCity) {4888									apWonderTargetCities.push_back(pCity);4889									// CUSTOMLOG("%s is a potential destination for a Wonder Resource trade route", pCity->getName().c_str());4890								}4891							}4892						}4893					}4894				}4895			}4896			if (apWonderTargetCities.size() > 0)4897			{4898				aWonderSortedTR.clear();4899				for (uint ui = 0; ui < aTradeConnectionList.size(); ui++)4900				{4901					TRSortElement kElement;4902					kElement.m_kTradeConnection = aTradeConnectionList[ui];4903					kElement.m_iScore = ScoreWonderTR(aTradeConnectionList[ui], apWonderTargetCities);4904					if (kElement.m_iScore > 0)4905					{4906						aWonderSortedTR.push_back(kElement);4907					}4908				}4909				std::stable_sort(aWonderSortedTR.begin(), aWonderSortedTR.end(), SortTR());4910			}4911		}4912	}4913#endif49144915	// GOLD GOLD GOLD GOLD4916	aGoldSortedTR.clear();4917	for (uint ui = 0; ui < aTradeConnectionList.size(); ui++)4918	{4919		TRSortElement kElement;4920		kElement.m_kTradeConnection = aTradeConnectionList[ui];4921		kElement.m_iScore = ScoreInternationalTR(aTradeConnectionList[ui]);4922		if (kElement.m_iScore > 0)4923		{4924			aGoldSortedTR.push_back(kElement);4925		}4926	}4927	std::stable_sort(aGoldSortedTR.begin(), aGoldSortedTR.end(), SortTR());4928	uint uiNumInternationalTradesToAdd = 2;49294930	// clear list4931	aTradeConnectionList.clear();49324933	// add all the gold trs4934	for (uint ui = 0; ui < uiNumInternationalTradesToAdd && ui < aGoldSortedTR.size(); ui++)4935	{4936		aTradeConnectionList.push_back(aGoldSortedTR[ui].m_kTradeConnection);4937	}49384939	// add first food4940	if (aFoodSortedTR.size() > 0) 4941	{4942		aTradeConnectionList.push_back(aFoodSortedTR[0].m_kTradeConnection);4943	}4944	4945#if defined(MOD_TRADE_WONDER_RESOURCE_ROUTES)4946	if (MOD_TRADE_WONDER_RESOURCE_ROUTES) {4947		// add first wonder resource tr4948		if (aWonderSortedTR.size() > 0)4949		{4950			aTradeConnectionList.push_back(aWonderSortedTR[0].m_kTradeConnection);4951		}4952	}4953#endif49544955	// add first production tr4956	if (aProductionSortedTR.size() > 0)4957	{4958		aTradeConnectionList.push_back(aProductionSortedTR[0].m_kTradeConnection);4959	}49604961	// add all the gold trs4962	for (uint ui = uiNumInternationalTradesToAdd; ui < aGoldSortedTR.size(); ui++)4963	{4964		aTradeConnectionList.push_back(aGoldSortedTR[ui].m_kTradeConnection);4965	}4966}

Calls

Called by