From 91e9b9f75e6da616cb90aaa5a0055221f8eede1b Mon Sep 17 00:00:00 2001 From: rtxa Date: Tue, 9 Jan 2024 03:01:03 -0300 Subject: [PATCH] CTF: Improve OnPlayerKilled readability Also fixed a bug by checking if attacker is a player when checking if they're teammates --- valve/addons/amxmodx/scripting/agmodx_ctf.sma | 49 ++++++++++--------- 1 file changed, 26 insertions(+), 23 deletions(-) diff --git a/valve/addons/amxmodx/scripting/agmodx_ctf.sma b/valve/addons/amxmodx/scripting/agmodx_ctf.sma index a4d660d..84b54ec 100644 --- a/valve/addons/amxmodx/scripting/agmodx_ctf.sma +++ b/valve/addons/amxmodx/scripting/agmodx_ctf.sma @@ -459,6 +459,10 @@ stock Speak(id, const speak[]) { client_cmd(id, "speak ^"%s^"", speak); } +stock AreTeamMates(firstPlayer, secondPlayer) { + return hl_get_user_team(firstPlayer) == hl_get_user_team(secondPlayer); +} + public DropFlagSpec(id) { if (hl_get_user_spectator(id)) DropFlag(id); @@ -511,30 +515,9 @@ public OnCapturePointTouch(touched, toucher) { } public OnPlayerKilled(victim, attacker) { - new entFlag = GetFlagCarriedByPlayer(victim); - new areTeamMates = hl_get_user_team(attacker) == hl_get_user_team(victim); - - // Bonus attacker for defending his flag from the enemy - if (IsPlayer(attacker) && victim != attacker && !areTeamMates && !Player_IsCarryingFlag(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) { - AddPoints(attacker, get_pcvar_num(gCvarDefendPoints)); - } - } - if (Player_IsCarryingFlag(victim)) { // Give points to attacker for killing flag stealer - if (IsPlayer(attacker) && !areTeamMates) { + if (IsPlayer(attacker) && !AreTeamMates(attacker, victim)) { AddPoints(attacker, get_pcvar_num(gCvarCarrierKillPoints)); } @@ -545,11 +528,31 @@ public OnPlayerKilled(victim, attacker) { // will return the flag to base if (equal(classname, "trigger_hurt")) { SetFlagCarriedByPlayer(victim, 0); - Flag_Reset(entFlag); + Flag_Reset(GetFlagCarriedByPlayer(victim)); return HAM_IGNORED; } DropFlag(victim); + + return HAM_IGNORED; + } + + // Bonus attacker for defending his flag from the enemy + 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) { + AddPoints(attacker, get_pcvar_num(gCvarDefendPoints)); + } } return HAM_IGNORED;