Skip to content
View in the app

A better way to browse. Learn more.

Learn more

weapon-fire-rate-control/addons/amxmodx/scripting/olds_firerate.sma

olds_firerate.sma
217 lines · 7.2 KB · Pawn (AMX Mod X)
Raw
/*
 * Weapon Fire Rate Control (olds.ro workshop card LIB-2032)
 *
 * Makes each class of weapons fire slower or faster: after every shot the
 * time until the next shot is multiplied by the class's value, so 2.0 means
 * twice the stock gap between shots (half the rate) and 0.5 half the gap
 * (double the rate). The knife and the grenades are never changed.
 *
 *   pistols   olds_rof_pistol   P228, Glock-18, USP, Desert Eagle, Five-SeveN, Elites
 *   SMGs      olds_rof_smg      MAC-10, TMP, MP5, UMP45, P90
 *   rifles    olds_rof_rifle    FAMAS, Galil, AK-47, M4A1, SG 552, AUG, M249 and
 *                               the shotguns (M3, XM1014)
 *   snipers   olds_rof_sniper   Scout, AWP, G3SG1, SG 550
 *
 * The cvars are read at every shot, so a change works from the next shot,
 * without a map change. The Glock-18's burst mode waits the same scaled time.
 *
 * Cvars (configs/plugins/olds_firerate.cfg; AutoExecConfig writes that file
 * with these defaults if it is missing). Values outside 0.2 - 3.0 are clamped:
 *   olds_rof_pistol 1.0   Gap between pistol shots x this value.
 *   olds_rof_rifle  1.0   Gap between rifle, shotgun and M249 shots x this value.
 *   olds_rof_sniper 1.0   Gap between sniper rifle shots x this value.
 *   olds_rof_smg    1.0   Gap between SMG shots x this value.
 *
 * amx_rof_reload (ADMIN_CVAR, flag g) runs configs/plugins/olds_firerate.cfg
 * again and prints the values in use.
 *
 * Every text players read comes from data/lang/olds_firerate.txt (English).
 *
 * Requires AMX Mod X 1.10 or newer and ReAPI with ReGameDLL_CS. Written for
 * 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 <hamsandwich>
#include <reapi>

#pragma semicolon 1

new const PLUGIN_NAME[] = "Weapon Fire Rate Control";
new const PLUGIN_VERSION[] = "1.0.0";
new const PLUGIN_AUTHOR[] = "alexit (olds.ro)";

new const DICTIONARY[] = "olds_firerate.txt";
new const CONFIG_NAME[] = "olds_firerate";

const ACCESS_RELOAD = ADMIN_CVAR;

enum
{
	CLASS_NONE = 0,
	CLASS_PISTOL,
	CLASS_SMG,
	CLASS_RIFLE,
	CLASS_SNIPER
};

new Float:g_pistol;    // olds_rof_pistol
new Float:g_rifle;     // olds_rof_rifle
new Float:g_sniper;    // olds_rof_sniper
new Float:g_smg;       // olds_rof_smg

// The class of every weapon id; CLASS_NONE = never changed.
new g_weaponClass[CSW_P90 + 1];

// The weapon's clip before the attack, to tell a real shot from a dry click.
new g_clipBefore;

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

	bind_pcvar_float(create_rate_cvar("olds_rof_pistol", "Gap between pistol shots x this value (0.2 - 3.0)"), g_pistol);
	bind_pcvar_float(create_rate_cvar("olds_rof_rifle", "Gap between rifle, shotgun and M249 shots x this value (0.2 - 3.0)"), g_rifle);
	bind_pcvar_float(create_rate_cvar("olds_rof_sniper", "Gap between sniper rifle shots x this value (0.2 - 3.0)"), g_sniper);
	bind_pcvar_float(create_rate_cvar("olds_rof_smg", "Gap between SMG shots x this value (0.2 - 3.0)"), g_smg);

	AutoExecConfig(true, CONFIG_NAME);

	register_concmd("amx_rof_reload", "cmd_reload", ACCESS_RELOAD,
		"- runs configs/plugins/olds_firerate.cfg again and prints the fire rate values");

	set_class(CLASS_PISTOL, CSW_P228, CSW_GLOCK18, CSW_USP, CSW_DEAGLE, CSW_FIVESEVEN, CSW_ELITE);
	set_class(CLASS_SMG, CSW_MAC10, CSW_TMP, CSW_MP5NAVY, CSW_UMP45, CSW_P90);
	set_class(CLASS_RIFLE, CSW_FAMAS, CSW_GALIL, CSW_AK47, CSW_M4A1, CSW_SG552, CSW_AUG, CSW_M249, CSW_M3, CSW_XM1014);
	set_class(CLASS_SNIPER, CSW_SCOUT, CSW_AWP, CSW_G3SG1, CSW_SG550);

	new weaponName[32];

	for (new weaponId = 1; weaponId <= CSW_P90; weaponId++)
	{
		if (g_weaponClass[weaponId] == CLASS_NONE || !get_weaponname(weaponId, weaponName, charsmax(weaponName)))
			continue;

		RegisterHam(Ham_Weapon_PrimaryAttack, weaponName, "OnPrimaryAttack_Pre", false);
		RegisterHam(Ham_Weapon_PrimaryAttack, weaponName, "OnPrimaryAttack_Post", true);
	}
}

create_rate_cvar(const name[], const description[])
{
	return create_cvar(name, "1.0", FCVAR_NONE, description, true, 0.2, true, 3.0);
}

set_class(class, any:...)
{
	for (new i = 1; i < numargs(); i++)
		g_weaponClass[getarg(i)] = class;
}

public OnPrimaryAttack_Pre(weapon)
{
	g_clipBefore = get_member(weapon, m_Weapon_iClip);
	return HAM_IGNORED;
}

public OnPrimaryAttack_Post(weapon)
{
	// A dry click (empty clip) keeps the stock delay.
	if (get_member(weapon, m_Weapon_iClip) >= g_clipBefore)
		return HAM_IGNORED;

	new weaponId = get_member(weapon, m_iId);

	if (weaponId < 1 || weaponId > CSW_P90)
		return HAM_IGNORED;

	new Float:factor = class_factor(g_weaponClass[weaponId]);

	if (factor == 1.0)
		return HAM_IGNORED;

	// The delays are counted down from the shot (client weapon prediction).
	set_member(weapon, m_Weapon_flNextPrimaryAttack, Float:get_member(weapon, m_Weapon_flNextPrimaryAttack) * factor);

	// The Glock-18 sets both delays after a shot; the burst mode waits on both.
	if (weaponId == CSW_GLOCK18)
		set_member(weapon, m_Weapon_flNextSecondaryAttack, Float:get_member(weapon, m_Weapon_flNextSecondaryAttack) * factor);

	return HAM_IGNORED;
}

Float:class_factor(class)
{
	switch (class)
	{
		case CLASS_PISTOL: return g_pistol;
		case CLASS_SMG: return g_smg;
		case CLASS_RIFLE: return g_rifle;
		case CLASS_SNIPER: return g_sniper;
	}

	return 1.0;
}

public cmd_reload(id, level, cid)
{
	if (id && !(get_user_flags(id) & level))
	{
		console_print(id, "%L", id, "OLDS_ROF_NO_ACCESS");
		return PLUGIN_HANDLED;
	}

	new path[PLATFORM_MAX_PATH];
	get_configsdir(path, charsmax(path));
	format(path, charsmax(path), "%s/plugins/%s.cfg", path, CONFIG_NAME);

	if (file_exists(path))
	{
		server_cmd("exec ^"%s^"", path);
		server_exec();
	}
	else
	{
		console_print(id, "%L", id, "OLDS_ROF_NO_CONFIG", path);
	}

	new pistol[8], smg[8], rifle[8], sniper[8];
	format_rate(g_pistol, pistol, charsmax(pistol));
	format_rate(g_smg, smg, charsmax(smg));
	format_rate(g_rifle, rifle, charsmax(rifle));
	format_rate(g_sniper, sniper, charsmax(sniper));

	console_print(id, "%L", id, "OLDS_ROF_VALUES", pistol, smg, rifle, sniper);

	new name[MAX_NAME_LENGTH], authId[MAX_AUTHID_LENGTH];
	get_user_name(id, name, charsmax(name));
	get_user_authid(id, authId, charsmax(authId));
	log_amx("Cmd: ^"%s<%d><%s><>^" reloaded the fire rates: pistol %s, smg %s, rifle %s, sniper %s",
		name, id ? get_user_userid(id) : 0, authId, pistol, smg, rifle, sniper);

	return PLUGIN_HANDLED;
}

/**
 * The value rounded to hundredths (%.2f would cut 0.7, stored as 0.6999, to 0.69).
 */
format_rate(Float:value, text[], len)
{
	new hundredths = floatround(value * 100.0);
	formatex(text, len, "%d.%02d", hundredths / 100, hundredths % 100);
}

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.