Skip to content
View in the app

A better way to browse. Learn more.

Learn more

welcome-hud-text-on-join/addons/amxmodx/scripting/olds_welcome.sma

olds_welcome.sma
309 lines · 8.5 KB · Pawn (AMX Mod X)
Raw
/*
 * Welcome HUD Text on Join (olds.ro workshop card LIB-2042)
 *
 * Shows a welcome message in the middle of the screen when a player joins:
 * 3 seconds after entering the server, or at the player's first spawn if
 * they are still choosing a team then, so it never hides behind the team
 * menu.
 *
 * The text comes from addons/amxmodx/configs/olds_welcome.txt, one HUD line
 * per line (lines starting with ; or // are comments). These tokens are
 * replaced:
 *   %name%         the player's name
 *   %map%          the current map
 *   %players%      players in the server now, the new one included
 *   %maxplayers%   the server's player slots
 * If the file is missing or empty, a default text from the dictionary is
 * used and the server console says so.
 *
 * Cvars (configs/plugins/olds_welcome.cfg; AutoExecConfig writes that file
 * with these defaults if it is missing):
 *   olds_welc_time  6           Seconds the message stays on the screen (1 - 30).
 *   olds_welc_color "0 160 255" Text colour, red green blue (0 - 255 each).
 *   olds_welc_once  1           1 = once per map for each player (a player who
 *                               reconnects during the same map does not get it
 *                               again), 0 = again at every spawn.
 *
 * Requires AMX Mod X 1.10 or newer and ReAPI with ReGameDLL_CS. Written and
 * tested on 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 <amxmisc>
#include <reapi>

#pragma semicolon 1

new const PLUGIN_NAME[] = "Welcome HUD Text on Join";
new const PLUGIN_VERSION[] = "1.0.0";
new const PLUGIN_AUTHOR[] = "alexit (olds.ro)";

new const DICTIONARY[] = "olds_welcome.txt";
new const TEXT_FILE[] = "olds_welcome.txt";

// Seconds between entering the server and the message.
const Float:JOIN_DELAY = 3.0;

// Seconds between a spawn and the message, after the game's own HUD reset.
const Float:SPAWN_DELAY = 0.5;

// Longest text sent; a HUD message holds a little under 512 characters.
const MAX_TEXT = 400;

const TASK_JOIN = 42420;
const TASK_SPAWN = 42460;

new g_holdTime;              // olds_welc_time
new g_color[16];             // olds_welc_color
new g_once;                  // olds_welc_once

new g_template[MAX_TEXT + 1];

// Players welcomed during this map, by Steam ID (only unique IDs are kept).
new Trie:g_welcomedIds;

new bool:g_delayDone[MAX_PLAYERS + 1];   // the 3 seconds after joining are over
new bool:g_shown[MAX_PLAYERS + 1];       // welcomed during this connection or map

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

	bind_pcvar_num(create_cvar("olds_welc_time", "6", FCVAR_NONE,
		"Seconds the welcome message stays on the screen (1 - 30)",
		true, 1.0, true, 30.0), g_holdTime);

	bind_pcvar_string(create_cvar("olds_welc_color", "0 160 255", FCVAR_NONE,
		"Colour of the welcome text: red green blue, 0 - 255 each"), g_color, charsmax(g_color));

	bind_pcvar_num(create_cvar("olds_welc_once", "1", FCVAR_NONE,
		"1 = once per map for each player, 0 = again at every spawn",
		true, 0.0, true, 1.0), g_once);

	AutoExecConfig(true, "olds_welcome");

	RegisterHookChain(RG_CBasePlayer_Spawn, "OnPlayerSpawn_Post", true);

	g_welcomedIds = TrieCreate();
}

public plugin_cfg()
{
	load_template();
}

public plugin_end()
{
	TrieDestroy(g_welcomedIds);
}

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

	if (is_user_hltv(id))
		return;

	// Already welcomed earlier in this map under the same Steam ID.
	new authId[MAX_AUTHID_LENGTH];

	if (g_once && unique_auth_id(id, authId, charsmax(authId)) && TrieKeyExists(g_welcomedIds, authId))
		g_shown[id] = true;

	set_task(JOIN_DELAY, "task_join_delay", TASK_JOIN + id);
}

public client_disconnected(id)
{
	remove_task(TASK_JOIN + id);
	remove_task(TASK_SPAWN + id);
	g_delayDone[id] = false;
	g_shown[id] = false;
}

public task_join_delay(taskId)
{
	new id = taskId - TASK_JOIN;
	g_delayDone[id] = true;

	// Still in the team menu (not spawned yet): the first spawn shows it.
	if (is_user_alive(id) && !g_shown[id])
		show_welcome(id);
}

public OnPlayerSpawn_Post(id)
{
	if (!is_user_alive(id) || !g_delayDone[id])
		return;

	if (!g_shown[id] || !g_once)
	{
		remove_task(TASK_SPAWN + id);
		set_task(SPAWN_DELAY, "task_spawn", TASK_SPAWN + id);
	}
}

public task_spawn(taskId)
{
	new id = taskId - TASK_SPAWN;

	if (is_user_alive(id))
		show_welcome(id);
}

show_welcome(id)
{
	new text[MAX_TEXT + 1];
	build_text(id, text, charsmax(text));

	new red, green, blue;
	parse_color(red, green, blue);

	set_hudmessage(red, green, blue, -1.0, 0.25, 0, 0.0, float(g_holdTime), 0.5, 1.0, -1);

	// The text goes in as an argument, never as the format, so a % in a name
	// or in the file is printed as it is.
	show_hudmessage(id, "%s", text);

	g_shown[id] = true;

	new authId[MAX_AUTHID_LENGTH];

	if (unique_auth_id(id, authId, charsmax(authId)))
		TrieSetCell(g_welcomedIds, authId, 1);
}

/**
 * The template with its tokens replaced. The name goes in last, so a name
 * that contains a token such as %map% is shown as typed.
 */
