/*
* Buy Armor Command (olds.ro workshop card LIB-0144)
*
* Players buy armor with a chat command instead of the buy menu, under the
* same rules as the menu: only alive, inside a buy zone and while the buy
* time lasts. A purchase that would waste money is refused.
*
* Commands (say or say_team):
* /armor Kevlar: armor is filled up to 100 for olds_ba_kevlar money.
* A helmet the player already has is kept.
* /helmet Kevlar and helmet for olds_ba_vesthelm money. A player whose
* Kevlar is already full pays only the helmet (the difference
* between the two prices), as in the buy menu.
*
* Refused, with a chat line and no money taken:
* - dead, outside a buy zone, or after the buy time (mp_buytime);
* - /armor when the armor is already 100 or more;
* - /helmet when the player already has a helmet;
* - not enough money.
*
* Cvars (in configs/plugins/olds_buyarmor.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_kevlar 650 Price of /armor (Kevlar).
* olds_ba_vesthelm 1000 Price of /helmet (Kevlar and helmet).
*
* How it works, as the card asks: the say commands check cs_get_user_buyzone
* and the buy time, then give the armor with cs_set_user_armor
* (CS_ARMOR_KEVLAR or CS_ARMOR_VESTHELM) and take the money 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_buyarmor.txt; the purchase sound is the stock
* items/ammopickup2.wav, played with "spk" for the buyer only.
*
* Requires AMX Mod X 1.10 with ReAPI (ReGameDLL_CS) and the cstrike module.
* Written and tested on 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 <cstrike>
#include <reapi>
#pragma semicolon 1
new const PLUGIN_NAME[] = "Buy Armor Command";
new const PLUGIN_VERSION[] = "1.0.0";
new const PLUGIN_AUTHOR[] = "alexit (olds.ro)";
// Stock sound, played on the buyer's client with "spk" (no download).
new const BUY_SOUND[] = "items/ammopickup2";
// Full armor, as the buy menu gives it.
const FULL_ARMOR = 100;
new g_kevlarPrice; // olds_ba_kevlar
new g_vestHelmPrice; // olds_ba_vesthelm
new g_buyTimeCvar; // mp_buytime
public plugin_init()
{
register_plugin(PLUGIN_NAME, PLUGIN_VERSION, PLUGIN_AUTHOR);
// The round start time comes from ReGameDLL_CS.
if (!is_regamedll())
set_fail_state("ReGameDLL_CS is not running. This plugin needs ReGameDLL_CS and ReAPI.");
register_dictionary("olds_buyarmor.txt");
bind_pcvar_num(create_cvar("olds_ba_kevlar", "650", FCVAR_NONE,
"Price of /armor (Kevlar)",
true, 0.0), g_kevlarPrice);
bind_pcvar_num(create_cvar("olds_ba_vesthelm", "1000", FCVAR_NONE,
"Price of /helmet (Kevlar and helmet)",
true, 0.0), g_vestHelmPrice);
AutoExecConfig(true, "olds_buyarmor");
g_buyTimeCvar = get_cvar_pointer("mp_buytime");
register_clcmd("say /armor", "cmd_armor");
register_clcmd("say_team /armor", "cmd_armor");
register_clcmd("say /helmet", "cmd_helmet");
register_clcmd("say_team /helmet", "cmd_helmet");
}
/**
* /armor: Kevlar up to 100. A helmet the player has is kept.
*/
public cmd_armor(id)
{
if (!can_buy_now(id))
return PLUGIN_HANDLED;
new CsArmorType:type;
new armor = cs_get_user_armor(id, type);
if (armor >= FULL_ARMOR)
{
client_print_color(id, print_team_default, "%L", id, "BA_FULL_ARMOR");
return PLUGIN_HANDLED;
}
if (!pay(id, g_kevlarPrice))
return PLUGIN_HANDLED;
cs_set_user_armor(id, FULL_ARMOR, (type == CS_ARMOR_VESTHELM) ? CS_ARMOR_VESTHELM : CS_ARMOR_KEVLAR);
bought(id, "BA_BOUGHT_KEVLAR", g_kevlarPrice);
return PLUGIN_HANDLED;
}
/**
* /helmet: Kevlar and helmet. With full Kevlar only the helmet is paid.
*/
public cmd_helmet(id)
{
if (!can_buy_now(id))
return PLUGIN_HANDLED;
new CsArmorType:type;
new armor = cs_get_user_armor(id, type);
if (type == CS_ARMOR_VESTHELM)
{
client_print_color(id, print_team_default, "%L", id, "BA_HAS_HELMET");
return PLUGIN_HANDLED;
}
if (armor >= FULL_ARMOR)
{
// The Kevlar is full already: only the helmet is bought.
new price = max(g_vestHelmPrice - g_kevlarPrice, 0);
if (!pay(id, price))
return PLUGIN_HANDLED;
cs_set_user_armor(id, armor, CS_ARMOR_VESTHELM);
bought(id, "BA_BOUGHT_HELMET", price);
return PLUGIN_HANDLED;
}
if (!pay(id, g_vestHelmPrice))
return PLUGIN_HANDLED;
cs_set_user_armor(id, FULL_ARMOR, CS_ARMOR_VESTHELM);
bought(id, "BA_BOUGHT_VESTHELM", g_vestHelmPrice);
return PLUGIN_HANDLED;
}
/**
* Alive, in a buy zone and within the buy time; tells the player otherwise.
*/
bool:can_buy_now(id)
{
if (!is_user_alive(id))
{
client_print_color(id, print_team_default, "%L", id, "BA_NOT_ALIVE");
return false;
}
if (!cs_get_user_buyzone(id))
{
client_print_color(id, print_team_default, "%L", id, "BA_NO_BUYZONE");
return false;
}
new Float:buyTime = g_buyTimeCvar ? get_pcvar_float(g_buyTimeCvar) * 60.0 : 90.0;
// mp_buytime -1 means no time limit (ReGameDLL_CS).
if (buyTime >= 0.0 && get_gametime() - Float:get_member_game(m_fRoundStartTime) > buyTime)
{
client_print_color(id, print_team_default, "%L", id, "BA_BUYTIME_OVER", floatround(buyTime));
return false;
}
return true;
}
/**
* Takes the price from the player's money; refuses when it is not enough.
*/
bool:pay(id, price)
{
new money = cs_get_user_money(id);
if (money < price)
{
client_print_color(id, print_team_default, "%L", id, "BA_NO_MONEY", price);
return false;
}
cs_set_user_money(id, money - price);
return true;
}
bought(id, const key[], price)
{
client_print_color(id, print_team_default, "%L", id, key, price);
client_cmd(id, "spk %s", BUY_SOUND);
}