/*
* Knife Demotion Rule (olds.ro) - Arms Race 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/>.
*
* A knife kill pushes the victim back one weapon level (never below level 1)
* and gives the killer olds_ardemote_progress progress points instead of
* whatever the core gives for a kill; the killer keeps their level. Both
* players get a short director HUD line and the victim hears the knife hit.
*
* Levels are read and written through the Arms Race core natives
* ar_get_level(id), ar_set_level(id, level) required
* ar_get_progress(id), ar_set_progress(id, points) optional
* (a native filter lets the plugin load without them; it then stays idle,
* or, without the progress natives, leaves the killer's progress alone).
* The core handles the kill first: 0.1 s later the killer's level is put
* back to what it was before the kill and the progress point added.
*
* A kill counts as a knife kill when the killer's active item is the knife
* (get_member(attacker, m_pActiveItem)) and the killing blow came straight
* from the killer, not from a grenade or other entity, and was not a blast.
*/
#include <amxmodx>
#include <fakemeta>
#include <reapi>
#define PLUGIN "Knife Demotion Rule"
#define VERSION "1.0.0"
#define AUTHOR "daemon (olds.ro)"
#define TASK_APPLY 56448
#define APPLY_DELAY 0.1
native ar_get_level(id)
native ar_set_level(id, level)
native ar_get_progress(id)
native ar_set_progress(id, points)
new bool:g_hasLevels = true, bool:g_hasProgress = true
new g_cvOn, g_cvProgress
new bool:g_directHit[MAX_PLAYERS + 1] // the last damage came straight from a player, not a blast
new g_victimKiller[MAX_PLAYERS + 1] // pending demotion: who knifed this victim
new g_victimLevel[MAX_PLAYERS + 1] // the victim's level before the kill
new g_killerPending[MAX_PLAYERS + 1] // knife kills of this killer waiting to be applied
new g_killerLevel[MAX_PLAYERS + 1] // the killer's level before the first of them
new g_killerProgress[MAX_PLAYERS + 1]
new g_killerAward[MAX_PLAYERS + 1]
public plugin_natives()
{
set_native_filter("native_filter")
}
public native_filter(const name[], index, trap)
{
if (trap)
return PLUGIN_CONTINUE
if (equal(name, "ar_get_level") || equal(name, "ar_set_level"))
{
g_hasLevels = false
return PLUGIN_HANDLED
}
if (equal(name, "ar_get_progress") || equal(name, "ar_set_progress"))
{
g_hasProgress = false
return PLUGIN_HANDLED
}
return PLUGIN_CONTINUE
}
public plugin_init()
{
register_plugin(PLUGIN, VERSION, AUTHOR)
register_dictionary("olds_ardemote.txt")
g_cvOn = create_cvar("olds_ardemote", "1", FCVAR_NONE, "1 = a knife kill demotes the victim one level", true, 0.0, true, 1.0)
g_cvProgress = create_cvar("olds_ardemote_progress", "1", FCVAR_NONE, "Progress points the killer gets for a knife kill", true, 0.0, true, 10.0)
AutoExecConfig(true, "olds_ardemote")
RegisterHookChain(RG_CBasePlayer_TakeDamage, "on_take_damage_pre", false)
RegisterHookChain(RG_CBasePlayer_Killed, "on_killed_pre", false)
if (!g_hasLevels)
log_amx("The Arms Race core natives ar_get_level/ar_set_level are missing: knife demotion is off")
else if (!g_hasProgress)
log_amx("The Arms Race core has no ar_get_progress/ar_set_progress: knife kills give no progress point")
}
public client_disconnected(id)
{
remove_task(TASK_APPLY + id)
g_directHit[id] = false
g_victimKiller[id] = 0
g_killerPending[id] = 0
g_killerAward[id] = 0
for (new v = 1; v <= MaxClients; v++)
{
if (g_victimKiller[v] == id)
g_victimKiller[v] = 0
}
}
public on_take_damage_pre(victim, inflictor, attacker, Float:damage, bits)
{
g_directHit[victim] = attacker >= 1 && attacker <= MaxClients && inflictor == attacker && !(bits & DMG_BLAST)
return HC_CONTINUE
}
bool:knife_kill(victim, attacker)
{
if (attacker < 1 || attacker > MaxClients || attacker == victim || !is_user_connected(attacker))
return false
if (!g_directHit[victim])
return false
new item = get_member(attacker, m_pActiveItem)
return !is_nullent(item) && WeaponIdType:get_member(item, m_iId) == WEAPON_KNIFE
}
public on_killed_pre(victim, attacker, gib)
{
new bool:knife = knife_kill(victim, attacker)
g_directHit[victim] = false
if (!knife || !g_hasLevels || !get_pcvar_num(g_cvOn))
return HC_CONTINUE
g_victimKiller[victim] = attacker
g_victimLevel[victim] = ar_get_level(victim)
if (!g_killerPending[attacker])
{
g_killerLevel[attacker] = ar_get_level(attacker)
g_killerProgress[attacker] = 0
if (g_hasProgress)
g_killerProgress[attacker] = ar_get_progress(attacker)
g_killerAward[attacker] = 0
}
g_killerPending[attacker]++
g_killerAward[attacker] += get_pcvar_num(g_cvProgress)
remove_task(TASK_APPLY + victim)
set_task(APPLY_DELAY, "apply_demotion", TASK_APPLY + victim)
return HC_CONTINUE
}
public apply_demotion(task)
{
new victim = task - TASK_APPLY
new killer = g_victimKiller[victim]
g_victimKiller[victim] = 0
if (!killer || !is_user_connected(victim))
return
new before = g_victimLevel[victim]
new after = max(1, before - 1)
ar_set_level(victim, after)
new killerLevel = g_killerLevel[killer], award = g_killerAward[killer]
if (g_killerPending[killer])
{
// Undo what the core gave for the kill: same level, plus the progress point(s).
ar_set_level(killer, killerLevel)
if (g_hasProgress && award > 0)
ar_set_progress(killer, g_killerProgress[killer] + award)
g_killerPending[killer] = 0
g_killerAward[killer] = 0
}
new vname[32], kname[32]
get_user_name(victim, vname, charsmax(vname))
get_user_name(killer, kname, charsmax(kname))
set_dhudmessage(255, 90, 60, -1.0, 0.30, 0, 0.0, 3.0, 0.1, 0.4)
if (after < before)
{
show_dhudmessage(victim, "%L", victim, "ARD_VICTIM", kname, before, after)
client_cmd(victim, "spk weapons/knife_hit1")
}
else
show_dhudmessage(victim, "%L", victim, "ARD_VICTIM_FLOOR", kname)
set_dhudmessage(90, 220, 90, -1.0, 0.30, 0, 0.0, 3.0, 0.1, 0.4)
if (g_hasProgress && award > 0)
show_dhudmessage(killer, "%L", killer, "ARD_KILLER", vname, award, killerLevel)
else
show_dhudmessage(killer, "%L", killer, "ARD_KILLER_NOPOINT", vname, killerLevel)
log_amx("Knife kill: %s level %d -> %d; %s stays on level %d, +%d progress", vname, before, after, kname, killerLevel, award)
}