Skip to content
View in the app

A better way to browse. Learn more.

Learn more

armor-weight-speed-penalty/addons/amxmodx/scripting/olds_armorweight.sma

olds_armorweight.sma
160 lines · 5.1 KB · Pawn (AMX Mod X)
Raw
/*
 * Armor Weight Speed Penalty (olds.ro workshop card LIB-0084)
 *
 * Armor is heavy: every armor point lowers the player's top speed a little,
 * and a helmet adds a bit more. The penalty follows the armor as it is
 * bought or shot away.
 *
 * Commands: none.
 *
 * Cvars (in configs/plugins/olds_armorweight.cfg, which ships with the
 * plugin; if the file is missing, the plugin writes it with these defaults
 * the first time it starts):
 *   olds_aw_factor  0.0005  Speed lost per armor point, as a fraction
 *                           (100 armor = 0.05 = 5 percent).
 *   olds_aw_helmet  0.01    Extra speed lost with a helmet (0.01 = 1
 *                           percent).
 *   olds_aw_min     0.8     The player keeps at least this part of his
 *                           speed (1.0 = no penalty at all).
 *
 * Rules:
 *   - The speed is the game's own speed for the weapon in hand (knife,
 *     rifle, zoomed sniper, shield...), multiplied by
 *     max(1 - armor * olds_aw_factor - helmet, olds_aw_min).
 *   - The penalty never stacks: the game works the speed out again from the
 *     weapon each time, and the plugin multiplies once.
 *   - It follows the armor at once: buying armor, losing armor to damage
 *     and a change of the cvars all update the speed.
 *   - Freeze time is left alone (players cannot move then); the penalty
 *     applies as soon as it ends.
 *
 * How it works, as the card asks: RG_CBasePlayer_ResetMaxSpeed (post)
 * lowers the speed the game just set; the Battery and ArmorType messages
 * (sent when the armor or the helmet changes) make the game reset the speed
 * again with rg_reset_maxspeed.
 *
 * 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 Weight Speed Penalty";
new const PLUGIN_VERSION[] = "1.0.0";
new const PLUGIN_AUTHOR[] = "alexit (olds.ro)";

new Float:g_factor;    // olds_aw_factor
new Float:g_helmet;    // olds_aw_helmet
new Float:g_min;       // olds_aw_min

public plugin_init()
{
	register_plugin(PLUGIN_NAME, PLUGIN_VERSION, PLUGIN_AUTHOR);

	// The speed hook comes from ReGameDLL_CS.
	if (!is_regamedll())
		set_fail_state("ReGameDLL_CS is not running. This plugin needs ReGameDLL_CS and ReAPI.");

	new cvar;

	cvar = create_cvar("olds_aw_factor", "0.0005", FCVAR_NONE,
		"Speed lost per armor point, as a fraction (100 armor x 0.0005 = 5 percent)",
		true, 0.0, true, 0.01);
	bind_pcvar_float(cvar, g_factor);
	hook_cvar_change(cvar, "on_cvar_change");

	cvar = create_cvar("olds_aw_helmet", "0.01", FCVAR_NONE,
		"Extra speed lost with a helmet (0.01 = 1 percent)",
		true, 0.0, true, 1.0);
	bind_pcvar_float(cvar, g_helmet);
	hook_cvar_change(cvar, "on_cvar_change");

	cvar = create_cvar("olds_aw_min", "0.8", FCVAR_NONE,
		"The player keeps at least this part of his speed (1.0 = no penalty)",
		true, 0.0, true, 1.0);
	bind_pcvar_float(cvar, g_min);
	hook_cvar_change(cvar, "on_cvar_change");

	AutoExecConfig(true, "olds_armorweight");

	register_event("Battery", "on_armor_message", "be");
	register_event("ArmorType", "on_armor_message", "be");

	RegisterHookChain(RG_CBasePlayer_ResetMaxSpeed, "on_reset_max_speed_post", true);
}

/**
 * The game has just set the speed for the weapon in hand: armor lowers it.
 */
public on_reset_max_speed_post(id)
{
	if (!is_user_alive(id) || get_member_game(m_bFreezePeriod))
		return HC_CONTINUE;

	new Float:speed = Float:get_entvar(id, var_maxspeed);

	// Frozen in some other way: nothing to lower.
	if (speed <= 1.0)
		return HC_CONTINUE;

	new Float:multiplier = speed_multiplier(id);

	if (multiplier < 1.0)
		set_entvar(id, var_maxspeed, speed * multiplier);

	return HC_CONTINUE;
}

/**
 * Battery (armor points) or ArmorType (helmet) changed: the game works the
 * speed out again, and the hook above applies the new penalty.
 */
public on_armor_message(id)
{
	if (is_user_alive(id) && !get_member_game(m_bFreezePeriod))
		rg_reset_maxspeed(id);
}

public on_cvar_change(pcvar, const oldValue[], const newValue[])
{
	if (get_member_game(m_bFreezePeriod))
		return;

	for (new id = 1; id <= MaxClients; id++)
	{
		if (is_user_alive(id))
			rg_reset_maxspeed(id);
	}
}

Float:speed_multiplier(id)
{
	new ArmorType:type;
	new armor = rg_get_user_armor(id, type);

	if (armor <= 0)
		return 1.0;

	new Float:penalty = float(armor) * g_factor;

	if (type == ARMOR_VESTHELM)
		penalty += g_helmet;

	return floatmax(1.0 - penalty, g_min);
}

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.