Skip to content
View in the app

A better way to browse. Learn more.

Learn more

escort-receipt/addons/amxmodx/scripting/olds_escort.sma

olds_escort.sma
271 lines · 8.4 KB · Pawn (AMX Mod X)
Raw
/*
 * Escort Receipt (olds.ro workshop card AMX-022)
 *
 * Hostage rescue: when the round ends, everyone gets a short receipt naming,
 * for each hostage, the player who first led it (the escort) and the player
 * who brought it to the rescue zone (the rescuer). The same player can be
 * both; a hostage killed before rescue is listed as lost, with its escort.
 *
 * Callbacks (ReGameDLL_CS; ReAPI 5.29 has no hostage hookchain, so these are
 * the game's own):
 *   - first escort: Ham_Use on hostage_entity / monster_scientist (post), the
 *     first living counter-terrorist to use a hostage this round;
 *   - rescue: the game's "Rescued_A_Hostage" log event, which names the player
 *     the hostage was following; the hostage that just left play (and follows
 *     that player) is matched to it;
 *   - killed: Ham_Killed on the hostage (post).
 * Records are keyed by the hostage entity and a round counter, so events from
 * an earlier round, a second rescue event for the same hostage or an entity
 * that is gone are ignored.
 *
 * Cvar: olds_escort_show 1 (0 = no receipt).
 * Command: olds_escort_status (ADMIN_CFG).
 *
 * Copyright (C) 2026 daemon (olds.ro)
 * SPDX-License-Identifier: GPL-3.0-or-later
 */
#include <amxmodx>
#include <amxmisc>
#include <hamsandwich>
#include <reapi>

#define PLUGIN  "Escort Receipt"
#define VERSION "1.0.0"
#define AUTHOR  "olds.ro"

const MAX_HOSTAGES = 16
const TASK_WATCH = 7811

enum HostageState
{
    HS_WAITING = 0,
    HS_RESCUED,
    HS_LOST
}

new g_hostage[MAX_HOSTAGES]                 // entity index; numbered 1.. in map order
new g_hostageCount
new g_round                                  // round counter
new g_recordRound[MAX_HOSTAGES]              // round the record below belongs to
new HostageState:g_state[MAX_HOSTAGES]
new g_escort[MAX_HOSTAGES][MAX_NAME_LENGTH]
new g_rescuer[MAX_HOSTAGES][MAX_NAME_LENGTH]
new bool:g_printed
new bool:g_roundLive

new g_show

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

    bind_pcvar_num(create_cvar("olds_escort_show", "1", FCVAR_NONE,
        "1 = print the escort receipt to everyone at round end.", true, 0.0, true, 1.0), g_show)
    AutoExecConfig(true, "olds_escort")

    register_concmd("olds_escort_status", "cmd_status", ADMIN_CFG, "- this round's escorts and rescuers")

    RegisterHam(Ham_Use, "hostage_entity", "on_hostage_use_post", true)
    RegisterHam(Ham_Killed, "hostage_entity", "on_hostage_killed_post", true)
    RegisterHam(Ham_Use, "monster_scientist", "on_hostage_use_post", true)
    RegisterHam(Ham_Killed, "monster_scientist", "on_hostage_killed_post", true)
    register_logevent("on_rescued", 3, "2=Rescued_A_Hostage")

    RegisterHookChain(RG_CSGameRules_RestartRound, "on_restart_round_post", true)
    RegisterHookChain(RG_RoundEnd, "on_round_end_post", true)
    set_task(1.0, "task_watch_round_end", TASK_WATCH, _, _, "b")
}

/* ---------- hostages ---------- */

collect_hostages()
{
    g_hostageCount = 0
    add_hostages("hostage_entity")
    add_hostages("monster_scientist")
}

add_hostages(const classname[])
{
    new entity = MaxClients
    while (g_hostageCount < MAX_HOSTAGES && (entity = rg_find_ent_by_class(entity, classname)) > 0)
        g_hostage[g_hostageCount++] = entity
}

slot_of(entity)
{
    for (new i = 0; i < g_hostageCount; i++)
        if (g_hostage[i] == entity)
            return is_nullent(entity) ? -1 : i
    return -1
}

/* Out of play but not dead: the game removes a rescued hostage this way. */
bool:left_play(entity)
{
    return Float:get_entvar(entity, var_health) > 0.0
        && ((get_entvar(entity, var_effects) & EF_NODRAW) || get_entvar(entity, var_solid) == SOLID_NOT)
}

record_live(slot)
{
    // A record from an earlier round is reset before use.
    if (g_recordRound[slot] != g_round)
    {
        g_recordRound[slot] = g_round
        g_state[slot] = HS_WAITING
        g_escort[slot][0] = EOS
        g_rescuer[slot][0] = EOS
    }
}

