Skip to content
View in the app

A better way to browse. Learn more.

Learn more

base-builder-human-class-menu/addons/amxmodx/scripting/olds_bbclass.sma

olds_bbclass.sma
315 lines · 9.8 KB · Pawn (AMX Mod X)
Raw
/*
 * Base Builder Human Class Menu (olds.ro workshop card LIB-0011)
 *
 * When the build phase ends every living human gets a class menu (also on say /class):
 * each class has its own weapons, health, armour, speed and a small perk. The choice is
 * kept for the map and applied again on every human spawn, without the menu.
 * Classes: configs/olds_bb_classes.ini.
 *
 *   say /class   the class menu (humans only)
 *
 * Perks: regen (health back up to the class health), armor (armour back up to the class
 * armour), none. Both tick every 2 seconds.
 *
 * Needs the Base Builder Core Mode (library "olds_basebuilder",
 * include/olds_basebuilder.inc): olds_bb_get_phase, olds_bb_is_zombie and the forward
 * olds_bb_phase_changed. A block the player holds is let go (-bbgrab) before the loadout.
 *
 * Cvars: olds_bbc_change_once 1 (one class per round once it is applied),
 *        olds_bbc_default 0 (class number given to players who chose none; 0 = none).
 *
 * Copyright (C) 2026 daemon (olds.ro)
 * SPDX-License-Identifier: GPL-3.0-or-later
 */
#include <amxmodx>
#include <fun>
#include <reapi>
#include <olds_basebuilder>

#define PLUGIN  "Base Builder Human Class Menu"
#define VERSION "1.0.0"
#define AUTHOR  "olds.ro"

#define TASK_APPLY  11100     // + player id
#define TASK_PERK   11140

const CLASS_MAX = 12;
const WEAPON_SLOTS_MAX = 6;

enum { PERK_NONE = 0, PERK_REGEN, PERK_ARMOR };

new g_className[CLASS_MAX][32];
new g_classHealth[CLASS_MAX];
new g_classArmor[CLASS_MAX];
new Float:g_classSpeed[CLASS_MAX];
new g_classWeapons[CLASS_MAX][WEAPON_SLOTS_MAX][24];
new g_classWeaponCount[CLASS_MAX];
new g_classPerk[CLASS_MAX];
new g_classes;

new g_changeOnce, g_default;
new g_choice[MAX_PLAYERS + 1] = { -1, ... };     // class index, -1 = none
new g_appliedRound[MAX_PLAYERS + 1];
new g_applied[MAX_PLAYERS + 1] = { -1, ... };    // class applied this life
new g_round = 1;
new g_lastPhase;

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

    bind_pcvar_num(create_cvar("olds_bbc_change_once", "1", FCVAR_NONE,
        "1 = no class change in a round once a class is applied", true, 0.0, true, 1.0), g_changeOnce);
    bind_pcvar_num(create_cvar("olds_bbc_default", "0", FCVAR_NONE,
        "Class number given to players who chose none (0 = none)", true, 0.0, true, float(CLASS_MAX)), g_default);
    AutoExecConfig(true, "olds_bbclass");

    register_clcmd("say /class", "cmd_class");
    register_clcmd("say_team /class", "cmd_class");
    RegisterHookChain(RG_CBasePlayer_Spawn, "on_spawn", true);
    RegisterHookChain(RG_CBasePlayer_ResetMaxSpeed, "on_reset_maxspeed", true);
    RegisterHookChain(RG_CSGameRules_RestartRound, "on_restart_round", false);
    RegisterHookChain(RG_CBasePlayer_Killed, "on_killed", true);

    load_classes();
    set_task(2.0, "task_perk", TASK_PERK, _, _, "b");
}

/* ---------- classes file ---------- */

perk_of(const word[])
{
    if (equali(word, "regen"))
        return PERK_REGEN;
    if (equali(word, "armor"))
        return PERK_ARMOR;
    return PERK_NONE;
}

