Skip to content
View in the app

A better way to browse. Learn more.

Learn more

auto-respawn/addons/amxmodx/scripting/olds_autorespawn.sma

olds_autorespawn.sma
231 lines · 6.7 KB · Pawn (AMX Mod X)
Raw
/*
 * Auto Respawn (olds.ro workshop card LIB-0777)
 * Copyright (C) 2026 daemon (olds.ro)
 *
 * 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/>.
 *
 * Players respawn olds_ar_delay seconds after death, with a HUD countdown. No respawn for
 * spectators, for a team that olds_ar_team leaves out, after the round has ended, or once a
 * player used up olds_ar_maxdeaths respawns this round (0 = no limit).
 */
#include <amxmodx>
#include <fakemeta>
#include <reapi>

#define PLUGIN  "Auto Respawn"
#define VERSION "1.0.0"
#define AUTHOR  "daemon (olds.ro)"

#define TASK_RESPAWN 77700
#define MAX_SPAWNS   128

new g_cvDelay, g_cvTeam, g_cvRandom, g_cvMaxDeaths
new Float:g_delay, g_team, g_random, g_maxDeaths
new bool:g_roundOver
new g_left[MAX_PLAYERS + 1], g_used[MAX_PLAYERS + 1]
new g_hud
new Float:g_spawns[MAX_SPAWNS][3], g_spawnCount

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

    g_cvDelay = create_cvar("olds_ar_delay", "3.0", FCVAR_NONE, "Seconds from death to respawn", true, 0.5, true, 60.0)
    g_cvTeam = create_cvar("olds_ar_team", "3", FCVAR_NONE, "Teams that respawn: 1 T, 2 CT, 3 both", true, 1.0, true, 3.0)
    g_cvRandom = create_cvar("olds_ar_random", "1", FCVAR_NONE, "1 = respawn at a random free spawn point", true, 0.0, true, 1.0)
    g_cvMaxDeaths = create_cvar("olds_ar_maxdeaths", "0", FCVAR_NONE, "Respawns per player per round, 0 = no limit", true, 0.0, true, 1000.0)
    bind_pcvar_float(g_cvDelay, g_delay)
    bind_pcvar_num(g_cvTeam, g_team)
    bind_pcvar_num(g_cvRandom, g_random)
    bind_pcvar_num(g_cvMaxDeaths, g_maxDeaths)
    AutoExecConfig(true, "olds_autorespawn")

    RegisterHookChain(RG_CBasePlayer_Killed, "on_killed_post", true)
    RegisterHookChain(RG_RoundEnd, "on_round_end_post", true)
    RegisterHookChain(RG_CSGameRules_RestartRound, "on_restart_pre", false)
    RegisterHookChain(RG_CBasePlayer_Spawn, "on_spawn_post", true)

    register_concmd("amx_ar_status", "cmd_status", ADMIN_RCON, "- shows who is waiting to respawn")

    g_hud = CreateHudSyncObj()
    collect_spawns("info_player_deathmatch")
    collect_spawns("info_player_start")
}

collect_spawns(const classname[])
{
    new ent = -1
    while (g_spawnCount < MAX_SPAWNS && (ent = engfunc(EngFunc_FindEntityByString, ent, "classname", classname)) > 0)
    {
        pev(ent, pev_origin, g_spawns[g_spawnCount])
        g_spawnCount++
    }
}

public client_disconnected(id)
{
    cancel(id)
    g_used[id] = 0
}

cancel(id)
{
    remove_task(TASK_RESPAWN + id)
    g_left[id] = 0
}

bool:team_allowed(id)
{
    new TeamName:team = TeamName:get_member(id, m_iTeam)
    if (team == TEAM_TERRORIST)
        return (g_team & 1) != 0
    if (team == TEAM_CT)
        return (g_team & 2) != 0
    return false
}

bool:round_over()
{
    return g_roundOver || get_member_game(m_bRoundTerminating) != 0
}

