Promote server
Black Friday Ends in 50% off a Master Server place and the Client list, which comes with LIFETIME PREMIUM
03 Days 21 Hrs 42 Min 14 Sec
See the deals
csb-strafe-trainer / addons / amxmodx / scripting / csb_strafe_trainer.sma
176 lines 4.7 KiB Pawn Raw Permalink History
/*
 * CSB Strafe Trainer
 * Copyright (C) 2026 counter-strike-boost.com
 *
 * Real-time strafe analysis for KZ / surf / bhop practice. While a player who
 * enabled the trainer is airborne, every PreThink tick samples the yaw delta
 * and the strafe keys: a tick where the mouse turns left while IN_MOVELEFT is
 * held (or right + IN_MOVERIGHT) counts as synced; a tick where the mouse
 * turns without the matching key counts as wasted. Gain is measured from the
 * actual horizontal speed increase per tick. A live HUD shows the running
 * numbers and a chat summary is printed after each jump with more than a
 * minimum airtime.
 *
 * 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. It 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 <https://www.gnu.org/licenses/> for details.
 */

#include <amxmodx>
#include <fakemeta>

new const PLUGIN[] = "CSB Strafe Trainer"
new const VERSION[] = "1.0.0"
new const AUTHOR[]  = "counter-strike-boost.com"

#define HUD_CHANNEL 3

new bool:g_bOn[33]
new bool:g_bAir[33]

new Float:g_fLastYaw[33]
new Float:g_fLastSpeed[33]
new Float:g_fAirTime[33]

new g_iTicks[33]
new g_iGoodTicks[33]
new g_iWastedTicks[33]
new g_iGainTicks[33]

new g_pMinAir

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

    g_pMinAir = register_cvar("csb_strafe_minair", "0.4")

    register_forward(FM_PlayerPreThink, "fwPreThink", 0)
    register_clcmd("say /strafe", "cmdToggle")
    register_clcmd("say_team /strafe", "cmdToggle")
}

public cmdToggle(id)
{
    g_bOn[id] = !g_bOn[id]
    client_print(id, print_chat, "[CSB] Strafe trainer %s.", g_bOn[id] ? "enabled" : "disabled")

    if (!g_bOn[id])
        resetJump(id)

    return PLUGIN_HANDLED
}

public client_putinserver(id)
{
    g_bOn[id] = false
    resetJump(id)
}

resetJump(id)
{
    g_bAir[id]         = false
    g_fAirTime[id]     = 0.0
    g_iTicks[id]       = 0
    g_iGoodTicks[id]   = 0
    g_iWastedTicks[id] = 0
    g_iGainTicks[id]   = 0
}

public fwPreThink(id)
{
    if (!g_bOn[id] || !is_user_alive(id))
        return FMRES_IGNORED

    new flags = pev(id, pev_flags)

    if (flags & FL_ONGROUND)
    {
        if (g_bAir[id])
        {
            /* just landed -> summarize the jump */
            if (g_fAirTime[id] >= get_pcvar_float(g_pMinAir) && g_iTicks[id] > 0)
                summarize(id)

            resetJump(id)
        }

        return FMRES_IGNORED
    }

    /* airborne: sample this tick */
    g_bAir[id] = true
    g_fAirTime[id] += 0.01
    g_iTicks[id]++

    static Float:ang[3]
    pev(id, pev_angles, ang)

    static Float:dyaw
    dyaw = ang[1] - g_fLastYaw[id]

    while (dyaw > 180.0)
        dyaw -= 360.0
    while (dyaw < -180.0)
        dyaw += 360.0

    g_fLastYaw[id] = ang[1]

    new buttons = pev(id, pev_button)

    if (floatabs(dyaw) > 0.1)
    {
        new bool:leftTurn = (dyaw < 0.0)
        new bool:leftKey  = (buttons & IN_MOVELEFT) && !(buttons & IN_MOVERIGHT)
        new bool:rightKey = (buttons & IN_MOVERIGHT) && !(buttons & IN_MOVELEFT)

        if ((leftTurn && leftKey) || (!leftTurn && rightKey))
            g_iGoodTicks[id]++
        else
            g_iWastedTicks[id]++
    }

    /* gain: did horizontal speed grow this tick? */
    static Float:vel[3]
    pev(id, pev_velocity, vel)

    static Float:speed
    speed = floatsqroot(vel[0] * vel[0] + vel[1] * vel[1])

    if (speed > g_fLastSpeed[id] + 0.5)
        g_iGainTicks[id]++

    g_fLastSpeed[id] = speed

    /* live HUD */
    new total = g_iGoodTicks[id] + g_iWastedTicks[id]
    new sync  = total > 0 ? (100 * g_iGoodTicks[id]) / total : 100

    set_hudmessage(80, 200, 255, 0.02, 0.60, 0, 0.0, 0.15, 0.0, 0.0, HUD_CHANNEL)
    show_hudmessage(id, "sync: %d%%  gain: %d/%d  speed: %.1f",
        sync, g_iGainTicks[id], g_iTicks[id], speed)

    return FMRES_IGNORED
}

summarize(id)
{
    new total = g_iGoodTicks[id] + g_iWastedTicks[id]
    new sync  = total > 0 ? (100 * g_iGoodTicks[id]) / total : 100

    client_print(id, print_chat,
        "[CSB] Jump: %.2f s airtime, sync %d%%, gain ticks %d/%d, wasted %d.",
        g_fAirTime[id], sync, g_iGainTicks[id], g_iTicks[id], g_iWastedTicks[id])

    if (sync < 70)
    {
        client_print(id, print_chat,
            "[CSB] Tip: turn the mouse and the strafe key together - A with left, D with right.")
    }

    log_amx("[CSB Strafe] id %d air=%.2f sync=%d%% gain=%d/%d",
        id, g_fAirTime[id], sync, g_iGainTicks[id], g_iTicks[id])
}