Skip to content
View in the app

A better way to browse. Learn more.

Learn more

long-jump-statistics/addons/amxmodx/scripting/olds_ljstats.sma

olds_ljstats.sma
471 lines · 15.1 KB · Pawn (AMX Mod X)
Raw
/*
 * Long Jump Statistics (olds.ro)
 * Copyright (C) 2026 daemon (olds.ro), written by daemon's plugin agent
 *
 * 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/>.
 *
 * Long jump: measures each jump's distance, pre-speed and strafe sync and keeps every
 * player's record and a top table. Every frame (FM_PlayerPreThink) the speed and the strafe
 * keys are sampled: the pre-speed is the ground speed at take-off, the sync is the share of
 * air frames in which the speed grew, and strafes are counted when the strafe key changes.
 * Jumps of at least olds_ljs_min units landed at the take-off height are counted; teleports,
 * noclip, water, ladders and ice (low friction) make a jump invalid. A counted jump with a
 * sync of at least olds_ljs_sync percent can set a record. Records and the top
 * olds_ljs_top live in the nVault "olds_ljs".
 *
 * Cvars: olds_ljs_min 220, olds_ljs_top 15, olds_ljs_sync 1
 * Commands: say /ljstats, say /ljtop
 * Admin: amx_ljtop (ADMIN_KICK) prints the top table in the console
 */

#include <amxmodx>
#include <amxmisc>
#include <fakemeta>
#include <nvault>

#pragma semicolon 0

#define PLUGIN  "Long Jump Statistics"
#define VERSION "1.0.0"
#define AUTHOR  "daemon (olds.ro)"

#define HULL_WIDTH  32.0
#define SAME_HEIGHT 0.5
#define MAX_TOP     30
#define KEY_LEN     40

new g_min
new g_topSize
new g_minSync

new g_vault = INVALID_HANDLE

/* in-flight state */
new bool:g_inAir[MAX_PLAYERS + 1]
new bool:g_jumped[MAX_PLAYERS + 1]
new bool:g_valid[MAX_PLAYERS + 1]
new g_badReason[MAX_PLAYERS + 1][16]
new Float:g_groundOrigin[MAX_PLAYERS + 1][3]
new Float:g_groundSpeed[MAX_PLAYERS + 1]
new bool:g_groundDuck[MAX_PLAYERS + 1]
new Float:g_takeoff[MAX_PLAYERS + 1][3]
new Float:g_takeoffFeet[MAX_PLAYERS + 1]
new Float:g_pre[MAX_PLAYERS + 1]
new Float:g_lastSpeed[MAX_PLAYERS + 1]
new Float:g_lastOrigin[MAX_PLAYERS + 1][3]
new Float:g_lastTime[MAX_PLAYERS + 1]
new g_airFrames[MAX_PLAYERS + 1]
new g_gainFrames[MAX_PLAYERS + 1]
new g_strafes[MAX_PLAYERS + 1]
new g_lastKey[MAX_PLAYERS + 1]

/* last counted jump and personal best (this connection; the best is also in the vault) */
new bool:g_haveLast[MAX_PLAYERS + 1]
new Float:g_lastDist[MAX_PLAYERS + 1]
new Float:g_lastPre[MAX_PLAYERS + 1]
new g_lastSync[MAX_PLAYERS + 1]
new g_lastStrafes[MAX_PLAYERS + 1]
new Float:g_best[MAX_PLAYERS + 1]
new g_jumps[MAX_PLAYERS + 1]

/* the top table */
new g_topCount
new g_topKey[MAX_TOP][KEY_LEN]
new g_topName[MAX_TOP][32]
new Float:g_topDist[MAX_TOP]
new Float:g_topPre[MAX_TOP]
new g_topSync[MAX_TOP]

public plugin_init()
{
    register_plugin(PLUGIN, VERSION, AUTHOR)
    register_dictionary("olds_ljstats.txt")

    bind_pcvar_num(create_cvar("olds_ljs_min", "220", FCVAR_NONE,
        "Shortest jump (units) that is counted", true, 100.0, true, 300.0), g_min)
    bind_pcvar_num(create_cvar("olds_ljs_top", "15", FCVAR_NONE,
        "Entries in the top table", true, 1.0, true, float(MAX_TOP)), g_topSize)
    bind_pcvar_num(create_cvar("olds_ljs_sync", "1", FCVAR_NONE,
        "Lowest strafe sync (percent) for a jump to set a record", true, 0.0, true, 100.0), g_minSync)
    AutoExecConfig(true, "olds_ljstats")

    register_forward(FM_PlayerPreThink, "on_prethink")
    register_clcmd("say /ljstats", "cmd_stats")
    register_clcmd("say_team /ljstats", "cmd_stats")
    register_clcmd("say /ljtop", "cmd_top")
    register_clcmd("say_team /ljtop", "cmd_top")
    register_concmd("amx_ljtop", "cmd_admin_top", ADMIN_KICK, "- prints the long jump top in the console")
}

