Bazaar developer reference

CvUnit::CanSwapWithUnitHere(CvPlot &) const

Defined in
CvUnit.cpp, line 21204
Class
CvUnit
Hook name
CivHooks::CvUnit_CanSwapWithUnitHere

Hooks

static bool BeforeCvUnit_CanSwapWithUnitHere(const CvUnit* self, CvPlot& swapPlot, bool& result);static void AfterCvUnit_CanSwapWithUnitHere(const CvUnit* self, CvPlot& swapPlot, bool& result);static bool ReplaceCvUnit_CanSwapWithUnitHere(bool (*original)(const CvUnit*, CvPlot&), const CvUnit* self, CvPlot& swapPlot);CivRegisterBefore<CivHooks::CvUnit_CanSwapWithUnitHere>(host, &BeforeCvUnit_CanSwapWithUnitHere);CivRegisterAfter<CivHooks::CvUnit_CanSwapWithUnitHere>(host, &AfterCvUnit_CanSwapWithUnitHere);CivRegisterReplace<CivHooks::CvUnit_CanSwapWithUnitHere>(host, &ReplaceCvUnit_CanSwapWithUnitHere);

Code

21204bool CvUnit::CanSwapWithUnitHere(CvPlot& swapPlot) const21205{21206	VALIDATE_OBJECT21207	bool bSwapPossible = false;2120821209	if(getDomainType() == DOMAIN_LAND || getDomainType() == DOMAIN_SEA)21210	{21211		if(canEnterTerrain(swapPlot))21212		{21213			// Can I get there this turn?21214			CvUnit* pUnit = (CvUnit*)this;21215			if(GC.getIgnoreUnitsPathFinder().DoesPathExist(*(pUnit), plot(), &swapPlot))21216			{21217				CvPlot* pEndTurnPlot = GC.getIgnoreUnitsPathFinder().GetPathEndTurnPlot();21218				if(pEndTurnPlot == &swapPlot)21219				{21220#if defined(MOD_GLOBAL_STACKING_RULES)21221					if(swapPlot.getNumFriendlyUnitsOfType(this) >= swapPlot.getUnitLimit())21222#else21223					if(swapPlot.getNumFriendlyUnitsOfType(this) >= GC.getPLOT_UNIT_LIMIT())21224#endif21225					{21226						const IDInfo* pUnitNode;21227						CvUnit* pLoopUnit;21228						pUnitNode = swapPlot.headUnitNode();21229						while(pUnitNode != NULL)21230						{21231							pLoopUnit = (CvUnit*)::getUnit(*pUnitNode);21232							pUnitNode = swapPlot.nextUnitNode(pUnitNode);2123321234							// A unit can't swap with itself (slewis)21235							if (pLoopUnit == this)21236							{21237								continue;21238							}2123921240							// Make sure units belong to the same player21241							if(pLoopUnit && pLoopUnit->getOwner() == getOwner())21242							{21243								if(AreUnitsOfSameType(*pLoopUnit))21244								{21245									CvPlot* here = plot();21246									if(here && pLoopUnit->canEnterTerrain(*here))21247									{21248										// Can the unit I am swapping with get to me this turn?21249										if(pLoopUnit->ReadyToMove() && GC.getIgnoreUnitsPathFinder().DoesPathExist(*(pLoopUnit), &swapPlot, plot()))21250										{21251											CvPlot* pPathEndTurnPlot = GC.getIgnoreUnitsPathFinder().GetPathEndTurnPlot();21252											if(pPathEndTurnPlot == plot())21253											{21254												bSwapPossible = true;21255												break;21256											}21257										}21258									}21259								}21260							}21261						}21262					}21263				}21264			}21265		}21266	}2126721268	return bSwapPossible;21269}

Calls

Called by