Skip to content
View in the app

A better way to browse. Learn more.

Learn more

tower-breach-register/addons/amxmodx/scripting/olds_breach.sma

olds_breach.sma
253 lines · 7.8 KB · Pawn (AMX Mod X)
Raw
/*
 * Tower Breach Register (olds.ro workshop card AMX-100)
 *
 * Tower Defence: for each lane of the map, records the first enemy that
 * damaged the tower during the wave, and when. At the end of the wave the
 * register is posted to chat:
 *     Wave 4 breaches: lane 1 (North) monster #212 at 0:12, lane 2 (Bridge) none
 * amx_breach prints the current or last register to the console.
 *
 * Adapter: Tower Defence mods have no common API, so the mod, or a small
 * bridge, reports the wave and the tower damage through three natives
 * (library "olds_breach", include/olds_breach.inc):
 *     olds_breach_wave_start(wave)          a wave began
 *     olds_breach_tower_damage(enemy)       this enemy entity just damaged the real tower
 *     olds_breach_wave_end(bool:cancelled)  the wave ended (cancelled: no report)
 * The enemy's lane is the lane box holding its origin at the moment of the
 * damage; outside every box it is not recorded. Each lane keeps only its
 * first breach: later damage ticks, the same enemy hitting again, or a new
 * enemy that reuses an old entity index never overwrite it.
 *
 * Lanes: addons/amxmodx/configs/olds_breach/<map>.ini, one box per line,
 * numbered from 1 in file order:
 *     "Lane name"  x1 y1 z1  x2 y2 z2
 *
 * Cvar: olds_breach_enable 1. Command: amx_breach (ADMIN_MAP).
 *
 * Copyright (C) 2026 daemon (olds.ro)
 * SPDX-License-Identifier: GPL-3.0-or-later
 */
#include <amxmodx>
#include <amxmisc>
#include <reapi>

#define PLUGIN  "Tower Breach Register"
#define VERSION "1.0.0"
#define AUTHOR  "olds.ro"

const MAX_LANES = 16
const LANE_NAME = 32

new g_laneName[MAX_LANES][LANE_NAME]
new Float:g_min[MAX_LANES][3]
new Float:g_max[MAX_LANES][3]
new g_lanes
new g_file[160]

new g_wave
new bool:g_waveLive
new Float:g_waveStart
new g_enemy[MAX_LANES]                 // entity index of the first breach, 0 = none
new g_enemyClass[MAX_LANES][32]
new g_seconds[MAX_LANES]
new bool:g_haveReport                  // the register holds a finished wave

new g_enable

public plugin_natives()
{
    register_library("olds_breach")
    register_native("olds_breach_wave_start", "native_wave_start")
    register_native("olds_breach_tower_damage", "native_tower_damage")
    register_native("olds_breach_wave_end", "native_wave_end")
}

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

    bind_pcvar_num(create_cvar("olds_breach_enable", "1", FCVAR_NONE,
        "1 = record tower breaches per lane and post the wave report.", true, 0.0, true, 1.0), g_enable)
    AutoExecConfig(true, "olds_breach")

    register_concmd("amx_breach", "cmd_breach", ADMIN_MAP, "- the tower breach register per lane")
    load_lanes()
}

/* ---------- lanes ---------- */

load_lanes()
{
    new map[64]
    get_mapname(map, charsmax(map))
    get_configsdir(g_file, charsmax(g_file))
    format(g_file, charsmax(g_file), "%s/olds_breach/%s.ini", g_file, map)
    new file = fopen(g_file, "rt")
    if (!file)
        return
    new line[256], name[LANE_NAME], value[6][16], number
    while (fgets(file, line, charsmax(line)))
    {
        number++
        trim(line)
        if (!line[0] || line[0] == ';' || (line[0] == '/' && line[1] == '/'))
            continue
        if (g_lanes == MAX_LANES)
        {
            log_amx("%s: more than %d lanes, the rest are ignored", g_file, MAX_LANES)
            break
        }
        if (parse(line, name, charsmax(name), value[0], charsmax(value[]), value[1], charsmax(value[]),
            value[2], charsmax(value[]), value[3], charsmax(value[]), value[4], charsmax(value[]),
            value[5], charsmax(value[])) != 7 || !name[0] || !all_numbers(value))
        {
            log_amx("%s line %d: expected ^"name^" x1 y1 z1 x2 y2 z2, skipped", g_file, number)
            continue
        }
        for (new axis = 0; axis < 3; axis++)
        {
            new Float:a = str_to_float(value[axis]), Float:b = str_to_float(value[axis + 3])
            g_min[g_lanes][axis] = floatmin(a, b)
            g_max[g_lanes][axis] = floatmax(a, b)
        }
        copy(g_laneName[g_lanes], charsmax(g_laneName[]), name)
        g_lanes++
    }
    fclose(file)
}

