Bazaar developer reference

CvUnit::canRebaseAt(const CvPlot *, int, int) const

Defined in
CvUnit.cpp, line 7449
Class
CvUnit
Hook name
CivHooks::CvUnit_canRebaseAt
Lua events
GameEvents.CanRebaseInCity, GameEvents.CanRebaseTo

Hooks

static bool BeforeCvUnit_canRebaseAt(const CvUnit* self, const CvPlot* pPlot, int iX, int iY, bool& result);static void AfterCvUnit_canRebaseAt(const CvUnit* self, const CvPlot* pPlot, int iX, int iY, bool& result);static bool ReplaceCvUnit_canRebaseAt(bool (*original)(const CvUnit*, const CvPlot*, int, int), const CvUnit* self, const CvPlot* pPlot, int iX, int iY);CivRegisterBefore<CivHooks::CvUnit_canRebaseAt>(host, &BeforeCvUnit_canRebaseAt);CivRegisterAfter<CivHooks::CvUnit_canRebaseAt>(host, &AfterCvUnit_canRebaseAt);CivRegisterReplace<CivHooks::CvUnit_canRebaseAt>(host, &ReplaceCvUnit_canRebaseAt);

Code

7449bool CvUnit::canRebaseAt(const CvPlot* pPlot, int iX, int iY) const7450{7451	// If we can't rebase ANYWHERE then we definitely can't rebase at this X,Y7452	if(!canRebase(pPlot))7453	{7454		return false;7455	}74567457	CvPlot* pToPlot = GC.getMap().plot(iX, iY);74587459	// Null plot...7460	if(pToPlot == NULL)7461	{7462		return false;7463	}74647465	// Same plot...7466	if(pToPlot == pPlot)7467	{7468		return false;7469	}74707471	// too far7472	int iRange = GetRange();7473	iRange *= /*200*/ GC.getAIR_UNIT_REBASE_RANGE_MULTIPLIER();7474	iRange /= 100;74757476	if(plotDistance(getX(), getY(), iX, iY) > iRange)7477	{7478		return false;7479	}74807481	// Can't load to the target plot7482	if(!canLoad(*pToPlot))7483	{7484		return false;7485	}74867487	// Rebase to a City?7488	bool bCityToRebase = false;7489	if(pToPlot->isCity())7490	{7491		// City must be owned by us7492		if(pToPlot->getPlotCity()->getOwner() == getOwner())7493		{7494			bCityToRebase = true;7495		}74967497		if (!bCityToRebase)7498		{7499#if defined(MOD_EVENTS_REBASE)7500			if (GAMEEVENTINVOKE_TESTANY(GAMEEVENT_CanRebaseInCity, getOwner(), GetID(), iX, iY) == GAMEEVENTRETURN_TRUE) {7501				bCityToRebase = true;7502			} else {7503#endif7504			ICvEngineScriptSystem1* pkScriptSystem = gDLL->GetScriptSystem();7505			if (pkScriptSystem)7506			{7507				CvLuaArgsHandle args;7508				args->Push(getOwner());7509				args->Push(GetID());7510				args->Push(iX);7511				args->Push(iY);75127513				// Attempt to execute the game events.7514				// Will return false if there are no registered listeners.7515				bool bResult = false;7516				if (LuaSupport::CallTestAny(pkScriptSystem, "CanRebaseInCity", args.get(), bResult))7517				{7518					// Check the result.7519					if (bResult == true)7520					{7521						bCityToRebase = true;7522					}7523				}7524			}7525#if defined(MOD_EVENTS_REBASE)7526			}7527#endif7528		}75297530		int iUnitsThere = pToPlot->countNumAirUnits(getTeam());7531		if (iUnitsThere >= pToPlot->getPlotCity()->GetMaxAirUnits())7532		{7533			return false;7534		}7535	}75367537	// Rebase onto Unit which can hold cargo7538	bool bUnitToRebase = false;75397540	if(!bCityToRebase)7541	{7542		IDInfo* pUnitNode;7543		CvUnit* pLoopUnit;7544		pUnitNode = pToPlot->headUnitNode();7545		while(pUnitNode != NULL)7546		{7547			pLoopUnit = ::getUnit(*pUnitNode);7548			pUnitNode = pToPlot->nextUnitNode(pUnitNode);75497550			// Can this Unit hold us?7551			if(canLoadUnit(*pLoopUnit, *pToPlot))7552			{7553				bUnitToRebase = true;75547555				break;7556			}7557		}7558	}75597560	// No City or Unit at the target to rebase to7561	if(!bCityToRebase && !bUnitToRebase)7562	{7563#if defined(MOD_GLOBAL_RELOCATION)7564		// Is there a friendly improvement that AllowsRebaseTo7565		ImprovementTypes eImprovement = pToPlot->getImprovementType();7566		if (eImprovement != NO_IMPROVEMENT)7567		{7568			CvImprovementEntry *pkEntry = GC.getImprovementInfo(eImprovement);7569			if (pkEntry && pkEntry->IsAllowsRebaseTo() && !pToPlot->IsImprovementPillaged() && pToPlot->IsFriendlyTerritory(getOwner()) && !pToPlot->isVisibleEnemyUnit(getOwner()))7570			{7571				// Check the loading limit for this improvement7572				if (pToPlot->countNumAirUnits(getTeam()) < (GC.getBASE_CITY_AIR_STACKING() / 2))7573				{7574					return true;7575				}7576			}7577		}7578#endif75797580#if defined(MOD_EVENTS_REBASE)7581		if (MOD_EVENTS_REBASE) {7582			if (GAMEEVENTINVOKE_TESTANY(GAMEEVENT_CanRebaseTo, getOwner(), GetID(), iX, iY, bCityToRebase) == GAMEEVENTRETURN_TRUE) {7583				return true;7584			}7585		} else {7586#endif7587		ICvEngineScriptSystem1* pkScriptSystem = gDLL->GetScriptSystem();7588		if (pkScriptSystem) 7589		{7590			CvLuaArgsHandle args;7591			args->Push(getOwner());7592			args->Push(GetID());7593			args->Push(iX);7594			args->Push(iY);75957596			// Attempt to execute the game events.7597			// Will return false if there are no registered listeners.7598			bool bResult = false;7599			if (LuaSupport::CallTestAny(pkScriptSystem, "CanRebaseTo", args.get(), bResult))7600			{7601				// Check the result.7602				if (bResult == true)7603				{7604					return true;7605				}7606			}7607		}7608#if defined(MOD_EVENTS_REBASE)7609		}7610#endif76117612		return false;7613	}76147615	return true;7616}

Calls

Called by