Skip to content
View in the app

A better way to browse. Learn more.

Learn more

buy-a-frag/addons/amxmodx/scripting/olds_buyfrag.sma

olds_buyfrag.sma
222 lines · 6.7 KB · Pawn (AMX Mod X)
Raw
/*
 * Buy a Frag (olds.ro workshop card LIB-0143)
 *
 * Players spend money to gain an extra frag. The price rises with every
 * frag the player buys on the map, and only a few can be bought per round.
 *
 * Commands:
 *   say /buyfrag, say_team /buyfrag   buy one frag
 *
 * Price: olds_bf_base for the first frag on the map, then olds_bf_step more
 * for each one after it (5000, 7500, 10000, ... by default). The count
 * belongs to the player (by SteamID, so reconnecting does not reset it) and
 * starts again at the next map.
 *
 * Refused, with a chat line and no money taken:
 *   - not on the Terrorist or Counter-Terrorist team;
 *   - olds_bf_max frags already bought this round;
 *   - not enough money (the current price is shown).
 *
 * Cvars (in configs/plugins/olds_buyfrag.cfg, which ships with the plugin;
 * if the file is missing, the plugin writes it with these defaults the first
 * time it starts):
 *   olds_bf_base  5000  Price of the first frag on the map.
 *   olds_bf_step  2500  Added to the price for each frag bought before.
 *   olds_bf_max   3     Frags a player can buy per round (0 = no limit).
 *
 * How it works, as the card asks: the command adds the frag with
 * set_user_frags and sends the game's ScoreInfo message so the scoreboard
 * shows it at once, then takes the money with cs_set_user_money. Every
 * purchase is written to the server log:
 *   "Name<uid><steamid><TEAM>" triggered "buy_frag" (price "5000")
 * The per-round count is cleared in RG_CSGameRules_RestartRound. The chat
 * text is in data/lang/olds_buyfrag.txt.
 *
 * Requires AMX Mod X 1.10 with ReAPI (ReGameDLL_CS) and the cstrike and fun
 * modules. 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 <fun>
#include <reapi>

#pragma semicolon 1

new const PLUGIN_NAME[] = "Buy a Frag";
new const PLUGIN_VERSION[] = "1.0.0";
new const PLUGIN_AUTHOR[] = "alexit (olds.ro)";

new g_basePrice;       // olds_bf_base
new g_priceStep;       // olds_bf_step
new g_roundLimit;      // olds_bf_max

new g_msgScoreInfo;

// Frags bought on this map, by player key (SteamID, or the slot for bots
// and players without a unique SteamID).
new Trie:g_mapCounts;

// Frags bought by each player in this round.
new g_roundCount[MAX_PLAYERS + 1];

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

	// The round hook 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_buyfrag.txt");

	bind_pcvar_num(create_cvar("olds_bf_base", "5000", FCVAR_NONE,
		"Price of the first frag a player buys on the map",
		true, 0.0), g_basePrice);

	bind_pcvar_num(create_cvar("olds_bf_step", "2500", FCVAR_NONE,
		"Added to the price for each frag the player bought before on this map",
		true, 0.0), g_priceStep);

	bind_pcvar_num(create_cvar("olds_bf_max", "3", FCVAR_NONE,
		"Frags a player can buy per round (0 = no limit)",
		true, 0.0), g_roundLimit);

	AutoExecConfig(true, "olds_buyfrag");

	g_msgScoreInfo = get_user_msgid("ScoreInfo");
	g_mapCounts = TrieCreate();

	register_clcmd("say /buyfrag", "cmd_buy_frag");
	register_clcmd("say_team /buyfrag", "cmd_buy_frag");

	RegisterHookChain(RG_CSGameRules_RestartRound, "on_restart_round_post", true);
}

public plugin_end()
{
	TrieDestroy(g_mapCounts);
}

/**
 * A new round: the round limit starts again for everyone.
 */
public on_restart_round_post()
{
	arrayset(g_roundCount, 0, sizeof(g_roundCount));
	return HC_CONTINUE;
}

public client_disconnected(id)
{
	g_roundCount[id] = 0;

	// A slot key belongs to this connection only; a SteamID key stays.
	new key[64];

	if (player_key(id, key, charsmax(key)))
		TrieDeleteKey(g_mapCounts, key);
}

public cmd_buy_frag(id)
{
	new TeamName:team = TeamName:get_member(id, m_iTeam);

	if (team != TEAM_TERRORIST && team != TEAM_CT)
	{
		client_print_color(id, print_team_default, "%L", id, "BF_NO_TEAM");
		return PLUGIN_HANDLED;
	}

	if (g_roundLimit > 0 && g_roundCount[id] >= g_roundLimit)
	{
		client_print_color(id, print_team_default, "%L", id, "BF_ROUND_LIMIT", g_roundLimit);
		return PLUGIN_HANDLED;
	}

	new key[64], bought;
	player_key(id, key, charsmax(key));
	TrieGetCell(g_mapCounts, key, bought);

	new price = g_basePrice + g_priceStep * bought;
	new money = cs_get_user_money(id);

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

	cs_set_user_money(id, money - price);
	set_user_frags(id, get_user_frags(id) + 1);
	update_scoreboard(id);

	TrieSetCell(g_mapCounts, key, bought + 1);
	g_roundCount[id]++;

	log_purchase(id, price);
	client_print_color(id, print_team_default, "%L", id, "BF_BOUGHT", price, g_basePrice + g_priceStep * (bought + 1));
	return PLUGIN_HANDLED;
}

/**
 * The key the map count is kept under. Returns true when it is a slot key
 * (bots, LAN or pending IDs), which must not outlive the connection.
 */
bool:player_key(id, key[], length)
{
	new authid[44];
	get_user_authid(id, authid, charsmax(authid));

	if (is_user_bot(id) || !authid[0] || equal(authid, "BOT") || equal(authid, "STEAM_ID_LAN")
		|| equal(authid, "VALVE_ID_LAN") || equal(authid, "STEAM_ID_PENDING") || equal(authid, "VALVE_ID_PENDING"))
	{
		formatex(key, length, "#slot%d", id);
		return true;
	}

	copy(key, length, authid);
	return false;
}

/**
 * Sends the player's new score to every client, as the game does after a
 * kill.
 */
update_scoreboard(id)
{
	message_begin(MSG_BROADCAST, g_msgScoreInfo);
	write_byte(id);
	write_short(get_user_frags(id));
	write_short(get_member(id, m_iDeaths));
	write_short(0);
	write_short(get_member(id, m_iTeam));
	message_end();
}

/**
 * A standard Half-Life log line with the player and the price.
 */
log_purchase(id, price)
{
	new name[MAX_NAME_LENGTH], authid[44], team[16];
	get_user_name(id, name, charsmax(name));
	get_user_authid(id, authid, charsmax(authid));
	get_user_team(id, team, charsmax(team));

	log_message("^"%s<%d><%s><%s>^" triggered ^"buy_frag^" (price ^"%d^")", name, get_user_userid(id), authid, team, price);
}

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.