Skip to content
View in the app

A better way to browse. Learn more.

Learn more

spectator-list/addons/amxmodx/scripting/olds_speclist.sma

olds_speclist.sma
309 lines · 8.4 KB · Pawn (AMX Mod X)
Raw
/*
 * Spectator List (olds.ro workshop card LIB-2075)
 *
 * Shows every living player who is watching him: a small HUD list in the top
 * left corner with the names of the spectators (and dead players) whose
 * camera follows him, refreshed every second and at once when a spectator
 * switches to another player or camera mode.
 *
 *   - Admins with immunity (ADMIN_IMMUNITY, flag a) can be left out of the
 *     list (olds_speclist_hideadmin); admins with the kick flag (ADMIN_KICK,
 *     flag c) still see them when olds_speclist_admin is 1.
 *   - At most olds_speclist_max names are listed, then "+N" for the rest.
 *   - say /speclist (or say_team /speclist) hides or shows the list for that
 *     player; the choice is saved (nVault "olds_speclist") by SteamID, or by
 *     name on servers without unique SteamIDs, so it stays on the next map.
 *
 * Cvars (configs/plugins/olds_speclist.cfg; AutoExecConfig writes that file
 * with these defaults if it is missing):
 *   olds_speclist_admin     1  1 = admins (flag c) also see hidden admins.
 *   olds_speclist_hideadmin 1  1 = spectators with immunity (flag a) are left
 *                              out of the list.
 *   olds_speclist_max       5  Most names listed (1 - 32); the rest is "+N".
 *
 * Every text players read comes from data/lang/olds_speclist.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 <nvault>
#include <reapi>

#pragma semicolon 1

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

new const DICTIONARY[] = "olds_speclist.txt";
new const VAULT_NAME[] = "olds_speclist";

// Saved choices not used for this many days are removed at map end.
const VAULT_KEEP_DAYS = 60;

const TASK_REFRESH = 42750;
const Float:REFRESH_INTERVAL = 1.0;

// Where the list is drawn: top left corner, HUD channel 2.
const Float:HUD_X = 0.02;
const Float:HUD_Y = 0.15;
const HUD_CHANNEL = 2;

// Camera modes (pev_iuser1) that follow one player (pev_iuser2).
const MODE_CHASE_LOCKED = 1;
const MODE_CHASE_FREE = 2;
const MODE_IN_EYE = 4;
const MODE_MAP_CHASE = 6;

// Who sees hidden admins, and who is hidden.
const ADMIN_SEES_ALL = ADMIN_KICK;
const ADMIN_HIDDEN = ADMIN_IMMUNITY;

const MAX_TEXT = 512;

// nvault_open() gives -1 when the vault cannot be opened.
const NO_VAULT = -1;

new g_adminSeesAll;    // olds_speclist_admin
new g_hideAdmins;      // olds_speclist_hideadmin
new g_maxNames;        // olds_speclist_max

new g_vault = NO_VAULT;

// Players who turned their list off with /speclist.
new bool:g_off[MAX_PLAYERS + 1];

// The list each player has on his screen now ("" = none).
new g_shownText[MAX_PLAYERS + 1][MAX_TEXT];

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

	bind_pcvar_num(create_cvar("olds_speclist_admin", "1", FCVAR_NONE,
		"1 = admins with the kick flag (c) also see hidden admins in their list",
		true, 0.0, true, 1.0), g_adminSeesAll);

	bind_pcvar_num(create_cvar("olds_speclist_hideadmin", "1", FCVAR_NONE,
		"1 = spectators with immunity (flag a) are left out of the list",
		true, 0.0, true, 1.0), g_hideAdmins);

	bind_pcvar_num(create_cvar("olds_speclist_max", "5", FCVAR_NONE,
		"Most names listed (1 - 32); the others are shown as +N",
		true, 1.0, true, 32.0), g_maxNames);

	AutoExecConfig(true, "olds_speclist");

	register_clcmd("say /speclist", "cmd_speclist");
	register_clcmd("say_team /speclist", "cmd_speclist");

	// A spectator who picks another player or camera mode: update at once.
	RegisterHookChain(RG_CBasePlayer_Observer_SetMode, "OnObserverChange_Post", true);
	RegisterHookChain(RG_CBasePlayer_Observer_FindNextPlayer, "OnObserverChange_Post", true);

	g_vault = nvault_open(VAULT_NAME);

	set_task(REFRESH_INTERVAL, "task_refresh", TASK_REFRESH, _, _, "b");
}

public plugin_end()
{
	if (g_vault != NO_VAULT)
	{
		nvault_prune(g_vault, 0, get_systime() - VAULT_KEEP_DAYS * 86400);
		nvault_close(g_vault);
	}
}

public client_putinserver(id)
{
	g_shownText[id][0] = EOS;
	g_off[id] = false;

	new key[MAX_AUTHID_LENGTH + MAX_NAME_LENGTH];

	if (g_vault != NO_VAULT && vault_key(id, key, charsmax(key)))
		g_off[id] = nvault_get(g_vault, key) == 1;
}

public client_disconnected(id)
{
	g_shownText[id][0] = EOS;
}

public cmd_speclist(id)
{
	g_off[id] = !g_off[id];

	new key[MAX_AUTHID_LENGTH + MAX_NAME_LENGTH];

	if (g_vault != NO_VAULT && vault_key(id, key, charsmax(key)))
	{
		if (g_off[id])
			nvault_set(g_vault, key, "1");
		else
			nvault_remove(g_vault, key);
	}

	client_print_color(id, print_team_default, "^4*^1 %L", id, g_off[id] ? "OLDS_SPECLIST_OFF" : "OLDS_SPECLIST_ON");

	update_player(id, true);

	return PLUGIN_HANDLED;
}

public OnObserverChange_Post(id)
{
	refresh_all(false);
}

public task_refresh()
{
	refresh_all(true);
}

/**
 * @param redraw    true = draw every list again (they fade after a second),
 *                  false = only the lists that changed
 */
