/*
* Dropped Weapon Cleanup (olds.ro workshop card LIB-0232)
*
* Weapons that players drop (by hand or when they die) are removed after a
* set number of seconds, or at once, so the map does not fill up with guns
* and the server does not run short of entities. The dropped bomb can be
* kept, and the weapons placed on the map (armoury_entity) can be cleared at
* every round start.
*
* Commands: none. The plugin only removes entities and shows nothing.
*
* Cvars (in configs/plugins/olds_weaponcleanup.cfg, which ships with the
* plugin; if the file is missing, the plugin writes it with these defaults
* the first time it starts):
* olds_wcl_life 20.0 Seconds a dropped weapon stays on the ground
* (1 - 600). A new value applies to weapons
* dropped after the change.
* olds_wcl_instant 0 1 = remove dropped weapons on the next frame.
* olds_wcl_mapweapons 0 1 = remove the map's own weapons
* (armoury_entity) at each round start. Removed
* map weapons come back with the next map.
* olds_wcl_keepbomb 1 1 = a dropped bomb is never removed.
*
* How it works, as the card asks: RG_CWeaponBox_SetModel (post) runs once
* the game has packed the dropped weapon into its weaponbox. The plugin
* looks through m_WeaponBox_rgpPlayerItems for the C4, then starts
* set_task(olds_wcl_life) for that box (or waits one frame in instant mode).
* The removal uses rg_remove_entity, first on the weapons packed in the box
* (as the game's own clean-up does, so no weapon entity is left behind),
* then on the box. Every pending removal is tied to the one box it was made
* for: when the box is picked up or the round restarts, the task is dropped,
* and a task never removes a newer entity that reused the same index.
*
* Requires AMX Mod X 1.10 with ReAPI (ReGameDLL_CS) and the fakemeta and
* hamsandwich modules. 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 <fakemeta>
#include <hamsandwich>
#include <reapi>
#pragma semicolon 1
new const PLUGIN_NAME[] = "Dropped Weapon Cleanup";
new const PLUGIN_VERSION[] = "1.0.0";
new const PLUGIN_AUTHOR[] = "alexit (olds.ro)";
// Task id of a box's removal is TASK_REMOVE + box entity index.
const TASK_REMOVE = 23200;
// Number of item slots in a weaponbox (MAX_ITEM_TYPES in the game).
const BOX_SLOTS = 6;
new Float:g_life; // olds_wcl_life
new g_instant; // olds_wcl_instant
new g_mapWeapons; // olds_wcl_mapweapons
new g_keepBomb; // olds_wcl_keepbomb
// For each entity index, the stamp of the removal waiting for the box that
// has that index (0 = none). A task carries its stamp and acts only when the
// stamp still matches, so it can never touch a newer entity.
new Array:g_stamps;
new g_maxEntities;
new g_lastStamp;
public plugin_init()
{
register_plugin(PLUGIN_NAME, PLUGIN_VERSION, PLUGIN_AUTHOR);
// The weaponbox and round hooks come from ReGameDLL_CS.
if (!is_regamedll())
set_fail_state("ReGameDLL_CS is not running. This plugin needs ReGameDLL_CS and ReAPI.");
bind_pcvar_float(create_cvar("olds_wcl_life", "20.0", FCVAR_NONE,
"Seconds a dropped weapon stays on the ground before it is removed",
true, 1.0, true, 600.0), g_life);
bind_pcvar_num(create_cvar("olds_wcl_instant", "0", FCVAR_NONE,
"1 = remove dropped weapons on the next frame",
true, 0.0, true, 1.0), g_instant);
bind_pcvar_num(create_cvar("olds_wcl_mapweapons", "0", FCVAR_NONE,
"1 = remove the map's own weapons (armoury_entity) at each round start",
true, 0.0, true, 1.0), g_mapWeapons);
bind_pcvar_num(create_cvar("olds_wcl_keepbomb", "1", FCVAR_NONE,
"1 = a dropped bomb is never removed",
true, 0.0, true, 1.0), g_keepBomb);
AutoExecConfig(true, "olds_weaponcleanup");
g_maxEntities = global_get(glb_maxEntities);
g_stamps = ArrayCreate(1, g_maxEntities);
for (new i = 0; i < g_maxEntities; i++)
ArrayPushCell(g_stamps, 0);
RegisterHookChain(RG_CWeaponBox_SetModel, "on_box_set_model_post", true);
RegisterHookChain(RG_CSGameRules_RestartRound, "on_restart_round_post", true);
RegisterHam(Ham_Touch, "weaponbox", "on_box_touch_post", true);
}
public plugin_cfg()
{
// The first round of a map starts without a RestartRound call. The
// config file is run a moment after plugin_cfg, so the cvar is read a
// second later.
set_task(1.0, "task_map_start");
}
public task_map_start()
{
if (g_mapWeapons)
remove_map_weapons();
}
public plugin_end()
{
ArrayDestroy(g_stamps);
}
/**
* The game calls this last when it creates a dropped weapon's box, so the
* weapon (and the bomb, if any) is already packed inside.
*/
public on_box_set_model_post(box, const model[])
{
if (!is_valid_index(box))
return HC_CONTINUE;
// A new box at this index: anything still waiting for an older entity
// with the same index is dropped.
forget_box(box);
if (g_keepBomb && box_has_bomb(box))
return HC_CONTINUE;
new stamp = next_stamp();
ArraySetCell(g_stamps, box, stamp);
if (g_instant)
{
RequestFrame("frame_remove_box", box);
}
else
{
new data[1];
data[0] = stamp;
set_task(g_life, "task_remove_box", TASK_REMOVE + box, data, sizeof(data));
}
return HC_CONTINUE;
}
/**
* After a touch the game may have handed the box to a player and marked it
* for deletion. Its pending removal goes with it.
*/
public on_box_touch_post(box, other)
{
if (is_valid_index(box) && (get_entvar(box, var_flags) & FL_KILLME))
forget_box(box);
return HAM_IGNORED;
}
/**
* A new round: the game has already removed every dropped weapon and put
* the map's weapons back, so every pending removal is dropped, and the map
* weapons are cleared when olds_wcl_mapweapons is 1.
*/
public on_restart_round_post()
{
for (new box = 1; box < g_maxEntities; box++)
{
if (ArrayGetCell(g_stamps, box))
forget_box(box);
}
if (g_mapWeapons)
remove_map_weapons();
return HC_CONTINUE;
}
public task_remove_box(const data[], task)
{
new box = task - TASK_REMOVE;
if (is_valid_index(box) && ArrayGetCell(g_stamps, box) == data[0])
remove_box(box);
}
/**
* Instant mode. An entity index is never handed out again within half a
* second of being freed, so on the next frame this index still belongs to
* the same box, or to nothing.
*/
public frame_remove_box(box)
{
if (ArrayGetCell(g_stamps, box))
remove_box(box);
}
remove_box(box)
{
ArraySetCell(g_stamps, box, 0);
if (is_nullent(box) || (get_entvar(box, var_flags) & FL_KILLME) || !FClassnameIs(box, "weaponbox"))
return;
// The weapons packed in the box are entities of their own. The game's
// own clean-up removes them with the box, and so does this.
for (new slot = 0; slot < BOX_SLOTS; slot++)
{
new item = get_member(box, m_WeaponBox_rgpPlayerItems, slot);
while (item > 0 && !is_nullent(item))
{
new next = get_member(item, m_pNext);
rg_remove_entity(item);
item = next;
}
}
rg_remove_entity(box);
}
forget_box(box)
{
remove_task(TASK_REMOVE + box);
ArraySetCell(g_stamps, box, 0);
}
/**
* Looks through the weapons packed in the box for the C4. The game marks a
* bomb box only after this hook, so the items are what tells.
*/
bool:box_has_bomb(box)
{
for (new slot = 0; slot < BOX_SLOTS; slot++)
{
new item = get_member(box, m_WeaponBox_rgpPlayerItems, slot);
while (item > 0 && !is_nullent(item))
{
if (get_member(item, m_iId) == WEAPON_C4)
return true;
item = get_member(item, m_pNext);
}
}
return false;
}
remove_map_weapons()
{
new entity = NULLENT;
while ((entity = rg_find_ent_by_class(entity, "armoury_entity")) > 0)
rg_remove_entity(entity);
}
bool:is_valid_index(entity)
{
return entity > MaxClients && entity < g_maxEntities;
}
/**
* Stamps are positive and never 0, also after the counter wraps around.
*/
next_stamp()
{
if (++g_lastStamp <= 0)
g_lastStamp = 1;
return g_lastStamp;
}