Skip to content
View in the app

A better way to browse. Learn more.

Learn more

barrier-zones/addons/amxmodx/scripting/olds_barrier.sma

olds_barrier.sma
554 lines · 16.1 KB · Pawn (AMX Mod X)
Raw
/*
 * Barrier Zones (olds.ro workshop card LIB-1015)
 *
 * Mapping: admins draw invisible boxes that block, slow or hurt a chosen team,
 * for custom rules such as keeping the terrorists out of a base.
 *   amx_barrier add <t|ct|all> <block|slow|damage> [<x1> <y1> <z1> <x2> <y2> <z2>]
 *                        without corners (in game): a 128-unit box on you
 *   amx_barrier del <n>
 *   amx_barrier list     the boxes, and where every living player is
 *   amx_barrier show     your preview of the boxes on or off (in game)
 * All need ADMIN_CFG. Boxes are saved at once to
 * addons/amxmodx/configs/barriers/<map>.ini ("x1 y1 z1 x2 y2 z2 team action")
 * and loaded at map start.
 *
 * Cvars: olds_barrier_dmg 5 (damage per second in a damage box),
 * olds_barrier_slow 0.5 (speed factor in a slow box).
 *
 * FM_PlayerPreThink tests each living player of the box's team against the
 * box (the player's own box touching it):
 *   block    pev_velocity pushes him out through the nearest side
 *   slow     his top speed is olds_barrier_slow of normal while inside
 *            (applied after every RG_CBasePlayer_ResetMaxSpeed)
 *   damage   ExecuteHamB(Ham_TakeDamage) every second while inside
 * The preview is for admins who turned it on: laser beams along the edges
 * (sprites/laserbeam.spr) and a TE_BOX when a box is added. Other players see
 * nothing.
 *
 * Copyright (C) 2026 daemon (olds.ro)
 * SPDX-License-Identifier: GPL-3.0-or-later
 */
#include <amxmodx>
#include <amxmisc>
#include <fakemeta>
#include <hamsandwich>
#include <reapi>

#define PLUGIN  "Barrier Zones"
#define VERSION "1.0.0"
#define AUTHOR  "olds.ro"

const MAX_BOXES = 64
const ACCESS = ADMIN_CFG
const TASK_PREVIEW = 10150
const Float:PREVIEW_EVERY = 1.0
const Float:DAMAGE_EVERY = 1.0
const Float:PUSH = 320.0
const Float:HALF_W = 16.0
const Float:HALF_H = 36.0
const Float:HAND_BOX = 64.0

enum
{
    TEAM_T_ONLY = 1,
    TEAM_CT_ONLY = 2,
    TEAM_BOTH = 3
}
enum
{
    ACT_BLOCK = 0,
    ACT_SLOW,
    ACT_DAMAGE,
    ACTIONS
}
new const ACTION_WORD[ACTIONS][] = { "block", "slow", "damage" }
new const ACTION_COLOR[ACTIONS][3] = { { 255, 60, 40 }, { 60, 160, 255 }, { 255, 200, 40 } }
new const BEAM_SPRITE[] = "sprites/laserbeam.spr"

new Float:g_min[MAX_BOXES][3], Float:g_max[MAX_BOXES][3]
new g_team[MAX_BOXES], g_action[MAX_BOXES]
new g_count

new Float:g_damage, Float:g_slow
new g_file[160]
new g_beam

new bool:g_slowed[MAX_PLAYERS + 1]
new Float:g_nextHurt[MAX_PLAYERS + 1]
new bool:g_preview[MAX_PLAYERS + 1]

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

