Skip to content
View in the app

A better way to browse. Learn more.

Learn more

gungame-core/addons/amxmodx/scripting/olds_gungame.sma

olds_gungame.sma
391 lines · 12.9 KB · Pawn (AMX Mod X)
Raw
/*
 * GunGame Core (olds.ro workshop card LIB-0838)
 *
 * The base gun game: every kill moves the killer one weapon up a fixed
 * ladder, and the first player to kill with the final weapon wins: the
 * round ends with the winner announced, and everyone starts again at level
 * 1 the next round.
 *
 *   - Weapons: each spawn strips every weapon and gives the knife plus the
 *     level's weapon with full ammo. After a level-up the new weapon comes
 *     on the next frame. Buying and picking weapons up are blocked; an HE
 *     grenade level gets a new grenade a second after each throw.
 *   - Knife kill (when the killer is not on the knife level): the victim
 *     loses olds_gg_knifelevel levels and the killer gains one.
 *   - Suicide (falls, the world, own grenade): one level down when
 *     olds_gg_reset_on_suicide is 1.
 *   - Headshots: RG_CBasePlayer_TakeDamage notes where the killing hit
 *     landed; with olds_gg_hs_bonus 1 a headshot kill is worth two levels
 *     (never past the last level: the win always needs a kill on it).
 *   - Team kills change nothing.
 *
 * Ladder: addons/amxmodx/configs/gungame.ini, one weapon per line in order
 * (glock18, usp, ..., hegrenade, knife; with or without "weapon_"). Unknown
 * names are logged and skipped. Without the file, or with fewer than two
 * valid weapons, a built-in ladder of 25 levels is used.
 *
 * Win level: olds_gg_winlevel 0 = the last level; n = level n wins (a kill
 * with level n's weapon).
 *
 * Sounds: sound/olds/gg/levelup.wav and levelsteal.wav if the server has
 * them, otherwise the stock buttons/bell1.wav.
 *
 * Cvars: olds_gg_knifelevel 1 (0-5), olds_gg_winlevel 0, olds_gg_reset_on_suicide 1,
 * olds_gg_hs_bonus 0. Commands: amx_gg_status, amx_gg_setlevel <name> <level> (ADMIN_CFG).
 *
 * Copyright (C) 2026 daemon (olds.ro)
 * SPDX-License-Identifier: GPL-3.0-or-later
 */
#include <amxmodx>
#include <amxmisc>
#include <reapi>

#define PLUGIN  "GunGame Core"
#define VERSION "1.0.0"
#define AUTHOR  "olds.ro"

const MAX_LEVELS = 40
const WEAPON_NAME = 24
const TASK_GRENADE = 3811

new const DEFAULT_LADDER[][] =
{
    "glock18", "usp", "p228", "deagle", "fiveseven", "elite", "m3", "xm1014", "tmp", "mac10", "mp5navy",
    "ump45", "p90", "galil", "famas", "ak47", "m4a1", "sg552", "aug", "scout", "awp", "g3sg1", "sg550",
    "m249", "hegrenade", "knife"
}
new const LEVEL_UP_SOUND[] = "olds/gg/levelup.wav"
new const LEVEL_STEAL_SOUND[] = "olds/gg/levelsteal.wav"
new const FALLBACK_SOUND[] = "buttons/bell1.wav"

new g_ladder[MAX_LEVELS][WEAPON_NAME]    // "weapon_..." names
new g_levels

new g_level[MAX_PLAYERS + 1]             // 0-based ladder index
new bool:g_headshot[MAX_PLAYERS + 1]     // the last hit taken was to the head
new bool:g_resetNextRound
new bool:g_won

new g_soundUp[48]
new g_soundSteal[48]

new g_knifeLevels
new g_winLevel
new g_resetOnSuicide
new g_hsBonus

