#pragma semicolon 1

#include <sourcemod>
#include <sdktools>
#include <multicolors>

public Plugin:myinfo = {
	name = "BombSite Lock",
	author = "naXe",
	description = "Blokuje X bombsite, gdy jest mniej niż Y CT",
	version = "0.1",
	url = "http://topmasters.pl/"
};

new g_iBombSiteEnt[2];
new bool:g_bBombSiteStatus;
new String:g_szMapName[32];
new String:g_szCvarBS[10];
new g_iCvarCT;
new Handle:g_hCvarCT;
new Handle:g_hCvarBS;

public OnPluginStart() {
	CreateConVar("sm_bs_lock_version", "0.1", "Plugin version",  FCVAR_REPLICATED|FCVAR_NOTIFY);
	g_hCvarCT = CreateConVar("sm_bs_lock_min", "5", "Jesli graczy w CT jest mniej niz wartosc tego cvara to zablokuje BS'a", FCVAR_NONE, true, 1.0);
	g_hCvarBS = CreateConVar("sm_bs_lock_bs_name", "B", "Nazwa zablokowanego BS'a");

	HookEvent("round_start", RoundStart, EventHookMode_Post);
	HookEvent("bomb_pickup", BombPickup);
}

public OnMapStart() {
	g_iCvarCT = GetConVarInt(g_hCvarCT);
	GetConVarString(g_hCvarBS, g_szCvarBS, 9);
	
	new iBombSite[2];
	GetCurrentMap(g_szMapName, 32);

	if(StrEqual(g_szMapName, "de_dust2") || StrEqual(g_szMapName, "de_chateau") || StrEqual(g_szMapName, "de_train")) {
		iBombSite[0] = 1;
		iBombSite[1] = 0;
	} else {
		iBombSite[0] = 0;
		iBombSite[1] = 1;
	}
	g_iBombSiteEnt[iBombSite[0]] = FindEntityByClassname(-1, "func_bomb_target");
	g_iBombSiteEnt[iBombSite[1]] = FindEntityByClassname(g_iBombSiteEnt[iBombSite[0]], "func_bomb_target");
}

public Action:RoundStart(Handle:event, const String:name[], bool:dontBroadcast) {
	new bool:bLockState = GetTeamClientCount(3) < g_iCvarCT ? true : false;

	AcceptEntityInput(g_iBombSiteEnt[1], bLockState ? "Disable" : "Enable");
	g_bBombSiteStatus = bLockState;
	if(bLockState) CPrintToChatAll("\x04BombSite %s\x01 został zablokowany, ponieważ jest mniej niż\x02 %i CT!", g_szCvarBS, g_iCvarCT);
}

public Action:BombPickup(Handle:event, const String:name[], bool:dontBroadcast) {
	new id = GetClientOfUserId(GetEventInt(event, "userid"));

	if(!(1 <= id <= MaxClients) || !IsPlayerAlive(id) || !g_bBombSiteStatus) return;

	CPrintToChat(id, "\x03WAŻNE!\x01 BombSite %s jest zablokowany!", g_szCvarBS);
}