build_text(id, text[], len)
{
	copy(text, len, g_template);

	new number[12];
	new mapName[32];
	get_mapname(mapName, charsmax(mapName));
	replace_string(text, len, "%map%", mapName, false);

	num_to_str(get_playersnum(), number, charsmax(number));
	replace_string(text, len, "%players%", number, false);

	num_to_str(MaxClients, number, charsmax(number));
	replace_string(text, len, "%maxplayers%", number, false);

	new name[MAX_NAME_LENGTH];
	get_user_name(id, name, charsmax(name));
	replace_string(text, len, "%name%", name, false);
}

/**
 * Reads configs/olds_welcome.txt: comment lines (; or //) are skipped, the
 * other lines become HUD lines. Falls back to the dictionary's default text.
 */
load_template()
{
	g_template[0] = EOS;

	new path[PLATFORM_MAX_PATH];
	get_configsdir(path, charsmax(path));
	format(path, charsmax(path), "%s/%s", path, TEXT_FILE);

	new file = fopen(path, "rt");

	if (file)
	{
		new line[256];
		new length = 0;
		new pendingBreaks = 0;

		while (!feof(file) && fgets(file, line, charsmax(line)))
		{
			// Strip the line end and trailing spaces, keep leading spaces.
			new end = strlen(line);

			while (end > 0 && (line[end - 1] == '^n' || line[end - 1] == '^r' || line[end - 1] == ' ' || line[end - 1] == '^t'))
				line[--end] = EOS;

			if (line[0] == ';' || (line[0] == '/' && line[1] == '/'))
				continue;

			// Empty lines are kept between text lines, never at the start or end.
			if (!line[0])
			{
				if (length)
					pendingBreaks++;

				continue;
			}

			if (length)
				pendingBreaks++;

			for (; pendingBreaks > 0 && length < MAX_TEXT; pendingBreaks--)
				g_template[length++] = '^n';

			length += copy(g_template[length], MAX_TEXT - length, line);
			pendingBreaks = 0;
		}

		g_template[length] = EOS;
		fclose(file);
	}

	if (!g_template[0])
	{
		// The default text is read as it is (no formatting), so its %tokens% stay.
		new serverLang = LANG_SERVER;
		LookupLangKey(g_template, charsmax(g_template), "OLDS_WELC_DEFAULT", serverLang);
		server_print("[%s] %L", PLUGIN_NAME, LANG_SERVER, "OLDS_WELC_NO_FILE", path);
	}
}

/**
 * olds_welc_color "R G B"; missing or bad numbers count as 0, and every
 * number is kept within 0 - 255.
 */
parse_color(&red, &green, &blue)
{
	new r[8], g[8], b[8];
	parse(g_color, r, charsmax(r), g, charsmax(g), b, charsmax(b));

	red = clamp(str_to_num(r), 0, 255);
	green = clamp(str_to_num(g), 0, 255);
	blue = clamp(str_to_num(b), 0, 255);
}

/**
 * The player's Steam ID when it tells players apart. Bots, LAN and pending
 * IDs are shared by many players, so those are tracked per connection only.
 */
bool:unique_auth_id(id, authId[], len)
{
	get_user_authid(id, authId, len);

	if (is_user_bot(id))
		return false;

	if (equal(authId, "STEAM_ID_LAN") || equal(authId, "STEAM_ID_PENDING")
		|| equal(authId, "VALVE_ID_LAN") || equal(authId, "VALVE_ID_PENDING")
		|| equal(authId, "BOT") || equal(authId, "HLTV") || !authId[0])
		return false;

	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.