Skip to content
View in the app

A better way to browse. Learn more.

Learn more

longest-kill-record/addons/amxmodx/scripting/olds_longest.sma

olds_longest.sma
383 lines · 11.2 KB · Pawn (AMX Mod X)
Raw
/*
 * Longest Kill Record (olds.ro workshop card IDEA-2804ED3EE2032BB2)
 *
 * Keeps the five longest gun kills of each map, in metres. A kill that beats
 * the map's record is announced to everyone ("New longest kill: 74 m with
 * the AWP"), and /longest lists the five longest kills of the map with
 * names, weapons and distances. The records stay over map changes and
 * server restarts; each map has its own.
 *
 * Commands:
 *   say /longest          (also say_team) the five longest kills on this map
 *   olds_longest_status   (server console) the same list with the distances in
 *                         units and the SteamIDs
 *
 * Cvars (in configs/plugins/olds_longest.cfg, written with the defaults on
 * the first start if the file is missing):
 *   olds_longest_enable 1        1 = on, 0 = off (nothing is recorded)
 *   olds_longest_scale  0.0254   metres per game unit (1 unit = 1 inch)
 *
 * How it works, as the card asks: in RG_CBasePlayer_Killed post the
 * distance between the killer and the victim (get_distance_f) is turned
 * into metres with olds_longest_scale. Only gun kills count: the kill must
 * come from bullets fired by the killer himself (the damage's inflictor is
 * the killer and its type has DMG_BULLET, seen in RG_CBasePlayer_TakeDamage)
 * with a gun in hand, so grenades, the knife, the bomb, falls and suicides
 * never count. Teammates count only on free-for-all servers
 * (mp_freeforall 1). The top five of each map are kept in the nvault file
 * olds_longest: distance, weapon, SteamID and name of each kill.
 *
 * Requires AMX Mod X 1.10 with ReAPI (ReGameDLL_CS) and the nvault 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 <nvault>
#include <reapi>

#pragma semicolon 1

new const PLUGIN_NAME[] = "Longest Kill Record";
new const PLUGIN_VERSION[] = "1.0.0";
new const PLUGIN_AUTHOR[] = "alexit (olds.ro)";

new const DICTIONARY[] = "olds_longest.txt";
new const VAULT_NAME[] = "olds_longest";

const TOP_KILLS = 5;

// Names of the guns, by weapon id (CSW_*). Empty = not a gun.
new const GUN_NAMES[][] =
{
	"",             // 0
	"P228",         // CSW_P228
	"",             // 2 (unused)
	"Scout",        // CSW_SCOUT
	"",             // CSW_HEGRENADE
	"XM1014",       // CSW_XM1014
	"",             // CSW_C4
	"MAC-10",       // CSW_MAC10
	"AUG",          // CSW_AUG
	"",             // CSW_SMOKEGRENADE
	"Dual Elites",  // CSW_ELITE
	"Five-SeveN",   // CSW_FIVESEVEN
	"UMP45",        // CSW_UMP45
	"SG 550",       // CSW_SG550
	"Galil",        // CSW_GALIL
	"FAMAS",        // CSW_FAMAS
	"USP",          // CSW_USP
	"Glock-18",     // CSW_GLOCK18
	"AWP",          // CSW_AWP
	"MP5",          // CSW_MP5NAVY
	"M249",         // CSW_M249
	"M3",           // CSW_M3
	"M4A1",         // CSW_M4A1
	"TMP",          // CSW_TMP
	"G3SG1",        // CSW_G3SG1
	"",             // CSW_FLASHBANG
	"Desert Eagle", // CSW_DEAGLE
	"SG 552",       // CSW_SG552
	"AK-47",        // CSW_AK47
	"",             // CSW_KNIFE
	"P90"           // CSW_P90
};

new g_enable;            // olds_longest_enable
new Float:g_scale;       // olds_longest_scale
new g_pcvarFreeForAll;   // mp_freeforall (ReGameDLL_CS), 0 if missing

new g_map[64];
new g_vault = INVALID_HANDLE;

// The longest kills on this map, longest first.
new g_count;
new g_units[TOP_KILLS];
new g_weapon[TOP_KILLS];
new g_steamId[TOP_KILLS][MAX_AUTHID_LENGTH];
new g_name[TOP_KILLS][MAX_NAME_LENGTH];

// The last damage each player took: who dealt it, through what, how, and
// the gun the attacker held when the damage came from his own bullets.
new g_lastAttacker[MAX_PLAYERS + 1];
new g_lastInflictor[MAX_PLAYERS + 1];
new g_lastDamageBits[MAX_PLAYERS + 1];
new g_lastGun[MAX_PLAYERS + 1];

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

	bind_pcvar_num(create_cvar("olds_longest_enable", "1", FCVAR_NONE,
		"1 = the longest gun kills of each map are recorded, 0 = off",
		true, 0.0, true, 1.0), g_enable);

	bind_pcvar_float(create_cvar("olds_longest_scale", "0.0254", FCVAR_NONE,
		"Metres per game unit",
		true, 0.0001, true, 1.0), g_scale);

	AutoExecConfig(true, "olds_longest");

	g_pcvarFreeForAll = get_cvar_pointer("mp_freeforall");

	register_clcmd("say /longest", "cmd_longest");
	register_clcmd("say_team /longest", "cmd_longest");
	register_srvcmd("olds_longest_status", "cmd_status");

	RegisterHookChain(RG_CBasePlayer_TakeDamage, "on_take_damage", false);
	RegisterHookChain(RG_CBasePlayer_Killed, "on_killed_post", true);

	get_mapname(g_map, charsmax(g_map));
	strtolower(g_map);

	g_vault = nvault_open(VAULT_NAME);

	if (g_vault == INVALID_HANDLE)
		log_amx("Could not open the nvault file %s; records are kept only until the map changes.", VAULT_NAME);

	load_records();
}

public plugin_end()
{
	if (g_vault != INVALID_HANDLE)
		nvault_close(g_vault);
}

public client_connect(id)
{
	g_lastAttacker[id] = 0;
	g_lastInflictor[id] = 0;
	g_lastDamageBits[id] = 0;
	g_lastGun[id] = 0;
}

/* -------------------------------------------------------------------------
 * Kills
 * ---------------------------------------------------------------------- */

