Skip to content
View in the app

A better way to browse. Learn more.

Learn more

soccer-anti-abuse-fixes/addons/amxmodx/scripting/olds_soccerfix.sma

olds_soccerfix.sma
470 lines · 14.7 KB · Pawn (AMX Mod X)
Raw
/*
 * Soccer Anti-Abuse Fixes (olds.ro workshop card LIB-2054)
 * Copyright (C) 2026 daemon (olds.ro)
 *
 * 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/>.
 *
 * For SoccerJam style mods (ball classname "PwnBall" by default):
 *  - goal camping: a player inside olds_sa_goalbox units of a goal for olds_sa_camptime
 *    seconds is put back out (engfunc EngFunc_SetOrigin) and warned once a round;
 *  - ball hogging: more than olds_sa_maxtouch touches in a row by one player are ignored
 *    (the ball Touch is blocked) with a notice;
 *  - stuck ball: a ball that has not moved for olds_sa_stuck seconds is put back at the
 *    centre with no velocity;
 *  - kick-off: damage between players is blocked for olds_sa_kickoff seconds after the
 *    whistle (end of the freeze time, or the ball put back at the centre).
 * Goals and the centre come from configs/olds_sa_<map>.ini, else from the map's
 * soccerjam_goalnet and soccerjam_ballspawn entities. Actions go to logs/olds_sa.log.
 */
#include <amxmodx>
#include <amxmisc>
#include <engine>
#include <fakemeta>
#include <reapi>

#define PLUGIN  "Soccer Anti-Abuse Fixes"
#define VERSION "1.0.0"
#define AUTHOR  "daemon (olds.ro)"

#define MAX_GOALS   4
#define MAX_CLASSES 4
#define TASK_CAMP   20540
#define TASK_CHECK  20580
#define LOG_FILE    "olds_sa.log"
#define TOUCH_GAP   0.25
#define STREAK_IDLE 3.0
#define WARN_SOUND  "buttons/button10"

new g_maxTouch, Float:g_goalBox, Float:g_campTime, Float:g_stuckTime, Float:g_kickoff
new g_classText[96], g_classes[MAX_CLASSES][32], g_classCount

new Float:g_goal[MAX_GOALS][3], g_goals
new Float:g_centre[3], bool:g_haveCentre

new g_ball
new Float:g_lastPos[3], Float:g_stillSince
new g_lastToucher, g_streak, Float:g_lastTouch, bool:g_streakNoticed
new g_blocked
new Float:g_kickoffUntil
new bool:g_inGoal[MAX_PLAYERS + 1], bool:g_warned[MAX_PLAYERS + 1], g_pushes[MAX_PLAYERS + 1]

public plugin_init()
{
    register_plugin(PLUGIN, VERSION, AUTHOR)
    register_dictionary("olds_soccerfix.txt")
    bind_pcvar_num(create_cvar("olds_sa_maxtouch", "3", FCVAR_NONE, "Touches in a row one player may make", true, 1.0, true, 50.0), g_maxTouch)
    bind_pcvar_float(create_cvar("olds_sa_goalbox", "200", FCVAR_NONE, "Radius around a goal that counts as camping", true, 16.0, true, 2000.0), g_goalBox)
    bind_pcvar_float(create_cvar("olds_sa_camptime", "8", FCVAR_NONE, "Seconds a player may stay in a goal box", true, 1.0, true, 120.0), g_campTime)
    bind_pcvar_float(create_cvar("olds_sa_stuck", "6", FCVAR_NONE, "Seconds a still ball waits before going back to the centre", true, 1.0, true, 120.0), g_stuckTime)
    bind_pcvar_float(create_cvar("olds_sa_kickoff", "3", FCVAR_NONE, "Seconds of no damage after the kick-off whistle", true, 0.0, true, 30.0), g_kickoff)
    bind_pcvar_string(create_cvar("olds_sa_ball", "PwnBall", FCVAR_NONE, "Ball classnames, separated by spaces"), g_classText, charsmax(g_classText))
    AutoExecConfig(true, "olds_soccerfix")

    register_concmd("amx_sa_status", "cmd_status", ADMIN_KICK, "- shows the soccer anti-abuse state")
    RegisterHookChain(RG_CSGameRules_OnRoundFreezeEnd, "on_freeze_end_post", true)
    RegisterHookChain(RG_CBasePlayer_TakeDamage, "on_damage_pre", false)
}

