Skip to content
View in the app

A better way to browse. Learn more.

Learn more

unstick-command/addons/amxmodx/scripting/olds_unstick.sma

olds_unstick.sma
600 lines · 15.3 KB · Pawn (AMX Mod X)
Raw
/*
 * Unstick Command (olds.ro workshop card LIB-2007)
 *
 * A player stuck inside a wall or inside another player types /stuck in
 * chat, stands still for olds_ustk_delay seconds and is moved to the nearest
 * free spot. Each player may do this olds_ustk_uses times per round. Admins
 * with ADMIN_KICK can free another player at once with amx_unstick.
 *
 *   say /stuck                       (also say_team /stuck)
 *   amx_unstick <name|#userid>       ADMIN_KICK
 *
 * "Stuck" means the player's hull (standing or crouched) overlaps the world
 * or another solid entity at the current position, checked with
 * EngFunc_TraceHull. The free spot is searched on rings around the player,
 * 8 units apart up to olds_ustk_radius, 16 directions per ring, each ring
 * turned a little further (a spiral), first at the same height and then one
 * ring higher. A player who is stuck only in another player is never moved
 * through a wall. When no spot is free and olds_ustk_spawn is 1, the player
 * goes to a free spawn point of their team.
 *
 * Cvars (configs/plugins/olds_unstick.cfg; AutoExecConfig writes that file
 * with these defaults if it is missing):
 *   olds_ustk_radius 48   Farthest distance searched for a free spot (16 - 256 units).
 *   olds_ustk_uses   2    /stuck moves per player per round (0 = no limit).
 *   olds_ustk_delay  3    Seconds the player must stand still first (0 - 30, 0 = at once).
 *   olds_ustk_spawn  1    1 = with no free spot nearby, move the player to a team spawn.
 *
 * Every text players read comes from data/lang/olds_unstick.txt (English).
 *
 * 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 <fakemeta>
#include <reapi>

#pragma semicolon 1

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

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

new const COMMAND_NAME[] = "amx_unstick";
new const COMMAND_USAGE[] = "<name|#userid>";

// Distance between two rings of the search, and directions tried on each ring.
const Float:RING_STEP = 8.0;
const DIRECTIONS = 16;

// How far a waiting player may drift before /stuck is cancelled, and how
// often that is checked.
const Float:MOVE_TOLERANCE = 8.0;
const Float:CHECK_INTERVAL = 0.2;

// Most spawn points of one team looked at.
const MAX_SPAWNS = 64;

const TASK_WAIT = 72070;

// Where unstick() put the player.
enum UnstickResult
{
	MOVED_NOWHERE,
	MOVED_NEAR,
	MOVED_TO_SPAWN
};

new Float:g_radius;  // olds_ustk_radius
new g_maxUses;       // olds_ustk_uses
new g_delay;         // olds_ustk_delay
new g_toSpawn;       // olds_ustk_spawn

new g_used[MAX_PLAYERS + 1];                // /stuck moves this round
new bool:g_waiting[MAX_PLAYERS + 1];        // standing still before the move
new Float:g_waitOrigin[MAX_PLAYERS + 1][3];
new Float:g_waitEnd[MAX_PLAYERS + 1];

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

	register_clcmd("say /stuck", "cmd_stuck");
	register_clcmd("say_team /stuck", "cmd_stuck");

	// The help line shown by amx_help comes from the dictionary.
	register_concmd(COMMAND_NAME, "cmd_unstick", ADMIN_KICK, "OLDS_USTK_HELP", -1, true);

	bind_pcvar_float(create_cvar("olds_ustk_radius", "48", FCVAR_NONE,
		"Farthest distance searched for a free spot, in units (16 - 256)",
		true, 16.0, true, 256.0), g_radius);

	bind_pcvar_num(create_cvar("olds_ustk_uses", "2", FCVAR_NONE,
		"How many times each player may use /stuck per round (0 = no limit)",
		true, 0.0, true, 100.0), g_maxUses);

	bind_pcvar_num(create_cvar("olds_ustk_delay", "3", FCVAR_NONE,
		"Seconds the player must stand still before being moved (0 = at once)",
		true, 0.0, true, 30.0), g_delay);

	bind_pcvar_num(create_cvar("olds_ustk_spawn", "1", FCVAR_NONE,
		"1 = with no free spot nearby, move the player to a spawn point of their team",
		true, 0.0, true, 1.0), g_toSpawn);

	AutoExecConfig(true, "olds_unstick");

	RegisterHookChain(RG_CSGameRules_RestartRound, "OnRestartRound_Pre", false);
}

public client_disconnected(id)
{
	stop_waiting(id);
	g_used[id] = 0;
}

/**
 * A new round: everyone gets their uses back, and nobody keeps waiting
 * across the respawn.
 */
