Skip to content
View in the app

A better way to browse. Learn more.

Learn more

custom-hud-message-command/addons/amxmodx/scripting/olds_hudmsg.sma

olds_hudmsg.sma
420 lines · 9.5 KB · Pawn (AMX Mod X)
Raw
/*
 * Custom HUD Message Command (olds.ro workshop card LIB-1525)
 *
 * Lets an admin put a text on the screen of everyone, one team or one
 * player, at a chosen place, in a chosen colour, for a chosen time.
 *
 * Admin command (ADMIN_CHAT, flag "i"; also from the server console):
 *   amx_hudmsg <target> <x> <y> <r> <g> <b> <seconds> <text>
 *     target   @all, @t, @ct, a player's name (or part of it) or #userid
 *     x, y     0.0 (left/top) to 1.0 (right/bottom); -1 = centred.
 *              Other values are clamped to 0.0 - 1.0.
 *     r g b    colour, 0 to 255 each (higher values are clamped to 255)
 *     seconds  how long the text stays, 0.5 to 60 (clamped)
 *     text     the rest of the line; \n starts a new line
 *   Example: amx_hudmsg @all -1 0.3 0 255 0 8 Welcome to olds.ro
 *
 * Every message is written to the AMXX log with the admin's name.
 *
 * Cvars (saved with their descriptions in configs/plugins/olds_hudmsg.cfg the
 * first time the plugin starts):
 *   olds_hudmsg_channel 3    HUD channel (1-4); another plugin's text on
 *                            the same channel is replaced.
 *   olds_hudmsg_maxlen  120  Longest text in bytes; a longer one is cut.
 *
 * Requires AMX Mod X 1.10 or newer. No extra modules; ReAPI is not used.
 *
 * 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 <amxmisc>

#pragma semicolon 1

new const PLUGIN_NAME[] = "Custom HUD Message Command";
new const PLUGIN_VERSION[] = "1.0.0";
new const PLUGIN_AUTHOR[] = "alexit (olds.ro)";

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

// Highest value olds_hudmsg_maxlen can take.
const MAX_TEXT = 250;

const Float:CENTRE = -1.0;
const Float:MIN_HOLD = 0.5;
const Float:MAX_HOLD = 60.0;

new g_channel;      // olds_hudmsg_channel
new g_maxLength;    // olds_hudmsg_maxlen

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

	register_concmd("amx_hudmsg", "cmd_hudmsg", ADMIN_CHAT, "OLDS_HUDMSG_HELP", -1, true);

	bind_pcvar_num(create_cvar("olds_hudmsg_channel", "3", FCVAR_NONE,
		"HUD channel (1-4) used by amx_hudmsg",
		true, 1.0, true, 4.0), g_channel);

	bind_pcvar_num(create_cvar("olds_hudmsg_maxlen", "120", FCVAR_NONE,
		"Longest amx_hudmsg text in bytes; a longer text is cut",
		true, 1.0, true, float(MAX_TEXT)), g_maxLength);

	AutoExecConfig(true, "olds_hudmsg");
}

public cmd_hudmsg(id, level, cid)
{
	if (!admin_allowed(id, level))
		return PLUGIN_HANDLED;

	if (read_argc() < 9)
	{
		console_print(id, "%L", id, "OLDS_HUDMSG_USAGE");
		return PLUGIN_HANDLED;
	}

	new bool:clamped;
	new Float:x, Float:y;

	if (!read_position(2, x, clamped) || !read_position(3, y, clamped))
	{
		console_print(id, "%L", id, "OLDS_HUDMSG_BAD_POSITION");
		return PLUGIN_HANDLED;
	}

	new color[3];

	for (new i = 0; i < 3; i++)
	{
		if (!read_color(4 + i, color[i], clamped))
		{
			console_print(id, "%L", id, "OLDS_HUDMSG_BAD_COLOR");
			return PLUGIN_HANDLED;
		}
	}

	new Float:hold;

	if (!read_hold(7, hold, clamped))
	{
		console_print(id, "%L", id, "OLDS_HUDMSG_BAD_SECONDS");
		return PLUGIN_HANDLED;
	}

	new text[MAX_TEXT + 1];
	read_text(8, text, charsmax(text));

	if (!text[0])
	{
		console_print(id, "%L", id, "OLDS_HUDMSG_USAGE");
		return PLUGIN_HANDLED;
	}

	if (strlen(text) > g_maxLength)
	{
		utf8_cut(text, g_maxLength);
		console_print(id, "%L", id, "OLDS_HUDMSG_CUT", g_maxLength);
	}

	new target[MAX_NAME_LENGTH], targetName[MAX_NAME_LENGTH];
	new players[MAX_PLAYERS], count;
	read_argv(1, target, charsmax(target));

	if (!get_recipients(id, target, players, count, targetName, charsmax(targetName)))
		return PLUGIN_HANDLED;

	// "\n" in the command becomes a line break on screen.
	new shown[MAX_TEXT + 1];
	copy(shown, charsmax(shown), text);
	replace_string(shown, charsmax(shown), "\n", "^n");

	set_hudmessage(color[0], color[1], color[2], x, y, 0, 6.0, hold, 0.1, 0.2, g_channel);

	for (new i = 0; i < count; i++)
		show_hudmessage(players[i], "%s", shown);

	if (clamped)
		console_print(id, "%L", id, "OLDS_HUDMSG_CLAMPED");

	console_print(id, "%L", id, "OLDS_HUDMSG_SENT", targetName, count, x, y, color[0], color[1], color[2], hold);

	new who[96];
	describe_admin(id, who, charsmax(who));
	log_amx("Cmd: %s HUD message to %s (%d players) at %.2f %.2f, colour %d %d %d, %.1f s: %s",
		who, targetName, count, x, y, color[0], color[1], color[2], hold, text);

	return PLUGIN_HANDLED;
}

/**
 * Fills players[] with the receivers: @all, @t, @ct, #userid, an exact
 * name, or the one player whose name contains the text. Prints why and
 * returns false when nobody matches.
 */
