Skip to content
View in the app

A better way to browse. Learn more.

Learn more

client-demo-recorder/addons/amxmodx/scripting/olds_cdemo.sma

olds_cdemo.sma
203 lines · 6.2 KB · Pawn (AMX Mod X)
Raw
/*
 * Client Demo Recorder (olds.ro)
 * Copyright (C) 2026 daemon (olds.ro), written by daemon's plugin agent
 *
 * This program is free software: you can redistribute it and/or modify it under the terms
 * of the GNU General Public License as published by the Free Software Foundation, either
 * version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details: <https://www.gnu.org/licenses/>.
 *
 * Starts a demo on each player's own client olds_cdemo_delay seconds after they join
 * (client_cmd "record <map>_<hhmm>"), so players can replay their rounds. With
 * olds_cdemo_optin 1 nothing is recorded until the player says /demo; /demo while
 * recording stops it. The choice is kept in the player's own userinfo (_cdemo), so an
 * opted-in player records again on the next map, under the new map's name.
 * A client that blocks "record" simply ignores the command: the server sees no error.
 *
 * Cvars: olds_cdemo_enable 1, olds_cdemo_delay 6.0, olds_cdemo_optin 0,
 *        olds_cdemo_bots 0 (1 = also send to bots; for testing only)
 * Admin: amx_cdemo_status (ADMIN_KICK)
 */

#include <amxmodx>
#include <amxmisc>

#pragma semicolon 0

#define PLUGIN  "Client Demo Recorder"
#define VERSION "1.0.0"
#define AUTHOR  "daemon (olds.ro)"

#define TASK_START 8340
#define INFO_KEY   "_cdemo"

new g_enable
new Float:g_delay
new g_optin
new g_bots

new bool:g_recording[MAX_PLAYERS + 1]
new g_demo[MAX_PLAYERS + 1][64]

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

    bind_pcvar_num(create_cvar("olds_cdemo_enable", "1", FCVAR_NONE,
        "1 = record a demo on each player's client", true, 0.0, true, 1.0), g_enable)
    bind_pcvar_float(create_cvar("olds_cdemo_delay", "6.0", FCVAR_NONE,
        "Seconds after joining before the recording starts", true, 0.0, true, 60.0), g_delay)
    bind_pcvar_num(create_cvar("olds_cdemo_optin", "0", FCVAR_NONE,
        "1 = record only for players who say /demo", true, 0.0, true, 1.0), g_optin)
    bind_pcvar_num(create_cvar("olds_cdemo_bots", "0", FCVAR_NONE,
        "1 = also send the commands to bots (for testing only)", true, 0.0, true, 1.0), g_bots)
    AutoExecConfig(true, "olds_cdemo")

    register_clcmd("say /demo", "cmd_demo")
    register_clcmd("say_team /demo", "cmd_demo")
    register_concmd("amx_cdemo_status", "cmd_status", ADMIN_KICK, "- shows who is recording a demo")
}

bool:wanted(id)
{
    if (!is_user_connected(id) || is_user_hltv(id))
        return false
    return !is_user_bot(id) || g_bots
}

public client_putinserver(id)
{
    g_recording[id] = false
    g_demo[id][0] = EOS
    remove_task(TASK_START + id)
    if (!g_enable || is_user_hltv(id))
        return
    if (g_optin && !opted_in(id))
        return
    set_task(floatclamp(g_delay, 0.0, 60.0) + 0.1, "task_start", TASK_START + id)
}

public client_disconnected(id)
{
    remove_task(TASK_START + id)
    if (g_recording[id] && is_user_connected(id) && !is_user_bot(id))
        client_cmd(id, "stop")
    if (g_recording[id])
        log_name("Recording stopped (disconnect)", id)
    g_recording[id] = false
    g_demo[id][0] = EOS
}

opted_in(id)
{
    new value[4]
    get_user_info(id, INFO_KEY, value, charsmax(value))
    return value[0] == '1'
}

public task_start(taskid)
{
    new id = taskid - TASK_START
    if (g_enable && wanted(id) && !g_recording[id])
        start_demo(id)
}

demo_name(out[], len)
{
    new map[32], hhmm[8]
    get_mapname(map, charsmax(map))
    get_time("%H%M", hhmm, charsmax(hhmm))
    /* Keep only characters every client file system accepts. */
    for (new i = 0; map[i]; i++)
    {
        new c = map[i]
        if (!((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '_' || c == '-'))
            map[i] = '_'
    }
    formatex(out, len, "%s_%s", map, hhmm)
}

start_demo(id)
{
    demo_name(g_demo[id], charsmax(g_demo[]))
    /* "stop" first ends a demo still running from the previous map. */
    if (!is_user_bot(id))
        client_cmd(id, "stop;record ^"%s^"", g_demo[id])
    g_recording[id] = true
    client_print_color(id, print_team_default, "%L", id, "CDEMO_STARTED", g_demo[id])
    log_name("Recording started", id)
}

stop_demo(id)
{
    if (!is_user_bot(id))
        client_cmd(id, "stop")
    g_recording[id] = false
    client_print_color(id, print_team_default, "%L", id, "CDEMO_STOPPED", g_demo[id])
    log_name("Recording stopped", id)
}

log_name(const what[], id)
{
    new name[MAX_NAME_LENGTH]
    get_user_name(id, name, charsmax(name))
    log_amx("%s for %s: %s.dem", what, name, g_demo[id])
}

public cmd_demo(id)
{
    if (!is_user_connected(id))
        return PLUGIN_HANDLED
    if (!g_enable)
    {
        client_print_color(id, print_team_default, "%L", id, "CDEMO_OFF")
        return PLUGIN_HANDLED
    }
    if (!wanted(id))
        return PLUGIN_HANDLED
    remove_task(TASK_START + id)
    if (g_recording[id])
    {
        stop_demo(id)
        if (!is_user_bot(id))
            client_cmd(id, "setinfo %s ^"^"", INFO_KEY)
        else
            set_user_info(id, INFO_KEY, "")
    }
    else
    {
        if (!is_user_bot(id))
            client_cmd(id, "setinfo %s 1", INFO_KEY)
        else
            set_user_info(id, INFO_KEY, "1")
        start_demo(id)
    }
    return PLUGIN_HANDLED
}

public cmd_status(id, level, cid)
{
    if (!cmd_access(id, level, cid, 1))
        return PLUGIN_HANDLED
    console_print(id, "Client demos: enable %d, delay %.1f, optin %d", g_enable, g_delay, g_optin)
    new name[MAX_NAME_LENGTH], demo[64], shown
    for (new i = 1; i <= MaxClients; i++)
    {
        if (!is_user_connected(i))
            continue
        get_user_name(i, name, charsmax(name))
        if (g_demo[i][0])
            copy(demo, charsmax(demo), g_demo[i])
        else
            copy(demo, charsmax(demo), "-")
        console_print(id, "  %s: recording %d, opted in %d, demo %s", name, g_recording[i] ? 1 : 0, opted_in(i), demo)
        shown++
    }
    if (!shown)
        console_print(id, "  no players")
    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.