Skip to content
View in the app

A better way to browse. Learn more.

Learn more

disable-sniper-zoom/addons/amxmodx/scripting/olds_nozoom.sma

olds_nozoom.sma
193 lines · 5.7 KB · Pawn (AMX Mod X)
Raw
/*
 * Disable Sniper Zoom (olds.ro workshop card LIB-1561)
 *
 * Takes the scope away from sniper rifles, or allows only the first zoom
 * step, chosen per weapon, for modes that should not allow scoping.
 *
 * The zoom is the rifle's secondary attack (right mouse button). The plugin
 * hooks it (Ham_Weapon_SecondaryAttack) for weapon_awp, weapon_scout,
 * weapon_sg550 and weapon_g3sg1 and blocks it (HAM_SUPERCEDE) for the
 * rifles that are turned on:
 *   olds_nozoom_levels 0   no zoom at all
 *   olds_nozoom_levels 1   the first zoom step only; pressing again zooms
 *                          out instead of going to the second step
 * A player who is still zoomed in when a press is refused, or who puts the
 * rifle away while zoomed in, is set back to the normal view (field of view
 * 90) and normal speed.
 *
 * Cvars (saved with their descriptions in configs/plugins/olds_nozoom.cfg
 * the first time the plugin starts):
 *   olds_nozoom_awp     1   1 = AWP zoom limited, 0 = as in the game
 *   olds_nozoom_scout   0   the same for the Scout
 *   olds_nozoom_sg550   1   the same for the SG 550
 *   olds_nozoom_g3sg1   1   the same for the G3/SG-1
 *   olds_nozoom_levels  0   0 = no zoom, 1 = first zoom step only
 *
 * Server command:
 *   olds_nozoom_status   prints the settings and every player's weapon and
 *                        field of view
 *
 * Requires AMX Mod X 1.10 or newer with the hamsandwich module, and ReAPI
 * (ReGameDLL_CS) for the weapon and player members.
 *
 * 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 <hamsandwich>
#include <reapi>

#pragma semicolon 1

new const PLUGIN_NAME[] = "Disable Sniper Zoom";
new const PLUGIN_VERSION[] = "1.0.0";
new const PLUGIN_AUTHOR[] = "alexit (olds.ro)";

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

const NORMAL_FOV = 90;

new const WEAPON_CLASSES[][] = { "weapon_awp", "weapon_scout", "weapon_sg550", "weapon_g3sg1" };
new const WEAPON_IDS[] = { CSW_AWP, CSW_SCOUT, CSW_SG550, CSW_G3SG1 };
new const CVAR_NAMES[][] = { "olds_nozoom_awp", "olds_nozoom_scout", "olds_nozoom_sg550", "olds_nozoom_g3sg1" };
new const CVAR_DEFAULTS[][] = { "1", "0", "1", "1" };

new g_limited[sizeof(WEAPON_IDS)];
new g_levels;

// For olds_nozoom_status.
new g_refused;
new g_resets;

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

	new description[96];

	for (new i = 0; i < sizeof(WEAPON_CLASSES); i++)
	{
		formatex(description, charsmax(description),
			"1 = %s zoom limited (see olds_nozoom_levels), 0 = as in the game", WEAPON_CLASSES[i][7]);

		bind_pcvar_num(create_cvar(CVAR_NAMES[i], CVAR_DEFAULTS[i], FCVAR_NONE, description,
			true, 0.0, true, 1.0), g_limited[i]);

		RegisterHam(Ham_Weapon_SecondaryAttack, WEAPON_CLASSES[i], "fw_secondary_attack_pre", false);
		RegisterHam(Ham_Item_Holster, WEAPON_CLASSES[i], "fw_holster_post", true);
	}

	bind_pcvar_num(create_cvar("olds_nozoom_levels", "0", FCVAR_NONE,
		"For the limited rifles: 0 = no zoom at all, 1 = the first zoom step only",
		true, 0.0, true, 1.0), g_levels);

	AutoExecConfig(true, "olds_nozoom");

	register_srvcmd("olds_nozoom_status", "cmd_status");
}

/**
 * The rifle's zoom button.
 */
public fw_secondary_attack_pre(weapon)
{
	new slot = weapon_slot(get_member(weapon, m_iId));

	if (slot == -1 || !g_limited[slot])
		return HAM_IGNORED;

	new id = get_member(weapon, m_pPlayer);

	if (id < 1 || id > MaxClients || !is_user_alive(id))
		return HAM_IGNORED;

	new fov = get_member(id, m_iFOV);

	// Level 1: from the normal view the first step is allowed.
	if (g_levels >= 1 && fov >= NORMAL_FOV)
		return HAM_IGNORED;

	// Level 0, or the second step: refused. A zoomed player zooms out.
	if (fov < NORMAL_FOV)
		reset_view(id);

	g_refused++;
	return HAM_SUPERCEDE;
}

/**
 * The rifle is put away: no zoom may stay behind.
 */
public fw_holster_post(weapon)
{
	new id = get_member(weapon, m_pPlayer);

	if (id < 1 || id > MaxClients || !is_user_connected(id))
		return HAM_IGNORED;

	if (get_member(id, m_iFOV) != NORMAL_FOV)
		reset_view(id);

	return HAM_IGNORED;
}

reset_view(id)
{
	set_member(id, m_iFOV, NORMAL_FOV);
	set_entvar(id, var_fov, float(NORMAL_FOV));
	ExecuteHamB(Ham_CS_Player_ResetMaxSpeed, id);
	g_resets++;
}

weapon_slot(weaponId)
{
	for (new i = 0; i < sizeof(WEAPON_IDS); i++)
	{
		if (WEAPON_IDS[i] == weaponId)
			return i;
	}

	return -1;
}

/**
 * olds_nozoom_status
 */
public cmd_status()
{
	server_print("%L", LANG_SERVER, "OLDS_NOZOOM_STATUS",
		g_limited[0], g_limited[1], g_limited[2], g_limited[3], g_levels, g_refused, g_resets);

	new players[MAX_PLAYERS], count, name[MAX_NAME_LENGTH], weaponName[32], player, weaponId;
	get_players(players, count, "a");

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

		// 0 = nothing in hand (for example just after a respawn).
		weaponId = get_user_weapon(player);

		if (CSW_P228 <= weaponId <= CSW_P90)
			get_weaponname(weaponId, weaponName, charsmax(weaponName));
		else
			copy(weaponName, charsmax(weaponName), "no weapon");

		server_print("%L", LANG_SERVER, "OLDS_NOZOOM_PLAYER", name, weaponName, get_member(player, m_iFOV));
	}

	return PLUGIN_HANDLED;
}

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.