Skip to content
View in the app

A better way to browse. Learn more.

Learn more

control-points-capture-mode/addons/amxmodx/scripting/olds_cpoints.sma

olds_cpoints.sma
441 lines · 13.5 KB · Pawn (AMX Mod X)
Raw
/*
 * Control Points Capture Mode (olds.ro workshop card LIB-0917)
 *
 * King of the Hill: capture points placed around the map. Standing near a
 * point with only your team for olds_cp_capture seconds converts it to
 * your team; every olds_cp_interval seconds each team scores olds_cp_points
 * for every point it holds, and the first team to olds_cp_goal wins the
 * round (rg_round_end). Scores and owners carry over between rounds until a
 * team reaches the goal; then everything starts over.
 *
 *   - Capture: checked every half second (get_players and get_distance_f).
 *     Only one team on the point: its progress grows; both teams: contested,
 *     the bar freezes; nobody: the progress slowly drains. Capturing a point
 *     the other team owns takes the same time.
 *   - Rings: TE_BEAMCYLINDER with the stock sprites/laserbeam.spr, red for
 *     terrorists, blue for counter-terrorists, white for nobody. A capture
 *     plays the stock ambience/alarm1.wav for everyone.
 *   - HUD: the score, and the capture bar for players on a point.
 *
 * Points: addons/amxmodx/configs/flags/<map>.ini, one per line:
 *     "Point name"  x y z  [radius 32-512, default 128]
 * A map without the file (or without a valid point) disables the plugin
 * there, with one log line.
 *
 * Cvars: olds_cp_capture 10 (2-120), olds_cp_interval 5 (1-60),
 * olds_cp_points 1 (1-100), olds_cp_goal 200 (10-10000).
 * Command: amx_cp [reload | reset] (ADMIN_MAP).
 *
 * Copyright (C) 2026 daemon (olds.ro)
 * SPDX-License-Identifier: GPL-3.0-or-later
 */
#include <amxmodx>
#include <amxmisc>
#include <reapi>

#define PLUGIN  "Control Points Capture Mode"
#define VERSION "1.0.0"
#define AUTHOR  "olds.ro"

const MAX_POINTS = 16
const POINT_NAME = 32
const TASK_POLL = 9171
const TASK_RINGS = 9172
const Float:POLL = 0.5
const Float:WORLD_LIMIT = 4096.0

enum
{
    SIDE_NONE = 0,
    SIDE_T,
    SIDE_CT
}

new const RING_SPRITE[] = "sprites/laserbeam.spr"
new const CAPTURE_SOUND[] = "ambience/alarm1.wav"

new g_name[MAX_POINTS][POINT_NAME]
new Float:g_origin[MAX_POINTS][3]
new Float:g_radius[MAX_POINTS]
new g_points
new g_file[128]

new g_owner[MAX_POINTS]
new g_capturing[MAX_POINTS]              // side making progress
new Float:g_progress[MAX_POINTS]         // seconds of capture so far
new bool:g_contested[MAX_POINTS]
new g_score[3]
new Float:g_sinceScore
new bool:g_roundOver
new bool:g_resetNext
new g_ring
new g_hud

new Float:g_captureTime
new g_interval
new g_perPoint
new g_goal

