Skip to content

Commit

Permalink
Reduced distances to avoid lag spikes, and high CPU usage from tracel…
Browse files Browse the repository at this point in the history
…ile. Even in larger maps distance of 3200.0 should be enough...
  • Loading branch information
SmileYzn committed Aug 13, 2023
1 parent 5ebe85c commit 102bc1a
Show file tree
Hide file tree
Showing 8 changed files with 123 additions and 77 deletions.
67 changes: 46 additions & 21 deletions AccuracyFix/AccuracyFix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ CAccuracyFix gAccuracyFix;

void CAccuracyFix::ServerActivate()
{
#ifndef ACCURACY_DISABLE_RECOIL_CONTROL
this->m_Data.clear();

this->m_af_accuracy_all = this->CvarRegister("af_accuracy_all", "-1.0");

this->m_af_recoil_all = this->CvarRegister("af_recoil_all", "-1.0");
#endif

this->m_af_accuracy_all = this->CvarRegister("af_accuracy_all", "-1.0");

this->m_af_distance_all = this->CvarRegister("af_distance_all", "-1.0");

Expand All @@ -18,23 +20,25 @@ void CAccuracyFix::ServerActivate()

if (SlotInfo)
{
if (SlotInfo->slot == PRIMARY_WEAPON_SLOT || SlotInfo->slot == PISTOL_SLOT)
if ((SlotInfo->slot == PRIMARY_WEAPON_SLOT) || (SlotInfo->slot == PISTOL_SLOT))
{
if (SlotInfo->weaponName)
{
char cvarName[32] = { 0 };

Q_snprintf(cvarName, sizeof(cvarName), "af_accuracy_%s", SlotInfo->weaponName);

this->m_af_accuracy[WeaponID] = this->CvarRegister(cvarName, "8192.0");
this->m_af_accuracy[WeaponID] = this->CvarRegister(cvarName, "3200.0");

#ifndef ACCURACY_DISABLE_RECOIL_CONTROL
Q_snprintf(cvarName, sizeof(cvarName), "af_recoil_%s", SlotInfo->weaponName);

this->m_af_recoil[WeaponID] = this->CvarRegister(cvarName, "1.0");
#endif

Q_snprintf(cvarName, sizeof(cvarName), "af_distance_%s", SlotInfo->weaponName);

this->m_af_distance[WeaponID] = this->CvarRegister(cvarName, "8192.0");
this->m_af_distance[WeaponID] = this->CvarRegister(cvarName, "3200.0");
}
}
}
Expand All @@ -44,6 +48,7 @@ void CAccuracyFix::ServerActivate()
g_engfuncs.pfnServerExecute();
}

#ifndef ACCURACY_DISABLE_RECOIL_CONTROL
void CAccuracyFix::CmdEnd(const edict_t* pEdict)
{
auto Player = UTIL_PlayerByIndexSafe(ENTINDEX(pEdict));
Expand All @@ -56,6 +61,7 @@ void CAccuracyFix::CmdEnd(const edict_t* pEdict)
}
}
}
#endif

