Skip to content

Commit

Permalink
Fixed accuracy fix traceline and other functions and values
Browse files Browse the repository at this point in the history
  • Loading branch information
SmileYzn committed Feb 9, 2024
1 parent a0d0f2d commit d29273e
Show file tree
Hide file tree
Showing 13 changed files with 282 additions and 214 deletions.
195 changes: 70 additions & 125 deletions AccuracyFix/AccuracyFix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ CAccuracyFix gAccuracyFix;

void CAccuracyFix::ServerActivate()
{
#ifndef ACCURACY_DISABLE_RECOIL_CONTROL
this->m_Data.clear();
this->m_af_accuracy_all = gAccuracyUtil.CvarRegister("af_accuracy_all", "-1.0");

this->m_af_recoil_all = this->CvarRegister("af_recoil_all", "-1.0");
#endif
this->m_af_distance_all = gAccuracyUtil.CvarRegister("af_distance_all", "-1.0");

this->m_af_accuracy_all = this->CvarRegister("af_accuracy_all", "-1.0");
#ifdef ACCURACY_ENABLE_RECOIL_CONTROL
this->m_Data.clear();

this->m_af_distance_all = this->CvarRegister("af_distance_all", "-1.0");
this->m_af_recoil_all = gAccuracyUtil.CvarRegister("af_recoil_all", "-1.0");
#endif

for (int WeaponID = WEAPON_P228; WeaponID <= WEAPON_P90; WeaponID++)
{
Expand All @@ -24,31 +24,37 @@ void CAccuracyFix::ServerActivate()
{
if (SlotInfo->weaponName)
{
char cvarName[32] = { 0 };
if (SlotInfo->weaponName[0u] != '\0')
{
std::string cvarName = "af_distance_";

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

this->m_af_accuracy[WeaponID] = this->CvarRegister(cvarName, "9999.0");
this->m_af_distance[WeaponID] = gAccuracyUtil.CvarRegister(cvarName.c_str(), "8192.0");

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

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

this->m_af_accuracy[WeaponID] = gAccuracyUtil.CvarRegister(cvarName.c_str(), "9999.0");

#ifdef ACCURACY_ENABLE_RECOIL_CONTROL
cvarName = "af_recoil_";

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

this->m_af_distance[WeaponID] = this->CvarRegister(cvarName, "2048.0");
this->m_af_recoil[WeaponID] = gAccuracyUtil.CvarRegister(cvarName.c_str(), "1.0");
#endif
}
}
}
}
}

g_engfuncs.pfnServerCommand("exec addons/accuracyfix/accuracyfix.cfg\n");
g_engfuncs.pfnServerExecute();
gAccuracyUtil.ServerCommand("exec %s/accuracyfix.cfg", gAccuracyUtil.GetPath());
}

