Skip to content
View in the app

A better way to browse. Learn more.

Learn more

session-splits/addons/amxmodx/scripting/olds_splits.sma

olds_splits.sma
225 lines · 7 KB · Pawn (AMX Mod X)
Raw
/*
 * Session Splits (olds.ro workshop card AMX-035)
 *
 * Deathmatch practice: a player times equal practice blocks and compares the
 * last two. /block starts a block; it completes by itself after
 * olds_splits_minutes minutes of active play. /splits compares kills per
 * minute for the two most recent completed blocks.
 *
 *   say /block     start a block (while one runs: cancel it)
 *   say /splits    compare the last two completed blocks
 *
 * Active play: the clock runs once a second while the player is on a team
 * (alive, or dead and waiting for the respawn, as with CSDM); it stops while
 * they spectate, so a block simply lasts longer. Kills (RG_CBasePlayer_Killed:
 * another player killed by this player while the block runs) count toward the
 * block. A block that is not finished is dropped on disconnect and on map
 * change; completed blocks last for the map.
 *
 * Cvar: olds_splits_minutes 5 (1-30), read when a block starts.
 * Command: olds_splits_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  "Session Splits"
#define VERSION "1.0.0"
#define AUTHOR  "olds.ro"

const TASK_CLOCK = 9311

new bool:g_running[MAX_PLAYERS + 1]
new g_length[MAX_PLAYERS + 1]          // seconds this block must run
new g_active[MAX_PLAYERS + 1]          // active seconds so far
new g_kills[MAX_PLAYERS + 1]

// The two latest completed blocks: [0] older, [1] newer.
new g_doneKills[MAX_PLAYERS + 1][2]
new g_doneLength[MAX_PLAYERS + 1][2]
new g_doneCount[MAX_PLAYERS + 1]

new Float:g_nextUse[MAX_PLAYERS + 1]
new g_minutes

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

    bind_pcvar_num(create_cvar("olds_splits_minutes", "5", FCVAR_NONE,
        "Active minutes in one practice block (1-30).", true, 1.0, true, 30.0), g_minutes)
    AutoExecConfig(true, "olds_splits")

    register_clcmd("say /block", "cmd_block")
    register_clcmd("say_team /block", "cmd_block")
    register_clcmd("say /splits", "cmd_splits")
    register_clcmd("say_team /splits", "cmd_splits")
    register_concmd("olds_splits_status", "cmd_status", ADMIN_CFG, "- every player's block and splits")

    RegisterHookChain(RG_CBasePlayer_Killed, "on_killed_post", true)
    set_task(1.0, "task_clock", TASK_CLOCK, _, _, "b")
}

/* ---------- blocks ---------- */

bool:is_playing(id)
{
    new TeamName:team = TeamName:get_member(id, m_iTeam)
    return team == TEAM_TERRORIST || team == TEAM_CT
}

public task_clock()
{
    for (new id = 1; id <= MaxClients; id++)
    {
        if (!g_running[id] || !is_user_connected(id) || !is_playing(id))
            continue
        if (++g_active[id] >= g_length[id])
            complete_block(id)
    }
}

complete_block(id)
{
    g_running[id] = false
    // Keep the two latest: the newer moves to the older slot.
    if (g_doneCount[id] == 2)
    {
        g_doneKills[id][0] = g_doneKills[id][1]
        g_doneLength[id][0] = g_doneLength[id][1]
        g_doneCount[id] = 1
    }
    g_doneKills[id][g_doneCount[id]] = g_kills[id]
    g_doneLength[id][g_doneCount[id]] = g_length[id]
    g_doneCount[id]++

    new rate[16]
    format_rate(g_kills[id], g_length[id], rate, charsmax(rate))
    client_print_color(id, print_team_default, "^4[Splits]^1 %L", id, "SPLITS_DONE", g_kills[id], rate)
    show_dhud(id, 80, 220, 120, fmt("%L", id, "SPLITS_HUD_DONE", rate))
}

public on_killed_post(victim, attacker, gib)
{
    if (1 <= attacker <= MaxClients && attacker != victim && g_running[attacker] && is_user_connected(attacker)
        && 1 <= victim <= MaxClients)
        g_kills[attacker]++
    return HC_CONTINUE
}

