//Zombie Plague Fear Meter by request - http://forums.alliedmods.net/showthread.php?t=80832

/* CVARS - copy and paste to zombieplague.cfg

zp_fear_active 1	//turns fear meter on/off

*/

#include <amxmodx>
#include <fakemeta>
#include <zombieplague>

#define panic_distance 985	//about 25 meters in inches

new const g_heartbeat[] = "player/heartbeat1.wav"

new max_players = 32, pcvar_active, user_scared[33];
//---------------------------------------------------------------------------------------
public plugin_init()
{
	register_plugin("ZP Fear Meter", "0.1", "Random1");
	
	pcvar_active = register_cvar("zp_fear_active", "1");
	
	//LOOP
	set_task(0.1, "fear_loop", _, _, _, "b");
	
	//Bad Aim stuff
	register_forward(FM_PlayerPreThink, "fw_prethink");

	max_players = global_get(glb_maxClients);
}
//---------------------------------------------------------------------------------------
public plugin_precache()
	precache_sound(g_heartbeat);
//---------------------------------------------------------------------------------------
public fear_loop()
{
	static id, player, Origin[3], eOrigin[3], dist, fear;
	
	if ( !get_pcvar_num(pcvar_active) ) {	//in case plugin gets turned off mid game while heartbeat sound is playing to someone
		for ( id = 1; id < max_players; id++ )
			if ( is_user_connected(id) ) 
				emit_sound(id, CHAN_AUTO, g_heartbeat, 1.0, ATTN_NORM, SND_STOP, PITCH_NORM);
		return;
	}
	
	for (id = 1; id < max_players; id++) {
		if ( !is_user_connected(id) || is_user_bot(id) || zp_get_user_zombie(id) ) continue;
		get_user_origin(id, Origin);
		fear = 0; //reset our meter for each player

		for (player = 1; player < max_players; player++) {
			if ( !is_user_connected(player) || id == player ) continue;
			
			get_user_origin(player, eOrigin)
			dist = get_distance(eOrigin, Origin);

			if ( dist > panic_distance ) {
				switch(floatround(dist * 0.0254, floatround_floor))
				{
					case 0 .. 8: fear += zp_get_user_zombie(player) ? 3 : -3;
					case 9 .. 17: fear += zp_get_user_zombie(player) ? 2 : -2;
					case 18 .. 25: fear += zp_get_user_zombie(player) ? 1 : -1;
				}
			}				
		}
		//now for the calcualtions based on our variable
		if ( fear <= 1 ) {	
			emit_sound(id, CHAN_AUTO, g_heartbeat, 1.0, ATTN_NORM, SND_STOP, PITCH_NORM);	//stop heartbeat sound
			user_scared[id] = 0;
			continue;
		}
		
		emit_sound(id, CHAN_AUTO, g_heartbeat, 1.0, ATTN_NORM, 0, PITCH_NORM);
		user_scared[id] = fear/5
	}
}
//---------------------------------------------------------------------------------------
public fw_prethink(id)
{
	if ( !user_scared[id] ) return;
	
	static Float:badvec[3]
	badvec[0] = badvec[1] = badvec[2] = (user_scared[id] * 30.0);
	
	set_pev(id, pev_punchangle, badvec);
}