Bazaar developer reference

CvUnit::jumpToNearestValidPlot()

Defined in
CvUnit.cpp, line 3635
Class
CvUnit
Hook name
CivHooks::CvUnit_jumpToNearestValidPlot

Hooks

static bool BeforeCvUnit_jumpToNearestValidPlot(CvUnit* self, bool& result);static void AfterCvUnit_jumpToNearestValidPlot(CvUnit* self, bool& result);static bool ReplaceCvUnit_jumpToNearestValidPlot(bool (*original)(CvUnit*), CvUnit* self);CivRegisterBefore<CivHooks::CvUnit_jumpToNearestValidPlot>(host, &BeforeCvUnit_jumpToNearestValidPlot);CivRegisterAfter<CivHooks::CvUnit_jumpToNearestValidPlot>(host, &AfterCvUnit_jumpToNearestValidPlot);CivRegisterReplace<CivHooks::CvUnit_jumpToNearestValidPlot>(host, &ReplaceCvUnit_jumpToNearestValidPlot);

Code

3635bool CvUnit::jumpToNearestValidPlot()3636{3637	VALIDATE_OBJECT3638	CvCity* pNearestCity;3639	CvPlot* pLoopPlot;3640	CvPlot* pBestPlot;3641	int iValue;3642	int iBestValue;3643	int iI;36443645	CvAssertMsg(!isAttacking(), "isAttacking did not return false as expected");3646	CvAssertMsg(!isFighting(), "isFighting did not return false as expected");36473648	pNearestCity = GC.getMap().findCity(getX(), getY(), getOwner());36493650	iBestValue = INT_MAX;3651	pBestPlot = NULL;36523653	for(iI = 0; iI < GC.getMap().numPlots(); iI++)3654	{3655		pLoopPlot = GC.getMap().plotByIndexUnchecked(iI);36563657		if(pLoopPlot && pLoopPlot->isValidDomainForLocation(*this))3658		{3659			if(canMoveInto(*pLoopPlot))3660			{3661#if defined(MOD_GLOBAL_STACKING_RULES)3662				if(pLoopPlot->getNumFriendlyUnitsOfType(this) < pLoopPlot->getUnitLimit())3663#else3664				if(pLoopPlot->getNumFriendlyUnitsOfType(this) < GC.getPLOT_UNIT_LIMIT())3665#endif3666				{3667					// Can only jump to a plot if we can enter the territory, and it's NOT enemy territory OR we're a barb3668					if(canEnterTerritory(pLoopPlot->getTeam()) && (isBarbarian() || !isEnemy(pLoopPlot->getTeam(), pLoopPlot)) && !pLoopPlot->isMountain())3669					{3670						CvAssertMsg(!atPlot(*pLoopPlot), "atPlot(pLoopPlot) did not return false as expected");36713672						if((getDomainType() != DOMAIN_AIR) || pLoopPlot->isFriendlyCity(*this, true))3673						{3674#if defined(MOD_GLOBAL_PASSABLE_FORTS)3675#if defined(MOD_BUGFIX_NAVAL_NEAREST_WATER)3676							bool bCanEnter = getDomainType() != DOMAIN_SEA || (pLoopPlot->isFriendlyCityOrPassableImprovement(*this, true) && (MOD_GLOBAL_PASSABLE_FORTS_ANY || pLoopPlot->isCoastalLand())) || (pLoopPlot->isWater() && !pLoopPlot->isLake());3677#else3678							bool bCanEnter = getDomainType() != DOMAIN_SEA || (pLoopPlot->isFriendlyCityOrPassableImprovement(*this, true) && (MOD_GLOBAL_PASSABLE_FORTS_ANY || pLoopPlot->isCoastalLand())) || pLoopPlot->isWater());3679#endif3680							if(bCanEnter)3681#else3682#if defined(MOD_BUGFIX_NAVAL_NEAREST_WATER)3683							if(getDomainType() != DOMAIN_SEA || (pLoopPlot->isFriendlyCity(*this, true) && pLoopPlot->isCoastalLand()) || (pLoopPlot->isWater() && !pLoopPlot->isLake()))3684#else3685							if(getDomainType() != DOMAIN_SEA || (pLoopPlot->isFriendlyCity(*this, true) && pLoopPlot->isCoastalLand()) || pLoopPlot->isWater())3686#endif3687#endif3688							{3689								if(pLoopPlot->isRevealed(getTeam()))3690								{3691									iValue = (plotDistance(getX(), getY(), pLoopPlot->getX(), pLoopPlot->getY()) * 2);36923693									if(pNearestCity != NULL)3694									{3695										iValue += plotDistance(pLoopPlot->getX(), pLoopPlot->getY(), pNearestCity->getX(), pNearestCity->getY());3696									}36973698#if defined(MOD_BUGFIX_NAVAL_NEAREST_WATER)3699									if (!MOD_BUGFIX_NAVAL_NEAREST_WATER) {3700										// This is crazy!  Why would we always want to send naval units to the nearest city and not the nearest water plot???3701#endif3702										if(pLoopPlot->area() != area())3703										{3704											iValue *= 3;3705										}3706#if defined(MOD_BUGFIX_NAVAL_NEAREST_WATER)3707									}3708#endif37093710									if(iValue < iBestValue)3711									{3712										iBestValue = iValue;3713										pBestPlot = pLoopPlot;3714									}3715								}3716							}3717						}3718					}3719				}3720			}3721		}3722	}37233724	if(GC.getLogging() && GC.getAILogging())3725	{3726		CvString strLogString;3727		if(pBestPlot != NULL)3728		{3729			strLogString.Format("Jump to nearest valid plot by %s , X: %d, Y: %d, From X: %d, From Y: %d", getName().GetCString(),3730			                    pBestPlot->getX(), pBestPlot->getY(), getX(), getY());3731#if defined(MOD_BUGFIX_USE_GETTERS)3732			GET_MY_PLAYER().GetHomelandAI()->LogHomelandMessage(strLogString);3733#else3734			GET_PLAYER(m_eOwner).GetHomelandAI()->LogHomelandMessage(strLogString);3735#endif3736		}3737		else3738		{3739			strLogString.Format("Can't find a valid plot within range. %s deleted, X: %d, Y: %d", getName().GetCString(), getX(), getY());3740#if defined(MOD_BUGFIX_USE_GETTERS)3741			GET_MY_PLAYER().GetHomelandAI()->LogHomelandMessage(strLogString);3742#else3743			GET_PLAYER(m_eOwner).GetHomelandAI()->LogHomelandMessage(strLogString);3744#endif3745		}3746	}37473748	if(pBestPlot != NULL)3749	{3750		setXY(pBestPlot->getX(), pBestPlot->getY());3751		ClearMissionQueue();3752		SetActivityType(ACTIVITY_AWAKE);3753	}3754	else3755	{3756		CUSTOMLOG("jumpToNearestValidPlot failed for unit %s at plot (%i, %i)", getName().GetCString(), getX(), getY());3757		return false;3758	}37593760	return true;3761}

Calls

Called by