public plugin_precache()
{
    // A missing file would stop the server at precache: check first.
    copy(g_soundUp, charsmax(g_soundUp), FALLBACK_SOUND)
    if (file_exists(fmt("sound/%s", LEVEL_UP_SOUND)))
        copy(g_soundUp, charsmax(g_soundUp), LEVEL_UP_SOUND)
    copy(g_soundSteal, charsmax(g_soundSteal), FALLBACK_SOUND)
    if (file_exists(fmt("sound/%s", LEVEL_STEAL_SOUND)))
        copy(g_soundSteal, charsmax(g_soundSteal), LEVEL_STEAL_SOUND)
    precache_sound(g_soundUp)
    if (!equal(g_soundSteal, g_soundUp))
        precache_sound(g_soundSteal)
}

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

    bind_pcvar_num(create_cvar("olds_gg_knifelevel", "1", FCVAR_NONE,
        "Levels a knife kill takes from the victim (0-5, 0 = none).", true, 0.0, true, 5.0), g_knifeLevels)
    bind_pcvar_num(create_cvar("olds_gg_winlevel", "0", FCVAR_NONE,
        "Level whose kill wins; 0 = the last level.", true, 0.0, true, float(MAX_LEVELS)), g_winLevel)
    bind_pcvar_num(create_cvar("olds_gg_reset_on_suicide", "1", FCVAR_NONE,
        "1 = a suicide costs one level.", true, 0.0, true, 1.0), g_resetOnSuicide)
    bind_pcvar_num(create_cvar("olds_gg_hs_bonus", "0", FCVAR_NONE,
        "1 = a headshot kill is worth two levels.", true, 0.0, true, 1.0), g_hsBonus)
    AutoExecConfig(true, "olds_gungame")

    register_concmd("amx_gg_status", "cmd_status", ADMIN_CFG, "- the ladder and every player's level")
    register_concmd("amx_gg_setlevel", "cmd_setlevel", ADMIN_CFG, "<name or #userid> <level> - set a player's level")
    register_clcmd("drop", "cmd_drop")

    RegisterHookChain(RG_CBasePlayer_Spawn, "on_spawn_post", true)
    RegisterHookChain(RG_CBasePlayer_Killed, "on_killed_post", true)
    RegisterHookChain(RG_CBasePlayer_TakeDamage, "on_take_damage_post", true)
    RegisterHookChain(RG_CBasePlayer_HasRestrictItem, "on_restrict_item_pre", false)
    RegisterHookChain(RG_ThrowHeGrenade, "on_throw_he_post", true)
    RegisterHookChain(RG_CSGameRules_RestartRound, "on_restart_round_pre", false)

    load_ladder()
}

/* ---------- the ladder ---------- */

load_ladder()
{
    new path[128]
    get_configsdir(path, charsmax(path))
    add(path, charsmax(path), "/gungame.ini")
    new file = fopen(path, "rt")
    if (file)
    {
        new line[64], number
        while (fgets(file, line, charsmax(line)))
        {
            number++
            trim(line)
            if (!line[0] || line[0] == ';' || (line[0] == '/' && line[1] == '/'))
                continue
            if (g_levels == MAX_LEVELS)
            {
                log_amx("%s: more than %d levels, the rest are ignored", path, MAX_LEVELS)
                break
            }
            if (!add_level(line))
                log_amx("%s line %d: unknown weapon ^"%s^", skipped", path, number, line)
        }
        fclose(file)
    }
    if (g_levels < 2)
    {
        if (file)
            log_amx("%s has fewer than two valid weapons: the built-in ladder is used", path)
        g_levels = 0
        for (new i = 0; i < sizeof(DEFAULT_LADDER); i++)
            add_level(DEFAULT_LADDER[i])
    }
}

bool:add_level(const text[])
{
    new name[WEAPON_NAME]
    if (equali(text, "weapon_", 7))
        copy(name, charsmax(name), text)
    else
        formatex(name, charsmax(name), "weapon_%s", text)
    strtolower(name)
    if (!rg_get_weapon_info(name, WI_ID))
        return false
    copy(g_ladder[g_levels++], charsmax(g_ladder[]), name)
    return true
}