public plugin_precache()
{
    g_ring = precache_model(RING_SPRITE)
    precache_sound(CAPTURE_SOUND)
}

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

    bind_pcvar_float(create_cvar("olds_cp_capture", "10", FCVAR_NONE,
        "Seconds alone on a point to capture it (2-120).", true, 2.0, true, 120.0), g_captureTime)
    bind_pcvar_num(create_cvar("olds_cp_interval", "5", FCVAR_NONE,
        "Seconds between score ticks (1-60).", true, 1.0, true, 60.0), g_interval)
    bind_pcvar_num(create_cvar("olds_cp_points", "1", FCVAR_NONE,
        "Score per held point each tick (1-100).", true, 1.0, true, 100.0), g_perPoint)
    bind_pcvar_num(create_cvar("olds_cp_goal", "200", FCVAR_NONE,
        "Score that wins (10-10000).", true, 10.0, true, 10000.0), g_goal)
    AutoExecConfig(true, "olds_cpoints")

    register_concmd("amx_cp", "cmd_cp", ADMIN_MAP, "[reload | reset] - control points, owners and scores")
    RegisterHookChain(RG_CSGameRules_RestartRound, "on_restart_round_post", true)
    RegisterHookChain(RG_RoundEnd, "on_round_end_post", true)
    g_hud = CreateHudSyncObj()

    new map[64]
    get_mapname(map, charsmax(map))
    get_configsdir(g_file, charsmax(g_file))
    format(g_file, charsmax(g_file), "%s/flags/%s.ini", g_file, map)
    load_points()
    if (!g_points)
        log_amx("no control points for this map (%s): the plugin is off here", g_file)
    set_task(POLL, "task_poll", TASK_POLL, _, _, "b")
    set_task(1.0, "task_rings", TASK_RINGS, _, _, "b")
}

/* ---------- points ---------- */

load_points()
{
    g_points = 0
    new file = fopen(g_file, "rt")
    if (!file)
        return
    new line[160], name[POINT_NAME], value[4][16], number
    while (fgets(file, line, charsmax(line)))
    {
        number++
        trim(line)
        if (!line[0] || line[0] == ';' || (line[0] == '/' && line[1] == '/'))
            continue
        if (g_points == MAX_POINTS)
        {
            log_amx("%s: more than %d points, the rest are ignored", g_file, MAX_POINTS)
            break
        }
        new parts = parse(line, name, charsmax(name), value[0], charsmax(value[]), value[1], charsmax(value[]),
            value[2], charsmax(value[]), value[3], charsmax(value[]))
        if ((parts != 4 && parts != 5) || !name[0] || !numbers(value, parts - 1))
        {
            log_amx("%s line %d: expected ^"name^" x y z [radius], skipped", g_file, number)
            continue
        }
        new Float:origin[3], bool:inside = true
        for (new axis = 0; axis < 3; axis++)
        {
            origin[axis] = str_to_float(value[axis])
            if (floatabs(origin[axis]) > WORLD_LIMIT)
                inside = false
        }
        new Float:radius = (parts == 5) ? str_to_float(value[3]) : 128.0
        if (!inside || !(32.0 <= radius <= 512.0))
        {
            log_amx("%s line %d: point outside the map or radius not 32-512, skipped", g_file, number)
            continue
        }
        copy(g_name[g_points], charsmax(g_name[]), name)
        g_origin[g_points] = origin
        g_radius[g_points] = radius
        g_points++
    }
    fclose(file)
    reset_all()
}

bool:numbers(const value[][], count)
{
    for (new v = 0; v < count; v++)
    {
        new i = (value[v][0] == '-' || value[v][0] == '+') ? 1 : 0, digits
        for (; value[v][i]; i++)
        {
            if (isdigit(value[v][i]))
                digits++
            else if (value[v][i] != '.')
                return false
        }
        if (!digits)
            return false
    }
    return true
}

reset_all()
{
    for (new p = 0; p < MAX_POINTS; p++)
    {
        g_owner[p] = SIDE_NONE
        g_capturing[p] = SIDE_NONE
        g_progress[p] = 0.0
        g_contested[p] = false
    }
    g_score[SIDE_T] = 0
    g_score[SIDE_CT] = 0
    g_sinceScore = 0.0
}

/* ---------- capture and score ---------- */

side_of(id)
{
    switch (TeamName:get_member(id, m_iTeam))
    {
        case TEAM_TERRORIST: return SIDE_T
        case TEAM_CT: return SIDE_CT
    }
    return SIDE_NONE
}