public OnRestartRound_Pre()
{
	for (new id = 1; id <= MaxClients; id++)
	{
		stop_waiting(id);
		g_used[id] = 0;
	}
}

public cmd_stuck(id)
{
	if (!is_user_alive(id))
	{
		tell(id, "OLDS_USTK_NOT_ALIVE");
		return PLUGIN_HANDLED;
	}

	if (g_waiting[id])
	{
		tell(id, "OLDS_USTK_ALREADY");
		return PLUGIN_HANDLED;
	}

	if (!is_stuck(id))
	{
		tell(id, "OLDS_USTK_NOT_STUCK");
		return PLUGIN_HANDLED;
	}

	if (g_maxUses > 0 && g_used[id] >= g_maxUses)
	{
		client_print_color(id, print_team_default, "^4*^1 %L", id, "OLDS_USTK_NO_USES", g_maxUses);
		return PLUGIN_HANDLED;
	}

	if (g_delay <= 0)
	{
		self_unstick(id);
		return PLUGIN_HANDLED;
	}

	g_waiting[id] = true;
	get_entvar(id, var_origin, g_waitOrigin[id]);
	g_waitEnd[id] = get_gametime() + float(g_delay);

	client_print_color(id, print_team_default, "^4*^1 %L", id, "OLDS_USTK_WAIT", g_delay);
	set_task(CHECK_INTERVAL, "task_wait", TASK_WAIT + id, _, _, "b");

	return PLUGIN_HANDLED;
}

/**
 * Runs every CHECK_INTERVAL seconds while a player waits to be moved.
 */
public task_wait(taskId)
{
	new id = taskId - TASK_WAIT;

	if (!is_user_alive(id))
	{
		stop_waiting(id);
		return;
	}

	new Float:origin[3];
	get_entvar(id, var_origin, origin);

	if (get_distance_f(origin, g_waitOrigin[id]) > MOVE_TOLERANCE)
	{
		stop_waiting(id);
		tell(id, "OLDS_USTK_MOVED");
		return;
	}

	if (get_gametime() < g_waitEnd[id])
		return;

	stop_waiting(id);

	// The player got free while waiting: nothing to do, no use spent.
	if (!is_stuck(id))
	{
		tell(id, "OLDS_USTK_FREE_NOW");
		return;
	}

	self_unstick(id);
}

stop_waiting(id)
{
	g_waiting[id] = false;
	remove_task(TASK_WAIT + id);
}

/**
 * Moves a player who asked with /stuck, and spends one use when it worked.
 */
self_unstick(id)
{
	new UnstickResult:result = unstick(id);

	if (result == MOVED_NOWHERE)
	{
		tell(id, "OLDS_USTK_NO_SPOT");
		return;
	}

	g_used[id]++;

	new bool:near = result == MOVED_NEAR;

	if (g_maxUses > 0)
	{
		client_print_color(id, print_team_default, "^4*^1 %L", id,
			near ? "OLDS_USTK_FREED_LEFT" : "OLDS_USTK_SPAWN_LEFT", g_maxUses - g_used[id]);
	}
	else
	{
		tell(id, near ? "OLDS_USTK_FREED" : "OLDS_USTK_SPAWN");
	}
}

/**
 * amx_unstick <name|#userid>: an admin moves a stuck player at once, without
 * the wait and without spending the player's uses.
 */
public cmd_unstick(id, level, cid)
{
	if (!has_command_access(id, level))
	{
		console_print(id, "%L", id, "OLDS_USTK_NO_ACCESS", COMMAND_NAME);
		return PLUGIN_HANDLED;
	}

	if (read_argc() != 2)
	{
		console_print(id, "%L", id, "OLDS_USTK_USAGE", COMMAND_NAME, COMMAND_USAGE);
		return PLUGIN_HANDLED;
	}

	// One cell more than the longest name (31 characters), so a full-length
	// name still fits; an argument that fills the buffer was cut short.
	new targetArg[MAX_NAME_LENGTH + 1];

	if (read_argv(1, targetArg, charsmax(targetArg)) >= charsmax(targetArg))
	{
		console_print(id, "%L", id, "OLDS_USTK_NAME_TOO_LONG");
		return PLUGIN_HANDLED;
	}

	new target = find_target(id, targetArg);

	if (!target)
		return PLUGIN_HANDLED;

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

	if (!is_user_alive(target))
	{
		console_print(id, "%L", id, "OLDS_USTK_TARGET_DEAD", name);
		return PLUGIN_HANDLED;
	}

	if (!is_stuck(target))
	{
		console_print(id, "%L", id, "OLDS_USTK_TARGET_NOT_STUCK", name);
		return PLUGIN_HANDLED;
	}

	stop_waiting(target);

	switch (unstick(target))
	{
		case MOVED_NOWHERE:
		{
			console_print(id, "%L", id, "OLDS_USTK_TARGET_NO_SPOT", name);
			return PLUGIN_HANDLED;
		}
		case MOVED_NEAR:
		{
			console_print(id, "%L", id, "OLDS_USTK_TARGET_FREED", name);
		}
		case MOVED_TO_SPAWN:
		{
			console_print(id, "%L", id, "OLDS_USTK_TARGET_SPAWN", name);
		}
	}

	tell(target, "OLDS_USTK_BY_ADMIN");
	log_unstick(id, target);

	return PLUGIN_HANDLED;
}

