/*
* Readable Message Hold (olds.ro workshop card IDEA-3626EAE5A4C5A12F)
*
* Players who read slowly can make the HUD messages of participating plugins
* stay on screen longer: 1x, 1.5x, 2x and so on up to olds_readtime_max. The
* choice is saved. A message sent to everyone still keeps the normal time
* for players on 1x.
*
* This is a library for other plugins. A plugin takes part by showing its
* HUD messages with olds_hud_show() instead of set_hudmessage() and
* show_hudmessage(); see scripting/include/olds_readtime.inc:
*
* #include <olds_readtime>
* olds_hud_show(0, 0, 200, 0, -1.0, 0.25, 4.0, "Round starts in 5 seconds");
*
* Chat commands (chat and team chat):
* /readtime opens a menu with the hold times
* /readtime <n> sets the hold time directly, e.g. /readtime 1.5
*
* Server command:
* olds_readtime_status prints each player's hold time and the last hold
* time a message got for them
*
* Cvars (in configs/plugins/olds_readtime.cfg, written with the defaults on
* the first start if the file is missing):
* olds_readtime_enable 1 1 = the choices are used, 0 = every message
* keeps its normal time and /readtime only says
* so (choices are kept).
* olds_readtime_max 2.0 Longest hold time a player can choose (1 - 5),
* a multiple of 0.1. A saved choice above it is
* used as this value.
*
* Choices are saved in the nvault file olds_readtime: by SteamID, or by name
* for bots and players without a unique SteamID (STEAM_ID_LAN).
*
* Requires AMX Mod X 1.10 with the nvault module. Written and tested on 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>
#pragma semicolon 1
new const PLUGIN_NAME[] = "Readable Message Hold";
new const PLUGIN_VERSION[] = "1.0.0";
new const PLUGIN_AUTHOR[] = "alexit (olds.ro)";
new const LIBRARY_NAME[] = "olds_readtime";
new const DICTIONARY[] = "olds_readtime.txt";
new const VAULT_NAME[] = "olds_readtime";
// Prefix of vault keys that are a player's name instead of a SteamID.
new const NAME_KEY_PREFIX[] = "name:";
// Hold times are kept in tenths (10 = 1.0x), so that comparisons and texts
// are exact.
const NORMAL_TENTHS = 10;
const MENU_STEP_TENTHS = 5;
new g_enable; // olds_readtime_enable
new Float:g_max; // olds_readtime_max
new g_vault = INVALID_HANDLE;
new g_tenths[MAX_PLAYERS + 1]; // the player's choice
new Float:g_lastHold[MAX_PLAYERS + 1]; // last hold time shown to the player
new g_saveKey[MAX_PLAYERS + 1][MAX_AUTHID_LENGTH + 8];
public plugin_natives()
{
register_library(LIBRARY_NAME);
register_native("olds_readtime_get", "native_readtime_get");
register_native("olds_hud_show", "native_hud_show");
}
public plugin_init()
{
register_plugin(PLUGIN_NAME, PLUGIN_VERSION, PLUGIN_AUTHOR);
register_dictionary(DICTIONARY);
bind_pcvar_num(create_cvar("olds_readtime_enable", "1", FCVAR_NONE,
"1 = players' hold times are used, 0 = every HUD message keeps its normal time",
true, 0.0, true, 1.0), g_enable);
bind_pcvar_float(create_cvar("olds_readtime_max", "2.0", FCVAR_NONE,
"Longest message hold time a player can choose (1 - 5)",
true, 1.0, true, 5.0), g_max);
AutoExecConfig(true, "olds_readtime");
register_clcmd("say", "cmd_say");
register_clcmd("say_team", "cmd_say");
register_srvcmd("olds_readtime_status", "cmd_status");
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);
}
public plugin_end()
{
if (g_vault != INVALID_HANDLE)
nvault_close(g_vault);
}
public client_connect(id)
{
g_tenths[id] = NORMAL_TENTHS;
g_lastHold[id] = 0.0;
g_saveKey[id][0] = EOS;
}
public client_authorized(id, const authid[])
{
if (has_unique_steamid(authid))
load_choice(id, authid);
}
public client_putinserver(id)
{
if (g_saveKey[id][0])
return;
new authid[MAX_AUTHID_LENGTH];
get_user_authid(id, authid, charsmax(authid));
// A Steam player not validated yet is loaded by client_authorized later.
if (equal(authid, "STEAM_ID_PENDING"))
return;
if (is_user_bot(id) || !has_unique_steamid(authid))
{
new key[MAX_AUTHID_LENGTH + 8];
formatex(key, charsmax(key), "%s%n", NAME_KEY_PREFIX, id);
load_choice(id, key);
}
}
/**
* True for a SteamID that belongs to one person. Bots, LAN ids and ids that
* are not known yet are shared by many players.
*/
bool:has_unique_steamid(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);
}
load_choice(id, const key[])
{
copy(g_saveKey[id], charsmax(g_saveKey[]), key);
if (g_vault == INVALID_HANDLE)
return;
new saved[8], timestamp;
if (nvault_lookup(g_vault, key, saved, charsmax(saved), timestamp))
g_tenths[id] = clamp(str_to_num(saved), NORMAL_TENTHS, 50);
}
save_choice(id)
{
if (!g_saveKey[id][0] || g_vault == INVALID_HANDLE)
return;
new value[8];
num_to_str(g_tenths[id], value, charsmax(value));
nvault_set(g_vault, g_saveKey[id], value);
}
max_tenths()
{
return floatround(g_max * 10.0);
}
/**
* The multiplier in use for the player, in tenths: the choice, never above
* olds_readtime_max, and 1.0x while the plugin is off.
*/
used_tenths(id)
{
if (!g_enable || !(1 <= id <= MaxClients))
return NORMAL_TENTHS;
return min(g_tenths[id], max_tenths());
}
/**
* Writes tenths as "1.5". With short = true whole numbers lose the ".0",
* as in "2" for the menu and the allowed range.
*/
format_tenths(tenths, out[], len, bool:short = false)
{
if (short && tenths % 10 == 0)
return formatex(out, len, "%d", tenths / 10);
return formatex(out, len, "%d.%d", tenths / 10, tenths % 10);
}
public cmd_say(id)
{
new text[64];
read_args(text, charsmax(text));
remove_quotes(text);
trim(text);
new command[16], value[16];
parse(text, command, charsmax(command), value, charsmax(value));
if (!equali(command, "/readtime"))
return PLUGIN_CONTINUE;
if (!g_enable)
{
client_print_color(id, print_team_default, "%L", id, "READTIME_OFF");
return PLUGIN_HANDLED;
}
if (!value[0])
{
show_menu_readtime(id);
return PLUGIN_HANDLED;
}
// "1.5" and "1.5x" both work.
new tenths = floatround(str_to_float(value) * 10.0);
if (tenths < NORMAL_TENTHS || tenths > max_tenths())
{
new maxText[8];
format_tenths(max_tenths(), maxText, charsmax(maxText), true);
client_print_color(id, print_team_default, "%L", id, "READTIME_ALLOWED", maxText);
return PLUGIN_HANDLED;
}
set_choice(id, tenths);
return PLUGIN_HANDLED;
}
set_choice(id, tenths)
{
g_tenths[id] = tenths;
save_choice(id);
new valueText[8];
format_tenths(tenths, valueText, charsmax(valueText));
client_print_color(id, print_team_default, "%L", id, "READTIME_SET", valueText);
}
show_menu_readtime(id)
{
new title[64];
formatex(title, charsmax(title), "%L", id, "READTIME_MENU_TITLE");
new menu = menu_create(title, "menu_readtime");
new last = max_tenths();
for (new tenths = NORMAL_TENTHS; tenths <= last; tenths += MENU_STEP_TENTHS)
add_menu_item(menu, id, tenths);
// A maximum between two steps, such as 1.8, is offered too.
if ((last - NORMAL_TENTHS) % MENU_STEP_TENTHS)
add_menu_item(menu, id, last);
menu_display(id, menu);
}
add_menu_item(menu, id, tenths)
{
new name[48], valueText[8], info[8];
format_tenths(tenths, valueText, charsmax(valueText), true);
if (tenths == g_tenths[id])
formatex(name, charsmax(name), "%sx \d%L", valueText, id, "READTIME_MENU_CURRENT");
else
formatex(name, charsmax(name), "%sx", valueText);
num_to_str(tenths, info, charsmax(info));
menu_additem(menu, name, info);
}
public menu_readtime(id, menu, item)
{
if (item < 0 || !is_user_connected(id))
{
menu_destroy(menu);
return PLUGIN_HANDLED;
}
new info[8], access, callback;
menu_item_getinfo(menu, item, access, info, charsmax(info), _, _, callback);
menu_destroy(menu);
if (!g_enable)
{
client_print_color(id, print_team_default, "%L", id, "READTIME_OFF");
return PLUGIN_HANDLED;
}
// The maximum may have been lowered while the menu was open.
set_choice(id, min(str_to_num(info), max_tenths()));
return PLUGIN_HANDLED;
}
public cmd_status()
{
new choiceText[8], lastText[8], shown;
for (new id = 1; id <= MaxClients; id++)
{
if (!is_user_connected(id))
continue;
format_tenths(g_tenths[id], choiceText, charsmax(choiceText));
if (g_lastHold[id] > 0.0)
format_tenths(floatround(g_lastHold[id] * 10.0), lastText, charsmax(lastText));
else
copy(lastText, charsmax(lastText), "-");
server_print("%n %sx last %s", id, choiceText, lastText);
shown++;
}
server_print("Readable Message Hold: %d player(s), %s, longest choice %d.%dx",
shown, g_enable ? "on" : "off", max_tenths() / 10, max_tenths() % 10);
return PLUGIN_HANDLED;
}
/**
* Float:olds_readtime_get(id)
*/
public Float:native_readtime_get(plugin, params)
{
return used_tenths(get_param(1)) / 10.0;
}
/**
* olds_hud_show(id, r, g, b, Float:x, Float:y, Float:holdtime, const text[])
*/
public native_hud_show(plugin, params)
{
new id = get_param(1);
new red = get_param(2), green = get_param(3), blue = get_param(4);
new Float:x = get_param_f(5), Float:y = get_param_f(6), Float:holdTime = get_param_f(7);
new text[512];
get_string(8, text, charsmax(text));
if (id)
{
if (!(1 <= id <= MaxClients) || !is_user_connected(id))
return 0;
show_for(id, red, green, blue, x, y, holdTime, text);
return 1;
}
new shown;
for (new player = 1; player <= MaxClients; player++)
{
if (is_user_connected(player))
{
show_for(player, red, green, blue, x, y, holdTime, text);
shown++;
}
}
return shown;
}
show_for(id, red, green, blue, Float:x, Float:y, Float:holdTime, const text[])
{
new Float:hold = holdTime * used_tenths(id) / 10.0;
g_lastHold[id] = hold;
set_hudmessage(red, green, blue, x, y, 0, 6.0, hold, 0.1, 0.2, -1);
show_hudmessage(id, "%s", text);
}