
getgtstring(gt)
{
  switch(gt)
  {
    case "chq":
      gtstring = &"MPUI_CLASSIC_HEADQUARTERS";
      break;
    case "cnq":
      gtstring = &"MPUI_CONQUEST";
      break;
    case "ctf":
      gtstring = &"MPUI_CAPTURE_THE_FLAG";
      break;
    case "ctfb":
      gtstring = &"MPUI_CAPTURE_THE_FLAG_BACK";
      break;
    case "dm":
      gtstring = &"MPUI_DEATHMATCH";
      break;
    case "dom":
      gtstring = &"MPUI_DOMINATION";
      break;
    case "esd":
      gtstring = &"MPUI_ENHANCED_SD";
      break;
    //case "ft":
      //gtstring = &"MPUI_FREEZETAG";
      //break;
    case "hm":
      gtstring = &"MPUI_HITMAN";
      break;
    case "hq":
      gtstring = &"MPUI_HEADQUARTERS";
      break;
    case "htf":
      gtstring = &"MPUI_HOLD_THE_FLAG";
      break;
    case "ihtf":
      gtstring = &"MPUI_I_HOLD_THE_FLAG";
      break;
    case "lib":
      gtstring = &"MPUI_LIBERATION";
      break;
    case "lms":
      gtstring = &"MPUI_LAST_MAN_STANDING";
      break;
    case "lts":
      gtstring = &"MPUI_LAST_TEAM_STANDING";
      break;
    case "ons":
      gtstring = &"MPUI_ONSLAUGHT";
      break;
    case "rbcnq":
      gtstring = &"MPUI_ROUNDBASED_CNQ";
      break;
    case "rbctf":
      gtstring = &"MPUI_ROUNDBASED_CTF";
      break;
    case "sd":
      gtstring = &"MPUI_SEARCH_AND_DESTROY";
      break;
    case "tdm":
      gtstring = &"MPUI_TEAM_DEATHMATCH";
      break;
    case "vip":
      gtstring = &"MPUI_VERY_IMPORTANT_PERSON";
      break;
    default:
      gtstring = gt;
      break;
  }

  return gtstring;
}

getgtstringshort(gt)
{
  switch(gt)
  {
    case "chq":
      gtstring = &"MPUI_CHQ";
      break;
    case "cnq":
      gtstring = &"MPUI_CNQ";
      break;
    case "ctf":
      gtstring = &"MPUI_CTF";
      break;
    case "ctfb":
      gtstring = &"MPUI_CTFB";
      break;
    case "dm":
      gtstring = &"MPUI_DM";
      break;
    case "dom":
      gtstring = &"MPUI_DOM";
      break;
    case "esd":
      gtstring = &"MPUI_ESD";
      break;
    //case "ft":
      //gtstring = &"MPUI_FT";
      //break;
    case "hm":
      gtstring = &"MPUI_HM";
      break;
    case "hq":
      gtstring = &"MPUI_HQ";
      break;
    case "htf":
      gtstring = &"MPUI_HTF";
      break;
    case "ihtf":
      gtstring = &"MPUI_IHTF";
      break;
    case "lib":
      gtstring = &"MPUI_LIB";
      break;
    case "lms":
      gtstring = &"MPUI_LMS";
      break;
    case "lts":
      gtstring = &"MPUI_LTS";
      break;
    case "ons":
      gtstring = &"MPUI_ONS";
      break;
    case "rbcnq":
      gtstring = &"MPUI_RBCNQ";
      break;
    case "rbctf":
      gtstring = &"MPUI_RBCTF";
      break;
    case "sd":
      gtstring = &"MPUI_SD";
      break;
    case "tdm":
      gtstring = &"MPUI_TDM";
      break;
    case "vip":
      gtstring = &"MPUI_VIP";
      break;
    default:
      gtstring = gt;
      break;
  }

  return gtstring;
}

getmapstring(map)
{
  level.msc = false;
	if(!IsCustomMap(map)) level.msc = true;

	mapstring = level.ex_maps[0].loclname;
  
	for(i = 0; i < level.ex_maps.size; i++)
	{
		if(level.ex_maps[i].mapname == map)
		mapstring = level.ex_maps[i].loclname;
	}
	return mapstring;
}

StringToArray(pString)
{
  thread extreme\_ex_logfile::Log("StringToArray");

  //Convert a string to an array of words
  j=0;
  temparr[j] = "";  

  for(i=0;i<pString.size;i++)
  {
    if(pString[i] == " ")
    {
      j++;
    }
    else
    {
      if (!isdefined(temparr[j]))
        temparr[j] = "";
        
      temparr[j] += pString[i];
    }
  }
  return temparr;
}

