Skip to content
View in the app

A better way to browse. Learn more.

Learn more

zero-hp-bug-fix/addons/amxmodx/scripting/olds_zerohp.sma

olds_zerohp.sma
353 lines · 10.4 KB · Pawn (AMX Mod X)
Raw
/*
 * Zero HP Bug Fix (olds.ro workshop card LIB-2045)
 *
 * Sometimes a player stays alive with 0 HP or less, for example when a plugin
 * sets the health to 0 (or to less than 1, which the HUD of the original game
 * shows as 0) without killing the player. This plugin finds such players and
 * kills them through the game's normal kill path, credited to the last player
 * who hurt them, so frags, money, the kill feed (DeathMsg) and the game log
 * are right.
 *
 *   - Right after every hit (RG_CBasePlayer_TakeDamage post) the victim is
 *     checked, and every 0.5 seconds all living players are checked.
 *   - The killer is the player who caused the victim's last damage, with the
 *     weapon of that hit; with no such player still on the server, the death
 *     counts as a death by the world (like a fall), without a frag.
 *   - During freeze time nothing is done; a player found at 0 HP then is
 *     killed when the freeze time ends.
 *   - Every fix can be written to addons/amxmodx/logs/olds_zerohp.log with
 *     the player's and the killer's SteamID and the weapon.
 *
 * Cvars (configs/plugins/olds_zerohp.cfg; AutoExecConfig writes that file
 * with these defaults if it is missing):
 *   olds_zerohp_fix 1  1 = kill players left alive with 0 HP or less, 0 = off.
 *   olds_zerohp_log 1  1 = write one line per fix to olds_zerohp.log, 0 = no log.
 *
 * The log text comes from data/lang/olds_zerohp.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 <hamsandwich>
#include <reapi>

#pragma semicolon 1

new const PLUGIN_NAME[] = "Zero HP Bug Fix";
new const PLUGIN_VERSION[] = "1.0.0";
new const PLUGIN_AUTHOR[] = "alexit (olds.ro)";

new const DICTIONARY[] = "olds_zerohp.txt";
new const LOG_FILE[] = "olds_zerohp.log";

// Seconds between two checks of all living players.
const Float:CHECK_INTERVAL = 0.5;

const TASK_CHECK = 42450;

new g_fix;     // olds_zerohp_fix
new g_log;     // olds_zerohp_log

// The last damage each player took since their spawn.
new g_hitAttacker[MAX_PLAYERS + 1];             // the player who caused it, 0 = none
new g_hitAttackerUserId[MAX_PLAYERS + 1];       // that player's userid, to spot a new player in the slot
new g_hitInflictor[MAX_PLAYERS + 1];            // the entity that did it (gun owner, grenade...), 0 = world
new g_hitInflictorClass[MAX_PLAYERS + 1][32];   // its classname at the time
new g_hitWeapon[MAX_PLAYERS + 1][32];           // the weapon as the kill feed names it

// A forced death another plugin stopped: not retried before the next spawn.
new bool:g_blocked[MAX_PLAYERS + 1];

// The death being forced right now (0 = none) and its inflictor.
new g_forcedVictim;
new g_forcedInflictor;

new HookChain:g_hookPlayerKilled;
new HookChain:g_hookDeathMessage;

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

	bind_pcvar_num(create_cvar("olds_zerohp_fix", "1", FCVAR_NONE,
		"1 = kill players left alive with 0 HP or less, 0 = off",
		true, 0.0, true, 1.0), g_fix);

	bind_pcvar_num(create_cvar("olds_zerohp_log", "1", FCVAR_NONE,
		"1 = write one line per fix to addons/amxmodx/logs/olds_zerohp.log, 0 = no log",
		true, 0.0, true, 1.0), g_log);

	AutoExecConfig(true, "olds_zerohp");

	RegisterHookChain(RG_CBasePlayer_TakeDamage, "OnPlayerTakeDamage_Post", true);
	RegisterHookChain(RG_CBasePlayer_Spawn, "OnPlayerSpawn_Post", true);
	RegisterHookChain(RG_CSGameRules_OnRoundFreezeEnd, "OnRoundFreezeEnd_Post", true);

	// Only on while this plugin forces a death.
	g_hookPlayerKilled = RegisterHookChain(RG_CSGameRules_PlayerKilled, "OnPlayerKilled_Pre", false);
	g_hookDeathMessage = RegisterHookChain(RG_CSGameRules_SendDeathMessage, "OnSendDeathMessage_Pre", false);
	DisableHookChain(g_hookPlayerKilled);
	DisableHookChain(g_hookDeathMessage);

	set_task(CHECK_INTERVAL, "task_check", TASK_CHECK, _, _, "b");
}

public client_putinserver(id)
{
	clear_player(id);
}

public client_disconnected(id)
{
	clear_player(id);
}

public OnPlayerSpawn_Post(id)
{
	if (is_user_alive(id))
		clear_player(id);
}

public OnPlayerTakeDamage_Post(victim, inflictor, attacker, Float:damage, damageBits)
{
	// No damage taken (blocked hit), or already dead.
	if (!GetHookChainReturn(ATYPE_INTEGER) || !is_user_connected(victim))
		return;

	remember_hit(victim, inflictor, attacker);

	if (g_fix && !g_forcedVictim && !in_freeze_time() && has_zero_hp(victim))
		force_death(victim);
}

public task_check()
{
	if (!g_fix || g_forcedVictim || in_freeze_time())
		return;

	for (new id = 1; id <= MaxClients; id++)
	{
		if (has_zero_hp(id))
			force_death(id);
	}
}

public OnRoundFreezeEnd_Post()
{
	task_check();
}

/**
 * The forced death goes through the game with the inflictor of the last hit,
 * so the game names the right weapon and logs the kill as usual.
 */
