Skip to content
View in the app

A better way to browse. Learn more.

Learn more

fast-spectator-transfer/addons/amxmodx/scripting/olds_fastspec.sma

olds_fastspec.sma
278 lines · 7.8 KB · Pawn (AMX Mod X)
Raw
/*
 * Fast Spectator Transfer (olds.ro workshop card LIB-2063)
 *
 * say /spec moves a player to the spectators at once, also while alive, and
 * remembers his team; say /back puts him back in that team, unless the team
 * is full or the return would unbalance the teams.
 *
 *   - A living player who types /spec dies quietly (no kill feed line) and
 *     counts as dead for the round; whatever he carries is dropped or removed
 *     as when the game moves a player to the spectators, the C4 is dropped.
 *   - After /back the player is back in his team and plays from his next
 *     spawn (at once if the round still lets new players spawn).
 *   - Both commands share a cooldown, and by default they cannot be used in
 *     freeze time.
 *
 * Cvars (configs/plugins/olds_fastspec.cfg; AutoExecConfig writes that file
 * with these defaults if it is missing):
 *   olds_fspec_cd      5  Seconds between two uses of /spec or /back (0 - 60).
 *   olds_fspec_keep    1  1 = the move to spectators costs nothing on the
 *                         scoreboard; 0 = a living player gets one death, as
 *                         when the game's team menu moves him.
 *   olds_fspec_maxdiff 2  /back is refused if the player's team would then
 *                         have more than this many players over the other
 *                         team (0 = no check).
 *   olds_fspec_freeze  0  1 = the commands also work in freeze time.
 *
 * With the game's allow_spectators 0, /spec is refused.
 *
 * Every text players read comes from data/lang/olds_fastspec.txt (English).
 *
 * Requires AMX Mod X 1.10 or newer and ReAPI with ReGameDLL_CS. Written for
 * AMX Mod X 1.10.0.5490, ReAPI 5.29.0.358, ReGameDLL_CS 5.30.0.814.
 *
 * Copyright (C) 2026 alexit (olds.ro workshop)
 * SPDX-License-Identifier: GPL-3.0-or-later
 *
 * 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/>.
 */

#include <amxmodx>
#include <reapi>

#pragma semicolon 1

new const PLUGIN_NAME[] = "Fast Spectator Transfer";
new const PLUGIN_VERSION[] = "1.0.0";
new const PLUGIN_AUTHOR[] = "alexit (olds.ro)";

new const DICTIONARY[] = "olds_fastspec.txt";

new g_cooldown;     // olds_fspec_cd
new g_keep;         // olds_fspec_keep
new g_maxDiff;      // olds_fspec_maxdiff
new g_inFreeze;     // olds_fspec_freeze

// The team each player left with /spec (TEAM_UNASSIGNED = none).
new TeamName:g_formerTeam[MAX_PLAYERS + 1];

// When each player last used /spec or /back.
new Float:g_lastUse[MAX_PLAYERS + 1];

new g_msgScoreInfo;

// The game's allow_spectators cvar.
new g_allowSpectators;

public plugin_init()
{
	register_plugin(PLUGIN_NAME, PLUGIN_VERSION, PLUGIN_AUTHOR);
	register_dictionary(DICTIONARY);

	bind_pcvar_num(create_cvar("olds_fspec_cd", "5", FCVAR_NONE,
		"Seconds between two uses of /spec or /back (0 - 60)",
		true, 0.0, true, 60.0), g_cooldown);

	bind_pcvar_num(create_cvar("olds_fspec_keep", "1", FCVAR_NONE,
		"1 = moving to spectators costs nothing on the scoreboard, 0 = a living player gets one death",
		true, 0.0, true, 1.0), g_keep);

	bind_pcvar_num(create_cvar("olds_fspec_maxdiff", "2", FCVAR_NONE,
		"Most players a team may have over the other after /back (0 = no check)",
		true, 0.0, true, 32.0), g_maxDiff);

	bind_pcvar_num(create_cvar("olds_fspec_freeze", "0", FCVAR_NONE,
		"1 = /spec and /back also work in freeze time, 0 = not in freeze time",
		true, 0.0, true, 1.0), g_inFreeze);

	AutoExecConfig(true, "olds_fastspec");

	register_clcmd("say /spec", "cmd_spec");
	register_clcmd("say_team /spec", "cmd_spec");
	register_clcmd("say /back", "cmd_back");
	register_clcmd("say_team /back", "cmd_back");

	g_msgScoreInfo = get_user_msgid("ScoreInfo");
	bind_pcvar_num(get_cvar_pointer("allow_spectators"), g_allowSpectators);
}

public client_putinserver(id)
{
	g_formerTeam[id] = TEAM_UNASSIGNED;
	g_lastUse[id] = -1000.0;
}

public client_disconnected(id)
{
	g_formerTeam[id] = TEAM_UNASSIGNED;
}

