/*
* Enhanced Hostage Behavior (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/>.
*
* Helps following hostages: after each hostage think (Ham_Think post on hostage_entity),
* every olds_hostai_retarget seconds a following hostage is checked:
* - closer than olds_hostai_followdist to its leader: it halts (no pushing the leader);
* - farther, and it has not moved for over 2 seconds: it is nudged. Line traces look ahead
* towards the leader; a blocked way makes it step to a free side, a small gap makes it hop
* across (olds_hostai_jumpgap 1), and a deep drop or lava/slime ahead makes it wait.
* Dead hostages are skipped. amx_hostai 0 stops all of this at once (vanilla behaviour).
*
* Cvars: olds_hostai_followdist 80 (40-400), olds_hostai_jumpgap 1, olds_hostai_retarget 0.5
* (0.1-5 s), olds_hostai_enabled 1 (set by amx_hostai)
* Admin: amx_hostai [0|1] (ADMIN_MAP) switches it; without an argument prints the hostages
*/
#include <amxmodx>
#include <amxmisc>
#include <fakemeta>
#include <hamsandwich>
#pragma semicolon 0
#define PLUGIN "Enhanced Hostage Behavior"
#define VERSION "1.0.0"
#define AUTHOR "daemon (olds.ro)"
#define MAX_ENTS 2048
#define STUCK_TIME 2.0
#define MOVED_EPS 8.0
#define LOOK_AHEAD 48.0
#define GAP_DEPTH 72.0
#define DROP_DEPTH 256.0
#define NUDGE_SPEED 220.0
#define HOP_SPEED 260.0
#define HOP_UP 270.0
new g_pEnabled
new g_enabled
new Float:g_followDist
new g_jumpGap
new Float:g_retarget
new Float:g_nextCheck[MAX_ENTS]
new Float:g_stuckFor[MAX_ENTS]
new Float:g_lastPos[MAX_ENTS][3]
new g_nudges[MAX_ENTS]
public plugin_init()
{
register_plugin(PLUGIN, VERSION, AUTHOR)
g_pEnabled = create_cvar("olds_hostai_enabled", "1", FCVAR_NONE,
"1 = hostage help on (amx_hostai switches it)", true, 0.0, true, 1.0)
bind_pcvar_num(g_pEnabled, g_enabled)
bind_pcvar_float(create_cvar("olds_hostai_followdist", "80", FCVAR_NONE,
"Distance at which a following hostage halts", true, 40.0, true, 400.0), g_followDist)
bind_pcvar_num(create_cvar("olds_hostai_jumpgap", "1", FCVAR_NONE,
"1 = hostages hop across small gaps", true, 0.0, true, 1.0), g_jumpGap)
bind_pcvar_float(create_cvar("olds_hostai_retarget", "0.5", FCVAR_NONE,
"Seconds between checks of each hostage", true, 0.1, true, 5.0), g_retarget)
RegisterHam(Ham_Think, "hostage_entity", "fw_hostage_think_post", true)
register_logevent("on_round_start", 2, "1=Round_Start")
register_concmd("amx_hostai", "cmd_hostai", ADMIN_MAP, "[0|1] - switch hostage help, or list hostages")
register_dictionary("olds_hostai.txt")
AutoExecConfig(true, "olds_hostai")
}
public on_round_start()
{
for (new i = 0; i < MAX_ENTS; i++)
{
g_stuckFor[i] = 0.0
g_nudges[i] = 0
g_nextCheck[i] = 0.0
}
}
bool:hostage_alive(ent)
{
if (!pev_valid(ent))
return false
new Float:health
pev(ent, pev_health, health)
return pev(ent, pev_deadflag) == DEAD_NO && health > 0.0
}
/* The player the hostage follows, or 0. */
leader_of(ent)
{
new leader = get_ent_data_entity(ent, "CBaseMonster", "m_hTargetEnt")
if (leader < 1 || leader > MaxClients || !is_user_alive(leader))
return 0
return leader
}
public fw_hostage_think_post(ent)
{
if (!g_enabled || ent < 1 || ent >= MAX_ENTS)
return HAM_IGNORED
if (!hostage_alive(ent))
{
g_stuckFor[ent] = 0.0
return HAM_IGNORED
}
new Float:now = get_gametime()
if (now < g_nextCheck[ent])
return HAM_IGNORED
new Float:step = floatclamp(g_retarget, 0.1, 5.0)
g_nextCheck[ent] = now + step
new leader = leader_of(ent)
new Float:pos[3]
pev(ent, pev_origin, pos)
if (!leader)
{
g_stuckFor[ent] = 0.0
remember(ent, pos)
return HAM_IGNORED
}
new Float:lpos[3], Float:dir[3]
pev(leader, pev_origin, lpos)
dir[0] = lpos[0] - pos[0]
dir[1] = lpos[1] - pos[1]
dir[2] = 0.0
new Float:dist = floatsqroot(dir[0] * dir[0] + dir[1] * dir[1])
if (dist <= g_followDist)
{
/* Close enough: stop walking into the leader. */
halt(ent)
g_stuckFor[ent] = 0.0
remember(ent, pos)
return HAM_IGNORED
}
if (get_distance_f(pos, g_lastPos[ent]) < MOVED_EPS)
g_stuckFor[ent] += step
else
g_stuckFor[ent] = 0.0
remember(ent, pos)
if (g_stuckFor[ent] < STUCK_TIME)
return HAM_IGNORED
dir[0] /= dist
dir[1] /= dist
nudge(ent, pos, dir)
g_stuckFor[ent] = 0.0
return HAM_IGNORED
}
remember(ent, const Float:pos[3])
{
g_lastPos[ent][0] = pos[0]
g_lastPos[ent][1] = pos[1]
g_lastPos[ent][2] = pos[2]
}
halt(ent)
{
new Float:vel[3]
pev(ent, pev_velocity, vel)
if (vel[0] != 0.0 || vel[1] != 0.0)
{
vel[0] = 0.0
vel[1] = 0.0
set_pev(ent, pev_velocity, vel)
}
}
/* 0 = free, 1 = blocked by something solid, 2 = gap that can be hopped, 3 = drop or hazard */
look(ent, const Float:pos[3], const Float:dir[3])
{
new Float:start[3], Float:end[3], Float:hit[3]
start[0] = pos[0]
start[1] = pos[1]
start[2] = pos[2] + 8.0
end[0] = start[0] + dir[0] * LOOK_AHEAD
end[1] = start[1] + dir[1] * LOOK_AHEAD
end[2] = start[2]
new Float:fraction
engfunc(EngFunc_TraceLine, start, end, DONT_IGNORE_MONSTERS, ent, 0)
get_tr2(0, TR_flFraction, fraction)
if (fraction < 1.0)
return 1
/* Is there ground ahead? */
start[0] = end[0]
start[1] = end[1]
end[2] = start[2] - DROP_DEPTH
engfunc(EngFunc_TraceLine, start, end, IGNORE_MONSTERS, ent, 0)
get_tr2(0, TR_flFraction, fraction)
get_tr2(0, TR_vecEndPos, hit)
new contents = engfunc(EngFunc_PointContents, hit)
if (contents == CONTENTS_LAVA || contents == CONTENTS_SLIME)
return 3
if (fraction >= 1.0)
return 3
if (start[2] - hit[2] > GAP_DEPTH)
return 2
return 0
}
nudge(ent, const Float:pos[3], const Float:dir[3])
{
new Float:vel[3], Float:side[3]
new result = look(ent, pos, dir)
g_nudges[ent]++
switch (result)
{
case 0:
{
vel[0] = dir[0] * NUDGE_SPEED
vel[1] = dir[1] * NUDGE_SPEED
}
case 1:
{
/* Blocked: step sideways, the free side first, alternating if both are free. */
side[0] = -dir[1]
side[1] = dir[0]
if (look(ent, pos, side) != 0 || (g_nudges[ent] & 1))
{
side[0] = -side[0]
side[1] = -side[1]
if (look(ent, pos, side) != 0)
{
side[0] = -side[0]
side[1] = -side[1]
}
}
vel[0] = (side[0] + dir[0] * 0.3) * NUDGE_SPEED
vel[1] = (side[1] + dir[1] * 0.3) * NUDGE_SPEED
}
case 2:
{
if (!g_jumpGap || !(pev(ent, pev_flags) & FL_ONGROUND))
return
vel[0] = dir[0] * HOP_SPEED
vel[1] = dir[1] * HOP_SPEED
vel[2] = HOP_UP
}
default:
{
halt(ent)
return
}
}
if (vel[2] == 0.0)
{
new Float:cur[3]
pev(ent, pev_velocity, cur)
vel[2] = cur[2]
}
set_pev(ent, pev_velocity, vel)
}
print_state(id, bool:on)
{
if (on)
console_print(id, "%L", id, "HOSTAI_ON")
else
console_print(id, "%L", id, "HOSTAI_OFF")
}
public cmd_hostai(id, level, cid)
{
if (!cmd_access(id, level, cid, 1))
return PLUGIN_HANDLED
if (read_argc() >= 2)
{
new arg[4]
read_argv(1, arg, charsmax(arg))
if (!isdigit(arg[0]) || (arg[0] != '0' && arg[0] != '1') || arg[1])
{
console_print(id, "%L", id, "HOSTAI_USAGE")
return PLUGIN_HANDLED
}
set_pcvar_num(g_pEnabled, arg[0] - '0')
print_state(id, arg[0] == '1')
new name[32]
if (id)
get_user_name(id, name, charsmax(name))
else
copy(name, charsmax(name), "server")
log_amx("%s set hostage help %c", name, arg[0])
return PLUGIN_HANDLED
}
print_state(id, g_enabled != 0)
new ent = -1, count, Float:pos[3]
while ((ent = engfunc(EngFunc_FindEntityByString, ent, "classname", "hostage_entity")) > 0)
{
pev(ent, pev_origin, pos)
new leader = hostage_alive(ent) ? leader_of(ent) : 0
console_print(id, "%L", id, "HOSTAI_ROW", ent, hostage_alive(ent) ? 1 : 0, leader,
pos[0], pos[1], pos[2], ent < MAX_ENTS ? g_nudges[ent] : 0)
count++
}
console_print(id, "%L", id, "HOSTAI_COUNT", count)
return PLUGIN_HANDLED
}