Skip to content
View in the app

A better way to browse. Learn more.

Learn more

reserve-runner/addons/amxmodx/scripting/olds_lifegift.sma

olds_lifegift.sma
340 lines · 10 KB · Pawn (AMX Mod X)
Raw
/*
 * Reserve Runner (olds.ro workshop card AMX-040)
 *
 * Deathrun: a runner with spare lives can give one to a teammate who has been
 * eliminated. The teammate gets a yes/no menu; only a yes moves the life, and
 * the teammate respawns at once.
 *
 *   say /gift <name>    offer one of your lives to a dead runner
 *
 * Needs: AMX Mod X 1.10, ReGameDLL_CS + ReAPI, and Deathrun Mod by Mistrick
 * (github.com/Mistrick/DeathrunMod) with its lives plugin deathrun_lifes
 * (natives dr_get_lifes / dr_set_lifes) and core (dr_get_terrorist).
 * Runners are counter-terrorists; the activator is the terrorist.
 *
 * The transfer: deathrun_lifes has no single "move a life" native, so the
 * plugin reads both balances, checks them, writes the donor's and then the
 * recipient's in one function call, and reads them back. AMX Mod X runs one
 * plugin call at a time, so nothing can change a balance in between; if the
 * read-back does not match (another plugin refused the change), both balances
 * are put back and the gift is cancelled.
 *
 * Rules: one pending offer per donor and per recipient; the offer lasts
 * olds_lifegift_timeout seconds and is cancelled at round end, on disconnect,
 * or when either player's role changes. The donor needs at least
 * olds_lifegift_min lives when offering and again when the gift is accepted,
 * so they always keep one.
 *
 * Cvars: olds_lifegift_min 2 (2-20), olds_lifegift_timeout 15 (5-60).
 * Command: olds_lifegift_status (ADMIN_CFG).
 *
 * Copyright (C) 2026 daemon (olds.ro)
 * SPDX-License-Identifier: GPL-3.0-or-later
 */
#include <amxmodx>
#include <amxmisc>
#include <reapi>

#define PLUGIN  "Reserve Runner"
#define VERSION "1.0.0"
#define AUTHOR  "olds.ro"

/* Deathrun Mod API (deathrun_lifes.sma, deathrun_core.inc), declared here. */
native dr_get_lifes(id)
native dr_set_lifes(id, lifes)
native dr_get_terrorist()

new g_offerTo[MAX_PLAYERS + 1]          // donor -> recipient of their pending offer
new g_offerFrom[MAX_PLAYERS + 1]        // recipient -> donor
new g_offerMenu[MAX_PLAYERS + 1]        // recipient -> menu handle
new Float:g_nextUse[MAX_PLAYERS + 1]

new g_minLives
new g_timeout

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

    bind_pcvar_num(create_cvar("olds_lifegift_min", "2", FCVAR_NONE,
        "Lives a donor needs to give one away (2-20); they always keep at least one.", true, 2.0, true, 20.0), g_minLives)
    bind_pcvar_num(create_cvar("olds_lifegift_timeout", "15", FCVAR_NONE,
        "Seconds a gift offer stays open (5-60).", true, 5.0, true, 60.0), g_timeout)
    AutoExecConfig(true, "olds_lifegift")

    register_clcmd("say", "on_say")
    register_clcmd("say_team", "on_say")
    register_concmd("olds_lifegift_status", "cmd_status", ADMIN_CFG, "- lives and pending gift offers")

    RegisterHookChain(RG_RoundEnd, "on_round_end_post", true)
    RegisterHookChain(RG_CSGameRules_RestartRound, "on_restart_round_pre", false)
}

/* ---------- roles ---------- */

bool:is_runner(id)
{
    return is_user_connected(id) && TeamName:get_member(id, m_iTeam) == TEAM_CT && id != dr_get_terrorist()
}

/* ---------- /gift ---------- */

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, "/gift", 5) || (text[5] && text[5] != ' '))
        return PLUGIN_CONTINUE

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

    new name[MAX_NAME_LENGTH]
    copy(name, charsmax(name), text[5])
    trim(name)
    offer(id, name)
    return PLUGIN_HANDLED
}

offer(donor, const name[])
{
    if (!name[0])
    {
        say_to(donor, "GIFT_USAGE")
        return
    }
    if (!is_runner(donor) || !is_user_alive(donor))
    {
        say_to(donor, "GIFT_DONOR_NOT_RUNNER")
        return
    }
    new lives = dr_get_lifes(donor)
    if (lives < g_minLives)
    {
        say_to(donor, "GIFT_TOO_FEW", g_minLives, lives)
        return
    }
    if (g_offerTo[donor])
    {
        say_to(donor, "GIFT_ALREADY_OFFERED")
        return
    }

    new target = find_player_ex(FindPlayer_MatchNameSubstring | FindPlayer_CaseInsensitive, name)
    if (!target || target == donor)
    {
        say_to(donor, "GIFT_NO_PLAYER", name)
        return
    }
    if (!is_runner(target))
    {
        say_to(donor, "GIFT_NOT_RUNNER")
        return
    }
    if (is_user_alive(target))
    {
        say_to(donor, "GIFT_TARGET_ALIVE")
        return
    }
    if (g_offerFrom[target])
    {
        say_to(donor, "GIFT_TARGET_BUSY")
        return
    }

    new donorName[MAX_NAME_LENGTH], title[160], yes[48], no[48]
    get_user_name(donor, donorName, charsmax(donorName))
    formatex(title, charsmax(title), "%L", target, "GIFT_MENU_TITLE", donorName, g_timeout)
    formatex(yes, charsmax(yes), "%L", target, "GIFT_MENU_YES")
    formatex(no, charsmax(no), "%L", target, "GIFT_MENU_NO")

    new menu = menu_create(title, "on_gift_answer")
    menu_additem(menu, yes, "1")
    menu_additem(menu, no, "0")
    menu_setprop(menu, MPROP_EXIT, MEXIT_NEVER)

    g_offerTo[donor] = target
    g_offerFrom[target] = donor
    g_offerMenu[target] = menu
    menu_display(target, menu, 0, g_timeout)

    new targetName[MAX_NAME_LENGTH]
    get_user_name(target, targetName, charsmax(targetName))
    say_to(donor, "GIFT_OFFERED", targetName, g_timeout)
}

