Skip to content
View in the app

A better way to browse. Learn more.

Learn more

ladder-vote/addons/amxmodx/scripting/olds_arvote.sma

olds_arvote.sma
378 lines · 11.1 KB · Pawn (AMX Mod X)
Raw
/*
 * Ladder Vote (olds.ro) - Arms Race add-on
 * 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. It is distributed WITHOUT ANY WARRANTY; see
 * <https://www.gnu.org/licenses/>.
 *
 * Once per map, after the first round restart, the players vote between the
 * weapon ladders of configs/olds_arladders.ini (olds_arvote_time seconds).
 * The ladder with the most votes is used for the whole map; a tie, or no
 * votes, picks ladder number olds_arvote_default (0 = the first). The
 * ladder goes to the Arms Race core through its ar_set_ladder native, or,
 * without it, into the core's ladder cvar (olds_arvote_cvar); then the round
 * restarts so the first live round uses it. Every round start names the
 * ladder in chat for latecomers.
 *
 * olds_arladders.ini, one ladder per line:   Name = weapon_x, weapon_y, ...
 * A line with a weapon the game does not know is rejected at load with a log
 * line naming the line. With fewer than two valid ladders there is no vote
 * (a single valid ladder is used as it is).
 */
#include <amxmodx>
#include <amxmisc>
#include <reapi>

#define PLUGIN  "Ladder Vote"
#define VERSION "1.0.0"
#define AUTHOR  "daemon (olds.ro)"

#define MAX_LADDERS 7
#define MAX_WEAPONS 32
#define NAME_LEN    32
#define LIST_LEN    512
#define TASK_CLOSE  98330
#define TASK_SHOW   98340
#define TASK_SAY    98380

native ar_set_ladder(const weapons[])

new bool:g_hasNative = true
new g_cvTime, g_cvDefault, g_cvCvar

new g_name[MAX_LADDERS][NAME_LEN]
new g_list[MAX_LADDERS][LIST_LEN]          // weapon_a,weapon_b,...
new g_size[MAX_LADDERS]
new g_ladders

new bool:g_voteDone                         // the vote of this map has been held (or skipped)
new bool:g_voteOpen
new Float:g_voteEnd
new g_vote[MAX_PLAYERS + 1]                 // 0 = none, else ladder + 1
new g_menu[MAX_PLAYERS + 1]
new g_active = -1                           // the ladder in use, -1 = none chosen

public plugin_natives()
{
    set_native_filter("native_filter")
}

public native_filter(const name[], index, trap)
{
    if (!trap && equal(name, "ar_set_ladder"))
    {
        g_hasNative = false
        return PLUGIN_HANDLED
    }
    return PLUGIN_CONTINUE
}

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

    g_cvTime = create_cvar("olds_arvote_time", "15", FCVAR_NONE, "Seconds the ladder vote stays open", true, 5.0, true, 60.0)
    g_cvDefault = create_cvar("olds_arvote_default", "0", FCVAR_NONE, "Ladder used on a tie or with no votes (0 = the first)", true, 0.0, true, float(MAX_LADDERS - 1))
    g_cvCvar = create_cvar("olds_arvote_cvar", "ar_ladder", FCVAR_NONE, "The core's ladder cvar, used when the core has no ar_set_ladder native")
    AutoExecConfig(true, "olds_arvote")

    RegisterHookChain(RG_CSGameRules_RestartRound, "on_restart_round_post", true)
    register_concmd("amx_arladder", "cmd_status", ADMIN_KICK, "- shows the ladders and the vote")
}

public plugin_cfg()
{
    load_ladders()
}

public client_putinserver(id)
{
    g_vote[id] = 0
    g_menu[id] = 0
    if (g_voteOpen)
        set_task(1.0, "show_late", TASK_SHOW + id)
}

public client_disconnected(id)
{
    remove_task(TASK_SHOW + id)
    g_vote[id] = 0
    g_menu[id] = 0
}

/* ---------- olds_arladders.ini ---------- */

