Skip to content
View in the app

A better way to browse. Learn more.

Learn more

trap-interval-audit/addons/amxmodx/scripting/olds_trapaudit.sma

olds_trapaudit.sma
249 lines · 7.4 KB · Pawn (AMX Mod X)
Raw
/*
 * Trap Interval Audit (olds.ro) - deathrun mapping aid. For the trap buttons
 * listed per map it records every accepted activation and shows admins the
 * shortest and longest time between two of them next to the intended
 * cooldown. It only watches: the buttons are never changed.
 *
 * 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>
#include <hamsandwich>

#define PLUGIN  "Trap Interval Audit"
#define VERSION "1.0.0"
#define AUTHOR  "daemon (olds.ro)"

#define MAX_TRAPS 64
#define TS_AT_BOTTOM 1              // CBaseToggle states (hlsdk)

new g_key[MAX_TRAPS][32]            // "*12" (brush model) or a targetname
new g_label[MAX_TRAPS][32]
new Float:g_intended[MAX_TRAPS]
new g_ent[MAX_TRAPS]                // the func_button found for the line, 0 = not found
new g_traps

new g_uses[MAX_TRAPS]
new Float:g_last[MAX_TRAPS]
new Float:g_min[MAX_TRAPS]
new Float:g_max[MAX_TRAPS]

new g_stateBefore[MAX_TRAPS]        // m_toggle_state seen in the pre hook

public plugin_init()
{
    register_plugin(PLUGIN, VERSION, AUTHOR)
    register_concmd("amx_trapaudit", "cmd_audit", ADMIN_MAP, "[reset] - shortest and longest reuse interval per trap button")
    RegisterHam(Ham_Use, "func_button", "on_activate_pre", 0)
    RegisterHam(Ham_Use, "func_button", "on_activate_post", 1)
    RegisterHam(Ham_Touch, "func_button", "on_touch_pre", 0)
    RegisterHam(Ham_Touch, "func_button", "on_touch_post", 1)
    register_event("HLTV", "on_new_round", "a", "1=0", "2=0")
}

public plugin_cfg()
{
    load_config()
    resolve()
}

/* ---------- configuration ---------- */

load_config()
{
    new path[192], map[64]
    get_configsdir(path, charsmax(path))
    get_mapname(map, charsmax(map))
    format(path, charsmax(path), "%s/olds_trapaudit/%s.ini", path, map)
    new file = fopen(path, "rt")
    if (!file)
    {
        server_print("[TrapAudit] No trap list for %s (%s): nothing is audited on this map.", map, path)
        return
    }
    new line[160], key[32], seconds[16], label[32], lineNo
    while (!feof(file) && fgets(file, line, charsmax(line)))
    {
        lineNo++
        trim(line)
        if (!line[0] || line[0] == ';' || line[0] == '#')
            continue
        new parts = parse(line, key, charsmax(key), seconds, charsmax(seconds), label, charsmax(label))
        new Float:value = str_to_float(seconds)
        if (parts < 2 || !key[0] || value <= 0.0)
        {
            log_amx("[TrapAudit] %s line %d skipped: expected ^"targetname or *model^" <seconds> [^"label^"]", path, lineNo)
            continue
        }
        if (g_traps >= MAX_TRAPS)
        {
            log_amx("[TrapAudit] %s: more than %d buttons, the rest are skipped", path, MAX_TRAPS)
            break
        }
        copy(g_key[g_traps], charsmax(g_key[]), key)
        if (parts >= 3)
            copy(g_label[g_traps], charsmax(g_label[]), label)
        else
            copy(g_label[g_traps], charsmax(g_label[]), key)
        g_intended[g_traps] = value
        g_traps++
    }
    fclose(file)
}

