/* AMX Mod X
*   More Real CS Plugin
*
Плагин позволяет замедлять раненных игроков, увеличивать взрывную силу гранат.
Смертельно раненные игроки погибают.
Перезарядка: в cs некоторые оружия (например, 1-1, 3-1, 3-3) имеют магазины с одним кол-вом патронов при покупке и с другим - при стрельбе

*/

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

 // тип оружия
 #define W_NONE 0
 #define W_PRIMARY 1
 #define W_SECONDARY 2
 #define W_KNIFE 3
 #define W_GRENADE 4
 #define W_BOMB 5

// #define SOUND_PISC "mrcs/pisc.wav"

new PLUGIN[]="MoreRealCS"
new AUTHOR[]="JohnJ"
new VERSION[]="1.00"

new MRCS_ENABLED[]="mrcs_enabled"   // включен ли плагин
new MRCS_HPNORM[]="mrcs_hpnorm"	// процент жизни, при котором игрок ещё не умирает от ран
new MRCS_HPLOSE[]="mrcs_hplose"	// отнимание жизней от ран за единицу времени
new MRCS_HPLOSETIME[]="mrcs_hplosetime"	// время, через которое будут отниматься жизни от ран (mrcs_hplosetime>=1)
new MRCS_SLOWING[]="mrcs_slowing"	// коэффициент замедления раненного игрока (в %)
new MRCS_HEPOWER[]="mrcs_hepower"	// сила взрывной гранаты в процентах от стандарта

new speednormal[9]={250,210,220,221,230,235,240,245,260}

public plugin_init()
{
	register_plugin(PLUGIN, VERSION, AUTHOR)
	register_dictionary("morerealcs.txt")

	register_cvar(MRCS_ENABLED, "0")
	register_cvar(MRCS_HPNORM, "20")
	register_cvar(MRCS_HPLOSE, "1")
	register_cvar(MRCS_HPLOSETIME, "2")
	register_cvar(MRCS_SLOWING, "70")	// по формуле: v=v0*hp/k, где v - новая скорость игрока, v0 - начальная скорость 
							// игрока, hp - число жизней игрока, k - коэффициент замедления (k>=100)
	register_cvar(MRCS_HEPOWER, "250")

	register_event("Damage","OnDamage","b","2!0")
	register_event("CurWeapon","OnChangeWeap","b")
	register_event("WeapPickup","OnChangeWeap","b")
	register_event("HideWeapon","OnChangeWeap","b")

	register_logevent("roundstart", 2, "0=World triggered", "1=Round_Start")
}
//public plugin_precache()
//{
// new str[51]
// format(str,50,"sound/%s",SOUND_PISC)
// if (file_exists(str))
// {
//  precache_sound(SOUND_PISC)
// }
//}


public OnDamage(victim)
{
 new enable = get_cvar_num(MRCS_ENABLED)
 if ((enable==1) && is_user_alive(victim))
 {
  slowestspeed(victim,1)

  new wp, attacker=get_user_attacker(victim,wp)
  new hp=get_user_health(victim)

  if (wp==CSW_HEGRENADE)
  {
   new dmg=read_data(2)
   new dmgtype=read_data(3)

   new hepower=get_cvar_num(MRCS_HEPOWER)

   new Float:damage=1.0*dmg*hepower/100
   if (damage<0.0) damage=1.0

   if (hp<damage)
   {
    user_silentkill(victim)
    make_deathmsg(attacker,victim,0,"grenade")
   }
   else
   {
//    emit_sound(victim,CHAN_VOICE,SOUND_PISC,VOL_NORM,ATTN_NORM,0,PITCH_NORM)
   }
   fakedamage(victim,"weapon_grenage",damage,dmgtype)
   if (attacker!=victim)
   {
    new frags=get_user_frags(attacker)
    set_user_frags(attacker, frags+1 )
   }
    }

  new Float:hptime=get_cvar_float(MRCS_HPLOSETIME)
  if (hptime<1)
  {
   hptime=1.0
  }

  new hpnorm = get_cvar_num(MRCS_HPNORM)
  if (!task_exists(victim) && (hpnorm>hp))
  {
   new hplose = get_cvar_num(MRCS_HPLOSE)
   new nrp=hp/hplose
//   set_task(hptime,"notlonglive",victim)
   set_task(hptime,"notlonglive",victim, _, _, "a",nrp)
//   set_task(10.0, "task_vote", _, _, _, "a", 2) 
  }
 }
 return PLUGIN_CONTINUE
}
public OnChangeWeap(id)
{
 slowestspeed(id,0)
 return PLUGIN_CONTINUE
 
}

