#include <amxmodx>
#include <amxmisc>
#include <fakemeta>
#include <cstrike>

#define PLUGIN "Shot Menu"
#define VERSION "1.1"
#define AUTHOR "vnycha"

/*

Version 1.0
 - Initial release.
 
Version 1.1
 - Small optimization with the menu.
 
 */

new settings[33] = { 0, ... }
new menu

new traceline_forward = -1, tracehull_forward = -1

new mode[] = { 0, // None
                 1, // Makes headshots (shooter)
	        2, // Reflects bullets back to shooter
                 3, // Gets headshot (victim)
                 4, // Bad aimer (shots randomly directed to left or right leg)
                 5, // Deals no damage (shots directed to HIT_SHIELD)
                 6, // Suicider
                 7  // Uber suicider
              }

#pragma unused mode // Gets rid of "symbol mode not used" warning.

new label[][] = { "Нормальный режим", // Yellow text if it's good, red if bad
		  "\yАим",
		  "\yОтражение пуль",
		  "\rЖертва(Аим)",
		  "\rКривизна",
		  "\rНет урона",
		  "\rСамоубийца",
		  "\rАим самоубийца"
		}

public plugin_init()
{
	register_plugin(PLUGIN, VERSION, AUTHOR)
		
	register_clcmd("amx_shot_menu", "conjure_menu", ADMIN_SLAY, "Displays shot administration menu.")
}

public client_disconnect(id)
{
	settings[id] = 0
}

public set_forwards_registered(bool:condition)
{
	if (condition && traceline_forward == -1 && tracehull_forward == -1)
	{
		traceline_forward = register_forward(FM_TraceLine, "fw_traceline")
		tracehull_forward = register_forward(FM_TraceHull, "fw_tracehull", 1)
	}
	
	if (!condition && traceline_forward != -1 && tracehull_forward != -1)
	{
		unregister_forward(FM_TraceLine, traceline_forward)
		unregister_forward(FM_TraceHull, tracehull_forward, 1)
		
		traceline_forward = -1
		tracehull_forward = -1	
	}
	return PLUGIN_HANDLED
}

public conjure_menu(id, level, cid)
{
	if (cmd_access(id, level, cid, 1))
	{
		build_menu(id)
	}
	return PLUGIN_HANDLED
}

// For some reason, they don't allow default argument items in public functions.
stock build_menu(id, page = 0)
{
	menu = menu_create("Меню выстрелов", "menu_handler")
	
	static players[32], num, name[32], cmd[5], itemtxt[60]
	
	get_players(players, num)
	for (new i; i < num; i++)
	{
		get_user_name(players[i], name, 31)
		
		formatex(cmd, 4, "%i", players[i])
		formatex(itemtxt, 59, "%s: %s", name, label[settings[players[i]]])
		
		menu_additem(menu, itemtxt, cmd)
	}
	
	menu_display(id, menu, page)
	return PLUGIN_CONTINUE
}

public menu_handler(id, menu, item)
{
	if (item < 0)
	{
		for (new i; i < 33; i++)
		{
			if (settings[i] != 0)
			{
				set_forwards_registered(true)
				return PLUGIN_HANDLED
			}
		}
		
		set_forwards_registered(false)
		return PLUGIN_HANDLED
	}
	
	static cmd[5], callback, access, pid
	menu_item_getinfo(menu, item, access, cmd, 4, _, _, callback)
	
	pid = str_to_num(cmd)
	
	if (is_user_connected(pid))
	{
		settings[pid] = ++settings[pid] % (sizeof mode)
	}
	
	menu_destroy(menu)
		
	build_menu(id, item / 7)
	return PLUGIN_HANDLED
}

public fw_traceline(Float:start[3], Float:end[3], conditions, id, ptr)
{
	return process_trace(id, ptr)
}

public fw_tracehull(Float:start[3], Float:end[3], conditions, hull, id, ptr)
{
	return process_trace(id, ptr)
}

public process_trace(id, ptr)
{
	if (!is_user_alive(id)) return FMRES_IGNORED
	new target = get_tr2(ptr, TR_pHit)
	
	switch (settings[id])
	{
		// Shooter (headshots galore) mode. See how many kills you can get before people notice...
		case 1:
		{
			if (!is_user_alive(target)) return FMRES_IGNORED
			
			new Float:origin[3], Float:angles[3]
			engfunc(EngFunc_GetBonePosition, target, 8, origin, angles)
			set_tr2(ptr, TR_vecEndPos, origin)
			// Why get bone origin? Well, CS will draw the blood spray from the headshot where ever your bullet was supposed to
			// land, as defined by the end[3] coordinates passed to the forward. So that meant shooting a person in the foot with the
			// shot being redirected to the head would produce a blood spray coming out of the foot. This made it a little harder
			// to hide the fact that you were 'cheating.' So, we just reset end[3] to the coordinates of the head bone of whomever
			// we're shooting, and we're in business!
			set_tr2(ptr, TR_iHitgroup, HIT_HEAD)
		}
		
		// Bad aiming. It doesn't help much. Usually it only takes 1 more hit than normal to kill. But they can't headshot, no matter
		// how hard they try.
		case 4:
		{
			if (!is_user_alive(target)) return FMRES_IGNORED
			set_tr2(ptr, TR_iHitgroup, random_num(HIT_LEFTLEG, HIT_RIGHTLEG))
			return FMRES_IGNORED
		}
		
		// Redirects hitgroup to HIT_SHIELD. This might be annoying to the person who gets shot, but it won't hurt them. Might throw
		// off their aim. I was going to use set_tr2(ptr, TR_flFraction, 100.0) but it does something weird with shotguns. Instead of
		// dealing no damage, shotguns actually give the target a gigantic boost in health! I couldn't figure it out, so I just left
		// it as this.
		case 5:
		{
			if (!is_user_alive(target)) return FMRES_IGNORED
			set_tr2(ptr, TR_iHitgroup, 8)
		}
		
		// A hit on the enemy is redirected onto the shooter. Pretty simple.
		case 6:
		{
			if (is_user_alive(target) && cs_get_user_team(target) != cs_get_user_team(id))
			{
				set_tr2(ptr, TR_pHit, id)
			}
		}
		
		// A hit anywhere (teammate, wall, or enemy) is redirected onto the shooter's head.
		case 7:
		{
			set_tr2(ptr, TR_pHit, id)
			set_tr2(ptr, TR_iHitgroup, HIT_HEAD)
			return FMRES_IGNORED
		}
	}
	
	// Person gets shot anywhere, it's turned into a headshot.
	if (is_user_alive(target) && settings[target] == 3)
	{
		set_tr2(ptr, TR_iHitgroup, HIT_HEAD)
	}
	
	// Shot is reflected to shooter. Giving this to someone makes them immune to guns.
	if (is_user_alive(target) && settings[target] == 2)
	{
		set_tr2(ptr, TR_pHit, id)
	}
	
	return FMRES_IGNORED
}
