#include maps\mp\gametypes\_weapons;

wwxPlayerConnect()
{
		
	// check security status for player
	self ww2x\_wwx_security::checkInit(false);
	self ww2x\_wwx_security::set_ignore_player();
	
       if(level.wwx_forcerate != 0) self setClientCvar("rate", level.wwx_forcerate);

	// if roundbased, no need to display the connecting information if they've already been playing
	if(level.wwx_roundbased && isDefined(self.pers) && isDefined(self.pers["score"])) return;

	// if using the gtsDelay system, no need to display the connecting information if they've already been playing
	if(level.wwx_gtsdelay && !level.wwx_roundbased && isDefined(game["gtsdone"]) && isDefined(self.pers["team"])) return;

	if(isDefined(self.wwx_name))
	{
		if(level.wwx_clano[self.wwx_clid])
		{
                  if(level.wwx_linux_server >= 1)
                  {
			iprintln(&"CLIENTCONTROL_CONNECTING", self.name);
			splay = true;
                  }
                  else
                  {
			iprintln(&"CLIENTCONTROL_CONNECTING", self);
			splay = true;
                  }
		}
		else splay = false;
	}
	else
	{
            if(level.wwx_linux_server >= 1)
            {
		iprintln(&"CLIENTCONTROL_CONNECTING", self.name);
		splay = true;
            }
            else
            {
		iprintln(&"CLIENTCONTROL_CONNECTING", self);
		splay = true;
            }
	}

	if(level.wwx_plcdsound && splay)
	{
		players = getEntArray("player", "classname");
		for(m=0;m<players.size;m++)
		players[m] playLocalSound("gomplayersjoined");
	}
}

wwxPlayerJoinedServer()
{
	self thread setOneOffVars();

	// start music monitor
	if(level.wwx_specmusic || level.wwx_deathmusic) self thread musicSystem();

	// check to see if the players name has changed since connecting
	self ww2x\_wwx_security::checkInit(true);

	// detect forced auto assign
	if(level.wwx_forceauto) self setClientCvar("ui_allow_select_team", "0");
	else self setClientCvar("ui_allow_select_team", "1");	

	// if roundbased, no need to display the connecting information if they've already been playing
	if(level.wwx_roundbased && isDefined(self.pers["score"])) return;

	// if using the gtsDelay system, no need to display the connecting information if they've already been playing
	if(level.wwx_gtsdelay && !level.wwx_roundbased && isDefined(game["gtsdone"]) && isDefined(self.pers["team"])) return;

	if(isDefined(self.wwx_name))
	{
		if(level.wwx_clano[self.wwx_clid])
            {
                  if(level.wwx_linux_server >= 1)
			iprintln(&"CLIENTCONTROL_HASJOINED", self.name);
                  else
			iprintln(&"CLIENTCONTROL_HASJOINED", self);
            }
	}
	else 
      {
      if(level.wwx_linux_server >= 1)
      iprintln(&"CLIENTCONTROL_HASJOINED", self.name);
      else 
      iprintln(&"CLIENTCONTROL_HASJOINED", self);
      }
}

wwxPrintJoinedTeam(team)
{
	if(!isDefined(self.name)) return;

	level.wwx_clanside = [[level.drm_cvardef]]("wwx_clanteam", "off", "" , "", "string");

	if((level.wwx_clanside == "axis" || level.wwx_clanside == "allies") && isDefined(self.wwx_name) && level.wwx_clano[self.wwx_clid])
	{
		if(team == "allies")
		{
			switch(game["allies"])
			{
				case "american":
					iprintln(&"CLIENTCONTROL_FORCED_JOIN_AMERICAN", self);
					break;
				case "british":
					iprintln(&"CLIENTCONTROL_FORCED_JOIN_BRITISH", self);
					break;
				default:
					iprintln(&"CLIENTCONTROL_FORCED_JOIN_RUSSIAN", self);
					break;
			}
		}
		else if(team == "axis")
		{
			switch(game["axis"])
			{
				case "german":
					iprintln(&"CLIENTCONTROL_FORCED_JOIN_GERMAN", self);
					break;
			}
		}
	}
	else if(isDefined(self.wwx_name) && level.wwx_clano[self.wwx_clid])
	{
		if(team == "allies")
		{
			switch(game["allies"])
			{
				case "american":
				iprintln(&"CLIENTCONTROL_RECRUIT_AMERICAN", self);
					break;
				case "british":
					iprintln(&"CLIENTCONTROL_RECRUIT_BRITISH", self);
					break;
				default:
					iprintln(&"CLIENTCONTROL_RECRUIT_RUSSIAN", self);
					break;
			}
		}
		else if(team == "axis")
		{
			switch(game["axis"])
			{
				case "german":
					iprintln(&"CLIENTCONTROL_RECRUIT_GERMAN", self);
					break;
			}
		}
	}
	else if(!isDefined(self.wwx_name))
	{
		if(team == "allies")
		{
			switch(game["allies"])
			{
				case "american":
				iprintln(&"CLIENTCONTROL_RECRUIT_AMERICAN", self);
					break;
				case "british":
					iprintln(&"CLIENTCONTROL_RECRUIT_BRITISH", self);
					break;
				default:
					iprintln(&"CLIENTCONTROL_RECRUIT_RUSSIAN", self);
					break;
			}
		}
		else if(team == "axis")
		{
			switch(game["axis"])
			{
				case "german":
					iprintln(&"CLIENTCONTROL_RECRUIT_GERMAN", self);
					break;
			}
		}
	}
}

