Skip to content
View in the app

A better way to browse. Learn more.

Learn more

backpack-ammo-limits/addons/amxmodx/scripting/olds_bpammo.sma

olds_bpammo.sma
282 lines · 8.1 KB · Pawn (AMX Mod X)
Raw
/*
 * Backpack Ammo Limits (olds.ro workshop card LIB-0097)
 *
 * Admins set the most reserve (backpack) ammo each weapon may carry, in a
 * config file. Ammo bought or picked up above the limit is not kept.
 *
 * File: addons/amxmodx/configs/olds_bpammo.ini, one weapon per line:
 *   weapon_ak47 120
 *   weapon_awp 15
 * Empty lines and lines starting with ; # or // are skipped. A line that is
 * not "weapon_<name> <0-255>" is skipped and the log names its number.
 *
 * Commands:
 *   amx_bpammo_reload   (ADMIN_CFG) reads the file again.
 *
 * Cvars (in configs/plugins/olds_bpammo.cfg, which ships with the plugin; if
 * the file is missing, the plugin writes it with these defaults the first
 * time it starts):
 *   olds_ba_enforce  1  1 = the limits apply; 0 = the plugin does nothing.
 *
 * Rules:
 *   - Weapons that share an ammo type (for example the AK-47 and the Scout)
 *     share one reserve. When the player carries more than one of them with
 *     a limit, the smallest limit counts.
 *   - Buying ammo at the limit is refused, so no money is spent. Ammo picked
 *     up above the limit is thrown away.
 *   - Grenades count too: weapon_flashbang 1 lets a player carry one flash.
 *   - New limits from amx_bpammo_reload apply to ammo given from then on
 *     and at each player's next spawn.
 *
 * How it works, as the card asks: RG_CBasePlayer_GiveAmmo (pre) lowers the
 * game's own maximum for that ammo to the limit, so the game itself stops
 * at it; RG_BuyGunAmmo (pre) refuses a purchase at the limit. On
 * RG_CBasePlayer_OnSpawnEquip and RG_CBasePlayer_AddPlayerItem (post) a
 * reserve above the limit is cut with cs_set_user_bpammo.
 *
 * Requires AMX Mod X 1.10 with ReAPI (ReGameDLL_CS) and the cstrike 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 <amxmisc>
#include <cstrike>
#include <reapi>

#pragma semicolon 1

new const PLUGIN_NAME[] = "Backpack Ammo Limits";
new const PLUGIN_VERSION[] = "1.0.0";
new const PLUGIN_AUTHOR[] = "alexit (olds.ro)";

new const LIMITS_FILE[] = "olds_bpammo.ini";

const LAST_WEAPON = CSW_P90;

// Weapon names and the ammo each one uses, by CSW_ id. An empty ammo name
// means the weapon cannot have a limit.
new const WEAPON_NAMES[LAST_WEAPON + 1][] =
{
	"", "weapon_p228", "", "weapon_scout", "weapon_hegrenade", "weapon_xm1014",
	"weapon_c4", "weapon_mac10", "weapon_aug", "weapon_smokegrenade", "weapon_elite",
	"weapon_fiveseven", "weapon_ump45", "weapon_sg550", "weapon_galil", "weapon_famas",
	"weapon_usp", "weapon_glock18", "weapon_awp", "weapon_mp5navy", "weapon_m249",
	"weapon_m3", "weapon_m4a1", "weapon_tmp", "weapon_g3sg1", "weapon_flashbang",
	"weapon_deagle", "weapon_sg552", "weapon_ak47", "weapon_knife", "weapon_p90"
};

new const AMMO_NAMES[LAST_WEAPON + 1][] =
{
	"", "357SIG", "", "762Nato", "HEGrenade", "buckshot",
	"", "45acp", "556Nato", "SmokeGrenade", "9mm",
	"57mm", "45acp", "556Nato", "556Nato", "556Nato",
	"45acp", "9mm", "338Magnum", "9mm", "556NatoBox",
	"buckshot", "556Nato", "9mm", "762Nato", "Flashbang",
	"50AE", "556Nato", "762Nato", "", "57mm"
};

new g_enforce;         // olds_ba_enforce

// Limit by CSW_ id, -1 = none.
new g_limit[LAST_WEAPON + 1];

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

	if (!is_regamedll())
		set_fail_state("ReGameDLL_CS is not running. This plugin needs ReGameDLL_CS and ReAPI.");

	bind_pcvar_num(create_cvar("olds_ba_enforce", "1", FCVAR_NONE,
		"1 = the backpack ammo limits from olds_bpammo.ini apply",
		true, 0.0, true, 1.0), g_enforce);

	AutoExecConfig(true, "olds_bpammo");

	register_concmd("amx_bpammo_reload", "cmd_reload", ADMIN_CFG, "- reads olds_bpammo.ini again");

	RegisterHookChain(RG_CBasePlayer_GiveAmmo, "on_give_ammo_pre", false);
	RegisterHookChain(RG_BuyGunAmmo, "on_buy_gun_ammo_pre", false);
	RegisterHookChain(RG_CBasePlayer_OnSpawnEquip, "on_spawn_equip_post", true);
	RegisterHookChain(RG_CBasePlayer_AddPlayerItem, "on_add_player_item_post", true);

	load_limits();
}

public cmd_reload(id, level, cid)
{
	if (!cmd_access(id, level, cid, 1))
		return PLUGIN_HANDLED;

	new count = load_limits();
	console_print(id, "[Ammo] %s read again: %d limit(s).", LIMITS_FILE, count);
	return PLUGIN_HANDLED;
}

/**
 * The game is about to give ammo: its maximum becomes the limit.
 */
