Skip to content
View in the app

A better way to browse. Learn more.

Learn more

blinded-kill-bonus/addons/amxmodx/scripting/olds_blindbonus.sma

olds_blindbonus.sma
304 lines · 9 KB · Pawn (AMX Mod X)
Raw
/*
 * Blinded Kill Bonus (olds.ro workshop card LIB-0104)
 *
 * A kill on an enemy who is still blinded by a flashbang pays the killer
 * extra money and, if set, extra frags. The killer gets a chat line.
 *
 * Commands: none.
 *
 * Cvars (in configs/plugins/olds_blindbonus.cfg, which ships with the
 * plugin; if the file is missing, the plugin writes it with these defaults
 * the first time it starts):
 *   olds_bb_money     300  Money for killing a blinded enemy.
 *   olds_bb_frag      0    Extra frags for killing a blinded enemy.
 *   olds_bb_minalpha  200  How white the victim's screen must still be
 *                          (0-255) for him to count as blinded. A flashbang
 *                          seen face on gives 255, one seen from the side
 *                          or from behind gives 200.
 *
 * Rules:
 *   - The victim counts as blinded while the white of his flash is at least
 *     olds_bb_minalpha: during the whole hold of the flash, then during the
 *     part of the fade-out that is still that white. A flash weaker than
 *     olds_bb_minalpha never counts.
 *   - Only an enemy kill counts (any player with mp_freeforall 1). Killing a
 *     blinded teammate with friendly fire gives nothing.
 *   - Any weapon counts: a grenade kill pays the player who threw it.
 *   - Money never goes above mp_maxmoney (16000 by default); money that is
 *     already higher is not lowered.
 *
 * How it works, as the card asks: the ScreenFade message that the game sends
 * to a flashed player (flags FFADE_IN, white, alpha 255 or 200) gives the
 * time his blindness ends, kept per player in an array, and
 * RG_CBasePlayer_Killed checks that the victim is still blind. The game sends
 * no ScreenFade to its own bots, so for a player who got none the same values
 * are taken from ReAPI's RG_PlayerBlind, which the game calls for every
 * flashed player.
 *
 * Requires AMX Mod X 1.10 with ReAPI (ReGameDLL_CS) and the cstrike and fun
 * modules. Written for 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 <cstrike>
#include <fun>
#include <reapi>

#pragma semicolon 1

new const PLUGIN_NAME[] = "Blinded Kill Bonus";
new const PLUGIN_VERSION[] = "1.0.0";
new const PLUGIN_AUTHOR[] = "alexit (olds.ro)";

// Money limit when the server has no mp_maxmoney cvar.
const DEFAULT_MAX_MONEY = 16000;

// ScreenFade flags (FFADE_IN is 0: none of these).
const FFADE_OUT = 0x0001;
const FFADE_MODULATE = 0x0002;
const FFADE_STAYOUT = 0x0004;

// ScreenFade times are in 1/4096 s.
const Float:FADE_UNITS = 4096.0;

new g_money;           // olds_bb_money
new g_frags;           // olds_bb_frag
new g_minAlpha;        // olds_bb_minalpha

new g_maxMoneyCvar;    // mp_maxmoney, 0 if the server has none
new g_freeForAllCvar;  // mp_freeforall, 0 if the server has none
new g_msgScoreInfo;

// Game time at which each player stops counting as blinded (0.0 = not blind).
new Float:g_blindUntil[MAX_PLAYERS + 1];

// The player got a ScreenFade message during the current RG_PlayerBlind.
new bool:g_fadeSeen[MAX_PLAYERS + 1];

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

	// The kill and blind hooks come from ReGameDLL_CS.
	if (!is_regamedll())
		set_fail_state("ReGameDLL_CS is not running. This plugin needs ReGameDLL_CS and ReAPI.");

	register_dictionary("olds_blindbonus.txt");

	bind_pcvar_num(create_cvar("olds_bb_money", "300", FCVAR_NONE,
		"Money for killing a flash-blinded enemy",
		true, 0.0), g_money);

	bind_pcvar_num(create_cvar("olds_bb_frag", "0", FCVAR_NONE,
		"Extra frags for killing a flash-blinded enemy",
		true, 0.0, true, 100.0), g_frags);

	bind_pcvar_num(create_cvar("olds_bb_minalpha", "200", FCVAR_NONE,
		"How white the victim's screen must still be (0-255) for him to count as blinded",
		true, 0.0, true, 255.0), g_minAlpha);

	AutoExecConfig(true, "olds_blindbonus");

	g_maxMoneyCvar = get_cvar_pointer("mp_maxmoney");
	g_freeForAllCvar = get_cvar_pointer("mp_freeforall");
	g_msgScoreInfo = get_user_msgid("ScoreInfo");

	register_message(get_user_msgid("ScreenFade"), "on_screen_fade");

	RegisterHookChain(RG_PlayerBlind, "on_player_blind_pre", false);
	RegisterHookChain(RG_PlayerBlind, "on_player_blind_post", true);
	RegisterHookChain(RG_CBasePlayer_Killed, "on_killed_post", true);
	RegisterHookChain(RG_CSGameRules_RestartRound, "on_restart_round_post", true);
}

public client_putinserver(id)
{
	g_blindUntil[id] = 0.0;
}

public client_disconnected(id)
{
	g_blindUntil[id] = 0.0;
}

/**
 * A new round: nobody is blinded any more.
 */
