Skip to content
View in the app

A better way to browse. Learn more.

Learn more

demotion-shield/addons/amxmodx/scripting/olds_demshield.sma

olds_demshield.sma
235 lines · 7.8 KB · Pawn (AMX Mod X)
Raw
/*
 * Demotion Shield (olds.ro workshop idea IDEA-B50EE9DF8AE8C804) - Arms Race add-on
 *
 * After a player is knifed down a level he cannot be demoted again for olds_shield_time
 * seconds, and (olds_shield_samekiller 1) the player who demoted him last cannot demote
 * him again at all until someone else has: one knife player can no longer farm a single
 * victim. The kill itself still counts; only the level loss is undone.
 *
 *   say /shield   the seconds left on your own shield
 *
 * Levels come from the Arms Race core (library "armsrace"): ar_get_level(id) and
 * ar_set_level(id, level) (required). If the core fires the pre-forward
 *     ar_knife_demote_pre(victim, killer)   (return PLUGIN_HANDLED to keep the level)
 * the plugin answers it; otherwise it watches RG_CBasePlayer_Killed: a kill made with the
 * knife in hand, by a direct hit, after which the victim's level dropped, is a demotion,
 * and a blocked one is undone with ar_set_level 0.1 s after the kill (after the core).
 *
 * Cvars: olds_shield_time 20, olds_shield_samekiller 1, olds_shield_notify 1.
 *
 * Copyright (C) 2026 daemon (olds.ro)
 * SPDX-License-Identifier: GPL-3.0-or-later
 */
#include <amxmodx>
#include <reapi>

#define PLUGIN  "Demotion Shield"
#define VERSION "1.0.0"
#define AUTHOR  "olds.ro"

#define TASK_CHECK 50500      // + victim id
const Float:CHECK_DELAY = 0.1;

native ar_get_level(id);
native ar_set_level(id, level);

new bool:g_hasLevels = true;
new Float:g_time, g_sameKiller, g_notify;

new Float:g_shieldUntil[MAX_PLAYERS + 1];
new g_lastDemoter[MAX_PLAYERS + 1];
new g_levelBefore[MAX_PLAYERS + 1];       // victim level when the kill began
new g_pendingKiller[MAX_PLAYERS + 1];     // knife kill waiting to be checked
new bool:g_answeredPre[MAX_PLAYERS + 1];  // the core asked through the pre-forward
new Float:g_nextUse[MAX_PLAYERS + 1];

public plugin_natives()
{
    set_native_filter("native_filter");
}

public native_filter(const name[], index, trap)
{
    if (!trap && (equal(name, "ar_get_level") || equal(name, "ar_set_level")))
    {
        g_hasLevels = false;
        return PLUGIN_HANDLED;
    }
    return PLUGIN_CONTINUE;
}

public plugin_init()
{
    register_plugin(PLUGIN, VERSION, AUTHOR);
    register_dictionary("olds_demshield.txt");

    bind_pcvar_float(create_cvar("olds_shield_time", "20", FCVAR_NONE,
        "Seconds a demoted player cannot be demoted again", true, 0.0, true, 600.0), g_time);
    bind_pcvar_num(create_cvar("olds_shield_samekiller", "1", FCVAR_NONE,
        "1 = the last demoter cannot demote the same player again", true, 0.0, true, 1.0), g_sameKiller);
    bind_pcvar_num(create_cvar("olds_shield_notify", "1", FCVAR_NONE,
        "Tell the players when a shield starts or blocks a demotion", true, 0.0, true, 1.0), g_notify);
    AutoExecConfig(true, "olds_demshield");

    register_clcmd("say /shield", "cmd_shield");
    register_clcmd("say_team /shield", "cmd_shield");
    RegisterHookChain(RG_CBasePlayer_Killed, "on_killed_pre", false);
    RegisterHookChain(RG_CBasePlayer_Killed, "on_killed_post", true);
}

public plugin_cfg()
{
    if (!g_hasLevels || !LibraryExists("armsrace", LibType_Library))
    {
        g_hasLevels = false;
        log_amx("No Arms Race core with ar_get_level/ar_set_level: the shield stays off");
    }
}

/* ---------- the rule ---------- */

/* true when this demotion must not happen; tells the players either way. */
bool:blocked(victim, killer)
{
    new Float:now = get_gametime();
    if (now < g_shieldUntil[victim])
    {
        if (g_notify)
        {
            client_print_color(victim, print_team_default, "^4[Shield]^1 %L", victim, "SH_KEPT",
                floatround(g_shieldUntil[victim] - now, floatround_ceil));
            client_print_color(killer, print_team_default, "^4[Shield]^1 %L", killer, "SH_SHIELDED");
        }
        return true;
    }
    if (g_sameKiller && killer == g_lastDemoter[victim])
    {
        if (g_notify)
        {
            client_print_color(victim, print_team_default, "^4[Shield]^1 %L", victim, "SH_SAME_VICTIM");
            client_print_color(killer, print_team_default, "^4[Shield]^1 %L", killer, "SH_FIND_ANOTHER");
        }
        return true;
    }
    return false;
}

