#include extreme\_ex_lin;

playerRankMonitor()
{
	level endon("ex_gameover");
	self endon("ex_disconnect");
	self endon("ex_dead");

	self.ex_artillery_strike = false;
	self.ex_mortar_strike = false;
	self.ex_air_strike = false;

	self.pers["rank"] = self getRank();
	self.pers["newrank"] = self.pers["rank"];

	if(level.ex_roundbased) level.newround_time = ((level.roundlength * 60) - level.graceperiod);

	while(isPlayer(self) && !level.ex_gameover)
	{
		self.pers["newrank"] = self getRank();

		// If old "rank" isnt the same as the new rank check
		if(self.pers["rank"] != self.pers["newrank"])
		{
			// promoted or demoted?
			if(self.pers["rank"] < self.pers["newrank"])
			{
				// Update here, so the weapon update is based on the new rank
				self.pers["rank"] = self.pers["newrank"];

				self thread rankupdate_hudOnly(true);

				if(level.ex_roundbased && level.roundstarted && level.newround_time > 2)
					self thread rankupdate(true);
				else if(!level.ex_roundbased)
					self thread rankupdate(true);
			}
			else if(self.pers["rank"] > self.pers["newrank"])
			{
				// Update here, so the weapon update is based on the new rank
				self.pers["rank"] = self.pers["newrank"];

				self thread rankupdate_hudOnly(false);

				if(level.ex_roundbased && level.roundstarted && level.newround_time > 2)
					self thread rankupdate(false);
				else if(!level.ex_roundbased)
					self thread rankupdate(false);
			}
		}

		if(level.drawfriend && level.ex_teamplay && level.ex_currentgt != "hm")
		{
			self.headicon = extreme\_ex_ranksystem::get_Headicon(self.pers["rank"]);
		}
		else if(level.ex_currentgt != "hm")
		{
			self.headicon = "";
		}

		self.statusicon = self thread get_Current_Rank();

		// launch wdm (if the wdm binoculars are not restricted)
		if(isPlayer(self) && isDefined(self.ex_haswmdbinocs) && self.ex_haswmdbinocs) self thread checkWmd();

		wait 1;
	}
}

checkWmd()
{
	self endon("disconnect");

	if(isDefined(self.ex_checkingwmd) || isDefined(self.mortar_available_icon) || isDefined(self.artillery_available_icon) || isDefined(self.airstrike_available_icon)) return;

	self.ex_checkingwmd = true;
	
	if(level.ex_rank_wmdtype == 2) self thread wmdAnyAllowed();
	else if(level.ex_rank_wmdtype == 1) self thread wmdRandom();
	else if(level.ex_rank_wmdtype == 0) self thread wmdFixed();

	wait 5;

	if(isPlayer(self)) self.ex_checkingwmd = undefined;
}

wmdAnyAllowed()
{
	self endon("disconnect");

	// if not allowed any wmd's return
	if(!level.ex_rank_allow_mortar && !level.ex_rank_allow_artillery && !level.ex_rank_allow_airstrike && !level.ex_rank_allow_napalm) return;

	if(isPlayer(self) && self.pers["rank"] < level.ex_rank_allow_rank) return;

	if(self.ex_mortar_strike || self.ex_artillery_strike || self.ex_air_strike) return;

	self.ex_mortar_allowed = false;
	self.ex_artillery_allowed = false;
	self.ex_airstrike_allowed = false;

	if(level.ex_rank_allow_mortar) self.ex_mortar_allowed = true;
	if(level.ex_rank_allow_artillery) self.ex_artillery_allowed = true;
	if(level.ex_rank_allow_airstrike || level.ex_rank_allow_napalm) self.ex_airstrike_allowed = true;

	for(;;)
	{
		rand = randomInt(3);
		
		if(rand == 0 && self.ex_mortar_allowed) break;
		if(rand == 1 && self.ex_artillery_allowed) break;
		if(rand == 2 && self.ex_airstrike_allowed) break;

		wait 0.1;
	}

	// clear old hud elems
	if(isDefined(self.mortar_available_icon)) self.mortar_available_icon destroy();
	if(isDefined(self.artillery_available_icon)) self.artillery_available_icon destroy();
	if(isDefined(self.airstrike_available_icon)) self.airstrike_available_icon destroy();

	delay = randomInt(level.ex_rank_wmd_max - level.ex_rank_wmd_min) + level.ex_rank_wmd_min;

	if(rand == 0) self thread extreme\_ex_mortar_player::init(delay);
	else if(rand == 1) self thread extreme\_ex_artillery_player::init(delay);
	else if(rand == 2) self thread extreme\_ex_airstrike_player::init(delay);
}