last_level()
{
    return (g_winLevel >= 1 && g_winLevel <= g_levels) ? g_winLevel - 1 : g_levels - 1
}

/* ---------- weapons ---------- */

give_level_weapon(id)
{
    if (!is_user_alive(id))
        return
    rg_remove_all_items(id)
    rg_give_item(id, "weapon_knife")
    new weapon[WEAPON_NAME]
    copy(weapon, charsmax(weapon), g_ladder[g_level[id]])
    if (equal(weapon, "weapon_knife"))
        return
    rg_give_item(id, weapon)
    new WeaponIdType:weaponId = WeaponIdType:rg_get_weapon_info(weapon, WI_ID)
    new maxAmmo = rg_get_weapon_info(weaponId, WI_MAX_ROUNDS)
    if (maxAmmo > 0 && weaponId != WEAPON_HEGRENADE)
        rg_set_user_bpammo(id, weaponId, maxAmmo)
    engclient_cmd(id, weapon)
}

public frame_give(id)
{
    give_level_weapon(id)
}

public on_spawn_post(id)
{
    if (is_user_alive(id))
    {
        g_headshot[id] = false
        give_level_weapon(id)
    }
    return HC_CONTINUE
}

/* Weapons come only from the ladder: no buying, no picking up. */
public on_restrict_item_pre(id, ItemID:item, ItemRestType:type)
{
    if (type == ITEM_TYPE_BUYING || type == ITEM_TYPE_TOUCHED)
    {
        SetHookChainReturn(ATYPE_BOOL, true)
        return HC_SUPERCEDE
    }
    return HC_CONTINUE
}

public cmd_drop(id)
{
    return PLUGIN_HANDLED
}

public on_throw_he_post(id)
{
    if (1 <= id <= MaxClients && equal(g_ladder[g_level[id]], "weapon_hegrenade"))
    {
        remove_task(TASK_GRENADE + id)
        set_task(1.0, "task_grenade", TASK_GRENADE + id)
    }
    return HC_CONTINUE
}

public task_grenade(taskId)
{
    new id = taskId - TASK_GRENADE
    if (is_user_alive(id) && equal(g_ladder[g_level[id]], "weapon_hegrenade") && !user_has_weapon(id, CSW_HEGRENADE))
        rg_give_item(id, "weapon_hegrenade")
}

/* ---------- kills ---------- */

public on_take_damage_post(victim, inflictor, attacker, Float:damage, damageBits)
{
    if (1 <= victim <= MaxClients && is_user_connected(victim))
        g_headshot[victim] = get_member(victim, m_LastHitGroup) == HIT_HEAD
    return HC_CONTINUE
}

public on_killed_post(victim, attacker, gib)
{
    if (g_won || !(1 <= victim <= MaxClients))
        return HC_CONTINUE

    // Suicide: the world, a fall, own grenade, kill command.
    if (attacker == victim || !(1 <= attacker <= MaxClients) || !is_user_connected(attacker))
    {
        if (g_resetOnSuicide && g_level[victim] > 0)
        {
            g_level[victim]--
            say_to(victim, "GG_SUICIDE", g_level[victim] + 1, g_ladder[g_level[victim]][7])
        }
        return HC_CONTINUE
    }
    if (get_member(attacker, m_iTeam) == get_member(victim, m_iTeam))
        return HC_CONTINUE

    new active = get_member(attacker, m_pActiveItem)
    new bool:knife = active > 0 && WeaponIdType:get_member(active, m_iId) == WEAPON_KNIFE
    new bool:knifeLevel = equal(g_ladder[g_level[attacker]], "weapon_knife")

    // A kill on the winning level wins, whatever the weapon in hand.
    if (g_level[attacker] >= last_level())
    {
        win(attacker)
        return HC_CONTINUE
    }

    if (knife && !knifeLevel && g_knifeLevels > 0)
    {
        new lost = min(g_knifeLevels, g_level[victim])
        g_level[victim] -= lost
        if (lost)
        {
            say_to(victim, "GG_STOLEN", lost, g_level[victim] + 1, g_ladder[g_level[victim]][7])
            emit_sound(victim, CHAN_ITEM, g_soundSteal, VOL_NORM, ATTN_NORM, 0, PITCH_NORM)
        }
    }
    new gain = (g_hsBonus && g_headshot[victim]) ? 2 : 1
    g_level[attacker] = min(g_level[attacker] + gain, last_level())
    say_to(attacker, "GG_LEVEL_UP", g_level[attacker] + 1, last_level() + 1, g_ladder[g_level[attacker]][7])
    emit_sound(attacker, CHAN_ITEM, g_soundUp, VOL_NORM, ATTN_NORM, 0, PITCH_NORM)
    RequestFrame("frame_give", attacker)
    return HC_CONTINUE
}

