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

#define PLUGIN "Jail Menu"
#define VERSION "1.0.6"
#define AUTHOR "Cypis"
#define TAG "JailBreak"

new bool:menu_mikro, bool:menu_walka;
new w_trakcie[33];
new gracz_id[33],wybrany;
new autobh = 0;
new weapon_id;

public plugin_init() 
{
	register_plugin(PLUGIN, VERSION, AUTHOR)
	
	register_event("Damage", "Damage", "b", "2!=0");
	register_event("ResetHUD", "SpawnPlayer", "be")
	
	register_clcmd("+revision", "wlacz_rewizje");
	register_clcmd("-revision", "wylacz_rewizje");
	register_clcmd("say /menu", "MenuGraczy");
	register_clcmd("jail_amenu", "MenuAdmina", ADMIN_LEVEL_A);
	register_clcmd("say /a", "MenuAdmina", ADMIN_LEVEL_A);
	register_clcmd("say /dajdeagla", "menu")
	register_clcmd("say /podziel", "Podziel")
}

public plugin_precache()
{
	precache_sound("weapons/c4_disarm.wav");
	precache_sound("weapons/c4_disarmed.wav");
	precache_sound("JailBreak[k]/dzwonek.wav");
}
public SpawnPlayer(id)
{
	w_trakcie[id] = 0;
	remove_task(5000+id);
}
public MenuGraczy(id)
{
	if(!is_user_alive(id))
	{
		MenuGraczaNieZywego(id);
		return PLUGIN_HANDLED;
	}
	
	switch(get_user_team(id))
	{
		case 1: MenuGraczaTT(id);
		case 2: MenuGraczaCT(id);
	}
	return PLUGIN_HANDLED;
}

public MenuGraczaTT(id)
{
	new menu = menu_create("\dMenu Wieznia by: \rdraxe", "Handel_Menu");
	new cb = menu_makecallback("Menu_Callback");	

	menu_additem(menu, "\wCzapki \y- Wybierz czapke", 		"3");
	menu_additem(menu, "\wSklep \y- Sklep za szlugi", 		"19");
	menu_additem(menu, "\wUkradnij bron \y- Kradziez broni CT", 	"4");
	menu_additem(menu, "\wRuletka \y- Wylosuj bonus", 		"20");
	menu_additem(menu, "\rZyczenie \y- Dla ostatniego wieznia", 	"18", 4, cb);

	menu_additem(menu, "\dVIP INFO", 	"0");
	menu_additem(menu, "\dSuperVIP INFO", 	"1");
	menu_additem(menu, "\dRegulamin", 		"2");
	
	
	menu_setprop(menu, MPROP_EXITNAME, "Wyjdz");
	menu_setprop(menu, MPROP_BACKNAME, "Poprzednia");
	menu_setprop(menu, MPROP_NEXTNAME, "Nastepna");
	menu_setprop(menu, MPROP_PERPAGE, 7);
	menu_display(id, menu);
}

public MenuAdmina(id)
{
if(!(get_user_flags(id) & ADMIN_IMMUNITY)){
        client_print(id, print_chat, "Menu jest tylko dla Admina!")
		return PLUGIN_HANDLED;
    }
	new menu = menu_create("Menu Admina:", "Handel_Menu");
		
	menu_additem(menu, "\yPrzenies Gracza",       "13");
	menu_additem(menu, "Ozyw Gracza", "15");
	menu_additem(menu, "Mikro dla TT", 	"9");
	menu_additem(menu, "Otworz Cele", 	"7");
	menu_additem(menu, "Tryb Walki", 	"8");
	menu_additem(menu, "\rBan na CT",        "12");
	
	
	menu_setprop(menu, MPROP_EXITNAME, "Wyjdz");
	menu_setprop(menu, MPROP_BACKNAME, "<<<");
	menu_setprop(menu, MPROP_NEXTNAME, ">>>");
	menu_display(id, menu);

}