/* ---------- commands ---------- */

bool:cooled(id)
{
    new Float:now = get_gametime()
    if (now < g_nextUse[id])
        return false
    g_nextUse[id] = now + 1.0
    return true
}

public cmd_block(id)
{
    if (!is_user_connected(id) || !cooled(id))
        return PLUGIN_HANDLED

    if (g_running[id])
    {
        g_running[id] = false
        client_print_color(id, print_team_default, "^4[Splits]^1 %L", id, "SPLITS_CANCELLED")
        return PLUGIN_HANDLED
    }
    g_running[id] = true
    g_length[id] = g_minutes * 60
    g_active[id] = 0
    g_kills[id] = 0
    client_print_color(id, print_team_default, "^4[Splits]^1 %L", id, "SPLITS_STARTED", g_minutes)
    show_dhud(id, 255, 220, 120, fmt("%L", id, "SPLITS_HUD_STARTED", g_minutes))
    return PLUGIN_HANDLED
}

public cmd_splits(id)
{
    if (!is_user_connected(id) || !cooled(id))
        return PLUGIN_HANDLED

    if (g_doneCount[id] < 2)
    {
        client_print_color(id, print_team_default, "^4[Splits]^1 %L", id, "SPLITS_NEED_TWO", g_doneCount[id])
        return PLUGIN_HANDLED
    }
    new older[16], newer[16], change[16]
    format_rate(g_doneKills[id][0], g_doneLength[id][0], older, charsmax(older))
    format_rate(g_doneKills[id][1], g_doneLength[id][1], newer, charsmax(newer))

    // Change in tenths of a kill per minute, rounded like the two rates.
    new delta = rate_tenths(g_doneKills[id][1], g_doneLength[id][1]) - rate_tenths(g_doneKills[id][0], g_doneLength[id][0])
    formatex(change, charsmax(change), "%s%d.%d", delta < 0 ? "-" : "+", abs(delta) / 10, abs(delta) % 10)

    client_print_color(id, print_team_default, "^4[Splits]^1 %L", id, "SPLITS_COMPARE", older, g_doneKills[id][0],
        newer, g_doneKills[id][1], change)
    return PLUGIN_HANDLED
}

/* Kills per minute in tenths, rounded half up: 26 kills in 300 s -> 52. */
rate_tenths(kills, seconds)
{
    if (seconds <= 0)
        return 0
    return (kills * 600 + seconds / 2) / seconds
}

format_rate(kills, seconds, output[], len)
{
    new tenths = rate_tenths(kills, seconds)
    formatex(output, len, "%d.%d", tenths / 10, tenths % 10)
}

show_dhud(id, r, g, b, const text[])
{
    set_dhudmessage(r, g, b, -1.0, 0.25, 0, 0.0, 3.0, 0.1, 0.5)
    show_dhudmessage(id, "%s", text)
}

public client_putinserver(id)
{
    reset_player(id)
}

public client_disconnected(id)
{
    reset_player(id)
}

reset_player(id)
{
    g_running[id] = false
    g_active[id] = 0
    g_kills[id] = 0
    g_doneCount[id] = 0
    g_nextUse[id] = 0.0
}

public cmd_status(id, level, cid)
{
    if (!cmd_access(id, level, cid, 1))
        return PLUGIN_HANDLED
    console_print(id, "[Splits] blocks are %d minute(s)", g_minutes)
    new players[MAX_PLAYERS], count, name[MAX_NAME_LENGTH]
    get_players(players, count, "h")
    for (new i = 0; i < count; i++)
    {
        new p = players[i]
        get_user_name(p, name, charsmax(name))
        console_print(id, "[Splits] %s: %s %d/%d s, %d kill(s); %d completed%s", name,
            g_running[p] ? "running" : "idle", g_active[p], g_running[p] ? g_length[p] : 0, g_kills[p], g_doneCount[p],
            is_playing(p) ? "" : " (spectating: clock paused)")
        for (new b = 0; b < g_doneCount[p]; b++)
            console_print(id, "[Splits]   block %d: %d kill(s) in %d s", b + 1, g_doneKills[p][b], g_doneLength[p][b])
    }
    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.