Skip to content
View in the app

A better way to browse. Learn more.

Learn more

courier-chain/addons/amxmodx/scripting/olds_courier.sma

olds_courier.sma
451 lines · 13.1 KB · Pawn (AMX Mod X)
Raw
/*
 * Courier Chain (olds.ro) - Zombie Plague 5.0 add-on. Humans carry a virtual
 * cure parcel through the map's named checkpoints in order; at the last one
 * the map's dispenser entity is triggered once and one zombie is cured.
 *
 * 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. You should have received a copy of the GNU General Public
 * License along with this program. If not, see <https://www.gnu.org/licenses/>.
 */

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

#pragma reqlib "zp50_core"
native zp_core_is_zombie(id)
native zp_core_cure(id, attacker = 0)

#define PLUGIN  "Courier Chain"
#define VERSION "1.0.0"
#define AUTHOR  "daemon (olds.ro)"

#define MAX_POINTS 16
#define POINT_NAME 32
#define TASK_POLL 7200
#define POLL 0.2
#define PICKUP_RANGE 64.0
#define USE_TOGGLE_VALUE 3

new g_pointName[MAX_POINTS][POINT_NAME]
new Float:g_min[MAX_POINTS][3]
new Float:g_max[MAX_POINTS][3]
new g_points
new g_dispenser[64]

new g_carrier                    // 0 = nobody carries the parcel
new g_next                       // index of the checkpoint the parcel needs next
new bool:g_dropped
new Float:g_dropAt[3]
new bool:g_done                  // the dispenser was unlocked this round
new Float:g_lastOrigin[3]        // carrier position at the last poll
new g_lastZone = -1              // carrier's zone at the last poll, for hints

new g_cvarDrop

public plugin_init()
{
    register_plugin(PLUGIN, VERSION, AUTHOR)

    g_cvarDrop = create_cvar("olds_courier_drop", "1", FCVAR_NONE,
        "1 = the parcel drops where its carrier dies, 0 = it goes back to the first checkpoint", true, 0.0, true, 1.0)
    AutoExecConfig(true, "olds_courier")

    register_clcmd("say /parcel", "cmd_parcel")
    register_clcmd("say_team /parcel", "cmd_parcel")
    register_concmd("olds_courier_status", "cmd_status", ADMIN_CFG, "- checkpoints and the parcel")
    register_logevent("on_round_end", 2, "1=Round_End")
    register_event("HLTV", "on_new_round", "a", "1=0", "2=0")
}

public plugin_cfg()
{
    load_route()
    reset_parcel()
    if (g_points)
        set_task(POLL, "task_poll", TASK_POLL, _, _, "b")
}

/* ---------- route ---------- */

load_route()
{
    new path[192], map[64]
    get_configsdir(path, charsmax(path))
    get_mapname(map, charsmax(map))
    format(path, charsmax(path), "%s/olds_courier/%s.ini", path, map)
    new file = fopen(path, "rt")
    if (!file)
    {
        server_print("[Courier] No route for %s (%s): the plugin is idle on this map.", map, path)
        return
    }

    new line[256], name[POINT_NAME], part[6][16], lineNo
    while (!feof(file) && fgets(file, line, charsmax(line)))
    {
        lineNo++
        trim(line)
        if (!line[0] || line[0] == ';' || line[0] == '#')
            continue
        new parts = parse(line, name, charsmax(name), part[0], charsmax(part[]), part[1], charsmax(part[]),
            part[2], charsmax(part[]), part[3], charsmax(part[]), part[4], charsmax(part[]), part[5], charsmax(part[]))
        if (parts == 2 && equali(name, "dispenser"))
        {
            copy(g_dispenser, charsmax(g_dispenser), part[0])
            continue
        }
        if (parts != 7 || !name[0] || !all_numbers(part))
        {
            log_amx("[Courier] %s line %d skipped: expected ^"name^" x1 y1 z1 x2 y2 z2 or dispenser <targetname>", path, lineNo)
            continue
        }
        if (g_points >= MAX_POINTS)
        {
            log_amx("[Courier] %s: more than %d checkpoints, the rest are skipped", path, MAX_POINTS)
            break
        }
        for (new axis = 0; axis < 3; axis++)
        {
            new Float:a = str_to_float(part[axis]), Float:b = str_to_float(part[axis + 3])
            g_min[g_points][axis] = floatmin(a, b)
            g_max[g_points][axis] = floatmax(a, b)
        }
        copy(g_pointName[g_points], charsmax(g_pointName[]), name)
        g_points++
    }
    fclose(file)

    if (g_points < 2)
    {
        log_amx("[Courier] %s needs at least two checkpoints; the plugin is idle.", path)
        g_points = 0
        return
    }
    if (!g_dispenser[0])
        log_amx("[Courier] %s has no ^"dispenser <targetname>^" line: the last checkpoint only cures.", path)
    server_print("[Courier] %d checkpoints loaded for %s; dispenser ^"%s^".", g_points, map, g_dispenser)
}

