// * AdmiralMOD by Matthias Lorenz, http://www.cod2mod.com *

init() {
	
	if(game["am_error_logfile"] == 1) logPrint("Tournament Init\n");


	if(game["am_tournament_mode"] == 0) return;

	
	if(!isDefined(game["am_tournament_status"])) 	game["am_tournament_status"] 	= "waiting_teams";
	if(!isDefined(game["am_tournament_halftime"])) 	game["am_tournament_halftime"] 	= "no_halftime_running";
	

	
	if(!isDefined(game["matchstarted"]) || (isDefined(game["matchstarted"]) && !game["matchstarted"])) {
	
	
		precacheStatusIcon("am_status_point");

		
		if(game["am_tournament_public_mode"] == 1) 		game["tournament_title"] = &"AM_MP_TOURNAMENT_MODE_PUBLIC";
		else 											game["tournament_title"] = &"AM_MP_TOURNAMENT_MODE_MATCH";
				
		precacheString(game["tournament_title"]);


		game["tournament_ready_text"]		= &"AM_MP_PRESS_USE_BUTTON_WHEN_READY";
		game["tournament_waiting_on_text"] 	= &"AM_MP_WAITING_ON";
		game["tournament_players_text"] 	= &"AM_MP_PLAYERS";
		game["tournament_your_status_text"] = &"AM_MP_YOUR_STATUS";
		game["tournament_player_ready"] 	= &"AM_MP_READY";
		game["tournament_player_notready"] 	= &"AM_MP_NOT_READY";
		
		precacheString(game["tournament_ready_text"]);
		precacheString(game["tournament_waiting_on_text"]);	
		precacheString(game["tournament_players_text"]);	
		precacheString(game["tournament_your_status_text"]);
		precacheString(game["tournament_player_ready"]);	
		precacheString(game["tournament_player_notready"]);


		if(!isDefined(game["leaguemode"]) || game["am_tournament_league_setting"] == "free") game["leaguemode"] = &"AM_MP_FREE_LEAGUE_SETTINGS";
		precacheString(game["leaguemode"]);


		game["am_saved_alliedscore"] 	= 0;
		game["am_saved_axisscore"] 		= 0;


		// * Thread, weil 1 Sekunde warten *
		ShowTournamentTitle();
		ShowLeagueMode();
	}


	// * Thread, weil 2 Sekunde warten bis GameInit zuende, eh Counter angezeigt werden kann *
	level thread ShowMatchCounter();
		
	
	level thread onPlayerConnect();
	
}


onPlayerConnect()
{

	if(game["am_error_logfile"] == 1) logPrint("onPlayerConnect\n");


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

		
		player thread onPlayerButtonPressed();
		player thread onPlayerSpawned();
		player thread onPlayerKilled();
			
		player.pers["tournament_ready"] = undefined;		
	}
}



onPlayerSpawned() {

	if(game["am_error_logfile"] == 1) logPrint("onPlayerSpawned\n");

	self endon("disconnect");
	
	for(;;) {
	
		self waittill("spawned_player");	
		
		if(game["am_tournament_status"] == "waiting_players") self CreatePlayerHudElements();		
	}
}

onPlayerKilled() {

	if(game["am_error_logfile"] == 1) logPrint("onPlayerKilled\n");


	self endon("disconnect");
	
	for(;;)	{
	
		self waittill("killed_player");
		
			
		wait 1;
	}
}	



