Skip to content
View in the app

A better way to browse. Learn more.

Learn more

afk-c4-handoff/addons/amxmodx/scripting/olds_afkc4.sma

olds_afkc4.sma
272 lines · 7.4 KB · Pawn (AMX Mod X)
Raw
/*
 * AFK C4 Handoff (olds.ro workshop card LIB-0061)
 *
 * If the bomb carrier stays idle after the freeze time, the bomb is passed
 * to the nearest Terrorist who is active, so the round is not lost to an
 * AFK player.
 *
 * Commands: none.
 *
 * Cvars (in configs/plugins/olds_afkc4.cfg, which ships with the plugin; if
 * the file is missing, the plugin writes it with these defaults the first
 * time it starts):
 *   olds_afk_time  15  Seconds the carrier must stay idle after the freeze
 *                      time before the bomb is passed on (5 to 120).
 *   olds_afk_msg   1   1 = the old and the new carrier get a chat line.
 *
 * Rules:
 *   - A player is idle while he presses no key, does not move and does not
 *     turn. Counting starts when the freeze time ends.
 *   - The bomb goes to the nearest living Terrorist who was active in the
 *     last 5 seconds. If there is none (all dead or idle too), the bomb
 *     stays where it is and the check goes on until someone is active.
 *   - At most one handoff per round. After it, nothing more is done that
 *     round (if the new carrier leaves, the bomb drops as usual).
 *   - Nothing happens once the bomb is planted or lies on the ground.
 *
 * How it works, as the card asks: RG_CBasePlayer_PreThink keeps each
 * player's last activity (pev button, origin and view angles; origin and
 * angles are also compared once a second by the timer). After
 * RG_CSGameRules_OnRoundFreezeEnd a 1 second timer looks at the carrier;
 * when he has been idle for olds_afk_time seconds, rg_transfer_c4(carrier,
 * target) hands the bomb over and log_amx records it. RG_PlantBomb and
 * RG_CSGameRules_RestartRound end the check.
 *
 * 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[] = "AFK C4 Handoff";
new const PLUGIN_VERSION[] = "1.0.0";
new const PLUGIN_AUTHOR[] = "alexit (olds.ro)";

const TASK_CHECK = 6106;

// A teammate counts as active when he did something this recently.
const Float:RECENT_SECONDS = 5.0;

// Keys that show a player is at the keyboard.
const ACTIVITY_KEYS = IN_ATTACK | IN_JUMP | IN_DUCK | IN_FORWARD | IN_BACK | IN_USE
	| IN_MOVELEFT | IN_MOVERIGHT | IN_ATTACK2 | IN_RELOAD | IN_LEFT | IN_RIGHT;

new Float:g_afkTime;   // olds_afk_time
new g_message;         // olds_afk_msg

new bool:g_checking;   // between the freeze time end and the handoff / round end

new Float:g_lastActive[MAX_PLAYERS + 1];
new Float:g_lastOrigin[MAX_PLAYERS + 1][3];
new Float:g_lastAngles[MAX_PLAYERS + 1][3];

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

	if (!is_regamedll())
		set_fail_state("ReGameDLL_CS is not running. This plugin needs ReGameDLL_CS and ReAPI.");

	bind_pcvar_float(create_cvar("olds_afk_time", "15", FCVAR_NONE,
		"Seconds the bomb carrier must stay idle after the freeze time before the bomb is passed on",
		true, 5.0, true, 120.0), g_afkTime);

	bind_pcvar_num(create_cvar("olds_afk_msg", "1", FCVAR_NONE,
		"1 = the old and the new carrier get a chat line",
		true, 0.0, true, 1.0), g_message);

	AutoExecConfig(true, "olds_afkc4");

	RegisterHookChain(RG_CBasePlayer_PreThink, "on_player_prethink", false);
	RegisterHookChain(RG_CSGameRules_OnRoundFreezeEnd, "on_freeze_end_post", true);
	RegisterHookChain(RG_CSGameRules_RestartRound, "on_restart_round_pre", false);
	RegisterHookChain(RG_PlantBomb, "on_plant_bomb_post", true);
}

public client_putinserver(id)
{
	g_lastActive[id] = get_gametime();
}

public on_player_prethink(id)
{
	if (is_user_alive(id))
		note_activity(id, true);

	return HC_CONTINUE;
}

public on_freeze_end_post()
{
	// Idle time counts from now for everyone.
	new Float:now = get_gametime();

	for (new id = 1; id <= MaxClients; id++)
	{
		g_lastActive[id] = now;

		if (is_user_connected(id))
			remember_position(id);
	}

	g_checking = true;
	remove_task(TASK_CHECK);
	set_task(1.0, "task_check", TASK_CHECK, _, _, "b");
}

public on_restart_round_pre()
{
	stop_checking();
	return HC_CONTINUE;
}

/**
 * The bomb is planted: nothing more to hand over this round.
 */