wwxPlayerDisconnect()
{
	if(isDefined(self.wwx_name))
	{
		if(level.wwx_clano[self.wwx_clid])
		{
                  if(level.wwx_linux_server >= 1)
                  {
			iprintln(&"CLIENTCONTROL_DISCONNECTED", self.name);
			splay = true;
                  }
                  else
                  {
			iprintln(&"CLIENTCONTROL_DISCONNECTED", self);
			splay = true;
                  }
		}
		else splay = false;
	}
	else
	{
            if(level.wwx_linux_server >= 1)
            {
		iprintln(&"CLIENTCONTROL_DISCONNECTED", self.name);
		splay = true;
            }
            else
            {
		iprintln(&"CLIENTCONTROL_DISCONNECTED", self);
		splay = true;
            }           
	}

	if(level.wwx_plcdsound && splay)
	{
		players = getEntArray("player", "classname");
		for(m=0;m<players.size;m++)
		players[m] playLocalSound("gomplayersleft");
	}

	if(isDefined(self.wwx_name) && level.wwx_clano[self.wwx_clid]) return;
}

musicSystem()
{
	self endon("disconnect");
	
	//if(!level.wwx_deathmusic || !level.wwx_specmusic)
	//	return;

	self.pers["music_on"] = false;
	self.pers["music_choke"] = true;

	if(!isDefined(self.wwx_oldsessionstate)) self.wwx_oldsessionstate = "";

	for(;;)
	{
		wait 0.05;

		if(self.wwx_oldsessionstate == self.sessionstate) continue;

		if(isPlayer(self) && getSessionState(self.sessionstate) && !self.pers["music_on"] && !self.wwx_sinbin)
		{
			if(!level.wwx_gameover)
			{
				if(level.wwx_deathmusic && self.sessionstate == "dead")
				{
					self.pers["music_choke"] = false;
					self.pers["music_on"] = true;
					self notify("end_choke");
					self playLocalSound("death_music");
				}
				else if(level.wwx_specmusic && self.sessionstate != "dead")
				{
					self.pers["music_choke"] = false;
					self.pers["music_on"] = true;
					self notify("end_choke");
					self playLocalSound("death_music");
					self playLocalSound("spec_music");
				}
			}
		}
		else if(isPlayer(self) && !getSessionState(self.sessionstate) && self.pers["music_on"] && !self.pers["music_choke"] || (level.wwx_gameover || self.wwx_sinbin))
		{
			self.pers["music_choke"] = true;
			self.pers["music_on"] = false;
			self thread chokeMusicChannel();
			
			if(!level.wwx_gameover && !self.wwx_sinbin)
			{
				if(level.wwx_specmusic || level.wwx_deathmusic)
				{
					wait 0.05;
					self playLocalSound("spec_music_stop");
				}
			}
		}

		self.wwx_oldsessionstate = self.sessionstate;	
	}
}

chokeMusicChannel()
{
	self endon("disconnect");
	self endon("end_choke");
	
	for(;;)
	{
		if(isPlayer(self) && self.pers["music_choke"]) self playLocalSound("spec_music_null");
		wait 10;
	}
}

getSessionState(ss)
{
	switch(ss)
	{
		case "spectator":
		case "intermission":
		case "dead":
		return true;
		
		default:
		return false;
	}
}

