/*
===================================================================================================
"Бойцовский клуб"
Версия: 1.1
Автор: DJ_WEST
Copyright (C) Reallite Labs, 2002-2007
Веб-сайт: http://reallite.cs2.ru
===================================================================================================
Список команд:
fc_screenfade 0/1  - включить/выключить покраснение изображения при получении повреждения
fc_bleed 0/1 	   - включить/выключить постоянную потерю крови, если жизнь равна меньше 20 HP
fc_blood 0/1 	   - включить/выключить потерю крови при нанесении повреждения
fc_health X 	   - количество HP, где X число (по умолчанию: 200)
fc_armor X 	   - количество брони, где X бронь (по умолчанию: 100)
fc_models 0/1 	   - включить/выключить новые модели игроков
===================================================================================================
Список изменений версии 1.1:
* Добавлены новые модели игроков (fc_models)
* Добавлена система информирования, которая показывает к какой команде принадлежит игрок
Список изменений версии 1.0:
* На карте ka_fightclub увеличено освещение
* На карте ka_fightclub изменен звуковой файл на русский (fight_rules_ru.wav)
* Модель ножа заменена на модель рук
* Звуки от ударов ножом заменены на другие
* Иконка оружия при убийстве заменена на другую
* Игрок, получивший повреждение, теряет кровь (fc_blood)
* У игрока, получившего повреждения, изображение становится на время красным (fc_screenfade)
* Если у игрока жизнь равна меньше 20 HP, то он теряет кровь (fc_bleed)
* Жизнь в начале каждого раунда автоматически равняется 200 HP, а бронь 100 (fc_health и fc_armor)
===================================================================================================
*/

#include <amxmodx>
#include <amxmisc>
#include <cstrike>
#include <engine>
#include <fun>
#include <fakemeta>

#define PLUGIN "Fight Club"
#define VERSION "1.1"
#define AUTHOR "DJ_WEST"

#define MAX_PLAYERS 32
#define MAX_MODELS 8

new Models[MAX_MODELS][] = {"fight_club_1","fight_club_2","fight_club_3","fight_club_4","fight_club_5","fight_club_6","fight_club_7","fight_club_8"}

new New_Sounds[7][48]
new Old_Sounds[7][48]
new Fade
new Map_Name[32]
new Offset[8][3] = {{0,0,15},{0,0,30},{0,0,20},{0,0,12},{2,2,17},{2,2,17},{2,2,5},{2,2,5}}
new Health[MAX_PLAYERS+1]

new g_CvarScreenFade
new g_CvarBleed
new g_CvarBlood
new g_CvarHealth
new g_CvarArmor
new g_CvarModels

new Sprite_File[21][255]
new Sprite[21]

new Config_Dir[128]

public plugin_init() 
{
	register_plugin(PLUGIN, VERSION, AUTHOR)
	
	get_mapname(Map_Name,63)
	
	if(equali(Map_Name,"ka_fightclub"))
	{
		Fade = get_user_msgid("ScreenFade")
		register_forward(FM_EmitSound,"Change_Sound")
		register_event("CurWeapon","Change_Weapon_Models","b")
		register_logevent("New_Round", 2, "1=Round_Start")
		register_event("DeathMsg", "Change_Death", "a","4&kni")
		register_event( "StatusValue", "on_ShowStatus", "be", "1=2","2!0")
		register_message(get_user_msgid("DeathMsg"),"Block_Message");
		register_event("Damage", "Check_Damage", "b", "2!0", "3=0", "4!0")
		set_lights("z") 
		set_task(1.5,"Check_Health",100,"",0,"b")
		
		format(New_Sounds[1],47,"weapons/fight_hit1.wav")
		format(Old_Sounds[1],47,"weapons/knife_hit1.wav")
		format(New_Sounds[2],47,"weapons/fight_hit2.wav")
		format(Old_Sounds[2],47,"weapons/knife_hit2.wav")
		format(New_Sounds[3],47,"weapons/fight_hit3.wav")
		format(Old_Sounds[3],47,"weapons/knife_hit3.wav")
		format(New_Sounds[4],47,"weapons/fight_hit4.wav")
		format(Old_Sounds[4],47,"weapons/knife_hit4.wav")
		format(New_Sounds[5],47,"weapons/fight_stab.wav")
		format(Old_Sounds[5],47,"weapons/knife_stab.wav")
		format(New_Sounds[6],47,"weapons/fight_hitwall1.wav")
		format(Old_Sounds[6],47,"weapons/knife_hitwall1.wav")
		
		g_CvarScreenFade	= register_cvar("fc_screenfade", "1")
		g_CvarBleed		= register_cvar("fc_bleed", "1")
		g_CvarBlood		= register_cvar("fc_blood", "1")
		g_CvarHealth		= register_cvar("fc_health", "200")
		g_CvarArmor		= register_cvar("fc_armor", "100")
		g_CvarModels		= register_cvar("fc_models", "1")
	}
}

