//----------------------------------------------------------------------------- // Purpose: Used to confirm a pointer is a pointer to an entity, useful for // asserts. //----------------------------------------------------------------------------- bool CGlobalEntityList::IsEntityPtr( void *pTest ) { if ( pTest ) { const CEntInfo *pInfo = FirstEntInfo(); for ( ;pInfo; pInfo = pInfo->m_pNext ) { if ( pTest == (void *)pInfo->m_pEntity ) return true; } } return false; }
//----------------------------------------------------------------------------- // Purpose: Find the nearest entity along the facing direction from the given origin // within the angular threshold (ignores worldspawn) with the // given classname. // Input : origin - // facing - // threshold - // classname - //----------------------------------------------------------------------------- CBaseEntity *CGlobalEntityList::FindEntityClassNearestFacing( const Vector &origin, const Vector &facing, float threshold, char *classname) { float bestDot = threshold; CBaseEntity *best_ent = NULL; const CEntInfo *pInfo = FirstEntInfo(); for ( ;pInfo; pInfo = pInfo->m_pNext ) { CBaseEntity *ent = (CBaseEntity *)pInfo->m_pEntity; if ( !ent ) { DevWarning( "NULL entity in global entity list!\n" ); continue; } // FIXME: why is this skipping pointsize entities? if (ent->IsPointSized() ) continue; // Make vector to entity Vector to_ent = (ent->GetAbsOrigin() - origin); VectorNormalize( to_ent ); float dot = DotProduct (facing , to_ent ); if (dot > bestDot) { if (FClassnameIs(ent,classname)) { // Ignore if worldspawn if (!FClassnameIs( ent, "worldspawn" ) && !FClassnameIs( ent, "soundent")) { bestDot = dot; best_ent = ent; } } } } return best_ent; }
//----------------------------------------------------------------------------- // Purpose: Find the nearest entity along the facing direction from the given origin // within the angular threshold (ignores worldspawn) // Input : origin - // facing - // threshold - //----------------------------------------------------------------------------- CBaseEntity *CGlobalEntityList::FindEntityNearestFacing( const Vector &origin, const Vector &facing, float threshold) { float bestDot = threshold; CBaseEntity *best_ent = NULL; const CEntInfo *pInfo = FirstEntInfo(); for ( ;pInfo; pInfo = pInfo->m_pNext ) { CBaseEntity *ent = (CBaseEntity *)pInfo->m_pEntity; if ( !ent ) { DevWarning( "NULL entity in global entity list!\n" ); continue; } // Ignore logical entities if (!ent->edict()) continue; // Make vector to entity Vector to_ent = ent->WorldSpaceCenter() - origin; VectorNormalize(to_ent); float dot = DotProduct( facing, to_ent ); if (dot <= bestDot) continue; // Ignore if worldspawn if (!FStrEq( STRING(ent->m_iClassname), "worldspawn") && !FStrEq( STRING(ent->m_iClassname), "soundent")) { bestDot = dot; best_ent = ent; } } return best_ent; }
//----------------------------------------------------------------------------- // Purpose: Used to iterate all the entities within a sphere. // Input : pStartEntity - // vecCenter - // flRadius - //----------------------------------------------------------------------------- CBaseEntity *CGlobalEntityList::FindEntityInSphere( CBaseEntity *pStartEntity, const Vector &vecCenter, float flRadius ) { const CEntInfo *pInfo = pStartEntity ? GetEntInfoPtr( pStartEntity->GetRefEHandle() )->m_pNext : FirstEntInfo(); for ( ;pInfo; pInfo = pInfo->m_pNext ) { CBaseEntity *ent = (CBaseEntity *)pInfo->m_pEntity; if ( !ent ) { DevWarning( "NULL entity in global entity list!\n" ); continue; } if ( !ent->edict() ) continue; Vector vecRelativeCenter; ent->CollisionProp()->WorldToCollisionSpace( vecCenter, &vecRelativeCenter ); if ( !IsBoxIntersectingSphere( ent->CollisionProp()->OBBMins(), ent->CollisionProp()->OBBMaxs(), vecRelativeCenter, flRadius ) ) continue; return ent; } // nothing found return NULL; }
//----------------------------------------------------------------------------- // Purpose: Iterates the entities with a given target. // Input : pStartEntity - // szName - //----------------------------------------------------------------------------- // FIXME: obsolete, remove CBaseEntity *CGlobalEntityList::FindEntityByTarget( CBaseEntity *pStartEntity, const char *szName ) { const CEntInfo *pInfo = pStartEntity ? GetEntInfoPtr( pStartEntity->GetRefEHandle() )->m_pNext : FirstEntInfo(); for ( ;pInfo; pInfo = pInfo->m_pNext ) { CBaseEntity *ent = (CBaseEntity *)pInfo->m_pEntity; if ( !ent ) { DevWarning( "NULL entity in global entity list!\n" ); continue; } if ( !ent->m_target ) continue; if ( FStrEq( STRING(ent->m_target), szName ) ) return ent; } return NULL; }
//----------------------------------------------------------------------------- // Purpose: Iterates the entities with a given name. // Input : pStartEntity - Last entity found, NULL to start a new iteration. // szName - Name to search for. // pActivator - Activator entity if this was called from an input // handler or Use handler. //----------------------------------------------------------------------------- CBaseEntity *CGlobalEntityList::FindEntityByName( CBaseEntity *pStartEntity, const char *szName, CBaseEntity *pSearchingEntity, CBaseEntity *pActivator, CBaseEntity *pCaller, IEntityFindFilter *pFilter ) { if ( !szName || szName[0] == 0 ) return NULL; if ( szName[0] == '!' ) { // // Avoid an infinite loop, only find one match per procedural search! // if (pStartEntity == NULL) return FindEntityProcedural( szName, pSearchingEntity, pActivator, pCaller ); return NULL; } const CEntInfo *pInfo = pStartEntity ? GetEntInfoPtr( pStartEntity->GetRefEHandle() )->m_pNext : FirstEntInfo(); for ( ;pInfo; pInfo = pInfo->m_pNext ) { CBaseEntity *ent = (CBaseEntity *)pInfo->m_pEntity; if ( !ent ) { DevWarning( "NULL entity in global entity list!\n" ); continue; } if ( !ent->m_iName ) continue; if ( ent->NameMatches( szName ) ) { if ( pFilter && !pFilter->ShouldFindEntity(ent) ) continue; return ent; } } return NULL; }
//----------------------------------------------------------------------------- // Purpose: Iterates the entities with a given classname. // Input : pStartEntity - Last entity found, NULL to start a new iteration. // szName - Classname to search for. //----------------------------------------------------------------------------- CBaseEntity *CGlobalEntityList::FindEntityByClassname( CBaseEntity *pStartEntity, const char *szName ) { const CEntInfo *pInfo = pStartEntity ? GetEntInfoPtr( pStartEntity->GetRefEHandle() )->m_pNext : FirstEntInfo(); for ( ;pInfo; pInfo = pInfo->m_pNext ) { CBaseEntity *pEntity = (CBaseEntity *)pInfo->m_pEntity; if ( !pEntity ) { DevWarning( "NULL entity in global entity list!\n" ); continue; } if ( pEntity->ClassMatches(szName) ) return pEntity; } return NULL; }