Added fast ent lookups for server dll

This commit is contained in:
Kamay Xutax 2024-09-05 15:31:51 +02:00
parent d8598a8d22
commit 8376d865cf
7 changed files with 115 additions and 75 deletions

View file

@ -518,18 +518,22 @@ CFastEntityLookUp::CFastEntityLookUp()
cl_entitylist->AddListenerEntity( this ); cl_entitylist->AddListenerEntity( this );
} }
void CFastEntityLookUp::OnEntityCreated( C_BaseEntity* pEntity ) void CFastEntityLookUp::OnEntityCreated( CBaseEntity* pEntity )
{ {
if ( pEntity->index > 0 && pEntity->index < MAX_EDICTS ) auto index = pEntity->entindex();
if ( index >= 0 && index < NUM_ENT_ENTRIES )
{ {
entities[pEntity->index] = pEntity; entities[index] = pEntity;
} }
} }
void CFastEntityLookUp::OnEntityDeleted( C_BaseEntity* pEntity ) void CFastEntityLookUp::OnEntityDeleted( CBaseEntity* pEntity )
{ {
if ( pEntity->index > 0 && pEntity->index < MAX_EDICTS ) auto index = pEntity->entindex();
if ( index >= 0 && index < NUM_ENT_ENTRIES )
{ {
entities[pEntity->index] = NULL; entities[index] = NULL;
} }
} }

View file

@ -312,8 +312,8 @@ class CFastEntityLookUp : public IClientEntityListener
virtual void OnEntityCreated( C_BaseEntity* pEntity ); virtual void OnEntityCreated( C_BaseEntity* pEntity );
virtual void OnEntityDeleted( C_BaseEntity* pEntity ); virtual void OnEntityDeleted( C_BaseEntity* pEntity );
// Let have a chance to let the CPU autovectorize these! // Let have a chance to the CPU in order to autovectorize these!
CBaseEntity* entities[MAX_EDICTS]; CBaseEntity* entities[NUM_ENT_ENTRIES];
}; };
extern CFastEntityLookUp* g_pFastEntityLookUp; extern CFastEntityLookUp* g_pFastEntityLookUp;

View file

@ -33,6 +33,9 @@ static CUtlVector<IServerNetworkable*> g_DeleteList;
CGlobalEntityList gEntList; CGlobalEntityList gEntList;
CBaseEntityList *g_pEntityList = &gEntList; CBaseEntityList *g_pEntityList = &gEntList;
static CFastEntityLookUp g_FastEntityLookUp;
CFastEntityLookUp* g_pFastEntityLookUp = &g_FastEntityLookUp;
class CAimTargetManager : public IEntityListener class CAimTargetManager : public IEntityListener
{ {
public: public:
@ -1634,3 +1637,32 @@ CON_COMMAND(report_simthinklist, "Lists all simulating/thinking entities")
list.ReportEntityList(); list.ReportEntityList();
} }
CFastEntityLookUp::CFastEntityLookUp()
{
for ( int i = 0; i < MAX_EDICTS; i++ )
{
entities[i] = NULL;
}
gEntList.AddListenerEntity( this );
}
void CFastEntityLookUp::OnEntityCreated( CBaseEntity* pEntity )
{
auto index = pEntity->entindex();
if ( index >= 0 && index < NUM_ENT_ENTRIES )
{
entities[index] = pEntity;
}
}
void CFastEntityLookUp::OnEntityDeleted( CBaseEntity* pEntity )
{
auto index = pEntity->entindex();
if ( index >= 0 && index < NUM_ENT_ENTRIES )
{
entities[index] = NULL;
}
}

View file

@ -9,6 +9,7 @@
#ifndef ENTITYLIST_H #ifndef ENTITYLIST_H
#define ENTITYLIST_H #define ENTITYLIST_H
#include "const.h"
#ifdef _WIN32 #ifdef _WIN32
#pragma once #pragma once
#endif #endif
@ -349,6 +350,20 @@ public:
virtual void OnEntityDeleted( CBaseEntity *pEntity ) {}; virtual void OnEntityDeleted( CBaseEntity *pEntity ) {};
}; };
class CFastEntityLookUp : public IEntityListener
{
public:
CFastEntityLookUp();
virtual void OnEntityCreated( CBaseEntity* pEntity );
virtual void OnEntityDeleted( CBaseEntity* pEntity );
// Let have a chance to the CPU in order to autovectorize these!
CBaseEntity* entities[NUM_ENT_ENTRIES];
};
extern CFastEntityLookUp* g_pFastEntityLookUp;
// singleton // singleton
extern INotify *g_pNotify; extern INotify *g_pNotify;

