Bazaar developer reference

CvTacticalAI::CanCoverFromEnemy(CvPlot *, int &, int)

Defined in
CvTacticalAI.cpp, line 10452
Class
CvTacticalAI
Hook name
CivHooks::CvTacticalAI_CanCoverFromEnemy

Hooks

static bool BeforeCvTacticalAI_CanCoverFromEnemy(CvTacticalAI* self, CvPlot* pPlot, int& iNumUnitsRequiredToCover, int iAttackingUnitID, bool& result);static void AfterCvTacticalAI_CanCoverFromEnemy(CvTacticalAI* self, CvPlot* pPlot, int& iNumUnitsRequiredToCover, int iAttackingUnitID, bool& result);static bool ReplaceCvTacticalAI_CanCoverFromEnemy(bool (*original)(CvTacticalAI*, CvPlot*, int&, int), CvTacticalAI* self, CvPlot* pPlot, int& iNumUnitsRequiredToCover, int iAttackingUnitID);CivRegisterBefore<CivHooks::CvTacticalAI_CanCoverFromEnemy>(host, &BeforeCvTacticalAI_CanCoverFromEnemy);CivRegisterAfter<CivHooks::CvTacticalAI_CanCoverFromEnemy>(host, &AfterCvTacticalAI_CanCoverFromEnemy);CivRegisterReplace<CivHooks::CvTacticalAI_CanCoverFromEnemy>(host, &ReplaceCvTacticalAI_CanCoverFromEnemy);

Code

10451/// Do I have available friendly units that can stop this hex from being attacked?10452bool CvTacticalAI::CanCoverFromEnemy(CvPlot* pPlot, int& iNumUnitsRequiredToCover, int iAttackingUnitID)10453{10454	CvPlot* pLoopPlot;10455	int iPlotIndex;1045610457	iNumUnitsRequiredToCover = 0;1045810459	// Can't melee attack at sea so those hexes are always covered10460	if(pPlot->isWater())10461	{10462		return true;10463	}1046410465	// Find all the hexes we need to cover10466	for(int iI = 0; iI < NUM_DIRECTION_TYPES; iI++)10467	{10468		pLoopPlot = plotDirection(pPlot->getX(), pPlot->getY(), ((DirectionTypes)iI));1046910470		// Don't need to cover a water hex10471		if(pLoopPlot != NULL && !pLoopPlot->isWater())10472		{10473			iPlotIndex = GC.getMap().plotNum(pLoopPlot->getX(), pLoopPlot->getY());10474			if(m_pMap->GetCell(iPlotIndex)->IsEnemyCanMovePast() && !m_pMap->GetCell(iPlotIndex)->IsFriendlyTurnEndTile())10475			{10476				iNumUnitsRequiredToCover++;10477			}10478		}10479	}1048010481	if(iNumUnitsRequiredToCover == 0)10482	{10483		m_ChosenBlocks.clear();10484		return true;10485	}10486	else10487	{10488		if(iNumUnitsRequiredToCover > (int)m_CurrentTurnUnits.size())10489		{10490			return false;10491		}1049210493		// Have some unit that can cover each hex this turn?10494		m_PotentialBlocks.clear();10495		for(int iI = 0; iI < NUM_DIRECTION_TYPES; iI++)10496		{10497			pLoopPlot = plotDirection(pPlot->getX(), pPlot->getY(), ((DirectionTypes)iI));10498			if(pLoopPlot != NULL && !pLoopPlot->isWater())10499			{10500				iPlotIndex = GC.getMap().plotNum(pLoopPlot->getX(), pLoopPlot->getY());10501				if(m_pMap->GetCell(iPlotIndex)->IsEnemyCanMovePast() && !m_pMap->GetCell(iPlotIndex)->IsFriendlyTurnEndTile())10502				{10503					if(!FindClosestUnit(pLoopPlot, 1, false/*bMustHaveHalfHP*/, false /*bMustBeRangedUnit*/, 2 /*iRangeRequired*/, false /*bNeedsIgnoreLOS*/, true /*bMustBeMeleeUnit*/))10504					{10505						return false;10506					}10507					else10508					{10509						// Save off the units that could get here10510						for(unsigned int iJ = 0; iJ < m_CurrentMoveUnits.size(); iJ++)10511						{10512							if(m_CurrentMoveUnits[iJ].GetID() != iAttackingUnitID)10513							{10514								CvBlockingUnit block;10515								block.SetUnitID(m_CurrentMoveUnits[iJ].GetID());10516								block.SetPlot(pLoopPlot);10517								block.SetNumChoices((int)m_CurrentMoveUnits.size());10518								block.SetDistanceToTarget(m_CurrentMoveUnits[iJ].GetMovesToTarget());10519								m_PotentialBlocks.push_back(block);10520							}10521						}10522					}10523				}10524			}10525		}10526	}1052710528	// Now select exact covering units, making sure we didn't over commit a unit to covering more than one hex10529	return AssignCoveringUnits(iNumUnitsRequiredToCover);10530}

Calls

Called by