init()
{
	precacheString(&"MP_AMERICAN");
	precacheString(&"MP_BRITISH");
	precacheString(&"MP_RUSSIAN");
	
	switch(game["allies"])
	{
	case "american":
		precacheShader("mpflag_american");
		break;
	
	case "british":
		precacheShader("mpflag_british");
		break;
	
	case "russian":
		precacheShader("mpflag_russian");
		break;
	}

	assert(game["axis"] == "german");
	precacheShader("mpflag_german");
	precacheShader("mpflag_spectator");

	if(getCvar("scr_teambalance") == "")
		setCvar("scr_teambalance", "0");
	level.teambalance = getCvarInt("scr_teambalance");
	level.teambalancetimer = 0;
	
	// * Wenn Clanplay, dann kein teambalance *
	// hat zu Irretationen geführt... deshalb deaktiviert
	//if(game["am_enabled"] == 1 && game["am_clan_vs_public"] == 1) level.teambalance = 0;
	

	level.maxclients = getCvarInt("sv_maxclients");
	
	setPlayerModels();

	level thread onPlayerConnecting();
	level thread onPlayerConnected();

	if(getcvar("g_gametype") != "dm")
	{
		level.teamlimit = level.maxclients / 2;

		level thread updateTeamBalanceCvar();


		//wait .15;		
		wait 2;  // bei Verwendung der AM muß hier eine längere Wartezeit folgen, da sonst kein Autobalance ausgeführt wird!
		
	
		if(getcvar("g_gametype") == "sd")
		{
			if(level.teambalance > 0)
			{
			
				
				if(game["am_enabled"] == 1 && game["am_sd_teambalance_every_round"] == 1 && getTeamBalance()) {
				
					iprintlnbold(&"MP_AUTOBALANCE_NOW");
					
					level balanceTeams();
					game["BalanceTeamsNextRound"] = undefined;				
				}
				else {
			
			
					if(isdefined(game["BalanceTeamsNextRound"]))
							iprintlnbold(&"MP_AUTOBALANCE_NEXT_ROUND");

					level waittill("restarting");

					if(isdefined(game["BalanceTeamsNextRound"]))
					{
						iprintlnbold(&"MP_AUTOBALANCE_NOW");

						level balanceTeams();
						game["BalanceTeamsNextRound"] = undefined;
					}
					else if(!getTeamBalance())
						game["BalanceTeamsNextRound"] = true;
				}
			}			
			
		}
		else
		{
			for(;;)
			{
				if(level.teambalance > 0)
				{
					if(!getTeamBalance())
					{
						iprintlnbold(&"MP_AUTOBALANCE_SECONDS", 15);
					    wait 15;
	
						if(!getTeamBalance())
							level balanceTeams();
					}
					
					wait 59;
				}
				
				wait 1;
			}
		}
		
		
		if(getcvar("g_gametype") == "sdr")
		{
			if(level.teambalance > 0)
			{
			
				
				if(game["am_enabled"] == 1 && game["am_sdr_teambalance_every_round"] == 1 && getTeamBalance()) {
				
					iprintlnbold(&"MP_AUTOBALANCE_NOW");
					
					level balanceTeams();
					game["BalanceTeamsNextRound"] = undefined;				
				}
				else {
			
			
					if(isdefined(game["BalanceTeamsNextRound"]))
							iprintlnbold(&"MP_AUTOBALANCE_NEXT_ROUND");

					level waittill("restarting");

					if(isdefined(game["BalanceTeamsNextRound"]))
					{
						iprintlnbold(&"MP_AUTOBALANCE_NOW");

						level balanceTeams();
						game["BalanceTeamsNextRound"] = undefined;
					}
					else if(!getTeamBalance())
						game["BalanceTeamsNextRound"] = true;
				}
			}			
			
		}
		else
		{
			for(;;)
			{
				if(level.teambalance > 0)
				{
					if(!getTeamBalance())
					{
						iprintlnbold(&"MP_AUTOBALANCE_SECONDS", 15);
					    wait 15;
	
						if(!getTeamBalance())
							level balanceTeams();
					}
					
					wait 59;
				}
				
				wait 1;
			}
		}
		




		if(getcvar("g_gametype") == "csd")
		{
			if(level.teambalance > 0)
			{


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

					iprintlnbold(&"MP_AUTOBALANCE_NOW");

					level balanceTeams();
					game["BalanceTeamsNextRound"] = undefined;				
				}
				else {


					if(isdefined(game["BalanceTeamsNextRound"]))
							iprintlnbold(&"MP_AUTOBALANCE_NEXT_ROUND");

					level waittill("restarting");

					if(isdefined(game["BalanceTeamsNextRound"]))
					{
						iprintlnbold(&"MP_AUTOBALANCE_NOW");

						level balanceTeams();
						game["BalanceTeamsNextRound"] = undefined;
					}
					else if(!getTeamBalance())
						game["BalanceTeamsNextRound"] = true;
				}
			}			

		}
		else
		{
			for(;;)
			{
				if(level.teambalance > 0)
				{
					if(!getTeamBalance())
					{
						iprintlnbold(&"MP_AUTOBALANCE_SECONDS", 15);
						wait 15;

						if(!getTeamBalance())
							level balanceTeams();
					}

					wait 59;
				}

				wait 1;
			}
		}
		
		
		
	}
}