public plugin_cfg()
{
    g_vault = nvault_open("olds_ljs")
    if (g_vault == INVALID_HANDLE)
        log_amx("Cannot open the nVault olds_ljs: records are kept for this map only")
    load_top()
}

public plugin_end()
{
    if (g_vault != INVALID_HANDLE)
        nvault_close(g_vault)
}

/* ------------------------------------------------------------ players */

player_key(id, out[], len)
{
    /* Bots share one auth id, so they are told apart by name. */
    if (is_user_bot(id))
    {
        new name[32]
        get_user_name(id, name, charsmax(name))
        formatex(out, len, "BOT:%s", name)
    }
    else
        get_user_authid(id, out, len)
}

public client_putinserver(id)
{
    g_inAir[id] = false
    g_haveLast[id] = false
    g_jumps[id] = 0
    g_best[id] = 0.0
    if (g_vault == INVALID_HANDLE)
        return
    new key[KEY_LEN], vkey[KEY_LEN + 4], value[64]
    player_key(id, key, charsmax(key))
    formatex(vkey, charsmax(vkey), "pb:%s", key)
    if (nvault_get(g_vault, vkey, value, charsmax(value)))
        g_best[id] = str_to_float(value)
}

public client_disconnected(id)
{
    g_inAir[id] = false
    g_haveLast[id] = false
}

/* ------------------------------------------------------------ sampling */

strafe_key(buttons)
{
    new left = buttons & IN_MOVELEFT, right = buttons & IN_MOVERIGHT
    if (left && !right)
        return 1
    if (right && !left)
        return 2
    return 0
}

spoil(id, const why[])
{
    if (g_valid[id])
    {
        g_valid[id] = false
        copy(g_badReason[id], charsmax(g_badReason[]), why)
    }
}

public on_prethink(id)
{
    if (!is_user_alive(id))
    {
        g_inAir[id] = false
        return FMRES_IGNORED
    }
    new flags = pev(id, pev_flags)
    new Float:origin[3], Float:velocity[3], Float:now = get_gametime()
    pev(id, pev_origin, origin)
    pev(id, pev_velocity, velocity)
    new Float:speed = floatsqroot(velocity[0] * velocity[0] + velocity[1] * velocity[1])
    new bool:ground = (flags & FL_ONGROUND) != 0
    new Float:friction
    pev(id, pev_friction, friction)

    if (!g_inAir[id])
    {
        if (ground)
        {
            g_groundOrigin[id] = origin
            g_groundSpeed[id] = speed
            g_groundDuck[id] = (flags & FL_DUCKING) != 0
        }
        else
        {
            take_off(id, velocity[2], friction)
            g_lastSpeed[id] = speed
        }
    }
    else
    {
        new Float:dt = now - g_lastTime[id]
        new Float:moved = get_distance_f(origin, g_lastOrigin[id])
        new Float:limit = (speed + floatabs(velocity[2])) * floatmax(dt, 0.001) * 2.0 + 64.0
        if (moved > limit)
            spoil(id, "teleport")
        new movetype = pev(id, pev_movetype)
        if (movetype == MOVETYPE_FLY)
            spoil(id, "ladder")
        else if (movetype != MOVETYPE_WALK)
            spoil(id, "noclip")
        if (pev(id, pev_waterlevel) >= 2)
            spoil(id, "water")
        if (ground)
            land(id, origin, flags, friction)
        else
        {
            g_airFrames[id]++
            if (speed > g_lastSpeed[id] + 0.0001)
                g_gainFrames[id]++
            new key = strafe_key(pev(id, pev_button))
            if (key && key != g_lastKey[id])
            {
                g_strafes[id]++
                g_lastKey[id] = key
            }
            g_lastSpeed[id] = speed
        }
    }
    g_lastOrigin[id] = origin
    g_lastTime[id] = now
    return FMRES_IGNORED
}

Float:feet_z(Float:z, bool:ducked)
{
    return ducked ? z - 18.0 : z - 36.0
}

take_off(id, Float:upward, Float:friction)
{
    g_inAir[id] = true
    g_jumped[id] = upward > 0.0
    g_valid[id] = true
    g_badReason[id][0] = EOS
    if (pev(id, pev_movetype) != MOVETYPE_WALK)
        spoil(id, "noclip")
    if (friction < 1.0)
        spoil(id, "ice")
    g_takeoff[id] = g_groundOrigin[id]
    g_takeoffFeet[id] = feet_z(g_groundOrigin[id][2], g_groundDuck[id])
    g_pre[id] = g_groundSpeed[id]
    g_airFrames[id] = 0
    g_gainFrames[id] = 0
    g_strafes[id] = 0
    g_lastKey[id] = 0
}

