/*
* Flash Dark Mode (olds.ro workshop card IDEA-FA3831F0BCBA17F4)
*
* Players who are sensitive to bright flashes can switch flashbang blindness
* from a white screen to a black one. Only the colour changes: the blind
* time, the hold time and the strength (alpha) stay exactly as the game sent
* them, so the option gives comfort and no advantage.
*
* Commands:
* say /darkflash (also say_team) turns the option on or off for the
* player; saved by SteamID
* olds_darkflash_status (server console) the settings, then each player's
* choice, the last flash the game gave them and how
* many white fades were turned black for them
*
* Cvars (in configs/plugins/olds_darkflash.cfg, written with the defaults on
* the first start if the file is missing):
* olds_darkflash_enable 1 1 = on, 0 = off (fades are left alone)
* olds_darkflash_default 0 1 = the option is on for players who never
* chose, 0 = off
* olds_darkflash_announce 300 seconds between chat hints about /darkflash
* to the players who have it off, 0 = never
*
* How it works, as the card asks: register_message on ScreenFade. When the
* receiver has the option and the colour is the flashbang white
* (255 255 255), set_msg_arg_int rewrites the colour to 0 0 0; duration,
* hold time, flags and alpha are not touched. The game sends the fade to the
* flashed player and to each spectator watching them in first person, one
* message per receiver, so every receiver gets their own colour. As a second
* path, ReAPI's RG_PlayerBlind gives the exact fade time, hold time and alpha
* of each flash (shown by olds_darkflash_status).
*
* register_message sees the game's own fades and those other plugins send
* with emessage_begin, but not those sent with message_begin. ReAPI's
* RegisterMessage hooks the same message in the engine and does the same
* there, so a white fade from any plugin turns black too. A white fade sent
* to everyone at once is sent again to each player with their own colour.
*
* Requires ReHLDS, ReGameDLL_CS, ReAPI with RegisterMessage and AMX Mod X
* 1.10 with the nvault module. Written and tested on ReHLDS 3.15,
* ReGameDLL_CS 5.30.0.814, ReAPI 5.29.0.358 and AMX Mod X 1.10.0.5490.
*
* 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 <nvault>
#include <reapi>
#pragma semicolon 1
new const PLUGIN_NAME[] = "Flash Dark Mode";
new const PLUGIN_VERSION[] = "1.0.0";
new const PLUGIN_AUTHOR[] = "alexit (olds.ro)";
new const DICTIONARY[] = "olds_darkflash.txt";
new const VAULT_NAME[] = "olds_darkflash";
new const SAVED_ON[] = "1";
new const SAVED_OFF[] = "0";
const TASK_ANNOUNCE = 6470;
// ScreenFade: short duration, short hold time, short flags, byte r, g, b, a.
const FADE_ARGS = 7;
const ARG_DURATION = 1;
const ARG_HOLD = 2;
const ARG_FLAGS = 3;
const ARG_RED = 4;
const ARG_GREEN = 5;
const ARG_BLUE = 6;
const ARG_ALPHA = 7;
const NO_CHOICE = -1;
new g_enable; // olds_darkflash_enable
new g_default; // olds_darkflash_default
new g_announce; // olds_darkflash_announce
new g_vault = INVALID_HANDLE;
new g_choice[MAX_PLAYERS + 1]; // NO_CHOICE, 0 or 1
new g_saveKey[MAX_PLAYERS + 1][MAX_AUTHID_LENGTH];
// What RG_PlayerBlind reported for each player's last flash.
new g_flashes[MAX_PLAYERS + 1];
new Float:g_lastFadeTime[MAX_PLAYERS + 1];
new Float:g_lastHoldTime[MAX_PLAYERS + 1];
new g_lastAlpha[MAX_PLAYERS + 1];
// White fades turned black for each receiver.
new g_blackFades[MAX_PLAYERS + 1];
public plugin_init()
{
register_plugin(PLUGIN_NAME, PLUGIN_VERSION, PLUGIN_AUTHOR);
register_dictionary(DICTIONARY);
bind_pcvar_num(create_cvar("olds_darkflash_enable", "1", FCVAR_NONE,
"1 = players can turn flashbang white into black with /darkflash, 0 = off",
true, 0.0, true, 1.0), g_enable);
bind_pcvar_num(create_cvar("olds_darkflash_default", "0", FCVAR_NONE,
"1 = the option is on for players who never chose, 0 = off",
true, 0.0, true, 1.0), g_default);
new pcvar = create_cvar("olds_darkflash_announce", "300", FCVAR_NONE,
"Seconds between chat hints about /darkflash, 0 = never",
true, 0.0);
bind_pcvar_num(pcvar, g_announce);
hook_cvar_change(pcvar, "on_announce_changed");
AutoExecConfig(true, "olds_darkflash");
register_clcmd("say /darkflash", "cmd_darkflash");
register_clcmd("say_team /darkflash", "cmd_darkflash");
register_srvcmd("olds_darkflash_status", "cmd_status");
RegisterHookChain(RG_PlayerBlind, "on_player_blind", false);
g_vault = nvault_open(VAULT_NAME);
if (g_vault == INVALID_HANDLE)
log_amx("Could not open the nvault file %s; choices are kept only until players leave.", VAULT_NAME);
schedule_announce(g_announce);
new msgScreenFade = get_user_msgid("ScreenFade");
register_message(msgScreenFade, "msg_screen_fade");
// Last, so that everything above is already set up on an engine without
// ReHLDS's message hooks, where this native stops with an error.
RegisterMessage(msgScreenFade, "engine_screen_fade");
}
public plugin_end()
{
if (g_vault != INVALID_HANDLE)
nvault_close(g_vault);
}
/* -------------------------------------------------------------------------
* Players and their choice
* ---------------------------------------------------------------------- */
public client_connect(id)
{
g_choice[id] = NO_CHOICE;
g_saveKey[id][0] = EOS;
g_flashes[id] = 0;
g_lastFadeTime[id] = 0.0;
g_lastHoldTime[id] = 0.0;
g_lastAlpha[id] = 0;
g_blackFades[id] = 0;
}
public client_authorized(id, const authid[])
{
if (!can_save(authid))
return;
copy(g_saveKey[id], charsmax(g_saveKey[]), authid);
if (g_vault == INVALID_HANDLE)
return;
new saved[4], timestamp;
if (nvault_lookup(g_vault, authid, saved, charsmax(saved), timestamp))
g_choice[id] = equal(saved, SAVED_ON) ? 1 : 0;
}
public client_disconnected(id)
{
g_choice[id] = NO_CHOICE;
g_saveKey[id][0] = EOS;
}
/**
* Only a real SteamID belongs to one person. Bots, LAN ids and ids that are
* not known yet are shared by many players, so nothing is saved for them.
*/
bool:can_save(const authid[])
{
if (equal(authid, "STEAM_ID_LAN") || equal(authid, "STEAM_ID_PENDING") || equal(authid, "VALVE_ID_LAN"))
return false;
return equal(authid, "STEAM_", 6) || equal(authid, "VALVE_", 6);
}
/**
* True when the player wants black flashes: their own choice, else the
* olds_darkflash_default setting.
*/
bool:wants_dark(id)
{
if (!(1 <= id <= MaxClients) || !is_user_connected(id))
return false;
if (g_choice[id] == NO_CHOICE)
return g_default != 0;
return g_choice[id] != 0;
}
bool:anyone_wants_dark()
{
for (new id = 1; id <= MaxClients; id++)
{
if (wants_dark(id))
return true;
}
return false;
}
public cmd_darkflash(id)
{
if (!g_enable)
{
client_print_color(id, print_team_default, "%L", id, "DARKFLASH_DISABLED");
return PLUGIN_HANDLED;
}
new bool:dark = !wants_dark(id);
g_choice[id] = dark ? 1 : 0;
if (g_saveKey[id][0] && g_vault != INVALID_HANDLE)
nvault_set(g_vault, g_saveKey[id], dark ? SAVED_ON : SAVED_OFF);
client_print_color(id, print_team_default, "%L", id, dark ? "DARKFLASH_NOW_ON" : "DARKFLASH_NOW_OFF");
// The command itself is not shown in chat.
return PLUGIN_HANDLED;
}
/* -------------------------------------------------------------------------
* Flashes
* ---------------------------------------------------------------------- */
/**
* The game blinds a player with a flashbang. The fade it is about to send
* carries these values; they are kept for olds_darkflash_status.
*/
public on_player_blind(id, inflictor, attacker, Float:fadeTime, Float:fadeHold, alpha)
{
if (1 <= id <= MaxClients)
{
g_flashes[id]++;
g_lastFadeTime[id] = fadeTime;
g_lastHoldTime[id] = fadeHold;
g_lastAlpha[id] = alpha;
}
return HC_CONTINUE;
}
/**
* A ScreenFade is about to be sent (the game's own, or one a plugin sends
* with emessage_begin). A white fade to one player who wants black flashes
* becomes black. A white fade to everyone is sent again to each player with
* their own colour.
*/
public msg_screen_fade(msgId, msgDest, msgEntity)
{
if (!g_enable || get_msg_args() < FADE_ARGS)
return PLUGIN_CONTINUE;
if (get_msg_arg_int(ARG_RED) != 255 || get_msg_arg_int(ARG_GREEN) != 255 || get_msg_arg_int(ARG_BLUE) != 255)
return PLUGIN_CONTINUE;
switch (msgDest)
{
case MSG_ONE, MSG_ONE_UNRELIABLE:
{
if (wants_dark(msgEntity))
{
set_msg_arg_int(ARG_RED, ARG_BYTE, 0);
set_msg_arg_int(ARG_GREEN, ARG_BYTE, 0);
set_msg_arg_int(ARG_BLUE, ARG_BYTE, 0);
g_blackFades[msgEntity]++;
}
}
case MSG_ALL, MSG_BROADCAST:
{
if (!anyone_wants_dark())
return PLUGIN_CONTINUE;
send_to_each(msgId, msgDest == MSG_ALL ? MSG_ONE : MSG_ONE_UNRELIABLE,
get_msg_arg_int(ARG_DURATION), get_msg_arg_int(ARG_HOLD),
get_msg_arg_int(ARG_FLAGS), get_msg_arg_int(ARG_ALPHA));
return PLUGIN_HANDLED;
}
}
// Fades sent to a place (MSG_PVS, MSG_PAS) have no single receiver and
// are left alone; the game never sends ScreenFade that way.
return PLUGIN_CONTINUE;
}
/**
* The same in the engine, for the fades that msg_screen_fade never saw
* because a plugin sent them with message_begin. The game's own fades were
* already made black above, so they are no longer white here.
*/
public engine_screen_fade(msgId, msgDest, msgEntity)
{
if (!g_enable || GetMessageArgsNum() < FADE_ARGS)
return HC_CONTINUE;
if (GetMessageData(MsgArg, ARG_RED) != 255 || GetMessageData(MsgArg, ARG_GREEN) != 255 || GetMessageData(MsgArg, ARG_BLUE) != 255)
return HC_CONTINUE;
switch (msgDest)
{
case MSG_ONE, MSG_ONE_UNRELIABLE:
{
if (wants_dark(msgEntity))
{
SetMessageData(MsgArg, ARG_RED, 0);
SetMessageData(MsgArg, ARG_GREEN, 0);
SetMessageData(MsgArg, ARG_BLUE, 0);
g_blackFades[msgEntity]++;
}
}
case MSG_ALL, MSG_BROADCAST:
{
if (!anyone_wants_dark())
return HC_CONTINUE;
send_to_each(msgId, msgDest == MSG_ALL ? MSG_ONE : MSG_ONE_UNRELIABLE,
GetMessageData(MsgArg, ARG_DURATION), GetMessageData(MsgArg, ARG_HOLD),
GetMessageData(MsgArg, ARG_FLAGS), GetMessageData(MsgArg, ARG_ALPHA));
return HC_SUPERCEDE;
}
}
return HC_CONTINUE;
}
/**
* Sends a white fade that was meant for everyone to each player on their
* own, black for those who want it. The copies go to one player each, so
* the engine hook lets them through unchanged.
*/
send_to_each(msgId, dest, duration, hold, flags, alpha)
{
for (new id = 1; id <= MaxClients; id++)
{
if (!is_user_connected(id))
continue;
new colour = 255;
if (wants_dark(id))
{
colour = 0;
g_blackFades[id]++;
}
message_begin(dest, msgId, _, id);
write_short(duration);
write_short(hold);
write_short(flags);
write_byte(colour);
write_byte(colour);
write_byte(colour);
write_byte(alpha);
message_end();
}
}
/* -------------------------------------------------------------------------
* Chat hint
* ---------------------------------------------------------------------- */
public on_announce_changed(pcvar, const oldValue[], const newValue[])
{
schedule_announce(str_to_num(newValue));
}
schedule_announce(seconds)
{
remove_task(TASK_ANNOUNCE);
if (seconds > 0)
set_task(float(seconds), "task_announce", TASK_ANNOUNCE);
}
public task_announce()
{
if (g_enable)
{
for (new id = 1; id <= MaxClients; id++)
{
if (is_user_connected(id) && !is_user_bot(id) && !wants_dark(id))
client_print_color(id, print_team_default, "%L", id, "DARKFLASH_HINT");
}
}
schedule_announce(g_announce);
}
/* -------------------------------------------------------------------------
* Server console
* ---------------------------------------------------------------------- */
public cmd_status()
{
new hint[32];
if (g_announce > 0)
formatex(hint, charsmax(hint), "every %d s", g_announce);
else
copy(hint, charsmax(hint), "off");
server_print("Flash Dark Mode: %s, default %s, hint %s",
g_enable ? "enabled" : "disabled", g_default ? "on" : "off", hint);
for (new id = 1; id <= MaxClients; id++)
{
if (!is_user_connected(id))
continue;
new choice[16];
copy(choice, charsmax(choice), g_choice[id] == NO_CHOICE ? "default" : "chosen");
if (g_flashes[id])
{
server_print("%n: dark flash %s (%s), black fades %d, flashed %d, last flash fade %d.%d s, hold %d.%d s, alpha %d",
id, wants_dark(id) ? "on" : "off", choice, g_blackFades[id], g_flashes[id],
tenths(g_lastFadeTime[id]) / 10, tenths(g_lastFadeTime[id]) % 10,
tenths(g_lastHoldTime[id]) / 10, tenths(g_lastHoldTime[id]) % 10,
g_lastAlpha[id]);
}
else
{
server_print("%n: dark flash %s (%s), black fades %d, never flashed",
id, wants_dark(id) ? "on" : "off", choice, g_blackFades[id]);
}
}
return PLUGIN_HANDLED;
}
// A time in tenths of a second, rounded, for printing without float errors.
tenths(Float:seconds)
{
return max(0, floatround(seconds * 10.0));
}