Skip to content
View in the app

A better way to browse. Learn more.

Learn more

dead-and-all-talk-toggle/addons/amxmodx/scripting/olds_voice.sma

olds_voice.sma
392 lines · 10.7 KB · Pawn (AMX Mod X)
Raw
/*
 * Dead And All Talk Toggle (olds.ro workshop card LIB-1542)
 *
 * Switches the voice rules between normal (each team hears only itself),
 * all-talk (everyone hears everyone) and dead-talk (dead players hear the
 * living, the living do not hear the dead). While few players are on, it
 * turns all-talk on by itself.
 *
 * Admin command (ADMIN_CFG, flag "h"; also from the server console):
 *   amx_voicemode <0|1|2>   0 = normal, 1 = all-talk, 2 = dead-talk.
 *                           Without a value it prints the rules in use.
 *
 * Server command:
 *   olds_voice_status   prints the rules in use and, for every player, whom
 *                       they hear under those rules
 *
 * Cvars (saved with their descriptions in configs/plugins/olds_voice.cfg
 * the first time the plugin starts):
 *   olds_voice_mode  0   the rules amx_voicemode sets (0, 1 or 2)
 *   olds_voice_auto  6   all-talk while fewer players than this are
 *                        connected (bots count); 0 = never
 *   olds_voice_dead  1   dead-talk: 1 = dead players hear both teams,
 *                        0 = only their own team
 *
 * Automatic all-talk is checked when a player joins or leaves and at every
 * round start. It turns on at once when the count drops below
 * olds_voice_auto; when enough players are on again, the normal rules come
 * back at the next round start, never in the middle of a round.
 *
 * The rules are applied in FM_Voice_SetClientListening, which the game
 * calls for every pair of players a few times a second: the plugin sets
 * its answer with EngFunc_SetClientListening and stops the game's own.
 * With mode 0 and no automatic all-talk the game's own rules (and
 * sv_alltalk) are left alone.
 *
 * Requires AMX Mod X 1.10 or newer with the fakemeta module, and ReAPI
 * (ReGameDLL_CS) for the round start hook.
 *
 * 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 <amxmisc>
#include <fakemeta>
#include <reapi>

#pragma semicolon 1

new const PLUGIN_NAME[] = "Dead And All Talk Toggle";
new const PLUGIN_VERSION[] = "1.0.0";
new const PLUGIN_AUTHOR[] = "alexit (olds.ro)";

new const DICTIONARY[] = "olds_voice.txt";

enum
{
	MODE_NORMAL = 0,
	MODE_ALLTALK,
	MODE_DEADTALK
};

new const MODE_NAMES[][] = { "normal", "all-talk", "dead-talk" };
new const NOW_KEYS[][] = { "OLDS_VOICE_NOW_0", "OLDS_VOICE_NOW_1", "OLDS_VOICE_NOW_2" };
new const CHAT_KEYS[][] = { "OLDS_VOICE_CHAT_0", "OLDS_VOICE_CHAT_1", "OLDS_VOICE_CHAT_2" };

new g_pcvarMode;
new g_pcvarAllTalk;

new g_mode;             // olds_voice_mode
new g_auto;             // olds_voice_auto
new g_deadHearAll;      // olds_voice_dead

new bool:g_autoAllTalk;
new bool:g_normalPending;

public plugin_init()
{
	register_plugin(PLUGIN_NAME, PLUGIN_VERSION, PLUGIN_AUTHOR);
	register_dictionary(DICTIONARY);

	g_pcvarMode = create_cvar("olds_voice_mode", "0", FCVAR_NONE,
		"Voice rules: 0 = normal (team only), 1 = all-talk, 2 = dead-talk; amx_voicemode sets it",
		true, 0.0, true, 2.0);
	bind_pcvar_num(g_pcvarMode, g_mode);

	new pcvar = create_cvar("olds_voice_auto", "6", FCVAR_NONE,
		"All-talk while fewer players than this are connected (bots count); 0 = never",
		true, 0.0, true, 32.0);
	bind_pcvar_num(pcvar, g_auto);
	hook_cvar_change(pcvar, "on_auto_change");

	bind_pcvar_num(create_cvar("olds_voice_dead", "1", FCVAR_NONE,
		"Dead-talk: 1 = dead players hear both teams, 0 = only their own team",
		true, 0.0, true, 1.0), g_deadHearAll);

	AutoExecConfig(true, "olds_voice");

	register_concmd("amx_voicemode", "cmd_voicemode", ADMIN_CFG, "OLDS_VOICE_HELP", -1, true);
	register_srvcmd("olds_voice_status", "cmd_status");

	register_forward(FM_Voice_SetClientListening, "fw_set_client_listening");
	RegisterHookChain(RG_CSGameRules_RestartRound, "fw_restart_round_post", true);

	g_pcvarAllTalk = get_cvar_pointer("sv_alltalk");
}

public OnConfigsExecuted()
{
	update_auto(g_auto, true, 0);
}

public client_putinserver(id)
{
	update_auto(g_auto, false, 0);
}

public client_disconnected(id)
{
	update_auto(g_auto, false, id);
}

public fw_restart_round_post()
{
	update_auto(g_auto, true, 0);
}

public on_auto_change(pcvar, const oldValue[], const newValue[])
{
	update_auto(str_to_num(newValue), true, 0);
}

/**
 * Turns automatic all-talk on at once when fewer than limit players are
 * connected. Turns it off only when switchOff is true (round start, map
 * start, cvar change); otherwise the change waits for the next round start.
 * exclude = a player who is leaving and must not be counted.
 */
