Skip to content
View in the app

A better way to browse. Learn more.

Learn more

buy-a-bomb/addons/amxmodx/scripting/olds_buybomb.sma

olds_buybomb.sma
243 lines · 6.4 KB · Pawn (AMX Mod X)
Raw
/*
 * Buy a Bomb (olds.ro workshop card LIB-0142)
 *
 * A Terrorist can buy the bomb when nobody on the team carries it, early in
 * the round, on a map with a bombsite.
 *
 * Commands:
 *   say /buybomb, say_team /buybomb   buy the bomb
 *   c4 (console, like the game's buy aliases)   the same
 *
 * Cvars (in configs/plugins/olds_buybomb.cfg, which ships with the plugin;
 * if the file is missing, the plugin writes it with these defaults the first
 * time it starts):
 *   olds_bb_price    3000  Price of the bomb.
 *   olds_bb_maxtime  60    The bomb can be bought during freeze time and
 *                          this many seconds after it (0 = any time in the
 *                          round).
 *
 * Rules:
 *   - Only a living Terrorist, only on a map with a bombsite, only while no
 *     player holds the bomb and no bomb is planted, and only with enough
 *     money.
 *   - If the bomb lies on the ground somewhere (dropped, or stuck where
 *     nobody can reach it), buying brings that bomb to the buyer, through
 *     the game's own pick-up; there is never a second bomb.
 *   - The bought bomb is a normal bomb: the icon, the backpack, the radar
 *     and planting all work as usual. The game's own pick-up sound
 *     (items/gunpickup2.wav) plays.
 *   - A refused purchase changes nothing and says why.
 *
 * How it works, as the card asks: the money is taken with
 * cs_set_user_money, the bomb is given with ReAPI's rg_give_item (the
 * game's own give, the same as give_item) and m_bHasC4 is set.
 *
 * Requires AMX Mod X 1.10 with ReAPI (ReGameDLL_CS) and the cstrike and
 * hamsandwich 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 <hamsandwich>
#include <reapi>

#pragma semicolon 1

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

new g_price;           // olds_bb_price
new g_maxTime;         // olds_bb_maxtime

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

	// The bomb members 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_buybomb.txt");

	bind_pcvar_num(create_cvar("olds_bb_price", "3000", FCVAR_NONE,
		"Price of the bomb",
		true, 0.0, true, 1000000.0), g_price);

	bind_pcvar_num(create_cvar("olds_bb_maxtime", "60", FCVAR_NONE,
		"The bomb can be bought during freeze time and this many seconds after it (0 = any time)",
		true, 0.0, true, 3600.0), g_maxTime);

	AutoExecConfig(true, "olds_buybomb");

	register_clcmd("say /buybomb", "cmd_buy");
	register_clcmd("say_team /buybomb", "cmd_buy");
	register_clcmd("c4", "cmd_buy");
}

public cmd_buy(id)
{
	if (!is_user_alive(id))
	{
		client_print_color(id, print_team_red, "%L", id, "BB_DEAD");
		return PLUGIN_HANDLED;
	}

	if (TeamName:get_member(id, m_iTeam) != TEAM_TERRORIST)
	{
		client_print_color(id, print_team_red, "%L", id, "BB_NOT_T");
		return PLUGIN_HANDLED;
	}

	if (!get_member_game(m_bMapHasBombTarget))
	{
		client_print_color(id, print_team_red, "%L", id, "BB_NO_SITE");
		return PLUGIN_HANDLED;
	}

	new holder = find_holder();

	if (holder)
	{
		new name[MAX_NAME_LENGTH];
		get_user_name(holder, name, charsmax(name));
		client_print_color(id, print_team_red, "%L", id, holder == id ? "BB_YOURS" : "BB_HELD", name);
		return PLUGIN_HANDLED;
	}

	if (find_planted())
	{
		client_print_color(id, print_team_red, "%L", id, "BB_PLANTED");
		return PLUGIN_HANDLED;
	}

	if (g_maxTime > 0 && !get_member_game(m_bFreezePeriod)
		&& get_gametime() - Float:get_member_game(m_fRoundStartTime) > float(g_maxTime))
	{
		client_print_color(id, print_team_red, "%L", id, "BB_TOO_LATE", g_maxTime);
		return PLUGIN_HANDLED;
	}

	new money = cs_get_user_money(id);

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

	new box = find_dropped_box();

	if (box)
	{
		// The bomb on the ground comes to the buyer: the game's own pick-up.
		ExecuteHamB(Ham_Touch, box, id);

		if (!get_member(id, m_bHasC4))
		{
			client_print_color(id, print_team_red, "%L", id, "BB_MOVING");
			return PLUGIN_HANDLED;
		}
	}
	else
	{
		if (rg_give_item(id, "weapon_c4") <= 0)
		{
			client_print_color(id, print_team_red, "%L", id, "BB_FAILED");
			return PLUGIN_HANDLED;
		}

		set_member(id, m_bHasC4, true);
	}

	cs_set_user_money(id, money - g_price);
	client_print_color(id, print_team_default, "%L", id, box ? "BB_FETCHED" : "BB_BOUGHT", g_price);
	log_buy(id);
	return PLUGIN_HANDLED;
}

/**
 * The living player holding the bomb, 0 if none.
 */
find_holder()
{
	for (new id = 1; id <= MaxClients; id++)
	{
		if (is_user_alive(id) && (get_member(id, m_bHasC4) || user_has_c4(id)))
			return id;
	}

	return 0;
}

bool:user_has_c4(id)
{
	new item = get_member(id, m_rgpPlayerItems, C4_SLOT);

	while (item > 0)
	{
		if (get_member(item, m_iId) == CSW_C4)
			return true;

		item = get_member(item, m_pNext);
	}

	return false;
}

/**
 * A planted bomb, 0 if none.
 */
find_planted()
{
	new entity = MaxClients;

	while ((entity = rg_find_ent_by_class(entity, "grenade")) > 0)
	{
		if (get_member(entity, m_Grenade_bIsC4))
			return entity;
	}

	return 0;
}

/**
 * The weaponbox of a bomb lying on the ground, 0 if none.
 */
find_dropped_box()
{
	new entity = MaxClients;

	while ((entity = rg_find_ent_by_class(entity, "weapon_c4")) > 0)
	{
		new owner = get_entvar(entity, var_owner);

		if (owner > MaxClients && is_entity(owner))
			return owner;
	}

	return 0;
}

log_buy(id)
{
	new name[MAX_NAME_LENGTH], authId[MAX_AUTHID_LENGTH];
	get_user_name(id, name, charsmax(name));
	get_user_authid(id, authId, charsmax(authId));

	log_message("^"%s<%d><%s><TERRORIST>^" triggered ^"buy_bomb^" (price ^"%d^")", name, get_user_userid(id), authId, g_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.