wmdFixed()
{
	self endon("disconnect");

	if(isPlayer(self) && self.pers["rank"] >= level.ex_rank_mortar && self.pers["rank"] < level.ex_rank_artillery && !self.ex_mortar_strike)
	{
		self thread extreme\_ex_mortar_player::init();
		return;
	}

	if(isPlayer(self) && self.pers["rank"] >= level.ex_rank_artillery && self.pers["rank"] < level.ex_rank_airstrike && !self.ex_artillery_strike)
	{
		// stop mortars
		self.ex_mortar_strike = false;
		self notify("mortar_over");
		self notify("end_mortar");
		wait 0.5;

		if(isPlayer(self)) self thread extreme\_ex_artillery_player::init();
		return;
	}

	if(isPlayer(self) && self.pers["rank"] >= level.ex_rank_airstrike && !self.ex_air_strike)
	{
		// stop mortars
		self.ex_mortar_strike = false;
		self notify("mortar_over");
		self notify("end_mortar");

		// stop artillery
		self.ex_artillery_strike = false;
		self notify("artillery_over");
		self notify("end_artillery");
		
		wait 0.5;
		
		if(isPlayer(self)) self thread extreme\_ex_airstrike_player::init();
	}
}

wmdRandom()
{
	self endon("disconnect");

	if(self.ex_mortar_strike || self.ex_artillery_strike || self.ex_air_strike) return;
	
	self.ex_mortar_allowed = false;
	self.ex_artillery_allowed = false;
	self.ex_airstrike_allowed = false;

	if(isPlayer(self) && self.pers["rank"] >= level.ex_rank_mortar) self.ex_mortar_allowed = true;
	if(isPlayer(self) && self.pers["rank"] >= level.ex_rank_artillery) self.ex_artillery_allowed = true;
	if(isPlayer(self) && self.pers["rank"] >= level.ex_rank_airstrike) self.ex_airstrike_allowed = true;

	wmdtodo = 0;
	delay = randomInt(level.ex_rank_wmd_max - level.ex_rank_wmd_min) + level.ex_rank_wmd_min;
	chance = randomInt(100);

	if(self.ex_mortar_allowed && !self.ex_artillery_allowed && !self.ex_airstrike_allowed) self thread extreme\_ex_mortar_player::init(delay);
	else if(self.ex_mortar_allowed && self.ex_artillery_allowed && !self.ex_airstrike_allowed)
	{
		if(chance < 50) wmdtodo = 1;
		else if(chance >= 50) wmdtodo = 2;
	}
	else if(self.ex_mortar_allowed && self.ex_artillery_allowed && self.ex_airstrike_allowed)
	{
		if(chance < 33) wmdtodo = 1;
		else if(chance >= 33 && chance < 66) wmdtodo = 2;
		else if(chance >= 66) wmdtodo = 3;
	}

	if(wmdtodo == 0) return;

	// clear old hud elems
	if(isDefined(self.mortar_available_icon)) self.mortar_available_icon destroy();
	if(isDefined(self.artillery_available_icon)) self.artillery_available_icon destroy();
	if(isDefined(self.airstrike_available_icon)) self.airstrike_available_icon destroy();

	// stop mortars
	self.ex_mortar_strike = false;
	self notify("mortar_over");
	self notify("end_mortar");

	// stop artillery
	self.ex_artillery_strike = false;
	self notify("artillery_over");
	self notify("end_artillery");

	// stop airstrike
	self.ex_air_strike = false;
	self notify("airstrike_over");
	self notify("end_airstike");

	if(wmdtodo == 1) self thread extreme\_ex_mortar_player::init(delay);
	else if(wmdtodo == 2) self thread extreme\_ex_artillery_player::init(delay);
	else if(wmdtodo == 3) self thread extreme\_ex_airstrike_player::init(delay);
}