bool:get_recipients(id, const target[], players[MAX_PLAYERS], &count, name[], maxLength)
{
	count = 0;

	if (equali(target, "@all"))
	{
		get_players(players, count);
		copy(name, maxLength, "@all");
		return true;
	}

	if (equali(target, "@t"))
	{
		get_players(players, count, "e", "TERRORIST");
		copy(name, maxLength, "@T");
		return true;
	}

	if (equali(target, "@ct"))
	{
		get_players(players, count, "e", "CT");
		copy(name, maxLength, "@CT");
		return true;
	}

	new player, matches;
	new userId = (target[0] == '#' && is_str_num(target[1])) ? str_to_num(target[1]) : 0;
	new playerName[MAX_NAME_LENGTH];

	for (new i = 1; i <= MaxClients; i++)
	{
		if (!is_user_connected(i))
			continue;

		get_user_name(i, playerName, charsmax(playerName));

		if ((userId && get_user_userid(i) == userId) || equali(playerName, target))
		{
			player = i;
			matches = 1;
			break;
		}

		if (!userId && containi(playerName, target) != -1)
		{
			player = i;
			matches++;
		}
	}

	if (!matches)
	{
		console_print(id, "%L", id, "OLDS_HUDMSG_NO_TARGET", target);
		return false;
	}

	if (matches > 1)
	{
		console_print(id, "%L", id, "OLDS_HUDMSG_MANY_TARGETS", target);
		return false;
	}

	players[0] = player;
	count = 1;
	get_user_name(player, name, maxLength);
	return true;
}

/**
 * x or y: -1 = centred, anything else is clamped to 0.0 - 1.0.
 */
