/*
* Bomb Distance Display (olds.ro workshop card LIB-0111)
*
* Once the bomb is planted, a HUD line shows each player how far away it
* is, in meters or in game units. Counter-Terrorists also get a direction:
* ahead, to the left or to the right. Each player can switch the line off
* and on with /bombdist.
*
* Commands:
* say /bombdist, say_team /bombdist switch the line off or on for
* yourself
*
* Cvars (in configs/plugins/olds_bombdist.cfg, which ships with the plugin;
* if the file is missing, the plugin writes it with these defaults the first
* time it starts):
* olds_bdist_units 1 1 = meters; 0 = game units.
* olds_bdist_all 0 0 = only Counter-Terrorists see the line; 1 =
* Terrorists see it too (without the direction).
*
* Rules:
* - The line is updated every 0.5 s from the moment the bomb is planted
* until it is defused, it explodes or the round restarts; then it is
* cleared for everyone at once.
* - A dead player or spectator watching someone (first person or chase
* camera) sees the line of the player he watches.
* - Direction: "ahead" when the bomb is within 30 degrees of where the
* player looks, otherwise the side to turn to.
* - One meter is 39.37 game units (one unit is one inch).
* - The /bombdist choice lasts until the player leaves the server.
*
* How it works, as the card asks: after RG_PlantBomb a 0.5 s repeating task
* measures get_distance_f from each player to the bomb and prints it with
* ShowSyncHudMsg; the direction comes from the angle between the player's
* view and 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 Distance Display";
new const PLUGIN_VERSION[] = "1.0.0";
new const PLUGIN_AUTHOR[] = "alexit (olds.ro)";
const TASK_UPDATE = 11100;
const Float:UPDATE_EVERY = 0.5;
const Float:HUD_HOLD = 0.7;
const Float:HUD_X = -1.0;
const Float:HUD_Y = 0.18;
const Float:UNITS_PER_METER = 39.37;
// Within this many degrees of the view the bomb is "ahead".
const Float:AHEAD_ANGLE = 30.0;
enum
{
DIRECTION_AHEAD = 0,
DIRECTION_LEFT,
DIRECTION_RIGHT
};
new const DIRECTION_KEYS[][] = { "BD_AHEAD", "BD_LEFT", "BD_RIGHT" };
new g_meters; // olds_bdist_units
new g_all; // olds_bdist_all
new g_sync;
// The planted bomb (0 = none).
new g_bomb;
new bool:g_shown[MAX_PLAYERS + 1];
new bool:g_hidden[MAX_PLAYERS + 1];
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_bombdist.txt");
bind_pcvar_num(create_cvar("olds_bdist_units", "1", FCVAR_NONE,
"1 = distance in meters; 0 = in game units",
true, 0.0, true, 1.0), g_meters);
bind_pcvar_num(create_cvar("olds_bdist_all", "0", FCVAR_NONE,
"0 = only Counter-Terrorists see the distance; 1 = Terrorists too",
true, 0.0, true, 1.0), g_all);
AutoExecConfig(true, "olds_bombdist");
g_sync = CreateHudSyncObj();
register_clcmd("say /bombdist", "cmd_toggle");
register_clcmd("say_team /bombdist", "cmd_toggle");
RegisterHookChain(RG_PlantBomb, "on_plant_bomb_post", true);
RegisterHookChain(RG_CGrenade_DefuseBombEnd, "on_defuse_end_post", true);
RegisterHookChain(RG_CGrenade_ExplodeBomb, "on_explode_bomb_pre", false);
RegisterHookChain(RG_CSGameRules_RestartRound, "on_restart_round_post", true);
}
public client_putinserver(id)
{
g_shown[id] = false;
g_hidden[id] = false;
}
public client_disconnected(id)
{
g_shown[id] = false;
g_hidden[id] = false;
}
public cmd_toggle(id)
{
g_hidden[id] = !g_hidden[id];
if (g_hidden[id])
clear_line(id);
client_print_color(id, print_team_default, "%L", id, g_hidden[id] ? "BD_OFF" : "BD_ON");
return PLUGIN_HANDLED;
}
/**
* The bomb is planted: the line starts.
*/
public on_plant_bomb_post(planter, Float:origin[3], Float:velocity[3])
{
new bomb = GetHookChainReturn(ATYPE_INTEGER);
if (bomb <= 0)
return HC_CONTINUE;
g_bomb = bomb;
remove_task(TASK_UPDATE);
set_task(UPDATE_EVERY, "task_update", TASK_UPDATE, _, _, "b");
task_update();
return HC_CONTINUE;
}
public on_defuse_end_post(bomb, defuser, bool:defused)
{
if (defused && bomb == g_bomb)
stop_display();
return HC_CONTINUE;
}
public on_explode_bomb_pre(bomb, trace, damageBits)
{
if (bomb == g_bomb)
stop_display();
return HC_CONTINUE;
}
public on_restart_round_post()
{
if (g_bomb)
stop_display();
return HC_CONTINUE;
}
stop_display()
{
g_bomb = 0;
remove_task(TASK_UPDATE);
for (new id = 1; id <= MaxClients; id++)
{
if (is_user_connected(id))
clear_line(id);
}
}
public task_update()
{
if (!g_bomb || !is_entity(g_bomb) || !get_member(g_bomb, m_Grenade_bIsC4))
{
stop_display();
return;
}
new Float:bombOrigin[3];
get_entvar(g_bomb, var_origin, bombOrigin);
for (new id = 1; id <= MaxClients; id++)
{
if (!is_user_connected(id))
continue;
new viewer = g_hidden[id] ? 0 : viewer_of(id);
if (!viewer || !can_see(viewer))
{
clear_line(id);
continue;
}
show_line(id, viewer, bombOrigin);
}
}
/**
* The living player whose position the line uses: the player himself, or
* the one a dead player or spectator watches. 0 = nobody.
*/
viewer_of(id)
{
if (is_user_alive(id))
return id;
switch (get_entvar(id, var_iuser1))
{
case OBS_CHASE_LOCKED, OBS_CHASE_FREE, OBS_IN_EYE, OBS_MAP_CHASE:
{
new target = get_entvar(id, var_iuser2);
if (target >= 1 && target <= MaxClients && is_user_alive(target))
return target;
}
}
return 0;
}
bool:can_see(viewer)
{
new TeamName:team = TeamName:get_member(viewer, m_iTeam);
return team == TEAM_CT || (g_all && team == TEAM_TERRORIST);
}
show_line(id, viewer, const Float:bombOrigin[3])
{
new Float:origin[3];
get_entvar(viewer, var_origin, origin);
new Float:distance = get_distance_f(origin, bombOrigin);
new line[96];
if (g_meters)
formatex(line, charsmax(line), "%L", id, "BD_METERS", floatround(distance / UNITS_PER_METER));
else
formatex(line, charsmax(line), "%L", id, "BD_UNITS", floatround(distance));
if (TeamName:get_member(viewer, m_iTeam) == TEAM_CT)
{
new length = strlen(line);
formatex(line[length], charsmax(line) - length, " %L", id, DIRECTION_KEYS[direction(viewer, origin, bombOrigin)]);
}
set_hudmessage(255, 170, 0, HUD_X, HUD_Y, 0, 0.0, HUD_HOLD, 0.0, 0.0, -1);
ShowSyncHudMsg(id, g_sync, "%s", line);
g_shown[id] = true;
}
/**
* Ahead, left or right: the angle between where the viewer looks and the
* bomb, both flat on the ground. Angles grow to the left in Half-Life.
*/
direction(viewer, const Float:origin[3], const Float:bombOrigin[3])
{
new Float:angles[3];
get_entvar(viewer, var_v_angle, angles);
new Float:bombYaw = floatatan2(bombOrigin[1] - origin[1], bombOrigin[0] - origin[0], degrees);
new Float:difference = bombYaw - angles[1];
while (difference > 180.0)
difference -= 360.0;
while (difference < -180.0)
difference += 360.0;
if (floatabs(difference) <= AHEAD_ANGLE)
return DIRECTION_AHEAD;
return (difference > 0.0) ? DIRECTION_LEFT : DIRECTION_RIGHT;
}
clear_line(id)
{
if (!g_shown[id])
return;
ClearSyncHud(id, g_sync);
g_shown[id] = false;
}