Skip to content
View in the app

A better way to browse. Learn more.

Learn more

bomb-timer-bar/addons/amxmodx/scripting/olds_c4bar.sma

olds_c4bar.sma
325 lines · 7.6 KB · Pawn (AMX Mod X)
Raw
/*
 * Bomb Timer Bar (olds.ro workshop card LIB-0119)
 *
 * When the bomb is planted, the game's progress bar (the one used for
 * defusing) appears at the bottom of the screen and fills up as the time
 * passes: it is full when the bomb explodes.
 *
 * Commands: none.
 *
 * Cvars (in configs/plugins/olds_c4bar.cfg, which ships with the plugin; if
 * the file is missing, the plugin writes it with these defaults the first
 * time it starts):
 *   olds_btb_ct  1  1 = Counter-Terrorists see the bar.
 *   olds_btb_t   1  1 = Terrorists see the bar.
 *
 * Rules:
 *   - Spectators see the bar too.
 *   - A player who joins a team, or whose team gets the bar back, sees it
 *     at the right point within a second.
 *   - While a player defuses, the game's defuse bar takes the place of this
 *     one (also for spectators watching him). If he stops, the timer bar
 *     comes back at the right point.
 *   - The bar goes away when the bomb is defused, explodes, or the round
 *     restarts.
 *   - The game's progress bar can only fill up, and its colour is the
 *     player's HUD colour: a server cannot change either, so the card's
 *     olds_btb_color has no setting here.
 *
 * How it works, as the card asks: on RG_PlantBomb (post) every player who
 * may see it gets message_begin(MSG_ONE, BarTime) with the bomb's seconds.
 * A player who comes later gets BarTime2, which starts the same bar part
 * way. BarTime 0 clears it on defuse, explosion and round restart.
 *
 * 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 Timer Bar";
new const PLUGIN_VERSION[] = "1.0.0";
new const PLUGIN_AUTHOR[] = "alexit (olds.ro)";

const TASK_UPDATE = 4119;

new g_showCT;          // olds_btb_ct
new g_showT;           // olds_btb_t

new g_msgBarTime;
new g_msgBarTime2;

new g_bomb;            // the planted bomb, 0 = none
new Float:g_plantTime;
new Float:g_bombTime;  // seconds from the plant to the explosion
new g_defuser;         // the player defusing now, 0 = nobody

new bool:g_shown[MAX_PLAYERS + 1];

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

	if (!is_regamedll())
		set_fail_state("ReGameDLL_CS is not running. This plugin needs ReGameDLL_CS and ReAPI.");

	bind_pcvar_num(create_cvar("olds_btb_ct", "1", FCVAR_NONE,
		"1 = Counter-Terrorists see the bomb timer bar",
		true, 0.0, true, 1.0), g_showCT);

	bind_pcvar_num(create_cvar("olds_btb_t", "1", FCVAR_NONE,
		"1 = Terrorists see the bomb timer bar",
		true, 0.0, true, 1.0), g_showT);

	AutoExecConfig(true, "olds_c4bar");

	g_msgBarTime = get_user_msgid("BarTime");
	g_msgBarTime2 = get_user_msgid("BarTime2");

	RegisterHookChain(RG_PlantBomb, "on_plant_bomb_post", true);
	RegisterHookChain(RG_CGrenade_DefuseBombStart, "on_defuse_start_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_pre", false);
	RegisterHookChain(RG_CBasePlayer_Spawn, "on_player_spawn_post", true);
}

public client_disconnected(id)
{
	g_shown[id] = false;

	if (id == g_defuser)
		g_defuser = 0;
}

public on_plant_bomb_post(id, Float:origin[3], Float:velocity[3])
{
	new bomb = GetHookChainReturn(ATYPE_INTEGER);

	if (bomb <= 0)
		return HC_CONTINUE;

	clear_all();

	g_bomb = bomb;
	g_plantTime = get_gametime();
	g_bombTime = floatmax(Float:get_member(bomb, m_Grenade_flC4Blow) - g_plantTime, 1.0);
	g_defuser = 0;

	new seconds = floatround(g_bombTime, floatround_ceil);

	for (new player = 1; player <= MaxClients; player++)
	{
		if (!may_see(player))
			continue;

		message_begin(MSG_ONE, g_msgBarTime, _, player);
		write_byte(min(seconds, 255));
		message_end();

		g_shown[player] = true;
	}

	set_task(1.0, "task_update", TASK_UPDATE, _, _, "b");
	return HC_CONTINUE;
}

public on_defuse_start_post(bomb, id)
{
	if (bomb != g_bomb)
		return HC_CONTINUE;

	// The game's defuse bar now takes the place of ours, for him and for
	// the spectators who watch him.
	g_defuser = id;

	for (new player = 1; player <= MaxClients; player++)
	{
		if (player == id || watches(player, id))
			g_shown[player] = false;
	}

	return HC_CONTINUE;
}

public on_defuse_end_post(bomb, id, bool:defused)
{
	if (bomb != g_bomb)
		return HC_CONTINUE;

	g_defuser = 0;

	if (defused)
		stop_bar();
	else
		update_all();      // the timer bar comes back at once

	return HC_CONTINUE;
}

public on_explode_bomb_pre(bomb)
{
	if (bomb == g_bomb)
		stop_bar();

	return HC_CONTINUE;
}

/**
 * Before the players respawn for the new round.
 */