public MenuGraczaCT(id)
{
	new menu = menu_create("\dMenu Straznika:", "Handel_Menu");
	new cb = menu_makecallback("Menu_Callback");
	

	menu_additem(menu, "\yWez prowadzenie", 		"6", 1, cb);
	menu_additem(menu, "\wPrzeszukaj Wieznia","5");
	menu_additem(menu, "\wOtworz Cele", 	"7");
	menu_additem(menu, "\wWalka wiezniow: \rON/OFF", 	"8", 3, cb);
	menu_additem(menu, "\wMikrofon dla Wiezniow: \rON/OFF", 	"9");
	menu_additem(menu, "\wZabawy \y- Wlacz zabawe", 		"10", 3, cb);
	menu_additem(menu, "\wWlacz Gong \y- Sygnal gongu","17", 3, cb);
        menu_additem(menu, "\wOznacz wieznia kolorem",   "43");
        menu_additem(menu, "\wWylosuj wieznia",    "44");
	menu_additem(menu, "\wDaj: \yFreeDay'a lub Duszka","11");
	menu_additem(menu, "\wDaj Deagle'a Wiezniowi","21", 3, cb);
	menu_additem(menu, "\wPodziel Wiezniow","22", 3, cb);
	new tekst[128];
	menu_additem(menu, "\dVIP INFO", 	"0");
	menu_additem(menu, "\dSuperVIP INFO", 	"1");
	menu_additem(menu, "\dRegulamin", 		"2");
	
	menu_setprop(menu, MPROP_EXITNAME, "Wyjdz");
	menu_setprop(menu, MPROP_BACKNAME, "Poprzednia");
	menu_setprop(menu, MPROP_NEXTNAME, "Nastepna");
	menu_display(id, menu);
}

public MenuGraczaNieZywego(id)
{
	new menu = menu_create("Menu Widza:", "Handel_Menu");

	menu_additem(menu, "VIP INFO", 	"0");
	menu_additem(menu, "SUPERVIP INFO", 	"1");

	menu_setprop(menu, MPROP_EXITNAME, "Wyjdz");
	menu_setprop(menu, MPROP_BACKNAME, "Poprzednia");
	menu_setprop(menu, MPROP_NEXTNAME, "Nastepna");
	menu_display(id, menu);
}

public Menu_Callback(id, menu, item)
{
	static num[10], acces, callback;
	menu_item_getinfo(menu, item, acces, num, 9, _, _, callback);
 
	switch(acces)
	{
		case 1:{
			if(jail_get_prowadzacy() || !jail_get_days()) {
				return ITEM_DISABLED;
			}
		}
		case 2:{
			if(id != jail_get_prowadzacy() && jail_get_days()) {
				return ITEM_DISABLED;
			}
		}
		case 3:{
			if(id != jail_get_prowadzacy()) {
				return ITEM_DISABLED;
			}
		}
		case 4:{
			if(id != jail_get_prisoner_last()) {
				return ITEM_DISABLED;
			}
		}
		case 5:{
			if(!is_user_admin(id)) {
				return ITEM_DISABLED;
			}
		}
	}
	return ITEM_ENABLED;
}

public Handel_Menu(id, menu, item)
{
	if(item == MENU_EXIT)
		return;

	new num[10], acces, callback;
	menu_item_getinfo(menu, item, acces, num, 9, _, _, callback);
	switch(str_to_num(num))
	{
		case 0: client_cmd(id, "say /vip");
		case 1: client_cmd(id, "say /svip");
		case 2: client_cmd(id, "say /regulamin");
		
		case 3: client_cmd(id, "say /czapki");
		case 4:
		{
			if(jail_get_prisoner_free(id) || jail_get_user_block(id))
				return;
			
			new body, target;
			get_user_aiming(id, target, body, 50);
						
			if(target && get_user_team(target) == 1)
			{
				ColorChat(id, TEAM_COLOR, "^x04***JailBreak***^x03 Aby zabrac bron musisz byc blisko straznika!");
				return;
			}
			if(!is_user_alive(target))
				return;
						
			new weapons = Jaki_Pistolet(target);
			if(!weapons)
			{
				ColorChat(id, TEAM_COLOR, "^x04***JailBreak***^x03 Straznik nie ma pistoletu!");
				return;
			}
						
			new weaponname[24];
			get_weaponname(weapons, weaponname, 23);
							
			ham_strip_weapon(target, weapons, weaponname);
			give_item(id, weaponname);
					
			ColorChat(id, TEAM_COLOR, "^x04***JailBreak***^x03 Gratulacje - Ukradles pistolet");		
		}
		
		case 5: w_trakcie[id]? wylacz_rewizje(id): wlacz_rewizje(id);
		
		case 6:{
			if(!jail_get_prowadzacy())
				jail_set_prowadzacy(id);
		}

		case 7: jail_open_cele();
		case 8: jail_set_prisoners_fight((menu_walka = !menu_walka), !menu_walka);
		case 9: jail_set_prisoners_micro((menu_mikro = !menu_mikro));
		case 10: client_cmd(id, "say /zabawy");
		case 11: MenuFreeday(id);
		case 12: client_cmd(id, "jail_menuban", "Ban na CT"); 
		case 13: client_cmd(id, "amx_teammenu", "Przenies TT/CT/SPEC");
		case 14: client_cmd(id, "jail_amenu");
		case 15: client_cmd(id, "say /ozyw");
		case 16: client_cmd(id, "jail_mute");
		case 17: client_cmd(0, "spk sound/JailBreak[k]/dzwonek.wav");
		case 18: client_cmd(id, "say /lr");
		case 19: client_cmd(id, "say /sklep");
		case 20: client_cmd(id, "say /ruletka");
		case 21: client_cmd(id, "say /dajdeagla");
		case 22: client_cmd(id, "say /podziel");
                case 111: client_cmd(id, "say /topg"); 
                case 43: client_cmd(id, "say /oznacz"); 
                case 44: client_cmd(id, "say /losuj");
                case 112: client_cmd(id, "say /rankg"); 
		case 23: 
			{
				ColorChat(id, TEAM_COLOR, "^x04[%s]^x03 AutoBH dla wiezniow - %s", autobh?"Wl":"Wyl");
				if(autobh == 0)
				autobh = 1;
				else 
				autobh = 0;
			}
		
	}
}

