init()
{

	// * Nur ein Mal Precache *
	if(isDefined(game["menu_precached"])) {
	
		level thread onPlayerConnect();
		return;
	}

	game["menu_precached"] = true;
	
	

	// * AdmiralMod *
	if(game["am_enabled"] == 1 && game["am_use_player_class_system"] == 1) {

		game["menu_ingame"] = "ingame_admiralmod";
	}
	else {
	
		game["menu_ingame"] = "ingame";	
	}
	
	game["menu_team"] 						= "team_" + game["allies"] + game["axis"];
	game["menu_weapon_allies"] 				= "weapon_" + game["allies"];
	game["menu_weapon_axis"] 				= "weapon_" + game["axis"];
	game["menu_weapon_secondary_allies"] 	= "weapon_" + game["allies"];
	game["menu_weapon_secondary_axis"] 		= "weapon_" + game["axis"];


	if(game["am_only_rifles"] == 1) {
	
		game["menu_weapon_allies"] 				= "weapon_rifles";
		game["menu_weapon_axis"] 				= "weapon_rifles";
		game["menu_weapon_secondary_allies"] 	= "weapon_rifles";
		game["menu_weapon_secondary_axis"] 		= "weapon_rifles";
	}
	
	if(game["am_only_snipers"] == 1) {
	
		game["menu_weapon_allies"] 				= "weapon_snipers";
		game["menu_weapon_axis"] 				= "weapon_snipers";
		game["menu_weapon_secondary_allies"] 	= "weapon_snipers";
		game["menu_weapon_secondary_axis"] 		= "weapon_snipers";
	}

	// * AdmiralMod *
	if(game["am_enabled"] == 1 && game["am_use_player_class_system"] == 1) {
	
		game["menu_class_selection_allies"] = "player_class_selection_allies";
		game["menu_class_selection_axis"] 	= "player_class_selection_axis";
		
		precacheMenu(game["menu_class_selection_allies"]);
		precacheMenu(game["menu_class_selection_axis"]);
		
		precacheMenu("weapon_class_0_" + game["allies"]);
		precacheMenu("weapon_class_1_" + game["allies"]);
		precacheMenu("weapon_class_2_" + game["allies"]);
		precacheMenu("weapon_class_3_" + game["allies"]);
		precacheMenu("weapon_class_4_" + game["allies"]);
		precacheMenu("weapon_class_5_" + game["allies"]);
		precacheMenu("weapon_class_6_" + game["allies"]);
		precacheMenu("weapon_class_7_" + game["allies"]);

		precacheMenu("weapon_class_0_" + game["axis"]);
		precacheMenu("weapon_class_1_" + game["axis"]);
		precacheMenu("weapon_class_2_" + game["axis"]);
		precacheMenu("weapon_class_3_" + game["axis"]);
		precacheMenu("weapon_class_4_" + game["axis"]);
		precacheMenu("weapon_class_5_" + game["axis"]);
		precacheMenu("weapon_class_6_" + game["axis"]);
		precacheMenu("weapon_class_7_" + game["axis"]);
	}



	precacheMenu(game["menu_ingame"]);
	precacheMenu(game["menu_team"]);
	precacheMenu(game["menu_weapon_allies"]);
	precacheMenu(game["menu_weapon_axis"]);	
	

	if(!level.xenon)
	{
	
		game["menu_serverinfo"] = "serverinfo_" + getCvar("g_gametype");

	
		game["menu_callvote"] = "callvote";
		game["menu_muteplayer"] = "muteplayer";


		precacheMenu(game["menu_serverinfo"]);


		precacheMenu(game["menu_callvote"]);
		precacheMenu(game["menu_muteplayer"]);
	}
	else
	{
		level.splitscreen = isSplitScreen();
		if(level.splitscreen)
		{
			game["menu_team"] += "_splitscreen";
			game["menu_weapon_allies"] += "_splitscreen";
			game["menu_weapon_axis"] += "_splitscreen";
			game["menu_ingame_onteam"] = "ingame_onteam_splitscreen";
			game["menu_ingame_spectator"] = "ingame_spectator_splitscreen";

			precacheMenu(game["menu_team"]);
			precacheMenu(game["menu_weapon_allies"]);
			precacheMenu(game["menu_weapon_axis"]);
			precacheMenu(game["menu_ingame_onteam"]);
			precacheMenu(game["menu_ingame_spectator"]);
		}
	}

	level thread onPlayerConnect();
}

