Skip to content
View in the app

A better way to browse. Learn more.

Learn more

spawn-protection/addons/amxmodx/scripting/olds_spawnprotect.sma

olds_spawnprotect.sma
360 lines · 10.1 KB · Pawn (AMX Mod X)
Raw
/*
 * Spawn Protection (olds.ro workshop card LIB-1960)
 *
 * On the maps named in a list, a player who spawns takes no damage for a few
 * seconds and glows in a team colour shell (red for the Terrorists, blue for
 * the Counter-Terrorists) so everyone can see it. The protection ends early
 * the moment the player attacks: pressing fire (+attack), a shot, a grenade,
 * and also a knife attack unless olds_spawnp_knife is 0. It also ends when
 * the player dies.
 *
 * When a player spawns during the freeze time, the seconds are counted from
 * the end of the freeze time, because nobody can be shot before that.
 * Every spawn starts a fresh protection, so a player who changes team gets
 * the full time again on the next spawn.
 *
 * Cvars (saved with their descriptions in configs/plugins/olds_spawnprotect.cfg
 * the first time the plugin starts):
 *   olds_spawnp_time  2.0             Seconds of protection (0.1 - 30).
 *   olds_spawnp_maps  "aim_,awp_,fy_" Comma separated map name beginnings
 *                                     where the protection is used. Empty =
 *                                     every map. Checked at every spawn.
 *   olds_spawnp_glow  1               1 = show the glow shell while protected.
 *   olds_spawnp_knife 1               1 = a knife attack ends the protection
 *                                     too, 0 = only shots and grenades do.
 *   olds_spawnp_falls 0               1 = fall damage is blocked as well,
 *                                     0 = falling still hurts.
 *
 * How it works, as the card asks: RG_CBasePlayer_Spawn (post) starts the
 * protection, RG_CBasePlayer_TakeDamage (pre) blocks the damage, and both
 * Ham_Weapon_PrimaryAttack and +attack seen in client_PreThink end it.
 *
 * Requires AMX Mod X 1.10 with ReAPI (ReGameDLL_CS) and the hamsandwich and
 * engine 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 <engine>
#include <hamsandwich>
#include <reapi>

#pragma semicolon 1

new const PLUGIN_NAME[] = "Spawn Protection";
new const PLUGIN_VERSION[] = "1.1.0";
new const PLUGIN_AUTHOR[] = "alexit (olds.ro)";

// Task ids are TASK_END + player index.
const TASK_END = 19600;

// Thickness of the glow shell.
const Float:GLOW_AMOUNT = 25.0;

// Longest map list read from the cvar.
const MAPS_LENGTH = 128;

// Glow colours: Terrorists red, Counter-Terrorists blue.
new const Float:GLOW_T[3] = { 255.0, 40.0, 40.0 };
new const Float:GLOW_CT[3] = { 40.0, 90.0, 255.0 };

new Float:g_time;       // olds_spawnp_time
new g_glow;             // olds_spawnp_glow
new g_knife;            // olds_spawnp_knife
new g_falls;            // olds_spawnp_falls

new g_pcvarMaps;

new g_mapName[32];

new bool:g_protected[MAX_PLAYERS + 1];

// Spawned during the freeze time: the timer starts when the freeze ends.
new bool:g_waitingFreeze[MAX_PLAYERS + 1];

new bool:g_glowing[MAX_PLAYERS + 1];

// Rendering the player had before the glow, put back when it ends.
new g_oldFx[MAX_PLAYERS + 1];
new g_oldMode[MAX_PLAYERS + 1];
new Float:g_oldAmount[MAX_PLAYERS + 1];
new Float:g_oldColor[MAX_PLAYERS + 1][3];

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

	bind_pcvar_float(create_cvar("olds_spawnp_time", "2.0", FCVAR_NONE,
		"Seconds of spawn protection, counted from the end of the freeze time",
		true, 0.1, true, 30.0), g_time);

	g_pcvarMaps = create_cvar("olds_spawnp_maps", "aim_,awp_,fy_", FCVAR_NONE,
		"Comma separated map name beginnings that use spawn protection (empty = every map)");

	bind_pcvar_num(create_cvar("olds_spawnp_glow", "1", FCVAR_NONE,
		"1 = protected players glow in their team colour",
		true, 0.0, true, 1.0), g_glow);

	bind_pcvar_num(create_cvar("olds_spawnp_knife", "1", FCVAR_NONE,
		"1 = a knife attack ends the protection too, 0 = only shots and grenades",
		true, 0.0, true, 1.0), g_knife);

	bind_pcvar_num(create_cvar("olds_spawnp_falls", "0", FCVAR_NONE,
		"1 = fall damage is blocked while protected, 0 = falling still hurts",
		true, 0.0, true, 1.0), g_falls);

	AutoExecConfig(true, "olds_spawnprotect");

	get_mapname(g_mapName, charsmax(g_mapName));

	RegisterHookChain(RG_CBasePlayer_Spawn, "fw_player_spawn_post", true);
	RegisterHookChain(RG_CBasePlayer_TakeDamage, "fw_player_take_damage", false);
	RegisterHookChain(RG_CBasePlayer_Killed, "fw_player_killed_post", true);
	RegisterHookChain(RG_CSGameRules_OnRoundFreezeEnd, "fw_freeze_end_post", true);

	// Every weapon that can hurt someone ends the protection when it is used.
	// The bomb is left out: planting it is not an attack.
	new weaponName[32];

	for (new weapon = CSW_P228; weapon <= CSW_P90; weapon++)
	{
		if (weapon == CSW_C4 || !get_weaponname(weapon, weaponName, charsmax(weaponName)))
			continue;

		RegisterHam(Ham_Weapon_PrimaryAttack, weaponName, "fw_weapon_attack");
	}

	// The knife's second button is a stab, also an attack.
	RegisterHam(Ham_Weapon_SecondaryAttack, "weapon_knife", "fw_weapon_attack");
}

public client_disconnected(id)
{
	remove_task(TASK_END + id);
	g_protected[id] = false;
	g_waitingFreeze[id] = false;
	g_glowing[id] = false;
}

public fw_player_spawn_post(const id)
{
	// A new spawn always starts over, whatever was left of an earlier one.
	end_protection(id);

	if (!is_user_alive(id) || !map_is_listed())
		return HC_CONTINUE;

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

	if (team != TEAM_TERRORIST && team != TEAM_CT)
		return HC_CONTINUE;

	g_protected[id] = true;

	if (g_glow)
		start_glow(id, team == TEAM_TERRORIST ? GLOW_T : GLOW_CT);

	// Nobody can shoot before the freeze time is over, so the seconds start
	// counting from then.
	if (get_member_game(m_bFreezePeriod))
		g_waitingFreeze[id] = true;
	else
		set_task(g_time, "task_end_protection", TASK_END + id);

	return HC_CONTINUE;
}

public fw_freeze_end_post()
{
	for (new id = 1; id <= MaxClients; id++)
	{
		if (g_protected[id] && g_waitingFreeze[id])
		{
			g_waitingFreeze[id] = false;
			set_task(g_time, "task_end_protection", TASK_END + id);
		}
	}
}

public fw_player_take_damage(const victim, inflictor, attacker, Float:damage, damageBits)
{
	if (!g_protected[victim])
		return HC_CONTINUE;

	if ((damageBits & DMG_FALL) && !g_falls)
		return HC_CONTINUE;

	// TakeDamage returns whether damage was done.
	SetHookChainReturn(ATYPE_INTEGER, 0);
	return HC_SUPERCEDE;
}

public fw_player_killed_post(const victim, attacker, gib)
{
	end_protection(victim);
	return HC_CONTINUE;
}

/**
 * The weapon really attacks (also for bots, and when a plugin makes it fire).
 */
