Skip to content
View in the app

A better way to browse. Learn more.

Learn more

armor-loan/addons/amxmodx/scripting/olds_armorloan.sma

olds_armorloan.sma
334 lines · 9.8 KB · Pawn (AMX Mod X)
Raw
/*
 * Armor Loan (olds.ro workshop card AMX-066)
 *
 * Biohazard: a human offers some of their own armor to a nearby human; the
 * other player accepts or declines in a menu. Armor only moves between the
 * two: the combined total never changes.
 *
 *   say /armorloan <name> <amount>   offer armor (up to olds_armorloan_max)
 *
 * When the offer is accepted everything is checked again: both players
 * connected, alive and human (not zombies), still within olds_armorloan_range
 * units, and the donor still has armor. The amount moved is the smallest of
 * the offer, the donor's armor and the room the recipient has left (armor
 * holds at most 100); if that is 0 nothing moves. A recipient without armor
 * gets a plain vest. An offer lasts 15 seconds; a new offer, a decline, either
 * player leaving or the round ending cancels it.
 *
 * Needs: AMX Mod X 1.10 (cstrike module), ReGameDLL_CS + ReAPI, and
 * Biohazard (library "biohazardf", is_user_zombie()).
 *
 * Cvars: olds_armorloan_range 200 (50-1000), olds_armorloan_max 50 (1-100).
 *
 * Copyright (C) 2026 daemon (olds.ro)
 * SPDX-License-Identifier: GPL-3.0-or-later
 */
#include <amxmodx>
#include <amxmisc>
#include <cstrike>
#include <reapi>

#define PLUGIN  "Armor Loan"
#define VERSION "1.0.0"
#define AUTHOR  "olds.ro"

/* Biohazard API (biohazard.inc), declared here so the plugin compiles without it. */
#pragma reqlib "biohazardf"
native is_user_zombie(id)

const ARMOR_CAP = 100
const Float:OFFER_LIFE = 15.0
new const CONFIRM_SOUND[] = "items/ammopickup2.wav"

new g_offerFrom[MAX_PLAYERS + 1]        // for each recipient: the donor, 0 = no offer
new g_offerAmount[MAX_PLAYERS + 1]
new Float:g_offerUntil[MAX_PLAYERS + 1]
new g_offerSerial[MAX_PLAYERS + 1]      // changes with every new offer, so an old menu cannot accept it
new g_menu[MAX_PLAYERS + 1] = { -1, ... }
new Float:g_nextUse[MAX_PLAYERS + 1]

new g_range
new g_max

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

    bind_pcvar_num(create_cvar("olds_armorloan_range", "200", FCVAR_NONE,
        "Most distance in units between the two players (50-1000).", true, 50.0, true, 1000.0), g_range)
    bind_pcvar_num(create_cvar("olds_armorloan_max", "50", FCVAR_NONE,
        "Most armor in one offer (1-100).", true, 1.0, true, 100.0), g_max)
    AutoExecConfig(true, "olds_armorloan")

    register_clcmd("say", "on_say")
    register_clcmd("say_team", "on_say")

    RegisterHookChain(RG_CSGameRules_RestartRound, "on_restart_round_pre", false)
}

/* ---------- /armorloan ---------- */

public on_say(id)
{
    if (!is_user_connected(id))
        return PLUGIN_CONTINUE
    new text[96]
    read_args(text, charsmax(text))
    remove_quotes(text)
    trim(text)
    if (!equali(text, "/armorloan", 10) || (text[10] && text[10] != ' '))
        return PLUGIN_CONTINUE

    new Float:now = get_gametime()
    if (now < g_nextUse[id])
        return PLUGIN_HANDLED
    g_nextUse[id] = now + 1.0

    // "<name> <amount>": the amount is the last word, the name may have spaces.
    new rest[80], name[64], amountText[8]
    copy(rest, charsmax(rest), text[10])
    trim(rest)
    new space = -1
    for (new i = strlen(rest) - 1; i >= 0; i--)
    {
        if (rest[i] == ' ')
        {
            space = i
            break
        }
    }
    if (space <= 0)
    {
        say_to(id, "ARMORLOAN_USAGE", g_max)
        return PLUGIN_HANDLED
    }
    copy(amountText, charsmax(amountText), rest[space + 1])
    copy(name, min(space, charsmax(name)), rest)
    trim(name)
    if (!is_digits(amountText) || !name[0])
    {
        say_to(id, "ARMORLOAN_USAGE", g_max)
        return PLUGIN_HANDLED
    }
    offer(id, name, str_to_num(amountText))
    return PLUGIN_HANDLED
}

