Skip to content
View in the app

A better way to browse. Learn more.

Learn more

admin-map-change-menu/addons/amxmodx/scripting/olds_mapmenu.sma

olds_mapmenu.sma
328 lines · 9.5 KB · Pawn (AMX Mod X)
Raw
/*
 * Admin Map Change Menu (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/>.
 *
 * amx_mapmenu (ADMIN_MAP) opens a paged menu of the maps in the map cycle file (read with
 * read_file, every name checked with is_map_valid, duplicates and bad lines skipped). The
 * first item switches between "change now" and "change at round end" (olds_mapmenu_endround
 * is the default). Now: the level changes olds_mapmenu_delay seconds later. Round end: the
 * change waits for RG_RoundEnd, then the same delay. A chat line announces it and log_amx
 * records the admin. From the console: amx_mapmenu <map> [now|endround] or amx_mapmenu cancel.
 *
 * Cvars: olds_mapmenu_delay 5 (0-60), olds_mapmenu_endround 1
 */

#include <amxmodx>
#include <amxmisc>
#include <reapi>

#pragma semicolon 0

#define PLUGIN  "Admin Map Change Menu"
#define VERSION "1.0.0"
#define AUTHOR  "daemon (olds.ro)"

#define MAX_MAPS  512
#define TASK_CHANGE 8900

new Float:g_delay
new g_endround

new Array:g_maps
new g_mapCount
new g_pending[32]
new bool:g_waitRoundEnd
new bool:g_modeEnd[MAX_PLAYERS + 1]

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

    bind_pcvar_float(create_cvar("olds_mapmenu_delay", "5", FCVAR_NONE,
        "Seconds before the level changes", true, 0.0, true, 60.0), g_delay)
    bind_pcvar_num(create_cvar("olds_mapmenu_endround", "1", FCVAR_NONE,
        "1 = the menu starts in 'change at round end' mode", true, 0.0, true, 1.0), g_endround)

    register_concmd("amx_mapmenu", "cmd_mapmenu", ADMIN_MAP, "- open the map menu, or <map> [now|endround], or cancel")
    RegisterHookChain(RG_RoundEnd, "on_round_end_post", true)

    register_dictionary("olds_mapmenu.txt")
    g_maps = ArrayCreate(32)
    AutoExecConfig(true, "olds_mapmenu")
}

public plugin_cfg()
{
    load_maps()
}

public plugin_end()
{
    ArrayDestroy(g_maps)
}

bool:plain_map_name(const name[])
{
    new len = strlen(name)
    if (len < 1 || len > 31)
        return false
    for (new i = 0; i < len; i++)
    {
        if (!isalnum(name[i]) && name[i] != '_' && name[i] != '-' && name[i] != '.')
            return false
    }
    return true
}

bool:listed(const name[])
{
    new other[32]
    for (new i = 0; i < g_mapCount; i++)
    {
        ArrayGetString(g_maps, i, other, charsmax(other))
        if (equali(other, name))
            return true
    }
    return false
}

load_maps()
{
    ArrayClear(g_maps)
    g_mapCount = 0
    new file[128]
    get_cvar_string("mapcyclefile", file, charsmax(file))
    trim(file)
    if (!file[0] || contain(file, "..") >= 0)
        copy(file, charsmax(file), "mapcycle.txt")
    if (!file_exists(file))
    {
        log_amx("Map cycle file %s not found: the menu is empty", file)
        return
    }
    new line[128], name[64], len, skipped, pos
    while ((pos = read_file(file, pos, line, charsmax(line), len)) != 0)
    {
        new cut = contain(line, "//")
        if (cut >= 0)
            line[cut] = EOS
        trim(line)
        if (!line[0])
            continue
        parse(line, name, charsmax(name))
        new dot = contain(name, ".bsp")
        if (dot > 0)
            name[dot] = EOS
        if (!plain_map_name(name) || !is_map_valid(name))
        {
            skipped++
            log_amx("Map cycle: skipped ^"%s^" (no such map)", name)
            continue
        }
        if (listed(name))
            continue
        if (g_mapCount >= MAX_MAPS)
        {
            log_amx("Map cycle: only the first %d maps are listed", MAX_MAPS)
            break
        }
        ArrayPushString(g_maps, name)
        g_mapCount++
    }
    log_amx("Map menu: %d maps from %s, %d skipped", g_mapCount, file, skipped)
}

public client_putinserver(id)
{
    g_modeEnd[id] = g_endround != 0
}

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

public cmd_mapmenu(id, level, cid)
{
    if (!cmd_access(id, level, cid, 1))
        return PLUGIN_HANDLED
    if (read_argc() >= 2)
    {
        new map[64], mode[16]
        read_argv(1, map, charsmax(map))
        read_argv(2, mode, charsmax(mode))
        if (equali(map, "cancel"))
        {
            cancel_change(id)
            return PLUGIN_HANDLED
        }
        new bool:atEnd = g_endround != 0
        if (equali(mode, "now"))
            atEnd = false
        else if (equali(mode, "endround"))
            atEnd = true
        else if (mode[0])
        {
            console_print(id, "%L", id, "MAPMENU_USAGE")
            return PLUGIN_HANDLED
        }
        if (!plain_map_name(map) || !listed(map))
        {
            console_print(id, "%L", id, "MAPMENU_NOT_LISTED", map)
            return PLUGIN_HANDLED
        }
        choose(id, map, atEnd)
        return PLUGIN_HANDLED
    }
    if (!id)
    {
        new map[32]
        server_print("%L", LANG_SERVER, "MAPMENU_LIST", g_mapCount)
        for (new i = 0; i < g_mapCount; i++)
        {
            ArrayGetString(g_maps, i, map, charsmax(map))
            server_print("  %d. %s", i + 1, map)
        }
        return PLUGIN_HANDLED
    }
    if (!g_mapCount)
    {
        console_print(id, "%L", id, "MAPMENU_EMPTY")
        client_print_color(id, print_team_default, "%L", id, "MAPMENU_EMPTY")
        return PLUGIN_HANDLED
    }
    show_menu_for(id, 0)
    return PLUGIN_HANDLED
}

