Skip to content
View in the app

A better way to browse. Learn more.

Learn more

teammate-grenade-damage-off/addons/amxmodx/scripting/olds_he_nofriendly.sma

olds_he_nofriendly.sma
225 lines · 7.4 KB · Pawn (AMX Mod X)
Raw
/*
 * Teammate Grenade Damage Off (olds.ro workshop card LIB-2266)
 *
 * HE grenades stop hurting the thrower's teammates and the thrower, while
 * ordinary friendly fire (bullets, knife) keeps working. Enemies take the
 * full blast as usual, and a kill made by the grenade is credited as usual.
 *
 * Run it with mp_friendlyfire 1. With mp_friendlyfire 0 the game already
 * spares teammates from HE grenades, and only olds_henofd_self has an effect.
 * In free-for-all (mp_freeforall 1) nobody is a teammate, so only
 * olds_henofd_self has an effect.
 *
 * A teammate is a player on the team the grenade was thrown for, which is
 * the thrower's team at the moment of the throw. This is the rule the game
 * itself uses for HE friendly fire, and it stays right when the thrower
 * leaves and another player takes the same slot before the blast.
 *
 * Cvars (configs/plugins/olds_he_nofriendly.cfg; AutoExecConfig writes that
 * file with these defaults if it is missing):
 *   olds_henofd_self  1   1 = a player's own HE grenade does not hurt them.
 *   olds_henofd_team  1   1 = an HE grenade does not hurt the thrower's teammates.
 *   olds_henofd_knock 0   1 = a player spared by this plugin is still pushed
 *                         away from the blast. Counter-Strike itself never
 *                         pushes a living player with a blast, so the plugin
 *                         applies the Half-Life blast push (the DamageForce
 *                         formula of the game code) for the blocked damage.
 *
 * Players see no text from this plugin. The one server console notice comes
 * from data/lang/olds_he_nofriendly.txt (English).
 *
 * Requires AMX Mod X 1.10 or newer and ReAPI with ReGameDLL_CS. Written and
 * tested on AMX Mod X 1.10.0.5490, ReAPI 5.29.0.358, 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 <reapi>

#pragma semicolon 1

new const PLUGIN_NAME[] = "Teammate Grenade Damage Off";
new const PLUGIN_VERSION[] = "1.0.0";
new const PLUGIN_AUTHOR[] = "alexit (olds.ro)";

new const DICTIONARY[] = "olds_he_nofriendly.txt";

// Volume of a standing player (32 x 32 x 72 units) and the strongest push,
// both taken from the game's CBaseMonster::DamageForce.
const Float:STANDARD_VOLUME = 73728.0;
const Float:FORCE_PER_DAMAGE = 5.0;
const Float:MAX_FORCE = 1000.0;

// The game aims the blast 10 units below the centre of the grenade.
const Float:BLAST_DROP = 10.0;

new g_blockSelf;     // olds_henofd_self
new g_blockTeam;     // olds_henofd_team
new g_knock;         // olds_henofd_knock

// mp_freeforall of ReGameDLL; stays 0 if the cvar does not exist.
new g_freeForAll;

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

	bind_pcvar_num(create_cvar("olds_henofd_self", "1", FCVAR_NONE,
		"1 = a player's own HE grenade does not hurt them",
		true, 0.0, true, 1.0), g_blockSelf);

	bind_pcvar_num(create_cvar("olds_henofd_team", "1", FCVAR_NONE,
		"1 = an HE grenade does not hurt the thrower's teammates",
		true, 0.0, true, 1.0), g_blockTeam);

	bind_pcvar_num(create_cvar("olds_henofd_knock", "0", FCVAR_NONE,
		"1 = a player spared from HE damage is still pushed away from the blast",
		true, 0.0, true, 1.0), g_knock);

	new freeForAll = get_cvar_pointer("mp_freeforall");

	if (freeForAll)
		bind_pcvar_num(freeForAll, g_freeForAll);

	AutoExecConfig(true, "olds_he_nofriendly");

	RegisterHookChain(RG_CBasePlayer_TakeDamage, "OnPlayerTakeDamage_Pre", false);
}

public plugin_cfg()
{
	if (!get_cvar_num("mp_friendlyfire"))
		server_print("[%s] %L", PLUGIN_NAME, LANG_SERVER, "OLDS_HENOFD_FF_OFF");
}

/**
 * Every hit a player takes passes here before the game applies it. Only HE
 * grenade damage (DMG_GRENADE) from the player's own grenade or from a
 * teammate's grenade is stopped; everything else goes on unchanged.
 */