/**
 * Moves the player to the nearest free spot, or to a free spawn point of
 * their team when olds_ustk_spawn is 1.
 */
UnstickResult:unstick(id)
{
	new Float:spot[3];

	if (find_free_spot(id, spot))
	{
		engfunc(EngFunc_SetOrigin, id, spot);
		set_entvar(id, var_velocity, Float:{0.0, 0.0, 0.0});
		return MOVED_NEAR;
	}

	new Float:angles[3];

	if (g_toSpawn && find_team_spawn(id, spot, angles))
	{
		engfunc(EngFunc_SetOrigin, id, spot);
		set_entvar(id, var_velocity, Float:{0.0, 0.0, 0.0});
		set_entvar(id, var_angles, angles);
		set_entvar(id, var_v_angle, angles);
		set_entvar(id, var_fixangle, 1);
		return MOVED_TO_SPAWN;
	}

	return MOVED_NOWHERE;
}

/**
 * The player's hull overlaps the world or another solid entity right where
 * the player is.
 */
bool:is_stuck(id)
{
	new Float:origin[3];
	get_entvar(id, var_origin, origin);

	return !hull_is_free(id, origin, DONT_IGNORE_MONSTERS);
}

bool:hull_is_free(id, const Float:origin[3], ignoreMonsters)
{
	engfunc(EngFunc_TraceHull, origin, origin, ignoreMonsters, player_hull(id), id, 0);

	return !get_tr2(0, TR_StartSolid) && !get_tr2(0, TR_AllSolid);
}

player_hull(id)
{
	return (get_entvar(id, var_flags) & FL_DUCKING) ? HULL_HEAD : HULL_HUMAN;
}

/**
 * Searches rings around the player, nearest first: on each ring 16
 * directions at the same height, then straight up and the 16 directions one
 * ring higher. Each ring starts a little further round than the one before,
 * so the points form a spiral.
 */
bool:find_free_spot(id, Float:spot[3])
{
	new Float:origin[3];
	get_entvar(id, var_origin, origin);

	// A player stuck only in another player stands in open space: the spot
	// must then be reachable from there without crossing a wall.
	new bool:inOpenSpace = hull_is_free(id, origin, IGNORE_MONSTERS);

	new rings = floatround(g_radius / RING_STEP, floatround_floor);
	new Float:turn = 360.0 / float(DIRECTIONS);

	for (new ring = 1; ring <= rings; ring++)
	{
		new Float:distance = RING_STEP * float(ring);
		new Float:start = turn * 0.5 * float(ring - 1);

		for (new level = 0; level <= 1; level++)
		{
			new Float:height = level ? distance : 0.0;

			// Straight up, before the raised directions.
			if (level)
			{
				spot[0] = origin[0];
				spot[1] = origin[1];
				spot[2] = origin[2] + height;

				if (spot_is_usable(id, origin, spot, inOpenSpace))
					return true;
			}

			for (new d = 0; d < DIRECTIONS; d++)
			{
				new Float:angle = start + turn * float(d);

				spot[0] = origin[0] + floatcos(angle, degrees) * distance;
				spot[1] = origin[1] + floatsin(angle, degrees) * distance;
				spot[2] = origin[2] + height;

				if (spot_is_usable(id, origin, spot, inOpenSpace))
					return true;
			}
		}
	}

	return false;
}

bool:spot_is_usable(id, const Float:origin[3], const Float:spot[3], bool:inOpenSpace)
{
	if (!hull_is_free(id, spot, DONT_IGNORE_MONSTERS))
		return false;

	if (!inOpenSpace)
		return true;

	// The whole hull must pass from the player to the spot without touching
	// the world (other players are ignored here; the spot itself is free).
	engfunc(EngFunc_TraceHull, origin, spot, IGNORE_MONSTERS, player_hull(id), id, 0);

	new Float:fraction;
	get_tr2(0, TR_flFraction, fraction);

	return fraction >= 1.0 && !get_tr2(0, TR_StartSolid);
}

