/*
* Fork Ledger (olds.ro workshop card AMX-005)
*
* Times surfers on routes that split into two branches. A run starts when the
* player leaves the shared entry zone, records the first branch zone touched,
* and finishes at the shared exit zone. Each player keeps a private best for
* each branch on the current map; say /fork shows both and marks the faster.
*
* Needs: AMX Mod X 1.10 (engine and hamsandwich modules). No ReAPI needed.
*
* Zones: configs/olds_fork/<map>.ini, one zone per line:
* entry x1 y1 z1 x2 y2 z2
* branchA ["Label"] x1 y1 z1 x2 y2 z2
* branchB ["Label"] x1 y1 z1 x2 y2 z2
* exit x1 y1 z1 x2 y2 z2
* Two opposite corners make a box. All four zones are required; a map
* without a complete file keeps the timer off.
*
* A run, per player (checked every 0.1 s by one repeating task):
* in entry -> armed; an unfinished run is cancelled (teleported back)
* leaves entry -> run starts, a short tick plays
* first branch zone -> that branch is the run's branch; the other is ignored
* exit zone -> run ends; the time counts only if a branch was crossed
* death, respawn, disconnect, team change to spectator -> run cancelled
* Times are measured on the 0.1 s poll, so they are accurate to 0.1 s. At surf
* speeds a player moves up to ~350 units per poll, so every zone should be at
* least 400 units deep along the route, or it can be skipped.
*
* Cvar: olds_fork_enable (0/1, default 1).
* Commands: say /fork, say_team /fork; olds_fork_status and olds_fork_where (ADMIN_CFG).
*
* Copyright (C) 2026 daemon (olds.ro)
* SPDX-License-Identifier: GPL-3.0-or-later
*/
#include <amxmodx>
#include <amxmisc>
#include <engine>
#include <hamsandwich>
#define PLUGIN "Fork Ledger"
#define VERSION "1.0.0"
#define AUTHOR "olds.ro"
const TASK_POLL = 5170
const Float:POLL_INTERVAL = 0.1
const LABEL_LEN = 32
const Float:FORK_COOLDOWN = 2.0
new const TICK_SOUND[] = "buttons/blip2"
enum _:Zone
{
ZONE_ENTRY,
ZONE_A,
ZONE_B,
ZONE_EXIT
}
new const ZONE_KEY[Zone][] = { "entry", "branchA", "branchB", "exit" }
enum RunState
{
RUN_IDLE, // not in a run: must go back to the entry zone
RUN_ARMED, // standing in the entry zone
RUN_RUNNING
}
// Zones of the current map.
new Float:g_zoneMin[Zone][3]
new Float:g_zoneMax[Zone][3]
new bool:g_zoneSet[Zone]
new g_label[2][LABEL_LEN] // branch A, branch B
new bool:g_zonesReady
// Per player.
new RunState:g_state[MAX_PLAYERS + 1]
new Float:g_startTime[MAX_PLAYERS + 1]
new g_branch[MAX_PLAYERS + 1] // -1 none, 0 = A, 1 = B
new Float:g_best[MAX_PLAYERS + 1][2] // 0.0 = no time yet
new Float:g_nextFork[MAX_PLAYERS + 1]
new g_enabled
new g_hudSync
public plugin_init()
{
register_plugin(PLUGIN, VERSION, AUTHOR)
register_dictionary("olds_fork.txt")
new pcvar = create_cvar("olds_fork_enable", "1", FCVAR_NONE, "Fork Ledger branch timer (0 off, 1 on).",
true, 0.0, true, 1.0)
bind_pcvar_num(pcvar, g_enabled)
hook_cvar_change(pcvar, "on_enable_changed")
AutoExecConfig(true, "olds_fork")
register_clcmd("say /fork", "cmd_fork")
register_clcmd("say_team /fork", "cmd_fork")
register_concmd("olds_fork_status", "cmd_status", ADMIN_CFG, "- run state and bests of every player")
register_concmd("olds_fork_where", "cmd_where", ADMIN_CFG, "- your position and zone, for writing zone files")
RegisterHamPlayer(Ham_Killed, "on_killed_post", true)
RegisterHamPlayer(Ham_Spawn, "on_spawn_post", true)
g_hudSync = CreateHudSyncObj()
copy(g_label[0], charsmax(g_label[]), "A")
copy(g_label[1], charsmax(g_label[]), "B")
}
public plugin_cfg()
{
load_zones()
update_poll()
}
public plugin_end()
{
remove_task(TASK_POLL)
}
public on_enable_changed(pcvar, const oldValue[], const newValue[])
{
// bind_pcvar_num updates g_enabled after this callback, so read the new value.
g_enabled = str_to_num(newValue)
if (!g_enabled)
{
for (new id = 1; id <= MaxClients; id++)
g_state[id] = RUN_IDLE
}
update_poll()
}
update_poll()
{
remove_task(TASK_POLL)
if (g_enabled && g_zonesReady)
set_task(POLL_INTERVAL, "poll_players", TASK_POLL, _, _, "b")
}
/* ---------- zones ---------- */
load_zones()
{
new map[32], path[PLATFORM_MAX_PATH]
get_mapname(map, charsmax(map))
get_configsdir(path, charsmax(path))
format(path, charsmax(path), "%s/olds_fork/%s.ini", path, map)
new file = fopen(path, "rt")
if (!file)
{
log_amx("No zones for %s (%s): the branch timer is off on this map.", map, path)
return
}
new line[256], token[8][LABEL_LEN], lineNumber
while (fgets(file, line, charsmax(line)))
{
lineNumber++
trim(line)
if (!line[0] || line[0] == ';' || line[0] == '#' || (line[0] == '/' && line[1] == '/'))
continue
new parts = parse(line, token[0], charsmax(token[]), token[1], charsmax(token[]), token[2], charsmax(token[]),
token[3], charsmax(token[]), token[4], charsmax(token[]), token[5], charsmax(token[]),
token[6], charsmax(token[]), token[7], charsmax(token[]))
new zone = zone_by_key(token[0])
if (zone == -1)
{
log_amx("%s line %d: unknown zone ^"%s^" (use entry, branchA, branchB or exit).", path, lineNumber, token[0])
continue
}
// A branch may carry a label before its corners.
new first = 1
if (parts == 8 && (zone == ZONE_A || zone == ZONE_B))
first = 2
else if (parts != 7)
{
log_amx("%s line %d: expected %s x1 y1 z1 x2 y2 z2.", path, lineNumber, ZONE_KEY[zone])
continue
}
new Float:a[3], Float:b[3]
if (!read_float(token[first], a[0]) || !read_float(token[first + 1], a[1]) || !read_float(token[first + 2], a[2])
|| !read_float(token[first + 3], b[0]) || !read_float(token[first + 4], b[1]) || !read_float(token[first + 5], b[2]))
{
log_amx("%s line %d: the six corner values must be numbers.", path, lineNumber)
continue
}
if (a[0] == b[0] || a[1] == b[1] || a[2] == b[2])
{
log_amx("%s line %d: zone %s has no volume.", path, lineNumber, ZONE_KEY[zone])
continue
}
if (g_zoneSet[zone])
{
log_amx("%s line %d: zone %s is already defined.", path, lineNumber, ZONE_KEY[zone])
continue
}
for (new axis = 0; axis < 3; axis++)
{
g_zoneMin[zone][axis] = floatmin(a[axis], b[axis])
g_zoneMax[zone][axis] = floatmax(a[axis], b[axis])
}
g_zoneSet[zone] = true
if (first == 2 && token[1][0])
copy(g_label[zone - ZONE_A], charsmax(g_label[]), token[1])
}
fclose(file)
g_zonesReady = true
for (new zone = 0; zone < Zone; zone++)
{
if (!g_zoneSet[zone])
{
log_amx("%s: zone %s is missing, so the branch timer is off on this map.", path, ZONE_KEY[zone])
g_zonesReady = false
}
}
if (g_zonesReady)
log_amx("Zones loaded for %s (branches ^"%s^" and ^"%s^").", map, g_label[0], g_label[1])
}
zone_by_key(const key[])
{
for (new zone = 0; zone < Zone; zone++)
{
if (equali(key, ZONE_KEY[zone]))
return zone
}
return -1
}
bool:read_float(const text[], &Float:value)
{
new i, digits, dots
if (text[i] == '-' || text[i] == '+')
i++
for (; text[i]; i++)
{
if (isdigit(text[i]))
digits++
else if (text[i] == '.' && ++dots == 1)
continue
else
return false
}
if (!digits)
return false
value = str_to_float(text)
return true
}
bool:in_zone(zone, const Float:origin[3])
{
return origin[0] >= g_zoneMin[zone][0] && origin[0] <= g_zoneMax[zone][0]
&& origin[1] >= g_zoneMin[zone][1] && origin[1] <= g_zoneMax[zone][1]
&& origin[2] >= g_zoneMin[zone][2] && origin[2] <= g_zoneMax[zone][2]
}
/* ---------- runs ---------- */
public poll_players()
{
new players[MAX_PLAYERS], count
get_players(players, count, "ah")
new Float:now = get_gametime()
new Float:origin[3]
for (new i = 0; i < count; i++)
{
new id = players[i]
entity_get_vector(id, EV_VEC_origin, origin)
step_run(id, origin, now)
}
}
step_run(id, const Float:origin[3], Float:now)
{
if (in_zone(ZONE_ENTRY, origin))
{
// Back at the entry (walked or teleported): any unfinished run is dropped.
g_state[id] = RUN_ARMED
return
}
if (g_state[id] == RUN_ARMED)
{
g_state[id] = RUN_RUNNING
g_startTime[id] = now
g_branch[id] = -1
client_cmd(id, "spk %s", TICK_SOUND)
}
if (g_state[id] != RUN_RUNNING)
return
if (g_branch[id] == -1)
{
if (in_zone(ZONE_A, origin))
g_branch[id] = 0
else if (in_zone(ZONE_B, origin))
g_branch[id] = 1
}
if (in_zone(ZONE_EXIT, origin))
finish_run(id, now - g_startTime[id])
}
finish_run(id, Float:time)
{
g_state[id] = RUN_IDLE
new branch = g_branch[id]
set_hudmessage(80, 200, 255, -1.0, 0.30, 0, 0.0, 4.0, 0.1, 0.4, -1)
if (branch == -1)
{
ShowSyncHudMsg(id, g_hudSync, "%L", id, "FORK_NO_BRANCH")
return
}
new bool:record = g_best[id][branch] == 0.0 || time < g_best[id][branch]
if (record)
g_best[id][branch] = time
ShowSyncHudMsg(id, g_hudSync, "%L", id, record ? "FORK_FINISH_BEST" : "FORK_FINISH", g_label[branch], time, g_best[id][branch])
}
cancel_run(id)
{
g_state[id] = RUN_IDLE
g_branch[id] = -1
}
public on_killed_post(victim, attacker, shouldGib)
{
if (1 <= victim <= MaxClients)
cancel_run(victim)
return HAM_IGNORED
}
public on_spawn_post(id)
{
if (1 <= id <= MaxClients)
cancel_run(id)
return HAM_IGNORED
}
/* ---------- commands ---------- */
public cmd_fork(id)
{
if (!is_user_connected(id))
return PLUGIN_HANDLED
new Float:now = get_gametime()
if (now < g_nextFork[id])
return PLUGIN_HANDLED
g_nextFork[id] = now + FORK_COOLDOWN
new text[192]
format_bests(id, id, text, charsmax(text))
set_hudmessage(80, 200, 255, -1.0, 0.30, 0, 0.0, 6.0, 0.1, 0.4, -1)
ShowSyncHudMsg(id, g_hudSync, "%s", text)
return PLUGIN_HANDLED
}
/* lang is a player index or LANG_SERVER. */
format_bests(lang, id, text[], len)
{
if (!g_zonesReady)
{
formatex(text, len, "%L", lang, "FORK_NO_ZONES")
return
}
new line[2][80]
for (new branch = 0; branch < 2; branch++)
{
if (g_best[id][branch] == 0.0)
formatex(line[branch], charsmax(line[]), "%L", lang, "FORK_BEST_NONE", g_label[branch])
else
formatex(line[branch], charsmax(line[]), "%L", lang, "FORK_BEST", g_label[branch], g_best[id][branch])
}
// Mark the faster branch when both have a time.
new faster = -1
if (g_best[id][0] > 0.0 && g_best[id][1] > 0.0 && g_best[id][0] != g_best[id][1])
faster = g_best[id][0] < g_best[id][1] ? 0 : 1
if (faster != -1)
add(line[faster], charsmax(line[]), " <")
formatex(text, len, "%L^n%s^n%s", lang, "FORK_TITLE", line[0], line[1])
}
public cmd_status(id, level, cid)
{
if (!cmd_access(id, level, cid, 1))
return PLUGIN_HANDLED
console_print(id, "[Fork] enabled %d, zones %s, branches ^"%s^" / ^"%s^"", g_enabled,
g_zonesReady ? "ready" : "missing", g_label[0], g_label[1])
new players[MAX_PLAYERS], count, name[32]
get_players(players, count, "h")
for (new i = 0; i < count; i++)
{
new player = players[i]
get_user_name(player, name, charsmax(name))
console_print(id, "[Fork] %-20s state %d branch %2d best A %6.1f best B %6.1f", name,
_:g_state[player], g_branch[player], g_best[player][0], g_best[player][1])
}
return PLUGIN_HANDLED
}
public cmd_where(id, level, cid)
{
if (!cmd_access(id, level, cid, 1))
return PLUGIN_HANDLED
if (!id || !is_user_alive(id))
{
console_print(id, "[Fork] Only a living player has a position.")
return PLUGIN_HANDLED
}
new Float:origin[3]
entity_get_vector(id, EV_VEC_origin, origin)
console_print(id, "[Fork] %.0f %.0f %.0f", origin[0], origin[1], origin[2])
for (new zone = 0; zone < Zone; zone++)
{
if (g_zoneSet[zone] && in_zone(zone, origin))
console_print(id, "[Fork] inside %s", ZONE_KEY[zone])
}
return PLUGIN_HANDLED
}
/* ---------- players ---------- */
public client_putinserver(id)
{
reset_player(id)
}
public client_disconnected(id)
{
reset_player(id)
}
reset_player(id)
{
g_state[id] = RUN_IDLE
g_branch[id] = -1
g_startTime[id] = 0.0
g_best[id][0] = g_best[id][1] = 0.0
g_nextFork[id] = 0.0
}