/* ---------- events ---------- */

public on_restart_round_post()
{
    g_round++
    g_printed = false
    g_roundLive = true
    collect_hostages()
}

public on_hostage_use_post(hostage, caller, activator, useType, Float:value)
{
    if (!g_roundLive || !(1 <= activator <= MaxClients) || !is_user_alive(activator)
        || TeamName:get_member(activator, m_iTeam) != TEAM_CT)
        return HAM_IGNORED
    new slot = slot_of(hostage)
    if (slot < 0 || Float:get_entvar(hostage, var_health) <= 0.0)
        return HAM_IGNORED

    record_live(slot)
    if (g_state[slot] == HS_WAITING && !g_escort[slot][0])
        get_user_name(activator, g_escort[slot], charsmax(g_escort[]))
    return HAM_IGNORED
}

public on_hostage_killed_post(hostage, attacker, gib)
{
    new slot = slot_of(hostage)
    if (!g_roundLive || slot < 0)
        return HAM_IGNORED
    record_live(slot)
    if (g_state[slot] == HS_WAITING)
        g_state[slot] = HS_LOST
    return HAM_IGNORED
}

/* "Name<uid><steamid><CT>" triggered "Rescued_A_Hostage" */
public on_rescued()
{
    if (!g_roundLive)
        return
    new logUser[96], name[MAX_NAME_LENGTH]
    read_logargv(0, logUser, charsmax(logUser))
    parse_loguser(logUser, name, charsmax(name))
    new rescuer = find_player_ex(FindPlayer_MatchName, name)

    // The hostage that just left play: the one following this player first.
    new pick = -1
    for (new i = 0; i < g_hostageCount; i++)
    {
        if (is_nullent(g_hostage[i]) || !left_play(g_hostage[i]))
            continue
        record_live(i)
        if (g_state[i] != HS_WAITING)
            continue
        if (pick < 0)
            pick = i
        if (rescuer && get_member(g_hostage[i], m_hTargetEnt) == rescuer)
        {
            pick = i
            break
        }
    }
    if (pick < 0)
        return      // repeated or unmatched event: nothing to record

    g_state[pick] = HS_RESCUED
    copy(g_rescuer[pick], charsmax(g_rescuer[]), name)
    if (!g_escort[pick][0])
        copy(g_escort[pick], charsmax(g_escort[]), name)
}

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

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

/* ---------- the receipt ---------- */

print_receipt()
{
    g_roundLive = false
    if (g_printed)
        return
    g_printed = true
    if (!g_show)
        return

    new lines
    for (new i = 0; i < g_hostageCount; i++)
    {
        if (g_recordRound[i] != g_round || !g_escort[i][0])
            continue
        if (!lines++)
            client_print_color(0, print_team_default, "^4[Escort]^1 %L", LANG_PLAYER, "ESCORT_TITLE")
        print_line(0, i)
    }
}

print_line(id, slot)
{
    switch (g_state[slot])
    {
        case HS_RESCUED:
        {
            if (equal(g_escort[slot], g_rescuer[slot]))
                client_print_color(id, print_team_default, "^4[Escort]^1 %L", id ? id : LANG_PLAYER, "ESCORT_BOTH",
                    slot + 1, g_escort[slot])
            else
                client_print_color(id, print_team_default, "^4[Escort]^1 %L", id ? id : LANG_PLAYER, "ESCORT_PAIR",
                    slot + 1, g_escort[slot], g_rescuer[slot])
        }
        case HS_LOST:
            client_print_color(id, print_team_default, "^4[Escort]^1 %L", id ? id : LANG_PLAYER, "ESCORT_LOST",
                slot + 1, g_escort[slot])
        default:
            client_print_color(id, print_team_default, "^4[Escort]^1 %L", id ? id : LANG_PLAYER, "ESCORT_NOT_RESCUED",
                slot + 1, g_escort[slot])
    }
}

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

    console_print(id, "[Escort] round %d, %d hostage(s), round %s, receipt %d", g_round, g_hostageCount,
        g_roundLive ? "live" : "over", g_show)
    new const stateName[HostageState][] = { "waiting", "rescued", "lost" }
    for (new i = 0; i < g_hostageCount; i++)
    {
        new bool:current = g_recordRound[i] == g_round
        console_print(id, "[Escort] hostage %d (entity %d): %s; escort %s; rescuer %s", i + 1, g_hostage[i],
            current ? stateName[g_state[i]] : "waiting", current && g_escort[i][0] ? g_escort[i] : "-",
            current && g_rescuer[i][0] ? g_rescuer[i] : "-")
    }
    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.