Skip to content
View in the app

A better way to browse. Learn more.

Learn more

capture-the-awp/addons/amxmodx/scripting/olds_cawp.sma

olds_cawp.sma
527 lines · 16.3 KB · Pawn (AMX Mod X)
Raw
/*
 * Capture the AWP (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. It is distributed WITHOUT ANY WARRANTY; see
 * <https://www.gnu.org/licenses/>.
 *
 * One AWP is the capture item. It lies at the start point of
 * configs/olds_cawp_<map>.ini (the stock w_awp model with a glow and a
 * laserbeam locator); a player who touches it (Ham_Touch) carries it as a
 * real weapon_awp, glows in their team colour and both teams are told.
 * Carrying it into their own team's goal zone (origin + radius) scores a
 * capture and sends the AWP back to the start; olds_cawp_score captures
 * win the round. Dropped (death, the drop key, leaving at the last
 * position), it lies there until someone picks it up - a teammate or an
 * enemy - or returns to the start after olds_cawp_return seconds untouched.
 *
 * olds_cawp_<map>.ini:
 *   start   x y z
 *   goal_t  x y z radius
 *   goal_ct x y z radius
 * Admins: amx_cawp_set <start|goal_t|goal_ct> [radius] at their feet, amx_cawp_save, amx_cawp_status.
 */
#include <amxmodx>
#include <amxmisc>
#include <fakemeta>
#include <hamsandwich>
#include <reapi>

#define PLUGIN  "Capture the AWP"
#define VERSION "1.0.0"
#define AUTHOR  "daemon (olds.ro)"

#define AWP_MARK    5505
#define TASK_TICK   5505
#define TASK_RETURN 5506
#define TICK        0.2

new const ITEM_CLASS[] = "olds_cawp_item"
new const AWP_MODEL[] = "models/w_awp.mdl"
new const BEAM_SPRITE[] = "sprites/laserbeam.spr"

new g_cvReturn, g_cvScore
new g_beam, g_hud

new g_file[128], g_map[32]
new Float:g_start[3], bool:g_hasStart
new Float:g_goal[2][3], Float:g_goalRadius[2], bool:g_hasGoal[2]   // 0 = T, 1 = CT

new g_item                     // the AWP lying on the ground, 0 = none
new g_carrier                  // the player carrying it, 0 = none
new g_weapon                   // the carrier's weapon_awp entity
new bool:g_dropped             // lying away from the start
new g_caps[2]
new Float:g_lastPos[MAX_PLAYERS + 1][3]
new Float:g_nextBeam
new bool:g_roundOver

public plugin_precache()
{
    precache_model(AWP_MODEL)
    g_beam = precache_model(BEAM_SPRITE)
}

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

    g_cvReturn = create_cvar("olds_cawp_return", "15", FCVAR_NONE, "Seconds a dropped AWP lies untouched before it returns", true, 1.0, true, 300.0)
    g_cvScore = create_cvar("olds_cawp_score", "5", FCVAR_NONE, "Captures that win the round", true, 1.0, true, 100.0)
    AutoExecConfig(true, "olds_cawp")

    RegisterHam(Ham_Touch, "info_target", "on_touch_pre", 0)
    RegisterHookChain(RG_CBasePlayer_Killed, "on_killed_pre", false)
    RegisterHookChain(RG_CBasePlayer_DropPlayerItem, "on_drop_item_pre", false)
    RegisterHookChain(RG_CSGameRules_RestartRound, "on_restart_round_pre", false)
    RegisterHookChain(RG_CSGameRules_RestartRound, "on_restart_round_post", true)
    RegisterHookChain(RG_RoundEnd, "on_round_end_post", true)

    register_concmd("amx_cawp_set", "cmd_set", ADMIN_CFG, "<start|goal_t|goal_ct> [radius] - sets a point where you stand")
    register_concmd("amx_cawp_save", "cmd_save", ADMIN_CFG, "- writes this map's points to olds_cawp_<map>.ini")
    register_concmd("amx_cawp_status", "cmd_status", ADMIN_KICK, "- shows the AWP, the carrier and the captures")

    g_hud = CreateHudSyncObj()
    get_mapname(g_map, charsmax(g_map))
    get_configsdir(g_file, charsmax(g_file))
    format(g_file, charsmax(g_file), "%s/olds_cawp_%s.ini", g_file, g_map)
}