onPlayerButtonPressed() {

	if(game["am_error_logfile"] == 1) logPrint("onPlayerButtonPressed\n");


	self endon("disconnect");

		
	for(;;) {
		
		
		if(game["am_tournament_status"] == "waiting_players" && self.sessionstate != "spectator") {

			
			// * Auf USE-Button warten... *
			while(game["am_tournament_status"] == "waiting_players" && (self.sessionstate == "playing" || self.sessionstate == "dead") && !self useButtonPressed()) wait 0.1;


			
			
			// * Wenn Spectator *
			if(self.sessionstate == "spectator") {
			
				wait 1;
				continue;
			}


			// * Wenn während des Button Wartens das Spiel gestartet wurde... nicht mehr switchen *
			if(game["am_tournament_status"] != "waiting_players") {
			
				wait 1;
				continue;
			}


			
			// * Status-Icon in der Highscore anzeigen *
			if(!isDefined(self.pers["tournament_ready"])) {

				self.statusicon = "";
				self.pers["tournament_ready"] = true;
				self maps\mp\gametypes\_admiral_mod_hud::changePlayerHudElementTextByName("tournament_hud_player_status_text", game["tournament_player_ready"], (0.66,1,0.66));
			}
			else {

				self.statusicon = "am_status_point";
				self.pers["tournament_ready"] = undefined;	
				self maps\mp\gametypes\_admiral_mod_hud::changePlayerHudElementTextByName("tournament_hud_player_status_text", game["tournament_player_notready"], (1,0.66,0.66));
			}



			// * Warten auf Button loslassen *
			while(game["am_tournament_status"] == "waiting_players" && self useButtonPressed()) wait 0.1;
			

			wait 1;			
			continue;
		}
		
		
		wait 1;
	}
}	





CheckHalftimes() {

	if(game["am_error_logfile"] == 1) logPrint("CheckHalftimes\n");


	if(game["am_tournament_mode"] == 0) return;


	UpdateMatchCounter();

	
	// * Wenn noch keine Halbzeit gespielt dann auf die Teams + Ready warten *
	if(game["am_tournament_halftime"] == "no_halftime_running" || game["am_tournament_halftime"] == "wait_for_second_halftime_running") {
	
		WaitAllPlayersReady();
		
		if(game["am_tournament_halftime"] == "no_halftime_running") {
		
			level.nodamage = true;
		
			game["am_tournament_halftime"] = "first_halftime_running";
			iprintlnbold(&"AM_MP_MATCHSTARTING_FIRST_HALFTIME");
			
			StartWaitTimer(15,288,208);
			
			// * Tournament und Live-Score *
			if(game["am_tournament_live_score"] == 1) setCvar("sv_hostname", "AM Cup Server: running");
			
			ResetScores();
			wait 0.2;
			
			game["matchstarted"] = true;
			level.roundended = true;
									
			
			level notify("restarting");			
			map_restart(true);	
			
		}
		
		if(game["am_tournament_halftime"] == "wait_for_second_halftime_running") {
		
			level.nodamage = true;
		
			game["am_tournament_halftime"] = "second_halftime_running";
			iprintlnbold(&"AM_MP_MATCHSTARTING_SECOND_HALFTIME");
			
			level.free_respawn = true;
			
			StartWaitTimer(15,288,208);	
			
			level.free_respawn = undefined;
			
			
			ResetScores();	
			wait 0.2;
			
			game["matchstarted"] = true;
			level.roundended = true;			
			
			
			level notify("restarting");			
			map_restart(true);	
		}
	}
	

	
		
	// * Wenn die erste Hälfte gespielt dann auf die Teams + Ready warten *
	if(game["am_tournament_halftime"] == "first_halftime_running") {
	
		halftime_switch = false;
		
		// * Runden-Halbzeit *
		if(isDefined(game["roundsplayed"]) && game["roundsplayed"] >= game["am_tournament_halftime_switch_round"] && game["am_tournament_halftime_switch_round"] > 0) 	halftime_switch = true;
		
		// * Score-Halbzeit *
		if(isDefined(game["alliedscore"]) && game["alliedscore"] >= game["am_tournament_halftime_switch_score"] && game["am_tournament_halftime_switch_score"] > 0) 	halftime_switch = true;
		if(isDefined(game["axisscore"]) && game["axisscore"] >= game["am_tournament_halftime_switch_score"] && game["am_tournament_halftime_switch_score"] > 0) 	halftime_switch = true;


		// * Wenn TDM und Zeit abgelaufen *
		if(game["am_gametype"] == "tdm" && level.mapended) halftime_switch = true;
	
	
		if(halftime_switch) {
				
			game["am_tournament_halftime"] = "wait_for_second_halftime_running";	

			level.nodamage = true;
			EndOfFirstHalftime();

			return;
		}
	}

		
	// * Wenn die zweite Hälfte gespielt dann ENDE *
	if(game["am_tournament_halftime"] == "second_halftime_running") {

		end_of_game = false;

		// * Runden-Halbzeit *
		if(isDefined(game["roundsplayed"]) && game["roundsplayed"] >= game["am_tournament_end_at_round"] && game["am_tournament_end_at_round"] > 0) 	end_of_game = true;

		// * Score-Halbzeit *
		if(isDefined(game["alliedscore"]) && game["alliedscore"] >= game["am_tournament_end_at_score"] && game["am_tournament_end_at_score"] > 0) 	end_of_game = true;
		if(isDefined(game["axisscore"]) && game["axisscore"] >= game["am_tournament_end_at_score"] && game["am_tournament_end_at_score"] > 0) 		end_of_game = true;


		// * Wenn TDM und Zeit abgelaufen *
		if(game["am_gametype"] == "tdm" && level.mapended) end_of_game = true;


		if(end_of_game) {
	
			game["am_tournament_halftime"] = "end_of_second_halftime";	
		
			level.nodamage = true;
			EndOfSecondHalftime();
		
			return;
		}
	}



	level thread ShowTournamentTitle();
	level thread ShowLeagueMode();

	wait 4;
	StartWaitTimer(4,570,188);
	
}



