Skip to content
View in the app

A better way to browse. Learn more.

Learn more

supply-draft/addons/amxmodx/scripting/olds_supplydraft.sma

olds_supplydraft.sma
372 lines · 11.2 KB · Pawn (AMX Mod X)
Raw
/*
 * Supply Draft (olds.ro) - Zombie Plague 5.0 add-on. While a round prepares
 * (from the new round until the game mode starts), the humans vote between
 * two team supplies; when the mode starts the winner is handed to every
 * living human once, through the shop's zp_items_force_buy().
 *
 * 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/>.
 *
 * The two supplies offered are the first two items of configs/olds_supplydraft.ini
 * that the shop knows (zp_items_get_id). Every living human has one vote and
 * can change it with /supply until the vote closes (olds_supplydraft_secs, or
 * the mode starting, whichever comes first); only the last choice counts.
 * A tie goes to the first supply in odd rounds and the second in even rounds
 * (rounds counted from the map start). A preparation that ends without the
 * mode starting (a restart, a new round, the round ending) is void: no supply.
 */

#include <amxmodx>
#include <amxmisc>

#pragma reqlib "zp50_core"
#pragma reqlib "zp50_items"
native zp_core_is_zombie(id)
native zp_items_get_id(const real_name[])
native zp_items_get_name(itemid, name[], len)
native zp_items_force_buy(id, itemid, ignorecost = 0)

#define PLUGIN  "Supply Draft"
#define VERSION "1.0.0"
#define AUTHOR  "daemon (olds.ro)"

#define MAX_SUPPLIES 16
#define NAME_LEN     32
#define TASK_OPEN    6301
#define TASK_CLOSE   6302

new g_list[MAX_SUPPLIES][NAME_LEN], g_listCount
new g_cvSecs

new g_round                      // preparations started since the map began
new bool:g_active                // a preparation is running and has not ended
new bool:g_open                  // votes are taken
new g_item[2] = { -1, -1 }       // the two supplies (shop item ids)
new g_itemName[2][NAME_LEN]
new g_vote[MAX_PLAYERS + 1]      // 0 = none, 1 or 2
new g_menu[MAX_PLAYERS + 1]      // our menu the player has open, 0 = none
new g_winner = -1                // 0 or 1 once the vote closed, -1 = none
new Float:g_closeAt

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

    g_cvSecs = create_cvar("olds_supplydraft_secs", "15", FCVAR_NONE, "Seconds the supply vote stays open", true, 3.0, true, 120.0)
    AutoExecConfig(true, "olds_supplydraft")

    register_event("HLTV", "on_new_round", "a", "1=0", "2=0")
    register_logevent("on_round_end", 2, "1=Round_End")
    register_clcmd("say /supply", "cmd_supply")
    register_clcmd("say_team /supply", "cmd_supply")
    register_concmd("amx_supplydraft", "cmd_status", ADMIN_KICK, "- shows the supply draft")
}

public plugin_cfg()
{
    new path[160]
    get_configsdir(path, charsmax(path))
    add(path, charsmax(path), "/olds_supplydraft.ini")
    new file = fopen(path, "rt")
    if (!file)
    {
        log_amx("%s not found: no supplies to offer", path)
        return
    }
    new line[96]
    while (g_listCount < MAX_SUPPLIES && fgets(file, line, charsmax(line)))
    {
        trim(line)
        if (!line[0] || line[0] == ';' || (line[0] == '/' && line[1] == '/'))
            continue
        if (strlen(line) >= NAME_LEN)
        {
            log_amx("Supply name too long, skipped: %s", line)
            continue
        }
        copy(g_list[g_listCount++], NAME_LEN - 1, line)
    }
    fclose(file)
    log_amx("%d supplies listed in %s", g_listCount, path)
}

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

bool:eligible(id)
{
    if (!is_user_alive(id) || zp_core_is_zombie(id))
        return false
    new team = get_user_team(id)
    return team == 1 || team == 2
}

/* ---------- the preparation phase ---------- */

public on_new_round()
{
    void_draft("a new round began")
    g_round++
    g_active = true
    remove_task(TASK_OPEN)
    // The players respawn right after this event.
    set_task(0.5, "open_draft", TASK_OPEN)
}

public on_round_end()
{
    void_draft("the round ended")
}

/* An unfinished preparation gives nothing. */
void_draft(const why[])
{
    remove_task(TASK_OPEN)
    remove_task(TASK_CLOSE)
    if (g_active)
    {
        if (g_item[0] >= 0)
        {
            client_print_color(0, print_team_default, "%L", LANG_PLAYER, "SD_VOID")
            log_amx("Round %d: supply draft void, %s before the mode started", g_round, why)
        }
        g_active = false
    }
    g_open = false
    close_menus()
    g_item[0] = -1
    g_item[1] = -1
    g_winner = -1
    arrayset(g_vote, 0, sizeof g_vote)
}

public open_draft()
{
    if (!g_active)
        return
    new found
    for (new i = 0; i < g_listCount && found < 2; i++)
    {
        new item = zp_items_get_id(g_list[i])
        if (item < 0 || (found == 1 && item == g_item[0]))
            continue
        g_item[found] = item
        zp_items_get_name(item, g_itemName[found], NAME_LEN - 1)
        found++
    }
    if (found < 2)
    {
        log_amx("Round %d: fewer than two listed supplies are in the shop, no draft", g_round)
        g_item[0] = -1
        g_item[1] = -1
        g_active = false
        return
    }
    new secs = get_pcvar_num(g_cvSecs)
    g_open = true
    g_winner = -1
    g_closeAt = get_gametime() + float(secs)
    set_task(float(secs), "close_vote", TASK_CLOSE)
    client_print_color(0, print_team_default, "%L", LANG_PLAYER, "SD_OPEN", g_itemName[0], g_itemName[1], secs)
    log_amx("Round %d: supply draft %s / %s open for %d s", g_round, g_itemName[0], g_itemName[1], secs)
    for (new id = 1; id <= MaxClients; id++)
    {
        if (eligible(id))
            show_vote_menu(id)
    }
}