public task_poll()
{
    if (!g_points || g_roundOver || get_member_game(m_bFreezePeriod))
        return

    new players[MAX_PLAYERS], count, Float:pos[3]
    get_players(players, count, "ah")
    new onPoint[MAX_PLAYERS + 1]             // point index + 1 the player stands on
    for (new p = 0; p < g_points; p++)
    {
        new present[3]
        for (new i = 0; i < count; i++)
        {
            new id = players[i]
            get_entvar(id, var_origin, pos)
            if (get_distance_f(pos, g_origin[p]) <= g_radius[p])
            {
                present[side_of(id)]++
                onPoint[id] = p + 1
            }
        }
        update_point(p, present[SIDE_T], present[SIDE_CT])
    }

    // Score ticks.
    g_sinceScore += POLL
    if (g_sinceScore >= float(g_interval))
    {
        g_sinceScore = 0.0
        for (new p = 0; p < g_points; p++)
            if (g_owner[p] != SIDE_NONE)
                g_score[g_owner[p]] += g_perPoint
        check_goal()
    }
    show_hud(players, count, onPoint)
}

update_point(p, terrorists, cts)
{
    g_contested[p] = terrorists && cts
    if (g_contested[p])
        return                              // the bar freezes
    if (!terrorists && !cts)
    {
        g_progress[p] = floatmax(g_progress[p] - POLL / 2.0, 0.0)
        if (g_progress[p] == 0.0)
            g_capturing[p] = SIDE_NONE
        return
    }
    new side = terrorists ? SIDE_T : SIDE_CT
    if (side == g_owner[p])
    {
        g_progress[p] = 0.0
        g_capturing[p] = SIDE_NONE
        return
    }
    if (g_capturing[p] != side)
    {
        g_capturing[p] = side
        g_progress[p] = 0.0
    }
    g_progress[p] += POLL
    if (g_progress[p] >= g_captureTime)
    {
        g_owner[p] = side
        g_progress[p] = 0.0
        g_capturing[p] = SIDE_NONE
        for (new id = 1; id <= MaxClients; id++)
            if (is_user_connected(id) && !is_user_bot(id))
                client_cmd(id, "spk ^"%s^"", CAPTURE_SOUND)
        if (side == SIDE_T)
            tell_all("CP_CAPTURED_T", g_name[p])
        else
            tell_all("CP_CAPTURED_CT", g_name[p])
    }
}

check_goal()
{
    new winner = SIDE_NONE
    if (g_score[SIDE_T] >= g_goal && g_score[SIDE_T] >= g_score[SIDE_CT])
        winner = SIDE_T
    else if (g_score[SIDE_CT] >= g_goal)
        winner = SIDE_CT
    if (winner == SIDE_NONE || get_member_game(m_bRoundTerminating))
        return
    g_roundOver = true
    g_resetNext = true
    if (winner == SIDE_T)
        tell_all("CP_WIN_T", g_score[SIDE_T], g_score[SIDE_CT])
    else
        tell_all("CP_WIN_CT", g_score[SIDE_T], g_score[SIDE_CT])
    if (winner == SIDE_T)
        rg_round_end(5.0, WINSTATUS_TERRORISTS, ROUND_TERRORISTS_WIN, "Terrorists hold the hill!", "default", true)
    else
        rg_round_end(5.0, WINSTATUS_CTS, ROUND_CTS_WIN, "Counter-Terrorists hold the hill!", "default", true)
}

public on_restart_round_post()
{
    g_roundOver = false
    if (g_resetNext)
    {
        g_resetNext = false
        reset_all()
    }
    // Capture progress never carries into a new round; owners and scores do.
    for (new p = 0; p < g_points; p++)
    {
        g_progress[p] = 0.0
        g_capturing[p] = SIDE_NONE
        g_contested[p] = false
    }
    return HC_CONTINUE
}

public on_round_end_post(WinStatus:status, ScenarioEventEndRound:event, Float:delay)
{
    g_roundOver = true
    return HC_CONTINUE
}

/* ---------- display ---------- */