start_shield(victim, killer)
{
    g_shieldUntil[victim] = get_gametime() + g_time;
    g_lastDemoter[victim] = killer;
    if (g_notify && g_time > 0.0)
        client_print_color(victim, print_team_default, "^4[Shield]^1 %L", victim, "SH_STARTED",
            floatround(g_time));
}

/* ---------- the core's pre-forward, when it has one ---------- */

public ar_knife_demote_pre(victim, killer)
{
    if (!g_hasLevels || !(1 <= victim <= MaxClients) || !(1 <= killer <= MaxClients))
        return PLUGIN_CONTINUE;
    g_answeredPre[victim] = true;
    if (blocked(victim, killer))
        return PLUGIN_HANDLED;
    start_shield(victim, killer);
    return PLUGIN_CONTINUE;
}

/* ---------- without it: watch the kill ---------- */

/* The killer holds the knife and the blow came straight from him. */
bool:knife_kill(killer, victim)
{
    new item = get_member(killer, m_pActiveItem);
    if (item <= 0 || get_member(item, m_iId) != WEAPON_KNIFE)
        return false;
    new inflictor = get_entvar(victim, var_dmg_inflictor);
    return bool:(inflictor == 0 || inflictor == killer);
}

public on_killed_pre(victim, killer, gib)
{
    if (!g_hasLevels || !(1 <= victim <= MaxClients))
        return HC_CONTINUE;
    g_answeredPre[victim] = false;
    g_pendingKiller[victim] = 0;
    if (!(1 <= killer <= MaxClients) || killer == victim || !is_user_connected(killer))
        return HC_CONTINUE;
    if (!knife_kill(killer, victim))
        return HC_CONTINUE;
    g_levelBefore[victim] = ar_get_level(victim);
    g_pendingKiller[victim] = killer;
    return HC_CONTINUE;
}

public on_killed_post(victim, killer, gib)
{
    if (1 <= victim <= MaxClients && g_pendingKiller[victim])
    {
        remove_task(TASK_CHECK + victim);
        set_task(CHECK_DELAY, "task_check", TASK_CHECK + victim);
    }
    return HC_CONTINUE;
}

public task_check(task)
{
    new victim = task - TASK_CHECK;
    new killer = g_pendingKiller[victim];
    g_pendingKiller[victim] = 0;
    if (!killer || g_answeredPre[victim] || !is_user_connected(victim))
        return;
    new before = g_levelBefore[victim];
    new now_level = ar_get_level(victim);
    if (now_level >= before)
        return;                       // no level lost (level 1, or not a demotion)

    if (!is_user_connected(killer))
    {
        start_shield(victim, 0);
        return;
    }
    if (blocked(victim, killer))
    {
        ar_set_level(victim, before);
        new vname[MAX_NAME_LENGTH], kname[MAX_NAME_LENGTH];
        get_user_name(victim, vname, charsmax(vname));
        get_user_name(killer, kname, charsmax(kname));
        log_amx("Demotion of %s by %s blocked (level %d kept)", vname, kname, before);
        return;
    }
    start_shield(victim, killer);
}

/* ---------- players ---------- */

public cmd_shield(id)
{
    if (!is_user_connected(id))
        return PLUGIN_HANDLED;
    new Float:now = get_gametime();
    if (now < g_nextUse[id])
        return PLUGIN_HANDLED;
    g_nextUse[id] = now + 1.0;
    if (now < g_shieldUntil[id])
        client_print_color(id, print_team_default, "^4[Shield]^1 %L", id, "SH_LEFT",
            floatround(g_shieldUntil[id] - now, floatround_ceil));
    else
        client_print_color(id, print_team_default, "^4[Shield]^1 %L", id, "SH_NONE");
    return PLUGIN_HANDLED;
}

public client_disconnected(id)
{
    remove_task(TASK_CHECK + id);
    g_shieldUntil[id] = 0.0;
    g_lastDemoter[id] = 0;
    g_pendingKiller[id] = 0;
    g_answeredPre[id] = false;
    g_nextUse[id] = 0.0;
    // Nobody keeps a "last demoter" that is a different player in this slot later.
    for (new v = 1; v <= MaxClients; v++)
        if (g_lastDemoter[v] == id)
            g_lastDemoter[v] = 0;
}

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.