WaitAllPlayersReady() {

	if(game["am_error_logfile"] == 1) logPrint("WaitAllPlayersReady\n");

	
	// * Wenn Public-Mode, dann kein Waiting of Players Ready *
	if(game["am_tournament_public_mode"] == 1) {
	

		game["am_tournament_status"] = "waiting_teams";

		// * Warten bis in einem Team je 1 Spieler *
		while(!maps\mp\gametypes\_admiral_mod_utils::check_teams_exist_plus()) wait 1;
	

	
		game["am_tournament_status"] = "running";
		
				
		// * Wenn noch keine Halbzeit gespielt dann Demo Record *
		if(game["am_tournament_halftime"] == "no_halftime_running") {
				
			StopRecord();
			StartRecordDemo();							
		}



		level.free_respawn = undefined;
	
		return;
	}


	DeleteHudElements();
	HideRoundTimer();


	
	level.free_respawn = true;
	
	
	game["am_tournament_status"] = "waiting_teams";
	

	activated_hud = 0;	
	wait_for_count_old = -1;


	
	// * Warten bis in einem Team je 1 Spieler *
	while(!maps\mp\gametypes\_admiral_mod_utils::check_teams_exist_plus()) wait 1;
	
	
	
	CreateHudElements();	


	game["am_tournament_status"] = "waiting_players";
	
	
	while(1) {


		// * Wenn während dem warten Spieler das "waiting_players" verlassen, dann Waiting neu starten*
		if(!maps\mp\gametypes\_admiral_mod_utils::check_teams_exist()) {
						
			WaitAllPlayersReady();
			break;
		}


		player_count = 0; 	// * Spielende Spieler *
		ready_count = 0; 	// * Spieler RDY *


		players = getentarray("player", "classname");
		for(i = 0; i < players.size; i++) {

			if(players[i].sessionstate == "playing" || players[i].sessionstate == "dead") {
			
				player_count++;
			
				if(isDefined(players[i].pers["tournament_ready"])) {
							
					ready_count++;
				}
			}
		}

		
		
		wait_for_count = player_count-ready_count;
					
		// * Spieler-Anzahl Anzeige updaten * 
		if(wait_for_count != wait_for_count_old) {
			
			wait_for_count_old = wait_for_count;
			level maps\mp\gametypes\_admiral_mod_hud::changeLevelTextHudElementByName("tournament_hud_player_value",undefined,wait_for_count,1.2,1,(0.98,0.98,0.60));
		}
	
	
		// * Wenn alle Spieler READY! *
		if(player_count > 1 && player_count == ready_count) {
		
			game["am_tournament_status"] = "running";
			
			
			wait 1; // * Anzeige Waiting 0 betrachten *
			
			DeleteHudElements();
			
			
			// * Wenn noch keine Halbzeit gespielt dann Demo Record *
			if(game["am_tournament_halftime"] == "no_halftime_running") {
			
				StopRecord();
				StartRecordDemo();							
			}
			
			
			level.free_respawn = undefined;
			
			return;	
		}
				
		wait 0.5;	
		
	}
}