bool:all_numbers(const value[6][16])
{
    for (new v = 0; v < 6; 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
}

lane_at(const Float:origin[3])
{
    for (new l = 0; l < g_lanes; l++)
        if (g_min[l][0] <= origin[0] <= g_max[l][0] && g_min[l][1] <= origin[1] <= g_max[l][1]
            && g_min[l][2] <= origin[2] <= g_max[l][2])
            return l
    return -1
}

/* ---------- natives for the mod ---------- */

public native_wave_start(plugin, params)
{
    g_wave = get_param(1)
    g_waveLive = true
    g_waveStart = get_gametime()
    g_haveReport = false
    for (new l = 0; l < MAX_LANES; l++)
    {
        g_enemy[l] = 0
        g_enemyClass[l][0] = EOS
        g_seconds[l] = 0
    }
    return 1
}

public native_tower_damage(plugin, params)
{
    new enemy = get_param(1)
    if (!g_enable || !g_waveLive || enemy <= MaxClients || is_nullent(enemy))
        return 0
    new Float:origin[3]
    get_entvar(enemy, var_origin, origin)
    new lane = lane_at(origin)
    // Only the first breach of each lane: later ticks and reused indices change nothing.
    if (lane < 0 || g_enemy[lane])
        return 0
    g_enemy[lane] = enemy
    get_entvar(enemy, var_classname, g_enemyClass[lane], charsmax(g_enemyClass[]))
    g_seconds[lane] = floatround(get_gametime() - g_waveStart, floatround_floor)
    return 1
}

public native_wave_end(plugin, params)
{
    if (!g_waveLive)
        return 0
    g_waveLive = false
    if (get_param(1))
    {
        // Cancelled: no report, and the partial register is dropped.
        for (new l = 0; l < MAX_LANES; l++)
            g_enemy[l] = 0
        return 1
    }
    g_haveReport = true
    if (g_enable && g_lanes)
        post_report()
    return 1
}

/* ---------- reports ---------- */

lane_text(l, output[], length, id)
{
    if (!g_enemy[l])
        formatex(output, length, "%L", id, "BREACH_LANE_NONE", l + 1, g_laneName[l])
    else
        formatex(output, length, "%L", id, "BREACH_LANE", l + 1, g_laneName[l], g_enemyClass[l], g_enemy[l],
            g_seconds[l] / 60, g_seconds[l] % 60)
}

post_report()
{
    new line[192], part[96]
    for (new p = 1; p <= MaxClients; p++)
    {
        if (!is_user_connected(p) || is_user_bot(p))
            continue
        client_print_color(p, print_team_default, "^4[Breach]^1 %L", p, "BREACH_TITLE", g_wave)
        line[0] = EOS
        for (new l = 0; l < g_lanes; l++)
        {
            lane_text(l, part, charsmax(part), p)
            // Two lanes per chat line keeps lines short.
            if (line[0])
                add(line, charsmax(line), "; ")
            add(line, charsmax(line), part)
            if (l % 2 == 1 || l == g_lanes - 1)
            {
                client_print_color(p, print_team_default, "^4[Breach]^1 %s", line)
                line[0] = EOS
            }
        }
    }
}

public cmd_breach(id, level, cid)
{
    if (!cmd_access(id, level, cid, 1))
        return PLUGIN_HANDLED
    if (!g_lanes)
    {
        console_print(id, "[Breach] no lanes for this map (%s)", g_file)
        return PLUGIN_HANDLED
    }
    console_print(id, "[Breach] wave %d %s; recording %s", g_wave,
        g_waveLive ? "running" : (g_haveReport ? "finished" : "not started"), g_enable ? "on" : "off")
    new part[96]
    for (new l = 0; l < g_lanes; l++)
    {
        lane_text(l, part, charsmax(part), id)
        console_print(id, "[Breach]   %s", part)
    }
    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.