Skip to content
View in the app

A better way to browse. Learn more.

Learn more

custom-gravity-control/addons/amxmodx/scripting/olds_gravity.sma

olds_gravity.sma
429 lines · 10.6 KB · Pawn (AMX Mod X)
Raw
/*
 * Custom Gravity Control (olds.ro workshop card LIB-1523)
 *
 * Admins set the gravity of everyone, one team or one player; players can
 * switch low gravity on and off for themselves with /lowgrav. Every value is
 * kept inside a safe range.
 *
 * Gravity is a multiplier of sv_gravity: 1.0 = normal, 0.5 = half (players
 * jump about twice as high), 2.0 = double.
 *
 * Admin command (ADMIN_LEVEL_A, flag "m"; also from the server console):
 *   amx_gravity <@all|@t|@ct|player> <value>
 *   Example: amx_gravity @all 0.5
 *   A value outside olds_grav_min - olds_grav_max is clamped, and the admin
 *   is told so. Players who join later start with normal gravity.
 *
 * Chat command (chat and team chat), when olds_grav_player is 1:
 *   /lowgrav   switches the player's own gravity between olds_grav_low and
 *              normal
 *
 * The gravity a player was given is set again after every respawn. With
 * olds_grav_persist 0 every player is back to normal at each round restart.
 *
 * Server command:
 *   olds_grav_status   prints every player's gravity now and the gravity
 *                      the plugin keeps for them
 *
 * Cvars (saved with their descriptions in configs/plugins/olds_gravity.cfg
 * the first time the plugin starts):
 *   olds_grav_min     0.2   Lowest gravity allowed.
 *   olds_grav_max     2.0   Highest gravity allowed.
 *   olds_grav_player  1     1 = players may use /lowgrav, 0 = only admins.
 *   olds_grav_low     0.5   Gravity /lowgrav gives.
 *   olds_grav_persist 1     1 = keep the gravity over round restarts,
 *                           0 = back to normal at every round restart.
 *
 * Requires AMX Mod X 1.10 or newer with the fun module, and ReAPI
 * (ReGameDLL_CS) for the spawn and round restart hooks.
 *
 * 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 <fun>
#include <reapi>

#pragma semicolon 1

new const PLUGIN_NAME[] = "Custom Gravity Control";
new const PLUGIN_VERSION[] = "1.0.0";
new const PLUGIN_AUTHOR[] = "alexit (olds.ro)";

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

const Float:NORMAL_GRAVITY = 1.0;

new Float:g_min;        // olds_grav_min
new Float:g_max;        // olds_grav_max
new g_playerCommand;    // olds_grav_player
new Float:g_low;        // olds_grav_low
new g_persist;          // olds_grav_persist

// The gravity each player was given; set again after every respawn.
new Float:g_gravity[MAX_PLAYERS + 1] = { NORMAL_GRAVITY, ... };

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

	register_concmd("amx_gravity", "cmd_gravity", ADMIN_LEVEL_A, "OLDS_GRAV_HELP", -1, true);

	register_srvcmd("olds_grav_status", "cmd_status");

	register_clcmd("say /lowgrav", "cmd_lowgrav");
	register_clcmd("say_team /lowgrav", "cmd_lowgrav");

	bind_pcvar_float(create_cvar("olds_grav_min", "0.2", FCVAR_NONE,
		"Lowest gravity allowed (1.0 = normal)",
		true, 0.05, true, 10.0), g_min);

	bind_pcvar_float(create_cvar("olds_grav_max", "2.0", FCVAR_NONE,
		"Highest gravity allowed (1.0 = normal)",
		true, 0.05, true, 10.0), g_max);

	bind_pcvar_num(create_cvar("olds_grav_player", "1", FCVAR_NONE,
		"1 = players may use /lowgrav, 0 = only admins change gravity",
		true, 0.0, true, 1.0), g_playerCommand);

	bind_pcvar_float(create_cvar("olds_grav_low", "0.5", FCVAR_NONE,
		"Gravity that /lowgrav gives (1.0 = normal)",
		true, 0.05, true, 10.0), g_low);

	bind_pcvar_num(create_cvar("olds_grav_persist", "1", FCVAR_NONE,
		"1 = keep the gravity over round restarts, 0 = back to normal at every round restart",
		true, 0.0, true, 1.0), g_persist);

	AutoExecConfig(true, "olds_gravity");

	RegisterHookChain(RG_CBasePlayer_Spawn, "fw_player_spawn_post", true);
	RegisterHookChain(RG_CSGameRules_RestartRound, "fw_restart_round_pre", false);
}

public client_putinserver(id)
{
	g_gravity[id] = NORMAL_GRAVITY;
}

public client_disconnected(id)
{
	g_gravity[id] = NORMAL_GRAVITY;
}

/**
 * A respawn sets gravity back to normal: give the player's own value again.
 */