public on_give_ammo_pre(id, amount, const ammoName[], maxAmmo)
{
	new limit = ammo_limit(id, ammoName);

	if (limit >= 0 && limit < maxAmmo)
		SetHookChainArg(4, ATYPE_INTEGER, limit);

	return HC_CONTINUE;
}

/**
 * Buying ammo for a weapon already at its limit is refused (no money is
 * taken).
 */
public on_buy_gun_ammo_pre(id, weapon, bool:blinkMoney)
{
	if (weapon <= 0)
		return HC_CONTINUE;

	new weaponId = get_member(weapon, m_iId);

	if (!(1 <= weaponId <= LAST_WEAPON) || !AMMO_NAMES[weaponId][0])
		return HC_CONTINUE;

	new limit = ammo_limit(id, AMMO_NAMES[weaponId]);

	if (limit < 0 || cs_get_user_bpammo(id, weaponId) < limit)
		return HC_CONTINUE;

	SetHookChainReturn(ATYPE_BOOL, false);
	return HC_SUPERCEDE;
}

public on_spawn_equip_post(id)
{
	trim_ammo(id);
	return HC_CONTINUE;
}

public on_add_player_item_post(id, item)
{
	trim_ammo(id);
	return HC_CONTINUE;
}

/**
 * Cuts every reserve above its limit.
 */
trim_ammo(id)
{
	if (!g_enforce || !is_user_alive(id))
		return;

	for (new weaponId = 1; weaponId <= LAST_WEAPON; weaponId++)
	{
		if (g_limit[weaponId] < 0 || !user_has_weapon(id, weaponId))
			continue;

		new limit = ammo_limit(id, AMMO_NAMES[weaponId]);

		if (limit >= 0 && cs_get_user_bpammo(id, weaponId) > limit)
			cs_set_user_bpammo(id, weaponId, limit);
	}
}

/**
 * The smallest limit among the weapons the player carries that use this
 * ammo, or -1 when none of them has a limit.
 */
ammo_limit(id, const ammoName[])
{
	if (!g_enforce || !is_user_alive(id) || !ammoName[0])
		return -1;

	new limit = -1;

	for (new weaponId = 1; weaponId <= LAST_WEAPON; weaponId++)
	{
		if (g_limit[weaponId] < 0 || !equali(AMMO_NAMES[weaponId], ammoName) || !user_has_weapon(id, weaponId))
			continue;

		if (limit < 0 || g_limit[weaponId] < limit)
			limit = g_limit[weaponId];
	}

	return limit;
}

/**
 * Reads the limits file. Returns how many limits were read.
 */
load_limits()
{
	for (new weaponId = 0; weaponId <= LAST_WEAPON; weaponId++)
		g_limit[weaponId] = -1;

	new path[PLATFORM_MAX_PATH];
	get_configsdir(path, charsmax(path));
	format(path, charsmax(path), "%s/%s", path, LIMITS_FILE);

	new file = fopen(path, "rt");

	if (!file)
	{
		log_amx("%s not found: no backpack ammo limits.", LIMITS_FILE);
		return 0;
	}

	new line[128], name[48], value[16], extra[8];
	new lineNumber = 0, count = 0;

	while (fgets(file, line, charsmax(line)))
	{
		lineNumber++;
		trim(line);

		if (!line[0] || line[0] == ';' || line[0] == '#' || (line[0] == '/' && line[1] == '/'))
			continue;

		new parts = parse(line, name, charsmax(name), value, charsmax(value), extra, charsmax(extra));
		new weaponId = find_weapon(name);

		if (parts != 2 || !weaponId || !is_str_num(value) || str_to_num(value) > 255)
		{
			log_amx("%s line %d skipped: ^"%s^" is not ^"weapon_<name> <0-255>^" for a weapon with ammo.", LIMITS_FILE, lineNumber, line);
			continue;
		}

		g_limit[weaponId] = str_to_num(value);
		count++;
	}

	fclose(file);
	log_amx("%s: %d backpack ammo limit(s) read.", LIMITS_FILE, count);
	return count;
}

/**
 * The CSW_ id of a weapon name that can have a limit, or 0.
 */
find_weapon(const name[])
{
	for (new weaponId = 1; weaponId <= LAST_WEAPON; weaponId++)
	{
		if (AMMO_NAMES[weaponId][0] && equali(WEAPON_NAMES[weaponId], name))
			return weaponId;
	}

	return 0;
}

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.