Skip to content

Commit

Permalink
CTF: Refactor OnPlayerKilled()
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
rtxa committed Jan 10, 2024
1 parent 91e9b9f commit 2c5c4d8
Showing 1 changed file with 13 additions and 13 deletions.
26 changes: 13 additions & 13 deletions valve/addons/amxmodx/scripting/agmodx_ctf.sma
Original file line number Diff line number Diff line change
Expand Up @@ -537,27 +537,27 @@ 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));
}
}

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);
}
Expand Down

0 comments on commit 2c5c4d8

Please sign in to comment.