/* Plugin generated by AMXX-Studio */

#include <amxmodx>
#include <codmod>
#include <fakemeta>
#include <colorchat>

new const perk_name[] = "Defibrylator";
new const perk_desc[] = "Masz 1/LW szansy na zreanimowanie czlonka druzyny po zabiciu wroga";

new bool:ma_perk[33];
new wartosc_perku[33];

public plugin_init() 
{
	register_plugin(perk_name, "1.0", "QTM_Peyote");
	
	cod_register_perk(perk_name, perk_desc, 2, 6);
	
	register_event("DeathMsg", "Death", "ade");
}
public cod_perk_enabled(id, wartosc)
{
	ma_perk[id] = true;
	wartosc_perku[id] = wartosc;
}
public cod_perk_disabled(id)
	ma_perk[id] = false;

public Death()
{
	new attacker = read_data(1);
	
	if(!is_user_connected(attacker))
		return PLUGIN_CONTINUE;
	
	if(!ma_perk[attacker])
		return PLUGIN_CONTINUE;
		
	if(random(wartosc_perku[attacker]))
		return PLUGIN_CONTINUE;
		
	new id = read_data(2);
	new attacker_team = get_user_team(attacker);
	
	if(get_user_team(id) == attacker_team)
		return PLUGIN_CONTINUE;
		
	new Players[32], playersCount;	
		
	get_players(Players, playersCount, "beh", (attacker_team == 1)? "TERRORIST" : "CT");
	
	if(!playersCount)
		return PLUGIN_CONTINUE;
	
	new nick_zreanimowanego[33], nick_reanimujacego[33], zreanimowany = Players[random(playersCount)];
	
	Respawn(zreanimowany);
	
	get_user_name(zreanimowany, nick_zreanimowanego, 32);
	get_user_name(attacker, nick_reanimujacego, 32);
	
	ColorChat(zreanimowany, GREEN, "Zostales zreanimowany przez %s.", nick_reanimujacego);
	ColorChat(attacker, GREEN, "Zreanimowales %s.", nick_zreanimowanego);
	
	return PLUGIN_CONTINUE;
}
public Respawn(id)
{
	set_pev(id, pev_deadflag, DEAD_RESPAWNABLE);
	set_pev(id, pev_iuser1, 0);
	dllfunc(DLLFunc_Think, id)
}
