Skip to content
View in the app

A better way to browse. Learn more.

Learn more

paintball-item-shop/addons/amxmodx/scripting/olds_pbshop.sma

olds_pbshop.sma
362 lines · 11.6 KB · Pawn (AMX Mod X)
Raw
/*
 * Paintball Item Shop (olds.ro) - in paintball mode players spend points
 * (frags or money) on a fast marker, a bigger hopper, armour, a speed boost
 * or a grenade, with a limit of purchases per round.
 *
 * 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/>.
 */

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

#define PLUGIN  "Paintball Item Shop"
#define VERSION "1.0.0"
#define AUTHOR  "daemon (olds.ro)"

// The paintball mode tells whether it runs (library "paintball"). The native is optional:
// without the library the shop runs while the "Paintball Gun" plugin is loaded.
native pb_is_active()

#define FAST_RATE 0.7          // fast marker: the time between shots is 70 %
#define HOPPER_EXTRA 20
#define SPEED_FACTOR 1.25

enum
{
    ITEM_FAST,
    ITEM_HOPPER,
    ITEM_ARMOR,
    ITEM_SPEED,
    ITEM_GRENADE,
    ITEMS
}
new const ITEM_NAME[ITEMS][] = { "Fast marker", "Hopper +20", "Armor", "Speed boost", "Grenade" }

new const SOUND_BUY[] = "buttons/button9"       // stock, played with spk
new const SOUND_EQUIP[] = "items/gunpickup2"    // stock, played with spk

new const MARKERS[][] =
{
    "weapon_p228", "weapon_scout", "weapon_xm1014", "weapon_mac10", "weapon_aug", "weapon_elite",
    "weapon_fiveseven", "weapon_ump45", "weapon_sg550", "weapon_galil", "weapon_famas", "weapon_usp",
    "weapon_glock18", "weapon_awp", "weapon_mp5navy", "weapon_m249", "weapon_m3", "weapon_m4a1",
    "weapon_tmp", "weapon_g3sg1", "weapon_deagle", "weapon_sg552", "weapon_ak47", "weapon_p90"
}

new g_price[ITEMS]
new bool:g_has[MAX_PLAYERS + 1][ITEMS]
new g_bought[MAX_PLAYERS + 1]
new g_callback

new g_cvarCurrency, g_cvarMaxBuy, g_cvarPrice, g_cvarMoneyUnit

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

// Lets the plugin load without the paintball library; pb_is_active() is only called when it exists.
public native_filter(const name[], index, trap)
{
    if (!trap && equal(name, "pb_is_active"))
        return PLUGIN_HANDLED
    return PLUGIN_CONTINUE
}

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

    g_cvarCurrency = create_cvar("olds_pbshop_currency", "0", FCVAR_NONE, "0 = pay with frags, 1 = pay with money", true, 0.0, true, 1.0)
    g_cvarMaxBuy = create_cvar("olds_pbshop_maxbuy", "3", FCVAR_NONE, "Items a player can buy per round", true, 1.0, true, float(ITEMS))
    g_cvarPrice = create_cvar("olds_pbshop_price", "2,3,2,4,5", FCVAR_NONE,
        "Prices in points: fast marker, hopper, armor, speed boost, grenade")
    g_cvarMoneyUnit = create_cvar("olds_pbshop_money_unit", "500", FCVAR_NONE, "Dollars per point when paying with money", true, 1.0, true, 16000.0)
    hook_cvar_change(g_cvarPrice, "on_price_change")
    AutoExecConfig(true, "olds_pbshop")

    register_clcmd("say /pshop", "cmd_shop")
    register_clcmd("say_team /pshop", "cmd_shop")
    register_clcmd("chooseteam", "cmd_chooseteam")
    register_concmd("olds_pbshop_status", "cmd_status", ADMIN_CFG, "- shop state per player")

    RegisterHookChain(RG_CSGameRules_RestartRound, "on_restart_round_post", true)
    RegisterHookChain(RG_CBasePlayer_Killed, "on_killed_post", true)
    RegisterHookChain(RG_CBasePlayer_ResetMaxSpeed, "on_reset_maxspeed_post", true)
    for (new i = 0; i < sizeof(MARKERS); i++)
        RegisterHam(Ham_Weapon_PrimaryAttack, MARKERS[i], "on_primary_attack_post", 1)

    g_callback = menu_makecallback("menu_item_state")
    read_prices()
}

/* ---------- prices and state ---------- */

public on_price_change(pcvar, const old[], const value[])
{
    read_prices()
}

