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

#define PLUGIN "HIT Bot Manager"
#define VERSION "2.0"
#define AUTHOR "HIT"

new const BOT_T_NAME[] = "HITBOT";
new const BOT_SPEC_NAME[] = "HIZ TEKNIK IRADE";

new g_t_bot = 0;
new g_spec_bot = 0;

public plugin_init() {
    register_plugin(PLUGIN, VERSION, AUTHOR);

    RegisterHookChain(RG_CBasePlayer_Killed, "fw_PlayerKilled_Post", true);
    RegisterHookChain(RG_CSGameRules_RestartRound, "fw_RestartRound_Post", true);

    register_forward(FM_CmdStart, "fw_CmdStart");

    set_task(3.0, "task_init_bots");
    set_task(10.0, "task_bot_guard", _, _, _, "b");
}

public task_init_bots() {
    create_t_bot();
    create_spec_bot();
}

public create_t_bot() {
    g_t_bot = find_bot_by_name(BOT_T_NAME);
    if (!g_t_bot) {
        new bot = engfunc(EngFunc_CreateFakeClient, BOT_T_NAME);
        if (pev_valid(bot)) {
            engfunc(EngFunc_FreeEntPrivateData, bot);
            dllfunc(DLLFunc_ClientConnect, bot, BOT_T_NAME, "127.0.0.1", "");
            dllfunc(DLLFunc_ClientPutInServer, bot);
            
            cs_set_user_team(bot, CS_TEAM_T);
            g_t_bot = bot;
            set_task(1.0, "task_respawn_t_bot", g_t_bot);
        }
    } else {
        if (cs_get_user_team(g_t_bot) != CS_TEAM_T) {
            cs_set_user_team(g_t_bot, CS_TEAM_T);
        }
        if (!is_user_alive(g_t_bot)) {
            rg_round_respawn(g_t_bot);
        }
    }
}

public create_spec_bot() {
    g_spec_bot = find_bot_by_name(BOT_SPEC_NAME);
    if (!g_spec_bot) {
        new bot = engfunc(EngFunc_CreateFakeClient, BOT_SPEC_NAME);
        if (pev_valid(bot)) {
            engfunc(EngFunc_FreeEntPrivateData, bot);
            dllfunc(DLLFunc_ClientConnect, bot, BOT_SPEC_NAME, "127.0.0.1", "");
            dllfunc(DLLFunc_ClientPutInServer, bot);
            
            cs_set_user_team(bot, CS_TEAM_SPECTATOR);
            g_spec_bot = bot;
        }
    } else {
        if (cs_get_user_team(g_spec_bot) != CS_TEAM_SPECTATOR) {
            cs_set_user_team(g_spec_bot, CS_TEAM_SPECTATOR);
        }
    }
}

public task_bot_guard() {
    // 1. T Bot Check
    g_t_bot = find_bot_by_name(BOT_T_NAME);
    if (!g_t_bot || !is_user_connected(g_t_bot)) {
        create_t_bot();
    } else {
        if (cs_get_user_team(g_t_bot) != CS_TEAM_T) {
            cs_set_user_team(g_t_bot, CS_TEAM_T);
        }
        if (!is_user_alive(g_t_bot)) {
            rg_round_respawn(g_t_bot);
        }
        // Sabit durmasi icin frozen yap
        set_entvar(g_t_bot, var_flags, get_entvar(g_t_bot, var_flags) | FL_FROZEN);
    }

    // 2. Spec Bot Check
    g_spec_bot = find_bot_by_name(BOT_SPEC_NAME);
    if (!g_spec_bot || !is_user_connected(g_spec_bot)) {
        create_spec_bot();
    } else {
        if (cs_get_user_team(g_spec_bot) != CS_TEAM_SPECTATOR) {
            cs_set_user_team(g_spec_bot, CS_TEAM_SPECTATOR);
        }
    }
}

public fw_RestartRound_Post() {
    set_task(1.0, "task_round_bot_check");
}

public task_round_bot_check() {
    if (g_t_bot && is_user_connected(g_t_bot)) {
        if (cs_get_user_team(g_t_bot) != CS_TEAM_T) {
            cs_set_user_team(g_t_bot, CS_TEAM_T);
        }
        if (!is_user_alive(g_t_bot)) {
            rg_round_respawn(g_t_bot);
        }
        set_entvar(g_t_bot, var_flags, get_entvar(g_t_bot, var_flags) | FL_FROZEN);
    }
}

public fw_PlayerKilled_Post(victim, attacker, shouldgib) {
    if (victim == g_t_bot) {
        // T botu oldugunde 3 saniye sonra tekrar dogur (testler icin kesintisiz canli kalsin)
        set_task(3.0, "task_respawn_t_bot", victim);
    }
}

public task_respawn_t_bot(id) {
    if (is_user_connected(id) && is_user_bot(id)) {
        if (cs_get_user_team(id) != CS_TEAM_T) {
            cs_set_user_team(id, CS_TEAM_T);
        }
        if (!is_user_alive(id)) {
            rg_round_respawn(id);
        }
        set_entvar(id, var_flags, get_entvar(id, var_flags) | FL_FROZEN);
    }
}

public fw_CmdStart(id, uc_handle, random_seed) {
    if (id == g_t_bot) {
        // Botun hareket etmesini, ziplamasini, ates etmesini tamamen engelle (sabit kalsin)
        set_uc(uc_handle, UC_Buttons, 0);
        set_uc(uc_handle, UC_ForwardMove, 0.0);
        set_uc(uc_handle, UC_SideMove, 0.0);
        set_uc(uc_handle, UC_UpMove, 0.0);
        return FMRES_SUPERCEDE;
    }
    return FMRES_IGNORED;
}

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;
}
