Skip to content
View in the app

A better way to browse. Learn more.

Learn more

piece-labeler/addons/amxmodx/scripting/olds_label.sma

olds_label.sma
352 lines · 9.7 KB · Pawn (AMX Mod X)
Raw
/*
 * Piece Labeler (olds.ro workshop card AMX-073)
 *
 * Base Builder: during the build phase a builder can tag a block they own as
 * a door, a ramp or cover. Teammates who aim at the block see the tag in a
 * short on-screen line, so the base plan is clear without asking.
 *
 *   say /label <door|ramp|cover>   tag the block you aim at (replaces its old tag)
 *   say /label clear               remove the tag from the block you aim at
 *
 * Needs: AMX Mod X 1.10, ReGameDLL_CS + ReAPI, and ReBaseBuilder
 * (github.com/byjumpe/ReBaseBuilder, library "rebb_api"):
 *   - build phase: rebb_is_building_phase(); the barrier is never a block;
 *   - owner: the builder holding the block now (rebb_get_owned_ent), else the
 *     one who locked it (entvar iuser1), else the one moving it (iuser3), else
 *     the last one who moved it (iuser4), as ReBaseBuilder stores them;
 *   - rebb_build_start and every round restart remove all tags (the core
 *     resets the layout).
 * Every time a tag is shown it is checked first: the block must still exist
 * and still belong to the builder who tagged it, or the tag is dropped.
 *
 * Teammates: living counter-terrorists (builders). Every 0.2 s a trace from
 * each one's eyes (EngFunc_TraceLine) finds the block they aim at.
 *
 * Cvar: olds_label_max 10 (1-50), tags per builder.
 * Command: olds_label_status (ADMIN_CFG).
 *
 * Copyright (C) 2026 daemon (olds.ro)
 * SPDX-License-Identifier: GPL-3.0-or-later
 */
#include <amxmodx>
#include <amxmisc>
#include <fakemeta>
#include <reapi>

#define PLUGIN  "Piece Labeler"
#define VERSION "1.0.0"
#define AUTHOR  "olds.ro"

/* ReBaseBuilder API (re_basebuilder.inc), declared here so the plugin compiles without it. */
#pragma reqlib "rebb_api"
native rebb_is_building_phase()
native rebb_get_barrier_ent_index()
native rebb_get_owned_ent(id)

const MAX_LABELS = 128
const TASK_AIM = 7311
const Float:AIM_PERIOD = 0.2
const Float:AIM_RANGE = 2048.0
const Float:REFRESH = 0.9

enum
{
    KIND_DOOR = 0,
    KIND_RAMP,
    KIND_COVER,
    KIND_COUNT
}
new const KIND_WORD[KIND_COUNT][] = { "door", "ramp", "cover" }
new const KIND_KEY[KIND_COUNT][] = { "LABEL_DOOR", "LABEL_RAMP", "LABEL_COVER" }

new g_block[MAX_LABELS]               // 0 = free slot
new g_kind[MAX_LABELS]
new g_author[MAX_LABELS]              // player id of the builder who tagged it

new g_lastSeen[MAX_PLAYERS + 1]       // block last shown to this player
new Float:g_lastShown[MAX_PLAYERS + 1]
new Float:g_nextUse[MAX_PLAYERS + 1]

new g_maxLabels

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

    bind_pcvar_num(create_cvar("olds_label_max", "10", FCVAR_NONE,
        "Block tags each builder may have (1-50).", true, 1.0, true, 50.0), g_maxLabels)
    AutoExecConfig(true, "olds_label")

    register_clcmd("say", "on_say")
    register_clcmd("say_team", "on_say")
    register_concmd("olds_label_status", "cmd_status", ADMIN_CFG, "- every block tag")

    RegisterHookChain(RG_CSGameRules_RestartRound, "on_restart_round_pre", false)
    set_task(AIM_PERIOD, "task_aim", TASK_AIM, _, _, "b")
}

/* ---------- blocks and owners ---------- */