bool:is_digits(const text[])
{
    if (!text[0] || strlen(text) > 3)
        return false
    for (new i = 0; text[i]; i++)
        if (!isdigit(text[i]))
            return false
    return true
}

bool:is_human(id)
{
    return is_user_alive(id) && !is_user_zombie(id)
}

bool:near(a, b)
{
    new Float:originA[3], Float:originB[3]
    get_entvar(a, var_origin, originA)
    get_entvar(b, var_origin, originB)
    return get_distance_f(originA, originB) <= float(g_range)
}

offer(id, const name[], amount)
{
    if (amount < 1 || amount > g_max)
    {
        say_to(id, "ARMORLOAN_AMOUNT", g_max)
        return
    }
    new target = find_player_ex(FindPlayer_MatchNameSubstring | FindPlayer_CaseInsensitive | FindPlayer_ExcludeBots, name)
    if (!target)
        target = find_player_ex(FindPlayer_MatchNameSubstring | FindPlayer_CaseInsensitive, name)
    if (!target || target == id)
    {
        say_to(id, "ARMORLOAN_NO_TARGET")
        return
    }
    if (!is_human(id) || !is_human(target))
    {
        say_to(id, "ARMORLOAN_HUMANS_ONLY")
        return
    }
    new CsArmorType:type
    if (cs_get_user_armor(id, type) < 1)
    {
        say_to(id, "ARMORLOAN_NO_ARMOR")
        return
    }
    if (!near(id, target))
    {
        say_to(id, "ARMORLOAN_TOO_FAR", g_range)
        return
    }

    g_offerFrom[target] = id
    g_offerAmount[target] = amount
    g_offerUntil[target] = get_gametime() + OFFER_LIFE
    g_offerSerial[target]++
    show_offer(target)

    new targetName[MAX_NAME_LENGTH]
    get_user_name(target, targetName, charsmax(targetName))
    say_to(id, "ARMORLOAN_SENT", amount, targetName)
}

/* ---------- the recipient's menu ---------- */

show_offer(target)
{
    close_menu(target)
    new donorName[MAX_NAME_LENGTH], title[128], item[48], info[12]
    get_user_name(g_offerFrom[target], donorName, charsmax(donorName))
    formatex(title, charsmax(title), "%L", target, "ARMORLOAN_MENU_TITLE", donorName, g_offerAmount[target])
    new menu = menu_create(title, "menu_handler")
    num_to_str(g_offerSerial[target], info, charsmax(info))
    formatex(item, charsmax(item), "%L", target, "ARMORLOAN_ACCEPT")
    menu_additem(menu, item, info)
    formatex(item, charsmax(item), "%L", target, "ARMORLOAN_DECLINE")
    menu_additem(menu, item, info)
    menu_setprop(menu, MPROP_EXIT, MEXIT_NEVER)
    g_menu[target] = menu
    menu_display(target, menu, 0, floatround(OFFER_LIFE))
}

close_menu(id)
{
    new menu = g_menu[id]
    if (menu < 0)
        return
    if (is_user_connected(id))
    {
        new oldMenu, newMenu
        player_menu_info(id, oldMenu, newMenu)
        if (newMenu == menu)
            menu_cancel(id)
    }
    if (g_menu[id] == menu)
    {
        g_menu[id] = -1
        menu_destroy(menu)
    }
}

