/*
* Juggernaut Damage Shares (olds.ro) - Juggernaut add-on
* Copyright (C) 2026 daemon (olds.ro), written by daemon's plugin agent
*
* 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. It is distributed WITHOUT ANY WARRANTY; see
* <https://www.gnu.org/licenses/>.
*
* Every player's damage to the current juggernaut is counted
* (RG_CBasePlayer_TakeDamage: health and armour the hit took away). The
* juggernaut's own falls, the world and other entities do not count. When
* the juggernaut dies, olds_jshare_pool is shared by damage (rg_add_account,
* rounded down): a share above 5% gets at least olds_jshare_min, a share of
* 5% or less gets nothing. A player who left keeps their part of the total
* but is paid nothing; nobody gets their money instead. At the round end a
* HUD table names the five biggest damage dealers, highest first.
*
* The juggernaut: named by a core with olds_jshare_set_juggernaut(id)
* (include/olds_jshare.inc), or else the only player on the terrorist team
* while counter-terrorists play. A new juggernaut starts a new count.
*/
#include <amxmodx>
#include <amxmisc>
#include <reapi>
#define PLUGIN "Juggernaut Damage Shares"
#define VERSION "1.0.0"
#define AUTHOR "daemon (olds.ro)"
#define TOP 5
#define MIN_SHARE 0.05
#define TASK_WATCH 72355
new g_cvPool, g_cvMin
new g_hud
new g_jugg
new bool:g_byCore
new bool:g_paid // this juggernaut's pool was paid
new Float:g_damage[MAX_PLAYERS + 1] // damage of each player still here
new g_paidTo[MAX_PLAYERS + 1]
new Float:g_goneDamage // damage of players who left
new Float:g_hpBefore[MAX_PLAYERS + 1], Float:g_armorBefore[MAX_PLAYERS + 1]
public plugin_natives()
{
register_library("olds_jshare")
register_native("olds_jshare_set_juggernaut", "native_set_juggernaut")
}
public native_set_juggernaut(plugin, params)
{
new id = get_param(1)
if (id < 0 || id > MaxClients || (id && !is_user_connected(id)))
{
log_error(AMX_ERR_NATIVE, "Invalid player %d", id)
return 0
}
g_byCore = id != 0
set_juggernaut(id)
return 1
}
public plugin_init()
{
register_plugin(PLUGIN, VERSION, AUTHOR)
register_dictionary("olds_jshare.txt")
g_cvPool = create_cvar("olds_jshare_pool", "6000", FCVAR_NONE, "Money shared when the juggernaut dies", true, 0.0, true, 100000.0)
g_cvMin = create_cvar("olds_jshare_min", "100", FCVAR_NONE, "Least money for a share above 5%", true, 0.0, true, 16000.0)
AutoExecConfig(true, "olds_jshare")
RegisterHookChain(RG_CBasePlayer_TakeDamage, "on_take_damage_pre", false)
RegisterHookChain(RG_CBasePlayer_TakeDamage, "on_take_damage_post", true)
RegisterHookChain(RG_CBasePlayer_Killed, "on_killed_post", true)
RegisterHookChain(RG_CSGameRules_RestartRound, "on_restart_round_post", true)
RegisterHookChain(RG_RoundEnd, "on_round_end_post", true)
register_concmd("amx_jshare", "cmd_status", ADMIN_KICK, "- shows the damage on the juggernaut")
g_hud = CreateHudSyncObj()
set_task(0.5, "watch", TASK_WATCH, _, _, "b")
}
public client_putinserver(id)
{
g_damage[id] = 0.0
g_paidTo[id] = 0
}
public client_disconnected(id)
{
// Their part stays in the total; nobody else gets it.
g_goneDamage += g_damage[id]
g_damage[id] = 0.0
g_paidTo[id] = 0
if (id == g_jugg)
{
g_byCore = false
set_juggernaut(0)
}
}
reset_count()
{
for (new id = 0; id <= MaxClients; id++)
{
g_damage[id] = 0.0
g_paidTo[id] = 0
}
g_goneDamage = 0.0
g_paid = false
}
set_juggernaut(id)
{
if (id == g_jugg)
return
g_jugg = id
reset_count()
}
/* The lone terrorist, when counter-terrorists play too (unless a core names one). */
public watch()
{
if (g_byCore)
return
new t, cts, lone
for (new id = 1; id <= MaxClients; id++)
{
if (!is_user_connected(id))
continue
new TeamName:team = TeamName:get_member(id, m_iTeam)
if (team == TEAM_TERRORIST)
{
t++
lone = id
}
else if (team == TEAM_CT)
cts++
}
if (t == 1 && cts > 0)
set_juggernaut(lone)
else if (!g_paid)
set_juggernaut(0)
}
public on_restart_round_post()
{
reset_count()
if (!g_byCore)
{
g_jugg = 0
set_task(0.3, "watch")
}
}
public on_take_damage_pre(victim, inflictor, attacker, Float:damage, bits)
{
if (victim == g_jugg && g_jugg)
{
g_hpBefore[victim] = Float:get_entvar(victim, var_health)
g_armorBefore[victim] = Float:get_entvar(victim, var_armorvalue)
}
return HC_CONTINUE
}
public on_take_damage_post(victim, inflictor, attacker, Float:damage, bits)
{
if (victim != g_jugg || !g_jugg || g_paid)
return HC_CONTINUE
// Only another player's hit counts: not a fall, the world or an entity.
if (attacker < 1 || attacker > MaxClients || attacker == victim || !is_user_connected(attacker))
return HC_CONTINUE
new Float:hp = Float:get_entvar(victim, var_health), Float:armor = Float:get_entvar(victim, var_armorvalue)
new Float:lost = floatmax(0.0, g_hpBefore[victim] - floatmax(hp, 0.0)) + floatmax(0.0, g_armorBefore[victim] - armor)
if (lost > 0.0)
g_damage[attacker] += lost
return HC_CONTINUE
}
Float:total_damage()
{
new Float:total = g_goneDamage
for (new id = 1; id <= MaxClients; id++)
total += g_damage[id]
return total
}
public on_killed_post(victim, attacker, gib)
{
if (victim != g_jugg || !g_jugg || g_paid)
return HC_CONTINUE
g_paid = true
new Float:total = total_damage()
if (total <= 0.0)
{
log_amx("The juggernaut died with no damage from players: no shares")
return HC_CONTINUE
}
new pool = get_pcvar_num(g_cvPool), least = get_pcvar_num(g_cvMin), name[32], given
for (new id = 1; id <= MaxClients; id++)
{
if (g_damage[id] <= 0.0 || !is_user_connected(id) || id == victim)
continue
new Float:share = g_damage[id] / total
new amount = 0
if (share > MIN_SHARE)
amount = max(floatround(float(pool) * share, floatround_floor), least)
g_paidTo[id] = amount
get_user_name(id, name, charsmax(name))
log_amx("Juggernaut share: %s %.0f damage (%.1f%%) gets $%d", name, g_damage[id], share * 100.0, amount)
if (amount > 0)
{
rg_add_account(id, amount)
client_print_color(id, print_team_default, "%L", id, "JS_PAID", floatround(share * 100.0, floatround_floor), amount)
given += amount
}
}
if (g_goneDamage > 0.0)
log_amx("Juggernaut share: %.0f damage by players who left (%.1f%%) is not paid", g_goneDamage, g_goneDamage / total * 100.0)
log_amx("Juggernaut shares paid: $%d of the $%d pool", given, pool)
return HC_CONTINUE
}
public on_round_end_post()
{
show_table()
}
show_table()
{
new order[MAX_PLAYERS], count
for (new id = 1; id <= MaxClients; id++)
{
if (g_damage[id] > 0.0 && is_user_connected(id))
order[count++] = id
}
if (!count)
return
// Highest damage first.
for (new i = 1; i < count; i++)
{
new key = order[i], j = i - 1
while (j >= 0 && g_damage[order[j]] < g_damage[key])
{
order[j + 1] = order[j]
j--
}
order[j + 1] = key
}
new shown = min(count, TOP)
new Float:total = total_damage()
new text[512], line[96], name[32]
for (new viewer = 0; viewer <= MaxClients; viewer++)
{
if (viewer && (!is_user_connected(viewer) || is_user_bot(viewer)))
continue
new lang = viewer ? viewer : LANG_SERVER
formatex(text, charsmax(text), "%L", lang, "JS_TABLE_TITLE")
for (new i = 0; i < shown; i++)
{
new id = order[i]
get_user_name(id, name, charsmax(name))
formatex(line, charsmax(line), "^n%d. %s %.0f (%d%%) +$%d", i + 1, name, g_damage[id],
floatround(g_damage[id] / total * 100.0, floatround_floor), g_paidTo[id])
add(text, charsmax(text), line)
}
if (!viewer)
{
log_amx("Round-end damage table: %s", text)
continue
}
set_hudmessage(255, 220, 120, 0.02, 0.25, 0, 0.0, 8.0, 0.2, 0.5, -1)
ShowSyncHudMsg(viewer, g_hud, "%s", text)
}
}
public cmd_status(id, level, cid)
{
if (!cmd_access(id, level, cid, 1))
return PLUGIN_HANDLED
new name[32]
copy(name, charsmax(name), "none")
if (g_jugg && is_user_connected(g_jugg))
get_user_name(g_jugg, name, charsmax(name))
console_print(id, "[Shares] Juggernaut %s, %.0f damage counted (%.0f by players who left), paid %d.", name, total_damage(), g_goneDamage, g_paid)
for (new i = 1; i <= MaxClients; i++)
{
if (g_damage[i] > 0.0 && is_user_connected(i))
{
get_user_name(i, name, charsmax(name))
console_print(id, "[Shares] %s %.0f", name, g_damage[i])
}
}
return PLUGIN_HANDLED
}