Skip to content
View in the app

A better way to browse. Learn more.

Learn more

shoreline-trials/addons/amxmodx/scripting/olds_shore.sma

olds_shore.sma
332 lines · 10.8 KB · Pawn (AMX Mod X)
Raw
/*
 * Shoreline Trials (olds.ro workshop card AMX-082)
 * Copyright (C) 2026 daemon (olds.ro)
 *
 * 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/>.
 *
 * Kreedz practice: a jump out of a named water launch to one of the map's shore zones is
 * measured. Every frame (FM_PlayerPostThink) the plugin reads pev_waterlevel and FL_ONGROUND:
 * leaving the water inside a launch volume starts an attempt at that point; the first landing
 * afterwards ends it. A landing inside a shore zone, at least olds_shore_minlen units (flat)
 * from the take-off, is shown to the jumper only (DHUD); the farthest one is their best
 * (say /shore). Landing elsewhere, falling back into the water, a teleport or restore (a move
 * faster than the speed allows), a death or a respawn drop the attempt without a result.
 * Routes: configs/olds_shore/<map>.ini; maps without one have no sessions.
 */
#include <amxmodx>
#include <amxmisc>
#include <fakemeta>
#include <reapi>

#define PLUGIN  "Shoreline Trials"
#define VERSION "1.0.0"
#define AUTHOR  "daemon (olds.ro)"

#define MAX_LAUNCH 8
#define MAX_SHORE  3
#define NAME_LEN   32

enum
{
    S_IDLE = 0,
    S_LEFT,         // just left the water, not yet in the air
    S_AIR           // in the air since leaving the water
}

new Float:g_minLen
new g_launches, g_launchName[MAX_LAUNCH][NAME_LEN], Float:g_launchMin[MAX_LAUNCH][3], Float:g_launchMax[MAX_LAUNCH][3]
new bool:g_shore[MAX_SHORE + 1], g_shoreName[MAX_SHORE + 1][NAME_LEN], Float:g_shoreMin[MAX_SHORE + 1][3], Float:g_shoreMax[MAX_SHORE + 1][3]
new bool:g_route
new g_file[160]
new g_fwThink

new g_state[MAX_PLAYERS + 1]
new g_from[MAX_PLAYERS + 1]                 // launch of the attempt
new Float:g_takeoff[MAX_PLAYERS + 1][3]
new Float:g_leftAt[MAX_PLAYERS + 1]
new g_prevWater[MAX_PLAYERS + 1]
new g_prevLaunch[MAX_PLAYERS + 1]           // launch volume the player was in last frame, -1 none
new Float:g_last[MAX_PLAYERS + 1][3]
new Float:g_lastTime[MAX_PLAYERS + 1]
new Float:g_best[MAX_PLAYERS + 1], g_bestZone[MAX_PLAYERS + 1], g_bestFrom[MAX_PLAYERS + 1]
new Float:g_lastLen[MAX_PLAYERS + 1], g_lastZone[MAX_PLAYERS + 1]
new g_results[MAX_PLAYERS + 1], g_dropped[MAX_PLAYERS + 1]

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

    bind_pcvar_float(create_cvar("olds_shore_minlen", "128", FCVAR_NONE, "Shortest landing that counts, in units", true, 0.0, true, 4096.0), g_minLen)
    AutoExecConfig(true, "olds_shore")

    register_clcmd("say /shore", "cmd_shore")
    register_clcmd("say_team /shore", "cmd_shore")
    register_concmd("amx_shore_status", "cmd_status", ADMIN_RCON, "- shows the routes and every player's attempts")
    RegisterHookChain(RG_CBasePlayer_Spawn, "on_spawn_post", true)
    RegisterHookChain(RG_CBasePlayer_Killed, "on_killed_post", true)

    new map[32]
    get_mapname(map, charsmax(map))
    get_configsdir(g_file, charsmax(g_file))
    format(g_file, charsmax(g_file), "%s/olds_shore/%s.ini", g_file, map)
}

public plugin_cfg()
{
    load_route()
    if (g_route)
        g_fwThink = register_forward(FM_PlayerPostThink, "on_post_think", true)
}

public plugin_end()
{
    if (g_fwThink)
        unregister_forward(FM_PlayerPostThink, g_fwThink, true)
}

/* ------------------------------------------------------------------ the route file */