bool:all_numbers(const part[][])
{
    for (new i = 0; i < 6; i++)
    {
        new j, digits, dots
        if (part[i][0] == '-' || part[i][0] == '+')
            j++
        for (; part[i][j]; j++)
        {
            if (part[i][j] == '.')
            {
                if (++dots > 1)
                    return false
            }
            else if ('0' <= part[i][j] <= '9')
                digits++
            else
                return false
        }
        if (!digits)
            return false
    }
    return true
}

zone_at(const Float:origin[3])
{
    for (new p = 0; p < g_points; p++)
    {
        if (g_min[p][0] <= origin[0] <= g_max[p][0]
            && g_min[p][1] <= origin[1] <= g_max[p][1]
            && g_min[p][2] <= origin[2] <= g_max[p][2])
            return p
    }
    return -1
}

/* ---------- parcel ---------- */

reset_parcel()
{
    g_carrier = 0
    g_next = 0
    g_dropped = false
    g_done = false
    g_lastZone = -1
}

public on_round_end()
{
    if (g_points && (g_carrier || g_dropped || g_next))
        tell_humans("The round is over: the parcel delivery is cancelled.")
    reset_parcel()
}

public on_new_round()
{
    reset_parcel()
}

bool:is_human(id)
{
    return is_user_alive(id) && !zp_core_is_zombie(id)
}

give_parcel(id)
{
    g_carrier = id
    g_dropped = false
    g_lastZone = -1
    pev(id, pev_origin, g_lastOrigin)
}

public cmd_parcel(id)
{
    if (!g_points)
    {
        client_print(id, print_chat, "[Courier] This map has no courier route.")
        return PLUGIN_HANDLED
    }
    if (g_done)
    {
        client_print(id, print_chat, "[Courier] The parcel was already delivered this round.")
        return PLUGIN_HANDLED
    }
    if (!is_human(id))
    {
        client_print(id, print_chat, "[Courier] Only a living human can carry the parcel.")
        return PLUGIN_HANDLED
    }
    if (g_carrier)
    {
        new name[32]
        get_user_name(g_carrier, name, charsmax(name))
        client_print(id, print_chat, "[Courier] %s already carries the parcel.", name)
        return PLUGIN_HANDLED
    }
    new Float:origin[3]
    pev(id, pev_origin, origin)
    if (g_dropped)
    {
        if (get_distance_f(origin, g_dropAt) > PICKUP_RANGE * 1.5)
        {
            client_print(id, print_chat, "[Courier] The parcel lies where its carrier fell; walk over it to pick it up.")
            return PLUGIN_HANDLED
        }
        pick_up(id)
        return PLUGIN_HANDLED
    }
    if (zone_at(origin) != 0)
    {
        client_print(id, print_chat, "[Courier] The parcel waits at %s. Go there and type /parcel.", g_pointName[0])
        return PLUGIN_HANDLED
    }
    // Picking it up at the first checkpoint counts as reaching it.
    give_parcel(id)
    g_next = 1
    g_lastZone = 0
    new name[32], text[160]
    get_user_name(id, name, charsmax(name))
    formatex(text, charsmax(text), "%s picked up the cure parcel at %s. Next: %s.", name, g_pointName[0], g_pointName[1])
    tell_humans(text)
    return PLUGIN_HANDLED
}

pick_up(id)
{
    give_parcel(id)
    new name[32], text[160]
    get_user_name(id, name, charsmax(name))
    formatex(text, charsmax(text), "%s picked up the dropped parcel. Next: %s.", name, g_pointName[g_next])
    tell_humans(text)
}

drop_parcel()
{
    new name[32], text[160]
    if (is_user_connected(g_carrier))
        get_user_name(g_carrier, name, charsmax(name))
    else
        copy(name, charsmax(name), "The carrier")
    g_carrier = 0
    if (get_pcvar_num(g_cvarDrop))
    {
        g_dropped = true
        g_dropAt = g_lastOrigin
        formatex(text, charsmax(text), "%s lost the parcel. Any human can pick it up where it fell. Next: %s.", name, g_pointName[g_next])
    }
    else
    {
        g_dropped = false
        g_next = 0
        formatex(text, charsmax(text), "%s lost the parcel. It is back at %s.", name, g_pointName[0])
    }
    tell_humans(text)
}

