/*
* Hostage Down Alert (olds.ro workshop card LIB-0864)
*
* Hostage Rescue: when a hostage dies, the chosen team hears the "hostage down"
* radio and reads a chat line with the hostage's number, who killed it and how
* many hostages are left.
*
* Cvars
* olds_hdown_radio 1 play the stock radio (%!MRAD_HOSDOWN, radio/hosdown.wav)
* olds_hdown_name 1 name the killer (0 = only "hostage #n is down")
* olds_hdown_team 3 who is told: 1 counter-terrorists, 2 terrorists, 3 both
* Only living players of those teams are told (dead players and spectators are
* skipped).
*
* amx_hdown_last (ADMIN_KICK) the last alert and who received it
*
* The killer: a player (or the player who owns the grenade or entity that did
* it); "the world" for falls and map damage; "an unknown killer" otherwise.
* A death is seen in Ham_TakeDamage (the hostage's health falls to 0) and in
* Ham_Killed, once per hostage per round. ReAPI has no hostage death hook.
*
* Copyright (C) 2026 daemon (olds.ro)
* SPDX-License-Identifier: GPL-3.0-or-later
*/
#include <amxmodx>
#include <amxmisc>
#include <fakemeta>
#include <hamsandwich>
#include <reapi>
#define PLUGIN "Hostage Down Alert"
#define VERSION "1.0.0"
#define AUTHOR "olds.ro"
const EDICTS = 2049
new const RADIO[] = "%!MRAD_HOSDOWN"
new g_radio, g_name, g_team
new g_msgSendAudio
new bool:g_alive[EDICTS] // health before the hit
new bool:g_reported[EDICTS]
new g_last[192]
new g_lastTo[256]
public plugin_init()
{
register_plugin(PLUGIN, VERSION, AUTHOR)
register_dictionary("olds_hdown.txt")
bind_pcvar_num(create_cvar("olds_hdown_radio", "1", FCVAR_NONE,
"1 = play the hostage down radio to the players told.", true, 0.0, true, 1.0), g_radio)
bind_pcvar_num(create_cvar("olds_hdown_name", "1", FCVAR_NONE,
"1 = name the killer in the chat line.", true, 0.0, true, 1.0), g_name)
bind_pcvar_num(create_cvar("olds_hdown_team", "3", FCVAR_NONE,
"Who is told: 1 counter-terrorists, 2 terrorists, 3 both.", true, 1.0, true, 3.0), g_team)
AutoExecConfig(true, "olds_hdown")
g_msgSendAudio = get_user_msgid("SendAudio")
RegisterHam(Ham_TakeDamage, "hostage_entity", "on_damage_pre", false)
RegisterHam(Ham_TakeDamage, "hostage_entity", "on_damage_post", true)
RegisterHam(Ham_Killed, "hostage_entity", "on_killed_post", true)
RegisterHookChain(RG_CSGameRules_RestartRound, "on_restart_round_post", true)
register_concmd("amx_hdown_last", "cmd_last", ADMIN_KICK, "- the last hostage down alert")
copy(g_last, charsmax(g_last), "none yet")
}
public on_restart_round_post()
{
for (new ent = 0; ent < EDICTS; ent++)
g_reported[ent] = false
}
bool:in_range(ent)
{
return 0 < ent < EDICTS
}
public on_damage_pre(ent, inflictor, attacker, Float:damage, bits)
{
if (in_range(ent))
g_alive[ent] = Float:get_entvar(ent, var_health) > 0.0
}
public on_damage_post(ent, inflictor, attacker, Float:damage, bits)
{
if (in_range(ent) && g_alive[ent] && Float:get_entvar(ent, var_health) <= 0.0)
hostage_down(ent, attacker, inflictor)
}
public on_killed_post(ent, attacker, shouldGib)
{
if (in_range(ent))
hostage_down(ent, attacker, 0)
}
// The player behind an attacker or inflictor, or 0.
player_behind(ent)
{
if (1 <= ent <= MaxClients)
return is_user_connected(ent) ? ent : 0
if (ent > MaxClients && pev_valid(ent))
{
new owner = get_entvar(ent, var_owner)
if (1 <= owner <= MaxClients && is_user_connected(owner))
return owner
}
return 0
}
bool:is_world(ent)
{
if (ent <= 0)
return true
if (ent <= MaxClients || !pev_valid(ent))
return false
new classname[32]
get_entvar(ent, var_classname, classname, charsmax(classname))
return equal(classname, "worldspawn") || equal(classname, "trigger_hurt") || equal(classname, "func_door")
|| equal(classname, "func_door_rotating") || equal(classname, "func_train") || equal(classname, "func_tracktrain")
}
// Number of the hostage on the map (1..), and how many are still alive.
hostage_numbers(victim, &left)
{
new ent = MaxClients, index, number
left = 0
while ((ent = rg_find_ent_by_class(ent, "hostage_entity")) > 0)
{
index++
if (ent == victim)
number = index
else if (Float:get_entvar(ent, var_health) > 0.0 && get_entvar(ent, var_deadflag) == DEAD_NO
&& !(get_entvar(ent, var_effects) & EF_NODRAW))
left++
}
return number
}
hostage_down(ent, attacker, inflictor)
{
if (g_reported[ent])
return
g_reported[ent] = true
new left, number = hostage_numbers(ent, left)
new killer = player_behind(attacker)
if (!killer)
killer = player_behind(inflictor)
new bool:world = !killer && is_world(attacker)
new killerName[MAX_NAME_LENGTH]
if (killer)
get_user_name(killer, killerName, charsmax(killerName))
new told, toNames[256]
for (new id = 1; id <= MaxClients; id++)
{
if (!is_user_alive(id))
continue
new TeamName:team = TeamName:get_member(id, m_iTeam)
new bool:wanted = (team == TEAM_CT && (g_team & 1)) || (team == TEAM_TERRORIST && (g_team & 2))
if (!wanted)
continue
if (g_radio)
radio(id)
new line[192]
alert_line(id, line, charsmax(line), number, left, killer, killerName, world, true)
if (killer)
client_print_color(id, killer, "^4[Hostage]^1 %s", line)
else
client_print_color(id, print_team_default, "^4[Hostage]^1 %s", line)
new name[MAX_NAME_LENGTH]
get_user_name(id, name, charsmax(name))
if (told)
add(toNames, charsmax(toNames), ", ")
add(toNames, charsmax(toNames), name)
told++
}
alert_line(LANG_SERVER, g_last, charsmax(g_last), number, left, killer, killerName, world, false)
if (told)
formatex(g_lastTo, charsmax(g_lastTo), "%d player(s): %s", told, toNames)
else
copy(g_lastTo, charsmax(g_lastTo), "nobody")
log_amx("%s (told %s)", g_last, g_lastTo)
}
alert_line(id, line[], len, number, left, killer, const killerName[], bool:world, bool:colors)
{
new who[64]
if (killer)
{
if (colors)
formatex(who, charsmax(who), "^3%s^1", killerName)
else
copy(who, charsmax(who), killerName)
}
else if (world)
formatex(who, charsmax(who), "%L", id, "HDOWN_WORLD")
else
formatex(who, charsmax(who), "%L", id, "HDOWN_UNKNOWN")
if (g_name)
formatex(line, len, "%L", id, "HDOWN_KILLED_BY", number, who)
else
formatex(line, len, "%L", id, "HDOWN_DOWN", number)
new tail[64]
if (left)
formatex(tail, charsmax(tail), " %L", id, "HDOWN_LEFT", left)
else
formatex(tail, charsmax(tail), " %L", id, "HDOWN_NONE_LEFT")
add(line, len, tail)
}
radio(id)
{
message_begin(MSG_ONE, g_msgSendAudio, _, id)
write_byte(id)
write_string(RADIO)
write_short(PITCH_NORM)
message_end()
}
public cmd_last(id, level, cid)
{
if (!cmd_access(id, level, cid, 1))
return PLUGIN_HANDLED
console_print(id, "[Hostage] last alert: %s", g_last)
console_print(id, "[Hostage] told: %s", g_lastTo)
console_print(id, "[Hostage] radio %d, name %d, team %d", g_radio, g_name, g_team)
return PLUGIN_HANDLED
}