public plugin_cfg()
{
    load_points()
    if (g_hasStart)
        spawn_item(g_start, false)
    set_task(TICK, "tick", TASK_TICK, _, _, "b")
}

/* ---------- olds_cawp_<map>.ini ---------- */

load_points()
{
    new file = fopen(g_file, "rt")
    if (!file)
    {
        log_amx("%s not found: no AWP on this map (amx_cawp_set and amx_cawp_save make it)", g_file)
        return
    }
    new line[128], key[16], sx[16], sy[16], sz[16], sr[16], number
    while (fgets(file, line, charsmax(line)))
    {
        number++
        trim(line)
        if (!line[0] || line[0] == ';' || (line[0] == '/' && line[1] == '/'))
            continue
        sr[0] = EOS
        new parts = parse(line, key, charsmax(key), sx, charsmax(sx), sy, charsmax(sy), sz, charsmax(sz), sr, charsmax(sr))
        new Float:spot[3]
        spot[0] = str_to_float(sx)
        spot[1] = str_to_float(sy)
        spot[2] = str_to_float(sz)
        if (equali(key, "start") && parts >= 4)
        {
            g_start = spot
            g_hasStart = true
        }
        else if ((equali(key, "goal_t") || equali(key, "goal_ct")) && parts >= 5 && str_to_float(sr) > 0.0)
        {
            new side = equali(key, "goal_t") ? 0 : 1
            g_goal[side] = spot
            g_goalRadius[side] = floatmin(str_to_float(sr), 2000.0)
            g_hasGoal[side] = true
        }
        else
            log_amx("%s line %d: expected start x y z or goal_t/goal_ct x y z radius", g_file, number)
    }
    fclose(file)
    if (!g_hasStart || !g_hasGoal[0] || !g_hasGoal[1])
        log_amx("%s: start, goal_t and goal_ct are all needed (%d %d %d)", g_file, g_hasStart, g_hasGoal[0], g_hasGoal[1])
}

/* ---------- the item ---------- */

side_of(id)
{
    new TeamName:team = TeamName:get_member(id, m_iTeam)
    if (team == TEAM_TERRORIST)
        return 0
    if (team == TEAM_CT)
        return 1
    return -1
}

remove_item()
{
    remove_task(TASK_RETURN)
    if (g_item && pev_valid(g_item))
    {
        set_pev(g_item, pev_iuser4, 0)
        set_pev(g_item, pev_flags, pev(g_item, pev_flags) | FL_KILLME)
    }
    g_item = 0
}

spawn_item(const Float:spot[3], bool:dropped)
{
    remove_item()
    new ent = engfunc(EngFunc_CreateNamedEntity, engfunc(EngFunc_AllocString, "info_target"))
    if (!pev_valid(ent))
        return
    set_pev(ent, pev_classname, ITEM_CLASS)
    engfunc(EngFunc_SetModel, ent, AWP_MODEL)
    engfunc(EngFunc_SetSize, ent, Float:{-16.0, -16.0, 0.0}, Float:{16.0, 16.0, 16.0})
    engfunc(EngFunc_SetOrigin, ent, spot)
    set_pev(ent, pev_solid, SOLID_TRIGGER)
    set_pev(ent, pev_movetype, MOVETYPE_TOSS)
    set_pev(ent, pev_renderfx, kRenderFxGlowShell)
    set_pev(ent, pev_rendercolor, Float:{255.0, 200.0, 40.0})
    set_pev(ent, pev_renderamt, 20.0)
    set_pev(ent, pev_iuser4, AWP_MARK)
    engfunc(EngFunc_DropToFloor, ent)
    g_item = ent
    g_dropped = dropped
    if (dropped)
        set_task(get_pcvar_float(g_cvReturn), "auto_return", TASK_RETURN)
}

