#include extreme\_ex_lin;

spawnprotection()
{
	level endon("ex_gameover");
	self endon("disconnect");
	self endon("ex_dead");

	count = 0;
	beepcount = 0;
	rangex = 0;
	spos = self.origin;
	stimer = level.ex_spwn_time;

	if(level.ex_spwn_hud)
	{
		self.ex_spwpro = newClientHudElem(self);
		self.ex_spwpro.x = 120;
		self.ex_spwpro.y = 390;
		self.ex_spwpro.alignX = "center";
		self.ex_spwpro.alignY = "middle";
		self.ex_spwpro.alpha = 1;
		self.ex_spwpro setShader("gfx/hud/hud@health_cross.tga",32,32);
	}

	self.ex_invulnerable = true;
	self.ex_spawnprotected = true;

	msg1 = &"SPAWNPROTECTION_ENABLED";
	msg2 = extreme\_ex_utils::time_convert(stimer);

	switch(level.ex_spwn_msg)
	{
		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;
		}
	}

	if(level.ex_spwn_wepdisable) self disableWeapon();

	// Invisible Spawn Protection ON
	if(level.ex_spwn_invisible) self hide();

	while(isAlive(self) && self.ex_invulnerable && !isdefined(self.teamhaschanged) && self.sessionstate == "playing" && count < (stimer * 20) && !((self attackButtonPressed() || self meleeButtonPressed()) && self getCurrentWeapon()!="none" ) )
	{
		if(level.ex_spwn_range && !isdefined(self.ex_isparachuting))
		{
			distmoved = distance(spos, self.origin);
			if(distmoved > level.ex_spwn_range)
			{
				rangex = 1;
				count = (stimer * 20);
			}
		}

		if(isdefined(self.ex_isparachuting))
			count = 0;

		wait 0.05;
		count++;
		beepcount++;
		if(beepcount == 20)
		{
			if(level.ex_spwn_headicon)
			{
				self.headicon = game["headicon_protect"];
				self.headiconteam = "none";
			}
			beepcount = 0;
		}
	}

	msg3 = undefined;

	if(self attackButtonPressed() && (!isdefined(self.teamhaschanged))) msg3 = &"SPAWNPROTECTION_FIRE_BUTTON_PRESSED";
	if(self.sessionstate == "playing" && rangex == 1) msg3 = &"SPAWNPROTECTION_MOVED_AWAY_AREA";

	if(isdefined(msg3))
	{
		switch(level.ex_spwn_msg)
		{
			case 0: self iprintln(msg3); break;
			case 1: self iprintlnbold(msg3); break;
			default:
			{
				self thread extreme\_ex_utils::ex_hud_announce(msg3, 0);
				break;
			}
		}
	}

	// restore the headicon if changed
	if(level.ex_spwn_headicon && self.sessionstate == "playing")
	{
		if(level.ex_currentgt == "hm")
			self thread maps\mp\gametypes\hm::Headicon_Restore();
		else
			self thread extreme\_ex_utils::restoreHeadicon(game["headicon_protect"]);
	}

	msg4 = &"SPAWNPROTECTION_DISABLED";

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

	if(isdefined(self.ex_spwpro))
		self.ex_spwpro destroy();

	// Invisible Spawn Protection OFF
	if(level.ex_spwn_invisible) self show();

	if(level.ex_spwn_wepdisable) self enableWeapon();
	self.ex_spawnprotected = undefined;
	self.ex_invulnerable = false;
}

punish(reason)
{
	level endon("ex_gameover");
	self endon("ex_dead");
	self endon("disconnect");

	if(isDefined(self.ex_spwn_punish)) return;
	self.ex_spwn_punish = true;

	if(isPlayer(self))
	{
		if(reason == "abusing")
		{
			iprintlnFIXED(&"SPAWNPROTECTION_PUNISH_ABUSER_MSG", self);
			self iprintlnbold(&"SPAWNPROTECTION_PUNISH_ABUSER_PMSG");
		}

		if(reason == "attacking")
		{
			iprintlnFIXED(&"SPAWNPROTECTION_PUNISH_ATTACKER_MSG", self);
			self iprintlnbold(&"SPAWNPROTECTION_PUNISH_ATTACKER_PMSG");
		}
	}

	pun = randomInt(100);

	if(pun < 50)
	{
		if(isPlayer(self)) self disableWeapon();
		wait 5;
	}
	else for(i = 0; i < 5; i++)
	{
		if(isPlayer(self)) self extreme\_ex_weapons::dropcurrentweapon();
		wait 1;
	}

	if(isPlayer(self))
	{
		if(reason == "abusing") self iprintlnbold(&"SPAWNPROTECTION_FREE_ABUSER_PMSG");
		else if(reason == "attacking") self iprintlnbold(&"SPAWNPROTECTION_FREE_ATTACKER_PMSG");

		self enableWeapon();
		self.ex_spwn_punish = undefined;
	}
}