bool:is_gun(weapon)
{
	return 0 < weapon < sizeof(GUN_NAMES) && GUN_NAMES[weapon][0] != EOS;
}

public on_take_damage(victim, inflictor, attacker, Float:damage, damageBits)
{
	if (!(1 <= victim <= MaxClients))
		return HC_CONTINUE;

	g_lastAttacker[victim] = attacker;
	g_lastInflictor[victim] = inflictor;
	g_lastDamageBits[victim] = damageBits;
	g_lastGun[victim] = 0;

	// A gun's bullets come with the shooter himself as the inflictor; a
	// grenade's damage comes from the grenade.
	if (inflictor == attacker && 1 <= attacker <= MaxClients && is_user_connected(attacker))
	{
		new item = get_member(attacker, m_pActiveItem);

		if (!is_nullent(item))
			g_lastGun[victim] = get_member(item, m_iId);
	}

	return HC_CONTINUE;
}

public on_killed_post(victim, attacker, gib)
{
	if (!g_enable || !(1 <= victim <= MaxClients))
		return HC_CONTINUE;

	if (!(1 <= attacker <= MaxClients) || attacker == victim || !is_user_connected(attacker))
		return HC_CONTINUE;

	// The fatal damage: the killer's own bullets from a gun.
	if (g_lastAttacker[victim] != attacker || g_lastInflictor[victim] != attacker
		|| !(g_lastDamageBits[victim] & DMG_BULLET) || !is_gun(g_lastGun[victim]))
	{
		return HC_CONTINUE;
	}

	if (get_member(attacker, m_iTeam) == get_member(victim, m_iTeam)
		&& !(g_pcvarFreeForAll && get_pcvar_num(g_pcvarFreeForAll)))
	{
		return HC_CONTINUE;
	}

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

	add_kill(attacker, victim, g_lastGun[victim], get_distance_f(killerOrigin, victimOrigin));
	return HC_CONTINUE;
}

