/*
* Damage Dealt Display (olds.ro workshop card LIB-0001)
*
* After you hit an enemy, a small number under the crosshair shows the
* damage you just dealt. While you keep hitting the same target the number
* keeps adding up. Each player can switch it off and on with /dmg.
*
* Commands:
* say /dmg, say_team /dmg switch the number off or on for yourself
*
* Cvars (in configs/plugins/olds_damagedealt.cfg, which ships with the
* plugin; if the file is missing, the plugin writes it with these defaults
* the first time it starts):
* olds_dd_stack_time 1.5 Hits on the same target closer than
* this (seconds) are added together.
* olds_dd_color_ct "0 120 255" Colour (red green blue) for a CT.
* olds_dd_color_t "255 90 60" Colour for a Terrorist.
*
* Rules:
* - The number is the health the target really lost (after armor), so a
* grenade shows its damage once, and the killing bullet shows the total
* right after the death message.
* - Hitting another target, or pausing longer than olds_dd_stack_time,
* starts a new total.
* - Damage to yourself is not shown. Damage to a teammate is not shown
* with mp_friendlyfire 0 (unless mp_freeforall 1).
* - Director HUD texts cannot be replaced or cleared, so the number is
* redrawn at most every 0.75 s (it shows for 0.5 s and fades out in
* 0.2 s). Hits in between are added and appear in the next redraw; two
* numbers are never drawn on top of each other.
* - The /dmg choice lasts until the player leaves the server.
*
* How it works, as the card asks: RG_CBasePlayer_TakeDamage (pre) keeps the
* target's health, (post) gives the attacker and the final damage; the
* number is shown with set_dhudmessage near the crosshair.
*
* 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[] = "Damage Dealt Display";
new const PLUGIN_VERSION[] = "1.0.0";
new const PLUGIN_AUTHOR[] = "alexit (olds.ro)";
// Task ids: TASK_SHOW + player index.
const TASK_SHOW = 1100;
// Where and how long the number is drawn.
const Float:DHUD_X = -1.0;
const Float:DHUD_Y = 0.55;
const Float:SHOW_HOLD = 0.5;
const Float:SHOW_FADE = 0.2;
// Extra wait after a number has faded, before the next one is drawn.
const Float:SHOW_GAP = 0.05;
new Float:g_stackTime; // olds_dd_stack_time
new g_colorCt[32]; // olds_dd_color_ct
new g_colorT[32]; // olds_dd_color_t
new g_friendlyFireCvar; // mp_friendlyfire
new g_freeForAllCvar; // mp_freeforall, 0 if the server has none
// Health of each player when the current damage started.
new Float:g_healthBefore[MAX_PLAYERS + 1];
// Per attacker: the target of the running total, the total, the time of the
// last hit, the time his last number has faded, and the /dmg choice.
new g_target[MAX_PLAYERS + 1];
new g_total[MAX_PLAYERS + 1];
new Float:g_lastHit[MAX_PLAYERS + 1];
new Float:g_shownUntil[MAX_PLAYERS + 1];
new bool:g_hidden[MAX_PLAYERS + 1];
public plugin_init()
{
register_plugin(PLUGIN_NAME, PLUGIN_VERSION, PLUGIN_AUTHOR);
// The damage hook comes from ReGameDLL_CS.
if (!is_regamedll())
set_fail_state("ReGameDLL_CS is not running. This plugin needs ReGameDLL_CS and ReAPI.");
register_dictionary("olds_damagedealt.txt");
bind_pcvar_float(create_cvar("olds_dd_stack_time", "1.5", FCVAR_NONE,
"Hits on the same target closer than this (seconds) are added together",
true, 0.0, true, 60.0), g_stackTime);
bind_pcvar_string(create_cvar("olds_dd_color_ct", "0 120 255", FCVAR_NONE,
"Colour of the number for a CT: red green blue (0-255)"),
g_colorCt, charsmax(g_colorCt));
bind_pcvar_string(create_cvar("olds_dd_color_t", "255 90 60", FCVAR_NONE,
"Colour of the number for a Terrorist: red green blue (0-255)"),
g_colorT, charsmax(g_colorT));
AutoExecConfig(true, "olds_damagedealt");
g_friendlyFireCvar = get_cvar_pointer("mp_friendlyfire");
g_freeForAllCvar = get_cvar_pointer("mp_freeforall");
register_clcmd("say /dmg", "cmd_toggle");
register_clcmd("say_team /dmg", "cmd_toggle");
RegisterHookChain(RG_CBasePlayer_TakeDamage, "on_take_damage_pre", false);
RegisterHookChain(RG_CBasePlayer_TakeDamage, "on_take_damage_post", true);
}
public client_putinserver(id)
{
reset_player(id);
g_hidden[id] = false;
}
public client_disconnected(id)
{
reset_player(id);
g_hidden[id] = false;
}
reset_player(id)
{
remove_task(TASK_SHOW + id);
g_target[id] = 0;
g_total[id] = 0;
g_lastHit[id] = 0.0;
g_shownUntil[id] = 0.0;
}
public cmd_toggle(id)
{
g_hidden[id] = !g_hidden[id];
if (g_hidden[id])
reset_player(id);
client_print_color(id, print_team_default, "%L", id, g_hidden[id] ? "DD_OFF" : "DD_ON");
return PLUGIN_HANDLED;
}
public on_take_damage_pre(victim, inflictor, attacker, Float:damage, damageBits)
{
if (victim >= 1 && victim <= MaxClients)
g_healthBefore[victim] = Float:get_entvar(victim, var_health);
return HC_CONTINUE;
}
public on_take_damage_post(victim, inflictor, attacker, Float:damage, damageBits)
{
if (victim < 1 || victim > MaxClients)
return HC_CONTINUE;
// The health the target really lost: after armor, as the game took it.
new dealt = floatround(g_healthBefore[victim] - Float:get_entvar(victim, var_health));
if (dealt < 1)
return HC_CONTINUE;
if (attacker < 1 || attacker > MaxClients || attacker == victim || !is_user_connected(attacker) || g_hidden[attacker])
return HC_CONTINUE;
if (is_teammate(attacker, victim) && !get_pcvar_num(g_friendlyFireCvar))
return HC_CONTINUE;
new Float:now = get_gametime();
if (g_target[attacker] == victim && now - g_lastHit[attacker] <= g_stackTime)
{
g_total[attacker] += dealt;
}
else
{
g_target[attacker] = victim;
g_total[attacker] = dealt;
}
g_lastHit[attacker] = now;
// The last number is still on screen: the new total follows it.
if (now < g_shownUntil[attacker])
{
if (!task_exists(TASK_SHOW + attacker))
set_task(g_shownUntil[attacker] - now, "task_show", TASK_SHOW + attacker);
}
else
{
show_total(attacker);
}
return HC_CONTINUE;
}
public task_show(taskId)
{
new id = taskId - TASK_SHOW;
if (is_user_connected(id) && !g_hidden[id] && g_total[id] > 0)
show_total(id);
}
show_total(id)
{
new red = 255, green = 255, blue = 255;
switch (get_member(id, m_iTeam))
{
case TEAM_CT:
parse_color(g_colorCt, red, green, blue);
case TEAM_TERRORIST:
parse_color(g_colorT, red, green, blue);
}
set_dhudmessage(red, green, blue, DHUD_X, DHUD_Y, 0, 0.0, SHOW_HOLD, 0.0, SHOW_FADE);
show_dhudmessage(id, "%d", g_total[id]);
g_shownUntil[id] = get_gametime() + SHOW_HOLD + SHOW_FADE + SHOW_GAP;
}
parse_color(const text[], &red, &green, &blue)
{
new part[3][8];
if (parse(text, part[0], charsmax(part[]), part[1], charsmax(part[]), part[2], charsmax(part[])) != 3)
return;
red = clamp(str_to_num(part[0]), 0, 255);
green = clamp(str_to_num(part[1]), 0, 255);
blue = clamp(str_to_num(part[2]), 0, 255);
}
bool:is_teammate(attacker, victim)
{
if (g_freeForAllCvar && get_pcvar_num(g_freeForAllCvar))
return false;
return get_member(attacker, m_iTeam) == get_member(victim, m_iTeam);
}