Skip to content
View in the app

A better way to browse. Learn more.

Learn more

ak-vs-m4-duel-mode/addons/amxmodx/scripting/olds_akm4.sma

olds_akm4.sma
343 lines · 10.4 KB · Pawn (AMX Mod X)
Raw
/*
 * AK vs M4 Duel Mode (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/>.
 *
 * Deathmatch rifle rivalry: every player fights with the AK-47 or the M4A1 and a knife.
 * At spawn (RG_CBasePlayer_Spawn) the player is stripped (rg_remove_all_items) and gets the
 * picked rifle; a menu offers the choice again with the last pick marked. Buying (the buy
 * menu commands, and every purchase through RG_CBasePlayer_HasRestrictItem) and picking up
 * anything else (the same hook) are blocked. Kills with each rifle are counted (RG_CBasePlayer_Killed) and a HUD line shows
 * which rifle leads. Dead players respawn after olds_akm4_respawn seconds.
 *
 * Cvars: olds_akm4_enable 1, olds_akm4_respawn 2.0, olds_akm4_pick 1
 * Commands: say /rifle (open the menu), amx_akm4_status (ADMIN_KICK), amx_akm4_reset (ADMIN_CFG)
 */

#include <amxmodx>
#include <amxmisc>
#include <reapi>

#pragma semicolon 0

#define PLUGIN  "AK vs M4 Duel Mode"
#define VERSION "1.0.0"
#define AUTHOR  "daemon (olds.ro)"

#define TASK_RESPAWN 7760
#define TASK_HUD     7700

enum
{
    RIFLE_NONE = 0,
    RIFLE_AK,
    RIFLE_M4
}

new g_enable
new Float:g_respawn
new g_menuOn

new g_pick[MAX_PLAYERS + 1]
new g_carry[MAX_PLAYERS + 1]
new bool:g_spawning[MAX_PLAYERS + 1]
new g_kills[3]
new g_blockedBuys
new g_blockedPickups
new g_hudSync

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

    bind_pcvar_num(create_cvar("olds_akm4_enable", "1", FCVAR_NONE,
        "1 = AK vs M4 duel mode is on", true, 0.0, true, 1.0), g_enable)
    bind_pcvar_float(create_cvar("olds_akm4_respawn", "2.0", FCVAR_NONE,
        "Seconds before a dead player respawns", true, 0.5, true, 30.0), g_respawn)
    bind_pcvar_num(create_cvar("olds_akm4_pick", "1", FCVAR_NONE,
        "1 = show the rifle menu at each spawn", true, 0.0, true, 1.0), g_menuOn)
    AutoExecConfig(true, "olds_akm4")

    RegisterHookChain(RG_CBasePlayer_Spawn, "on_spawn_pre", false)
    RegisterHookChain(RG_CBasePlayer_Spawn, "on_spawn_post", true)
    RegisterHookChain(RG_CBasePlayer_HasRestrictItem, "on_restrict_item", false)
    RegisterHookChain(RG_CBasePlayer_Killed, "on_killed_post", true)

    register_clcmd("buy", "cmd_buy_blocked")
    register_clcmd("buyequip", "cmd_buy_blocked")
    register_clcmd("client_buy_open", "cmd_buy_blocked")
    register_clcmd("say /rifle", "cmd_rifle")
    register_clcmd("say_team /rifle", "cmd_rifle")
    register_concmd("amx_akm4_status", "cmd_status", ADMIN_KICK, "- shows the rifle score and each player's pick")
    register_concmd("amx_akm4_reset", "cmd_reset", ADMIN_CFG, "- sets the rifle score to 0 : 0")

    g_hudSync = CreateHudSyncObj()
    set_task(1.0, "task_hud", TASK_HUD, _, _, "b")
}

public client_putinserver(id)
{
    g_pick[id] = RIFLE_NONE
    g_carry[id] = RIFLE_NONE
    g_spawning[id] = false
}

public client_disconnected(id)
{
    remove_task(TASK_RESPAWN + id)
    g_pick[id] = RIFLE_NONE
    g_carry[id] = RIFLE_NONE
    g_spawning[id] = false
}

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

rifle_class(rifle, out[], len)
{
    if (rifle == RIFLE_M4)
        copy(out, len, "weapon_m4a1")
    else
        copy(out, len, "weapon_ak47")
}