public plugin_precache()
{
	get_mapname(Map_Name,63)
	
	if(equali(Map_Name,"ka_fightclub"))
	{
		precache_model("models/p_hands.mdl")
		precache_model("models/v_hands.mdl")
		precache_model("models/player/fight_club_1/fight_club_1.mdl")
		precache_model("models/player/fight_club_2/fight_club_2.mdl")
		precache_model("models/player/fight_club_3/fight_club_3.mdl")
		precache_model("models/player/fight_club_4/fight_club_4.mdl")
		precache_model("models/player/fight_club_5/fight_club_5.mdl")
		precache_model("models/player/fight_club_6/fight_club_6.mdl")
		precache_model("models/player/fight_club_7/fight_club_7.mdl")
		precache_model("models/player/fight_club_8/fight_club_8.mdl")
		precache_sound("weapons/fight_hit1.wav")
		precache_sound("weapons/fight_hit2.wav")
		precache_sound("weapons/fight_hit3.wav")
		precache_sound("weapons/fight_hit4.wav")
		precache_sound("weapons/fight_stab.wav")
		precache_sound("weapons/fight_hitwall1.wav")
		
		format(Sprite_File[1],255,"sprites/fight_club/fight_club_t.spr")
		format(Sprite_File[2],255,"sprites/fight_club/fight_club_ct.spr")
		Sprite[1] = precache_model(Sprite_File[1])
		Sprite[2] = precache_model(Sprite_File[2])
	}
}

public plugin_cfg()
{
	get_mapname(Map_Name,63)
	
	if(equali(Map_Name,"ka_fightclub"))
	{
		get_configsdir(Config_Dir,127)
		server_cmd("exec %s/fight_club.cfg",Config_Dir)
	}
}

public client_putinserver(id)
{
	get_mapname(Map_Name,63)
	
	if(equali(Map_Name,"ka_fightclub"))
	{
		if(!is_user_bot(id))
		{
			client_cmd(id,"violence_ablood 1")
			client_cmd(id,"violence_agibs 1")
			client_cmd(id,"violence_hblood 1")
			client_cmd(id,"violence_hgibs 1")
		}
		
		if(get_pcvar_num(g_CvarModels) == 1)
		{
			new Number
			Number = random_num(0,MAX_MODELS-1)
			cs_set_user_model(id, Models[Number])
		}
	}
	else
		cs_reset_user_model(id)
	
	return PLUGIN_CONTINUE
}
public Change_Weapon_Models(id) 
{
	if (is_user_alive(id))
	{
		entity_set_string(id,EV_SZ_viewmodel,"models/v_hands.mdl")
		entity_set_string(id,EV_SZ_weaponmodel,"models/p_hands.mdl")
	}
	
	return PLUGIN_CONTINUE
}

public New_Round()
{
	new Players[MAX_PLAYERS], PlayerCount, i, CvarHealth, CvarArmor, Player
	
	get_players(Players,PlayerCount)
	CvarHealth = get_pcvar_num(g_CvarHealth)
	CvarArmor = get_pcvar_num(g_CvarArmor)
	
	for(i=0;i<PlayerCount;i++)
	{
		Player = Players[i]
		cs_set_user_money(Player, 0)
		set_user_health(Player,CvarHealth)
		cs_set_user_armor(Player, CvarArmor, CS_ARMOR_VESTHELM)
	}
}

public Change_Sound(id,channel,sample[])
{
	if(!is_user_alive(id))
	{
		return FMRES_IGNORED
	}
	
	if(channel!=CHAN_WEAPON && channel!=CHAN_ITEM)
	{
		return FMRES_IGNORED
	}

	static i

	for(i=0;i<7;i++)
	{
		if(equali(sample,Old_Sounds[i]))
		{
			engfunc(EngFunc_EmitSound,id,CHAN_WEAPON,New_Sounds[i],1.0,ATTN_NORM,0,PITCH_NORM)
			return FMRES_SUPERCEDE
		}
	}
	return FMRES_IGNORED
}