trim(s)
{
  thread extreme\_ex_logfile::Log("trim");

  if( (s=="") )
    return "";

  s2="";
  s3="";

  i=0;
  while( (i<s.size) && (s[i]==" ") )
    i++;

  // String is just blanks?
  if(i==s.size)
    return "";
  
  for(;i<s.size;i++)
  {
    s2 += s[i];
  }

  i=s2.size-1;
  while(s2[i]==" " && i>0)
    i--;

  for(j=0;j<=i;j++)
  {
    s3 += s2[j];
  }
    
  return s3;
}

DisplayMapRotation()
{
  thread extreme\_ex_logfile::Log("DisplayMapRotation");

  level.ex_pausesvrmsg = true;

  endmsg = &"MAPROTATION_NORMAL";

  if(level.ex_randommaprotation) endmsg = &"MAPROTATION_RANDOM";
  if(level.ex_pbrotate) endmsg = &"MAPROTATION_PLAYER";
  if(level.ex_mapvote) endmsg = &"MAPROTATION_VOTING";

  iprintln(&"CUSTOM_SERVER_NAME", endmsg);
  wait 2;

  if(level.ex_mapvote)
  {
    level.ex_pausesvrmsg = false;
    return;
  }

  if(!isdefined(level.MapRotation)) GetMapRotation();
    
  sGametype = getgtstring(level.MapRotation[0]["gametype"]);  
  sMapName = getmapstring(level.MapRotation[0]["map"]); 
  bCustomMap = IsCustomMap(level.MapRotation[0]["map"]);
        
  if(level.ex_svrmsg_info >= 1)
  {
    iprintln(&"MAPROTATION_NEXT_MAP", sMapName);
    wait 2;
  }
  
  if(level.ex_svrmsg_info >= 2)
  {
    if(bCustomMap)
    {
      //display a warning that the next map is a custom map
      iprintln(&"MAPROTATION_CUSTOM_NEXT");
      wait 2;
    }
  
    iprintln(&"MAPROTATION_NEXT_GT", sGametype);
    wait 2;
  }

  // if no map rotation display, end here!
  if(!level.ex_svrmsg_rotation)
  {
    level.ex_pausesvrmsg = false;
    return;
  }

  iprintln(&"MAPROTATION_TITLE");
  wait 2;
  
  for( i = 0; i < level.MapRotation.size; i++)
  {
    sG = getgtstring(level.MapRotation[i]["gametype"]); 
    sMN = getmapstring(level.MapRotation[i]["map"]);  
    bCM = IsCustomMap(level.MapRotation[i]["map"]); 
            
    if (bCM)
      MapColour = &"COLOUR_GREEN";
    else
      MapColour = &"COLOUR_WHITE";

    wait 1;
    iprintln(sG, &"MISC_BLANK_LINE_TXT", MapColour, sMN, &"COLOUR_WHITE");
    wait 1;
  }

  // start up server message again
  level.ex_pausesvrmsg = false;
}

GetMapRotation(DontUseCurrent)
{
  thread extreme\_ex_logfile::Log("GetMapRotation");

  maprotcur = trim(getcvar("sv_maprotationcurrent"));  

  if ( maprotcur == "" )
    return;
  
  //convert to array of strings
  maprotarray = StringToArray(maprotcur);
  
  //now go through array of words getting gametypes and maps
  sGametype = getcvar("g_gametype");
  arraypos = 0;
  mapnum = 0;
  
  while (arraypos < maprotarray.size)
  {
    if ( maprotarray[arraypos] == "gametype" )
    {
      //found in array string "gametype type map mapname"
      sGametype = maprotarray[arraypos + 1];
      level.MapRotation[mapnum]["gametype"] = maprotarray[arraypos + 1];
      level.MapRotation[mapnum]["map"] = maprotarray[arraypos + 3];
      arraypos = arraypos + 4;
    }
    else
    {
      //no gametype so presumably just a map "map mapname"
      level.MapRotation[mapnum]["gametype"] = sGametype;
      level.MapRotation[mapnum]["map"] = maprotarray[arraypos + 1];
      arraypos = arraypos + 2;      
    }
    
    mapnum++;
  
  }
  
  if(!isdefined(DontUseCurrent))
  {
    //now add the current map as it won't be on there!
    level.MapRotation[mapnum]["gametype"] = level.ex_currentgt;
    level.MapRotation[mapnum]["map"] = level.ex_currentmap;
  }
  
  return;
  
}

IsCustomMap(map)
{
  thread extreme\_ex_logfile::Log("IsCustomMap");

  switch(map)
  {
    case "mp_breakout":
    case "mp_brecourt":
    case "mp_burgundy":
    case "mp_carentan":
    case "mp_dawnville":
    case "mp_decoy":
    case "mp_downtown":
    case "mp_harbor":
    case "mp_farmhouse":
    case "mp_leningrad":
    case "mp_matmata":
    case "mp_railyard":
    case "mp_toujane":
    case "mp_trainstation":
    case "mp_rhine":
      return false;
      
    default:
      return true;
  }
}
