Skip to content
View in the app

A better way to browse. Learn more.

Learn more

health-regeneration/addons/amxmodx/scripting/olds_healthregen.sma

olds_healthregen.sma
237 lines · 7.7 KB · Pawn (AMX Mod X)
Raw
/*
 * Health Regeneration (olds.ro workshop card LIB-0369)
 *
 * Living players slowly get health back over time, faster while they stand
 * in a buy zone. Taking damage pauses the regeneration for a few seconds, so
 * it never helps in the middle of a fight. Health never goes above a set
 * maximum, and a player who already has more is left alone.
 *
 * Commands: none. The regeneration is silent and shows nothing on screen
 * except the health number itself.
 *
 * Cvars (in configs/plugins/olds_healthregen.cfg, which ships with the
 * plugin; if the file is missing, the plugin writes it with these defaults
 * the first time it starts):
 *   olds_regen_tick       1.0   Seconds between two regeneration steps
 *                               (0.1 - 60). A new value applies from the
 *                               next step.
 *   olds_regen_hp         2     Health added per step (0 - 100, 0 = none).
 *   olds_regen_buyzone_hp 6     Health added per step inside a buy zone,
 *                               instead of olds_regen_hp (0 - 100, 0 = none).
 *   olds_regen_delay      5.0   Seconds without damage before regeneration
 *                               starts again (0 - 60).
 *   olds_regen_max        100   Health is never raised above this (1 - 255,
 *                               the HUD shows up to 255).
 *   olds_regen_team       0     0 = both teams, 1 = Terrorists only,
 *                               2 = Counter-Terrorists only.
 *   olds_regen_flag       ""    Access flags needed, for example "b" or "bt"
 *                               (any one of them is enough). Empty = every
 *                               player.
 *
 * How it works, as the card asks: each living player has a repeating task
 * (started at RG_CBasePlayer_Spawn, first step one olds_regen_tick after the
 * spawn) that adds health with cs_get_user_buyzone choosing the amount.
 * RG_CBasePlayer_TakeDamage (post) records the time of the last damage that
 * was really taken, and RG_CBasePlayer_Killed removes the task.
 *
 * Requires AMX Mod X 1.10 with ReAPI (ReGameDLL_CS) and the cstrike 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 <cstrike>
#include <reapi>

#pragma semicolon 1

new const PLUGIN_NAME[] = "Health Regeneration";
new const PLUGIN_VERSION[] = "1.0.0";
new const PLUGIN_AUTHOR[] = "alexit (olds.ro)";

// Task id of a player's regeneration is TASK_REGEN + player index.
const TASK_REGEN = 36900;

// Value of g_lastHurt[] for a player who has taken no damage since spawning.
const Float:NEVER = -1.0;

// Values of olds_regen_team.
enum
{
	REGEN_BOTH_TEAMS,
	REGEN_TERRORISTS,
	REGEN_CTS
};

new Float:g_tick;          // olds_regen_tick
new g_regenHp;             // olds_regen_hp
new g_buyzoneHp;           // olds_regen_buyzone_hp
new Float:g_delay;         // olds_regen_delay
new g_maxHealth;           // olds_regen_max
new g_team;                // olds_regen_team
new g_flags[32];           // olds_regen_flag

// Game time of each player's last damage, for olds_regen_delay.
new Float:g_lastHurt[MAX_PLAYERS + 1] = { NEVER, ... };

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_float(create_cvar("olds_regen_tick", "1.0", FCVAR_NONE,
		"Seconds between two health regeneration steps",
		true, 0.1, true, 60.0), g_tick);

	bind_pcvar_num(create_cvar("olds_regen_hp", "2", FCVAR_NONE,
		"Health added per step (0 = none)",
		true, 0.0, true, 100.0), g_regenHp);

	bind_pcvar_num(create_cvar("olds_regen_buyzone_hp", "6", FCVAR_NONE,
		"Health added per step inside a buy zone, instead of olds_regen_hp (0 = none)",
		true, 0.0, true, 100.0), g_buyzoneHp);

	bind_pcvar_float(create_cvar("olds_regen_delay", "5.0", FCVAR_NONE,
		"Seconds without taking damage before regeneration starts again",
		true, 0.0, true, 60.0), g_delay);

	bind_pcvar_num(create_cvar("olds_regen_max", "100", FCVAR_NONE,
		"Health is never raised above this value",
		true, 1.0, true, 255.0), g_maxHealth);

	bind_pcvar_num(create_cvar("olds_regen_team", "0", FCVAR_NONE,
		"0 = both teams regenerate, 1 = Terrorists only, 2 = Counter-Terrorists only",
		true, 0.0, true, 2.0), g_team);

	bind_pcvar_string(create_cvar("olds_regen_flag", "", FCVAR_NONE,
		"Access flags a player needs to regenerate, any one is enough (empty = every player)"),
		g_flags, charsmax(g_flags));

	AutoExecConfig(true, "olds_healthregen");

	RegisterHookChain(RG_CBasePlayer_Spawn, "on_player_spawn_post", true);
	RegisterHookChain(RG_CBasePlayer_TakeDamage, "on_take_damage_post", true);
	RegisterHookChain(RG_CBasePlayer_Killed, "on_player_killed_post", true);
}

public client_disconnected(id)
{
	remove_task(TASK_REGEN + id);
	g_lastHurt[id] = NEVER;
}

/**
 * Every spawn starts a fresh regeneration. The first step comes one
 * olds_regen_tick later, never at the moment of the spawn.
 */
