Skip to content
View in the app

A better way to browse. Learn more.

Learn more

kill-distance-display/addons/amxmodx/scripting/olds_kdist.sma

olds_kdist.sma
158 lines · 5.3 KB · Pawn (AMX Mod X)
Raw
/*
 * Kill Distance Display (olds.ro workshop card LIB-2111)
 *
 * After every kill the killer sees, in the centre of the screen, how far
 * away the victim was, such as "Kill distance: 34.2 m". The victim can see
 * it too ("Killed by Terry from 34.2 m").
 *
 *   - The distance is measured between the origins of the killer and the
 *     victim at the moment of the kill, in game units, and shown in metres
 *     (1 unit = 0.0254 m) or in feet (12 units = 1 ft).
 *   - A grenade kill shows the distance from where the thrower stands when
 *     the grenade kills.
 *   - Suicides, kills by the world (falls, the bomb, map hazards) and kills
 *     by non-player entities show nothing.
 *   - Knife kills can be left out (olds_kdist_knife 0), and short kills
 *     can be hidden with olds_kdist_min.
 *
 * Cvars (configs/plugins/olds_kdist.cfg; AutoExecConfig writes that file
 * with these defaults if it is missing):
 *   olds_kdist_unit   1  1 = metres, 2 = feet.
 *   olds_kdist_victim 1  1 = the victim also sees the distance.
 *   olds_kdist_min    0  Kills closer than this (in the chosen unit) show
 *                        nothing; 0 = every kill.
 *   olds_kdist_knife  1  1 = knife kills show the distance too, 0 = not.
 *
 * Every text players read comes from data/lang/olds_kdist.txt (English).
 *
 * Requires AMX Mod X 1.10 or newer and ReAPI with ReGameDLL_CS. Written for
 * AMX Mod X 1.10.0.5490, ReAPI 5.29.0.358, 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[] = "Kill Distance Display";
new const PLUGIN_VERSION[] = "1.0.0";
new const PLUGIN_AUTHOR[] = "alexit (olds.ro)";

new const DICTIONARY[] = "olds_kdist.txt";

const UNIT_METRES = 1;
const UNIT_FEET = 2;

// One game unit is one inch.
const Float:METRES_PER_UNIT = 0.0254;
const Float:UNITS_PER_FOOT = 12.0;

new g_unit;             // olds_kdist_unit
new g_victim;           // olds_kdist_victim
new Float:g_min;        // olds_kdist_min
new g_knife;            // olds_kdist_knife

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

	bind_pcvar_num(create_cvar("olds_kdist_unit", "1", FCVAR_NONE,
		"Unit of the kill distance: 1 = metres, 2 = feet",
		true, 1.0, true, 2.0), g_unit);

	bind_pcvar_num(create_cvar("olds_kdist_victim", "1", FCVAR_NONE,
		"1 = the victim also sees from how far he was killed, 0 = only the killer",
		true, 0.0, true, 1.0), g_victim);

	bind_pcvar_float(create_cvar("olds_kdist_min", "0", FCVAR_NONE,
		"Kills closer than this distance (in the chosen unit) show nothing; 0 = every kill",
		true, 0.0), g_min);

	bind_pcvar_num(create_cvar("olds_kdist_knife", "1", FCVAR_NONE,
		"1 = knife kills also show the distance, 0 = knife kills show nothing",
		true, 0.0, true, 1.0), g_knife);

	AutoExecConfig(true, "olds_kdist");

	RegisterHookChain(RG_CBasePlayer_Killed, "OnPlayerKilled_Post", true);
}

public OnPlayerKilled_Post(victim, killer)
{
	// Only kills made by another player.
	if (killer < 1 || killer > MaxClients || killer == victim || !is_user_connected(killer))
		return HC_CONTINUE;

	if (!g_knife && is_knife_kill(victim, killer))
		return HC_CONTINUE;

	new Float:killerOrigin[3], Float:victimOrigin[3];
	get_entvar(killer, var_origin, killerOrigin);
	get_entvar(victim, var_origin, victimOrigin);

	new Float:distance = convert_units(get_distance_f(killerOrigin, victimOrigin));

	if (distance < g_min)
		return HC_CONTINUE;

	// Rounded to tenths (%.1f would cut 25.39 down to 25.3).
	new tenths = floatround(distance * 10.0);
	new text[32];

	format_distance(killer, tenths, text, charsmax(text));
	client_print(killer, print_center, "%L", killer, "OLDS_KDIST_KILLER", text);

	if (g_victim && is_user_connected(victim) && !is_user_bot(victim))
	{
		new name[MAX_NAME_LENGTH];
		get_user_name(killer, name, charsmax(name));

		format_distance(victim, tenths, text, charsmax(text));
		client_print(victim, print_center, "%L", victim, "OLDS_KDIST_VICTIM", name, text);
	}

	return HC_CONTINUE;
}

/**
 * The killer used the knife: the victim was not killed by a grenade (or the
 * bomb) and the killer holds the knife.
 */
bool:is_knife_kill(victim, killer)
{
	if (get_member(victim, m_bKilledByGrenade) || get_member(victim, m_bKilledByBomb))
		return false;

	return is_user_alive(killer) && get_user_weapon(killer) == CSW_KNIFE;
}

Float:convert_units(Float:units)
{
	if (g_unit == UNIT_FEET)
		return units / UNITS_PER_FOOT;

	return units * METRES_PER_UNIT;
}

/**
 * 254 tenths -> "25.4 m" (or "ft"), in the player's language.
 */
format_distance(id, tenths, text[], len)
{
	formatex(text, len, "%d.%d %L", tenths / 10, tenths % 10, id,
		g_unit == UNIT_FEET ? "OLDS_KDIST_FEET" : "OLDS_KDIST_METRES");
}

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.