#include <amxmodx>
#include <amxmisc>
#include <cstrike>
#include <fakemeta>

#define PLUGIN "HIT Permanent Spec Bot"
#define VERSION "1.0"
#define AUTHOR "HIT"

new const BOT_NAME[] = "HIZ TEKNIK IRADE";
new g_bot_id = 0;

public plugin_init() {
    register_plugin(PLUGIN, VERSION, AUTHOR);
    set_task(4.0, "task_create_bot");
    set_task(15.0, "task_check_bot", _, _, _, "b");
}

public task_create_bot() {
    if (find_spec_bot() > 0) {
        return;
    }
    
    new bot = engfunc(EngFunc_CreateFakeClient, BOT_NAME);
    if (pev_valid(bot)) {
        engfunc(EngFunc_FreeEntPrivateData, bot);
        dllfunc(DLLFunc_ClientConnect, bot, BOT_NAME, "127.0.0.1", "");
        dllfunc(DLLFunc_ClientPutInServer, bot);
        
        cs_set_user_team(bot, CS_TEAM_SPECTATOR);
        g_bot_id = bot;
    }
}

public task_check_bot() {
    new bot = find_spec_bot();
    if (bot <= 0) {
        task_create_bot();
    } else {
        if (cs_get_user_team(bot) != CS_TEAM_SPECTATOR) {
            cs_set_user_team(bot, CS_TEAM_SPECTATOR);
        }
    }
}

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