load_classes()
{
    new path[128];
    get_localinfo("amxx_configsdir", path, charsmax(path));
    add(path, charsmax(path), "/olds_bb_classes.ini");
    new fp = fopen(path, "rt");
    if (!fp)
    {
        log_amx("Cannot open %s: no classes", path);
        return;
    }
    new line[256], name[32], health[8], armor[8], speed[8], weapons[160], perk[16];
    new weapon[24], rest[160];
    while (g_classes < CLASS_MAX && fgets(fp, line, charsmax(line)))
    {
        trim(line);
        if (!line[0] || line[0] == ';' || line[0] == '#')
            continue;
        perk[0] = EOS;
        if (parse(line, name, charsmax(name), health, charsmax(health), armor, charsmax(armor),
            speed, charsmax(speed), weapons, charsmax(weapons), perk, charsmax(perk)) < 5)
        {
            log_amx("olds_bb_classes.ini: bad line skipped: %s", line);
            continue;
        }
        new c = g_classes;
        copy(g_className[c], charsmax(g_className[]), name);
        g_classHealth[c] = clamp(str_to_num(health), 1, 255);
        g_classArmor[c] = clamp(str_to_num(armor), 0, 255);
        g_classSpeed[c] = floatmin(floatmax(str_to_float(speed), 0.0), 400.0);
        g_classPerk[c] = perk_of(perk);
        g_classWeaponCount[c] = 0;
        while (weapons[0] && g_classWeaponCount[c] < WEAPON_SLOTS_MAX)
        {
            strtok2(weapons, weapon, charsmax(weapon), rest, charsmax(rest), ' ', TRIM_FULL);
            copy(weapons, charsmax(weapons), rest);
            if (!weapon[0])
                continue;
            if (!equal(weapon, "weapon_", 7) || rg_get_weapon_info(weapon, WI_ID) == WEAPON_NONE)
            {
                log_amx("olds_bb_classes.ini: unknown weapon %s in %s", weapon, name);
                continue;
            }
            copy(g_classWeapons[c][g_classWeaponCount[c]++], charsmax(g_classWeapons[][]), weapon);
        }
        g_classes++;
    }
    fclose(fp);
    log_amx("%d human classes loaded", g_classes);
}

/* ---------- phases and rounds ---------- */

public olds_bb_phase_changed(phase)
{
    // The build phase is over: everyone who is human and alive picks a class.
    if (g_lastPhase == OLDS_BB_BUILD && (phase == OLDS_BB_PREP || phase == OLDS_BB_FIGHT))
    {
        for (new id = 1; id <= MaxClients; id++)
            if (is_user_alive(id) && !olds_bb_is_zombie(id))
                show_menu_to(id);
    }
    g_lastPhase = phase;
}

public on_restart_round()
{
    g_round++;
}

public on_killed(victim)
{
    if (1 <= victim <= MaxClients)
        g_applied[victim] = -1;
    return HC_CONTINUE;
}

public client_disconnected(id)
{
    remove_task(TASK_APPLY + id);
    g_choice[id] = -1;
    g_applied[id] = -1;
    g_appliedRound[id] = 0;
}

/* ---------- menu ---------- */

public cmd_class(id)
{
    if (!is_user_connected(id))
        return PLUGIN_HANDLED;
    if (!is_user_alive(id) || olds_bb_is_zombie(id))
    {
        client_print_color(id, print_team_default, "^4[BB]^1 %L", id, "BBC_HUMANS");
        return PLUGIN_HANDLED;
    }
    show_menu_to(id);
    return PLUGIN_HANDLED;
}

show_menu_to(id)
{
    if (!g_classes)
        return;
    new title[64], item[96];
    formatex(title, charsmax(title), "%L", id, "BBC_TITLE");
    new menu = menu_create(title, "menu_class");
    for (new c = 0; c < g_classes; c++)
    {
        formatex(item, charsmax(item), "%s \d(%d HP, %d AP)", g_className[c], g_classHealth[c], g_classArmor[c]);
        if (c == g_choice[id])
            add(item, charsmax(item), " \y*");
        menu_additem(menu, item);
    }
    menu_display(id, menu);
}