StartWaitTimer(zeit,x,y) {

	if(game["am_error_logfile"] == 1) logPrint("StartWaitTimer\n");

	// * Auf alle Spieler warten *
	waittimer = newHudElem();
	waittimer.x = x;
	waittimer.y = y;
	waittimer.horzAlign = "left";
	waittimer.vertAlign = "top";
	waittimer setClock(zeit, 60, "hudStopwatch", 64, 64);

	wait zeit;

	if(isDefined(waittimer)) waittimer destroy();
}




EndOfFirstHalftime() {

	if(game["am_error_logfile"] == 1) logPrint("EndOfFirstHalftime\n");


	level notify("update_allhud_score");

	wait 5;


	// * Bildschirm aufräumen *

	maps\mp\gametypes\_admiral_mod_hud::deleteLevelHudElementAll();
	if(isDefined(level.clock))	level.clock destroy();		

	players = getentarray("player","classname");
	for(i=0;i<players.size;i++) {

		players[i] closeMenu(); 
		players[i] closeInGameMenu();	
		players[i] maps\mp\gametypes\_admiral_mod_player::ClearHud();
		players[i] maps\mp\gametypes\_admiral_mod_player::SpawnVoteIntermission();		

		if(isDefined(players[i].hud_teamicon))		players[i].hud_teamicon destroy();		
		if(isDefined(players[i].hud_enemyicon))		players[i].hud_enemyicon destroy();		
		if(isDefined(players[i].hud_teamscore))		players[i].hud_teamscore destroy();		
		if(isDefined(players[i].hud_enemyscore))	players[i].hud_enemyscore destroy();	
	
		players[i] freezeControls(true);	
	}


	wait 0.1;



	// * Highscore anzeigen *

	if(game["alliedscore"] == game["axisscore"])		text = &"AM_MP_THE_GAME_IS_A_TIE_FIRST_HALFTIME";
	else if(game["alliedscore"] > game["axisscore"])	text = &"AM_MP_ALLIES_WIN_FIRST_HALFTIME";
	else												text = &"AM_MP_AXIS_WIN_FIRST_HALFTIME";


	players = getentarray("player","classname");

	for(i=0;i<players.size;i++) {

		players[i] openMenu("open_highscore"); 
		players[i] setClientCvar("cg_objectiveText", text);
		wait 0.05;
		players[i] closeMenu(); 
	}


	wait 1;
	maps\mp\gametypes\_admiral_mod_tournament::MakeScreenshot();

	wait 6;



	// * Teamwechsel *
	players = getentarray("player","classname");
	for(i=0;i<players.size;i++) {

		// * Wenn Spectator, dann nicht wechseln *                
		if(!isDefined(players[i].pers["team"]) || (players[i].pers["team"] != "allies" && players[i].pers["team"] != "axis")) continue;

		if(players[i].pers["team"] == "axis")           players[i] maps\mp\gametypes\_teams::changeTeam("allies");
		else if(players[i].pers["team"] == "allies")    players[i] maps\mp\gametypes\_teams::changeTeam("axis");     
	
	}            


	// * Punkte-Reset für Highscore *		

	game["am_saved_alliedscore"] 		= game["alliedscore"];
	game["am_saved_axisscore"] 			= game["axisscore"];

	game["alliedscore"] = 0;
	game["axisscore"]   = 0;
	
	
	setTeamScore("allies", game["alliedscore"]);
	setTeamScore("axis", game["axisscore"]);



	players = getentarray("player", "classname");
	for(i=0;i<players.size;i++) {

		players[i].pers["old_score"]    = players[i].score;
		players[i].pers["old_deaths"]   = players[i].deaths;		
	}


	level.roundstarted = false;
	level.roundended = false;
	level.mapended = false;

		


	level notify("restarting");
	map_restart(true);      

	wait 5;

	return;		

	
}



