Skip to content
View in the app

A better way to browse. Learn more.

Learn more

run-shadow-splits/addons/amxmodx/scripting/olds_shadow.sma

olds_shadow.sma
402 lines · 12.4 KB · Pawn (AMX Mod X)
Raw
/*
 * Run Shadow Splits (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/>.
 *
 * Kreedz: compares each checkpoint of a live run with the player's previous valid run of the
 * same map, route and style, and shows the difference privately as a DHUD line (-0.42 in the
 * faster colour, +0.42 in the slower one). No ghost model. KZ timers keep runs differently,
 * so the timer (or a small bridge plugin) calls the natives in include/olds_shadow.inc
 * (library "olds_shadow"): start, checkpoint, finish, reset, style.
 *
 * One reference run per SteamID, map, route and style lives in the nvault "olds_shadow",
 * with at most olds_shadow_maxcp checkpoints. A reference whose route signature differs from
 * the one the timer gives at start is deleted. Reset and style switches stop the deltas and
 * leave the reference alone.
 *
 * Cvars:  olds_shadow_maxcp 40 (1-200), olds_shadow_best 0 (1 = keep the best run instead of
 *         the latest valid one), olds_shadow_savebots 0
 * Say:    /shadow toggles the deltas for yourself
 * Admin:  amx_shadow_ref <player> (ADMIN_ADMIN) prints the player's run and reference
 */

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

#pragma semicolon 0

#define PLUGIN  "Run Shadow Splits"
#define VERSION "1.0.0"
#define AUTHOR  "daemon (olds.ro)"

#define MAX_CP     200
#define ROUTE_LEN  32
#define STYLE_LEN  16
#define SIG_LEN    32

new g_maxCp
new g_best
new g_saveBots

new g_vault = INVALID_HANDLE
new g_map[64]

new bool:g_on[MAX_PLAYERS + 1] = { true, ... }
new bool:g_running[MAX_PLAYERS + 1]
new g_route[MAX_PLAYERS + 1][ROUTE_LEN]
new g_style[MAX_PLAYERS + 1][STYLE_LEN]
new g_sig[MAX_PLAYERS + 1][SIG_LEN]
new Float:g_live[MAX_PLAYERS + 1][MAX_CP + 1]
new g_liveCount[MAX_PLAYERS + 1]
new bool:g_hasRef[MAX_PLAYERS + 1]
new Float:g_ref[MAX_PLAYERS + 1][MAX_CP + 1]
new g_refCount[MAX_PLAYERS + 1]
new Float:g_refFinal[MAX_PLAYERS + 1]
new Float:g_lastDelta[MAX_PLAYERS + 1]
new g_lastCp[MAX_PLAYERS + 1]

public plugin_natives()
{
    register_library("olds_shadow")
    register_native("olds_shadow_start", "native_start")
    register_native("olds_shadow_checkpoint", "native_checkpoint")
    register_native("olds_shadow_finish", "native_finish")
    register_native("olds_shadow_reset", "native_reset")
    register_native("olds_shadow_style", "native_style")
}

public plugin_init()
{
    register_plugin(PLUGIN, VERSION, AUTHOR)

    bind_pcvar_num(create_cvar("olds_shadow_maxcp", "40", FCVAR_NONE,
        "Most checkpoints stored in a reference run", true, 1.0, true, float(MAX_CP)), g_maxCp)
    bind_pcvar_num(create_cvar("olds_shadow_best", "0", FCVAR_NONE,
        "0 = compare with the previous valid run, 1 = with the best run", true, 0.0, true, 1.0), g_best)
    bind_pcvar_num(create_cvar("olds_shadow_savebots", "0", FCVAR_NONE,
        "1 = keep references for bots too (by name)", true, 0.0, true, 1.0), g_saveBots)

    register_clcmd("say /shadow", "cmd_toggle")
    register_clcmd("say_team /shadow", "cmd_toggle")
    register_concmd("amx_shadow_ref", "cmd_ref", ADMIN_ADMIN, "<player> - show a player's run and reference")

    register_dictionary("olds_shadow.txt")
    get_mapname(g_map, charsmax(g_map))
    strtolower(g_map)
    g_vault = nvault_open("olds_shadow")
    if (g_vault == INVALID_HANDLE)
        log_amx("Could not open the nvault olds_shadow: references are not kept")
    AutoExecConfig(true, "olds_shadow")
}

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

public client_putinserver(id)
{
    g_on[id] = true
    stop_run(id)
    g_style[id][0] = EOS
}

public client_disconnected(id)
{
    stop_run(id)
}

stop_run(id)
{
    g_running[id] = false
    g_liveCount[id] = 0
    g_hasRef[id] = false
    g_refCount[id] = 0
    g_lastCp[id] = 0
}

/* ------------------------------------------------------------ names and keys */

