/* ----------------------------------------------------
   CREATE THE AREA EFFECT AND DAMAGE FOR THE GAS NADE
---------------------------------------------------- */
MonitorMustardGas(eInflictor, eAttacker, iDamage, iDFlags, sMeansOfDeath, sWeapon, vPoint, vDir, sHitLoc, psOffsetTime)
{
      level endon("wwx_gameover");

	// Play effect for 10 seconds
	gastime = 10;     
	for(j = 0; j <= gastime; j++)
	{
		// Loop through players and determine who is in range
		//if(!isDefined(vPoint))
		//vPoint = self.origin + (0,0,11);
		players = getentarray("player", "classname"); 
		for(i = 0; i < players.size; i++)
		{
			// Skip spawn protected players
			if(players[i].sessionteam == "spectator" || players[i].wwx_invulnerable)
				continue;

			// Respect friendly fire settings 0 (off) and 2 (reflect; it doesn't damage the attacker though)
			if(level.wwx_teamplay && (level.friendlyfire == "0" || level.friendlyfire == "2"))
				if(isPlayer(eAttacker) && (players[i] != eAttacker) && (players[i].pers["team"] == eAttacker.pers["team"]))
					continue;

			dst = distance(vPoint, players[i].origin); 

			// Area which player will be affected expands along with the gas/smoke (~2ft per sec)
			if( dst > 200 + (j * 20) || players[i].sessionstate != "playing" || !isAlive(players[i]) )
				continue; 

			// Calculate damage
			pcnt = 200 + (j * 20);
			if(dst > pcnt * .75 )                         // 0-25%
				iDamage = randomint(5);
			else if(dst > pcnt * .5 && dst <= pcnt * .75) // 25-50%
				iDamage = 10 + randomint(5);
			else if(dst > pcnt *.25 && dst <= pcnt * .5)  // 50-75%
				iDamage = 20 + randomint(5);
			else if(dst <= pcnt * .25)                    // 75-100%
				iDamage = 30 + randomint(5);

			// Damage the player
			players[i] thread GasPlayer(eAttacker, eAttacker, iDamage, 1, "MOD_GRENADE_SPLASH", sWeapon, vPoint, undefined, sHitLoc, psOffsetTime);
		}

		wait(1); 
	}
}

/* ----------------------------------------------------
   CAUSE THE DAMAGE & EFFECT 
---------------------------------------------------- */ 
GasPlayer(eAttacker, eAttacker, iDamage, iDFlags , sMeansOfDeath , sWeapon, vPoint , vDir, sHitLoc, psOffsetTime)
{
	self endon("killed_player");
	self endon("disconnect");

	if(!isDefined(self.wwx_puked) && randomint(100) > 30) // 30% chance to puke
	{
		playfxontag(level.wwx_effect["puke"], self, "j_Head");
		ww2x\_wwx_utils::forceto("crouch");
		self soundGassed("puke");
		self.wwx_puked = 1;
	}

	self soundGassed("choke");

	if(iDamage < self.health)
	{
		self.health = self.health - iDamage;
		self thread ww2x\_wwx_bleeding::doBleedPainSound();
	}
	else
		self finishPlayerDamage(eAttacker, eAttacker, iDamage, iDFlags, sMeansOfDeath, sWeapon, (self.origin + (0,0,-300)), vDir, sHitLoc, psOffsetTime);
}

/* ----------------------------------------------------
   CREATE THE AREA EFFECT AND DAMAGE FOR THE COCKTAIL
---------------------------------------------------- */
MonitorCocktail(eInflictor, eAttacker, iDamage, iDFlags, sMeansOfDeath, sWeapon, vPoint, vDir, sHitLoc, psOffsetTime)
{ 	
      level endon("wwx_gameover");

	// Play effect for 13 seconds
	burntime = 13;
	for(j = 0; j < burntime; j++)
	{ 
		// Loop through players and determine who is in range
		//if (!isDefined(vPoint))
		//	vPoint = self.origin + (0,0,11);

		players = getentarray("player", "classname"); 
		for(i = 0; i < players.size; i++)
		{
			// Skip spawn protected players
			if(players[i].sessionteam == "spectator" || players[i].wwx_invulnerable)
				continue;

			// Respect friendly fire settings 0 (off) and 2 (reflect; it doesn't damage the attacker though)
			if(level.wwx_teamplay && (level.friendlyfire == "0" || level.friendlyfire == "2"))
				if(isPlayer(eAttacker) && (players[i] != eAttacker) && (players[i].pers["team"] == eAttacker.pers["team"]))
					continue;

			dst = distance(vPoint, players[i].origin); 
			if( dst > 240 || players[i].sessionstate != "playing" || !isAlive(players[i]) ) 
				continue; 

			// Calculate damage
			if(dst < 240)
				iDamage = int(45 * (1 - (dst / 240)));

			if(dst <= 140) players[i] thread Burn(13);
				else players[i] thread Burn(5);

			// Damage the player
			if(iDamage < players[i].health)
			{
				players[i].health = players[i].health - iDamage;
				players[i] thread ww2x\_wwx_bleeding::doBleedPainSound();
			}
			else
				players[i] finishPlayerDamage(eAttacker, eAttacker, iDamage, 1, "MOD_EXPLOSIVE", sWeapon, (players[i].origin + (0,0,-300)), vDir, sHitLoc, psOffsetTime);
		}

		wait(1); 
	}
}

/* ----------------------------------------------------
   CAUSE THE DAMAGE & EFFECT
---------------------------------------------------- */
Burn(burntime)
{
	self endon("killed_player");
	self endon("disconnect");

	if(isDefined(self.wwx_isonfire))
		return;	

	self.wwx_isonfire = 1;
	tag = "j_spine1";
	
	for(i = 0; i < burntime; i++)
	{
		switch(randomint(13))
		{
			case 0: tag = "j_hip_le"; break;
			case 1: tag = "j_hip_ri"; break;
			case 2: tag = "j_knee_le"; break;
			case 3: tag = "j_ankle_ri"; break;
			case 4: tag = "j_knee_ri"; break;
			case 5: tag = "j_spine1"; break;
			case 6:	tag = "j_wrist_ri"; break;
			case 7: tag = "j_head"; break;
			case 8:tag = "j_shoulder_le"; break;
			case 9:tag = "j_shoulder_ri"; break;
			case 10:tag = "j_elbow_le"; break;
			case 11: tag = "j_elbow_ri"; break;
			case 12: tag = "j_wrist_le"; break;
		}

		if(burntime == 5)
		{
			if(isDefined(self))
				self thread burnfx(tag, level.wwx_effect["playerburn2"], .1);
		}
		else
		{
			if(isDefined(self))
			{
				playfxontag(level.wwx_effect["playerburn2"], self, tag);
				playfxontag(level.wwx_effect["playerburn"], self, "j_spine1");
			}		
		}

		wait .25;
	}

	if(isAlive(self))
		self.wwx_isonfire = undefined;
}

burnfx(tag, fxName, loopTime)
{
	self endon("killed_player");
	self endon("disconnect");

	while(isdefined(self) && isDefined(self.wwx_isonfire))
	{
		playfxOnTag(fxName, self, tag);
		wait(loopTime);
	}
}

soundGassed(sound)
{
	self endon("killed_player");
      self endon("disconnect");

	if(!isDefined(self.gassed))
	{
		self.gassed = 1;

		if(sound == "puke" || sound == "choke")
		{
			if(sound == "choke")
				self playsound("choke");
			else if(sound == "puke")
				self playsound("puke");
		}
	}

	wait 2;

	if(isAlive(self))
		self.gassed = undefined;
}