EndOfSecondHalftime() {

	if(game["am_error_logfile"] == 1) logPrint("EndOfSecondHalftime\n");

	
	level notify("update_allhud_score");

	wait 5;


	// * Bildschirm aufräumen *

	maps\mp\gametypes\_admiral_mod_hud::deleteLevelHudElementAll();
	if(isDefined(level.clock))	level.clock destroy();		

	players = getentarray("player","classname");
	for(i=0;i<players.size;i++) {

		players[i] closeMenu(); 
		players[i] closeInGameMenu();	
		players[i] maps\mp\gametypes\_admiral_mod_player::ClearHud();
		players[i] maps\mp\gametypes\_admiral_mod_player::SpawnVoteIntermission();		

		if(isDefined(players[i].hud_teamicon))		players[i].hud_teamicon destroy();		
		if(isDefined(players[i].hud_enemyicon))		players[i].hud_enemyicon destroy();		
		if(isDefined(players[i].hud_teamscore))		players[i].hud_teamscore destroy();		
		if(isDefined(players[i].hud_enemyscore))	players[i].hud_enemyscore destroy();	
		
		players[i] freezeControls(true);	
	}


	wait 0.1;
	
	

	// * Highscore anzeigen *

	if(game["alliedscore"] == game["axisscore"])		text = &"AM_MP_THE_GAME_IS_A_TIE_SECOND_HALFTIME";
	else if(game["alliedscore"] > game["axisscore"])	text = &"AM_MP_ALLIES_WIN_SECOND_HALFTIME";
	else												text = &"AM_MP_AXIS_WIN_SECOND_HALFTIME";


	players = getentarray("player","classname");
	for(i=0;i<players.size;i++) {

		players[i] openMenu("open_highscore"); 
		players[i] setClientCvar("cg_objectiveText", text);
		wait 0.05;
		players[i] closeMenu(); 
	}


	wait 1;
	maps\mp\gametypes\_admiral_mod_tournament::MakeScreenshot();

	wait 1;
		
	maps\mp\gametypes\_admiral_mod_tournament::StopRecord();

	
	wait 5;


	players = getentarray("player","classname");
	for(i=0;i<players.size;i++) {

		players[i] openMenu("close_highscore"); 
		wait 0.05;
		players[i] closeMenu(); 
	}

	
	wait 1;
	
	
	
	// * Gesamtpunktzahl erstellen *
	game["alliedscore"] += game["am_saved_axisscore"];
	game["axisscore"] 	+= game["am_saved_alliedscore"];
	
	setTeamScore("allies", game["alliedscore"]);
	setTeamScore("axis", game["axisscore"]);

	//level notify("update_allhud_score");



	players = getentarray("player", "classname");
	for(i=0;i<players.size;i++) {

		if(isDefined(players[i].pers["old_score"])) 	players[i].score    += players[i].pers["old_score"];
		if(isDefined(players[i].pers["old_deaths"])) 	players[i].deaths   += players[i].pers["old_deaths"];			
	}
	
	
	level.mapended = true;
		
}


CreateHudElements() {

	if(game["am_error_logfile"] == 1) logPrint("CreateHudElements\n");


	// * Hud-Elemente anzeigen *

	level maps\mp\gametypes\_admiral_mod_hud::createLevelTextHudElement("tournament_hud_ready_text", 320, 370,"center","middle","fullscreen","fullscreen",false, game["tournament_ready_text"], undefined,1.2,0,1,0.8,1,1);
	level maps\mp\gametypes\_admiral_mod_hud::createLevelTextHudElement("tournament_hud_waiting", 595, 105,"center","middle","fullscreen","fullscreen",false, game["tournament_waiting_on_text"], undefined,1.2,0,1,0.8,1,1);
	level maps\mp\gametypes\_admiral_mod_hud::createLevelTextHudElement("tournament_hud_player", 595, 145,"center","middle","fullscreen","fullscreen",false, game["tournament_players_text"], undefined,1.2,0,1,0.8,1,1);
	level maps\mp\gametypes\_admiral_mod_hud::createLevelTextHudElement("tournament_hud_player_value", 595, 125,"center","middle","fullscreen","fullscreen",false, undefined, 0,1.2,0,1,0.98,0.98,0.60);


	players = getentarray("player", "classname");
	for(i = 0; i < players.size; i++) {

		players[i] CreatePlayerHudElements();		
	}
}