public auto_return()
{
    if (!g_dropped || g_carrier)
        return
    log_amx("The AWP lay untouched for %d s: back to the start", get_pcvar_num(g_cvReturn))
    client_print_color(0, print_team_default, "%L", LANG_PLAYER, "CAWP_RETURNED")
    spawn_item(g_start, false)
}

public on_touch_pre(ent, toucher)
{
    if (ent != g_item || !g_item || pev(ent, pev_iuser4) != AWP_MARK)
        return HAM_IGNORED
    if (toucher < 1 || toucher > MaxClients || !is_user_alive(toucher) || side_of(toucher) < 0 || g_carrier || g_roundOver)
        return HAM_SUPERCEDE
    pick_up(toucher)
    return HAM_SUPERCEDE
}

pick_up(id)
{
    new weapon = rg_give_item(id, "weapon_awp", GT_DROP_AND_REPLACE)
    if (is_nullent(weapon))
        return
    remove_item()
    g_dropped = false
    g_carrier = id
    g_weapon = weapon
    set_entvar(weapon, var_iuser4, AWP_MARK)
    engclient_cmd(id, "weapon_awp")
    if (side_of(id) == 0)
        set_glow(id, kRenderFxGlowShell, 255, 40, 40, kRenderNormal, 18)
    else
        set_glow(id, kRenderFxGlowShell, 40, 80, 255, kRenderNormal, 18)
    new name[32]
    get_user_name(id, name, charsmax(name))
    client_print_color(0, print_team_default, "%L", LANG_PLAYER, "CAWP_TAKEN", name)
    log_amx("%s picks up the AWP", name)
}

set_glow(id, fx, r, g, b, mode, amount)
{
    new Float:colour[3]
    colour[0] = float(r)
    colour[1] = float(g)
    colour[2] = float(b)
    set_entvar(id, var_renderfx, fx)
    set_entvar(id, var_rendercolor, colour)
    set_entvar(id, var_rendermode, mode)
    set_entvar(id, var_renderamt, float(amount))
}

/* The carrier lets go of it here (their AWP is taken away). */
release_carrier(const Float:spot[3], const why[])
{
    new id = g_carrier
    g_carrier = 0
    if (id && is_user_connected(id))
    {
        set_glow(id, kRenderFxNone, 255, 255, 255, kRenderNormal, 16)
        if (is_user_alive(id) && g_weapon && !is_nullent(g_weapon) && get_member(g_weapon, m_pPlayer) == id)
            rg_remove_item(id, "weapon_awp")
    }
    g_weapon = 0
    new name[32]
    if (id)
        get_user_name(id, name, charsmax(name))
    if (spot[0] == 0.0 && spot[1] == 0.0 && spot[2] == 0.0)
        return
    new Float:at[3]
    at = spot
    spawn_item(at, true)
    client_print_color(0, print_team_default, "%L", LANG_PLAYER, "CAWP_DROPPED", name, get_pcvar_num(g_cvReturn))
    log_amx("%s drops the AWP (%s) at %.0f %.0f %.0f", name, why, at[0], at[1], at[2])
}

feet_of(id, Float:out[3])
{
    get_entvar(id, var_origin, out)
    out[2] -= 30.0
}

public on_killed_pre(victim, attacker, gib)
{
    if (victim != g_carrier)
        return HC_CONTINUE
    new Float:spot[3]
    feet_of(victim, spot)
    release_carrier(spot, "died")
    return HC_CONTINUE
}

public on_drop_item_pre(id, const name[])
{
    if (id != g_carrier)
        return HC_CONTINUE
    new item = get_member(id, m_pActiveItem)
    if (!equal(name, "weapon_awp") && (name[0] || item != g_weapon))
        return HC_CONTINUE
    // The drop key: the AWP lands in front of the carrier as the capture item.
    new Float:spot[3], Float:angles[3], Float:fwd[3]
    feet_of(id, spot)
    get_entvar(id, var_v_angle, angles)
    angles[0] = 0.0
    angle_vector(angles, ANGLEVECTOR_FORWARD, fwd)
    spot[0] += fwd[0] * 48.0
    spot[1] += fwd[1] * 48.0
    release_carrier(spot, "dropped it")
    SetHookChainReturn(ATYPE_INTEGER, 0)
    return HC_SUPERCEDE
}