public on_plant_bomb_post()
{
	stop_checking();
	return HC_CONTINUE;
}

public task_check()
{
	if (!g_checking)
	{
		stop_checking();
		return;
	}

	// Movement is also checked here, once a second, for players whose
	// frames do not run PreThink.
	for (new id = 1; id <= MaxClients; id++)
	{
		if (is_user_alive(id))
			note_activity(id, false);
	}

	new carrier = find_carrier();

	// Planted, dropped, or nobody alive has it: nothing to do.
	if (!carrier)
		return;

	new Float:now = get_gametime();
	new Float:idle = now - g_lastActive[carrier];

	if (idle < g_afkTime)
		return;

	new target = nearest_active_teammate(carrier, now);

	// Everyone else is dead or idle: the bomb stays, look again later.
	if (!target)
		return;

	if (!rg_transfer_c4(carrier, target))
		return;

	stop_checking();

	log_amx("AFK C4 handoff: ^"%n^" was idle for %d s, the bomb went to ^"%n^"", carrier, floatround(idle), target);

	if (g_message)
	{
		client_print_color(carrier, target, "%L", carrier, "AFK_C4_TAKEN", target);
		client_print_color(target, carrier, "%L", target, "AFK_C4_GIVEN", carrier);
	}
}

/**
 * Marks the player active when he presses a key (from PreThink), moved or
 * turned since the last look.
 */
note_activity(id, bool:checkKeys)
{
	new Float:origin[3], Float:angles[3];
	get_entvar(id, var_origin, origin);
	get_entvar(id, var_v_angle, angles);

	if ((checkKeys && (get_entvar(id, var_button) & ACTIVITY_KEYS))
		|| get_distance_f(origin, g_lastOrigin[id]) > 0.5
		|| floatabs(angles[0] - g_lastAngles[id][0]) > 0.1
		|| floatabs(angles[1] - g_lastAngles[id][1]) > 0.1)
	{
		g_lastActive[id] = get_gametime();
	}

	g_lastOrigin[id] = origin;
	g_lastAngles[id] = angles;
}

remember_position(id)
{
	get_entvar(id, var_origin, g_lastOrigin[id]);
	get_entvar(id, var_v_angle, g_lastAngles[id]);
}

stop_checking()
{
	g_checking = false;
	remove_task(TASK_CHECK);
}

/**
 * The living Terrorist who holds the bomb, or 0.
 */
find_carrier()
{
	for (new id = 1; id <= MaxClients; id++)
	{
		if (is_user_alive(id) && get_member(id, m_iTeam) == TEAM_TERRORIST && get_member(id, m_bHasC4))
			return id;
	}

	return 0;
}

nearest_active_teammate(carrier, Float:now)
{
	new Float:carrierOrigin[3], Float:origin[3];
	get_entvar(carrier, var_origin, carrierOrigin);

	new best = 0;
	new Float:bestDistance = 0.0;

	for (new id = 1; id <= MaxClients; id++)
	{
		if (id == carrier || !is_user_alive(id) || get_member(id, m_iTeam) != TEAM_TERRORIST)
			continue;

		if (now - g_lastActive[id] >= RECENT_SECONDS)
			continue;

		get_entvar(id, var_origin, origin);
		new Float:distance = get_distance_f(carrierOrigin, origin);

		if (!best || distance < bestDistance)
		{
			best = id;
			bestDistance = distance;
		}
	}

	return best;
}

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.