/*
* Aim Teleport (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/>.
*
* "+aimtele" (a bind) or "say /tp" moves the player to the spot they aim at: a line trace
* along the view (EngFunc_TraceLine, at most olds_atp_maxdist units) finds the surface, the
* spot steps back 40 units along its normal, and the player's hull must fit there (a hull
* trace; a little higher or lower is tried too). Sky, nothing in range, or no room: refused
* and the player stays. A spark and a zap mark the arrival.
*
* Only on maps matching olds_atp_maps (patterns with *, separated by ;, empty = every map),
* only for players with the olds_atp_flag admin flags (empty = everyone: practice mode), once
* per olds_atp_cooldown seconds, and not in the first olds_atp_roundstart seconds of a round.
*
* Cvars: olds_atp_flag "t", olds_atp_cooldown 5, olds_atp_maxdist 3000,
* olds_atp_maps "fy_*;aim_*", olds_atp_roundstart 0
*/
#include <amxmodx>
#include <fakemeta>
#pragma semicolon 0
#define PLUGIN "Aim Teleport"
#define VERSION "1.0.0"
#define AUTHOR "daemon (olds.ro)"
#define STEP_BACK 40.0
new g_flagText[32]
new Float:g_cooldown
new Float:g_maxDist
new g_mapsText[192]
new Float:g_roundStartBlock
new g_pMaps
new bool:g_mapAllowed
new Float:g_roundStart
new Float:g_next[MAX_PLAYERS + 1]
new g_spark
new g_sparkSprite[48]
new const ZAP_SOUND[] = "weapons/electro4.wav"
public plugin_precache()
{
/* The stock spark; a server without it uses the game's own explosion sprite. */
if (file_exists("sprites/xspark2.spr", true))
copy(g_sparkSprite, charsmax(g_sparkSprite), "sprites/xspark2.spr")
else
copy(g_sparkSprite, charsmax(g_sparkSprite), "sprites/zerogxplode.spr")
g_spark = precache_model(g_sparkSprite)
precache_sound(ZAP_SOUND)
}
public plugin_init()
{
register_plugin(PLUGIN, VERSION, AUTHOR)
bind_pcvar_string(create_cvar("olds_atp_flag", "t", FCVAR_NONE,
"Admin flags needed to teleport (empty = everyone, practice mode)"), g_flagText, charsmax(g_flagText))
bind_pcvar_float(create_cvar("olds_atp_cooldown", "5", FCVAR_NONE,
"Seconds between teleports", true, 0.0, true, 120.0), g_cooldown)
bind_pcvar_float(create_cvar("olds_atp_maxdist", "3000", FCVAR_NONE,
"Farthest teleport", true, 64.0, true, 8192.0), g_maxDist)
g_pMaps = create_cvar("olds_atp_maps", "fy_*;aim_*", FCVAR_NONE,
"Maps where teleport works: patterns with *, separated by ; (empty = all)")
bind_pcvar_string(g_pMaps, g_mapsText, charsmax(g_mapsText))
hook_cvar_change(g_pMaps, "on_maps_change")
bind_pcvar_float(create_cvar("olds_atp_roundstart", "0", FCVAR_NONE,
"Seconds after round start without teleports", true, 0.0, true, 60.0), g_roundStartBlock)
register_clcmd("+aimtele", "cmd_teleport")
register_clcmd("-aimtele", "cmd_release")
register_clcmd("say /tp", "cmd_teleport")
register_clcmd("say_team /tp", "cmd_teleport")
register_logevent("on_round_start", 2, "1=Round_Start")
register_dictionary("olds_aimtele.txt")
AutoExecConfig(true, "olds_aimtele")
}
public plugin_cfg()
{
g_mapAllowed = map_allowed(g_mapsText)
}
public on_maps_change(pcvar, const oldValue[], const newValue[])
{
g_mapAllowed = map_allowed(newValue)
}
public on_round_start()
{
g_roundStart = get_gametime()
}
public client_putinserver(id)
{
g_next[id] = 0.0
}
/* "fy_*", "*_night", "de_dust2" or "*". */
bool:pattern_match(const name[], const pattern[])
{
new star = contain(pattern, "*")
if (star < 0)
return bool:equali(name, pattern)
new nameLen = strlen(name), patLen = strlen(pattern)
new tailLen = patLen - star - 1
if (nameLen < star + tailLen)
return false
if (star > 0 && !equali(name, pattern, star))
return false
if (tailLen > 0 && !equali(name[nameLen - tailLen], pattern[star + 1]))
return false
return true
}
bool:map_allowed(const list[])
{
new map[64], rest[192], piece[48], tail[192]
get_mapname(map, charsmax(map))
copy(rest, charsmax(rest), list)
trim(rest)
if (!rest[0])
return true
while (rest[0])
{
strtok2(rest, piece, charsmax(piece), tail, charsmax(tail), ';', TRIM_FULL)
if (piece[0] && pattern_match(map, piece))
return true
copy(rest, charsmax(rest), tail)
}
return false
}
bool:hull_vacant(const Float:spot[3], hull, id)
{
engfunc(EngFunc_TraceHull, spot, spot, DONT_IGNORE_MONSTERS, hull, id, 0)
return !get_tr2(0, TR_StartSolid) && !get_tr2(0, TR_AllSolid)
}
public cmd_release(id)
{
return PLUGIN_HANDLED
}
public cmd_teleport(id)
{
if (!is_user_alive(id))
return PLUGIN_HANDLED
if (!g_mapAllowed)
{
client_print_color(id, print_team_default, "%L", id, "ATP_MAP")
return PLUGIN_HANDLED
}
if (g_flagText[0] && (get_user_flags(id) & read_flags(g_flagText)) != read_flags(g_flagText))
{
client_print_color(id, print_team_default, "%L", id, "ATP_ACCESS")
return PLUGIN_HANDLED
}
new Float:now = get_gametime()
if (g_roundStartBlock > 0.0 && now < g_roundStart + g_roundStartBlock)
{
client_print_color(id, print_team_default, "%L", id, "ATP_ROUNDSTART",
floatround(g_roundStart + g_roundStartBlock - now, floatround_ceil))
return PLUGIN_HANDLED
}
if (now < g_next[id])
{
client_print_color(id, print_team_default, "%L", id, "ATP_WAIT", floatround(g_next[id] - now, floatround_ceil))
return PLUGIN_HANDLED
}
/* Where the player aims (fm_get_aim_origin), limited to olds_atp_maxdist. */
new Float:eye[3], Float:ofs[3], Float:angles[3], Float:dir[3], Float:end[3], Float:hit[3], Float:normal[3]
pev(id, pev_origin, eye)
pev(id, pev_view_ofs, ofs)
eye[2] += ofs[2]
pev(id, pev_v_angle, angles)
engfunc(EngFunc_MakeVectors, angles)
global_get(glb_v_forward, dir)
new Float:range = floatclamp(g_maxDist, 64.0, 8192.0)
for (new k = 0; k < 3; k++)
end[k] = eye[k] + dir[k] * range
new Float:fraction
engfunc(EngFunc_TraceLine, eye, end, DONT_IGNORE_MONSTERS, id, 0)
get_tr2(0, TR_flFraction, fraction)
get_tr2(0, TR_vecEndPos, hit)
get_tr2(0, TR_vecPlaneNormal, normal)
new Float:inside[3]
for (new k = 0; k < 3; k++)
inside[k] = hit[k] + dir[k] * 2.0
if (fraction >= 1.0 || engfunc(EngFunc_PointContents, inside) == CONTENTS_SKY)
{
client_print_color(id, print_team_default, "%L", id, "ATP_NO_TARGET")
return PLUGIN_HANDLED
}
/* Step back from the surface, then make sure the player fits. */
new Float:spot[3], Float:tryAt[3]
for (new k = 0; k < 3; k++)
spot[k] = hit[k] + normal[k] * STEP_BACK
new hull = (pev(id, pev_flags) & FL_DUCKING) ? HULL_HEAD : HULL_HUMAN
new const Float:lifts[] = { 0.0, 18.0, 36.0, -18.0 }
new bool:found
for (new i = 0; i < sizeof(lifts) && !found; i++)
{
tryAt[0] = spot[0]
tryAt[1] = spot[1]
tryAt[2] = spot[2] + lifts[i]
found = hull_vacant(tryAt, hull, id)
}
if (!found)
{
client_print_color(id, print_team_default, "%L", id, "ATP_NO_ROOM")
return PLUGIN_HANDLED
}
new Float:from[3]
pev(id, pev_origin, from)
emit_sound(id, CHAN_ITEM, ZAP_SOUND, 0.8, ATTN_NORM, 0, PITCH_NORM)
engfunc(EngFunc_SetOrigin, id, tryAt)
set_pev(id, pev_velocity, Float:{0.0, 0.0, 0.0})
g_next[id] = now + floatclamp(g_cooldown, 0.0, 120.0)
engfunc(EngFunc_MessageBegin, MSG_PVS, SVC_TEMPENTITY, tryAt, 0)
write_byte(TE_SPRITE)
engfunc(EngFunc_WriteCoord, tryAt[0])
engfunc(EngFunc_WriteCoord, tryAt[1])
engfunc(EngFunc_WriteCoord, tryAt[2])
write_short(g_spark)
write_byte(8)
write_byte(200)
message_end()
emit_sound(id, CHAN_STATIC, ZAP_SOUND, 0.8, ATTN_NORM, 0, PITCH_HIGH)
client_print_color(id, print_team_default, "%L", id, "ATP_DONE", floatround(get_distance_f(from, tryAt)))
return PLUGIN_HANDLED
}