Skip to content
View in the app

A better way to browse. Learn more.

Learn more

arms-race-leader-bounty/addons/amxmodx/scripting/olds_arleader.sma

olds_arleader.sma
305 lines · 9.1 KB · Pawn (AMX Mod X)
Raw
/*
 * Arms Race Leader Bounty (olds.ro workshop idea IDEA-17469E9F68329C8D)
 *
 * The highest-level player of each team (from olds_ar_leader_min up) is its leader: he
 * glows in his team's colour and the other team sees "Leader: <name>" on the HUD. Killing
 * the enemy leader gives one extra level at once (unless the killer is on the final
 * level), so trailing players always have a clear target.
 *
 * Ties: of two teammates on the same level, the one who reached it first stays leader, so
 * the glow does not jump between them.
 *
 * Levels come from the Arms Race core (library "armsrace"):
 *     ar_get_level(id), ar_set_level(id, level)       required
 *     ar_get_max_level()                             optional (else olds_ar_leader_final)
 *     forward ar_level_changed(id, old_level, new_level)   optional
 * The plugin also checks every player's level twice a second, so a core without the
 * forward works too; without the level natives the plugin stays idle.
 *
 * Cvars: olds_ar_leader_bonus 1, olds_ar_leader_glow 1, olds_ar_leader_min 3,
 *        olds_ar_leader_final 0 (final level when the core has no ar_get_max_level;
 *        0 = no limit).
 *
 * Copyright (C) 2026 daemon (olds.ro)
 * SPDX-License-Identifier: GPL-3.0-or-later
 */
#include <amxmodx>
#include <reapi>

#define PLUGIN  "Arms Race Leader Bounty"
#define VERSION "1.0.0"
#define AUTHOR  "olds.ro"

#define TASK_CHECK  17460
#define TASK_HUD    17461
#define TASK_BONUS  17470      // + killer id
#define SND_BONUS   "buttons/bell1.wav"

new const SIDE[2][] = { "T", "CT" };

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

new bool:g_hasLevels = true, bool:g_hasMax = true;
new g_bonus, g_glow, g_min, g_final;

new g_level[MAX_PLAYERS + 1];             // last level seen
new Float:g_reached[MAX_PLAYERS + 1];     // when the player reached that level
new g_leader[2];                          // 0 = T, 1 = CT; player id or 0
new bool:g_glowing[MAX_PLAYERS + 1];
new g_pendingBonus[MAX_PLAYERS + 1];      // leader kills of this killer to reward
new g_hud;

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

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

public plugin_precache()
{
    precache_sound(SND_BONUS);
}

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

    bind_pcvar_num(create_cvar("olds_ar_leader_bonus", "1", FCVAR_NONE,
        "Killing the enemy leader gives one extra level", true, 0.0, true, 1.0), g_bonus);
    bind_pcvar_num(create_cvar("olds_ar_leader_glow", "1", FCVAR_NONE,
        "Leaders glow in their team's colour", true, 0.0, true, 1.0), g_glow);
    bind_pcvar_num(create_cvar("olds_ar_leader_min", "3", FCVAR_NONE,
        "Lowest level at which a player is marked as leader", true, 1.0, true, 100.0), g_min);
    bind_pcvar_num(create_cvar("olds_ar_leader_final", "0", FCVAR_NONE,
        "Final level if the core has no ar_get_max_level (0 = no limit)", true, 0.0, true, 100.0), g_final);
    AutoExecConfig(true, "olds_arleader");

    RegisterHookChain(RG_CBasePlayer_Killed, "on_killed", true);
    g_hud = CreateHudSyncObj();
}

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: leaders are not marked");
        return;
    }
    set_task(0.5, "task_check", TASK_CHECK, _, _, "b");
    set_task(2.0, "task_hud", TASK_HUD, _, _, "b");
}

/* ---------- levels ---------- */

/* From the core, when it has the forward: re-check at once. */
public ar_level_changed(id, old_level, new_level)
{
    if (!g_hasLevels || !(1 <= id <= MaxClients))
        return;
    note_level(id, new_level);
    update_leaders();
}

public task_check()
{
    for (new id = 1; id <= MaxClients; id++)
        if (is_user_connected(id))
            note_level(id, ar_get_level(id));
    update_leaders();
}

note_level(id, level)
{
    if (level != g_level[id])
    {
        g_level[id] = level;
        g_reached[id] = get_gametime();
    }
}

team_slot(id)
{
    new TeamName:team = get_member(id, m_iTeam);
    if (team == TEAM_TERRORIST)
        return 0;
    if (team == TEAM_CT)
        return 1;
    return -1;
}

