Skip to content
View in the app

A better way to browse. Learn more.

Learn more

first-touch-sheet/addons/amxmodx/scripting/olds_firsttouch.sma

olds_firsttouch.sma
208 lines · 6.5 KB · Pawn (AMX Mod X)
Raw
/*
 * First Touch Sheet (olds.ro workshop card AMX-053)
 *
 * SoccerJam practice: counts each player's first touches (taking possession
 * of the ball) made in the air and on the ground. /touches shows your sheet:
 *     First touches: 3 in the air, 7 on the ground (30% airborne)
 *
 *   say /touches    your sheet
 *
 * Needs: AMX Mod X 1.10, fakemeta, and SoccerJam+ (library "soccerjam",
 * sj_get_gamemode()). The core has no possession forward. It plays its
 * "got ball" sound on the ball (an entity of class PwnBall) at every new
 * possession, and only then: carrying the ball does not play it again. The
 * plugin hooks that sound (EngFunc_EmitSound) as the confirmed possession.
 * The new holder is the nearest living player touching distance from the
 * ball; their FL_ONGROUND flag at that moment decides air or ground. The
 * same player taking the same ball again within one second is not counted
 * twice.
 *
 * Practice only: with olds_firsttouch_practice 1, touches count only while
 * sj_get_gamemode() reports the pre-game (practice) mode; a match counts
 * nothing. Tallies live in memory and are cleared when the player leaves,
 * when a match starts (the mode turns to the game mode) and on a full
 * restart (sv_restart).
 *
 * Cvars: olds_firsttouch_practice 1; olds_firsttouch_sound "kickball/gotball.wav"
 * (the core's possession sound). Command: olds_firsttouch_status (ADMIN_CFG).
 *
 * Copyright (C) 2026 daemon (olds.ro)
 * SPDX-License-Identifier: GPL-3.0-or-later
 */
#include <amxmodx>
#include <amxmisc>
#include <fakemeta>
#include <reapi>

#define PLUGIN  "First Touch Sheet"
#define VERSION "1.0.0"
#define AUTHOR  "olds.ro"

/* SoccerJam+ API, declared here so the plugin compiles without its sources. */
#pragma reqlib "soccerjam"
native sj_get_gamemode()

const SJ_MODE_PREGAME = 1
const SJ_MODE_GAME = 2
const TASK_MODE = 5311
const Float:TOUCH_RANGE = 96.0
const Float:SAME_TOUCH = 1.0

new g_air[MAX_PLAYERS + 1]
new g_ground[MAX_PLAYERS + 1]
new g_lastBall[MAX_PLAYERS + 1]
new Float:g_lastTime[MAX_PLAYERS + 1]
new Float:g_nextUse[MAX_PLAYERS + 1]
new g_lastMode = -1

new g_practiceOnly
new g_sound[64]

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

    bind_pcvar_num(create_cvar("olds_firsttouch_practice", "1", FCVAR_NONE,
        "1 = count first touches only in SoccerJam's pre-game (practice) mode.", true, 0.0, true, 1.0), g_practiceOnly)
    bind_pcvar_string(create_cvar("olds_firsttouch_sound", "kickball/gotball.wav", FCVAR_NONE,
        "The sound SoccerJam plays on the ball at a new possession."), g_sound, charsmax(g_sound))
    AutoExecConfig(true, "olds_firsttouch")

    register_clcmd("say /touches", "cmd_touches")
    register_clcmd("say_team /touches", "cmd_touches")
    register_concmd("olds_firsttouch_status", "cmd_status", ADMIN_CFG, "- every player's first-touch sheet")

    register_forward(FM_EmitSound, "on_emit_sound", false)
    RegisterHookChain(RG_CSGameRules_RestartRound, "on_restart_round_pre", false)
    set_task(1.0, "task_mode", TASK_MODE, _, _, "b")
}

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

public on_emit_sound(entity, channel, const sample[], Float:volume, Float:attenuation, flags, pitch)
{
    if (entity <= MaxClients || !pev_valid(entity) || !g_sound[0] || !equal(sample, g_sound))
        return FMRES_IGNORED
    new classname[16]
    pev(entity, pev_classname, classname, charsmax(classname))
    if (!equal(classname, "PwnBall"))
        return FMRES_IGNORED
    if (g_practiceOnly && sj_get_gamemode() != SJ_MODE_PREGAME)
        return FMRES_IGNORED

    new holder = nearest_player(entity)
    if (!holder)
        return FMRES_IGNORED

    // The same player taking the same ball again at once is one touch.
    new Float:now = get_gametime()
    if (g_lastBall[holder] == entity && now - g_lastTime[holder] < SAME_TOUCH)
        return FMRES_IGNORED
    g_lastBall[holder] = entity
    g_lastTime[holder] = now

    if (pev(holder, pev_flags) & FL_ONGROUND)
        g_ground[holder]++
    else
        g_air[holder]++
    return FMRES_IGNORED
}

nearest_player(ball)
{
    new Float:ballOrigin[3], Float:origin[3], best, Float:bestDistance = TOUCH_RANGE
    pev(ball, pev_origin, ballOrigin)
    for (new id = 1; id <= MaxClients; id++)
    {
        if (!is_user_alive(id))
            continue
        pev(id, pev_origin, origin)
        new Float:distance = get_distance_f(origin, ballOrigin)
        if (distance <= bestDistance)
        {
            best = id
            bestDistance = distance
        }
    }
    return best
}

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

clear(id)
{
    g_air[id] = 0
    g_ground[id] = 0
    g_lastBall[id] = 0
    g_lastTime[id] = 0.0
}

clear_all()
{
    for (new id = 1; id <= MaxClients; id++)
        clear(id)
}

/* A match starting (the mode turning to the game mode) clears every sheet. */
public task_mode()
{
    new mode = sj_get_gamemode()
    if (mode != g_lastMode)
    {
        if (mode == SJ_MODE_GAME && g_lastMode != -1)
            clear_all()
        g_lastMode = mode
    }
}

public on_restart_round_pre()
{
    if (get_member_game(m_bCompleteReset))
        clear_all()
    return HC_CONTINUE
}

public client_disconnected(id)
{
    clear(id)
    g_nextUse[id] = 0.0
}

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

public cmd_touches(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 + 1.0
    new total = g_air[id] + g_ground[id]
    if (!total)
        client_print_color(id, print_team_default, "^4[Touches]^1 %L", id, "TOUCH_NONE")
    else
        client_print_color(id, print_team_default, "^4[Touches]^1 %L", id, "TOUCH_SHEET", g_air[id], g_ground[id],
            g_air[id] * 100 / total)
    if (g_practiceOnly && sj_get_gamemode() != SJ_MODE_PREGAME)
        client_print_color(id, print_team_default, "^4[Touches]^1 %L", id, "TOUCH_MATCH")
    return PLUGIN_HANDLED
}

public cmd_status(id, level, cid)
{
    if (!cmd_access(id, level, cid, 1))
        return PLUGIN_HANDLED
    console_print(id, "[Touches] SoccerJam mode %d; practice only %d; sound ^"%s^"", sj_get_gamemode(), g_practiceOnly,
        g_sound)
    new players[MAX_PLAYERS], count, name[MAX_NAME_LENGTH]
    get_players(players, count, "h")
    for (new i = 0; i < count; i++)
    {
        new p = players[i]
        get_user_name(p, name, charsmax(name))
        console_print(id, "[Touches]   %s: %d air, %d ground", name, g_air[p], g_ground[p])
    }
    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.