setOneOffVars()
{
	self.wwx_sinbin = false;
	self.wwx_glplay = undefined;
}

menuAutoAssign()
{
	numonteam["allies"] = 0;
	numonteam["axis"] = 0;

	players = getentarray("player", "classname");
	for(i = 0; i < players.size; i++)
	{
		player = players[i];
		if(!isDefined(player.pers["team"]) || player.pers["team"] == "spectator" || player == self) continue;
		numonteam[player.pers["team"]]++;
	}

	// if teams are equal return the team with the lowest score
	if(numonteam["allies"] == numonteam["axis"])
	{
		if(getTeamScore("allies") == getTeamScore("axis"))
		{
			teams[0] = "allies";
			teams[1] = "axis";
			assignment = teams[randomInt(2)];
		}
		else if(getTeamScore("allies") < getTeamScore("axis")) assignment = "allies";
		else assignment = "axis";
	}
	else if(numonteam["allies"] < numonteam["axis"]) assignment = "allies";
	else assignment = "axis";

	if(assignment == self.pers["team"] && (self.sessionstate == "playing" || self.sessionstate == "dead"))
	{
		if(!isDefined(self.pers["weapon"]))
	    {
			if(self.pers["team"] == "allies") self openMenu(game["menu_weapon_allies"]);
			else self openMenu(game["menu_weapon_axis"]);
	    }

		return;
	}

	if(assignment != self.pers["team"] && (self.sessionstate == "playing" || self.sessionstate == "dead"))
	{
		self.switching_teams = true;
		self.joining_team = assignment;
		self.leaving_team = self.pers["team"];
		self suicide();
	}

	self.pers["team"] = assignment;
	self.pers["savedmodel"] = undefined;

	// create the WW2X weapon array
	self ww2x\_wwx_weapons::setWeaponArray();

	// clear game weapon arrarys
	self clearWeapons();
	
	self setClientCvar("ui_allow_weaponchange", "1");
	
	self updateAllAllowedSingleClient();

	if(level.wwx_gameover)
	{
		menuSpectator();
		return;
	}
	else if(self.pers["team"] == "allies")
	{	
		self openMenu(game["menu_weapon_allies"]);
		self setClientCvar("g_scriptMainMenu", game["menu_weapon_allies"]);
	}
	else
	{	
		self openMenu(game["menu_weapon_axis"]);
		self setClientCvar("g_scriptMainMenu", game["menu_weapon_axis"]);
	}

	self notify("joined_team");

	if(!level.wwx_roundbased) self notify("end_respawn");
	else self.pers["spawnweapon"] = undefined;
}

menuAutoAssignDM()
{
	if(self.pers["team"] != "allies" && self.pers["team"] != "axis")
	{
		if(self.sessionstate == "playing")
		{
			self.switching_teams = true;
			self suicide();
		}

		teams[0] = "allies";
		teams[1] = "axis";
		self.pers["team"] = teams[randomInt(2)];
		self.pers["savedmodel"] = undefined;

		// create the WW2X weapon array
		self ww2x\_wwx_weapons::setWeaponArray();

		// clear game weapon arrarys
		self clearWeapons();

		self setClientCvar("ui_allow_weaponchange", "1");

		if(self.pers["team"] == "allies") self setClientCvar("g_scriptMainMenu", game["menu_weapon_allies"]);
		else self setClientCvar("g_scriptMainMenu", game["menu_weapon_axis"]);

		self notify("joined_team");
		self notify("end_respawn");
	}

	if(level.wwx_gameover)
	{
		menuSpectator();
		return;
	}
	else if(!isDefined(self.pers["weapon"]))
	{
		if(self.pers["team"] == "allies") self openMenu(game["menu_weapon_allies"]);
		else self openMenu(game["menu_weapon_axis"]);
	}
}

