Skip to content
View in the app

A better way to browse. Learn more.

Learn more

enemy-radar-display/addons/amxmodx/scripting/olds_enemyradar.sma

olds_enemyradar.sma
223 lines · 6.3 KB · Pawn (AMX Mod X)
Raw
/*
 * Enemy Radar Display (olds.ro workshop card LIB-0004)
 *
 * A player with the allowed admin flag types /radar: for olds_er_duration seconds the
 * enemies within olds_er_range units show on his radar, even behind walls, updated every
 * half second. When it runs out, when he dies or when a new round starts, the dots are
 * taken off and the radar works as before.
 *
 * The plugin only sends the stock Radar message (player index and position) to the
 * watcher; it uses emessage_* so other plugins can see or block it. A dot is taken off by
 * sending a position far outside the radar range.
 *
 *   say /radar   switch the enemy radar on (olds_er_flag)
 *
 * Cvars: olds_er_flag "b" (empty = everyone), olds_er_duration 10, olds_er_range 1500.
 *
 * Copyright (C) 2026 daemon (olds.ro)
 * SPDX-License-Identifier: GPL-3.0-or-later
 */
#include <amxmodx>
#include <fakemeta>
#include <reapi>

#define PLUGIN  "Enemy Radar Display"
#define VERSION "1.0.0"
#define AUTHOR  "olds.ro"

#define TASK_SCAN 40040     // + watcher id
#define TASK_END  40080     // + watcher id

new g_cvarFlag, Float:g_duration, Float:g_range;
new g_msgRadar;

new bool:g_active[MAX_PLAYERS + 1];
new g_shown[MAX_PLAYERS + 1];          // bit per enemy (id - 1) whose dot this watcher sees
new Float:g_nextUse[MAX_PLAYERS + 1];

public plugin_init()
{
    register_plugin(PLUGIN, VERSION, AUTHOR);
    register_dictionary("olds_enemyradar.txt");

    g_cvarFlag = create_cvar("olds_er_flag", "b", FCVAR_NONE,
        "Admin flag(s) allowed to use /radar; empty = everyone");
    bind_pcvar_float(create_cvar("olds_er_duration", "10", FCVAR_NONE,
        "Seconds the enemy radar stays on", true, 1.0, true, 300.0), g_duration);
    bind_pcvar_float(create_cvar("olds_er_range", "1500", FCVAR_NONE,
        "Range in units of the enemy radar", true, 100.0, true, 8192.0), g_range);
    AutoExecConfig(true, "olds_enemyradar");

    register_clcmd("say /radar", "cmd_radar");
    register_clcmd("say_team /radar", "cmd_radar");
    RegisterHookChain(RG_CSGameRules_RestartRound, "on_restart_round", true);
    RegisterHookChain(RG_CBasePlayer_Killed, "on_killed", true);

    g_msgRadar = get_user_msgid("Radar");
}

/* ---------- switching on and off ---------- */

public cmd_radar(id)
{
    if (!is_user_connected(id))
        return PLUGIN_HANDLED;
    new Float:now = get_gametime();
    if (now < g_nextUse[id])
        return PLUGIN_HANDLED;
    g_nextUse[id] = now + 1.0;

    new flags[24];
    get_pcvar_string(g_cvarFlag, flags, charsmax(flags));
    trim(flags);
    if (flags[0] && !(get_user_flags(id) & read_flags(flags)))
    {
        client_print_color(id, print_team_default, "^4[Radar]^1 %L", id, "ER_DENIED");
        return PLUGIN_HANDLED;
    }
    if (!is_user_alive(id))
    {
        client_print_color(id, print_team_default, "^4[Radar]^1 %L", id, "ER_DEAD");
        return PLUGIN_HANDLED;
    }
    if (g_active[id])
    {
        client_print_color(id, print_team_default, "^4[Radar]^1 %L", id, "ER_ALREADY");
        return PLUGIN_HANDLED;
    }

    g_active[id] = true;
    g_shown[id] = 0;
    set_task(0.5, "task_scan", TASK_SCAN + id, _, _, "b");
    set_task(g_duration, "task_end", TASK_END + id);
    scan(id);
    client_print_color(id, print_team_default, "^4[Radar]^1 %L", id, "ER_ON", floatround(g_duration),
        floatround(g_range));
    return PLUGIN_HANDLED;
}