public plugin_init()
{
    register_plugin(PLUGIN, VERSION, AUTHOR)
    register_dictionary("olds_barrier.txt")
    bind_pcvar_float(create_cvar("olds_barrier_dmg", "5", FCVAR_NONE,
        "Damage per second inside a damage barrier.", true, 0.0, true, 100.0), g_damage)
    bind_pcvar_float(create_cvar("olds_barrier_slow", "0.5", FCVAR_NONE,
        "Speed factor inside a slow barrier.", true, 0.05, true, 1.0), g_slow)
    AutoExecConfig(true, "olds_barrier")

    register_forward(FM_PlayerPreThink, "on_prethink")
    RegisterHookChain(RG_CBasePlayer_ResetMaxSpeed, "on_reset_maxspeed_post", true)
    register_concmd("amx_barrier", "cmd_barrier", ACCESS, "add <t|ct|all> <block|slow|damage> [x1 y1 z1 x2 y2 z2] | del <n> | list | show")
    set_task(PREVIEW_EVERY, "task_preview", TASK_PREVIEW, _, _, "b")

    new map[64]
    get_mapname(map, charsmax(map))
    strtolower(map)
    get_configsdir(g_file, charsmax(g_file))
    format(g_file, charsmax(g_file), "%s/barriers/%s.ini", g_file, map)
}

public plugin_cfg()
{
    load_boxes()
}

public client_disconnected(id)
{
    g_slowed[id] = false
    g_nextHurt[id] = 0.0
    g_preview[id] = false
}

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

// Corners in any order; false for a flat box.
bool:put_box(slot, const Float:a[3], const Float:b[3], team, action)
{
    for (new axis = 0; axis < 3; axis++)
    {
        g_min[slot][axis] = floatmin(a[axis], b[axis])
        g_max[slot][axis] = floatmax(a[axis], b[axis])
        if (g_max[slot][axis] - g_min[slot][axis] < 1.0)
            return false
    }
    g_team[slot] = team
    g_action[slot] = action
    return true
}

team_of(const word[])
{
    if (equali(word, "t"))
        return TEAM_T_ONLY
    if (equali(word, "ct"))
        return TEAM_CT_ONLY
    if (equali(word, "all"))
        return TEAM_BOTH
    return 0
}

action_of(const word[])
{
    for (new a = 0; a < ACTIONS; a++)
        if (equali(word, ACTION_WORD[a]))
            return a
    return -1
}

team_word(team, out[], len)
{
    switch (team)
    {
        case TEAM_T_ONLY: copy(out, len, "t")
        case TEAM_CT_ONLY: copy(out, len, "ct")
        default: copy(out, len, "all")
    }
}

bool:is_number(const text[])
{
    if (!text[0])
        return false
    new dots
    for (new i = 0; text[i]; i++)
    {
        if (text[i] == '-' && i == 0)
            continue
        if (text[i] == '.' && !dots++)
            continue
        if (!isdigit(text[i]))
            return false
    }
    return true
}

load_boxes()
{
    g_count = 0
    new file = fopen(g_file, "rt")
    if (!file)
        return
    new line[160], arg[8][16], number
    while (fgets(file, line, charsmax(line)) && g_count < MAX_BOXES)
    {
        number++
        trim(line)
        if (!line[0] || line[0] == ';')
            continue
        new parts = parse(line, arg[0], charsmax(arg[]), arg[1], charsmax(arg[]), arg[2], charsmax(arg[]), arg[3],
            charsmax(arg[]), arg[4], charsmax(arg[]), arg[5], charsmax(arg[]), arg[6], charsmax(arg[]), arg[7], charsmax(arg[]))
        new bool:ok = parts == 8
        for (new i = 0; i < 6 && ok; i++)
            ok = is_number(arg[i])
        new team = team_of(arg[6]), action = action_of(arg[7])
        new Float:a[3], Float:b[3]
        if (ok && team && action >= 0)
        {
            for (new axis = 0; axis < 3; axis++)
            {
                a[axis] = str_to_float(arg[axis])
                b[axis] = str_to_float(arg[axis + 3])
            }
            ok = put_box(g_count, a, b, team, action)
        }
        else
            ok = false
        if (ok)
            g_count++
        else
            log_amx("%s line %d: expected x1 y1 z1 x2 y2 z2 <t|ct|all> <block|slow|damage> (not flat), skipped", g_file,
                number)
    }
    fclose(file)
}