public menu_handler(target, menu, item)
{
    new info[12], access, callback
    if (item >= 0)
        menu_item_getinfo(menu, item, access, info, charsmax(info), _, _, callback)
    if (g_menu[target] == menu)
        g_menu[target] = -1
    menu_destroy(menu)

    if (item < 0 || str_to_num(info) != g_offerSerial[target] || !g_offerFrom[target])
        return PLUGIN_HANDLED
    new donor = g_offerFrom[target], amount = g_offerAmount[target]
    new bool:expired = get_gametime() > g_offerUntil[target]
    clear_offer(target)
    if (expired)
    {
        say_to(target, "ARMORLOAN_EXPIRED")
        return PLUGIN_HANDLED
    }
    if (item == 1)
    {
        if (is_user_connected(donor))
            say_to(donor, "ARMORLOAN_DECLINED")
        return PLUGIN_HANDLED
    }
    accept(target, donor, amount)
    return PLUGIN_HANDLED
}

accept(target, donor, amount)
{
    if (!is_user_connected(donor) || !is_human(donor) || !is_human(target))
    {
        say_to(target, "ARMORLOAN_FAILED_HUMANS")
        if (is_user_connected(donor))
            say_to(donor, "ARMORLOAN_FAILED_HUMANS")
        return
    }
    if (!near(donor, target))
    {
        say_to(target, "ARMORLOAN_TOO_FAR", g_range)
        say_to(donor, "ARMORLOAN_TOO_FAR", g_range)
        return
    }
    new CsArmorType:donorType, CsArmorType:targetType
    new donorArmor = cs_get_user_armor(donor, donorType)
    new targetArmor = cs_get_user_armor(target, targetType)
    new moved = min(amount, min(donorArmor, ARMOR_CAP - targetArmor))
    if (moved <= 0)
    {
        say_to(target, "ARMORLOAN_NOTHING")
        say_to(donor, "ARMORLOAN_NOTHING")
        return
    }

    // One step: what the donor loses, the recipient gains.
    cs_set_user_armor(donor, donorArmor - moved, donorArmor - moved > 0 ? donorType : CS_ARMOR_NONE)
    cs_set_user_armor(target, targetArmor + moved, targetType == CS_ARMOR_NONE ? CS_ARMOR_KEVLAR : targetType)
    rg_send_audio(donor, CONFIRM_SOUND)
    rg_send_audio(target, CONFIRM_SOUND)

    new donorName[MAX_NAME_LENGTH], targetName[MAX_NAME_LENGTH]
    get_user_name(donor, donorName, charsmax(donorName))
    get_user_name(target, targetName, charsmax(targetName))
    say_to(donor, "ARMORLOAN_GAVE", moved, targetName, donorArmor - moved)
    say_to(target, "ARMORLOAN_GOT", moved, donorName, targetArmor + moved)
    log_amx("Armor loan: %s gave %d armor to %s (%d+%d before, %d+%d after)", donorName, moved, targetName,
        donorArmor, targetArmor, donorArmor - moved, targetArmor + moved)
}

clear_offer(target)
{
    g_offerFrom[target] = 0
    g_offerAmount[target] = 0
    g_offerUntil[target] = 0.0
    g_offerSerial[target]++
}

/* ---------- cancelling ---------- */

public on_restart_round_pre()
{
    for (new id = 1; id <= MaxClients; id++)
    {
        if (g_offerFrom[id])
            clear_offer(id)
        close_menu(id)
    }
    return HC_CONTINUE
}

public client_disconnected(id)
{
    close_menu(id)
    clear_offer(id)
    // Offers this player made to others end too.
    for (new target = 1; target <= MaxClients; target++)
    {
        if (g_offerFrom[target] != id)
            continue
        clear_offer(target)
        close_menu(target)
    }
    g_nextUse[id] = 0.0
}

say_to(id, const key[], any:...)
{
    new pattern[160], message[192]
    formatex(pattern, charsmax(pattern), "%L", id, key)
    vformat(message, charsmax(message), pattern, 3)
    client_print_color(id, print_team_default, "^4[Armor Loan]^1 %s", message)
}

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.