refresh_all(bool:redraw)
{
	for (new id = 1; id <= MaxClients; id++)
	{
		if (is_user_connected(id))
			update_player(id, redraw);
	}
}

update_player(id, bool:redraw)
{
	new text[MAX_TEXT];

	if (!g_off[id] && is_user_alive(id))
		build_list(id, text, charsmax(text));

	if (!text[0])
	{
		// Clear a list that is still on the screen.
		if (g_shownText[id][0])
		{
			set_hudmessage(0, 0, 0, HUD_X, HUD_Y, 0, 0.0, 0.1, 0.0, 0.0, HUD_CHANNEL);
			show_hudmessage(id, " ");
			g_shownText[id][0] = EOS;
		}

		return;
	}

	if (!redraw && equal(text, g_shownText[id]))
		return;

	copy(g_shownText[id], charsmax(g_shownText[]), text);

	// Stays a little longer than the refresh interval, so it never blinks.
	set_hudmessage(200, 200, 200, HUD_X, HUD_Y, 0, 0.0, REFRESH_INTERVAL + 0.2, 0.0, 0.0, HUD_CHANNEL);
	show_hudmessage(id, "%s", text);
}

/**
 * The spectators following the player, at most olds_speclist_max names and
 * then "+N". Empty when nobody (visible) watches him.
 */
build_list(id, text[], len)
{
	new bool:seesHidden = g_adminSeesAll && (get_user_flags(id) & ADMIN_SEES_ALL);
	new count = 0;
	new names[MAX_TEXT];
	new length = 0;
	new name[MAX_NAME_LENGTH];

	for (new spectator = 1; spectator <= MaxClients; spectator++)
	{
		if (spectator == id || !is_user_connected(spectator) || is_user_alive(spectator))
			continue;

		if (!follows(spectator, id))
			continue;

		if (g_hideAdmins && !seesHidden && (get_user_flags(spectator) & ADMIN_HIDDEN))
			continue;

		count++;

		if (count <= g_maxNames)
		{
			get_user_name(spectator, name, charsmax(name));
			length += formatex(names[length], charsmax(names) - length, "^n%s", name);
		}
	}

	if (!count)
	{
		text[0] = EOS;
		return;
	}

	new written = formatex(text, len, "%L%s", id, "OLDS_SPECLIST_TITLE", count, names);

	if (count > g_maxNames)
		formatex(text[written], len - written, "^n+%d", count - g_maxNames);
}

/**
 * The spectator's camera follows the player.
 */
bool:follows(spectator, id)
{
	if (get_entvar(spectator, var_iuser2) != id)
		return false;

	switch (get_entvar(spectator, var_iuser1))
	{
		case MODE_CHASE_LOCKED, MODE_CHASE_FREE, MODE_IN_EYE, MODE_MAP_CHASE:
			return true;
	}

	return false;
}

/**
 * The SteamID when it tells players apart, else the name. Bots are not saved.
 */
bool:vault_key(id, key[], len)
{
	if (is_user_bot(id) || is_user_hltv(id))
		return false;

	new authId[MAX_AUTHID_LENGTH];
	get_user_authid(id, authId, charsmax(authId));

	if (!authId[0] || equal(authId, "STEAM_ID_LAN") || equal(authId, "STEAM_ID_PENDING")
		|| equal(authId, "VALVE_ID_LAN") || equal(authId, "VALVE_ID_PENDING"))
	{
		new name[MAX_NAME_LENGTH];
		get_user_name(id, name, charsmax(name));
		formatex(key, len, "name:%s", name);
	}
	else
	{
		copy(key, len, authId);
	}

	return true;
}

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.