Skip to content
View in the app

A better way to browse. Learn more.

Learn more

forward-pass-throw/addons/amxmodx/scripting/olds_pass.sma

olds_pass.sma
423 lines · 12.5 KB · Pawn (AMX Mod X)
Raw
/*
 * Forward Pass Throw (olds.ro workshop idea IDEA-EAFC63FE55C1F017)
 *
 * American Football add-on: the ball carrier holds +attack2 to charge a throw and
 * releases it to throw the ball along his crosshair. A teammate who touches the ball in
 * flight catches it (a completed pass), an opponent who touches it first intercepts it,
 * and a ball that hits the ground untouched is an incomplete pass and stays loose. Each
 * possession allows one forward pass; the count resets when the ball changes sides.
 *
 *   say /passes   completions, attempts, incompletions and interceptions per player
 *
 * Needs the football core (library "olds_afootball", include/olds_afootball.inc):
 * olds_af_get_carrier, olds_af_drop_ball, olds_af_set_carrier and the forward
 * olds_af_ball_pickup. Without the core the plugin loads and stays idle.
 *
 * Cvars: olds_pass_enable 1, olds_pass_min 400, olds_pass_max 900, olds_pass_charge 1.5,
 *        olds_pass_cooldown 1.0.
 *
 * Copyright (C) 2026 daemon (olds.ro)
 * SPDX-License-Identifier: GPL-3.0-or-later
 */
#include <amxmodx>
#include <fakemeta>
#include <reapi>
#include <olds_afootball>

#define PLUGIN  "Forward Pass Throw"
#define VERSION "1.0.0"
#define AUTHOR  "olds.ro"

#define SND_THROW "weapons/knife_slash1.wav"

const MAX_RECORDS = 64;
const LIST_LINES = 8;
const Float:FLIGHT_MAX = 6.0;     // a throw that never lands ends after this

enum _:Stat { S_ATTEMPTS, S_COMPLETIONS, S_INCOMPLETE, S_INTERCEPTIONS };

new g_enable, Float:g_min, Float:g_max, Float:g_charge, Float:g_cooldown;
new bool:g_core;

new Float:g_chargeStart[MAX_PLAYERS + 1];     // 0 = not charging
new Float:g_nextThrow[MAX_PLAYERS + 1];
new Float:g_nextUse[MAX_PLAYERS + 1];
new Float:g_nextHint[MAX_PLAYERS + 1];

new TeamName:g_possession;       // the side that has the ball (TEAM_UNASSIGNED = nobody yet)
new bool:g_passUsed;

new bool:g_inFlight;
new g_flightBall, g_thrower, g_oldSolid;
new TeamName:g_throwerTeam;
new Float:g_throwFrom[3];
new Float:g_flightEnd;
new g_touchHook;

new g_recAuth[MAX_RECORDS][MAX_AUTHID_LENGTH];
new g_recName[MAX_RECORDS][MAX_NAME_LENGTH];
new g_recStat[MAX_RECORDS][Stat];
new g_records;

public plugin_natives()
{
    set_native_filter("native_filter");
}

public native_filter(const name[], index, trap)
{
    /* The core is optional: without it this plugin stays idle. */
    if (!trap && equal(name, "olds_af_", 8))
        return PLUGIN_HANDLED;
    return PLUGIN_CONTINUE;
}

