Skip to content
View in the app

A better way to browse. Learn more.

Learn more

backstab-protection-in-dm/addons/amxmodx/scripting/olds_nobackkill.sma

olds_nobackkill.sma
137 lines · 5.6 KB · Pawn (AMX Mod X)
Raw
/*
 * Backstab Protection in DM (olds.ro workshop card LIB-0778)
 * Copyright (C) 2026 daemon (olds.ro)
 *
 * This program is free software: you can redistribute it and/or modify it under the terms
 * of the GNU General Public License as published by the Free Software Foundation, either
 * version 3 of the License, or (at your option) any later version.
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details: <https://www.gnu.org/licenses/>.
 *
 * For deathmatch: a bullet hit that would kill a player who faces away from the shooter is
 * stopped. In RG_CBasePlayer_TakeDamage (pre) the victim's view yaw (v_angle) is compared
 * with the direction from the victim to the attacker; the attacker is "behind" when that
 * direction lies inside the back arc, olds_nobackkill_angle degrees wide, centred straight
 * behind the victim. Only a lethal hit is touched (damage at least the victim's health):
 * olds_nobackkill_mode 1 cancels that hit, 2 lets it through but leaves the victim at 1 HP.
 * The attacker gets a HUD line and the stock buttons/button10.wav. Only bullet damage counts:
 * grenades, falls and the world are never changed; knife hits are allowed while
 * olds_nobackkill_knife is 1.
 */
#include <amxmodx>
#include <amxmisc>
#include <reapi>

#define PLUGIN  "Backstab Protection in DM"
#define VERSION "1.0.0"
#define AUTHOR  "daemon (olds.ro)"

#define WARN_SOUND "buttons/button10.wav"

new g_angle, g_mode, g_knife
new Float:g_lastWarn[MAX_PLAYERS + 1]
new g_saved

public plugin_precache()
{
    precache_sound(WARN_SOUND)
}

public plugin_init()
{
    register_plugin(PLUGIN, VERSION, AUTHOR)
    register_dictionary("olds_nobackkill.txt")
    bind_pcvar_num(create_cvar("olds_nobackkill_angle", "100", FCVAR_NONE, "Width in degrees of the back arc, centred straight behind the victim", true, 10.0, true, 300.0), g_angle)
    bind_pcvar_num(create_cvar("olds_nobackkill_mode", "1", FCVAR_NONE, "1 = cancel a lethal back hit, 2 = leave the victim at 1 HP", true, 1.0, true, 2.0), g_mode)
    bind_pcvar_num(create_cvar("olds_nobackkill_knife", "1", FCVAR_NONE, "1 = knife backstabs are allowed", true, 0.0, true, 1.0), g_knife)
    AutoExecConfig(true, "olds_nobackkill")
    register_concmd("amx_nobackkill_status", "cmd_status", ADMIN_KICK, "- who stands behind whom right now")
    RegisterHookChain(RG_CBasePlayer_TakeDamage, "on_damage_pre", false)
}

/* The attacker is inside the victim's back arc. */
bool:is_behind(victim, attacker)
{
    new Float:angles[3], Float:a[3], Float:v[3]
    get_entvar(victim, var_v_angle, angles)
    get_entvar(victim, var_origin, v)
    get_entvar(attacker, var_origin, a)
    new Float:dx = a[0] - v[0], Float:dy = a[1] - v[1]
    new Float:len = floatsqroot(dx * dx + dy * dy)
    if (len < 1.0)
        return false
    new Float:yaw = angles[1] * 3.14159265 / 180.0
    /* cosine of the angle between where the victim looks and where the attacker is */
    new Float:cosine = (floatcos(yaw) * dx + floatsin(yaw) * dy) / len
    /* behind when that angle is more than 180 - width / 2 degrees */
    new Float:limit = floatcos((180.0 - float(g_angle) / 2.0) * 3.14159265 / 180.0)
    return cosine < limit
}

bool:knife_hit(inflictor, attacker)
{
    return inflictor == attacker && get_user_weapon(attacker) == CSW_KNIFE
}

public on_damage_pre(victim, inflictor, attacker, Float:damage, bits)
{
    if (attacker < 1 || attacker > MaxClients || attacker == victim || !is_user_connected(attacker) || !is_user_alive(victim))
        return HC_CONTINUE
    /* Bullets only (the knife also deals bullet damage): grenades and the rest are unchanged. */
    if (!(bits & DMG_BULLET) || (bits & DMG_GRENADE))
        return HC_CONTINUE
    if (g_knife && knife_hit(inflictor, attacker))
        return HC_CONTINUE
    new Float:health = Float:get_entvar(victim, var_health)
    if (damage < health || !is_behind(victim, attacker))
        return HC_CONTINUE
    g_saved++
    warn(attacker, victim)
    if (g_mode == 2 && health > 1.0)
    {
        SetHookChainArg(4, ATYPE_FLOAT, health - 1.0)
        return HC_CONTINUE
    }
    SetHookChainReturn(ATYPE_INTEGER, 0)
    return HC_SUPERCEDE
}

warn(attacker, victim)
{
    new Float:now = get_gametime()
    if (now - g_lastWarn[attacker] < 1.0)
        return
    g_lastWarn[attacker] = now
    if (is_user_bot(attacker))
        return
    new name[MAX_NAME_LENGTH]
    get_user_name(victim, name, charsmax(name))
    set_hudmessage(255, 160, 40, -1.0, 0.70, 0, 0.0, 2.0, 0.1, 0.3, -1)
    show_hudmessage(attacker, "%L", attacker, "NBK_WARN", name)
    client_cmd(attacker, "spk ^"%s^"", WARN_SOUND)
}

public cmd_status(id, level, cid)
{
    if (!cmd_access(id, level, cid, 1))
        return PLUGIN_HANDLED
    console_print(id, "[NoBack] arc %d degrees, mode %d, knife allowed %d, lethal back hits stopped %d", g_angle, g_mode, g_knife, g_saved)
    new name[MAX_NAME_LENGTH], other[MAX_NAME_LENGTH], Float:angles[3]
    for (new v = 1; v <= MaxClients; v++)
    {
        if (!is_user_alive(v))
            continue
        get_user_name(v, name, charsmax(name))
        get_entvar(v, var_v_angle, angles)
        console_print(id, "[NoBack]   %s: health %.0f, yaw %.0f", name, Float:get_entvar(v, var_health), angles[1])
        for (new a = 1; a <= MaxClients; a++)
        {
            if (a == v || !is_user_alive(a) || !is_behind(v, a))
                continue
            get_user_name(a, other, charsmax(other))
            console_print(id, "[NoBack]     %s is behind %s", other, name)
        }
    }
    return PLUGIN_HANDLED
}

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.