public plugin_cfg()
{
    new rest[96], piece[32], left[96]
    copy(rest, charsmax(rest), g_classText)
    while (rest[0] && g_classCount < MAX_CLASSES)
    {
        strtok2(rest, piece, charsmax(piece), left, charsmax(left), ' ', TRIM_FULL)
        copy(rest, charsmax(rest), left)
        if (!piece[0])
            continue
        copy(g_classes[g_classCount++], 31, piece)
        register_touch(piece, "player", "on_ball_touch")
    }
    load_places()
    set_task(0.5, "check", TASK_CHECK, _, _, "b")
}

/* ------------------------------------------------------------------ goals and centre */

bool:is_number_text(const text[])
{
    new i, digits
    if (text[0] == '-' || text[0] == '+')
        i = 1
    for (; text[i]; i++)
    {
        if (isdigit(text[i]))
            digits++
        else if (text[i] != '.')
            return false
    }
    return digits > 0 && floatabs(str_to_float(text)) <= 16384.0
}

bool:read_vec(const a[], const b[], const c[], Float:out[3])
{
    if (!is_number_text(a) || !is_number_text(b) || !is_number_text(c))
        return false
    out[0] = str_to_float(a)
    out[1] = str_to_float(b)
    out[2] = str_to_float(c)
    return true
}

load_places()
{
    new path[160], map[32]
    get_configsdir(path, charsmax(path))
    get_mapname(map, charsmax(map))
    format(path, charsmax(path), "%s/olds_sa_%s.ini", path, map)
    new file = fopen(path, "rt")
    if (file)
    {
        new line[128], kind[8], a[16], b[16], c[16], row
        while (!feof(file))
        {
            fgets(file, line, charsmax(line))
            row++
            trim(line)
            if (!line[0] || line[0] == ';' || line[0] == '/')
                continue
            new n = parse(line, kind, charsmax(kind), a, charsmax(a), b, charsmax(b), c, charsmax(c))
            new Float:v[3]
            if (n != 4 || !read_vec(a, b, c, v))
            {
                log_amx("%s line %d is malformed, skipped", path, row)
                continue
            }
            if (equali(kind, "goal") && g_goals < MAX_GOALS)
            {
                g_goal[g_goals] = v
                g_goals++
            }
            else if (equali(kind, "centre") || equali(kind, "center"))
            {
                g_centre = v
                g_haveCentre = true
            }
            else
                log_amx("%s line %d: unknown entry, skipped", path, row)
        }
        fclose(file)
    }
    /* The map's own entities fill what the file left out. */
    new ent = -1
    while (!g_goals && (ent = find_ent_by_class(ent, "soccerjam_goalnet")) > 0)
    {
        new Float:mins[3], Float:maxs[3]
        get_entvar(ent, var_absmin, mins)
        get_entvar(ent, var_absmax, maxs)
        for (new k = 0; k < 3; k++)
            g_goal[g_goals][k] = (mins[k] + maxs[k]) * 0.5
        if (++g_goals >= MAX_GOALS)
            break
    }
    if (!g_haveCentre)
    {
        ent = find_ent_by_class(-1, "soccerjam_ballspawn")
        if (ent > 0)
        {
            get_entvar(ent, var_origin, g_centre)
            g_haveCentre = true
        }
    }
}

/* ------------------------------------------------------------------ the ball */

bool:is_ball_class(const cls[])
{
    for (new i = 0; i < g_classCount; i++)
        if (equal(cls, g_classes[i]))
            return true
    return false
}

find_ball()
{
    if (g_ball > 0 && is_valid_ent(g_ball))
    {
        new cls[32]
        entity_get_string(g_ball, EV_SZ_classname, cls, charsmax(cls))
        if (is_ball_class(cls))
            return g_ball
    }
    g_ball = 0
    for (new i = 0; i < g_classCount && !g_ball; i++)
    {
        new ent = find_ent_by_class(-1, g_classes[i])
        if (ent > 0)
            g_ball = ent
    }
    if (g_ball)
    {
        get_entvar(g_ball, var_origin, g_lastPos)
        g_stillSince = get_gametime()
        if (!g_haveCentre)
        {
            g_centre = g_lastPos
            g_haveCentre = true
        }
    }
    return g_ball
}