public cmd_spec(id)
{
	new TeamName:team = get_member(id, m_iTeam);

	if (team == TEAM_SPECTATOR)
	{
		tell(id, "OLDS_FSPEC_ALREADY");
		return PLUGIN_HANDLED;
	}

	if (team != TEAM_TERRORIST && team != TEAM_CT)
	{
		tell(id, "OLDS_FSPEC_NO_TEAM");
		return PLUGIN_HANDLED;
	}

	if (!g_allowSpectators)
	{
		tell(id, "OLDS_FSPEC_NOT_ALLOWED");
		return PLUGIN_HANDLED;
	}

	if (!can_use_now(id))
		return PLUGIN_HANDLED;

	new bool:wasAlive = bool:is_user_alive(id);

	if (!rg_join_team(id, TEAM_SPECTATOR))
		return PLUGIN_HANDLED;

	g_formerTeam[id] = team;
	g_lastUse[id] = get_gametime();

	// The game's own team menu counts a death for a living player.
	if (wasAlive && !g_keep)
	{
		set_member(id, m_iDeaths, get_member(id, m_iDeaths) + 1);
		send_score(id);
	}

	new teamName[32];
	format_team_name(id, team, teamName, charsmax(teamName));
	client_print_color(id, print_team_default, "^4*^1 %L", id, "OLDS_FSPEC_SPEC", teamName);

	return PLUGIN_HANDLED;
}

public cmd_back(id)
{
	new TeamName:team = g_formerTeam[id];

	// Not a spectator (any more), or not through /spec.
	if (get_member(id, m_iTeam) != TEAM_SPECTATOR || team == TEAM_UNASSIGNED)
	{
		g_formerTeam[id] = TEAM_UNASSIGNED;
		tell(id, "OLDS_FSPEC_HINT");
		return PLUGIN_HANDLED;
	}

	if (!can_use_now(id))
		return PLUGIN_HANDLED;

	new teamName[32];
	format_team_name(id, team, teamName, charsmax(teamName));

	new TeamName:otherTeam = (team == TEAM_TERRORIST) ? TEAM_CT : TEAM_TERRORIST;
	new players = count_team(team);
	new others = count_team(otherTeam);

	if (team_full(team, players))
	{
		client_print_color(id, print_team_default, "^4*^1 %L", id, "OLDS_FSPEC_FULL", teamName);
		return PLUGIN_HANDLED;
	}

	if (g_maxDiff && players + 1 - others > g_maxDiff)
	{
		client_print_color(id, print_team_default, "^4*^1 %L", id, "OLDS_FSPEC_UNBALANCED",
			players + 1, others, teamName, g_maxDiff);
		return PLUGIN_HANDLED;
	}

	if (!rg_join_team(id, team))
		return PLUGIN_HANDLED;

	g_formerTeam[id] = TEAM_UNASSIGNED;
	g_lastUse[id] = get_gametime();

	client_print_color(id, print_team_default, "^4*^1 %L", id, "OLDS_FSPEC_BACK", teamName);

	return PLUGIN_HANDLED;
}

/**
 * Freeze time and the cooldown; tells the player why not.
 */
bool:can_use_now(id)
{
	if (!g_inFreeze && get_member_game(m_bFreezePeriod))
	{
		tell(id, "OLDS_FSPEC_FREEZE");
		return false;
	}

	new Float:wait = g_lastUse[id] + float(g_cooldown) - get_gametime();

	if (wait > 0.0)
	{
		client_print_color(id, print_team_default, "^4*^1 %L", id, "OLDS_FSPEC_COOLDOWN", floatround(wait, floatround_ceil));
		return false;
	}

	return true;
}

/**
 * The game's limit: no more players in a team than it has spawn points.
 */
bool:team_full(TeamName:team, players)
{
	new spawns = get_member_game(team == TEAM_TERRORIST ? m_iSpawnPointCount_Terrorist : m_iSpawnPointCount_CT);

	return players >= spawns;
}

count_team(TeamName:team)
{
	new count = 0;

	for (new i = 1; i <= MaxClients; i++)
	{
		if (is_user_connected(i) && get_member(i, m_iTeam) == team)
			count++;
	}

	return count;
}

format_team_name(id, TeamName:team, name[], len)
{
	formatex(name, len, "%L", id, team == TEAM_TERRORIST ? "OLDS_FSPEC_TEAM_T" : "OLDS_FSPEC_TEAM_CT");
}

tell(id, const key[])
{
	client_print_color(id, print_team_default, "^4*^1 %L", id, key);
}

/**
 * Updates the player's line on everyone's scoreboard (team 0 = spectator).
 */
send_score(id)
{
	emessage_begin(MSG_ALL, g_msgScoreInfo);
	ewrite_byte(id);
	ewrite_short(floatround(Float:get_entvar(id, var_frags)));
	ewrite_short(get_member(id, m_iDeaths));
	ewrite_short(0);
	ewrite_short(0);
	emessage_end();
}

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.