Skip to content
View in the app

A better way to browse. Learn more.

Learn more

gungame-catch-up-handicap/addons/amxmodx/scripting/olds_gghcap.sma

olds_gghcap.sma
169 lines · 5.6 KB · Pawn (AMX Mod X)
Raw
/*
 * GunGame Catch-up Handicap (olds.ro workshop card IDEA-16FF798A8D571AF8)
 * Copyright (C) 2026 daemon (olds.ro)
 *
 * 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/>.
 *
 * At every spawn (the first spawn is the join) a player whose GunGame level is more than
 * olds_gghcap_gap levels below the server average is put at the average minus one
 * (rounded down), through the GunGame core's gg_set_level native. The average counts the
 * other players on a team, without bots (unless olds_gghcap_bots 1). The new level is never
 * the leader's level or above (at most the leader minus one) and never the last (knife)
 * level. The core is library "gungame" with gg_get_level(id), gg_set_level(id, level) and
 * gg_get_max_level(); without them the plugin does nothing.
 */
#include <amxmodx>
#include <amxmisc>
#include <reapi>

#define PLUGIN  "GunGame Catch-up Handicap"
#define VERSION "1.0.0"
#define AUTHOR  "daemon (olds.ro)"

#define TASK_CHECK 4400

native gg_get_level(id)
native gg_set_level(id, level)
native gg_get_max_level()

new g_on, g_gap, g_bots
new bool:g_noCore
new g_lifts

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

public native_filter(const name[], index, trap)
{
    if (!trap)
    {
        g_noCore = true
        return PLUGIN_HANDLED
    }
    return PLUGIN_CONTINUE
}

public plugin_init()
{
    register_plugin(PLUGIN, VERSION, AUTHOR)
    register_dictionary("olds_gghcap.txt")
    bind_pcvar_num(create_cvar("olds_gghcap", "1", FCVAR_NONE, "1 = players far behind start at the average level minus one", true, 0.0, true, 1.0), g_on)
    bind_pcvar_num(create_cvar("olds_gghcap_gap", "4", FCVAR_NONE, "Levels below the average before the handicap applies", true, 1.0, true, 50.0), g_gap)
    bind_pcvar_num(create_cvar("olds_gghcap_bots", "0", FCVAR_NONE, "1 = bots count in the average (for testing)", true, 0.0, true, 1.0), g_bots)
    AutoExecConfig(true, "olds_gghcap")
    register_concmd("amx_gghcap_status", "cmd_status", ADMIN_KICK, "- levels, the average and the handicap target")
    RegisterHookChain(RG_CBasePlayer_Spawn, "on_spawn_post", true)
    if (g_noCore)
        log_amx("The GunGame core natives (gg_get_level, gg_set_level, gg_get_max_level) are missing: the handicap is off")
}

bool:on_team(id)
{
    new TeamName:team = TeamName:get_member(id, m_iTeam)
    return team == TEAM_TERRORIST || team == TEAM_CT
}

/* The average level of the other counted players and the highest level among them. Returns
   how many were counted. */
average_of_others(id, &Float:average, &leader)
{
    new sum, n
    leader = 0
    for (new p = 1; p <= MaxClients; p++)
    {
        if (p == id || !is_user_connected(p) || !on_team(p))
            continue
        if (!g_bots && is_user_bot(p))
            continue
        new level = gg_get_level(p)
        sum += level
        n++
        if (level > leader)
            leader = level
    }
    average = 0.0
    if (n)
        average = float(sum) / float(n)
    return n
}

/* The level the player would be lifted to now, or 0 for none. */
handicap_target(id)
{
    if (g_noCore || !g_on || !is_user_connected(id))
        return 0
    new Float:average, leader
    if (!average_of_others(id, average, leader))
        return 0
    new level = gg_get_level(id)
    if (average - float(level) <= float(g_gap))
        return 0
    new target = floatround(average, floatround_floor) - 1
    if (target > leader - 1)
        target = leader - 1
    new last = gg_get_max_level()
    if (last > 0 && target > last - 1)
        target = last - 1
    if (target <= level)
        return 0
    return target
}

public on_spawn_post(id)
{
    if (!is_user_alive(id) || g_noCore || !g_on)
        return
    /* A moment later, after the core has handled the spawn itself. */
    remove_task(TASK_CHECK + id)
    set_task(0.2, "check_player", TASK_CHECK + id)
}

public check_player(taskid)
{
    new id = taskid - TASK_CHECK
    new target = handicap_target(id)
    if (!target)
        return
    new old = gg_get_level(id)
    gg_set_level(id, target)
    g_lifts++
    new name[MAX_NAME_LENGTH]
    get_user_name(id, name, charsmax(name))
    client_print(id, print_chat, "%L", id, "GGH_LIFTED", target)
    log_amx("Catch-up: %s from level %d to %d", name, old, target)
}

public client_disconnected(id)
{
    remove_task(TASK_CHECK + id)
}

public cmd_status(id, level, cid)
{
    if (!cmd_access(id, level, cid, 1))
        return PLUGIN_HANDLED
    if (g_noCore)
    {
        console_print(id, "[GGH] the GunGame core natives are missing")
        return PLUGIN_HANDLED
    }
    console_print(id, "[GGH] on %d, gap %d, bots counted %d, last level %d, lifts %d", g_on, g_gap, g_bots, gg_get_max_level(), g_lifts)
    new name[MAX_NAME_LENGTH], Float:average, leader
    for (new p = 1; p <= MaxClients; p++)
    {
        if (!is_user_connected(p))
            continue
        get_user_name(p, name, charsmax(name))
        new counted = average_of_others(p, average, leader)
        console_print(id, "[GGH]   %s: level %d, bot %d, others counted %d, their average %.2f, leader %d, target now %d", name,
            gg_get_level(p), is_user_bot(p), counted, average, leader, handicap_target(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.