Bazaar developer reference

CvUnit::IsEnemyInMovementRange(bool, bool)

Defined in
CvUnit.cpp, line 23761
Class
CvUnit
Hook name
CivHooks::CvUnit_IsEnemyInMovementRange

Hooks

static bool BeforeCvUnit_IsEnemyInMovementRange(CvUnit* self, bool bOnlyFortified, bool bOnlyCities, bool& result);static void AfterCvUnit_IsEnemyInMovementRange(CvUnit* self, bool bOnlyFortified, bool bOnlyCities, bool& result);static bool ReplaceCvUnit_IsEnemyInMovementRange(bool (*original)(CvUnit*, bool, bool), CvUnit* self, bool bOnlyFortified, bool bOnlyCities);CivRegisterBefore<CivHooks::CvUnit_IsEnemyInMovementRange>(host, &BeforeCvUnit_IsEnemyInMovementRange);CivRegisterAfter<CivHooks::CvUnit_IsEnemyInMovementRange>(host, &AfterCvUnit_IsEnemyInMovementRange);CivRegisterReplace<CivHooks::CvUnit_IsEnemyInMovementRange>(host, &ReplaceCvUnit_IsEnemyInMovementRange);

Code

23761bool CvUnit::IsEnemyInMovementRange(bool bOnlyFortified, bool bOnlyCities)23762{23763	VALIDATE_OBJECT2376423765	CvTeam& kTeam = GET_TEAM(getTeam());23766	int iMovementRange = maxMoves() / GC.getMOVE_DENOMINATOR();2376723768	// check quickly around the unit before hitting the pathfinder2376923770	bool bPotentialTargetFound = false;23771	for(int iPlayerLoop = 0; iPlayerLoop < MAX_PLAYERS; iPlayerLoop++)23772	{23773		if(bPotentialTargetFound)23774		{23775			break;23776		}2377723778		CvPlayer& kPlayer = GET_PLAYER((PlayerTypes) iPlayerLoop);23779		if(kTeam.isAtWar(kPlayer.getTeam()))23780		{23781			if(!bOnlyFortified)23782			{23783				CvCity* pLoopCity = NULL;23784				int iLoop = 0;23785				for(pLoopCity = kPlayer.firstCity(&iLoop); pLoopCity != NULL; pLoopCity = kPlayer.nextCity(&iLoop))23786				{23787					CvPlot* pPlot = pLoopCity->plot();23788					if(pPlot && pPlot->isVisible(getTeam()) && area() == pPlot->area())23789					{23790						if(plotDistance(getX(), getY(), pLoopCity->getX(), pLoopCity->getY()) <= iMovementRange)23791						{23792							bPotentialTargetFound = true;23793							break;23794						}23795					}23796				}23797			}2379823799			if(!bOnlyCities)23800			{23801				CvUnit* pLoopUnit = NULL;23802				int iLoop = 0;23803				for(pLoopUnit = kPlayer.firstUnit(&iLoop); pLoopUnit != NULL; pLoopUnit = kPlayer.nextUnit(&iLoop))23804				{23805					// shortcut out23806					if(bOnlyFortified)23807					{23808						// if the unit is not fortified, skip it!23809						if(!pLoopUnit->IsFortifiedThisTurn())23810						{23811							continue;23812						}23813					}2381423815					CvPlot* pPlot = pLoopUnit->plot();23816#if defined(MOD_BUGFIX_HOVERING_PATHFINDER)23817					if(pPlot && pPlot->isVisible(getTeam()) && (area() == pPlot->area() || IsHoveringUnit() || canMoveAllTerrain()))23818#else23819					if(pPlot && pPlot->isVisible(getTeam()) && (area() == pPlot->area() || IsHoveringUnit()))23820#endif23821					{23822						if(plotDistance(getX(), getY(), pLoopUnit->getX(), pLoopUnit->getY()) <= iMovementRange)23823						{23824							bPotentialTargetFound = true;23825							break;23826						}23827					}23828				}23829			}23830		}23831	}2383223833	if(!bPotentialTargetFound)23834	{23835		return false;23836	}2383723838	CvTwoLayerPathFinder& thePathfinder = GC.getInterfacePathFinder();2383923840	// change the unit pathfinder to use these funcs instead23841	thePathfinder.SetDestValidFunc(NULL);23842	thePathfinder.SetValidFunc(UIPathValid);23843	if(bOnlyFortified)23844	{23845		thePathfinder.SetIsPathDestFunc(AttackFortifiedPathDest);23846	}23847	else if(bOnlyCities)23848	{23849		thePathfinder.SetIsPathDestFunc(AttackCityPathDest);23850	}23851	else23852	{23853		thePathfinder.SetIsPathDestFunc(AttackPathDest);23854	}2385523856	// call the pathfinder23857	thePathfinder.SetData(this);23858	bool bCanFindPath = thePathfinder.GeneratePath(getX(), getY(), -1, -1, MOVE_DECLARE_WAR, false);2385923860	// change the unit pathfinder back23861	thePathfinder.SetValidFunc(PathValid);23862	thePathfinder.SetDestValidFunc(PathDestValid);23863	thePathfinder.SetIsPathDestFunc(PathDest);23864	thePathfinder.ForceReset();2386523866	return bCanFindPath;23867}

Calls

Called by