menuAllies()
{
	if(self.pers["team"] != "allies")
	{
		if(self.sessionstate == "playing")
		{
			if(level.wwx_currentgt != "dm" || level.wwx_currentgt != "lms")
			{
				self.joining_team = "allies";
				self.leaving_team = self.pers["team"];
			}

			self updateAllAllowedSingleClient();
			self.switching_teams = true;
			self suicide();
		}

		if(level.wwx_wepo_secondary)
		{
			self updateAllAllowedSingleClient();
			if(level.wwx_roundbased) self.pers["first_time"] = true;
		}
		else
			self updateAllAllowedSingleClient();

		self.pers["team"] = "allies";
		self.pers["savedmodel"] = undefined;

		// create the WW2X weapon array
		self ww2x\_wwx_weapons::setWeaponArray();

		// clear game weapon arrarys
		self clearWeapons();

		self setClientCvar("ui_allow_weaponchange", "1");
		self setClientCvar("g_scriptMainMenu", game["menu_weapon_allies"]);

		// allow team change option on weapons menu if not deathmatch
		if(level.wwx_currentgt == "dm" || level.wwx_currentgt == "lms" || level.wwx_forceauto) self setClientCvar("ui_allow_teamchange", 0);
		else self setClientCvar("ui_allow_teamchange", 1);

		self notify("joined_team");

		if(!level.wwx_roundbased) self notify("end_respawn");
		else self.pers["spawnweapon"] = undefined;
	}

	if(level.wwx_gameover)
	{
		menuSpectator();
		return;
	}
	else if(!isDefined(self.pers["weapon"])) self openMenu(game["menu_weapon_allies"]);
}

menuAxis()
{
	if(self.pers["team"] != "axis")
	{
		if(self.sessionstate == "playing")
		{
			if(level.wwx_currentgt != "dm" || level.wwx_currentgt != "lms")
			{
				self.joining_team = "axis";
				self.leaving_team = self.pers["team"];
			}

			self updateAllAllowedSingleClient();
			self.switching_teams = true;
			self suicide();
		}

		if(level.wwx_wepo_secondary)
		{
			self updateAllAllowedSingleClient();
			if(level.wwx_roundbased) self.pers["first_time"] = true;
		}
		else
			self updateAllAllowedSingleClient();

		self.pers["team"] = "axis";
		self.pers["savedmodel"] = undefined;

		// create the WW2X weapon array
		self ww2x\_wwx_weapons::setWeaponArray();

		// clear game weapon arrarys
		self clearWeapons();

		self setClientCvar("ui_allow_weaponchange", "1");
		self setClientCvar("g_scriptMainMenu", game["menu_weapon_axis"]);

		// allow team change option on weapons menu if not deathmatch
		if(level.wwx_currentgt == "dm" || level.wwx_currentgt == "lms" || level.wwx_forceauto) self setClientCvar("ui_allow_teamchange", 0);
		else self setClientCvar("ui_allow_teamchange", 1);

		self notify("joined_team");

		if(!level.wwx_roundbased) self notify("end_respawn");
		else self.pers["spawnweapon"] = undefined;
	}

	if(level.wwx_gameover)
	{
		menuSpectator();
		return;
	}
	else if(!isDefined(self.pers["weapon"])) self openMenu(game["menu_weapon_axis"]);
}

menuSpectator()
{
	if(self.pers["team"] != "spectator")
	{
		if(isAlive(self))
		{
			if(level.wwx_currentgt != "dm" || level.wwx_currentgt != "lms")
			{
				self.joining_team = "spectator";
				self.leaving_team = self.pers["team"];
			}

			self updateAllAllowedSingleClient();	
			self.switching_teams = true;
			self suicide();
			
			if(level.wwx_ranksystem)
			{
				if(isdefined(self.wwx_rankhud1)) self.wwx_rankhud1 destroy();
				if(isdefined(self.wwx_rankhud2)) self.wwx_rankhud2 destroy();
				if(isdefined(self.wwx_rankhud0)) self.wwx_rankhud0 destroy();
			}
		}

		if(level.wwx_wepo_secondary)
		{
			self updateAllAllowedSingleClient();
			if(level.wwx_roundbased) self.pers["first_time"] = true;
		}
		else
			self updateAllAllowedSingleClient();

		// create the WW2X weapon array
		self ww2x\_wwx_weapons::setWeaponArray();

		// clear game weapon arrarys
		self clearWeapons();

		if(level.wwx_roundbased) self.pers["spawnweapon"] = undefined;

		self.sessionteam = "spectator";
		self setClientCvar("ui_allow_weaponchange", "0");

		if(level.wwx_currentgt == "hq") self thread maps\mp\gametypes\hq::updateTimer();

		ww2x\_wwx_spawn::spawnspectator();
		
		if(level.wwx_currentgt == "dom")
		{	
			thread maps\mp\gametypes\dom::CheckTeams();
		}

		else if(level.wwx_currentgt == "ons")
		{	
			thread maps\mp\gametypes\ons::CheckTeams();
		}

		self setClientCvar("g_scriptMainMenu", game["menu_ingame"]);

		self notify("joined_spectators");
	}
}

