#pragma semicolon 1
#include <sourcemod>
#include <cstrike>
#include <sdktools>

public Plugin:myinfo = 
{
	name = "Bot Replacer",
	author = "SenatoR",
	description = "Замена бота игроком :D",
	version = "1.1"
}

new Handle:CheckSpecTimer[MAXPLAYERS+1];
new BotAmount[MAXPLAYERS+1];
new Handle:AmountBot;
new Handle:PriceBot;
new Handle:ShowMessageHud;
new Handle:TypeKick;

public OnPluginStart()
{
	AmountBot = CreateConVar("sm_bot_replacer_amount", "1",	"Сколько раз за раунд игрок может сыграть вместо бота?");
	PriceBot = CreateConVar("sm_bot_replacer_price", "2000",	"Сколько стоит сыграть за бота?");
	ShowMessageHud = CreateConVar("sm_bot_replacer_showhud", "1", "Показывать сообщение, что можно играть за бота?");
	TypeKick = CreateConVar("sm_bot_replacer_typekick", "0", "0 - кикнуть бота, 1 - удалить бота");
	AutoExecConfig(true, "sm_bot_replacer");
	HookEvent("round_start", EventRoundStart);
}

public OnClientPostAdminCheck(client)
{

	if (!IsFakeClient(client) && GetConVarBool(ShowMessageHud))
		CheckSpecTimer[client] = CreateTimer(1.0, Spec_Timer, client, TIMER_REPEAT|TIMER_FLAG_NO_MAPCHANGE);
}

public Action:Spec_Timer(Handle:timer, any:client)
{
	new i = 0;
	i = GetEntPropEnt(client, Prop_Send, "m_hObserverTarget");
	if (i > 0 && IsFakeClient(i) && GetClientTeam(client)==GetClientTeam(i))
	{
		PrintHintText(client, "Нажмите e, чтобы играть за бота %N",i);
	}
}

public Action:OnPlayerRunCmd(client, &buttons, &impulse, Float:vel[3], Float:angles[3], &weapon)
{
	if (buttons & IN_USE && !IsPlayerAlive(client))
	{
		new i = 0;
		i = GetEntPropEnt(client, Prop_Send, "m_hObserverTarget");
		new price = GetConVarInt(PriceBot);
		new cash = GetEntProp(client, Prop_Send, "m_iAccount");
		if (i > 0 && IsFakeClient(i) && GetClientTeam(client)==GetClientTeam(i) && BotAmount[client]<GetConVarInt(AmountBot)&& price<=cash)
		{
			SetEntProp(client, Prop_Send, "m_iAccount", cash - price);
			new health = GetClientHealth(i);
			new armor = GetClientArmor(i);
			new Float:pos[3];
			GetClientAbsOrigin(i,pos);
			if(GetConVarBool(TypeKick))
				ServerCommand("bot_kick %N",i);
			else 
				KickClient(i);
			CS_RespawnPlayer(client);
			SetEntityHealth(client, health);
			SetEntProp(client, Prop_Data, "m_ArmorValue", armor);
			TeleportEntity(client, pos, NULL_VECTOR, NULL_VECTOR);
			BotAmount[client]++;
		}
	}
}

public EventRoundStart(Handle:event, const String:name[], bool:dontBroadcast)
{
	new client = GetClientOfUserId(GetEventInt(event, "userid"));
	BotAmount[client]=0;
}

public OnClientDisconnect(client)
{
	if(CheckSpecTimer[client] != INVALID_HANDLE)
	{
		KillTimer(CheckSpecTimer[client]);
		CheckSpecTimer[client] = INVALID_HANDLE;
	}
}