public task_poll()
{
    if (g_done)
        return

    if (g_carrier)
    {
        if (!is_user_connected(g_carrier) || !is_human(g_carrier))
        {
            drop_parcel()
            return
        }
        new Float:origin[3]
        pev(g_carrier, pev_origin, origin)
        g_lastOrigin = origin
        new zone = zone_at(origin)
        if (zone == g_lastZone)
            return                                  // still in the same box: one touch
        g_lastZone = zone
        if (zone < 0 || zone < g_next)
            return                                  // outside, or a checkpoint already passed
        if (zone == g_next)
            reach(zone)
        else
            client_print(g_carrier, print_center, "Wrong checkpoint: take the parcel to %s first.", g_pointName[g_next])
        return
    }

    if (g_dropped)
    {
        new players[MAX_PLAYERS], count, Float:origin[3]
        get_players(players, count, "a")
        for (new i = 0; i < count; i++)
        {
            if (zp_core_is_zombie(players[i]))
                continue
            pev(players[i], pev_origin, origin)
            if (get_distance_f(origin, g_dropAt) <= PICKUP_RANGE)
            {
                pick_up(players[i])
                return
            }
        }
    }
}

reach(zone)
{
    g_next = zone + 1
    new name[32], text[160]
    get_user_name(g_carrier, name, charsmax(name))
    if (g_next < g_points)
    {
        formatex(text, charsmax(text), "%s brought the parcel to %s (%d/%d). Next: %s.", name, g_pointName[zone], g_next, g_points, g_pointName[g_next])
        tell_humans(text)
        return
    }
    deliver(name)
}

deliver(const name[])
{
    new carrier = g_carrier
    g_done = true
    g_carrier = 0

    new fired
    if (g_dispenser[0])
    {
        new ent = -1
        while ((ent = engfunc(EngFunc_FindEntityByString, ent, "targetname", g_dispenser)) > 0)
        {
            ExecuteHamB(Ham_Use, ent, carrier, carrier, USE_TOGGLE_VALUE, 0.0)
            fired++
        }
        if (!fired)
            log_amx("[Courier] No entity named ^"%s^" on this map.", g_dispenser)
    }

    new cured = nearest_zombie(carrier)
    if (cured)
        zp_core_cure(cured, carrier)

    new text[192], who[32]
    if (cured)
    {
        get_user_name(cured, who, charsmax(who))
        formatex(text, charsmax(text), "%s delivered the parcel: the dispenser opens once and %s is cured.", name, who)
    }
    else
    {
        copy(who, charsmax(who), "nobody")
        formatex(text, charsmax(text), "%s delivered the parcel: the dispenser opens once (no zombie to cure).", name)
    }
    tell_humans(text)
    log_amx("[Courier] %s delivered the parcel; %d dispenser entity(ies) triggered; cured: %s", name, fired, who)
}

nearest_zombie(from)
{
    new players[MAX_PLAYERS], count, best, Float:bestDistance = 999999.0, Float:here[3], Float:there[3]
    pev(from, pev_origin, here)
    get_players(players, count, "a")
    for (new i = 0; i < count; i++)
    {
        if (!zp_core_is_zombie(players[i]))
            continue
        pev(players[i], pev_origin, there)
        new Float:distance = get_distance_f(here, there)
        if (distance < bestDistance)
        {
            bestDistance = distance
            best = players[i]
        }
    }
    return best
}

tell_humans(const text[])
{
    new players[MAX_PLAYERS], count
    get_players(players, count, "ch")
    set_dhudmessage(120, 255, 120, -1.0, 0.18, 0, 0.0, 3.0, 0.1, 0.5)
    for (new i = 0; i < count; i++)
    {
        if (is_user_alive(players[i]) && zp_core_is_zombie(players[i]))
            continue
        show_dhudmessage(players[i], "%s", text)
    }
    server_print("[Courier] %s", text)
}

public client_disconnected(id)
{
    if (id == g_carrier)
        drop_parcel()
}

public cmd_status(id, level, cid)
{
    if (!cmd_access(id, level, cid, 1))
        return PLUGIN_HANDLED
    console_print(id, "[Courier] %d checkpoint(s), dispenser ^"%s^", olds_courier_drop %d", g_points, g_dispenser, get_pcvar_num(g_cvarDrop))
    for (new p = 0; p < g_points; p++)
        console_print(id, "  %d. %s", p + 1, g_pointName[p])
    new carrier[32]
    if (g_carrier)
        get_user_name(g_carrier, carrier, charsmax(carrier))
    else if (g_dropped)
        formatex(carrier, charsmax(carrier), "dropped at %.0f %.0f %.0f", g_dropAt[0], g_dropAt[1], g_dropAt[2])
    else
        copy(carrier, charsmax(carrier), "nobody")
    console_print(id, "  Parcel: %s; next checkpoint %d; delivered %d", carrier, g_next + 1, g_done)
    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.