Skip to content
View in the app

A better way to browse. Learn more.

Learn more

assist-chain/addons/amxmodx/scripting/olds_assistchain.sma

olds_assistchain.sma
274 lines · 8.4 KB · Pawn (AMX Mod X)
Raw
/*
 * Assist Chain (olds.ro workshop card AMX-052)
 *
 * SoccerJam: when a goal is confirmed, chat names the scorer and the last two
 * different teammates who passed the ball on the way:
 *     Goal: Charlie, assist Bravo, Alpha
 *
 * The SoccerJam cores (Soccer Jam 2.07a, SoccerJam+ 2.x) have no touch or
 * goal forwards for other plugins, so this plugin reads the same things the
 * core itself uses:
 *   - possession: the ball entity ("PwnBall") touching a player;
 *   - a confirmed goal: the core raising a team's score in the TeamScore
 *     message (it does that only after it has accepted the goal);
 *   - a ball reset: the ball jumping to another spot faster than it can fly
 *     (the core moves it back to the middle or to a team's side).
 *
 * Rules
 *   - Touching the ball again yourself is one possession, not a pass.
 *   - A teammate taking the ball makes the previous holder a passer; each team
 *     keeps its two latest distinct passers, newest first.
 *   - An opponent touching the ball clears the other team's chain; a goal or a
 *     ball reset clears both.
 *   - A pass older than olds_assistchain_win seconds at the goal does not count.
 *   - The scorer is the last player of the scoring team to touch the ball; an
 *     own goal (the chain was cleared) announces nothing.
 *
 * Cvar: olds_assistchain_win 8 (1-60). Command: olds_assistchain_status (ADMIN_CFG).
 *
 * Copyright (C) 2026 daemon (olds.ro)
 * SPDX-License-Identifier: GPL-3.0-or-later
 */
#include <amxmodx>
#include <amxmisc>
#include <engine>

#define PLUGIN  "Assist Chain"
#define VERSION "1.0.0"
#define AUTHOR  "olds.ro"

const TEAM_T = 1
const TEAM_CT = 2
const PASSERS = 2
const MAX_BALLS = 8
const TASK_BALLS = 5211
const Float:BALL_CHECK = 0.2
const Float:RESET_SLACK = 120.0

new const BALL_CLASS[] = "PwnBall"

new g_holder[3]                     // current holder per team (player id), 0 = none
new g_passer[3][PASSERS]            // newest first
new Float:g_passTime[3][PASSERS]
new g_score[3] = { -1, -1, -1 }     // last score seen per team, -1 = not seen yet

new g_ball[MAX_BALLS]
new Float:g_ballOrigin[MAX_BALLS][3]
new g_ballCount

new g_window

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

    bind_pcvar_num(create_cvar("olds_assistchain_win", "8", FCVAR_NONE,
        "A pass older than this many seconds at the goal is not an assist (1-60).", true, 1.0, true, 60.0), g_window)
    AutoExecConfig(true, "olds_assistchain")

    register_concmd("olds_assistchain_status", "cmd_status", ADMIN_CFG, "- current holders and passers")

    register_touch(BALL_CLASS, "player", "on_ball_touch")
    register_event("TeamScore", "on_team_score", "a")
    register_logevent("on_round_start", 2, "1=Round_Start")
    set_task(BALL_CHECK, "task_check_balls", TASK_BALLS, _, _, "b")
}

/* ---------- possession ---------- */

public on_ball_touch(ball, id)
{
    if (!(1 <= id <= MaxClients) || !is_user_alive(id))
        return PLUGIN_CONTINUE
    new team = get_user_team(id)
    if (team != TEAM_T && team != TEAM_CT)
        return PLUGIN_CONTINUE

    // The other team loses its chain the moment we touch the ball.
    clear_team(team == TEAM_T ? TEAM_CT : TEAM_T)

    new holder = g_holder[team]
    if (holder == id)
        return PLUGIN_CONTINUE          // the same possession

    if (holder && is_user_connected(holder))
        add_passer(team, holder)
    g_holder[team] = id
    return PLUGIN_CONTINUE
}

add_passer(team, id)
{
    // Distinct passers: an earlier pass by the same player moves to the front.
    new keep[PASSERS], Float:keepTime[PASSERS], count
    for (new i = 0; i < PASSERS && count < PASSERS - 1; i++)
    {
        if (g_passer[team][i] && g_passer[team][i] != id)
        {
            keep[count] = g_passer[team][i]
            keepTime[count] = g_passTime[team][i]
            count++
        }
    }
    g_passer[team][0] = id
    g_passTime[team][0] = get_gametime()
    for (new i = 0; i < PASSERS - 1; i++)
    {
        g_passer[team][i + 1] = i < count ? keep[i] : 0
        g_passTime[team][i + 1] = i < count ? keepTime[i] : 0.0
    }
}

