Skip to content
View in the app

A better way to browse. Learn more.

Learn more

spawn-breather/addons/amxmodx/scripting/olds_breather.sma

olds_breather.sma
287 lines · 8.9 KB · Pawn (AMX Mod X)
Raw
/*
 * Spawn Breather (olds.ro workshop card AMX-012)
 *
 * Deathmatch: finds the spawn points that keep leading to very short lives.
 * Every life that starts at a spawn point is tallied for that spawn; a life
 * that ends by an enemy's kill within olds_breather_secs seconds is a short
 * life. Admins read the per-spawn short-life rate, with its sample size, in
 * the console. The plugin never moves, blocks or picks a spawn: that stays
 * with CSDM.
 *
 * Needs: AMX Mod X 1.10, ReGameDLL_CS + ReAPI, CSDM 2.1 (csdm_main).
 *   - Lives start in CSDM's respawn callback, csdm_PostSpawn(player, fake),
 *     after CSDM has placed the player.
 *   - Spawn ids come from the spawn manager's own list: the valid lines of
 *     addons/amxmodx/configs/csdm/<map>.spawns, numbered 1, 2, 3... in file
 *     order, as the CSDM preset spawn mode loads them. Without that file
 *     (CSDM's "none" spawn mode) the map's own spawn points are used:
 *     info_player_start then info_player_deathmatch, numbered in entity order.
 *     The player's spawn is the listed point nearest to where CSDM put them
 *     (within 64 units); a spawn off the list is not tallied.
 *
 * What counts
 *   - A life ended by another player's kill counts; it is short when it lasted
 *     less than olds_breather_secs.
 *   - Suicides (the kill command, falls, the world), admin slays and team
 *     changes add nothing.
 *   - A life still going at a round restart, a disconnect or the next spawn
 *     counts as a long life if it already lasted olds_breather_secs, and adds
 *     nothing otherwise.
 *   - Spawns with fewer than olds_breather_minlives lives are shown as
 *     "low data" instead of a rate. Tallies start over on every map.
 *
 * Cvars: olds_breather_secs 5 (1-60), olds_breather_minlives 20 (1-1000).
 * Command: amx_spawnreport (ADMIN_MAP), console only.
 *
 * Copyright (C) 2026 daemon (olds.ro)
 * SPDX-License-Identifier: GPL-3.0-or-later
 */
#include <amxmodx>
#include <amxmisc>
#include <fakemeta>
#include <reapi>

#define PLUGIN  "Spawn Breather"
#define VERSION "1.0.0"
#define AUTHOR  "olds.ro"

const MAX_SPAWNS = 128
const Float:MATCH_DISTANCE = 64.0

enum
{
    SOURCE_NONE = 0,
    SOURCE_CSDM_FILE,
    SOURCE_MAP
}

new Float:g_spawnOrigin[MAX_SPAWNS][3]
new g_spawnCount
new g_source
new g_spawnFile[128]

new g_lives[MAX_SPAWNS]
new g_short[MAX_SPAWNS]

new g_lifeSpawn[MAX_PLAYERS + 1]          // spawn index + 1 of the current life, 0 = none
new Float:g_lifeStart[MAX_PLAYERS + 1]
new TeamName:g_lifeTeam[MAX_PLAYERS + 1]

new Float:g_shortSecs
new g_minLives

public plugin_init()
{
    register_plugin(PLUGIN, VERSION, AUTHOR)

    bind_pcvar_float(create_cvar("olds_breather_secs", "5", FCVAR_NONE,
        "A life shorter than this many seconds is a short life (1-60).", true, 1.0, true, 60.0), g_shortSecs)
    bind_pcvar_num(create_cvar("olds_breather_minlives", "20", FCVAR_NONE,
        "Spawns with fewer lives than this show 'low data' instead of a rate (1-1000).", true, 1.0, true, 1000.0),
        g_minLives)
    AutoExecConfig(true, "olds_breather")

    register_concmd("amx_spawnreport", "cmd_report", ADMIN_MAP, "- per-spawn short-life rates for this map")

    RegisterHookChain(RG_CBasePlayer_Killed, "on_killed_post", true)
    RegisterHookChain(RG_CSGameRules_RestartRound, "on_restart_round_pre", false)

    load_spawns()
}

/* ---------- the spawn list ---------- */

load_spawns()
{
    new map[64]
    get_mapname(map, charsmax(map))
    get_configsdir(g_spawnFile, charsmax(g_spawnFile))
    format(g_spawnFile, charsmax(g_spawnFile), "%s/csdm/%s.spawns", g_spawnFile, map)

    if (load_csdm_file())
    {
        g_source = SOURCE_CSDM_FILE
        return
    }
    add_map_spawns("info_player_start")
    add_map_spawns("info_player_deathmatch")
    g_source = g_spawnCount ? SOURCE_MAP : SOURCE_NONE
}

/* Valid lines start with three numbers (the origin); the rest of the line is CSDM's. */
bool:load_csdm_file()
{
    new file = fopen(g_spawnFile, "rt")
    if (!file)
        return false

    new line[256], x[16], y[16], z[16]
    while (g_spawnCount < MAX_SPAWNS && fgets(file, line, charsmax(line)))
    {
        trim(line)
        if (!line[0] || line[0] == ';' || (line[0] == '/' && line[1] == '/'))
            continue
        if (parse(line, x, charsmax(x), y, charsmax(y), z, charsmax(z)) < 3
            || !is_number_text(x) || !is_number_text(y) || !is_number_text(z))
            continue
        g_spawnOrigin[g_spawnCount][0] = str_to_float(x)
        g_spawnOrigin[g_spawnCount][1] = str_to_float(y)
        g_spawnOrigin[g_spawnCount][2] = str_to_float(z)
        g_spawnCount++
    }
    fclose(file)
    return g_spawnCount > 0
}