bool:save_boxes()
{
    new dir[160]
    get_configsdir(dir, charsmax(dir))
    add(dir, charsmax(dir), "/barriers")
    if (!dir_exists(dir))
        mkdir(dir)
    new file = fopen(g_file, "wt")
    if (!file)
    {
        log_amx("cannot write %s", g_file)
        return false
    }
    fputs(file, "; Barrier Zones: x1 y1 z1 x2 y2 z2 team(t|ct|all) action(block|slow|damage)^n")
    new team[4]
    for (new i = 0; i < g_count; i++)
    {
        team_word(g_team[i], team, charsmax(team))
        fprintf(file, "%.0f %.0f %.0f %.0f %.0f %.0f %s %s^n", g_min[i][0], g_min[i][1], g_min[i][2], g_max[i][0],
            g_max[i][1], g_max[i][2], team, ACTION_WORD[g_action[i]])
    }
    fclose(file)
    return true
}

/* ---------- players ---------- */

bool:applies(box, id)
{
    new TeamName:team = TeamName:get_member(id, m_iTeam)
    if (team == TEAM_TERRORIST)
        return (g_team[box] & TEAM_T_ONLY) != 0
    if (team == TEAM_CT)
        return (g_team[box] & TEAM_CT_ONLY) != 0
    return false
}

bool:touches(box, const Float:origin[3])
{
    return origin[0] > g_min[box][0] - HALF_W && origin[0] < g_max[box][0] + HALF_W
        && origin[1] > g_min[box][1] - HALF_W && origin[1] < g_max[box][1] + HALF_W
        && origin[2] > g_min[box][2] - HALF_H && origin[2] < g_max[box][2] + HALF_H
}

push_out(id, box, const Float:origin[3])
{
    // The side the player is closest to; out through it.
    new Float:best = 999999.0, bestAxis, Float:bestSign
    new Float:lo[3], Float:hi[3]
    lo[0] = g_min[box][0] - HALF_W; hi[0] = g_max[box][0] + HALF_W
    lo[1] = g_min[box][1] - HALF_W; hi[1] = g_max[box][1] + HALF_W
    lo[2] = g_min[box][2] - HALF_H; hi[2] = g_max[box][2] + HALF_H
    for (new axis = 0; axis < 3; axis++)
    {
        new Float:down = origin[axis] - lo[axis], Float:up = hi[axis] - origin[axis]
        if (down < best)
        {
            best = down
            bestAxis = axis
            bestSign = -1.0
        }
        if (up < best)
        {
            best = up
            bestAxis = axis
            bestSign = 1.0
        }
    }
    new Float:velocity[3]
    get_entvar(id, var_velocity, velocity)
    if (bestAxis == 2)
        velocity[2] = bestSign * PUSH
    else
    {
        velocity[0] = 0.0
        velocity[1] = 0.0
        velocity[bestAxis] = bestSign * PUSH
    }
    set_entvar(id, var_velocity, velocity)
}

public on_prethink(id)
{
    if (!is_user_alive(id))
    {
        g_slowed[id] = false
        return FMRES_IGNORED
    }
    new Float:origin[3], bool:slow, bool:hurt, pushBox = -1
    get_entvar(id, var_origin, origin)
    for (new box = 0; box < g_count; box++)
    {
        if (!touches(box, origin) || !applies(box, id))
            continue
        switch (g_action[box])
        {
            case ACT_BLOCK: pushBox = box
            case ACT_SLOW: slow = true
            case ACT_DAMAGE: hurt = true
        }
    }
    if (pushBox >= 0)
        push_out(id, pushBox, origin)
    if (slow != g_slowed[id])
    {
        g_slowed[id] = slow
        rg_reset_maxspeed(id)
    }
    if (hurt && g_damage > 0.0)
    {
        new Float:now = get_gametime()
        if (now >= g_nextHurt[id])
        {
            g_nextHurt[id] = now + DAMAGE_EVERY
            ExecuteHamB(Ham_TakeDamage, id, 0, 0, g_damage, DMG_GENERIC)
        }
    }
    return FMRES_IGNORED
}

