Skip to content
View in the app

A better way to browse. Learn more.

Learn more

vip-objective-balance/addons/amxmodx/scripting/olds_vipbalance.sma

olds_vipbalance.sma
234 lines · 7.9 KB · Pawn (AMX Mod X)
Raw
/*
 * VIP Objective Balance (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/>.
 *
 * VIP Escape: counts each side's wins in a row (RG_RoundEnd). When one side has won
 * olds_vipb_streak rounds in a row, the next round starts balanced: every player of the
 * losing side gets olds_vipb_bonus extra money (cs_set_user_money), and with olds_vipb_swap 1
 * the winning side's top scorer (never the VIP) moves to the losing side (rg_set_user_team)
 * with a notice naming them. The streak then starts again from zero. Nothing is given or
 * moved with fewer than olds_vipb_minplayers players on the teams. Only maps with a VIP
 * (func_vip_safetyzone or info_vip_start) are balanced; a new map starts every count at zero.
 *
 * Cvars: olds_vipb_streak 4, olds_vipb_bonus 1500, olds_vipb_swap 1, olds_vipb_minplayers 6
 * Admin: amx_vipb_status (ADMIN_KICK)
 */

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

#pragma semicolon 0

#define PLUGIN  "VIP Objective Balance"
#define VERSION "1.0.0"
#define AUTHOR  "daemon (olds.ro)"

#define MAX_MONEY 16000

new g_streakLimit
new g_bonus
new g_swap
new g_minPlayers

new bool:g_vipMap
new CsTeams:g_streakTeam
new g_streak
new CsTeams:g_pendingLoser
new g_balances
new g_hudSync

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

    bind_pcvar_num(create_cvar("olds_vipb_streak", "4", FCVAR_NONE,
        "Wins in a row by one side before the next round is balanced", true, 2.0, true, 20.0), g_streakLimit)
    bind_pcvar_num(create_cvar("olds_vipb_bonus", "1500", FCVAR_NONE,
        "Extra money for every player of the losing side", true, 0.0, true, 16000.0), g_bonus)
    bind_pcvar_num(create_cvar("olds_vipb_swap", "1", FCVAR_NONE,
        "1 = also move the winning side's top scorer to the losing side", true, 0.0, true, 1.0), g_swap)
    bind_pcvar_num(create_cvar("olds_vipb_minplayers", "6", FCVAR_NONE,
        "Fewest players on the teams for any balancing", true, 2.0, true, 32.0), g_minPlayers)
    AutoExecConfig(true, "olds_vipbalance")

    RegisterHookChain(RG_RoundEnd, "on_round_end_post", true)
    RegisterHookChain(RG_CSGameRules_RestartRound, "on_restart_pre", false)
    RegisterHookChain(RG_CSGameRules_RestartRound, "on_restart_post", true)
    g_hudSync = CreateHudSyncObj()
    register_concmd("amx_vipb_status", "cmd_status", ADMIN_KICK, "- shows the win streak")
}

public plugin_cfg()
{
    g_vipMap = engfunc(EngFunc_FindEntityByString, MaxClients, "classname", "func_vip_safetyzone") > 0
        || engfunc(EngFunc_FindEntityByString, MaxClients, "classname", "info_vip_start") > 0
    if (!g_vipMap)
        log_amx("No VIP on this map: rounds are not balanced here")
}

team_players()
{
    new count
    for (new i = 1; i <= MaxClients; i++)
    {
        if (!is_user_connected(i))
            continue
        new CsTeams:team = cs_get_user_team(i)
        if (team == CS_TEAM_T || team == CS_TEAM_CT)
            count++
    }
    return count
}

team_name(CsTeams:team, out[], len)
{
    if (team == CS_TEAM_T)
        copy(out, len, "T")
    else
        copy(out, len, "CT")
}

/* ------------------------------------------------------------ counting */

public on_round_end_post(WinStatus:status, ScenarioEventEndRound:event, Float:delay)
{
    if (!g_vipMap)
        return HC_CONTINUE
    new CsTeams:winner
    if (status == WINSTATUS_CTS)
        winner = CS_TEAM_CT
    else if (status == WINSTATUS_TERRORISTS)
        winner = CS_TEAM_T
    else
        return HC_CONTINUE
    if (winner == g_streakTeam)
        g_streak++
    else
    {
        g_streakTeam = winner
        g_streak = 1
        /* A win by the other side ends a streak and any balance it had earned. */
        g_pendingLoser = CS_TEAM_UNASSIGNED
    }
    new label[4]
    team_name(winner, label, charsmax(label))
    log_amx("%s win, streak %d/%d", label, g_streak, g_streakLimit)
    if (g_streak >= g_streakLimit)
    {
        if (winner == CS_TEAM_CT)
            g_pendingLoser = CS_TEAM_T
        else
            g_pendingLoser = CS_TEAM_CT
        g_streak = 0
        log_amx("%s won %d rounds in a row: the next round is balanced", label, g_streakLimit)
    }
    return HC_CONTINUE
}