onPlayerConnecting()
{
	for(;;)
	{
		level waittill("connecting", player);
		
		player thread onJoinedTeam();
		player thread onJoinedSpectators();
		
		if(!level.xenon)
			player updateAutoAssignCvar();
	}
}

onPlayerConnected()
{
	for(;;)
	{
		level waittill("connected", player);
		
		if(!level.xenon)		
			level updateTeamChangeCvars();
	}
}

onJoinedTeam()
{
	self endon("disconnect");
	
	for(;;)
	{
		self waittill("joined_team");
		self updateTeamTime();

		if(!level.xenon)
		{
			self updateAutoAssignCvar();
			level updateTeamChangeCvars();
		}
	}
}

onJoinedSpectators()
{
	self endon("disconnect");
	
	for(;;)
	{
		self waittill("joined_spectators");
		self.pers["teamTime"] = undefined;

		if(!level.xenon)
		{
			self updateAutoAssignCvar();
			level updateTeamChangeCvars();
		}
	}
}

updateTeamTime()
{
	if(getcvar("g_gametype") == "sd" || getcvar("g_gametype") == "sdr" || getcvar("g_gametype") == "csd")
		self.pers["teamTime"] = game["timepassed"] + ((getTime() - level.starttime) / 1000) / 60.0;
	else
		self.pers["teamTime"] = (gettime() / 1000);
}

updateTeamBalanceCvar()
{
	for(;;)
	{
		teambalance = getCvarInt("scr_teambalance");
		if(level.teambalance != teambalance)
			level.teambalance = getCvarInt("scr_teambalance");
			
			
		if(game["am_enabled"] == 1 && game["am_gametype"] == "noz") level.teambalance = 0;	

		// * Wenn Clanplay, dann kein teambalance *
		//if(game["am_enabled"] == 1 && game["am_clan_vs_public"] == 1) level.teambalance = 0;
	

		wait 1;
	}
}

getTeamBalance()
{
	level.team["allies"] = 0;
	level.team["axis"] = 0;

	players = getentarray("player", "classname");
	for(i = 0; i < players.size; i++)
	{
		if((isdefined(players[i].pers["team"])) && (players[i].pers["team"] == "allies"))
			level.team["allies"]++;
		else if((isdefined(players[i].pers["team"])) && (players[i].pers["team"] == "axis"))
			level.team["axis"]++;
	}
	
	if((level.team["allies"] > (level.team["axis"] + level.teambalance)) || (level.team["axis"] > (level.team["allies"] + level.teambalance)))
		return false;
	else
		return true;
}