menuWeapon(response)
{
	if(!isDefined(self.pers["team"]) || (self.pers["team"] != "allies" && self.pers["team"] != "axis")) return;

	weapon = self restrictWeaponByServerCvars(response);

	if(weapon == "restricted")
	{
		if(self.pers["team"] == "allies") self openMenu(game["menu_weapon_allies"]);
		else if(self.pers["team"] == "axis") self openMenu(game["menu_weapon_axis"]);

		return;
	}

	self setClientCvar("g_scriptMainMenu", game["menu_ingame"]);

	if(level.wwx_wepo_secondary)
	{
		goback = false;
		
		if(isDefined(self.pers["weapon2"]) && self.pers["weapon2"] == response && !isDefined(self.pers["weapon2_new"])) goback = true;
		else if(isDefined(self.pers["weapon2_new"]) && self.pers["weapon2_new"] == response) goback = true;
			
		if(goback)
		{
			if(self.pers["team"] == "allies") self openMenu(game["menu_weapon_allies"]);
			else if(self.pers["team"] == "axis") self openMenu(game["menu_weapon_axis"]);
	
			return;
		}
	}
	else	if(isDefined(self.pers["weapon"]) && self.pers["weapon"] == weapon) return;

	if(!isDefined(self.pers["weapon"]))
	{
		self.pers["weapon"] = weapon;
		self.pers["weapon1"] = self.pers["weapon"];

		if(!level.wwx_wepo_secondary)
		{
			if(level.wwx_currentgt == "hq")
			{
				if(isDefined(self.WaitingOnTimer) || ((self.pers["team"] == level.DefendingRadioTeam) && isDefined(self.WaitingOnNeutralize)))
				{
					self thread maps\mp\gametypes\hq::respawn();
					self thread maps\mp\gametypes\hq::updateTimer();
				}
				else [[level.spawnplayer]]();
			}
			else if(isDefined(self.WaitingToSpawn) && (level.wwx_currentgt == "htf" || level.wwx_currentgt == "ctf" || level.wwx_currentgt == "ctfb" || level.wwx_currentgt == "dom" || level.wwx_currentgt == "cnq" || level.wwx_currentgt == "vip" || level.wwx_currentgt == "ihtf" || level.wwx_currentgt == "ons" || level.wwx_currentgt == "esd"))
			{
				if(level.wwx_currentgt == "htf")
				{
					self thread maps\mp\gametypes\htf::respawn();
					self thread maps\mp\gametypes\htf::updateTimer();
				}
				else if(level.wwx_currentgt == "ihtf")
				{
					self thread maps\mp\gametypes\ihtf::respawn();
					self thread maps\mp\gametypes\ihtf::updateTimer();
				} 
				else if(level.wwx_currentgt == "ctfb")
				{
					self thread maps\mp\gametypes\ctfb::respawn();
					self thread maps\mp\gametypes\ctfb::updateTimer();
				} 
				else if(level.wwx_currentgt == "dom")
				{
					self thread maps\mp\gametypes\dom::respawn();
					self thread maps\mp\gametypes\dom::updateTimer();
				}
				else if(level.wwx_currentgt == "cnq")
				{
					self thread maps\mp\gametypes\cnq::respawn();
					self thread maps\mp\gametypes\cnq::updateTimer();
				}
				else if(level.wwx_currentgt == "vip")
				{
					self thread maps\mp\gametypes\vip::respawn();
					self thread maps\mp\gametypes\vip::updateTimer();
				} 
	                  else if(level.wwx_currentgt == "ons")
				{
					self thread maps\mp\gametypes\ons::respawn();
					self thread maps\mp\gametypes\ons::updateTimer();
				} 
	                  else if(level.wwx_currentgt == "esd")
				{
					self thread maps\mp\gametypes\esd::respawn();
					self thread maps\mp\gametypes\esd::updateTimer();
				}
				else
				{
					self thread maps\mp\gametypes\ctf::respawn();
					self thread maps\mp\gametypes\ctf::updateTimer();
				}
			}
			else [[level.spawnplayer]]();
			
			if(level.wwx_currentgt == "dom")
			{	
				thread maps\mp\gametypes\dom::CheckTeams();
			}

			else if(level.wwx_currentgt == "ons")
			{	
				thread maps\mp\gametypes\ons::CheckTeams();
			}
			
		}
		else if(level.wwx_wepo_secondary)
		{
			if(self.pers["team"] == "allies") self openMenu(game["menu_weapon_allies_sec"]);
			else if(self.pers["team"] == "axis") self openMenu(game["menu_weapon_axis_sec"]);

			self updateDisabledSingleClient(self.pers["weapon"]);
			return;
		}
		else
		{
			[[level.spawnplayer]]();
			self ww2x\_wwx_clientcontrol::wwxPrintJoinedTeam(self.pers["team"]);
		}
	}
	else
	{
		if(level.wwx_wepo_secondary)
		{
			self.pers["weapon"] = weapon;
			self.pers["weapon1"] = weapon;
			self.pers["weapon1_new"] = response;
			weaponname = getWeaponName(self.pers["weapon1"]);
		}
		else
		{
			self.pers["weapon"] = weapon;
			weaponname = getWeaponName(self.pers["weapon"]);
		}

		if(useAn(self.pers["weapon"])) self iprintln(&"MP_YOU_WILL_RESPAWN_WITH_AN", weaponname);
		else self iprintln(&"MP_YOU_WILL_RESPAWN_WITH_A", weaponname);
	}

	self thread maps\mp\gametypes\_spectating::setSpectatePermissions();
}