bool:good_name(const text[], maxLen)
{
    new len = strlen(text)
    if (len < 1 || len >= maxLen)
        return false
    for (new i = 0; i < len; i++)
    {
        if (!isalnum(text[i]) && text[i] != '-' && text[i] != '_' && text[i] != '.')
            return false
    }
    return true
}

/* nvault key for the player's current route and style, or false when they are not saved. */
bool:make_key(id, key[], len)
{
    new auth[44]
    get_user_authid(id, auth, charsmax(auth))
    if (is_user_bot(id))
    {
        if (!g_saveBots)
            return false
        new name[32], clean[32], n
        get_user_name(id, name, charsmax(name))
        for (new i = 0; name[i] && n < charsmax(clean); i++)
        {
            if (isalnum(name[i]))
                clean[n++] = name[i]
        }
        clean[n] = EOS
        formatex(auth, charsmax(auth), "BOT_%s", clean)
    }
    else if ((!equal(auth, "STEAM_", 6) && !equal(auth, "VALVE_", 6)) || containi(auth, "LAN") >= 0
        || containi(auth, "PENDING") >= 0)
        return false
    formatex(key, len, "%s|%s|%s|%s", auth, g_map, g_route[id], g_style[id])
    return true
}

/* ------------------------------------------------------------ reference runs */

load_ref(id)
{
    g_hasRef[id] = false
    g_refCount[id] = 0
    if (g_vault == INVALID_HANDLE)
        return
    new key[160], value[2200], stamp
    if (!make_key(id, key, charsmax(key)) || !nvault_lookup(g_vault, key, value, charsmax(value), stamp))
        return
    /* value: sig;final;t1 t2 ... tn */
    new sig[SIG_LEN], finalText[16], rest[2200]
    strtok2(value, sig, charsmax(sig), rest, charsmax(rest), ';')
    if (!equal(sig, g_sig[id]))
    {
        nvault_remove(g_vault, key)
        client_print_color(id, print_team_default, "%L", id, "SHADOW_ROUTE_CHANGED")
        return
    }
    copy(value, charsmax(value), rest)
    strtok2(value, finalText, charsmax(finalText), rest, charsmax(rest), ';')
    new Float:final = str_to_float(finalText)
    if (final <= 0.0)
        return
    new piece[16], count, limit = clamp(g_maxCp, 1, MAX_CP)
    while (rest[0] && count < limit)
    {
        strtok2(rest, piece, charsmax(piece), value, charsmax(value), ' ', TRIM_FULL)
        new Float:t = str_to_float(piece)
        g_ref[id][++count] = t
        copy(rest, charsmax(rest), value)
    }
    g_refCount[id] = count
    g_refFinal[id] = final
    g_hasRef[id] = true
}

bool:save_ref(id, Float:final)
{
    if (g_vault == INVALID_HANDLE)
        return false
    new key[160]
    if (!make_key(id, key, charsmax(key)))
        return false
    new value[2200], len, limit = min(g_liveCount[id], clamp(g_maxCp, 1, MAX_CP))
    len = formatex(value, charsmax(value), "%s;%.3f;", g_sig[id], final)
    for (new cp = 1; cp <= limit && len < charsmax(value) - 12; cp++)
    {
        if (cp > 1)
            value[len++] = ' '
        len += formatex(value[len], charsmax(value) - len, "%.3f", g_live[id][cp])
    }
    nvault_set(g_vault, key, value)
    /* The new run is the reference from now on. */
    for (new cp = 1; cp <= limit; cp++)
        g_ref[id][cp] = g_live[id][cp]
    g_refCount[id] = limit
    g_refFinal[id] = final
    g_hasRef[id] = true
    return true
}

show_delta(id, Float:delta, const label[])
{
    if (!g_on[id] || is_user_bot(id))
        return
    if (delta < -0.005)
        set_dhudmessage(60, 230, 90, -1.0, 0.30, 0, 0.0, 1.6, 0.05, 0.3)
    else if (delta > 0.005)
        set_dhudmessage(240, 70, 60, -1.0, 0.30, 0, 0.0, 1.6, 0.05, 0.3)
    else
        set_dhudmessage(230, 230, 230, -1.0, 0.30, 0, 0.0, 1.6, 0.05, 0.3)
    new sign[2]
    sign[0] = delta < 0.0 ? '-' : '+'
    show_dhudmessage(id, "%s %s%.2f", label, sign, floatabs(delta))
}

/* ------------------------------------------------------------ natives */

bool:native_player(id)
{
    if (id < 1 || id > MaxClients || !is_user_connected(id))
    {
        log_error(AMX_ERR_NATIVE, "Invalid player %d", id)
        return false
    }
    return true
}

