Skip to content
View in the app

A better way to browse. Learn more.

Learn more

captain-s-succession-card/addons/amxmodx/scripting/olds_succession.sma

olds_succession.sma
252 lines · 7.2 KB · Pawn (AMX Mod X)
Raw
/*
 * Captain's Succession Card (olds.ro workshop card AMX-099)
 *
 * Captain modes: records who was each team's captain during the round, when
 * the role changed hands, and how long it stood vacant, and shows both
 * teams' cards to everyone once the round has ended:
 *     T: (vacant) 0:00-0:20, Alpha 0:20-1:45
 *     CT: Bravo 0:00-0:52, Charlie 0:52-1:45
 * Times are from the start of the round. /captains repeats the last card,
 * but only after the round: during the round it says so and shows nothing.
 *
 *   say /captains   the last round's succession card
 *
 * Adapter: captain mods have no common API, so the mod, or a small bridge,
 * reports the role through one native (library "olds_succession",
 * include/olds_succession.inc):
 *     olds_succession_set(team, id)   team 1 = T, 2 = CT; id = the new
 *                                     captain, or 0 when the role is vacant
 * A captain who leaves the server leaves the role vacant. Each team starts
 * every round vacant until the mod names a captain. Gaps under one second are
 * not listed.
 *
 * The card is revealed only at the real round end (RG_RoundEnd, or a round
 * closed by another plugin, checked every second). A game restart
 * (Game Commencing or sv_restart) clears everything.
 *
 * Cvar: olds_succession_max 6 (2-12), entries kept per team per round.
 *
 * Copyright (C) 2026 daemon (olds.ro)
 * SPDX-License-Identifier: GPL-3.0-or-later
 */
#include <amxmodx>
#include <amxmisc>
#include <reapi>

#define PLUGIN  "Captain's Succession Card"
#define VERSION "1.0.0"
#define AUTHOR  "olds.ro"

const MAX_ENTRIES = 12
const TEAMS = 2
const TASK_WATCH = 9911

/* The round being recorded. */
new g_name[TEAMS][MAX_ENTRIES][MAX_NAME_LENGTH]   // empty = vacant
new g_from[TEAMS][MAX_ENTRIES]                    // seconds from round start
new g_to[TEAMS][MAX_ENTRIES]
new g_count[TEAMS]
new g_captain[TEAMS]                              // player id, 0 = vacant
new Float:g_openSince[TEAMS]
new bool:g_truncated[TEAMS]

/* The last finished round's card, one line per team. */
new g_card[TEAMS][192]
new bool:g_haveCard

new Float:g_roundStart
new bool:g_roundLive
new Float:g_nextUse[MAX_PLAYERS + 1]
new g_max

public plugin_natives()
{
    register_library("olds_succession")
    register_native("olds_succession_set", "native_set")
}

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

    bind_pcvar_num(create_cvar("olds_succession_max", "6", FCVAR_NONE,
        "Entries kept per team per round (2-12).", true, 2.0, true, float(MAX_ENTRIES)), g_max)
    AutoExecConfig(true, "olds_succession")

    register_clcmd("say /captains", "cmd_captains")
    register_clcmd("say_team /captains", "cmd_captains")

    RegisterHookChain(RG_CSGameRules_RestartRound, "on_restart_round_pre", false)
    RegisterHookChain(RG_RoundEnd, "on_round_end_post", true)
    set_task(1.0, "task_watch_round_end", TASK_WATCH, _, _, "b")
}

/* ---------- recording ---------- */

now_seconds()
{
    return max(floatround(get_gametime() - g_roundStart, floatround_floor), 0)
}

/* Closes the open entry (captain or vacancy) and opens the next one. */
change(team, captain)
{
    new t = team - 1
    if (g_roundLive && g_captain[t] == captain && g_openSince[t] > 0.0)
        return
    close_open(t)
    g_captain[t] = captain
    g_openSince[t] = get_gametime()
}