/**
 * A random free spawn point of the player's team (info_player_deathmatch for
 * the Terrorists, info_player_start for the Counter-Terrorists).
 */
bool:find_team_spawn(id, Float:spot[3], Float:angles[3])
{
	new classname[24];

	switch (get_member(id, m_iTeam))
	{
		case TEAM_TERRORIST: copy(classname, charsmax(classname), "info_player_deathmatch");
		case TEAM_CT: copy(classname, charsmax(classname), "info_player_start");
		default: return false;
	}

	new spawns[MAX_SPAWNS], count = 0;
	new ent = -1;
	new Float:origin[3];

	while (count < MAX_SPAWNS && (ent = rg_find_ent_by_class(ent, classname, true)) > 0)
	{
		get_entvar(ent, var_origin, origin);

		// The game puts a spawning player one unit above the spawn point.
		origin[2] += 1.0;

		if (hull_is_free(id, origin, DONT_IGNORE_MONSTERS))
			spawns[count++] = ent;
	}

	if (!count)
		return false;

	ent = spawns[random(count)];
	get_entvar(ent, var_origin, spot);
	spot[2] += 1.0;
	get_entvar(ent, var_angles, angles);

	return true;
}

tell(id, const key[])
{
	client_print_color(id, print_team_default, "^4*^1 %L", id, key);
}

/**
 * Whether id may run a command registered with this access level: the server
 * console and the host of a listen server always may; ADMIN_ALL is open to
 * everyone; ADMIN_ADMIN needs any admin flag; any other level needs one of
 * its flags. (amxmisc's cmd_access prints through another dictionary, so the
 * plugin checks the access itself to keep every text in its own file.)
 */
bool:has_command_access(id, level)
{
	if (id == 0 || (id == 1 && !is_dedicated_server()))
		return true;

	if (level == ADMIN_ALL)
		return true;

	if (level == ADMIN_ADMIN)
		return is_user_admin(id) != 0;

	return (get_user_flags(id) & level) != 0;
}

/**
 * The player named by arg: #userid, an exact name, or a unique part of a
 * name. Prints why when there is none. HLTV proxies are left out.
 */
find_target(id, const arg[])
{
	new players[MAX_PLAYERS], count;
	get_players_ex(players, count, GetPlayers_ExcludeHLTV);

	if (arg[0] == '#' && arg[1] != EOS)
	{
		if (!is_str_num(arg[1]))
		{
			console_print(id, "%L", id, "OLDS_USTK_NOT_USERID", arg);
			return 0;
		}

		new userid = str_to_num(arg[1]);

		for (new i = 0; i < count; i++)
		{
			if (get_user_userid(players[i]) == userid)
				return players[i];
		}

		console_print(id, "%L", id, "OLDS_USTK_NO_USERID", arg);
		return 0;
	}

	new name[MAX_NAME_LENGTH];
	new match = 0;
	new matches = 0;

	for (new i = 0; i < count; i++)
	{
		get_user_name(players[i], name, charsmax(name));

		// An exact name wins even when it is also part of other names.
		if (equali(name, arg))
			return players[i];

		if (containi(name, arg) != -1)
		{
			match = players[i];
			matches++;
		}
	}

	if (matches == 1)
		return match;

	console_print(id, "%L", id, matches ? "OLDS_USTK_MANY_MATCH" : "OLDS_USTK_NO_MATCH", arg);
	return 0;
}

log_unstick(admin, target)
{
	new targetName[MAX_NAME_LENGTH], targetAuth[MAX_AUTHID_LENGTH];
	get_user_name(target, targetName, charsmax(targetName));
	get_user_authid(target, targetAuth, charsmax(targetAuth));

	if (!admin)
	{
		log_amx("Cmd: Server unstuck ^"%s<%d><%s><>^"", targetName, get_user_userid(target), targetAuth);
		return;
	}

	new adminName[MAX_NAME_LENGTH], adminAuth[MAX_AUTHID_LENGTH];
	get_user_name(admin, adminName, charsmax(adminName));
	get_user_authid(admin, adminAuth, charsmax(adminAuth));

	log_amx("Cmd: ^"%s<%d><%s><>^" unstuck ^"%s<%d><%s><>^"", adminName, get_user_userid(admin),
		adminAuth, targetName, get_user_userid(target), targetAuth);
}

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.