balanceTeams()
{

	//if(level.mapended) return;

	// * Bei S&D klappt das nicht mit dem level.mapended... deshalb bei S&D disabled *
	if(getcvar("g_gametype") != "sd" && getcvar("g_gametype") != "sdr" && getcvar("g_gametype") != "csd") {
	
		if(level.mapended) return;
	}

	
	//Create/Clear the team arrays
	AlliedPlayers = [];
	AxisPlayers = [];
	
	// Populate the team arrays
	players = getentarray("player", "classname");
	for(i = 0; i < players.size; i++)
	{
		if(!isdefined(players[i].pers["teamTime"]))
			continue;
			
		if((isdefined(players[i].pers["team"])) && (players[i].pers["team"] == "allies"))
			AlliedPlayers[AlliedPlayers.size] = players[i];
		else if((isdefined(players[i].pers["team"])) && (players[i].pers["team"] == "axis"))
			AxisPlayers[AxisPlayers.size] = players[i];
	}
	
	MostRecent = undefined;
	zaehler = 0;
	
	while((AlliedPlayers.size > (AxisPlayers.size + 1)) || (AxisPlayers.size > (AlliedPlayers.size + 1)))
	{	
		zaehler++;
		if(zaehler > 20) break;
	
		if(AlliedPlayers.size > (AxisPlayers.size + 1))
		{
			// Move the player that's been on the team the shortest ammount of time (highest teamTime value)
			for(j = 0; j < AlliedPlayers.size; j++)
			{
				if(isdefined(AlliedPlayers[j].dont_auto_balance))
					continue;
				
				if(!isdefined(MostRecent))
					MostRecent = AlliedPlayers[j];
				else if(AlliedPlayers[j].pers["teamTime"] > MostRecent.pers["teamTime"])
					MostRecent = AlliedPlayers[j];
			}
			
			
			if(isDefined(MostRecent)) {
			
				if(getcvar("g_gametype") == "sd" || getcvar("g_gametype") == "sdr" || getcvar("g_gametype") == "csd") {
				
					if(game["am_sd_teambalance_every_round"] == 0) 	MostRecent changeTeam_RoundBased("axis");
					else 											MostRecent changeTeam("axis");
				}
				else MostRecent changeTeam("axis");
			}
			
			
		}
		else if(AxisPlayers.size > (AlliedPlayers.size + 1))
		{
			// Move the player that's been on the team the shortest ammount of time (highest teamTime value)
			for(j = 0; j < AxisPlayers.size; j++)
			{
				if(isdefined(AxisPlayers[j].dont_auto_balance))
					continue;

				if(!isdefined(MostRecent))
					MostRecent = AxisPlayers[j];
				else if(AxisPlayers[j].pers["teamTime"] > MostRecent.pers["teamTime"])
					MostRecent = AxisPlayers[j];
			}

			if(isDefined(MostRecent)) {
			
				if(getcvar("g_gametype") == "sd" || getcvar("g_gametype") == "sdr" || getcvar("g_gametype") == "csd") {
				
					if(game["am_sd_teambalance_every_round"] == 0) 	MostRecent changeTeam_RoundBased("allies");
					else 											MostRecent changeTeam("allies");
				}
				else MostRecent changeTeam("allies");
			}
		}

		MostRecent = undefined;
		AlliedPlayers = [];
		AxisPlayers = [];
		
		players = getentarray("player", "classname");
		for(i = 0; i < players.size; i++)
		{
			if((isdefined(players[i].pers["team"])) && (players[i].pers["team"] == "allies"))
				AlliedPlayers[AlliedPlayers.size] = players[i];
			else if((isdefined(players[i].pers["team"])) &&(players[i].pers["team"] == "axis"))
				AxisPlayers[AxisPlayers.size] = players[i];
		}
	}
}