/* launch "<name>" x1 y1 z1 x2 y2 z2
   shore <1-3> "<name>" x1 y1 z1 x2 y2 z2 */
load_route()
{
    new file = fopen(g_file, "rt")
    if (!file)
        return
    new line[256], arg[10][NAME_LEN], bad
    while (!feof(file))
    {
        fgets(file, line, charsmax(line))
        trim(line)
        if (!line[0] || line[0] == ';' || line[0] == '/')
            continue
        new n = parse(line, arg[0], NAME_LEN - 1, arg[1], NAME_LEN - 1, arg[2], NAME_LEN - 1, arg[3], NAME_LEN - 1,
            arg[4], NAME_LEN - 1, arg[5], NAME_LEN - 1, arg[6], NAME_LEN - 1, arg[7], NAME_LEN - 1, arg[8], NAME_LEN - 1)
        if (equali(arg[0], "launch") && n == 8 && g_launches < MAX_LAUNCH)
        {
            new i = g_launches++
            copy(g_launchName[i], NAME_LEN - 1, arg[1])
            read_box(arg, 2, g_launchMin[i], g_launchMax[i])
        }
        else if (equali(arg[0], "shore") && n == 9 && 1 <= str_to_num(arg[1]) <= MAX_SHORE)
        {
            new z = str_to_num(arg[1])
            g_shore[z] = true
            copy(g_shoreName[z], NAME_LEN - 1, arg[2])
            read_box(arg, 3, g_shoreMin[z], g_shoreMax[z])
        }
        else
            bad++
    }
    fclose(file)
    if (bad)
        log_amx("%s: %d bad line(s) skipped", g_file, bad)
    new shores
    for (new z = 1; z <= MAX_SHORE; z++)
        if (g_shore[z])
            shores++
    g_route = g_launches > 0 && shores > 0
    if (!g_route)
        log_amx("%s needs at least one launch and one shore: no sessions on this map", g_file)
}

read_box(const arg[][], first, Float:mins[3], Float:maxs[3])
{
    for (new k = 0; k < 3; k++)
    {
        new Float:a = str_to_float(arg[first + k]), Float:b = str_to_float(arg[first + 3 + k])
        mins[k] = floatmin(a, b)
        maxs[k] = floatmax(a, b)
    }
}

bool:inside(const Float:p[3], const Float:mins[3], const Float:maxs[3])
{
    return mins[0] <= p[0] <= maxs[0] && mins[1] <= p[1] <= maxs[1] && mins[2] <= p[2] <= maxs[2]
}

launch_at(const Float:p[3])
{
    for (new i = 0; i < g_launches; i++)
        if (inside(p, g_launchMin[i], g_launchMax[i]))
            return i
    return -1
}

shore_at(const Float:p[3])
{
    for (new z = 1; z <= MAX_SHORE; z++)
        if (g_shore[z] && inside(p, g_shoreMin[z], g_shoreMax[z]))
            return z
    return 0
}

/* ------------------------------------------------------------------ every frame */

drop(id)
{
    if (g_state[id] != S_IDLE)
        g_dropped[id]++
    g_state[id] = S_IDLE
}

public on_spawn_post(id)
{
    if (is_user_alive(id))
    {
        drop(id)
        g_lastTime[id] = 0.0
    }
}

public on_killed_post(victim, killer, gib)
{
    drop(victim)
}

public client_disconnected(id)
{
    g_state[id] = S_IDLE
    g_best[id] = 0.0
    g_bestZone[id] = 0
    g_lastLen[id] = 0.0
    g_lastZone[id] = 0
    g_results[id] = 0
    g_dropped[id] = 0
    g_lastTime[id] = 0.0
}