View file

@ -6,6 +6,7 @@
//=============================================================================// //=============================================================================//
#include "cbase.h" #include "cbase.h"
#include "entitylist.h"
#include "icvar.h" #include "icvar.h"
#include "player.h" #include "player.h"
#include "shareddefs.h" #include "shareddefs.h"
@ -155,9 +156,12 @@ void CLagCompensationManager::TrackEntities()
VPROF_BUDGET( "TrackEntities", "CLagCompensationManager" ); VPROF_BUDGET( "TrackEntities", "CLagCompensationManager" );
auto entities = g_pFastEntityLookUp->entities;
// Iterate all active entities
for ( int i = 0; i < MAX_EDICTS; i++ ) for ( int i = 0; i < MAX_EDICTS; i++ )
{ {
CBaseEntity* pEntity = UTIL_EntityByIndex( i ); CBaseEntity* pEntity = entities[i];
if ( !pEntity ) if ( !pEntity )
{ {
@ -248,9 +252,12 @@ void CLagCompensationManager::StartLagCompensation( CBasePlayer* player, CUserCm
// Iterate all active entities // Iterate all active entities
const CBitVec< MAX_EDICTS >* pEntityTransmitBits = engine->GetEntityTransmitBitsForClient( player->entindex() - 1 ); const CBitVec< MAX_EDICTS >* pEntityTransmitBits = engine->GetEntityTransmitBitsForClient( player->entindex() - 1 );
auto entities = g_pFastEntityLookUp->entities;
// Iterate all active entities
for ( int i = 0; i < MAX_EDICTS; i++ ) for ( int i = 0; i < MAX_EDICTS; i++ )
{ {
CBaseEntity* pEntity = UTIL_EntityByIndex( i ); CBaseEntity* pEntity = entities[i];
if ( !pEntity ) if ( !pEntity )
{ {
@ -287,10 +294,8 @@ inline void CLagCompensationManager::BacktrackEntity( CBaseEntity* pEntity, int
LagRecord* recordSim; LagRecord* recordSim;
LagRecord* recordAnim; LagRecord* recordAnim;
int pl_index = loopindex; float flTargetSimTime = cmd->simulationdata[loopindex].sim_time;
float flTargetAnimTime = cmd->simulationdata[loopindex].anim_time;
float flTargetSimTime = cmd->simulationdata[pl_index].sim_time;
float flTargetAnimTime = cmd->simulationdata[pl_index].anim_time;
// Somehow the client didn't care. // Somehow the client didn't care.
if ( flTargetSimTime == 0 ) if ( flTargetSimTime == 0 )
@ -305,7 +310,7 @@ inline void CLagCompensationManager::BacktrackEntity( CBaseEntity* pEntity, int
} }
// get track history of this entity // get track history of this entity
auto track = &m_EntityTrack[pl_index]; auto track = &m_EntityTrack[loopindex];
bool foundSim = false; bool foundSim = false;
bool foundAnim = false; bool foundAnim = false;
@ -375,8 +380,8 @@ inline void CLagCompensationManager::BacktrackEntity( CBaseEntity* pEntity, int
// See if this represents a change for the entity // See if this represents a change for the entity
int flags = 0; int flags = 0;
LagRecord* restore = &m_RestoreData[pl_index]; LagRecord* restore = &m_RestoreData[loopindex];
LagRecord* change = &m_ChangeData[pl_index]; LagRecord* change = &m_ChangeData[loopindex];
QAngle angdiff = pEntity->GetLocalAngles() - ang; QAngle angdiff = pEntity->GetLocalAngles() - ang;
Vector orgdiff = pEntity->GetLocalOrigin() - org; Vector orgdiff = pEntity->GetLocalOrigin() - org;
@ -438,7 +443,7 @@ inline void CLagCompensationManager::BacktrackEntity( CBaseEntity* pEntity, int
} }
} }
m_RestoreEntity.Set( pl_index ); // remember that we changed this entity m_RestoreEntity.Set( loopindex ); // remember that we changed this entity
m_bNeedToRestore = true; // we changed at least one entity m_bNeedToRestore = true; // we changed at least one entity
restore->m_fFlags = flags; // we need to restore these flags restore->m_fFlags = flags; // we need to restore these flags
change->m_fFlags = flags; // we have changed these flags change->m_fFlags = flags; // we have changed these flags
@ -486,8 +491,6 @@ inline void CLagCompensationManager::BacktrackEntity( CBaseEntity* pEntity, int
return; return;
} }
auto pAnimOverlay = dynamic_cast< CBaseAnimatingOverlay* >( pEntity );
if ( pAnim && foundAnim ) if ( pAnim && foundAnim )
{ {
// Sorry for the loss of the optimization for the case of people // Sorry for the loss of the optimization for the case of people
@ -527,6 +530,8 @@ inline void CLagCompensationManager::BacktrackEntity( CBaseEntity* pEntity, int
} }
} }
auto pAnimOverlay = dynamic_cast< CBaseAnimatingOverlay* >( pEntity );
if ( pAnimOverlay && foundAnim ) if ( pAnimOverlay && foundAnim )
{ {
//////////////////////// ////////////////////////
@ -569,6 +574,8 @@ void CLagCompensationManager::FinishLagCompensation( CBasePlayer* player )
return; // no entities was changed at all return; // no entities was changed at all
} }
auto entities = g_pFastEntityLookUp->entities;
// Iterate all active entities // Iterate all active entities
for ( int i = 0; i < MAX_EDICTS; i++ ) for ( int i = 0; i < MAX_EDICTS; i++ )
{ {
@ -578,7 +585,8 @@ void CLagCompensationManager::FinishLagCompensation( CBasePlayer* player )
continue; continue;
} }
CBaseEntity* pEntity = UTIL_EntityByIndex( i ); CBaseEntity* pEntity = entities[i];
if ( !pEntity ) if ( !pEntity )
{ {
continue; continue;
@ -603,7 +611,6 @@ void CLagCompensationManager::FinishLagCompensation( CBasePlayer* player )
} }
auto pAnim = dynamic_cast< CBaseAnimating* >( pEntity ); auto pAnim = dynamic_cast< CBaseAnimating* >( pEntity );
auto pAnimOverlay = dynamic_cast< CBaseAnimatingOverlay* >( pEntity );
if ( pAnim ) if ( pAnim )
{ {
@ -635,7 +642,9 @@ void CLagCompensationManager::FinishLagCompensation( CBasePlayer* player )
} }
} }
if ( restore->m_fFlags & LC_ANIM_OVERS_CHANGED && pAnimOverlay ) auto pAnimOverlay = dynamic_cast< CBaseAnimatingOverlay* >( pEntity );
if ( pAnimOverlay && restore->m_fFlags & LC_ANIM_OVERS_CHANGED )
{ {
int layerCount = pAnimOverlay->GetNumAnimOverlays(); int layerCount = pAnimOverlay->GetNumAnimOverlays();

View file

@ -559,18 +559,12 @@ void UTIL_RemoveImmediate( CBaseEntity *oldObj )
// Index is 1 based // Index is 1 based
CBasePlayer* UTIL_PlayerByIndex( int playerIndex ) CBasePlayer* UTIL_PlayerByIndex( int playerIndex )
{ {
CBasePlayer *pPlayer = NULL;
if ( playerIndex > 0 && playerIndex <= gpGlobals->maxClients ) if ( playerIndex > 0 && playerIndex <= gpGlobals->maxClients )
{ {
edict_t *pPlayerEdict = INDEXENT( playerIndex ); return ( CBasePlayer* )( g_pFastEntityLookUp->entities[playerIndex] );
if ( pPlayerEdict && !pPlayerEdict->IsFree() )
{
pPlayer = (CBasePlayer*)GetContainingEntity( pPlayerEdict );
}
} }
return pPlayer; return NULL;
} }
CBasePlayer* UTIL_PlayerByName( const char *name ) CBasePlayer* UTIL_PlayerByName( const char *name )
@ -698,18 +692,12 @@ bool UTIL_IsCommandIssuedByServerAdmin( void )
*/ */
CBaseEntity *UTIL_EntityByIndex( int entityIndex ) CBaseEntity *UTIL_EntityByIndex( int entityIndex )
{ {
CBaseEntity *entity = NULL;
if ( entityIndex > 0 ) if ( entityIndex > 0 )
{ {
edict_t *edict = INDEXENT( entityIndex ); return g_pFastEntityLookUp->entities[entityIndex];
if ( edict && !edict->IsFree() )
{
entity = GetContainingEntity( edict );
}
} }
return entity; return NULL;
} }

View file

@ -122,9 +122,6 @@ void FX_FireBullets(
float flSoundTime float flSoundTime
) )
{ {
// Fallback if failed to find the interpolated shoot position.
Vector vHookedOrigin = vOrigin;
if (weapon_accuracy_noinaccuracy.GetBool()) if (weapon_accuracy_noinaccuracy.GetBool())
{ {
fInaccuracy = 0.0f; fInaccuracy = 0.0f;
@ -227,7 +224,7 @@ void FX_FireBullets(
// Dispatch one message for all the bullet impacts and sounds. // Dispatch one message for all the bullet impacts and sounds.
TE_FireBullets( TE_FireBullets(
iPlayerIndex, iPlayerIndex,
vHookedOrigin, vOrigin,
vAngles, vAngles,
iWeaponID, iWeaponID,
iMode, iMode,
@ -282,7 +279,7 @@ void FX_FireBullets(
if ( bDoEffects) if ( bDoEffects)
{ {
FX_WeaponSound( iPlayerIndex, sound_type, vHookedOrigin, pWeaponInfo, flSoundTime ); FX_WeaponSound( iPlayerIndex, sound_type, vOrigin, pWeaponInfo, flSoundTime );
} }
@ -305,19 +302,8 @@ void FX_FireBullets(
float x0 = fRadius0 * cosf(fTheta0); float x0 = fRadius0 * cosf(fTheta0);
float y0 = fRadius0 * sinf(fTheta0); float y0 = fRadius0 * sinf(fTheta0);
const int kMaxBullets = 16;
float x1[kMaxBullets], y1[kMaxBullets];
Assert(pWeaponInfo->m_iBullets <= kMaxBullets); Assert(pWeaponInfo->m_iBullets <= kMaxBullets);
// the RNG can be desynchronized by FireBullet(), so pre-generate all spread offsets
for ( int iBullet=0; iBullet < pWeaponInfo->m_iBullets; iBullet++ )
{
float fTheta1 = RandomFloat(0.0f, 2.0f * M_PI);
float fRadius1 = RandomFloat(0.0f, fSpread);
x1[iBullet] = fRadius1 * cosf(fTheta1);
y1[iBullet] = fRadius1 * sinf(fTheta1);
}
#ifdef CLIENT_DLL #ifdef CLIENT_DLL
static ConVarRef cl_showfirebullethitboxes("cl_showfirebullethitboxes"); static ConVarRef cl_showfirebullethitboxes("cl_showfirebullethitboxes");
static ConVarRef cl_showimpacts( "cl_showimpacts" ); static ConVarRef cl_showimpacts( "cl_showimpacts" );
@ -374,9 +360,14 @@ void FX_FireBullets(
gpGlobals->client_taking_screenshot = true; gpGlobals->client_taking_screenshot = true;
} }
#endif #endif
pPlayer->FireBullet(
iBullet, float fTheta1 = RandomFloat( 0.0f, 2.0f * M_PI );
vHookedOrigin, float fRadius1 = RandomFloat( 0.0f, fSpread );
float x1 = fRadius1 * cosf( fTheta1 );
float y1 = fRadius1 * sinf( fTheta1 );
pPlayer->FireBullet( iBullet,
vOrigin,
vAngles, vAngles,
flRange, flRange,
iPenetration, iPenetration,
@ -385,7 +376,8 @@ void FX_FireBullets(
flRangeModifier, flRangeModifier,
pPlayer, pPlayer,
bDoEffects, bDoEffects,
x0 + x1[iBullet], y0 + y1[iBullet] ); x0 + x1,
y0 + y1 );
} }
EndGroupingSounds(); EndGroupingSounds();