Skip to content
View in the app

A better way to browse. Learn more.

Learn more

screen-shake-opt-out/addons/amxmodx/scripting/olds_noshake.sma

olds_noshake.sma
357 lines · 10.3 KB · Pawn (AMX Mod X)
Raw
/*
 * Screen Shake Opt-Out (olds.ro workshop card IDEA-53980C084ACF0E61)
 *
 * A per-player switch that removes screen shakes for players who get motion
 * sick. With the option on, the ScreenShake messages the server sends to that
 * player are dropped: the game's own (env_shake entities on maps) and those
 * of other plugins. Damage, sounds and everything else stay normal, and
 * every other player keeps their shakes.
 *
 * Chat command (chat and team chat):
 *   /noshake      turn the option on, or off again
 *
 * Cvars (in configs/plugins/olds_noshake.cfg, written with the defaults on
 * the first start if the file is missing):
 *   olds_noshake_enable  1   1 = the option works, 0 = every shake is sent as
 *                            usual and /noshake only says so.
 *   olds_noshake_punch   0   1 = also clear the view punch (the camera kick)
 *                            a player with the option gets when hit.
 *   olds_noshake_default 0   1 = the option starts on for players who never
 *                            chose, 0 = it starts off.
 *
 * The choice is saved by SteamID in the nvault file olds_noshake, so it is
 * still there after a reconnect or a map change. Players without a real
 * SteamID (bots, LAN ids) keep it only until they leave.
 *
 * How it works, as the card asks: register_message on ScreenShake drops the
 * message for a receiver with the option on. A shake sent to everyone at
 * once (MSG_ALL or MSG_BROADCAST) is dropped and sent again to each player
 * without the option. The view punch is cleared in RG_CBasePlayer_TakeDamage
 * (post), after the game has applied it.
 *
 * register_message sees the game's own shakes and those other plugins send
 * with emessage_begin, but not those sent with message_begin: AMX Mod X
 * hands them straight to the engine. ReAPI's RegisterMessage hooks the
 * engine (ReHLDS) itself, so the same check runs there too and also catches
 * those. A shake that the first hook lets through reaches the second one
 * unchanged and is let through again.
 *
 * Requires ReHLDS, ReGameDLL_CS, ReAPI with RegisterMessage and AMX Mod X
 * 1.10 with the nvault module. Written and tested on ReHLDS 3.15.0.896,
 * ReGameDLL_CS 5.30.0.814, ReAPI 5.29.0.358 and AMX Mod X 1.10.0.5490.
 *
 * 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 <nvault>
#include <reapi>

#pragma semicolon 1

new const PLUGIN_NAME[] = "Screen Shake Opt-Out";
new const PLUGIN_VERSION[] = "1.0.0";
new const PLUGIN_AUTHOR[] = "alexit (olds.ro)";

new const DICTIONARY[] = "olds_noshake.txt";
new const VAULT_NAME[] = "olds_noshake";

// Values saved in the vault.
new const SAVED_ON[] = "1";
new const SAVED_OFF[] = "0";

new g_enable;        // olds_noshake_enable
new g_clearPunch;    // olds_noshake_punch
new g_startOn;       // olds_noshake_default

new g_vault = INVALID_HANDLE;

// true = this player gets no screen shakes.
new bool:g_noShake[MAX_PLAYERS + 1];

// The player's SteamID when it can be saved, or an empty string.
new g_saveKey[MAX_PLAYERS + 1][MAX_AUTHID_LENGTH];

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

	bind_pcvar_num(create_cvar("olds_noshake_enable", "1", FCVAR_NONE,
		"1 = players can turn screen shakes off with /noshake, 0 = every shake is sent as usual",
		true, 0.0, true, 1.0), g_enable);

	bind_pcvar_num(create_cvar("olds_noshake_punch", "0", FCVAR_NONE,
		"1 = also clear the view punch from damage for players with the option on",
		true, 0.0, true, 1.0), g_clearPunch);

	bind_pcvar_num(create_cvar("olds_noshake_default", "0", FCVAR_NONE,
		"1 = the option starts on for players who never chose, 0 = it starts off",
		true, 0.0, true, 1.0), g_startOn);

	AutoExecConfig(true, "olds_noshake");

	register_clcmd("say /noshake", "cmd_noshake");
	register_clcmd("say_team /noshake", "cmd_noshake");

	RegisterHookChain(RG_CBasePlayer_TakeDamage, "on_take_damage_post", true);

	g_vault = nvault_open(VAULT_NAME);

	if (g_vault == INVALID_HANDLE)
		log_amx("Could not open the nvault file %s; choices are kept only until players leave.", VAULT_NAME);

	new msgScreenShake = get_user_msgid("ScreenShake");

	register_message(msgScreenShake, "msg_screen_shake");

	// Last, so that everything above is already set up on an engine without
	// ReHLDS's message hooks, where this native stops with an error.
	RegisterMessage(msgScreenShake, "engine_screen_shake");
}

public plugin_end()
{
	if (g_vault != INVALID_HANDLE)
		nvault_close(g_vault);
}

public client_connect(id)
{
	// The cvar is read once the config has run, so it is current here.
	g_noShake[id] = g_startOn != 0;
	g_saveKey[id][0] = EOS;
}

public client_authorized(id, const authid[])
{
	if (!can_save(authid))
		return;

	copy(g_saveKey[id], charsmax(g_saveKey[]), authid);

	if (g_vault == INVALID_HANDLE)
		return;

	new saved[4], timestamp;

	if (nvault_lookup(g_vault, authid, saved, charsmax(saved), timestamp))
		g_noShake[id] = equal(saved, SAVED_ON) != 0;
}

public client_disconnected(id)
{
	g_noShake[id] = false;
	g_saveKey[id][0] = EOS;
}

/**
 * Only a real SteamID belongs to one person. Bots, LAN ids and ids that are
 * not known yet are shared by many players, so nothing is saved for them.
 */
