Bazaar developer reference

CvUnit::canRangeStrikeAt(int, int, bool, bool) const

Defined in
CvUnit.cpp, line 21753
Class
CvUnit
Hook name
CivHooks::CvUnit_canRangeStrikeAt
Lua events
GameEvents.UnitCanRangeAttackAt

Hooks

static bool BeforeCvUnit_canRangeStrikeAt(const CvUnit* self, int iX, int iY, bool bNeedWar, bool bNoncombatAllowed, bool& result);static void AfterCvUnit_canRangeStrikeAt(const CvUnit* self, int iX, int iY, bool bNeedWar, bool bNoncombatAllowed, bool& result);static bool ReplaceCvUnit_canRangeStrikeAt(bool (*original)(const CvUnit*, int, int, bool, bool), const CvUnit* self, int iX, int iY, bool bNeedWar, bool bNoncombatAllowed);CivRegisterBefore<CivHooks::CvUnit_canRangeStrikeAt>(host, &BeforeCvUnit_canRangeStrikeAt);CivRegisterAfter<CivHooks::CvUnit_canRangeStrikeAt>(host, &AfterCvUnit_canRangeStrikeAt);CivRegisterReplace<CivHooks::CvUnit_canRangeStrikeAt>(host, &ReplaceCvUnit_canRangeStrikeAt);

Code

21753bool CvUnit::canRangeStrikeAt(int iX, int iY, bool bNeedWar, bool bNoncombatAllowed) const21754{21755	VALIDATE_OBJECT2175621757	if(!canRangeStrike())21758	{21759		return false;21760	}2176121762	if(!canEverRangeStrikeAt(iX, iY))21763	{21764		return false;21765	}2176621767	CvPlot* pTargetPlot = GC.getMap().plot(iX, iY);2176821769	// If it's NOT a city, see if there are any units to aim for21770	if(!pTargetPlot->isCity())21771	{21772		if(bNeedWar)21773		{21774			const CvUnit* pDefender = airStrikeTarget(*pTargetPlot, bNoncombatAllowed);21775			if(NULL == pDefender)21776			{21777#if defined(MOD_EVENTS_UNIT_RANGEATTACK)21778				if (MOD_EVENTS_UNIT_RANGEATTACK) {21779					if (GAMEEVENTINVOKE_TESTANY(GAMEEVENT_UnitCanRangeAttackAt, getOwner(), GetID(), iX, iY, bNeedWar) == GAMEEVENTRETURN_TRUE) {21780						return true;21781					}21782				}21783#endif2178421785				return false;21786			}2178721788#if defined(MOD_GLOBAL_SUBS_UNDER_ICE_IMMUNITY)21789				// if the defender is a sub21790				if (pDefender->getInvisibleType() == 0) {21791					// if the plot is ice, unless the attacker is also a sub, return false21792					if (pTargetPlot->getFeatureType() == FEATURE_ICE && this->getInvisibleType() != 0) {21793						return false;21794					}2179521796					// if the attacker is an archer type unit, return false21797					if (this->getUnitCombatType() == (UnitCombatTypes)GC.getInfoTypeForString("UNITCOMBAT_ARCHER", true)) {21798						return false;21799					}21800				}21801#endif21802		}21803		// We don't need to be at war (yet) with a Unit here, so let's try to find one21804		else21805		{21806			const IDInfo* pUnitNode = pTargetPlot->headUnitNode();21807			const CvUnit* pLoopUnit;21808			bool bFoundUnit = false;2180921810			CvTeam& myTeam = GET_TEAM(getTeam());2181121812			while(pUnitNode != NULL)21813			{21814				pLoopUnit = ::getUnit(*pUnitNode);21815				pUnitNode = pTargetPlot->nextUnitNode(pUnitNode);2181621817#if defined(MOD_GLOBAL_SUBS_UNDER_ICE_IMMUNITY)21818				// if the defender is a sub21819				if (pLoopUnit->getInvisibleType() == 0) {21820					// if the plot is ice, unless the attacker is also a sub, ignore them21821					if (pTargetPlot->getFeatureType() == FEATURE_ICE && this->getInvisibleType() != 0) {21822						continue;21823					}2182421825					// if the attacker is an archer type unit, ignore them21826					if (this->getUnitCombatType() == (UnitCombatTypes)GC.getInfoTypeForString("UNITCOMBAT_ARCHER", true)) {21827						continue;21828					}21829				}21830#endif2183121832				if(!pLoopUnit) continue;2183321834				TeamTypes loopTeam = pLoopUnit->getTeam();2183521836				// Make sure it's a valid Team21837#if defined(MOD_EVENTS_WAR_AND_PEACE)21838				if(myTeam.isAtWar(loopTeam) || myTeam.canDeclareWar(loopTeam, getOwner()))21839#else21840				if(myTeam.isAtWar(loopTeam) || myTeam.canDeclareWar(loopTeam))21841#endif21842				{21843					bFoundUnit = true;21844					break;21845				}21846			}2184721848			if(!bFoundUnit)21849			{21850#if defined(MOD_EVENTS_UNIT_RANGEATTACK)21851				if (MOD_EVENTS_UNIT_RANGEATTACK) {21852					if (GAMEEVENTINVOKE_TESTANY(GAMEEVENT_UnitCanRangeAttackAt, getOwner(), GetID(), iX, iY, bNeedWar) == GAMEEVENTRETURN_TRUE) {21853						return true;21854					}21855				}21856#endif2185721858				return false;21859			}21860		}21861	}21862	// If it is a City, only consider those we're at war with21863	else21864	{21865		CvAssert(pTargetPlot->getPlotCity() != NULL);2186621867		// If you're already at war don't need to check21868		if(!atWar(getTeam(), pTargetPlot->getPlotCity()->getTeam()))21869		{21870			if(bNeedWar)21871			{21872				return false;21873			}21874			// Don't need to be at war with this City's owner (yet)21875			else21876			{21877#if defined(MOD_EVENTS_WAR_AND_PEACE)21878				if(!GET_TEAM(getTeam()).canDeclareWar(pTargetPlot->getPlotCity()->getTeam(), getOwner()))21879#else21880				if(!GET_TEAM(getTeam()).canDeclareWar(pTargetPlot->getPlotCity()->getTeam()))21881#endif21882				{21883					return false;21884				}21885			}21886		}21887	}2188821889	return true;21890}

Calls

Called by