Bazaar developer reference

CvPlayerAI::FindBestMerchantTargetPlot(CvUnit *, bool)

Defined in
CvPlayerAI.cpp, line 1460
Class
CvPlayerAI
Hook name
CivHooks::CvPlayerAI_FindBestMerchantTargetPlot

Hooks

static bool BeforeCvPlayerAI_FindBestMerchantTargetPlot(CvPlayerAI* self, CvUnit* pGreatMerchant, bool bOnlySafePaths, CvPlot*& result);static void AfterCvPlayerAI_FindBestMerchantTargetPlot(CvPlayerAI* self, CvUnit* pGreatMerchant, bool bOnlySafePaths, CvPlot*& result);static CvPlot* ReplaceCvPlayerAI_FindBestMerchantTargetPlot(CvPlot* (*original)(CvPlayerAI*, CvUnit*, bool), CvPlayerAI* self, CvUnit* pGreatMerchant, bool bOnlySafePaths);CivRegisterBefore<CivHooks::CvPlayerAI_FindBestMerchantTargetPlot>(host, &BeforeCvPlayerAI_FindBestMerchantTargetPlot);CivRegisterAfter<CivHooks::CvPlayerAI_FindBestMerchantTargetPlot>(host, &AfterCvPlayerAI_FindBestMerchantTargetPlot);CivRegisterReplace<CivHooks::CvPlayerAI_FindBestMerchantTargetPlot>(host, &ReplaceCvPlayerAI_FindBestMerchantTargetPlot);

Code

1460CvPlot* CvPlayerAI::FindBestMerchantTargetPlot(CvUnit* pGreatMerchant, bool bOnlySafePaths)1461{1462	CvAssertMsg(pGreatMerchant, "pGreatMerchant is null");1463	if(!pGreatMerchant)1464	{1465		return NULL;1466	}14671468	int iBestTurnsToReach = MAX_INT;1469	CvPlot* pBestTargetPlot = NULL;1470	int iPathTurns;1471	UnitHandle pMerchant = UnitHandle(pGreatMerchant);1472	CvTeam& kTeam = GET_TEAM(getTeam());14731474	//bool bIsVenice = GetPlayerTraits()->IsNoAnnexing();1475	//bool bWantsCash = GreatMerchantWantsCash();14761477	// Loop through each city state1478	for(int iI = 0; iI < MAX_PLAYERS; iI++)1479	{1480		CvPlayer& kPlayer = GET_PLAYER((PlayerTypes)iI);1481		if (!kPlayer.isMinorCiv())1482		{1483			continue;1484		}14851486		// if I'm Venice, I don't want to send a Merchant of Venice to a buy a city that I have trade routes 1487		// with because it's probably more valuable as a trade partner than as an owned entity1488		//if (!bWantsCash)1489		//{1490		//	if (bIsVenice)1491		//	{1492		//		if (GetTrade()->IsConnectedToPlayer(kPlayer.GetID()))1493		//		{1494		//			continue;1495		//		}1496		//	}1497		//}14981499		CvPlot* pCSPlot = kPlayer.getStartingPlot();1500		if (!pCSPlot)1501		{1502			continue;1503		}15041505		if (!pCSPlot->isRevealed(getTeam()))1506		{1507			continue;1508		}15091510		// Is this a minor we are friendly with?1511		bool bMinorCivApproachIsCorrect = (GetDiplomacyAI()->GetMinorCivApproach(kPlayer.GetID()) != MINOR_CIV_APPROACH_CONQUEST);1512		bool bNotAtWar = !kTeam.isAtWar(kPlayer.getTeam());1513		bool bNotPlanningAWar = GetDiplomacyAI()->GetWarGoal(kPlayer.GetID()) == NO_WAR_GOAL_TYPE;15141515		if(bMinorCivApproachIsCorrect && bNotAtWar && bNotPlanningAWar)1516		{1517			// Search all the plots adjacent to this city (since can't enter the minor city plot itself)1518			for(int jJ = 0; jJ < NUM_DIRECTION_TYPES; jJ++)1519			{1520				CvPlot* pAdjacentPlot = plotDirection(pCSPlot->getX(), pCSPlot->getY(), ((DirectionTypes)jJ));1521				if(pAdjacentPlot != NULL)1522				{1523					// Make sure this is still owned by the city state and is revealed to us and isn't a water tile1524					//if(pAdjacentPlot->getOwner() == (PlayerTypes)iI && pAdjacentPlot->isRevealed(getTeam()) && !pAdjacentPlot->isWater())1525					bool bRightOwner = (pAdjacentPlot->getOwner() == (PlayerTypes)iI);1526					bool bIsRevealed = pAdjacentPlot->isRevealed(getTeam());1527					if(bRightOwner && bIsRevealed)1528					{1529						iPathTurns = TurnsToReachTarget(pMerchant, pAdjacentPlot, true /*bReusePaths*/, !bOnlySafePaths/*bIgnoreUnits*/);1530						if(iPathTurns < iBestTurnsToReach)1531						{1532							iBestTurnsToReach = iPathTurns;1533							pBestTargetPlot = pAdjacentPlot;1534						}1535					}1536				}1537			}1538		}1539	}15401541	return pBestTargetPlot;1542}

Calls

Called by