Bazaar developer reference

CvHomelandAI::FindHomelandTargets()

Defined in
CvHomelandAI.cpp, line 518
Class
CvHomelandAI
Hook name
CivHooks::CvHomelandAI_FindHomelandTargets

Hooks

static bool BeforeCvHomelandAI_FindHomelandTargets(CvHomelandAI* self);static void AfterCvHomelandAI_FindHomelandTargets(CvHomelandAI* self);static void ReplaceCvHomelandAI_FindHomelandTargets(void (*original)(CvHomelandAI*), CvHomelandAI* self);CivRegisterBefore<CivHooks::CvHomelandAI_FindHomelandTargets>(host, &BeforeCvHomelandAI_FindHomelandTargets);CivRegisterAfter<CivHooks::CvHomelandAI_FindHomelandTargets>(host, &AfterCvHomelandAI_FindHomelandTargets);CivRegisterReplace<CivHooks::CvHomelandAI_FindHomelandTargets>(host, &ReplaceCvHomelandAI_FindHomelandTargets);

Code

517/// Make lists of everything we might want to target with the homeland AI this turn518void CvHomelandAI::FindHomelandTargets()519{520	int iI;521	CvPlot* pLoopPlot;522	CvHomelandTarget newTarget;523524	// Clear out target lists since we rebuild them each turn525	m_TargetedCities.clear();526	m_TargetedSentryPoints.clear();527	m_TargetedForts.clear();528	m_TargetedNavalResources.clear();529	m_TargetedHomelandRoads.clear();530	m_TargetedAncientRuins.clear();531	m_TargetedAntiquitySites.clear();532533	TeamTypes eTeam = m_pPlayer->getTeam();534535	// Look at every tile on map536	CvMap& theMap = GC.getMap();537	int iNumPlots = theMap.numPlots();538	for(iI = 0; iI < iNumPlots; iI++)539	{540		pLoopPlot = theMap.plotByIndexUnchecked(iI);541542		if(pLoopPlot->isVisible(m_pPlayer->getTeam()))543		{544			// Have a ...545			// ... friendly city?546			CvCity* pCity = pLoopPlot->getPlotCity();547			if(pCity != NULL)548			{549				if(m_pPlayer->GetID() == pCity->getOwner())550				{551					// Don't send another unit if the tactical AI already sent a garrison here552					UnitHandle pUnit = pLoopPlot->getBestDefender(m_pPlayer->GetID());553					if(!pUnit || !pUnit->isUnderTacticalControl())554					{555						newTarget.SetTargetType(AI_HOMELAND_TARGET_CITY);556						newTarget.SetTargetX(pLoopPlot->getX());557						newTarget.SetTargetY(pLoopPlot->getY());558						newTarget.SetAuxData((void*)pCity);559						newTarget.SetAuxIntData(pCity->getThreatValue());560						m_TargetedCities.push_back(newTarget);561					}562				}563			}564565			// ... naval resource?566			else if(pLoopPlot->isWater() &&567			        pLoopPlot->getImprovementType() == NO_IMPROVEMENT)568			{569				ResourceTypes eNonObsoleteResource = pLoopPlot->getNonObsoleteResourceType(eTeam);570				if(eNonObsoleteResource != NO_RESOURCE)571				{572					CvCity* pWorkingCity = pLoopPlot->getWorkingCity();573					if(NULL != pWorkingCity && pWorkingCity->getOwner() == m_pPlayer->GetID())574					{575						// Find proper improvement576						BuildTypes eBuild;577						for(int iJ = 0; iJ < GC.getNumBuildInfos(); iJ++)578						{579							eBuild = ((BuildTypes)iJ);580							CvBuildInfo* pkBuildInfo = GC.getBuildInfo(eBuild);581							if(pkBuildInfo && pkBuildInfo->getImprovement() != NO_IMPROVEMENT && pkBuildInfo->IsWater())582							{583								if(GC.getImprovementInfo((ImprovementTypes) GC.getBuildInfo(eBuild)->getImprovement())->IsImprovementResourceTrade(eNonObsoleteResource))584								{585									newTarget.SetTargetType(AI_HOMELAND_TARGET_NAVAL_RESOURCE);586									newTarget.SetTargetX(pLoopPlot->getX());587									newTarget.SetTargetY(pLoopPlot->getY());588									newTarget.SetAuxData(pLoopPlot);589									newTarget.SetAuxIntData((int)eBuild);590									m_TargetedNavalResources.push_back(newTarget);591									break;592								}593							}594						}595					}596				}597			}598599			// ... unpopped goody hut?600			else if(!m_pPlayer->isMinorCiv() && pLoopPlot->isGoody())601			{602				newTarget.SetTargetType(AI_HOMELAND_TARGET_ANCIENT_RUIN);603				newTarget.SetTargetX(pLoopPlot->getX());604				newTarget.SetTargetY(pLoopPlot->getY());605				newTarget.SetAuxData(pLoopPlot);606				m_TargetedAncientRuins.push_back(newTarget);607			}608609			// ... antiquity site?610			else if((pLoopPlot->getResourceType(eTeam) == GC.getARTIFACT_RESOURCE() || pLoopPlot->getResourceType(eTeam) == GC.getHIDDEN_ARTIFACT_RESOURCE()) && 611				!(pLoopPlot->getOwner() != NO_PLAYER && m_pPlayer->GetDiplomacyAI()->IsPlayerMadeNoDiggingPromise(pLoopPlot->getOwner())))612			{613				newTarget.SetTargetType(AI_HOMELAND_TARGET_ANTIQUITY_SITE);614				newTarget.SetTargetX(pLoopPlot->getX());615				newTarget.SetTargetY(pLoopPlot->getY());616				newTarget.SetAuxData(pLoopPlot);617				m_TargetedAntiquitySites.push_back(newTarget);618			}619620			// ... enemy civilian (or embarked) unit?621			else if(pLoopPlot->isVisibleOtherUnit(m_pPlayer->GetID()))622			{623				CvUnit* pTargetUnit = pLoopPlot->getUnitByIndex(0);624				if(!pTargetUnit->isDelayedDeath() && atWar(eTeam, pTargetUnit->getTeam()) && !pTargetUnit->IsCanDefend())625				{626					newTarget.SetTargetType(AI_HOMELAND_TARGET_ANCIENT_RUIN);627					newTarget.SetTargetX(pLoopPlot->getX());628					newTarget.SetTargetY(pLoopPlot->getY());629					newTarget.SetAuxData(pLoopPlot);630					m_TargetedAncientRuins.push_back(newTarget);631				}632			}633634			// ... possible sentry point? (must be empty or only have friendly units)635			else if(!pLoopPlot->isWater() && (!pLoopPlot->isUnit() || pLoopPlot->getNumDefenders(m_pPlayer->GetID()) > 0))636			{637				// Must be at least adjacent to our land638				if(pLoopPlot->getOwner() == m_pPlayer->GetID() ||639				        (pLoopPlot->isAdjacentTeam(eTeam, true /*bLandOnly*/) && pLoopPlot->getOwner() == NO_PLAYER))640				{641					// See how many outside plots are nearby to monitor642					int iOutsidePlots = pLoopPlot->GetNumAdjacentDifferentTeam(eTeam, true /*bIgnoreWater*/);643644					if(iOutsidePlots > 0)645					{646						newTarget.SetTargetType(AI_HOMELAND_TARGET_SENTRY_POINT);647						newTarget.SetTargetX(pLoopPlot->getX());648						newTarget.SetTargetY(pLoopPlot->getY());649						newTarget.SetAuxData(pLoopPlot);650651						// Get weight for this sentry point652						int iWeight = iOutsidePlots * 100;653						iWeight += pLoopPlot->defenseModifier(eTeam, true);654						iWeight += m_pPlayer->GetPlotDanger(*pLoopPlot);655656						CvCity* pFriendlyCity = m_pPlayer->GetClosestFriendlyCity(*pLoopPlot, 5 /*i SearchRadius */);657						if(pFriendlyCity && pFriendlyCity->getOwner() == m_pPlayer->GetID())658						{659							iWeight += pFriendlyCity->getThreatValue() * pFriendlyCity->getPopulation() / 50;660							if(pFriendlyCity->isCapital())661							{662								iWeight = (iWeight * GC.getAI_MILITARY_CITY_THREAT_WEIGHT_CAPITAL()) / 100;663							}664						}665666						if(pLoopPlot->isHills())667						{668							iWeight *= 2;669						}670						if(pLoopPlot->isCoastalLand())671						{672							iWeight /= 2;673						}674675						newTarget.SetAuxIntData(iWeight);676						m_TargetedSentryPoints.push_back(newTarget);677					}678				}679			}680681			// ... road segment in friendly territory?682			else if(pLoopPlot->getTeam() == eTeam && pLoopPlot->isRoute())683			{684				newTarget.SetTargetType(AI_HOMELAND_TARGET_HOME_ROAD);685				newTarget.SetTargetX(pLoopPlot->getX());686				newTarget.SetTargetY(pLoopPlot->getY());687				newTarget.SetAuxData(pLoopPlot);688				m_TargetedHomelandRoads.push_back(newTarget);689			}690		}691	}692693	// Post-processing on targets694	EliminateAdjacentSentryPoints();695	EliminateAdjacentHomelandRoads();696	std::stable_sort(m_TargetedCities.begin(), m_TargetedCities.end());697}

Calls

Called by