bool:can_save(const authid[])
{
	if (equal(authid, "STEAM_ID_LAN") || equal(authid, "STEAM_ID_PENDING") || equal(authid, "VALVE_ID_LAN"))
		return false;

	return equal(authid, "STEAM_", 6) || equal(authid, "VALVE_", 6);
}

public cmd_noshake(id)
{
	if (!g_enable)
	{
		client_print_color(id, print_team_default, "%L", id, "NOSHAKE_DISABLED");
		return PLUGIN_HANDLED;
	}

	g_noShake[id] = !g_noShake[id];

	if (g_saveKey[id][0] && g_vault != INVALID_HANDLE)
		nvault_set(g_vault, g_saveKey[id], g_noShake[id] ? SAVED_ON : SAVED_OFF);

	client_print_color(id, print_team_default, "%L", id, g_noShake[id] ? "NOSHAKE_NOW_OFF" : "NOSHAKE_NOW_ON");

	// The command itself is not shown in chat.
	return PLUGIN_HANDLED;
}

/**
 * A ScreenShake is about to be sent. One addressed to a single player is
 * dropped when that player has the option on. One addressed to everyone is
 * dropped and sent again only to the players without the option.
 */
public msg_screen_shake(msgId, msgDest, msgEntity)
{
	if (!g_enable)
		return PLUGIN_CONTINUE;

	switch (msgDest)
	{
		case MSG_ONE, MSG_ONE_UNRELIABLE:
		{
			if (is_opted_out(msgEntity))
				return PLUGIN_HANDLED;
		}
		case MSG_ALL, MSG_BROADCAST:
		{
			if (!anyone_opted_out())
				return PLUGIN_CONTINUE;

			resend_to_others(msgId, msgDest == MSG_ALL ? MSG_ONE : MSG_ONE_UNRELIABLE, false);
			return PLUGIN_HANDLED;
		}
	}

	// Shakes sent to a place (MSG_PVS, MSG_PAS) have no single receiver and
	// are left alone; the game never sends ScreenShake that way.
	return PLUGIN_CONTINUE;
}

