/*
* Ammo Purchase Command (olds.ro workshop card LIB-0075)
*
* A buy command that fills up the backpack ammo of the gun in the player's
* hands, for a price set per weapon class.
*
* Commands:
* say /buyammo, say_team /buyammo fill up the backpack ammo of the gun
* in hand
* amx_buyammo the same from the console or a key
* (bind x amx_buyammo)
*
* Refused, with a chat line and no money taken:
* - dead, or holding the knife, a grenade or the bomb;
* - after the buy time (mp_buytime), when olds_ba_buytime is 1;
* - the backpack is already full (the player is told so);
* - not enough money (the price is shown).
*
* Prices: configs/olds_ammoprices.ini gives the price of a full backpack for
* each weapon class (pistol, shotgun, smg, rifle, sniper, machinegun), one
* "class price" pair per line; lines starting with ; # or // are comments.
* A class that is not listed costs olds_ba_price. The file is read when the
* map starts.
*
* Cvars (in configs/plugins/olds_buyammo.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_price 400 Price for a weapon class that is not in
* olds_ammoprices.ini.
* olds_ba_buytime 1 1 = only during the buy time; 0 = any time.
* olds_ba_free_pistol 0 1 = pistol ammo is free.
*
* How it works, as the card asks: the commands read the gun in hand, then
* set its backpack ammo to the game's limit (rg_get_weapon_info
* WI_MAX_ROUNDS) with cs_set_user_bpammo and take the price with
* cs_set_user_money. The buy time is the game's own rule: mp_buytime minutes
* from the start of the round (-1 = no limit). The chat text is in
* data/lang/olds_buyammo.txt; the sound is the stock items/gunpickup2.wav,
* played with "spk" for the buyer only.
*
* 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[] = "Ammo Purchase Command";
new const PLUGIN_VERSION[] = "1.0.0";
new const PLUGIN_AUTHOR[] = "alexit (olds.ro)";
// Price list in the configs folder, read at map start.
new const PRICES_FILE[] = "olds_ammoprices.ini";
// Stock sound, played on the buyer's client with "spk" (no download).
new const BUY_SOUND[] = "items/gunpickup2";
enum WeaponClass
{
CLASS_NONE = -1,
CLASS_PISTOL,
CLASS_SHOTGUN,
CLASS_SMG,
CLASS_RIFLE,
CLASS_SNIPER,
CLASS_MACHINEGUN
};
// Class names as written in olds_ammoprices.ini.
new const CLASS_NAMES[WeaponClass][] = { "pistol", "shotgun", "smg", "rifle", "sniper", "machinegun" };
new g_defaultPrice; // olds_ba_price
new g_useBuyTime; // olds_ba_buytime
new g_freePistol; // olds_ba_free_pistol
new g_buyTimeCvar; // mp_buytime
// Price of each class from the file, -1 = not listed (olds_ba_price).
new g_classPrice[WeaponClass];
public plugin_init()
{
register_plugin(PLUGIN_NAME, PLUGIN_VERSION, PLUGIN_AUTHOR);
// The weapon info and the round start time 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_buyammo.txt");
bind_pcvar_num(create_cvar("olds_ba_price", "400", FCVAR_NONE,
"Price of a full backpack for a weapon class that is not in olds_ammoprices.ini",
true, 0.0), g_defaultPrice);
bind_pcvar_num(create_cvar("olds_ba_buytime", "1", FCVAR_NONE,
"1 = ammo can be bought only during the buy time; 0 = any time",
true, 0.0, true, 1.0), g_useBuyTime);
bind_pcvar_num(create_cvar("olds_ba_free_pistol", "0", FCVAR_NONE,
"1 = pistol ammo is free",
true, 0.0, true, 1.0), g_freePistol);
AutoExecConfig(true, "olds_buyammo");
g_buyTimeCvar = get_cvar_pointer("mp_buytime");
load_prices();
register_clcmd("say /buyammo", "cmd_buy_ammo");
register_clcmd("say_team /buyammo", "cmd_buy_ammo");
register_clcmd("amx_buyammo", "cmd_buy_ammo");
}
public cmd_buy_ammo(id)
{
if (!is_user_alive(id))
{
client_print_color(id, print_team_default, "%L", id, "BAM_NOT_ALIVE");
return PLUGIN_HANDLED;
}
new weapon = get_member(id, m_pActiveItem);
new weaponId = (weapon > 0) ? get_member(weapon, m_iId) : 0;
new WeaponClass:class = weapon_class(weaponId);
if (class == CLASS_NONE)
{
client_print_color(id, print_team_default, "%L", id, "BAM_WRONG_WEAPON");
return PLUGIN_HANDLED;
}
if (g_useBuyTime && is_buy_time_over())
{
client_print_color(id, print_team_default, "%L", id, "BAM_BUYTIME_OVER");
return PLUGIN_HANDLED;
}
new maxAmmo = rg_get_weapon_info(weaponId, WI_MAX_ROUNDS);
if (cs_get_user_bpammo(id, weaponId) >= maxAmmo)
{
client_print_color(id, print_team_default, "%L", id, "BAM_FULL");
return PLUGIN_HANDLED;
}
new price = class_price(class);
new money = cs_get_user_money(id);
if (money < price)
{
client_print_color(id, print_team_default, "%L", id, "BAM_NO_MONEY", price);
return PLUGIN_HANDLED;
}
cs_set_user_bpammo(id, weaponId, maxAmmo);
if (price > 0)
cs_set_user_money(id, money - price);
client_print_color(id, print_team_default, "%L", id, "BAM_BOUGHT", price);
client_cmd(id, "spk %s", BUY_SOUND);
return PLUGIN_HANDLED;
}
/**
* The class of a gun; CLASS_NONE for the knife, grenades, the bomb or
* nothing in hand.
*/
WeaponClass:weapon_class(weaponId)
{
switch (weaponId)
{
case CSW_P228, CSW_ELITE, CSW_FIVESEVEN, CSW_USP, CSW_GLOCK18, CSW_DEAGLE:
return CLASS_PISTOL;
case CSW_M3, CSW_XM1014:
return CLASS_SHOTGUN;
case CSW_MAC10, CSW_UMP45, CSW_MP5NAVY, CSW_TMP, CSW_P90:
return CLASS_SMG;
case CSW_GALIL, CSW_FAMAS, CSW_AK47, CSW_M4A1, CSW_SG552, CSW_AUG:
return CLASS_RIFLE;
case CSW_SCOUT, CSW_AWP, CSW_G3SG1, CSW_SG550:
return CLASS_SNIPER;
case CSW_M249:
return CLASS_MACHINEGUN;
}
return CLASS_NONE;
}
class_price(WeaponClass:class)
{
if (class == CLASS_PISTOL && g_freePistol)
return 0;
return (g_classPrice[class] >= 0) ? g_classPrice[class] : g_defaultPrice;
}
/**
* The game's own buy time: mp_buytime minutes from the start of the round,
* -1 = no limit (ReGameDLL_CS).
*/
bool:is_buy_time_over()
{
new Float:buyTime = g_buyTimeCvar ? get_pcvar_float(g_buyTimeCvar) * 60.0 : 90.0;
if (buyTime < 0.0)
return false;
return get_gametime() - Float:get_member_game(m_fRoundStartTime) > buyTime;
}
/**
* Reads configs/olds_ammoprices.ini: one "class price" pair per line.
*/
load_prices()
{
for (new WeaponClass:class = CLASS_PISTOL; class <= CLASS_MACHINEGUN; class++)
g_classPrice[class] = -1;
new path[PLATFORM_MAX_PATH];
get_configsdir(path, charsmax(path));
format(path, charsmax(path), "%s/%s", path, PRICES_FILE);
new file = fopen(path, "rt");
if (!file)
return;
new line[128], name[32], value[16];
while (fgets(file, line, charsmax(line)))
{
trim(line);
if (!line[0] || line[0] == ';' || line[0] == '#' || (line[0] == '/' && line[1] == '/'))
continue;
parse(line, name, charsmax(name), value, charsmax(value));
for (new WeaponClass:class = CLASS_PISTOL; class <= CLASS_MACHINEGUN; class++)
{
if (equali(name, CLASS_NAMES[class]) && value[0])
{
g_classPrice[class] = max(str_to_num(value), 0);
break;
}
}
}
fclose(file);
}