CreatePlayerHudElements() {

	if(game["am_error_logfile"] == 1) logPrint("CreatePlayerHudElements\n");


	self endon("disconnect");


	self maps\mp\gametypes\_admiral_mod_hud::deletePlayerHudElementByName("tournament_hud_player_status");
	self maps\mp\gametypes\_admiral_mod_hud::createPlayerTextHudElement("tournament_hud_player_status", 595, 190,"center","middle","fullscreen","fullscreen",false,game["tournament_your_status_text"],undefined,1.2,0,1,0.8,1,1);


	self maps\mp\gametypes\_admiral_mod_hud::deletePlayerHudElementByName("tournament_hud_player_status_text");

	if(!isDefined(self.pers["tournament_ready"])) 	{

		self maps\mp\gametypes\_admiral_mod_hud::createPlayerTextHudElement("tournament_hud_player_status_text", 595,210,"center","middle","fullscreen","fullscreen",false,game["tournament_player_notready"],undefined,1.2,0,1,1,0.66,0.66);
		self.statusicon = "am_status_point";
	}
	else {

		self maps\mp\gametypes\_admiral_mod_hud::createPlayerTextHudElement("tournament_hud_player_status_text", 595,210,"center","middle","fullscreen","fullscreen",false,game["tournament_player_ready"],undefined,1.2,0,1,0.66,1,0.66);
		self.statusicon = "";
	}			
}


DeleteHudElements() {

	if(game["am_error_logfile"] == 1) logPrint("DeleteHudElements\n");


	// * Hud-Elemente köschen *
		
	//level maps\mp\gametypes\_admiral_mod_hud::deleteLevelHudElementByName("tournament_hud_title");
	//level maps\mp\gametypes\_admiral_mod_hud::deleteLevelHudElementByName("tournament_hud_leaguemode");
	level maps\mp\gametypes\_admiral_mod_hud::deleteLevelHudElementByName("tournament_hud_ready_text");
	level maps\mp\gametypes\_admiral_mod_hud::deleteLevelHudElementByName("tournament_hud_waiting");
	level maps\mp\gametypes\_admiral_mod_hud::deleteLevelHudElementByName("tournament_hud_player");
	level maps\mp\gametypes\_admiral_mod_hud::deleteLevelHudElementByName("tournament_hud_player_value");
	
	
	players = getentarray("player", "classname");
	for(i = 0; i < players.size; i++) {

		players[i] maps\mp\gametypes\_admiral_mod_hud::deletePlayerHudElementByName("tournament_hud_player_status");
		players[i] maps\mp\gametypes\_admiral_mod_hud::deletePlayerHudElementByName("tournament_hud_player_status_text");
	}
}



ShowRoundTimer() {

	if(game["am_error_logfile"] == 1) logPrint("ShowRoundTimer\n");


	if(isDefined(level.clock)) level.clock.alpha = 1;
}


HideRoundTimer() {

	if(game["am_error_logfile"] == 1) logPrint("HideRoundTimer\n");


	if(isDefined(level.clock)) level.clock.alpha = 0;
}



ShowTournamentTitle() {

	if(game["am_error_logfile"] == 1) logPrint("ShowTournamentTitle\n");

	level maps\mp\gametypes\_admiral_mod_hud::createLevelTextHudElement("tournament_hud_title", 8, 6,"left","top","fullscreen","fullscreen",false, game["tournament_title"], undefined,1.2,0,1,0.85,0.85,0);
}