onPlayerConnect()
{
	for(;;)
	{
		level waittill("connecting", player);
		player thread onMenuResponse();
		
		
		// * nur 1 mal Reseten (z.B. beim 1. Start von SD) *
		if(!isDefined(player.pers["player_class"])) {
		
			player.pers["remember_player_class"] 	= -1;
			player.pers["preselected_player_class"] = -1;
			player.pers["player_class"] 			= -1;	
		}
	}
}

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

	
	
	for(;;)
	{
		self waittill("menuresponse", menu, response);
		
		//iprintln("^1", menu);
		//iprintln("^6", response);
		
		
		if(isDefined(menu) && (menu == "quickmessage" || menu == "-1")) {
				
			if(response == "4") {
								
				self thread playTaunts();
				continue;
			}			
			
		}
		

		//iprintln("", menu);
		//iprintln("", response);

			
			
		// * Klasse Limit durchführen und anzeigen *
		self maps\mp\gametypes\_admiral_mod_player_class::MakeClassLimit();	

		// * Waffen Limit durchführen und anzeigen *
		self maps\mp\gametypes\_admiral_mod_weapons::MakeWeaponLimit();
		
		
		
		

		
		// * Wenn Weapon ausgewählt wurde *
		if(!isDefined(self.pers["imabot"]) && !isDefined(self.pers["imabot_automatic"]) && isDefined(menu) && maps\mp\gametypes\_admiral_mod_utils::find_first_string(menu, "weapon_") > -1) {

			// * Checken ob in der Zwischenzeit ein Spieler diese Waffe bereits besetzt hat *
			if(self maps\mp\gametypes\_admiral_mod_weapons::CheckWeaponIsLimited(response) == true) {

				// * wieder Klassenauswahl aufrufen *	
				if(self.pers["team"] == "allies")		self openMenu(game["menu_weapon_allies"]);
				else if(self.pers["team"] == "axis")	self openMenu(game["menu_weapon_axis"]);					

				continue;
			}		
		}
		

		
	
		// * Wenn Klasse ausgewählt wurde *
		if(maps\mp\gametypes\_admiral_mod_utils::find_first_string(response, "class") > -1) {
		
							
					
			switch(response) {
			
				case "class_soldier_light":
				self.pers["remember_player_class"] = 0;				
				break;
				
				case "class_soldier_heavy":
				self.pers["remember_player_class"] = 1;				
				break;
				
				case "class_soldier_support":
				self.pers["remember_player_class"] = 2;				
				break;
				
				case "class_sharpshooter":
				self.pers["remember_player_class"] = 3;	
				break;

				case "class_medic":
				self.pers["remember_player_class"] = 4;	
				break;

				case "class_tnt":
				self.pers["remember_player_class"] = 5;	
				break;

				case "class_officer":
				self.pers["remember_player_class"] = 6;	
				break;

				case "class_spy":				
				self.pers["remember_player_class"] = 7;	
				break;
				
			}


			// * Checken ob in der Zwischenzeit ein Spieler diese Klasse bereits besetzt hat *
			if(self maps\mp\gametypes\_admiral_mod_player_class::CheckClassIsLimited() == true) {

				// * wieder Klassenauswahl aufrufen *	
				if(self.pers["team"] == "allies")		self openMenu(game["menu_class_selection_allies"]);
				else if(self.pers["team"] == "axis")	self openMenu(game["menu_class_selection_axis"]);					

				continue;
			}

			
			// * Wenn eine Klasse ausgewählt + zuvor ein Team ausgewählt wurde (Axis/Allies) *  
			if(isDefined(self.pers["remember_player_class"]) && self.pers["remember_player_class"] > -1 && isDefined(self.pers["team"]) && (self.pers["team"] == "axis" || self.pers["team"] == "allies") ) {
			
				// * die richtige Waffenseite zur Klasse aufrufen *
				m = "weapon_class_" + self.pers["remember_player_class"] + "_" + game[self.pers["team"]];						
				self openMenu(m);
			}
			else {
			
				// * Wenn zu keinem Team zugehörig, dann erst Team wählen *										
				self openMenu(game["menu_team"]);
			}
					
		}
		

		if(response == "back")
		{
			self closeMenu();
			self closeInGameMenu();

			if(menu == game["menu_team"])
			{
				if(level.splitscreen)
				{
					if(self.pers["team"] == "spectator")
						self openMenu(game["menu_ingame_spectator"]);
					else
						self openMenu(game["menu_ingame_onteam"]);
				}
				else
					self openMenu(game["menu_ingame"]);
			}
			else {
			
				if(isDefined(menu) && (menu == game["menu_weapon_allies"] || menu == game["menu_weapon_axis"]))	self openMenu(game["menu_team"]);
			}
				
			continue;
		}

		if(response == "endgame")
		{
			if(level.splitscreen)
				level thread [[level.endgameconfirmed]]();
				
			continue;
		}


		// * Neu * 
		if(response == "endround")
		{
			level thread [[level.endgameconfirmed]]();
			continue;
		}

		
		if(isDefined(menu) && (menu == game["menu_ingame"] || (level.splitscreen && (menu == game["menu_ingame_onteam"] || menu == game["menu_ingame_spectator"]))))
		{
			
			switch(response)
			{
				// * Waffen oder Klassenauswahl *
				case "changeweapon":


					self closeMenu();
					self closeInGameMenu();


					// * Wenn zu keinem Team zugehörig, dann erst Team wählen *										
					if(isDefined(self.pers["team"]) && self.pers["team"] != "axis" && self.pers["team"] != "allies") {
					
						self openMenu(game["menu_team"]);
						continue;
					}


					// * Klassenauswahl *
					if(game["am_enabled"] == 1 && game["am_use_player_class_system"] == 1) {

						if(self.pers["team"] == "allies")
							self openMenu(game["menu_class_selection_allies"]);
						else if(self.pers["team"] == "axis")
							self openMenu(game["menu_class_selection_axis"]);
					}
					// * Waffenauswahl *
					else {

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

					break;	

				case "changeteam":
										
					self closeMenu();
					self closeInGameMenu();
					self openMenu(game["menu_team"]);
					break;

				case "muteplayer":
					if(!level.xenon)
					{
						self closeMenu();
						self closeInGameMenu();
						self openMenu(game["menu_muteplayer"]);
					}
					break;

				case "callvote":
					if(!level.xenon)
					{
						self closeMenu();
						self closeInGameMenu();
						self openMenu(game["menu_callvote"]);
					}
					break;
			}
				
		
		}
		
		// * Wenn ein TEAM ausgewählt wurde *
		else if(isDefined(menu) && menu == game["menu_team"])
		{
			
			// * Wenn Player kein Clanmember und ins Clan-Member (Mehrheit) Joinen will, dann verhindern *
			if(game["am_clan_vs_public"] == 1 && isDefined(response) && (response == "axis" || response == "allies")) {
			
				clan_team = maps\mp\gametypes\_admiral_mod_player::GetClanTeam();		
				
				if(!isDefined(self.pers["clanmember"]) && clan_team == response) continue;				
			}
		
	
			switch(response)
			{
			case "allies":
				self closeMenu();
				self closeInGameMenu();
				self [[level.allies]]();
				break;

			case "axis":
				self closeMenu();
				self closeInGameMenu();
				self [[level.axis]]();
				break;

			case "autoassign":
				self closeMenu();
				self closeInGameMenu();
				self [[level.autoassign]]();
				break;

			case "spectator":
				self closeMenu();
				self closeInGameMenu();
				self [[level.spectator]]();
				break;
			}
		}
				
		// * Wenn Waffenauswahl oder Waffen-Klassenauswahl angezeigt wird *
		else if(isDefined(menu) && (menu == game["menu_weapon_allies"] || menu == game["menu_weapon_axis"] || maps\mp\gametypes\_admiral_mod_utils::find_first_string(menu, "weapon_class") > -1))
		{
			// * AdmiralMOD *						
			
			// * Wenn diese Waffe nicht zum gewählten Team: German, British, American, Russian gehört! (unknown Weapon) *
			if(!self maps\mp\gametypes\_admiral_mod_weapons::TeamWeaponCheck(response)) continue;
			

			if(game["am_enabled"] == 1 && game["am_use_player_class_system"] == 1) {


				// * Checken ob in der Zwischenzeit ein Spieler diese Klasse bereits besetzt hat *
				if(self maps\mp\gametypes\_admiral_mod_player_class::CheckClassIsLimited() == true) {

					// * wieder Klassenauswahl aufrufen *	
					if(self.pers["team"] == "allies")		self openMenu(game["menu_class_selection_allies"]);
					else if(self.pers["team"] == "axis")	self openMenu(game["menu_class_selection_axis"]);					
				
					continue;
				}


				self.pers["preselected_player_class"] = self.pers["remember_player_class"];
				

				// * Player-Class *				 
				self maps\mp\gametypes\_admiral_mod_player_class::ChangeClassByDying();
				
			}

		
		
			self.pers["am_weapon_changed"] = true;
		

			self closeMenu();
			self closeInGameMenu();
			self [[level.weapon]](response);
		}
		else if(!level.xenon && isDefined(menu))
		{
			if(menu == game["menu_quickcommands"])
				maps\mp\gametypes\_quickmessages::quickcommands(response);
			else if(menu == game["menu_quickstatements"])
				maps\mp\gametypes\_quickmessages::quickstatements(response);
			else if(menu == game["menu_quickresponses"])
				maps\mp\gametypes\_quickmessages::quickresponses(response);
		
			
			// * Neu *
		
			else if(menu == game["menu_serverinfo"] && response == "close")
			{
				self closeMenu();
				self closeInGameMenu();
				self openMenu(game["menu_team"]);
				self.pers["skipserverinfo"] = true;
			}		
		
		
		}
	}
}


