Skip to content
View in the app

A better way to browse. Learn more.

Learn more

crossfire-debrief/addons/amxmodx/scripting/olds_crossfire.sma

olds_crossfire.sma
278 lines · 8.4 KB · Pawn (AMX Mod X)
Raw
/*
 * Crossfire Debrief (olds.ro workshop card AMX-023)
 *
 * Hostage rescue: counts the damage players deal to hostages in each named
 * map sector (where the hostage stood when hit), so admins can see where
 * hostages keep getting caught in crossfire. amx_crossfire prints the totals
 * for this round; with olds_crossfire_post 1 the worst sector is posted to
 * chat at round end.
 *
 * Damage: Ham_TakeDamage on hostage_entity and monster_scientist. The pre
 * hook notes the hostage's health, the post hook counts what was actually
 * taken, only when the attacker is a player (world, falls and other entities
 * are ignored). The plugin never changes damage, frags or money: it only
 * reads.
 *
 * Sectors: addons/amxmodx/configs/olds_crossfire/<map>.ini, one box per line:
 *     "Sector name"  x1 y1 z1  x2 y2 z2
 * The first box that holds the hostage wins; outside every box the damage is
 * counted as "(no sector)". A map without a file still gets totals under
 * "(no sector)".
 *
 * Counters start over at every round restart.
 *
 * Cvar: olds_crossfire_post 1. Command: amx_crossfire (ADMIN_MAP).
 *
 * Copyright (C) 2026 daemon (olds.ro)
 * SPDX-License-Identifier: GPL-3.0-or-later
 */
#include <amxmodx>
#include <amxmisc>
#include <hamsandwich>
#include <reapi>

#define PLUGIN  "Crossfire Debrief"
#define VERSION "1.0.0"
#define AUTHOR  "olds.ro"

const MAX_SECTORS = 64
const SECTOR_NAME = 32
const EDICT_LIMIT = 2048
const TASK_WATCH = 8611

new g_name[MAX_SECTORS + 1][SECTOR_NAME]   // the last slot is "(no sector)"
new Float:g_min[MAX_SECTORS][3]
new Float:g_max[MAX_SECTORS][3]
new g_count
new g_file[128]

new Float:g_damage[MAX_SECTORS + 1]
new Float:g_healthBefore[EDICT_LIMIT]
new bool:g_posted
new bool:g_roundLive

new g_post

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

    bind_pcvar_num(create_cvar("olds_crossfire_post", "1", FCVAR_NONE,
        "1 = post the sector with the most hostage damage to chat at round end.", true, 0.0, true, 1.0), g_post)
    AutoExecConfig(true, "olds_crossfire")

    register_concmd("amx_crossfire", "cmd_crossfire", ADMIN_MAP, "- hostage damage by map sector this round")

    RegisterHam(Ham_TakeDamage, "hostage_entity", "on_take_damage_pre", false)
    RegisterHam(Ham_TakeDamage, "hostage_entity", "on_take_damage_post", true)
    RegisterHam(Ham_TakeDamage, "monster_scientist", "on_take_damage_pre", false)
    RegisterHam(Ham_TakeDamage, "monster_scientist", "on_take_damage_post", true)

    RegisterHookChain(RG_CSGameRules_RestartRound, "on_restart_round_pre", false)
    RegisterHookChain(RG_RoundEnd, "on_round_end_post", true)
    set_task(1.0, "task_watch_round_end", TASK_WATCH, _, _, "b")

    load_sectors()
    copy(g_name[MAX_SECTORS], charsmax(g_name[]), "(no sector)")
}

/* ---------- sectors ---------- */

load_sectors()
{
    new map[64]
    get_mapname(map, charsmax(map))
    get_configsdir(g_file, charsmax(g_file))
    format(g_file, charsmax(g_file), "%s/olds_crossfire/%s.ini", g_file, map)

    new file = fopen(g_file, "rt")
    if (!file)
        return
    new line[256], name[SECTOR_NAME], value[6][16], number
    while (fgets(file, line, charsmax(line)))
    {
        number++
        trim(line)
        if (!line[0] || line[0] == ';' || (line[0] == '/' && line[1] == '/'))
            continue
        if (g_count == MAX_SECTORS)
        {
            log_amx("%s: more than %d sectors, the rest are ignored", g_file, MAX_SECTORS)
            break
        }
        if (parse(line, name, charsmax(name), value[0], charsmax(value[]), value[1], charsmax(value[]),
            value[2], charsmax(value[]), value[3], charsmax(value[]), value[4], charsmax(value[]),
            value[5], charsmax(value[])) != 7 || !name[0] || !all_numbers(value))
        {
            log_amx("%s line %d: expected ^"name^" x1 y1 z1 x2 y2 z2, skipped", g_file, number)
            continue
        }
        for (new axis = 0; axis < 3; axis++)
        {
            new Float:a = str_to_float(value[axis]), Float:b = str_to_float(value[axis + 3])
            g_min[g_count][axis] = floatmin(a, b)
            g_max[g_count][axis] = floatmax(a, b)
        }
        copy(g_name[g_count], charsmax(g_name[]), name)
        g_count++
    }
    fclose(file)
}

