Skip to content
View in the app

A better way to browse. Learn more.

Learn more

return-fund/addons/amxmodx/scripting/olds_returnfund.sma

olds_returnfund.sma
357 lines · 10.1 KB · Pawn (AMX Mod X)
Raw
/*
 * Return Fund (olds.ro workshop card AMX-076)
 *
 * Capture the Flag: teammates pledge adrenaline toward one reward. When a
 * teammate genuinely returns the team's dropped flag (picks it up and sends
 * it home), the whole fund is paid once to that player as adrenaline and the
 * adrenaline shop opens for them. An automatic return, an admin return, a
 * round restart or a team change gives the pledges back instead.
 *
 *   say /pledge <amount>   add to your pledge (your total up to olds_returnfund_max)
 *   say /pledge            your pledge and your team's fund
 *
 * Needs: AMX Mod X 1.10, ReGameDLL_CS + ReAPI, and jCTF by Digi with the
 * adrenaline shop (library "jctf"; github.com/OsweRRR/jCTF-by-Digi):
 *   - get_user_adrenaline / set_user_adrenaline: the pledge is taken from the
 *     player when made (held here, per player) so it cannot be spent twice;
 *   - the jctf_flag forward: FLAG_RETURNED for the returning player (not the
 *     assists, which arrive with bAssist set) settles the fund; FLAG_AUTORETURN
 *     and FLAG_ADMINRETURN refund it;
 *   - display_shop: the shop native, opened for the returner after payment.
 * A fund is paid at most once: it is emptied before anything is paid, so a
 * repeated or late callback finds nothing to pay. Adrenaline the returner
 * cannot hold (the core keeps at most 200) goes back to the pledgers.
 * A player who leaves loses their pledge from the fund (nobody is paid it).
 *
 * Teams: red = terrorists, blue = counter-terrorists, as in the core.
 *
 * Cvar: olds_returnfund_max 25 (1-200). Command: olds_returnfund_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  "Return Fund"
#define VERSION "1.0.0"
#define AUTHOR  "olds.ro"

/* jCTF API (jctf.inc), declared here so the plugin compiles without it. */
#pragma reqlib "jctf"
native get_user_adrenaline(id)
native set_user_adrenaline(id, amount)
native display_shop(id)

/* jctf_flag events, in the core's order. */
enum
{
    FLAG_STOLEN = 0,
    FLAG_PICKED,
    FLAG_DROPPED,
    FLAG_MANUALDROP,
    FLAG_RETURNED,
    FLAG_CAPTURED,
    FLAG_AUTORETURN,
    FLAG_ADMINRETURN
}

const CTF_RED = 1
const CTF_BLUE = 2
const ADRENALINE_LIMIT = 200
const TASK_TEAMS = 7611

new g_pledge[MAX_PLAYERS + 1]     // adrenaline held for this player
new g_pledgeTeam[MAX_PLAYERS + 1] // the team the pledge is for (1 red, 2 blue)
new Float:g_nextUse[MAX_PLAYERS + 1]
new g_max

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

    bind_pcvar_num(create_cvar("olds_returnfund_max", "25", FCVAR_NONE,
        "Most adrenaline one player may pledge to the return fund (1-200).", true, 1.0, true, 200.0), g_max)
    AutoExecConfig(true, "olds_returnfund")

    register_clcmd("say", "on_say")
    register_clcmd("say_team", "on_say")
    register_concmd("olds_returnfund_status", "cmd_status", ADMIN_CFG, "- every pledge and both funds")

    RegisterHookChain(RG_CSGameRules_RestartRound, "on_restart_round_pre", false)
    set_task(1.0, "task_teams", TASK_TEAMS, _, _, "b")
}

/* ---------- teams ---------- */

team_of(id)
{
    switch (TeamName:get_member(id, m_iTeam))
    {
        case TEAM_TERRORIST: return CTF_RED
        case TEAM_CT: return CTF_BLUE
    }
    return 0
}

fund_of(team)
{
    new total
    for (new id = 1; id <= MaxClients; id++)
        if (g_pledge[id] && g_pledgeTeam[id] == team)
            total += g_pledge[id]
    return total
}

/* A player who changed team gets their pledge back (checked every second). */
public task_teams()
{
    for (new id = 1; id <= MaxClients; id++)
        if (g_pledge[id] && is_user_connected(id) && team_of(id) != g_pledgeTeam[id])
            refund(id, "FUND_REFUND_TEAM")
}

/* ---------- /pledge ---------- */

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

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

    new arg[16]
    copy(arg, charsmax(arg), text[7])
    trim(arg)
    if (!arg[0])
        show_pledge(id)
    else
        pledge(id, arg)
    return PLUGIN_HANDLED
}

show_pledge(id)
{
    new team = team_of(id)
    if (!team)
    {
        say_to(id, "FUND_NO_TEAM")
        return
    }
    say_to(id, "FUND_SHOW", g_pledge[id], g_max, fund_of(team), get_user_adrenaline(id))
}

pledge(id, const arg[])
{
    new team = team_of(id)
    if (!team)
    {
        say_to(id, "FUND_NO_TEAM")
        return
    }
    if (!is_number(arg) || strlen(arg) > 3)
    {
        say_to(id, "FUND_USAGE", g_max)
        return
    }
    new amount = str_to_num(arg)
    if (amount < 1)
    {
        say_to(id, "FUND_USAGE", g_max)
        return
    }
    if (g_pledge[id] + amount > g_max)
    {
        say_to(id, "FUND_OVER_MAX", g_max, g_pledge[id])
        return
    }
    new balance = get_user_adrenaline(id)
    if (amount > balance)
    {
        say_to(id, "FUND_BALANCE", amount, balance)
        return
    }

    set_user_adrenaline(id, balance - amount)
    g_pledge[id] += amount
    g_pledgeTeam[id] = team

    new name[MAX_NAME_LENGTH], total = fund_of(team)
    get_user_name(id, name, charsmax(name))
    tell_team(team, "FUND_PLEDGED", name, amount, total)
    hud_team(team, total)
}