read_prices()
{
    new const DEFAULTS[ITEMS] = { 2, 3, 2, 4, 5 }
    new text[64], part[8], pos, item
    get_pcvar_string(g_cvarPrice, text, charsmax(text))
    replace_all(text, charsmax(text), " ", "")
    while (item < ITEMS && pos < strlen(text))
    {
        new next = contain(text[pos], ",")
        if (next < 0)
            next = strlen(text[pos])
        copy(part, min(next, charsmax(part)), text[pos])
        pos += next + 1
        new value = str_to_num(part)
        g_price[item] = (part[0] && is_str_num(part) && value >= 0) ? value : DEFAULTS[item]
        item++
    }
    for (; item < ITEMS; item++)
        g_price[item] = DEFAULTS[item]
}

bool:mode_active()
{
    if (LibraryExists("paintball", LibType_Library))
        return pb_is_active() != 0
    return is_plugin_loaded("Paintball Gun") >= 0
}

points_of(id)
{
    if (get_pcvar_num(g_cvarCurrency))
        return get_member(id, m_iAccount) / max(get_pcvar_num(g_cvarMoneyUnit), 1)
    return floatround(Float:get_entvar(id, var_frags), floatround_floor)
}

pay(id, price)
{
    if (get_pcvar_num(g_cvarCurrency))
    {
        rg_add_account(id, -price * max(get_pcvar_num(g_cvarMoneyUnit), 1))
        return
    }
    new Float:frags = Float:get_entvar(id, var_frags) - float(price)
    set_entvar(id, var_frags, frags)
    // show the new frag count on the scoreboard
    message_begin(MSG_ALL, get_user_msgid("ScoreInfo"))
    write_byte(id)
    write_short(floatround(frags))
    write_short(get_member(id, m_iDeaths))
    write_short(0)
    write_short(get_member(id, m_iTeam))
    message_end()
}

clear_items(id)
{
    for (new i = 0; i < ITEMS; i++)
        g_has[id][i] = false
}

public client_disconnected(id)
{
    clear_items(id)
    g_bought[id] = 0
}

public on_restart_round_post()
{
    for (new id = 1; id <= MaxClients; id++)
    {
        clear_items(id)
        g_bought[id] = 0
    }
}

public on_killed_post(victim, attacker, gib)
{
    if (1 <= victim <= MaxClients)
        clear_items(victim)         // unused items go with the life
}

/* ---------- shop ---------- */

public cmd_chooseteam(id)
{
    // M opens the shop while paintball runs and the player is alive; dead players keep the team menu.
    if (is_user_alive(id) && mode_active())
    {
        open_shop(id)
        return PLUGIN_HANDLED
    }
    return PLUGIN_CONTINUE
}

public cmd_shop(id)
{
    open_shop(id)
    return PLUGIN_HANDLED
}

open_shop(id)
{
    if (!mode_active())
    {
        client_print(id, print_chat, "[Shop] Paintball is not running, so the shop is closed.")
        return
    }
    if (!is_user_alive(id))
    {
        client_print(id, print_chat, "[Shop] The shop is for living players.")
        return
    }
    new title[96], label[64], info[4], unit[8]
    if (get_pcvar_num(g_cvarCurrency))
        copy(unit, charsmax(unit), "$")
    else
        copy(unit, charsmax(unit), "frags")
    formatex(title, charsmax(title), "Paintball shop^n\dYou have %d point(s), %d of %d buys used", points_of(id), g_bought[id], get_pcvar_num(g_cvarMaxBuy))
    new menu = menu_create(title, "on_shop")
    for (new i = 0; i < ITEMS; i++)
    {
        if (get_pcvar_num(g_cvarCurrency))
            formatex(label, charsmax(label), "%s \y%d pt ($%d)", ITEM_NAME[i], g_price[i], g_price[i] * get_pcvar_num(g_cvarMoneyUnit))
        else
            formatex(label, charsmax(label), "%s \y%d %s", ITEM_NAME[i], g_price[i], unit)
        num_to_str(i, info, charsmax(info))
        menu_additem(menu, label, info, 0, g_callback)
    }
    menu_display(id, menu)
}

bool:can_buy(id, item)
{
    return 0 <= item < ITEMS && !g_has[id][item] && g_bought[id] < get_pcvar_num(g_cvarMaxBuy) && points_of(id) >= g_price[item]
}