update_leaders()
{
    new best[2], slot;
    for (new id = 1; id <= MaxClients; id++)
    {
        if (!is_user_connected(id) || g_level[id] < g_min)
            continue;
        slot = team_slot(id);
        if (slot < 0)
            continue;
        new other = best[slot];
        if (!other || g_level[id] > g_level[other]
            || (g_level[id] == g_level[other] && g_reached[id] < g_reached[other]))
            best[slot] = id;
    }
    for (slot = 0; slot < 2; slot++)
    {
        if (best[slot] != g_leader[slot])
        {
            new old = g_leader[slot];
            g_leader[slot] = best[slot];
            if (old)
                set_glow(old, false, slot);
            if (best[slot])
            {
                new name[MAX_NAME_LENGTH];
                get_user_name(best[slot], name, charsmax(name));
                log_amx("%s leader: %s (level %d)", SIDE[slot], name, g_level[best[slot]]);
            }
        }
        if (g_leader[slot])
            set_glow(g_leader[slot], g_glow != 0 && is_user_alive(g_leader[slot]), slot);
    }
}

set_glow(id, bool:on, slot)
{
    if (!is_user_connected(id))
    {
        g_glowing[id] = false;
        return;
    }
    if (on)
    {
        set_entvar(id, var_renderfx, kRenderFxGlowShell);
        if (slot)
            set_entvar(id, var_rendercolor, Float:{0.0, 80.0, 255.0});
        else
            set_entvar(id, var_rendercolor, Float:{255.0, 40.0, 0.0});
        set_entvar(id, var_rendermode, kRenderNormal);
        set_entvar(id, var_renderamt, 16.0);
        g_glowing[id] = true;
    }
    else if (g_glowing[id])
    {
        set_entvar(id, var_renderfx, kRenderFxNone);
        set_entvar(id, var_rendercolor, Float:{0.0, 0.0, 0.0});
        set_entvar(id, var_renderamt, 0.0);
        g_glowing[id] = false;
    }
}

public task_hud()
{
    new slot, enemy, name[MAX_NAME_LENGTH];
    for (new id = 1; id <= MaxClients; id++)
    {
        if (!is_user_connected(id) || is_user_bot(id))
            continue;
        slot = team_slot(id);
        if (slot < 0)
            continue;
        enemy = g_leader[1 - slot];
        if (!enemy || !is_user_connected(enemy))
            continue;
        get_user_name(enemy, name, charsmax(name));
        if (slot)
            set_hudmessage(255, 80, 40, 0.02, 0.22, 0, 0.0, 2.1, 0.0, 0.0, -1);
        else
            set_hudmessage(60, 140, 255, 0.02, 0.22, 0, 0.0, 2.1, 0.0, 0.0, -1);
        ShowSyncHudMsg(id, g_hud, "%L", id, "ARL_HUD", name, g_level[enemy]);
    }
}

/* ---------- the bounty ---------- */

public on_killed(victim, killer, gib)
{
    if (!g_hasLevels || !(1 <= victim <= MaxClients))
        return HC_CONTINUE;
    new slot = team_slot(victim);
    if (slot < 0 || g_leader[slot] != victim)
        return HC_CONTINUE;
    set_glow(victim, false, slot);

    if (!g_bonus || !(1 <= killer <= MaxClients) || killer == victim || !is_user_connected(killer))
        return HC_CONTINUE;
    new killer_slot = team_slot(killer);
    if (killer_slot < 0 || killer_slot == slot)
        return HC_CONTINUE;

    // Let the core handle the kill first, then add the level.
    g_pendingBonus[killer]++;
    remove_task(TASK_BONUS + killer);
    set_task(0.1, "task_bonus", TASK_BONUS + killer);

    new killer_name[MAX_NAME_LENGTH], victim_name[MAX_NAME_LENGTH];
    get_user_name(killer, killer_name, charsmax(killer_name));
    get_user_name(victim, victim_name, charsmax(victim_name));
    log_amx("%s killed the enemy leader %s", killer_name, victim_name);
    return HC_CONTINUE;
}

public task_bonus(task)
{
    new id = task - TASK_BONUS;
    new count = g_pendingBonus[id];
    g_pendingBonus[id] = 0;
    if (!count || !is_user_connected(id))
        return;

    new final_level = g_hasMax ? ar_get_max_level() : g_final;
    new level = ar_get_level(id);
    new target = level + count;
    if (final_level > 0 && target > final_level)
        target = final_level;
    if (target <= level)
    {
        client_print_color(id, print_team_default, "^4[Arms Race]^1 %L", id, "ARL_FINAL");
        return;
    }
    ar_set_level(id, target);
    note_level(id, ar_get_level(id));
    update_leaders();
    client_cmd(id, "spk %s", SND_BONUS);
    client_print_color(id, print_team_default, "^4[Arms Race]^1 %L", id, "ARL_BONUS", target);
}

public client_putinserver(id)
{
    g_level[id] = 0;
    g_reached[id] = get_gametime();
}

public client_disconnected(id)
{
    remove_task(TASK_BONUS + id);
    g_pendingBonus[id] = 0;
    g_glowing[id] = false;
    g_level[id] = 0;
    for (new slot = 0; slot < 2; slot++)
        if (g_leader[slot] == id)
            g_leader[slot] = 0;
    if (g_hasLevels)
        set_task(0.1, "task_check");
}

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.