Skip to content
View in the app

A better way to browse. Learn more.

Learn more

hostage-carry/addons/amxmodx/scripting/olds_hcarry.sma

olds_hcarry.sma
348 lines · 10.8 KB · Pawn (AMX Mod X)
Raw
/*
 * Hostage Carry (olds.ro)
 * Copyright (C) 2026 daemon (olds.ro), written by daemon's plugin agent
 *
 * 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/>.
 *
 * Hostage Rescue: a counter-terrorist picks a hostage up (+use on it, or the +carry bind
 * aimed at it) and carries it on the back: the hostage follows the carrier
 * (pev_movetype MOVETYPE_FOLLOW, pev_aiment) and the carrier runs at olds_hcarry_speed of
 * the normal speed. +carry again puts the hostage down at the carrier's feet and it goes on
 * following. Carrying it into a rescue zone sets it down there and the game rescues it as
 * usual. When the carrier dies (RG_CBasePlayer_Killed) the hostage is put down at the body.
 * Terrorists cannot pick hostages up.
 *
 * Cvars: olds_hcarry_speed 0.8, olds_hcarry_dropdmg 1, olds_hcarry_maxhp 100
 * Admin: amx_hcarry_status (ADMIN_KICK)
 */

#include <amxmodx>
#include <amxmisc>
#include <cstrike>
#include <fakemeta>
#include <hamsandwich>
#include <reapi>

#pragma semicolon 0

#define PLUGIN  "Hostage Carry"
#define VERSION "1.0.0"
#define AUTHOR  "daemon (olds.ro)"

#define TASK_ZONE 8620
#define REACH     96.0

new const PICKUP_SOUND[] = "hostage/hos1.wav"

new Float:g_speed
new g_dropDamage
new g_maxHp

new g_carried[MAX_PLAYERS + 1]
new g_rescues

public plugin_precache()
{
    precache_sound(PICKUP_SOUND)
}

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

    bind_pcvar_float(create_cvar("olds_hcarry_speed", "0.8", FCVAR_NONE,
        "Carrier speed as a share of the normal speed", true, 0.3, true, 1.0), g_speed)
    bind_pcvar_num(create_cvar("olds_hcarry_dropdmg", "1", FCVAR_NONE,
        "1 = a carrier who takes damage drops the hostage", true, 0.0, true, 1.0), g_dropDamage)
    bind_pcvar_num(create_cvar("olds_hcarry_maxhp", "100", FCVAR_NONE,
        "Only hostages with at most this much health can be carried", true, 1.0, true, 1000.0), g_maxHp)
    AutoExecConfig(true, "olds_hcarry")

    RegisterHam(Ham_Use, "hostage_entity", "on_hostage_use", false)
    RegisterHam(Ham_CS_Player_ResetMaxSpeed, "player", "on_maxspeed_post", true)
    RegisterHookChain(RG_CBasePlayer_Killed, "on_killed_post", true)
    RegisterHookChain(RG_CBasePlayer_TakeDamage, "on_damage_post", true)
    RegisterHookChain(RG_CSGameRules_RestartRound, "on_restart_pre", false)
    register_logevent("on_rescued", 3, "2=Rescued_A_Hostage")

    register_clcmd("+carry", "cmd_carry")
    register_clcmd("-carry", "cmd_carry_release")
    register_concmd("amx_hcarry_status", "cmd_status", ADMIN_KICK, "- shows who carries a hostage")
}

/* ------------------------------------------------------------ picking up */

bool:can_lift(hostage)
{
    if (!pev_valid(hostage) || pev(hostage, pev_deadflag) != DEAD_NO)
        return false
    new Float:health
    pev(hostage, pev_health, health)
    return health > 0.0 && health <= float(g_maxHp)
}

bool:is_hostage(ent)
{
    if (!pev_valid(ent))
        return false
    new cls[16]
    pev(ent, pev_classname, cls, charsmax(cls))
    return equal(cls, "hostage_entity")
}

public on_hostage_use(hostage, caller, activator, type, Float:value)
{
    if (caller < 1 || caller > MaxClients || !is_user_alive(caller))
        return HAM_IGNORED
    if (cs_get_user_team(caller) != CS_TEAM_CT)
    {
        client_print_color(caller, print_team_red, "%L", caller, "HCARRY_T")
        return HAM_SUPERCEDE
    }
    if (g_carried[caller])
        return HAM_SUPERCEDE
    /* A plain use (no carry) still works for hostages too badly hurt to lift. */
    if (!can_lift(hostage))
        return HAM_IGNORED
    pick_up(caller, hostage)
    return HAM_SUPERCEDE
}

