Skip to content
View in the app

A better way to browse. Learn more.

Learn more

offside-penalty-flag/addons/amxmodx/scripting/olds_offside.sma

olds_offside.sma
211 lines · 6.7 KB · Pawn (AMX Mod X)
Raw
/*
 * Offside Penalty Flag (olds.ro)
 * Copyright (C) 2026 daemon (olds.ro), written by daemon's plugin agent
 *
 * 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/>.
 *
 * American Football: nobody may cross the line of scrimmage before the snap. The football
 * core reports the line with olds_af_ball_spot (which starts the pre-snap phase) and the snap
 * with olds_af_snap. Before the snap every player's field-axis coordinate is checked ten times
 * a second (RG_CBasePlayer_PreThink); the first player on the wrong side of the line gets a
 * yellow flag dropped where he crossed (rg_create_entity info_target, the stock weaponbox
 * model with a yellow glow shell, removed after 6 s), a whistle, the play whistled dead
 * (olds_af_whistle) and the ball moved olds_offside_yards toward his team's end zone
 * (olds_af_move_spot). The olds_offside_warn-th offence of one player in a half prints a
 * warning line; counters reset at half-time (olds_af_halftime). Without a football core the
 * plugin stays idle. olds_offside 0 turns detection off.
 *
 * Cvars: olds_offside 1, olds_offside_warn 3, olds_offside_yards 5
 */

#include <amxmodx>
#include <amxmisc>
#include <fakemeta>
#include <reapi>
#include <olds_afootball>

#pragma semicolon 0

#define PLUGIN  "Offside Penalty Flag"
#define VERSION "1.0.0"
#define AUTHOR  "daemon (olds.ro)"

#define FLAG_CLASS "olds_offside_flag"
#define FLAG_LIFE  6.0
#define CHECK_STEP 0.1

new const FLAG_MODEL[] = "models/w_weaponbox.mdl"
new const SND_WHISTLE[] = "buttons/blip1.wav"
new const TEAM_TEXT[3][] = { "", "Terrorists", "Counter-Terrorists" }

new g_enable
new g_warn
new g_yards

new bool:g_preSnap
new g_axis
new Float:g_line
new g_tSide
new Float:g_nextCheck[MAX_PLAYERS + 1]
new g_offences[MAX_PLAYERS + 1]
new bool:g_core

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_model(FLAG_MODEL)
    precache_sound(SND_WHISTLE)
}

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

    bind_pcvar_num(create_cvar("olds_offside", "1", FCVAR_NONE,
        "Offside detection before the snap", true, 0.0, true, 1.0), g_enable)
    bind_pcvar_num(create_cvar("olds_offside_warn", "3", FCVAR_NONE,
        "Offences in one half that give a warning line", true, 1.0, true, 20.0), g_warn)
    bind_pcvar_num(create_cvar("olds_offside_yards", "5", FCVAR_NONE,
        "Yards the ball moves for an offside", true, 1.0, true, 50.0), g_yards)
    AutoExecConfig(true, "olds_offside")

    RegisterHookChain(RG_CBasePlayer_PreThink, "on_prethink", false)
}

public plugin_cfg()
{
    g_core = LibraryExists("olds_afootball", LibType_Library) != 0
    if (!g_core)
        log_amx("No football core (library olds_afootball): offside detection stays idle")
}

/* ------------------------------------------------------------ the core's forwards */

public olds_af_ball_spot(axis, Float:line, t_side)
{
    if ((axis != 0 && axis != 1) || (t_side != 1 && t_side != -1))
    {
        log_amx("Ignored a ball spot with axis %d, side %d", axis, t_side)
        g_preSnap = false
        return
    }
    g_axis = axis
    g_line = line
    g_tSide = t_side
    g_preSnap = true
    for (new id = 1; id <= MaxClients; id++)
        g_nextCheck[id] = 0.0
}

public olds_af_snap()
{
    g_preSnap = false
}

public olds_af_halftime()
{
    for (new id = 1; id <= MaxClients; id++)
        g_offences[id] = 0
    log_amx("Half-time: offside counters reset")
}

/* ------------------------------------------------------------ detection */

public on_prethink(id)
{
    if (!g_preSnap || !g_enable || !g_core)
        return HC_CONTINUE
    new Float:now = get_gametime()
    if (now < g_nextCheck[id])
        return HC_CONTINUE
    g_nextCheck[id] = now + CHECK_STEP
    if (!is_user_alive(id))
        return HC_CONTINUE
    new team = get_member(id, m_iTeam)
    if (team != 1 && team != 2)
        return HC_CONTINUE
    new Float:origin[3]
    get_entvar(id, var_origin, origin)
    /* Which way this player's own side lies from the line. */
    new side = (team == 1) ? g_tSide : -g_tSide
    new Float:past = (g_line - origin[g_axis]) * float(side)
    if (past > 0.0)
        offside(id, team, origin)
    return HC_CONTINUE
}

offside(id, team, const Float:origin[3])
{
    g_preSnap = false
    g_offences[id]++
    new Float:spot[3]
    spot = origin
    spot[g_axis] = g_line
    drop_flag(spot)
    emit_sound(id, CHAN_AUTO, SND_WHISTLE, VOL_NORM, ATTN_NORM, 0, PITCH_NORM)

    new name[MAX_NAME_LENGTH]
    get_user_name(id, name, charsmax(name))
    log_amx("Offside: %s (%s) crossed the line at %.0f; flag at %.0f %.0f %.0f; offence %d this half",
        name, TEAM_TEXT[team], g_line, spot[0], spot[1], spot[2], g_offences[id])
    client_print_color(0, print_team_default, "%L", LANG_PLAYER, "OFFSIDE_CALL", name, g_yards)
    olds_af_whistle("offside")
    olds_af_move_spot(team, clamp(g_yards, 1, 50))
    if (g_offences[id] == clamp(g_warn, 1, 20))
    {
        client_print_color(0, print_team_red, "%L", LANG_PLAYER, "OFFSIDE_WARN", name, g_offences[id])
        log_amx("Offside warning for %s (%d offences this half)", name, g_offences[id])
    }
}

drop_flag(const Float:spot[3])
{
    new ent = rg_create_entity("info_target")
    if (!ent)
        return
    set_entvar(ent, var_classname, FLAG_CLASS)
    engfunc(EngFunc_SetModel, ent, FLAG_MODEL)
    engfunc(EngFunc_SetSize, ent, Float:{-4.0, -4.0, 0.0}, Float:{4.0, 4.0, 8.0})
    set_entvar(ent, var_solid, SOLID_NOT)
    set_entvar(ent, var_movetype, MOVETYPE_TOSS)
    set_entvar(ent, var_renderfx, kRenderFxGlowShell)
    set_entvar(ent, var_rendercolor, Float:{255.0, 220.0, 0.0})
    set_entvar(ent, var_renderamt, 16.0)
    engfunc(EngFunc_SetOrigin, ent, spot)
    engfunc(EngFunc_DropToFloor, ent)
    set_task(FLAG_LIFE, "task_remove_flag", ent)
}

public task_remove_flag(ent)
{
    if (!is_entity(ent))
        return
    new cls[24]
    get_entvar(ent, var_classname, cls, charsmax(cls))
    if (equal(cls, FLAG_CLASS))
        set_entvar(ent, var_flags, get_entvar(ent, var_flags) | FL_KILLME)
}

public client_disconnected(id)
{
    g_offences[id] = 0
}

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.