public on_post_think(id)
{
    if (!is_user_alive(id))
        return FMRES_IGNORED
    new Float:now = get_gametime()
    new Float:origin[3], Float:velocity[3]
    get_entvar(id, var_origin, origin)
    get_entvar(id, var_velocity, velocity)
    new water = get_entvar(id, var_waterlevel)
    new bool:onGround = (get_entvar(id, var_flags) & FL_ONGROUND) != 0

    /* Moved further than the speed allows since the last frame: a teleport or a restore. */
    if (g_state[id] != S_IDLE && g_lastTime[id] > 0.0)
    {
        new Float:allowed = (vector_length(velocity) + 200.0) * (now - g_lastTime[id]) + 32.0
        if (get_distance_f(origin, g_last[id]) > allowed)
            drop(id)
    }
    g_last[id] = origin
    g_lastTime[id] = now

    new here = launch_at(origin)
    if (g_state[id] == S_IDLE)
    {
        /* Out through the surface of a launch: the attempt starts here. */
        if (water == 0 && g_prevWater[id] > 0 && g_prevLaunch[id] >= 0)
        {
            g_state[id] = S_AIR
            if (onGround)
                g_state[id] = S_LEFT
            g_from[id] = g_prevLaunch[id]
            g_takeoff[id] = origin
            g_leftAt[id] = now
        }
    }
    else if (g_state[id] == S_LEFT)
    {
        if (water > 0)
            drop(id)
        else if (!onGround)
            g_state[id] = S_AIR
        else if (now - g_leftAt[id] > 0.3)
            drop(id)        // walked out of the water: not a jump
    }
    else if (g_state[id] == S_AIR)
    {
        if (water > 0)
            drop(id)        // back into the water
        else if (onGround)
            landed(id, origin)
    }
    g_prevWater[id] = water
    g_prevLaunch[id] = -1
    if (water > 0)
        g_prevLaunch[id] = here
    return FMRES_IGNORED
}

landed(id, const Float:spot[3])
{
    g_state[id] = S_IDLE
    new zone = shore_at(spot)
    new Float:dx = spot[0] - g_takeoff[id][0], Float:dy = spot[1] - g_takeoff[id][1]
    new Float:len = floatsqroot(dx * dx + dy * dy)
    if (!zone || len < g_minLen)
    {
        g_dropped[id]++
        return
    }
    g_results[id]++
    g_lastLen[id] = len
    g_lastZone[id] = zone
    new bool:best = len > g_best[id]
    if (best)
    {
        g_best[id] = len
        g_bestZone[id] = zone
        g_bestFrom[id] = g_from[id]
    }
    set_dhudmessage(80, 200, 255, -1.0, 0.70, 0, 0.0, 3.0, 0.1, 0.5)
    if (best)
        show_dhudmessage(id, "%L", id, "SHORE_RESULT_BEST", floatround(len), zone, g_shoreName[zone], g_launchName[g_from[id]])
    else
        show_dhudmessage(id, "%L", id, "SHORE_RESULT", floatround(len), zone, g_shoreName[zone], g_launchName[g_from[id]])
}

/* ------------------------------------------------------------------ commands */

public cmd_shore(id)
{
    if (!g_route)
        client_print(id, print_chat, "%L", id, "SHORE_NO_ROUTE")
    else if (g_best[id] <= 0.0)
        client_print(id, print_chat, "%L", id, "SHORE_NO_BEST")
    else
        client_print(id, print_chat, "%L", id, "SHORE_BEST", floatround(g_best[id]), g_bestZone[id],
            g_shoreName[g_bestZone[id]], g_launchName[g_bestFrom[id]])
    return PLUGIN_CONTINUE
}

public cmd_status(id, level, cid)
{
    if (id && !(get_user_flags(id) & level))
    {
        console_print(id, "You have no access to that command.")
        return PLUGIN_HANDLED
    }
    if (!g_route)
    {
        console_print(id, "[Shore] no water route is configured on this map (%s)", g_file)
        return PLUGIN_HANDLED
    }
    console_print(id, "[Shore] %d launch(es), min length %.0f", g_launches, g_minLen)
    for (new z = 1; z <= MAX_SHORE; z++)
        if (g_shore[z])
            console_print(id, "[Shore] shore %d %s: %.0f %.0f %.0f .. %.0f %.0f %.0f", z, g_shoreName[z],
                g_shoreMin[z][0], g_shoreMin[z][1], g_shoreMin[z][2], g_shoreMax[z][0], g_shoreMax[z][1], g_shoreMax[z][2])
    new name[MAX_NAME_LENGTH]
    for (new p = 1; p <= MaxClients; p++)
    {
        if (!is_user_connected(p))
            continue
        get_user_name(p, name, charsmax(name))
        console_print(id, "[Shore] #%d %s state %d, results %d, dropped %d, last %.0f (zone %d), best %.0f (zone %d)", p, name,
            g_state[p], g_results[p], g_dropped[p], g_lastLen[p], g_lastZone[p], g_best[p], g_bestZone[p])
    }
    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.