update_auto(limit, bool:switchOff, exclude)
{
	new count = count_players(exclude);

	if (limit > 0 && count < limit)
	{
		g_normalPending = false;

		if (!g_autoAllTalk)
		{
			g_autoAllTalk = true;
			client_print_color(0, print_team_default, "^1%L", LANG_PLAYER, "OLDS_VOICE_AUTO_ON", limit);
			log_amx("Automatic all-talk on: %d player(s), fewer than olds_voice_auto %d.", count, limit);
		}

		return;
	}

	if (!g_autoAllTalk)
	{
		g_normalPending = false;
		return;
	}

	if (switchOff)
	{
		g_autoAllTalk = false;
		g_normalPending = false;
		client_print_color(0, print_team_default, "^1%L", LANG_PLAYER, "OLDS_VOICE_AUTO_BACK", count);
		log_amx("Automatic all-talk off: %d player(s); the %s rules are back.", count, MODE_NAMES[g_mode]);
		return;
	}

	if (!g_normalPending)
	{
		g_normalPending = true;
		client_print_color(0, print_team_default, "^1%L", LANG_PLAYER, "OLDS_VOICE_AUTO_NEXT", count);
		log_amx("Enough players (%d): automatic all-talk ends at the next round start.", count);
	}
}

count_players(exclude)
{
	new players[MAX_PLAYERS], count, total;
	get_players(players, count, "h");

	for (new i = 0; i < count; i++)
	{
		if (players[i] != exclude)
			total++;
	}

	return total;
}

/**
 * The game asks whether receiver hears sender.
 */
public fw_set_client_listening(receiver, sender, listen)
{
	if (receiver == sender || (!g_autoAllTalk && g_mode == MODE_NORMAL))
		return FMRES_IGNORED;

	if (!is_user_connected(receiver) || !is_user_connected(sender))
		return FMRES_IGNORED;

	new bool:hear = can_hear(receiver, sender);

	engfunc(EngFunc_SetClientListening, receiver, sender, hear);
	forward_return(FMV_CELL, hear);
	return FMRES_SUPERCEDE;
}

/**
 * The rules in use. For mode 0 it gives what the game itself decides
 * (used by olds_voice_status only).
 */
bool:can_hear(receiver, sender)
{
	if (g_autoAllTalk || g_mode == MODE_ALLTALK)
		return true;

	new bool:sameTeam = (get_user_team(receiver) == get_user_team(sender));
	new bool:receiverAlive = (is_user_alive(receiver) != 0);
	new bool:senderAlive = (is_user_alive(sender) != 0);

	if (g_mode == MODE_DEADTALK)
	{
		// The dead hear the living and the dead; the living hear only
		// living team mates.
		if (!receiverAlive)
			return (g_deadHearAll != 0 || sameTeam);

		return (senderAlive && sameTeam);
	}

	if (g_pcvarAllTalk && get_pcvar_num(g_pcvarAllTalk))
		return true;

	if (!sameTeam)
		return false;

	return !(receiverAlive && !senderAlive);
}

