UTIL_EntitiesInSphere

From Valve Developer Community
Jump to navigation Jump to search

UTIL_EntitiesInSphere is a UTIL provided in the Source code for finding entities within a specified sphere. It takes an entity list, a max amount of entities to find, the center of the sphere, a radius and a flagmask.


Usage

//-----------------------------------------------------------------------------
// Purpose: Returns the number of entities in the sphere, plus an array of pointers to the entities.
//          
// Input  : CBaseEntity - *pList   | A pointer to an array, the function will store a pointer to the Entities it finds in this array.
// Input  : int - listMax     | Maximum number of entities to return
// Input  : Vector - &center  | Center of the Sphere to check from
// Input  : float - radius    | radius of the sphere to check in
// Input  : int - flagmask    | 
// Output : *pList
// Output : int - Returns the amount of entities it finds
//-----------------------------------------------------------------------------
inline int UTIL_EntitiesInSphere( CBaseEntity **pList, int listMax, const Vector &center, float radius, int flagMask )
//-----------------------------------------------------------------------------
// Purpose: Returns the number of entities in the sphere.
//          
// Input  : Vector - &mins    | The mins of a box to check. World space coordinate.
// Input  : Vector - &maxs    | The maxs of a box to check. World space coordinate.
// Input  : CFlaggedEntitiesEnum *pEnum    | TODO
// Output : int - Returns the amount of entities it finds
//-----------------------------------------------------------------------------
int  UTIL_EntitiesInSphere( const Vector &center, float radius, CFlaggedEntitiesEnum *pEnum  );

flagmask is avaible in const.h, lines 97-132

Examples

//What this does, is look through entities in a sphere, and then
//checks to see if they are valid, and if they are
//adds them to a second list of valid entities.
//Create an array of CBaseEntity pointers
CBaseEntity *ppEnts[256];

//Get the center of the object
Vector vecCenter = WorldSpaceCenter();

float flRadius = 500.0f;
vecCenter.z -= flRadius * 0.8f;
int nEntCount = UTIL_EntitiesInSphere( ppEnts, 256, vecCenter, flRadius, 0 );
CBaseEntity *ppCandidates[256];
int nCandidateCount = 0;
int i;
for ( i = 0; i < nEntCount; i++ )
{
        //Look through the entities it found
	if ( ppEnts[i] == NULL )
		continue;

	// Zap metal or flesh things.
	if ( !IsValidZapTarget( ppEnts[i] ) )
		continue;
        //Add them to the new, valid only, Canidate list.
	ppCandidates[ nCandidateCount++ ] = ppEnts[i];
}
//This example defines a variable named RALLY_SEARCH_ENTS, as a clear
//and easy way to see what it is instead of just writing '30' into
//both spots.

const int RALLY_SEARCH_ENTS	= 30;
CBaseEntity *pEntities[RALLY_SEARCH_ENTS];
int iNumEntities = UTIL_EntitiesInSphere( pEntities, RALLY_SEARCH_ENTS, vecCenter, flRadius, 0 )