public client_disconnected(id)
{
    if (id != g_carrier)
        return
    // At the last position seen.
    new Float:spot[3]
    if (is_user_alive(id))
        get_entvar(id, var_origin, spot)
    else
        spot = g_lastPos[id]
    spot[2] -= 30.0
    release_carrier(spot, "left the server")
}

/* ---------- goals, locator, HUD ---------- */

public tick()
{
    new Float:now = get_gametime()
    if (g_carrier)
    {
        new id = g_carrier
        if (!is_user_alive(id) || is_nullent(g_weapon) || get_member(g_weapon, m_pPlayer) != id)
        {
            // The weapon went some other way (stripped, team change): back to the start.
            g_carrier = 0
            g_weapon = 0
            if (is_user_connected(id))
                set_glow(id, kRenderFxNone, 255, 255, 255, kRenderNormal, 16)
            if (g_hasStart)
                spawn_item(g_start, false)
        }
        else
        {
            get_entvar(id, var_origin, g_lastPos[id])
            new side = side_of(id)
            if (side >= 0 && g_hasGoal[side] && !g_roundOver && get_distance_f(g_lastPos[id], g_goal[side]) <= g_goalRadius[side])
                capture(id, side)
        }
    }
    if (now >= g_nextBeam)
    {
        g_nextBeam = now + 1.0
        locator()
    }
}

capture(id, side)
{
    g_caps[side]++
    new name[32]
    get_user_name(id, name, charsmax(name))
    release_carrier(Float:{0.0, 0.0, 0.0}, "captured")
    spawn_item(g_start, false)
    new need = get_pcvar_num(g_cvScore)
    client_print_color(0, side == 0 ? print_team_red : print_team_blue, "%L", LANG_PLAYER, "CAWP_CAPTURE", name, g_caps[0], g_caps[1], need)
    log_amx("%s captures the AWP: T %d, CT %d (of %d)", name, g_caps[0], g_caps[1], need)
    show_score()
    if (g_caps[side] >= need && !get_member_game(m_bRoundTerminating))
    {
        g_roundOver = true
        if (side == 0)
        {
            rg_update_teamscores(0, 1)
            rg_round_end(5.0, WINSTATUS_TERRORISTS, ROUND_TERRORISTS_WIN)
        }
        else
        {
            rg_update_teamscores(1, 0)
            rg_round_end(5.0, WINSTATUS_CTS, ROUND_CTS_WIN)
        }
    }
}

show_score()
{
    set_hudmessage(255, 220, 80, -1.0, 0.04, 0, 0.0, 4.0, 0.1, 0.4, -1)
    ShowSyncHudMsg(0, g_hud, "AWP captures   T %d : %d CT   (%d to win)", g_caps[0], g_caps[1], get_pcvar_num(g_cvScore))
}

locator()
{
    new Float:spot[3]
    if (g_carrier && is_user_alive(g_carrier))
        get_entvar(g_carrier, var_origin, spot)
    else if (g_item && pev_valid(g_item))
        pev(g_item, pev_origin, spot)
    else
        return
    message_begin(MSG_ALL, SVC_TEMPENTITY)
    write_byte(TE_BEAMPOINTS)
    engfunc(EngFunc_WriteCoord, spot[0])
    engfunc(EngFunc_WriteCoord, spot[1])
    engfunc(EngFunc_WriteCoord, spot[2] + 40.0)
    engfunc(EngFunc_WriteCoord, spot[0])
    engfunc(EngFunc_WriteCoord, spot[1])
    engfunc(EngFunc_WriteCoord, spot[2] + 400.0)
    write_short(g_beam)
    write_byte(0)
    write_byte(0)
    write_byte(11)
    write_byte(12)
    write_byte(0)
    write_byte(255)
    write_byte(200)
    write_byte(40)
    write_byte(160)
    write_byte(0)
    message_end()
}