public on_restart_round_pre()
{
	if (g_bomb)
		stop_bar();

	return HC_CONTINUE;
}

/**
 * A spawn while the bomb ticks (a mod that respawns players) may reset the
 * HUD: the bar is sent again, at the right point.
 */
public on_player_spawn_post(id)
{
	if (g_bomb && is_user_alive(id))
	{
		g_shown[id] = false;
		update_player(id);
	}

	return HC_CONTINUE;
}

public task_update()
{
	update_all();
}

update_all()
{
	if (!g_bomb)
		return;

	if (!is_entity(g_bomb) || !get_member(g_bomb, m_Grenade_bIsC4))
	{
		stop_bar();
		return;
	}

	for (new player = 1; player <= MaxClients; player++)
		update_player(player);
}

/**
 * Sends the bar to a player who should see it and has not got it, or takes
 * it away from one who should no longer see it.
 */
update_player(id)
{
	if (!is_user_connected(id))
	{
		g_shown[id] = false;
		return;
	}

	// The defuse bar is there now; leave it alone.
	if (g_defuser && (id == g_defuser || watches(id, g_defuser)))
		return;

	if (may_see(id))
	{
		if (!g_shown[id])
		{
			new Float:elapsed = get_gametime() - g_plantTime;
			new percent = clamp(floatround(elapsed * 100.0 / g_bombTime, floatround_floor), 0, 99);

			message_begin(MSG_ONE, g_msgBarTime2, _, id);
			write_short(floatround(g_bombTime, floatround_ceil));
			write_short(percent);
			message_end();

			g_shown[id] = true;
		}
	}
	else if (g_shown[id])
	{
		clear_bar(id);
	}
}

stop_bar()
{
	remove_task(TASK_UPDATE);
	clear_all();

	g_bomb = 0;
	g_defuser = 0;
}

clear_all()
{
	for (new player = 1; player <= MaxClients; player++)
	{
		if (g_shown[player])
			clear_bar(player);
	}
}

clear_bar(id)
{
	g_shown[id] = false;

	if (!is_user_connected(id))
		return;

	message_begin(MSG_ONE, g_msgBarTime, _, id);
	write_byte(0);
	message_end();
}

bool:may_see(id)
{
	if (!is_user_connected(id))
		return false;

	switch (get_member(id, m_iTeam))
	{
		case TEAM_TERRORIST:
			return bool:g_showT;
		case TEAM_CT:
			return bool:g_showCT;
	}

	// Spectators and players who have not chosen a team yet.
	return true;
}

/**
 * True when the spectator follows the target (in-eye or chase camera).
 */
bool:watches(spectator, target)
{
	if (spectator == target || !is_user_connected(spectator))
		return false;

	new mode = get_entvar(spectator, var_iuser1);
	return (mode == OBS_CHASE_LOCKED || mode == OBS_CHASE_FREE || mode == OBS_IN_EYE)
		&& get_entvar(spectator, var_iuser2) == target;
}

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.