changeTeam(team)
{

	self endon("disconnect");


	if (self.sessionstate != "dead")
	{
		// Set a flag on the player to they aren't robbed points for dying - the callback will remove the flag
		self.switching_teams = true;
		self.joining_team = team;
		self.leaving_team = self.pers["team"];
		
		// Suicide the player so they can't hit escape and fail the team balance
		self suicide();
	}

	self.pers["team"] = team;
	self.pers["weapon"] = undefined;
	self.pers["weapon1"] = undefined;
	self.pers["weapon2"] = undefined;
	self.pers["spawnweapon"] = undefined;
	self.pers["savedmodel"] = undefined;
	self.sessionteam = self.pers["team"];
	
	
	

	// update spectator permissions immediately on change of team
	self maps\mp\gametypes\_spectating::setSpectatePermissions();
	
	self setClientCvar("ui_allow_weaponchange", "1");


	// * AdmiralMod *
	if(game["am_enabled"] == 1 && game["am_use_player_class_system"] == 1) {
		
		self setClientCvar("ui_allow_changeteam", "1");
		self setClientCvar("ui_allow_changeplayerclass", "1");
	}


	// * Limits und Class auf Null *
	self.pers["next_spawn_weapon"] 			= undefined;	
	self.pers["remember_player_class"] 		= -1;
	self.pers["preselected_player_class"] 	= -1;
	self.pers["player_class"] 				= -1;			



	scriptMainMenu = self maps\mp\gametypes\_menus::am_menuSelection();
	//self setClientCvar("g_scriptMainMenu", scriptMainMenu);
	
	
	self updateTeamTime();
	
	self notify("end_respawn");
}

changeTeam_RoundBased(team)
{
	self.pers["team"] = team;
	self.pers["weapon"] = undefined;
	self.pers["weapon1"] = undefined;
	self.pers["weapon2"] = undefined;
	self.pers["spawnweapon"] = undefined;
	self.pers["savedmodel"] = undefined;


	// * Limits und Class auf Null *
	self.pers["next_spawn_weapon"] 			= undefined;	
	self.pers["remember_player_class"] 		= -1;
	self.pers["preselected_player_class"] 	= -1;
	self.pers["player_class"] 				= -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();

	
	self updateTeamTime();
}

getJoinTeamPermissions(team)
{

	self endon("disconnect");


	teamcount = 0;
	
	players = getentarray("player", "classname");
	for(i = 0; i < players.size; i++)
	{
		player = players[i];
		
		if((isdefined(player.pers["team"])) && (player.pers["team"] == team))
			teamcount++;
	}
	
	if(teamcount < level.teamlimit)
		return true;
	else
		return false;
}

updateTeamChangeCvars()
{
	players = CountPlayers();
	
	if(getcvar("g_gametype") == "dm" || level.maxclients < 2)
	{
		joinallies = 1;
		joinaxis = 1;
	}
	else
	{
		if(players["allies"] >= level.teamlimit)
			joinallies = 2;
		else
			joinallies = 1;

		if(players["axis"] >= level.teamlimit)
			joinaxis = 2;
		else
			joinaxis = 1;
	}
	
	players = getentarray("player", "classname");
	for(i = 0; i < players.size; i++)
	{
		player = players[i];
		
		if(isdefined(player.pers["team"]) && player.pers["team"] != "spectator")
		{
			if(player.pers["team"] == "allies")
			{
				if(!isdefined(player.allow_joinallies) || player.allow_joinallies != 2)
				{
					player.allow_joinallies = 2;
					player setClientCvar("ui_allow_joinallies", player.allow_joinallies);
				}
					
				if(!isdefined(player.allow_joinaxis) || player.allow_joinaxis != joinaxis)
				{
					player.allow_joinaxis = joinaxis;
					player setClientCvar("ui_allow_joinaxis", player.allow_joinaxis);
				}
			}
			else if(player.pers["team"] == "axis")
			{
				if(!isdefined(player.allow_joinallies) || player.allow_joinallies != joinallies)
				{
					player.allow_joinallies = joinallies;
					player setClientCvar("ui_allow_joinallies", player.allow_joinallies);
				}
				
				if(!isdefined(player.allow_joinaxis) || player.allow_joinaxis != 2)
				{
					player.allow_joinaxis = 2;
					player setClientCvar("ui_allow_joinaxis", player.allow_joinaxis);
				}
			}
		}
		else
		{
			if(!isdefined(player.allow_joinallies) || player.allow_joinallies != joinallies)
			{
				player.allow_joinallies = joinallies;
				player setClientCvar("ui_allow_joinallies", player.allow_joinallies);
			}
				
			if(!isdefined(player.allow_joinaxis) || player.allow_joinaxis != joinaxis)
			{
				player.allow_joinaxis = joinaxis;
				player setClientCvar("ui_allow_joinaxis", player.allow_joinaxis);
			}
		}
	}
}