public on_gift_answer(target, menu, item)
{
    new bool:current = 1 <= target <= MaxClients && g_offerMenu[target] == menu && g_offerFrom[target]
    new bool:accepted
    if (current && item >= 0)
    {
        new info[4], access, callback
        menu_item_getinfo(menu, item, access, info, charsmax(info), _, _, callback)
        accepted = info[0] == '1'
    }
    menu_destroy(menu)
    if (!current)
        return PLUGIN_HANDLED

    new donor = g_offerFrom[target]
    clear_offer(donor, target)

    if (!accepted)
    {
        say_to(target, item == MENU_TIMEOUT ? "GIFT_EXPIRED" : "GIFT_DECLINED")
        if (is_user_connected(donor))
            say_to(donor, item == MENU_TIMEOUT ? "GIFT_EXPIRED" : "GIFT_REFUSED")
        return PLUGIN_HANDLED
    }
    transfer(donor, target)
    return PLUGIN_HANDLED
}

/* Checks again, then moves one life in one go; undoes it if the lives plugin did not take it. */
transfer(donor, target)
{
    if (get_member_game(m_bRoundTerminating))
    {
        say_both(donor, target, "GIFT_ROUND_OVER")
        return
    }
    if (!is_runner(donor) || !is_user_alive(donor) || !is_runner(target) || is_user_alive(target))
    {
        say_both(donor, target, "GIFT_ROLES_CHANGED")
        return
    }
    new donorLives = dr_get_lifes(donor), targetLives = dr_get_lifes(target)
    if (donorLives < g_minLives)
    {
        say_both(donor, target, "GIFT_DONOR_SHORT")
        return
    }

    dr_set_lifes(donor, donorLives - 1)
    dr_set_lifes(target, targetLives + 1)
    if (dr_get_lifes(donor) != donorLives - 1 || dr_get_lifes(target) != targetLives + 1)
    {
        dr_set_lifes(donor, donorLives)
        dr_set_lifes(target, targetLives)
        say_both(donor, target, "GIFT_REJECTED")
        return
    }

    rg_round_respawn(target)

    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, "GIFT_SENT", targetName, donorLives - 1)
    say_to(target, "GIFT_RECEIVED", donorName, targetLives + 1)
}

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

clear_offer(donor, target)
{
    if (1 <= donor <= MaxClients)
        g_offerTo[donor] = 0
    if (1 <= target <= MaxClients)
    {
        g_offerFrom[target] = 0
        g_offerMenu[target] = 0
    }
}

cancel_all(const key[])
{
    for (new donor = 1; donor <= MaxClients; donor++)
    {
        new target = g_offerTo[donor]
        if (!target)
            continue
        clear_offer(donor, target)
        if (is_user_connected(target))
        {
            menu_cancel(target)
            say_to(target, key)
        }
        if (is_user_connected(donor))
            say_to(donor, key)
    }
}

public on_round_end_post(WinStatus:status, ScenarioEventEndRound:event, Float:delay)
{
    if (get_member_game(m_bRoundTerminating))
        cancel_all("GIFT_ROUND_OVER")
    return HC_CONTINUE
}

public on_restart_round_pre()
{
    cancel_all("GIFT_ROUND_OVER")
    return HC_CONTINUE
}

public client_disconnected(id)
{
    new target = g_offerTo[id], donor = g_offerFrom[id]
    if (target)
    {
        clear_offer(id, target)
        if (is_user_connected(target))
        {
            menu_cancel(target)
            say_to(target, "GIFT_LEFT")
        }
    }
    if (donor)
    {
        clear_offer(donor, id)
        if (is_user_connected(donor))
            say_to(donor, "GIFT_LEFT")
    }
    g_nextUse[id] = 0.0
}

/* ---------- helpers ---------- */

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

say_both(donor, target, const key[])
{
    if (is_user_connected(donor))
        say_to(donor, key)
    if (is_user_connected(target))
        say_to(target, key)
}

public cmd_status(id, level, cid)
{
    if (!cmd_access(id, level, cid, 1))
        return PLUGIN_HANDLED
    console_print(id, "[Gift] donors need %d lives; offers last %d s; activator #%d", g_minLives, g_timeout,
        dr_get_terrorist())
    new players[MAX_PLAYERS], count, name[MAX_NAME_LENGTH]
    get_players(players, count, "h")
    for (new i = 0; i < count; i++)
    {
        new p = players[i]
        get_user_name(p, name, charsmax(name))
        console_print(id, "[Gift] %s: %d lives, %s, %s%s", name, dr_get_lifes(p), is_runner(p) ? "runner" : "not a runner",
            is_user_alive(p) ? "alive" : "dead", g_offerTo[p] ? " (offer pending)" : (g_offerFrom[p] ? " (offered a life)" : ""))
    }
    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.