public native_start(plugin, params)
{
    new id = get_param(1)
    if (!native_player(id))
        return 0
    new route[ROUTE_LEN], style[STYLE_LEN], sig[SIG_LEN]
    get_string(2, route, charsmax(route))
    get_string(3, style, charsmax(style))
    get_string(4, sig, charsmax(sig))
    if (!good_name(route, sizeof(route)) || !good_name(style, sizeof(style)) || strlen(sig) >= SIG_LEN - 1
        || contain(sig, ";") >= 0 || contain(sig, "|") >= 0)
    {
        log_error(AMX_ERR_NATIVE, "Bad route, style or signature")
        return 0
    }
    stop_run(id)
    copy(g_route[id], charsmax(g_route[]), route)
    copy(g_style[id], charsmax(g_style[]), style)
    copy(g_sig[id], charsmax(g_sig[]), sig)
    g_running[id] = true
    load_ref(id)
    return g_hasRef[id] ? 1 : 0
}

public native_checkpoint(plugin, params)
{
    new id = get_param(1)
    if (!native_player(id))
        return 0
    new cp = get_param(2)
    new Float:time = get_param_f(3)
    if (!g_running[id] || cp < 1 || cp > MAX_CP || time < 0.0)
        return 0
    if (cp <= clamp(g_maxCp, 1, MAX_CP))
    {
        g_live[id][cp] = time
        if (cp > g_liveCount[id])
            g_liveCount[id] = cp
    }
    if (!g_hasRef[id] || cp > g_refCount[id])
        return 0
    g_lastDelta[id] = time - g_ref[id][cp]
    g_lastCp[id] = cp
    new label[24]
    formatex(label, charsmax(label), "%L", id, "SHADOW_CP", cp)
    show_delta(id, g_lastDelta[id], label)
    return 1
}

public native_finish(plugin, params)
{
    new id = get_param(1)
    if (!native_player(id))
        return 0
    new Float:time = get_param_f(2)
    new valid = get_param(3)
    if (!g_running[id] || time <= 0.0)
        return 0
    g_running[id] = false
    new bool:had = g_hasRef[id]
    new Float:old = g_refFinal[id]
    if (had)
    {
        g_lastDelta[id] = time - old
        g_lastCp[id] = 0
        new label[24]
        formatex(label, charsmax(label), "%L", id, "SHADOW_FINISH")
        show_delta(id, g_lastDelta[id], label)
    }
    if (!valid)
        return 0
    if (had && g_best && time >= old)
        return 0
    if (!save_ref(id, time))
        return 0
    if (!had)
        client_print_color(id, print_team_default, "%L", id, "SHADOW_STORED", g_style[id])
    return 1
}

public native_reset(plugin, params)
{
    new id = get_param(1)
    if (!native_player(id))
        return 0
    g_running[id] = false
    g_liveCount[id] = 0
    return 1
}

public native_style(plugin, params)
{
    new id = get_param(1)
    if (!native_player(id))
        return 0
    new style[STYLE_LEN]
    get_string(2, style, charsmax(style))
    if (!good_name(style, sizeof(style)))
    {
        log_error(AMX_ERR_NATIVE, "Bad style name")
        return 0
    }
    stop_run(id)
    copy(g_style[id], charsmax(g_style[]), style)
    return 1
}

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

public cmd_toggle(id)
{
    g_on[id] = !g_on[id]
    if (g_on[id])
        client_print_color(id, print_team_default, "%L", id, "SHADOW_ON")
    else
        client_print_color(id, print_team_default, "%L", id, "SHADOW_OFF")
    return PLUGIN_HANDLED
}

public cmd_ref(id, level, cid)
{
    if (!cmd_access(id, level, cid, 2))
        return PLUGIN_HANDLED
    new arg[32]
    read_argv(1, arg, charsmax(arg))
    new player = cmd_target(id, arg, CMDTARGET_ALLOW_SELF)
    if (!player)
        return PLUGIN_HANDLED
    new name[32]
    get_user_name(player, name, charsmax(name))
    console_print(id, "%L", id, "SHADOW_REF_HEAD", name, g_route[player][0] ? 1 : 0, g_running[player] ? 1 : 0,
        g_on[player] ? 1 : 0, g_liveCount[player])
    console_print(id, "  route %s, style %s, signature %s", g_route[player], g_style[player], g_sig[player])
    if (g_hasRef[player])
    {
        console_print(id, "%L", id, "SHADOW_REF_RUN", g_refCount[player], g_refFinal[player])
        for (new cp = 1; cp <= g_refCount[player]; cp++)
            console_print(id, "    cp %d: %.3f", cp, g_ref[player][cp])
    }
    else
        console_print(id, "%L", id, "SHADOW_REF_NONE")
    if (g_lastCp[player] > 0)
        console_print(id, "%L", id, "SHADOW_REF_LAST", g_lastCp[player], g_lastDelta[player])
    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.