Skip to content
View in the app

A better way to browse. Learn more.

Learn more

nomination-reserve/addons/amxmodx/scripting/olds_reserve.sma

olds_reserve.sma
375 lines · 10 KB · Pawn (AMX Mod X)
Raw
/*
 * Nomination Reserve (olds.ro workshop card AMX-031)
 *
 * Map voting: after nominating a map, a player can name a backup with
 * /backup <map>. If the nominated map drops out before the vote opens (the
 * map file is gone, it was taken out of the map cycle, or the map chooser
 * blocks it), the backup goes into the vote in its place.
 *
 *   say /backup <map>   attach a backup to your latest nomination
 *   say /backup         list your nominations and backups
 *
 * Needs: AMX Mod X 1.10 and Map Manager Modular by Mistrick
 * (github.com/Mistrick/MapManagerModular: map_manager_core and
 * map_manager_nomination). Load this plugin after map_manager_nomination in
 * plugins.ini so the nominated maps are in the vote list before the backups
 * are resolved.
 *   - mapm_can_be_nominated(id, map): records each player's nominations;
 *   - mapm_prepare_votelist: a nomination that is not in the vote and is no
 *     longer valid (is_map_valid) or no longer in the server's map cycle is
 *     replaced by its backup, pushed with mapm_push_map_to_votelist. A map the
 *     player simply took back is still valid and listed, so its backup is not
 *     used;
 *   - mapm_vote_started / mapm_vote_canceled: the vote has used the
 *     nominations (the chooser empties its own list), so the records are
 *     released either way.
 *
 * A backup must be an installed map (is_map_valid), in the map cycle file
 * (the mapcyclefile cvar, read again at every check), not the current map, and
 * not already nominated or named as a backup by anyone.
 *
 * Cvar: olds_reserve_max 1 (1-5), backups per player.
 * Command: olds_reserve_status (ADMIN_CFG).
 *
 * Copyright (C) 2026 daemon (olds.ro)
 * SPDX-License-Identifier: GPL-3.0-or-later
 */
#include <amxmodx>
#include <amxmisc>

#define PLUGIN  "Nomination Reserve"
#define VERSION "1.0.0"
#define AUTHOR  "olds.ro"

/* Map Manager Modular core API (map_manager.inc), declared here so it compiles without it. */
#pragma reqlib "map_manager_core"
native mapm_push_map_to_votelist(map[], type = 1, ignore_check = 0)
native mapm_get_count_maps_in_vote()
native mapm_get_voteitem_info(item, map[], len)
native bool:is_vote_started()

const PUSH_BY_NATIVE = 1
const PUSH_SUCCESS = 1
const NOMINATION_ALLOWED = 0
const MAP_LENGTH = 32
const MAX_RECORDS = 64
const MAX_BACKUPS = 5

new g_player[MAX_RECORDS]                   // 0 = free slot
new g_primary[MAX_RECORDS][MAP_LENGTH]
new g_backup[MAX_RECORDS][MAP_LENGTH]       // empty = no backup yet

new Trie:g_cycle
new Float:g_nextUse[MAX_PLAYERS + 1]
new g_maxBackups

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

    bind_pcvar_num(create_cvar("olds_reserve_max", "1", FCVAR_NONE,
        "Backups each player may name (1-5).", true, 1.0, true, float(MAX_BACKUPS)), g_maxBackups)
    AutoExecConfig(true, "olds_reserve")

    register_clcmd("say", "on_say")
    register_clcmd("say_team", "on_say")
    register_concmd("olds_reserve_status", "cmd_status", ADMIN_CFG, "- nominations and their backups")

    g_cycle = TrieCreate()
}

public plugin_end()
{
    TrieDestroy(g_cycle)
}

/* ---------- the map cycle ---------- */

