#include <amxmodx>
#include <amxmisc>
#include <fun>
#include <cstrike>
#include <jailbreak>
#include <hamsandwich>

#define PLUGIN "[Jail] KillDay"
#define VERSION "1.0.6"
#define AUTHOR "Cypis"

new const maxAmmo[31] = {0,52,0,90,1,31,1,100,90,1,120,100,100,90,90,90,100,120,30,120,200,31,90,120,90,2,35,90,90,0,100};
new const idWeapons[] = {3,5,7,8,12,13,14,15,18,19,20,21,22,23,27,28,30};

new id_killday;
new bool:zmienna;
public plugin_init() {
	register_plugin(PLUGIN, VERSION, AUTHOR)
	
	id_killday = jail_register_game("KillDay");
}

public OnLastPrisonerTakeWish(id){
	OnRemoveData(id_killday);
}
public OnRemoveData(day)
{
	Zabawa(false)
}
public OnDayStartPre(day, szInfo[256], szInfo2[512], setting[10], gTimeRound)
{	
	static szTime[12];
	if(day == id_killday)
	{
		static szTimes[12];
		format_time(szTime, 11, "%M:%S", gTimeRound-30);
		format_time(szTimes, 11, "%M:%S", gTimeRound-60);
		szInfo = "Dzisiaj jest KillDay";
		
		jail_set_prisoners_micro(true, true);
		jail_set_ct_hit_tt(true);
		jail_set_god_ct(true);
		
		setting[0] = 1;
		setting[1] = 1;
		setting[2] = 1;
		setting[4] = 1;
		setting[6] = 0;
		setting[7] = 1;
	}
}

public OnDayStartPost(day)
{
	if(day == id_killday)
	{
		Zabawa(true);
		jail_open_cele();
		jail_set_game_hud(60, "Rozpoczecie zabawy za");
	}
}

public OnGameHudTick(day, count)
{
	if(count != 30)
		return;
	
	if(day != id_killday)
		return;
	
	new nameweapon[24], wid = idWeapons[random(charsmax(idWeapons))];
	get_weaponname(wid, nameweapon, 23);
	
	for(new i=1; i<=MAX; i++)
	{
		if(!is_user_alive(i) || !is_user_connected(i) || cs_get_user_team(i) != CS_TEAM_T)
			continue;
		
		strip_user_weapons(i);
		give_item(i, "weapon_knife");
		give_item(i, "weapon_deagle");
		give_item(i, nameweapon);
		
		cs_set_user_bpammo(i, wid, maxAmmo[wid]);
		cs_set_user_bpammo(i, CSW_DEAGLE, maxAmmo[CSW_DEAGLE]);
	}
}

public OnGameHudEnd(day)
{
	if(day == id_killday)
	{
		set_hudmessage(255, 0, 0, -1.0, -1.0, 0, 6.0, 5.0);
		show_hudmessage(0, "== TT vs TT ==");
		
		jail_set_prisoners_fight(true, false, false);
	}
}

new HamHook:hKilled;
public Zabawa(bool:wartosc)
{
	if(wartosc)
	{
		zmienna = true;
		if(!hKilled)
			RegisterHam(Ham_Killed, "player", "Ham_Killed_Post", 1);
		else
			EnableHamForward(hKilled);
	}
	else
	{
		zmienna = false;
		if(hKilled)
			DisableHamForward(hKilled);
	}
}

public Ham_Killed_Post(vid, kid){
	if(!zmienna)
		return PLUGIN_CONTINUE;
	new groupsNum=0;	// tutaj bedziemy przechowywac liczbe tych grup w tablicy groups[]
	
	for(new id=1; id<=32; ++id){
		if( is_user_alive(id) &&get_user_team(id)==1)
			++groupsNum;
	}	
	if(groupsNum==2){
		for(new i=1; i<=32; i++){
			if(is_user_alive(i) && get_user_team(i) == 1){
				strip_user_weapons(i);
				give_item(i, "weapon_knife");
				set_user_health(i, 200);
				
			}
		}
	}
	return PLUGIN_CONTINUE
}