win(id)
{
    g_won = true
    g_resetNextRound = true
    new name[MAX_NAME_LENGTH]
    get_user_name(id, name, charsmax(name))
    for (new p = 1; p <= MaxClients; p++)
        if (is_user_connected(p) && !is_user_bot(p))
            client_print_color(p, id, "^4[GunGame]^1 %L", p, "GG_WINNER", name, g_ladder[last_level()][7])
    log_amx("GunGame: %s won with %s", name, g_ladder[last_level()])
    new TeamName:team = TeamName:get_member(id, m_iTeam)
    rg_round_end(5.0, team == TEAM_CT ? WINSTATUS_CTS : WINSTATUS_TERRORISTS,
        team == TEAM_CT ? ROUND_CTS_WIN : ROUND_TERRORISTS_WIN, fmt("%s wins the GunGame!", name), "default", true)
}

public on_restart_round_pre()
{
    g_won = false
    if (g_resetNextRound)
    {
        g_resetNextRound = false
        for (new id = 1; id <= MaxClients; id++)
            g_level[id] = 0
    }
    return HC_CONTINUE
}

public client_putinserver(id)
{
    g_level[id] = 0
    g_headshot[id] = false
}

public client_disconnected(id)
{
    g_level[id] = 0
    remove_task(TASK_GRENADE + id)
}

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

say_to(id, const key[], any:...)
{
    if (!is_user_connected(id) || is_user_bot(id))
        return
    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[GunGame]^1 %s", message)
}

public cmd_status(id, level, cid)
{
    if (!cmd_access(id, level, cid, 1))
        return PLUGIN_HANDLED
    console_print(id, "[GunGame] %d levels, winning level %d (%s); knife steals %d; suicide costs %d; headshot bonus %d",
        g_levels, last_level() + 1, g_ladder[last_level()], g_knifeLevels, g_resetOnSuicide, g_hsBonus)
    new name[MAX_NAME_LENGTH]
    for (new p = 1; p <= MaxClients; p++)
    {
        if (!is_user_connected(p))
            continue
        get_user_name(p, name, charsmax(name))
        new active = is_user_alive(p) ? get_member(p, m_pActiveItem) : 0, held[WEAPON_NAME] = "-"
        if (active > 0)
            get_entvar(active, var_classname, held, charsmax(held))
        console_print(id, "[GunGame]   %s: level %d (%s), holding %s", name, g_level[p] + 1, g_ladder[g_level[p]], held)
    }
    return PLUGIN_HANDLED
}

public cmd_setlevel(id, level, cid)
{
    if (!cmd_access(id, level, cid, 3))
        return PLUGIN_HANDLED
    new arg[32]
    read_argv(1, arg, charsmax(arg))
    new target = cmd_target(id, arg, CMDTARGET_ALLOW_SELF)
    if (!target)
        return PLUGIN_HANDLED
    new wanted = read_argv_int(2)
    if (!(1 <= wanted <= g_levels))
    {
        console_print(id, "[GunGame] level 1-%d", g_levels)
        return PLUGIN_HANDLED
    }
    g_level[target] = wanted - 1
    give_level_weapon(target)
    get_user_name(target, arg, charsmax(arg))
    console_print(id, "[GunGame] %s is on level %d (%s)", arg, wanted, g_ladder[wanted - 1])
    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.