load_cycle()
{
    TrieClear(g_cycle)
    new path[128]
    get_cvar_string("mapcyclefile", path, charsmax(path))
    if (!path[0])
        copy(path, charsmax(path), "mapcycle.txt")

    new file = fopen(path, "rt")
    if (!file)
        return
    new line[128], map[MAP_LENGTH]
    while (fgets(file, line, charsmax(line)))
    {
        trim(line)
        if (!line[0] || line[0] == ';' || (line[0] == '/' && line[1] == '/'))
            continue
        parse(line, map, charsmax(map))
        strtolower(map)
        if (map[0])
            TrieSetCell(g_cycle, map, 1)
    }
    fclose(file)
}

bool:in_cycle(const map[])
{
    new key[MAP_LENGTH]
    copy(key, charsmax(key), map)
    strtolower(key)
    return TrieKeyExists(g_cycle, key)
}

/* ---------- records ---------- */

/* Map Manager nomination: called before a nomination is added. */
public mapm_can_be_nominated(id, map[])
{
    if (!(1 <= id <= MaxClients) || !map[0])
        return NOMINATION_ALLOWED
    for (new r = 0; r < MAX_RECORDS; r++)
        if (g_player[r] == id && equali(g_primary[r], map))
            return NOMINATION_ALLOWED      // already known
    new r = free_record()
    if (r >= 0)
    {
        g_player[r] = id
        copy(g_primary[r], charsmax(g_primary[]), map)
        g_backup[r][0] = EOS
    }
    return NOMINATION_ALLOWED
}

free_record()
{
    for (new r = 0; r < MAX_RECORDS; r++)
        if (!g_player[r])
            return r
    return -1
}

clear_record(r)
{
    g_player[r] = 0
    g_primary[r][0] = EOS
    g_backup[r][0] = EOS
}

clear_all()
{
    for (new r = 0; r < MAX_RECORDS; r++)
        clear_record(r)
}

bool:map_taken(const map[])
{
    for (new r = 0; r < MAX_RECORDS; r++)
        if (g_player[r] && (equali(g_primary[r], map) || equali(g_backup[r], map)))
            return true
    return false
}

bool:map_in_vote(const map[])
{
    new item[MAP_LENGTH]
    for (new i = mapm_get_count_maps_in_vote() - 1; i >= 0; i--)
    {
        mapm_get_voteitem_info(i, item, charsmax(item))
        if (equali(item, map))
            return true
    }
    return false
}

public client_disconnected(id)
{
    // Map Manager drops a leaving player's nominations too.
    for (new r = 0; r < MAX_RECORDS; r++)
        if (g_player[r] == id)
            clear_record(r)
    g_nextUse[id] = 0.0
}

/* ---------- /backup ---------- */

public on_say(id)
{
    if (!is_user_connected(id))
        return PLUGIN_CONTINUE

    new text[96]
    read_args(text, charsmax(text))
    remove_quotes(text)
    trim(text)
    if (!equali(text, "/backup", 7) || (text[7] && text[7] != ' '))
        return PLUGIN_CONTINUE

    new Float:now = get_gametime()
    if (now < g_nextUse[id])
        return PLUGIN_HANDLED
    g_nextUse[id] = now + 1.0

    new map[MAP_LENGTH]
    copy(map, charsmax(map), text[7])
    trim(map)
    if (!map[0])
        list_own(id)
    else
        add_backup(id, map)
    return PLUGIN_HANDLED
}

