#include extreme\_ex_lin;
#include extreme\_ex_utils;

campercheck()
{
	self notify("ex_campercheck_stop");
	self endon("ex_campercheck_stop");

	level endon("round_ended");
	self endon("spawned");
	self endon("ex_dead");
	self endon("disconnect");

	campingtime = 0;
	snipercampingtime = 0;
	show_warning = false;

	while(isPlayer(self) && isAlive(self) && self.sessionstate == "playing")
	{
		wait 0.5;

		// loop if?
		if(self.ex_isunknown || self.ex_iscamper || self.ex_sinbin || self.ex_inmenu) continue;

		// loop if player is in jail (LIB)
		if(level.ex_currentgt == "lib" && isDefined(self.in_jail) && self.in_jail) continue;

		startpos = self.origin;
		wait 0.5;
		endpos = self.origin;

		if(extreme\_ex_weapons::isWeaponType(self getcurrentweapon(),"sniper"))
		{
			if(level.ex_campsniper_warntime)
			{
				if(distance(startpos, endpos) < 20 && !self.ex_sinbin) snipercampingtime++;
					else snipercampingtime = 0;

				// show them a warning message
				if(snipercampingtime == level.ex_campsniper_warntime && !self.ex_isunknown)
					show_warning = true;
			}
			else continue;
		}
		else
		{
			if(level.ex_campwarntime)
			{
				if(distance(startpos, endpos) < 25 && !self.ex_sinbin) campingtime++;
					else campingtime = 0;

				// show them a warning message
				if(campingtime == level.ex_campwarntime && !self.ex_isunknown)
					show_warning = true;
			}
			else continue;
		}

		if(show_warning)
		{
			iprintlnboldFIXED(&"CAMPING_WARNING_MESSAGE_SELF", self, self);
			show_warning = false;
		}

		// ok, they didn't listen, punish them!
		if((campingtime >= level.ex_campobjtime || snipercampingtime >= level.ex_campsniper_objtime) && !self.ex_isunknown)
		{
			switch(level.ex_camppunish)
			{
				case 1:	self thread markTheCamper(); break;
				case 2:	self thread makeThemFart(); break;
				case 3:	self thread blowTheCamper(); break;
				case 4:	self thread shellshockPlayer(false); break;
				case 5:	self thread shellshockPlayer(true); break;
				default:
				{
					switch(randomInt(6))
					{
						case 2:	self thread makeThemFart(); break;
						case 3:	self thread blowTheCamper(); break;
						case 4:	self thread shellshockPlayer(false); break;
						case 5:	self thread shellshockPlayer(true); break;
						default:	self thread markTheCamper(); break;
					}
				}
			}
			
			campingtime = 0;
			snipercampingtime = 0;
			show_warning = false;
		}
	}
}

markTheCamper()
{
	level endon("round_ended");
	self endon("ex_dead");
	self endon("stopcamper");

	if(self.ex_iscamper || (isDefined(level.roundended) && level.roundended) || self.sessionstate != "playing") return;
 
	self removeCamper();
	self.ex_objnum = getObjective();

	if(self.ex_objnum)
	{
		self.ex_iscamper = true;

		// notify player and players
		iprintlnboldFIXED(&"CAMPING_MARKED_MESSAGE_SELF", self, self);
		self iprintlnbold(&"CAMPING_TIME_MESSAGE_SELF", level.ex_camptimer);
		iprintlnFIXED(&"CAMPING_MARKED_MESSAGE_ALL", self);
		iprintln(&"CAMPING_TIME_MESSAGE_ALL", level.ex_camptimer);

		compass_team = "none";
		if(self.pers["team"] == "allies") compass_icon = "objective_" + game["allies"];
			else compass_icon = "objective_" + game["axis"];

		objective_add(self.ex_objnum, "current", self.origin, compass_icon);
		objective_team(self.ex_objnum, compass_team);

		if(level.ex_camptimer >= 1) self thread countCamper();
	
		while(isPlayer(self) && isAlive(self) && self.pers["team"] != "spectator")
		{
			for(i=0;(i<60 && isPlayer(self) && isAlive(self));i++)
			{
				if((i <= 29) && self.ex_iscamper) objective_icon(self.ex_objnum, "objpoint_radio");
					else if((i >= 30) && self.ex_iscamper) objective_icon(self.ex_objnum, compass_icon);

				if(self.ex_iscamper) objective_position(self.ex_objnum, self.origin);

				wait 0.05;
			}
		}
	}
}