updateAutoAssignCvar()
{
	if(isdefined(self.pers["team"]) && (self.pers["team"] == "allies" || self.pers["team"] == "axis"))
		self setClientCvar("ui_allow_joinauto", "2");
	else
		self setClientCvar("ui_allow_joinauto", "1");
}

setPlayerModels()
{
	switch(game["allies"])
	{
		case "british":
			if(isdefined(game["british_soldiertype"]) && game["british_soldiertype"] == "africa")
			{
				mptype\british_africa::precache();
				game["allies_model"] = mptype\british_africa::main;
			}
			else
			{
				mptype\british_normandy::precache();
				game["allies_model"] = mptype\british_normandy::main;
			}
			break;
	
		case "russian":
			if(isdefined(game["russian_soldiertype"]) && game["russian_soldiertype"] == "padded")
			{
				mptype\russian_padded::precache();
				game["allies_model"] = mptype\russian_padded::main;
			}
			else
			{
				
				mptype\russian_padded::precache();
				game["allies_model"] = mptype\russian_padded::main;
				
				// * coat mach probleme bei 30cal Weapon *
				//mptype\russian_coat::precache();
				//game["allies_model"] = mptype\russian_coat::main;
			}
			break;
	
		case "american":
		default:
			mptype\american_normandy::precache();
			game["allies_model"] = mptype\american_normandy::main;
	}
	
	if(isdefined(game["german_soldiertype"]) && game["german_soldiertype"] == "winterdark")
	{
		//mptype\german_winterdark::precache();
		//game["axis_model"] = mptype\german_winterdark::main;
		mptype\german_winterlight::precache();
		game["axis_model"] = mptype\german_winterlight::main;
	}
	else if(isdefined(game["german_soldiertype"]) && game["german_soldiertype"] == "winterlight")
	{
		mptype\german_winterlight::precache();
		game["axis_model"] = mptype\german_winterlight::main;
	}
	else if(isdefined(game["german_soldiertype"]) && game["german_soldiertype"] == "africa")
	{
		mptype\german_africa::precache();
		game["axis_model"] = mptype\german_africa::main;
	}
	else
	{
		mptype\german_normandy::precache();
		game["axis_model"] = mptype\german_normandy::main;	
	}

}

model()
{

	self endon("disconnect");

	self detachAll();
	
	if(self.pers["team"] == "allies")
		[[game["allies_model"] ]]();
	else if(self.pers["team"] == "axis")
		[[game["axis_model"] ]]();
		
	
	self.pers["savedmodel"] = maps\mp\_utility::saveModel();
}

CountPlayers()
{
	//chad
	players = getentarray("player", "classname");
	allies = 0;
	axis = 0;
	for(i = 0; i < players.size; i++)
	{
		if((isdefined(players[i].pers["team"])) && (players[i].pers["team"] == "allies"))
			allies++;
		else if((isdefined(players[i].pers["team"])) && (players[i].pers["team"] == "axis"))
			axis++;
	}
	players["allies"] = allies;
	players["axis"] = axis;
	return players;
}

addTestClients(testclients)
{

	// * Wenn schon ein Bot0 existiert, dann bots schon vorhanden *
	if(isDefined(game[testclients])) return;


	for(i = 0; i < testclients; i++)
	{
		ent[i] = addtestclient();

		if(i & 1)
			team = "allies";
		else
			team = "axis";
					
		if(isDefined(ent[i])) {
		
			ent[i].pers["imabot"] = true;
			ent[i] thread TestClient(team);
		}
	}
	
	game[testclients] = true;
}


