Skip to content
View in the app

A better way to browse. Learn more.

Learn more

captain-s-last-order/addons/amxmodx/scripting/olds_lastorder.sma

olds_lastorder.sma
266 lines · 7.7 KB · Pawn (AMX Mod X)
Raw
/*
 * Captain's Last Order (olds.ro)
 * 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. It is distributed WITHOUT ANY WARRANTY; see
 * <https://www.gnu.org/licenses/>.
 *
 * The human captain picks a short order with /lastorder. If the captain falls
 * (confirmed by the zombie core), the living humans get it once.
 */
#include <amxmodx>
#include <amxmisc>
#include <hamsandwich>

#define PLUGIN  "Captain's Last Order"
#define VERSION "1.0.0"
#define AUTHOR  "daemon (olds.ro)"

#define MAX_PRESETS 12

new g_cvCount, g_cvConfirm, g_cvTeam
new g_fwConfirm
new g_captain
new g_order
new g_sent

public plugin_natives()
{
    register_library("olds_lastorder")
    register_native("olds_lastorder_set_captain", "native_set_captain")
    register_native("olds_lastorder_get_captain", "native_get_captain")
    register_native("olds_lastorder_eliminated", "native_eliminated")
}

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

    g_cvCount = create_cvar("olds_lastorder_count", "6", FCVAR_NONE, "Presets offered in the /lastorder menu", true, 1.0, true, float(MAX_PRESETS))
    g_cvConfirm = create_cvar("olds_lastorder_confirm", "1", FCVAR_NONE, "1 = a captain's death needs the core's confirmation, 0 = every death counts", true, 0.0, true, 1.0)
    g_cvTeam = create_cvar("olds_lastorder_team", "2", FCVAR_NONE, "Team of the humans: 1 = T, 2 = CT", true, 1.0, true, 2.0)
    AutoExecConfig(true, "olds_lastorder")

    register_clcmd("say /lastorder", "cmd_lastorder")
    register_clcmd("say_team /lastorder", "cmd_lastorder")
    register_concmd("amx_lastorder", "cmd_admin", ADMIN_KICK, "[captain <player|none>] - shows the captain and the pending order, or sets the captain")
    register_event("HLTV", "on_new_round", "a", "1=0", "2=0")
    RegisterHam(Ham_Killed, "player", "on_killed_post", 1)

    g_fwConfirm = CreateMultiForward("olds_lastorder_confirm", ET_STOP, FP_CELL, FP_CELL)
}

public plugin_end()
{
    DestroyForward(g_fwConfirm)
}

public client_disconnected(id)
{
    if (id == g_captain)
    {
        g_captain = 0
        g_order = 0
    }
}

public on_new_round()
{
    // A restart or a new round: nothing carries over.
    g_order = 0
    g_sent = 0
}

set_captain(id)
{
    if (id != g_captain)
        g_order = 0
    g_captain = id
    g_sent = 0
}

/* ---------- the menu ---------- */

public cmd_lastorder(id)
{
    if (!is_user_connected(id))
        return PLUGIN_HANDLED
    if (id != g_captain || !is_user_alive(id) || get_user_team(id) != get_pcvar_num(g_cvTeam))
    {
        client_print_color(id, print_team_default, "%L", id, "LO_CAPTAIN_ONLY")
        return PLUGIN_HANDLED
    }
    new title[96], item[96], key[16], info[4]
    formatex(title, charsmax(title), "%L", id, "LO_MENU_TITLE")
    new menu = menu_create(title, "menu_pick")
    new count = clamp(get_pcvar_num(g_cvCount), 1, MAX_PRESETS)
    for (new i = 1; i <= count; i++)
    {
        formatex(key, charsmax(key), "LO_ORDER_%d", i)
        formatex(item, charsmax(item), "%L", id, key)
        if (i == g_order)
            add(item, charsmax(item), " \y*")
        num_to_str(i, info, charsmax(info))
        menu_additem(menu, item, info)
    }
    menu_display(id, menu)
    return PLUGIN_HANDLED
}

