/*
* Relic Custody (olds.ro workshop card AMX-095)
*
* Teamplay: records who carried the single team artifact during the round and, when the
* round ends, names the longest uninterrupted hold in chat. /relic shows the round's
* timeline after the round has ended; during the round it is locked, so it cannot
* be used to find the carrier. Owner positions are never recorded or printed.
* Relic timeline: 1. Alpha 0:05-0:45 (40 s) 2. Bravo 0:45-1:10 (25 s) ...
*
* say /relic the last round's timeline (after the round has ended)
*
* Adapter: the artifact add-on, or a small bridge, confirms what happens to the
* artifact through four natives (library "olds_relic", include/olds_relic.inc):
* olds_relic_granted(id) id now carries it
* olds_relic_dropped(id) the carrier dropped it
* olds_relic_transferred(from, to) handed straight from one player to another
* olds_relic_reset() returned to base / removed
* A hold also ends when the carrier leaves the server and when the round ends. A drop
* followed by a pick-up of the same player within 1 second stays one hold. At most 16
* holds per round are listed; later ones still count for the longest hold.
*
* Cvar: olds_relic_minsec 3 (0-600): shorter holds are left out of the table and the
* end line.
*
* Copyright (C) 2026 daemon (olds.ro)
* SPDX-License-Identifier: GPL-3.0-or-later
*/
#include <amxmodx>
#define PLUGIN "Relic Custody"
#define VERSION "1.0.0"
#define AUTHOR "olds.ro"
const MAX_HOLDS = 16;
const Float:REGRAB_WINDOW = 1.0;
new g_holdAuth[MAX_HOLDS][MAX_AUTHID_LENGTH];
new g_holdName[MAX_HOLDS][MAX_NAME_LENGTH];
new Float:g_holdStart[MAX_HOLDS];
new Float:g_holdEnd[MAX_HOLDS];
new g_holdCount;
new g_holdsUnlisted; // holds past MAX_HOLDS this round
new g_owner; // 0 = nobody
new g_ownerHold = -1; // record index of the open hold, -1 = not listed
new Float:g_ownerSince;
new g_dropOwner; // who dropped it last (for the 1 s re-grab)
new g_dropHold = -1;
new Float:g_dropSince;
new Float:g_dropTime;
new g_bestSeconds;
new g_bestName[MAX_NAME_LENGTH];
new Float:g_roundStart;
new bool:g_roundOver;
new bool:g_haveTable;
new Float:g_nextUse[MAX_PLAYERS + 1];
new g_minSec;
public plugin_natives()
{
register_library("olds_relic");
register_native("olds_relic_granted", "native_granted");
register_native("olds_relic_dropped", "native_dropped");
register_native("olds_relic_transferred", "native_transferred");
register_native("olds_relic_reset", "native_reset");
}
public plugin_init()
{
register_plugin(PLUGIN, VERSION, AUTHOR);
register_dictionary("olds_relic.txt");
bind_pcvar_num(create_cvar("olds_relic_minsec", "3", FCVAR_NONE,
"Shortest hold in seconds that is listed (0-600).", true, 0.0, true, 600.0), g_minSec);
AutoExecConfig(true, "olds_relic");
register_clcmd("say /relic", "cmd_relic");
register_clcmd("say_team /relic", "cmd_relic");
register_event("HLTV", "on_new_round", "a", "1=0", "2=0");
register_logevent("on_round_end", 2, "1=Round_End");
register_logevent("on_round_end", 2, "1=Game_Commencing");
g_roundStart = get_gametime();
}
/* ---------- natives for the artifact add-on ---------- */
bool:valid_player(id)
{
return bool:(1 <= id <= MaxClients && is_user_connected(id));
}
public native_granted(plugin, params)
{
new id = get_param(1);
if (!valid_player(id))
{
log_error(AMX_ERR_NATIVE, "olds_relic_granted: invalid player %d", id);
return 0;
}
take(id);
return 1;
}
public native_dropped(plugin, params)
{
new id = get_param(1);
if (!id || id != g_owner)
return 0;
new hold = g_ownerHold;
new Float:since = g_ownerSince;
close_hold();
g_dropOwner = id;
g_dropHold = hold;
g_dropSince = since;
g_dropTime = get_gametime();
return 1;
}
public native_transferred(plugin, params)
{
new to = get_param(2);
if (!valid_player(to))
{
log_error(AMX_ERR_NATIVE, "olds_relic_transferred: invalid player %d", to);
return 0;
}
if (to == g_owner)
return 1;
close_hold();
forget_drop();
take(to);
return 1;
}
public native_reset(plugin, params)
{
close_hold();
forget_drop();
return 1;
}
/* ---------- holds ---------- */
take(id)
{
if (id == g_owner)
return;
close_hold();
new Float:now = get_gametime();
if (g_roundOver)
{
// Between round end and the next round: nothing is timed.
forget_drop();
return;
}
if (id == g_dropOwner && now - g_dropTime <= REGRAB_WINDOW)
{
// Picked up again at once: the same hold goes on.
g_owner = id;
g_ownerHold = g_dropHold;
g_ownerSince = g_dropSince;
if (g_ownerHold >= 0)
g_holdEnd[g_ownerHold] = 0.0;
else
g_holdsUnlisted--; // it was counted when it closed
forget_drop();
return;
}
forget_drop();
g_owner = id;
g_ownerSince = now;
if (g_holdCount < MAX_HOLDS)
{
g_ownerHold = g_holdCount++;
get_user_authid(id, g_holdAuth[g_ownerHold], charsmax(g_holdAuth[]));
get_user_name(id, g_holdName[g_ownerHold], charsmax(g_holdName[]));
g_holdStart[g_ownerHold] = now;
g_holdEnd[g_ownerHold] = 0.0;
}
else
g_ownerHold = -1;
}
close_hold()
{
if (!g_owner)
return;
new owner = g_owner;
new Float:now = get_gametime();
g_owner = 0;
if (g_ownerHold >= 0)
g_holdEnd[g_ownerHold] = now;
else
g_holdsUnlisted++;
new seconds = floatround(now - g_ownerSince, floatround_floor);
if (seconds >= g_minSec && seconds > g_bestSeconds)
{
g_bestSeconds = seconds;
if (g_ownerHold >= 0)
copy(g_bestName, charsmax(g_bestName), g_holdName[g_ownerHold]);
else if (is_user_connected(owner))
get_user_name(owner, g_bestName, charsmax(g_bestName));
}
g_ownerHold = -1;
}
forget_drop()
{
g_dropOwner = 0;
g_dropHold = -1;
}
/* ---------- rounds ---------- */
public on_new_round()
{
close_hold();
forget_drop();
g_holdCount = 0;
g_holdsUnlisted = 0;
g_bestSeconds = 0;
g_bestName[0] = EOS;
g_roundStart = get_gametime();
g_roundOver = false;
g_haveTable = false;
}
public on_round_end()
{
if (g_roundOver)
return;
close_hold();
forget_drop();
g_roundOver = true;
g_haveTable = true;
for (new p = 1; p <= MaxClients; p++)
{
if (!is_user_connected(p) || is_user_bot(p))
continue;
if (g_bestSeconds)
client_print_color(p, print_team_default, "^4[Relic]^1 %L", p, "RELIC_LONGEST",
g_bestName, g_bestSeconds);
else
client_print_color(p, print_team_default, "^4[Relic]^1 %L", p, "RELIC_NO_HOLD");
}
new seconds = 0;
if (g_bestSeconds)
seconds = g_bestSeconds;
log_amx("Round end: %d hold(s), longest %s %d s", g_holdCount + g_holdsUnlisted, g_bestName, seconds);
for (new i = 0; i < g_holdCount; i++)
log_amx(" hold %d: %s <%s> %.1f s", i + 1, g_holdName[i], g_holdAuth[i],
g_holdEnd[i] - g_holdStart[i]);
}
public client_disconnected(id)
{
if (id == g_owner)
close_hold();
if (id == g_dropOwner)
forget_drop();
g_nextUse[id] = 0.0;
}
/* ---------- output ---------- */
clock(Float:at, out[], len)
{
new s = floatround(at - g_roundStart, floatround_floor);
if (s < 0)
s = 0;
formatex(out, len, "%d:%02d", s / 60, s % 60);
}
public cmd_relic(id)
{
if (!is_user_connected(id))
return PLUGIN_HANDLED;
new Float:now = get_gametime();
if (now < g_nextUse[id])
return PLUGIN_HANDLED;
g_nextUse[id] = now + 1.0;
if (!g_roundOver || !g_haveTable)
{
client_print_color(id, print_team_default, "^4[Relic]^1 %L", id, "RELIC_LOCKED");
return PLUGIN_HANDLED;
}
new line[192], len, shown, seconds;
new from[8], to[8];
len = formatex(line, charsmax(line), "%L", id, "RELIC_TABLE");
for (new i = 0; i < g_holdCount; i++)
{
seconds = floatround(g_holdEnd[i] - g_holdStart[i], floatround_floor);
if (seconds < g_minSec)
continue;
clock(g_holdStart[i], from, charsmax(from));
clock(g_holdEnd[i], to, charsmax(to));
shown++;
new add_len = formatex(line[len], charsmax(line) - len, " %d. %s %s-%s (%d s)",
shown, g_holdName[i], from, to, seconds);
if (len + add_len >= charsmax(line) - 1)
{
// Full: send this line, start the next one.
line[len] = EOS;
client_print_color(id, print_team_default, "^4[Relic]^1 %s", line);
len = formatex(line, charsmax(line), "%d. %s %s-%s (%d s)", shown, g_holdName[i], from, to, seconds);
}
else
len += add_len;
}
if (!shown)
len += formatex(line[len], charsmax(line) - len, " %L", id, "RELIC_EMPTY");
client_print_color(id, print_team_default, "^4[Relic]^1 %s", line);
if (g_holdsUnlisted)
client_print_color(id, print_team_default, "^4[Relic]^1 %L", id, "RELIC_MORE", g_holdsUnlisted);
return PLUGIN_HANDLED;
}