Skip to content

Instantly share code, notes, and snippets.

@mrsteyk
Last active November 6, 2017 12:42
Show Gist options
  • Save mrsteyk/57e277c440a34bb54c349019bc5603fb to your computer and use it in GitHub Desktop.
Save mrsteyk/57e277c440a34bb54c349019bc5603fb to your computer and use it in GitHub Desktop.
pBackstab that doesnt depend on vfunc bullshit
// brought to you by MrSteyk and Volvo
bool IsBehindAndFacingTarget(IClientEntity *pVictim)
{
CTFPlayer *pSpy = ToTFPlayer(this->GetPlayerOwner());
if (pSpy == nullptr) return false;
Vector2D wsc_spy_to_victim = (pVictim->WorldSpaceCenter() - pSpy->WorldSpaceCenter()).AsVector2D();
wsc_spy_to_victim.NormalizeInPlace();
Vector temp1; pSpy->EyeVectors(&temp1);
Vector2D eye_spy = temp1.AsVector2D();
eye_spy.NormalizeInPlace();
Vector temp2; pVictim->EyeVectors(&temp2);
Vector2D eye_victim = temp2.AsVector2D();
eye_victim.NormalizeInPlace();
if (DotProduct2D(wsc_spy_to_victim, eye_victim) <= 0.0f) return false;
if (DotProduct2D(wsc_spy_to_victim, eye_spy) <= 0.5f) return false;
if (DotProduct2D(eye_spy, eye_victim) <= -0.3f) return false;
return true;
}
void FindHullIntersection( const Vector &vecSrc, trace_t &tr, const Vector &mins, const Vector &maxs, CBaseEntity *pEntity )
{
int i, j, k;
trace_t tmpTrace;
Vector vecEnd;
float distance = 1e6f;
Vector minmaxs[2] = {mins, maxs};
Vector vecHullEnd = tr.endpos;
vecHullEnd = vecSrc + ((vecHullEnd - vecSrc)*2);
UTIL_TraceLine( vecSrc, vecHullEnd, MASK_SOLID, pEntity, COLLISION_GROUP_NONE, &tmpTrace );
if ( tmpTrace.fraction < 1.0 )
{
tr = tmpTrace;
return;
}
for ( i = 0; i < 2; i++ )
{
for ( j = 0; j < 2; j++ )
{
for ( k = 0; k < 2; k++ )
{
vecEnd.x = vecHullEnd.x + minmaxs[i][0];
vecEnd.y = vecHullEnd.y + minmaxs[j][1];
vecEnd.z = vecHullEnd.z + minmaxs[k][2];
UTIL_TraceLine( vecSrc, vecEnd, MASK_SOLID, pEntity, COLLISION_GROUP_NONE, &tmpTrace );
if ( tmpTrace.fraction < 1.0 )
{
float thisDistance = (tmpTrace.endpos - vecSrc).Length();
if ( thisDistance < distance )
{
tr = tmpTrace;
distance = thisDistance;
}
}
}
}
}
}
bool DoSwingTrace( trace_t &trace, IClientEntity* pPlayer)
{
// Setup a volume for the melee weapon to be swung - approx size, so all melee behave the same.
static Vector vecSwingMins( -18, -18, -18 );
static Vector vecSwingMaxs( 18, 18, 18 );
// Get the current player.
//CTFPlayer *pPlayer = GetTFPlayerOwner(); // idk 'bout dis
//if ( !pPlayer )
// return false;
// Setup the swing range.
Vector vecForward;
AngleVectors( pPlayer->EyeAngles(), &vecForward ); // already exists
Vector vecSwingStart = pPlayer->Weapon_ShootPosition(); // wut // exists! phew!!!!
Vector vecSwingEnd = vecSwingStart + vecForward * 48;
// See if we hit anything.
UTIL_TraceLine( vecSwingStart, vecSwingEnd, MASK_SOLID, pPlayer, COLLISION_GROUP_NONE, &trace ); // thonk // exists
if ( trace.fraction >= 1.0 )
{
UTIL_TraceHull( vecSwingStart, vecSwingEnd, vecSwingMins, vecSwingMaxs, MASK_SOLID, pPlayer, COLLISION_GROUP_NONE, &trace ); // should
if ( trace.fraction < 1.0 )
{
// Calculate the point of intersection of the line (or hull) and the object we hit
// This is and approximation of the "best" intersection
CBaseEntity *pHit = trace.m_pEnt;
if ( !pHit || pHit->IsBSPModel() ) // thinking // exists
{
// Why duck hull min/max?
FindHullIntersection( vecSwingStart, trace, VEC_DUCK_HULL_MIN, VEC_DUCK_HULL_MAX, pPlayer ); // oh shit
}
// This is the point on the actual surface (the hull could have hit space)
vecSwingEnd = trace.endpos;
}
}
return ( trace.fraction < 1.0f );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment