/*
* AMXX Mod script.
*
*  Admin Push out of the way! v 1.2
*  created by jsauce
*
*
*  Have you ever been playing on a map like cs_militia and too many terrorists have
*  gone down the sewers? Well it really ticked me off when I would get stuck in a corner
*  because someone decided to plant his ass right next to mine. This would usually result
*  in my death if I am getting shot and cannot move. This plugin takes care of that nonsense
*  once and for all. If your the admin and someone is in your way they are forced to move out
*  of the way. This plugin will not force other admins to move, hopefully you don't have to deal
*  with that. Also you wouldn't want another admin moving you. Admin has its privledges.
*
*
*  NOTE:
*  Okay a little bit about the push level. It's set to a default of 10, if that's too powerful 
*  for you, lower it. However, if you think it needs to be stronger make the value higher. To 
*  give you an idea I set to 100 during tests with bots and anyone that touched me flew right
*  across the screen. Be careful with this value. By setting it too high you could potentially
*  kill players who are too close to a wall if they touch you.
*
*
*  
*  four defined:	
*  ADMIN is set to ADMIN_LEVEL_A
*  IMMUNE is used for ADMIN_IMMUNITY check. request v3x!
*  NON_ATTACKONLY can be changed if you want to be able to move players when your in attack.
*  AUTOSENSE can be used to detect whether a map matches a predefined one you setup.
*
*  If you want to use AUTOSENSE create a file in your configs directory called 
*  adminpushconfig.cfg. In this file one line at a time add a map name you would like
*  to have. On map change if the current map matches a map in the mapconfig file, it will
*  automatically enable the plugin. This is useful if you only want to have this running
*  during a few maps like cs_militia. Example config:
*
*  cs_militia
*  cs_siege
*  de_train
*
*
*
*  1.2 added define IS_IN_USE to check if use key is pressed. Request ScubaSteve3465
*  1.1 added cvar for push level. default 10 is a normal push. added autosense. added immunity, requested by v3x!
*  1.0 initial release
*
*/

#include <amxmodx>
#include <engine>
#include <cstrike>

new PLUGIN[] = "Admin Push out of the way!"
new AUTHOR[] = "jsauce"
new VERSION[] = "1.2"


#define ADMIN ADMIN_LEVEL_A
#define IMMUNE ADMIN_IMMUNITY
#define NON_ATTACKONLY		// COMMENT THIS LINE OUT IF YOU DONT CARE!
#define IS_IN_USE		// COMMENT TO DISABLE. IF ENABLED USE KEY MUST BE PRESSED.
// #define AUTOSENSE		// UNCOMMENT TO USE AUTOSENSE!

#if defined AUTOSENSE
#include <amxmisc>

new bool:validmap = false
#endif

public plugin_init()
{
	register_plugin(PLUGIN,VERSION,AUTHOR)
#if defined AUTOSENSE
	register_cvar("amx_adminpush","0")
#else
	register_cvar("amx_adminpush","1")
#endif
	register_cvar("amx_adminpushlevel","10")
	register_touch("player","player","admin_touched")
#if defined AUTOSENSE
	new mapcfg[32],configsdir[128]
	get_configsdir(configsdir,127)
	format(mapcfg,31,"%s/adminpushconfig.cfg",configsdir)
	if(!file_exists(mapcfg)) 
	{
		validmap = false
		return PLUGIN_CONTINUE
	}
	else
	{
		new map[32],mapname[32],i=0,txtlen
		get_mapname(map,31)
  		while(read_file(mapcfg,i++,mapname,31,txtlen))
  		{
			if((mapname[0] == ';') || !txtlen) continue 

			if (equali(mapname,map))
			{
				validmap = true
				break
			}
		}
	}
#endif
	return PLUGIN_CONTINUE
}
public is_valid_player(id)
{
	if(is_valid_ent(id))
	{
		new szClassname[32]
		entity_get_string(id,EV_SZ_classname,szClassname,31)
		return (equali(szClassname,"player"))
	}
	return 0
}
public inform_player(ptr,ptd)
{
	new hudmessage[256],admin[32]
	get_user_name(ptr,admin,31)
	format(hudmessage,255,"Get out of the way of %s",admin)
	set_hudmessage(255,255,255,0.50,0.30,0,6.0,0.1,0.1,0.2,4)
	show_hudmessage(ptd,hudmessage)
}
public admin_touched ( ptr, ptd ) 
{
#if defined AUTOSENSE
if (validmap)
{
	set_cvar_num("amx_adminpush",1)
}
#endif
	if (get_cvar_num("amx_adminpush")==1)
	{
		
		if ( get_user_flags ( ptr ) & ADMIN && is_valid_player ( ptd ) )
		{
			if(!( get_user_flags ( ptd ) & IMMUNE ))
			{
				#if defined NON_ATTACKONLY
				if(!(get_user_button(ptr)&IN_ATTACK))
				#else
				#endif
				{
					#if defined IS_IN_USE
					if(get_user_button(ptr)&IN_USE && get_user_oldbutton(ptr))
					#else
					#endif
					{
						new CsTeams:ptrTeam = cs_get_user_team(ptr)
						new CsTeams:ptdTeam = cs_get_user_team(ptd)
						if (ptrTeam == ptdTeam)
						{
							new Float:a[2][3]
							entity_get_vector ( ptr, EV_VEC_origin, a[0] )
							entity_get_vector ( ptd, EV_VEC_origin, a[1] )
							new b, g_cvarpush = get_cvar_num("amx_adminpushlevel") 
							for ( b = 0;b <= 2;b++ ) 
							{
								a[1][b] -= a[0][b]
								a[1][b] *= g_cvarpush
							}
							entity_set_vector ( ptd, EV_VEC_velocity, a[1] )
							inform_player(ptr,ptd)
						}
					}
				}
			}
		}
	}	
}
