Skip to content
View in the app

A better way to browse. Learn more.

Learn more

field-goal-attempt/addons/amxmodx/scripting/olds_fieldgoal.sma

olds_fieldgoal.sma
491 lines · 15.1 KB · Pawn (AMX Mod X)
Raw
/*
 * Field Goal Attempt (olds.ro workshop idea IDEA-D7B6EED2E5A96F34)
 *
 * American Football add-on: a carrier within olds_fg_range of the opponents' goal box may
 * try a field goal instead of running. /kick starts a power bar of olds_fg_time seconds
 * during which he cannot move; a hit cancels it ("kick blocked") and he keeps the ball.
 * When the bar is full the ball flies on an arc toward the point he aims at, with up to
 * olds_fg_spread degrees of random error. If it enters the goal box the kicking team
 * scores olds_fg_points (fewer than a touchdown) through the core, which restarts play as
 * after a touchdown; otherwise the ball stays loose.
 *
 *   say /kick                                   try a field goal (carrier)
 *   olds_fg_setbox <t|ct>                       (ADMIN_CFG) first use: corner 1 where you
 *                                               stand; second use: corner 2, then saved
 *   olds_fg_setbox <t|ct> x1 y1 z1 x2 y2 z2     (ADMIN_CFG) the box from coordinates
 * The box of a team is the goal that team defends.
 *
 * Needs the football core (library "olds_afootball", include/olds_afootball.inc):
 * olds_af_get_carrier, olds_af_drop_ball, olds_af_add_score and the forward
 * olds_af_ball_pickup. Without the core the plugin loads and stays idle.
 *
 * Cvars: olds_fg_range 1200, olds_fg_points 3, olds_fg_time 2.0, olds_fg_spread 6.
 *
 * Copyright (C) 2026 daemon (olds.ro)
 * SPDX-License-Identifier: GPL-3.0-or-later
 */
#include <amxmodx>
#include <amxmisc>
#include <fakemeta>
#include <reapi>
#include <olds_afootball>

#define PLUGIN  "Field Goal Attempt"
#define VERSION "1.0.0"
#define AUTHOR  "olds.ro"

#define TASK_BAR    47600    // + kicker id
#define TASK_WATCH  47640
#define SND_GOOD    "buttons/bell1.wav"

const Float:WATCH_STEP = 0.05;

new const BOX_WORD[2][] = { "t", "ct" };

new Float:g_range, g_points, Float:g_time, Float:g_spread;
new bool:g_core;

/* Goal boxes: index 0 = defended by the Terrorists, 1 = by the CTs. */
new Float:g_boxMin[2][3], Float:g_boxMax[2][3];
new bool:g_haveBox[2];
new Float:g_corner[MAX_PLAYERS + 1][2][3];     // olds_fg_setbox in two steps
new bool:g_haveCorner[MAX_PLAYERS + 1][2];
new g_mapFile[160];

new bool:g_kicking[MAX_PLAYERS + 1];
new Float:g_nextUse[MAX_PLAYERS + 1];

new bool:g_inFlight;
new g_flightBall, g_kicker, g_kickTeam, g_targetBox;
new Float:g_flightEnd;

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_GOOD);
}

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

    bind_pcvar_float(create_cvar("olds_fg_range", "1200", FCVAR_NONE,
        "Greatest distance to the goal box for /kick", true, 100.0, true, 8192.0), g_range);
    bind_pcvar_num(create_cvar("olds_fg_points", "3", FCVAR_NONE,
        "Points of a field goal", true, 1.0, true, 100.0), g_points);
    bind_pcvar_float(create_cvar("olds_fg_time", "2.0", FCVAR_NONE,
        "Seconds of the power bar", true, 0.5, true, 10.0), g_time);
    bind_pcvar_float(create_cvar("olds_fg_spread", "6", FCVAR_NONE,
        "Greatest random error of the kick in degrees", true, 0.0, true, 45.0), g_spread);
    AutoExecConfig(true, "olds_fieldgoal");

    register_clcmd("say /kick", "cmd_kick");
    register_clcmd("say_team /kick", "cmd_kick");
    register_concmd("olds_fg_setbox", "cmd_setbox", ADMIN_CFG,
        "<t|ct> [x1 y1 z1 x2 y2 z2] - set the goal box a team defends");

    RegisterHookChain(RG_CBasePlayer_TakeDamage, "on_take_damage", true);
    RegisterHookChain(RG_CBasePlayer_ResetMaxSpeed, "on_reset_maxspeed", true);
    RegisterHookChain(RG_CSGameRules_RestartRound, "on_restart_round", true);
    RegisterHookChain(RG_CBasePlayer_Killed, "on_killed", true);
}

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

    new map[64];
    get_mapname(map, charsmax(map));
    get_configsdir(g_mapFile, charsmax(g_mapFile));
    add(g_mapFile, charsmax(g_mapFile), "/olds_fieldgoal");
    if (!dir_exists(g_mapFile))
        mkdir(g_mapFile);
    format(g_mapFile, charsmax(g_mapFile), "%s/%s.ini", g_mapFile, map);
    load_boxes();
}

