Skip to content
View in the app

A better way to browse. Learn more.

Learn more

ammo-for-hp/addons/amxmodx/scripting/olds_ammohp.sma

olds_ammohp.sma
202 lines · 6.7 KB · Pawn (AMX Mod X)
Raw
/*
 * Ammo for HP (olds.ro workshop card LIB-0073)
 *
 * A player can trade a little health for one magazine of backpack ammo for
 * the weapon in his hands, with a chat command or a key.
 *
 * Commands:
 *   say /ammo, say_team /ammo   trade olds_ah_cost health for one magazine
 *   +ammo                       the same from a key (bind x +ammo)
 *
 * Refused, with a chat line and no health taken:
 *   - dead, or holding the knife, a grenade or the bomb;
 *   - health at olds_ah_minhp or lower, so health never comes near zero;
 *   - olds_ah_limit trades already made this round;
 *   - the backpack is already full for that weapon.
 * When the backpack has room for less than a magazine, it is filled up.
 *
 * Cvars (in configs/plugins/olds_ammohp.cfg, which ships with the plugin; if
 * the file is missing, the plugin writes it with these defaults the first
 * time it starts):
 *   olds_ah_cost   15   Health taken for one magazine. Health never drops
 *                       below 1.
 *   olds_ah_limit  3    Trades per player per round (0 = no limit).
 *   olds_ah_minhp  20   A player with this much health or less cannot
 *                       trade.
 *
 * How it works, as the card asks: the commands read the active weapon, take
 * the health (set directly, never as damage, so the trade can never kill or
 * count as a death) and add one magazine with cs_get_user_bpammo /
 * cs_set_user_bpammo. The magazine size and the backpack limit are the
 * game's own (rg_get_weapon_info: WI_GUN_CLIP_SIZE, WI_MAX_ROUNDS). Each
 * trade is written to the server log as a non-fatal event:
 *   "Name<uid><steamid><TEAM>" triggered "ammo_for_hp" (health "15") (ammo "30")
 * The per-round count is cleared in RG_CSGameRules_RestartRound. The chat
 * text is in data/lang/olds_ammohp.txt; the sound is the stock
 * items/9mmclip1.wav, played with "spk" for the player 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[] = "Ammo for HP";
new const PLUGIN_VERSION[] = "1.0.0";
new const PLUGIN_AUTHOR[] = "alexit (olds.ro)";

// Stock sound, played on the player's client with "spk" (no download).
new const AMMO_SOUND[] = "items/9mmclip1";

// Weapons that have no backpack ammo to buy.
const NO_AMMO_WEAPONS = (1 << CSW_KNIFE) | (1 << CSW_HEGRENADE) | (1 << CSW_FLASHBANG) | (1 << CSW_SMOKEGRENADE) | (1 << CSW_C4);

new g_cost;            // olds_ah_cost
new g_limit;           // olds_ah_limit
new g_minHealth;       // olds_ah_minhp

// Trades made by each player in this round.
new g_trades[MAX_PLAYERS + 1];

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

	// The weapon info and the round hook 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_ammohp.txt");

	bind_pcvar_num(create_cvar("olds_ah_cost", "15", FCVAR_NONE,
		"Health taken for one magazine (health never drops below 1)",
		true, 1.0, true, 255.0), g_cost);

	bind_pcvar_num(create_cvar("olds_ah_limit", "3", FCVAR_NONE,
		"Trades per player per round (0 = no limit)",
		true, 0.0), g_limit);

	bind_pcvar_num(create_cvar("olds_ah_minhp", "20", FCVAR_NONE,
		"A player with this much health or less cannot trade",
		true, 0.0, true, 255.0), g_minHealth);

	AutoExecConfig(true, "olds_ammohp");

	register_clcmd("say /ammo", "cmd_ammo");
	register_clcmd("say_team /ammo", "cmd_ammo");
	register_clcmd("+ammo", "cmd_ammo");
	register_clcmd("-ammo", "cmd_ammo_release");

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

/**
 * A new round: every player can trade again.
 */
public on_restart_round_post()
{
	arrayset(g_trades, 0, sizeof(g_trades));
	return HC_CONTINUE;
}

public client_disconnected(id)
{
	g_trades[id] = 0;
}

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

	new weapon = get_member(id, m_pActiveItem);
	new weaponId = (weapon > 0) ? get_member(weapon, m_iId) : 0;

	if (weaponId <= 0 || weaponId > CSW_P90 || (NO_AMMO_WEAPONS & (1 << weaponId)))
	{
		client_print_color(id, print_team_default, "%L", id, "AH_WRONG_WEAPON");
		return PLUGIN_HANDLED;
	}

	if (g_limit > 0 && g_trades[id] >= g_limit)
	{
		client_print_color(id, print_team_default, "%L", id, "AH_LIMIT", g_limit);
		return PLUGIN_HANDLED;
	}

	new health = get_user_health(id);

	if (health <= g_minHealth)
	{
		client_print_color(id, print_team_default, "%L", id, "AH_LOW_HEALTH", g_minHealth);
		return PLUGIN_HANDLED;
	}

	new ammo = cs_get_user_bpammo(id, weaponId);
	new room = rg_get_weapon_info(weaponId, WI_MAX_ROUNDS) - ammo;

	if (room <= 0)
	{
		client_print_color(id, print_team_default, "%L", id, "AH_FULL");
		return PLUGIN_HANDLED;
	}

	new added = min(rg_get_weapon_info(weaponId, WI_GUN_CLIP_SIZE), room);
	new newHealth = max(health - g_cost, 1);

	// Health is set, not dealt as damage: the trade can never kill.
	set_entvar(id, var_health, float(newHealth));
	cs_set_user_bpammo(id, weaponId, ammo + added);
	g_trades[id]++;

	log_trade(id, health - newHealth, added);

	client_print_color(id, print_team_default, "%L", id, "AH_TRADED", added, health - newHealth, newHealth);
	client_cmd(id, "spk %s", AMMO_SOUND);
	return PLUGIN_HANDLED;
}

/**
 * Releasing the +ammo key does nothing; registered so the client does not
 * print "unknown command".
 */
public cmd_ammo_release(id)
{
	return PLUGIN_HANDLED;
}

/**
 * A standard Half-Life log line, so stats tools see the trade as a
 * non-fatal event and not as damage or a death.
 */
log_trade(id, health, ammo)
{
	new name[MAX_NAME_LENGTH], authid[64], 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 ^"ammo_for_hp^" (health ^"%d^") (ammo ^"%d^")",
		name, get_user_userid(id), authid, team, health, ammo);
}

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.