public Change_Death()
{
	new Attacker = read_data(1)
	new Victim = read_data(2)
	new Headshot = read_data(3)
	if(Victim != Attacker)
	{		
		message_begin(MSG_ALL, get_user_msgid("DeathMsg"))
		write_byte(Attacker)
		write_byte(Victim)
		write_byte(Headshot)
		write_string("fight")
		message_end()
	}
	
	return PLUGIN_HANDLED
}

public Block_Message(msg_id,msg_dest,msg_entity) 
{	
	if(msg_id == get_user_msgid("DeathMsg")) 
	{
		return PLUGIN_HANDLED
	}

	return PLUGIN_CONTINUE
}

public Check_Health()
{
	if(get_pcvar_num(g_CvarBleed) == 1)
	{
		new Players[MAX_PLAYERS], PlayerCount, i, Player
		new iOrigin[3]
		
		get_players(Players,PlayerCount,"a")
		
		for(i=0;i<PlayerCount;i++)
		{
			Player = Players[i]
			Health[Player] = get_user_health(Player)
			if(Health[Player] < 20) 
			{
				get_user_origin(Player,iOrigin)
				Bleed(iOrigin)
			}
		} 
	}
	
	return PLUGIN_CONTINUE
}

public Check_Damage(Victim)
{
	new Weapon, iHitPlace, Attacker = get_user_attacker(Victim, Weapon, iHitPlace)
	new iOrigin[3], iOrigin2[3]
	
	if (!Attacker || !is_user_alive(Victim))
		return
	
	if(Weapon != CSW_KNIFE)
		return
	if(get_pcvar_num(g_CvarBlood) == 1)
	{
		get_user_origin(Victim,iOrigin)
		get_user_origin(Attacker,iOrigin2)
		Blood(iOrigin,iOrigin2,iHitPlace)
	}
	
	if(get_pcvar_num(g_CvarScreenFade) == 1)
		Screen_Fade(Victim)
}

Screen_Fade(Victim)
{
	message_begin(MSG_ONE_UNRELIABLE,Fade,{0,0,0},Victim)
	write_short(2000)
	write_short(2000)
	write_short(0)
	write_byte(175)
	write_byte(0)
	write_byte(0)
	write_byte(115)
	message_end()
}

Blood(Origin[3],Origin2[3],HitPlace)
{
	if (HitPlace < 0 || HitPlace > 7) HitPlace = 0
	new Distance = get_distance(Origin,Origin2) ? get_distance(Origin,Origin2) : 1

	new X = ((Origin[0]-Origin[0]) * 300) / Distance
	new Y = ((Origin[1]-Origin[1]) * 300) / Distance
	new Z = ((Origin[2]-Origin[2]) * 300) / Distance

	message_begin(MSG_BROADCAST,SVC_TEMPENTITY)
	write_byte(TE_BLOODSTREAM)
	write_coord(Origin[0]+Offset[HitPlace][0])
	write_coord(Origin[1]+Offset[HitPlace][1])
	write_coord(Origin[2]+Offset[HitPlace][2])
	write_coord(X)
	write_coord(Y)
	write_coord(Z)
	write_byte(70)
	write_byte(random_num(100,200))
	message_end()
}

Bleed(Origin[3])
{
	message_begin(MSG_BROADCAST,SVC_TEMPENTITY)
	write_byte(TE_BLOODSTREAM)
	write_coord(Origin[0])
	write_coord(Origin[1])
	write_coord(Origin[2]+10)
	write_coord(random_num(-100,100))
	write_coord(random_num(-100,100)) 
	write_coord(random_num(-10,10)) 
	write_byte(70)
	write_byte(random_num(50,100))
	message_end()		
}

public on_ShowStatus(id)
{
	if(get_pcvar_num(g_CvarModels) == 1)
	{
		new iTarget = read_data(2)
		
		if (cs_get_user_team(iTarget) == CS_TEAM_T)
			Show_Icon(id,iTarget,55,Sprite[1],20)
		else
			Show_Icon(id,iTarget,55,Sprite[2],20)
	}
}		

stock Show_Icon(id, entity, vOffset, iSprite, life)
{
	message_begin(MSG_ONE, SVC_TEMPENTITY, { 0, 0, 0 }, id)
	write_byte(TE_PLAYERATTACHMENT)
	write_byte(entity)
	write_coord(vOffset)
	write_short(iSprite)
	write_short(life)
	message_end()
}