/* ---------- goal boxes ---------- */

load_boxes()
{
    g_haveBox[0] = false;
    g_haveBox[1] = false;
    new fp = fopen(g_mapFile, "rt");
    if (!fp)
        return;
    new line[160], parts[7][16], box;
    while (fgets(fp, line, charsmax(line)))
    {
        trim(line);
        if (!line[0] || line[0] == ';' || line[0] == '#')
            continue;
        if (parse(line, parts[0], charsmax(parts[]), parts[1], charsmax(parts[]), parts[2], charsmax(parts[]),
            parts[3], charsmax(parts[]), parts[4], charsmax(parts[]), parts[5], charsmax(parts[]),
            parts[6], charsmax(parts[])) < 7)
            continue;
        box = team_box(parts[0]);
        if (box < 0)
            continue;
        new Float:a[3], Float:b[3];
        for (new k = 0; k < 3; k++)
        {
            a[k] = str_to_float(parts[1 + k]);
            b[k] = str_to_float(parts[4 + k]);
        }
        set_box(box, a, b);
    }
    fclose(fp);
}

bool:save_boxes()
{
    new fp = fopen(g_mapFile, "wt");
    if (!fp)
        return false;
    fputs(fp, "; Field Goal Attempt: goal boxes (the goal each team defends), two corners each^n");
    for (new box = 0; box < 2; box++)
    {
        if (!g_haveBox[box])
            continue;
        fprintf(fp, "%s %.0f %.0f %.0f %.0f %.0f %.0f^n", BOX_WORD[box],
            g_boxMin[box][0], g_boxMin[box][1], g_boxMin[box][2],
            g_boxMax[box][0], g_boxMax[box][1], g_boxMax[box][2]);
    }
    fclose(fp);
    return true;
}

team_box(const word[])
{
    if (equali(word, "t"))
        return 0;
    if (equali(word, "ct"))
        return 1;
    return -1;
}

set_box(box, const Float:a[3], const Float:b[3])
{
    for (new k = 0; k < 3; k++)
    {
        g_boxMin[box][k] = floatmin(a[k], b[k]);
        g_boxMax[box][k] = floatmax(a[k], b[k]);
    }
    g_haveBox[box] = true;
}

public cmd_setbox(id, level, cid)
{
    if (!cmd_access(id, level, cid, 2))
        return PLUGIN_HANDLED;
    new word[8];
    read_argv(1, word, charsmax(word));
    new box = team_box(word);
    if (box < 0)
    {
        console_print(id, "Usage: olds_fg_setbox <t|ct> [x1 y1 z1 x2 y2 z2]");
        return PLUGIN_HANDLED;
    }

    if (read_argc() >= 8)
    {
        new value[16], Float:a[3], Float:b[3];
        for (new k = 0; k < 3; k++)
        {
            read_argv(2 + k, value, charsmax(value));
            a[k] = str_to_float(value);
            read_argv(5 + k, value, charsmax(value));
            b[k] = str_to_float(value);
        }
        set_box(box, a, b);
    }
    else
    {
        if (!id || !is_user_alive(id))
        {
            console_print(id, "Stand at a corner (alive) or give the coordinates.");
            return PLUGIN_HANDLED;
        }
        new step = g_haveCorner[id][0] ? 1 : 0;
        pev(id, pev_origin, g_corner[id][step]);
        g_haveCorner[id][step] = true;
        if (!step)
        {
            console_print(id, "Corner 1 of the %s box set; go to the opposite corner and run it again.", word);
            return PLUGIN_HANDLED;
        }
        set_box(box, g_corner[id][0], g_corner[id][1]);
        g_haveCorner[id][0] = false;
        g_haveCorner[id][1] = false;
    }
    if (save_boxes())
        console_print(id, "Goal box %s saved: %.0f %.0f %.0f - %.0f %.0f %.0f", word,
            g_boxMin[box][0], g_boxMin[box][1], g_boxMin[box][2],
            g_boxMax[box][0], g_boxMax[box][1], g_boxMax[box][2]);
    else
        console_print(id, "Cannot write %s", g_mapFile);
    return PLUGIN_HANDLED;
}