public menu_pick(id, menu, item)
{
    if (item == MENU_EXIT || item < 0)
    {
        menu_destroy(menu)
        return PLUGIN_HANDLED
    }
    new info[4], access, callback
    menu_item_getinfo(menu, item, access, info, charsmax(info), _, _, callback)
    menu_destroy(menu)

    // The menu may have stayed open while the role or the cvar changed.
    new choice = str_to_num(info)
    if (id != g_captain || !is_user_alive(id) || choice < 1 || choice > clamp(get_pcvar_num(g_cvCount), 1, MAX_PRESETS))
    {
        client_print_color(id, print_team_default, "%L", id, "LO_CAPTAIN_ONLY")
        return PLUGIN_HANDLED
    }
    new bool:replaced = g_order != 0 && g_order != choice
    g_order = choice
    g_sent = 0
    new key[16], text[96]
    formatex(key, charsmax(key), "LO_ORDER_%d", choice)
    formatex(text, charsmax(text), "%L", id, key)
    if (replaced)
        client_print_color(id, print_team_default, "%L", id, "LO_REPLACED", text)
    else
        client_print_color(id, print_team_default, "%L", id, "LO_STORED", text)
    return PLUGIN_HANDLED
}

/* ---------- delivery ---------- */

public on_killed_post(victim, killer, shouldgib)
{
    if (victim != g_captain || !g_order || g_sent)
        return HAM_IGNORED
    if (get_pcvar_num(g_cvConfirm) == 1)
    {
        new result = PLUGIN_CONTINUE
        ExecuteForward(g_fwConfirm, result, victim, killer)
        if (result < PLUGIN_HANDLED)
        {
            log_amx("Captain died, the core did not confirm the elimination: order kept")
            return HAM_IGNORED
        }
    }
    deliver()
    return HAM_IGNORED
}

deliver()
{
    if (!g_captain || !g_order || g_sent)
        return 0
    g_sent = 1
    new name[MAX_NAME_LENGTH], key[16], text[96], count
    get_user_name(g_captain, name, charsmax(name))
    formatex(key, charsmax(key), "LO_ORDER_%d", g_order)
    new team = get_pcvar_num(g_cvTeam)
    for (new id = 1; id <= MaxClients; id++)
    {
        if (id == g_captain || !is_user_alive(id) || get_user_team(id) != team)
            continue
        formatex(text, charsmax(text), "%L", id, key)
        client_print_color(id, print_team_default, "%L", id, "LO_DELIVER", name, text)
        count++
    }
    formatex(text, charsmax(text), "%L", LANG_SERVER, key)
    log_amx("Last order ^"%s^" from %s delivered to %d humans", text, name, count)
    g_order = 0
    return 1
}

/* ---------- admin ---------- */

public cmd_admin(id, level, cid)
{
    if (!cmd_access(id, level, cid, 1))
        return PLUGIN_HANDLED
    new action[16]
    read_argv(1, action, charsmax(action))
    if (equali(action, "captain"))
    {
        new arg[32]
        read_argv(2, arg, charsmax(arg))
        if (equali(arg, "none"))
            set_captain(0)
        else
        {
            new target = cmd_target(id, arg, CMDTARGET_ALLOW_SELF | CMDTARGET_ONLY_ALIVE)
            if (!target)
                return PLUGIN_HANDLED
            set_captain(target)
        }
    }
    else if (action[0])
    {
        console_print(id, "%L", id, "LO_USAGE")
        return PLUGIN_HANDLED
    }
    new name[MAX_NAME_LENGTH], text[96]
    if (g_captain)
        get_user_name(g_captain, name, charsmax(name))
    else
        copy(name, charsmax(name), "-")
    if (g_order)
    {
        new key[16]
        formatex(key, charsmax(key), "LO_ORDER_%d", g_order)
        formatex(text, charsmax(text), "%L", id, key)
    }
    else
        copy(text, charsmax(text), "-")
    console_print(id, "%L", id, "LO_STATUS", name, text, g_sent)
    return PLUGIN_HANDLED
}

/* ---------- natives ---------- */

public native_set_captain(plugin, params)
{
    if (params < 1)
        return 0
    new id = get_param(1)
    if (id != 0 && (id < 1 || id > MaxClients || !is_user_connected(id)))
    {
        log_error(AMX_ERR_NATIVE, "olds_lastorder_set_captain: invalid player %d", id)
        return 0
    }
    set_captain(id)
    return 1
}

public native_get_captain(plugin, params)
{
    return g_captain
}

public native_eliminated(plugin, params)
{
    if (params < 1)
        return 0
    new id = get_param(1)
    if (id < 1 || id > MaxClients)
    {
        log_error(AMX_ERR_NATIVE, "olds_lastorder_eliminated: invalid player %d", id)
        return 0
    }
    if (id != g_captain)
        return 0
    return deliver()
}

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.