default_rifle(id)
{
    if (g_pick[id] != RIFLE_NONE)
        return g_pick[id]
    if (get_member(id, m_iTeam) == _:TEAM_CT)
        return RIFLE_M4
    return RIFLE_AK
}

arm(id, rifle)
{
    rg_remove_all_items(id)
    g_carry[id] = rifle
    rg_give_item(id, "weapon_knife")
    new cls[16]
    rifle_class(rifle, cls, charsmax(cls))
    rg_give_item(id, cls)
    if (rifle == RIFLE_M4)
        rg_set_user_bpammo(id, WEAPON_M4A1, 90)
    else
        rg_set_user_bpammo(id, WEAPON_AK47, 90)
    engclient_cmd(id, cls)
}

public on_spawn_pre(id)
{
    /* The game's own spawn equipment is given here and stripped in the post hook. */
    g_spawning[id] = true
    return HC_CONTINUE
}

public on_spawn_post(id)
{
    g_spawning[id] = false
    if (!g_enable || !is_user_alive(id))
        return HC_CONTINUE
    remove_task(TASK_RESPAWN + id)
    arm(id, default_rifle(id))
    if (g_menuOn)
        show_rifle_menu(id)
    return HC_CONTINUE
}

public cmd_buy_blocked(id)
{
    /* The buy menu does not open while the mode is on. */
    if (!g_enable || !is_user_connected(id))
        return PLUGIN_CONTINUE
    g_blockedBuys++
    client_print_color(id, print_team_default, "%L", id, "AKM4_NO_BUY")
    return PLUGIN_HANDLED
}

public on_restrict_item(id, ItemID:item, ItemRestType:type)
{
    if (!g_enable || g_spawning[id])
        return HC_CONTINUE
    if (type == ITEM_TYPE_BUYING)
    {
        /* Every purchase (buy aliases, old-style menus) is refused. */
        g_blockedBuys++
        client_print_color(id, print_team_default, "%L", id, "AKM4_NO_BUY")
        SetHookChainReturn(ATYPE_BOOL, true)
        return HC_SUPERCEDE
    }
    if (type != ITEM_TYPE_TOUCHED)
        return HC_CONTINUE
    if (item == ITEM_KNIFE || item == ITEM_C4)
        return HC_CONTINUE
    if ((item == ITEM_AK47 && g_carry[id] == RIFLE_AK) || (item == ITEM_M4A1 && g_carry[id] == RIFLE_M4))
        return HC_CONTINUE
    g_blockedPickups++
    SetHookChainReturn(ATYPE_BOOL, true)
    return HC_SUPERCEDE
}

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

show_rifle_menu(id)
{
    new title[64], ak[48], m4[48]
    formatex(title, charsmax(title), "%L", id, "AKM4_MENU_TITLE")
    new last = default_rifle(id)
    if (last == RIFLE_AK)
        formatex(ak, charsmax(ak), "\yAK-47 \w%L", id, "AKM4_MENU_LAST")
    else
        copy(ak, charsmax(ak), "AK-47")
    if (last == RIFLE_M4)
        formatex(m4, charsmax(m4), "\yM4A1 \w%L", id, "AKM4_MENU_LAST")
    else
        copy(m4, charsmax(m4), "M4A1")
    new menu = menu_create(title, "menu_rifle")
    menu_additem(menu, ak, "1")
    menu_additem(menu, m4, "2")
    menu_display(id, menu)
}

public menu_rifle(id, menu, item)
{
    menu_destroy(menu)
    if (item < 0 || !is_user_connected(id))
        return PLUGIN_HANDLED
    new rifle = RIFLE_AK
    if (item == 1)
        rifle = RIFLE_M4
    g_pick[id] = rifle
    if (g_enable && is_user_alive(id) && g_carry[id] != rifle)
        arm(id, rifle)
    if (rifle == RIFLE_M4)
        client_print_color(id, print_team_default, "%L", id, "AKM4_PICKED_M4")
    else
        client_print_color(id, print_team_default, "%L", id, "AKM4_PICKED_AK")
    return PLUGIN_HANDLED
}

public cmd_rifle(id)
{
    if (g_enable && is_user_connected(id))
        show_rifle_menu(id)
    return PLUGIN_HANDLED
}

