Skip to content
View in the app

A better way to browse. Learn more.

Learn more

afk-detection-and-kick/addons/amxmodx/scripting/olds_afk.sma

olds_afk.sma
322 lines · 9.1 KB · Pawn (AMX Mod X)
Raw
/*
 * AFK Detection And Kick (olds.ro workshop card LIB-1395)
 *
 * A living player who neither moves nor turns the view for olds_afk_time
 * seconds is moved to the spectators (or kicked). During the last
 * olds_afk_warn seconds the player sees a countdown in the centre of the
 * screen and hears a short beep every second.
 *
 * The idle clock pauses while it would be unfair to count: during the freeze
 * time, and while the player has a game menu (team or buy menu) or a plugin
 * menu open, for at most 30 seconds in a row (a text menu closed with Esc
 * still counts as open on the server, so a longer pause could hide an AFK
 * player). Dead players and spectators are not counted at all, and every
 * new round starts everyone from zero. Admins with the olds_afk_immune flags
 * are never counted. With fewer than olds_afk_minplayers players on the
 * server the plugin does nothing.
 *
 * How it works, as the card asks: FM_PlayerPreThink samples each player's
 * pev_origin and pev_v_angle once a second, the warning uses
 * client_print(print_center) and client_cmd(id, "spk buttons/button10"), and
 * RG_CSGameRules_RestartRound resets the counters at every new round.
 *
 * Cvars (in configs/plugins/olds_afk.cfg, written with the defaults on the
 * first start if the file is missing):
 *   olds_afk_time       45    Idle seconds before the action (10 - 3600).
 *   olds_afk_warn       10    Seconds of warning before the action (0 - 60).
 *   olds_afk_action     1     1 = move to the spectators, 2 = kick.
 *   olds_afk_minplayers 6     Do nothing with fewer players than this on the
 *                             server (0 - 32).
 *   olds_afk_immune     "a"   Admin flags that are never counted (any of
 *                             them is enough); empty = nobody is immune.
 *
 * Player texts are in data/lang/olds_afk.txt.
 *
 * Requires AMX Mod X 1.10 with ReAPI (ReGameDLL_CS) and the fakemeta and fun
 * modules. Written and tested on 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 <fakemeta>
#include <fun>
#include <reapi>

#pragma semicolon 1

new const PLUGIN_NAME[] = "AFK Detection And Kick";
new const PLUGIN_VERSION[] = "1.0.0";
new const PLUGIN_AUTHOR[] = "alexit (olds.ro)";

// How often each player is sampled.
const Float:SAMPLE_PERIOD = 1.0;

// Longest time a menu that stays open pauses the idle clock.
const Float:MENU_PAUSE_LIMIT = 30.0;

// Smallest change that counts as moving (units) or turning (degrees).
const Float:MOVE_DISTANCE = 1.0;
const Float:TURN_ANGLE = 0.5;

new const WARN_SOUND[] = "buttons/button10";

enum
{
	ACTION_SPECTATE = 1,
	ACTION_KICK
};

new g_afkTime;       // olds_afk_time
new g_warnTime;      // olds_afk_warn
new g_action;        // olds_afk_action
new g_minPlayers;    // olds_afk_minplayers

new g_pcvarImmune;
new g_msgScoreInfo;

// Seconds each player has been idle, and when they were sampled next.
new Float:g_idle[MAX_PLAYERS + 1];

// Seconds a menu has paused the idle clock since the player last moved.
new Float:g_menuPause[MAX_PLAYERS + 1];
new Float:g_nextSample[MAX_PLAYERS + 1];

// Position and view at the last sample; g_hasSample is false until the first.
new bool:g_hasSample[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_afk.txt");

	bind_pcvar_num(create_cvar("olds_afk_time", "45", FCVAR_NONE,
		"Idle seconds before an AFK player is moved or kicked",
		true, 10.0, true, 3600.0), g_afkTime);

	bind_pcvar_num(create_cvar("olds_afk_warn", "10", FCVAR_NONE,
		"Seconds of countdown warning before the action",
		true, 0.0, true, 60.0), g_warnTime);

	bind_pcvar_num(create_cvar("olds_afk_action", "1", FCVAR_NONE,
		"1 = move AFK players to the spectators, 2 = kick them",
		true, 1.0, true, 2.0), g_action);

	bind_pcvar_num(create_cvar("olds_afk_minplayers", "6", FCVAR_NONE,
		"Do nothing while fewer players than this are on the server",
		true, 0.0, true, 32.0), g_minPlayers);

	g_pcvarImmune = create_cvar("olds_afk_immune", "a", FCVAR_NONE,
		"Admin flags that are never counted as AFK (empty = nobody)");

	AutoExecConfig(true, "olds_afk");

	g_msgScoreInfo = get_user_msgid("ScoreInfo");

	register_forward(FM_PlayerPreThink, "fw_player_prethink");
	RegisterHookChain(RG_CSGameRules_RestartRound, "fw_restart_round_pre", false);
}

public client_putinserver(id)
{
	reset_player(id);
}

public client_disconnected(id)
{
	reset_player(id);
}

public fw_restart_round_pre()
{
	for (new id = 1; id <= MaxClients; id++)
		reset_player(id);

	return HC_CONTINUE;
}

public fw_player_prethink(id)
{
	new Float:now = get_gametime();

	if (now < g_nextSample[id])
		return FMRES_IGNORED;

	g_nextSample[id] = now + SAMPLE_PERIOD;
	sample_player(id);

	return FMRES_IGNORED;
}

sample_player(id)
{
	// Only living players in a team, never the HLTV proxy, and only on a
	// server with enough players.
	if (!is_user_alive(id) || is_user_hltv(id) || get_playersnum() < g_minPlayers)
	{
		reset_player(id);
		return;
	}

	new TeamName:team = get_member(id, m_iTeam);

	if (team != TEAM_TERRORIST && team != TEAM_CT)
	{
		reset_player(id);
		return;
	}

	if (is_immune(id))
	{
		reset_player(id);
		return;
	}

	new Float:origin[3], Float:angles[3];
	pev(id, pev_origin, origin);
	pev(id, pev_v_angle, angles);

	new bool:moved = !g_hasSample[id]
		|| get_distance_f(origin, g_lastOrigin[id]) > MOVE_DISTANCE
		|| floatabs(angles[0] - g_lastAngles[id][0]) > TURN_ANGLE
		|| floatabs(angles[1] - g_lastAngles[id][1]) > TURN_ANGLE;

	g_lastOrigin[id] = origin;
	g_lastAngles[id] = angles;
	g_hasSample[id] = true;

	if (moved)
	{
		g_idle[id] = 0.0;
		g_menuPause[id] = 0.0;
		return;
	}

	// Nobody can move in the freeze time.
	if (get_member_game(m_bFreezePeriod))
		return;

	// A player choosing a team, a weapon or a menu item is not away, for a
	// while.
	if (in_menu(id) && g_menuPause[id] < MENU_PAUSE_LIMIT)
	{
		g_menuPause[id] += SAMPLE_PERIOD;
		return;
	}

	g_idle[id] += SAMPLE_PERIOD;

	new Float:left = float(g_afkTime) - g_idle[id];

	if (left <= 0.0)
	{
		take_action(id);
		return;
	}

	if (left <= float(g_warnTime))
		warn_player(id, floatround(left, floatround_ceil));
}

warn_player(id, seconds)
{
	client_print(id, print_center, "%L", id,
		g_action == ACTION_KICK ? "AFK_WARN_KICK" : "AFK_WARN_SPEC", seconds);
	client_cmd(id, "spk %s", WARN_SOUND);
}

take_action(id)
{
	new name[MAX_NAME_LENGTH];
	get_user_name(id, name, charsmax(name));

	reset_player(id);

	if (g_action == ACTION_KICK)
	{
		new reason[128];
		formatex(reason, charsmax(reason), "%L", id, "AFK_KICK_REASON");

		// kick takes the rest of the line as the message, quotes included,
		// so none are added; quotes and semicolons are taken out so the text
		// can never end the command.
		replace_all(reason, charsmax(reason), "^"", "'");
		replace_all(reason, charsmax(reason), ";", ",");

		log_amx("^"%s<%d>^" kicked for being AFK", name, get_user_userid(id));
		client_print_color(0, print_team_default, "%L", LANG_PLAYER, "AFK_KICKED", name);
		server_cmd("kick #%d %s", get_user_userid(id), reason);
		return;
	}

	log_amx("^"%s<%d>^" moved to the spectators for being AFK", name, get_user_userid(id));

	// The silent kill gives the frag back but still counts a death, so the
	// deaths are put back afterwards. The bomb, if carried, drops.
	new deaths = get_member(id, m_iDeaths);

	user_silentkill(id, 1);
	rg_join_team(id, TEAM_SPECTATOR);

	set_member(id, m_iDeaths, deaths);
	send_score_info(id);

	client_print_color(0, print_team_default, "%L", LANG_PLAYER, "AFK_MOVED", name);
}

/**
 * Sends the player's scoreboard line to everyone.
 */
send_score_info(id)
{
	emessage_begin(MSG_ALL, g_msgScoreInfo);
	ewrite_byte(id);
	ewrite_short(floatround(Float:get_entvar(id, var_frags)));
	ewrite_short(get_member(id, m_iDeaths));
	ewrite_short(0);                        // class id, not used by Counter-Strike
	ewrite_short(get_member(id, m_iTeam));
	emessage_end();
}

reset_player(id)
{
	g_idle[id] = 0.0;
	g_menuPause[id] = 0.0;
	g_hasSample[id] = false;
}

bool:in_menu(id)
{
	// Bots never see the game menus; the value left from their team choice
	// would pause them for nothing.
	if (!is_user_bot(id) && _:get_member(id, m_iMenu) != _:Menu_OFF)
		return true;

	new oldMenu, newMenu;
	return bool:player_menu_info(id, oldMenu, newMenu);
}

bool:is_immune(id)
{
	new flags[32];
	get_pcvar_string(g_pcvarImmune, flags, charsmax(flags));

	if (!flags[0])
		return false;

	return (get_user_flags(id) & read_flags(flags)) != 0;
}

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.