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

#define PLUGIN "Snowwar"
#define VERSION "1.0"
#define AUTHOR "cypis"

#define TASK_HUD 6781252
#define NADE_SNOW 3349

new id_zabawa;

new HamHook:hSpawn, HamHook:fHamKill, HamHook:fHamTakeDamage[2];
new fmClientDisconnect;
new HamHook:hItemDeployHE, fmSetModelGrenade, HamHook:hThinkGrenade, fmTouchGrenade;

new kolory[][] = {"Czerwony", "Zielony", "Niebieski", "Fioletowy", "Zolty", "Blekitny"};
new kolor_przydzial[][3] = {
	{255,0,0},	//czerwony
	{0,255,0},	//zielony
	{0,0,255},	//niebieski
	{255,0,255},	//fioletowy
	{255,255, 0},	//zolty
	{0,255,255}	//blekitny
};
new max_przydzial, user_przydzial[33];
new g_trailSpr, blood1, blood2;
public plugin_init()
{
	register_plugin(PLUGIN, VERSION, AUTHOR);
	
	id_zabawa = jail_register_game("SnowWar");
}

new g_Models[][] = {
	"models/jb_prosiak/sniezki/v.mdl",
	"models/jb_prosiak/sniezki/w.mdl",
	"models/jb_prosiak/sniezki/p.mdl"
}

new g_Dzwieki[][] = {
	"jb_prosiak/swieta_zabawy/swieta1.mp3",
	"jb_prosiak/swieta_zabawy/swieta2.mp3",
	"jb_prosiak/swieta_zabawy/swieta3.mp3",
	"jb_prosiak/swieta_zabawy/swieta4.mp3",
	"jb_prosiak/swieta_zabawy/swieta5.mp3",
	"jb_prosiak/swieta_zabawy/swieta6.mp3",
	"jb_prosiak/swieta_zabawy/swieta7.mp3",
	"jb_prosiak/swieta_zabawy/swieta8.mp3",
	"jb_prosiak/swieta_zabawy/swieta9.mp3"
}

public plugin_precache()
{
	for(new i=0; i<sizeof(g_Models); i++)
		precache_model(g_Models[i]);
	g_trailSpr = precache_generic("sprites/laserbeam.spr");
	precache_model("models/player/swietamikolajjb/swietamikolajjb.mdl");
	blood1 = precache_model("sprites/blood.spr");
	blood2 = precache_model("sprites/bloodspray.spr");
	for (new i=0; i<sizeof(g_Dzwieki); i++)
		precache_sound(g_Dzwieki[i]);
}

public OnLastPrisonerShowWish(id)
{
	if(jail_get_play_game_id() == id_zabawa)
	{
		OnRemoveData(id_zabawa);
		
		strip_user_weapons(id);
		give_item(id, "weapon_knife");
		
		fm_set_rendering(id);
	}
}

public OnRemoveData(day)
{
	if(day == id_zabawa)
	{
		SetZabawa(false);
		client_cmd(0, "mp3 stop");
	}
}

public OnDayStartPre(day, szInfo[256], szInfo2[512], setting[10], gTimeRound)
{	
	static szTime[12];
	if(day == id_zabawa)
	{
		format_time(szTime, 11, "%M:%S", gTimeRound-30);
		formatex(szInfo2, 511, "Zasady:^n%s - walka wiezniow miedzy grupami^nOstatni wiezien ma zyczenie", szTime);
		szInfo = "Dzisiaj jest SnowWar";
					
		jail_set_prisoners_micro(true, true);
		jail_set_ct_hit_tt(true);
		jail_set_god_ct(true);
		SetZabawa(true);
		
		setting[0] = 1;
		setting[1] = 1;
		setting[2] = 1;
		setting[4] = 1;
		setting[6] = 1;
		setting[7] = 1;
		setting[8] = 1;
		new losowa = random_num(0,sizeof(g_Dzwieki)-1)
		client_cmd(0, "mp3 loop sound/%s", g_Dzwieki[losowa]);
	}
}