/* ------------------------------------------------------------ score and respawn */

public on_killed_post(victim, killer, gib)
{
    g_carry[victim] = RIFLE_NONE
    if (!g_enable)
        return HC_CONTINUE
    new team = get_member(victim, m_iTeam)
    if (team == _:TEAM_TERRORIST || team == _:TEAM_CT)
    {
        remove_task(TASK_RESPAWN + victim)
        set_task(floatclamp(g_respawn, 0.5, 30.0), "task_respawn", TASK_RESPAWN + victim)
    }
    if (killer < 1 || killer > MaxClients || killer == victim || !is_user_connected(killer))
        return HC_CONTINUE
    if (get_member(killer, m_iTeam) == team)
        return HC_CONTINUE
    new weapon = get_user_weapon(killer)
    new rifle = RIFLE_NONE
    if (weapon == CSW_AK47)
        rifle = RIFLE_AK
    else if (weapon == CSW_M4A1)
        rifle = RIFLE_M4
    if (rifle == RIFLE_NONE)
        return HC_CONTINUE
    g_kills[rifle]++
    new name[MAX_NAME_LENGTH]
    get_user_name(killer, name, charsmax(name))
    new cls[16]
    rifle_class(rifle, cls, charsmax(cls))
    log_amx("%s kill by %s: AK %d vs M4 %d", cls[7], name, g_kills[RIFLE_AK], g_kills[RIFLE_M4])
    show_score()
    return HC_CONTINUE
}

public task_respawn(taskid)
{
    new id = taskid - TASK_RESPAWN
    if (!g_enable || !is_user_connected(id) || is_user_alive(id))
        return
    new team = get_member(id, m_iTeam)
    if (team == _:TEAM_TERRORIST || team == _:TEAM_CT)
        rg_round_respawn(id)
}

show_score()
{
    new ak = g_kills[RIFLE_AK], m4 = g_kills[RIFLE_M4]
    new lead[32]
    set_hudmessage(255, 220, 80, -1.0, 0.03, 0, 0.0, 1.2, 0.0, 0.0, -1)
    for (new i = 1; i <= MaxClients; i++)
    {
        if (!is_user_connected(i) || is_user_bot(i))
            continue
        if (ak > m4)
            formatex(lead, charsmax(lead), "%L", i, "AKM4_LEAD_AK")
        else if (m4 > ak)
            formatex(lead, charsmax(lead), "%L", i, "AKM4_LEAD_M4")
        else
            formatex(lead, charsmax(lead), "%L", i, "AKM4_LEAD_TIE")
        ShowSyncHudMsg(i, g_hudSync, "%L", i, "AKM4_HUD", ak, m4, lead)
    }
}

public task_hud()
{
    if (g_enable)
        show_score()
}

/* ------------------------------------------------------------ admin */

public cmd_status(id, level, cid)
{
    if (!cmd_access(id, level, cid, 1))
        return PLUGIN_HANDLED
    console_print(id, "AK vs M4: enable %d, AK %d vs M4 %d, buys blocked %d, pickups blocked %d",
        g_enable, g_kills[RIFLE_AK], g_kills[RIFLE_M4], g_blockedBuys, g_blockedPickups)
    new name[MAX_NAME_LENGTH], weapons[32], count, list[128], wname[32]
    for (new i = 1; i <= MaxClients; i++)
    {
        if (!is_user_connected(i))
            continue
        get_user_name(i, name, charsmax(name))
        list[0] = EOS
        if (is_user_alive(i))
        {
            count = 0
            get_user_weapons(i, weapons, count)
            for (new w = 0; w < count; w++)
            {
                get_weaponname(weapons[w], wname, charsmax(wname))
                add(list, charsmax(list), wname[7])
                add(list, charsmax(list), " ")
            }
        }
        console_print(id, "  %s: alive %d, pick %d, carry %d, weapons: %s", name, is_user_alive(i), g_pick[i], g_carry[i], list)
    }
    return PLUGIN_HANDLED
}

public cmd_reset(id, level, cid)
{
    if (!cmd_access(id, level, cid, 1))
        return PLUGIN_HANDLED
    g_kills[RIFLE_AK] = 0
    g_kills[RIFLE_M4] = 0
    console_print(id, "AK vs M4 score is 0 : 0")
    log_amx("Rifle score reset")
    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.