public fw_player_spawn_post(const id)
{
	if (is_user_alive(id) && g_gravity[id] != NORMAL_GRAVITY)
		set_user_gravity(id, g_gravity[id]);
}

/**
 * Before the players respawn for the new round.
 */
public fw_restart_round_pre()
{
	if (g_persist)
		return;

	for (new id = 1; id <= MaxClients; id++)
	{
		if (g_gravity[id] == NORMAL_GRAVITY)
			continue;

		g_gravity[id] = NORMAL_GRAVITY;

		if (is_user_alive(id))
			set_user_gravity(id, NORMAL_GRAVITY);
	}
}

/**
 * amx_gravity <@all|@t|@ct|player> <value>
 */
public cmd_gravity(id, level, cid)
{
	if (!admin_allowed(id, level))
		return PLUGIN_HANDLED;

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

	new text[16];
	read_argv(2, text, charsmax(text));

	if (!is_decimal(text))
	{
		console_print(id, "%L", id, "OLDS_GRAV_BAD_VALUE");
		return PLUGIN_HANDLED;
	}

	new Float:asked = str_to_float(text);
	new Float:value = clamp_gravity(asked);

	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;

	if (value != asked)
		console_print(id, "%L", id, "OLDS_GRAV_CLAMPED", asked, lowest(), highest(), value);

	for (new i = 0; i < count; i++)
		give_gravity(players[i], value);

	console_print(id, "%L", id, "OLDS_GRAV_SET", value, targetName, count);

	new who[96];
	describe_admin(id, who, charsmax(who));
	log_amx("Cmd: %s set gravity %.2f for %s (%d players)", who, value, targetName, count);
	return PLUGIN_HANDLED;
}

/**
 * olds_grav_status: one line per player, for the server console and logs.
 */
public cmd_status()
{
	new players[MAX_PLAYERS], count;
	get_players(players, count);

	new lowgrav[4];
	copy(lowgrav, charsmax(lowgrav), "off");

	if (g_playerCommand)
		copy(lowgrav, charsmax(lowgrav), "on");

	server_print("[olds_gravity] %d player(s); allowed %.2f - %.2f, /lowgrav %s (%.2f), persist %d",
		count, lowest(), highest(), lowgrav, clamp_gravity(g_low), g_persist);

	new name[MAX_NAME_LENGTH], player;

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

		if (is_user_alive(player))
		{
			server_print("[olds_gravity] %s: gravity %.2f (kept %.2f)",
				name, get_user_gravity(player), g_gravity[player]);
		}
		else
		{
			server_print("[olds_gravity] %s: dead (kept %.2f)", name, g_gravity[player]);
		}
	}

	return PLUGIN_HANDLED;
}

/**
 * /lowgrav: low gravity on, or back to normal if it is on.
 */
public cmd_lowgrav(id)
{
	if (!g_playerCommand)
	{
		client_print_color(id, print_team_default, "^1%L", id, "OLDS_GRAV_PLAYER_OFF");
		return PLUGIN_CONTINUE;
	}

	new Float:low = clamp_gravity(g_low);

	if (g_gravity[id] == low)
	{
		give_gravity(id, NORMAL_GRAVITY);
		client_print_color(id, print_team_default, "^1%L", id, "OLDS_GRAV_LOW_OFF");
	}
	else
	{
		give_gravity(id, low);
		client_print_color(id, print_team_default, "^1%L", id, "OLDS_GRAV_LOW_ON", low);
	}

	// The typed command stays a normal chat line for other plugins.
	return PLUGIN_CONTINUE;
}

give_gravity(id, Float:value)
{
	g_gravity[id] = value;

	if (is_user_alive(id))
		set_user_gravity(id, value);
}

Float:lowest()
{
	return (g_min < g_max) ? g_min : g_max;
}

Float:highest()
{
	return (g_min < g_max) ? g_max : g_min;
}

Float:clamp_gravity(Float:value)
{
	new Float:low = lowest();
	new Float:high = highest();

	if (value < low)
		return low;

	if (value > high)
		return high;

	return value;
}

/**
 * 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_GRAV_NO_TARGET", target);
		return false;
	}

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

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

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

	for (new i = 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_GRAV_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);
}

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.