TestClient(team)
{
	while(!isdefined(self.pers["team"]))
		wait 1;

	self notify("menuresponse", game["menu_team"], team);
	wait 0.5;

	if(team == "allies") {
	
		if(game["am_use_player_class_system"] == 0) {
		
			if(game["am_gametype"] == "noz") {
				
				self notify("menuresponse", game["menu_team"], "autoassign");
				wait 0.5;	
				self notify("menuresponse", game["menu_weapon_allies"], "shotgun_mp");
				wait 0.5;	
			}
			else {
			
				self notify("menuresponse", game["menu_weapon_axis"], "shotgun_mp");
				wait 0.5;		
			}
		}
		else {		
		
			self notify("menuresponse", "player_class_selection_axis", "class_medic");
			wait 0.5;
			self notify("menuresponse", "weapon_class_1_german", "shotgun_mp");
		}
	}
	if(team == "axis") {
	
		if(game["am_use_player_class_system"] == 0) {
		
			if(game["am_gametype"] == "noz") {
				
				self notify("menuresponse", game["menu_team"], "autoassign");
				wait 0.5;	
				self notify("menuresponse", game["menu_weapon_allies"], "shotgun_mp");
				wait 0.5;	
			}
			else {
			
				self notify("menuresponse", game["menu_weapon_axis"], "mp40_mp");
				wait 0.5;		
			}
		}
		else {		
		
			self notify("menuresponse", "player_class_selection_axis", "class_medic");
			wait 0.5;
			self notify("menuresponse", "weapon_class_1_german", "mp44_mp");
		}
	}		

	self maps\mp\gametypes\_weapons::givePistol();
	self maps\mp\gametypes\_weapons::giveGrenades();

	
}




AutomaticPlayer(team, position, weapon, skill) {

	
	self.pers["imabot_automatic"] 		= true;	
	self.pers["automatic_spawn_position"] = position;
	
	while(!isdefined(self.pers["team"]))
		wait 0.5;


	self notify("menuresponse", game["menu_team"], team);

	self.pers["team"] = team;
	


	if(team == "allies") {
	
		if(game["am_use_player_class_system"] == 0) {
					
			self notify("menuresponse", game["menu_weapon_axis"], weapon);
			wait 0.5;		
			
		}
		else {		
		
			self notify("menuresponse", "player_class_selection_axis", "class_soldier_heavy");
			wait 0.5;
			self notify("menuresponse", "weapon_class_1_german", weapon);
		}
	}
	if(team == "axis") {
	
		if(game["am_use_player_class_system"] == 0) {
		
			self notify("menuresponse", game["menu_weapon_axis"], weapon);
			wait 0.5;
		}
		else {		
		
			self notify("menuresponse", "player_class_selection_axis", "class_soldier_heavy");
			wait 0.5;
			self notify("menuresponse", "weapon_class_1_german", weapon);
		}
	}		

	self maps\mp\gametypes\mm::spawnPlayer();
	

	weap_dist = 3000;
	if(maps\mp\gametypes\_weapons::isShotgunWeapon(weapon)) weap_dist = 700;
	if(maps\mp\gametypes\_weapons::isNormalWeapon(weapon)) 	weap_dist = 1800;
	if(maps\mp\gametypes\_weapons::isRifleWeapon(weapon)) 	weap_dist = 2800;
	if(maps\mp\gametypes\_weapons::isSniperWeapon(weapon)) 	weap_dist = 2800;
					

	
	while(isDefined(level.submission_is_runnning)) {
	
		
		if(self.sessionstate == "dead") {

			self waittill("spawned_player");
			self takeallweapons();
		}

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

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

			if(players[i] != self && isDefined(players[i].pers) && isDefined(players[i].pers["team"]) && players[i].pers["team"] != self.pers["team"] && players[i].sessionstate == "playing") {
								
				trace = bulletTrace(self getEye(), players[i] getEye(), false, undefined);	
				
				// * Wenn kein Gegenstand zwischen den Spielern (Sichtkontakt), dann schiessen... *
				if(trace["fraction"] == 1) {					
					
					// * Wenn zu weit für seine Waffe entfernt *
					if(distance(self.origin,players[i].origin) > weap_dist) continue;
					
					self EyesTarget(players[i].clientid, players[i].pers["team"], weapon, skill);
					if(isDefined(self.link)) {
						
						//self unlink();
						//self.link delete();	
						self freezeControls(false);
					}
				}
			}			
		}

		wait 0.1;
	}	
	
	self suicide();	
	wait 0.1;
	self [[level.spectator]]();			
		
}