land(id, const Float:origin[3], flags, Float:friction)
{
    g_inAir[id] = false
    g_groundOrigin[id] = origin
    g_groundDuck[id] = (flags & FL_DUCKING) != 0
    if (!g_jumped[id])
        return
    if (friction < 1.0)
        spoil(id, "ice")
    new Float:dx = origin[0] - g_takeoff[id][0], Float:dy = origin[1] - g_takeoff[id][1]
    new Float:distance = floatsqroot(dx * dx + dy * dy) + HULL_WIDTH
    new Float:dz = feet_z(origin[2], g_groundDuck[id]) - g_takeoffFeet[id]
    if (distance < float(g_min))
        return
    new name[MAX_NAME_LENGTH]
    get_user_name(id, name, charsmax(name))
    if (!g_valid[id] || floatabs(dz) > SAME_HEIGHT)
    {
        if (g_valid[id])
            copy(g_badReason[id], charsmax(g_badReason[]), "height")
        client_print_color(id, print_team_red, "%L", id, "LJS_INVALID", distance, g_badReason[id])
        log_amx("%s: invalid jump %.2f units (%s)", name, distance, g_badReason[id])
        return
    }
    new sync = g_airFrames[id] ? (g_gainFrames[id] * 100) / g_airFrames[id] : 0
    g_haveLast[id] = true
    g_lastDist[id] = distance
    g_lastPre[id] = g_pre[id]
    g_lastSync[id] = sync
    g_lastStrafes[id] = g_strafes[id]
    g_jumps[id]++

    new bool:record = sync >= g_minSync && distance > g_best[id]
    if (record)
    {
        g_best[id] = distance
        save_best(id, distance, g_pre[id], sync)
        client_print_color(id, print_team_default, "%L", id, "LJS_RECORD", distance, g_pre[id], sync, g_strafes[id])
    }
    else if (sync < g_minSync)
        client_print_color(id, print_team_default, "%L", id, "LJS_LOW_SYNC", distance, g_pre[id], sync, g_strafes[id], g_minSync)
    else
        client_print_color(id, print_team_default, "%L", id, "LJS_JUMP", distance, g_pre[id], sync, g_strafes[id])
    log_amx("%s: long jump %.2f units, pre %.0f, sync %d%%, %d strafes, record %d", name, distance, g_pre[id], sync, g_strafes[id], record ? 1 : 0)
}

/* ------------------------------------------------------------ records and the top */

clean_name(const name[], out[], len)
{
    copy(out, len, name)
    replace_all(out, len, "|", "_")
    replace_all(out, len, ";", "_")
    replace_all(out, len, "<", "_")
    replace_all(out, len, ">", "_")
    replace_all(out, len, "&", "_")
}

save_best(id, Float:distance, Float:pre, sync)
{
    new key[KEY_LEN], name[32], raw[32]
    player_key(id, key, charsmax(key))
    get_user_name(id, raw, charsmax(raw))
    clean_name(raw, name, charsmax(name))
    if (g_vault != INVALID_HANDLE)
    {
        new vkey[KEY_LEN + 4], value[64]
        formatex(vkey, charsmax(vkey), "pb:%s", key)
        formatex(value, charsmax(value), "%.2f %.0f %d", distance, pre, sync)
        nvault_set(g_vault, vkey, value)
    }
    /* Put the player's entry in place: remove the old one, then insert in order. */
    for (new i = 0; i < g_topCount; i++)
    {
        if (equal(g_topKey[i], key))
        {
            for (new k = i; k < g_topCount - 1; k++)
                copy_entry(k, k + 1)
            g_topCount--
            break
        }
    }
    new place = g_topCount
    for (new i = 0; i < g_topCount; i++)
    {
        if (distance > g_topDist[i])
        {
            place = i
            break
        }
    }
    if (place >= MAX_TOP)
        return
    new last = min(g_topCount, MAX_TOP - 1)
    for (new k = last; k > place; k--)
        copy_entry(k, k - 1)
    copy(g_topKey[place], KEY_LEN - 1, key)
    copy(g_topName[place], 31, name)
    g_topDist[place] = distance
    g_topPre[place] = pre
    g_topSync[place] = sync
    g_topCount = min(g_topCount + 1, MAX_TOP)
    save_top()
}

