Skip to content
View in the app

A better way to browse. Learn more.

Learn more

armoury-item-randomizer/addons/amxmodx/scripting/olds_armoury.sma

olds_armoury.sma
275 lines · 8.8 KB · Pawn (AMX Mod X)
Raw
/*
 * Armoury Item Randomizer (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/>.
 *
 * Every round (RG_CSGameRules_RestartRound, post) each armoury_entity of the map gets, with an
 * olds_arm_chance percent roll, a random item from the enabled pool; otherwise it keeps the
 * map's own item. The item is set in the entity (m_iItem), with its stock world model, and the
 * count is reset: the map's count with olds_arm_keepcount 1, one item with 0. The pool lives in
 * configs/armoury.ini ("<item> <1|0>" lines; unknown items are ignored). Admins toggle items
 * in the amx_armoury menu or with amx_armoury <item> <0|1>. A map without armoury entities is
 * left alone, without a word in the log.
 *
 * Cvars: olds_arm_chance 100, olds_arm_keepcount 1
 * Admin: amx_armoury [item 0|1] (ADMIN_CFG)
 */

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

#pragma semicolon 0

#define PLUGIN  "Armoury Item Randomizer"
#define VERSION "1.0.0"
#define AUTHOR  "daemon (olds.ro)"

#define ITEMS     19
#define MAX_ARMS  128

/* The stock armoury items (ArmouryItemPack 0-18) and their world models. */
new const ITEM_NAME[ITEMS][] =
{
    "mp5navy", "tmp", "p90", "mac10", "ak47", "sg552", "m4a1", "aug", "scout", "g3sg1",
    "awp", "m3", "xm1014", "m249", "flashbang", "hegrenade", "kevlar", "assaultsuit", "smokegrenade"
}
new const ITEM_MODEL[ITEMS][] =
{
    "models/w_mp5.mdl", "models/w_tmp.mdl", "models/w_p90.mdl", "models/w_mac10.mdl", "models/w_ak47.mdl",
    "models/w_sg552.mdl", "models/w_m4a1.mdl", "models/w_aug.mdl", "models/w_scout.mdl", "models/w_g3sg1.mdl",
    "models/w_awp.mdl", "models/w_m3.mdl", "models/w_xm1014.mdl", "models/w_m249.mdl", "models/w_flashbang.mdl",
    "models/w_hegrenade.mdl", "models/w_kevlar.mdl", "models/w_assault.mdl", "models/w_smokegrenade.mdl"
}

new g_chance
new g_keepCount

new bool:g_enabled[ITEMS]
new g_iniPath[PLATFORM_MAX_PATH]

new g_armCount
new g_arm[MAX_ARMS]
new g_origItem[MAX_ARMS]
new g_origCount[MAX_ARMS]

public plugin_precache()
{
    for (new i = 0; i < ITEMS; i++)
        precache_model(ITEM_MODEL[i])
}

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

    bind_pcvar_num(create_cvar("olds_arm_chance", "100", FCVAR_NONE,
        "Percent chance that an armoury gets a random item each round", true, 0.0, true, 100.0), g_chance)
    bind_pcvar_num(create_cvar("olds_arm_keepcount", "1", FCVAR_NONE,
        "1 keeps the map's item count, 0 gives one item", true, 0.0, true, 1.0), g_keepCount)
    AutoExecConfig(true, "olds_armoury")

    RegisterHookChain(RG_CSGameRules_RestartRound, "on_restart_post", true)
    register_concmd("amx_armoury", "cmd_armoury", ADMIN_CFG, "[item 0|1] - menu or toggle of the armoury item pool")
}

public plugin_cfg()
{
    get_configsdir(g_iniPath, charsmax(g_iniPath))
    add(g_iniPath, charsmax(g_iniPath), "/armoury.ini")
    load_pool()

    /* Remember the map's own items before any round changes them. */
    new ent = -1
    while ((ent = rg_find_ent_by_class(ent, "armoury_entity")) > 0 && g_armCount < MAX_ARMS)
    {
        g_arm[g_armCount] = ent
        g_origItem[g_armCount] = clamp(get_member(ent, m_Armoury_iItem), 0, ITEMS - 1)
        g_origCount[g_armCount] = max(get_member(ent, m_Armoury_iInitialCount), 1)
        g_armCount++
    }
}

/* ------------------------------------------------------------ the pool */

item_index(const name[])
{
    for (new i = 0; i < ITEMS; i++)
        if (equali(name, ITEM_NAME[i]))
            return i
    return -1
}

load_pool()
{
    for (new i = 0; i < ITEMS; i++)
        g_enabled[i] = true
    if (!file_exists(g_iniPath))
    {
        save_pool()
        return
    }
    new file = fopen(g_iniPath, "rt")
    if (!file)
        return
    new line[96], name[32], value[8], number
    while (fgets(file, line, charsmax(line)))
    {
        number++
        trim(line)
        if (!line[0] || line[0] == ';' || line[0] == '#' || (line[0] == '/' && line[1] == '/'))
            continue
        parse(line, name, charsmax(name), value, charsmax(value))
        new item = item_index(name)
        if (item < 0 || (value[0] != '0' && value[0] != '1') || value[1])
        {
            server_print("[Armoury] armoury.ini line %d ignored: %s", number, line)
            continue
        }
        g_enabled[item] = value[0] == '1'
    }
    fclose(file)
}