makeThemFart()
{
	level endon("ex_gameover");
	level endon("round_ended");

	self endon("ex_dead");
	self endon("stopcamper");
	self endon("disconnect");

	if(self.ex_iscamper || (isDefined(level.roundended) && level.roundended) || self.sessionstate != "playing") return;
 
	self.ex_iscamper = true;

	// notify player and players
	iprintlnboldFIXED(&"CAMPING_FART_MESSAGE_SELF", self, self);
	self iprintlnbold(&"CAMPING_TIME_MESSAGE_SELF", level.ex_camptimer);
	iprintlnFIXED(&"CAMPING_FART_MESSAGE_ALL", self);
	iprintln(&"CAMPING_TIME_MESSAGE_ALL", level.ex_camptimer);
	self thread countCamper();

	while(isPlayer(self) && isAlive(self) && self.pers["team"] != "spectator" && self.ex_iscamper)
	{
		if(isPlayer(self))
		{
			playfxontag(level.ex_effect["fartbomb"], self, "pelvis");
			self playLocalSound("fart");
		}

		wait randomInt(2) + 2;
	}
}

blowTheCamper()
{
	level endon("ex_gameover");
	level endon("round_ended");

	self endon("ex_dead");
	self endon("disconnect");

	if(self.ex_iscamper || (isDefined(level.roundended) && level.roundended) || self.sessionstate != "playing") return;

	self.ex_iscamper = true;
	
	iprintlnboldFIXED(&"CAMPING_BLOWN_MESSAGE_SELF", self, self);
	iprintlnFIXED(&"CAMPING_BLOWN_MESSAGE_ALL", self);
	wait 1.5;
	
	playfx(level.ex_effect["blowthefag"], self.origin);
	self playsound("mortar_explosion1");
	wait 0.05;
	self suicide();            
}

shellshockPlayer(diswep)
{
	level endon("ex_gameover");
	level endon("round_ended");

	self endon("ex_dead");
	self endon("disconnect");

	if(!isDefined(diswep)) diswep = false;

	if(self.ex_iscamper || (isDefined(level.roundended) && level.roundended) || self.sessionstate != "playing") return;
 
	self.ex_iscamper = true;
	time = undefined;

	// notify player and players
	iprintlnboldFIXED(&"CAMPING_SHOCK_MESSAGE_SELF", self, self);
	self iprintlnbold(&"CAMPING_TIME_MESSAGE_SELF", level.ex_camptimer);
	iprintlnFIXED(&"CAMPING_SHOCK_MESSAGE_ALL", self);
	iprintln(&"CAMPING_TIME_MESSAGE_ALL", level.ex_camptimer);
	self thread countCamper();

	while(isPlayer(self) && isAlive(self) && self.pers["team"] != "spectator" && self.ex_iscamper)
	{
		time = randomInt(5) + 5;
		if(isPlayer(self))
		{
			self shellshock("medical", time);
			if(diswep) self thread extreme\_ex_weapons::dropCurrentWeapon();
		}

		wait time + randomInt(5);
	}
}

countCamper()
{
	self endon("ex_dead");
	self endon("disconnect");
	self endon("spawned");

	wait level.ex_camptimer - 1;

	if(isPlayer(self))
	{
		self.ex_iscamper = false;
		wait 1;
	}

	if(isPlayer(self))
	{
		if(isDefined(self.ex_objnum)) self removeCamper();
		self notify("stopcamper");
		iprintlnboldFIXED(&"CAMPING_SURVIVED_MESSAGE_SELF",self, self);
		iprintlnFIXED(&"CAMPING_SURVIVED_MESSAGE_ALL", self);
	}
}

removeCampers()
{
	player = "";
	players = getentarray("player", "classname");

	for(i = 0; i < players.size; i++)
	{
		if(isPlayer(players[i]) && isDefined(players[i].ex_objnum))
			players[i] removeCamper();
	}
}

removeCamper()
{
	self endon("ex_dead");
	self endon("disconnect");

	if(isDefined(self.ex_objnum))
	{
		deleteObjective(self.ex_objnum);
		self.ex_objnum = undefined;
	}
}
