Bazaar developer reference

CvAIOperationNukeAttack::FindBestTarget()

Defined in
CvAIOperation.cpp, line 5643
Class
CvAIOperationNukeAttack
Hook name
CivHooks::CvAIOperationNukeAttack_FindBestTarget

Hooks

static bool BeforeCvAIOperationNukeAttack_FindBestTarget(CvAIOperationNukeAttack* self, CvPlot*& result);static void AfterCvAIOperationNukeAttack_FindBestTarget(CvAIOperationNukeAttack* self, CvPlot*& result);static CvPlot* ReplaceCvAIOperationNukeAttack_FindBestTarget(CvPlot* (*original)(CvAIOperationNukeAttack*), CvAIOperationNukeAttack* self);CivRegisterBefore<CivHooks::CvAIOperationNukeAttack_FindBestTarget>(host, &BeforeCvAIOperationNukeAttack_FindBestTarget);CivRegisterAfter<CivHooks::CvAIOperationNukeAttack_FindBestTarget>(host, &AfterCvAIOperationNukeAttack_FindBestTarget);CivRegisterReplace<CivHooks::CvAIOperationNukeAttack_FindBestTarget>(host, &ReplaceCvAIOperationNukeAttack_FindBestTarget);

Code

5643CvPlot* CvAIOperationNukeAttack::FindBestTarget()5644{5645	CvUnit* pLoopUnit;5646	CvUnit* pBestUnit = NULL;5647	CvCity* pBestCity = NULL;5648	int iBestCity = 0;5649	int iUnitLoop = 0;5650	int iCityLoop = 0;5651	CvPlayerAI& ownerPlayer = GET_PLAYER(m_eOwner);5652	TeamTypes eTeam = ownerPlayer.getTeam();5653	CvTeam& ourTeam = GET_TEAM(eTeam);5654	CvPlayerAI& enemyPlayer = GET_PLAYER(m_eEnemy);56555656	int iBlastRadius = GC.getNUKE_BLAST_RADIUS();56575658	// check all of our units to find the nukes5659	for(pLoopUnit = ownerPlayer.firstUnit(&iUnitLoop); pLoopUnit != NULL; pLoopUnit = ownerPlayer.nextUnit(&iUnitLoop))5660	{5661		if(pLoopUnit && pLoopUnit->canNuke(NULL))5662		{5663			int iUnitRange = pLoopUnit->GetRange();5664			// for all cities of this enemy5665			CvCity* pLoopCity;5666			for(pLoopCity = enemyPlayer.firstCity(&iCityLoop); pLoopCity != NULL; pLoopCity = enemyPlayer.nextCity(&iCityLoop))5667			{5668				if(pLoopCity)5669				{5670					if(plotDistance(pLoopUnit->getX(),pLoopUnit->getY(),pLoopCity->getX(),pLoopCity->getY()) <= iUnitRange)5671					{5672						CvPlot* pCityPlot = pLoopCity->plot();5673						int iThisCityValue = pLoopCity->getPopulation();5674						iThisCityValue -= pLoopCity->getDamage() / 5; // No point nuking a city that is already trashed unless it is good city56755676						// check to see if there is anything good or bad in the radius that we should account for56775678						for(int iDX = -iBlastRadius; iDX <= iBlastRadius; iDX++)5679						{5680							for(int iDY = -iBlastRadius; iDY <= iBlastRadius; iDY++)5681							{5682								CvPlot* pLoopPlot = plotXYWithRangeCheck(pCityPlot->getX(), pCityPlot->getY(), iDX, iDY, iBlastRadius);5683								if(pLoopPlot)5684								{5685									// who owns this plot?5686									PlayerTypes ePlotOwner = pLoopPlot->getOwner();5687									TeamTypes ePlotTeam = pLoopPlot->getTeam();5688									// are we at war with them (or are they us)5689									if(ePlotOwner == m_eOwner)5690									{5691										iThisCityValue -= 1;5692										if(pLoopPlot->getImprovementType() != NO_IMPROVEMENT)5693										{5694											if(!pLoopPlot->IsImprovementPillaged())5695											{5696												iThisCityValue -= 5;5697												if(pLoopPlot->getResourceType(ePlotTeam) != NO_RESOURCE)  // we aren't nuking our own resources5698												{5699													iThisCityValue -= 1000;5700												}5701											}5702										}5703									}5704									else if(ePlotTeam != NO_TEAM && ourTeam.isAtWar(ePlotTeam))5705									{5706										iThisCityValue += 1;5707										if(pLoopPlot->getImprovementType() != NO_IMPROVEMENT)5708										{5709											if(!pLoopPlot->IsImprovementPillaged())5710											{5711												iThisCityValue += 2;5712												if(pLoopPlot->getResourceType(ePlotTeam) != NO_RESOURCE)  // we like nuking our their resources5713												{5714													iThisCityValue += 5;5715												}5716											}5717										}5718									}5719									else if (ePlotOwner != NO_PLAYER) // this will trigger a war5720									{5721										iThisCityValue -= 1000;5722									}57235724									// will we hit any units here?57255726									// Do we want a visibility check here?  We shouldn't know they are here.57275728									const IDInfo* pUnitNode = pLoopPlot->headUnitNode();5729									const CvUnit* pInnerLoopUnit;5730									while(pUnitNode != NULL)5731									{5732										pInnerLoopUnit = ::getUnit(*pUnitNode);5733										pUnitNode = pLoopPlot->nextUnitNode(pUnitNode);5734										if(pInnerLoopUnit != NULL)5735										{5736											PlayerTypes eUnitOwner = pInnerLoopUnit->getOwner();5737											TeamTypes eUnitTeam = pInnerLoopUnit->getTeam();5738											// are we at war with them (or are they us)5739											if(eUnitOwner == m_eOwner)5740											{5741												iThisCityValue -= 2;5742											}5743											else if(ourTeam.isAtWar(eUnitTeam))5744											{5745												iThisCityValue += 2;5746											}5747											else if (ePlotOwner != NO_PLAYER) // this will trigger a war5748											{5749												iThisCityValue -= 1000;5750											}5751										}5752									}5753								}5754							}5755						}57565757						// if this is the capital5758						if(pLoopCity->isCapital())5759						{5760							iThisCityValue *= 2;5761						}57625763						if(iThisCityValue > iBestCity)5764						{5765							pBestUnit = pLoopUnit;5766							pBestCity = pLoopCity;5767							iBestCity = iThisCityValue;5768						}5769					}5770				}5771			}5772		}5773	}57745775	if(pBestCity && pBestUnit)5776	{5777		m_iMusterX = pBestUnit->getX();5778		m_iMusterY = pBestUnit->getY();5779		m_iBestUnitID = pBestUnit->GetID();5780		return pBestCity->plot();5781	}57825783	return NULL;5784}

Calls

Called by