load_ladders()
{
    new path[160]
    get_configsdir(path, charsmax(path))
    add(path, charsmax(path), "/olds_arladders.ini")
    new file = fopen(path, "rt")
    if (!file)
    {
        log_amx("%s not found: no ladder vote", path)
        return
    }
    new line[LIST_LEN + 64], number
    while (fgets(file, line, charsmax(line)))
    {
        number++
        trim(line)
        if (!line[0] || line[0] == ';' || (line[0] == '/' && line[1] == '/'))
            continue
        if (g_ladders >= MAX_LADDERS)
        {
            log_amx("%s line %d: more than %d ladders, ignored", path, number, MAX_LADDERS)
            continue
        }
        parse_ladder(path, number, line)
    }
    fclose(file)
    log_amx("%d valid ladder(s) in %s", g_ladders, path)
}

parse_ladder(const path[], number, line[])
{
    new eq = contain(line, "=")
    if (eq <= 0)
    {
        log_amx("%s line %d: expected ^"Name = weapon_x, weapon_y^", line rejected", path, number)
        return
    }
    new name[64], rest[LIST_LEN + 64]
    copy(name, min(eq, charsmax(name)), line)
    trim(name)
    copy(rest, charsmax(rest), line[eq + 1])
    if (!name[0] || strlen(name) >= NAME_LEN)
    {
        log_amx("%s line %d: the ladder name must be 1 to %d characters, line rejected", path, number, NAME_LEN - 1)
        return
    }

    new list[LIST_LEN], weapon[64], tail[LIST_LEN + 64], count
    while (rest[0])
    {
        strtok2(rest, weapon, charsmax(weapon), tail, charsmax(tail), ',', TRIM_FULL)
        copy(rest, charsmax(rest), tail)
        if (!weapon[0])
            continue
        new id = get_weaponid(weapon)
        if (!equal(weapon, "weapon_", 7) || id <= 0 || id == CSW_C4)
        {
            log_amx("%s line %d: unknown weapon ^"%s^" in ladder ^"%s^", ladder rejected", path, number, weapon, name)
            return
        }
        if (count >= MAX_WEAPONS || strlen(list) + strlen(weapon) + 1 >= LIST_LEN)
        {
            log_amx("%s line %d: ladder ^"%s^" is too long (%d weapons at most), ladder rejected", path, number, name, MAX_WEAPONS)
            return
        }
        if (count)
            add(list, charsmax(list), ",")
        add(list, charsmax(list), weapon)
        count++
    }
    if (count < 2)
    {
        log_amx("%s line %d: ladder ^"%s^" needs at least two weapons, ladder rejected", path, number, name)
        return
    }
    copy(g_name[g_ladders], NAME_LEN - 1, name)
    copy(g_list[g_ladders], LIST_LEN - 1, list)
    g_size[g_ladders] = count
    g_ladders++
}

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

public on_restart_round_post()
{
    if (g_active >= 0)
    {
        remove_task(TASK_SAY)
        set_task(1.0, "announce", TASK_SAY)
    }
    if (g_voteDone)
        return
    g_voteDone = true
    if (g_ladders < 2)
    {
        if (g_ladders == 1)
        {
            log_amx("Only one valid ladder: no vote, %s is used", g_name[0])
            apply_ladder(0, false)
        }
        else
            log_amx("No valid ladder: no vote")
        return
    }
    open_vote()
}

open_vote()
{
    new secs = get_pcvar_num(g_cvTime)
    g_voteOpen = true
    g_voteEnd = get_gametime() + float(secs)
    arrayset(g_vote, 0, sizeof g_vote)
    set_task(float(secs), "close_vote", TASK_CLOSE)
    log_amx("Ladder vote open for %d s between %d ladders", secs, g_ladders)
    client_print_color(0, print_team_default, "%L", LANG_PLAYER, "ARV_OPEN", secs)
    for (new id = 1; id <= MaxClients; id++)
    {
        if (is_user_connected(id) && !is_user_hltv(id))
            show_vote_menu(id)
    }
}

public show_late(task)
{
    new id = task - TASK_SHOW
    if (g_voteOpen && is_user_connected(id))
        show_vote_menu(id)
}

show_vote_menu(id)
{
    cancel_menu(id)
    new title[96], text[96]
    new left = max(1, floatround(g_voteEnd - get_gametime(), floatround_ceil))
    formatex(title, charsmax(title), "%L", id, "ARV_MENU_TITLE", left)
    new menu = menu_create(title, "on_vote")
    for (new i = 0; i < g_ladders; i++)
    {
        formatex(text, charsmax(text), "%s \d(%d weapons)", g_name[i], g_size[i])
        menu_additem(menu, text)
    }
    menu_setprop(menu, MPROP_EXIT, MEXIT_ALL)
    g_menu[id] = menu
    menu_display(id, menu)
}