// Greys out what the player cannot buy now.
public menu_item_state(id, menu, item)
{
    new info[4], access, callback
    menu_item_getinfo(menu, item, access, info, charsmax(info), _, _, callback)
    return can_buy(id, str_to_num(info)) ? ITEM_ENABLED : ITEM_DISABLED
}

public on_shop(id, menu, item)
{
    if (item < 0)
    {
        menu_destroy(menu)
        return PLUGIN_HANDLED
    }
    new info[4], access, callback
    menu_item_getinfo(menu, item, access, info, charsmax(info), _, _, callback)
    menu_destroy(menu)
    new choice = str_to_num(info)
    if (!(0 <= choice < ITEMS) || !is_user_alive(id) || !mode_active())
        return PLUGIN_HANDLED

    if (g_bought[id] >= get_pcvar_num(g_cvarMaxBuy))
    {
        client_print(id, print_chat, "[Shop] You already bought %d item(s) this round.", g_bought[id])
        return PLUGIN_HANDLED
    }
    if (g_has[id][choice])
    {
        client_print(id, print_chat, "[Shop] You already have the %s.", ITEM_NAME[choice])
        return PLUGIN_HANDLED
    }
    if (points_of(id) < g_price[choice])
    {
        client_print(id, print_chat, "[Shop] The %s costs %d point(s); you have %d.", ITEM_NAME[choice], g_price[choice], points_of(id))
        return PLUGIN_HANDLED
    }

    pay(id, g_price[choice])
    g_bought[id]++
    g_has[id][choice] = true
    give(id, choice)
    client_cmd(id, "spk %s", SOUND_BUY)
    client_print(id, print_chat, "[Shop] Bought: %s (%d point(s) left, %d of %d buys used).", ITEM_NAME[choice], points_of(id), g_bought[id], get_pcvar_num(g_cvarMaxBuy))
    return PLUGIN_HANDLED
}

give(id, item)
{
    switch (item)
    {
        case ITEM_HOPPER:
        {
            new weapon = get_member(id, m_pActiveItem)
            if (!is_nullent(weapon))
            {
                new WeaponIdType:wid = WeaponIdType:get_member(weapon, m_iId)
                rg_set_user_bpammo(id, wid, rg_get_user_bpammo(id, wid) + HOPPER_EXTRA)
            }
        }
        case ITEM_ARMOR:
            rg_set_user_armor(id, 100, ARMOR_VESTHELM)
        case ITEM_SPEED:
            rg_reset_maxspeed(id)                    // the post hook adds the boost
        case ITEM_GRENADE:
            rg_give_item(id, "weapon_hegrenade")
    }
    client_cmd(id, "spk %s", SOUND_EQUIP)
}

public on_reset_maxspeed_post(id)
{
    if (1 <= id <= MaxClients && g_has[id][ITEM_SPEED] && is_user_alive(id))
        set_entvar(id, var_maxspeed, Float:get_entvar(id, var_maxspeed) * SPEED_FACTOR)
    return HC_CONTINUE
}

public on_primary_attack_post(weapon)
{
    new id = get_member(weapon, m_pPlayer)
    if (!(1 <= id <= MaxClients) || !g_has[id][ITEM_FAST])
        return HAM_IGNORED
    new Float:next = Float:get_member(weapon, m_Weapon_flNextPrimaryAttack)
    if (next > 0.0)
        set_member(weapon, m_Weapon_flNextPrimaryAttack, next * FAST_RATE)   // weapon times are relative
    return HAM_IGNORED
}

public cmd_status(id, level, cid)
{
    if (!cmd_access(id, level, cid, 1))
        return PLUGIN_HANDLED
    console_print(id, "[Shop] paintball %d, currency %d, max %d, prices %d %d %d %d %d", mode_active(), get_pcvar_num(g_cvarCurrency),
        get_pcvar_num(g_cvarMaxBuy), g_price[0], g_price[1], g_price[2], g_price[3], g_price[4])
    new name[32], items[64]
    for (new player = 1; player <= MaxClients; player++)
    {
        if (!is_user_connected(player))
            continue
        get_user_name(player, name, charsmax(name))
        items[0] = EOS
        for (new i = 0; i < ITEMS; i++)
        {
            if (g_has[player][i])
            {
                add(items, charsmax(items), ITEM_NAME[i])
                add(items, charsmax(items), "; ")
            }
        }
        console_print(id, "  %s: %d point(s), bought %d, items: %s maxspeed %.0f", name, points_of(player), g_bought[player], items,
            Float:get_entvar(player, var_maxspeed))
    }
    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.