add_backup(id, const map[])
{
    if (is_vote_started())
    {
        say_to(id, "RESERVE_VOTE_RUNNING")
        return
    }

    // The latest nomination of this player that has no backup yet.
    new target = -1, used
    for (new r = 0; r < MAX_RECORDS; r++)
    {
        if (g_player[r] != id)
            continue
        if (g_backup[r][0])
            used++
        else
            target = r
    }
    if (used >= g_maxBackups)
    {
        say_to(id, "RESERVE_LIMIT", g_maxBackups)
        return
    }
    if (target < 0)
    {
        say_to(id, "RESERVE_NOMINATE_FIRST")
        return
    }

    for (new i = 0; map[i]; i++)
    {
        if (!isalnum(map[i]) && map[i] != '_' && map[i] != '-' && map[i] != '.')
        {
            say_to(id, "RESERVE_UNKNOWN", "?")
            return
        }
    }
    if (!is_map_valid(map))
    {
        say_to(id, "RESERVE_UNKNOWN", map)
        return
    }
    load_cycle()
    if (!in_cycle(map))
    {
        say_to(id, "RESERVE_NOT_IN_CYCLE", map)
        return
    }
    new current[MAP_LENGTH]
    get_mapname(current, charsmax(current))
    if (equali(map, current))
    {
        say_to(id, "RESERVE_CURRENT", map)
        return
    }
    if (map_taken(map))
    {
        say_to(id, "RESERVE_DUPLICATE", map)
        return
    }

    copy(g_backup[target], charsmax(g_backup[]), map)
    strtolower(g_backup[target])
    say_to(id, "RESERVE_SET", g_backup[target], g_primary[target])
}

list_own(id)
{
    new count
    for (new r = 0; r < MAX_RECORDS; r++)
    {
        if (g_player[r] != id)
            continue
        count++
        if (g_backup[r][0])
            say_to(id, "RESERVE_LINE", g_primary[r], g_backup[r])
        else
            say_to(id, "RESERVE_LINE_NONE", g_primary[r])
    }
    if (!count)
        say_to(id, "RESERVE_USAGE")
}

/* ---------- the vote ---------- */

/* Map Manager core, before the vote list is completed. */
public mapm_prepare_votelist(type)
{
    load_cycle()
    for (new r = 0; r < MAX_RECORDS; r++)
    {
        if (!g_player[r] || !g_backup[r][0] || map_in_vote(g_primary[r]))
            continue
        // Taken back by the player, or left out because the list was full: no backup.
        if (is_map_valid(g_primary[r]) && in_cycle(g_primary[r]))
            continue
        if (!is_map_valid(g_backup[r]) || !in_cycle(g_backup[r]) || map_in_vote(g_backup[r]))
            continue

        if (mapm_push_map_to_votelist(g_backup[r], PUSH_BY_NATIVE) == PUSH_SUCCESS)
        {
            new name[MAX_NAME_LENGTH]
            if (is_user_connected(g_player[r]))
                get_user_name(g_player[r], name, charsmax(name))
            else
                copy(name, charsmax(name), "-")
            client_print_color(0, print_team_default, "^4[Backup]^1 %L", LANG_PLAYER, "RESERVE_USED",
                g_backup[r], g_primary[r], name)
        }
    }
}

public mapm_vote_started(type)
{
    clear_all()
}

public mapm_vote_canceled(type)
{
    clear_all()
}

/* ---------- helpers ---------- */

say_to(id, const key[], any:...)
{
    new pattern[160], message[192]
    formatex(pattern, charsmax(pattern), "%L", id, key)
    vformat(message, charsmax(message), pattern, 3)
    client_print_color(id, print_team_default, "^4[Backup]^1 %s", message)
}

public cmd_status(id, level, cid)
{
    if (!cmd_access(id, level, cid, 1))
        return PLUGIN_HANDLED

    load_cycle()
    console_print(id, "[Backup] %d map(s) in the cycle; up to %d backup(s) per player; vote running %d",
        TrieGetSize(g_cycle), g_maxBackups, is_vote_started())
    new name[MAX_NAME_LENGTH]
    for (new r = 0; r < MAX_RECORDS; r++)
    {
        if (!g_player[r])
            continue
        if (is_user_connected(g_player[r]))
            get_user_name(g_player[r], name, charsmax(name))
        else
            copy(name, charsmax(name), "-")
        console_print(id, "[Backup] %s: %s (valid %d, in cycle %d) -> %s", name, g_primary[r],
            is_map_valid(g_primary[r]), in_cycle(g_primary[r]), g_backup[r][0] ? g_backup[r] : "-")
    }
    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.