public OnPlayerKilled_Pre(victim, killer, inflictor)
{
	// ReAPI passes the inflictor as an entity index (0 = the world).
	if (victim == g_forcedVictim)
		SetHookChainArg(3, ATYPE_INTEGER, g_forcedInflictor);
}

/**
 * The kill feed shows the weapon of the last hit, even if the grenade that
 * did it is gone or the killer has switched weapons since.
 */
public OnSendDeathMessage_Pre(killer, victim, assister, inflictor, const weaponName[], flags, rarity)
{
	if (victim == g_forcedVictim && g_hitWeapon[victim][0])
		SetHookChainArg(5, ATYPE_STRING, g_hitWeapon[victim]);
}

/**
 * Alive by the game (not dying, not watching) but with less than 1 HP:
 * get_user_health() shows 0 or less.
 */
bool:has_zero_hp(id)
{
	if (!is_user_connected(id) || g_blocked[id])
		return false;

	if (get_entvar(id, var_deadflag) != DEAD_NO || get_entvar(id, var_iuser1) != OBS_NONE)
		return false;

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

	if (team != TEAM_TERRORIST && team != TEAM_CT)
		return false;

	return Float:get_entvar(id, var_health) < 1.0;
}

bool:in_freeze_time()
{
	return bool:get_member_game(m_bFreezePeriod);
}

remember_hit(victim, inflictor, attacker)
{
	if (attacker >= 1 && attacker <= MaxClients && is_user_connected(attacker))
	{
		g_hitAttacker[victim] = attacker;
		g_hitAttackerUserId[victim] = get_user_userid(attacker);
	}
	else
	{
		g_hitAttacker[victim] = 0;
		g_hitAttackerUserId[victim] = 0;
	}

	if (inflictor > 0 && is_entity(inflictor))
	{
		g_hitInflictor[victim] = inflictor;
		get_entvar(inflictor, var_classname, g_hitInflictorClass[victim], charsmax(g_hitInflictorClass[]));
	}
	else
	{
		g_hitInflictor[victim] = 0;
		g_hitInflictorClass[victim][0] = EOS;
	}

	killer_weapon_name(victim, g_hitInflictor[victim], g_hitAttacker[victim], g_hitWeapon[victim], charsmax(g_hitWeapon[]));
}

