
firstaid()
{
	level endon("ex_gameover");
	self endon("disconnect");
	self endon("ex_spawned");
	self endon("ex_dead");

	if(!isDefined(self.ex_firstaidicon))
	{
		self.ex_firstaidicon = newClientHudElem(self);			
		self.ex_firstaidicon.alignX = "center";
		self.ex_firstaidicon.alignY = "middle";
		self.ex_firstaidicon.alpha = 1;
		//self.ex_firstaidicon.x = 580;
		//self.ex_firstaidicon.y = 297;
		self.ex_firstaidicon.x = 607;
		self.ex_firstaidicon.y = 367;
		self.ex_firstaidicon setShader(game["firstaidicon"], 16, 16);		
	}

	if(!isDefined(self.ex_firstaidtxt))
	{
		self.ex_firstaidtxt = newClientHudElem(self);			
		self.ex_firstaidtxt.alignX = "center";
		self.ex_firstaidtxt.alignY = "middle";
		self.ex_firstaidtxt.alpha = 1;
		self.ex_firstaidtxt.fontScale = 0.5;
		self.ex_firstaidtxt.x = 607;
		self.ex_firstaidtxt.y = 367;
		self.ex_firstaidtxt.color = (1,1,1);
		self.ex_firstaidtxt setText(&"FIRSTAID_MEDI");	
	}
	
	if(!isDefined(self.ex_firstaidval))
	{
		self.ex_firstaidval = newClientHudElem(self);
		self.ex_firstaidval.fontScale = 1;
		self.ex_firstaidval.alignX = "center";
		self.ex_firstaidval.alignY = "middle";
		self.ex_firstaidval.alpha = 1;
		//self.ex_firstaidval.x = 563;
		//self.ex_firstaidval.y = 296;
		self.ex_firstaidval.x = 618;
		self.ex_firstaidval.y = 367;	
		if(self.ex_firstaidkits > 0) self.ex_firstaidval.color = (1, 1, 1);
		else self.ex_firstaidval.color = (1, 0, 0);
			
		self.ex_firstaidval setValue(self.ex_firstaidkits);
	}

	self.ex_canheal = true;
	self.ex_targetplayer = undefined;

	while(isPlayer(self) && isDefined(self.ex_firstaidicon) && self.sessionstate != "spectator")
	{
		wait 0.5;

		if(isPlayer(self) && (self useButtonPressed()) && self isOnGround() && self.ex_canheal)
		{
			players = getentarray("player", "classname");
			for(i = 0; i < players.size; i++)
			{
				if(level.ex_medicself == 0)
				{
					if(players[i] == self)
						continue;	// can't heal yourself
				}

				if(players[i].pers["team"] == self.pers["team"] 			// teammate
					&& isalive(players[i]) 												// who is playing
					&& players[i].health <= 80										// and is injured
					&& !isDefined(players[i].gettingfirstaid)					// and is not currently being treated
					&& distance(players[i].origin, self.origin) < 48		// and within 4 feet of player
				)
				{
					if(!level.ex_teamplay)
					{
						if(players[i] == self && level.ex_medicself != 0)
						{
							self.ex_targetplayer = players[i];
							break;
						}
					}
					else if(level.ex_teamplay)
					{
						self.ex_targetplayer = players[i];
							break;
					}					
				}
			}

			// not in range of any friendlies that need healing
			if(!isDefined(self.ex_targetplayer)) continue;

			// all systems go, commence healing
			// wait 0.5 seconds (make sure they mean it, are holding USE)
			holdtime = 0;

			while(isPlayer(self) && self useButtonPressed() && holdtime < 0.5 && self isOnGround() && self.ex_targetplayer isOnGround() && distance(self.ex_targetplayer.origin, self.origin) < 48 )
			{
				holdtime += 0.05;
				wait 0.05;
			}

			if(holdtime < 0.5) continue;

			if(isPlayer(self))
			{
				// can't heal while defusing a bomb	
				if(isDefined(self.defuseicon)) continue;
	
				// can't heal while moving
				if(isDefined(self.ex_moving) && self.ex_moving) continue;
	
				// can't heal if calling in mortars, artillery or an airstrike
				if(self.ex_binocuse) continue;
	
				// can't heal if target players health is 100%
				if(self.ex_targetplayer.health == 100) continue;
	
				// can't heal near ammo crates
				if(isDefined(self.ex_amc_check)) continue;
	
				//stop them flashing on compass as needing medic
				self.ex_targetplayer.needshealing = false;
		
				healamount = (level.ex_minheal + randomInt(level.ex_maxheal - level.ex_minheal));
				healtime = int(healamount / 2) * .1;
				
				self playlocalsound("medi_bag");
				self.ex_targetplayer shellshock("medical", 4);
				self disableWeapon();
	
				// fade counter
				if(isDefined(self.ex_firstaidval))
				{
					self.ex_firstaidval fadeOverTime(1);
					self.ex_firstaidval.alpha = 0;
				}
	
				if(isDefined(self.ex_firstaidval))
				{
					self.ex_firstaidtxt fadeOverTime(1);
					self.ex_firstaidtxt.alpha = 0;
				}
				
				if(isDefined(self.ex_firstaidicon))
					self.ex_firstaidicon scaleOverTime(healtime, 32, 32);
		
				healnow = 0;
				holdtime = 0;
				beepcount = 0;
				sprintcount = 0;
	
				while(self useButtonPressed()											// still holding the USE key
					&& !(self meleeButtonPressed())									// player hasn't melee'd
					&& !(self.ex_targetplayer meleeButtonPressed())			// target hasn't melee'd
					&& !(self attackButtonPressed())									// player hasn't fired
					&& !(self.ex_targetplayer attackButtonPressed())			// target hasn't fired
					&& isalive(self) && isalive(self.ex_targetplayer) 			// both still alive
					&& self.ex_targetplayer.health < 100 							// hasn't filled target's health
					&& healamount > 0														// hasn't run out of healamount
				){
					if(healnow == 1)
					{
						self.ex_targetplayer.health++;	 // 10 health per second, 1 point every other 1/20th of a second (server frame) had to do that 'cause of integer rounding issues
						healamount--;
						healnow = -1;
	
						self.ex_ishealing = true;
					}
	
					healnow++;
					beepcount++;
					sprintcount++;
					holdtime += 0.05;
					wait 0.05;
	
					// still recover from sprint
					if(level.ex_sprint && sprintcount > 1)
					{
						if(self.ex_sprinttime < level.ex_sprinttime)
							self.ex_sprinttime++;
						
						sprintcount = 0;
					}
	
	
					if(beepcount > 20)
					{
						if (self.health >70)
						{
							self playlocalsound("medi_use_high");
							beepcount = 0;
						}
						else
						{
							self playlocalsound("medi_use_low");
							beepcount = 0;
						}
					}
				}
	
				if(isDefined(self.ex_ishealing)) self.ex_ishealing = undefined;
	
				if(isPlayer(self.ex_targetplayer)) self.ex_targetplayer playsound("sprintover");
	
				if((healamount == 0 || self.ex_targetplayer.health == 100) && isalive(self.ex_targetplayer) && isalive(self))
				{
					if(self.name == self.ex_targetplayer.name)
					{
						iprintln(&"FIRSTAID_APPLIED_SELF", self);
						self playSound("health_pickup_medium");
					}
					else
					{
						iprintln(&"FIRSTAID_APPLIED_TEAM_MSG1",self.ex_targetplayer);
						iprintln(&"FIRSTAID_APPLIED_TEAM_MSG2", self);
						self playSound("health_pickup_medium");
						self.score++;
						teamscore = getTeamScore(self.pers["team"]);
						teamscore++;
						setTeamScore(self.pers["team"], teamscore);
					}
				}
	
				if(isDefined(self.ex_firstaidicon)) self.ex_firstaidicon scaleOverTime(1, 16, 16);
	
				self.ex_firstaidkits--;
				self enableWeapon();
	
				if(isDefined(self.ex_firstaidval))
				{
					if(self.ex_firstaidkits > 0) self.ex_firstaidval.color = (1, 1, 1);
					else self.ex_firstaidval.color = (1, 0, 0);
		
					self.ex_firstaidval setValue(self.ex_firstaidkits);
					self.ex_firstaidicon.alpha = 0.8;
				}
	
				// fadein counter
				if(isDefined(self.ex_firstaidval))
				{
					self.ex_firstaidval fadeOverTime(1);
					self.ex_firstaidval.alpha = 1;
				}
	
				if(isDefined(self.ex_firstaidtxt))
				{
					self.ex_firstaidtxt fadeOverTime(1);
					self.ex_firstaidtxt.alpha = 1;
				}
	
				wait 0.5;

				if(isPlayer(self))
				{				
					if(self.ex_firstaidkits == 0) self.ex_canheal = false;
		
					if(level.ex_firstaidkitmsg)
					{
						if(self.ex_firstaidkits >= 2) self iprintlnbold(&"FIRSTAID_YOU_HAVE_NUMBER_LEFT", self.ex_firstaidkits);
						else if(self.ex_firstaidkits == 1) self iprintlnbold(&"FIRSTAID_ONE_KIT_LEFT");
						else if (self.ex_firstaidkits == 0) self iprintlnbold(&"FIRSTAID_NO_KIT_LEFT");
					}
	
					// Remove bulletholes if present
					if(level.ex_bulletholes && isDefined(self.ex_targetplayer.ex_bulletholes) && self.ex_targetplayer.health == 100 && isalive(self.ex_targetplayer))
					{
						for(i=0;i<self.ex_targetplayer.ex_bulletholes.size;i++)
						{
							if(isDefined(self.ex_targetplayer.ex_bulletholes[i])) self.ex_targetplayer.ex_bulletholes[i] destroy();
						}
					}
				
					if(isDefined(self.spamdelay)) self.spamdelay = undefined;
				}
			}
		}
	}
}