public on_reset_maxspeed_post(id)
{
    if (g_slowed[id] && is_user_alive(id))
        set_entvar(id, var_maxspeed, Float:get_entvar(id, var_maxspeed) * g_slow)
}

/* ---------- preview ---------- */

beam(id, const Float:a[3], const Float:b[3], const color[3])
{
    message_begin(MSG_ONE_UNRELIABLE, SVC_TEMPENTITY, _, id)
    write_byte(TE_BEAMPOINTS)
    write_coord_f(a[0])
    write_coord_f(a[1])
    write_coord_f(a[2])
    write_coord_f(b[0])
    write_coord_f(b[1])
    write_coord_f(b[2])
    write_short(g_beam)
    write_byte(0)
    write_byte(0)
    write_byte(floatround(PREVIEW_EVERY * 10.0) + 1)
    write_byte(8)
    write_byte(0)
    write_byte(color[0])
    write_byte(color[1])
    write_byte(color[2])
    write_byte(200)
    write_byte(0)
    message_end()
}

draw_box(id, box)
{
    new Float:c[8][3]
    for (new i = 0; i < 8; i++)
    {
        c[i][0] = (i & 1) ? g_max[box][0] : g_min[box][0]
        c[i][1] = (i & 2) ? g_max[box][1] : g_min[box][1]
        c[i][2] = (i & 4) ? g_max[box][2] : g_min[box][2]
    }
    new const edges[12][2] = { {0,1}, {2,3}, {4,5}, {6,7}, {0,2}, {1,3}, {4,6}, {5,7}, {0,4}, {1,5}, {2,6}, {3,7} }
    for (new e = 0; e < 12; e++)
        beam(id, c[edges[e][0]], c[edges[e][1]], ACTION_COLOR[g_action[box]])
}

flash_box(box)
{
    message_begin(MSG_BROADCAST, SVC_TEMPENTITY)
    write_byte(TE_BOX)
    write_coord_f(g_min[box][0])
    write_coord_f(g_min[box][1])
    write_coord_f(g_min[box][2])
    write_coord_f(g_max[box][0])
    write_coord_f(g_max[box][1])
    write_coord_f(g_max[box][2])
    write_short(30)
    write_byte(ACTION_COLOR[g_action[box]][0])
    write_byte(ACTION_COLOR[g_action[box]][1])
    write_byte(ACTION_COLOR[g_action[box]][2])
    message_end()
}

public task_preview()
{
    for (new id = 1; id <= MaxClients; id++)
    {
        if (!g_preview[id] || !is_user_connected(id) || !(get_user_flags(id) & ACCESS))
            continue
        for (new box = 0; box < g_count; box++)
            draw_box(id, box)
    }
}

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

public cmd_barrier(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, "add"))
        cmd_add(id)
    else if (equali(action, "del"))
    {
        new number = read_argv_int(2)
        if (!(1 <= number <= g_count))
            console_print(id, "[Barrier] no barrier %d (see amx_barrier list)", number)
        else
        {
            for (new i = number - 1; i < g_count - 1; i++)
            {
                for (new axis = 0; axis < 3; axis++)
                {
                    g_min[i][axis] = g_min[i + 1][axis]
                    g_max[i][axis] = g_max[i + 1][axis]
                }
                g_team[i] = g_team[i + 1]
                g_action[i] = g_action[i + 1]
            }
            g_count--
            save_boxes()
            console_print(id, "[Barrier] barrier %d deleted, %d left (saved)", number, g_count)
        }
    }
    else if (equali(action, "list"))
        list(id)
    else if (equali(action, "show"))
    {
        if (!id)
            console_print(id, "[Barrier] the preview is for players in game")
        else
        {
            g_preview[id] = !g_preview[id]
            if (g_preview[id])
                client_print_color(id, print_team_default, "^4[Barrier]^1 %L", id, "BARRIER_SHOW_ON")
            else
                client_print_color(id, print_team_default, "^4[Barrier]^1 %L", id, "BARRIER_SHOW_OFF")
        }
    }
    else
        console_print(id, "[Barrier] usage: amx_barrier add <t|ct|all> <block|slow|damage> [x1 y1 z1 x2 y2 z2] | del <n> | list | show")
    return PLUGIN_HANDLED
}