/* ---------- rounds ---------- */

public on_restart_round_pre()
{
    // Survivors keep their weapons into the new round; the capture AWP does not.
    if (g_carrier)
        release_carrier(Float:{0.0, 0.0, 0.0}, "new round")
    return HC_CONTINUE
}

public on_restart_round_post()
{
    g_roundOver = false
    g_caps[0] = 0
    g_caps[1] = 0
    if (g_hasStart)
        spawn_item(g_start, false)
}

public on_round_end_post()
{
    g_roundOver = true
}

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

public cmd_set(id, level, cid)
{
    if (!cmd_access(id, level, cid, 2))
        return PLUGIN_HANDLED
    if (!id || !is_user_alive(id))
    {
        console_print(id, "[AWP] Stand where the point goes (alive, in game).")
        return PLUGIN_HANDLED
    }
    new what[16], arg[16]
    read_argv(1, what, charsmax(what))
    read_argv(2, arg, charsmax(arg))
    new Float:spot[3]
    feet_of(id, spot)
    if (equali(what, "start"))
    {
        g_start = spot
        g_hasStart = true
        if (!g_carrier)
            spawn_item(g_start, false)
    }
    else if (equali(what, "goal_t") || equali(what, "goal_ct"))
    {
        new Float:radius = str_to_float(arg)
        if (radius <= 0.0)
            radius = 96.0
        new side = equali(what, "goal_t") ? 0 : 1
        get_entvar(id, var_origin, g_goal[side])
        g_goalRadius[side] = floatmin(radius, 2000.0)
        g_hasGoal[side] = true
    }
    else
    {
        console_print(id, "[AWP] Usage: amx_cawp_set <start|goal_t|goal_ct> [radius]")
        return PLUGIN_HANDLED
    }
    console_print(id, "[AWP] %s set (amx_cawp_save writes it).", what)
    return PLUGIN_HANDLED
}

public cmd_save(id, level, cid)
{
    if (!cmd_access(id, level, cid, 1))
        return PLUGIN_HANDLED
    if (!g_hasStart || !g_hasGoal[0] || !g_hasGoal[1])
    {
        console_print(id, "[AWP] Set start, goal_t and goal_ct first.")
        return PLUGIN_HANDLED
    }
    new file = fopen(g_file, "wt")
    if (!file)
    {
        console_print(id, "[AWP] Cannot write %s.", g_file)
        return PLUGIN_HANDLED
    }
    fprintf(file, "; Capture the AWP points for %s^n", g_map)
    fprintf(file, "start %.1f %.1f %.1f^n", g_start[0], g_start[1], g_start[2])
    fprintf(file, "goal_t %.1f %.1f %.1f %.0f^n", g_goal[0][0], g_goal[0][1], g_goal[0][2], g_goalRadius[0])
    fprintf(file, "goal_ct %.1f %.1f %.1f %.0f^n", g_goal[1][0], g_goal[1][1], g_goal[1][2], g_goalRadius[1])
    fclose(file)
    console_print(id, "[AWP] Saved to %s.", g_file)
    return PLUGIN_HANDLED
}

public cmd_status(id, level, cid)
{
    if (!cmd_access(id, level, cid, 1))
        return PLUGIN_HANDLED
    new name[32]
    copy(name, charsmax(name), "nobody")
    if (g_carrier && is_user_connected(g_carrier))
        get_user_name(g_carrier, name, charsmax(name))
    new Float:spot[3]
    if (g_item && pev_valid(g_item))
        pev(g_item, pev_origin, spot)
    new where[16]
    copy(where, charsmax(where), "carried")
    if (!g_carrier)
    {
        if (g_dropped)
            copy(where, charsmax(where), "dropped")
        else
            copy(where, charsmax(where), "at the start")
    }
    console_print(id, "[AWP] %s; carrier %s; item at %.0f %.0f %.0f; captures T %d, CT %d (of %d).",
        where, name, spot[0], spot[1], spot[2], g_caps[0], g_caps[1], get_pcvar_num(g_cvScore))
    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.