bool:is_block(entity)
{
    if (entity <= MaxClients || is_nullent(entity) || entity == rebb_get_barrier_ent_index())
        return false
    new classname[16], target[16]
    get_entvar(entity, var_classname, classname, charsmax(classname))
    if (!equal(classname, "func_wall"))
        return false
    get_entvar(entity, var_target, target, charsmax(target))
    return !equal(target, "ignore")
}

bool:valid_player(id)
{
    return 1 <= id <= MaxClients && is_user_connected(id)
}

block_owner(entity)
{
    for (new id = 1; id <= MaxClients; id++)
        if (is_user_connected(id) && rebb_get_owned_ent(id) == entity)
            return id
    new owner = get_entvar(entity, var_iuser1)
    if (!valid_player(owner))
        owner = get_entvar(entity, var_iuser3)
    if (!valid_player(owner))
        owner = get_entvar(entity, var_iuser4)
    return valid_player(owner) ? owner : 0
}

label_of(entity)
{
    for (new n = 0; n < MAX_LABELS; n++)
        if (g_block[n] == entity)
            return n
    return -1
}

labels_by(id)
{
    new count
    for (new n = 0; n < MAX_LABELS; n++)
        if (g_block[n] && g_author[n] == id)
            count++
    return count
}

remove_label(n)
{
    g_block[n] = 0
    g_author[n] = 0
}

clear_all()
{
    for (new n = 0; n < MAX_LABELS; n++)
        remove_label(n)
}

/* A tag is valid while its block exists and still belongs to its author. */
bool:still_valid(n)
{
    if (is_block(g_block[n]) && valid_player(g_author[n]) && block_owner(g_block[n]) == g_author[n])
        return true
    remove_label(n)
    return false
}

/* ---------- /label ---------- */

public on_say(id)
{
    if (!is_user_connected(id))
        return PLUGIN_CONTINUE
    new text[64]
    read_args(text, charsmax(text))
    remove_quotes(text)
    trim(text)
    if (!equali(text, "/label", 6) || (text[6] && text[6] != ' '))
        return PLUGIN_CONTINUE

    new Float:now = get_gametime()
    if (now < g_nextUse[id])
        return PLUGIN_HANDLED
    g_nextUse[id] = now + 1.0

    new word[16]
    copy(word, charsmax(word), text[6])
    trim(word)
    set_label(id, word)
    return PLUGIN_HANDLED
}

set_label(id, const word[])
{
    new kind = -1
    for (new k = 0; k < KIND_COUNT; k++)
        if (equali(word, KIND_WORD[k]))
            kind = k
    new bool:clear = bool:equali(word, "clear")
    if (kind < 0 && !clear)
    {
        say_to(id, "LABEL_USAGE")
        return
    }
    if (!rebb_is_building_phase() || !is_user_alive(id) || TeamName:get_member(id, m_iTeam) != TEAM_CT)
    {
        say_to(id, "LABEL_BUILD_ONLY")
        return
    }

    new entity = aimed_block(id)
    if (!entity)
    {
        say_to(id, "LABEL_AIM")
        return
    }
    if (block_owner(entity) != id)
    {
        say_to(id, "LABEL_NOT_YOURS")
        return
    }

    new n = label_of(entity)
    if (clear)
    {
        if (n >= 0)
            remove_label(n)
        say_to(id, "LABEL_CLEARED")
        return
    }

    new kindName[24]
    formatex(kindName, charsmax(kindName), "%L", id, KIND_KEY[kind])
    // One tag per block: a new one replaces it and does not use another slot.
    if (n >= 0)
    {
        g_kind[n] = kind
        g_author[n] = id
        say_to(id, "LABEL_REPLACED", kindName)
        return
    }
    if (labels_by(id) >= g_maxLabels)
    {
        say_to(id, "LABEL_LIMIT", g_maxLabels)
        return
    }
    n = label_of(0)
    if (n < 0)
    {
        say_to(id, "LABEL_FULL")
        return
    }
    g_block[n] = entity
    g_kind[n] = kind
    g_author[n] = id
    say_to(id, "LABEL_SET", kindName, labels_by(id), g_maxLabels)
}