show_hud(const players[], count, const onPoint[])
{
    for (new i = 0; i < count; i++)
    {
        new id = players[i]
        if (is_user_bot(id))
            continue
        new line[128], p = onPoint[id] - 1
        formatex(line, charsmax(line), "%L", id, "CP_HUD_SCORE", g_score[SIDE_T], g_score[SIDE_CT], g_goal)
        if (p >= 0)
        {
            if (g_contested[p])
                format(line, charsmax(line), "%s^n%L", line, id, "CP_HUD_CONTESTED", g_name[p],
                    floatround(g_progress[p] * 100.0 / g_captureTime))
            else if (g_capturing[p] != SIDE_NONE)
                format(line, charsmax(line), "%s^n%L", line, id, "CP_HUD_CAPTURING", g_name[p],
                    floatround(g_progress[p] * 100.0 / g_captureTime))
        }
        set_hudmessage(230, 230, 230, -1.0, 0.12, 0, 0.0, POLL + 0.2, 0.0, 0.0)
        ShowSyncHudMsg(id, g_hud, "%s", line)
    }
}

public task_rings()
{
    if (!g_points)
        return
    for (new p = 0; p < g_points; p++)
    {
        new red = 220, green = 220, blue = 220
        if (g_owner[p] == SIDE_T)
        {
            green = 40
            blue = 40
        }
        else if (g_owner[p] == SIDE_CT)
        {
            red = 40
            green = 90
        }
        message_begin(MSG_BROADCAST, SVC_TEMPENTITY)
        write_byte(TE_BEAMCYLINDER)
        write_coord_f(g_origin[p][0])
        write_coord_f(g_origin[p][1])
        write_coord_f(g_origin[p][2] - 30.0)
        write_coord_f(g_origin[p][0])
        write_coord_f(g_origin[p][1])
        write_coord_f(g_origin[p][2] - 30.0 + g_radius[p])
        write_short(g_ring)
        write_byte(0)       // start frame
        write_byte(0)       // frame rate
        write_byte(10)      // life, tenths of a second
        write_byte(8)       // width
        write_byte(0)       // noise
        write_byte(red)
        write_byte(green)
        write_byte(blue)
        write_byte(160)     // brightness
        write_byte(0)       // speed
        message_end()
    }
}

tell_all(const key[], any:...)
{
    new pattern[160], message[192]
    for (new id = 1; id <= MaxClients; id++)
    {
        if (!is_user_connected(id) || is_user_bot(id))
            continue
        formatex(pattern, charsmax(pattern), "%L", id, key)
        vformat(message, charsmax(message), pattern, 2)
        client_print_color(id, print_team_default, "^4[Points]^1 %s", message)
    }
    formatex(pattern, charsmax(pattern), "%L", LANG_SERVER, key)
    vformat(message, charsmax(message), pattern, 2)
    server_print("[Points] %s", message)
}

public cmd_cp(id, level, cid)
{
    if (!cmd_access(id, level, cid, 1))
        return PLUGIN_HANDLED
    new action[16]
    read_argv(1, action, charsmax(action))
    if (equali(action, "reload"))
    {
        load_points()
        console_print(id, "[Points] %d point(s) read from %s; owners and scores cleared", g_points, g_file)
    }
    else if (equali(action, "reset"))
    {
        reset_all()
        console_print(id, "[Points] owners and scores cleared")
    }
    if (!g_points)
    {
        console_print(id, "[Points] no control points for this map (%s): off", g_file)
        return PLUGIN_HANDLED
    }
    new const sides[][] = { "nobody", "T", "CT" }
    console_print(id, "[Points] score T %d, CT %d of %d; capture %.0f s; %d per point every %d s", g_score[SIDE_T],
        g_score[SIDE_CT], g_goal, g_captureTime, g_perPoint, g_interval)
    for (new p = 0; p < g_points; p++)
        console_print(id, "[Points]   %s: owner %s, %s %d%%%s", g_name[p], sides[g_owner[p]],
            g_capturing[p] ? sides[g_capturing[p]] : "-", floatround(g_progress[p] * 100.0 / g_captureTime),
            g_contested[p] ? " (contested)" : "")
    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.