Skip to content
View in the app

A better way to browse. Learn more.

Learn more

buyable-helmet/addons/amxmodx/scripting/olds_buyhelmet.sma

olds_buyhelmet.sma
162 lines · 5.2 KB · Pawn (AMX Mod X)
Raw
/*
 * Buyable Helmet (olds.ro workshop card LIB-0148)
 *
 * A player who already wears Kevlar can buy just the helmet, with a chat
 * command or a console command, without buying the whole Kevlar + helmet
 * set again. The armor points he has are kept.
 *
 * Commands:
 *   say /buyhelmet, say_team /buyhelmet   buy a helmet
 *   helmet                                the same from the console or a
 *                                         key (bind x helmet)
 *
 * Refused, with a chat line and no money taken:
 *   - dead, or outside the player's own buy zone;
 *   - after the buy time (mp_buytime), when olds_bh_buytime is 1;
 *   - no Kevlar (the player is told to buy Kevlar first);
 *   - a helmet already on;
 *   - not enough money (the price is shown).
 *
 * Cvars (in configs/plugins/olds_buyhelmet.cfg, which ships with the plugin;
 * if the file is missing, the plugin writes it with these defaults the first
 * time it starts):
 *   olds_bh_price    350  Price of the helmet.
 *   olds_bh_buytime  1    1 = only during the buy time; 0 = any time, but
 *                         still only inside the buy zone.
 *
 * How it works, as the card asks: the commands check cs_get_user_buyzone and
 * the buy time, then add the helmet with cs_set_user_armor(id, armor,
 * 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_buyhelmet.txt; the
 * sound is the stock items/ammopickup1.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 <cstrike>
#include <reapi>

#pragma semicolon 1

new const PLUGIN_NAME[] = "Buyable Helmet";
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/ammopickup1";

new g_price;           // olds_bh_price
new g_useBuyTime;      // olds_bh_buytime

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_buyhelmet.txt");

	bind_pcvar_num(create_cvar("olds_bh_price", "350", FCVAR_NONE,
		"Price of the helmet",
		true, 0.0), g_price);

	bind_pcvar_num(create_cvar("olds_bh_buytime", "1", FCVAR_NONE,
		"1 = the helmet can be bought only during the buy time; 0 = any time inside the buy zone",
		true, 0.0, true, 1.0), g_useBuyTime);

	AutoExecConfig(true, "olds_buyhelmet");

	g_buyTimeCvar = get_cvar_pointer("mp_buytime");

	register_clcmd("say /buyhelmet", "cmd_buy_helmet");
	register_clcmd("say_team /buyhelmet", "cmd_buy_helmet");
	register_clcmd("helmet", "cmd_buy_helmet");
}

public cmd_buy_helmet(id)
{
	if (!is_user_alive(id))
	{
		client_print_color(id, print_team_default, "%L", id, "BH_NOT_ALIVE");
		return PLUGIN_HANDLED;
	}

	if (!cs_get_user_buyzone(id))
	{
		client_print_color(id, print_team_default, "%L", id, "BH_NO_BUYZONE");
		return PLUGIN_HANDLED;
	}

	if (g_useBuyTime && is_buy_time_over())
	{
		client_print_color(id, print_team_default, "%L", id, "BH_BUYTIME_OVER");
		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, "BH_HAS_HELMET");
		return PLUGIN_HANDLED;
	}

	if (armor <= 0 || type == CS_ARMOR_NONE)
	{
		client_print_color(id, print_team_default, "%L", id, "BH_NO_KEVLAR");
		return PLUGIN_HANDLED;
	}

	new money = cs_get_user_money(id);

	if (money < g_price)
	{
		client_print_color(id, print_team_default, "%L", id, "BH_NO_MONEY", g_price);
		return PLUGIN_HANDLED;
	}

	// The armor points stay as they are; only the helmet is added.
	cs_set_user_armor(id, armor, CS_ARMOR_VESTHELM);
	cs_set_user_money(id, money - g_price);

	client_print_color(id, print_team_default, "%L", id, "BH_BOUGHT", g_price);
	client_cmd(id, "spk %s", BUY_SOUND);
	return PLUGIN_HANDLED;
}

/**
 * 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;
}

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.