/**
 * The weapon name the game puts in the kill feed: the attacker's weapon in
 * hand when he hit directly, else the inflictor (grenade...), without the
 * weapon_, monster_ or func_ prefix.
 */
killer_weapon_name(victim, inflictor, attacker, name[], len)
{
	copy(name, len, "world");

	if (attacker)
	{
		if (inflictor == attacker)
		{
			if (attacker != victim)
			{
				new item = get_member(attacker, m_pActiveItem);

				if (item > 0)
					get_entvar(item, var_classname, name, len);
			}
		}
		else if (inflictor)
		{
			get_entvar(inflictor, var_classname, name, len);
		}
	}
	else if (inflictor)
	{
		get_entvar(inflictor, var_classname, name, len);
	}

	static const prefixes[][] = { "weapon_", "monster_", "func_" };

	for (new i = 0; i < sizeof(prefixes); i++)
	{
		new prefixLength = strlen(prefixes[i]);

		if (equal(name, prefixes[i], prefixLength))
		{
			copy(name, len, name[prefixLength]);
			break;
		}
	}
}

force_death(victim)
{
	// The killer: the player behind the last damage, if still on the server.
	new killer = g_hitAttacker[victim];

	if (killer && (!is_user_connected(killer) || get_user_userid(killer) != g_hitAttackerUserId[victim]))
		killer = 0;

	// The inflictor: the one of the last hit while it still exists (a
	// grenade is removed soon after it explodes), else the killer himself.
	new inflictor = killer;
	new stored = g_hitInflictor[victim];

	if (stored > MaxClients && is_entity(stored))
	{
		new className[32];
		get_entvar(stored, var_classname, className, charsmax(className));

		if (equal(className, g_hitInflictorClass[victim]))
			inflictor = stored;
	}

	new Float:health = Float:get_entvar(victim, var_health);

	g_forcedVictim = victim;
	g_forcedInflictor = inflictor;
	EnableHookChain(g_hookPlayerKilled);

	if (killer)
		EnableHookChain(g_hookDeathMessage);

	ExecuteHamB(Ham_Killed, victim, killer, GIB_NORMAL);

	DisableHookChain(g_hookPlayerKilled);
	DisableHookChain(g_hookDeathMessage);
	g_forcedVictim = 0;

	// Another plugin stopped the death: leave this player alone until the next spawn.
	if (is_user_connected(victim) && get_entvar(victim, var_deadflag) == DEAD_NO)
	{
		g_blocked[victim] = true;
		return;
	}

	if (g_log)
		log_fix(victim, killer, health);
}

log_fix(victim, killer, Float:health)
{
	new victimName[MAX_NAME_LENGTH], victimAuth[MAX_AUTHID_LENGTH];
	get_user_name(victim, victimName, charsmax(victimName));
	get_user_authid(victim, victimAuth, charsmax(victimAuth));

	if (killer)
	{
		new killerName[MAX_NAME_LENGTH], killerAuth[MAX_AUTHID_LENGTH];
		get_user_name(killer, killerName, charsmax(killerName));
		get_user_authid(killer, killerAuth, charsmax(killerAuth));

		log_to_file(LOG_FILE, "%L", LANG_SERVER, "OLDS_ZEROHP_LOG_KILLER",
			victimName, get_user_userid(victim), victimAuth, health,
			killerName, get_user_userid(killer), killerAuth, g_hitWeapon[victim]);
	}
	else
	{
		log_to_file(LOG_FILE, "%L", LANG_SERVER, "OLDS_ZEROHP_LOG_WORLD",
			victimName, get_user_userid(victim), victimAuth, health);
	}
}

clear_player(id)
{
	g_hitAttacker[id] = 0;
	g_hitAttackerUserId[id] = 0;
	g_hitInflictor[id] = 0;
	g_hitInflictorClass[id][0] = EOS;
	g_hitWeapon[id][0] = EOS;
	g_blocked[id] = false;
}

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.