/*
* Contact Ledger (olds.ro workshop card AMX-004)
*
* Traces every infection of a Biohazard round back to its carrier. After the
* round, say /chain prints the infection tree to the caller: each outbreak
* origin, then whoever they infected, then whoever those infected, in the
* order it happened.
*
* Needs: AMX Mod X 1.10, ReGameDLL_CS + ReAPI, and the Biohazard core
* (library "biohazardf"). Verified against biohazard.inc and biohazard.sma of
* Biohazard 2.00 Beta 3 (github.com/119call911/biohazard-amxx, commit e6415c0).
*
* How the core reports infections (event_infect(victim, attacker)):
* - At the outbreak the core infects the first zombie (or every pre-infected
* player) with attacker 0, then fires event_gamestart. Those players are
* the outbreak's origins.
* - A zombie's hit infects a survivor with attacker = that zombie: the zombie
* is the carrier.
* - An admin's infect command also uses attacker 0 mid-round: that player is
* another origin.
* A new outbreak (an origin infected before the game has started, or
* event_gamestart) clears the previous ledger, so the last round's ledger stays
* available until then.
*
* Each record keeps the victim's and the carrier's names and Steam ids as they
* were when it happened, so a player who has left stays in the tree. Players
* are told apart by user id; a player cured and infected again gets a new
* record, and later infections hang under the latest one. A carrier with no
* record of their own (a zombie this plugin did not see infected) heads their
* own branch, marked "(carrier)".
*
* Cvar: olds_ledger_maxrows (1-64, default 12) caps the lines /chain prints;
* the rest are summed up as "+N more".
* Commands: say /chain (after the round); olds_ledger_status (ADMIN_CFG)
* prints what /chain would show and every record with its Steam ids.
* The report plays the stock sound buttons/button9.wav to the caller.
*
* Copyright (C) 2026 daemon (olds.ro)
* SPDX-License-Identifier: GPL-3.0-or-later
*/
#include <amxmodx>
#include <amxmisc>
#include <reapi>
#define PLUGIN "Contact Ledger"
#define VERSION "1.0.0"
#define AUTHOR "olds.ro"
/*
* Biohazard API used here (biohazard.inc). Declared in this file so it
* compiles without the core's include; the library is still required at run
* time, and the plugin will not load without it.
*/
#pragma reqlib "biohazardf"
native bool:game_started()
native is_user_zombie(index)
const MAX_RECORDS = 128
const MAX_ROWS = 64
const MAX_DEPTH = 8
const AUTH_LEN = 35
const NO_RECORD = -1
const Float:CHAIN_COOLDOWN = 2.0
new const CUE_SOUND[] = "buttons/button9"
enum LedgerState
{
LEDGER_EMPTY, // no outbreak seen on this map yet
LEDGER_LIVE, // an outbreak is running: /chain waits for the round to end
LEDGER_CLOSED // the round is over: /chain prints it
}
// One record per infection, in the order they happened.
new g_victimUserId[MAX_RECORDS]
new g_victimName[MAX_RECORDS][MAX_NAME_LENGTH]
new g_victimAuth[MAX_RECORDS][AUTH_LEN]
new g_carrierName[MAX_RECORDS][MAX_NAME_LENGTH] // empty for an origin
new g_carrierAuth[MAX_RECORDS][AUTH_LEN]
new g_parent[MAX_RECORDS] // the carrier's record, or NO_RECORD
new g_recordCount
new g_lost // infections past MAX_RECORDS
new LedgerState:g_state = LEDGER_EMPTY
new bool:g_outbreakOpened // this round's outbreak has cleared the old ledger
new g_maxRows
new Float:g_nextChain[MAX_PLAYERS + 1]
// Report building (filled by build_rows).
new g_rowRecord[MAX_ROWS]
new g_rowDepth[MAX_ROWS]
new g_rowCount
new g_rowsTotal
public plugin_init()
{
register_plugin(PLUGIN, VERSION, AUTHOR)
register_dictionary("olds_ledger.txt")
bind_pcvar_num(create_cvar("olds_ledger_maxrows", "12", FCVAR_NONE,
"Most lines say /chain prints (1-64).", true, 1.0, true, float(MAX_ROWS)), g_maxRows)
AutoExecConfig(true, "olds_ledger")
register_clcmd("say /chain", "cmd_chain")
register_clcmd("say_team /chain", "cmd_chain")
register_concmd("olds_ledger_status", "cmd_status", ADMIN_CFG, "- the ledger as /chain shows it, with Steam ids")
RegisterHookChain(RG_RoundEnd, "on_round_end_post", true)
RegisterHookChain(RG_CSGameRules_RestartRound, "on_restart_round_pre", false)
}
/* ---------- the round ---------- */
open_outbreak()
{
if (g_outbreakOpened)
return
g_outbreakOpened = true
g_recordCount = 0
g_lost = 0
g_state = LEDGER_LIVE
}
close_outbreak()
{
if (g_state != LEDGER_LIVE)
return
g_state = LEDGER_CLOSED
if (!g_recordCount)
return
new players[MAX_PLAYERS], count
get_players(players, count, "ch")
for (new i = 0; i < count; i++)
client_print_color(players[i], print_team_default, "^4[Ledger]^1 %L", players[i], "LEDGER_READY",
g_recordCount + g_lost)
}
public event_gamestart()
{
// Normally the origins came first and opened it already.
open_outbreak()
}
public on_round_end_post(WinStatus:status, ScenarioEventEndRound:event, Float:delay)
{
check_round_decided()
return HC_CONTINUE
}
/*
* Closes the ledger once the game rules say the round is decided. Also called
* from the commands and on each infection, because a round ended by another
* plugin (rg_round_end) skips the RoundEnd hook.
*/
check_round_decided()
{
if (g_state == LEDGER_LIVE && get_member_game(m_bRoundTerminating))
close_outbreak()
}
public on_restart_round_pre()
{
// Covers rounds ended without the RoundEnd hook (rg_round_end, sv_restart).
close_outbreak()
g_outbreakOpened = false
return HC_CONTINUE
}
/* ---------- infections ---------- */
public event_infect(victim, attacker)
{
if (!(1 <= victim <= MaxClients) || !is_user_connected(victim))
return
new bool:origin = !(1 <= attacker <= MaxClients) || attacker == victim || !is_user_connected(attacker)
check_round_decided()
if (origin && !game_started())
open_outbreak() // the outbreak's first zombies: a new ledger
else if (g_state != LEDGER_LIVE)
return // the round is over (or this plugin missed the outbreak's start)
if (g_recordCount == MAX_RECORDS)
{
g_lost++
return
}
new record = g_recordCount++
g_victimUserId[record] = get_user_userid(victim)
get_user_name(victim, g_victimName[record], charsmax(g_victimName[]))
get_user_authid(victim, g_victimAuth[record], charsmax(g_victimAuth[]))
if (origin)
{
g_carrierName[record][0] = EOS
g_carrierAuth[record][0] = EOS
g_parent[record] = NO_RECORD
return
}
get_user_name(attacker, g_carrierName[record], charsmax(g_carrierName[]))
get_user_authid(attacker, g_carrierAuth[record], charsmax(g_carrierAuth[]))
g_parent[record] = latest_record_of(get_user_userid(attacker), record)
if (!is_user_zombie(attacker))
log_amx("Infection of %s credited to %s, who is not a zombie in the core's roles.",
g_victimName[record], g_carrierName[record])
}
/* The carrier's latest own record before this one, or NO_RECORD. */
latest_record_of(userId, before)
{
for (new record = before - 1; record >= 0; record--)
{
if (g_victimUserId[record] == userId)
return record
}
return NO_RECORD
}
/* ---------- the report ---------- */
/*
* Lays the tree out depth first, children in the order they were infected,
* keeping the first `limit` rows. g_rowsTotal counts every row, so the caller
* can print "+N more". A carrier without a record of their own heads a branch
* as row (record, -1 depth marker handled by the printer).
*/
build_rows(limit)
{
g_rowCount = 0
g_rowsTotal = 0
for (new record = 0; record < g_recordCount; record++)
{
if (g_parent[record] != NO_RECORD)
continue
if (g_carrierName[record][0] && !carrier_seen_before(record))
{
// An unrecorded carrier: one header row, then every record they carried.
add_row(record, -1, limit)
for (new child = record; child < g_recordCount; child++)
{
if (g_parent[child] == NO_RECORD && g_carrierName[child][0]
&& same_carrier(child, record))
add_subtree(child, 1, limit)
}
}
else if (!g_carrierName[record][0])
add_subtree(record, 0, limit)
}
}
/* True when an earlier record already headed this unrecorded carrier's branch. */
bool:carrier_seen_before(record)
{
for (new earlier = 0; earlier < record; earlier++)
{
if (g_parent[earlier] == NO_RECORD && g_carrierName[earlier][0] && same_carrier(earlier, record))
return true
}
return false
}
bool:same_carrier(a, b)
{
return equal(g_carrierName[a], g_carrierName[b]) && equal(g_carrierAuth[a], g_carrierAuth[b])
}
add_subtree(record, depth, limit)
{
add_row(record, depth, limit)
for (new child = record + 1; child < g_recordCount; child++)
{
if (g_parent[child] == record)
add_subtree(child, depth + 1, limit)
}
}
add_row(record, depth, limit)
{
if (g_rowCount < limit)
{
g_rowRecord[g_rowCount] = record
g_rowDepth[g_rowCount] = depth
g_rowCount++
}
g_rowsTotal++
}
/* One row of the tree: "A (origin)", " -> B", " -> C", or "X (carrier)". */
format_row(row, text[], len, lang)
{
new record = g_rowRecord[row], depth = g_rowDepth[row]
if (depth == -1)
{
formatex(text, len, "%s %L", g_carrierName[record], lang, "LEDGER_CARRIER")
return
}
if (depth == 0)
{
formatex(text, len, "%s %L", g_victimName[record], lang, "LEDGER_ORIGIN")
return
}
new length
for (new level = 1; level < min(depth, MAX_DEPTH); level++)
length += copy(text[length], len - length, " ")
if (depth > MAX_DEPTH)
length += formatex(text[length], len - length, "(%d) ", depth)
formatex(text[length], len - length, "-> %s", g_victimName[record])
}
public cmd_chain(id)
{
if (!is_user_connected(id))
return PLUGIN_HANDLED
new Float:now = get_gametime()
if (now < g_nextChain[id])
return PLUGIN_HANDLED
g_nextChain[id] = now + CHAIN_COOLDOWN
check_round_decided()
if (g_state == LEDGER_LIVE)
{
client_print_color(id, print_team_default, "^4[Ledger]^1 %L", id, "LEDGER_AFTER_ROUND")
return PLUGIN_HANDLED
}
if (!g_recordCount)
{
client_print_color(id, print_team_default, "^4[Ledger]^1 %L", id, "LEDGER_NONE")
return PLUGIN_HANDLED
}
build_rows(g_maxRows)
client_print_color(id, print_team_default, "^4[Ledger]^1 %L", id, "LEDGER_TITLE", g_recordCount + g_lost)
new text[128]
for (new row = 0; row < g_rowCount; row++)
{
format_row(row, text, charsmax(text), id)
client_print_color(id, print_team_default, "^4[Ledger]^1 %s", text)
}
new more = g_rowsTotal - g_rowCount + g_lost
if (more > 0)
client_print_color(id, print_team_default, "^4[Ledger]^1 %L", id, "LEDGER_MORE", more)
client_cmd(id, "spk %s", CUE_SOUND)
return PLUGIN_HANDLED
}
public cmd_status(id, level, cid)
{
if (!cmd_access(id, level, cid, 1))
return PLUGIN_HANDLED
check_round_decided()
static const STATE_NAME[LedgerState][] = { "no outbreak yet", "outbreak running", "round over" }
console_print(id, "[Ledger] %s; %d record(s), %d past the limit; /chain shows %d line(s)",
STATE_NAME[g_state], g_recordCount, g_lost, g_maxRows)
// Exactly what /chain prints, in the server's language.
build_rows(g_maxRows)
new text[128]
for (new row = 0; row < g_rowCount; row++)
{
format_row(row, text, charsmax(text), LANG_SERVER)
console_print(id, "[Ledger] %s", text)
}
new more = g_rowsTotal - g_rowCount + g_lost
if (more > 0)
console_print(id, "[Ledger] %L", LANG_SERVER, "LEDGER_MORE", more)
for (new record = 0; record < g_recordCount; record++)
{
if (g_carrierName[record][0])
console_print(id, "[Ledger] #%d %s <%s> by %s <%s>", record + 1, g_victimName[record],
g_victimAuth[record], g_carrierName[record], g_carrierAuth[record])
else
console_print(id, "[Ledger] #%d %s <%s> origin", record + 1, g_victimName[record], g_victimAuth[record])
}
return PLUGIN_HANDLED
}
public client_disconnected(id)
{
// Records keep their own copies of names and Steam ids.
g_nextChain[id] = 0.0
}