bool:read_position(arg, &Float:value, &bool:clamped)
{
	new text[16];
	read_argv(arg, text, charsmax(text));

	if (!is_decimal(text))
		return false;

	value = str_to_float(text);

	if (value == CENTRE)
		return true;

	if (value < 0.0)
	{
		value = 0.0;
		clamped = true;
	}
	else if (value > 1.0)
	{
		value = 1.0;
		clamped = true;
	}

	return true;
}

bool:read_color(arg, &value, &bool:clamped)
{
	new text[16];
	read_argv(arg, text, charsmax(text));

	if (!is_str_num(text))
		return false;

	value = str_to_num(text);

	if (value > 255)
	{
		value = 255;
		clamped = true;
	}

	return true;
}

bool:read_hold(arg, &Float:value, &bool:clamped)
{
	new text[16];
	read_argv(arg, text, charsmax(text));

	if (!is_decimal(text))
		return false;

	value = str_to_float(text);

	if (value < MIN_HOLD)
	{
		value = MIN_HOLD;
		clamped = true;
	}
	else if (value > MAX_HOLD)
	{
		value = MAX_HOLD;
		clamped = true;
	}

	return true;
}

/**
 * The text is every argument from firstArg on, joined with spaces; one
 * quoted argument is used as it is.
 */
read_text(firstArg, text[], maxLength)
{
	new count = read_argc();
	new part[MAX_TEXT + 1], length;
	text[0] = EOS;

	for (new i = firstArg; i < count && length < maxLength; i++)
	{
		read_argv(i, part, charsmax(part));

		if (length)
			length += copy(text[length], maxLength - length, " ");

		length += copy(text[length], maxLength - length, part);
	}

	trim(text);
}

/**
 * A number with an optional leading minus and at most one decimal point.
 */
bool:is_decimal(const text[])
{
	new digits, dots;

	for (new i = (text[0] == '-') ? 1 : 0; text[i]; i++)
	{
		if (text[i] == '.')
		{
			if (++dots > 1)
				return false;
		}
		else if (isdigit(text[i]))
		{
			digits++;
		}
		else
		{
			return false;
		}
	}

	return digits > 0;
}

/**
 * The same rules as cmd_access: the server console (and the host of a listen
 * server) may always; ADMIN_ALL = everyone; ADMIN_ADMIN = any admin flag.
 * The level can be changed in configs/cmdaccess.ini.
 */
bool:admin_allowed(id, level)
{
	if (id == (is_dedicated_server() ? 0 : 1) || level == ADMIN_ALL)
		return true;

	if ((level == ADMIN_ADMIN && is_user_admin(id)) || (get_user_flags(id) & level))
		return true;

	console_print(id, "%L", id, "OLDS_HUDMSG_NO_ACCESS");
	return false;
}

describe_admin(id, out[], maxLength)
{
	if (!id)
	{
		copy(out, maxLength, "Server");
		return;
	}

	new name[MAX_NAME_LENGTH], authid[44];
	get_user_name(id, name, charsmax(name));
	get_user_authid(id, authid, charsmax(authid));
	formatex(out, maxLength, "^"%s<%d><%s><>^"", name, get_user_userid(id), authid);
}

/**
 * Shortens text to at most maxBytes bytes without leaving half of a UTF-8
 * character at the end.
 */
utf8_cut(text[], maxBytes)
{
	if (strlen(text) <= maxBytes)
		return;

	text[maxBytes] = EOS;

	// Find where the last character starts: continuation bytes are 10xxxxxx.
	new start = maxBytes - 1;

	while (start > 0 && (text[start] & 0xC0) == 0x80)
		start--;

	new lead = text[start] & 0xFF;
	new needed = 1;

	if (lead >= 0xF0)
		needed = 4;
	else if (lead >= 0xE0)
		needed = 3;
	else if (lead >= 0xC0)
		needed = 2;

	if (maxBytes - start < needed)
		text[start] = EOS;
}

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.