Skip to content
View in the app

A better way to browse. Learn more.

Learn more

guard-authority-grant/addons/amxmodx/scripting/olds_gauth.sma

olds_gauth.sma
261 lines · 7.4 KB · Pawn (AMX Mod X)
Raw
/*
 * Guard Authority Grant (olds.ro workshop card LIB-0876)
 *
 * Jailbreak: when fewer than olds_gauth_minadmins admins are on the guard (CT)
 * side, one living guard gets the olds_gauth_flags access flags for the round,
 * so commands such as the warden menu still work. The grant is taken back at
 * round end, when enough admins are guards again, when the guard dies, leaves
 * or changes team (another guard is chosen), and on map change.
 *
 * Cvars
 *   olds_gauth_minadmins 1     admins needed on CT before nobody is granted
 *   olds_gauth_pick longest    who is chosen: longest (on the server), score
 *                              (most frags) or random
 *   olds_gauth_flags "b"       the flags lent to the guard
 *   olds_gauth_adminflag "d"   a player with any of these flags is an admin
 *
 *   amx_gauth_status (ADMIN_KICK)   admins on CT, the holder and the flags
 *
 * Only flags the guard did not already have are lent, and only those are taken
 * back. Checks run at round start, when a player leaves, dies or changes team.
 * Every grant and removal is written to the AMX Mod X log.
 *
 * Copyright (C) 2026 daemon (olds.ro)
 * SPDX-License-Identifier: GPL-3.0-or-later
 */
#include <amxmodx>
#include <amxmisc>
#include <hamsandwich>
#include <reapi>

#define PLUGIN  "Guard Authority Grant"
#define VERSION "1.0.0"
#define AUTHOR  "olds.ro"

const TASK_CHECK = 5871
const Float:CHECK_DELAY = 0.1

enum
{
    PICK_LONGEST = 0,
    PICK_SCORE,
    PICK_RANDOM
}

new g_minAdmins
new g_pickWord[16], g_flagsWord[27], g_adminWord[27]

new g_holder                             // the guard holding the lent flags (0 = none)
new g_lent                               // the flags lent to him
new bool:g_roundOver

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

    bind_pcvar_num(create_cvar("olds_gauth_minadmins", "1", FCVAR_NONE,
        "Admins needed on the guard side before nobody is granted authority.", true, 0.0, true, 32.0), g_minAdmins)
    bind_pcvar_string(create_cvar("olds_gauth_pick", "longest", FCVAR_NONE,
        "Who is chosen: longest, score or random."), g_pickWord, charsmax(g_pickWord))
    bind_pcvar_string(create_cvar("olds_gauth_flags", "b", FCVAR_NONE,
        "Access flags lent to the chosen guard."), g_flagsWord, charsmax(g_flagsWord))
    bind_pcvar_string(create_cvar("olds_gauth_adminflag", "d", FCVAR_NONE,
        "A player with any of these flags counts as an admin."), g_adminWord, charsmax(g_adminWord))
    AutoExecConfig(true, "olds_gauth")

    RegisterHookChain(RG_CSGameRules_RestartRound, "on_restart_round_post", true)
    RegisterHookChain(RG_RoundEnd, "on_round_end_post", true)
    RegisterHam(Ham_Killed, "player", "on_killed_post", true)
    register_event("TeamInfo", "on_team_info", "a")
    register_concmd("amx_gauth_status", "cmd_status", ADMIN_KICK, "- guard authority: admins on CT and the holder")
}

public plugin_end()
{
    revoke("map change", false)
}

/* ---------- triggers ---------- */

public on_restart_round_post()
{
    g_roundOver = false
    schedule_check()
}

public on_round_end_post()
{
    g_roundOver = true
    revoke("round end")
}

public on_killed_post(victim, attacker, shouldGib)
{
    if (victim == g_holder)
        revoke("the guard died")
    schedule_check()
}

public on_team_info()
{
    schedule_check()
}

public client_disconnected(id)
{
    if (id == g_holder)
    {
        new name[MAX_NAME_LENGTH]
        get_user_name(id, name, charsmax(name))
        log_amx("guard authority (%s) dropped: %s left", g_flagsWord, name)
        g_holder = 0
        g_lent = 0
    }
    schedule_check()
}