bool:is_number(const text[])
{
    if (!text[0])
        return false
    for (new i = 0; text[i]; i++)
        if (!isdigit(text[i]))
            return false
    return true
}

/* ---------- flag events ---------- */

public jctf_flag(event, player, flagTeam, bool:assist)
{
    if (!(CTF_RED <= flagTeam <= CTF_BLUE))
        return
    switch (event)
    {
        case FLAG_RETURNED:
        {
            // Only the returner, not the assists, and only by a teammate of the flag.
            if (!assist && 1 <= player <= MaxClients && is_user_connected(player) && team_of(player) == flagTeam)
                settle(flagTeam, player)
        }
        case FLAG_AUTORETURN, FLAG_ADMINRETURN: refund_team(flagTeam, "FUND_REFUND_RETURN")
    }
}

settle(team, returner)
{
    // Empty the fund first, so nothing can be paid twice.
    new from[MAX_PLAYERS], took[MAX_PLAYERS], count, total
    for (new id = 1; id <= MaxClients; id++)
    {
        if (!g_pledge[id] || g_pledgeTeam[id] != team)
            continue
        from[count] = id
        took[count] = g_pledge[id]
        total += g_pledge[id]
        count++
        g_pledge[id] = 0
        g_pledgeTeam[id] = 0
    }
    if (!total)
        return

    new balance = get_user_adrenaline(returner)
    new paid = clamp(ADRENALINE_LIMIT - balance, 0, total)
    set_user_adrenaline(returner, balance + paid)

    // What the returner cannot hold goes back, last pledger first.
    new back = total - paid
    for (new i = count - 1; i >= 0 && back > 0; i--)
    {
        new part = min(back, took[i])
        back -= part
        if (is_user_connected(from[i]))
            set_user_adrenaline(from[i], get_user_adrenaline(from[i]) + part)
    }

    new name[MAX_NAME_LENGTH]
    get_user_name(returner, name, charsmax(name))
    tell_team(team, "FUND_PAID", name, paid, count)
    if (paid < total)
        tell_team(team, "FUND_PAID_BACK", total - paid)
    set_dhudmessage(255, 210, 60, -1.0, 0.25, 0, 0.0, 4.0, 0.2, 0.6)
    show_dhudmessage(returner, "%L", returner, "FUND_HUD_PAID", paid)
    display_shop(returner)
}

refund(id, const key[])
{
    new amount = g_pledge[id]
    g_pledge[id] = 0
    g_pledgeTeam[id] = 0
    if (!amount || !is_user_connected(id))
        return
    set_user_adrenaline(id, get_user_adrenaline(id) + amount)
    say_to(id, key, amount)
}

refund_team(team, const key[])
{
    new bool:refunded
    for (new id = 1; id <= MaxClients; id++)
    {
        if (g_pledge[id] && g_pledgeTeam[id] == team)
        {
            refund(id, key)
            refunded = true
        }
    }
    if (refunded)
        hud_team(team, 0)
}

public on_restart_round_pre()
{
    for (new id = 1; id <= MaxClients; id++)
        if (g_pledge[id])
            refund(id, "FUND_REFUND_RESET")
    return HC_CONTINUE
}

public client_disconnected(id)
{
    g_pledge[id] = 0
    g_pledgeTeam[id] = 0
    g_nextUse[id] = 0.0
}

/* ---------- messages ---------- */

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[Fund]^1 %s", message)
}

tell_team(team, const key[], any:...)
{
    new players[MAX_PLAYERS], count, pattern[160], message[192]
    get_players_ex(players, count, GetPlayers_ExcludeBots | GetPlayers_ExcludeHLTV)
    for (new i = 0; i < count; i++)
    {
        new id = players[i]
        if (team_of(id) != team)
            continue
        formatex(pattern, charsmax(pattern), "%L", id, key)
        vformat(message, charsmax(message), pattern, 3)
        client_print_color(id, print_team_default, "^4[Fund]^1 %s", message)
    }
}

hud_team(team, total)
{
    new players[MAX_PLAYERS], count
    get_players_ex(players, count, GetPlayers_ExcludeBots | GetPlayers_ExcludeHLTV)
    set_dhudmessage(255, 210, 60, 0.02, 0.62, 0, 0.0, 3.0, 0.2, 0.5)
    for (new i = 0; i < count; i++)
        if (team_of(players[i]) == team)
            show_dhudmessage(players[i], "%L", players[i], "FUND_HUD", total)
}

public cmd_status(id, level, cid)
{
    if (!cmd_access(id, level, cid, 1))
        return PLUGIN_HANDLED
    console_print(id, "[Fund] red %d, blue %d; up to %d per player", fund_of(CTF_RED), fund_of(CTF_BLUE), g_max)
    new name[MAX_NAME_LENGTH]
    for (new p = 1; p <= MaxClients; p++)
    {
        if (!g_pledge[p] || !is_user_connected(p))
            continue
        get_user_name(p, name, charsmax(name))
        console_print(id, "[Fund]   %s: %d for %s (adrenaline %d)", name, g_pledge[p],
            g_pledgeTeam[p] == CTF_RED ? "red" : "blue", get_user_adrenaline(p))
    }
    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.