From 2c5c4d8a449146ce43edd0644a833a7a4bd1dbdf Mon Sep 17 00:00:00 2001 From: rtxa Date: Tue, 9 Jan 2024 21:41:17 -0300 Subject: [PATCH] CTF: Refactor OnPlayerKilled() Use native entity_range() to get the distance instead of declaring two vars. Use new function GetFlagEntity() to get the flag by team, increasing readability of the code. --- valve/addons/amxmodx/scripting/agmodx_ctf.sma | 26 +++++++++---------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/valve/addons/amxmodx/scripting/agmodx_ctf.sma b/valve/addons/amxmodx/scripting/agmodx_ctf.sma index 84b54ec..71f54ef 100644 --- a/valve/addons/amxmodx/scripting/agmodx_ctf.sma +++ b/valve/addons/amxmodx/scripting/agmodx_ctf.sma @@ -537,20 +537,11 @@ public OnPlayerKilled(victim, attacker) { return HAM_IGNORED; } - // Bonus attacker for defending his flag from the enemy + // Bonus attacker for defending his flag from the enemy + // Only if the enemy is close to the flag within a radius of 192 units if (IsPlayer(attacker) && victim != attacker && !AreTeamMates(attacker, victim)) { - new Float:flagOrigin[3]; - if (hl_get_user_team(attacker) == BLUE_TEAM) { - pev(gFlagBlue, pev_origin, flagOrigin); - } else { - pev(gFlagRed, pev_origin, flagOrigin); - } - - new Float:victimOrigin[3]; - pev(victim, pev_origin, victimOrigin); - - // Give points only if enemy is close to the flag within a radius of 192 units - if (get_distance_f(victimOrigin, flagOrigin) < 192) { + new flag = GetFlagEntity(hl_get_user_team(attacker)); + if (entity_range(victim, flag) < 192.0) { AddPoints(attacker, get_pcvar_num(gCvarDefendPoints)); } } @@ -558,6 +549,15 @@ public OnPlayerKilled(victim, attacker) { return HAM_IGNORED; } +GetFlagEntity(teamindex) { + if (teamindex == BLUE_TEAM) { + return gFlagBlue; + } else if (teamindex == RED_TEAM) { + return gFlagRed; + } + return 0; +} + GetFlagCarriedByPlayer(id) { return pev(id, pev_iuser4); }