rankupdate_hudOnly(promotion)
{
	if(level.ex_rankhud && isPlayer(self)) self thread rankHud();
}

rankupdate(promotion)
{
	self endon("disconnect");
	
	rankstring = self getRankstring(self.pers["rank"]);

	while(self.sessionstate != "playing") wait 0.5;
	
	if(promotion)
	{
		if(level.ex_rank_announce == 1)
		{
			
			iprintlnboldFIXED(&"RANK_PROMOTION_MSG", self, self);
			self iprintlnbold(&"RANK_PROMOTION_START", &"RANK_MIDDLE_MSG", rankstring);
			self notify("rank changed");
			
			if(level.ex_currentgt == "cnq" || level.ex_currentgt == "rbctf" || level.ex_currentgt == "rbcnq")
				self playLocalSound("ctf_touchown");
			else
				self playLocalSound("promotion");
			
			self thread PromotionImage();
		}

		if(level.ex_rank_promo_addnades) self extreme\_ex_weapons::updateLoadout(true);
			else self extreme\_ex_weapons::updateLoadout(false);
	}
	else
	{
		if(level.ex_rank_announce == 1)
		{
			iprintlnboldFIXED(&"RANK_DEMOTION_MSG", self, self);
			self iprintlnbold(&"RANK_DEMOTION_START", &"RANK_MIDDLE_MSG", rankstring);
			self notify("rank changed");
			
			if(level.ex_currentgt == "cnq" || level.ex_currentgt == "rbctf" || level.ex_currentgt == "rbcnq")
				self playLocalSound("ctf_touchenemy");
			else
				self playLocalSound("demotion");

			self thread PromotionImage();
		}
		
		self extreme\_ex_weapons::updateLoadout(false);

		// stop mortars
		self.ex_mortar_strike = false;
		self notify("mortar_over");
		self notify("end_mortar");

		// stop artillery
		self.ex_artillery_strike = false;
		self notify("artillery_over");
		self notify("end_artillery");

		// stop airstrike
		self.ex_air_strike = false;
		self notify("airstrike_over");
		self notify("end_airstike");

		wait 0.5;
	}
}

rankHud()
{
	level endon("ex_gameover");
	self endon("disconnect");

	rankstring = self getRankstring(self.pers["rank"]);
	
	chevron = self getRank_Shader(self.pers["rank"]);
	
	if(!isDefined(self.ex_rankhud1))
	{
		self.ex_rankhud1 = newClientHudElem(self);
		self.ex_rankhud1.x = 10;
		self.ex_rankhud1.y = 474;
		self.ex_rankhud1.alignx = "left";
		self.ex_rankhud1.aligny = "middle";
		self.ex_rankhud1.alpha =0.8;
		self.ex_rankhud1.label = &"RANK_RANK";
		self.ex_rankhud1.fontScale = 1;
	}

	if(isDefined(self.ex_rankhud1)) self.ex_rankhud1 setText(rankstring);
	
	if(!isDefined(self.ex_rankhud2))
	{
		self.ex_rankhud2 = newClientHudElem(self);
		self.ex_rankhud2.x = 120;
		self.ex_rankhud2.y = 420;
		self.ex_rankhud2.alignx = "center";
		self.ex_rankhud2.aligny = "middle";
		self.ex_rankhud2.alpha = 1;
	}
	
	if(isDefined(self.ex_rankhud2))
	{	
		self.ex_rankhud2 setShader(chevron, 37, 37);
		self.ex_rankhud2 scaleOverTime(1, 22, 22);
	}
}

