Bazaar developer reference

CvTacticalAI::MoveToEmptySpaceNearTarget(UnitHandle, CvPlot *, bool)

Defined in
CvTacticalAI.cpp, line 9717
Class
CvTacticalAI
Hook name
CivHooks::CvTacticalAI_MoveToEmptySpaceNearTarget

Hooks

static bool BeforeCvTacticalAI_MoveToEmptySpaceNearTarget(CvTacticalAI* self, UnitHandle pUnit, CvPlot* pTarget, bool bLand, bool& result);static void AfterCvTacticalAI_MoveToEmptySpaceNearTarget(CvTacticalAI* self, UnitHandle pUnit, CvPlot* pTarget, bool bLand, bool& result);static bool ReplaceCvTacticalAI_MoveToEmptySpaceNearTarget(bool (*original)(CvTacticalAI*, UnitHandle, CvPlot*, bool), CvTacticalAI* self, UnitHandle pUnit, CvPlot* pTarget, bool bLand);CivRegisterBefore<CivHooks::CvTacticalAI_MoveToEmptySpaceNearTarget>(host, &BeforeCvTacticalAI_MoveToEmptySpaceNearTarget);CivRegisterAfter<CivHooks::CvTacticalAI_MoveToEmptySpaceNearTarget>(host, &AfterCvTacticalAI_MoveToEmptySpaceNearTarget);CivRegisterReplace<CivHooks::CvTacticalAI_MoveToEmptySpaceNearTarget>(host, &ReplaceCvTacticalAI_MoveToEmptySpaceNearTarget);

Code

9716/// Move up to our target avoiding our own units if possible9717bool CvTacticalAI::MoveToEmptySpaceNearTarget(UnitHandle pUnit, CvPlot* pTarget, bool bLand)9718{9719#if defined(MOD_AI_SMART_V3)9720	if (MOD_AI_SMART_V3 && pUnit->IsCanAttackRanged() && (pUnit->GetRange() > 1))9721	{9722		return MoveToEmptySpaceTwoFromTarget(pUnit, pTarget, bLand);9723	}97249725	if (MOD_AI_SMART_V3 && plotDistance(pUnit->getX(), pUnit->getY(), pTarget->getX(), pTarget->getY()) == 1 && pUnit->plot()->isWater() != bLand)9726	{9727		return true;9728	}9729#endif97309731#if defined(MOD_AI_SMART_V3)9732	CvWeightedVector<CvPlot*> pPossiblePlots;9733	int iTurnsNeeded;9734#endif9735	CvPlot* pLoopPlot;97369737	// Look at spaces adjacent to target9738	for(int iI = 0; iI < NUM_DIRECTION_TYPES; iI++)9739	{9740		pLoopPlot = plotDirection(pTarget->getX(), pTarget->getY(), ((DirectionTypes)iI));9741		if(pLoopPlot != NULL && pLoopPlot->isWater() != bLand)9742		{9743			// Must be currently empty of friendly combat units9744			if(!pLoopPlot->getBestDefender(m_pPlayer->GetID()))9745			{9746				// Enemies too9747				if(!pLoopPlot->getBestDefender(NO_PLAYER, m_pPlayer->GetID()))9748				{9749					// And if it is a city, make sure we are friends with them, else we will automatically attack9750					if(pLoopPlot->getPlotCity() == NULL || pLoopPlot->isFriendlyCity(*pUnit, false))9751					{9752						// Find a path to this space9753#if defined(MOD_AI_SMART_V3)9754						iTurnsNeeded = MAX_INT;9755						bool bPathFound = MOD_AI_SMART_V3 ? pUnit->GeneratePath(pLoopPlot, MOVE_UNITS_IGNORE_DANGER, false, &iTurnsNeeded) : pUnit->GeneratePath(pLoopPlot);9756						if(bPathFound)9757#else9758						if(pUnit->GeneratePath(pLoopPlot))9759#endif9760						{9761							// Go ahead with mission9762#if defined(MOD_AI_SMART_V3)9763							if (MOD_AI_SMART_V3)9764							{9765								pPossiblePlots.push_back(pLoopPlot, iTurnsNeeded);9766							}9767							else9768							{9769#endif9770								bool bMoveWasSafe;9771								MoveToUsingSafeEmbark(pUnit, pLoopPlot, bMoveWasSafe);9772								return true;9773#if defined(MOD_AI_SMART_V3)9774							}9775#endif9776						}9777					}9778				}9779			}9780		}9781	}9782	9783#if defined(MOD_AI_SMART_V3)9784	if (MOD_AI_SMART_V3 && pPossiblePlots.size() > 0)9785	{9786		iTurnsNeeded = pPossiblePlots.GetWeight(0);9787		pPossiblePlots.SortItems();9788		pLoopPlot = pPossiblePlots.GetElement(0);9789		for (int iI = 1; iI < pPossiblePlots.size(); iI++)9790		{9791			if (pPossiblePlots.GetWeight(iI) == iTurnsNeeded)9792			{9793				if (m_pPlayer->GetPlotDanger(*pLoopPlot) > m_pPlayer->GetPlotDanger(*pPossiblePlots.GetElement(iI)))9794				{9795					pLoopPlot = pPossiblePlots.GetElement(iI);9796				}9797			}9798			else9799				break;9800		}98019802		// Go ahead with mission9803		bool bMoveWasSafe;9804		MoveToUsingSafeEmbark(pUnit, pLoopPlot, bMoveWasSafe);9805		return true;9806	}9807#endif98089809	return false;9810}

Calls

Called by