public on_killed_post(id, attacker, gib)
{
    if (!is_user_connected(id) || is_user_alive(id))
        return
    cancel(id)
    if (round_over() || !team_allowed(id))
        return
    if (g_maxDeaths > 0 && g_used[id] >= g_maxDeaths)
    {
        client_print(id, print_center, "%L", id, "AR_NO_MORE", g_maxDeaths)
        return
    }
    g_left[id] = floatround(g_delay, floatround_ceil)
    new Float:first = g_delay - float(g_left[id] - 1)
    show_count(id)
    set_task(first, "tick", TASK_RESPAWN + id)
}

show_count(id)
{
    set_hudmessage(255, 200, 60, -1.0, 0.30, 0, 0.0, 1.1, 0.0, 0.1, -1)
    ShowSyncHudMsg(id, g_hud, "%L", id, "AR_COUNT", g_left[id])
}

public tick(task)
{
    new id = task - TASK_RESPAWN
    if (!is_user_connected(id) || is_user_alive(id) || round_over() || !team_allowed(id))
    {
        g_left[id] = 0
        if (is_user_connected(id))
            ClearSyncHud(id, g_hud)
        return
    }
    g_left[id]--
    if (g_left[id] > 0)
    {
        show_count(id)
        set_task(1.0, "tick", TASK_RESPAWN + id)
        return
    }
    ClearSyncHud(id, g_hud)
    g_used[id]++
    rg_round_respawn(id)
    if (g_random && is_user_alive(id))
        move_to_random_spawn(id)
}

move_to_random_spawn(id)
{
    if (!g_spawnCount)
        return
    new start = random(g_spawnCount)
    for (new i = 0; i < g_spawnCount; i++)
    {
        new n = (start + i) % g_spawnCount
        if (!spot_free(id, g_spawns[n]))
            continue
        engfunc(EngFunc_SetOrigin, id, g_spawns[n])
        set_pev(id, pev_velocity, Float:{0.0, 0.0, 0.0})
        return
    }
}

bool:spot_free(id, const Float:spot[3])
{
    new trace = create_tr2()
    engfunc(EngFunc_TraceHull, spot, spot, 0, HULL_HUMAN, id, trace)
    new bool:free = !get_tr2(trace, TR_StartSolid) && !get_tr2(trace, TR_AllSolid) && get_tr2(trace, TR_InOpen)
    free_tr2(trace)
    return free
}

public on_spawn_post(id)
{
    if (is_user_alive(id))
    {
        remove_task(TASK_RESPAWN + id)
        g_left[id] = 0
    }
}

public on_round_end_post()
{
    g_roundOver = true
    for (new id = 1; id <= MaxClients; id++)
    {
        if (task_exists(TASK_RESPAWN + id))
        {
            cancel(id)
            if (is_user_connected(id))
                ClearSyncHud(id, g_hud)
        }
    }
}

public on_restart_pre()
{
    g_roundOver = false
    for (new id = 1; id <= MaxClients; id++)
    {
        cancel(id)
        g_used[id] = 0
    }
}

public cmd_status(id, level, cid)
{
    if (!(get_user_flags(id) & level) && id)
    {
        console_print(id, "You have no access to that command.")
        return PLUGIN_HANDLED
    }
    new name[32], waiting, state_[8], round_[8]
    copy(round_, charsmax(round_), "running")
    if (round_over())
        copy(round_, charsmax(round_), "over")
    for (new p = 1; p <= MaxClients; p++)
    {
        if (!is_user_connected(p))
            continue
        get_user_name(p, name, charsmax(name))
        copy(state_, charsmax(state_), "dead")
        if (is_user_alive(p))
            copy(state_, charsmax(state_), "alive")
        console_print(id, "[AR] %s: %s, team %d, respawns used %d, %d s left", name,
            state_, get_member(p, m_iTeam), g_used[p], g_left[p])
        if (g_left[p] > 0)
            waiting++
    }
    console_print(id, "[AR] %d waiting, round %s, delay %.1f, team %d, random %d, max %d", waiting,
        round_, g_delay, g_team, g_random, g_maxDeaths)
    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.