Skip to content
View in the app

A better way to browse. Learn more.

Learn more

auto-restart-after-x-rounds/addons/amxmodx/scripting/olds_autorestart.sma

olds_autorestart.sma
195 lines · 6.1 KB · Pawn (AMX Mod X)
Raw
/*
 * Auto Restart After X Rounds (olds.ro workshop card LIB-1414)
 *
 * Ends a match by itself: when olds_ar_rounds rounds have been played, or a
 * team reaches olds_ar_score round wins, everyone is told and the game
 * restarts after olds_ar_delay seconds (sv_restart 1), so scores and money
 * start fresh for the next match. Admins can restart the match at once with
 * amx_restartmatch.
 *
 * The counts are the game's own (rounds played and team wins), so they start
 * from 0 on every map and after every restart.
 *
 * Console command (admins with the ADMIN_MAP flag, and the server console):
 *   amx_restartmatch   restarts the match now
 *
 * Cvars (in configs/plugins/olds_autorestart.cfg, written with the defaults on
 * the first start if the file is missing):
 *   olds_ar_rounds   30   Rounds per match (0 = no round limit, up to 500).
 *   olds_ar_score    16   Round wins that end the match (0 = no score limit, up to 500).
 *   olds_ar_delay    5    Seconds between the announcement and the restart (0 - 60).
 *   olds_ar_announce 1    1 = tell everyone in chat why and when the game restarts.
 *
 * How it works, as the card asks: rounds are checked in RG_RoundEnd (post)
 * with get_member_game(m_iTotalRoundsPlayed) and the team win counts, and the
 * restart is server_cmd("sv_restart 1"). Texts are in data/lang/olds_autorestart.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 <amxmisc>
#include <reapi>

#pragma semicolon 1

new const PLUGIN_NAME[] = "Auto Restart After X Rounds";
new const PLUGIN_VERSION[] = "1.0.0";
new const PLUGIN_AUTHOR[] = "alexit (olds.ro)";

const TASK_RESTART = 14140;

new g_roundLimit;    // olds_ar_rounds
new g_scoreLimit;    // olds_ar_score
new g_delay;         // olds_ar_delay
new g_announce;      // olds_ar_announce

// A restart is on its way; no second one is started meanwhile.
new bool:g_pending;

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

	bind_pcvar_num(create_cvar("olds_ar_rounds", "30", FCVAR_NONE,
		"Rounds per match before the game restarts (0 = no round limit)",
		true, 0.0, true, 500.0), g_roundLimit);

	bind_pcvar_num(create_cvar("olds_ar_score", "16", FCVAR_NONE,
		"Round wins of one team that end the match (0 = no score limit)",
		true, 0.0, true, 500.0), g_scoreLimit);

	bind_pcvar_num(create_cvar("olds_ar_delay", "5", FCVAR_NONE,
		"Seconds between the announcement and the restart",
		true, 0.0, true, 60.0), g_delay);

	bind_pcvar_num(create_cvar("olds_ar_announce", "1", FCVAR_NONE,
		"1 = tell everyone in chat why and when the game restarts",
		true, 0.0, true, 1.0), g_announce);

	AutoExecConfig(true, "olds_autorestart");

	register_concmd("amx_restartmatch", "cmd_restartmatch", ADMIN_MAP, "- restart the match now");

	RegisterHookChain(RG_RoundEnd, "fw_round_end_post", true);

	// A full restart (ours or anyone's) starts the next match.
	RegisterHookChain(RG_CSGameRules_RestartRound, "fw_restart_round_pre", false);
}

public fw_restart_round_pre()
{
	if (get_member_game(m_bCompleteReset))
	{
		remove_task(TASK_RESTART);
		g_pending = false;
	}

	return HC_CONTINUE;
}

public fw_round_end_post(WinStatus:status, ScenarioEventEndRound:event, Float:delay)
{
	// The game may refuse to end the round; only a real end counts.
	if (g_pending || !GetHookChainReturn(ATYPE_BOOL))
		return HC_CONTINUE;

	// The game adds the round that just ended to m_iTotalRoundsPlayed only
	// when the next round starts, so it is counted here.
	new rounds = get_member_game(m_iTotalRoundsPlayed) + 1;
	new winsT = get_member_game(m_iNumTerroristWins);
	new winsCT = get_member_game(m_iNumCTWins);

	if (g_scoreLimit > 0 && (winsT >= g_scoreLimit || winsCT >= g_scoreLimit))
	{
		new bool:terrorists = winsT >= g_scoreLimit;
		schedule_restart();

		if (g_announce)
		{
			new teamName[32];

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

				formatex(teamName, charsmax(teamName), "%L", id, terrorists ? "AR_TEAM_T" : "AR_TEAM_CT");
				client_print_color(id, print_team_default, "%L", id, "AR_SCORE_REACHED",
					teamName, terrorists ? winsT : winsCT, g_delay);
			}
		}

		log_amx("Match over: a team reached %d wins (T %d, CT %d) after %d rounds; restarting in %d s",
			g_scoreLimit, winsT, winsCT, rounds, g_delay);
		return HC_CONTINUE;
	}

	if (g_roundLimit > 0 && rounds >= g_roundLimit)
	{
		schedule_restart();

		if (g_announce)
			client_print_color(0, print_team_default, "%L", LANG_PLAYER, "AR_ROUNDS_REACHED", rounds, g_delay);

		log_amx("Match over: %d rounds played (T %d, CT %d); restarting in %d s", rounds, winsT, winsCT, g_delay);
	}

	return HC_CONTINUE;
}

public cmd_restartmatch(id, level, cid)
{
	if (!cmd_access(id, level, cid, 1))
		return PLUGIN_HANDLED;

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

	console_print(id, "%L", id, "AR_ADMIN_REPLY");

	if (g_announce)
		client_print_color(0, print_team_default, "%L", LANG_PLAYER, "AR_ADMIN_NOW");

	log_amx("^"%s<%d>^" restarted the match", name, id ? get_user_userid(id) : 0);

	remove_task(TASK_RESTART);
	do_restart();
	return PLUGIN_HANDLED;
}

schedule_restart()
{
	g_pending = true;

	if (g_delay > 0)
		set_task(float(g_delay), "task_restart", TASK_RESTART);
	else
		do_restart();
}

public task_restart()
{
	do_restart();
}

do_restart()
{
	g_pending = true;
	server_cmd("sv_restart 1");
}

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.