public plugin_precache()
{
    precache_sound(SND_THROW);
}

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

    bind_pcvar_num(create_cvar("olds_pass_enable", "1", FCVAR_NONE,
        "Forward passes with +attack2", true, 0.0, true, 1.0), g_enable);
    bind_pcvar_float(create_cvar("olds_pass_min", "400", FCVAR_NONE,
        "Throw speed with no charge", true, 50.0, true, 3000.0), g_min);
    bind_pcvar_float(create_cvar("olds_pass_max", "900", FCVAR_NONE,
        "Throw speed at full charge", true, 50.0, true, 3000.0), g_max);
    bind_pcvar_float(create_cvar("olds_pass_charge", "1.5", FCVAR_NONE,
        "Seconds of +attack2 for a full charge", true, 0.1, true, 10.0), g_charge);
    bind_pcvar_float(create_cvar("olds_pass_cooldown", "1.0", FCVAR_NONE,
        "Seconds between two throws of one player", true, 0.0, true, 30.0), g_cooldown);
    AutoExecConfig(true, "olds_pass");

    register_clcmd("say /passes", "cmd_passes");
    register_clcmd("say_team /passes", "cmd_passes");
    RegisterHookChain(RG_CBasePlayer_PreThink, "on_prethink", false);
    RegisterHookChain(RG_CSGameRules_RestartRound, "on_restart_round", true);
}

public plugin_cfg()
{
    g_core = LibraryExists("olds_afootball", LibType_Library) != 0;
    if (!g_core)
        log_amx("No football core (library olds_afootball): forward passes stay off");
}

public on_restart_round()
{
    end_flight();
    g_possession = TEAM_UNASSIGNED;
    g_passUsed = false;
    for (new i = 1; i <= MaxClients; i++)
    {
        g_chargeStart[i] = 0.0;
        g_nextThrow[i] = 0.0;
    }
}

public client_disconnected(id)
{
    g_chargeStart[id] = 0.0;
    g_nextThrow[id] = 0.0;
    g_nextUse[id] = 0.0;
    g_nextHint[id] = 0.0;
    if (id == g_thrower)
        g_thrower = 0;
}

/* ---------- possession and charge ---------- */

public on_prethink(id)
{
    if (!g_core)
        return HC_CONTINUE;

    // A throw that never touched anything (fell out of the map, stuck) ends here.
    if (g_inFlight && get_gametime() > g_flightEnd)
        land(true);

    if (!is_user_alive(id) || olds_af_get_carrier() != id)
    {
        stop_charge(id);
        return HC_CONTINUE;
    }

    new TeamName:team = get_member(id, m_iTeam);
    if (team != g_possession)
    {
        // The ball changed sides: a new possession with its own pass.
        g_possession = team;
        g_passUsed = false;
    }

    if (!g_enable)
    {
        stop_charge(id);
        return HC_CONTINUE;
    }

    new buttons = pev(id, pev_button);
    new old_buttons = pev(id, pev_oldbuttons);
    new Float:now = get_gametime();

    if ((buttons & IN_ATTACK2) && !(old_buttons & IN_ATTACK2))
    {
        if (g_passUsed)
        {
            hint(id, "PASS_ONE_PER");
            return HC_CONTINUE;
        }
        if (now < g_nextThrow[id])
        {
            hint(id, "PASS_WAIT");
            return HC_CONTINUE;
        }
        g_chargeStart[id] = now;
        rg_send_bartime(id, floatround(g_charge, floatround_ceil), false);
    }
    else if (!(buttons & IN_ATTACK2) && g_chargeStart[id] > 0.0)
    {
        new Float:held = now - g_chargeStart[id];
        stop_charge(id);
        throw_ball(id, held);
    }
    return HC_CONTINUE;
}

stop_charge(id)
{
    if (g_chargeStart[id] > 0.0)
    {
        g_chargeStart[id] = 0.0;
        if (is_user_connected(id))
            rg_send_bartime(id, 0, false);
    }
}

hint(id, const key[])
{
    new Float:now = get_gametime();
    if (now < g_nextHint[id])
        return;
    g_nextHint[id] = now + 1.0;
    client_print(id, print_center, "%L", id, key);
}

/* ---------- the throw ---------- */

