Skip to content
View in the app

A better way to browse. Learn more.

Learn more

operator-standby/addons/amxmodx/scripting/olds_standby.sma

olds_standby.sma
219 lines · 6.7 KB · Pawn (AMX Mod X)
Raw
/*
 * Operator Standby (olds.ro workshop card AMX-041)
 *
 * Deathrun: players who would like to take over as operator (the terrorist)
 * say /standby to join a queue. When the operator leaves the server, the
 * first connected volunteer in the queue becomes the operator at the next
 * round's role selection.
 *
 *   say /standby    join or leave the standby queue
 *
 * Needs: AMX Mod X 1.10, ReGameDLL_CS + ReAPI, and Mistrick's Deathrun Mod
 * (library "deathrun_core"; github.com/Mistrick/DeathrunMod):
 *   - dr_chosen_new_terrorist(id) tells who the operator is; when that
 *     player leaves, the operator role is marked vacant (the core puts a
 *     random stand-in in for the rest of the round, as it always does);
 *   - dr_set_next_terrorist(id) is called only at the role-selection point:
 *     at the round restart, just before the core picks the next round's
 *     terrorist, with the first volunteer still connected (stale entries
 *     are removed). If something has already chosen the next terrorist
 *     (dr_get_next_terrorist() names a connected player), the plugin leaves
 *     that choice alone and the vacancy is closed.
 * Only queued players are ever put forward. The chosen volunteer leaves the
 * queue once the core confirms them as operator.
 *
 * Cvar: olds_standby_max 5 (1-16). Command: olds_standby_status (ADMIN_CFG).
 *
 * Copyright (C) 2026 daemon (olds.ro)
 * SPDX-License-Identifier: GPL-3.0-or-later
 */
#include <amxmodx>
#include <amxmisc>
#include <reapi>

#define PLUGIN  "Operator Standby"
#define VERSION "1.0.0"
#define AUTHOR  "olds.ro"

/* Deathrun Mod API (deathrun_core.inc), declared here so the plugin compiles without it. */
#pragma reqlib "deathrun_core"
native dr_set_next_terrorist(id)
native dr_get_next_terrorist()

const MAX_QUEUE = 16

new g_queue[MAX_QUEUE]
new g_length
new g_operator                       // the operator the core last named
new bool:g_vacant                    // the operator left; fill at the next selection
new g_offered                        // volunteer handed to the core, 0 = none
new Float:g_nextUse[MAX_PLAYERS + 1]
new g_max

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

    bind_pcvar_num(create_cvar("olds_standby_max", "5", FCVAR_NONE,
        "Most volunteers in the operator standby queue (1-16).", true, 1.0, true, float(MAX_QUEUE)), g_max)
    AutoExecConfig(true, "olds_standby")

    register_clcmd("say /standby", "cmd_standby")
    register_clcmd("say_team /standby", "cmd_standby")
    register_concmd("olds_standby_status", "cmd_status", ADMIN_CFG, "- the standby queue and the vacancy")

    RegisterHookChain(RG_CSGameRules_RestartRound, "on_restart_round_pre", false)
}

/* ---------- the queue ---------- */

place_of(id)
{
    for (new i = 0; i < g_length; i++)
        if (g_queue[i] == id)
            return i
    return -1
}

bool:remove_from_queue(id)
{
    new place = place_of(id)
    if (place < 0)
        return false
    for (new i = place; i < g_length - 1; i++)
        g_queue[i] = g_queue[i + 1]
    g_queue[--g_length] = 0
    return true
}

/* Drops entries whose player is gone; returns the first one left, or 0. */
first_volunteer()
{
    for (new i = 0; i < g_length; )
    {
        if (is_user_connected(g_queue[i]))
            return g_queue[i]
        remove_from_queue(g_queue[i])
    }
    return 0
}

public cmd_standby(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 (remove_from_queue(id))
    {
        if (g_offered == id)
            withdraw_offer()
        client_print_color(id, print_team_default, "^4[Standby]^1 %L", id, "STANDBY_LEFT")
        return PLUGIN_HANDLED
    }
    if (id == g_operator)
    {
        client_print_color(id, print_team_default, "^4[Standby]^1 %L", id, "STANDBY_IS_OPERATOR")
        return PLUGIN_HANDLED
    }
    if (g_length >= min(g_max, MAX_QUEUE))
    {
        client_print_color(id, print_team_default, "^4[Standby]^1 %L", id, "STANDBY_FULL", g_max)
        return PLUGIN_HANDLED
    }
    g_queue[g_length++] = id
    client_print_color(id, print_team_default, "^4[Standby]^1 %L", id, "STANDBY_JOINED", g_length)
    return PLUGIN_HANDLED
}

/* ---------- the core ---------- */

/* New round or a disconnect replacement: this is the operator now. */
public dr_chosen_new_terrorist(id)
{
    g_operator = (1 <= id <= MaxClients) ? id : 0
    if (!g_offered)
        return
    if (g_operator == g_offered)
    {
        remove_from_queue(g_offered)
        new name[MAX_NAME_LENGTH]
        get_user_name(g_offered, name, charsmax(name))
        client_print_color(0, print_team_red, "^4[Standby]^1 %L", LANG_PLAYER, "STANDBY_TOOK_OVER", name)
        g_vacant = false
    }
    g_offered = 0
}

/* The role-selection point: the core reads its next terrorist right after this. */
public on_restart_round_pre()
{
    if (!g_vacant)
        return HC_CONTINUE
    new chosen = dr_get_next_terrorist()
    if (1 <= chosen <= MaxClients && is_user_connected(chosen) && chosen != g_offered)
    {
        // Something else already chose the next operator: leave it alone.
        g_vacant = false
        g_offered = 0
        return HC_CONTINUE
    }
    offer_first()
    return HC_CONTINUE
}

offer_first()
{
    new volunteer = first_volunteer()
    g_offered = volunteer
    dr_set_next_terrorist(volunteer)
    if (!volunteer)
        g_vacant = false             // nobody queued: the core picks as usual
}

withdraw_offer()
{
    // The handed-over volunteer is gone or left the queue: hand over the next one.
    if (g_vacant)
        offer_first()
    else
        g_offered = 0
}

public client_disconnected(id)
{
    new bool:wasQueued = remove_from_queue(id)
    if (id == g_operator)
    {
        g_operator = 0
        if (g_length)
            g_vacant = true
    }
    if (wasQueued && id == g_offered)
        withdraw_offer()
    g_nextUse[id] = 0.0
}

public cmd_status(id, level, cid)
{
    if (!cmd_access(id, level, cid, 1))
        return PLUGIN_HANDLED
    new name[MAX_NAME_LENGTH]
    if (g_operator && is_user_connected(g_operator))
        get_user_name(g_operator, name, charsmax(name))
    else
        copy(name, charsmax(name), "-")
    console_print(id, "[Standby] operator %s; vacancy %s; queue %d of %d", name, g_vacant ? "open" : "none", g_length, g_max)
    for (new i = 0; i < g_length; i++)
    {
        if (!is_user_connected(g_queue[i]))
            continue
        get_user_name(g_queue[i], name, charsmax(name))
        console_print(id, "[Standby]   %d. %s%s", i + 1, name, g_queue[i] == g_offered ? " (put forward)" : "")
    }
    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.