bool:all_numbers(const value[6][16])
{
    for (new v = 0; v < 6; v++)
    {
        new i = (value[v][0] == '-' || value[v][0] == '+') ? 1 : 0, digits
        for (; value[v][i]; i++)
        {
            if (isdigit(value[v][i]))
                digits++
            else if (value[v][i] != '.')
                return false
        }
        if (!digits)
            return false
    }
    return true
}

/* Sector index, or MAX_SECTORS for "(no sector)". */
sector_at(const Float:origin[3])
{
    for (new s = 0; s < g_count; s++)
        if (g_min[s][0] <= origin[0] <= g_max[s][0] && g_min[s][1] <= origin[1] <= g_max[s][1]
            && g_min[s][2] <= origin[2] <= g_max[s][2])
            return s
    return MAX_SECTORS
}

/* ---------- damage ---------- */

bool:is_hostage(entity)
{
    if (!(MaxClients < entity < EDICT_LIMIT) || is_nullent(entity))
        return false
    new classname[20]
    get_entvar(entity, var_classname, classname, charsmax(classname))
    return equal(classname, "hostage_entity") || equal(classname, "monster_scientist")
}

public on_take_damage_pre(hostage, inflictor, attacker, Float:damage, damageBits)
{
    if (is_hostage(hostage))
        g_healthBefore[hostage] = Float:get_entvar(hostage, var_health)
    return HAM_IGNORED
}

public on_take_damage_post(hostage, inflictor, attacker, Float:damage, damageBits)
{
    // Players only: the world, falls, doors and triggers are not crossfire.
    if (!g_roundLive || !is_hostage(hostage) || !(1 <= attacker <= MaxClients) || !is_user_connected(attacker))
        return HAM_IGNORED

    new Float:taken = g_healthBefore[hostage] - floatmax(Float:get_entvar(hostage, var_health), 0.0)
    if (taken <= 0.0)
        return HAM_IGNORED

    new Float:origin[3]
    get_entvar(hostage, var_origin, origin)
    g_damage[sector_at(origin)] += taken
    return HAM_IGNORED
}

/* ---------- rounds ---------- */

public on_restart_round_pre()
{
    for (new s = 0; s <= MAX_SECTORS; s++)
        g_damage[s] = 0.0
    g_posted = false
    g_roundLive = true
    return HC_CONTINUE
}

public on_round_end_post(WinStatus:status, ScenarioEventEndRound:event, Float:delay)
{
    if (event != ROUND_GAME_COMMENCE && event != ROUND_GAME_RESTART && get_member_game(m_bRoundTerminating))
        post_top()
    return HC_CONTINUE
}

/* A round closed by rg_round_end() from another plugin skips RG_RoundEnd. */
public task_watch_round_end()
{
    if (g_roundLive && get_member_game(m_bRoundTerminating))
        post_top()
}

post_top()
{
    g_roundLive = false
    if (g_posted)
        return
    g_posted = true
    if (!g_post)
        return
    new top = top_sector()
    if (top < 0)
        return
    client_print_color(0, print_team_default, "^4[Crossfire]^1 %L", LANG_PLAYER, "CROSSFIRE_TOP", g_name[top],
        floatround(g_damage[top]), floatround(total_damage()))
}

top_sector()
{
    new best = -1
    for (new s = 0; s <= MAX_SECTORS; s++)
    {
        if (s >= g_count && s != MAX_SECTORS)
            continue
        if (g_damage[s] > 0.0 && (best < 0 || g_damage[s] > g_damage[best]))
            best = s
    }
    return best
}

Float:total_damage()
{
    new Float:total
    for (new s = 0; s <= MAX_SECTORS; s++)
        total += g_damage[s]
    return total
}

/* ---------- report ---------- */

public cmd_crossfire(id, level, cid)
{
    if (!cmd_access(id, level, cid, 1))
        return PLUGIN_HANDLED

    console_print(id, "[Crossfire] hostage damage by players this round (%d sector(s) from %s):", g_count,
        g_count ? g_file : "no sector file")
    // Highest first, without sorting the counters themselves.
    new bool:done[MAX_SECTORS + 1], printed
    for (;;)
    {
        new best = -1
        for (new s = 0; s <= MAX_SECTORS; s++)
        {
            if (done[s] || g_damage[s] <= 0.0)
                continue
            if (best < 0 || g_damage[s] > g_damage[best])
                best = s
        }
        if (best < 0)
            break
        done[best] = true
        printed++
        console_print(id, "[Crossfire]   %s %d", g_name[best], floatround(g_damage[best]))
    }
    if (!printed)
        console_print(id, "[Crossfire]   no hostage damage yet")
    else
        console_print(id, "[Crossfire] total %d", floatround(total_damage()))
    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.