/* ------------------------------------------------------------ balancing */

public on_restart_pre()
{
    if (g_pendingLoser == CS_TEAM_UNASSIGNED || !g_swap)
        return HC_CONTINUE
    if (team_players() < g_minPlayers)
        return HC_CONTINUE
    /* Moved before the players respawn, so the player starts the round on the new side. */
    new CsTeams:winners = CS_TEAM_T
    if (g_pendingLoser == CS_TEAM_T)
        winners = CS_TEAM_CT
    new best, bestFrags = -99999
    for (new i = 1; i <= MaxClients; i++)
    {
        if (!is_user_connected(i) || cs_get_user_team(i) != winners || cs_get_user_vip(i))
            continue
        new frags = get_user_frags(i)
        if (frags > bestFrags)
        {
            bestFrags = frags
            best = i
        }
    }
    if (!best)
        return HC_CONTINUE
    new name[MAX_NAME_LENGTH], to[4]
    get_user_name(best, name, charsmax(name))
    team_name(g_pendingLoser, to, charsmax(to))
    if (g_pendingLoser == CS_TEAM_T)
        rg_set_user_team(best, TEAM_TERRORIST, MODEL_AUTO, true)
    else
        rg_set_user_team(best, TEAM_CT, MODEL_AUTO, true)
    for (new i = 1; i <= MaxClients; i++)
    {
        if (is_user_connected(i))
            client_print_color(i, print_team_default, "%L", i, "VIPB_SWAP", name, to)
    }
    log_amx("Balance: moved %s (%d frags) to %s", name, bestFrags, to)
    return HC_CONTINUE
}

public on_restart_post()
{
    if (g_pendingLoser == CS_TEAM_UNASSIGNED)
        return HC_CONTINUE
    new CsTeams:loser = g_pendingLoser
    g_pendingLoser = CS_TEAM_UNASSIGNED
    new players = team_players()
    if (players < g_minPlayers)
    {
        log_amx("Balance skipped: %d players on the teams, %d needed", players, g_minPlayers)
        return HC_CONTINUE
    }
    g_balances++
    new label[4], paid
    team_name(loser, label, charsmax(label))
    new bonus = clamp(g_bonus, 0, MAX_MONEY)
    for (new i = 1; i <= MaxClients; i++)
    {
        if (!is_user_connected(i) || cs_get_user_team(i) != loser)
            continue
        if (bonus > 0)
        {
            cs_set_user_money(i, min(cs_get_user_money(i) + bonus, MAX_MONEY))
            paid++
        }
        client_print_color(i, print_team_default, "%L", i, "VIPB_BONUS", bonus)
    }
    set_hudmessage(80, 200, 255, -1.0, 0.25, 0, 0.0, 4.0, 0.2, 0.5, -1)
    for (new i = 1; i <= MaxClients; i++)
    {
        if (is_user_connected(i) && !is_user_bot(i))
            ShowSyncHudMsg(i, g_hudSync, "%L", i, "VIPB_HUD", label, bonus)
    }
    log_amx("Balance: $%d extra to %d %s players", bonus, paid, label)
    return HC_CONTINUE
}

public cmd_status(id, level, cid)
{
    if (!cmd_access(id, level, cid, 1))
        return PLUGIN_HANDLED
    new label[4], pending[4]
    team_name(g_streakTeam, label, charsmax(label))
    if (g_pendingLoser == CS_TEAM_UNASSIGNED)
        copy(pending, charsmax(pending), "-")
    else
        team_name(g_pendingLoser, pending, charsmax(pending))
    if (!g_streak)
        copy(label, charsmax(label), "-")
    console_print(id, "VIP balance: VIP map %d, streak %s %d/%d, balance pending for %s, balances %d, players %d (need %d)",
        g_vipMap ? 1 : 0, label, g_streak, g_streakLimit, pending, g_balances, team_players(), g_minPlayers)
    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.