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

#define HITGROUP_HEAD (1 << 1)

new pcvar;
new id_headshot;

public plugin_init()
{
	pcvar = register_cvar("amx_onlyhs", "0");
	register_forward(FM_TraceLine, "Fwd_TraceLine");
	id_headshot = jail_register_game("Headshot Day");
}

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

public OnRemoveData(day)
{
	if(day == id_headshot)
	{
		server_cmd("amx_onlyhs 0");
		jail_set_ct_hit_tt(false);
		jail_set_god_ct(false);
	}
}

public OnDayStartPre(day, szInfo[256], szInfo2[512], setting[10], gTimeRound)
{ 
	static szTime[12];
	if(day == id_headshot)
	{
		format_time(szTime, 11, "%M:%S", gTimeRound-15);
                formatex(szInfo2, 511, "Zasady:^n%s - Walka miedzy wiezniami^nMozna zabic tylko strzalem w glowe^nOstatni wiezien ma zyczenie", szTime);
		szInfo = "Headshot Day";
 
		jail_set_prisoners_micro(true, true);
		jail_set_ct_hit_tt(true);
		jail_set_god_ct(true);
		server_cmd("amx_onlyhs 1");
 
		setting[0] = 1;
		setting[1] = 1;
		setting[2] = 1;
		setting[4] = 1;
		setting[6] = 0;
		setting[7] = 1;
	}
}

public OnDayStartPost(day)
{
	if(day == id_headshot)
	{
		jail_open_cele();
		jail_set_game_hud(30, "Rozpoczecie zabawy za");
	}
}

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

		for(new i=1; i<=MAX; i++)
		{
			if(!is_user_alive(i) || cs_get_user_team(i) != CS_TEAM_T)
				continue;

			give_item(i, "weapon_ak47");
			give_item(i, "weapon_awp");
			give_item(i, "weapon_scout");
			give_item(i, "weapon_famas");
			give_item(i, "weapon_deagle");
			give_item(i, "weapon_hegrenade");
			give_item(i, "weapon_flashbang");
		}
	}
}

public Fwd_TraceLine(Float:StartPos[3],Float:EndPos[3], SkipMonsters, id, Trace)
{
	if(!is_user_alive(id))
		return FMRES_IGNORED;

	if(!get_pcvar_num(pcvar))
		return FMRES_IGNORED;

	new Hited = get_tr2(Trace, TR_pHit);
	new HitGroup = (1 << get_tr2(Trace, TR_iHitgroup));
 
	if(!is_user_alive(Hited))
		return FMRES_IGNORED;
 
	if(!(HitGroup & HITGROUP_HEAD))
	{
		set_tr2(Trace, TR_flFraction, 1.0);
		return FMRES_SUPERCEDE;
	}
	return FMRES_IGNORED;
}