save_pool()
{
    new file = fopen(g_iniPath, "wt")
    if (!file)
    {
        log_amx("Cannot write %s", g_iniPath)
        return
    }
    fputs(file, "; Armoury Item Randomizer: items that can appear in armoury entities.^n")
    fputs(file, "; <item> <1|0>. Unknown items are ignored.^n")
    for (new i = 0; i < ITEMS; i++)
        fprintf(file, "%s %d^n", ITEM_NAME[i], g_enabled[i] ? 1 : 0)
    fclose(file)
}

/* ------------------------------------------------------------ the round */

set_item(ent, item, count)
{
    set_member(ent, m_Armoury_iItem, item)
    set_member(ent, m_Armoury_iInitialCount, count)
    set_member(ent, m_Armoury_iCount, count)
    engfunc(EngFunc_SetModel, ent, ITEM_MODEL[item])
    engfunc(EngFunc_SetSize, ent, Float:{-16.0, -16.0, 0.0}, Float:{16.0, 16.0, 16.0})
    set_pev(ent, pev_effects, pev(ent, pev_effects) & ~EF_NODRAW)
}

public on_restart_post()
{
    if (!g_armCount)
        return HC_CONTINUE
    new pool[ITEMS], poolSize
    for (new i = 0; i < ITEMS; i++)
        if (g_enabled[i])
            pool[poolSize++] = i
    new summary[256]
    for (new a = 0; a < g_armCount; a++)
    {
        new ent = g_arm[a]
        if (!pev_valid(ent))
            continue
        new item = g_origItem[a]
        if (poolSize && g_chance > 0 && random_num(1, 100) <= g_chance)
            item = pool[random(poolSize)]
        new count = 1
        if (g_keepCount)
            count = g_origCount[a]
        set_item(ent, item, count)
        add(summary, charsmax(summary), " ")
        add(summary, charsmax(summary), ITEM_NAME[item])
    }
    server_print("[Armoury] round items:%s", summary)
    return HC_CONTINUE
}

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

public cmd_armoury(id, level, cid)
{
    if (!cmd_access(id, level, cid, 1))
        return PLUGIN_HANDLED
    if (read_argc() < 2)
    {
        if (id)
            show_pool_menu(id, 0)
        else
        {
            for (new i = 0; i < ITEMS; i++)
                server_print("  %s %d", ITEM_NAME[i], g_enabled[i] ? 1 : 0)
        }
        return PLUGIN_HANDLED
    }
    new name[32], value[8]
    read_argv(1, name, charsmax(name))
    read_argv(2, value, charsmax(value))
    new item = item_index(name)
    if (item < 0 || (value[0] != '0' && value[0] != '1') || value[1])
    {
        console_print(id, "Usage: amx_armoury [item 0|1]. Items: mp5navy tmp p90 mac10 ak47 sg552 m4a1 aug scout g3sg1 awp m3 xm1014 m249 flashbang hegrenade kevlar assaultsuit smokegrenade")
        return PLUGIN_HANDLED
    }
    set_enabled(id, item, value[0] == '1')
    console_print(id, "Armoury item %s: %d", ITEM_NAME[item], g_enabled[item] ? 1 : 0)
    return PLUGIN_HANDLED
}

set_enabled(id, item, bool:on)
{
    g_enabled[item] = on
    save_pool()
    new admin[MAX_NAME_LENGTH]
    if (id)
        get_user_name(id, admin, charsmax(admin))
    else
        copy(admin, charsmax(admin), "server")
    log_amx("%s set armoury item %s to %d", admin, ITEM_NAME[item], on ? 1 : 0)
}

show_pool_menu(id, page)
{
    new menu = menu_create("Armoury items (next round)", "menu_pool")
    new text[48], info[4]
    for (new i = 0; i < ITEMS; i++)
    {
        if (g_enabled[i])
            formatex(text, charsmax(text), "%s \y[on]", ITEM_NAME[i])
        else
            formatex(text, charsmax(text), "%s \r[off]", ITEM_NAME[i])
        num_to_str(i, info, charsmax(info))
        menu_additem(menu, text, info)
    }
    menu_display(id, menu, clamp(page, 0, menu_pages(menu) - 1))
}

public menu_pool(id, menu, key)
{
    if (key < 0)
    {
        menu_destroy(menu)
        return PLUGIN_HANDLED
    }
    new info[4], access, callback, page
    menu_item_getinfo(menu, key, access, info, charsmax(info), _, _, callback)
    menu_destroy(menu)
    if (!is_user_connected(id) || !(get_user_flags(id) & ADMIN_CFG))
        return PLUGIN_HANDLED
    new item = str_to_num(info)
    if (item < 0 || item >= ITEMS)
        return PLUGIN_HANDLED
    set_enabled(id, item, !g_enabled[item])
    page = item / 7
    show_pool_menu(id, page)
    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.