playTaunts() {

	self endon("disconnect");

	
	if(isDefined(self.play_taunt)) return;
	
	self.play_taunt = true;
	
	kurz = maps\mp\gametypes\_admiral_mod_player::GetTeamKurz();


	soundalias = "am_"+ kurz +"_" + RandomInt(3) + "_taunt";
	saytext = &"AM_MP_MESSAGE_TAUNT";				

	self maps\mp\gametypes\_quickmessages::saveHeadIcon();				
	maps\mp\gametypes\_quickmessages::doQuickMessage(soundalias, saytext);
	
	wait 4;


	self maps\mp\gametypes\_quickmessages::restoreHeadIcon();
	self.play_taunt = undefined;
}



am_menuSelection() {


	self endon("disconnect");



	if(game["am_enabled"] == 1) {

		if(game["am_use_player_class_system"] == 1) {

			// * Klasse Limit durchführen und anzeigen *
			self maps\mp\gametypes\_admiral_mod_player_class::MakeClassLimit();	
		}

		// * Waffen Limit durchführen und anzeigen *
		self maps\mp\gametypes\_admiral_mod_weapons::MakeWeaponLimit();
	}
	


	scriptMainMenu = game["menu_ingame"];			

			
	if(self.pers["team"] == "allies") {

		if(game["am_enabled"] == 1 && game["am_use_player_class_system"] == 1) {

			scriptMainMenu = game["menu_class_selection_allies"];
			self openMenu(scriptMainMenu);	

		}
		else {

			self openMenu(game["menu_weapon_allies"]);
			scriptMainMenu = game["menu_weapon_allies"];
		}
	}
	else
	{
		
		if(game["am_enabled"] == 1 && game["am_use_player_class_system"] == 1) {

			scriptMainMenu = game["menu_class_selection_axis"];
			self openMenu(scriptMainMenu);	

		}
		else {
		
			self openMenu(game["menu_weapon_axis"]);
			scriptMainMenu = game["menu_weapon_axis"];
		}
	}

	
	return scriptMainMenu;

}

