Skip to content
View in the app

A better way to browse. Learn more.

Learn more

admin-beam-drawing/addons/amxmodx/scripting/olds_abeam.sma

olds_abeam.sma
326 lines · 9.8 KB · Pawn (AMX Mod X)
Raw
/*
 * Admin Beam Drawing (olds.ro) - admins draw temporary lines on the map to
 * mark routes or problems; lines vanish after olds_abeam_life seconds, on
 * amx_beam clear, or at the end of the round.
 *
 * 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. You should have received a copy of the GNU General Public
 * License along with this program. If not, see <https://www.gnu.org/licenses/>.
 */

#include <amxmodx>
#include <amxmisc>
#include <fakemeta>

#define PLUGIN  "Admin Beam Drawing"
#define VERSION "1.0.0"
#define AUTHOR  "daemon (olds.ro)"

#define MAX_LINES 64
#define TASK_DRAW 10100

new const BEAM_SPRITE[] = "sprites/laserbeam.spr"
new const DOT_SPRITE[] = "sprites/white.spr"

new Float:g_from[MAX_LINES][3]
new Float:g_to[MAX_LINES][3]
new Float:g_expires[MAX_LINES]       // 0.0 = until the round ends
new g_color[MAX_LINES][3]
new g_width[MAX_LINES]
new g_lines                          // lines in use, oldest first

new Float:g_start[MAX_PLAYERS + 1][3]
new bool:g_hasStart[MAX_PLAYERS + 1]

new g_beam, g_dot
new g_cvarLife, g_cvarWidth, g_cvarColor, g_cvarMax

public plugin_precache()
{
    g_beam = precache_model(BEAM_SPRITE)
    g_dot = precache_model(DOT_SPRITE)
}

public plugin_init()
{
    register_plugin(PLUGIN, VERSION, AUTHOR)

    g_cvarLife = create_cvar("olds_abeam_life", "30", FCVAR_NONE, "Seconds a line stays; 0 = until the round ends", true, 0.0, true, 3600.0)
    g_cvarWidth = create_cvar("olds_abeam_width", "8", FCVAR_NONE, "Line width (1-100)", true, 1.0, true, 100.0)
    g_cvarColor = create_cvar("olds_abeam_color", "255 0 0", FCVAR_NONE, "Line colour as ^"red green blue^", each 0-255")
    g_cvarMax = create_cvar("olds_abeam_max", "16", FCVAR_NONE, "Most lines at once; a new line past this removes the oldest", true, 1.0, true, float(MAX_LINES))
    AutoExecConfig(true, "olds_abeam")

    register_concmd("amx_beam", "cmd_beam", ADMIN_BAN, "<start | end | clear | list> [x y z] - draw a line from where you aim")
    register_logevent("on_round_end", 2, "1=Round_End")
    set_task(1.0, "task_draw", TASK_DRAW, _, _, "b")
}

public client_disconnected(id)
{
    g_hasStart[id] = false
}

public on_round_end()
{
    g_lines = 0
}

/* ---------- command ---------- */

public cmd_beam(id, level, cid)
{
    if (!cmd_access(id, level, cid, 2))
        return PLUGIN_HANDLED

    new action[8]
    read_argv(1, action, charsmax(action))
    if (equali(action, "clear"))
    {
        new had = g_lines
        g_lines = 0
        console_print(id, "[Beam] %d line(s) removed.", had)
        note_clear(id, had)
        return PLUGIN_HANDLED
    }
    if (equali(action, "list"))
    {
        list_lines(id)
        return PLUGIN_HANDLED
    }
    new bool:isStart = bool:equali(action, "start")
    if (!isStart && !equali(action, "end"))
    {
        console_print(id, "[Beam] Usage: amx_beam start | end | clear | list   (from the server console: start|end x y z)")
        return PLUGIN_HANDLED
    }

    new Float:point[3]
    if (!get_point(id, point))
        return PLUGIN_HANDLED

    if (isStart)
    {
        g_start[id] = point
        g_hasStart[id] = true
        console_print(id, "[Beam] Start set at %.0f %.0f %.0f. Aim at the end and use amx_beam end.", point[0], point[1], point[2])
        return PLUGIN_HANDLED
    }
    if (!g_hasStart[id])
    {
        console_print(id, "[Beam] Set a start first with amx_beam start.")
        return PLUGIN_HANDLED
    }
    g_hasStart[id] = false
    new removed = add_line(g_start[id], point)
    if (removed)
        console_print(id, "[Beam] Line drawn; the oldest line was removed (limit %d).", get_pcvar_num(g_cvarMax))
    else
        console_print(id, "[Beam] Line drawn (%d on the map).", g_lines)
    note_line(id, g_start[id], point)
    task_draw()
    return PLUGIN_HANDLED
}

note_clear(id, count)
{
    new name[32]
    get_user_name(id, name, charsmax(name))
    log_amx("[Beam] %s cleared %d line(s)", name, count)
}

note_line(id, const Float:from[3], const Float:to[3])
{
    new name[32]
    get_user_name(id, name, charsmax(name))
    log_amx("[Beam] %s drew a line %.0f %.0f %.0f -> %.0f %.0f %.0f", name, from[0], from[1], from[2], to[0], to[1], to[2])
}

bool:get_point(id, Float:point[3])
{
    if (read_argc() >= 5)
    {
        new value[16]
        for (new axis = 0; axis < 3; axis++)
        {
            read_argv(2 + axis, value, charsmax(value))
            if (!is_number(value))
            {
                console_print(id, "[Beam] The coordinates must be three numbers.")
                return false
            }
            point[axis] = floatclamp(str_to_float(value), -16384.0, 16384.0)
        }
        return true
    }
    if (!id || !is_user_connected(id))
    {
        console_print(id, "[Beam] From the server console give the point: amx_beam start|end x y z")
        return false
    }
    aim_point(id, point)
    return true
}