throw_ball(id, Float:held)
{
    if (g_inFlight || g_passUsed)
        return;
    new ball = olds_af_drop_ball();
    if (ball <= 0 || !pev_valid(ball))
        return;

    new Float:share = floatmin(floatmax(held / g_charge, 0.0), 1.0);
    new Float:speed = g_min + (floatmax(g_max, g_min) - g_min) * share;

    new Float:eye[3], Float:view_ofs[3], Float:angles[3], Float:dir[3];
    pev(id, pev_origin, eye);
    pev(id, pev_view_ofs, view_ofs);
    eye[0] += view_ofs[0];
    eye[1] += view_ofs[1];
    eye[2] += view_ofs[2];
    pev(id, pev_v_angle, angles);
    engfunc(EngFunc_MakeVectors, angles);
    global_get(glb_v_forward, dir);

    // Start in front of the thrower, but not inside a wall.
    new Float:start[3];
    for (new a = 0; a < 3; a++)
        start[a] = eye[a] + dir[a] * 24.0;
    engfunc(EngFunc_TraceLine, eye, start, IGNORE_MONSTERS, id, 0);
    new Float:fraction;
    get_tr2(0, TR_flFraction, fraction);
    if (fraction < 1.0)
        start = eye;

    new Float:velocity[3];
    for (new a = 0; a < 3; a++)
        velocity[a] = dir[a] * speed;

    g_oldSolid = pev(ball, pev_solid);
    set_pev(ball, pev_movetype, MOVETYPE_TOSS);
    set_pev(ball, pev_solid, SOLID_BBOX);
    set_pev(ball, pev_owner, id);          // the thrower does not collide with it
    engfunc(EngFunc_SetOrigin, ball, start);
    set_pev(ball, pev_velocity, velocity);
    emit_sound(id, CHAN_ITEM, SND_THROW, VOL_NORM, ATTN_NORM, 0, PITCH_NORM);

    g_inFlight = true;
    g_flightBall = ball;
    g_thrower = id;
    g_throwerTeam = get_member(id, m_iTeam);
    pev(id, pev_origin, g_throwFrom);
    g_flightEnd = get_gametime() + FLIGHT_MAX;
    g_passUsed = true;
    g_nextThrow[id] = get_gametime() + g_cooldown;
    stat_add(id, S_ATTEMPTS);
    if (!g_touchHook)
        g_touchHook = register_forward(FM_Touch, "on_touch", false);
}

public on_touch(touched, toucher)
{
    if (!g_inFlight)
        return FMRES_IGNORED;
    new other;
    if (touched == g_flightBall)
        other = toucher;
    else if (toucher == g_flightBall)
        other = touched;
    else
        return FMRES_IGNORED;

    if (1 <= other <= MaxClients)
    {
        if (other != g_thrower && is_user_alive(other))
            catch_ball(other);
        return FMRES_IGNORED;
    }
    // The world or a solid brush: the pass is incomplete. Triggers do not count.
    if (other == 0 || (pev_valid(other) && pev(other, pev_solid) == SOLID_BSP))
        land(false);
    return FMRES_IGNORED;
}

catch_ball(id)
{
    new thrower = g_thrower;
    new TeamName:team = get_member(id, m_iTeam);
    new Float:at[3];
    pev(id, pev_origin, at);
    new distance = floatround(get_distance_f(g_throwFrom, at));
    end_flight();
    if (!olds_af_set_carrier(id))
        return;

    new catcher_name[MAX_NAME_LENGTH], thrower_name[MAX_NAME_LENGTH];
    get_user_name(id, catcher_name, charsmax(catcher_name));
    if (thrower && is_user_connected(thrower))
        get_user_name(thrower, thrower_name, charsmax(thrower_name));
    else
        copy(thrower_name, charsmax(thrower_name), "?");

    if (team == g_throwerTeam)
    {
        if (thrower && is_user_connected(thrower))
            stat_add(thrower, S_COMPLETIONS);
        client_print_color(0, id, "^4[Football]^1 %l", "PASS_COMPLETE", thrower_name, catcher_name, distance);
        log_amx("Pass complete: %s to %s, %d units", thrower_name, catcher_name, distance);
    }
    else
    {
        stat_add(id, S_INTERCEPTIONS);
        client_print_color(0, id, "^4[Football]^1 %l", "PASS_INTERCEPT", catcher_name, thrower_name);
        log_amx("Interception: %s picks off %s", catcher_name, thrower_name);
    }
}

