Skip to content
View in the app

A better way to browse. Learn more.

Learn more

pair-balance/addons/amxmodx/scripting/olds_pairbalance.sma

olds_pairbalance.sma
374 lines · 9.5 KB · Pawn (AMX Mod X)
Raw
/*
 * Pair Balance (olds.ro workshop card AMX-026)
 *
 * Offers a volunteer move instead of forcing one. When the teams start
 * olds_pairbal_rounds rounds in a row with a gap of at least
 * olds_pairbal_gap players, the player who joined most recently on the
 * bigger team gets a Yes/No menu for 10 seconds: "Teams are uneven (7 v 4).
 * Move to the Counter-Terrorists?"
 *
 *   Yes: the team counts are checked again. If the gap is still there, the
 *        player moves (at once during the freeze time, otherwise at the start
 *        of the next round, checked once more then), hears a short click, and
 *        everyone reads who helped. If the gap has closed meanwhile, nothing
 *        happens and the player is told so.
 *   No, or no answer in 10 s: nobody moves and the streak starts over.
 *
 * An open offer ends when a new round starts or the offered player changes
 * team. Only one offer is open at a time.
 *
 * Cvars (in configs/plugins/olds_pairbalance.cfg, written with the defaults on
 * the first start if the file is missing):
 *   olds_pairbal_rounds 3   Uneven round starts in a row before an offer (1 - 20).
 *   olds_pairbal_gap    2   Least difference in players that counts as uneven (2 - 16).
 *
 * How it works, as the card asks: the counts are taken at every
 * RG_CSGameRules_RestartRound, and the move uses rg_set_user_team. The click is
 * the stock buttons/button9.wav. Texts are in data/lang/olds_pairbalance.txt.
 *
 * Requires AMX Mod X 1.10 with ReAPI (ReGameDLL_CS). 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 <reapi>

#pragma semicolon 1

new const PLUGIN_NAME[] = "Pair Balance";
new const PLUGIN_VERSION[] = "1.0.0";
new const PLUGIN_AUTHOR[] = "alexit (olds.ro)";

new const ACCEPT_SOUND[] = "buttons/button9";

// Seconds the offer menu stays open.
const OFFER_TIME = 10;

enum
{
	ANSWER_YES,
	ANSWER_NO
};

new g_streakNeeded;  // olds_pairbal_rounds
new g_gapNeeded;     // olds_pairbal_gap

// Uneven round starts in a row.
new g_streak;

// Number of the current round, so an answer from an older round is ignored.
new g_round;

// The open offer: who, from which team, in which round, and its menu.
new g_offerPlayer;
new TeamName:g_offerFrom;
new g_offerRound;
new g_offerMenu = -1;

// A move accepted after the freeze time, done at the next round start.
new TeamName:g_moveTo[MAX_PLAYERS + 1];

// When each player joined, to find the most recent one.
new Float:g_joinTime[MAX_PLAYERS + 1];

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

	bind_pcvar_num(create_cvar("olds_pairbal_rounds", "3", FCVAR_NONE,
		"Uneven round starts in a row before a volunteer move is offered",
		true, 1.0, true, 20.0), g_streakNeeded);

	bind_pcvar_num(create_cvar("olds_pairbal_gap", "2", FCVAR_NONE,
		"Least difference in players between the teams that counts as uneven",
		true, 2.0, true, 16.0), g_gapNeeded);

	AutoExecConfig(true, "olds_pairbalance");

	// Pre: the accepted moves happen before the players respawn.
	RegisterHookChain(RG_CSGameRules_RestartRound, "fw_restart_round_pre", false);

	register_event("TeamInfo", "ev_team_info", "a");
}

public client_putinserver(id)
{
	g_joinTime[id] = get_gametime();
	g_moveTo[id] = TEAM_UNASSIGNED;
}

public client_disconnected(id)
{
	g_moveTo[id] = TEAM_UNASSIGNED;

	if (id == g_offerPlayer)
		close_offer();
}

/**
 * The offered player changed team on their own: the offer is void.
 */
public ev_team_info()
{
	new id = read_data(1);

	if (id == g_offerPlayer && id && get_member(id, m_iTeam) != g_offerFrom)
		close_offer();
}

public fw_restart_round_pre()
{
	g_round++;

	// An offer from the last round has expired.
	close_offer();

	do_pending_moves();

	new countT, countCT;
	count_teams(countT, countCT);

	if (abs(countT - countCT) < g_gapNeeded)
	{
		g_streak = 0;
		return HC_CONTINUE;
	}

	if (g_streak < g_streakNeeded)
	{
		g_streak++;
		return HC_CONTINUE;
	}

	make_offer(countT, countCT);
	return HC_CONTINUE;
}

make_offer(countT, countCT)
{
	new TeamName:bigger = countT > countCT ? TEAM_TERRORIST : TEAM_CT;
	new player = newest_player(bigger);

	if (!player)
		return;

	new TeamName:smaller = bigger == TEAM_TERRORIST ? TEAM_CT : TEAM_TERRORIST;
	new title[128], teamName[32], yes[32], no[32];

	team_name(player, smaller, teamName, charsmax(teamName));
	formatex(title, charsmax(title), "%L", player, "PAIRBAL_MENU_TITLE",
		max(countT, countCT), min(countT, countCT), teamName);
	formatex(yes, charsmax(yes), "%L", player, "PAIRBAL_YES");
	formatex(no, charsmax(no), "%L", player, "PAIRBAL_NO");

	g_offerMenu = menu_create(title, "menu_pairbal");
	menu_additem(g_offerMenu, yes);
	menu_additem(g_offerMenu, no);
	menu_setprop(g_offerMenu, MPROP_EXIT, MEXIT_NEVER);

	g_offerPlayer = player;
	g_offerFrom = bigger;
	g_offerRound = g_round;

	menu_display(player, g_offerMenu, 0, OFFER_TIME);
}