public client_connect(id)
{
 new enable = get_cvar_num(MRCS_ENABLED)
 if (enable == 1)
 {
  set_task(20.0,"say_about",id)
 }
 return PLUGIN_CONTINUE
}
public client_command(id)
{
 slowestspeed(id,0)
 return PLUGIN_CONTINUE
}

get_weap_range(wp)
{
 if ((wp==CSW_SCOUT) || (wp==CSW_XM1014) || (wp==CSW_MAC10) || (wp==CSW_AUG) || (wp==CSW_UMP45) || (wp==CSW_SG550)
     || (wp==CSW_GALIL) || (wp==CSW_FAMAS) || (wp==CSW_AWP) || (wp==CSW_MP5NAVY) || (wp==CSW_M249) || (wp==CSW_M3)
     || (wp==CSW_M4A1) || (wp==CSW_TMP) || (wp==CSW_G3SG1) || (wp==CSW_SG552) || (wp==CSW_AK47) || (wp==CSW_P90))
 {
  return W_PRIMARY
 }
 else if ((wp==CSW_P228) || (wp==CSW_ELITE) || (wp==CSW_FIVESEVEN) || (wp==CSW_USP) || (wp==CSW_GLOCK18) || (wp==CSW_TMP)
           || (wp==CSW_DEAGLE))
 {  
  return W_SECONDARY
 }
 else if (wp==CSW_KNIFE)
 {
  return W_KNIFE
 }
 else if ((wp==CSW_HEGRENADE) || (wp==CSW_SMOKEGRENADE) || (wp==CSW_FLASHBANG))
 {
  return W_GRENADE
 }
 else if (wp==CSW_C4)
 {
  return W_BOMB
 }
 return W_NONE
}
get_normal_maxspeed(wp)
{
 new wrange=get_weap_range(wp)
 if (wrange==W_PRIMARY)
 {
  if (wp==CSW_MAC10) return speednormal[0]
  if (wp==CSW_MP5NAVY) return speednormal[0]
  if (wp==CSW_UMP45) return speednormal[0]
  if (wp==CSW_AWP) return speednormal[1]
  if (wp==CSW_SG550) return speednormal[1]
  if (wp==CSW_G3SG1) return speednormal[1]
  if (wp==CSW_M249) return speednormal[2]
  if (wp==CSW_AK47) return speednormal[3]
  if (wp==CSW_M4A1) return speednormal[4]
  if (wp==CSW_M3) return speednormal[4]
  if (wp==CSW_SG552) return speednormal[5]
  if (wp==CSW_AUG) return speednormal[6]
  if (wp==CSW_GALIL) return speednormal[6]
  if (wp==CSW_FAMAS) return speednormal[6]
  if (wp==CSW_XM1014) return speednormal[6]
  if (wp==CSW_P90) return speednormal[7]
  if (wp==CSW_SCOUT) return speednormal[8]
 }
 return speednormal[0]
}

public say_about(id)
{
 new slowing = get_cvar_num(MRCS_SLOWING)
 new hpnorm = get_cvar_num(MRCS_HPNORM)
 new hepower = get_cvar_num(MRCS_HEPOWER)

 client_print(id,print_chat,"[MoreRealCS] %L",id,"HELLO")
 client_print(id,print_chat,"[MoreRealCS] %L",id,"THACTP",slowing,hpnorm,hepower)
}

public slowestspeed(victim,show)
{
	new enable = get_cvar_num(MRCS_ENABLED)

	if ((enable==1) && is_user_alive(victim))
	{
	 new slowing = get_cvar_num(MRCS_SLOWING)
	 new hpvic   = get_user_health(victim);

	 new a,b
	 new weap=get_user_weapon(victim,a,b)
	 new maxspeed=get_normal_maxspeed(weap)

	 new Float:speedvic=1.0*maxspeed*(100-((100-hpvic)*slowing/100))/100
	
	 set_user_maxspeed(victim,speedvic)

	 if (show==1)
	 {
	  new sc=floatround(1.0*speedvic*100/maxspeed)

	  set_hudmessage(140, 0, 0, 0.05, 0.40, 2, 0.1, 4.0, 0.02, 0.02, 2)
	  show_hudmessage(victim,"[MoreRealCS] %L",victim,"PAHEH", sc)
	 }
	}
}

public notlonglive(victim)
{
 new enable = get_cvar_num(MRCS_ENABLED)
 if ((enable==1) && is_user_alive(victim))
 {
  new hp = get_user_health(victim)
  new hpnorm = get_cvar_num(MRCS_HPNORM)
  new hplose = get_cvar_num(MRCS_HPLOSE)

  if (hp<hpnorm) 
  {
   OnDamage(victim)
   set_user_health(victim,hp-hplose)
//   fakedamage(victim,"worldspawn",0,DMG_GENERIC)
  }
 }
}