disablePlayerHealing()
{
	level endon("ex_gameover");
	self endon("disconnect");
	self endon("ex_dead");
	self endon("spawned");
	
	self.ex_canheal = false;

	if(isDefined(self.ex_firstaidval))
	{
		self.ex_firstaidval fadeOverTime(1);
		self.ex_firstaidval.alpha = 0;
		self.ex_firstaidicon fadeOverTime(1);
		self.ex_firstaidicon.alpha = 0;
		self.ex_firstaidtxt fadeOverTime(1);
		self.ex_firstaidtxt.alpha = 0;
	}
	
	self thread shownohealtime(level.ex_revokeonth);

	self waittill("fa_punishover");
	
	if(isDefined(self.ex_firstaidval))
	{	
		for(i = 0; i < 1; i+=0.1)
		{
			if(isDefined(self.ex_firstaidicon))
				self.ex_firstaidicon.alpha = i;

			if(isDefined(self.ex_firstaidtxt))
				self.ex_firstaidtxt.alpha = i;

			if(isDefined(self.ex_firstaidkits))
			{
				if(isDefined(self.ex_firstaidval))
				{
					if(self.ex_firstaidkits > 0) self.ex_firstaidval.color = (1, 1, 1);
					else self.ex_firstaidval.color = (1, 0, 0);
				
					self.ex_firstaidval.alpha = i;
				}
			}

			wait 0.2;
		}

		if(isDefined(self.ex_firstaidkits))
		{
			if(isDefined(self.ex_firstaidval))
			{
				self.ex_firstaidval.alpha = 1;

				if(self.ex_firstaidkits > 0) self.ex_firstaidval.color = (1, 1, 1);
				else self.ex_firstaidval.color = (1, 0, 0);
			}
		}

		if(isDefined(self.ex_firstaidicon))
		{
			self.ex_firstaidicon.alpha = 1;
			self.ex_firstaidicon.color = (1, 1, 1);
		}
	}
}

