#include extreme\_ex_drm;

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

  if(level.ex_currentgt == "hm") return;

  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.ex_ranksystem)
    {
      self.statusicon = self thread extreme\_ex_ranksystem::get_Current_Rank();
      self.headicon = self thread extreme\_ex_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
  {
    self.headicon = "";
    self.headiconteam = "";
  }
}

updateFriendIconSettings()
{
  drawfriend = drm_getCvarInt("scr_drawfriend");
  ranksystem = drm_getCvarInt("ex_ranksystem");

  if(level.drawfriend != drawfriend || level.ex_ranksystem != ranksystem)
  {
    level.drawfriend = drawfriend;
    level.ex_ranksystem = ranksystem;
    updateFriendIcons();
  }
}

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