/**
 * The answer. item is negative when the menu timed out or was closed.
 */
public menu_pairbal(id, menu, item)
{
	new bool:current = menu == g_offerMenu && id == g_offerPlayer && g_offerRound == g_round;

	if (menu == g_offerMenu)
	{
		g_offerMenu = -1;
		g_offerPlayer = 0;
	}

	menu_destroy(menu);

	// An answer to an offer that has already been withdrawn changes nothing.
	if (!current || !is_user_connected(id))
		return PLUGIN_HANDLED;

	// No, or no answer: nobody moves and the counting starts over.
	if (item != ANSWER_YES)
	{
		g_streak = 0;
		return PLUGIN_HANDLED;
	}

	g_streak = 0;

	new TeamName:target = g_offerFrom == TEAM_TERRORIST ? TEAM_CT : TEAM_TERRORIST;

	if (get_member(id, m_iTeam) != g_offerFrom || !still_uneven(g_offerFrom))
	{
		client_print_color(id, print_team_default, "%L", id, "PAIRBAL_NOT_NEEDED");
		return PLUGIN_HANDLED;
	}

	new teamName[32];
	team_name(id, target, teamName, charsmax(teamName));
	client_cmd(id, "spk %s", ACCEPT_SOUND);

	// During the freeze time the move is fair at once; later in the round it
	// waits for the next round start.
	if (get_member_game(m_bFreezePeriod))
	{
		move_player(id, target);
		client_print_color(id, print_team_default, "%L", id, "PAIRBAL_MOVED_NOW", teamName);
	}
	else
	{
		g_moveTo[id] = target;
		client_print_color(id, print_team_default, "%L", id, "PAIRBAL_MOVE_NEXT", teamName);
	}

	return PLUGIN_HANDLED;
}

do_pending_moves()
{
	for (new id = 1; id <= MaxClients; id++)
	{
		if (g_moveTo[id] == TEAM_UNASSIGNED)
			continue;

		new TeamName:target = g_moveTo[id];
		g_moveTo[id] = TEAM_UNASSIGNED;

		if (!is_user_connected(id))
			continue;

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

		// Checked once more: the teams may have changed since the answer.
		if (from == target || (from != TEAM_TERRORIST && from != TEAM_CT) || !still_uneven(from))
		{
			client_print_color(id, print_team_default, "%L", id, "PAIRBAL_NOT_NEEDED");
			continue;
		}

		move_player(id, target);
	}
}

move_player(id, TeamName:target)
{
	rg_set_user_team(id, target);

	if (is_user_alive(id))
		rg_round_respawn(id);

	new name[MAX_NAME_LENGTH], teamName[32];
	get_user_name(id, name, charsmax(name));

	for (new player = 1; player <= MaxClients; player++)
	{
		if (!is_user_connected(player))
			continue;

		team_name(player, target, teamName, charsmax(teamName));
		client_print_color(player, id, "%L", player, "PAIRBAL_ANNOUNCE", name, teamName);
	}
}

/**
 * The team the player is on is still the bigger one by at least the gap.
 */
bool:still_uneven(TeamName:bigger)
{
	new countT, countCT;
	count_teams(countT, countCT);

	new difference = bigger == TEAM_TERRORIST ? countT - countCT : countCT - countT;
	return difference >= g_gapNeeded;
}

count_teams(&countT, &countCT)
{
	countT = 0;
	countCT = 0;

	for (new id = 1; id <= MaxClients; id++)
	{
		if (!is_user_connected(id) || is_user_hltv(id))
			continue;

		switch (get_member(id, m_iTeam))
		{
			case TEAM_TERRORIST: countT++;
			case TEAM_CT: countCT++;
		}
	}
}

newest_player(TeamName:team)
{
	new newest;
	new Float:newestTime = -1.0;

	for (new id = 1; id <= MaxClients; id++)
	{
		if (!is_user_connected(id) || is_user_hltv(id) || get_member(id, m_iTeam) != team)
			continue;

		if (g_moveTo[id] != TEAM_UNASSIGNED)
			continue;

		if (g_joinTime[id] > newestTime)
		{
			newestTime = g_joinTime[id];
			newest = id;
		}
	}

	return newest;
}

close_offer()
{
	if (g_offerMenu == -1)
		return;

	new player = g_offerPlayer;
	g_offerPlayer = 0;

	// Closing the menu calls menu_pairbal with a negative item; it sees the
	// offer is gone and only frees the menu.
	if (player && is_user_connected(player))
	{
		new oldMenu, newMenu;

		if (player_menu_info(player, oldMenu, newMenu) && newMenu == g_offerMenu)
			menu_cancel(player);
	}

	// If no handler call freed it, it is freed here.
	if (g_offerMenu != -1)
	{
		menu_destroy(g_offerMenu);
		g_offerMenu = -1;
	}
}

team_name(id, TeamName:team, name[], length)
{
	formatex(name, length, "%L", id, team == TEAM_TERRORIST ? "PAIRBAL_TEAM_T" : "PAIRBAL_TEAM_CT");
}

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.