/*
* Timeleft In Hostname (olds.ro workshop card LIB-1996)
*
* Adds the time left on the map to the server name, so the server list
* shows it before anyone joins, for example "My Server | 15 min".
*
* The plain server name (the "base") is read from the hostname cvar when the
* map starts. If something else changes hostname later (an admin, another
* plugin), that new name becomes the base and the time is added to it
* again. When the map ends, and while mp_timelimit is 0, the plain name is
* put back without any time part. A change of hostname, mp_timelimit or one
* of the cvars below is shown at once instead of at the next interval.
*
* Cvars (saved with their descriptions in configs/plugins/olds_timeleft_name.cfg
* the first time the plugin starts):
* olds_tlhn_interval 30 Seconds between updates (5 - 300).
* olds_tlhn_fmt "%base% | %left% min" Name while more than a minute
* is left.
* olds_tlhn_lastmin 1 1 = in the last minute, update
* every 5 seconds and use
* olds_tlhn_fmt_last.
* olds_tlhn_fmt_last "%base% | %secs% sec" Name in the last minute.
* In both formats %base% is the plain server name, %left% the minutes left
* (rounded up, so 14:30 left reads 15) and %secs% the seconds left.
*
* A server name holds at most 63 bytes. When the result would be longer,
* the base name is shortened first so the time stays visible, and the cut
* never splits a UTF-8 character.
*
* The name is written with set_cvar_string("hostname", ...), as the card asks.
*
* Requires AMX Mod X 1.10; no game module and no ReAPI. Written and tested
* on AMX Mod X 1.10.0.5490 with ReHLDS 3.15.0.896 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>
#pragma semicolon 1
new const PLUGIN_NAME[] = "Timeleft In Hostname";
new const PLUGIN_VERSION[] = "1.0.0";
new const PLUGIN_AUTHOR[] = "alexit (olds.ro)";
const TASK_UPDATE = 19960;
// Longest server name the engine keeps, without the terminating zero.
const NAME_BYTES = 63;
// Buffer for a format with its placeholders filled in, before the cut.
const WORK_LENGTH = 256;
// Update period in the last minute, and where the last minute starts.
const Float:LAST_MINUTE_PERIOD = 5.0;
const LAST_MINUTE = 60;
// First update after the map start, once the configs have run.
const Float:FIRST_UPDATE = 1.0;
// Update after a watched cvar changes (a short delay, so the change itself
// has finished before hostname is written).
const Float:CHANGE_UPDATE = 0.1;
new const BASE_TAG[] = "%base%";
new g_interval; // olds_tlhn_interval
new g_lastMin; // olds_tlhn_lastmin
new g_pcvarFormat;
new g_pcvarFormatLast;
new g_pcvarHostname;
new g_pcvarTimeLimit;
// The server name without the time part.
new g_base[NAME_BYTES + 1];
// The name this plugin set last; anything else in hostname is a new base.
new g_lastSet[NAME_BYTES + 1];
// The updates have started (plugin_cfg ran).
new bool:g_started;
// This plugin is writing hostname right now.
new bool:g_writing;
public plugin_init()
{
register_plugin(PLUGIN_NAME, PLUGIN_VERSION, PLUGIN_AUTHOR);
bind_pcvar_num(create_cvar("olds_tlhn_interval", "30", FCVAR_NONE,
"Seconds between server name updates",
true, 5.0, true, 300.0), g_interval);
g_pcvarFormat = create_cvar("olds_tlhn_fmt", "%base% | %left% min", FCVAR_NONE,
"Server name while more than a minute is left (%base%, %left% minutes, %secs% seconds)");
bind_pcvar_num(create_cvar("olds_tlhn_lastmin", "1", FCVAR_NONE,
"1 = in the last minute update every 5 seconds with olds_tlhn_fmt_last",
true, 0.0, true, 1.0), g_lastMin);
g_pcvarFormatLast = create_cvar("olds_tlhn_fmt_last", "%base% | %secs% sec", FCVAR_NONE,
"Server name in the last minute (%base%, %left% minutes, %secs% seconds)");
AutoExecConfig(true, "olds_timeleft_name");
g_pcvarHostname = get_cvar_pointer("hostname");
g_pcvarTimeLimit = get_cvar_pointer("mp_timelimit");
if (!g_pcvarHostname || !g_pcvarTimeLimit)
set_fail_state("The hostname or mp_timelimit cvar does not exist.");
hook_cvar_change(g_pcvarHostname, "cvar_changed");
hook_cvar_change(g_pcvarTimeLimit, "cvar_changed");
hook_cvar_change(g_pcvarFormat, "cvar_changed");
hook_cvar_change(g_pcvarFormatLast, "cvar_changed");
hook_cvar_change(get_cvar_pointer("olds_tlhn_lastmin"), "cvar_changed");
hook_cvar_change(get_cvar_pointer("olds_tlhn_interval"), "cvar_changed");
}
public plugin_cfg()
{
// server.cfg and the plugin configs have set hostname by now.
get_pcvar_string(g_pcvarHostname, g_base, charsmax(g_base));
copy(g_lastSet, charsmax(g_lastSet), g_base);
g_started = true;
set_task(FIRST_UPDATE, "task_update", TASK_UPDATE);
}
public plugin_end()
{
remove_task(TASK_UPDATE);
// Leave the plain name for the next map, unless someone else has set
// a name since the last update.
new current[NAME_BYTES + 1];
get_pcvar_string(g_pcvarHostname, current, charsmax(current));
if (equal(current, g_lastSet))
{
g_writing = true;
set_cvar_string("hostname", g_base);
g_writing = false;
}
}
public cvar_changed(pcvar, const oldValue[], const newValue[])
{
// Only once the map is running, and not for this plugin's own writes.
if (!g_started || g_writing)
return;
// Writing hostname from inside its own change hook would change it again
// mid-change, so the update runs a moment later.
remove_task(TASK_UPDATE);
set_task(CHANGE_UPDATE, "task_update", TASK_UPDATE);
}
public task_update()
{
new secondsLeft = update_name();
// The last minute gets its own pace; the first update in it lands on
// its start instead of up to one interval later.
new Float:delay = float(g_interval);
if (g_lastMin && secondsLeft > 0)
{
if (secondsLeft <= LAST_MINUTE)
delay = LAST_MINUTE_PERIOD;
else if (secondsLeft - LAST_MINUTE < g_interval)
delay = float(secondsLeft - LAST_MINUTE);
}
// Exactly one update is ever scheduled.
remove_task(TASK_UPDATE);
set_task(delay, "task_update", TASK_UPDATE);
}
/**
* Writes the current name into hostname and returns the seconds left on the
* map, or 0 when there is no time limit.
*/
update_name()
{
new current[NAME_BYTES + 1];
get_pcvar_string(g_pcvarHostname, current, charsmax(current));
// Someone else has set a name since the last update: that is the base now.
if (!equal(current, g_lastSet))
copy(g_base, charsmax(g_base), current);
new name[NAME_BYTES + 1], secondsLeft;
if (get_pcvar_float(g_pcvarTimeLimit) <= 0.0)
{
// No time limit: the plain name, never "0 min".
copy(name, charsmax(name), g_base);
}
else
{
secondsLeft = max(get_timeleft(), 0);
new pattern[WORK_LENGTH];
if (g_lastMin && secondsLeft <= LAST_MINUTE)
get_pcvar_string(g_pcvarFormatLast, pattern, charsmax(pattern));
else
get_pcvar_string(g_pcvarFormat, pattern, charsmax(pattern));
build_name(pattern, secondsLeft, name, charsmax(name));
}
if (!equal(name, current))
{
g_writing = true;
set_cvar_string("hostname", name);
g_writing = false;
}
copy(g_lastSet, charsmax(g_lastSet), name);
return secondsLeft;
}
/**
* Fills in the placeholders and fits the result into maxBytes, shortening
* the base name first so the time part stays visible.
*/
build_name(const pattern[], secondsLeft, out[], maxBytes)
{
new work[WORK_LENGTH], number[12];
copy(work, charsmax(work), pattern);
num_to_str((secondsLeft + 59) / 60, number, charsmax(number));
replace_all(work, charsmax(work), "%left%", number);
num_to_str(secondsLeft, number, charsmax(number));
replace_all(work, charsmax(work), "%secs%", number);
new baseCount = count_tags(work);
if (baseCount > 0)
{
// Room for the base names once everything else is in place.
new room = maxBytes - (strlen(work) - baseCount * (sizeof(BASE_TAG) - 1));
new base[NAME_BYTES + 1];
copy(base, charsmax(base), g_base);
utf8_cut(base, max(room, 0) / baseCount);
replace_all(work, charsmax(work), BASE_TAG, base);
}
// The pattern itself may still be too long.
utf8_cut(work, maxBytes);
copy(out, maxBytes, work);
}
count_tags(const text[])
{
new count, position, found;
while ((found = contain(text[position], BASE_TAG)) != -1)
{
count++;
position += found + sizeof(BASE_TAG) - 1;
}
return count;
}
/**
* Shortens text to at most maxBytes bytes without leaving half of a UTF-8
* character at the end.
*/
utf8_cut(text[], maxBytes)
{
if (strlen(text) <= maxBytes)
return;
text[maxBytes] = EOS;
// Find where the last character starts: continuation bytes are 10xxxxxx.
new start = maxBytes - 1;
while (start > 0 && (text[start] & 0xC0) == 0x80)
start--;
new lead = text[start] & 0xFF;
new needed = 1;
if (lead >= 0xF0)
needed = 4;
else if (lead >= 0xE0)
needed = 3;
else if (lead >= 0xC0)
needed = 2;
if (maxBytes - start < needed)
text[start] = EOS;
}