shownohealtime(sec)
{
	level endon("ex_gameover");
	self endon("disconnect");

	if(isDefined(self.ex_noheal))
	{
		self.ex_extrapen = true;
		return;
	}

	msg1 = &"FIRSTAID_DISABLED";
	msg2 = extreme\_ex_utils::time_convert(level.ex_revokeonth);

	switch(level.ex_firstaidmsg)
	{
		case 0: self iprintln(msg1); self iprintln(msg2); break;
		case 1: self iprintlnbold(msg1); self iprintlnbold(msg2); break;
		default:
		{
			self thread extreme\_ex_utils::ex_hud_announce(msg1, 0);
			self thread extreme\_ex_utils::ex_hud_announce(msg2, 1);
			break;
		}
	}

	self.ex_noheal = true;

	while(isAlive(self) && sec >= 1 && self.sessionstate != "spectator")
	{
		if(isDefined(self.ex_extrapen))
		{
			sec = sec + 10;
			self.ex_extrapen = undefined;

			if(sec >= 60) sec = 60;

			msg1 = &"FIRSTAID_DISABLED";
			msg2 = extreme\_ex_utils::time_convert(sec);

			switch(level.ex_firstaidmsg)
			{
				case 0: self iprintln(msg1); self iprintln(msg2); break;
				case 1: self iprintlnbold(msg1); self iprintlnbold(msg2); break;
				default:
				{
					self thread extreme\_ex_utils::ex_hud_announce(msg1, 0);
					self thread extreme\_ex_utils::ex_hud_announce(msg2, 1);
					break;
				}
			}
		}

		sec--;

		if(isDefined(self.ex_nohealtime)) self.ex_nohealtime setValue(sec);

		wait 1;
	}

	if(isDefined(self.ex_nohealtime)) self.ex_nohealtime setValue(0);	

	if(isPlayer(self))
	{
		self notify("fa_punishover");
		self.ex_canheal = true;
		self.ex_noheal = undefined;
	}
}

