#include <amxmodx>
#include <amxmisc>
#include <fakemeta>
#include <hamsandwich>

#define PLUGIN "Half-Life DM Stress Test Bots"
#define VERSION "1.0"
#define AUTHOR "OyunMabedi"

#define MAX_BOTS 30

new const BOT_NAMES[MAX_BOTS][] = {
    "[HL] Gordon_Freeman", "[HL] Barney_Calhoun", "[HL] Adrian_Shephard", "[HL] G-Man", "[HL] Scientist",
    "[HL] BlackMesa_Guard", "[HL] HECU_Grunt", "[HL] Alien_Grunt", "[HL] Vortigaunt", "[HL] Headcrab_King",
    "[HL] Lambda_Warrior", "[HL] Crossfire_Sniper", "[HL] Nuclear_Lord", "[HL] Rocket_Master", "[HL] Gauss_Gunner",
    "[HL] Tau_Specialist", "[HL] Crowbar_Hero", "[HL] Snark_Nest", "[HL] Bunker_Buster", "[HL] Xen_Invader",
    "[HL] Kastamonu_HL", "[HL] Kurtulus_DM", "[HL] Efsane_Freak", "[HL] Mega_Fragger", "[HL] Speed_Runner",
    "[HL] Bunny_King", "[HL] Longjump_Pro", "[HL] Python_Shooter", "[HL] Shotgun_Beast", "[HL] Subzero"
};

new g_bStressActive = 0;
new g_iBotCount = 30;

public plugin_init() {
    register_plugin(PLUGIN, VERSION, AUTHOR);
    
    register_srvcmd("stress_bots", "cmd_stress_bots");
    register_srvcmd("kick_stress_bots", "cmd_kick_bots");
    register_concmd("amx_stress_bots", "cmd_stress_bots", ADMIN_RCON, "<count> - Spawn HL stress bots");
    register_concmd("amx_kick_bots", "cmd_kick_bots", ADMIN_RCON, "- Kick all HL stress bots");
    
    RegisterHam(Ham_Killed, "player", "fw_PlayerKilled_Post", 1);
    
    set_task(4.0, "task_start_bots");
    set_task(10.0, "task_bot_watchdog", _, _, _, "b");
}

public task_start_bots() {
    spawn_stress_bots(g_iBotCount);
}

public cmd_stress_bots() {
    new arg[8];
    read_argv(1, arg, charsmax(arg));
    new count = str_to_num(arg);
    if (count <= 0 || count > 30) count = 30;
    
    spawn_stress_bots(count);
    server_print("[HL-STRESS] %d bots spawned for stress test!^n", count);
    return PLUGIN_HANDLED;
}

public cmd_kick_bots() {
    kick_all_stress_bots();
    server_print("[HL-STRESS] All HL stress bots kicked.^n");
    return PLUGIN_HANDLED;
}

spawn_stress_bots(count) {
    g_bStressActive = 1;
    g_iBotCount = count;
    
    for (new i = 0; i < count; i++) {
        create_single_bot(i);
    }
}

create_single_bot(index) {
    if (index >= MAX_BOTS) return;
    
    new name[32];
    copy(name, charsmax(name), BOT_NAMES[index]);
    
    new existing = find_bot_by_name(name);
    if (existing > 0) return;
    
    new bot = engfunc(EngFunc_CreateFakeClient, name);
    if (!pev_valid(bot)) return;
    
    engfunc(EngFunc_FreeEntPrivateData, bot);
    dllfunc(DLLFunc_ClientConnect, bot, name, "127.0.0.1", "");
    dllfunc(DLLFunc_ClientPutInServer, bot);
    
    set_task(1.0, "task_respawn_bot", bot);
}

kick_all_stress_bots() {
    g_bStressActive = 0;
    for (new i = 1; i <= get_maxplayers(); i++) {
        if (is_user_connected(i) && is_user_bot(i)) {
            server_cmd("kick #%d", get_user_userid(i));
        }
    }
}

public task_respawn_bot(id) {
    if (is_user_connected(id) && is_user_bot(id)) {
        if (!is_user_alive(id)) {
            ExecuteHamB(Ham_Spawn, id);
        }
    }
}

public fw_PlayerKilled_Post(victim, attacker, shouldgib) {
    if (g_bStressActive && is_user_connected(victim) && is_user_bot(victim)) {
        set_task(2.5, "task_respawn_bot", victim);
    }
}

public task_bot_watchdog() {
    if (!g_bStressActive) return;
    
    for (new i = 0; i < g_iBotCount; i++) {
        new id = find_bot_by_name(BOT_NAMES[i]);
        if (!id || !is_user_connected(id)) {
            create_single_bot(i);
        } else if (!is_user_alive(id)) {
            ExecuteHamB(Ham_Spawn, id);
        }
    }
}

find_bot_by_name(const name[]) {
    for (new i = 1; i <= get_maxplayers(); i++) {
        if (is_user_connected(i) && is_user_bot(i)) {
            new botname[32];
            get_user_name(i, botname, charsmax(botname));
            if (equal(botname, name)) {
                return i;
            }
        }
    }
    return 0;
}