land(bool:timeout)
{
    new thrower = g_thrower;
    end_flight();
    if (thrower && is_user_connected(thrower))
    {
        stat_add(thrower, S_INCOMPLETE);
        new name[MAX_NAME_LENGTH];
        get_user_name(thrower, name, charsmax(name));
        client_print_color(0, thrower, "^4[Football]^1 %l", "PASS_INCOMPLETE", name);
    }
    if (timeout)
        log_amx("Pass ended without touching anything");
}

end_flight()
{
    if (g_inFlight && pev_valid(g_flightBall))
    {
        set_pev(g_flightBall, pev_solid, g_oldSolid);
        set_pev(g_flightBall, pev_owner, 0);
        // Relink so the restored solid type takes effect.
        new Float:origin[3];
        pev(g_flightBall, pev_origin, origin);
        engfunc(EngFunc_SetOrigin, g_flightBall, origin);
    }
    g_inFlight = false;
    g_flightBall = 0;
    g_thrower = 0;
    if (g_touchHook)
    {
        unregister_forward(FM_Touch, g_touchHook, false);
        g_touchHook = 0;
    }
}

/* The core asks before a player picks the ball up: not while a pass is in the air. */
public olds_af_ball_pickup(id, ball)
{
    if (g_inFlight && ball == g_flightBall)
        return PLUGIN_HANDLED;
    return PLUGIN_CONTINUE;
}

/* ---------- statistics ---------- */

stat_add(id, stat)
{
    new auth[MAX_AUTHID_LENGTH], name[MAX_NAME_LENGTH];
    get_user_authid(id, auth, charsmax(auth));
    get_user_name(id, name, charsmax(name));
    // Bots and LAN ids share their auth string: key them by name.
    if (is_user_bot(id) || equal(auth, "STEAM_ID_LAN") || equal(auth, "BOT") || !auth[0])
        formatex(auth, charsmax(auth), "name:%s", name);

    new rec = -1;
    for (new i = 0; i < g_records; i++)
    {
        if (equal(g_recAuth[i], auth))
        {
            rec = i;
            break;
        }
    }
    if (rec < 0)
    {
        if (g_records >= MAX_RECORDS)
            return;
        rec = g_records++;
        copy(g_recAuth[rec], charsmax(g_recAuth[]), auth);
        for (new s = 0; s < Stat; s++)
            g_recStat[rec][s] = 0;
    }
    copy(g_recName[rec], charsmax(g_recName[]), name);
    g_recStat[rec][stat]++;
}

public cmd_passes(id)
{
    if (!is_user_connected(id))
        return PLUGIN_HANDLED;
    new Float:now = get_gametime();
    if (now < g_nextUse[id])
        return PLUGIN_HANDLED;
    g_nextUse[id] = now + 2.0;

    if (!g_records)
    {
        client_print_color(id, print_team_default, "^4[Football]^1 %L", id, "PASS_NONE");
        return PLUGIN_HANDLED;
    }
    client_print_color(id, print_team_default, "^4[Football]^1 %L", id, "PASS_HEAD");
    new lines = min(g_records, LIST_LINES);
    for (new i = 0; i < lines; i++)
        client_print_color(id, print_team_default, "^4[Football]^1 %L", id, "PASS_LINE", g_recName[i],
            g_recStat[i][S_COMPLETIONS], g_recStat[i][S_ATTEMPTS], g_recStat[i][S_INCOMPLETE],
            g_recStat[i][S_INTERCEPTIONS]);
    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.