getRank()
{
	self endon("disconnect");

	checkval = 0;

	// set the variables if undefined (special is used for extra points gained from gametype achievements)
	if(!isDefined(self.pers["kill"])) self.pers["kill"] = 0;
	if(!isDefined(self.pers["teamkill"])) self.pers["teamkill"] = 0;
	if(!isDefined(self.pers["suicide"])) self.pers["suicide"] = 0;
	if(!isDefined(self.pers["special"])) self.pers["special"] = 0;
	if(!isDefined(self.pers["conquest"])) self.pers["conquest"] = 0;

	// determine rank using
	if(level.ex_score)
    {
		points = self.score;
		points = self.score + game["rank_"+ self.pers["preset_rank"]];
    }
    else
	{
		points = (self.pers["kill"] + self.pers["special"] + self.pers["conquest"]) - (self.pers["teamkill"] + self.pers["death"]);
		points = points + game["rank_"+ self.pers["preset_rank"]];
    }

	checkval = points;

	if(checkval >= game["rank_7"]) return 7;
	else if(checkval >= game["rank_6"] && checkval < game["rank_7"]) return 6;
	else if(checkval >= game["rank_5"] && checkval < game["rank_6"]) return 5;
	else if(checkval >= game["rank_4"] && checkval < game["rank_5"]) return 4;
	else if(checkval >= game["rank_3"] && checkval < game["rank_4"]) return 3;
	else if(checkval >= game["rank_2"] && checkval < game["rank_3"]) return 2;
	else if(checkval >= game["rank_1"] && checkval < game["rank_2"]) return 1;
	else return 0;
}

PromotionImage()
{
	self endon("disconnect");

	chevron = getRank_Shader(self.pers["rank"]);

	if(!isDefined(self.ex_rankhud0))
	{
		self.ex_rankhud0 = newClientHudElem(self);
		self.ex_rankhud0.x = 320;
		self.ex_rankhud0.y = 130;
		self.ex_rankhud0.alignx = "center";
		self.ex_rankhud0.aligny = "middle";
		self.ex_rankhud0.alpha =0.8;
	}

	if(isDefined(self.ex_rankhud0)) self.ex_rankhud0 setShader(chevron, 50,50);
	if(isDefined(self.ex_rankhud0)) self.ex_rankhud0 scaleOverTime(1, 30, 30);
	wait 3;
	if(isDefined(self.ex_rankhud0)) self.ex_rankhud0 fadeOverTime(1, 0);
	if(isDefined(self.ex_rankhud0)) self.ex_rankhud0.alpha = 0;
	wait 1;
	if(isDefined(self.ex_rankhud0)) self.ex_rankhud0 destroy();
}

