Skip to content

Commit

Permalink
CTF: Improve OnPlayerKilled readability
Browse files Browse the repository at this point in the history
Also fixed a bug by checking if attacker is a player when checking if they're teammates
  • Loading branch information
rtxa committed Jan 9, 2024
1 parent dc9ada0 commit 91e9b9f
Showing 1 changed file with 26 additions and 23 deletions.
49 changes: 26 additions & 23 deletions valve/addons/amxmodx/scripting/agmodx_ctf.sma
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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));
}

Expand All @@ -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;
Expand Down

0 comments on commit 91e9b9f

Please sign in to comment.