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

#define PLUGIN "Jail Zyczenia"
#define VERSION "1.0.6b"
#define AUTHOR "Cypis"

/////////// Pojedynek ///////////
new bronie_pojedynek, pojedynek[2];
new HamHook:fHamKill, HamHook:fHamDamage, HamHook:fHamTrace, HamHook:fHamWeapon[31], fDropGranade, fDotykKnife;
/////////// Pojedynek ///////////

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 id_bezruch, id_freeday, id_duszek, id_rambomod, id_scouty, id_deagle, id_m4a1, id_ak47, id_noze, id_he, id_usp, id_awp, id_glock, id_famas, id_aug;
new blood, blood2;

public plugin_init() {
	register_plugin(PLUGIN, VERSION, AUTHOR)
	
	id_bezruch = jail_register_wish("Bezruch");
	id_freeday = jail_register_wish("FreeDay");
	id_duszek = jail_register_wish("Duszek");
	id_rambomod = jail_register_wish("RamboMod");
	id_scouty = jail_register_wish("Pojedynek na Scouty");
	id_deagle = jail_register_wish("Pojedynek na Deagle");
	id_m4a1 = jail_register_wish("Pojedynek na M4A1");
	id_ak47 = jail_register_wish("Pojedynek na AK47");
	id_noze = jail_register_wish("Pojedynek na NOZE");
	id_he = jail_register_wish("Pojedynek na HE");
	id_usp = jail_register_wish("Pojedynek na USP");
        id_awp = jail_register_wish("Pojedynek na AWP");
        id_glock = jail_register_wish("Pojedynek na Glocki");
        id_famas = jail_register_wish("Pojedynek na Famasy");
        id_aug = jail_register_wish("Pojedynek na AUG");
}

public plugin_precache()
{
	blood = precache_model("sprites/blood.spr");
	blood2 = precache_model("sprites/bloodspray.spr");
	
	precache_model("models/w_throw.mdl");
	
	precache_sound("jb_cypis/rambo.wav");
	precache_sound("jb_cypis/pojedynki.wav");
}

public OnRemoveData(day)
{
	/////////// Pojedynek ///////////
	if(pojedynek[0] || pojedynek[1])
	{
		pojedynek[0] = 0;
		pojedynek[1] = 0;
		RegisterHams(false);
	}
	bronie_pojedynek = 0;
	/////////// Pojedynek ///////////
}

public OnLastPrisonerTakeWish(id, zyczenie)
{
	if(zyczenie == id_bezruch)
	{
		strip_user_weapons(id);
		give_item(id, "weapon_knife");
		give_item(id, "weapon_deagle")
		cs_set_user_bpammo(id, CSW_DEAGLE, maxAmmo[CSW_DEAGLE]);
		
		jail_set_ct_hit_tt(true);
		
		for(new i=1; i<=32; i++)
		{
			if(!is_user_alive(i) || !is_user_connected(i) || cs_get_user_team(i) != CS_TEAM_CT)
				continue;
		
			strip_user_weapons(i);
			give_item(i, "weapon_knife");
			jail_set_user_speed(i, 0.1);
		}
	}
	else if(zyczenie == id_freeday)
	{
		if(jail_get_days() == CZWARTEK || jail_get_days() == SOBOTA) //w tych dniach nie mozna wziac fd
			return JAIL_HANDLED;
		
		user_kill(id);
		jail_set_prisoner_free(id);	
	}
	else if(zyczenie == id_duszek)
	{
		if(jail_get_days() == CZWARTEK || jail_get_days() == SOBOTA) //w tych dniach nie mozna wziac duszka
			return JAIL_HANDLED;
			
		user_kill(id);
		jail_set_prisoner_ghost(id);
	}
	else if(zyczenie == id_rambomod)
	{
		client_cmd(0, "spk jb_cypis/rambo.wav");
		set_hudmessage(255, 0, 0, -1.0, -1.0, 0, 6.0, 4.0);
		show_hudmessage(0, "RamboMod aktywny!");
		
		set_user_health(id, 1500);
		
		strip_user_weapons(id);
		give_item(id, "weapon_knife");
		give_item(id, "weapon_m249");
		cs_set_user_bpammo(id, CSW_M249, maxAmmo[CSW_M249]);
	}
	else if(zyczenie == id_scouty)
	{
		pojedynek[0] = id;
		bronie_pojedynek = CSW_SCOUT;
			
		MenuPojedynek(id);
	}
	else if(zyczenie == id_deagle)
	{
		pojedynek[0] = id;
		bronie_pojedynek = CSW_DEAGLE;
			
		MenuPojedynek(id);
	}
	else if(zyczenie == id_m4a1)
	{
		pojedynek[0] = id;
		bronie_pojedynek = CSW_M4A1;
		
		MenuPojedynek(id);
	}
	else if(zyczenie == id_ak47)
	{
		pojedynek[0] = id;
		bronie_pojedynek = CSW_AK47;
			
		MenuPojedynek(id);
	}
	else if(zyczenie == id_noze)
	{
		pojedynek[0] = id;
		bronie_pojedynek = CSW_KNIFE;
			
		MenuPojedynek(id);
	}
	else if(zyczenie == id_he)
	{
		pojedynek[0] = id;
		bronie_pojedynek = CSW_HEGRENADE;
			
		MenuPojedynek(id);
	}
	else if(zyczenie == id_usp)
	{
		pojedynek[0] = id;
		bronie_pojedynek = CSW_USP;
			
		MenuPojedynek(id);
	}
        else if(zyczenie == id_awp)
	{
		pojedynek[0] = id;
		bronie_pojedynek = CSW_AWP;
			
		MenuPojedynek(id);
        }
        else if(zyczenie == id_glock)
	{
		pojedynek[0] = id;
		bronie_pojedynek = CSW_GLOCK18;
			
		MenuPojedynek(id);
        }
        else if(zyczenie == id_famas)
	{
		pojedynek[0] = id;
		bronie_pojedynek = CSW_FAMAS;
			
		MenuPojedynek(id);
        }
        else if(zyczenie == id_aug)
	{
		pojedynek[0] = id;
		bronie_pojedynek = CSW_AUG;
			
		MenuPojedynek(id);
	}
	return JAIL_CONTINUE;
}	