carrier_of(hostage)
{
    for (new i = 1; i <= MaxClients; i++)
    {
        if (g_carried[i] == hostage)
            return i
    }
    return 0
}

find_hostage(id)
{
    /* The hostage the player looks at, or failing that the nearest one within reach. */
    new Float:eye[3], Float:ofs[3], Float:angles[3], Float:dir[3], Float:stop[3]
    pev(id, pev_origin, eye)
    pev(id, pev_view_ofs, ofs)
    eye[0] += ofs[0]
    eye[1] += ofs[1]
    eye[2] += ofs[2]
    pev(id, pev_v_angle, angles)
    angle_vector(angles, ANGLEVECTOR_FORWARD, dir)
    stop[0] = eye[0] + dir[0] * REACH
    stop[1] = eye[1] + dir[1] * REACH
    stop[2] = eye[2] + dir[2] * REACH
    new trace = create_tr2()
    engfunc(EngFunc_TraceLine, eye, stop, DONT_IGNORE_MONSTERS, id, trace)
    new hit = get_tr2(trace, TR_pHit)
    free_tr2(trace)
    if (hit > MaxClients && is_hostage(hit))
        return hit
    new best, Float:bestDist = REACH, Float:pos[3]
    new ent = MaxClients
    while ((ent = engfunc(EngFunc_FindEntityByString, ent, "classname", "hostage_entity")) > 0)
    {
        pev(ent, pev_origin, pos)
        new Float:dist = get_distance_f(eye, pos)
        if (dist < bestDist && !carrier_of(ent))
        {
            bestDist = dist
            best = ent
        }
    }
    return best
}

public cmd_carry(id)
{
    if (!is_user_alive(id))
        return PLUGIN_HANDLED
    if (g_carried[id])
    {
        put_down(id, false)
        return PLUGIN_HANDLED
    }
    if (cs_get_user_team(id) != CS_TEAM_CT)
    {
        client_print_color(id, print_team_red, "%L", id, "HCARRY_T")
        return PLUGIN_HANDLED
    }
    new hostage = find_hostage(id)
    if (!hostage || carrier_of(hostage))
    {
        client_print_color(id, print_team_default, "%L", id, "HCARRY_NONE")
        return PLUGIN_HANDLED
    }
    if (!can_lift(hostage))
    {
        client_print_color(id, print_team_default, "%L", id, "HCARRY_TOO_HEAVY")
        return PLUGIN_HANDLED
    }
    pick_up(id, hostage)
    return PLUGIN_HANDLED
}

public cmd_carry_release(id)
{
    return PLUGIN_HANDLED
}

pick_up(id, hostage)
{
    if (carrier_of(hostage))
        return
    g_carried[id] = hostage
    /* The hostage's own thinking waits while it rides on the carrier's back. */
    set_pev(hostage, pev_nextthink, get_gametime() + 99999.0)
    set_pev(hostage, pev_velocity, Float:{0.0, 0.0, 0.0})
    set_pev(hostage, pev_solid, SOLID_NOT)
    set_pev(hostage, pev_movetype, MOVETYPE_FOLLOW)
    set_pev(hostage, pev_aiment, id)
    /* MOVETYPE_FOLLOW keeps the hostage at the carrier's origin plus pev_v_angle. */
    set_pev(hostage, pev_v_angle, Float:{0.0, 0.0, -20.0})
    ExecuteHamB(Ham_CS_Player_ResetMaxSpeed, id)
    emit_sound(id, CHAN_ITEM, PICKUP_SOUND, VOL_NORM, ATTN_NORM, 0, PITCH_NORM)
    client_print_color(id, print_team_blue, "%L", id, "HCARRY_PICKED")
    remove_task(TASK_ZONE + id)
    set_task(0.2, "task_zone", TASK_ZONE + id, _, _, "b")
    log_name("picked up hostage", id, hostage)
}