cmd_add(id)
{
    new teamWord[8], actionWord[12]
    read_argv(2, teamWord, charsmax(teamWord))
    read_argv(3, actionWord, charsmax(actionWord))
    new team = team_of(teamWord), action = action_of(actionWord)
    if (!team || action < 0)
    {
        console_print(id, "[Barrier] usage: amx_barrier add <t|ct|all> <block|slow|damage> [x1 y1 z1 x2 y2 z2]")
        return
    }
    if (g_count >= MAX_BOXES)
    {
        console_print(id, "[Barrier] at most %d barriers per map", MAX_BOXES)
        return
    }
    new Float:a[3], Float:b[3]
    if (read_argc() >= 10)
    {
        new word[16]
        for (new i = 0; i < 6; i++)
        {
            read_argv(4 + i, word, charsmax(word))
            if (!is_number(word))
            {
                console_print(id, "[Barrier] corner %d is not a number: %s", i + 1, word)
                return
            }
            if (i < 3)
                a[i] = str_to_float(word)
            else
                b[i - 3] = str_to_float(word)
        }
    }
    else if (id && is_user_alive(id))
    {
        new Float:origin[3]
        get_entvar(id, var_origin, origin)
        for (new axis = 0; axis < 3; axis++)
        {
            a[axis] = origin[axis] - HAND_BOX
            b[axis] = origin[axis] + HAND_BOX
        }
    }
    else
    {
        console_print(id, "[Barrier] give the two corners (x1 y1 z1 x2 y2 z2)")
        return
    }
    if (!put_box(g_count, a, b, team, action))
    {
        console_print(id, "[Barrier] the box is flat: every side must be at least 1 unit")
        return
    }
    g_count++
    flash_box(g_count - 1)
    save_boxes()
    console_print(id, "[Barrier] barrier %d: %s %s from %.0f %.0f %.0f to %.0f %.0f %.0f (saved)", g_count, teamWord,
        ACTION_WORD[action], g_min[g_count - 1][0], g_min[g_count - 1][1], g_min[g_count - 1][2], g_max[g_count - 1][0],
        g_max[g_count - 1][1], g_max[g_count - 1][2])
}

list(id)
{
    console_print(id, "[Barrier] %d barrier(s) in %s; damage %.0f/s, slow x%.2f", g_count, g_file, g_damage, g_slow)
    new team[4]
    for (new i = 0; i < g_count; i++)
    {
        team_word(g_team[i], team, charsmax(team))
        console_print(id, "[Barrier]   %d. %s %s from %.0f %.0f %.0f to %.0f %.0f %.0f", i + 1, team, ACTION_WORD[g_action[i]],
            g_min[i][0], g_min[i][1], g_min[i][2], g_max[i][0], g_max[i][1], g_max[i][2])
    }
    new name[MAX_NAME_LENGTH], Float:origin[3]
    for (new p = 1; p <= MaxClients; p++)
    {
        if (!is_user_alive(p))
            continue
        get_user_name(p, name, charsmax(name))
        get_entvar(p, var_origin, origin)
        new inside
        for (new box = 0; box < g_count; box++)
            if (touches(box, origin))
                inside = box + 1
        console_print(id, "[Barrier]   %s: team %d at %.0f %.0f %.0f, in barrier %d, health %d, maxspeed %.0f", name,
            get_member(p, m_iTeam), origin[0], origin[1], origin[2], inside, get_user_health(p), Float:get_entvar(p, var_maxspeed))
    }
}

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.