Skip to content
View in the app

A better way to browse. Learn more.

Learn more

pressed-keys-display/addons/amxmodx/scripting/olds_keys.sma

olds_keys.sma
290 lines · 8 KB · Pawn (AMX Mod X)
Raw
/*
 * Pressed Keys Display (olds.ro workshop card LIB-2067)
 *
 * A spectator sees which movement and action keys the player he watches is
 * pressing, as a short HUD line such as "W A  JUMP  DUCK". The line follows
 * the spectator's target, shows "-" while no key is pressed, and disappears
 * when the camera stops following a player or the watched player dies.
 *
 *   - The keys are read from every living player's pev_button each frame
 *     (client_PreThink); a key tapped between two updates is still shown.
 *   - The line is sent only when its text or the target changes, plus once
 *     a second to keep it on the screen, so it never flickers.
 *   - say /keys (or say_team /keys) turns the line off or on for that
 *     player until he leaves the server.
 *
 * Cvars (configs/plugins/olds_keys.cfg; AutoExecConfig writes that file
 * with these defaults if it is missing):
 *   olds_keys_pos  "0.45 0.9"  HUD position "x y" (-1 = centred).
 *   olds_keys_self 0           1 = living players also see their own keys.
 *   olds_keys_rate 0.1         Seconds between two checks (0.05 - 1).
 *
 * Every text players read comes from data/lang/olds_keys.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[] = "Pressed Keys Display";
new const PLUGIN_VERSION[] = "1.0.0";
new const PLUGIN_AUTHOR[] = "alexit (olds.ro)";

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

const TASK_UPDATE = 42670;
const HUD_CHANNEL = 2;

// The line is sent again at least this often (seconds), and stays a little longer.
const Float:KEEP_ALIVE = 1.0;
const Float:HOLD_TIME = 1.5;

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

// The keys shown, in this order, with their dictionary labels.
new const KEY_BUTTONS[] = { IN_FORWARD, IN_MOVELEFT, IN_BACK, IN_MOVERIGHT, IN_JUMP, IN_DUCK, IN_RUN, IN_USE, IN_RELOAD, IN_ATTACK, IN_ATTACK2 };
new const KEY_LABELS[][] =
{
	"OLDS_KEYS_W", "OLDS_KEYS_A", "OLDS_KEYS_S", "OLDS_KEYS_D",
	"OLDS_KEYS_JUMP", "OLDS_KEYS_DUCK", "OLDS_KEYS_WALK", "OLDS_KEYS_USE",
	"OLDS_KEYS_RELOAD", "OLDS_KEYS_FIRE", "OLDS_KEYS_ALT"
};

// The four movement keys are separated by one space, the others by two.
const MOVE_KEYS = 4;

const MAX_TEXT = 96;

new Float:g_posX = 0.45;    // olds_keys_pos
new Float:g_posY = 0.9;
new g_self;                 // olds_keys_self
new Float:g_rate;           // olds_keys_rate

// Keys pressed by each living player since the last update.
new g_pressed[MAX_PLAYERS + 1];

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

// What each viewer has on his screen now: the target (0 = nothing) and the text.
new g_shownTarget[MAX_PLAYERS + 1];
new g_shownText[MAX_PLAYERS + 1][MAX_TEXT];
new Float:g_shownAt[MAX_PLAYERS + 1];

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

	new position = create_cvar("olds_keys_pos", "0.45 0.9", FCVAR_NONE,
		"HUD position of the keys line: x y (-1 = centred)");
	hook_cvar_change(position, "OnPositionChange");

	bind_pcvar_num(create_cvar("olds_keys_self", "0", FCVAR_NONE,
		"1 = living players also see their own keys, 0 = spectators only",
		true, 0.0, true, 1.0), g_self);

	new rate = create_cvar("olds_keys_rate", "0.1", FCVAR_NONE,
		"Seconds between two checks of the keys (0.05 - 1)",
		true, 0.05, true, 1.0);
	bind_pcvar_float(rate, g_rate);
	hook_cvar_change(rate, "OnRateChange");

	AutoExecConfig(true, "olds_keys");

	register_clcmd("say /keys", "cmd_keys");
	register_clcmd("say_team /keys", "cmd_keys");

	new value[32];
	get_pcvar_string(position, value, charsmax(value));
	read_position(value);

	start_updates(g_rate);
}

public OnPositionChange(pcvar, const oldValue[], const newValue[])
{
	read_position(newValue);

	// Draw every line again at the new place.
	for (new id = 1; id <= MaxClients; id++)
		g_shownText[id][0] = EOS;
}

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

public client_putinserver(id)
{
	g_pressed[id] = 0;
	g_off[id] = false;
	g_shownTarget[id] = 0;
	g_shownText[id][0] = EOS;
}

public client_PreThink(id)
{
	if (is_user_alive(id))
		g_pressed[id] |= get_entvar(id, var_button);
}

public cmd_keys(id)
{
	g_off[id] = !g_off[id];
	client_print_color(id, print_team_default, "^4*^1 %L", id, g_off[id] ? "OLDS_KEYS_OFF" : "OLDS_KEYS_ON");

	if (g_off[id])
		clear_line(id);
	else
		g_shownText[id][0] = EOS;

	return PLUGIN_HANDLED;
}

public task_update()
{
	new Float:now = get_gametime();
	new text[MAX_TEXT];

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

		new target = g_off[viewer] ? 0 : watched_player(viewer);

		if (!target)
		{
			if (g_shownTarget[viewer])
				clear_line(viewer);

			continue;
		}

		keys_text(viewer, target, text, charsmax(text));

		if (target == g_shownTarget[viewer] && equal(text, g_shownText[viewer]) && now - g_shownAt[viewer] < KEEP_ALIVE)
			continue;

		set_hudmessage(255, 255, 255, g_posX, g_posY, 0, 0.0, HOLD_TIME, 0.0, 0.0, HUD_CHANNEL);
		show_hudmessage(viewer, "%s", text);

		g_shownTarget[viewer] = target;
		copy(g_shownText[viewer], charsmax(g_shownText[]), text);
		g_shownAt[viewer] = now;
	}

	// Start collecting the keys of the next interval.
	for (new id = 1; id <= MaxClients; id++)
		g_pressed[id] = is_user_alive(id) ? get_entvar(id, var_button) : 0;
}

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

/**
 * The living player whose keys the viewer sees: the one his camera follows,
 * or himself with olds_keys_self 1. 0 = none.
 */
