/*
* Snowball Catch (olds.ro workshop card IDEA-E12DDC50F6CA3427)
*
* A player who presses USE just as a snowball reaches them from the front
* catches it instead of being hit, and the ball goes into their own supply.
* The window is short and there is a cooldown, so catching rewards skill
* without making anyone safe from every throw.
*
* This is an add-on for a Snow Wars game mode. The mode's core plugin must
* throw snowballs as info_target entities with the classname set in
* olds_catch_class, which hit in their own touch function (for example
* ReAPI SetTouch or Ham_Touch), and provide this native, which adds
* snowballs to a player's supply:
*
* native olds_snow_give(id, amount);
*
* Without such a core the plugin still loads, logs one line and changes
* nothing.
*
* Client command:
* olds_catch counts as a USE press (for players who bind it to
* another key)
*
* Server command:
* olds_catch_status catches of each player since they joined
*
* Cvars (in configs/plugins/olds_snowcatch.cfg, written with the defaults on
* the first start if the file is missing):
* olds_catch_enable 1 1 = on, 0 = off.
* olds_catch_class "snowball" Classname of the core's snowballs.
* olds_catch_window 0.2 Seconds a USE press stays good for (0.05 - 2).
* olds_catch_cd 2.0 Seconds after a catch before the next one
* (0 - 30).
* olds_catch_angle 60 How far from the middle of the view, in
* degrees to each side, a ball can come
* from and still be caught (10 - 180).
*
* How it works, as the card asks: RG_CBasePlayer_PreThink keeps the time of
* each new USE press (var_button). RegisterHam(Ham_Touch, "info_target")
* (pre) sees the ball touch the player; when the press is recent enough, the
* ball is in front and the cooldown is over, the ball is removed, the touch
* is stopped with HAM_SUPERCEDE (so the core's hit never happens) and one
* snowball is given with olds_snow_give.
*
* Requires AMX Mod X 1.10 with ReAPI (ReGameDLL_CS) and the hamsandwich
* module. Written and tested on AMX Mod X 1.10.0.5490, ReAPI 5.29.0.358 and
* 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[] = "Snowball Catch";
new const PLUGIN_VERSION[] = "1.0.0";
new const PLUGIN_AUTHOR[] = "alexit (olds.ro)";
new const DICTIONARY[] = "olds_snowcatch.txt";
new const CORE_NATIVE[] = "olds_snow_give";
// Stock Counter-Strike sound, already on every client.
new const CATCH_SOUND[] = "items/gunpickup2.wav";
const Float:LONG_AGO = -1000.0;
// Provided by the Snow Wars core.
native olds_snow_give(id, amount);
new bool:g_noCore;
new g_enable; // olds_catch_enable
new g_ballClass[32]; // olds_catch_class
new Float:g_window; // olds_catch_window
new Float:g_cooldown; // olds_catch_cd
new Float:g_maxAngle; // olds_catch_angle
new g_lastButtons[MAX_PLAYERS + 1];
new Float:g_pressTime[MAX_PLAYERS + 1];
new Float:g_catchTime[MAX_PLAYERS + 1];
new g_catches[MAX_PLAYERS + 1];
public plugin_natives()
{
set_native_filter("native_filter");
}
/**
* Lets the plugin load when no core provides olds_snow_give. The native is
* then never called.
*/
public native_filter(const name[], index, trap)
{
if (!trap && equal(name, CORE_NATIVE))
{
g_noCore = true;
return PLUGIN_HANDLED;
}
return PLUGIN_CONTINUE;
}
public plugin_precache()
{
precache_sound(CATCH_SOUND);
}
public plugin_init()
{
register_plugin(PLUGIN_NAME, PLUGIN_VERSION, PLUGIN_AUTHOR);
register_dictionary(DICTIONARY);
bind_pcvar_num(create_cvar("olds_catch_enable", "1", FCVAR_NONE,
"1 = players can catch snowballs with USE, 0 = off",
true, 0.0, true, 1.0), g_enable);
bind_pcvar_string(create_cvar("olds_catch_class", "snowball", FCVAR_NONE,
"Classname of the Snow Wars core's snowball entities"), g_ballClass, charsmax(g_ballClass));
bind_pcvar_float(create_cvar("olds_catch_window", "0.2", FCVAR_NONE,
"Seconds a USE press stays good for catching (0.05 - 2)",
true, 0.05, true, 2.0), g_window);
bind_pcvar_float(create_cvar("olds_catch_cd", "2.0", FCVAR_NONE,
"Seconds after a catch before the same player can catch again (0 - 30)",
true, 0.0, true, 30.0), g_cooldown);
bind_pcvar_float(create_cvar("olds_catch_angle", "60", FCVAR_NONE,
"Degrees to each side of the view a caught ball can come from (10 - 180)",
true, 10.0, true, 180.0), g_maxAngle);
AutoExecConfig(true, "olds_snowcatch");
register_clcmd("olds_catch", "cmd_catch");
register_srvcmd("olds_catch_status", "cmd_status");
if (g_noCore)
{
log_amx("No plugin provides the native %s (the Snow Wars core), so snowballs cannot be caught.", CORE_NATIVE);
return;
}
RegisterHookChain(RG_CBasePlayer_PreThink, "on_player_prethink", false);
RegisterHam(Ham_Touch, "info_target", "on_info_target_touch", false);
}
public client_putinserver(id)
{
g_lastButtons[id] = 0;
g_pressTime[id] = LONG_AGO;
g_catchTime[id] = LONG_AGO;
g_catches[id] = 0;
}
public on_player_prethink(id)
{
new buttons = get_entvar(id, var_button);
// A new press: USE is down now and was not in the frame before.
if ((buttons & IN_USE) && !(g_lastButtons[id] & IN_USE))
g_pressTime[id] = get_gametime();
g_lastButtons[id] = buttons;
return HC_CONTINUE;
}
public cmd_catch(id)
{
g_pressTime[id] = get_gametime();
return PLUGIN_HANDLED;
}
public on_info_target_touch(ball, id)
{
if (!g_enable || !(1 <= id <= MaxClients) || !is_user_alive(id))
return HAM_IGNORED;
new classname[32];
get_entvar(ball, var_classname, classname, charsmax(classname));
if (!equal(classname, g_ballClass) || get_entvar(ball, var_owner) == id)
return HAM_IGNORED;
new Float:now = get_gametime();
if (now - g_pressTime[id] > g_window || now - g_catchTime[id] < g_cooldown)
return HAM_IGNORED;
if (floatabs(direction_from_view(id, ball)) > g_maxAngle)
return HAM_IGNORED;
g_catchTime[id] = now;
g_catches[id]++;
// Removed after this frame; no more touches until then.
set_entvar(ball, var_solid, SOLID_NOT);
set_entvar(ball, var_flags, get_entvar(ball, var_flags) | FL_KILLME);
olds_snow_give(id, 1);
emit_sound(id, CHAN_ITEM, CATCH_SOUND, VOL_NORM, ATTN_NORM, 0, PITCH_NORM);
client_print_color(id, print_team_default, "%L", id, "CATCH_CAUGHT");
return HAM_SUPERCEDE;
}
/**
* Where the entity is, seen from the player: degrees from the middle of the
* player's view, -180 to 180 (0 = straight ahead, 180 = straight behind).
*/
Float:direction_from_view(id, ent)
{
new Float:from[3], Float:to[3], Float:view[3];
get_entvar(id, var_origin, from);
get_entvar(ent, var_origin, to);
get_entvar(id, var_v_angle, view);
new Float:dx = to[0] - from[0];
new Float:dy = to[1] - from[1];
// Straight above or below the player: no side to tell, counts as the front.
if (floatabs(dx) < 1.0 && floatabs(dy) < 1.0)
return 0.0;
new Float:angle = floatatan2(dy, dx, degrees) - view[1];
while (angle > 180.0)
angle -= 360.0;
while (angle <= -180.0)
angle += 360.0;
return angle;
}
public cmd_status()
{
new players;
for (new id = 1; id <= MaxClients; id++)
{
if (!is_user_connected(id))
continue;
server_print("%n catches %d", id, g_catches[id]);
players++;
}
new windowMs = floatround(g_window * 1000.0);
new cooldownTenths = floatround(g_cooldown * 10.0);
server_print("Snowball Catch: %s, %d player(s), window %d ms, cooldown %d.%d s, angle %d, class ^"%s^"%s",
g_enable ? "on" : "off", players, windowMs, cooldownTenths / 10, cooldownTenths % 10,
floatround(g_maxAngle), g_ballClass, g_noCore ? ", no Snow Wars core found" : "");
return PLUGIN_HANDLED;
}