public on_restart_round_post()
{
	for (new id = 1; id <= MaxClients; id++)
		g_blindUntil[id] = 0.0;

	return HC_CONTINUE;
}

/**
 * ScreenFade: duration, hold time, flags, red, green, blue, alpha. The newest
 * fade replaces the one on the player's screen, so it decides: a flash-like
 * fade (FFADE_IN) gives a new blind end, any other fade ends the blindness.
 */
public on_screen_fade(msgId, msgDest, msgEntity)
{
	new Float:duration = float(get_msg_arg_int(1) & 0xFFFF) / FADE_UNITS;
	new Float:hold = float(get_msg_arg_int(2) & 0xFFFF) / FADE_UNITS;
	new flags = get_msg_arg_int(3);
	new alpha = get_msg_arg_int(7) & 0xFF;

	new Float:until;

	if (!(flags & (FFADE_OUT | FFADE_MODULATE | FFADE_STAYOUT)) && duration > 0.0)
		until = blind_end(duration, hold, alpha);

	switch (msgDest)
	{
		case MSG_ONE, MSG_ONE_UNRELIABLE:
		{
			if (msgEntity >= 1 && msgEntity <= MaxClients)
				set_blind(msgEntity, until);
		}
		case MSG_ALL, MSG_BROADCAST:
		{
			for (new id = 1; id <= MaxClients; id++)
			{
				if (is_user_connected(id))
					set_blind(id, until);
			}
		}
	}

	return PLUGIN_CONTINUE;
}

/**
 * A flashbang blinds this player. The game sends the ScreenFade from inside
 * this call, except to its own bots.
 */
public on_player_blind_pre(id, inflictor, attacker, Float:fadeTime, Float:fadeHold, alpha, Float:color[3])
{
	if (id >= 1 && id <= MaxClients)
		g_fadeSeen[id] = false;

	return HC_CONTINUE;
}

public on_player_blind_post(id, inflictor, attacker, Float:fadeTime, Float:fadeHold, alpha, Float:color[3])
{
	if (id < 1 || id > MaxClients)
		return HC_CONTINUE;

	// No ScreenFade went to this player (a bot): use the blind values.
	if (!g_fadeSeen[id] && fadeTime > 0.0)
		g_blindUntil[id] = blind_end(fadeTime, fadeHold, alpha);

	return HC_CONTINUE;
}

/**
 * Someone died: a kill on a blinded enemy pays the killer.
 */
public on_killed_post(victim, killer, gib)
{
	new Float:until = g_blindUntil[victim];
	g_blindUntil[victim] = 0.0;

	if (until <= get_gametime())
		return HC_CONTINUE;

	if (killer == victim || killer < 1 || killer > MaxClients || !is_user_connected(killer))
		return HC_CONTINUE;

	if (!is_enemy(killer, victim))
		return HC_CONTINUE;

	reward(killer);
	return HC_CONTINUE;
}

set_blind(id, Float:until)
{
	g_blindUntil[id] = until;
	g_fadeSeen[id] = true;
}

/**
 * The white of a flash stays at alpha for the hold time, then fades out
 * evenly to nothing over the duration. Returns the game time at which it
 * drops below olds_bb_minalpha, or 0.0 when it never reaches it.
 */
Float:blind_end(Float:duration, Float:hold, alpha)
{
	if (alpha < max(g_minAlpha, 1))
		return 0.0;

	return get_gametime() + hold + duration * (1.0 - float(g_minAlpha) / float(alpha));
}

bool:is_enemy(killer, victim)
{
	if (g_freeForAllCvar && get_pcvar_num(g_freeForAllCvar))
		return true;

	return get_member(killer, m_iTeam) != get_member(victim, m_iTeam);
}

reward(id)
{
	if (g_money <= 0 && g_frags <= 0)
		return;

	if (g_money > 0)
	{
		new current = cs_get_user_money(id);
		cs_set_user_money(id, max(current, min(current + g_money, max_money())));
	}

	if (g_frags > 0)
	{
		set_user_frags(id, get_user_frags(id) + g_frags);
		update_scoreboard(id);
	}

	if (g_money > 0 && g_frags > 0)
		client_print_color(id, print_team_default, "%L", id, "BB_BOTH", g_money, g_frags);
	else if (g_money > 0)
		client_print_color(id, print_team_default, "%L", id, "BB_MONEY", g_money);
	else
		client_print_color(id, print_team_default, "%L", id, "BB_FRAGS", g_frags);
}

/**
 * Sends the player's new score to every client, as the game does after a
 * kill.
 */
update_scoreboard(id)
{
	message_begin(MSG_BROADCAST, g_msgScoreInfo);
	write_byte(id);
	write_short(get_user_frags(id));
	write_short(get_member(id, m_iDeaths));
	write_short(0);
	write_short(get_member(id, m_iTeam));
	message_end();
}

max_money()
{
	if (g_maxMoneyCvar)
		return get_pcvar_num(g_maxMoneyCvar);

	return DEFAULT_MAX_MONEY;
}

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.