public on_player_spawn_post(id)
{
	remove_task(TASK_REGEN + id);
	g_lastHurt[id] = NEVER;

	if (is_user_alive(id))
		set_task(g_tick, "task_regen", TASK_REGEN + id);

	return HC_CONTINUE;
}

/**
 * Only damage that was really taken pauses the regeneration. Damage the game
 * refused (friendly fire off, god mode) returns 0 and is ignored.
 */
public on_take_damage_post(id, inflictor, attacker, Float:damage, damageBits)
{
	if (GetHookChainReturn(ATYPE_INTEGER) && is_user_connected(id))
		g_lastHurt[id] = get_gametime();

	return HC_CONTINUE;
}

public on_player_killed_post(id, attacker, gib)
{
	// A dead body never regenerates. The next spawn starts a new task.
	remove_task(TASK_REGEN + id);
	return HC_CONTINUE;
}

/**
 * One regeneration step. The task arms itself again with the current
 * olds_regen_tick, so a changed value applies from the next step.
 */
public task_regen(task)
{
	new id = task - TASK_REGEN;

	// Also covers a death or a disconnect that was not seen by the hooks.
	if (!is_user_alive(id))
		return;

	set_task(g_tick, "task_regen", task);

	if (can_regenerate(id))
		regenerate(id);
}

bool:can_regenerate(id)
{
	switch (g_team)
	{
		case REGEN_TERRORISTS:
		{
			if (TeamName:get_member(id, m_iTeam) != TEAM_TERRORIST)
				return false;
		}
		case REGEN_CTS:
		{
			if (TeamName:get_member(id, m_iTeam) != TEAM_CT)
				return false;
		}
	}

	// Read every step, so a new olds_regen_flag and admin flags given after
	// the spawn both count at once.
	new needed = read_flags(g_flags);

	if (needed && !(get_user_flags(id) & needed))
		return false;

	if (g_lastHurt[id] != NEVER && get_gametime() - g_lastHurt[id] < g_delay)
		return false;

	return true;
}

regenerate(id)
{
	new Float:health = Float:get_entvar(id, var_health);
	new Float:maxHealth = float(g_maxHealth);

	// A player at or above the maximum (another plugin may give more) is
	// left alone, never lowered.
	if (health >= maxHealth)
		return;

	new amount = cs_get_user_buyzone(id) ? g_buyzoneHp : g_regenHp;

	if (amount <= 0)
		return;

	// The game sends the new health to the player's HUD by itself.
	set_entvar(id, var_health, floatmin(health + float(amount), maxHealth));
}

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.