Skip to content
View in the app

A better way to browse. Learn more.

Learn more

end-of-map-friendly-fire/addons/amxmodx/scripting/olds_endff.sma

olds_endff.sma
203 lines · 5.5 KB · Pawn (AMX Mod X)
Raw
/*
 * End-of-Map Friendly Fire (olds.ro workshop card LIB-1573)
 *
 * Turns friendly fire on shortly before the map ends, so the last minutes
 * become a free-for-all, and sets it back for the next map.
 *
 * A check runs every second: when the time left (get_timeleft) is
 * olds_eff_before seconds or less, mp_friendlyfire is set to 1 and the old
 * value is kept. At the map end (plugin_end) the old value is set back,
 * before the next map's first round. If the end moves away again (the map
 * is extended, mp_timelimit is set to 0, or olds_eff_before to 0), the old
 * value comes back at once. Without a time limit nothing happens.
 *
 * Cvars (saved with their descriptions in configs/plugins/olds_endff.cfg
 * the first time the plugin starts):
 *   olds_eff_before  120   seconds before the map end; 0 = never
 *   olds_eff_msg     1     1 = tell everyone in chat when friendly fire
 *                          turns on (and off again if the map is extended)
 *
 * Server command:
 *   olds_eff_status   prints the settings, the time left, mp_friendlyfire
 *                     and the value that will be set back
 *
 * Requires AMX Mod X 1.10 or newer. No extra modules and no ReAPI.
 *
 * 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>

#pragma semicolon 1

new const PLUGIN_NAME[] = "End-of-Map Friendly Fire";
new const PLUGIN_VERSION[] = "1.0.0";
new const PLUGIN_AUTHOR[] = "alexit (olds.ro)";

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

const TASK_CHECK = 15731;

new g_before;           // olds_eff_before
new g_message;          // olds_eff_msg

new g_pcvarTimelimit;

// True from the moment the end is near; g_changed when the plugin itself
// set mp_friendlyfire 1 (it was 0 before), with g_previous to set back.
new bool:g_switched;
new bool:g_changed;
new g_previous;

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

	bind_pcvar_num(create_cvar("olds_eff_before", "120", FCVAR_NONE,
		"Seconds before the map ends when friendly fire turns on; 0 = never",
		true, 0.0, true, 3600.0), g_before);

	bind_pcvar_num(create_cvar("olds_eff_msg", "1", FCVAR_NONE,
		"1 = tell everyone in chat when friendly fire turns on",
		true, 0.0, true, 1.0), g_message);

	AutoExecConfig(true, "olds_endff");

	register_srvcmd("olds_eff_status", "cmd_status");

	g_pcvarTimelimit = get_cvar_pointer("mp_timelimit");

	set_task(1.0, "task_check", TASK_CHECK, _, _, "b");
}

/**
 * Before the map changes: the old value back, so the next map starts with
 * the server's own setting.
 */
public plugin_end()
{
	if (g_switched)
		restore("the map ends");
}

/**
 * Every second.
 */
public task_check()
{
	new left = get_timeleft();
	new bool:limited = (get_pcvar_float(g_pcvarTimelimit) > 0.0);

	if (!g_switched)
	{
		if (g_before > 0 && limited && left > 0 && left <= g_before)
			switch_on(left);

		return;
	}

	if (g_before <= 0 || !limited)
	{
		restore("the end-of-map switch was turned off");
		announce_off();
	}
	else if (left > g_before)
	{
		restore("the map was extended");
		announce_off();
	}
}

switch_on(left)
{
	g_switched = true;
	g_previous = get_cvar_num("mp_friendlyfire");

	if (g_previous == 1)
	{
		g_changed = false;
		log_amx("%d seconds left: mp_friendlyfire is already 1, nothing to change.", left);
		return;
	}

	g_changed = true;
	set_cvar_num("mp_friendlyfire", 1);

	log_amx("%d seconds left: mp_friendlyfire set to 1 (was %d) until the map ends.", left, g_previous);

	if (g_message)
	{
		new clock[16];
		formatex(clock, charsmax(clock), "%d:%02d", left / 60, left % 60);
		client_print_color(0, print_team_red, "^3%L", LANG_PLAYER, "OLDS_EFF_ON", clock);
	}
}

/**
 * Sets the old value back, unless someone already changed mp_friendlyfire
 * away from 1 by hand.
 */
restore(const reason[])
{
	if (g_changed)
	{
		if (get_cvar_num("mp_friendlyfire") == 1)
		{
			set_cvar_num("mp_friendlyfire", g_previous);
			log_amx("mp_friendlyfire set back to %d: %s.", g_previous, reason);
		}
		else
		{
			log_amx("mp_friendlyfire was changed by hand; left at %d (%s).",
				get_cvar_num("mp_friendlyfire"), reason);
		}
	}

	g_switched = false;
	g_changed = false;
}

announce_off()
{
	if (g_message && get_cvar_num("mp_friendlyfire") != 1)
		client_print_color(0, print_team_default, "^1%L", LANG_PLAYER, "OLDS_EFF_OFF");
}

/**
 * olds_eff_status
 */
public cmd_status()
{
	server_print("[olds_endff] olds_eff_before %d, olds_eff_msg %d, mp_timelimit %.1f, time left %d s",
		g_before, g_message, get_pcvar_float(g_pcvarTimelimit), get_timeleft());

	if (g_changed)
	{
		server_print("[olds_endff] mp_friendlyfire %d, set by this plugin; %d is set back at the map end",
			get_cvar_num("mp_friendlyfire"), g_previous);
	}
	else if (g_switched)
	{
		server_print("[olds_endff] mp_friendlyfire %d, the end is near but it was already on",
			get_cvar_num("mp_friendlyfire"));
	}
	else
	{
		server_print("[olds_endff] mp_friendlyfire %d, not changed by this plugin",
			get_cvar_num("mp_friendlyfire"));
	}

	return PLUGIN_HANDLED;
}

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.