clear_team(team)
{
    g_holder[team] = 0
    for (new i = 0; i < PASSERS; i++)
    {
        g_passer[team][i] = 0
        g_passTime[team][i] = 0.0
    }
}

clear_all()
{
    clear_team(TEAM_T)
    clear_team(TEAM_CT)
}

/* ---------- goals ---------- */

/* TeamScore: "TERRORIST"/"CT", score. A rise is a goal the core has accepted. */
public on_team_score()
{
    new name[16]
    read_data(1, name, charsmax(name))
    new team = equal(name, "TERRORIST") ? TEAM_T : (equal(name, "CT") ? TEAM_CT : 0)
    if (!team)
        return
    new score = read_data(2)
    new previous = g_score[team]
    g_score[team] = score
    if (previous < 0 || score <= previous)
        return

    announce(team)
    clear_all()
}

announce(team)
{
    new scorer = g_holder[team]
    if (!scorer || !is_user_connected(scorer))
        return

    new names[2 * MAX_NAME_LENGTH + 8], name[MAX_NAME_LENGTH], count, length
    new Float:now = get_gametime()
    for (new i = 0; i < PASSERS; i++)
    {
        new passer = g_passer[team][i]
        if (!passer || passer == scorer || !is_user_connected(passer) || now - g_passTime[team][i] > float(g_window))
            continue
        get_user_name(passer, name, charsmax(name))
        length += formatex(names[length], charsmax(names) - length, "%s%s", count ? ", " : "", name)
        count++
    }

    get_user_name(scorer, name, charsmax(name))
    if (count)
        client_print_color(0, scorer, "^4[Goal]^1 %L", LANG_PLAYER, "ASSIST_GOAL_WITH", name, names)
    else
        client_print_color(0, scorer, "^4[Goal]^1 %L", LANG_PLAYER, "ASSIST_GOAL_ALONE", name)
}

/* ---------- ball resets ---------- */

public task_check_balls()
{
    // Track every ball; one that jumps further than its speed allows was reset by the core.
    new seen[MAX_BALLS], seenCount, ball = MaxClients, Float:origin[3], Float:velocity[3]
    while (seenCount < MAX_BALLS && (ball = find_ent_by_class(ball, BALL_CLASS)) > 0)
    {
        entity_get_vector(ball, EV_VEC_origin, origin)
        entity_get_vector(ball, EV_VEC_velocity, velocity)
        new slot = ball_slot(ball)
        if (slot >= 0)
        {
            new Float:moved = get_distance_f(origin, g_ballOrigin[slot])
            if (moved > vector_length(velocity) * BALL_CHECK * 2.0 + RESET_SLACK)
                clear_all()
        }
        else if (g_ballCount < MAX_BALLS)
        {
            slot = g_ballCount++
            g_ball[slot] = ball
        }
        if (slot >= 0)
            g_ballOrigin[slot] = origin
        seen[seenCount++] = ball
    }

    // Forget balls that are gone.
    for (new i = g_ballCount - 1; i >= 0; i--)
    {
        new bool:found
        for (new j = 0; j < seenCount; j++)
            if (seen[j] == g_ball[i])
                found = true
        if (!found)
        {
            g_ball[i] = g_ball[g_ballCount - 1]
            g_ballOrigin[i] = g_ballOrigin[g_ballCount - 1]
            g_ballCount--
        }
    }
}

ball_slot(ball)
{
    for (new i = 0; i < g_ballCount; i++)
        if (g_ball[i] == ball)
            return i
    return -1
}

public on_round_start()
{
    clear_all()
}

public client_disconnected(id)
{
    for (new team = TEAM_T; team <= TEAM_CT; team++)
    {
        if (g_holder[team] == id)
            g_holder[team] = 0
        for (new i = 0; i < PASSERS; i++)
            if (g_passer[team][i] == id)
                g_passer[team][i] = 0
    }
}

public cmd_status(id, level, cid)
{
    if (!cmd_access(id, level, cid, 1))
        return PLUGIN_HANDLED
    console_print(id, "[Assist] %d ball(s); window %d s; scores T %d, CT %d", g_ballCount, g_window, g_score[TEAM_T],
        g_score[TEAM_CT])
    new name[MAX_NAME_LENGTH], Float:now = get_gametime()
    for (new team = TEAM_T; team <= TEAM_CT; team++)
    {
        copy(name, charsmax(name), "-")
        if (g_holder[team] && is_user_connected(g_holder[team]))
            get_user_name(g_holder[team], name, charsmax(name))
        console_print(id, "[Assist] %s holder: %s", team == TEAM_T ? "T" : "CT", name)
        for (new i = 0; i < PASSERS; i++)
        {
            if (!g_passer[team][i] || !is_user_connected(g_passer[team][i]))
                continue
            get_user_name(g_passer[team][i], name, charsmax(name))
            console_print(id, "[Assist]   passer %d: %s, %.1f s ago", i + 1, name, now - g_passTime[team][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.