public MenuFreeday(id)
{
	new menu2 = menu_create("Manager FD i Duszek:", "Handel_ManagerFreeday");
	menu_additem(menu2, "\rDaj Freeday");
	menu_additem(menu2, "Daj Duszka^n");
	menu_additem(menu2, "\rZabierz Freeday");
	menu_additem(menu2, "Zabierz Duszka");
			
	menu_setprop(menu2, MPROP_EXITNAME, "Wyjdz");
	menu_display(id, menu2);
}

public Handel_ManagerFreeday(id, menu, item)
{
	if(item == MENU_EXIT)
		return;

	new acces, callback, data[3], iname[32];
	menu_item_getinfo(menu, item, acces, data, 2, iname, 31, callback);
	replace(iname, 31, "^n", "");

	new menu2 = menu_create(iname, (!item || item == 2)? "Handel_Menu_Freeday": "Handel_Menu_Duszek");
	for(new i=1; i<=32; i++)
	{
		if(!is_user_alive(i) || cs_get_user_team(i) != CS_TEAM_T)
			continue;

		switch(item)
		{
			case 0,1:{
				if(jail_get_prisoner_free(i) || jail_get_prisoner_ghost(i))
					continue;
			}
			case 2:{
				if(!jail_get_prisoner_free(i))
					continue;
			}
			case 3:{
				if(!jail_get_prisoner_ghost(i))
					continue;
			}
		}
		new name[32];
		get_user_name(i, name, 31);
		menu_additem(menu2, name, (!item || item == 1)? "1": "0");
	}
	menu_setprop(menu2, MPROP_EXITNAME, "Wyjdz");
	menu_display(id, menu2);
}

public Handel_Menu_Freeday(id, menu, item)
{
	if(item == MENU_EXIT)
		return;

	new id2, callback, data[3], name[32];
	menu_item_getinfo(menu, item, id2, data, 2, name, 31, callback);
	callback = data[0]-'0';

	id2 = get_user_index(name);
	jail_set_prisoner_free(id2, bool:callback, false);
	
	new szName[2][32];
	get_user_name(id, szName[0], 31);
	get_user_name(id2, szName[1], 31);
	ColorChat(0, TEAM_COLOR,"^x04[%s] ^x03%s ^x01%s freedaya ^x03%s",TAG,szName[0], callback? "dal": "zabral",szName[1]);
}

public Handel_Menu_Duszek(id, menu, item)
{
	if(item == MENU_EXIT)
		return;
		
	new id2, callback, data[3], name[32];
	menu_item_getinfo(menu, item, id2, data, 2, name, 31, callback);
	callback = data[0]-'0';
	
	id2 = get_user_index(name);
	jail_set_prisoner_ghost(id2, bool:callback, false);
	
	new szName[2][32];
	get_user_name(id, szName[0], 31);
	get_user_name(id2, szName[1], 31);
	ColorChat(0, TEAM_COLOR,"^x04[%s] ^x03%s ^x01%s duszka ^x03%s",TAG,szName[0], callback? "dal": "zabral",szName[1]);
}

public OnRemoveData(day)
{
	menu_mikro = false;
	menu_walka = false;
	autobh = 0;
}

public Handel_Menu_Widza(id, menu, item)
{
	if(item == MENU_EXIT)
		return;

	switch(item)
	{
		case 0: show_motd(id, "faq.txt", "FAQ Serwera");
		case 1: client_cmd(id, "say /zasady");
		case 2: show_motd(id, "komendy.txt", "Komendy");
	}
}