/////////// Pojedynek ///////////
public MenuPojedynek(id)
{
	new menu = menu_create("Pojedynek Z:", "Handel_Pojedynek");
	for(new i=1; i<=32; i++)
	{
		if(!is_user_alive(i) || !is_user_connected(i) || cs_get_user_team(i) != CS_TEAM_CT)
			continue;
		new name[64];
		get_user_name(i, name, 63);
		menu_additem(menu, name);
	}
	menu_setprop(menu, MPROP_EXITNAME, "\dWyjdz");
	menu_setprop(menu, MPROP_BACKNAME, "Poprzednia strona");
	menu_setprop(menu, MPROP_NEXTNAME, "Nastepna strona");
	menu_display(id, menu);
}

public Handel_Pojedynek(id, menu, item)
{
	if(pojedynek[0] != id || pojedynek[1] || !is_user_alive(id))
		return;
	
	if(item == MENU_EXIT)
	{
		menu_display(id, menu);
		return;
	}
	
	new acces, callback, data[3], szName2[64];
	menu_item_getinfo(menu, item, acces, data, 2, szName2, 63, callback);
	
	pojedynek[1] = get_user_index(szName2);
	if(!is_user_alive(pojedynek[1]) || !is_user_connected(pojedynek[1]))
	{
		pojedynek[1] = 0;
		MenuPojedynek(id);
		return;
	}
	new szName[32];
	get_user_name(id, szName, 31);
	
	ColorChat(0, TEAM_COLOR, "^x04***JailBreak***^x03 %s^x01 walczy z^x03 %s", szName, szName2);
	client_cmd(0, "spk jb_cypis/pojedynki.wav");
	
	RegisterHams(true);
	
	set_user_rendering(id, kRenderFxGlowShell, 255, 0, 0, kRenderNormal, 10);
	set_user_rendering(pojedynek[1], kRenderFxGlowShell, 0, 0, 255, kRenderNormal, 10);
	
	jail_set_user_block(pojedynek[0], true)
	jail_set_user_block(pojedynek[1], true);
	
	set_user_health(id, 100);
	set_user_health(pojedynek[1], 100);
	
	strip_user_weapons(id);
	strip_user_weapons(pojedynek[1]);
	
	if(bronie_pojedynek == CSW_G3SG1)
	{
		server_cmd("jail_add_rpg %d", id);
		server_cmd("jail_add_rpg %d", pojedynek[1]);
		return;
	}
	
	new weapon_name[24];
	get_weaponname(bronie_pojedynek, weapon_name, 23);
	
	new ent = give_item(id, weapon_name);
	new ent2 = give_item(pojedynek[1], weapon_name);
	
	if(bronie_pojedynek == CSW_KNIFE)
	{
		set_pev(id, pev_viewmodel2, "models/v_knife.mdl");
		set_pev(id, pev_weaponmodel2, "models/p_knife.mdl");
		
		set_pev(pojedynek[1], pev_viewmodel2, "models/v_knife.mdl");
		set_pev(pojedynek[1], pev_weaponmodel2, "models/p_knife.mdl");
		return;
	}
	
	if(bronie_pojedynek != CSW_HEGRENADE)
	{
		cs_set_weapon_ammo(ent, 1);
		cs_set_weapon_ammo(ent2, 1);
	}
}

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

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