bool CAccuracyFix::TraceLine(const float* start, const float* end, int fNoMonsters, edict_t* pentToSkip, TraceResult* ptr)
{
Expand All @@ -71,6 +77,7 @@ bool CAccuracyFix::TraceLine(const float* start, const float* end, int fNoMonste
{
if (!((BIT(WEAPON_NONE) | BIT(WEAPON_HEGRENADE) | BIT(WEAPON_C4) | BIT(WEAPON_SMOKEGRENADE) | BIT(WEAPON_FLASHBANG) | BIT(WEAPON_KNIFE)) & BIT(Player->m_pActiveItem->m_iId)))
{
#ifndef ACCURACY_DISABLE_RECOIL_CONTROL
auto EntityIndex = Player->entindex();

if ((Player->edict()->v.button & IN_ATTACK) && (Player->m_flLastFired != this->m_Data[EntityIndex].LastFired))
Expand All @@ -79,8 +86,7 @@ bool CAccuracyFix::TraceLine(const float* start, const float* end, int fNoMonste

this->m_Data[EntityIndex].WeaponId = Player->m_pActiveItem->m_iId;
}

int TargetIndex = 0, HitBoxPlace = 0;
#endif

auto aimDistance = this->m_af_distance[Player->m_pActiveItem->m_iId]->value;

Expand All @@ -89,32 +95,49 @@ bool CAccuracyFix::TraceLine(const float* start, const float* end, int fNoMonste
aimDistance = this->m_af_accuracy_all->value;
}

int TargetIndex = 0, HitBoxPlace = 0;

if (this->GetUserAiming(pentToSkip, &TargetIndex, &HitBoxPlace, aimDistance) > 0.0f)
{
if (TargetIndex && HitBoxPlace)
if (TargetIndex)
{
auto fwdDistance = this->m_af_accuracy[Player->m_pActiveItem->m_iId]->value;

if (fwdDistance > 0)
if (HitBoxPlace)
{
if (this->m_af_accuracy_all->value > 0)
if (!Player->IsBot())
{
fwdDistance = this->m_af_accuracy_all->value;
auto Target = UTIL_PlayerByIndexSafe(TargetIndex);

if (Target)
{
auto Distance = (Player->edict()->v.origin - Target->edict()->v.origin).Length();

LOG_CONSOLE(PLID, "[%s] Distance: %f", __func__, Distance);
}
}

g_engfuncs.pfnMakeVectors(pentToSkip->v.v_angle);
auto fwdDistance = this->m_af_accuracy[Player->m_pActiveItem->m_iId]->value;

Vector vEnd = Vector(0, 0, 0);
if (fwdDistance > 0.0f)
{
if (this->m_af_accuracy_all->value > 0.0f)
{
fwdDistance = this->m_af_accuracy_all->value;
}

vEnd = gpGlobals->v_forward * fwdDistance;
g_engfuncs.pfnMakeVectors(pentToSkip->v.v_angle);

vEnd[0] = start[0] + vEnd[0];
vEnd[1] = start[1] + vEnd[1];
vEnd[2] = start[2] + vEnd[2];
Vector vEnd = Vector(0.0f, 0.0f, 0.0f);

g_engfuncs.pfnTraceLine(start, vEnd, fNoMonsters, pentToSkip, ptr);
vEnd = gpGlobals->v_forward * fwdDistance;

return true;
vEnd[0] = start[0] + vEnd[0];
vEnd[1] = start[1] + vEnd[1];
vEnd[2] = start[2] + vEnd[2];

g_engfuncs.pfnTraceLine(start, vEnd, fNoMonsters, pentToSkip, ptr);

return true;
}
}
}
}
Expand All @@ -127,6 +150,7 @@ bool CAccuracyFix::TraceLine(const float* start, const float* end, int fNoMonste
return false;
}

#ifndef ACCURACY_DISABLE_RECOIL_CONTROL
void CAccuracyFix::PostThink(CBasePlayer* Player)
{
if (Player->IsAlive())
Expand Down Expand Up @@ -155,6 +179,7 @@ void CAccuracyFix::PostThink(CBasePlayer* Player)
}
}
}
#endif

float CAccuracyFix::GetUserAiming(edict_t* edict, int* cpId, int* cpBody, float distance)
{
Expand Down
6 changes: 6 additions & 0 deletions AccuracyFix/AccuracyFix.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
#pragma once

#ifndef ACCURACY_DISABLE_RECOIL_CONTROL
typedef struct S_PLAYER_DATA
{
float LastFired;
int WeaponId;
} P_PLAYER_DATA, *LP_PLAYER_DATA;
#endif

class CAccuracyFix
{
Expand All @@ -18,14 +20,18 @@ class CAccuracyFix
cvar_t* CvarRegister(const char* Name, const char* Value);

private:
#ifndef ACCURACY_DISABLE_RECOIL_CONTROL
std::map<int, P_PLAYER_DATA> m_Data;
#endif
std::map<std::string, cvar_t> m_Cvar;

cvar_t* m_af_accuracy_all;
std::array<cvar_t*, MAX_WEAPONS + 1> m_af_accuracy;

#ifndef ACCURACY_DISABLE_RECOIL_CONTROL
cvar_t* m_af_recoil_all;
std::array<cvar_t*, MAX_WEAPONS + 1> m_af_recoil;
#endif;

cvar_t* m_af_distance_all;
std::array<cvar_t*, MAX_WEAPONS + 1> m_af_distance;
Expand Down
4 changes: 4 additions & 0 deletions AccuracyFix/MetaDLL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ C_DLLEXPORT int GetEntityAPI2_Post(DLL_FUNCTIONS* pFunctionTable, int* interface
// Register Functions Here //
gDLL_FunctionTable_Post.pfnServerActivate = DLL_POST_ServerActivate;

#ifndef ACCURACY_DISABLE_RECOIL_CONTROL
gDLL_FunctionTable_Post.pfnCmdEnd = DLL_POST_CmdEnd;
#endif

memcpy(pFunctionTable, &gDLL_FunctionTable_Post, sizeof(DLL_FUNCTIONS));

Expand All @@ -41,8 +43,10 @@ void DLL_POST_ServerActivate(edict_t* pEdictList, int edictCount, int clientMax)
RETURN_META(MRES_IGNORED);
}

#ifndef ACCURACY_DISABLE_RECOIL_CONTROL
void DLL_POST_CmdEnd(const edict_t* pEdict)
{
gAccuracyFix.CmdEnd(pEdict);
}
#endif
#pragma endregion
2 changes: 2 additions & 0 deletions AccuracyFix/MetaDLL.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,7 @@

#pragma region DLL_POST
void DLL_POST_ServerActivate(edict_t* pEdictList, int edictCount, int clientMax);
#ifndef ACCURACY_DISABLE_RECOIL_CONTROL
void DLL_POST_CmdEnd(const edict_t* pEdict);
#endif
#pragma endregion
8 changes: 6 additions & 2 deletions AccuracyFix/ReGameDLL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,18 @@ bool ReGameDLL_Init()
return false;
}

#ifndef ACCURACY_DISABLE_RECOIL_CONTROL
g_ReGameHookchains->CBasePlayer_PostThink()->registerHook(ReGameDLL_CBasePlayer_PostThink);
#endif

return true;
}

bool ReGameDLL_Stop()
{
#ifndef ACCURACY_DISABLE_RECOIL_CONTROL
g_ReGameHookchains->CBasePlayer_PostThink()->unregisterHook(ReGameDLL_CBasePlayer_PostThink);

#endif
return true;
}

Expand All @@ -104,10 +107,11 @@ CGameRules *ReGameDLL_InstallGameRules(IReGameHook_InstallGameRules *chain)

return gamerules;
}

#ifndef ACCURACY_DISABLE_RECOIL_CONTROL
void ReGameDLL_CBasePlayer_PostThink(IReGameHook_CBasePlayer_PostThink* chain, CBasePlayer* pthis)
{
chain->callNext(pthis);

gAccuracyFix.PostThink(pthis);
}
#endif
4 changes: 3 additions & 1 deletion AccuracyFix/ReGameDLL.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@ extern bool ReGameDLL_Init();
extern bool ReGameDLL_Stop();

CGameRules *ReGameDLL_InstallGameRules(IReGameHook_InstallGameRules* chain);
void ReGameDLL_CBasePlayer_PostThink(IReGameHook_CBasePlayer_PostThink* chain, CBasePlayer* pthis);
#ifndef ACCURACY_DISABLE_RECOIL_CONTROL
void ReGameDLL_CBasePlayer_PostThink(IReGameHook_CBasePlayer_PostThink* chain, CBasePlayer* pthis);
#endif
3 changes: 3 additions & 0 deletions AccuracyFix/precompiled.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
#define _CRT_SECURE_NO_WARNINGS
#endif

// Disable Recoil Fix
#define ACCURACY_DISABLE_RECOIL_CONTROL

// If is not MSVC build
#ifndef _WIN32
#define _GLIBCXX_USE_CXX11_ABI 0
Expand Down
Loading

0 comments on commit 102bc1a

Please sign in to comment.