// Where the admin's crosshair meets the world (or the 8192-unit limit).
aim_point(id, Float:point[3])
{
    new Float:eyes[3], Float:offset[3], Float:angles[3], Float:dir[3], Float:far[3]
    pev(id, pev_origin, eyes)
    pev(id, pev_view_ofs, offset)
    eyes[0] += offset[0]
    eyes[1] += offset[1]
    eyes[2] += offset[2]
    pev(id, pev_v_angle, angles)
    engfunc(EngFunc_MakeVectors, angles)
    global_get(glb_v_forward, dir)
    for (new axis = 0; axis < 3; axis++)
        far[axis] = eyes[axis] + dir[axis] * 8192.0
    new trace = create_tr2()
    engfunc(EngFunc_TraceLine, eyes, far, DONT_IGNORE_MONSTERS, id, trace)
    get_tr2(trace, TR_vecEndPos, point)
    free_tr2(trace)
}

bool:is_number(const text[])
{
    new i, digits, dots
    if (text[0] == '-' || text[0] == '+')
        i++
    for (; text[i]; i++)
    {
        if (text[i] == '.')
        {
            if (++dots > 1)
                return false
        }
        else if ('0' <= text[i] <= '9')
            digits++
        else
            return false
    }
    return digits > 0
}

/* ---------- lines ---------- */

remove_line(index)
{
    for (new i = index; i < g_lines - 1; i++)
    {
        g_from[i] = g_from[i + 1]
        g_to[i] = g_to[i + 1]
        g_expires[i] = g_expires[i + 1]
        g_color[i] = g_color[i + 1]
        g_width[i] = g_width[i + 1]
    }
    g_lines--
}

// Returns 1 when the oldest line made room for this one.
add_line(const Float:from[3], const Float:to[3])
{
    new removed
    new limit = clamp(get_pcvar_num(g_cvarMax), 1, MAX_LINES)
    while (g_lines >= limit)
    {
        remove_line(0)
        removed = 1
    }
    new line = g_lines++
    g_from[line] = from
    g_to[line] = to
    new Float:life = get_pcvar_float(g_cvarLife)
    g_expires[line] = life > 0.0 ? get_gametime() + life : 0.0
    g_width[line] = clamp(get_pcvar_num(g_cvarWidth), 1, 100)
    read_color(g_color[line])
    return removed
}

read_color(color[3])
{
    new text[24], part[3][6]
    get_pcvar_string(g_cvarColor, text, charsmax(text))
    if (parse(text, part[0], charsmax(part[]), part[1], charsmax(part[]), part[2], charsmax(part[])) == 3
        && is_number(part[0]) && is_number(part[1]) && is_number(part[2]))
    {
        for (new i = 0; i < 3; i++)
            color[i] = clamp(str_to_num(part[i]), 0, 255)
        return
    }
    log_amx("[Beam] olds_abeam_color ^"%s^" is not ^"red green blue^"; using red.", text)
    color[0] = 255
    color[1] = 0
    color[2] = 0
}

public task_draw()
{
    new Float:now = get_gametime()
    for (new i = g_lines - 1; i >= 0; i--)
    {
        if (g_expires[i] > 0.0 && now >= g_expires[i])
            remove_line(i)
    }
    for (new i = 0; i < g_lines; i++)
    {
        message_begin(MSG_BROADCAST, SVC_TEMPENTITY)
        write_byte(TE_BEAMPOINTS)
        write_coord_f(g_from[i][0])
        write_coord_f(g_from[i][1])
        write_coord_f(g_from[i][2])
        write_coord_f(g_to[i][0])
        write_coord_f(g_to[i][1])
        write_coord_f(g_to[i][2])
        write_short(g_beam)
        write_byte(0)               // start frame
        write_byte(0)               // frame rate
        write_byte(11)              // life, tenths of a second (redrawn every second)
        write_byte(g_width[i])
        write_byte(0)               // noise
        write_byte(g_color[i][0])
        write_byte(g_color[i][1])
        write_byte(g_color[i][2])
        write_byte(200)             // brightness
        write_byte(0)               // scroll speed
        message_end()
        dot(g_from[i])
        dot(g_to[i])
    }
}

dot(const Float:at[3])
{
    message_begin(MSG_BROADCAST, SVC_TEMPENTITY)
    write_byte(TE_GLOWSPRITE)
    write_coord_f(at[0])
    write_coord_f(at[1])
    write_coord_f(at[2])
    write_short(g_dot)
    write_byte(11)                  // life, tenths of a second
    write_byte(2)                   // scale, tenths
    write_byte(180)                 // brightness
    message_end()
}

list_lines(id)
{
    new Float:now = get_gametime()
    console_print(id, "[Beam] %d line(s), limit %d, life %.0f s", g_lines, get_pcvar_num(g_cvarMax), get_pcvar_float(g_cvarLife))
    for (new i = 0; i < g_lines; i++)
    {
        new left = g_expires[i] > 0.0 ? floatround(g_expires[i] - now, floatround_ceil) : -1
        console_print(id, "  %d. %.0f %.0f %.0f -> %.0f %.0f %.0f  colour %d %d %d, width %d, %d s left (-1 = round end)", i + 1,
            g_from[i][0], g_from[i][1], g_from[i][2], g_to[i][0], g_to[i][1], g_to[i][2],
            g_color[i][0], g_color[i][1], g_color[i][2], g_width[i], left)
    }
}

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.