vTracerAttack(id, attacker, ent)
{
	if(!pojedynek[0] || !is_user_connected(id) || !is_user_connected(attacker) || id == attacker)
		return HAM_IGNORED;
	
	if(pojedynek[0] == id && pojedynek[1] != attacker)
		return HAM_SUPERCEDE;
		
	if(pojedynek[0] == attacker && pojedynek[1] != id)
		return HAM_SUPERCEDE;	
		
	if(bronie_pojedynek == CSW_KNIFE && ent != -1)
	{
		if(ent == attacker)
			return HAM_SUPERCEDE;

		new Float:ViewAngle[3], iOrigin[3];
		for(new i=0; i<3; i++) 
			ViewAngle[i] = random_float(-50.0, 50.0);

		get_user_origin(id, iOrigin);
		message_begin(MSG_BROADCAST, SVC_TEMPENTITY);
		write_byte(TE_BLOODSPRITE);
		write_coord(iOrigin[0]);
		write_coord(iOrigin[1]);
		write_coord(iOrigin[2]);
		write_short(blood2);
		write_short(blood);
		write_byte(229);
		write_byte(25);
		message_end();
			
		set_pev(id, pev_punchangle, ViewAngle);
		SetHamParamEntity(2, attacker);
		return HAM_IGNORED;
	}
	return HAM_IGNORED;
}

public SmiercGraczaPost(id, attacker, shouldgib)
{	
	if(!is_user_connected(id) || id != pojedynek[1])
		return HAM_IGNORED;
		
	jail_set_user_block(id, false);
	SzukajPrzeciwnika();
	return HAM_IGNORED;
}

public WeaponAttack(ent)
{
	new id = get_pdata_cbase(ent, 41, 4);
	if(pojedynek[0] == id || pojedynek[1] == id)
	{
		if(bronie_pojedynek == CSW_KNIFE)
			StworzKnife(id);
		else
			cs_set_user_bpammo(id, bronie_pojedynek, 1);
	}
}		

public client_disconnect(id)
{
	if(pojedynek[1] == id)
		SzukajPrzeciwnika();
}

public SzukajPrzeciwnika()
{
	pojedynek[1] = RandomPlayer(2);
	if(!pojedynek[1])
		return;
	
	if(is_user_alive(pojedynek[0]))
		set_user_health(pojedynek[0], 100);
		
	set_user_rendering(pojedynek[1], kRenderFxGlowShell, 0, 0, 255, kRenderNormal, 10);	
		
	jail_set_user_block(pojedynek[1], true);
	set_user_health(pojedynek[1], 100);
	strip_user_weapons(pojedynek[1]);
		
	if(bronie_pojedynek == CSW_G3SG1)	
	{
		server_cmd("jail_add_rpg %d", pojedynek[1]);
		return;
	}
		
	new weapon_name[24]
	get_weaponname(bronie_pojedynek, weapon_name, 23);	
	new ent = give_item(pojedynek[1], weapon_name);
	
	if(bronie_pojedynek == CSW_KNIFE)
	{
		set_pev(pojedynek[1], pev_viewmodel2, "models/v_knife.mdl");
		set_pev(pojedynek[1], pev_weaponmodel2, "models/p_knife.mdl");
		return;
	}
	
	if(bronie_pojedynek != CSW_HEGRENADE)
		cs_set_weapon_ammo(ent, 1);
}