public wlacz_rewizje(id)
{
	if(get_user_team(id) != 2 || !is_user_alive(id))
		return PLUGIN_HANDLED;
		
	new body, target;
	get_user_aiming(id, target, body, 50);
						
	if(target && get_user_team(target) == 2)
	{
		ColorChat(id, TEAM_COLOR, "^x04[%s]^x03 Nie nacelowales na wieznia", TAG);
		return PLUGIN_HANDLED;
	}
	if(!is_user_alive(target))
		return PLUGIN_HANDLED;
		
	if(jail_get_user_block(target))
		return PLUGIN_HANDLED;
	
	jail_set_user_speed(id, 0.1);
	set_bartime(id, 5);
	
	set_bartime(target, 5);
	jail_set_user_speed(target, 0.1);
	
	w_trakcie[id] = target;
	w_trakcie[target] = id;
	set_task(5.0, "wylacz_rewizje", 5000+id);
	emit_sound(id, CHAN_WEAPON, "weapons/c4_disarm.wav", 1.0, ATTN_NORM, 0, PITCH_NORM);
	return PLUGIN_HANDLED;
}

public wylacz_rewizje(taskid)
{
	new id = taskid;
	if(taskid > 32)
		id -= 5000;
		
	if(get_user_team(id) != 2 || !w_trakcie[id])
		return PLUGIN_HANDLED;
	
	remove_task(id+5000);
	jail_set_user_speed(id, 250.0);
	set_bartime(id, 0);
	
	if(is_user_alive(w_trakcie[id]))
	{
		jail_set_user_speed(w_trakcie[id], 250.0);
		set_bartime(w_trakcie[id], 0);
	}
	if(taskid > 32)
		Pokaz_bronie(id);
	
	w_trakcie[w_trakcie[id]] = 0;
	w_trakcie[id] = 0;
	return PLUGIN_HANDLED;
}

public Pokaz_bronie(id)
{
	if(!is_user_alive(id) || !is_user_alive(w_trakcie[id]))
		return;
	
	new weapons[32], numweapons, weaponname[32];
	get_user_weapons(w_trakcie[id], weapons, numweapons);
	ColorChat(id, GREEN, "Znalazles:");
	for(new i=0; i<numweapons; i++)
	{
		get_weaponname(weapons[i], weaponname, 31);
		replace(weaponname, 32, "weapon_", "");
		replace(weaponname, 32, "knife", "piesci");
		ColorChat(id, BLUE, weaponname);
	}
	emit_sound(id, CHAN_WEAPON, "weapons/c4_disarmed.wav", 1.0, ATTN_NORM, 0, PITCH_NORM);
}

public Damage(id)
{
	if(is_user_alive(id) && w_trakcie[id])
	{
		wylacz_rewizje(id);
	}
}

stock Jaki_Pistolet(id)
{
	if(!is_user_connected(id))
		return 0;
	
	new weapons[32], numweapons;
	get_user_weapons(id, weapons, numweapons);
	
	for(new i=0; i<numweapons; i++)
		if((1<<weapons[i]) & 0x4030402)
			return weapons[i];

	return 0;
}

stock ham_strip_weapon(id, wid, szname[])
{
	if(!wid) 
		return 0;
	
	new ent;
	while((ent = engfunc(EngFunc_FindEntityByString, ent, "classname", szname)) && pev(ent, pev_owner) != id) {}
	if(!ent)
		return 0;
	
	if(get_user_weapon(id) == wid) 
		ExecuteHam(Ham_Weapon_RetireWeapon, ent);
	
	if(ExecuteHam(Ham_RemovePlayerItem, id, ent)) 
	{
		ExecuteHam(Ham_Item_Kill, ent);
		set_pev(id, pev_weapons, pev(id, pev_weapons) & ~(1<<wid));
	}
	return 1;
}

stock set_bartime(id, czas)
{
	message_begin(MSG_ONE_UNRELIABLE, get_user_msgid("BarTime"), _, id);
	write_short(czas);
	message_end();
}


public menu(id, level, cid) {
	
	new menu = menu_create("Wybierz Gracza:","menu_broni");
	for(new i=0, n=0; i<=32; i++)
	{
		if(!is_user_connected(i) || !is_user_alive(i) || cs_get_user_team(i) != CS_TEAM_T)
			continue;
		gracz_id[n++] = i;
		new nazwa_gracza[64];
		get_user_name(i, nazwa_gracza, 63)
		menu_additem(menu, nazwa_gracza, "0", 0);
	}
	
	menu_setprop(menu, MPROP_BACKNAME, "Wroc");
	menu_setprop(menu, MPROP_NEXTNAME, "Dalej");
	menu_setprop(menu, MPROP_EXITNAME, "Wyjscie");
	menu_display(id, menu);
	return PLUGIN_HANDLED;
}


