Welcome...
This is preliminary code. It seems to be working, but there must be some reason why the coders at Valve removed their version... I haven't run into any problems yet but use at own risk!
In file dlls/player.cpp you will find the PlayerUse function. My modified version is below. Original code is in red, my modified code is in blue.
// // PlayerUse - handles USE keypress // #define PLAYER_SEARCH_RADIUS (float)64 void CBasePlayer::PlayerUse ( void ) { // Was use pressed or released? if ( ! ((pev->button | m_afButtonPressed | m_afButtonReleased) & IN_USE) ) return; // Hit Use on a train? if ( m_afButtonPressed & IN_USE ) { if ( m_pTank != NULL ) { // Stop controlling the tank // TODO: Send HUD Update m_pTank->Use( this, this, USE_OFF, 0 ); m_pTank = NULL; return; } else { if ( m_afPhysicsFlags & PFLAG_ONTRAIN ) { m_afPhysicsFlags &= ~PFLAG_ONTRAIN; m_iTrain = TRAIN_NEW|TRAIN_OFF; return; } else { // Start controlling the train! CBaseEntity *pTrain = CBaseEntity::Instance( pev->groundentity ); if ( pTrain && !(pev->button & IN_JUMP) && FBitSet(pev->flags, FL_ONGROUND) && (pTrain->ObjectCaps() & FCAP_DIRECTIONAL_USE) && pTrain->OnControls(pev) ) { m_afPhysicsFlags |= PFLAG_ONTRAIN; m_iTrain = TrainSpeed(pTrain->pev->speed, pTrain->pev->impulse); m_iTrain |= TRAIN_NEW; EMIT_SOUND( ENT(pev), CHAN_ITEM, "plats/train_use1.wav", 0.8, ATTN_NORM); return; } } } } CBaseEntity *pObject = NULL;// PRJ: Revised entity use code TraceResult tr; Vector vecSrc, vecDir; UTIL_MakeVectors( pev->v_angle + pev->punchangle ); vecSrc = pev->origin + pev->view_ofs; vecDir = gpGlobals->v_forward; UTIL_TraceLine( vecSrc, vecSrc + vecDir * PLAYER_SEARCH_RADIUS, dont_ignore_monsters, ENT(pev), &tr );//DecalGunshot(&tr,BULLET_PLAYER_9MM); //<- Very useful debugging tool! PRJif ( (tr.flFraction != 1.0) && !FNullEnt(tr.pHit) ) { pObject = CBaseEntity::Instance(tr.pHit); int caps = pObject->ObjectCaps(); if (caps & (FCAP_IMPULSE_USE | FCAP_CONTINUOUS_USE | FCAP_ONOFF_USE)) {if ( m_afButtonPressed & IN_USE ) EMIT_SOUND( ENT(pev), CHAN_ITEM, "common/wpn_select.wav", 0.4, ATTN_NORM); if ( ( (pev->button & IN_USE) && (caps & FCAP_CONTINUOUS_USE) ) || ( (m_afButtonPressed & IN_USE) && (caps & (FCAP_IMPULSE_USE|FCAP_ONOFF_USE)) ) ) { if ( caps & FCAP_CONTINUOUS_USE ) m_afPhysicsFlags |= PFLAG_USING; pObject->Use( this, this, USE_SET, 1 ); } // UNDONE: Send different USE codes for ON/OFF. Cache last ONOFF_USE object to send 'off' if you turn away else if ( (m_afButtonReleased & IN_USE) && (pObject->ObjectCaps() & FCAP_ONOFF_USE) ) // BUGBUG This is an "off" use { pObject->Use( this, this, USE_SET, 0 ); }} else {if ( m_afButtonPressed & IN_USE ) EMIT_SOUND( ENT(pev), CHAN_ITEM, "common/wpn_denyselect.wav", 0.4, ATTN_NORM);} }}
As it stands, the code first checks if we are on a TRAIN (which I didn't mess with!)
The original version of the code used to then search for every object within PLAYER_SEARCH_RADIUS, determined which was the closest entity in front of the player, then USEd that entity. I initially tweaked that code a little to test whether there was anything between the player and the entity before USEing it, to prevent activation of something through a wall. Unfortunately, I found at least one instance for which that code did not work properly; I do not entirely know why, but since I wanted to take a different approach anyway, I didn't spend much time trying to figure it out.
My new version of the code, as presented here, simply traces a line out from the player's POV to a maximum of PLAYER_SEARCH_RADIUS; if it hits a usable entity, it USEs it. This seems far too simple an approach for the Valve coders to have missed, and I'm wondering why they didn't do it this way themselves. There has to be a good reason, but I can't think of one, and the code appears to be running fine.
Some of you may also be interested in the DecalGunshot line which I have commented out. I found it very useful to spawn a bullet hit at the point where my trace was actually hitting something. This caught errors in both versions of my code; the unmissable bullet-hits make it crystal clear where my trace is actually hitting, as opposed to where I think it should be hitting!