callformedic()
{
	level endon("ex_gameover");
	self endon("spawned");
	self endon("death");

	if(!isDefined(self.pers["team"]) || self.pers["team"] == "spectator") return;

	if(isDefined(self.spamdelay))
	{
		if(level.ex_callformedic != 1) self iprintlnbold(&"FIRSTAID_SPAMMER");
		return;
	}

	if(!level.ex_teamplay)
	{
		if(level.ex_callformedic == 2) self iprintlnbold(&"FIRSTAID_NOT_TEAM_BASED");
		return;
	}

	if(!isDefined(self.pers["team"]) || self.pers["team"] == "spectator" || !level.ex_callformedic)
	{
		self iprintlnbold(&"FIRSTAID_UNAVAILABLE");
		return;
	}

	self.spamdelay = true;
	soundalias = undefined;

	if(level.ex_callformedic >= 1)
	{
		if (self.pers["team"] == "allies")
		{
			switch(game["allies"])
			{
				case "american":
				soundalias = "american_medic";
				break;
				
				case "british":
				soundalias = "british_medic";
				break;
		
				default:
				soundalias = "russian_medic";
				break;
			}
		}
		else if (self.pers["team"] == "axis")
		{
			switch(game["axis"])
			{
				case "german":
				soundalias = "german_medic";
				break;
			}
		}
	}

	self maps\mp\gametypes\_quickmessages::saveHeadIcon();
	self maps\mp\gametypes\_quickmessages::doQuickMessage(soundalias, &"FIRSTAID_MEDIC_CALL");

	if(level.ex_showinjp)
	{
		self.needshealing = true;
		self thread ShowInjured();
	}

	wait 2;

	if(isPlayer(self))
	{
		self.spamdelay = undefined;
		self maps\mp\gametypes\_quickmessages::restoreHeadIcon();
	}
}

ShowInjured()
{
	level endon("ex_gameover");
	self endon("disconnect");
	self endon("ex_dead");
	self endon("ex_spawned");

	while(isalive(self) && self.needshealing && self.sessionstate == "playing")
	{
		wait level.ex_showinjptime;
		self pingPlayer();
	}
}