bool:inside_box(box, const Float:origin[3])
{
    for (new k = 0; k < 3; k++)
        if (origin[k] < g_boxMin[box][k] || origin[k] > g_boxMax[box][k])
            return false;
    return true;
}

/* The box the player's team kicks at (the one the other team defends), -1 = none. */
enemy_box(id)
{
    new TeamName:team = get_member(id, m_iTeam);
    if (team == TEAM_TERRORIST)
        return g_haveBox[1] ? 1 : -1;
    if (team == TEAM_CT)
        return g_haveBox[0] ? 0 : -1;
    return -1;
}

/* ---------- the attempt ---------- */

public cmd_kick(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;

    if (!g_core || !is_user_alive(id) || olds_af_get_carrier() != id)
    {
        client_print_color(id, print_team_default, "^4[Football]^1 %L", id, "FG_CARRIER");
        return PLUGIN_HANDLED;
    }
    if (g_kicking[id] || g_inFlight)
        return PLUGIN_HANDLED;
    new box = enemy_box(id);
    if (box < 0)
    {
        client_print_color(id, print_team_default, "^4[Football]^1 %L", id, "FG_NO_BOX");
        return PLUGIN_HANDLED;
    }

    new Float:origin[3], Float:center[3];
    pev(id, pev_origin, origin);
    for (new k = 0; k < 3; k++)
        center[k] = (g_boxMin[box][k] + g_boxMax[box][k]) * 0.5;
    new Float:distance = get_distance_f(origin, center);
    if (distance > g_range)
    {
        client_print_color(id, print_team_default, "^4[Football]^1 %L", id, "FG_TOO_FAR",
            floatround(distance - g_range, floatround_ceil));
        return PLUGIN_HANDLED;
    }

    g_kicking[id] = true;
    set_entvar(id, var_velocity, Float:{0.0, 0.0, 0.0});
    rg_reset_maxspeed(id);
    rg_send_bartime(id, floatround(g_time, floatround_ceil), false);
    set_task(g_time, "task_bar_done", TASK_BAR + id);
    client_print_color(id, print_team_default, "^4[Football]^1 %L", id, "FG_SETUP");
    return PLUGIN_HANDLED;
}

public on_reset_maxspeed(id)
{
    if (g_kicking[id])
        set_entvar(id, var_maxspeed, 1.0);
    return HC_CONTINUE;
}

/* Ends the power bar without a kick. */
stop_kick(id)
{
    if (!g_kicking[id])
        return;
    g_kicking[id] = false;
    remove_task(TASK_BAR + id);
    if (is_user_connected(id))
    {
        rg_send_bartime(id, 0, false);
        if (is_user_alive(id))
            rg_reset_maxspeed(id);
    }
}

public on_take_damage(victim, inflictor, attacker, Float:damage, damage_bits)
{
    if (!(1 <= victim <= MaxClients) || !g_kicking[victim] || damage <= 0.0)
        return HC_CONTINUE;
    stop_kick(victim);
    new name[MAX_NAME_LENGTH];
    if (1 <= attacker <= MaxClients && attacker != victim && is_user_connected(attacker))
        get_user_name(attacker, name, charsmax(name));
    else
        copy(name, charsmax(name), "?");
    client_print_color(0, victim, "^4[Football]^1 %l", "FG_BLOCKED", name);
    return HC_CONTINUE;
}

public on_killed(victim)
{
    stop_kick(victim);
    return HC_CONTINUE;
}

public client_disconnected(id)
{
    stop_kick(id);
    g_haveCorner[id][0] = false;
    g_haveCorner[id][1] = false;
    g_nextUse[id] = 0.0;
    if (id == g_kicker)
        g_kicker = 0;
}

public on_restart_round()
{
    for (new id = 1; id <= MaxClients; id++)
        stop_kick(id);
    end_flight();
}

