Skip to content
View in the app

A better way to browse. Learn more.

Learn more

defuse-margin/addons/amxmodx/scripting/olds_defusemargin.sma

olds_defusemargin.sma
110 lines · 4.1 KB · Pawn (AMX Mod X)
Raw
/*
 * Defuse Margin (olds.ro workshop card AMX-020)
 *
 * After a successful defuse, the defuser sees how much time was left on the
 * bomb, for example "Defused with 4.3 s to spare", as a DHUD line. With
 * olds_margin_all 1 the rest of the defuser's team sees it too.
 *
 * Only a defuse that really ends with the bomb defused counts. An attempt that
 * is interrupted (the player lets go of +use, dies, the bomb explodes, a new
 * round starts) ends without a defuse and shows nothing, so after a restarted
 * attempt only the final, successful defuse shows a margin. If the bomb
 * explodes in the same frame as the defuse would finish, nothing is shown.
 * The time shown is rounded down to a tenth of a second, so it never claims
 * more time than there was.
 *
 * Cvars (in configs/plugins/olds_defusemargin.cfg, written with the defaults
 * on the first start if the file is missing):
 *   olds_margin_all 0   0 = only the defuser sees the margin,
 *                       1 = the defuser's teammates see it too.
 *
 * How it works, as the card asks: RG_CGrenade_DefuseBombEnd with its success
 * flag, and the bomb's m_Grenade_flC4Blow minus get_gametime(). The plugin
 * keeps no state between attempts. Texts are in data/lang/olds_defusemargin.txt.
 *
 * Requires AMX Mod X 1.10 with ReAPI (ReGameDLL_CS). Written and tested on
 * 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[] = "Defuse Margin";
new const PLUGIN_VERSION[] = "1.0.0";
new const PLUGIN_AUTHOR[] = "alexit (olds.ro)";

// DHUD placement and look.
const Float:DHUD_X = -1.0;
const Float:DHUD_Y = 0.25;
const Float:DHUD_HOLD = 4.0;

new g_showAll;       // olds_margin_all

public plugin_init()
{
	register_plugin(PLUGIN_NAME, PLUGIN_VERSION, PLUGIN_AUTHOR);
	register_dictionary("olds_defusemargin.txt");

	bind_pcvar_num(create_cvar("olds_margin_all", "0", FCVAR_NONE,
		"0 = only the defuser sees the defuse margin, 1 = the defuser's team sees it too",
		true, 0.0, true, 1.0), g_showAll);

	AutoExecConfig(true, "olds_defusemargin");

	// Before the game acts on the result, while the bomb still holds its time.
	RegisterHookChain(RG_CGrenade_DefuseBombEnd, "fw_defuse_end_pre", false);
}

public fw_defuse_end_pre(const bomb, const player, bool:defused)
{
	if (!defused || !(1 <= player <= MaxClients) || !is_user_connected(player) || is_nullent(bomb))
		return HC_CONTINUE;

	if (!get_member(bomb, m_Grenade_bIsC4) || get_member(bomb, m_Grenade_bJustBlew))
		return HC_CONTINUE;

	new Float:margin = Float:get_member(bomb, m_Grenade_flC4Blow) - get_gametime();

	// The bomb went off in the same frame: there was no time left.
	if (margin <= 0.0)
		return HC_CONTINUE;

	// Tenths, rounded down, written with whole numbers so the value is exact.
	new tenths = floatround(margin * 10.0, floatround_floor);

	set_dhudmessage(90, 200, 255, DHUD_X, DHUD_Y, 0, 0.0, DHUD_HOLD, 0.1, 0.5);
	show_dhudmessage(player, "%L", player, "MARGIN_SELF", tenths / 10, tenths % 10);

	if (g_showAll)
	{
		new TeamName:team = get_member(player, m_iTeam);
		new name[MAX_NAME_LENGTH];
		get_user_name(player, name, charsmax(name));

		for (new mate = 1; mate <= MaxClients; mate++)
		{
			if (mate == player || !is_user_connected(mate) || get_member(mate, m_iTeam) != team)
				continue;

			set_dhudmessage(90, 200, 255, DHUD_X, DHUD_Y, 0, 0.0, DHUD_HOLD, 0.1, 0.5);
			show_dhudmessage(mate, "%L", mate, "MARGIN_TEAM", name, tenths / 10, tenths % 10);
		}
	}

	return HC_CONTINUE;
}

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.