Skip to content
View in the app

A better way to browse. Learn more.

Learn more

spectator-hp-list/addons/amxmodx/scripting/olds_hplist.sma

olds_hplist.sma
238 lines · 6.3 KB · Pawn (AMX Mod X)
Raw
/*
 * Spectator HP List (olds.ro workshop card LIB-2072)
 *
 * Spectators and dead players see two lists on the sides of the screen: the
 * living Terrorists on the left and the living Counter-Terrorists on the
 * right, each with their health, lowest health first. The lists are
 * refreshed every second.
 *
 *   - Each list shows at most olds_hplist_max rows; its title gives the
 *     number of living players in that team.
 *   - Names longer than 16 characters are cut, so no row wraps.
 *   - say /hplist (or say_team /hplist) turns the lists off or on for that
 *     player until he leaves the server.
 *
 * Cvars (configs/plugins/olds_hplist.cfg; AutoExecConfig writes that file
 * with these defaults if it is missing):
 *   olds_hplist_max  8  Rows per team (1 - 32).
 *   olds_hplist_sort 1  1 = lowest health first, 0 = in join order.
 *   olds_hplist_dead 1  1 = only spectators and dead players see the lists,
 *                       0 = living players too.
 *
 * Every text players read comes from data/lang/olds_hplist.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[] = "Spectator HP List";
new const PLUGIN_VERSION[] = "1.0.0";
new const PLUGIN_AUTHOR[] = "alexit (olds.ro)";

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

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

// Terrorists on the left (channel 1), Counter-Terrorists on the right (channel 2).
const Float:LEFT_X = 0.01;
const Float:RIGHT_X = 0.80;
const Float:LIST_Y = 0.25;
const CHANNEL_T = 1;
const CHANNEL_CT = 2;

// Longest name shown, in characters.
const NAME_CHARS = 16;

const MAX_TEXT = 512;

new g_maxRows;       // olds_hplist_max
new g_sort;          // olds_hplist_sort
new g_deadOnly;      // olds_hplist_dead

// Players who turned the lists off with /hplist.
new bool:g_off[MAX_PLAYERS + 1];

// The lists are on this player's screen now.
new bool:g_shown[MAX_PLAYERS + 1];

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

	bind_pcvar_num(create_cvar("olds_hplist_max", "8", FCVAR_NONE,
		"Rows per team (1 - 32)",
		true, 1.0, true, 32.0), g_maxRows);

	bind_pcvar_num(create_cvar("olds_hplist_sort", "1", FCVAR_NONE,
		"1 = lowest health first, 0 = in join order",
		true, 0.0, true, 1.0), g_sort);

	bind_pcvar_num(create_cvar("olds_hplist_dead", "1", FCVAR_NONE,
		"1 = only spectators and dead players see the lists, 0 = living players too",
		true, 0.0, true, 1.0), g_deadOnly);

	AutoExecConfig(true, "olds_hplist");

	register_clcmd("say /hplist", "cmd_hplist");
	register_clcmd("say_team /hplist", "cmd_hplist");

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

public client_putinserver(id)
{
	g_off[id] = false;
	g_shown[id] = false;
}

public cmd_hplist(id)
{
	g_off[id] = !g_off[id];
	client_print_color(id, print_team_default, "^4*^1 %L", id, g_off[id] ? "OLDS_HPLIST_OFF" : "OLDS_HPLIST_ON");

	if (g_off[id])
		clear_lists(id);

	return PLUGIN_HANDLED;
}

public task_refresh()
{
	new viewers[MAX_PLAYERS], viewerCount = 0;

	for (new id = 1; id <= MaxClients; id++)
	{
		if (!is_user_connected(id) || g_off[id])
			continue;

		if (g_deadOnly && is_user_alive(id))
		{
			if (g_shown[id])
				clear_lists(id);

			continue;
		}

		viewers[viewerCount++] = id;
	}

	if (!viewerCount)
		return;

	new players[MAX_PLAYERS], count;
	new text[MAX_TEXT];

	// The texts depend on each viewer's language only through the titles.
	get_players(players, count, "ae", "TERRORIST");
	sort_by_health(players, count);

	for (new i = 0; i < viewerCount; i++)
	{
		build_list(viewers[i], "OLDS_HPLIST_T", players, count, text, charsmax(text));
		set_hudmessage(255, 90, 80, LEFT_X, LIST_Y, 0, 0.0, REFRESH_INTERVAL + 0.2, 0.0, 0.0, CHANNEL_T);
		show_hudmessage(viewers[i], "%s", text);
	}

	get_players(players, count, "ae", "CT");
	sort_by_health(players, count);

	for (new i = 0; i < viewerCount; i++)
	{
		build_list(viewers[i], "OLDS_HPLIST_CT", players, count, text, charsmax(text));
		set_hudmessage(110, 170, 255, RIGHT_X, LIST_Y, 0, 0.0, REFRESH_INTERVAL + 0.2, 0.0, 0.0, CHANNEL_CT);
		show_hudmessage(viewers[i], "%s", text);
		g_shown[viewers[i]] = true;
	}
}

/**
 * Lowest health first (with olds_hplist_sort 1); equal health keeps the
 * join order.
 */
sort_by_health(players[], count)
{
	if (!g_sort)
		return;

	for (new i = 1; i < count; i++)
	{
		new player = players[i];
		new health = get_user_health(player);
		new j = i - 1;

		while (j >= 0 && get_user_health(players[j]) > health)
		{
			players[j + 1] = players[j];
			j--;
		}

		players[j + 1] = player;
	}
}

build_list(viewer, const titleKey[], const players[], count, text[], len)
{
	new length = formatex(text, len, "%L", viewer, titleKey, count);
	new rows = min(count, g_maxRows);
	new name[MAX_NAME_LENGTH];

	for (new i = 0; i < rows && length < len; i++)
	{
		get_user_name(players[i], name, charsmax(name));
		cut_name(name, NAME_CHARS);
		length += formatex(text[length], len - length, "^n%3d  %s", get_user_health(players[i]), name);
	}
}

/**
 * Cuts a UTF-8 name after the given number of characters (never inside a
 * character).
 */
cut_name(name[], chars)
{
	new seen = 0;

	for (new i = 0; name[i]; i++)
	{
		// A byte that starts a character (not a 10xxxxxx continuation byte).
		if ((name[i] & 0xC0) != 0x80)
		{
			if (seen == chars)
			{
				name[i] = EOS;
				return;
			}

			seen++;
		}
	}
}

clear_lists(id)
{
	set_hudmessage(0, 0, 0, LEFT_X, LIST_Y, 0, 0.0, 0.1, 0.0, 0.0, CHANNEL_T);
	show_hudmessage(id, " ");
	set_hudmessage(0, 0, 0, RIGHT_X, LIST_Y, 0, 0.0, 0.1, 0.0, 0.0, CHANNEL_CT);
	show_hudmessage(id, " ");
	g_shown[id] = false;
}

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.