new bool:koniec;
public OnDayStartPost(day)
{
	if(day == id_zabawa)
	{
		koniec = false;
		max_przydzial = IloscGraczy();
		
		jail_open_cele();
		jail_set_game_hud(30, "Rozpoczecie zabawy za");
	
		for(new i=1; i<=MAX; i++)
		{
			UstawPrzydzial(i);
		}
	}
}

public OnGameHudEnd(day)
{
	if(day == id_zabawa)
	{
		if(!koniec)
		{
			koniec = true;
			jail_set_game_hud(300, "Koniec rundy za");
			
			set_hudmessage(255, 0, 0, -1.0, -1.0, 0, 6.0, 5.0);
			show_hudmessage(0, "== BITWA ==");
				
			jail_set_prisoners_fight(true, false, false);
		}
		else
		{
			koniec = false;	
			
			for(new i=1; i<=MAX; i++)
			{
				if(!is_user_alive(i) || !is_user_connected(i))
					continue;
						
				if(get_user_team(i) != 1)
					continue;

				user_kill(i);
			}
		}
	}
}

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

public UstawPrzydzial(id)
{
	if(!is_user_alive(id) || !is_user_connected(id))
		return;
			
	if(get_user_team(id) != 1)
		return;
	cs_set_user_model(id, "swietamikolajjb");
	strip_user_weapons(id);
	give_item(id, "weapon_hegrenade");

	static przydzial = 0;
	user_przydzial[id] = przydzial;
	fm_set_rendering(id, kRenderFxGlowShell, kolor_przydzial[przydzial][0], kolor_przydzial[przydzial][1], kolor_przydzial[przydzial][2], kRenderNormal, 10);
		
	if(++przydzial >= max_przydzial)
		przydzial = 0;
}

public SetZabawa(bool:wartosc)
{
	SetGrenade(wartosc);
	if(wartosc)
	{
		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");
	}
	else
	{
		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 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(get_user_team(i) != 1)
			continue;
			
		ShowSyncHudMsg(i, SyncHudObj, "Kolor: %s", kolory[user_przydzial[i]]);
	}
}

/////////////////////////////////
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) || get_user_team(id) != 1 || get_user_team(attacker) != 1)
		return HAM_IGNORED;
	
	if(user_przydzial[attacker] == user_przydzial[id])
		return HAM_SUPERCEDE;

	return HAM_IGNORED;
}

public fwClientDisconnect(id)
{
	SprawdzPrzydzial();
}

public SmiercGraczaPost(id, attacker, shouldgib)
{	
	if(!is_user_connected(id) || get_user_team(id) != 1)
		return;
	
	SprawdzPrzydzial();
}

public SprawdzPrzydzial()
{
	new ostatni_przydzial = -1;
	for(new i=1; i<=MAX; i++)
	{
		if(!is_user_alive(i) || !is_user_connected(i))
			continue;
			
		if(get_user_team(i) != 1)
			continue;
			
		if(ostatni_przydzial != -1 && ostatni_przydzial != user_przydzial[i]) //SPRAWDZENIE CZY SĄ INNE GRUPY
			return 0;

		ostatni_przydzial = user_przydzial[i];
	}
	if(ostatni_przydzial != -1)
	{
		if(fHamTakeDamage[0])
			DisableHamForward(fHamTakeDamage[0]);
				
		if(fHamTakeDamage[1])
			DisableHamForward(fHamTakeDamage[1]);
		
		return 1;
	}
	return 0;
}

//////////////////////////////////
public SetGrenade(bool:wartosc)
{
	if(wartosc)
	{
		if(!hItemDeployHE)
			hItemDeployHE = RegisterHam(Ham_Item_Deploy, "weapon_hegrenade", "fwWeaponHE", 1);
		else
			EnableHamForward(hItemDeployHE);
		
		if(!hThinkGrenade)
			hThinkGrenade = RegisterHam(Ham_Think, "grenade", "fwThinkGrenade");
		else
			EnableHamForward(hThinkGrenade);
		
		if(!fmSetModelGrenade)
			fmSetModelGrenade = register_forward(FM_SetModel, "fwSetModelGrenade");
		
		if(!fmTouchGrenade)
			fmTouchGrenade = register_forward(FM_Touch, "fwTouchGrenade");
	}
	else
	{
		if(hItemDeployHE)
			DisableHamForward(hItemDeployHE);
		
		if(hThinkGrenade)
			DisableHamForward(hThinkGrenade);
			
		if(fmSetModelGrenade)
		{
			unregister_forward(FM_SetModel, fmSetModelGrenade);
			fmSetModelGrenade = 0;
		}
		
		if(fmTouchGrenade)
		{
			unregister_forward(FM_Touch, fmTouchGrenade);
			fmTouchGrenade = 0;
		}
	}
}

