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

#define PLUGIN "[JB] Wojna Gangow"
#define VERSION "1.0.6"
#define AUTHOR "Cypis"

#define TASK_HUD 6781252

new jakie_bronie[] = {CSW_AWP, CSW_AK47, CSW_M4A1, CSW_FAMAS, CSW_HEGRENADE, CSW_FLASHBANG};
new kolor[][] = {"Czerwony", "Zielony", "Niebieski"};
new kolor_gang[][3] = {
	{255,0,0},	//czerwony
	{0,255,0},	//zielony
	{0,0,255}	//niebieski
};

new user_gangi[MAX+1];

new HamHook:hSpawn, HamHook:fHamKill, HamHook:fHamTakeDamage[2];
new fmClientDisconnect;

new id_zabawa;
public plugin_init() {
	register_plugin(PLUGIN, VERSION, AUTHOR)
	
	id_zabawa = jail_register_game("Wojna Gangow");
}

public OnLastPrisonerShowWish(id)
{
	OnRemoveData(id_zabawa);
}

public OnRemoveData(day)
{
	if(day == id_zabawa)
	{
		if(hSpawn)
			DisableHamForward(hSpawn);
			
		if(fHamKill)
			DisableHamForward(fHamKill);
			
		if(fHamTakeDamage[0])
			DisableHamForward(fHamTakeDamage[0]);
			
		if(fHamTakeDamage[1])
			DisableHamForward(fHamTakeDamage[1]);
			
		if(fmClientDisconnect)
		{
			unregister_forward(FM_ClientDisconnect, fmClientDisconnect);
			fmClientDisconnect = 0;
		}
		
		if(task_exists(TASK_HUD))
			remove_task(TASK_HUD);
	}
}

public OnDayStartPre(day, szInfo[256], szInfo2[512], setting[10], gTimeRound)
{	
	static szTime[12];
	if(day == id_zabawa)
	{
		format_time(szTime, 11, "%M:%S", gTimeRound-60);
		formatex(szInfo2, 511, "Zasady:^n%s - walka wiezniow miedzy grupami^nOstatni wiezien ma zyczenie", szTime);
		szInfo = "Dzisiaj jest Wojna Gangow";
					
		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] = 1;
		setting[7] = 1;
	}
}

public OnDayStartPost(day)
{
	if(day == id_zabawa)
	{
		jail_open_cele();
		jail_set_game_hud(60, "Rozpoczecie zabawy za");
		
		for(new i=1; i<=MAX; i++)
		{
			UstawPrzydzial(i);
		}
		
		if(!hSpawn)
			hSpawn = RegisterHam(Ham_Spawn, "player", "fwSpawn", 1);
		else
			EnableHamForward(hSpawn);
			
		if(!fHamKill)
			fHamKill = RegisterHam(Ham_Killed, "player", "SmiercGraczaPost", 1);
		else
			EnableHamForward(fHamKill);
			
		if(!fHamTakeDamage[0])
			fHamTakeDamage[0] = RegisterHam(Ham_TakeDamage, "player", "TakeDamage");
		else
			EnableHamForward(fHamTakeDamage[0]);
			
		if(!fHamTakeDamage[1])
			fHamTakeDamage[1] = RegisterHam(Ham_TraceAttack, "player", "TraceAttack");
		else
			EnableHamForward(fHamTakeDamage[1]);
			
		if(!fmClientDisconnect)
			fmClientDisconnect = register_forward(FM_ClientDisconnect, "fwClientDisconnect");
	
		set_task(1.0, "PokazWiadomoscHud", TASK_HUD, .flags="b");
	}
}

public fwSpawn(id)
{
	UstawPrzydzial(id);
}

public UstawPrzydzial(id)
{
	if(!is_user_alive(id) || !is_user_connected(id))
		return;
			
	if(cs_get_user_team(id) != CS_TEAM_T)
		return;
		
	static przydzial = 0;
	for(new j=0; j<sizeof(jakie_bronie); j++)
	{
		new weaponname[24];
		get_weaponname(jakie_bronie[j], weaponname, 23);
		give_item(id, weaponname);
		
		if(jakie_bronie[j] != CSW_HEGRENADE && jakie_bronie[j] != CSW_FLASHBANG && jakie_bronie[j] != CSW_SMOKEGRENADE)
			cs_set_user_bpammo(id, jakie_bronie[j], 100);
	}
	
	user_gangi[id] = przydzial;
	set_user_rendering(id, kRenderFxGlowShell, kolor_gang[przydzial][0], kolor_gang[przydzial][1], kolor_gang[przydzial][2], kRenderNormal, 10);
			
	if(++przydzial >= 3)
		przydzial = 0;
}

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

public TakeDamage(id, ent, attacker, Float:damage, damagebits)
	return vTakeDamage(id, attacker);

public TraceAttack(id, attacker, Float:damage, Float:direction[3], tracehandle, damagebits)
	return vTakeDamage(id, attacker);

vTakeDamage(id, attacker)
{
	if(!is_user_connected(id) || !is_user_connected(attacker) || cs_get_user_team(id) != CS_TEAM_T || cs_get_user_team(attacker) != CS_TEAM_T)
		return HAM_IGNORED;
	
	if(user_gangi[attacker] == user_gangi[id])
		return HAM_SUPERCEDE;

	return HAM_IGNORED;
}

public fwClientDisconnect(id)
{
	SprawdzGangi();
}

public SmiercGraczaPost(id, attacker, shouldgib)
{	
	if(!is_user_connected(id) || cs_get_user_team(id) != CS_TEAM_T)
		return;
		
	SprawdzGangi();
}

public SprawdzGangi()
{
	new gang[3];
	for(new i=1; i<=MAX; i++)
	{
		if(!is_user_alive(i) || !is_user_connected(i))
			continue;
			
		if(cs_get_user_team(i) != CS_TEAM_T)
			continue;
			
		gang[user_gangi[i]]++;
	}
	
	if(!(gang[0] || gang[1]) || !(gang[1] || gang[2]) || !(gang[0] || gang[2]))
	{
		if(fHamTakeDamage[0])
			DisableHamForward(fHamTakeDamage[0]);
			
		if(fHamTakeDamage[1])
			DisableHamForward(fHamTakeDamage[1]);
	}
}

public PokazWiadomoscHud(taskid)
{
	utilAllHudColor();
}

stock utilAllHudColor()
{
	static SyncHudObj;
	if(!SyncHudObj)
		SyncHudObj = CreateHudSyncObj();
	
	set_hudmessage(255, 0, 0, 0.05, 0.40, .holdtime=1.0);
	for(new i=1; i<=32; i++)
	{
		if(!is_user_connected(i) || !is_user_alive(i))
			continue;
			
		if(cs_get_user_team(i) != CS_TEAM_T)
			continue;
			
		ShowSyncHudMsg(i, SyncHudObj, "Kolor: %s", kolor[user_gangi[i]]);
	}
}
