/*
* Bomb Status HUD (olds.ro workshop card LIB-0118)
*
* A short HUD line tells where the bomb is: carried by a player, dropped
* near a part of the map, planted, or defused.
*
* Commands: none.
*
* Cvars (in configs/plugins/olds_bombstatus.cfg, which ships with the
* plugin; if the file is missing, the plugin writes it with these defaults
* the first time it starts):
* olds_bsh_x 0.02 Horizontal position of the line (0.0 left edge,
* 1.0 right edge, -1 centre).
* olds_bsh_y 0.2 Vertical position (0.0 top, 1.0 bottom).
* olds_bsh_team 0 Who sees the line: 0 both teams (and
* spectators), 1 Terrorists only, 2
* Counter-Terrorists only.
*
* Rules:
* - "Bomb: carried by <name>" while a player has it, also right after a
* round restart for the new carrier.
* - "Bomb: dropped near <place>" while it lies on the ground. The place is
* the nearest of the map's bombsites and the two spawn areas.
* Bombsites are lettered A, B... in the order the map file lists them,
* which may not match the letters players use on every map.
* - "Bomb: planted" from the plant until the round ends.
* - "Bomb: defused" for 5 seconds after the defuse, then it fades out.
* - Maps without a bombsite show nothing.
*
* How it works, as the card asks: the BombDrop, BombPickup and StatusIcon
* messages and the RG_PlantBomb and RG_CGrenade_DefuseBombEnd hooks change
* the state at once; a 1 s task draws the line (one fixed HUD sync object,
* so a new line replaces the old) and checks that the carrier still has
* the bomb.
*
* Requires AMX Mod X 1.10 with ReAPI (ReGameDLL_CS). 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 <reapi>
#pragma semicolon 1
new const PLUGIN_NAME[] = "Bomb Status HUD";
new const PLUGIN_VERSION[] = "1.0.0";
new const PLUGIN_AUTHOR[] = "alexit (olds.ro)";
const TASK_DRAW = 11800;
const Float:DRAW_EVERY = 1.0;
const Float:HUD_HOLD = 1.2;
const Float:DEFUSED_TIME = 5.0;
const Float:DEFUSED_FADE = 1.0;
const MAX_SITES = 4;
enum
{
BOMB_NONE = 0,
BOMB_CARRIED,
BOMB_DROPPED,
BOMB_PLANTED,
BOMB_DEFUSED
};
enum
{
PLACE_SITE = 0,
PLACE_T_SPAWN,
PLACE_CT_SPAWN
};
new Float:g_x; // olds_bsh_x
new Float:g_y; // olds_bsh_y
new g_team; // olds_bsh_team
new g_sync;
new g_state;
new g_carrier;
new Float:g_dropOrigin[3];
new Float:g_defusedAt;
// Places a dropped bomb is named after.
new g_siteCount;
new Float:g_siteOrigin[MAX_SITES][3];
new bool:g_haveTSpawn, Float:g_tSpawn[3];
new bool:g_haveCtSpawn, Float:g_ctSpawn[3];
public plugin_init()
{
register_plugin(PLUGIN_NAME, PLUGIN_VERSION, PLUGIN_AUTHOR);
// The bomb hooks 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_bombstatus.txt");
bind_pcvar_float(create_cvar("olds_bsh_x", "0.02", FCVAR_NONE,
"Horizontal position of the bomb line (0.0 left, 1.0 right, -1 centre)",
true, -1.0, true, 1.0), g_x);
bind_pcvar_float(create_cvar("olds_bsh_y", "0.2", FCVAR_NONE,
"Vertical position of the bomb line (0.0 top, 1.0 bottom)",
true, -1.0, true, 1.0), g_y);
bind_pcvar_num(create_cvar("olds_bsh_team", "0", FCVAR_NONE,
"Who sees the bomb line: 0 both teams, 1 Terrorists only, 2 Counter-Terrorists only",
true, 0.0, true, 2.0), g_team);
AutoExecConfig(true, "olds_bombstatus");
g_sync = CreateHudSyncObj();
register_message(get_user_msgid("BombDrop"), "on_bomb_drop");
register_message(get_user_msgid("BombPickup"), "on_bomb_pickup");
register_message(get_user_msgid("StatusIcon"), "on_status_icon");
RegisterHookChain(RG_PlantBomb, "on_plant_bomb_post", true);
RegisterHookChain(RG_CGrenade_DefuseBombEnd, "on_defuse_end_post", true);
RegisterHookChain(RG_CSGameRules_RestartRound, "on_restart_round_post", true);
find_places();
set_task(DRAW_EVERY, "task_draw", TASK_DRAW, _, _, "b");
}
public client_disconnected(id)
{
if (g_state == BOMB_CARRIED && g_carrier == id)
g_state = BOMB_NONE;
}
/**
* BombDrop: x, y, z, and 0 = dropped or 1 = planted.
*/
public on_bomb_drop(msgId, msgDest, msgEntity)
{
if (get_msg_arg_int(4) != 0 || g_state == BOMB_PLANTED || g_state == BOMB_DEFUSED)
return PLUGIN_CONTINUE;
g_dropOrigin[0] = get_msg_arg_float(1);
g_dropOrigin[1] = get_msg_arg_float(2);
g_dropOrigin[2] = get_msg_arg_float(3);
if (g_state != BOMB_DROPPED)
{
g_state = BOMB_DROPPED;
draw_all();
}
return PLUGIN_CONTINUE;
}
/**
* BombPickup: someone took the bomb from the ground.
*/
public on_bomb_pickup(msgId, msgDest, msgEntity)
{
new carrier = find_carrier();
if (carrier && (g_state != BOMB_CARRIED || g_carrier != carrier))
set_carried(carrier);
return PLUGIN_CONTINUE;
}
/**
* StatusIcon: status, sprite name... The "c4" icon is shown to the carrier.
*/
public on_status_icon(msgId, msgDest, msgEntity)
{
if (msgEntity < 1 || msgEntity > MaxClients || get_msg_arg_int(1) == 0)
return PLUGIN_CONTINUE;
new sprite[8];
get_msg_arg_string(2, sprite, charsmax(sprite));
if (equal(sprite, "c4") && is_user_alive(msgEntity) && get_member(msgEntity, m_bHasC4)
&& (g_state != BOMB_CARRIED || g_carrier != msgEntity))
set_carried(msgEntity);
return PLUGIN_CONTINUE;
}
public on_plant_bomb_post(planter, Float:origin[3], Float:velocity[3])
{
if (GetHookChainReturn(ATYPE_INTEGER) <= 0)
return HC_CONTINUE;
g_state = BOMB_PLANTED;
draw_all();
return HC_CONTINUE;
}
public on_defuse_end_post(bomb, defuser, bool:defused)
{
if (!defused)
return HC_CONTINUE;
g_state = BOMB_DEFUSED;
g_defusedAt = get_gametime();
draw_all();
return HC_CONTINUE;
}
/**
* A new round: the game has already given the bomb to the new carrier.
*/
public on_restart_round_post()
{
g_state = BOMB_NONE;
new carrier = find_carrier();
if (carrier)
set_carried(carrier);
else
draw_all();
return HC_CONTINUE;
}
public task_draw()
{
// The carrier lost the bomb without a message we saw: look again.
if (g_state == BOMB_CARRIED && !(is_user_alive(g_carrier) && get_member(g_carrier, m_bHasC4)))
{
new carrier = find_carrier();
if (carrier)
g_carrier = carrier;
else
g_state = BOMB_NONE;
}
else if (g_state == BOMB_NONE)
{
new carrier = find_carrier();
if (carrier)
{
g_state = BOMB_CARRIED;
g_carrier = carrier;
}
}
draw_all();
}
set_carried(carrier)
{
g_state = BOMB_CARRIED;
g_carrier = carrier;
draw_all();
}
find_carrier()
{
for (new id = 1; id <= MaxClients; id++)
{
if (is_user_alive(id) && get_member(id, m_bHasC4))
return id;
}
return 0;
}
draw_all()
{
if (!get_member_game(m_bMapHasBombTarget))
return;
new Float:fade;
if (g_state == BOMB_DEFUSED)
{
new Float:left = DEFUSED_TIME - (get_gametime() - g_defusedAt);
if (left <= 0.0)
{
g_state = BOMB_NONE;
return;
}
// The last draw fades out when the 5 seconds are over.
if (left <= DRAW_EVERY)
fade = DEFUSED_FADE;
}
for (new id = 1; id <= MaxClients; id++)
{
if (!is_user_connected(id))
continue;
if (g_state == BOMB_NONE || !team_sees(id))
{
ClearSyncHud(id, g_sync);
continue;
}
draw_line(id, fade);
}
}
bool:team_sees(id)
{
switch (g_team)
{
case 1:
return TeamName:get_member(id, m_iTeam) == TEAM_TERRORIST;
case 2:
return TeamName:get_member(id, m_iTeam) == TEAM_CT;
}
return true;
}
draw_line(id, Float:fade)
{
switch (g_state)
{
case BOMB_CARRIED:
{
new name[MAX_NAME_LENGTH];
get_user_name(g_carrier, name, charsmax(name));
set_hudmessage(255, 210, 0, g_x, g_y, 0, 0.0, HUD_HOLD, 0.0, 0.0, -1);
ShowSyncHudMsg(id, g_sync, "%L", id, "BS_CARRIED", name);
}
case BOMB_DROPPED:
{
new place[48];
place_name(id, place, charsmax(place));
set_hudmessage(255, 140, 0, g_x, g_y, 0, 0.0, HUD_HOLD, 0.0, 0.0, -1);
ShowSyncHudMsg(id, g_sync, "%L", id, "BS_DROPPED", place);
}
case BOMB_PLANTED:
{
set_hudmessage(255, 60, 60, g_x, g_y, 0, 0.0, HUD_HOLD, 0.0, 0.0, -1);
ShowSyncHudMsg(id, g_sync, "%L", id, "BS_PLANTED");
}
case BOMB_DEFUSED:
{
set_hudmessage(60, 220, 60, g_x, g_y, 0, 0.0, HUD_HOLD, 0.0, fade, -1);
ShowSyncHudMsg(id, g_sync, "%L", id, "BS_DEFUSED");
}
}
}
/**
* The nearest named place to the dropped bomb, in the reader's language.
*/
place_name(id, place[], length)
{
new type = -1, site;
new Float:best;
for (new i = 0; i < g_siteCount; i++)
check_place(g_siteOrigin[i], PLACE_SITE, i, type, site, best);
if (g_haveTSpawn)
check_place(g_tSpawn, PLACE_T_SPAWN, 0, type, site, best);
if (g_haveCtSpawn)
check_place(g_ctSpawn, PLACE_CT_SPAWN, 0, type, site, best);
switch (type)
{
case PLACE_SITE:
formatex(place, length, "%L", id, "BS_PLACE_SITE", 'A' + site);
case PLACE_T_SPAWN:
formatex(place, length, "%L", id, "BS_PLACE_T_SPAWN");
case PLACE_CT_SPAWN:
formatex(place, length, "%L", id, "BS_PLACE_CT_SPAWN");
default:
formatex(place, length, "%L", id, "BS_PLACE_UNKNOWN");
}
}
check_place(const Float:origin[3], placeType, index, &type, &site, &Float:best)
{
new Float:distance = get_distance_f(g_dropOrigin, origin);
if (type == -1 || distance < best)
{
type = placeType;
site = index;
best = distance;
}
}
/**
* The bombsites (in map order) and the middle of each team's spawn points.
*/
find_places()
{
new entity = MaxClients;
while (g_siteCount < MAX_SITES && (entity = rg_find_ent_by_class(entity, "func_bomb_target")) > 0)
add_site(entity, true);
entity = MaxClients;
while (g_siteCount < MAX_SITES && (entity = rg_find_ent_by_class(entity, "info_bomb_target")) > 0)
add_site(entity, false);
g_haveTSpawn = spawn_centre("info_player_deathmatch", g_tSpawn);
g_haveCtSpawn = spawn_centre("info_player_start", g_ctSpawn);
}
add_site(entity, bool:brush)
{
if (brush)
{
// A brush entity: the middle of its box.
new Float:mins[3], Float:maxs[3];
get_entvar(entity, var_absmin, mins);
get_entvar(entity, var_absmax, maxs);
for (new i = 0; i < 3; i++)
g_siteOrigin[g_siteCount][i] = (mins[i] + maxs[i]) / 2.0;
}
else
{
get_entvar(entity, var_origin, g_siteOrigin[g_siteCount]);
}
g_siteCount++;
}
bool:spawn_centre(const className[], Float:centre[3])
{
new entity = MaxClients, count;
new Float:origin[3], Float:sum[3];
while ((entity = rg_find_ent_by_class(entity, className)) > 0)
{
get_entvar(entity, var_origin, origin);
sum[0] += origin[0];
sum[1] += origin[1];
sum[2] += origin[2];
count++;
}
if (!count)
return false;
centre[0] = sum[0] / float(count);
centre[1] = sum[1] / float(count);
centre[2] = sum[2] / float(count);
return true;
}