/*
* Ammo Status Alerts (olds.ro workshop card LIB-0076)
*
* Short texts under the crosshair warn when the magazine is low, when it is
* empty and the player should reload, and when the weapon is out of ammo
* altogether. Each player can switch them off and on with /ammotext.
*
* Commands:
* say /ammotext, say_team /ammotext switch the texts off or on for
* yourself
*
* Cvars (in configs/plugins/olds_ammoalerts.cfg, which ships with the
* plugin; if the file is missing, the plugin writes it with these defaults
* the first time it starts):
* olds_as_low 20 "Low ammo" when the magazine holds this percent of a
* full magazine or less (AK-47: 6 of 30).
* olds_as_flash 1 1 = "Reload" and "No ammo" blink; 0 = they stay
* steady.
*
* Rules:
* - "Low ammo" is shown once when the magazine gets low, not on every
* shot; again only after a reload or with another weapon.
* - "Reload" (empty magazine, ammo left) and "No ammo" (nothing left at
* all) stay on screen until the reason is gone: the reload starts, the
* player switches weapons or buys ammo.
* - The knife, grenades and the C4 never show a text, nor does a dead
* player.
* - Director HUD texts cannot be cleared, so "Reload" and "No ammo" are
* drawn again every 0.5 s with a short life; they are gone at most 0.5 s
* after the reason. "Low ammo" lives 1 s.
* - The /ammotext choice lasts until the player leaves the server.
*
* How it works, as the card asks: the CurWeapon event (clip) and the AmmoX
* message (reserve ammo) tell when the ammo changes; the texts are printed
* with set_dhudmessage under the crosshair. A 0.1 s check also sees a reload
* start, which sends neither.
*
* Requires AMX Mod X 1.10 with ReAPI (ReGameDLL_CS). Written for 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 <reapi>
#pragma semicolon 1
new const PLUGIN_NAME[] = "Ammo Status Alerts";
new const PLUGIN_VERSION[] = "1.0.0";
new const PLUGIN_AUTHOR[] = "alexit (olds.ro)";
const TASK_CHECK = 7600;
const Float:CHECK_EVERY = 0.1;
// "Reload" and "No ammo" are drawn again this often.
const Float:REPEAT_EVERY = 0.5;
// Life of each text (the blinking ones show half of REPEAT_EVERY).
const Float:LOW_HOLD = 1.0;
const Float:STEADY_HOLD = 0.45;
const Float:BLINK_HOLD = 0.25;
// "Low ammo" a little above the other two, so they never overlap.
const Float:TEXT_X = -1.0;
const Float:LOW_Y = 0.57;
const Float:ALERT_Y = 0.61;
enum
{
STATE_NONE = 0,
STATE_LOW,
STATE_RELOAD,
STATE_EMPTY
};
new g_lowPercent; // olds_as_low
new g_flash; // olds_as_flash
// Per player: the current state, the weapon it belongs to, when "Reload" or
// "No ammo" is drawn next, and the /ammotext choice.
new g_state[MAX_PLAYERS + 1];
new g_weapon[MAX_PLAYERS + 1];
new Float:g_nextDraw[MAX_PLAYERS + 1];
new bool:g_hidden[MAX_PLAYERS + 1];
public plugin_init()
{
register_plugin(PLUGIN_NAME, PLUGIN_VERSION, PLUGIN_AUTHOR);
// The weapon members come from ReGameDLL_CS.
if (!is_regamedll())
set_fail_state("ReGameDLL_CS is not running. This plugin needs ReGameDLL_CS and ReAPI.");
register_dictionary("olds_ammoalerts.txt");
bind_pcvar_num(create_cvar("olds_as_low", "20", FCVAR_NONE,
"Low ammo when the magazine holds this percent of a full one or less",
true, 1.0, true, 99.0), g_lowPercent);
bind_pcvar_num(create_cvar("olds_as_flash", "1", FCVAR_NONE,
"1 = the Reload and No ammo texts blink; 0 = they stay steady",
true, 0.0, true, 1.0), g_flash);
AutoExecConfig(true, "olds_ammoalerts");
register_clcmd("say /ammotext", "cmd_toggle");
register_clcmd("say_team /ammotext", "cmd_toggle");
register_event("CurWeapon", "on_cur_weapon", "be", "1=1");
register_event("AmmoX", "on_ammo_x", "be");
set_task(CHECK_EVERY, "task_check", TASK_CHECK, _, _, "b");
}
public client_putinserver(id)
{
g_state[id] = STATE_NONE;
g_weapon[id] = 0;
g_hidden[id] = false;
}
public client_disconnected(id)
{
g_state[id] = STATE_NONE;
g_weapon[id] = 0;
g_hidden[id] = false;
}
public cmd_toggle(id)
{
g_hidden[id] = !g_hidden[id];
g_state[id] = STATE_NONE;
g_weapon[id] = 0;
client_print_color(id, print_team_default, "%L", id, g_hidden[id] ? "AS_OFF" : "AS_ON");
if (!g_hidden[id])
update(id);
return PLUGIN_HANDLED;
}
/**
* The clip of the active weapon changed, or another weapon was drawn.
*/
public on_cur_weapon(id)
{
update(id);
}
/**
* The player's reserve ammo of some kind changed.
*/
public on_ammo_x(id)
{
update(id);
}
/**
* Catches what sends no message (a reload starting) and draws "Reload" and
* "No ammo" again.
*/
public task_check()
{
new Float:now = get_gametime();
for (new id = 1; id <= MaxClients; id++)
{
if (!is_user_connected(id))
continue;
update(id);
if ((g_state[id] == STATE_RELOAD || g_state[id] == STATE_EMPTY) && now >= g_nextDraw[id])
draw_alert(id);
}
}
update(id)
{
new weapon;
new current = read_state(id, weapon);
new previous = g_state[id];
new bool:newWeapon = (weapon != g_weapon[id]);
g_state[id] = current;
g_weapon[id] = weapon;
if (current == previous && !newWeapon)
return;
switch (current)
{
case STATE_LOW:
draw_low(id);
case STATE_RELOAD, STATE_EMPTY:
draw_alert(id);
}
}
/**
* What the player's active weapon needs now. weapon gets its entity (0 for
* none, or for a weapon that never shows a text).
*/
read_state(id, &weapon)
{
weapon = 0;
if (g_hidden[id] || !is_user_alive(id))
return STATE_NONE;
new item = get_member(id, m_pActiveItem);
if (item <= 0)
return STATE_NONE;
new WeaponIdType:weaponId = WeaponIdType:get_member(item, m_iId);
switch (weaponId)
{
case WEAPON_NONE, WEAPON_KNIFE, WEAPON_HEGRENADE, WEAPON_FLASHBANG, WEAPON_SMOKEGRENADE, WEAPON_C4:
return STATE_NONE;
}
new clip = get_member(item, m_Weapon_iClip);
new clipSize = rg_get_weapon_info(weaponId, WI_GUN_CLIP_SIZE);
if (clip < 0 || clipSize <= 0)
return STATE_NONE;
weapon = item;
// Reloading (a shotgun loads shell by shell): nothing to tell.
if (get_member(item, m_Weapon_fInReload) || get_member(item, m_Weapon_fInSpecialReload) > 0)
return STATE_NONE;
if (clip == 0)
{
new ammoType = get_member(item, m_Weapon_iPrimaryAmmoType);
new reserve = (ammoType > 0) ? get_member(id, m_rgAmmo, ammoType) : 0;
return (reserve > 0) ? STATE_RELOAD : STATE_EMPTY;
}
if (clip * 100 <= clipSize * g_lowPercent)
return STATE_LOW;
return STATE_NONE;
}
draw_low(id)
{
set_dhudmessage(255, 200, 40, TEXT_X, LOW_Y, 0, 0.0, LOW_HOLD, 0.0, 0.0);
show_dhudmessage(id, "%L", id, "AS_LOW");
}
draw_alert(id)
{
new Float:hold = g_flash ? BLINK_HOLD : STEADY_HOLD;
if (g_state[id] == STATE_RELOAD)
{
set_dhudmessage(255, 130, 0, TEXT_X, ALERT_Y, 0, 0.0, hold, 0.0, 0.0);
show_dhudmessage(id, "%L", id, "AS_RELOAD");
}
else
{
set_dhudmessage(255, 40, 40, TEXT_X, ALERT_Y, 0, 0.0, hold, 0.0, 0.0);
show_dhudmessage(id, "%L", id, "AS_EMPTY");
}
g_nextDraw[id] = get_gametime() + REPEAT_EVERY;
}