public on_ball_touch(ball, id)
{
    if (id < 1 || id > MaxClients || !is_user_alive(id))
        return PLUGIN_CONTINUE
    new Float:now = get_gametime()
    if (id != g_lastToucher || now - g_lastTouch > STREAK_IDLE)
    {
        g_lastToucher = id
        g_streak = 1
        g_streakNoticed = false
        g_lastTouch = now
        return PLUGIN_CONTINUE
    }
    /* The same contact goes on for several frames: one touch per TOUCH_GAP. */
    if (now - g_lastTouch < TOUCH_GAP)
    {
        if (g_streak > g_maxTouch)
            return PLUGIN_HANDLED
        return PLUGIN_CONTINUE
    }
    g_lastTouch = now
    g_streak++
    if (g_streak <= g_maxTouch)
        return PLUGIN_CONTINUE
    g_blocked++
    if (!g_streakNoticed)
    {
        g_streakNoticed = true
        client_print(id, print_center, "%L", id, "SA_TOUCH", g_maxTouch)
        client_cmd(id, "spk %s", WARN_SOUND)
        new name[MAX_NAME_LENGTH], steam[40]
        get_user_name(id, name, charsmax(name))
        get_user_authid(id, steam, charsmax(steam))
        log_to_file(LOG_FILE, "%s <%s>: touch %d in a row ignored", name, steam, g_streak)
    }
    return PLUGIN_HANDLED
}

return_ball(const reason[])
{
    if (!g_ball || !g_haveCentre)
        return
    new Float:zero[3]
    engfunc(EngFunc_SetOrigin, g_ball, g_centre)
    set_entvar(g_ball, var_velocity, zero)
    set_entvar(g_ball, var_avelocity, zero)
    g_lastPos = g_centre
    g_stillSince = get_gametime()
    client_print(0, print_chat, "%L", LANG_PLAYER, "SA_BALL_BACK")
    log_to_file(LOG_FILE, "Ball put back at the centre (%s)", reason)
    start_kickoff()
}

/* ------------------------------------------------------------------ the 0.5 s check */

public check()
{
    new Float:now = get_gametime()
    if (find_ball())
    {
        new Float:pos[3], Float:vel[3]
        get_entvar(g_ball, var_origin, pos)
        get_entvar(g_ball, var_velocity, vel)
        new owner = get_entvar(g_ball, var_aiment)
        if (get_distance_f(pos, g_lastPos) > 8.0 || vector_length(vel) > 5.0 || (owner >= 1 && owner <= MaxClients))
        {
            /* A jump straight back to the centre means the mod restarted play. */
            if (g_haveCentre && get_distance_f(g_lastPos, pos) > 256.0 && get_distance_f(pos, g_centre) < 32.0)
                start_kickoff()
            g_lastPos = pos
            g_stillSince = now
        }
        else if (now - g_stillSince >= g_stuckTime)
        {
            if (g_haveCentre && get_distance_f(pos, g_centre) > 64.0)
                return_ball("not moved")
            else
                g_stillSince = now
        }
    }
    if (!g_goals)
        return
    new Float:o[3]
    for (new id = 1; id <= MaxClients; id++)
    {
        if (!is_user_alive(id))
        {
            leave_goal(id)
            continue
        }
        get_entvar(id, var_origin, o)
        if (nearest_goal(o) >= 0)
        {
            if (!g_inGoal[id])
            {
                g_inGoal[id] = true
                set_task(g_campTime, "camp_expired", TASK_CAMP + id)
            }
        }
        else
            leave_goal(id)
    }
}

nearest_goal(const Float:o[3])
{
    for (new g = 0; g < g_goals; g++)
        if (get_distance_f(o, g_goal[g]) <= g_goalBox)
            return g
    return -1
}

leave_goal(id)
{
    if (!g_inGoal[id])
        return
    g_inGoal[id] = false
    remove_task(TASK_CAMP + id)
}