close_open(t)
{
    if (g_openSince[t] == 0.0)
        return
    new from = max(floatround(g_openSince[t] - g_roundStart, floatround_floor), 0), to = now_seconds()
    new captain = g_captain[t]
    g_openSince[t] = 0.0
    // A vacancy under one second is not worth listing.
    if (!captain && to - from < 1)
        return
    if (g_count[t] >= min(g_max, MAX_ENTRIES))
    {
        g_truncated[t] = true
        return
    }
    new e = g_count[t]++
    g_from[t][e] = from
    g_to[t][e] = to
    if (captain && is_user_connected(captain))
        get_user_name(captain, g_name[t][e], charsmax(g_name[][]))
    else
        g_name[t][e][0] = EOS
}

public native_set(plugin, params)
{
    new team = get_param(1), id = get_param(2)
    if (!(1 <= team <= TEAMS))
    {
        log_error(AMX_ERR_NATIVE, "olds_succession_set: invalid team %d (1 = T, 2 = CT)", team)
        return 0
    }
    if (id && (!(1 <= id <= MaxClients) || !is_user_connected(id)))
    {
        log_error(AMX_ERR_NATIVE, "olds_succession_set: invalid player %d", id)
        return 0
    }
    if (!g_roundLive)
        return 0
    change(team, id)
    return 1
}

public client_disconnected(id)
{
    for (new t = 0; t < TEAMS; t++)
        if (g_roundLive && g_captain[t] == id)
            change(t + 1, 0)
    g_nextUse[id] = 0.0
}

/* ---------- rounds ---------- */

public on_restart_round_pre()
{
    if (get_member_game(m_bCompleteReset))
    {
        g_haveCard = false
        g_card[0][0] = EOS
        g_card[1][0] = EOS
    }
    g_roundStart = get_gametime()
    g_roundLive = true
    for (new t = 0; t < TEAMS; t++)
    {
        g_count[t] = 0
        g_captain[t] = 0
        g_truncated[t] = false
        g_openSince[t] = get_gametime()     // every team starts vacant
    }
    return HC_CONTINUE
}

public on_round_end_post(WinStatus:status, ScenarioEventEndRound:event, Float:delay)
{
    if (event != ROUND_GAME_COMMENCE && event != ROUND_GAME_RESTART && get_member_game(m_bRoundTerminating))
        finish_round()
    return HC_CONTINUE
}

/* A round closed by rg_round_end() from another plugin skips RG_RoundEnd. */
public task_watch_round_end()
{
    if (g_roundLive && get_member_game(m_bRoundTerminating))
        finish_round()
}

finish_round()
{
    if (!g_roundLive)
        return
    g_roundLive = false
    for (new t = 0; t < TEAMS; t++)
    {
        close_open(t)
        build_card(t)
    }
    g_haveCard = true
    for (new p = 1; p <= MaxClients; p++)
        if (is_user_connected(p) && !is_user_bot(p))
            print_card(p)
}

build_card(t)
{
    new written, from[8], to[8], vacant[16]
    copy(vacant, charsmax(vacant), "(vacant)")
    g_card[t][0] = EOS
    for (new e = 0; e < g_count[t]; e++)
    {
        clock(g_from[t][e], from, charsmax(from))
        clock(g_to[t][e], to, charsmax(to))
        written += formatex(g_card[t][written], charsmax(g_card[]) - written, "%s%s %s-%s", e ? ", " : "",
            g_name[t][e][0] ? g_name[t][e] : vacant, from, to)
    }
    if (g_truncated[t])
        add(g_card[t], charsmax(g_card[]), ", ...")
    if (!g_count[t])
        copy(g_card[t], charsmax(g_card[]), "-")
}

clock(seconds, output[], length)
{
    formatex(output, length, "%d:%02d", seconds / 60, seconds % 60)
}

print_card(id)
{
    client_print_color(id, print_team_red, "^4[Captains]^3 T:^1 %s", g_card[0])
    client_print_color(id, print_team_blue, "^4[Captains]^3 CT:^1 %s", g_card[1])
}

/* ---------- /captains ---------- */

public cmd_captains(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 (g_roundLive)
        client_print_color(id, print_team_default, "^4[Captains]^1 %L", id, "SUCCESSION_AFTER_ROUND")
    else if (!g_haveCard)
        client_print_color(id, print_team_default, "^4[Captains]^1 %L", id, "SUCCESSION_NONE")
    else
        print_card(id)
    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.