// Finds each line's func_button again: entity numbers are checked after every round reset.
resolve()
{
    for (new t = 0; t < g_traps; t++)
    {
        new field[12], ent = -1, found
        if (g_key[t][0] == '*')
            copy(field, charsmax(field), "model")
        else
            copy(field, charsmax(field), "targetname")
        while ((ent = engfunc(EngFunc_FindEntityByString, ent, field, g_key[t])) > 0)
        {
            new classname[16]
            pev(ent, pev_classname, classname, charsmax(classname))
            if (equal(classname, "func_button"))
            {
                found = ent
                break
            }
        }
        if (found != g_ent[t] && g_ent[t])
            log_amx("[TrapAudit] %s: button entity changed from #%d to #%d", g_label[t], g_ent[t], found)
        if (!found)
            log_amx("[TrapAudit] %s: no func_button with %s ^"%s^" on this map", g_label[t], field, g_key[t])
        g_ent[t] = found
    }
}

public on_new_round()
{
    resolve()
}

trap_of(ent)
{
    for (new t = 0; t < g_traps; t++)
    {
        if (g_ent[t] == ent)
            return t
    }
    return -1
}

/* ---------- activations ---------- */

toggle_state(ent)
{
    return get_ent_data(ent, "CBaseToggle", "m_toggle_state")
}

before(ent)
{
    new t = trap_of(ent)
    if (t >= 0)
        g_stateBefore[t] = toggle_state(ent)
}

after(ent)
{
    new t = trap_of(ent)
    if (t < 0)
        return
    // Accepted = the button was at rest and this input set it moving. Input while it is
    // moving or held down is ignored by the button, and so here.
    if (g_stateBefore[t] != TS_AT_BOTTOM || toggle_state(ent) == TS_AT_BOTTOM)
        return
    new Float:now = get_gametime()
    if (g_uses[t] > 0)
    {
        new Float:gap = now - g_last[t]
        if (g_uses[t] == 1 || gap < g_min[t])
            g_min[t] = gap
        if (g_uses[t] == 1 || gap > g_max[t])
            g_max[t] = gap
    }
    g_uses[t]++
    g_last[t] = now
}

public on_activate_pre(ent, caller, activator, type, Float:value)
{
    before(ent)
    return HAM_IGNORED
}

public on_activate_post(ent, caller, activator, type, Float:value)
{
    after(ent)
    return HAM_IGNORED
}

public on_touch_pre(ent, other)
{
    before(ent)
    return HAM_IGNORED
}

public on_touch_post(ent, other)
{
    after(ent)
    return HAM_IGNORED
}

/* ---------- report ---------- */

public cmd_audit(id, level, cid)
{
    if (!cmd_access(id, level, cid, 1))
        return PLUGIN_HANDLED
    new arg[8]
    read_argv(1, arg, charsmax(arg))
    if (equali(arg, "reset"))
    {
        for (new t = 0; t < g_traps; t++)
            g_uses[t] = 0
        console_print(id, "[TrapAudit] Observations cleared.")
        return PLUGIN_HANDLED
    }
    new map[64]
    get_mapname(map, charsmax(map))
    console_print(id, "[TrapAudit] %s: %d trap button(s)", map, g_traps)
    for (new t = 0; t < g_traps; t++)
    {
        if (!g_ent[t])
        {
            console_print(id, "  %s (%s): not found on this map", g_label[t], g_key[t])
            continue
        }
        if (g_uses[t] < 2)
        {
            console_print(id, "  %s #%d: intended %.1f s, %d use(s), not enough for an interval", g_label[t], g_ent[t], g_intended[t], g_uses[t])
            continue
        }
        new verdict[40]
        if (g_min[t] + 0.05 < g_intended[t])
            formatex(verdict, charsmax(verdict), "reused %.1f s sooner than intended", g_intended[t] - g_min[t])
        else
            copy(verdict, charsmax(verdict), "never sooner than intended")
        console_print(id, "  %s #%d: intended %.1f s, %d uses, shortest %.1f s, longest %.1f s - %s", g_label[t], g_ent[t],
            g_intended[t], g_uses[t], g_min[t], g_max[t], verdict)
    }
    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.