public menu_broni(id, menu, item)
{
	if (get_user_team(id) == 2)
	{
		wybrany = gracz_id[item];
		new menu = menu_create("\rMenu Broni", "wybieranie")
		
		menu_additem(menu, "\wDeagle bez naboi", "1", 0)
		menu_additem(menu, "\wDeagle z 1 nabojem", "2", 0)
		menu_additem(menu, "\wDeagle z 3 nabojami", "3", 0) 
		menu_additem(menu, "\wDeagle z pelnym magazynkiem", "4", 0)     
		
		menu_setprop(menu, MPROP_EXIT, MEXIT_ALL)
		
		menu_display(id, menu, 0)
	}
	else 
	{
		client_print(id, print_chat, "Nie masz do tego dostepu Ziomus!")
		return PLUGIN_HANDLED
	}
	return PLUGIN_HANDLED;
}
public wybieranie(id, menu, item)
{
	if (item == MENU_EXIT)
	{
		menu_destroy(menu)
		return PLUGIN_HANDLED
	}
	new data[6], iName[64]
	new acces, callback
	menu_item_getinfo(menu, item, acces, data,5, iName, 63, callback)
	
	new key = str_to_num(data)
	
	switch(key)
	{
		case 1:{
			give_item(wybrany, "weapon_deagle");
			new dgl_id = find_ent_by_owner(-1, "weapon_deagle", wybrany)
			if(dgl_id){
				cs_set_weapon_ammo(dgl_id, 0);
			}
			client_print(id, print_chat, "%s dostal pustego deagla",wybrany)
			menu_destroy(menu)
			return PLUGIN_HANDLED
		}
		
		case 2:{
			give_item(wybrany, "weapon_deagle");
			new dgl_id = find_ent_by_owner(-1, "weapon_deagle", wybrany)
			if(dgl_id){
			cs_set_weapon_ammo(dgl_id, 1);
			}
			client_print(id, print_chat, "%s dostal deagla z 1 nabojem",wybrany)
			menu_destroy(menu)
			return PLUGIN_HANDLED
		}
		
		case 3:{
			give_item(wybrany, "weapon_deagle");
			new dgl_id = find_ent_by_owner(-1, "weapon_deagle", wybrany)
			if(dgl_id){
				cs_set_weapon_ammo(dgl_id, 3);
			}
			client_print(id, print_chat, "%s dostal deagla z 3 nabojami",wybrany)
			menu_destroy(menu)
			return PLUGIN_HANDLED
		}
		case 4:{
			give_item(wybrany, "weapon_deagle");
			new dgl_id = find_ent_by_owner(-1, "weapon_deagle", wybrany)
			if(dgl_id){
				cs_set_weapon_ammo(dgl_id, 35);
			}
			client_print(id, print_chat, "%s dostal deagla z pelnym magazynkiem",wybrany)
			menu_destroy(menu)
			return PLUGIN_HANDLED
		}
	}
	menu_destroy(menu)
	return PLUGIN_HANDLED
}
//////////////////////////////////////////AUTOBH//////////////////////////////////
public client_PreThink(id) {
	entity_set_float(id, EV_FL_fuser2, 0.0)		// Disable slow down after jumping
	if (autobh == false || get_user_team(id) & CS_TEAM_CT)
		return PLUGIN_CONTINUE
		
	if (entity_get_int(id, EV_INT_button) & 2) {	// If holding jump
		new flags = entity_get_int(id, EV_INT_flags)

		if (flags & FL_WATERJUMP)
			return PLUGIN_CONTINUE
		if ( entity_get_int(id, EV_INT_waterlevel) >= 2 )
			return PLUGIN_CONTINUE
		if ( !(flags & FL_ONGROUND) )
			return PLUGIN_CONTINUE

		new Float:velocity[3]
		entity_get_vector(id, EV_VEC_velocity, velocity)
		velocity[2] += 250.0
		entity_set_vector(id, EV_VEC_velocity, velocity)

		entity_set_int(id, EV_INT_gaitsequence, 6)	// Play the Jump Animation
	}
	return PLUGIN_CONTINUE
}
///////////////////////

/* AMXX-Studio Notes - DO NOT MODIFY BELOW HERE
*{\\ rtf1\\ ansi\\ deff0{\\ fonttbl{\\ f0\\ fnil Tahoma;}}\n\\ viewkind4\\ uc1\\ pard\\ lang1045\\ f0\\ fs16 \n\\ par }
*/