ShowLeagueMode() {

	if(game["am_error_logfile"] == 1) logPrint("ShowLeagueMode\n");


	level maps\mp\gametypes\_admiral_mod_hud::createLevelTextHudElement("tournament_hud_leaguemode", 320, 6,"center","top","fullscreen","fullscreen",false, game["leaguemode"], undefined,1.2,0,1,0.85,0.85,0);
}


ShowMatchCounter() {

	if(game["am_error_logfile"] == 1) logPrint("ShowMatchCounter\n");


	// * Bei TDM überflüssig *
	if(game["am_gametype"] == "tdm") return;


	wait 2;
	
	// * Rundenanzeige *			
	if(!isDefined(level.hud_round_counter)) {
	
		level.hud_round_counter = newHudElem();
		level.hud_round_counter.horzAlign = "left";
		level.hud_round_counter.vertAlign = "top";
		level.hud_round_counter.x = 14;
		level.hud_round_counter.y = 85;
		level.hud_round_counter.font = "default";
		level.hud_round_counter.fontscale = 1.8;
		level.hud_round_counter.archived = false;
		level.hud_round_counter.alpha = 0.9;
		level.hud_round_counter.color = (.98, .827, .58);
		level.hud_round_counter setValue(0);
	}
	
	if(!isDefined(level.hud_round_limit)) {
		
		level.hud_round_limit = newHudElem();
		level.hud_round_limit.horzAlign = "left";
		level.hud_round_limit.vertAlign = "top";
		level.hud_round_limit.x = 26;
		level.hud_round_limit.y = 85;
		level.hud_round_limit.font = "default";
		level.hud_round_limit.fontscale = 1.8;
		level.hud_round_limit.archived = false;
		level.hud_round_limit.label = (&"MP_SLASH");
		level.hud_round_limit.alpha = 0.9;
		level.hud_round_limit.color = (.98, .827, .58);
		level.hud_round_limit setValue(0);
	}
	
	
	UpdateMatchCounter();	
}



UpdateMatchCounter() {

	if(game["am_error_logfile"] == 1) logPrint("UpdateMatchCounter\n");

	
	// * Bei TDM überflüssig *
	if(game["am_gametype"] == "tdm") return;

	
	
	if(!isDefined(level.hud_round_counter) || !isDefined(level.hud_round_limit)) return;
		
	if(isDefined(game["roundsplayed"])) level.hud_round_counter setValue(game["roundsplayed"]);	
	else 								level.hud_round_counter setValue(0);	


	if(isDefined(game["roundsplayed"]) && game["roundsplayed"] > 9) 	level.hud_round_counter.x = 6;
	else																level.hud_round_counter.x = 14;	
					
		
	// * Der niedrigste zu ereichende Wert (Rounds oder Score) wird angezeigt *
	if(game["am_tournament_end_at_score"] < 1) 																						level.hud_round_limit setValue(game["am_tournament_end_at_round"]);
	else if(game["am_tournament_end_at_round"] < 1) 																				level.hud_round_limit setValue(game["am_tournament_end_at_score"]);
	else if(game["am_tournament_end_at_round"] <= game["am_tournament_end_at_score"] && game["am_tournament_end_at_round"] > 0) 	level.hud_round_limit setValue(game["am_tournament_end_at_round"]);
	else if(game["am_tournament_end_at_score"] <= game["am_tournament_end_at_round"] && game["am_tournament_end_at_score"] > 0) 	level.hud_round_limit setValue(game["am_tournament_end_at_score"]);


	UpdateMatchStateServerName();							
}


