Skip to content
View in the app

A better way to browse. Learn more.

Learn more

spectator-hud-information/addons/amxmodx/scripting/olds_spechud.sma

olds_spechud.sma
227 lines · 6.6 KB · Pawn (AMX Mod X)
Raw
/*
 * Spectator HUD Information (olds.ro workshop card LIB-2073)
 *
 * A spectator (or dead player) whose camera follows a player sees that
 * player's health, armour, money and ammo in a small HUD block near the
 * bottom of the screen. The block is refreshed every olds_spech_rate
 * seconds, at once when the watched player fires or switches weapons, and
 * it disappears as soon as the camera stops following a player (free look,
 * free overview map).
 *
 * Cvars (configs/plugins/olds_spechud.cfg; AutoExecConfig writes that file
 * with these defaults if it is missing):
 *   olds_spech_money 1    1 = show the watched player's money, 0 = hide it.
 *   olds_spech_enemy 1    1 = also when a dead player watches an enemy,
 *                         0 = only teammates (spectators see everyone).
 *   olds_spech_rate  0.5  Seconds between two updates (0.1 - 5).
 *
 * Every text players read comes from data/lang/olds_spechud.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 <cstrike>
#include <fun>
#include <reapi>

#pragma semicolon 1

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

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

const TASK_UPDATE = 42730;

// Where the block is drawn: centred, near the bottom, HUD channel 4.
const Float:HUD_X = -1.0;
const Float:HUD_Y = 0.80;
const HUD_CHANNEL = 4;

// 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;

new g_showMoney;        // olds_spech_money
new g_showEnemy;        // olds_spech_enemy
new Float:g_rate;       // olds_spech_rate

// The block is on this spectator'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_spech_money", "1", FCVAR_NONE,
		"1 = show the watched player's money, 0 = hide it",
		true, 0.0, true, 1.0), g_showMoney);

	bind_pcvar_num(create_cvar("olds_spech_enemy", "1", FCVAR_NONE,
		"1 = dead players also see the block for an enemy they watch, 0 = only for teammates",
		true, 0.0, true, 1.0), g_showEnemy);

	new rate = create_cvar("olds_spech_rate", "0.5", FCVAR_NONE,
		"Seconds between two updates of the block (0.1 - 5)",
		true, 0.1, true, 5.0);
	bind_pcvar_float(rate, g_rate);
	hook_cvar_change(rate, "OnRateChange");

	AutoExecConfig(true, "olds_spechud");

	// The watched player fires or switches weapons: update his spectators at once.
	register_event("CurWeapon", "OnCurWeapon", "be", "1=1");

	// A spectator changes camera mode or target: update him at once.
	RegisterHookChain(RG_CBasePlayer_Observer_SetMode, "OnObserverChange_Post", true);
	RegisterHookChain(RG_CBasePlayer_Observer_FindNextPlayer, "OnObserverChange_Post", true);

	start_updates(g_rate);
}

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

public OnRateChange(pcvar, const oldValue[], const newValue[])
{
	start_updates(str_to_float(newValue));
}

public OnCurWeapon(target)
{
	for (new spectator = 1; spectator <= MaxClients; spectator++)
	{
		if (spectator != target && is_user_connected(spectator) && watched_player(spectator) == target)
			update_spectator(spectator);
	}
}

public OnObserverChange_Post(spectator)
{
	if (is_user_connected(spectator))
		update_spectator(spectator);
}

public task_update()
{
	for (new spectator = 1; spectator <= MaxClients; spectator++)
	{
		if (is_user_connected(spectator))
			update_spectator(spectator);
	}
}

start_updates(Float:rate)
{
	remove_task(TASK_UPDATE);
	set_task(floatmax(rate, 0.1), "task_update", TASK_UPDATE, _, _, "b");
}

update_spectator(spectator)
{
	new target = watched_player(spectator);

	if (!target || !may_see(spectator, target))
	{
		// Take a block still on the screen away at once.
		if (g_shown[spectator])
		{
			set_hudmessage(0, 0, 0, HUD_X, HUD_Y, 0, 0.0, 0.1, 0.0, 0.0, HUD_CHANNEL);
			show_hudmessage(spectator, " ");
			g_shown[spectator] = false;
		}

		return;
	}

	new text[192];
	build_block(spectator, target, text, charsmax(text));

	// Stays a little longer than the update interval, so it never blinks.
	set_hudmessage(255, 255, 255, HUD_X, HUD_Y, 0, 0.0, g_rate + 0.2, 0.0, 0.0, HUD_CHANNEL);
	show_hudmessage(spectator, "%s", text);
	g_shown[spectator] = true;
}

/**
 * The living player the spectator's camera follows, 0 = none.
 */
watched_player(spectator)
{
	if (is_user_alive(spectator))
		return 0;

	switch (get_entvar(spectator, var_iuser1))
	{
		case MODE_CHASE_LOCKED, MODE_CHASE_FREE, MODE_IN_EYE, MODE_MAP_CHASE:
		{
			new target = get_entvar(spectator, var_iuser2);

			if (target >= 1 && target <= MaxClients && target != spectator && is_user_alive(target))
				return target;
		}
	}

	return 0;
}

/**
 * Spectators see everyone; a dead player sees an enemy only with
 * olds_spech_enemy 1.
 */
bool:may_see(spectator, target)
{
	if (g_showEnemy)
		return true;

	new TeamName:team = get_member(spectator, m_iTeam);

	if (team != TEAM_TERRORIST && team != TEAM_CT)
		return true;

	return get_member(target, m_iTeam) == team;
}

build_block(spectator, target, text[], len)
{
	new name[MAX_NAME_LENGTH];
	get_user_name(target, name, charsmax(name));

	new length = formatex(text, len, "%s^n%L   %L", name,
		spectator, "OLDS_SPECH_HEALTH", get_user_health(target),
		spectator, "OLDS_SPECH_ARMOR", get_user_armor(target));

	if (g_showMoney)
		length += formatex(text[length], len - length, "   %L", spectator, "OLDS_SPECH_MONEY", cs_get_user_money(target));

	new clip, ammo;
	new weaponId = get_user_weapon(target, clip, ammo);

	// Guns only: the knife has no ammo, grenades and the C4 no clip.
	if (weaponId && clip >= 0)
	{
		get_user_ammo(target, weaponId, clip, ammo);
		formatex(text[length], len - length, "   %L", spectator, "OLDS_SPECH_AMMO", clip, ammo);
	}
}

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.