public menu_class(id, menu, item)
{
    menu_destroy(menu);
    if (item < 0 || item >= g_classes || !is_user_connected(id))
        return PLUGIN_HANDLED;
    if (!is_user_alive(id) || olds_bb_is_zombie(id))
    {
        client_print_color(id, print_team_default, "^4[BB]^1 %L", id, "BBC_HUMANS");
        return PLUGIN_HANDLED;
    }
    if (g_changeOnce && g_appliedRound[id] == g_round && g_applied[id] >= 0 && item != g_applied[id])
    {
        client_print_color(id, print_team_default, "^4[BB]^1 %L", id, "BBC_ONCE");
        return PLUGIN_HANDLED;
    }
    g_choice[id] = item;
    // Let go of a held block first (the core drops it on -bbgrab).
    amxclient_cmd(id, "-bbgrab");
    apply_class(id);
    return PLUGIN_HANDLED;
}

/* ---------- loadout ---------- */

public on_spawn(id)
{
    if (!is_user_alive(id))
        return HC_CONTINUE;
    g_applied[id] = -1;
    // After the core's own spawn handling.
    remove_task(TASK_APPLY + id);
    set_task(0.1, "task_apply", TASK_APPLY + id);
    return HC_CONTINUE;
}

public task_apply(task)
{
    new id = task - TASK_APPLY;
    if (!is_user_alive(id) || olds_bb_is_zombie(id))
        return;
    if (g_choice[id] < 0 && g_default > 0 && g_default <= g_classes)
        g_choice[id] = g_default - 1;
    if (g_choice[id] >= 0)
        apply_class(id);
}

apply_class(id)
{
    new c = g_choice[id];
    if (c < 0 || c >= g_classes || !is_user_alive(id))
        return;

    rg_remove_all_items(id);
    give_item(id, "weapon_knife");
    for (new w = 0; w < g_classWeaponCount[c]; w++)
    {
        if (give_item(id, g_classWeapons[c][w]) > 0)
        {
            new WeaponIdType:wid = WeaponIdType:rg_get_weapon_info(g_classWeapons[c][w], WI_ID);
            new max_ammo = rg_get_weapon_info(wid, WI_MAX_ROUNDS);
            if (max_ammo > 0)
                rg_set_user_bpammo(id, wid, max_ammo);
        }
    }
    set_user_health(id, g_classHealth[c]);
    if (g_classArmor[c] > 0)
        rg_set_user_armor(id, g_classArmor[c], ARMOR_VESTHELM);
    else
        rg_set_user_armor(id, 0, ARMOR_NONE);
    g_applied[id] = c;
    g_appliedRound[id] = g_round;
    rg_reset_maxspeed(id);
    client_print_color(id, print_team_default, "^4[BB]^1 %L", id, "BBC_APPLIED", g_className[c]);
}

public on_reset_maxspeed(id)
{
    new c = g_applied[id];
    if (c >= 0 && g_classSpeed[c] > 0.0 && is_user_alive(id) && !olds_bb_is_zombie(id))
    {
        // Frozen players (1.0) stay frozen.
        if (Float:get_entvar(id, var_maxspeed) > 1.0)
            set_entvar(id, var_maxspeed, g_classSpeed[c]);
    }
    return HC_CONTINUE;
}

public task_perk()
{
    new c, health, armor, ArmorType:type;
    for (new id = 1; id <= MaxClients; id++)
    {
        c = g_applied[id];
        if (c < 0 || !is_user_alive(id) || olds_bb_is_zombie(id))
            continue;
        if (g_classPerk[c] == PERK_REGEN)
        {
            health = get_user_health(id);
            if (health < g_classHealth[c])
                set_user_health(id, min(health + 3, g_classHealth[c]));
        }
        else if (g_classPerk[c] == PERK_ARMOR)
        {
            armor = rg_get_user_armor(id, type);
            if (armor < g_classArmor[c])
                rg_set_user_armor(id, min(armor + 5, g_classArmor[c]), ARMOR_VESTHELM);
        }
    }
}

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.