put_down(id, bool:atBody)
{
    new hostage = g_carried[id]
    g_carried[id] = 0
    remove_task(TASK_ZONE + id)
    if (is_user_alive(id))
        ExecuteHamB(Ham_CS_Player_ResetMaxSpeed, id)
    if (!pev_valid(hostage))
        return
    new Float:spot[3]
    pev(id, pev_origin, spot)
    /* The hostage stands on the floor at the carrier's feet (or where the body fell). */
    spot[2] -= (pev(id, pev_flags) & FL_DUCKING) ? 18.0 : 36.0
    spot[2] += 1.0
    set_pev(hostage, pev_aiment, 0)
    set_pev(hostage, pev_movetype, MOVETYPE_STEP)
    set_pev(hostage, pev_solid, SOLID_SLIDEBOX)
    set_pev(hostage, pev_v_angle, Float:{0.0, 0.0, 0.0})
    set_pev(hostage, pev_velocity, Float:{0.0, 0.0, 0.0})
    engfunc(EngFunc_SetOrigin, hostage, spot)
    engfunc(EngFunc_DropToFloor, hostage)
    set_pev(hostage, pev_nextthink, get_gametime() + 0.1)
    if (!atBody && is_user_alive(id))
        cs_set_hostage_foll(hostage, id)
    else
        cs_set_hostage_foll(hostage, 0)
    if (is_user_connected(id))
    {
        if (atBody)
            client_print_color(id, print_team_default, "%L", id, "HCARRY_LOST")
        else
            client_print_color(id, print_team_default, "%L", id, "HCARRY_DOWN")
    }
    pev(hostage, pev_origin, spot)
    new name[MAX_NAME_LENGTH]
    get_user_name(id, name, charsmax(name))
    if (atBody)
        log_amx("%s put hostage %d down at %.0f %.0f %.0f (carrier died)", name, hostage, spot[0], spot[1], spot[2])
    else
        log_amx("%s put hostage %d down at %.0f %.0f %.0f", name, hostage, spot[0], spot[1], spot[2])
}

/* ------------------------------------------------------------ while carrying */

public on_maxspeed_post(id)
{
    if (g_carried[id] && is_user_alive(id))
    {
        new Float:speed = Float:get_entvar(id, var_maxspeed)
        set_entvar(id, var_maxspeed, speed * floatclamp(g_speed, 0.3, 1.0))
    }
    return HAM_IGNORED
}

public task_zone(taskid)
{
    new id = taskid - TASK_ZONE
    if (!g_carried[id] || !is_user_alive(id))
        return
    if (cs_get_user_mapzones(id) & CS_MAPZONE_HOSTAGE_RESCUE)
    {
        log_name("reached the rescue zone with hostage", id, g_carried[id])
        put_down(id, false)
    }
}

public on_damage_post(victim, inflictor, attacker, Float:damage, bits)
{
    if (!g_dropDamage || victim < 1 || victim > MaxClients || !g_carried[victim] || !is_user_alive(victim))
        return HC_CONTINUE
    if (damage <= 0.0 || attacker == victim || attacker < 1 || attacker > MaxClients)
        return HC_CONTINUE
    put_down(victim, false)
    return HC_CONTINUE
}

public on_killed_post(victim, killer, gib)
{
    if (g_carried[victim])
        put_down(victim, true)
    return HC_CONTINUE
}

public client_disconnected(id)
{
    if (g_carried[id])
        put_down(id, true)
    remove_task(TASK_ZONE + id)
}

public on_restart_pre()
{
    for (new i = 1; i <= MaxClients; i++)
    {
        if (g_carried[i])
            put_down(i, true)
    }
    return HC_CONTINUE
}

public on_rescued()
{
    g_rescues++
}

log_name(const what[], id, hostage)
{
    new name[MAX_NAME_LENGTH]
    get_user_name(id, name, charsmax(name))
    log_amx("%s %s %d", name, what, hostage)
}

public cmd_status(id, level, cid)
{
    if (!cmd_access(id, level, cid, 1))
        return PLUGIN_HANDLED
    console_print(id, "Hostage carry: speed %.2f, drop on damage %d, max hp %d, rescues this map %d",
        g_speed, g_dropDamage, g_maxHp, g_rescues)
    new name[MAX_NAME_LENGTH], shown
    for (new i = 1; i <= MaxClients; i++)
    {
        if (!g_carried[i])
            continue
        get_user_name(i, name, charsmax(name))
        console_print(id, "  %s carries hostage %d (max speed %.0f)", name, g_carried[i], Float:get_entvar(i, var_maxspeed))
        shown++
    }
    if (!shown)
        console_print(id, "  nobody carries a hostage")
    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.