watched_player(viewer)
{
	if (is_user_alive(viewer))
		return g_self ? viewer : 0;

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

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

	return 0;
}

keys_text(viewer, target, text[], len)
{
	new buttons = g_pressed[target];
	new length = 0;

	text[0] = EOS;

	for (new i = 0; i < sizeof(KEY_BUTTONS); i++)
	{
		if (!(buttons & KEY_BUTTONS[i]))
			continue;

		if (length)
		{
			if (i < MOVE_KEYS)
				length += copy(text[length], len - length, " ");
			else
				length += copy(text[length], len - length, "  ");
		}

		length += formatex(text[length], len - length, "%L", viewer, KEY_LABELS[i]);
	}

	if (!length)
		formatex(text, len, "%L", viewer, "OLDS_KEYS_NONE");
}

/**
 * "0.45 0.9" -> the HUD position; a missing or bad number keeps the default.
 */
read_position(const value[])
{
	new x[16], y[16];
	parse(value, x, charsmax(x), y, charsmax(y));

	g_posX = x[0] ? keep_on_screen(str_to_float(x)) : 0.45;
	g_posY = y[0] ? keep_on_screen(str_to_float(y)) : 0.9;
}

/**
 * HUD coordinates go from 0 to 1, and -1 centres the text.
 */
Float:keep_on_screen(Float:value)
{
	if (value < 0.0)
		return -1.0;

	return floatmin(value, 1.0);
}

clear_line(viewer)
{
	set_hudmessage(0, 0, 0, g_posX, g_posY, 0, 0.0, 0.1, 0.0, 0.0, HUD_CHANNEL);
	show_hudmessage(viewer, " ");
	g_shownTarget[viewer] = 0;
	g_shownText[viewer][0] = 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.