/*
* CT Safe Buttons (olds.ro workshop card LIB-0809) - Deathrun
*
* Counter-terrorists (the runners) cannot set off trap buttons: pressing one (+use) and
* shooting a shootable one do nothing for them, with a chat line and a click. The
* terrorist (the trap master) uses them as normal. Buttons fired by other entities
* (relays, triggers) are not touched. Buttons named in configs/olds_ctsafe.ini work for
* everyone on that map.
*
* Cvars: olds_ctsafe_enable 1, olds_ctsafe_msg 1,
* olds_ctsafe_classes "func_button,func_rot_button" (read at map start).
*
* olds_ctsafe.ini: "<map> <button>" per line; <button> is the button's targetname or its
* brush model (*12); the map * means every map.
*
* Copyright (C) 2026 daemon (olds.ro)
* SPDX-License-Identifier: GPL-3.0-or-later
*/
#include <amxmodx>
#include <fakemeta>
#include <hamsandwich>
#include <reapi>
#define PLUGIN "CT Safe Buttons"
#define VERSION "1.0.0"
#define AUTHOR "olds.ro"
#define SND_DENY "buttons/button10.wav"
const EXCEPT_MAX = 64;
/* Button classes the plugin can hook (others in the cvar are skipped with a log line). */
new const KNOWN_CLASSES[][] = { "func_button", "func_rot_button", "momentary_rot_button", "button_target" };
new g_enable, g_msg;
new g_cvarClasses;
new g_except[EXCEPT_MAX][32];
new g_excepts;
new Float:g_nextMsg[MAX_PLAYERS + 1];
public plugin_precache()
{
precache_sound(SND_DENY);
}
public plugin_init()
{
register_plugin(PLUGIN, VERSION, AUTHOR);
register_dictionary("olds_ctsafe.txt");
bind_pcvar_num(create_cvar("olds_ctsafe_enable", "1", FCVAR_NONE,
"CTs cannot use trap buttons", true, 0.0, true, 1.0), g_enable);
bind_pcvar_num(create_cvar("olds_ctsafe_msg", "1", FCVAR_NONE,
"Tell the CT why the button did nothing", true, 0.0, true, 1.0), g_msg);
g_cvarClasses = create_cvar("olds_ctsafe_classes", "func_button,func_rot_button", FCVAR_NONE,
"Button classes guarded (comma separated; read at map start)");
AutoExecConfig(true, "olds_ctsafe");
}
public plugin_cfg()
{
load_exceptions();
new list[192], item[32], rest[192];
get_pcvar_string(g_cvarClasses, list, charsmax(list));
while (list[0])
{
strtok2(list, item, charsmax(item), rest, charsmax(rest), ',', TRIM_FULL);
copy(list, charsmax(list), rest);
if (!item[0])
continue;
new bool:known = false;
for (new k = 0; k < sizeof KNOWN_CLASSES; k++)
if (equal(item, KNOWN_CLASSES[k]))
known = true;
if (!known)
{
log_amx("olds_ctsafe_classes: %s is not a button class, skipped", item);
continue;
}
RegisterHam(Ham_Use, item, "on_use", false);
RegisterHam(Ham_TraceAttack, item, "on_trace_attack", false);
RegisterHam(Ham_TakeDamage, item, "on_take_damage", false);
}
}
/* ---------- exceptions ---------- */
load_exceptions()
{
new path[160], map[64];
get_localinfo("amxx_configsdir", path, charsmax(path));
add(path, charsmax(path), "/olds_ctsafe.ini");
get_mapname(map, charsmax(map));
g_excepts = 0;
new fp = fopen(path, "rt");
if (!fp)
return;
new line[128], word[64], name[32];
while (g_excepts < EXCEPT_MAX && fgets(fp, line, charsmax(line)))
{
trim(line);
if (!line[0] || line[0] == ';' || line[0] == '#')
continue;
if (parse(line, word, charsmax(word), name, charsmax(name)) < 2)
continue;
if (equal(word, "*") || equali(word, map))
copy(g_except[g_excepts++], charsmax(g_except[]), name);
}
fclose(fp);
if (g_excepts)
log_amx("%d button exception(s) on this map", g_excepts);
}
bool:is_exception(button)
{
if (!g_excepts)
return false;
new name[32], model[32];
pev(button, pev_targetname, name, charsmax(name));
pev(button, pev_model, model, charsmax(model));
for (new i = 0; i < g_excepts; i++)
{
if (name[0] && equal(name, g_except[i]))
return true;
if (model[0] == '*' && equal(model, g_except[i]))
return true;
}
return false;
}
/* ---------- the guard ---------- */
/* true when this player may not set off this button. */
bool:denied(button, id)
{
if (!g_enable || !(1 <= id <= MaxClients) || !is_user_connected(id))
return false;
if (get_member(id, m_iTeam) != TEAM_CT)
return false;
if (is_exception(button))
return false;
if (g_msg)
{
new Float:now = get_gametime();
if (now >= g_nextMsg[id])
{
g_nextMsg[id] = now + 1.5;
client_print_color(id, print_team_default, "^4[Deathrun]^1 %L", id, "CTSAFE_DENY");
client_cmd(id, "spk %s", SND_DENY);
}
}
return true;
}
public on_use(button, caller, activator, use_type, Float:value)
{
return denied(button, activator) ? HAM_SUPERCEDE : HAM_IGNORED;
}
public on_trace_attack(button, attacker, Float:damage, Float:direction[3], trace, damage_bits)
{
return denied(button, attacker) ? HAM_SUPERCEDE : HAM_IGNORED;
}
public on_take_damage(button, inflictor, attacker, Float:damage, damage_bits)
{
return denied(button, attacker) ? HAM_SUPERCEDE : HAM_IGNORED;
}
public client_disconnected(id)
{
g_nextMsg[id] = 0.0;
}