/**
 * The same check in the engine, for the shakes that msg_screen_shake never
 * saw because a plugin sent them with message_begin.
 */
public engine_screen_shake(msgId, msgDest, msgEntity)
{
	if (!g_enable)
		return HC_CONTINUE;

	switch (msgDest)
	{
		case MSG_ONE, MSG_ONE_UNRELIABLE:
		{
			if (is_opted_out(msgEntity))
				return HC_SUPERCEDE;
		}
		case MSG_ALL, MSG_BROADCAST:
		{
			if (!anyone_opted_out())
				return HC_CONTINUE;

			resend_to_others(msgId, msgDest == MSG_ALL ? MSG_ONE : MSG_ONE_UNRELIABLE, true);
			return HC_SUPERCEDE;
		}
	}

	return HC_CONTINUE;
}

bool:is_opted_out(id)
{
	return 1 <= id <= MaxClients && g_noShake[id];
}

bool:anyone_opted_out()
{
	for (new id = 1; id <= MaxClients; id++)
	{
		if (g_noShake[id] && is_user_connected(id))
			return true;
	}

	return false;
}

/**
 * Sends the message being hooked again, one copy per player who still wants
 * shakes. The copies are addressed to one player each, so when they pass the
 * engine hook they are let through and never copied again.
 */
resend_to_others(msgId, dest, bool:inEngine)
{
	new args = inEngine ? GetMessageArgsNum() : get_msg_args();

	for (new id = 1; id <= MaxClients; id++)
	{
		if (g_noShake[id] || !is_user_connected(id))
			continue;

		message_begin(dest, msgId, _, id);

		for (new arg = 1; arg <= args; arg++)
		{
			if (inEngine)
				write_engine_arg(arg);
			else
				write_amxx_arg(arg);
		}

		message_end();
	}
}

// One argument of the message that msg_screen_shake is handling.
write_amxx_arg(arg)
{
	switch (get_msg_argtype(arg))
	{
		case ARG_BYTE: write_byte(get_msg_arg_int(arg));
		case ARG_CHAR: write_char(get_msg_arg_int(arg));
		case ARG_SHORT: write_short(get_msg_arg_int(arg));
		case ARG_LONG: write_long(get_msg_arg_int(arg));
		case ARG_ANGLE: write_angle_f(get_msg_arg_float(arg));
		case ARG_COORD: write_coord_f(get_msg_arg_float(arg));
		case ARG_ENTITY: write_entity(get_msg_arg_int(arg));
		case ARG_STRING:
		{
			new text[192];
			get_msg_arg_string(arg, text, charsmax(text));
			write_string(text);
		}
	}
}

// One argument of the message that engine_screen_shake is handling.
write_engine_arg(arg)
{
	new Float:value;

	switch (GetMessageArgType(arg))
	{
		case ArgByte: write_byte(GetMessageData(MsgArg, arg));
		case ArgChar: write_char(GetMessageData(MsgArg, arg));
		case ArgShort: write_short(GetMessageData(MsgArg, arg));
		case ArgLong: write_long(GetMessageData(MsgArg, arg));
		case ArgEntity: write_entity(GetMessageData(MsgArg, arg));
		case ArgAngle:
		{
			GetMessageData(MsgArg, arg, value);
			write_angle_f(value);
		}
		case ArgCoord:
		{
			GetMessageData(MsgArg, arg, value);
			write_coord_f(value);
		}
		case ArgString:
		{
			new text[192];
			GetMessageData(MsgArg, arg, text, charsmax(text));
			write_string(text);
		}
	}
}

/**
 * The game kicks the view of a player who is hit before this runs, so the
 * kick can be undone here for players with the option on.
 */
public on_take_damage_post(victim, inflictor, attacker, Float:damage, damageBits)
{
	if (g_enable && g_clearPunch && is_user_alive(victim) && g_noShake[victim])
		set_entvar(victim, var_punchangle, Float:{ 0.0, 0.0, 0.0 });

	return HC_CONTINUE;
}

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.