bool:is_number_text(const text[])
{
    new i = (text[0] == '-' || text[0] == '+') ? 1 : 0, digits
    for (; text[i]; i++)
    {
        if (isdigit(text[i]))
            digits++
        else if (text[i] != '.')
            return false
    }
    return digits > 0
}

add_map_spawns(const classname[])
{
    new entity = MaxClients
    while (g_spawnCount < MAX_SPAWNS && (entity = engfunc(EngFunc_FindEntityByString, entity, "classname", classname)) > 0)
    {
        get_entvar(entity, var_origin, g_spawnOrigin[g_spawnCount])
        g_spawnCount++
    }
}

nearest_spawn(const Float:origin[3])
{
    new best = -1, Float:bestDistance = MATCH_DISTANCE
    for (new i = 0; i < g_spawnCount; i++)
    {
        new Float:distance = get_distance_f(origin, g_spawnOrigin[i])
        if (distance <= bestDistance)
        {
            bestDistance = distance
            best = i
        }
    }
    return best
}

/* ---------- lives ---------- */

/* CSDM's respawn callback (csdm.inc: forward csdm_PostSpawn(player, bool:fake)). */
public csdm_PostSpawn(player, bool:fake)
{
    if (!(1 <= player <= MaxClients) || !is_user_alive(player))
        return PLUGIN_CONTINUE

    close_life(player)
    new Float:origin[3]
    get_entvar(player, var_origin, origin)
    new spawn = nearest_spawn(origin)
    if (spawn < 0)
        return PLUGIN_CONTINUE

    g_lifeSpawn[player] = spawn + 1
    g_lifeStart[player] = get_gametime()
    g_lifeTeam[player] = TeamName:get_member(player, m_iTeam)
    return PLUGIN_CONTINUE
}

public on_killed_post(victim, attacker, gib)
{
    if (!(1 <= victim <= MaxClients) || !g_lifeSpawn[victim])
        return HC_CONTINUE

    new spawn = g_lifeSpawn[victim] - 1
    g_lifeSpawn[victim] = 0

    // Only a kill by another player, with the victim still on the team it spawned on.
    if (!(1 <= attacker <= MaxClients) || attacker == victim || !is_user_connected(attacker)
        || TeamName:get_member(victim, m_iTeam) != g_lifeTeam[victim])
        return HC_CONTINUE

    g_lives[spawn]++
    if (get_gametime() - g_lifeStart[victim] < g_shortSecs)
        g_short[spawn]++
    return HC_CONTINUE
}

/* A life that ends without a death: a long life if it already passed the threshold. */
close_life(id)
{
    if (!g_lifeSpawn[id])
        return
    new spawn = g_lifeSpawn[id] - 1
    g_lifeSpawn[id] = 0
    if (TeamName:get_member(id, m_iTeam) == g_lifeTeam[id] && get_gametime() - g_lifeStart[id] >= g_shortSecs)
        g_lives[spawn]++
}

public on_restart_round_pre()
{
    for (new id = 1; id <= MaxClients; id++)
    {
        if (is_user_connected(id))
            close_life(id)
        else
            g_lifeSpawn[id] = 0
    }
    return HC_CONTINUE
}

public client_disconnected(id)
{
    close_life(id)
}

public client_putinserver(id)
{
    g_lifeSpawn[id] = 0
}

/* ---------- report ---------- */

public cmd_report(id, level, cid)
{
    if (!cmd_access(id, level, cid, 1))
        return PLUGIN_HANDLED

    new map[64]
    get_mapname(map, charsmax(map))
    switch (g_source)
    {
        case SOURCE_CSDM_FILE: console_print(id, "[Breather] %s: %d spawns from %s", map, g_spawnCount, g_spawnFile)
        case SOURCE_MAP: console_print(id, "[Breather] %s: %d spawns from the map (no %s)", map, g_spawnCount, g_spawnFile)
        default:
        {
            console_print(id, "[Breather] %s: no spawn points found; nothing is tallied", map)
            return PLUGIN_HANDLED
        }
    }
    console_print(id, "[Breather] short life: under %.1f s, killed by another player; rates need %d lives",
        g_shortSecs, g_minLives)
    console_print(id, "[Breather] spawn  lives  short  rate")

    new totalLives, totalShort, reported
    for (new i = 0; i < g_spawnCount; i++)
    {
        totalLives += g_lives[i]
        totalShort += g_short[i]
        if (!g_lives[i])
            continue
        reported++
        if (g_lives[i] < g_minLives)
            console_print(id, "[Breather] #%-4d %5d  %5d  low data", i + 1, g_lives[i], g_short[i])
        else
            console_print(id, "[Breather] #%-4d %5d  %5d  %d%% (%d of %d)", i + 1, g_lives[i], g_short[i],
                (g_short[i] * 100 + g_lives[i] / 2) / g_lives[i], g_short[i], g_lives[i])
    }
    console_print(id, "[Breather] %d of %d spawns have lives; %d lives in all, %d short", reported, g_spawnCount,
        totalLives, totalShort)
    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.