public fwWeaponHE(ent)
{
	if(!pev_valid(ent))
		return;
	
	new id = get_pdata_cbase(ent, 41, 4);
	if(!is_user_alive(id) || get_user_team(id) != 1)
		return;

	set_pev(id, pev_viewmodel2, g_Models[0]);	
	set_pev(id, pev_weaponmodel2, g_Models[1]);
}

public fwThinkGrenade(ent)
{   
	if(!pev_valid(ent)) 
		return HAM_IGNORED;

	new Float:dmgtime;
	pev(ent, pev_dmgtime, dmgtime);

	if(dmgtime > get_gametime())
		return HAM_IGNORED;

	if(pev(ent, pev_flTimeStepSound) == NADE_SNOW)
		return HAM_SUPERCEDE;

	return HAM_IGNORED;
}

public fwSetModelGrenade(ent, const model[])
{
	if(!pev_valid(ent)) 
		return HAM_IGNORED;
	
	new Float:dmgtime;
	pev(ent, pev_dmgtime, dmgtime);

	if(dmgtime == 0.0)
		return HAM_IGNORED;

	if(!equal(model,"models/w_hegrenade.mdl"))
		return HAM_IGNORED;
	
	fm_entity_set_model(ent, g_Models[2]);
	
	new id = pev(ent, pev_owner);
	
	new Float:fVelocity[3]
	velocity_by_aim(id, 1000, fVelocity);
	set_pev(ent, pev_velocity, fVelocity);
	set_pev(ent, pev_gravity, 0.4);
	
	MakeTrail(ent, kolor_przydzial[user_przydzial[id]]);
	set_pev(ent, pev_flTimeStepSound, NADE_SNOW);
	
	if(is_user_alive(id))
		cs_set_user_bpammo(id, CSW_HEGRENADE, 2);
		
	return HAM_SUPERCEDE;
}

public fwTouchGrenade(ent, id)
{
	if(!pev_valid(ent))
		return;

	new szClass[32];
	pev(ent, pev_model, szClass, 31);

	if(!equal(szClass, g_Models[2]))
		return;
		
	if(is_user_alive(id))
	{
		ExecuteHamB(Ham_TakeDamage, id, ent, pev(ent, pev_owner), 150.0, DMG_BULLET);
	}
	sw_effect(ent);
	fm_remove_entity(ent);
}

stock IloscGraczy()
{
	switch(get_playersnum(1))
	{
		case 0..10: return 2;
		case 11..20: return 3;
		case 21..32: return 4;
	}
	return 2;
}

stock sw_effect(ent)
{
	new Float:Origin[3];
	pev(ent, pev_origin, Origin);
	
	message_begin(MSG_BROADCAST, SVC_TEMPENTITY);
	write_byte(TE_BLOODSPRITE);
	write_coord(floatround(Origin[0]));
	write_coord(floatround(Origin[1]));
	write_coord(floatround(Origin[2]));
	write_short(blood2);
	write_short(blood1);
	write_byte(255);
	write_byte(15);
	message_end();
}

stock MakeTrail(ent, colors[3])
{
	message_begin(MSG_BROADCAST, SVC_TEMPENTITY);
	write_byte(TE_BEAMFOLLOW);
	write_short(ent);
	write_short(g_trailSpr);
	write_byte(10);
	write_byte(10);
	write_byte(colors[0]);
	write_byte(colors[1]);
	write_byte(colors[2]);
	write_byte(500);
	message_end();
}
/* AMXX-Studio Notes - DO NOT MODIFY BELOW HERE
*{\\ rtf1\\ ansi\\ deff0{\\ fonttbl{\\ f0\\ fnil Tahoma;}}\n\\ viewkind4\\ uc1\\ pard\\ lang1045\\ f0\\ fs16 \n\\ par }
*/
