Skip to content
View in the app

A better way to browse. Learn more.

Learn more

enemy-health-display/addons/amxmodx/scripting/olds_hpinfo.sma

olds_hpinfo.sma
230 lines · 6.4 KB · Pawn (AMX Mod X)
Raw
/*
 * Enemy Health Display (olds.ro workshop card LIB-0002)
 *
 * While you aim at another living player, a HUD line under the crosshair
 * shows his name, health and armor. A cvar can limit it to teammates, and
 * each player can switch it off and on with /hpinfo.
 *
 * Commands:
 *   say /hpinfo, say_team /hpinfo   switch the line off or on for yourself
 *
 * Cvars (in configs/plugins/olds_hpinfo.cfg, which ships with the plugin; if
 * the file is missing, the plugin writes it with these defaults the first
 * time it starts):
 *   olds_eh_enemies  1     1 = enemies and teammates are shown;
 *                          0 = teammates only.
 *   olds_eh_range    2000  Players farther away than this (units) are not
 *                          shown.
 *
 * Rules:
 *   - Only a living player sees the line, and only for a living target that
 *     his aim really hits (a straight trace from his eyes: walls block it,
 *     smoke does not).
 *   - The line is red for an enemy and green for a teammate (everyone is an
 *     enemy with mp_freeforall 1).
 *   - When the aim leaves the target, the target goes out of range or dies,
 *     the line is cleared at the next refresh (0.2 s).
 *   - The /hpinfo choice lasts until the player leaves the server.
 *
 * How it works, as the card asks: RG_CBasePlayer_PreThink of a living player
 * finds the aimed player with get_user_aiming (an engine traceline), at most
 * every 0.1 s per player; a 0.2 s repeating set_task prints the line with
 * ShowSyncHudMsg on one fixed sync object, so a new line replaces the old.
 *
 * Requires AMX Mod X 1.10 with ReAPI (ReGameDLL_CS). Written for AMX Mod X
 * 1.10.0.5490, ReAPI 5.29.0.358 and 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[] = "Enemy Health Display";
new const PLUGIN_VERSION[] = "1.0.0";
new const PLUGIN_AUTHOR[] = "alexit (olds.ro)";

const TASK_REFRESH = 2200;

const Float:REFRESH = 0.2;
const Float:TRACE_EVERY = 0.1;

// The line stays a little longer than one refresh, so it never flickers.
const Float:HUD_HOLD = 0.35;
const Float:HUD_X = -1.0;
const Float:HUD_Y = 0.60;

new g_enemies;          // olds_eh_enemies
new g_range;            // olds_eh_range

new g_freeForAllCvar;   // mp_freeforall, 0 if the server has none
new g_sync;

// Per player: the player he aims at (0 = none), the time of his next trace,
// whether a line is on his screen, and the /hpinfo choice.
new g_aimed[MAX_PLAYERS + 1];
new Float:g_nextTrace[MAX_PLAYERS + 1];
new bool:g_shown[MAX_PLAYERS + 1];
new bool:g_hidden[MAX_PLAYERS + 1];

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

	// The think hook comes from ReGameDLL_CS.
	if (!is_regamedll())
		set_fail_state("ReGameDLL_CS is not running. This plugin needs ReGameDLL_CS and ReAPI.");

	register_dictionary("olds_hpinfo.txt");

	bind_pcvar_num(create_cvar("olds_eh_enemies", "1", FCVAR_NONE,
		"1 = enemies and teammates are shown; 0 = teammates only",
		true, 0.0, true, 1.0), g_enemies);

	bind_pcvar_num(create_cvar("olds_eh_range", "2000", FCVAR_NONE,
		"Players farther away than this (units) are not shown",
		true, 1.0, true, 8192.0), g_range);

	AutoExecConfig(true, "olds_hpinfo");

	g_freeForAllCvar = get_cvar_pointer("mp_freeforall");
	g_sync = CreateHudSyncObj();

	register_clcmd("say /hpinfo", "cmd_toggle");
	register_clcmd("say_team /hpinfo", "cmd_toggle");

	RegisterHookChain(RG_CBasePlayer_PreThink, "on_pre_think", false);

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

public client_putinserver(id)
{
	g_aimed[id] = 0;
	g_nextTrace[id] = 0.0;
	g_shown[id] = false;
	g_hidden[id] = false;
}

public client_disconnected(id)
{
	g_aimed[id] = 0;
	g_shown[id] = false;
	g_hidden[id] = false;

	// Nobody keeps aiming at a player who left.
	for (new other = 1; other <= MaxClients; other++)
	{
		if (g_aimed[other] == id)
			g_aimed[other] = 0;
	}
}

public cmd_toggle(id)
{
	g_hidden[id] = !g_hidden[id];

	if (g_hidden[id])
		clear_line(id);

	client_print_color(id, print_team_default, "%L", id, g_hidden[id] ? "EH_OFF" : "EH_ON");
	return PLUGIN_HANDLED;
}

/**
 * A living player thinks: at most every 0.1 s, find who he aims at.
 */
public on_pre_think(id)
{
	if (g_hidden[id] || !is_user_alive(id))
		return HC_CONTINUE;

	new Float:now = get_gametime();

	if (now < g_nextTrace[id])
		return HC_CONTINUE;

	g_nextTrace[id] = now + TRACE_EVERY;

	new target, body;
	get_user_aiming(id, target, body, g_range);

	g_aimed[id] = (target >= 1 && target <= MaxClients) ? target : 0;
	return HC_CONTINUE;
}

public task_refresh()
{
	for (new id = 1; id <= MaxClients; id++)
	{
		if (!is_user_connected(id))
			continue;

		new target = g_aimed[id];

		if (!g_hidden[id] && is_user_alive(id) && target && can_show(id, target))
			show_line(id, target);
		else
			clear_line(id);
	}
}

bool:can_show(id, target)
{
	if (!is_user_alive(target))
		return false;

	// get_user_aiming already kept to the range; the target may have moved.
	new Float:from[3], Float:to[3];
	get_entvar(id, var_origin, from);
	get_entvar(target, var_origin, to);

	if (get_distance_f(from, to) > float(g_range))
		return false;

	return g_enemies || is_teammate(id, target);
}

show_line(id, target)
{
	new name[MAX_NAME_LENGTH];
	get_user_name(target, name, charsmax(name));

	if (is_teammate(id, target))
		set_hudmessage(60, 220, 60, HUD_X, HUD_Y, 0, 0.0, HUD_HOLD, 0.0, 0.0, -1);
	else
		set_hudmessage(230, 60, 60, HUD_X, HUD_Y, 0, 0.0, HUD_HOLD, 0.0, 0.0, -1);

	ShowSyncHudMsg(id, g_sync, "%L", id, "EH_LINE", name, get_user_health(target), get_user_armor(target));
	g_shown[id] = true;
}

clear_line(id)
{
	if (!g_shown[id])
		return;

	ClearSyncHud(id, g_sync);
	g_shown[id] = false;
}

bool:is_teammate(id, other)
{
	if (g_freeForAllCvar && get_pcvar_num(g_freeForAllCvar))
		return false;

	return get_member(id, m_iTeam) == get_member(other, m_iTeam);
}

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.