menuSecWeapon(response)
{
	self endon("wwx_dead");
	self endon("disconnect");

	self.pers["weapon1"] = self.pers["weapon"];
	game[self.name]["weapon1"] =  self.pers["weapon1"];

	if(level.wwx_roundbased)
	{
		self.pers["weapon1_new"] = self.pers["weapon1"];

		if(self.pers["first_time"])
		{
			self updateDisabledSingleClient(self.pers["weapon"]);
	
			if(self.pers["team"] == "allies") self openMenu(game["menu_weapon_allies_sec"]);
			else if(self.pers["team"] == "axis") self openMenu(game["menu_weapon_axis_sec"]);

			for(;;)
			{
				self waittill("menuresponse", menu, response);
				
				if(response == "menuopen" || response == "menuclose") continue;
				else break;
			}

			self closeMenu();
			self closeInGameMenu();
			self setClientCvar("g_scriptMainMenu", game["menu_ingame"]);
		}
	}

	weapon = self restrictWeaponByServerCvars(response);

	goback = false;

	if(weapon == "restricted") goback = true;
	else if(isDefined(self.pers["weapon1"]) && self.pers["weapon1"] == response && !isDefined(self.pers["weapon1_new"])) goback = true;
	else if(isDefined(self.pers["weapon1_new"]) && self.pers["weapon1_new"] == response) goback = true;

	if(goback)
	{
		if(self.pers["team"] == "allies") self openMenu(game["menu_weapon_allies_sec"]);
		else if(self.pers["team"] == "axis") self openMenu(game["menu_weapon_axis_sec"]);

		return;
	}

	self updateDisabledSingleClient(response);

	if(!isDefined(self.pers["weapon2"]) && !level.wwx_roundbased)
	{
		self.pers["weapon2"] = response;

		if(level.wwx_currentgt == "hq")
		{
			if(isDefined(self.WaitingOnTimer) || ((self.pers["team"] == level.DefendingRadioTeam) && isDefined(self.WaitingOnNeutralize)))
			{
				self thread maps\mp\gametypes\hq::respawn();
				self thread maps\mp\gametypes\hq::updateTimer();
			}
			else [[level.spawnplayer]]();
		}
			else if(isDefined(self.WaitingToSpawn) && (level.wwx_currentgt == "htf" || level.wwx_currentgt == "ctf" || level.wwx_currentgt == "ctfb" || level.wwx_currentgt == "dom" || level.wwx_currentgt == "cnq" || level.wwx_currentgt == "vip" || level.wwx_currentgt == "ihtf" || level.wwx_currentgt == "ons" || level.wwx_currentgt == "esd"))
			{
				if(level.wwx_currentgt == "htf")
				{
					self thread maps\mp\gametypes\htf::respawn();
					self thread maps\mp\gametypes\htf::updateTimer();
				}
				else if(level.wwx_currentgt == "ihtf")
				{
					self thread maps\mp\gametypes\ihtf::respawn();
					self thread maps\mp\gametypes\ihtf::updateTimer();
				} 
				else if(level.wwx_currentgt == "ctfb")
				{
					self thread maps\mp\gametypes\ctfb::respawn();
					self thread maps\mp\gametypes\ctfb::updateTimer();
				} 
				else if(level.wwx_currentgt == "dom")
				{
					self thread maps\mp\gametypes\dom::respawn();
					self thread maps\mp\gametypes\dom::updateTimer();
				}
				else if(level.wwx_currentgt == "cnq")
				{
					self thread maps\mp\gametypes\cnq::respawn();
					self thread maps\mp\gametypes\cnq::updateTimer();
				}
				else if(level.wwx_currentgt == "vip")
				{
					self thread maps\mp\gametypes\vip::respawn();
					self thread maps\mp\gametypes\vip::updateTimer();
				} 
				else if(level.wwx_currentgt == "ons")
				{
					self thread maps\mp\gametypes\ons::respawn();
					self thread maps\mp\gametypes\ons::updateTimer();
				} 
				else if(level.wwx_currentgt == "esd")
				{
					self thread maps\mp\gametypes\esd::respawn();
					self thread maps\mp\gametypes\esd::updateTimer();
				}
				else
				{
					self thread maps\mp\gametypes\ctf::respawn();
					self thread maps\mp\gametypes\ctf::updateTimer();
				}
		}
		else [[level.spawnplayer]]();
	}
	else if(!level.wwx_roundbased && isDefined(weapon))
	{
		self.pers["weapon2"] = weapon;
		self.pers["weapon2_new"] = weapon;

		weaponname = getWeaponName(self.pers["weapon2"]);
		
		if(useAn(self.pers["weapon"])) self iprintln(&"MP_YOU_WILL_RESPAWN_WITH_AN_SECONDARY", weaponname);
		else self iprintln(&"MP_YOU_WILL_RESPAWN_WITH_A_SECONDARY", weaponname);
	}		
	else if(!isDefined(self.pers["weapon2"]) && level.wwx_roundbased)
	{
		self.pers["weapon2"] = weapon;
		self.pers["weapon2_new"] = weapon;
		game[self.pers["weapon2"]] = self.pers["weapon2"];
		self notify("sec_continue");
		self closeMenu();
		self thread maps\mp\gametypes\_spectating::setSpectatePermissions();
		if(!level.roundstarted) self.pers["first_time"] = false;
	}
	else	if(level.wwx_roundbased)
	{
		self.pers["weapon2"] = weapon;
		self.pers["weapon2_new"] = weapon;
		game[self.name]["weapon2"] =  self.pers["weapon2"];

		weaponname = getWeaponName(self.pers["weapon2"]);

		if(self.pers["team"] == "allies")
		{
			if(useAn(self.pers["weapon2"])) self iprintln(&"MP_YOU_WILL_SPAWN_ALLIED_WITH_AN_NEXT_ROUND_SECONDARY", weaponname);
			else self iprintln(&"MP_YOU_WILL_SPAWN_ALLIED_WITH_A_NEXT_ROUND_SECONDARY", weaponname);
		}
		else
		{
			if(useAn(self.pers["weapon2"])) self iprintln(&"MP_YOU_WILL_SPAWN_AXIS_WITH_AN_NEXT_ROUND_SECONDARY", weaponname);
			else self iprintln(&"MP_YOU_WILL_SPAWN_AXIS_WITH_A_NEXT_ROUND_SECONDARY", weaponname);
		}		
	}
	else [[level.spawnplayer]]();

	if(level.wwx_currentgt == "dom")
	{	
		thread maps\mp\gametypes\dom::CheckTeams();
	}

	else if(level.wwx_currentgt == "ons")
	{	
		thread maps\mp\gametypes\ons::CheckTeams();
	}
	
	self thread maps\mp\gametypes\_spectating::setSpectatePermissions();
}

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

	// clear standard weapons
	self.pers["weapon"] = undefined;
	self.pers["weapon1"] = undefined;
	self.pers["weapon2"] = undefined;
	self.pers["weapon1_new"] = undefined;
	self.pers["weapon2_new"] = undefined;
	self.pers["spawnweapon"] = undefined;

	// clear saved roundbased weapons
	if(isDefined(game[self.name]))
	{
		if(isDefined(game[self.name]["weapon1"])) game[self.name]["weapon1"] = undefined;
		if(isDefined(game[self.name]["weapon2"])) game[self.name]["weapon2"] = undefined;
	}
}