Skip to content
View in the app

A better way to browse. Learn more.

Learn more

weapon-speed-hud/addons/amxmodx/scripting/olds_wspeed.sma

olds_wspeed.sma
169 lines · 4.9 KB · Pawn (AMX Mod X)
Raw
/*
 * Weapon Speed HUD (olds.ro workshop card LIB-2034)
 *
 * A small HUD line in the bottom left corner shows the highest running speed
 * the game allows the player right now: 250 with the knife, 210 with the AWP,
 * 150 with the AWP scoped, 1 during freeze time, and so on. The value is the
 * player's own maxspeed, read again at every update, so it is always the one
 * the game uses (also when another plugin changes it).
 *
 *   - The line is refreshed every olds_wspd_period seconds for every living
 *     player; dead players and spectators do not get it.
 *   - say /speedhud (or say_team /speedhud) hides or shows the line for that
 *     player until he leaves the server.
 *
 * Cvars (configs/plugins/olds_wspeed.cfg; AutoExecConfig writes that file
 * with these defaults if it is missing):
 *   olds_wspd_show   1    1 = show the speed line, 0 = off for everyone.
 *   olds_wspd_period 0.5  Seconds between two updates of the line (0.1 - 5).
 *
 * Every text players read comes from data/lang/olds_wspeed.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 <fun>
#include <reapi>

#pragma semicolon 1

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

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

const TASK_SHOW = 42340;

// Where the line is drawn: bottom left corner.
const Float:HUD_X = 0.02;
const Float:HUD_Y = 0.9;

// The old line fades out while the new one is drawn, so it never blinks.
const Float:HUD_FADE_OUT = 0.1;

new g_show;               // olds_wspd_show
new Float:g_period;       // olds_wspd_period

// The game's maxspeed of each player, read after the game set it.
new g_speed[MAX_PLAYERS + 1];

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

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

	bind_pcvar_num(create_cvar("olds_wspd_show", "1", FCVAR_NONE,
		"1 = show the speed line to living players, 0 = off for everyone",
		true, 0.0, true, 1.0), g_show);

	new period = create_cvar("olds_wspd_period", "0.5", FCVAR_NONE,
		"Seconds between two updates of the speed line (0.1 - 5)",
		true, 0.1, true, 5.0);
	bind_pcvar_float(period, g_period);
	hook_cvar_change(period, "OnPeriodChange");

	AutoExecConfig(true, "olds_wspeed");

	register_clcmd("say /speedhud", "cmd_speedhud");
	register_clcmd("say_team /speedhud", "cmd_speedhud");

	// The speed changes with the weapon in hand, the scope and freeze time;
	// the game sets it in ResetMaxSpeed and on every weapon change.
	register_event("CurWeapon", "OnCurWeapon", "be", "1=1");
	RegisterHookChain(RG_CBasePlayer_ResetMaxSpeed, "OnResetMaxSpeed_Post", true);

	start_updates(g_period);
}

public client_putinserver(id)
{
	g_hidden[id] = false;
	g_speed[id] = 0;
}

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

public OnCurWeapon(id)
{
	read_speed(id);
}

public OnResetMaxSpeed_Post(id)
{
	read_speed(id);
}

public cmd_speedhud(id)
{
	if (!g_show)
	{
		client_print_color(id, print_team_default, "^4*^1 %L", id, "OLDS_WSPD_DISABLED");
		return PLUGIN_HANDLED;
	}

	g_hidden[id] = !g_hidden[id];
	client_print_color(id, print_team_default, "^4*^1 %L", id, g_hidden[id] ? "OLDS_WSPD_OFF" : "OLDS_WSPD_ON");

	if (!g_hidden[id] && is_user_alive(id))
		show_speed(id);

	return PLUGIN_HANDLED;
}

public task_show()
{
	if (!g_show)
		return;

	for (new id = 1; id <= MaxClients; id++)
	{
		if (!g_hidden[id] && is_user_alive(id))
		{
			// Read again: another plugin may have changed the speed since.
			read_speed(id);
			show_speed(id);
		}
	}
}

start_updates(Float:period)
{
	remove_task(TASK_SHOW);
	set_task(floatmax(period, 0.1), "task_show", TASK_SHOW, _, _, "b");
}

read_speed(id)
{
	if (is_user_alive(id))
		g_speed[id] = floatround(get_user_maxspeed(id));
}

show_speed(id)
{
	// Each line stays until the next update; channel -1 lets AMX Mod X pick a
	// free HUD channel.
	set_hudmessage(255, 220, 120, HUD_X, HUD_Y, 0, 0.0, g_period, 0.0, HUD_FADE_OUT, -1);
	show_hudmessage(id, "%L", id, "OLDS_WSPD_HUD", g_speed[id]);
}

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.