Skip to content
View in the app

A better way to browse. Learn more.

Learn more

anti-rapid-pistol-fire/addons/amxmodx/scripting/olds_antirapid.sma

olds_antirapid.sma
196 lines · 6.3 KB · Pawn (AMX Mod X)
Raw
/*
 * Anti Rapid Pistol Fire (olds.ro workshop card LIB-0079)
 *
 * Semi-automatic pistols cannot fire faster than a set interval, which stops
 * fire macros and scripts from going past it. Shots that come too early are
 * dropped; after a number of them the player is warned (and logged), and
 * can be kicked.
 *
 * Commands: none.
 *
 * Cvars (in configs/plugins/olds_antirapid.cfg, which ships with the plugin;
 * if the file is missing, the plugin writes it with these defaults the first
 * time it starts):
 *   olds_ap_interval  0.15  Shortest time between two pistol shots
 *                           (seconds).
 *   olds_ap_warn      5     Dropped shots before a warning; the player is
 *                           warned and logged again every this many.
 *   olds_ap_kick      0     1 = kick the player at the first warning
 *                           instead.
 *
 * Rules:
 *   - Pistols: USP, Glock-18 (semi-automatic mode only, its burst mode keeps
 *     the game's own timing), P228, Desert Eagle, Five-SeveN, Dual Elites.
 *   - Only a real shot counts: holding the fire button fires a pistol once,
 *     and the game's own calls while the button stays held are not shots.
 *   - Each weapon has its own timer, so switching weapons never drops a
 *     shot.
 *   - A shot that comes too early is dropped; it fires as soon as the
 *     interval has passed if the button is still held. Several early
 *     attempts before one shot count as one.
 *   - 10 ms are allowed for the server's frame timing, so a player who
 *     clicks exactly at the limit is never punished.
 *   - The count of dropped shots lasts until the player leaves.
 *
 * How it works, as the card asks: Ham_Weapon_PrimaryAttack (pre) on the six
 * pistols compares get_gametime() with the weapon's last shot and returns
 * HAM_SUPERCEDE when it is too early.
 *
 * Requires AMX Mod X 1.10 with ReAPI (ReGameDLL_CS) and the hamsandwich
 * module. Written for AMX Mod X 1.10.0.5490, ReAPI 5.29.0.358 and
 * 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 <hamsandwich>
#include <reapi>

#pragma semicolon 1

new const PLUGIN_NAME[] = "Anti Rapid Pistol Fire";
new const PLUGIN_VERSION[] = "1.0.0";
new const PLUGIN_AUTHOR[] = "alexit (olds.ro)";

new const PISTOLS[][] =
{
	"weapon_usp",
	"weapon_glock18",
	"weapon_p228",
	"weapon_deagle",
	"weapon_fiveseven",
	"weapon_elite"
};

// Allowance for the server's frame timing.
const Float:TIMING_ALLOWANCE = 0.01;

new Float:g_interval;      // olds_ap_interval
new g_warn;                // olds_ap_warn
new g_kick;                // olds_ap_kick

// Per player: the weapon of his last shot and its time, whether an early
// attempt was already counted since, and the count of dropped shots.
new g_lastWeapon[MAX_PLAYERS + 1];
new Float:g_lastShot[MAX_PLAYERS + 1];
new bool:g_countedSinceShot[MAX_PLAYERS + 1];
new g_dropped[MAX_PLAYERS + 1];

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

	// The weapon members come from ReGameDLL_CS.
	if (!is_regamedll())
		set_fail_state("ReGameDLL_CS is not running. This plugin needs ReGameDLL_CS and ReAPI.");

	register_dictionary("olds_antirapid.txt");

	bind_pcvar_float(create_cvar("olds_ap_interval", "0.15", FCVAR_NONE,
		"Shortest time between two pistol shots (seconds)",
		true, 0.0, true, 2.0), g_interval);

	bind_pcvar_num(create_cvar("olds_ap_warn", "5", FCVAR_NONE,
		"Dropped shots before a warning; warned and logged again every this many",
		true, 1.0, true, 1000.0), g_warn);

	bind_pcvar_num(create_cvar("olds_ap_kick", "0", FCVAR_NONE,
		"1 = kick the player at the first warning instead",
		true, 0.0, true, 1.0), g_kick);

	AutoExecConfig(true, "olds_antirapid");

	for (new i = 0; i < sizeof(PISTOLS); i++)
		RegisterHam(Ham_Weapon_PrimaryAttack, PISTOLS[i], "on_primary_attack_pre", false);
}

public client_putinserver(id)
{
	reset_player(id);
}

public client_disconnected(id)
{
	reset_player(id);
}

reset_player(id)
{
	g_lastWeapon[id] = 0;
	g_lastShot[id] = 0.0;
	g_countedSinceShot[id] = false;
	g_dropped[id] = 0;
}

public on_primary_attack_pre(weapon)
{
	new id = get_member(weapon, m_pPlayer);

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

	// The button is still held from the last shot: the game fires nothing.
	if (get_member(weapon, m_Weapon_iShotsFired) > 0)
		return HAM_IGNORED;

	// The Glock in burst mode keeps the game's own timing.
	if (get_member(weapon, m_iId) == CSW_GLOCK18
		&& (get_member(weapon, m_Weapon_iWeaponState) & WPNSTATE_GLOCK18_BURST_MODE))
		return HAM_IGNORED;

	new Float:now = get_gametime();

	// First shot with this weapon: its timer starts now.
	if (weapon != g_lastWeapon[id] || now - g_lastShot[id] >= g_interval - TIMING_ALLOWANCE)
	{
		g_lastWeapon[id] = weapon;
		g_lastShot[id] = now;
		g_countedSinceShot[id] = false;
		return HAM_IGNORED;
	}

	// Too early: dropped. Early attempts before one shot count once.
	if (!g_countedSinceShot[id])
	{
		g_countedSinceShot[id] = true;
		g_dropped[id]++;

		if (g_dropped[id] % g_warn == 0)
			punish(id, weapon);
	}

	return HAM_SUPERCEDE;
}

punish(id, weapon)
{
	new name[MAX_NAME_LENGTH], authId[MAX_AUTHID_LENGTH], weaponName[32];
	get_user_name(id, name, charsmax(name));
	get_user_authid(id, authId, charsmax(authId));
	rg_get_weapon_info(WeaponIdType:get_member(weapon, m_iId), WI_NAME, weaponName, charsmax(weaponName));

	log_amx("^"%s<%d><%s>^" fired the %s faster than %.2f s: %d shots dropped%s", name, get_user_userid(id), authId,
		weaponName, g_interval, g_dropped[id], g_kick ? ", kicked" : "");

	if (g_kick)
	{
		new reason[128];
		formatex(reason, charsmax(reason), "%L", id, "AP_KICK");
		server_cmd("kick #%d ^"%s^"", get_user_userid(id), reason);
		return;
	}

	client_print_color(id, print_team_red, "%L", id, "AP_WARN", g_dropped[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.