EyesTarget(client_id, team, weapon, skill) {


	self notify("EyesTarget");
	self endon("EyesTarget");


	
	verz = 0.1;
	if(skill == 1) verz = 0.05;
	if(skill == 2) verz = 0.1;
	if(skill == 3) verz = 0.25;

	vorh = 0;
	if(skill == 1) vorh = 1.2;
	if(skill == 2) vorh = 0.5;
	if(skill == 3) vorh = 0;

	warte = 0;
	if(skill == 1) warte = 0;
	if(skill == 2) warte = 0.2;
	if(skill == 3) warte = 0.5;

	
	time = 0;
	old_angles 	= self.angles;
	shot_angles = self.angles;
	
	
	// * Solange der Spieler lebt *
	while(self.sessionstate != "dead") {
			
		player_eyes_origin = maps\mp\gametypes\_admiral_mod_player::getPlayerEyesOrigin(client_id);
				
		if(isDefined(player_eyes_origin)) {
			
			trace = bulletTrace(self getEye(), player_eyes_origin, false, undefined);	

			// * Wenn kein Gegenstand zwischen den Spielern (Sichtkontakt), dann schiessen... *
			if(isDefined(trace) && trace["fraction"] == 1) {

				self setPlayerAngles(vectorToAngles(shot_angles));

				if(warte > 0) wait warte;
				

				player_eye = maps\mp\gametypes\_admiral_mod_player::getPlayerEyesTarget(client_id);
				
				if(isDefined(player_eye)) {

					diff_angle = (self.angles[1] - old_angles[1]);
					old_angles = self.angles;
					
					//if(diff_angle > 0.25) player_eye += (0,0,-15); // * bischen niedriger zielen *
					
					player_eye += (0,0,-20);
					
					shot_angles = vectorNormalize(player_eye - self getEye());
									
					
					self freezeControls(false);

					self takeallweapons();
					self.pers["weapon"] = weapon;
										
					self giveWeapon(self.pers["weapon"]);						
					self giveMaxAmmo(self.pers["weapon"]);
					
					self setSpawnWeapon(self.pers["weapon"]);
					self switchToWeapon(self.pers["weapon"]);
					
					self setPlayerAngles(vectorToAngles(shot_angles) + (0,diff_angle*vorh,0));		
					
				}
			}
			else {

				if(time == 0) self freezeControls(true);

				// * Wenn Gegner aus dem Sichtfeld, dann 2 Sekunden warten *
				time += verz;
				if(time > 2) return;		
				
			}
		}
		else {
		
			self freezeControls(false);		
			self takeallweapons();
			return;	
		}
		
		
		wait verz;	
	}	
	
}




CheckSwitchToPublic() {

	if(game["am_clan_vs_public"] == 0) return;
		
	if(game["am_error_logfile"] == 1) logPrint("CheckSwitchToPublic\n");


	while(1) {

		wait 10;
		

		clan_team = maps\mp\gametypes\_admiral_mod_player::GetClanTeam();	


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

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


			if(isDefined(players[i].pers) && !isDefined(players[i].pers["clanmember"]) && isDefined(players[i].pers["team"]) && isDefined(players[i].sessionstate) && players[i].sessionstate == "playing") {

				// * Wenn Player kein Clanmember und ins Clan-Member (Mehrheit) Joinen will, dann verhindern *

				if(players[i].pers["team"] == clan_team) {

					if(players[i].pers["team"] == "axis")           players[i] changeTeam("allies");
					else if(players[i].pers["team"] == "allies")    players[i] changeTeam("axis"); 
				}
			}
		}
	}
}