public fw_weapon_attack(weapon)
{
	new owner = get_member(weapon, m_pPlayer);

	if (!(1 <= owner <= MaxClients) || !g_protected[owner])
		return HAM_IGNORED;

	if (!g_knife && WeaponIdType:get_member(weapon, m_iId) == WEAPON_KNIFE)
		return HAM_IGNORED;

	end_protection(owner);
	return HAM_IGNORED;
}

/**
 * The player presses fire, even if the weapon cannot shoot at that moment
 * (empty, reloading). Only checked while the player is protected.
 */
public client_PreThink(id)
{
	if (!g_protected[id])
		return;

	new buttons = get_entvar(id, var_button);

	if (!(buttons & (IN_ATTACK | IN_ATTACK2)))
		return;

	// Nobody can attack during the freeze time.
	if (get_member_game(m_bFreezePeriod))
		return;

	new item = get_member(id, m_pActiveItem);

	if (is_nullent(item))
		return;

	new WeaponIdType:weapon = get_member(item, m_iId);

	// Planting the bomb is not an attack.
	if (weapon == WEAPON_C4)
		return;

	if (weapon == WEAPON_KNIFE)
	{
		// Both knife buttons attack.
		if (g_knife)
			end_protection(id);

		return;
	}

	// For guns and grenades only the first button attacks; the second one
	// zooms, adds a silencer or changes the fire mode.
	if (buttons & IN_ATTACK)
		end_protection(id);
}

public task_end_protection(taskId)
{
	end_protection(taskId - TASK_END);
}

end_protection(id)
{
	remove_task(TASK_END + id);
	g_protected[id] = false;
	g_waitingFreeze[id] = false;

	if (g_glowing[id])
		stop_glow(id);
}

start_glow(id, const Float:color[3])
{
	g_oldFx[id] = get_entvar(id, var_renderfx);
	g_oldMode[id] = get_entvar(id, var_rendermode);
	g_oldAmount[id] = Float:get_entvar(id, var_renderamt);
	get_entvar(id, var_rendercolor, g_oldColor[id]);

	set_entvar(id, var_renderfx, kRenderFxGlowShell);
	set_entvar(id, var_rendercolor, color);
	set_entvar(id, var_rendermode, kRenderNormal);
	set_entvar(id, var_renderamt, GLOW_AMOUNT);

	g_glowing[id] = true;
}

stop_glow(id)
{
	g_glowing[id] = false;

	if (!is_user_connected(id))
		return;

	// Another plugin may have changed the player's look in the meantime;
	// that look is left alone.
	if (get_entvar(id, var_renderfx) != kRenderFxGlowShell)
		return;

	set_entvar(id, var_renderfx, g_oldFx[id]);
	set_entvar(id, var_rendercolor, g_oldColor[id]);
	set_entvar(id, var_rendermode, g_oldMode[id]);
	set_entvar(id, var_renderamt, g_oldAmount[id]);
}

/**
 * Tells whether the current map starts with one of the names in
 * olds_spawnp_maps. An empty list means every map.
 */
bool:map_is_listed()
{
	new list[MAPS_LENGTH], prefix[32];
	get_pcvar_string(g_pcvarMaps, list, charsmax(list));
	trim(list);

	if (!list[0])
		return true;

	new position, length;

	while (position < sizeof(list) && list[position])
	{
		// One name per comma; the list itself is not changed.
		length = 0;

		while (list[position] && list[position] != ',')
		{
			if (length < charsmax(prefix))
				prefix[length++] = list[position];

			position++;
		}

		prefix[length] = EOS;

		if (list[position] == ',')
			position++;

		trim(prefix);

		if (prefix[0] && equali(g_mapName, prefix, strlen(prefix)))
			return true;
	}

	return false;
}

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.