Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code and logic improvements #15

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
208 changes: 96 additions & 112 deletions AccuracyFix/AccuracyFix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,157 +18,141 @@ void CAccuracyFix::ServerActivate()
{
auto SlotInfo = g_ReGameApi->GetWeaponSlot((WeaponIdType)WeaponID);

if (SlotInfo)
{
if ((SlotInfo->slot == PRIMARY_WEAPON_SLOT) || (SlotInfo->slot == PISTOL_SLOT))
{
if (SlotInfo->weaponName)
{
if (SlotInfo->weaponName[0u] != '\0')
{
char cvarName[64] = { 0 };
if (!SlotInfo)
continue;

if (!((SlotInfo->slot == PRIMARY_WEAPON_SLOT) || (SlotInfo->slot == PISTOL_SLOT)))
continue;

if (!SlotInfo->weaponName || SlotInfo->weaponName[0u] == '\0')
continue;

Q_snprintf(cvarName, sizeof(cvarName), "af_distance_%s", SlotInfo->weaponName);
char cvarName[64] = { 0 };

this->m_af_distance[WeaponID] = gAccuracyUtil.CvarRegister(cvarName, "8192.0");
Q_snprintf(cvarName, sizeof(cvarName), "af_distance_%s", SlotInfo->weaponName);

Q_snprintf(cvarName, sizeof(cvarName), "af_accuracy_%s", SlotInfo->weaponName);
this->m_af_distance[WeaponID] = gAccuracyUtil.CvarRegister(cvarName, "8192.0");

this->m_af_accuracy[WeaponID] = gAccuracyUtil.CvarRegister(cvarName, "9999.0");
Q_snprintf(cvarName, sizeof(cvarName), "af_accuracy_%s", SlotInfo->weaponName);

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

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

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

auto Path = gAccuracyUtil.GetPath();

if (Path)
{
if (Path[0u] != '\0')
{
gAccuracyUtil.ServerCommand("exec %s/accuracyfix.cfg", Path);
}
}
if (!Path || Path[0u] == '\0')
return;

gAccuracyUtil.ServerCommand("exec %s/accuracyfix.cfg", Path);
}

#ifdef ACCURACY_ENABLE_RECOIL_CONTROL
void CAccuracyFix::CmdEnd(const edict_t* pEdict)
{
auto Player = UTIL_PlayerByIndexSafe(ENTINDEX(pEdict));

if (Player)
{
if (Player->IsAlive())
{
this->m_Data[Player->entindex()].LastFired = Player->m_flLastFired;
}
}
if (!Player)
return;

if (!Player->IsAlive())
return;

this->m_Data[Player->entindex()].LastFired = Player->m_flLastFired;
}
#endif

void CAccuracyFix::TraceLine(const float* vStart, const float* vEnd, int fNoMonsters, edict_t* pentToSkip, TraceResult* ptr)
{
if (fNoMonsters == dont_ignore_monsters)
if (fNoMonsters != dont_ignore_monsters || FNullEnt(pentToSkip))
return;

auto EntityIndex = g_engfuncs.pfnIndexOfEdict(pentToSkip);

if (!(EntityIndex > 0 && EntityIndex <= gpGlobals->maxClients))
return;

auto Player = UTIL_PlayerByIndexSafe(EntityIndex);

if (!Player || !Player->IsAlive() || !Player->m_pActiveItem ||
!((Player->m_pActiveItem->iItemSlot() == PRIMARY_WEAPON_SLOT) || (Player->m_pActiveItem->iItemSlot() == PISTOL_SLOT)))
{
if (!FNullEnt(pentToSkip))
{
auto EntityIndex = g_engfuncs.pfnIndexOfEdict(pentToSkip);

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

if (Player)
{
if (Player->IsAlive())
{
if (Player->m_pActiveItem)
{
if ((Player->m_pActiveItem->iItemSlot() == PRIMARY_WEAPON_SLOT) || (Player->m_pActiveItem->iItemSlot() == PISTOL_SLOT))
{
return;
}

#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;
if (!((Player->edict()->v.button & IN_ATTACK) && (Player->m_flLastFired = this->m_Data[EntityIndex].LastFired)))
return;

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 DistanceLimit = this->m_af_distance[Player->m_pActiveItem->m_iId]->value;

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

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

if (!FNullEnt(trResult.pHit))
{
auto TargetIndex = ENTINDEX(trResult.pHit);

if (TargetIndex > 0 && TargetIndex <= gpGlobals->maxClients)
{
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;
}

g_engfuncs.pfnMakeVectors(pentToSkip->v.v_angle);

auto vEndRes = (Vector)vStart + gpGlobals->v_forward * fwdVelocity;

g_engfuncs.pfnTraceLine(vStart, vEndRes, fNoMonsters, pentToSkip, ptr);
}
}
}
}
}
}
}
}
}
auto DistanceLimit = this->m_af_distance[Player->m_pActiveItem->m_iId]->value;

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

if (!(DistanceLimit > 0.0f && Player->edict()->v.flags & FL_ONGROUND))
return;

auto trResult = gAccuracyUtil.GetUserAiming(pentToSkip, DistanceLimit);

if (!(trResult.flFraction != 1.0f && !FNullEnt(trResult.pHit)))
return;

auto TargetIndex = ENTINDEX(trResult.pHit);

if (!(TargetIndex > 0 && TargetIndex <= gpGlobals->maxClients))
return;

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

g_engfuncs.pfnMakeVectors(pentToSkip->v.v_angle);

auto vEndRes = (Vector)vStart + gpGlobals->v_forward * fwdVelocity;

g_engfuncs.pfnTraceLine(vStart, vEndRes, fNoMonsters, pentToSkip, ptr);
}

#ifdef ACCURACY_ENABLE_RECOIL_CONTROL
void CAccuracyFix::PostThink(CBasePlayer* Player)
{
if (Player->IsAlive())
{
auto EntityIndex = Player->entindex();

if (this->m_Data[EntityIndex].WeaponId != WEAPON_NONE)
{
auto Recoil = this->m_af_recoil[this->m_Data[EntityIndex].WeaponId]->value;
if (!Player->IsAlive())
return;
auto EntityIndex = Player->entindex();

if (Recoil > 0.0f)
{
if (this->m_af_recoil_all->value > 0.0f)
{
Recoil = this->m_af_recoil_all->value;
}
if (this->m_Data[EntityIndex].WeaponId == WEAPON_NONE)
return;

auto Recoil = this->m_af_recoil[this->m_Data[EntityIndex].WeaponId]->value;

auto PunchAngle = Player->edict()->v.punchangle;
if (Recoil > 0.0f)
{
if (this->m_af_recoil_all->value > 0.0f)
{
Recoil = this->m_af_recoil_all->value;
}

PunchAngle = PunchAngle * Recoil;
auto PunchAngle = Player->edict()->v.punchangle;

Player->edict()->v.punchangle = PunchAngle;
}
PunchAngle = PunchAngle * Recoil;

this->m_Data[EntityIndex].WeaponId = WEAPON_NONE;
}
Player->edict()->v.punchangle = PunchAngle;
}

this->m_Data[EntityIndex].WeaponId = WEAPON_NONE;
}
#endif
84 changes: 46 additions & 38 deletions AccuracyFix/AccuracyUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,56 +6,64 @@ cvar_t* CAccuracyUtil::CvarRegister(const char* Name, const char* Value)
{
cvar_t* pCvar = g_engfuncs.pfnCVarGetPointer(Name);

if (pCvar == nullptr)
{
this->m_CvarData[Name].name = Name;
if (pCvar != nullptr)
return pCvar;

this->m_CvarData[Name].name = Name;

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

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

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

pCvar = g_engfuncs.pfnCVarGetPointer(this->m_CvarData[Name].name);
pCvar = g_engfuncs.pfnCVarGetPointer(this->m_CvarData[Name].name);

if (pCvar != nullptr)
{
g_engfuncs.pfnCvar_DirectSet(pCvar, Value);
}
if (pCvar != nullptr)
{
g_engfuncs.pfnCvar_DirectSet(pCvar, Value);
}

return pCvar;
}

const char* CAccuracyUtil::GetPath()
{
if (!this->m_Path.empty())
return this->m_Path.c_str();

std::string GameDir = gpMetaUtilFuncs->pfnGetGameInfo(&Plugin_info, GINFO_GAMEDIR);

if (GameDir.empty())
return this->m_Path.c_str();
this->m_Path = gpMetaUtilFuncs->pfnGetPluginPath(&Plugin_info);

if (this->m_Path.empty())
{
std::string GameDir = gpMetaUtilFuncs->pfnGetGameInfo(&Plugin_info, GINFO_GAMEDIR);
return this->m_Path.c_str();

if (!GameDir.empty())
{
this->m_Path = gpMetaUtilFuncs->pfnGetPluginPath(&Plugin_info);
this->m_Path.erase(0, GameDir.length() + 1U);

if (!this->m_Path.empty())
{
this->m_Path.erase(0, GameDir.length() + 1U);
std::replace(this->m_Path.begin(), this->m_Path.end(), (char)(92), (char)(47));

std::replace(this->m_Path.begin(), this->m_Path.end(), (char)(92), (char)(47));
auto SlashPos = this->m_Path.find_last_of((char)(47));

this->m_Path.erase(this->m_Path.find_last_of((char)(47)), this->m_Path.length());
if (SlashPos != std::string::npos)
this->m_Path.erase(SlashPos, this->m_Path.length());

while (std::count(this->m_Path.begin(), this->m_Path.end(), (char)(47)) > 1)
{
this->m_Path.erase(this->m_Path.find_last_of((char)(47)), this->m_Path.length());
}
}
}
while (std::count(this->m_Path.begin(), this->m_Path.end(), (char)(47)) > 1)
{
SlashPos = this->m_Path.find_last_of((char)(47));

if (SlashPos == std::string::npos)
continue;

this->m_Path.erase(SlashPos, this->m_Path.length());
}

return this->m_Path.c_str();
}


void CAccuracyUtil::ServerCommand(const char* Format, ...)
{
char Command[255] = { 0 };
Expand All @@ -77,22 +85,22 @@ TraceResult CAccuracyUtil::GetUserAiming(edict_t* pEntity, float DistanceLimit)
{
TraceResult Result = { };

if (!FNullEnt(pEntity))
{
auto EntityIndex = g_engfuncs.pfnIndexOfEdict(pEntity);
if (FNullEnt(pEntity))
return Result;

auto EntityIndex = g_engfuncs.pfnIndexOfEdict(pEntity);

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

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

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

Vector v_dest = v_src + v_forward * DistanceLimit;
Vector v_dest = v_src + v_forward * DistanceLimit;

g_engfuncs.pfnTraceLine(v_src, v_dest, 0, pEntity, &Result);
}
g_engfuncs.pfnTraceLine(v_src, v_dest, 0, pEntity, &Result);
}

return Result;
Expand Down
2 changes: 1 addition & 1 deletion AccuracyFix/MetaMod.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ plugin_info_t Plugin_info =
{
META_INTERFACE_VERSION,
"Accuracy Fix",
"1.0.7",
"1.0.8",
__DATE__,
"SmileY",
"https://pugbr.net",
Expand Down
Loading