public OnPlayerTakeDamage_Pre(victim, inflictor, attacker, Float:damage, damageBits)
{
	if (!(damageBits & DMG_GRENADE))
		return HC_CONTINUE;

	if (attacker == victim)
	{
		if (!g_blockSelf)
			return HC_CONTINUE;
	}
	else
	{
		if (!g_blockTeam || g_freeForAll)
			return HC_CONTINUE;

		new TeamName:victimTeam = get_member(victim, m_iTeam);

		if (victimTeam == TEAM_UNASSIGNED || attacker_team(inflictor, attacker) != victimTeam)
			return HC_CONTINUE;
	}

	if (g_knock && damage > 0.0 && is_user_alive(victim))
		push_from_blast(victim, inflictor, damage);

	// The game reads this as "no damage taken".
	SetHookChainReturn(ATYPE_INTEGER, false);
	return HC_SUPERCEDE;
}

/**
 * The attacker's team for this hit: the team the grenade was thrown for,
 * read from the grenade (the game's own HE friendly-fire rule). For damage
 * that does not come from a "grenade" entity (another plugin may use
 * DMG_GRENADE), the attacker's current team. TEAM_UNASSIGNED when neither
 * is known.
 */
TeamName:attacker_team(inflictor, attacker)
{
	if (inflictor > MaxClients && is_entity(inflictor))
	{
		new classname[16];
		get_entvar(inflictor, var_classname, classname, charsmax(classname));

		if (equal(classname, "grenade"))
			return TeamName:get_member(inflictor, m_Grenade_iTeam);
	}

	if (attacker >= 1 && attacker <= MaxClients && is_user_connected(attacker))
		return TeamName:get_member(attacker, m_iTeam);

	return TEAM_UNASSIGNED;
}

/**
 * Pushes the player away from the blast with the strength the damage would
 * have had: the game's DamageForce (damage x 5 for a standing player, more
 * for a smaller, crouched hull, at most 1000 units per second), along the
 * line from just below the grenade to the player's centre.
 */
push_from_blast(victim, inflictor, Float:damage)
{
	if (inflictor <= 0 || !is_entity(inflictor))
		return;

	new Float:size[3];
	get_entvar(victim, var_size, size);

	new Float:volume = size[0] * size[1] * size[2];

	if (volume <= 0.0)
		return;

	new Float:force = floatmin(damage * (STANDARD_VOLUME / volume) * FORCE_PER_DAMAGE, MAX_FORCE);

	new Float:blast[3], Float:center[3], Float:direction[3];
	get_center(inflictor, blast);
	get_center(victim, center);
	blast[2] -= BLAST_DROP;

	for (new i = 0; i < 3; i++)
		direction[i] = center[i] - blast[i];

	new Float:length = vector_length(direction);

	// A blast exactly at the centre has no direction; like the game's
	// Vector::Normalize, push straight up.
	if (length < 0.001)
	{
		direction[0] = 0.0;
		direction[1] = 0.0;
		direction[2] = 1.0;
		length = 1.0;
	}

	new Float:velocity[3];
	get_entvar(victim, var_velocity, velocity);

	for (new i = 0; i < 3; i++)
		velocity[i] += direction[i] / length * force;

	set_entvar(victim, var_velocity, velocity);
}

// Centre of the entity's bounding box, as CBaseEntity::Center() gives it.
get_center(entity, Float:center[3])
{
	new Float:absMin[3], Float:absMax[3];
	get_entvar(entity, var_absmin, absMin);
	get_entvar(entity, var_absmax, absMax);

	for (new i = 0; i < 3; i++)
		center[i] = (absMin[i] + absMax[i]) * 0.5;
}

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.