add_kill(attacker, victim, weapon, Float:distance)
{
	new units = floatround(distance);

	// Longest first; a kill as long as one already listed goes after it.
	new place = g_count;

	while (place > 0 && units > g_units[place - 1])
		place--;

	if (place >= TOP_KILLS)
		return;

	for (new i = min(g_count, TOP_KILLS - 1); i > place; i--)
	{
		g_units[i] = g_units[i - 1];
		g_weapon[i] = g_weapon[i - 1];
		copy(g_steamId[i], charsmax(g_steamId[]), g_steamId[i - 1]);
		copy(g_name[i], charsmax(g_name[]), g_name[i - 1]);
	}

	g_units[place] = units;
	g_weapon[place] = weapon;
	get_user_authid(attacker, g_steamId[place], charsmax(g_steamId[]));
	get_user_name(attacker, g_name[place], charsmax(g_name[]));
	g_count = min(g_count + 1, TOP_KILLS);

	save_records();

	if (place == 0)
	{
		new victimName[MAX_NAME_LENGTH];
		get_user_name(victim, victimName, charsmax(victimName));

		client_print_color(0, attacker, "%L", LANG_PLAYER, "LONGEST_NEW_RECORD",
			metres(units), GUN_NAMES[weapon], g_name[0], victimName);
	}
}

metres(units)
{
	return floatround(float(units) * g_scale);
}

/* -------------------------------------------------------------------------
 * Records in the vault: "<map>#<place>" = "<units> <weapon id> <SteamID> <name>"
 * ---------------------------------------------------------------------- */

record_key(place, out[], len)
{
	formatex(out, len, "%s#%d", g_map, place + 1);
}

load_records()
{
	g_count = 0;

	if (g_vault == INVALID_HANDLE)
		return;

	new key[96], value[160], timestamp;
	new units[16], weapon[8], steamId[MAX_AUTHID_LENGTH], rest[160];

	for (new place = 0; place < TOP_KILLS; place++)
	{
		record_key(place, key, charsmax(key));

		if (!nvault_lookup(g_vault, key, value, charsmax(value), timestamp))
			break;

		// The name comes last and may hold spaces.
		strtok2(value, units, charsmax(units), rest, charsmax(rest), ' ');
		copy(value, charsmax(value), rest);
		strtok2(value, weapon, charsmax(weapon), rest, charsmax(rest), ' ');
		copy(value, charsmax(value), rest);
		strtok2(value, steamId, charsmax(steamId), rest, charsmax(rest), ' ');

		if (!is_str_num(units) || !is_gun(str_to_num(weapon)))
			break;

		g_units[g_count] = str_to_num(units);
		g_weapon[g_count] = str_to_num(weapon);
		copy(g_steamId[g_count], charsmax(g_steamId[]), steamId);
		copy(g_name[g_count], charsmax(g_name[]), rest);
		g_count++;
	}
}

save_records()
{
	if (g_vault == INVALID_HANDLE)
		return;

	new key[96], value[160];

	for (new place = 0; place < TOP_KILLS; place++)
	{
		record_key(place, key, charsmax(key));

		if (place < g_count)
		{
			formatex(value, charsmax(value), "%d %d %s %s", g_units[place], g_weapon[place], g_steamId[place], g_name[place]);
			nvault_set(g_vault, key, value);
		}
		else
		{
			nvault_remove(g_vault, key);
		}
	}
}

/* -------------------------------------------------------------------------
 * Commands
 * ---------------------------------------------------------------------- */

public cmd_longest(id)
{
	if (!g_enable)
	{
		client_print_color(id, print_team_default, "%L", id, "LONGEST_OFF");
		return PLUGIN_HANDLED;
	}

	if (!g_count)
	{
		client_print_color(id, print_team_default, "%L", id, "LONGEST_NONE", g_map);
		return PLUGIN_HANDLED;
	}

	client_print_color(id, print_team_default, "%L", id, "LONGEST_HEADER", g_map);

	for (new place = 0; place < g_count; place++)
	{
		client_print_color(id, print_team_default, "%L", id, "LONGEST_LINE",
			place + 1, metres(g_units[place]), GUN_NAMES[g_weapon[place]], g_name[place]);
	}

	return PLUGIN_HANDLED;
}

public cmd_status()
{
	server_print("Longest Kill Record: %s, map %s, %d of %d records",
		g_enable ? "enabled" : "disabled", g_map, g_count, TOP_KILLS);

	for (new place = 0; place < g_count; place++)
	{
		server_print("#%d %d m (%d units) with the %s by %s (%s)",
			place + 1, metres(g_units[place]), g_units[place], GUN_NAMES[g_weapon[place]],
			g_name[place], g_steamId[place]);
	}

	return PLUGIN_HANDLED;
}

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.