UpdateMatchStateServerName() {

	state = "";

	if(game["am_gametype"] == "sd" || game["am_gametype"] == "sdr" || game["am_gametype"] == "csd" ) {
	
			
		if(game["am_tournament_halftime"] == "no_halftime_running") 	state = "Warm Up";
		if(game["am_tournament_halftime"] == "first_halftime_running") 	state = "1st HT (Allies:"+ (game["alliedscore"]+game["am_saved_axisscore"]) + "|Axis:" + (game["axisscore"]+game["am_saved_alliedscore"]) + ")";
		if(game["am_tournament_halftime"] == "second_halftime_running") state = "2nd HT (Allies:"+ (game["alliedscore"]+game["am_saved_axisscore"]) + "|Axis:" + (game["axisscore"]+game["am_saved_alliedscore"]) + ")";
	}


	if(game["am_gametype"] == "tdm") {
		
		if(game["am_tournament_halftime"] == "no_halftime_running") 	state = "Warm Up";
		if(game["am_tournament_halftime"] == "first_halftime_running") 	state = "FIRST (Allies:"+ (getTeamScore("allies")+game["am_saved_axisscore"]) + "|Axis:" + (getTeamScore("axis")+game["am_saved_alliedscore"]) + ")";
		if(game["am_tournament_halftime"] == "second_halftime_running") state = "SECOND (Allies:"+ (getTeamScore("allies")+game["am_saved_axisscore"]) + "|Axis:" + (getTeamScore("axis")+game["am_saved_alliedscore"]) + ")";
	
	}
	
	// * Tournament und Live-Score *	
	if(game["am_tournament_live_score"] == 1 && state != "") setCvar("sv_hostname", "AM Cup Server: " + state);


}





StartRecordDemo() {

	if(game["am_error_logfile"] == 1) logPrint("StartRecordDemo\n");


	if(game["am_tournament_record_demo"] != 1) return;


	// * Game-Nummer, damit man alle Demos eines War/Matches finden kann *
	game_nr = randomint(50000);


	players = getentarray("player", "classname");
	for(i = 0; i < players.size; i++) {
		
		demo_name = "record demo_" + maps\mp\gametypes\_admiral_mod_utils::onlyNormalChars(players[i].name) + "_vs_" + maps\mp\gametypes\_admiral_mod_utils::onlyNormalChars(players[i] GetOtherPlayerName()) + "_" + game_nr;
								
		players[i] setClientCvar("am_ex_befehl", demo_name);		
	}

	wait 0.5;

	players = getentarray("player", "classname");
	for(i = 0; i < players.size; i++) {

		players[i] openMenu("ex_befehl");				
		players[i] closeMenu(); 
	}
}


GetOtherPlayerName() {
	
	if(game["am_error_logfile"] == 1) logPrint("GetOtherPlayerName\n");


	self endon("disconnect");

	players = getentarray("player", "classname");
	for(i = 0; i < players.size; i++) {
										
		if(players[i].pers["team"] != self.pers["team"]) return players[i].name; 		
	}
	
	return "unknown";
}


StopRecord() {

	if(game["am_error_logfile"] == 1) logPrint("StopRecord\n");

	if(game["am_tournament_record_demo"] != 1) return;


	players = getentarray("player", "classname");
	for(i = 0; i < players.size; i++) {
										
		players[i] setClientCvar("am_ex_befehl", "stoprecord");		
	}

	wait 0.5;

	players = getentarray("player", "classname");
	for(i = 0; i < players.size; i++) {

		players[i] openMenu("ex_befehl");				
		players[i] closeMenu(); 
	}
}

	

MakeScreenshot() {

	if(game["am_error_logfile"] == 1) logPrint("MakeScreenshot\n");


	if(game["am_tournament_make_screenshots"] != 1) return;


	players = getentarray("player", "classname");
	for(i = 0; i < players.size; i++) {
		
		players[i] setClientCvar("am_ex_befehl", "screenshotJPEG");				
	}

	wait 0.5;

	players = getentarray("player", "classname");
	for(i = 0; i < players.size; i++) {

		players[i] openMenu("ex_befehl");				
		players[i] closeMenu(); 
	}		

}



ResetScores() {

	if(game["am_error_logfile"] == 1) logPrint("ResetScores\n");


	players = getentarray("player", "classname");
	for(i = 0; i < players.size; i++)
	{
		player = players[i];
		
		player.score = 0;
		player.deaths = 0;
		
		player.pers["score"]    = 0;
		player.pers["deaths"]   = 0;		
	}
}


freezePlayers() {

	if(game["am_error_logfile"] == 1) logPrint("freezePlayers\n");

	     
	players = getentarray("player", "classname");
	
	for(i = 0; i < players.size; i++) players[i] freezeControls(true);			
	
}