copy_entry(to, from)
{
    copy(g_topKey[to], KEY_LEN - 1, g_topKey[from])
    copy(g_topName[to], 31, g_topName[from])
    g_topDist[to] = g_topDist[from]
    g_topPre[to] = g_topPre[from]
    g_topSync[to] = g_topSync[from]
}

save_top()
{
    if (g_vault == INVALID_HANDLE)
        return
    new data[MAX_TOP * 96], entry[96]
    for (new i = 0; i < g_topCount; i++)
    {
        formatex(entry, charsmax(entry), "%s|%s|%.2f|%.0f|%d;", g_topKey[i], g_topName[i], g_topDist[i], g_topPre[i], g_topSync[i])
        add(data, charsmax(data), entry)
    }
    nvault_set(g_vault, "top", data)
}

load_top()
{
    g_topCount = 0
    if (g_vault == INVALID_HANDLE)
        return
    new data[MAX_TOP * 96]
    if (!nvault_get(g_vault, "top", data, charsmax(data)))
        return
    new entry[96], rest[MAX_TOP * 96], part[5][KEY_LEN]
    while (data[0] && g_topCount < MAX_TOP)
    {
        strtok2(data, entry, charsmax(entry), rest, charsmax(rest), ';')
        copy(data, charsmax(data), rest)
        if (!entry[0])
            continue
        new n = explode_string(entry, "|", part, 5, KEY_LEN - 1)
        if (n != 5)
            continue
        copy(g_topKey[g_topCount], KEY_LEN - 1, part[0])
        copy(g_topName[g_topCount], 31, part[1])
        g_topDist[g_topCount] = str_to_float(part[2])
        g_topPre[g_topCount] = str_to_float(part[3])
        g_topSync[g_topCount] = str_to_num(part[4])
        g_topCount++
    }
}

/* ------------------------------------------------------------ commands */

public cmd_stats(id)
{
    if (!is_user_connected(id))
        return PLUGIN_HANDLED
    new name[MAX_NAME_LENGTH]
    get_user_name(id, name, charsmax(name))
    if (!g_haveLast[id])
    {
        client_print_color(id, print_team_default, "%L", id, "LJS_NO_DATA")
        log_amx("/ljstats for %s: no data yet", name)
        return PLUGIN_HANDLED
    }
    client_print_color(id, print_team_default, "%L", id, "LJS_STATS", g_lastDist[id], g_lastPre[id], g_lastSync[id],
        g_lastStrafes[id], g_best[id], g_jumps[id])
    log_amx("/ljstats for %s: last %.2f (pre %.0f, sync %d%%), best %.2f, jumps %d", name, g_lastDist[id], g_lastPre[id],
        g_lastSync[id], g_best[id], g_jumps[id])
    return PLUGIN_HANDLED
}

public cmd_top(id)
{
    if (!is_user_connected(id))
        return PLUGIN_HANDLED
    new motd[2048], len, shown = min(g_topCount, clamp(g_topSize, 1, MAX_TOP))
    len = formatex(motd, charsmax(motd), "<html><body bgcolor=#111111 text=#dddddd><h3>Long jump top %d</h3><table cellpadding=3>", shown)
    len += formatex(motd[len], charsmax(motd) - len, "<tr><th>#</th><th>Player</th><th>Distance</th><th>Pre</th><th>Sync</th></tr>")
    for (new i = 0; i < shown; i++)
        len += formatex(motd[len], charsmax(motd) - len, "<tr><td>%d</td><td>%s</td><td>%.2f</td><td>%.0f</td><td>%d%%</td></tr>",
            i + 1, g_topName[i], g_topDist[i], g_topPre[i], g_topSync[i])
    if (!shown)
        len += formatex(motd[len], charsmax(motd) - len, "<tr><td colspan=5>No records yet.</td></tr>")
    formatex(motd[len], charsmax(motd) - len, "</table></body></html>")
    show_motd(id, motd, "Long jump top")
    if (shown)
        log_amx("/ljtop: 1. %s %.2f", g_topName[0], g_topDist[0])
    else
        log_amx("/ljtop: no records yet")
    return PLUGIN_HANDLED
}

public cmd_admin_top(id, level, cid)
{
    if (!cmd_access(id, level, cid, 1))
        return PLUGIN_HANDLED
    new shown = min(g_topCount, clamp(g_topSize, 1, MAX_TOP))
    console_print(id, "Long jump top %d (of %d):", shown, g_topCount)
    for (new i = 0; i < shown; i++)
        console_print(id, "  %d. %s %.2f units, pre %.0f, sync %d%%", i + 1, g_topName[i], g_topDist[i], g_topPre[i], g_topSync[i])
    return PLUGIN_HANDLED
}

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.