public on_vote(id, menu, item)
{
    if (g_menu[id] == menu)
        g_menu[id] = 0
    menu_destroy(menu)
    if (item < 0 || item >= g_ladders || !g_voteOpen)
        return PLUGIN_HANDLED
    g_vote[id] = item + 1
    client_print_color(id, print_team_default, "%L", id, "ARV_VOTED", g_name[item])
    new name[32]
    get_user_name(id, name, charsmax(name))
    log_amx("%s votes for ladder %d (%s)", name, item, g_name[item])
    return PLUGIN_HANDLED
}

/* Closes our menu if the player still has it open (the handler destroys it). */
cancel_menu(id)
{
    if (!g_menu[id] || !is_user_connected(id))
    {
        g_menu[id] = 0
        return
    }
    new oldmenu, newmenu
    player_menu_info(id, oldmenu, newmenu)
    if (newmenu == g_menu[id])
    {
        menu_cancel(id)
        show_menu(id, 0, "^n", 1)
    }
    g_menu[id] = 0
}

public close_vote()
{
    if (!g_voteOpen)
        return
    g_voteOpen = false
    new counts[MAX_LADDERS], total
    for (new id = 1; id <= MaxClients; id++)
    {
        remove_task(TASK_SHOW + id)
        cancel_menu(id)
        if (is_user_connected(id) && g_vote[id] > 0)
        {
            counts[g_vote[id] - 1]++
            total++
        }
    }
    new best = 0, tied = 0
    for (new i = 0; i < g_ladders; i++)
    {
        if (counts[i] > counts[best])
        {
            best = i
            tied = 0
        }
        else if (i != best && counts[i] == counts[best])
            tied++
    }
    new chosen = best
    if (!total || tied)
    {
        chosen = get_pcvar_num(g_cvDefault)
        if (chosen < 0 || chosen >= g_ladders)
            chosen = 0
        log_amx("Ladder vote: %d votes, no single winner; olds_arvote_default picks %d (%s)", total, chosen, g_name[chosen])
    }
    else
        log_amx("Ladder vote: %d votes, %s wins with %d", total, g_name[chosen], counts[chosen])
    apply_ladder(chosen, true)
}

apply_ladder(index, bool:restart)
{
    g_active = index
    new bool:applied
    if (g_hasNative)
    {
        ar_set_ladder(g_list[index])
        applied = true
        log_amx("Ladder %s sent to the core (ar_set_ladder): %s", g_name[index], g_list[index])
    }
    else
    {
        new cvar[64]
        get_pcvar_string(g_cvCvar, cvar, charsmax(cvar))
        new pointer = cvar[0] ? get_cvar_pointer(cvar) : 0
        if (pointer)
        {
            set_pcvar_string(pointer, g_list[index])
            applied = true
            log_amx("Ladder %s written to %s: %s", g_name[index], cvar, g_list[index])
        }
        else
            log_amx("No ar_set_ladder native and no cvar ^"%s^": ladder %s is not applied", cvar, g_name[index])
    }
    client_print_color(0, print_team_default, "%L", LANG_PLAYER, "ARV_CHOSEN", g_name[index])
    // The first live round starts with the chosen ladder.
    if (applied && restart)
        server_cmd("sv_restart 1")
}

public announce()
{
    if (g_active >= 0)
        client_print_color(0, print_team_default, "%L", LANG_PLAYER, "ARV_ACTIVE", g_name[g_active], g_size[g_active])
}

public cmd_status(id, level, cid)
{
    if (!cmd_access(id, level, cid, 1))
        return PLUGIN_HANDLED
    for (new i = 0; i < g_ladders; i++)
        console_print(id, "[Ladder] %d. %s: %s", i, g_name[i], g_list[i])
    new active[NAME_LEN]
    copy(active, charsmax(active), "none")
    if (g_active >= 0)
        copy(active, charsmax(active), g_name[g_active])
    console_print(id, "[Ladder] %d ladder(s); vote held %d, open %d; in use: %s; core native %d.",
        g_ladders, g_voteDone, g_voteOpen, active, g_hasNative)
    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.