/*
* Bhop Par Jumps (olds.ro workshop card IDEA-2CFA4C7A90BDC4E8)
*
* Each bhop map gets a par: the number of jumps a clean run needs, set by an
* admin or worked out from the best runs. At the finish a runner sees their
* jump count against par, like golf ("40 jumps (2 under par)"), and /par
* shows how many players on the server finished at or under par.
*
* This is an add-on for a bhop timer. The timer's core plugin must call
* these two public functions with CreateMultiForward / ExecuteForward when a
* player starts and finishes a run:
*
* olds_timer_start(id)
* olds_timer_finish(id)
*
* Without such a timer nothing is counted.
*
* Commands:
* say /par the par of this map and how many players on the
* server finished at or under it
* amx_bhpar <jumps> (admin flag "h", ADMIN_CFG) sets the par of this
* map, saved in configs/olds_bhpar.ini; 0 clears it
* olds_bhpar_status (server console) the par and each player's run and
* best on this map
*
* Cvars (in configs/plugins/olds_bhpar.cfg, written with the defaults on the
* first start if the file is missing):
* olds_bhpar_enable 1 1 = on, 0 = off (no counting and no results).
* olds_bhpar_auto 1 1 = a map without an admin par gets one from the
* five lowest best counts (their rounded average)
* once five players have finished it; 0 = only
* admin pars.
*
* How it works, as the card asks: a start resets the player's count; a jump
* is counted in RG_CBasePlayer_Jump (pre) while the player is on the ground
* (var_flags FL_ONGROUND) and has just pressed jump (m_afButtonPressed
* IN_JUMP). The best count per map and player is kept in the nvault file
* olds_bhpar by SteamID, or by name for bots and players without a unique
* SteamID (STEAM_ID_LAN), together with the five lowest bests of each map
* for the automatic par.
*
* Requires AMX Mod X 1.10 with ReAPI (ReGameDLL_CS) and the nvault module.
* Written and tested on AMX Mod X 1.10.0.5490, ReAPI 5.29.0.358 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>
#include <amxmisc>
#include <nvault>
#include <reapi>
#pragma semicolon 1
new const PLUGIN_NAME[] = "Bhop Par Jumps";
new const PLUGIN_VERSION[] = "1.0.0";
new const PLUGIN_AUTHOR[] = "alexit (olds.ro)";
new const DICTIONARY[] = "olds_bhpar.txt";
new const VAULT_NAME[] = "olds_bhpar";
new const PAR_FILE[] = "olds_bhpar.ini";
// Prefix of player keys that are a name instead of a SteamID.
new const NAME_KEY_PREFIX[] = "name:";
// The automatic par is the rounded average of this many lowest bests.
const AUTO_PAR_RUNS = 5;
const MAX_PAR = 10000;
const NO_BEST = -1;
new g_enable; // olds_bhpar_enable
new g_auto; // olds_bhpar_auto
new g_map[64];
new g_parFile[256];
new g_vault = INVALID_HANDLE;
new g_adminPar; // 0 = none
new g_autoPar; // 0 = fewer than AUTO_PAR_RUNS players finished
// The lowest bests on this map, lowest first: count and player key hash.
new g_topCount;
new g_topBest[AUTO_PAR_RUNS];
new g_topHash[AUTO_PAR_RUNS];
new bool:g_running[MAX_PLAYERS + 1];
new g_jumps[MAX_PLAYERS + 1];
new g_best[MAX_PLAYERS + 1];
new g_playerKey[MAX_PLAYERS + 1][MAX_AUTHID_LENGTH + 8];
public plugin_init()
{
register_plugin(PLUGIN_NAME, PLUGIN_VERSION, PLUGIN_AUTHOR);
register_dictionary(DICTIONARY);
bind_pcvar_num(create_cvar("olds_bhpar_enable", "1", FCVAR_NONE,
"1 = jumps are counted and compared with the map's par, 0 = off",
true, 0.0, true, 1.0), g_enable);
bind_pcvar_num(create_cvar("olds_bhpar_auto", "1", FCVAR_NONE,
"1 = maps without an admin par get one from the five lowest best counts, 0 = admin pars only",
true, 0.0, true, 1.0), g_auto);
AutoExecConfig(true, "olds_bhpar");
register_clcmd("say /par", "cmd_par");
register_clcmd("say_team /par", "cmd_par");
register_concmd("amx_bhpar", "cmd_set_par", ADMIN_CFG, "<jumps> - par of this map, 0 clears it");
register_srvcmd("olds_bhpar_status", "cmd_status");
RegisterHookChain(RG_CBasePlayer_Jump, "on_player_jump", false);
get_mapname(g_map, charsmax(g_map));
strtolower(g_map);
get_configsdir(g_parFile, charsmax(g_parFile));
format(g_parFile, charsmax(g_parFile), "%s/%s", g_parFile, PAR_FILE);
g_vault = nvault_open(VAULT_NAME);
if (g_vault == INVALID_HANDLE)
log_amx("Could not open the nvault file %s; best counts are kept only until players leave.", VAULT_NAME);
g_adminPar = read_admin_par();
load_top_list();
}
public plugin_end()
{
if (g_vault != INVALID_HANDLE)
nvault_close(g_vault);
}
/* -------------------------------------------------------------------------
* The timer core's forwards
* ---------------------------------------------------------------------- */
public olds_timer_start(id)
{
if (!g_enable || !(1 <= id <= MaxClients))
return;
g_running[id] = true;
g_jumps[id] = 0;
}
public olds_timer_finish(id)
{
if (!(1 <= id <= MaxClients) || !g_running[id])
return;
g_running[id] = false;
if (!g_enable)
return;
new jumps = g_jumps[id];
if (g_best[id] == NO_BEST || jumps < g_best[id])
save_best(id, jumps);
new par = current_par();
if (!par)
client_print_color(id, print_team_default, "%L", id, "BHPAR_RESULT_NO_PAR", jumps);
else if (jumps < par)
client_print_color(id, print_team_default, "%L", id, "BHPAR_RESULT_UNDER", jumps, par - jumps);
else if (jumps == par)
client_print_color(id, print_team_default, "%L", id, "BHPAR_RESULT_AT", jumps);
else
client_print_color(id, print_team_default, "%L", id, "BHPAR_RESULT_OVER", jumps, jumps - par);
}
public on_player_jump(id)
{
if (g_enable && g_running[id]
&& (get_entvar(id, var_flags) & FL_ONGROUND)
&& (get_member(id, m_afButtonPressed) & IN_JUMP))
{
g_jumps[id]++;
}
return HC_CONTINUE;
}
/* -------------------------------------------------------------------------
* Players
* ---------------------------------------------------------------------- */
public client_connect(id)
{
g_running[id] = false;
g_jumps[id] = 0;
g_best[id] = NO_BEST;
g_playerKey[id][0] = EOS;
}
public client_authorized(id, const authid[])
{
if (has_unique_steamid(authid))
load_best(id, authid);
}
public client_putinserver(id)
{
if (g_playerKey[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_best(id, key);
}
}
public client_disconnected(id)
{
g_running[id] = false;
}
/**
* 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);
}
best_vault_key(const playerKey[], out[], len)
{
formatex(out, len, "best|%s|%s", g_map, playerKey);
}
load_best(id, const playerKey[])
{
copy(g_playerKey[id], charsmax(g_playerKey[]), playerKey);
if (g_vault == INVALID_HANDLE)
return;
new key[128], value[16], timestamp;
best_vault_key(playerKey, key, charsmax(key));
if (nvault_lookup(g_vault, key, value, charsmax(value), timestamp))
g_best[id] = str_to_num(value);
}
save_best(id, jumps)
{
g_best[id] = jumps;
if (!g_playerKey[id][0])
return;
if (g_vault != INVALID_HANDLE)
{
new key[128], value[16];
best_vault_key(g_playerKey[id], key, charsmax(key));
num_to_str(jumps, value, charsmax(value));
nvault_set(g_vault, key, value);
}
update_top_list(key_hash(g_playerKey[id]), jumps);
}
/* -------------------------------------------------------------------------
* Par
* ---------------------------------------------------------------------- */
/**
* The par in use on this map: the admin's, else the automatic one when
* olds_bhpar_auto is on; 0 when there is none.
*/
current_par()
{
if (g_adminPar)
return g_adminPar;
return g_auto ? g_autoPar : 0;
}
/**
* A number for a player key, so the list of lowest bests can tell players
* apart without storing names (which may hold any character).
*/
key_hash(const text[])
{
new hash = 5381;
for (new i = 0; text[i]; i++)
hash = hash * 33 + text[i];
return hash & 0x7FFFFFFF;
}
top_vault_key(out[], len)
{
formatex(out, len, "top|%s", g_map);
}
load_top_list()
{
g_topCount = 0;
if (g_vault != INVALID_HANDLE)
{
new key[96], value[192], timestamp;
top_vault_key(key, charsmax(key));
if (nvault_lookup(g_vault, key, value, charsmax(value), timestamp))
{
// "best:hash best:hash ...", lowest first
new entry[32], rest[192], best[16], hash[16];
while (value[0] && g_topCount < AUTO_PAR_RUNS)
{
strtok(value, entry, charsmax(entry), rest, charsmax(rest), ' ', 1);
copy(value, charsmax(value), rest);
strtok(entry, best, charsmax(best), hash, charsmax(hash), ':');
if (best[0] && hash[0])
{
g_topBest[g_topCount] = str_to_num(best);
g_topHash[g_topCount] = str_to_num(hash);
g_topCount++;
}
}
}
}
compute_auto_par();
}
update_top_list(hash, best)
{
// The player's earlier entry goes; a new best is never higher than it.
for (new i = 0; i < g_topCount; i++)
{
if (g_topHash[i] == hash)
{
for (new j = i; j < g_topCount - 1; j++)
{
g_topBest[j] = g_topBest[j + 1];
g_topHash[j] = g_topHash[j + 1];
}
g_topCount--;
break;
}
}
new place = g_topCount;
while (place > 0 && g_topBest[place - 1] > best)
place--;
if (place >= AUTO_PAR_RUNS)
return;
new last = min(g_topCount, AUTO_PAR_RUNS - 1);
for (new j = last; j > place; j--)
{
g_topBest[j] = g_topBest[j - 1];
g_topHash[j] = g_topHash[j - 1];
}
g_topBest[place] = best;
g_topHash[place] = hash;
g_topCount = min(g_topCount + 1, AUTO_PAR_RUNS);
if (g_vault != INVALID_HANDLE)
{
new key[96], value[192], length;
top_vault_key(key, charsmax(key));
for (new i = 0; i < g_topCount; i++)
length += formatex(value[length], charsmax(value) - length, "%s%d:%d", i ? " " : "", g_topBest[i], g_topHash[i]);
nvault_set(g_vault, key, value);
}
compute_auto_par();
}
compute_auto_par()
{
if (g_topCount < AUTO_PAR_RUNS)
{
g_autoPar = 0;
return;
}
new sum;
for (new i = 0; i < AUTO_PAR_RUNS; i++)
sum += g_topBest[i];
g_autoPar = max(1, floatround(float(sum) / float(AUTO_PAR_RUNS)));
}
/**
* Reads this map's par from configs/olds_bhpar.ini ("<map> <jumps>" lines).
*/
read_admin_par()
{
new file = fopen(g_parFile, "rt");
if (!file)
return 0;
new line[128], map[64], value[16], par;
while (fgets(file, line, charsmax(line)))
{
trim(line);
if (!line[0] || line[0] == ';' || line[0] == '/' || line[0] == '#')
continue;
parse(line, map, charsmax(map), value, charsmax(value));
if (equali(map, g_map))
par = clamp(str_to_num(value), 0, MAX_PAR);
}
fclose(file);
return par;
}
/**
* Writes this map's par to configs/olds_bhpar.ini, keeping every other line;
* 0 removes the map's line.
*/
bool:write_admin_par(par)
{
new Array:lines = ArrayCreate(128);
new file = fopen(g_parFile, "rt");
if (file)
{
new line[128], map[64];
while (fgets(file, line, charsmax(line)))
{
trim(line);
map[0] = EOS;
if (line[0] && line[0] != ';' && line[0] != '/' && line[0] != '#')
parse(line, map, charsmax(map));
if (!equali(map, g_map))
ArrayPushString(lines, line);
}
fclose(file);
}
else
{
ArrayPushString(lines, "; Bhop Par Jumps (olds_bhpar): par of each map, one map per line.");
ArrayPushString(lines, "; <map> <jumps>, written by amx_bhpar <jumps> on that map (0 removes it).");
}
if (par)
{
new entry[128];
formatex(entry, charsmax(entry), "%s %d", g_map, par);
ArrayPushString(lines, entry);
}
file = fopen(g_parFile, "wt");
if (!file)
{
ArrayDestroy(lines);
return false;
}
new line[128];
for (new i = 0, count = ArraySize(lines); i < count; i++)
{
ArrayGetString(lines, i, line, charsmax(line));
fprintf(file, "%s^n", line);
}
fclose(file);
ArrayDestroy(lines);
return true;
}
/* -------------------------------------------------------------------------
* Commands
* ---------------------------------------------------------------------- */
public cmd_par(id)
{
if (!g_enable)
{
client_print_color(id, print_team_default, "%L", id, "BHPAR_OFF");
return PLUGIN_HANDLED;
}
new par = current_par();
if (!par)
{
client_print_color(id, print_team_default, "%L", id, "BHPAR_NO_PAR");
return PLUGIN_HANDLED;
}
new count;
for (new player = 1; player <= MaxClients; player++)
{
if (is_user_connected(player) && g_best[player] != NO_BEST && g_best[player] <= par)
count++;
}
client_print_color(id, print_team_default, "%L", id, count == 1 ? "BHPAR_PAR_ONE" : "BHPAR_PAR_MANY", par, count);
return PLUGIN_HANDLED;
}
public cmd_set_par(id, level, cid)
{
if (!cmd_access(id, level, cid, 2))
return PLUGIN_HANDLED;
new arg[16];
read_argv(1, arg, charsmax(arg));
if (!is_str_num(arg) || str_to_num(arg) > MAX_PAR)
{
console_print(id, "%L", id, "BHPAR_SET_USAGE", MAX_PAR);
return PLUGIN_HANDLED;
}
new par = str_to_num(arg);
if (!write_admin_par(par))
{
console_print(id, "%L", id, "BHPAR_SET_FAILED", g_parFile);
return PLUGIN_HANDLED;
}
g_adminPar = par;
if (par)
{
console_print(id, "%L", id, "BHPAR_SET_DONE", g_map, par);
log_amx("%N set the par of %s to %d jumps.", id, g_map, par);
}
else
{
console_print(id, "%L", id, "BHPAR_SET_CLEARED", g_map);
log_amx("%N cleared the par of %s.", id, g_map);
}
return PLUGIN_HANDLED;
}
public cmd_status()
{
if (!g_enable)
{
server_print("Bhop Par Jumps: disabled");
return PLUGIN_HANDLED;
}
if (g_adminPar)
server_print("Bhop Par Jumps: map %s, par %d (admin)", g_map, g_adminPar);
else if (g_auto && g_autoPar)
server_print("Bhop Par Jumps: map %s, par %d (auto)", g_map, g_autoPar);
else if (!g_auto)
server_print("Bhop Par Jumps: map %s, par none (no admin par, olds_bhpar_auto is 0)", g_map);
else
server_print("Bhop Par Jumps: map %s, par none (%d of %d best runs for the automatic par)", g_map, g_topCount, AUTO_PAR_RUNS);
for (new id = 1; id <= MaxClients; id++)
{
if (!is_user_connected(id))
continue;
if (g_best[id] == NO_BEST)
server_print("%n run %d jumps%s, best none", id, g_jumps[id], g_running[id] ? " (running)" : "");
else
server_print("%n run %d jumps%s, best %d", id, g_jumps[id], g_running[id] ? " (running)" : "", g_best[id]);
}
return PLUGIN_HANDLED;
}