/*
* Armor and Health Regeneration (olds.ro workshop card LIB-0082)
*
* A living player who has not been hurt for a few seconds slowly gets his
* health and armor back, up to set caps. Players with a chosen admin flag
* (VIPs) regenerate twice as fast.
*
* Commands: none.
*
* Cvars (in configs/plugins/olds_regen.cfg, which ships with the plugin; if
* the file is missing, the plugin writes it with these defaults the first
* time it starts):
* olds_rg_hp 2 Health given every second (0 = no health).
* olds_rg_ap 1 Armor given every second (0 = no armor).
* olds_rg_delay 5 Seconds without damage before it starts.
* olds_rg_maxhp 100 Health is never raised above this.
* olds_rg_maxap 100 Armor is never raised above this.
* olds_rg_vip_flag "b" Admin flag(s) that double the rate ("" = none).
* olds_rg_newarmor 0 1 = a player without armor gets a vest from the
* regeneration; 0 = only armor the player still
* has is refilled.
*
* Rules:
* - Every point of damage restarts the wait, so a player who is shot
* during regeneration waits olds_rg_delay seconds again.
* - Health or armor already above its cap is never lowered.
* - The regeneration stops when the player dies and starts again when he
* spawns. It is silent: no text, no sound.
* - A vest keeps its helmet; armor that reached 0 is a destroyed vest and
* is refilled only with olds_rg_newarmor 1 (as Kevlar, no helmet).
*
* How it works, as the card asks: a 1 second repeating task per living
* player gives the health and armor; RG_CBasePlayer_TakeDamage (post) keeps
* the time of the last damage.
*
* 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[] = "Armor and Health Regeneration";
new const PLUGIN_VERSION[] = "1.0.0";
new const PLUGIN_AUTHOR[] = "alexit (olds.ro)";
// Task ids: TASK_REGEN + player index.
const TASK_REGEN = 82000;
const Float:TICK = 1.0;
new g_hp; // olds_rg_hp
new g_ap; // olds_rg_ap
new Float:g_delay; // olds_rg_delay
new g_maxHp; // olds_rg_maxhp
new g_maxAp; // olds_rg_maxap
new g_vipFlagText[32]; // olds_rg_vip_flag
new g_newArmor; // olds_rg_newarmor
// Game time of each player's last damage.
new Float:g_lastDamage[MAX_PLAYERS + 1];
public plugin_init()
{
register_plugin(PLUGIN_NAME, PLUGIN_VERSION, PLUGIN_AUTHOR);
// The spawn, damage and death 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_num(create_cvar("olds_rg_hp", "2", FCVAR_NONE,
"Health given every second (0 = no health)",
true, 0.0, true, 255.0), g_hp);
bind_pcvar_num(create_cvar("olds_rg_ap", "1", FCVAR_NONE,
"Armor given every second (0 = no armor)",
true, 0.0, true, 255.0), g_ap);
bind_pcvar_float(create_cvar("olds_rg_delay", "5", FCVAR_NONE,
"Seconds without damage before the regeneration starts",
true, 0.0, true, 600.0), g_delay);
bind_pcvar_num(create_cvar("olds_rg_maxhp", "100", FCVAR_NONE,
"Health is never raised above this",
true, 1.0, true, 1000.0), g_maxHp);
bind_pcvar_num(create_cvar("olds_rg_maxap", "100", FCVAR_NONE,
"Armor is never raised above this",
true, 0.0, true, 999.0), g_maxAp);
bind_pcvar_string(create_cvar("olds_rg_vip_flag", "b", FCVAR_NONE,
"Admin flag(s) that double the regeneration rate (empty = nobody)"),
g_vipFlagText, charsmax(g_vipFlagText));
bind_pcvar_num(create_cvar("olds_rg_newarmor", "0", FCVAR_NONE,
"1 = a player without armor gets a vest from the regeneration; 0 = only armor he still has is refilled",
true, 0.0, true, 1.0), g_newArmor);
AutoExecConfig(true, "olds_regen");
RegisterHookChain(RG_CBasePlayer_Spawn, "on_spawn_post", true);
RegisterHookChain(RG_CBasePlayer_TakeDamage, "on_take_damage_post", true);
RegisterHookChain(RG_CBasePlayer_Killed, "on_killed_post", true);
}
public client_disconnected(id)
{
remove_task(TASK_REGEN + id);
}
/**
* The player is alive again: his regeneration task starts.
*/
public on_spawn_post(id)
{
if (!is_user_alive(id))
return HC_CONTINUE;
g_lastDamage[id] = 0.0;
remove_task(TASK_REGEN + id);
set_task(TICK, "task_regen", TASK_REGEN + id, _, _, "b");
return HC_CONTINUE;
}
/**
* Any damage the player really took restarts the wait.
*/
public on_take_damage_post(victim, inflictor, attacker, Float:damage, damageBits)
{
if (victim >= 1 && victim <= MaxClients && damage > 0.0 && GetHookChainReturn(ATYPE_INTEGER))
g_lastDamage[victim] = get_gametime();
return HC_CONTINUE;
}
/**
* A dead player regenerates nothing.
*/
public on_killed_post(victim, killer, gib)
{
remove_task(TASK_REGEN + victim);
return HC_CONTINUE;
}
public task_regen(taskId)
{
new id = taskId - TASK_REGEN;
if (!is_user_alive(id))
{
remove_task(taskId);
return;
}
if (get_gametime() - g_lastDamage[id] < g_delay)
return;
new rate = is_vip(id) ? 2 : 1;
if (g_hp > 0)
{
new Float:health = Float:get_entvar(id, var_health);
new Float:maxHealth = float(g_maxHp);
if (health < maxHealth)
set_entvar(id, var_health, floatmin(health + float(g_hp * rate), maxHealth));
}
if (g_ap > 0)
{
new ArmorType:type;
new armor = rg_get_user_armor(id, type);
if (armor < g_maxAp)
{
if (armor > 0 && type != ARMOR_NONE)
rg_set_user_armor(id, min(armor + g_ap * rate, g_maxAp), type);
else if (g_newArmor)
rg_set_user_armor(id, min(armor + g_ap * rate, g_maxAp), type == ARMOR_NONE ? ARMOR_KEVLAR : type);
}
}
}
bool:is_vip(id)
{
if (!g_vipFlagText[0])
return false;
new flags = read_flags(g_vipFlagText);
return flags != 0 && (get_user_flags(id) & flags) == flags;
}