/**
 * amx_voicemode <0|1|2>
 */
public cmd_voicemode(id, level, cid)
{
	if (!admin_allowed(id, level))
		return PLUGIN_HANDLED;

	if (read_argc() < 2)
	{
		console_print(id, "%L", id, NOW_KEYS[g_mode]);

		if (g_autoAllTalk)
			console_print(id, "%L", id, "OLDS_VOICE_AUTO_ACTIVE", g_auto);

		return PLUGIN_HANDLED;
	}

	new arg[8];
	read_argv(1, arg, charsmax(arg));

	if (strlen(arg) != 1 || arg[0] < '0' || arg[0] > '2')
	{
		console_print(id, "%L", id, "OLDS_VOICE_USAGE");
		return PLUGIN_HANDLED;
	}

	new mode = arg[0] - '0';
	set_pcvar_num(g_pcvarMode, mode);
	g_mode = mode;

	console_print(id, "%L", id, NOW_KEYS[mode]);

	if (g_autoAllTalk)
		console_print(id, "%L", id, "OLDS_VOICE_AUTO_ACTIVE", g_auto);

	new name[MAX_NAME_LENGTH];

	if (id)
		get_user_name(id, name, charsmax(name));
	else
		copy(name, charsmax(name), "Server");

	client_print_color(0, print_team_default, "^1%L", LANG_PLAYER, CHAT_KEYS[mode], name);

	new who[96];
	describe_admin(id, who, charsmax(who));
	log_amx("Cmd: %s set the voice rules to %s (olds_voice_mode %d)", who, MODE_NAMES[mode], mode);
	return PLUGIN_HANDLED;
}

/**
 * olds_voice_status
 */
public cmd_status()
{
	new players[MAX_PLAYERS], count;
	get_players(players, count, "h");

	new rules[64];

	if (g_autoAllTalk)
		formatex(rules, charsmax(rules), "automatic all-talk (mode %d %s waits)", g_mode, MODE_NAMES[g_mode]);
	else
		formatex(rules, charsmax(rules), "mode %d %s", g_mode, MODE_NAMES[g_mode]);

	server_print("[olds_voice] rules: %s; %d player(s), olds_voice_auto %d, olds_voice_dead %d",
		rules, count, g_auto, g_deadHearAll);

	if (g_normalPending)
		server_print("[olds_voice] the normal rules come back at the next round start");

	new receiver, line[192], name[MAX_NAME_LENGTH], team[16], heard;

	for (new i = 0; i < count; i++)
	{
		receiver = players[i];
		get_user_name(receiver, name, charsmax(name));
		get_user_team(receiver, team, charsmax(team));

		formatex(line, charsmax(line), "%s (%s, ", name, team);

		if (is_user_alive(receiver))
			add(line, charsmax(line), "alive) hears:");
		else
			add(line, charsmax(line), "dead) hears:");

		heard = 0;

		for (new j = 0; j < count; j++)
		{
			if (players[j] == receiver || !can_hear(receiver, players[j]))
				continue;

			get_user_name(players[j], name, charsmax(name));
			add(line, charsmax(line), " ");
			add(line, charsmax(line), name);
			heard++;
		}

		if (!heard)
			add(line, charsmax(line), " nobody");

		server_print("[olds_voice] %s", line);
	}

	return PLUGIN_HANDLED;
}

/**
 * The same rules as cmd_access: the server console (and the host of a listen
 * server) may always; ADMIN_ALL = everyone; ADMIN_ADMIN = any admin flag.
 * The level can be changed in configs/cmdaccess.ini.
 */
bool:admin_allowed(id, level)
{
	if (id == (is_dedicated_server() ? 0 : 1) || level == ADMIN_ALL)
		return true;

	if ((level == ADMIN_ADMIN && is_user_admin(id)) || (get_user_flags(id) & level))
		return true;

	console_print(id, "%L", id, "OLDS_VOICE_NO_ACCESS");
	return false;
}

describe_admin(id, out[], maxLength)
{
	if (!id)
	{
		copy(out, maxLength, "Server");
		return;
	}

	new name[MAX_NAME_LENGTH], authid[44];
	get_user_name(id, name, charsmax(name));
	get_user_authid(id, authid, charsmax(authid));
	formatex(out, maxLength, "^"%s<%d><%s><>^"", name, get_user_userid(id), authid);
}

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.