/* Out of the goal box along the line from the goal to the centre (or straight up the box). */
bool:push_out(id, const Float:from[3], g)
{
    new Float:dir[3]
    if (g_haveCentre)
    {
        for (new k = 0; k < 2; k++)
            dir[k] = g_centre[k] - g_goal[g][k]
    }
    else
    {
        for (new k = 0; k < 2; k++)
            dir[k] = from[k] - g_goal[g][k]
    }
    new Float:len = floatsqroot(dir[0] * dir[0] + dir[1] * dir[1])
    if (len < 1.0)
    {
        dir[0] = 1.0
        dir[1] = 0.0
        len = 1.0
    }
    dir[0] /= len
    dir[1] /= len
    new hull = HULL_HUMAN
    if (get_entvar(id, var_flags) & FL_DUCKING)
        hull = HULL_HEAD
    new Float:spot[3], Float:zero[3]
    for (new step = 0; step < 6; step++)
    {
        new Float:dist = g_goalBox + 48.0 + float(step) * 32.0
        spot[0] = g_goal[g][0] + dir[0] * dist
        spot[1] = g_goal[g][1] + dir[1] * dist
        spot[2] = from[2]
        if (trace_hull(spot, hull, id, 0) & 3)
            continue
        engfunc(EngFunc_SetOrigin, id, spot)
        set_entvar(id, var_velocity, zero)
        return true
    }
    return false
}

public camp_expired(task)
{
    new id = task - TASK_CAMP
    g_inGoal[id] = false
    if (!is_user_alive(id))
        return
    new Float:o[3]
    get_entvar(id, var_origin, o)
    new g = nearest_goal(o)
    if (g < 0)
        return
    if (!push_out(id, o, g))
    {
        log_to_file(LOG_FILE, "No free place to put a goal camper (#%d) out", id)
        return
    }
    g_pushes[id]++
    new name[MAX_NAME_LENGTH], steam[40]
    get_user_name(id, name, charsmax(name))
    get_user_authid(id, steam, charsmax(steam))
    log_to_file(LOG_FILE, "%s <%s>: put out of goal box %d after %.0f s", name, steam, g + 1, g_campTime)
    if (!g_warned[id])
    {
        g_warned[id] = true
        set_hudmessage(255, 80, 40, -1.0, 0.3, 0, 0.0, 4.0, 0.1, 0.5, -1)
        show_hudmessage(id, "%L", id, "SA_CAMP", floatround(g_campTime))
        client_cmd(id, "spk %s", WARN_SOUND)
    }
}

/* ------------------------------------------------------------------ kick-off */

start_kickoff()
{
    g_kickoffUntil = get_gametime() + g_kickoff
}

public on_freeze_end_post()
{
    start_kickoff()
    g_lastToucher = 0
    g_streak = 0
    for (new id = 1; id <= MaxClients; id++)
        g_warned[id] = false
}

public on_damage_pre(victim, inflictor, attacker, Float:damage, bits)
{
    if (get_gametime() >= g_kickoffUntil || attacker < 1 || attacker > MaxClients || attacker == victim)
        return HC_CONTINUE
    SetHookChainReturn(ATYPE_INTEGER, 0)
    return HC_SUPERCEDE
}

public client_disconnected(id)
{
    remove_task(TASK_CAMP + id)
    g_inGoal[id] = false
    g_warned[id] = false
    g_pushes[id] = 0
    if (g_lastToucher == id)
    {
        g_lastToucher = 0
        g_streak = 0
    }
}

/* ------------------------------------------------------------------ admin */

public cmd_status(id, level, cid)
{
    if (!cmd_access(id, level, cid, 1))
        return PLUGIN_HANDLED
    new Float:pos[3]
    if (g_ball)
        get_entvar(g_ball, var_origin, pos)
    console_print(id, "[SoccerFix] ball #%d at %.0f %.0f %.0f, goals %d, centre %d (%.0f %.0f %.0f), streak %d by #%d, blocked touches %d, kick-off guard %.1f s",
        g_ball, pos[0], pos[1], pos[2], g_goals, g_haveCentre, g_centre[0], g_centre[1], g_centre[2], g_streak, g_lastToucher, g_blocked,
        floatmax(g_kickoffUntil - get_gametime(), 0.0))
    new name[MAX_NAME_LENGTH], Float:o[3]
    for (new p = 1; p <= MaxClients; p++)
    {
        if (!is_user_connected(p))
            continue
        get_user_name(p, name, charsmax(name))
        get_entvar(p, var_origin, o)
        console_print(id, "[SoccerFix]   %s: health %d, in goal box %d, put out %d, warned %d, at %.0f %.0f %.0f", name, get_user_health(p),
            g_inGoal[p], g_pushes[p], g_warned[p], o[0], o[1], o[2])
    }
    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.