public task_end(task)
{
    new id = task - TASK_END;
    if (!g_active[id])
        return;
    stop(id, true);
    if (is_user_connected(id))
        client_print_color(id, print_team_default, "^4[Radar]^1 %L", id, "ER_OFF");
}

/* Switches the watcher's enemy radar off; hide = take the dots off his radar. */
stop(id, bool:hide)
{
    remove_task(TASK_SCAN + id);
    remove_task(TASK_END + id);
    if (!g_active[id])
        return;
    g_active[id] = false;
    if (hide && is_user_connected(id))
    {
        for (new enemy = 1; enemy <= MaxClients; enemy++)
            if (g_shown[id] & (1 << (enemy - 1)))
                send_far(id, enemy);
    }
    g_shown[id] = 0;
}

public on_restart_round()
{
    for (new id = 1; id <= MaxClients; id++)
        stop(id, true);
}

public on_killed(victim)
{
    // A dead watcher gets no more Radar messages from this plugin.
    if (1 <= victim <= MaxClients && g_active[victim])
        stop(victim, false);
    // A dead enemy's dot is taken off for every watcher at the next scan.
    return HC_CONTINUE;
}

public client_disconnected(id)
{
    stop(id, false);
    g_nextUse[id] = 0.0;
    new bit = 1 << (id - 1);
    for (new w = 1; w <= MaxClients; w++)
    {
        if (g_active[w] && (g_shown[w] & bit))
        {
            g_shown[w] &= ~bit;
            send_far(w, id);
        }
    }
}

/* ---------- the dots ---------- */

public task_scan(task)
{
    new id = task - TASK_SCAN;
    if (!g_active[id] || !is_user_alive(id))
    {
        stop(id, false);
        return;
    }
    scan(id);
}

scan(id)
{
    new TeamName:team = get_member(id, m_iTeam);
    new Float:origin[3], Float:enemy_origin[3];
    pev(id, pev_origin, origin);
    new bit;

    for (new enemy = 1; enemy <= MaxClients; enemy++)
    {
        if (enemy == id)
            continue;
        bit = 1 << (enemy - 1);
        new bool:show = false;
        if (is_user_alive(enemy))
        {
            new TeamName:other = get_member(enemy, m_iTeam);
            if ((other == TEAM_TERRORIST || other == TEAM_CT) && other != team)
            {
                pev(enemy, pev_origin, enemy_origin);
                show = get_distance_f(origin, enemy_origin) <= g_range;
            }
        }
        if (show)
        {
            send_radar(id, enemy, enemy_origin);
            g_shown[id] |= bit;
        }
        else if (g_shown[id] & bit)
        {
            send_far(id, enemy);
            g_shown[id] &= ~bit;
        }
    }
}

send_radar(id, enemy, const Float:origin[3])
{
    emessage_begin(MSG_ONE_UNRELIABLE, g_msgRadar, _, id);
    ewrite_byte(enemy);
    ewrite_coord(floatround(origin[0]));
    ewrite_coord(floatround(origin[1]));
    ewrite_coord(floatround(origin[2]));
    emessage_end();
}

/* A position outside every radar range: the client stops drawing the dot. */
send_far(id, enemy)
{
    emessage_begin(MSG_ONE, g_msgRadar, _, id);
    ewrite_byte(enemy);
    ewrite_coord(4095);
    ewrite_coord(4095);
    ewrite_coord(4095);
    emessage_end();
}

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.