public RegisterHams(bool:wartosc)
{
	if(wartosc)
	{
		if(fHamKill)
			EnableHamForward(fHamKill);
		else
			fHamKill = RegisterHam(Ham_Killed, "player", "SmiercGraczaPost", 1);
		
		if(fHamDamage)
			EnableHamForward(fHamDamage);
		else
			fHamDamage = RegisterHam(Ham_TakeDamage, "player", "TakeDamage");
			
		if(fHamTrace)
			EnableHamForward(fHamTrace);
		else
			fHamTrace = RegisterHam(Ham_TraceAttack, "player", "TraceAttack");
			

		if(bronie_pojedynek != CSW_HEGRENADE && bronie_pojedynek != CSW_G3SG1)
		{
			if(fHamWeapon[bronie_pojedynek])
				EnableHamForward(fHamWeapon[bronie_pojedynek]);
			else
			{
				new WeaponName[24];
				get_weaponname(bronie_pojedynek, WeaponName, 23);
				fHamWeapon[bronie_pojedynek] = RegisterHam(Ham_Weapon_PrimaryAttack, WeaponName, "WeaponAttack", 1);
			}
		}
		
		if(bronie_pojedynek == CSW_HEGRENADE && !fDropGranade)
			fDropGranade = register_forward(FM_SetModel, "SetModel", 1);
		
		if(bronie_pojedynek == CSW_KNIFE && !fDotykKnife)
			fDotykKnife = register_forward(FM_Touch, "TouchKnife");
	}
	else
	{
		if(fHamKill)
			DisableHamForward(fHamKill);
			
		if(fHamDamage)
			DisableHamForward(fHamDamage);
			
		if(fHamTrace)
			DisableHamForward(fHamTrace);	
			
		if(fHamWeapon[bronie_pojedynek])
			DisableHamForward(fHamWeapon[bronie_pojedynek]);
			
		if(fDropGranade)
		{
			unregister_forward(FM_SetModel, fDropGranade, 1);
			fDropGranade = 0;
		}
			
		if(fDotykKnife)
		{
			unregister_forward(FM_Touch, fDotykKnife);
			fDotykKnife = 0;
		}
	}
}
/////////// Pojedynek ///////////

public SetModel(ent, model[])
{
	if(!pev_valid(ent)) 
		return FMRES_IGNORED;

	if(!equali(model, "models/w_hegrenade.mdl")) 
		return FMRES_IGNORED;

	new id = pev(ent, pev_owner);
	if(pojedynek[0] == id || pojedynek[1] == id)
		cs_set_user_bpammo(id, CSW_HEGRENADE, 2);
		
	return FMRES_IGNORED;
}

public TouchKnife(ent, id)
{
	if(!pev_valid(ent))
		return FMRES_IGNORED
	
	new class[32];
	pev(ent, pev_classname, class, 31);

	if(!equal(class, "throw_knife"))
		return FMRES_IGNORED;
		
	if((0 < id <= MAX) && is_user_alive(id))
	{
		new attacker = pev(ent, pev_owner);
		ExecuteHamB(Ham_TakeDamage, id, ent, attacker, 30.0, DMG_BULLET);
	}
	engfunc(EngFunc_RemoveEntity, ent);
	return FMRES_IGNORED;
}

public StworzKnife(id)
{
	new ent = engfunc(EngFunc_CreateNamedEntity, engfunc(EngFunc_AllocString, "info_target"));
	new Float:vAngles[3], Float:nVelocity[3], Float:vOriginf[3], vOrigin[3];
		
	set_pev(ent, pev_owner, id);
	set_pev(ent, pev_classname, "throw_knife");
	engfunc(EngFunc_SetModel, ent, "models/w_throw.mdl");
	set_pev(ent, pev_gravity, 0.25);	
	
	get_user_origin(id, vOrigin, 1);
	IVecFVec(vOrigin, vOriginf);
	set_pev(ent, pev_origin,vOriginf);
	
	static Float:player_angles[3];
	pev(id, pev_angles, player_angles);
	player_angles[2] = 0.0;
	set_pev(ent, pev_angles, player_angles);
	
	pev(id, pev_v_angle, vAngles);
	set_pev(ent, pev_v_angle, vAngles);
	
	pev(id, pev_view_ofs, vAngles);
	set_pev(ent, pev_view_ofs, vAngles);

	set_pev(ent, pev_movetype, MOVETYPE_TOSS);
	set_pev(ent, pev_solid, 2);
	
	velocity_by_aim(id, 700, nVelocity);	
	set_pev(ent, pev_velocity, nVelocity);
	
	set_pev(ent, pev_effects, pev(ent, pev_effects) & ~EF_NODRAW);
	set_pev(ent, pev_sequence, 0);
	set_pev(ent, pev_framerate, 1.0);
}