getRankstring(rank)
{
	self endon("disconnect");

	if(self.pers["team"] == "allies")
	{				
		switch(game["allies"])
		{
			case "american":
			{
				switch(rank)
				{
					case 7:
					rank = &"RANK_AMERICAN_7"; // General
					break;

					case 6:
					rank = &"RANK_AMERICAN_6"; // Colonel
					break;				

					case 5:
					rank = &"RANK_AMERICAN_5"; // Major
					break;

					case 4:
					rank = &"RANK_AMERICAN_4"; // Captain
					break;

					case 3:
					rank = &"RANK_AMERICAN_3"; // Lieutenant
					break;

					case 2:
					rank = &"RANK_AMERICAN_2"; // Sergeant
					break;

					case 1:
					rank = &"RANK_AMERICAN_1"; // Corporal
					break;

					case 0:
					rank = &"RANK_AMERICAN_0"; // Private
					break;
				}
				break;
			}	
			
			case "british":
			{
				switch(rank)
				{
					case 7:
					rank = &"RANK_BRITISH_7"; // General
					break;

					case 6:
					rank = &"RANK_BRITISH_6"; // Colonel
					break;

					case 5:
					rank = &"RANK_BRITISH_5"; // Major
					break;

					case 4:
					rank = &"RANK_BRITISH_4"; // Captain
					break;

					case 3:
					rank = &"RANK_BRITISH_3"; // Lieutenant
					break;

					case 2:
					rank = &"RANK_BRITISH_2"; // Sergeant
					break;

					case 1:
					rank = &"RANK_BRITISH_1"; // Corporal
					break;

					case 0:
					rank = &"RANK_BRITISH_0"; // Private
					break;
				}
				break;
			}
			
			case "russian":
			{
				switch(rank)
				{
					case 7:
					rank = &"RANK_RUSSIAN_7"; // General-Poruchik
					break;

					case 6:
					rank = &"RANK_RUSSIAN_6"; // Polkovnik
					break;

					case 5:
					rank = &"RANK_RUSSIAN_5"; // Mayor
					break;

					case 4:
					rank = &"RANK_RUSSIAN_4"; // Kapitan
					break;

					case 3:
					rank = &"RANK_RUSSIAN_3"; // Leytenant
					break;

					case 2:
					rank = &"RANK_RUSSIAN_2"; // Podpraporshchik
					break;

					case 1:
					rank = &"RANK_RUSSIAN_1"; // Kapral
					break;

					case 0:
					rank = &"RANK_RUSSIAN_0"; // Soldat
					break;
				}
				break;
			}
		}
	}
	else if (self.pers["team"] == "axis")
	{
		switch(game["axis"])
		{
			case "german":
			{
				switch(rank)
				{
					case 7:
					rank = &"RANK_GERMAN_7"; // General
					break;

					case 6:
					rank = &"RANK_GERMAN_6"; // Oberst
					break;

					case 5:
					rank = &"RANK_GERMAN_5"; // Major
					break;

					case 4:
					rank = &"RANK_GERMAN_4"; // Hauptmann
					break;

					case 3:
					rank = &"RANK_GERMAN_3"; // Leutnant
					break;

					case 2:
					rank = &"RANK_GERMAN_2"; // Unterfeldwebel
					break;

					case 1:
					rank = &"RANK_GERMAN_1"; // Unteroffizier
					break;

					case 0:
					rank = &"RANK_GERMAN_0"; // Grenadier
					break;
				}
				break;
			}
		}
	}
	
	return rank;
}

get_Current_Rank()
{
	if(!isdefined(self.pers["rank"]) || !isdefined(self.pers["team"]) || self.pers["team"] == "spectator") return "";

	chevron = self getRank_Shader(self.pers["rank"]);
	return chevron;
}

getRank_Shader(rank)
{
	self endon("disconnect");

	switch(rank)
	{
		case 7:
		chevron = game["rank_gen_a"];
		break;

		case 6:
		chevron = game["rank_colon_a"];
		break;

		case 5:
		chevron = game["rank_major_a"];
		break;

		case 4:
		chevron = game["rank_capt_a"];
		break;

		case 3:
		chevron = game["rank_lieut_a"];
		break;

		case 2:
		chevron = game["rank_serg_a"];
		break;

		case 1:
		chevron = game["rank_corp_a"];
		break;

		default:
		chevron = game["rank_priv_a"];
		break;
	}

	return chevron;
}

get_Current_Headicon()
{
	if(!isdefined(self.pers["rank"]) || !isdefined(self.pers["team"]) || self.pers["team"] == "spectator") return "";

	chevron = self get_Headicon(self.pers["rank"]);
	return chevron;
}

get_Headicon(rank)
{
	self endon("disconnect");

	switch(rank)
	{
		case 7:
		chevron = game["headicon_general_a"];
		break;

		case 6:
		chevron = game["headicon_colonel_a"];
		break;

		case 5:
		chevron = game["headicon_major_a"];
		break;

		case 4:
		chevron = game["headicon_captain_a"];
		break;

		case 3:
		chevron = game["headicon_lieutenant_a"];
		break;

		case 2:
		chevron = game["headicon_sergeant_a"];
		break;

		case 1:
		chevron = game["headicon_corporal_a"];
		break;

		default:
		chevron = game["headicon_private_a"];
		break;
	}

	return chevron;
}