/* The block under the player's crosshair, or 0. */
aimed_block(id)
{
    new Float:start[3], Float:viewOfs[3], Float:angles[3], Float:forwardDir[3], Float:stop[3]
    get_entvar(id, var_origin, start)
    get_entvar(id, var_view_ofs, viewOfs)
    get_entvar(id, var_v_angle, angles)
    for (new axis = 0; axis < 3; axis++)
        start[axis] += viewOfs[axis]
    angle_vector(angles, ANGLEVECTOR_FORWARD, forwardDir)
    for (new axis = 0; axis < 3; axis++)
        stop[axis] = start[axis] + forwardDir[axis] * AIM_RANGE

    new trace = create_tr2()
    engfunc(EngFunc_TraceLine, start, stop, DONT_IGNORE_MONSTERS, id, trace)
    new hit = get_tr2(trace, TR_pHit)
    free_tr2(trace)
    return is_block(hit) ? hit : 0
}

/* ---------- showing tags ---------- */

public task_aim()
{
    new players[MAX_PLAYERS], count
    get_players_ex(players, count, GetPlayers_ExcludeDead | GetPlayers_ExcludeHLTV | GetPlayers_MatchTeam, "CT")
    new Float:now = get_gametime()
    for (new i = 0; i < count; i++)
    {
        new id = players[i]
        new entity = aimed_block(id)
        new n = entity ? label_of(entity) : -1
        if (n < 0 || !still_valid(n))
        {
            g_lastSeen[id] = 0
            continue
        }
        // Show on a new block at once, then refresh before the line fades.
        if (g_lastSeen[id] == entity && now - g_lastShown[id] < REFRESH)
            continue
        g_lastSeen[id] = entity
        g_lastShown[id] = now

        new author[MAX_NAME_LENGTH], kindName[24]
        get_user_name(g_author[n], author, charsmax(author))
        formatex(kindName, charsmax(kindName), "%L", id, KIND_KEY[g_kind[n]])
        set_dhudmessage(120, 220, 255, -1.0, 0.56, 0, 0.0, 1.0, 0.0, 0.2)
        show_dhudmessage(id, "%L", id, "LABEL_HUD", kindName, author)
    }
}

/* ---------- resets ---------- */

/* ReBaseBuilder: a new build phase began. */
public rebb_build_start(buildTime)
{
    clear_all()
}

public on_restart_round_pre()
{
    clear_all()
    return HC_CONTINUE
}

public client_disconnected(id)
{
    for (new n = 0; n < MAX_LABELS; n++)
        if (g_block[n] && g_author[n] == id)
            remove_label(n)
    g_lastSeen[id] = 0
    g_nextUse[id] = 0.0
}

/* ---------- helpers ---------- */

say_to(id, const key[], any:...)
{
    new pattern[160], message[192]
    formatex(pattern, charsmax(pattern), "%L", id, key)
    vformat(message, charsmax(message), pattern, 3)
    client_print_color(id, print_team_default, "^4[Label]^1 %s", message)
}

public cmd_status(id, level, cid)
{
    if (!cmd_access(id, level, cid, 1))
        return PLUGIN_HANDLED
    new total, name[MAX_NAME_LENGTH]
    for (new n = 0; n < MAX_LABELS; n++)
    {
        if (!g_block[n])
            continue
        total++
        copy(name, charsmax(name), "-")
        if (valid_player(g_author[n]))
            get_user_name(g_author[n], name, charsmax(name))
        console_print(id, "[Label] block %d: %s by %s", g_block[n], KIND_WORD[g_kind[n]], name)
    }
    console_print(id, "[Label] %d tag(s); up to %d per builder; build phase %d", total, g_maxLabels,
        rebb_is_building_phase())
    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.