#include ww2x\_drm;

init()
{
	// Draws a team icon over teammates
	if(drm_getCvar("scr_drawfriend") == "")
		setCvar("scr_drawfriend", "1");
	level.drawfriend = drm_getCvarInt("scr_drawfriend");
	
	level.wwx_ranksystem = getCvarInt("wwx_ranksystem");	

	switch(game["allies"])
	{
	case "american":
		game["headicon_allies"] = "headicon_american";
		precacheHeadIcon(game["headicon_allies"]);
		break;
		
	case "british":
		game["headicon_allies"] = "headicon_british";
		precacheHeadIcon(game["headicon_allies"]);
		break;
		
	case "russian":
		game["headicon_allies"] = "headicon_russian";
		precacheHeadIcon(game["headicon_allies"]);
		break;
	}
	
	assert(game["axis"] == "german");
	game["headicon_axis"] = "headicon_german";
	precacheHeadIcon(game["headicon_axis"]);

	level thread onPlayerConnect();
	
	for(;;)
	{
		updateFriendIconSettings();
		wait 5;
	}
}

onPlayerConnect()
{
	for(;;)
	{
		level waittill("connecting", player);

		player thread onPlayerSpawned();
		player thread onPlayerKilled();
	}
}

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

onPlayerKilled()
{
	self endon("disconnect");
	
	for(;;)
	{
		self waittill("killed_player");
		self.headicon = "";
	}
}	

showFriendIcon()
{	
	if(level.drawfriend)
	{
		if(level.wwx_ranksystem)
		{
			
			self.headicon = ww2x\_wwx_ranksystem::get_Current_Headicon();
			self.headiconteam = self.pers["team"];
		}
		else
		{
			if(self.pers["team"] == "allies")
			{
				self.headicon = game["headicon_allies"];
				self.headiconteam = "allies";
			}
			else
			{
				self.headicon = game["headicon_axis"];
				self.headiconteam = "axis";
			}
		}
	}
	else if(level.wwx_ranksystem)
	{
		self.headicon = ww2x\_wwx_ranksystem::get_Current_Headicon();
	}
}
	
updateFriendIconSettings()
{
	drawfriend = drm_getCvarFloat("scr_drawfriend");
	ranksystem = drm_getCvarInt("wwx_ranksystem");
	
	if(level.drawfriend != drawfriend || level.wwx_ranksystem != ranksystem)
	{
		level.drawfriend = drawfriend;
		
		level.wwx_ranksystem = ranksystem;

		updateFriendIcons();
	}
}

updateFriendIcons()
{
	if(level.wwx_ranksystem)
	{
		// for all living players, show the appropriate headicon
		players = getentarray("player", "classname");
		for(i = 0; i < players.size; i++)
		{
			player = players[i];

			if(isDefined(player.pers["team"]) && player.pers["team"] != "spectator" && player.sessionstate == "playing")
			{
//				player.pers["newrank"] = player thread ww2x\_wwx_ranksystem::getRank();	

				player.statusicon = player thread ww2x\_wwx_ranksystem::get_Current_Headicon();
				if ( level.drawfriend )
				{
					player.headicon = ww2x\_wwx_ranksystem::get_Current_Headicon();
					player.headiconteam = player.pers["team"];
				}
				else
				{
					player.headicon = "";
				}
			}

		}
	}
	else if(level.drawfriend)
	{
		// for all living players, show the appropriate headicon
		players = getentarray("player", "classname");
		for(i = 0; i < players.size; i++)
		{
			player = players[i];
	
			if(isDefined(player.pers["team"]) && player.pers["team"] != "spectator" && player.sessionstate == "playing")
			{
				if(level.drawfriend)
				{
					if(level.wwx_ranksystem)
					{
						player.headicon = ww2x\_wwx_ranksystem::get_Current_Headicon();
					}
					else
					{
						if(player.pers["team"] == "allies")
						{
							player.headicon = game["headicon_allies"];
							player.headiconteam = "allies";
						}
						else
						{
							player.headicon = game["headicon_axis"];
							player.headiconteam = "axis";
						}
					}
				}
				else
				{
					players = getentarray("player", "classname");
					for(i = 0; i < players.size; i++)
					{
						player = players[i];
		
						if(isDefined(player.pers["team"]) && player.pers["team"] != "spectator" && player.sessionstate == "playing")
							player.headicon = "";
					}
				}
				
			}
		}
	}
}