public task_bar_done(task)
{
    new id = task - TASK_BAR;
    if (!g_kicking[id])
        return;
    stop_kick(id);
    if (!is_user_alive(id) || olds_af_get_carrier() != id || g_inFlight)
        return;
    new box = enemy_box(id);
    if (box < 0)
        return;

    // The aim with a random error, then the point it hits.
    new Float:angles[3], Float:dir[3], Float:eye[3], Float:view_ofs[3], Float:end[3];
    pev(id, pev_v_angle, angles);
    angles[0] += random_float(-g_spread, g_spread);
    angles[1] += random_float(-g_spread, g_spread);
    engfunc(EngFunc_MakeVectors, angles);
    global_get(glb_v_forward, dir);
    pev(id, pev_origin, eye);
    pev(id, pev_view_ofs, view_ofs);
    for (new k = 0; k < 3; k++)
    {
        eye[k] += view_ofs[k];
        end[k] = eye[k] + dir[k] * 4096.0;
    }
    engfunc(EngFunc_TraceLine, eye, end, IGNORE_MONSTERS, id, 0);
    new Float:target[3];
    get_tr2(0, TR_vecEndPos, target);

    new ball = olds_af_drop_ball();
    if (ball <= 0 || !pev_valid(ball))
        return;

    // An arc that reaches the target point after a flight time based on the distance.
    new Float:start[3];
    pev(ball, pev_origin, start);
    new Float:dx = target[0] - start[0], Float:dy = target[1] - start[1], Float:dz = target[2] - start[2];
    new Float:flat = floatsqroot(dx * dx + dy * dy);
    new Float:flight = floatmin(floatmax(flat / 600.0, 0.6), 2.5);
    new Float:gravity = get_cvar_float("sv_gravity");
    new Float:ball_gravity;
    pev(ball, pev_gravity, ball_gravity);
    if (ball_gravity > 0.0)
        gravity *= ball_gravity;
    new Float:velocity[3];
    velocity[0] = dx / flight;
    velocity[1] = dy / flight;
    velocity[2] = (dz + 0.5 * gravity * flight * flight) / flight;
    set_pev(ball, pev_movetype, MOVETYPE_TOSS);
    set_pev(ball, pev_flags, pev(ball, pev_flags) & ~FL_ONGROUND);
    set_pev(ball, pev_velocity, velocity);

    g_inFlight = true;
    g_flightBall = ball;
    g_kicker = id;
    g_kickTeam = _:get_member(id, m_iTeam);
    g_targetBox = box;
    g_flightEnd = get_gametime() + flight + 2.0;
    set_task(WATCH_STEP, "task_watch", TASK_WATCH, _, _, "b");

    new name[MAX_NAME_LENGTH];
    get_user_name(id, name, charsmax(name));
    client_print_color(0, id, "^4[Football]^1 %l", "FG_KICK", name, floatround(flat));
}

/* Follows the ball: in the box = good; on the ground (or too long) elsewhere = no good. */
public task_watch()
{
    if (!g_inFlight || !pev_valid(g_flightBall))
    {
        end_flight();
        return;
    }
    new Float:origin[3];
    pev(g_flightBall, pev_origin, origin);
    if (inside_box(g_targetBox, origin))
    {
        good();
        return;
    }
    new Float:velocity[3];
    pev(g_flightBall, pev_velocity, velocity);
    new bool:landed = (pev(g_flightBall, pev_flags) & FL_ONGROUND) != 0
        || (velocity[0] == 0.0 && velocity[1] == 0.0 && velocity[2] == 0.0);
    if (landed || get_gametime() > g_flightEnd)
    {
        end_flight();
        client_print_color(0, print_team_default, "^4[Football]^1 %l", "FG_MISS");
        log_amx("Field goal missed (landed at %.0f %.0f %.0f)", origin[0], origin[1], origin[2]);
    }
}

good()
{
    new team = g_kickTeam, kicker = g_kicker;
    end_flight();
    new name[MAX_NAME_LENGTH];
    if (kicker && is_user_connected(kicker))
        get_user_name(kicker, name, charsmax(name));
    else
        copy(name, charsmax(name), "?");
    client_print_color(0, print_team_default, "^4[Football]^1 %l", "FG_GOOD", name, g_points);
    client_cmd(0, "spk %s", SND_GOOD);
    log_amx("Field goal by %s: %d points for team %d", name, g_points, team);
    olds_af_add_score(team, g_points, "field goal");
}

end_flight()
{
    remove_task(TASK_WATCH);
    g_inFlight = false;
    g_flightBall = 0;
    g_kicker = 0;
}

/* No pick-up while the kick is in the air. */
public olds_af_ball_pickup(id, ball)
{
    if (g_inFlight && ball == g_flightBall)
        return PLUGIN_HANDLED;
    return PLUGIN_CONTINUE;
}

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.