#ifndef ACCURACY_DISABLE_RECOIL_CONTROL
#ifdef ACCURACY_ENABLE_RECOIL_CONTROL
void CAccuracyFix::CmdEnd(const edict_t* pEdict)
{
auto Player = UTIL_PlayerByIndexSafe(ENTINDEX(pEdict));
Expand All @@ -65,61 +71,67 @@ void CAccuracyFix::CmdEnd(const edict_t* pEdict)

void CAccuracyFix::TraceLine(const float* vStart, const float* vEnd, int fNoMonsters, edict_t* pentToSkip, TraceResult* ptr)
{
auto EntityIndex = ENTINDEX(pentToSkip);

if (EntityIndex > 0 && EntityIndex <= gpGlobals->maxClients)
if (fNoMonsters == dont_ignore_monsters)
{
auto Player = UTIL_PlayerByIndexSafe(EntityIndex);

if (Player)
if (!FNullEnt(pentToSkip))
{
if (Player->IsAlive())
auto EntityIndex = g_engfuncs.pfnIndexOfEdict(pentToSkip);

if (EntityIndex > 0 && EntityIndex <= gpGlobals->maxClients)
{
if (fNoMonsters == dont_ignore_monsters)
auto Player = UTIL_PlayerByIndexSafe(EntityIndex);

if (Player)
{
if (Player->m_pActiveItem)
if (Player->IsAlive())
{
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)))
if (Player->m_pActiveItem)
{
#ifndef ACCURACY_DISABLE_RECOIL_CONTROL
if ((Player->edict()->v.button & IN_ATTACK) && (Player->m_flLastFired != this->m_Data[EntityIndex].LastFired))
if ((Player->m_pActiveItem->iItemSlot() == PRIMARY_WEAPON_SLOT) || (Player->m_pActiveItem->iItemSlot() == PISTOL_SLOT))
{
this->m_Data[EntityIndex].LastFired = Player->m_flLastFired;
#ifdef ACCURACY_ENABLE_RECOIL_CONTROL
if ((Player->edict()->v.button & IN_ATTACK) && (Player->m_flLastFired != this->m_Data[EntityIndex].LastFired))
{
this->m_Data[EntityIndex].LastFired = Player->m_flLastFired;

this->m_Data[EntityIndex].WeaponId = Player->m_pActiveItem->m_iId;
}
this->m_Data[EntityIndex].WeaponId = Player->m_pActiveItem->m_iId;
}
#endif
auto aimDistance = this->m_af_distance[Player->m_pActiveItem->m_iId]->value;
auto DistanceLimit = this->m_af_distance[Player->m_pActiveItem->m_iId]->value;

if (this->m_af_distance_all->value > 0)
{
aimDistance = this->m_af_distance_all->value;
}
if (this->m_af_distance_all->value > 0)
{
DistanceLimit = this->m_af_distance_all->value;
}

int TargetIndex = 0, HitBoxPlace = 0;

if (this->GetUserAiming(pentToSkip, &TargetIndex, &HitBoxPlace, aimDistance) > 0.0f)
{
if (TargetIndex > 0 && TargetIndex <= gpGlobals->maxClients)
if (DistanceLimit > 0.0f)
{
auto fwdVelocity = this->m_af_accuracy[Player->m_pActiveItem->m_iId]->value;

if (this->m_af_accuracy_all->value > 0.0f)
{
fwdVelocity = this->m_af_accuracy_all->value;
}

if (fwdVelocity > 0.0f)
auto trResult = gAccuracyUtil.GetUserAiming(pentToSkip, DistanceLimit);

if (!FNullEnt(trResult.pHit))
{
g_engfuncs.pfnMakeVectors(pentToSkip->v.v_angle);
auto TargetIndex = ENTINDEX(trResult.pHit);

if (TargetIndex > 0 && TargetIndex <= gpGlobals->maxClients)
{
auto fwdVelocity = this->m_af_accuracy[Player->m_pActiveItem->m_iId]->value;

Vector Result = Vector(0.0f, 0.0f, 0.0f);
if (this->m_af_accuracy_all->value > 0.0f)
{
fwdVelocity = this->m_af_accuracy_all->value;
}

Result[0] = (vStart[0] + (gpGlobals->v_forward[0] * fwdVelocity));
Result[1] = (vStart[1] + (gpGlobals->v_forward[1] * fwdVelocity));
Result[2] = (vStart[2] + (gpGlobals->v_forward[2] * fwdVelocity));
g_engfuncs.pfnMakeVectors(pentToSkip->v.v_angle);

g_engfuncs.pfnTraceLine(vStart, Result, fNoMonsters, pentToSkip, ptr);
auto vEndRes = Vector
(
(vStart[0] + (gpGlobals->v_forward[0] * fwdVelocity)),
(vStart[1] + (gpGlobals->v_forward[1] * fwdVelocity)),
(vStart[2] + (gpGlobals->v_forward[2] * fwdVelocity))
);

g_engfuncs.pfnTraceLine(vStart, vEndRes, fNoMonsters, pentToSkip, ptr);
}
}
}
}
Expand All @@ -131,7 +143,7 @@ void CAccuracyFix::TraceLine(const float* vStart, const float* vEnd, int fNoMons
}
}

#ifndef ACCURACY_DISABLE_RECOIL_CONTROL
#ifdef ACCURACY_ENABLE_RECOIL_CONTROL
void CAccuracyFix::PostThink(CBasePlayer* Player)
{
if (Player->IsAlive())
Expand Down Expand Up @@ -161,70 +173,3 @@ void CAccuracyFix::PostThink(CBasePlayer* Player)
}
}
#endif

float CAccuracyFix::GetUserAiming(edict_t* pEdict, int* cpId, int* cpBody, float distance)
{
float Result = 0.0f;

if (!FNullEnt(pEdict))
{
auto Entityindex = ENTINDEX(pEdict);

if (Entityindex > 0 && Entityindex <= gpGlobals->maxClients)
{
Vector v_forward;

Vector v_src = pEdict->v.origin + pEdict->v.view_ofs;

g_engfuncs.pfnAngleVectors(pEdict->v.v_angle, v_forward, NULL, NULL);

TraceResult trEnd;

Vector v_dest = v_src + v_forward * distance;

g_engfuncs.pfnTraceLine(v_src, v_dest, 0, pEdict, &trEnd);

*cpId = FNullEnt(trEnd.pHit) ? 0 : ENTINDEX(trEnd.pHit);

*cpBody = trEnd.iHitgroup;

if (trEnd.flFraction < 1.0f)
{
Result = (trEnd.vecEndPos - v_src).Length();
}

return Result;
}
}

*cpId = 0;

*cpBody = 0;

return Result;
}

cvar_t* CAccuracyFix::CvarRegister(const char* Name, const char* Value)
{
cvar_t* Pointer = g_engfuncs.pfnCVarGetPointer(Name);

if (!Pointer)
{
this->m_Cvar[Name].name = Name;

this->m_Cvar[Name].string = (char*)(Value);

this->m_Cvar[Name].flags = (FCVAR_SERVER | FCVAR_SPONLY | FCVAR_UNLOGGED);

g_engfuncs.pfnCVarRegister(&this->m_Cvar[Name]);

Pointer = g_engfuncs.pfnCVarGetPointer(this->m_Cvar[Name].name);

if (Pointer)
{
g_engfuncs.pfnCvar_DirectSet(Pointer, Value);
}
}

return Pointer;
}
17 changes: 6 additions & 11 deletions AccuracyFix/AccuracyFix.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

#ifndef ACCURACY_DISABLE_RECOIL_CONTROL
#ifdef ACCURACY_ENABLE_RECOIL_CONTROL
typedef struct S_PLAYER_DATA
{
float LastFired;
Expand All @@ -16,25 +16,20 @@ class CAccuracyFix
void CmdEnd(const edict_t* pEdict);
void TraceLine(const float* vStart, const float* vEnd, int fNoMonsters, edict_t* pentToSkip, TraceResult* ptr);
void PostThink(CBasePlayer* Player);
float GetUserAiming(edict_t* edict, int* cpId, int* cpBody, float distance);
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_distance_all;
std::array<cvar_t*, MAX_WEAPONS + 1> m_af_distance;

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

#ifndef ACCURACY_DISABLE_RECOIL_CONTROL
#ifdef ACCURACY_ENABLE_RECOIL_CONTROL
std::map<int, P_PLAYER_DATA> m_Data;

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;
};

extern CAccuracyFix gAccuracyFix;
2 changes: 2 additions & 0 deletions AccuracyFix/AccuracyFix.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
</ItemGroup>
<ItemGroup>
<ClCompile Include="AccuracyFix.cpp" />
<ClCompile Include="AccuracyUtil.cpp" />
<ClCompile Include="MetaDLL.cpp" />
<ClCompile Include="MetaEngine.cpp" />
<ClCompile Include="MetaMod.cpp" />
Expand All @@ -27,6 +28,7 @@
</ItemGroup>
<ItemGroup>
<ClInclude Include="AccuracyFix.h" />
<ClInclude Include="AccuracyUtil.h" />
<ClInclude Include="MetaDLL.h" />
<ClInclude Include="MetaEngine.h" />
<ClInclude Include="MetaMod.h" />
Expand Down
9 changes: 9 additions & 0 deletions AccuracyFix/AccuracyFix.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
<Filter Include="ReGameDLL">
<UniqueIdentifier>{3a2f7dd5-133f-41d5-b627-5e16bbe5f958}</UniqueIdentifier>
</Filter>
<Filter Include="AccuracyFix\AccuracyUtil">
<UniqueIdentifier>{119d37cb-5db2-4d96-ae2b-a55ee606254c}</UniqueIdentifier>
</Filter>
</ItemGroup>
<ItemGroup>
<ClCompile Include="precompiled.cpp">
Expand All @@ -48,6 +51,9 @@
<ClCompile Include="ReGameDLL.cpp">
<Filter>ReGameDLL</Filter>
</ClCompile>
<ClCompile Include="AccuracyUtil.cpp">
<Filter>AccuracyFix\AccuracyUtil</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="precompiled.h">
Expand All @@ -71,6 +77,9 @@
<ClInclude Include="ReGameDLL.h">
<Filter>ReGameDLL</Filter>
</ClInclude>
<ClInclude Include="AccuracyUtil.h">
<Filter>AccuracyFix\AccuracyUtil</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="AccuracyFix.def">
Expand Down
Loading

0 comments on commit d29273e

Please sign in to comment.