#include ww2x\_drm;

init()
{
	if(drm_getcvar("scr_healthregen") == "")
		setCvar("scr_healthregen", "1");
	level.regen_model = drm_getcvarInt("scr_healthregen");
	if (level.regen_model < 0)
		level.regen_model = 1;
	if (level.regen_model > 4)
		level.regen_model = 1;

	if(drm_getcvar("scr_regendelay") == "")
		setCvar("scr_regendelay", "5000");
	level.playerHealth_RegularRegenDelay = drm_getcvarInt("scr_regendelay");

	precacheShader("overlay_low_health");

	if(level.regen_model)
	{
		level.healthOverlayCutoff = 0.45;
		level thread onPlayerConnect();
	}

	//Bleeding
	//thread maps\mp\gametypes\_bleeding::Init();
 }

onPlayerConnect()
{
	for(;;)
	{
		level waittill("connecting", player);
		player thread onPlayerSpawned();
		player thread onPlayerKilled();
		player thread onJoinedTeam();
		player thread onJoinedSpectators();
		player thread onPlayerDisconnect();
	}
}

onJoinedTeam()
{
	self endon("disconnect");
	
	for(;;)
	{
		self waittill("joined_team");
		self notify("end_healthregen");
	}
}

onJoinedSpectators()
{
	self endon("disconnect");
	
	for(;;)
	{
		self waittill("joined_spectators");
		self notify("end_healthregen");
	}
}

onPlayerSpawned()
{
	self endon("disconnect");
	
	for(;;)
	{
		self waittill("spawned_player");
		self thread playerHealthRegen();
	}
}

onPlayerKilled()
{
	self endon("disconnect");
	
	for(;;)
	{
		self waittill("killed_player");
		self notify("end_healthregen");
	}
}

onPlayerDisconnect()
{
	self waittill("disconnect");
	self notify("end_healthregen");
}

playerHealthRegen()
{
	level.regen_per_sec = 2;
	health_tick = 0;
	self endon("end_healthregen");

	maxhealth = self.health;
	oldhealth = maxhealth;
	player = self;

	gapHealth = 0;
	gapRecoverRatio = 0;
	sustainHealth = 0;
	sustainRegenRate = 0;
	woundGroan = 0;

	
	regenRate = 0.1; // 0.017;
	veryHurt = false;
	
	self thread playerBreathingSound(maxhealth * .45);
	lastSoundTime_Recover = 0;
	hurtTime = 0;
	newHealth = 0;
    //--------------V
    lastTime = 0;
	for (;;)
	{
		wait (0.05);
		if (player.health == maxhealth)
		{
			veryHurt = false;
			continue;
		}
					
		if (player.health <= 0)
			return;

		if (player.health < oldhealth)
		{
				gapHealth = (maxHealth - player.health);
				gapRecoverRatio = ((100 - gapHealth)/100);
				sustainHealth = int(player.health + (gapHealth * gapRecoverRatio));
				sustainRegenRate = regenRate * (gapRecoverRatio + .5);
				hurtTime = gettime();
		}
		wasVeryHurt = veryHurt;
		ratio = player.health / maxHealth;
		if (ratio <= level.healthOverlayCutoff)
			veryHurt = true;
			
		if (player.health >= oldhealth)
		{
			if (gettime() - hurttime < level.playerHealth_RegularRegenDelay)
				continue;

			if (gettime() - lastSoundTime_Recover > level.playerHealth_RegularRegenDelay)
			{
				if ( veryHurt && level.regen_model != 4)
				{
					lastSoundTime_Recover = gettime();
					if (player.health > oldhealth)
						self playLocalSound("breathing_better");
				}
			}

			if (level.regen_model > 2 && player.health >= sustainHealth)
				continue;
	
			if (veryHurt)
			{
				newHealth = ratio;
				//if(level.regen_model < 3)
				lastTime = hurtTime;

				if (gettime() > lastTime + 3000)
				{
					if(level.regen_model == 1)
					{
						newHealth += regenRate;
					}
					else
					{
						health_tick++;
						if (health_tick == 20)
						{
							player.health = player.health + level.regen_per_sec;
							if (player.health > maxhealth)
								player.health = maxhealth;

							if (level.regen_model > 2 && player.health > sustainHealth)
								player.health = sustainHealth;

							health_tick = 0;
						}
					}
					
					//lastTime = getTime();
				}
			}
			else
			{
				newHealth = ratio;
				//if(level.regen_model < 3)
				lastTime = hurtTime;

				if (gettime() > lastTime + 3000)
				{
					if(level.regen_model < 2)
					{
						newHealth =1;
					}
					else
					{
						health_tick++;
						if (health_tick == 20)
						{
							player.health = player.health + level.regen_per_sec;
							if (player.health > maxhealth)
								player.health = maxhealth;

							if (level.regen_model > 2 && player.health > sustainHealth)
								player.health = sustainHealth;

							health_tick = 0;
						}
					}

					//lastTime = getTime();
				}
			
			}
							
			if (newHealth > 1.0)
				newHealth = 1.0;
				
			if (newHealth <= 0)
			{
				// Player is dead
				player.health = 0;
				return;
			}
			
			if (level.regen_model == 1)
				player setnormalhealth (newHealth);

			oldhealth = player.health;
			continue;
		}

		oldhealth = player.health;
			
		hurtTime = gettime();
	}	
}

playerBreathingSound(healthcap)
{
	self endon("end_healthregen");
	self endon("disconnect");

	old_health = self.health;
	sound_limit_counter = 0;
	wait (2);

	for (;;)
	{
		wait (0.2);
		if (self.health <= 0)
			return;
			
		// Player still has a lot of health so no breathing sound
		if (self.health >= healthcap)
		{
			sound_limit_counter = 0;
			old_health = self.health;
			continue;
		}

		if (old_health == self.health)
		{
			if (sound_limit_counter < 20)
				sound_limit_counter++;
		}
		else
		{
			old_health = self.health;
			sound_limit_counter = 0;
		}

		if (sound_limit_counter > 7)
			continue;

		self playLocalSound("breathing_hurt");
		wait .784;
		wait (0.1 + randomfloat (0.8));
	}
}