count_votes(&first, &second)
{
    first = 0
    second = 0
    for (new id = 1; id <= MaxClients; id++)
    {
        if (!is_user_connected(id))
            continue
        if (g_vote[id] == 1)
            first++
        else if (g_vote[id] == 2)
            second++
    }
}

public close_vote()
{
    remove_task(TASK_CLOSE)
    if (!g_active || !g_open)
        return
    g_open = false
    close_menus()
    new first, second
    count_votes(first, second)
    if (first + second == 0)
    {
        g_winner = -1
        client_print_color(0, print_team_default, "%L", LANG_PLAYER, "SD_NO_VOTES")
        log_amx("Round %d: no supply votes", g_round)
        return
    }
    new bool:tie = first == second
    if (first > second)
        g_winner = 0
    else if (second > first)
        g_winner = 1
    else
        g_winner = (g_round % 2 == 1) ? 0 : 1
    if (tie)
        client_print_color(0, print_team_default, "%L", LANG_PLAYER, "SD_TIE", first, second, g_round, g_itemName[g_winner])
    else
        client_print_color(0, print_team_default, "%L", LANG_PLAYER, "SD_WINNER", g_itemName[g_winner], first, second)
    if (tie)
        log_amx("Round %d: votes %d / %d, tie, round parity picks %s", g_round, first, second, g_itemName[g_winner])
    else
        log_amx("Round %d: votes %d / %d, winner %s", g_round, first, second, g_itemName[g_winner])
}

/* Zombie Plague 5.0: the game mode starts, the preparation is over. */
public zp_fw_gamemodes_start(game_mode_id)
{
    remove_task(TASK_OPEN)
    if (!g_active)
        return
    if (g_open)
        close_vote()
    g_active = false
    if (g_winner < 0)
        return
    new given
    for (new id = 1; id <= MaxClients; id++)
    {
        if (!eligible(id))
            continue
        zp_items_force_buy(id, g_item[g_winner], 1)
        given++
    }
    client_print_color(0, print_team_default, "%L", LANG_PLAYER, "SD_GIVEN", g_itemName[g_winner], given)
    log_amx("Round %d: %s given to %d humans", g_round, g_itemName[g_winner], given)
    g_winner = -1
}

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

show_vote_menu(id)
{
    cancel_menu(id)
    new first, second, title[128], text[64]
    count_votes(first, second)
    new left = max(0, floatround(g_closeAt - get_gametime(), floatround_ceil))
    formatex(title, charsmax(title), "%L", id, "SD_MENU_TITLE", left)
    new menu = menu_create(title, "on_vote")
    formatex(text, charsmax(text), "%s \y(%d)", g_itemName[0], first)
    menu_additem(menu, text)
    formatex(text, charsmax(text), "%s \y(%d)", g_itemName[1], second)
    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 > 1)
        return PLUGIN_HANDLED
    if (!g_open)
    {
        client_print_color(id, print_team_default, "%L", id, "SD_CLOSED")
        return PLUGIN_HANDLED
    }
    if (!eligible(id))
    {
        client_print_color(id, print_team_default, "%L", id, "SD_NOT_ELIGIBLE")
        return PLUGIN_HANDLED
    }
    new before = g_vote[id]
    g_vote[id] = item + 1
    if (before && before != g_vote[id])
        client_print_color(id, print_team_default, "%L", id, "SD_CHANGED", g_itemName[item])
    else
        client_print_color(id, print_team_default, "%L", id, "SD_VOTED", g_itemName[item])
    new name[32]
    get_user_name(id, name, charsmax(name))
    log_amx("Round %d: %s votes %d (%s)", g_round, name, item + 1, g_itemName[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
}

close_menus()
{
    for (new id = 1; id <= MaxClients; id++)
        cancel_menu(id)
}

public cmd_supply(id)
{
    if (g_open)
    {
        if (eligible(id))
            show_vote_menu(id)
        else
            client_print_color(id, print_team_default, "%L", id, "SD_NOT_ELIGIBLE")
        return PLUGIN_HANDLED
    }
    if (g_active && g_winner >= 0)
        client_print_color(id, print_team_default, "%L", id, "SD_PENDING", g_itemName[g_winner])
    else
        client_print_color(id, print_team_default, "%L", id, "SD_CLOSED")
    return PLUGIN_HANDLED
}

public cmd_status(id, level, cid)
{
    if (!cmd_access(id, level, cid, 1))
        return PLUGIN_HANDLED
    if (g_item[0] < 0)
    {
        console_print(id, "[Supply] Round %d: no draft running (%d supplies listed).", g_round, g_listCount)
        return PLUGIN_HANDLED
    }
    new first, second
    count_votes(first, second)
    new winner[NAME_LEN]
    copy(winner, charsmax(winner), "-")
    if (g_winner >= 0)
        copy(winner, charsmax(winner), g_itemName[g_winner])
    console_print(id, "[Supply] Round %d: 1. %s (%d)  2. %s (%d); voting %d, waiting for the mode %d, winner %s.",
        g_round, g_itemName[0], first, g_itemName[1], second, g_open, g_active, winner)
    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.