schedule_check()
{
    remove_task(TASK_CHECK)
    set_task(CHECK_DELAY, "task_check", TASK_CHECK)
}

/* ---------- the check ---------- */

bool:is_guard(id)
{
    return is_user_connected(id) && TeamName:get_member(id, m_iTeam) == TEAM_CT
}

admins_on_ct()
{
    new adminFlags = read_flags(g_adminWord), count
    if (!adminFlags)
        return 0
    for (new id = 1; id <= MaxClients; id++)
    {
        if (id == g_holder || !is_guard(id))
            continue
        if (get_user_flags(id) & adminFlags)
            count++
    }
    return count
}

pick_mode()
{
    if (equali(g_pickWord, "score"))
        return PICK_SCORE
    if (equali(g_pickWord, "random"))
        return PICK_RANDOM
    return PICK_LONGEST
}

choose_guard()
{
    new mode = pick_mode(), best, bestValue = -1, candidates[MAX_PLAYERS], count
    for (new id = 1; id <= MaxClients; id++)
    {
        if (!is_guard(id) || !is_user_alive(id) || is_user_hltv(id))
            continue
        candidates[count++] = id
        new value
        if (mode == PICK_SCORE)
            value = get_user_frags(id)
        else
            value = get_user_time(id)
        if (value > bestValue)
        {
            bestValue = value
            best = id
        }
    }
    if (!count)
        return 0
    if (mode == PICK_RANDOM)
        return candidates[random(count)]
    return best
}

public task_check()
{
    if (g_roundOver)
        return
    if (g_holder && (!is_guard(g_holder) || !is_user_alive(g_holder)))
        revoke("the guard is no longer a living guard")
    if (admins_on_ct() >= g_minAdmins)
    {
        revoke("an admin guards the CT side")
        return
    }
    if (g_holder)
        return
    new flags = read_flags(g_flagsWord)
    if (!flags)
        return
    new guard = choose_guard()
    if (!guard)
        return
    grant(guard, flags)
}

grant(id, flags)
{
    g_lent = flags & ~get_user_flags(id)
    g_holder = id
    if (g_lent)
        set_user_flags(id, g_lent)
    new name[MAX_NAME_LENGTH], auth[34]
    get_user_name(id, name, charsmax(name))
    get_user_authid(id, auth, charsmax(auth))
    log_amx("guard authority (%s) granted to %s <%s>: fewer than %d admin(s) on CT, pick %s", g_flagsWord, name, auth,
        g_minAdmins, g_pickWord)
    client_print_color(0, id, "^4[Guards]^1 %L", LANG_PLAYER, "GAUTH_GRANTED", name)
}

revoke(const reason[], bool:announce = true)
{
    if (!g_holder)
        return
    new id = g_holder
    g_holder = 0
    if (!is_user_connected(id))
    {
        g_lent = 0
        return
    }
    if (g_lent)
        remove_user_flags(id, g_lent)
    g_lent = 0
    new name[MAX_NAME_LENGTH]
    get_user_name(id, name, charsmax(name))
    log_amx("guard authority (%s) removed from %s: %s", g_flagsWord, name, reason)
    if (announce)
        client_print_color(0, print_team_default, "^4[Guards]^1 %L", LANG_PLAYER, "GAUTH_REMOVED", name)
}

/* ---------- status ---------- */

public cmd_status(id, level, cid)
{
    if (!cmd_access(id, level, cid, 1))
        return PLUGIN_HANDLED
    new holder[MAX_NAME_LENGTH] = "nobody", lent[27]
    if (g_holder)
        get_user_name(g_holder, holder, charsmax(holder))
    get_flags(g_lent, lent, charsmax(lent))
    console_print(id, "[Guards] admins on CT %d (needed %d), holder %s, lent flags ^"%s^", pick %s, round over %d",
        admins_on_ct(), g_minAdmins, holder, lent, g_pickWord, g_roundOver)
    new name[MAX_NAME_LENGTH], flags[27]
    for (new p = 1; p <= MaxClients; p++)
    {
        if (!is_user_connected(p))
            continue
        get_user_name(p, name, charsmax(name))
        get_flags(get_user_flags(p), flags, charsmax(flags))
        console_print(id, "[Guards]   %s: team %d, alive %d, flags %s, time %d", name, get_member(p, m_iTeam),
            is_user_alive(p), flags, get_user_time(p))
    }
    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.