show_menu_for(id, page)
{
    new title[96], item[64], map[32]
    if (g_pending[0])
        formatex(title, charsmax(title), "%L", id, "MAPMENU_TITLE_PENDING", g_pending)
    else
        formatex(title, charsmax(title), "%L", id, "MAPMENU_TITLE")
    new menu = menu_create(title, "menu_handler")
    if (g_modeEnd[id])
        formatex(item, charsmax(item), "%L", id, "MAPMENU_MODE_END")
    else
        formatex(item, charsmax(item), "%L", id, "MAPMENU_MODE_NOW")
    menu_additem(menu, item, "#mode")
    for (new i = 0; i < g_mapCount; i++)
    {
        ArrayGetString(g_maps, i, map, charsmax(map))
        menu_additem(menu, map, map)
    }
    menu_display(id, menu, page)
}

public menu_handler(id, menu, item)
{
    if (item < 0 || !is_user_connected(id) || !(get_user_flags(id) & ADMIN_MAP))
    {
        menu_destroy(menu)
        return PLUGIN_HANDLED
    }
    new info[32], access, callback
    menu_item_getinfo(menu, item, access, info, charsmax(info), _, _, callback)
    new page = item / 7
    menu_destroy(menu)
    if (equal(info, "#mode"))
    {
        g_modeEnd[id] = !g_modeEnd[id]
        show_menu_for(id, page)
        return PLUGIN_HANDLED
    }
    /* The list may have changed since the menu was built. */
    if (!plain_map_name(info) || !listed(info) || !is_map_valid(info))
    {
        client_print_color(id, print_team_default, "%L", id, "MAPMENU_NOT_LISTED", info)
        return PLUGIN_HANDLED
    }
    choose(id, info, g_modeEnd[id])
    return PLUGIN_HANDLED
}

/* ------------------------------------------------------------ the change */

admin_name(id, name[], len)
{
    if (id)
        get_user_name(id, name, len)
    else
        copy(name, len, "Server")
}

choose(id, const map[], bool:atEnd)
{
    new name[32], auth[44]
    admin_name(id, name, charsmax(name))
    if (id)
        get_user_authid(id, auth, charsmax(auth))
    else
        copy(auth, charsmax(auth), "console")
    remove_task(TASK_CHANGE)
    copy(g_pending, charsmax(g_pending), map)
    new Float:delay = floatclamp(g_delay, 0.0, 60.0)
    if (atEnd)
    {
        g_waitRoundEnd = true
        client_print_color(0, print_team_default, "%L", LANG_PLAYER, "MAPMENU_ANNOUNCE_END", name, map)
        log_amx("Admin ^"%s<%s>^" chose map %s (at round end)", name, auth, map)
    }
    else
    {
        g_waitRoundEnd = false
        client_print_color(0, print_team_default, "%L", LANG_PLAYER, "MAPMENU_ANNOUNCE_NOW", name, map, floatround(delay))
        log_amx("Admin ^"%s<%s>^" chose map %s (in %.0f s)", name, auth, map, delay)
        set_task(floatmax(delay, 0.1), "task_change", TASK_CHANGE)
    }
    if (id)
        console_print(id, "%L", id, "MAPMENU_SET", map)
}

cancel_change(id)
{
    if (!g_pending[0])
    {
        console_print(id, "%L", id, "MAPMENU_NOTHING")
        return
    }
    new name[32]
    admin_name(id, name, charsmax(name))
    remove_task(TASK_CHANGE)
    log_amx("Admin ^"%s^" cancelled the change to %s", name, g_pending)
    client_print_color(0, print_team_default, "%L", LANG_PLAYER, "MAPMENU_CANCELLED", name, g_pending)
    g_pending[0] = EOS
    g_waitRoundEnd = false
}

public on_round_end_post(WinStatus:status, ScenarioEventEndRound:event, Float:delay)
{
    if (!g_waitRoundEnd || !g_pending[0])
        return HC_CONTINUE
    g_waitRoundEnd = false
    new Float:wait = floatclamp(g_delay, 0.0, 60.0)
    client_print_color(0, print_team_default, "%L", LANG_PLAYER, "MAPMENU_ROUND_OVER", g_pending, floatround(wait))
    remove_task(TASK_CHANGE)
    set_task(floatmax(wait, 0.1), "task_change", TASK_CHANGE)
    return HC_CONTINUE
}

public task_change()
{
    if (!g_pending[0] || !plain_map_name(g_pending) || !is_map_valid(g_pending))
    {
        g_pending[0] = EOS
        return
    }
    new map[32]
    copy(map, charsmax(map), g_pending)
    g_pending[0] = EOS
    server_cmd("changelevel %s", map)
}

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.