/*
* Underwater Shooting Control (olds.ro workshop card LIB-2003)
*
* Guns do not work under water: a player whose head is under water (or who
* is in water up to the level set by olds_uw_level) cannot fire or use the
* secondary attack (scope, silencer, burst mode) of any weapon that is not
* on the olds_uw_allow list. By default the knife, the USP and the Glock-18
* still work, so water becomes a safe place or a knife fight.
*
* - The weapon's next-attack timers are held back while the player is
* under water, so with weapon prediction on the client does not show a
* shot or a muzzle flash either. Ham_Weapon_PrimaryAttack and
* Ham_Weapon_SecondaryAttack are also blocked (HAM_SUPERCEDE).
* - A player who presses fire under water is told why nothing happens
* (olds_uw_msg), at most once every 2 seconds.
* - Grenades can still be thrown under water unless olds_uw_nades is 1.
* The C4 is never blocked.
*
* Cvars (configs/plugins/olds_uwshoot.cfg; AutoExecConfig writes that file
* with these defaults if it is missing):
* olds_uw_enable 1 1 = on, 0 = every weapon works under water.
* olds_uw_level 3 Water level from which guns are blocked:
* 1 = feet, 2 = waist, 3 = head under water.
* olds_uw_allow "knife usp glock18" Weapons that still work, names without
* "weapon_", separated by spaces.
* olds_uw_nades 0 1 = grenades cannot be thrown either.
* olds_uw_msg 1 1 = tell the player why he cannot shoot.
*
* Every text players read comes from data/lang/olds_uwshoot.txt (English).
*
* Requires AMX Mod X 1.10 or newer and ReAPI with ReGameDLL_CS. Written for
* AMX Mod X 1.10.0.5490, ReAPI 5.29.0.358, ReGameDLL_CS 5.30.0.814.
*
* Copyright (C) 2026 alexit (olds.ro workshop)
* SPDX-License-Identifier: GPL-3.0-or-later
*
* 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: <https://www.gnu.org/licenses/>.
*/
#include <amxmodx>
#include <hamsandwich>
#include <reapi>
#pragma semicolon 1
new const PLUGIN_NAME[] = "Underwater Shooting Control";
new const PLUGIN_VERSION[] = "1.0.0";
new const PLUGIN_AUTHOR[] = "alexit (olds.ro)";
new const DICTIONARY[] = "olds_uwshoot.txt";
// The timers of a blocked weapon are kept at least this far away (seconds),
// so neither the server nor the client's prediction fires it.
const Float:HOLD_DELAY = 0.2;
// Seconds between two "cannot shoot" messages to the same player.
const Float:MESSAGE_INTERVAL = 2.0;
new g_enable; // olds_uw_enable
new g_level; // olds_uw_level
new g_allowList[256]; // olds_uw_allow
new g_nades; // olds_uw_nades
new g_message; // olds_uw_msg
// Weapons on the olds_uw_allow list, by weapon id.
new bool:g_allowed[CSW_P90 + 1];
new Float:g_nextMessage[MAX_PLAYERS + 1];
public plugin_init()
{
register_plugin(PLUGIN_NAME, PLUGIN_VERSION, PLUGIN_AUTHOR);
register_dictionary(DICTIONARY);
bind_pcvar_num(create_cvar("olds_uw_enable", "1", FCVAR_NONE,
"1 = guns not on olds_uw_allow do not work under water, 0 = off",
true, 0.0, true, 1.0), g_enable);
bind_pcvar_num(create_cvar("olds_uw_level", "3", FCVAR_NONE,
"Water level from which guns are blocked: 1 = feet, 2 = waist, 3 = head under water",
true, 1.0, true, 3.0), g_level);
new allow = create_cvar("olds_uw_allow", "knife usp glock18", FCVAR_NONE,
"Weapons that still work under water, names without weapon_, separated by spaces");
bind_pcvar_string(allow, g_allowList, charsmax(g_allowList));
hook_cvar_change(allow, "OnAllowChange");
bind_pcvar_num(create_cvar("olds_uw_nades", "0", FCVAR_NONE,
"1 = grenades cannot be thrown under water either, 0 = grenades work",
true, 0.0, true, 1.0), g_nades);
bind_pcvar_num(create_cvar("olds_uw_msg", "1", FCVAR_NONE,
"1 = tell a player who tries to shoot under water why nothing happens",
true, 0.0, true, 1.0), g_message);
AutoExecConfig(true, "olds_uwshoot");
new weaponName[32];
for (new weaponId = 1; weaponId <= CSW_P90; weaponId++)
{
// The C4 is never blocked; id 2 is not a weapon.
if (weaponId == CSW_C4 || !get_weaponname(weaponId, weaponName, charsmax(weaponName)) || !weaponName[0])
continue;
RegisterHam(Ham_Item_PostFrame, weaponName, "OnItemPostFrame_Pre", false);
RegisterHam(Ham_Weapon_PrimaryAttack, weaponName, "OnAttack_Pre", false);
RegisterHam(Ham_Weapon_SecondaryAttack, weaponName, "OnAttack_Pre", false);
}
parse_allow_list(g_allowList);
}
public OnAllowChange(pcvar, const oldValue[], const newValue[])
{
parse_allow_list(newValue);
}
public client_putinserver(id)
{
g_nextMessage[id] = 0.0;
}
/**
* Every frame for the weapon in hand: while it is blocked, its timers are
* held back, so it does not fire (and the client does not predict a shot).
*/
public OnItemPostFrame_Pre(weapon)
{
new player = get_member(weapon, m_pPlayer);
if (!is_blocked(weapon, player))
return HAM_IGNORED;
hold_timer(weapon, m_Weapon_flNextPrimaryAttack);
hold_timer(weapon, m_Weapon_flNextSecondaryAttack);
if (g_message && (get_entvar(player, var_button) & (IN_ATTACK | IN_ATTACK2)))
tell_player(player);
return HAM_IGNORED;
}
/**
* Backstop: an attack that still comes through (another plugin calling it,
* or a timer already run out) is cancelled.
*/
public OnAttack_Pre(weapon)
{
new player = get_member(weapon, m_pPlayer);
if (!is_blocked(weapon, player))
return HAM_IGNORED;
if (g_message)
tell_player(player);
return HAM_SUPERCEDE;
}
bool:is_blocked(weapon, player)
{
if (!g_enable || player < 1 || player > MaxClients || !is_user_alive(player))
return false;
if (get_entvar(player, var_waterlevel) < g_level)
return false;
new weaponId = get_member(weapon, m_iId);
switch (weaponId)
{
case CSW_C4:
return false;
case CSW_HEGRENADE, CSW_FLASHBANG, CSW_SMOKEGRENADE:
return g_nades != 0;
}
if (weaponId < 1 || weaponId > CSW_P90)
return false;
return !g_allowed[weaponId];
}
hold_timer(weapon, any:member)
{
if (Float:get_member(weapon, member) < HOLD_DELAY)
set_member(weapon, member, HOLD_DELAY);
}
tell_player(player)
{
new Float:now = get_gametime();
if (now < g_nextMessage[player])
return;
g_nextMessage[player] = now + MESSAGE_INTERVAL;
client_print(player, print_center, "%L", player, "OLDS_UW_BLOCKED");
}
/**
* "knife usp glock18" -> the weapon ids on the list (spaces or commas between
* the names). Unknown names are reported in the server console.
*/
parse_allow_list(const list[])
{
arrayset(g_allowed, false, sizeof(g_allowed));
new name[32], weaponName[40];
new length = strlen(list);
new start = 0;
while (start < length)
{
while (start < length && (list[start] == ' ' || list[start] == ','))
start++;
new end = start;
while (end < length && list[end] != ' ' && list[end] != ',')
end++;
if (end > start)
{
copy(name, min(end - start, charsmax(name)), list[start]);
strtolower(name);
formatex(weaponName, charsmax(weaponName), "weapon_%s", name);
new weaponId = get_weaponid(weaponName);
if (weaponId >= 1 && weaponId <= CSW_P90)
g_allowed[weaponId] = true;
else
server_print("[%s] %L", PLUGIN_NAME, LANG_SERVER, "OLDS_UW_UNKNOWN", name);
}
start = end;
}
}