/*
* Fumble on Heavy Hit (olds.ro workshop idea IDEA-6D91FFB0948DB040)
*
* American Football add-on: when the ball carrier takes one heavy hit he fumbles. The
* ball pops loose in a random direction, and for a moment neither the fumbler nor the
* hitter can pick it up, so a third player decides the play.
*
* say /fumbles forced fumbles of this map, per hitter
*
* Needs the football core (library "olds_afootball", include/olds_afootball.inc):
* olds_af_get_carrier, olds_af_drop_ball and the forward olds_af_ball_pickup. Without
* the core the plugin loads and stays idle. A hit that kills the carrier is not a
* fumble: the core drops the ball at the body as usual.
*
* Cvars: olds_fumble_damage 40, olds_fumble_speed 250, olds_fumble_lock 1.5,
* olds_fumble_chance 100.
*
* Copyright (C) 2026 daemon (olds.ro)
* SPDX-License-Identifier: GPL-3.0-or-later
*/
#include <amxmodx>
#include <fakemeta>
#include <reapi>
#include <olds_afootball>
#define PLUGIN "Fumble on Heavy Hit"
#define VERSION "1.0.0"
#define AUTHOR "olds.ro"
#define SND_FUMBLE "common/bodydrop3.wav"
const MAX_RECORDS = 64;
const LIST_LINES = 8;
new Float:g_damage, Float:g_speed, Float:g_lock, g_chance;
new bool:g_core;
new Float:g_lockUntil[MAX_PLAYERS + 1];
new Float:g_nextUse[MAX_PLAYERS + 1];
/* Forced fumbles of the map, per hitter (kept across reconnects). */
new g_recAuth[MAX_RECORDS][MAX_AUTHID_LENGTH];
new g_recName[MAX_RECORDS][MAX_NAME_LENGTH];
new g_recCount[MAX_RECORDS];
new g_records;
public plugin_natives()
{
set_native_filter("native_filter");
}
public native_filter(const name[], index, trap)
{
/* The core is optional: without it this plugin stays idle. */
if (!trap && equal(name, "olds_af_", 8))
return PLUGIN_HANDLED;
return PLUGIN_CONTINUE;
}
public plugin_precache()
{
precache_sound(SND_FUMBLE);
}
public plugin_init()
{
register_plugin(PLUGIN, VERSION, AUTHOR);
register_dictionary("olds_fumble.txt");
bind_pcvar_float(create_cvar("olds_fumble_damage", "40", FCVAR_NONE,
"Damage of one hit that makes the carrier fumble", true, 1.0, true, 1000.0), g_damage);
bind_pcvar_float(create_cvar("olds_fumble_speed", "250", FCVAR_NONE,
"Horizontal speed of the loose ball", true, 0.0, true, 2000.0), g_speed);
bind_pcvar_float(create_cvar("olds_fumble_lock", "1.5", FCVAR_NONE,
"Seconds the fumbler and the hitter cannot pick the ball up", true, 0.0, true, 10.0), g_lock);
bind_pcvar_num(create_cvar("olds_fumble_chance", "100", FCVAR_NONE,
"Chance in percent that a heavy hit makes the carrier fumble", true, 0.0, true, 100.0), g_chance);
AutoExecConfig(true, "olds_fumble");
register_clcmd("say /fumbles", "cmd_fumbles");
register_clcmd("say_team /fumbles", "cmd_fumbles");
RegisterHookChain(RG_CBasePlayer_TakeDamage, "on_take_damage", true);
RegisterHookChain(RG_CSGameRules_RestartRound, "on_restart_round", true);
}
public plugin_cfg()
{
g_core = LibraryExists("olds_afootball", LibType_Library) != 0;
if (!g_core)
log_amx("No football core (library olds_afootball): fumbles stay off");
}
public on_restart_round()
{
for (new i = 1; i <= MaxClients; i++)
g_lockUntil[i] = 0.0;
}
public client_disconnected(id)
{
g_lockUntil[id] = 0.0;
g_nextUse[id] = 0.0;
}
/* ---------- the hit ---------- */
public on_take_damage(victim, inflictor, attacker, Float:damage, damage_bits)
{
if (!g_core || damage < g_damage)
return HC_CONTINUE;
if (!(1 <= attacker <= MaxClients) || attacker == victim || !is_user_connected(attacker))
return HC_CONTINUE;
// A hit that kills is not a fumble: the core drops the ball at the body.
if (!is_user_alive(victim) || olds_af_get_carrier() != victim)
return HC_CONTINUE;
if (g_chance < 100 && random_num(1, 100) > g_chance)
return HC_CONTINUE;
new ball = olds_af_drop_ball();
if (ball <= 0 || !pev_valid(ball))
return HC_CONTINUE;
new Float:angle = random_float(0.0, 360.0);
new Float:velocity[3];
velocity[0] = floatcos(angle, degrees) * g_speed;
velocity[1] = floatsin(angle, degrees) * g_speed;
velocity[2] = 160.0;
set_pev(ball, pev_velocity, velocity);
emit_sound(ball, CHAN_ITEM, SND_FUMBLE, VOL_NORM, ATTN_NORM, 0, PITCH_NORM);
new Float:until = get_gametime() + g_lock;
g_lockUntil[victim] = until;
g_lockUntil[attacker] = until;
count_fumble(attacker);
new victim_name[MAX_NAME_LENGTH], attacker_name[MAX_NAME_LENGTH];
get_user_name(victim, victim_name, charsmax(victim_name));
get_user_name(attacker, attacker_name, charsmax(attacker_name));
client_print_color(0, attacker, "^4[Football]^1 %l", "FUMBLE_CHAT", victim_name, attacker_name,
floatround(damage));
set_hudmessage(255, 160, 0, -1.0, 0.30, 0, 0.0, 2.0, 0.1, 0.2, -1);
show_hudmessage(0, "%l", "FUMBLE_HUD");
log_amx("Fumble: %s hit %s for %.0f, the ball is loose", attacker_name, victim_name, damage);
return HC_CONTINUE;
}
/* The core asks before a player picks the loose ball up. */
public olds_af_ball_pickup(id, ball)
{
if (1 <= id <= MaxClients && get_gametime() < g_lockUntil[id])
return PLUGIN_HANDLED;
return PLUGIN_CONTINUE;
}
/* ---------- forced fumbles ---------- */
count_fumble(id)
{
new auth[MAX_AUTHID_LENGTH], name[MAX_NAME_LENGTH];
get_user_authid(id, auth, charsmax(auth));
get_user_name(id, name, charsmax(name));
// Bots and LAN ids share their auth string: key them by name.
if (is_user_bot(id) || equal(auth, "STEAM_ID_LAN") || equal(auth, "BOT") || !auth[0])
formatex(auth, charsmax(auth), "name:%s", name);
for (new i = 0; i < g_records; i++)
{
if (equal(g_recAuth[i], auth))
{
g_recCount[i]++;
copy(g_recName[i], charsmax(g_recName[]), name);
return;
}
}
if (g_records >= MAX_RECORDS)
return;
copy(g_recAuth[g_records], charsmax(g_recAuth[]), auth);
copy(g_recName[g_records], charsmax(g_recName[]), name);
g_recCount[g_records] = 1;
g_records++;
}
public cmd_fumbles(id)
{
if (!is_user_connected(id))
return PLUGIN_HANDLED;
new Float:now = get_gametime();
if (now < g_nextUse[id])
return PLUGIN_HANDLED;
g_nextUse[id] = now + 2.0;
if (!g_records)
{
client_print_color(id, print_team_default, "^4[Football]^1 %L", id, "FUMBLE_NONE");
return PLUGIN_HANDLED;
}
// Most forced fumbles first (selection over a copy of the indexes).
new order[MAX_RECORDS], best, tmp;
for (new i = 0; i < g_records; i++)
order[i] = i;
for (new i = 0; i < g_records - 1; i++)
{
best = i;
for (new k = i + 1; k < g_records; k++)
if (g_recCount[order[k]] > g_recCount[order[best]])
best = k;
tmp = order[i];
order[i] = order[best];
order[best] = tmp;
}
client_print_color(id, print_team_default, "^4[Football]^1 %L", id, "FUMBLE_HEAD");
new lines = min(g_records, LIST_LINES);
for (new i = 0; i < lines; i++)
client_print_color(id, print_team_default, "^4[Football]^1 %d. %s: %d", i + 1,
g_recName[order[i]], g_recCount[order[i]]);
return PLUGIN_HANDLED;
}