


// Bomben Typen:

// Typ 1 = Time-Bomb
// Typ 2 = Time-Smoke
// Typ 3 = Move-Bomb
// Typ 4 = Move-Smoke




CheckTNT() {

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


	self notify("CheckTNT");
	self endon("CheckTNT");

	self endon("spawned");
	self endon("disconnect");
	self endon("killed_player");


	self.tnt_ammo_count = game["am_class_tnt_max_bombs_packs"];
	
	self setClientCvar("am_ui_show_medic_weapon_ammo", "0");
	self.pers["am_ui_show_medic_weapon_ammo"] = 0;
	
	self setClientCvar("am_ui_show_artillery_weapon_ammo", "0");
	self.pers["am_ui_show_artillery_weapon_ammo"] = 0;
	
	self setClientCvar("am_ui_show_tnt_weapon_ammo", "1");
	self.pers["am_ui_show_tnt_weapon_ammo"] = 1;

	self setClientCvar("am_ui_tnt_weapon_ammo", self.tnt_ammo_count);
	self.pers["am_ui_tnt_weapon_ammo"] = self.tnt_ammo_count;
	

	// * Auf USE-Button + MELEE-Button + ATTACK-Button Druck warten *
	
	while(1) {
	
		wait 0.20;
			
		if(self.do_status != "none") continue;
		
	
		
		if(self meleeButtonPressed() && self.do_status == "none" && self.tnt_ammo_count > 0) {
	
			if(self.pers["team"] == "allies") 	bomb_count = level.bomb_on_the_map_allies;
			else								bomb_count = level.bomb_on_the_map_axis;
		
		
			// * Wenn schon so viele Bomben auf der Map, dann keine mehr zulassen *
			if(self.tnt_ammo_count > 0 && bomb_count >= game["am_class_tnt_max_bombs_packs_on_map"]) {

				self setClientCvar("am_ui_player_class_info_text", "@AM_MP_LNG_BOMB_TO_MANY_ON_MAP");
				self.pers["am_ui_player_class_info_text"] = "AM_MP_LNG_BOMB_TO_MANY_ON_MAP";
				wait 8;
				self setClientCvar("am_ui_player_class_info_text", " ");	
				self.pers["am_ui_player_class_info_text"] = " ";

				continue;
			}	
	
	
			
			trace = self maps\mp\gametypes\_admiral_mod_player::GetEyeTrace();
			angles = vectortoangles(vectornormalize(trace["normal"])) + (90,0,0);

			if(distance(trace["position"],self.origin + (0,0,40)) > 90) {
			
				//iprintln("Position zu weit entfernt");
			
				continue;
			}
			

			// * Wenn Body Melee, dann nicht planten *
			if(trace["fraction"] == 1.0 || trace["surfacetype"] == "default") {			
										
				continue;							
			}
			
			self.do_status = "plant_TNT_bomb";
		
				
			bomb = spawn("script_model", trace["position"]);
			bomb.angles = angles;			
			bomb setmodel("xmodel/mp_tntbomb");

			bomb.planter_client_id 				= self.clientid;
			bomb.planter_team	 				= self.pers["team"];
			
			bomb.typ			 				= 0;
			bomb.time			 				= 0;
			bomb.time_left		 				= 0;
			bomb.radius							= 0;
			
			bomb.menu_activate_selection 		= 0;
			bomb.menu_defuse_selection 			= 0;
			bomb.menu_defuse_others_selection 	= 0;
			bomb.menu_status_text 				= "@AM_MP_LNG_SELECTION";	
			
			if(isDefined(level.bomb_ids)) 		level.bomb_ids++;			
			else 								level.bomb_ids = 0;
			bomb.id								= level.bomb_ids;
			
			
			bomb thread BombMenuThink();


			if(self.tnt_ammo_count > 0) {
			
				self.tnt_ammo_count--;
				self setClientCvar("am_ui_tnt_weapon_ammo", self.tnt_ammo_count);
				self.pers["am_ui_tnt_weapon_ammo"] = self.tnt_ammo_count;
			}

			if(bomb.planter_team == "allies") 	level.bomb_on_the_map_allies++;
			if(bomb.planter_team == "axis")		level.bomb_on_the_map_axis++;
			
			
			self.show_tnt_icon = true;
			thread maps\mp\gametypes\_tnticons::addTNTicon(bomb, bomb.id, bomb.planter_team);

			
			// * Warten bis Buttons losgelassen * 
			while(self meleeButtonPressed()) {
			
				wait 0.5;
			}

			
			if(self.do_status == "plant_TNT_bomb") self.do_status = "none";
			
		}	
	}
}



BombMenuThink() {


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


	team   = "all";
	radius = 100;
	

	activate_time = 0;
	

	while(isDefined(self)) {
	
	
		clientid = maps\mp\gametypes\_admiral_mod_player::checkPlayerInRange(self.origin,team,radius);

		if(isDefined(clientid) && clientid > -1) {
		
			if(maps\mp\gametypes\_admiral_mod_player::checkPlayerLookingToObject(clientid,self.origin,0.94)) {
			
				player_origin = maps\mp\gametypes\_admiral_mod_player::getPlayerOrigin(clientid);

				if(isDefined(self) && isDefined(player_origin)) {
				
					// * Damit das Radius Damage nicht durch den Bombenkörper behindert wird *
					vorne = maps\mp\_utility::vectorScale(anglesToForward(self.angles + (-90,0,0)), 10);

					trace = bulletTrace(player_origin + (0,0,50), self.origin + vorne, false, undefined);	
				
					// * Wenn kein Gegenstand zwischen (Sichtkontakt), dann Menu... *
					if(trace["fraction"] == 1) {
					
						if(isDefined(player_origin) && isDefined(self)) {
														
							players = getentarray("player", "classname");
						
							for(i=0;i<players.size;i++) {
						
								if(players[i].sessionstate == "playing" && players[i].clientid == clientid) {
						
									players[i] thread BombShowMenu(self, self.origin, radius);	
								}
							}							
						}
					}
				}		
			}
		}

		
		// * Wenn nach 20 Sekunden Bombe nicht aktiviert wurde, dann löschen * 
		if(isDefined(self) && self.typ == 0) {
		
			activate_time++;
		
			if(activate_time > 20) {
		
				self DeleteBomb();
				break;
			}
		}
		


		wait 1;
	}
}


BombShowMenu(bomb, bomb_origin, radius) {

	self endon("spawned");
	self endon("disconnect");
	self endon("killed_player");
	
	
	if(self.do_status != "none") return;
	self.do_status = "bomb_menu";


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

	
	origin = self.origin;
	
	
	self disableWeapon();
	self setClientCvar("am_ui_show_bomb_menu", "1");
	self.pers["am_ui_show_bomb_menu"] = 1;

	wait_art = 0;

	if(isDefined(bomb)) {
	
		self setClientCvar("am_ui_bomb_status", bomb.menu_status_text);	
		self.pers["am_ui_bomb_status"] = bomb.menu_status_text;
		
		// * Wenn die Bombe noch nicht scharf *
		if(bomb.typ == 0) 	{
		
			self thread BombActivateSelection(bomb);
			wait_art = 1;
		}
		else {
		
			// * Wenn die Bombe bereits scharf *
								
			if(isDefined(bomb) && (bomb.typ == 1 || bomb.typ == 2) ) {
				
				self maps\mp\gametypes\_admiral_mod_hud::deletePlayerHudElementByName("bomb_timer");
				self maps\mp\gametypes\_admiral_mod_hud::createPlayerClockHudElement("bomb_timer", 320,320,"center","middle","fullscreen","fullscreen",45,45,true,50,1,bomb.time,bomb.time_left);
			}

		
			if(isDefined(bomb) && bomb.typ > 0 && isDefined(bomb.planter_client_id) && self.clientid == bomb.planter_client_id && bomb.planter_team == self.pers["team"] && self.pers["player_class"] == 5) {
			
				// * Der Bombenleger selbst * 
				self thread BombDefuseSelection(bomb);
				wait_art = 2;
			}
			else if(isDefined(bomb) && bomb.typ > 0 && self.pers["player_class"] == 5 && bomb.planter_team != self.pers["team"]) {
			
				// * Ein anderer Bombenleger, und nicht vom selben Team *
				self thread BombDefuseSelection(bomb);
				wait_art = 3;							
			}
			else {
			
				if(isDefined(bomb) && bomb.typ > 0 && bomb.planter_team != self.pers["team"]) {
			
					// * kein Bombenleger, und nicht vom selben Team *
					self thread BombDefuseSelection(bomb);
					wait_art = 4;	
				}
			}			
		}
	}
	
	
	while(isDefined(bomb) && (distance(origin,self.origin) < radius && maps\mp\gametypes\_admiral_mod_player::checkPlayerLookingToObject(self.clientid,bomb_origin,0.94) || (isDefined(bomb.typ) && bomb.typ > 0 && !isDefined(bomb.isThinking))  )) {
			
		
		// * Warten auf aktivieren *
		if(wait_art == 1 && isDefined(bomb) && bomb.menu_activate_selection != 0) {
			
			wait 0.5;	
			
			if(isDefined(bomb)) bomb thread BombThink();
				
			break;		
		}
		
		// * Warten auf Entschärfen (Bombenleger selbst) *
		if(wait_art == 2 && isDefined(bomb) && bomb.menu_defuse_selection != 0) {

			bomb hide();

			wait 0.5;	
			
			if(isDefined(bomb)) bomb DeleteBomb();
			
			
			self.tnt_ammo_count++;
			self setClientCvar("am_ui_tnt_weapon_ammo", self.tnt_ammo_count);
			self.pers["am_ui_tnt_weapon_ammo"] = self.tnt_ammo_count;
			
			break;		
		}
		
		// * Warten auf Entschärfen (ein anderer Bombenleger) *
		if(wait_art == 3 && isDefined(bomb) && bomb.menu_defuse_others_selection != 0) {

					
			random_wire = RandomInt(100);
			
			// * Wenn falscher Draht durchgeschnitten *
			if(random_wire > 50) {
			
				bomb hide();
				
			
				// * Explosion *

				if(isDefined(bomb) && (bomb.typ == 1 || bomb.typ == 3) ) {

					bomb stoploopsound();
					bomb BombExplosion();					
				}
				else {

					if(isDefined(bomb)) bomb playSound("am_bomb_explosion_smoke");
					if(isDefined(bomb)) playfx(game["bomb_explosion_smoke"],bomb.origin);			
				}			
			
				
				wait 0.5;	
				if(isDefined(bomb)) bomb DeleteBomb();			
				break;	
			}			
			else {
						
				wait 0.5;	

				self thread maps\mp\gametypes\_admiral_mod_sounds::DoTalking(self maps\mp\gametypes\_admiral_mod_player::GetTeamKurz() + "_mp_stm_areasecure");								
				self sayAll(&"QUICKMESSAGE_AREA_SECURE");
			
			
				self.tnt_ammo_count++;
				self setClientCvar("am_ui_tnt_weapon_ammo", self.tnt_ammo_count);
				self.pers["am_ui_tnt_weapon_ammo"] = self.tnt_ammo_count;
			
				if(isDefined(bomb)) bomb DeleteBomb();			
				break;		
			}
		}
		
		// * Warten auf Entschärfen (kein Bombenleger) *
		if(wait_art == 4 && isDefined(bomb) && bomb.menu_defuse_others_selection != 0) {
			
			
			random_wire = RandomInt(100);
					
			// * Wenn falscher Draht durchgeschnitten *
			if(random_wire > 50) {

				bomb hide();
			
				// * Explosion *

				if(isDefined(bomb) && (bomb.typ == 1 || bomb.typ == 3) ) {

					bomb stoploopsound();
					bomb BombExplosion();					
				}
				else {

					if(isDefined(bomb)) bomb playSound("am_bomb_explosion_smoke");
					if(isDefined(bomb)) playfx(game["bomb_explosion_smoke"],bomb.origin);			
				}
				
				wait 0.5;
				if(isDefined(bomb)) bomb DeleteBomb();			
				break;					
			}
			else {

				wait 0.5;	
	
				self thread maps\mp\gametypes\_admiral_mod_sounds::DoTalking(self maps\mp\gametypes\_admiral_mod_player::GetTeamKurz() + "_mp_stm_areasecure");								
				self sayAll(&"QUICKMESSAGE_AREA_SECURE");
					
				// 	* Bombe hier nicht löschen, sie ist nur zerstört *
				if(isDefined(bomb)) {
					
					bomb.typ 			  = -1; // Bombe unbrauchbar
					bomb.menu_status_text = "^9Bomb is unusable";
					bomb stoploopsound();
								
					
					wait 1;		
					if(isDefined(bomb)) bomb DeleteBomb();
															
					break;
					
				}
				break;		
			}
		}
			
	
		wait 0.5;	
	}
	
	
	
	
	self notify("bomb_menu_end");
	
	
	self maps\mp\gametypes\_admiral_mod_hud::deletePlayerHudElementByName("bomb_timer");
	
	self setClientCvar("am_ui_show_bomb_menu", "0");
	self.pers["am_ui_show_bomb_menu"] = 0;
	
	self setClientCvar("am_ui_show_bomb_function_selection", "0");
	self.pers["am_ui_show_bomb_function_selection"] = 0;
	
	self setClientCvar("am_ui_show_bomb_time_selection", "0");
	self.pers["am_ui_show_bomb_time_selection"] = 0;
	
	self setClientCvar("am_ui_show_bomb_range_selection", "0");
	self.pers["am_ui_show_bomb_range_selection"] = 0;

	self setClientCvar("am_ui_show_bomb_defuse_selection", "0");
	self.pers["am_ui_show_bomb_defuse_selection"] = 0;
	
	self setClientCvar("am_ui_show_bomb_defuse_others_selection", "0");
	self.pers["am_ui_show_bomb_defuse_others_selection"] = 0;
	
	self enableWeapon();	
	if(self.do_status == "bomb_menu") self.do_status = "none";
}



BombActivateSelection(bomb) {


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


	self endon("spawned");
	self endon("disconnect");
	self endon("killed_player");


	self endon("bomb_menu_end");


	menu_selection = 1;

		

	self setClientCvar("am_ui_show_bomb_function_selection", menu_selection);
	self.pers["am_ui_show_bomb_function_selection"] = menu_selection;

	while(isDefined(bomb)) {
			
		wait 0.1;	
		if(self useButtonPressed()) {
		
			menu_selection++;
			if(menu_selection > 4) menu_selection = 1;
			self setClientCvar("am_ui_show_bomb_function_selection", menu_selection);
			self.pers["am_ui_show_bomb_function_selection"] = menu_selection;
		
			while(self useButtonPressed()) wait 0.1;
		}		
		else if(self attackButtonPressed()) {
		
			while(self attackButtonPressed()) wait 0.1;
			
			
			if(isDefined(bomb) && (menu_selection == 1 || menu_selection == 2) ) BombTimeSelection(bomb,menu_selection);
			if(isDefined(bomb) && (menu_selection == 3 || menu_selection == 4) ) BombRangeSelection(bomb,menu_selection);
			
						
		}		
	}
}



BombTimeSelection(bomb,menu_selection) {

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


	self endon("spawned");
	self endon("disconnect");
	self endon("killed_player");


	self endon("bomb_menu_end");
	
	
	time_selection = 1;
	status_text = "";

	self setClientCvar("am_ui_show_bomb_function_selection", "0");	
	self.pers["am_ui_show_bomb_function_selection"] = menu_selection;
	
	self setClientCvar("am_ui_show_bomb_time_selection", time_selection);
	self.pers["am_ui_show_bomb_time_selection"] = time_selection;

	while(isDefined(bomb)) {
			
		wait 0.1;	
		if(self useButtonPressed()) {
		
			time_selection++;
			if(time_selection > 4) time_selection = 1;
			self setClientCvar("am_ui_show_bomb_time_selection", time_selection);
			self.pers["am_ui_show_bomb_time_selection"] = time_selection;
		
			while(self useButtonPressed()) wait 0.1;
		}		
		else if(self attackButtonPressed()) {
		
			if(isDefined(bomb)) {
			
				if(time_selection == 1) bomb.time = 15;
				if(time_selection == 2) bomb.time = 30;
				if(time_selection == 3) bomb.time = 60;
				if(time_selection == 4) bomb.time = 120;
				
				bomb.time_left = bomb.time;
			
				bomb.typ 						= menu_selection;
				bomb.menu_activate_selection 	= menu_selection;				
			}
			
			if(menu_selection == 1) status_text = "@AM_MP_LNG_SELECTION_TIME_BOMB";
			if(menu_selection == 2) status_text = "@AM_MP_LNG_SELECTION_TIME_SMOKE";
			if(menu_selection == 3) status_text = "@AM_MP_LNG_SELECTION_MOVEMENT_BOMB";
			if(menu_selection == 4) status_text = "@AM_MP_LNG_SELECTION_MOVEMENT_SMOKE";
		
			self setClientCvar("am_ui_bomb_status", status_text);
			self.pers["am_ui_bomb_status"] = status_text;
			
			if(isDefined(bomb)) bomb.menu_status_text = status_text;
		
			if(isDefined(bomb)) bomb playLoopSound("am_bomb_tick");
		
			while(self attackButtonPressed()) wait 0.1;
		}		
	}
	
}


BombRangeSelection(bomb,menu_selection) {

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

	
	self endon("spawned");
	self endon("disconnect");
	self endon("killed_player");


	self endon("bomb_menu_end");
	
	
	range_selection = 1;
	status_text = "";


	self setClientCvar("am_ui_show_bomb_function_selection", "0");
	self.pers["am_ui_show_bomb_function_selection"] = 0;
	
	self setClientCvar("am_ui_show_bomb_range_selection", range_selection);
	self.pers["am_ui_show_bomb_range_selection"] = range_selection;

	while(isDefined(bomb)) {
			
		wait 0.1;	
		if(self useButtonPressed()) {
		
			range_selection++;
			if(range_selection > 4) range_selection = 1;
			self setClientCvar("am_ui_show_bomb_range_selection", range_selection);
			self.pers["am_ui_show_bomb_range_selection"] = range_selection;
		
			while(self useButtonPressed()) wait 0.1;
		}		
		else if(self attackButtonPressed()) {
		
			if(isDefined(bomb)) {
						
				if(range_selection == 1) bomb.range = 2;
				if(range_selection == 2) bomb.range = 5;
				if(range_selection == 3) bomb.range = 10;
				if(range_selection == 4) bomb.range = 15;

				bomb.typ 						= menu_selection;
				bomb.menu_activate_selection 	= menu_selection;				
			}
			
			if(menu_selection == 1) status_text = "^1Time Bomb";
			if(menu_selection == 2) status_text = "^1Time Smoke";
			if(menu_selection == 3) status_text = "^1Movement Bomb";
			if(menu_selection == 4) status_text = "^1Movement Smoke";
		
			self setClientCvar("am_ui_bomb_status", status_text);
			self.pers["am_ui_bomb_status"] = status_text;
			
			if(isDefined(bomb)) bomb.menu_status_text = status_text;
			
			self thread ShowBombRangeText();
		
			while(self attackButtonPressed()) wait 0.1;			
		}		
	}	
}


ShowBombRangeText() {

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


	self endon("spawned");
	self endon("disconnect");
	self endon("killed_player");

	self iprintlnbold(&"AM_MP_WEAPON_TNT_READY_IN_SECONDS");	


	self setClientCvar("am_ui_player_class_info_text", "@AM_MP_LNG_BOMB_DEFUSE_HINT");
	self.pers["am_ui_player_class_info_text"] = "AM_MP_LNG_BOMB_DEFUSE_HINT";
	wait 8;
	self setClientCvar("am_ui_player_class_info_text", " ");	
	self.pers["am_ui_player_class_info_text"] = " ";

}




BombDefuseSelection(bomb) {


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


	self endon("spawned");
	self endon("disconnect");
	self endon("killed_player");


	self endon("bomb_menu_end");


	// * Status-Text *
	
	// * Wenn der Spieler der Bombenleger ist *
	if(isDefined(bomb) && isDefined(bomb.planter_client_id) && self.clientid == bomb.planter_client_id && bomb.planter_team == self.pers["team"] && self.pers["player_class"] == 5) {
	
	
		// * Bomben-Status anzeigen (Time) * 
		
		self setClientCvar("am_ui_show_bomb_defuse_selection", "1");
		self.pers["am_ui_show_bomb_defuse_selection"] = 1;
		
		while(isDefined(bomb)) {
		
			wait 0.1;		
			if(self attackButtonPressed()) {
		
				if(isDefined(bomb)) bomb.menu_defuse_selection = 1;					
								
				status_text = "@AM_MP_LNG_BOMB_DEFUSED"; 
				
				self setClientCvar("am_ui_bomb_status", status_text);
				self.pers["am_ui_bomb_status"] = status_text;
				
				if(isDefined(bomb)) bomb.menu_status_text = status_text;
						
				while(self attackButtonPressed()) wait 0.1;
			}						
		}
		
	}
	// * Wenn der Spieler nicht der Planter ist, aber selbst ein Bombenleger *
	else if(isDefined(bomb) && self.pers["player_class"] == 5) {
			

		menu_selection = 1;

		status_text = "";


		self setClientCvar("am_ui_show_bomb_defuse_others_selection", "1");
		self.pers["am_ui_show_bomb_defuse_others_selection"] = 1;

		while(isDefined(bomb)) {

			wait 0.1;	
			if(self useButtonPressed()) {

				menu_selection++;
				if(menu_selection > 2) menu_selection = 1;
				self setClientCvar("am_ui_show_bomb_defuse_others_selection", menu_selection);
				self.pers["am_ui_show_bomb_defuse_others_selection"] = menu_selection;

				while(self useButtonPressed()) wait 0.1;
			}		
			else if(self attackButtonPressed()) {

				if(menu_selection == 1) status_text = "@AM_MP_LNG_RED_WIRE";
				if(menu_selection == 2) status_text = "@AM_MP_LNG_BLUE_WIRE";

				self setClientCvar("am_ui_bomb_status", status_text);
				self.pers["am_ui_bomb_status"] = status_text;
				
				if(isDefined(bomb)) bomb.menu_status_text = status_text;

				wait 0.5; // Spannung erzeugen

				//while(self attackButtonPressed()) wait 0.1;
								
				if(isDefined(bomb)) bomb.menu_defuse_others_selection = menu_selection;
			}		
		}
	}	
	// * Wenn der Spieler kein Bombenleger ist, dann bleibt die Bombe bei deaktivierung, ist aber zerstört *
	else {
			

		menu_selection = 1;

		status_text = "";


		self setClientCvar("am_ui_show_bomb_defuse_others_selection", "1");
		self.pers["am_ui_show_bomb_defuse_others_selection"] = 1;

		while(isDefined(bomb)) {

			wait 0.1;	
			if(self useButtonPressed()) {

				menu_selection++;
				if(menu_selection > 2) menu_selection = 1;
				self setClientCvar("am_ui_show_bomb_defuse_others_selection", menu_selection);
				self.pers["am_ui_show_bomb_defuse_others_selection"] = menu_selection;

				while(self useButtonPressed()) wait 0.1;
			}		
			else if(self attackButtonPressed()) {

				if(menu_selection == 1) status_text = "^1Red Wire";
				if(menu_selection == 2) status_text = "^4Blue Wire";

				self setClientCvar("am_ui_bomb_status", status_text);
				self.pers["am_ui_bomb_status"] = status_text;
				
				if(isDefined(bomb)) bomb.menu_status_text = status_text;

				wait 0.5; // Spannung erzeugen

				//while(self attackButtonPressed()) wait 0.1;
								
				if(isDefined(bomb)) bomb.menu_defuse_others_selection = menu_selection;									
			}		
		}
	}
}


// Bomben Typen:

// Typ 1 = Time-Bomb
// Typ 2 = Time-Smoke
// Typ 3 = Move-Bomb
// Typ 4 = Move-Smoke



BombThink() {


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


	self.isThinking = true;
	

	if(isDefined(self) && (self.typ == 1 || self.typ == 2)) {

		while(isDefined(self)) {
	
			// * Timer herunter zählen *
			self.time_left--;
			
			// * Wenn Zeit abgelaufen, dann explosion * 
			if(self.time_left < 1) {
			
				wait 2;	
			
				if(isDefined(self) && self.typ == 1) {
				
					if(isDefined(self)) self hide();
			
					if(isDefined(self)) self stoploopsound();
					if(isDefined(self)) self BombExplosion();
					
					wait 0.5;	
					if(isDefined(self)) self DeleteBomb();	
					break;	
				}
				else if(isDefined(self) && self.typ == 2) {

					if(isDefined(self)) self hide();

					if(isDefined(self)) self playSound("am_bomb_explosion_smoke");
					if(isDefined(self)) playfx(game["bomb_explosion_smoke"],self.origin);	

					wait 0.5;	
					if(isDefined(self)) self DeleteBomb();	
					break;	
				}
			}
	
			wait 1;	
		}
	}
	else if(isDefined(self) && (self.typ == 3 || self.typ == 4)) {
		
		team = "all";
		
		if(game["am_gametype"] != "dm" && game["am_class_tnt_ignore_plant_team"] == 1 && isDefined(self) && isDefined(self.planter_team)) {
		
			team = "allies";
		
			if(self.planter_team == "allies") team = "axis";			
		}
		
		
		radius = self.range * 50;


		wait 8; // * 8 Sekunden bis zur Aktivierung warten *

	
		while(isDefined(self)) {


			clientid = maps\mp\gametypes\_admiral_mod_player::checkPlayerInRange(self.origin,team,radius);

			if(isDefined(clientid) && clientid > -1) {
				
				player_origin = maps\mp\gametypes\_admiral_mod_player::getPlayerOrigin(clientid);
				player_stance = maps\mp\gametypes\_admiral_mod_player::getPlayerStance(clientid);
				
				if(isDefined(player_origin) && isDefined(player_stance) && player_stance == 2 && isDefined(self)) {
					
					// * Damit das Radius Damage nicht durch den Bombenkörper behindert wird *
					vorne = maps\mp\_utility::vectorScale(anglesToForward(self.angles + (-90,0,0)), 10);

					trace = bulletTrace(player_origin + (0,0,50), self.origin + vorne, false, undefined);	
				
					// * Wenn kein Gegenstand zwischen (Sichtkontakt), dann Explosion... *
					if(trace["fraction"] == 1 && isDefined(self)) {
					
						if(self.typ == 3) {
						
							if(isDefined(self)) self BombExplosion();
							
							wait 0.5;	
							if(isDefined(self)) self DeleteBomb();	
							break;					
						}
						else if(self.typ == 4) {
							
							if(isDefined(self)) self hide();
						
							if(isDefined(self)) self playSound("am_bomb_explosion_smoke");
							if(isDefined(self)) playfx(game["bomb_explosion_smoke"],self.origin);	
							
							wait 0.5;								
							if(isDefined(self)) self DeleteBomb();	
							break;	
						}
					}				
				}
			}

			wait 0.5;
		}	
	}
}


DeleteBomb() {

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


	if(isDefined(self)) {
	
		self.show_tnt_icon = false;
		maps\mp\gametypes\_tnticons::removeTNTIcon(self.id);

	
		if(self.planter_team == "allies" && level.bomb_on_the_map_allies > 0) 	level.bomb_on_the_map_allies--;
		if(self.planter_team == "axis" && level.bomb_on_the_map_axis > 0) 		level.bomb_on_the_map_axis--;
		 	
		self delete();	
		
	}	
}


BombExplosion() {

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


	if(isDefined(self)) self hide();

	// * Damit das Radius Damage nicht durch den Bombenkörper behindert wird *
	vorne = maps\mp\_utility::vectorScale(anglesToForward(self.angles + (-90,0,0)), 10);
	
	
	if(isDefined(self)) self playsound("am_bomb_explosion");

	if(isDefined(self)) playfx(game["bomb_explosion"],self.origin + vorne);
	
    
    
    // * Damage *
    points = game["am_points_for_tnt_kill"];
    if(isDefined(self)) 
    
    level maps\mp\gametypes\_admiral_mod_player::DamageToPlayers(points, self.planter_client_id, self.planter_team, self.origin + vorne, 1000, 500, 300, 1, 1, "MOD_EXPLOSIVE", "tnt_bomb_mp");
        
		
}




CheckArtillery() {

	self notify("CheckArtillery");
	self endon("CheckArtillery");
	
	self endon("spawned");
	self endon("disconnect");
	self endon("killed_player");


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


	if(isDefined(self.artillery_active)) return;
	self.artillery_active = true;
	
	
	self.artillery_ammo_count = game["am_player_artillerie_max_strikes"];

	if(game["am_use_player_class_system"] == 1) self.artillery_ammo_count = game["am_class_artillerie_max_strikes"];
	

	self setClientCvar("am_ui_show_medic_weapon_ammo", "0");
	self.pers["am_ui_show_medic_weapon_ammo"] = 0;
	
	self setClientCvar("am_ui_show_tnt_weapon_ammo", "0");
	self.pers["am_ui_show_tnt_weapon_ammo"] = 0;	
	

	self setClientCvar("am_ui_show_artillery_weapon_ammo", "1");
	self.pers["am_ui_show_artillery_weapon_ammo"] = 1;
	
	self setClientCvar("am_ui_show_artillery_weapon_ammo_rot", "1");
	self.pers["am_ui_show_artillery_weapon_ammo_rot"] = 1;
	
	self setClientCvar("am_ui_artillery_weapon_ammo", self.artillery_ammo_count);
	self.pers["am_ui_artillery_weapon_ammo"] = self.artillery_ammo_count;
	
	self setClientCvar("am_ui_show_artillery_shell", "0");
	self.pers["am_ui_show_artillery_shell"] = 0;



	if(!isDefined(self.artillery_indicator)) {

		vorne = maps\mp\_utility::vectorScale(anglesToForward(self.angles), 200) + (0,0,5);

		self.artillery_indicator = newClientHudElem(self);
		self.artillery_indicator.name = "ArtilleryIndicator";
		self.artillery_indicator.x = self.origin[0] + vorne[0];
		self.artillery_indicator.y = self.origin[1] + vorne[1];
		self.artillery_indicator.z = self.origin[2] + vorne[2];
		self.artillery_indicator.alpha = 0;
		self.artillery_indicator.archived = true;
		self.artillery_indicator setShader("objpoint_star", 16, 16);
		self.artillery_indicator setwaypoint(false);				
	}


	self thread FirstBinocularDo();


	self thread CheckBinocularEnter();
	self thread CheckBinocularExit();

	self thread CheckArtillerieRange();

}


CheckArtillerieRange() {


	self notify("CheckArtellerieRange");
	self endon("CheckArtellerieRange");

	self endon("spawned");
	self endon("disconnect");
	self endon("killed_player");


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



	while(1) {


		// * Wenn Mapdimension noch nicht berechnet nicht weiter *
		if(!isDefined(game["map_dimension_ready"])) {
		
			wait 1;
		
			continue;
		}

		
		if(isDefined(self.artillery_indicator) && self.artillery_indicator.alpha == 1) {

		
			trace = self maps\mp\gametypes\_admiral_mod_player::GetEyeTrace();

			if ( trace["fraction"] == 1.0 || trace["surfacetype"] == "default") {
			
				self.artillery_indicator.x = -1000;
				self.artillery_indicator.y = -1000;
				self.artillery_indicator.z = -1000;	
			
			}
			else {
			
				self.artillery_indicator.x = trace["position"][0];
				self.artillery_indicator.y = trace["position"][1];
				self.artillery_indicator.z = trace["position"][2] + 17;	
			}
							
					

			wait 0.2;
			
		}
		else {
		
			wait 0.4;
		}		
				
	}
}




CheckBinocularEnter() {

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


	self notify("CheckBinocularEnter");
	self endon("CheckBinocularEnter");


	self endon("spawned");
	self endon("disconnect");
	self endon("killed_player");

	while(1) {
		
		self waittill("binocular_enter");
		
		self.binocular_status = "enter";
	
		self thread CheckFirePressed();
		self thread CheckMeleePressed();
		
		self thread DoBinocularEnter();		
		
		wait 0.5;
		if(game["am_use_player_class_system"] == 1) self maps\mp\gametypes\_admiral_mod_hud::showPlayerInfo("@AM_MP_LNG_ARTY_BUTTON_INFO_CLASS");
		else 										self maps\mp\gametypes\_admiral_mod_hud::showPlayerInfo("@AM_MP_LNG_ARTY_BUTTON_INFO");

	}
}


CheckBinocularExit() {

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


	self notify("CheckBinocularExit");
	self endon("CheckBinocularExit");

	self endon("spawned");
	self endon("disconnect");
	self endon("killed_player");

	while(1) {
	
		self waittill("binocular_exit");
		
		self.binocular_status = "exit";
		
		self maps\mp\gametypes\_admiral_mod_hud::deletePlayerInfo();


		self thread DoBinocularExit();		
	}
}


CheckFirePressed() {

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


	self notify("CheckFirePressed");
	self endon("CheckFirePressed");

	self endon("spawned");
	self endon("disconnect");
	self endon("killed_player");


	self.firebutton_pressed = undefined;


	while(!isDefined(self.firebutton_pressed)) {
	
		if(self attackButtonPressed()) self.firebutton_pressed = true;
	

		if(isDefined(self.binocular_status) && self.binocular_status != "enter") break;
		wait .1;			
	}
	
}


CheckMeleePressed() {

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


	self notify("CheckMeleePressed");
	self endon("CheckMeleePressed");

	self endon("spawned");
	self endon("disconnect");
	self endon("killed_player");


	self.meleebutton_pressed = undefined;


	while(!isDefined(self.meleebutton_pressed)) {
	
		if(self meleeButtonPressed()) self.meleebutton_pressed = true;
	

		if(isDefined(self.binocular_status) && self.binocular_status != "enter") break;
		wait .1;			
	}
	
}



FirstBinocularDo() {

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


	self notify("FirstBinocularDo");
	self endon("FirstBinocularDo");

	self endon("spawned");
	self endon("disconnect");
	self endon("killed_player");


	self.artillery_is_firing = true;
	
	self setClientCvar("am_ui_show_artillery_weapon_ammo_rot", "1");
	self.pers["am_ui_show_artillery_weapon_ammo_rot"] = 1;

	// * Je höher der Rank, je kürzer die Wartezeit (Rang 5 = ~22 Sek, Rang 1 = ~2 Minuten) *	
	time = game["am_player_artillerie_delay_first_strike"] * ((6-self.pers["rank"]) * 0.5);	
	self maps\mp\gametypes\_admiral_mod_hud::deletePlayerHudElementByName("artillery_timer");
	self maps\mp\gametypes\_admiral_mod_hud::createPlayerTimerHudElement("artillery_timer", 262,63,"center","middle","center_safearea","center_safearea",1,false,"default",0.7,undefined,time); 	
	wait time;
	self maps\mp\gametypes\_admiral_mod_hud::deletePlayerHudElementByName("artillery_timer");
		
		
	self setClientCvar("am_ui_show_artillery_weapon_ammo_rot", "0");
	self.pers["am_ui_show_artillery_weapon_ammo_rot"] = 0;
	
	self.artillery_is_firing = undefined;


	self iprintlnbold(&"AM_MP_ARTILLERY_IN_PLACE");	

	self setClientCvar("am_ui_show_artillery_shell", "1");
	self.pers["am_ui_show_artillery_shell"] = 1;

}



DoBinocularEnter() {

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


	self endon("spawned");
	self endon("disconnect");
	self endon("killed_player");



	self disableWeapon();

	if(isDefined(self.artillery_is_firing) || self.artillery_ammo_count == 0 ) return;


	self.artillery_indicator.alpha = 1;		
}



DoBinocularExit() {

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


	self endon("spawned");
	self endon("disconnect");
	self endon("killed_player");


	self.artillery_indicator.alpha = 0;

	
	wait 0.5;		
	self enableWeapon();

	

	if(!isDefined(self.firebutton_pressed) && !isDefined(self.meleebutton_pressed)) {

		return;
	}



	if(isDefined(self.meleebutton_pressed) && game["am_use_player_class_system"] == 1) {
	
		trace = self maps\mp\gametypes\_admiral_mod_player::GetEyeTrace();
		if(trace["fraction"] == 1.0 || trace["surfacetype"] == "default") {
	
			self playSound("am_artillery_no_valid_target");			
		}
		else {
	
			scan = bulletTrace(trace["position"] + (0,0,game["map_dim_vMax"][2] - 250), trace["position"] + (0,0,-1000), false, undefined);		
					
			level thread maps\mp\gametypes\_admiral_mod_player_class::DropAmmoCrate(self.clientid, self.pers["team"], scan["position"], scan["normal"]);
		}
		
		return;
	}



	if(isDefined(self.artillery_is_firing)) {
	
		self playSound("am_artillery_reload");	
		return;
	}
		

	// * Wenn keine Muni mehr, dann Kommentar *
	if(self.artillery_ammo_count == 0) {

		wait 0.5 + RandomFloat(1);

		self playSound("am_artillery_noammo");				
		return;		
	}

		




	trace = self maps\mp\gametypes\_admiral_mod_player::GetEyeTrace();
	if(trace["fraction"] == 1.0 || trace["surfacetype"] == "default") {

		self playSound("am_artillery_no_valid_target");									

		return;			
	}
	else {


		// * Wenn Mates in der Nähe, dann nicht feuern *
		near_mates = self maps\mp\gametypes\_admiral_mod_utils::CheckMatesNaehe(trace["position"],600);

		if(near_mates > 2) {

			self playSound("am_artillery_bad_target");						

			return;	
		}


		self.artillery_is_firing = true;
		
		
		self.artillery_ammo_count--;
   		
  		if(self.artillery_ammo_count > 0) { 
  		
  			color_r = 1;
  			color_g = 1;
  			color_b = 1;
  		}
  		else {
  		
  			color_r = 1;
  			color_g = 0;
  			color_b = 0;
  		}
  		
  		self setClientCvar("am_ui_artillery_weapon_ammo", self.artillery_ammo_count);
  		self.pers["am_ui_artillery_weapon_ammo"] = self.artillery_ammo_count;
 		 		
		self setClientCvar("am_ui_show_artillery_shell", "0");
		self.pers["am_ui_show_artillery_shell"] = 0;

  		
  		// * Artillery Graphic Rot *
  		self setClientCvar("am_ui_show_artillery_weapon_ammo_rot", "1");
		self.pers["am_ui_show_artillery_weapon_ammo_rot"] = 1;


		level thread maps\mp\gametypes\_admiral_mod_ambient::fireArtilleryMarker(trace["position"]);	

		wait 1 + RandomFloat(2);
		self playSound("am_artillery_schuss");

		wait 2;					


		if(isDefined(self.firebutton_pressed)) level thread maps\mp\gametypes\_admiral_mod_ambient::ArtilleryAttack(self.clientid,self.pers["team"],trace["position"], 1000, game["am_player_artillerie_damage_range"], game["am_player_artillerie_player_damage"]);


		if(self.artillery_ammo_count > 0) {
					
			// * Je höher der Rank, je kürzer die Wartezeit (Rang 5 = ~22 Sek, Rang 1 = ~2 Minuten) *	
			time = game["am_player_artillerie_delay_first_strike"] * ((6-self.pers["rank"]) * 0.5);	
			self maps\mp\gametypes\_admiral_mod_hud::createPlayerTimerHudElement("artillery_timer", 262,63,"center","middle","center_safearea","center_safearea",1,false,"default",0.7,undefined,time); 	
			wait time;
			self maps\mp\gametypes\_admiral_mod_hud::deletePlayerHudElementByName("artillery_timer");


			self setClientCvar("am_ui_show_artillery_weapon_ammo_rot", "0");
			self.pers["am_ui_show_artillery_weapon_ammo_rot"] = 0;
			
			self.artillery_is_firing = undefined;
	
	
			self iprintlnbold(&"AM_MP_ARTILLERY_IN_PLACE");
			
			self setClientCvar("am_ui_show_artillery_shell", "1");
			self.pers["am_ui_show_artillery_shell"] = 1;
			
		}
		else {
		
			// * Damit keine "Reload Message" beim nächsten mal kommt *
			self.artillery_is_firing = undefined;
		}
	}
}





CheckMGCarring() {

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

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


	self notify("CheckMGSelected");
	self endon("CheckMGSelected");


	self endon("spawned");
	self endon("disconnect");
	self endon("killed_player");

	
	
	mg_use_info = "@AM_MP_LNG_PLANT_WITH_MELEE_BUTTON_MG_INFO";
	if(game["am_mg_plant_key"] == 1) mg_use_info = "@AM_MP_LNG_PLANT_WITH_USE_BUTTON_MG_INFO";
		
	while(1) {
		
		
		wait 1;
		
		
		if(isDefined(self.pers["turred_planted"])) continue;
						
		if(self.do_status != "none") continue;
				
		
		
		current = self getcurrentweapon();
		
		if(isDefined(current) && (current == "mg42_bipod_walk_mp" || current == "30cal_walk_mp")) {
			
			
			// * Nur wenn noch Munition vorhanden *
			weapon1 = self getweaponslotweapon("primary");
			weapon2 = self getweaponslotweapon("primaryb");
					
			currentslot = "primaryb";
			if(current == weapon1)	currentslot = "primary";
					
			slotammo 		= self getweaponslotammo(currentslot);	
			slotclipammo 	= self getweaponslotclipammo(currentslot);
			
						
			if(slotammo == 0 && slotclipammo == 0) continue;
			
						
			
			
			self maps\mp\gametypes\_admiral_mod_hud::showPlayerInfo(mg_use_info);
			self thread ShowTurretTarget();

						
			while(current == "mg42_bipod_walk_mp" || current == "30cal_walk_mp") {
			
							
				wait 0.1;
				
				current = self getcurrentweapon();
		
				plantKeyPressed = false;
				if(game["am_mg_plant_key"] == 1 && self useButtonPressed()) 	plantKeyPressed = true;
				if(game["am_mg_plant_key"] == 0 && self meleeButtonPressed()) 	plantKeyPressed = true;
		
				if(plantKeyPressed) {
				
									
					old = current;
				
					self maps\mp\gametypes\_admiral_mod_hud::showPlayerInfo("@AM_MP_LNG_PLANTING_MG_INFO");
		
							
					trace = self maps\mp\gametypes\_admiral_mod_player::GetEyeTrace();		
					if(distance(trace["position"],self.origin) > 60 || distance(trace["position"],self.origin) < 20) {
					
						message = "@AM_MP_LNG_MG_POSITION_TO_NEAR_INFO";					
						if(distance(trace["position"],self.origin) > 60) message = "@AM_MP_LNG_MG_POSITION_TO_FAR_INFO";	
					
						self maps\mp\gametypes\_admiral_mod_hud::showPlayerInfo(message);
						wait 1;
						self maps\mp\gametypes\_admiral_mod_hud::showPlayerInfo(mg_use_info);
					
						continue;
					}
						
						
					// * Player-Pose an der MG *
					player_high_pos = self.origin;
					target_high_pos = (self.origin[0],self.origin[1],trace["position"][2]);
					
					high = distance(player_high_pos,target_high_pos);
															
					stance = "stand";
					if(high < 35) 	stance = "duck";
					if(high < 10) 	stance = "prone";
															
						
									
					origin = trace["position"];
					angles = self.angles;
					
					typ = "30cal_"+stance+"_mp";
					model = "xmodel/weapon_30cal";
										
					if(current == "mg42_bipod_walk_mp") {
					
						typ = "mg42_bipod_"+stance+"_mp";	
						model = "xmodel/weapon_mg42";
					}
					
					turret = spawnTurret ("misc_turret", origin, typ);
					turret setmodel(model);
					turret.name = "MG";
					turret.angles = angles;
					turret hide();
					turret.turredid = self.clientid;

				  	turret SetTopArc(40);
					turret SetBottomArc(30);
					turret SetLeftArc(45);
					turret SetRightArc(45);


					self linkto(turret);
					
									
					
					self.pers["turred_planted"] = true;
					self disableWeapon();
					
			
					self notify("TurretEnd");
					wait 1;					
					
					self playSound("am_mg_plant");
					wait 2;
					
					self maps\mp\gametypes\_admiral_mod_hud::showPlayerInfo("");
					if(isDefined(turret)) turret show();
										
					if(isDefined(self.turret_indicator)) self.turret_indicator destroy();
					
					
					
					self unlink();			
					self enableWeapon();					
					
					wait 0.5;
					
					
					
					weapon1 = self getweaponslotweapon("primary");
					weapon2 = self getweaponslotweapon("primaryb");
					
					currentslot = "primaryb";
					if(old == weapon1)	currentslot = "primary";
					
					
					slotammo 		= self getweaponslotammo(currentslot);	
					slotclipammo 	= self getweaponslotclipammo(currentslot);
					if(slotammo <= 50 && slotclipammo > 0) {
					
						slotammo = 150;
						slotclipammo--;
						
						
						// * Rank-Ammo *
						if(game["am_enable_player_rank"] && game["am_reduce_ammo_per_rank"] == 1 && isDefined(self.pers["rank"])) {
							
							slotammo = int(slotammo/2);
							if(slotammo > 0) 	slotammo = slotammo + int((slotammo/5) * self.pers["rank"]);						
						}
					}
					
					ammo = slotammo;
					
					self setWeaponSlotAmmo(currentslot, ammo);	
					self setWeaponSlotClipAmmo(currentslot, slotclipammo);						
									
					
					
					// * Solange warten bis deplanting *
					while(isDefined(turret) && distance(turret.origin,self.origin) < 85 && ammo > 0) {
					
						current = self getcurrentweapon();
						
						if(old != current) break;
						
						// * Wenn Player *
						if(self attackButtonPressed() && ammo > 0) {
																			
							ammo -= 2;	
							self setWeaponSlotAmmo(currentslot, ammo);	
													
						}
						
						if(ammo <= 0) break;
						
						wait 0.1;	
																	
						
					}
					
					
					if(isDefined(turret)) 	turret playSound("am_mg_remove");
					else					self playSound("am_mg_remove");
					
					if(isDefined(turret)) turret hide();
					//wait 1.0;
					
					if(isDefined(turret)) turret delete();
						
										
					
					self.pers["turred_planted"] = undefined;					
					
					
										
					break;
				}
				
							
			}		
			
			self notify("TurretEnd");													
			if(isDefined(self.turret_indicator)) self.turret_indicator destroy();
			
			self maps\mp\gametypes\_admiral_mod_hud::showPlayerInfo("");
		}
		
	}
}



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


	self notify("ShowTurretTarget");
	self endon("ShowTurretTarget");
	
	self endon("TurretEnd");
	
	
	self endon("spawned");
	self endon("disconnect");
	self endon("killed_player");
	
		
	if(!isDefined(self.turret_indicator)) {
		
		self.turret_indicator = newClientHudElem(self);
		self.turret_indicator.name = "turretIndicator";
		self.turret_indicator.x = -1000;
		self.turret_indicator.y = -1000;
		self.turret_indicator.z = -1000;
		self.turret_indicator.alpha = 0.5;
		self.turret_indicator.archived = true;
		self.turret_indicator setShader("objpoint_star", 8, 8);
		self.turret_indicator setwaypoint(false);				
	}



	while(1) {


		// * Wenn Mapdimension noch nicht berechnet nicht weiter *
		if(!isDefined(game["map_dimension_ready"])) {
		
			wait 1;
		
			continue;
		}

		
		if(isDefined(self.turret_indicator)) {

		
			trace = self maps\mp\gametypes\_admiral_mod_player::GetEyeTrace();

			if ( trace["fraction"] == 1.0 || trace["surfacetype"] == "default" || distance(trace["position"],self.origin) < 20 || distance(trace["position"],self.origin) > 60) {
			
				self.turret_indicator.x = -1000;
				self.turret_indicator.y = -1000;
				self.turret_indicator.z = -1000;	
			
			}
			else {
			
				self.turret_indicator.x = trace["position"][0];
				self.turret_indicator.y = trace["position"][1];
				self.turret_indicator.z = trace["position"][2] + 9;	
			}
							
					

			wait 0.2;
			
		}		
				
	}
}



CheckMCCarring() {

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

	
	self notify("CheckMCSelected");
	self endon("CheckMCSelected");


	self endon("spawned");
	self endon("disconnect");
	self endon("killed_player");

	
	
	mg_use_info = "@AM_MP_LNG_PLANT_WITH_MELEE_BUTTON_MC_INFO";
	if(game["am_mg_plant_key"] == 1) mg_use_info = "@AM_MP_LNG_PLANT_WITH_USE_BUTTON_MC_INFO";
	
	
	reload_info = "@AM_MP_LNG_MC_RELOAD_INFO";


		
	while(1) {
		
		
		wait 1;
		
		
		if(isDefined(self.pers["mortar_cannon_planted"])) continue;
						
		if(self.do_status != "none") continue;
				
		
		
		current = self getcurrentweapon();
		
		if(isDefined(current) && current == "mortar_cannon_walk_mp") {
			
			
			// * Nur wenn noch Munition vorhanden *
			weapon1 = self getweaponslotweapon("primary");
			weapon2 = self getweaponslotweapon("primaryb");
					
			currentslot = "primaryb";
			if(current == weapon1)	currentslot = "primary";
					
			slotammo 		= self getweaponslotammo(currentslot);	
			slotclipammo 	= self getweaponslotclipammo(currentslot);			
						
			if(slotammo == 0 && slotclipammo == 0) continue;
			
						
			
			
			self maps\mp\gametypes\_admiral_mod_hud::showPlayerInfo(mg_use_info);
			self thread ShowMortarCannonTarget();

						
			while(current == "mortar_cannon_walk_mp") {
			
							
				wait 0.1;
				
				current = self getcurrentweapon();
		
				plantKeyPressed = false;
				if(game["am_mc_plant_key"] == 1 && self useButtonPressed()) 	plantKeyPressed = true;
				if(game["am_mc_plant_key"] == 0 && self meleeButtonPressed()) 	plantKeyPressed = true;
		
				if(plantKeyPressed) {
				
									
					old = current;
				
					self maps\mp\gametypes\_admiral_mod_hud::showPlayerInfo("@AM_MP_LNG_PLANTING_MC_INFO");
		
							
					trace = self maps\mp\gametypes\_admiral_mod_player::GetEyeTrace();		
					if(distance(trace["position"],self.origin) > 70) {
					
						message = "@AM_MP_LNG_MG_POSITION_TO_FAR_INFO";	
					
						self maps\mp\gametypes\_admiral_mod_hud::showPlayerInfo(message);
						wait 1;
						self maps\mp\gametypes\_admiral_mod_hud::showPlayerInfo(mg_use_info);
					
						continue;
					}
						
						
									
					origin = trace["position"];
					angles = self.angles;
					
					model = "xmodel/prop_mortar";
												
					
					self.mortar_cannon = spawn("script_model", origin);
					self.mortar_cannon setmodel(model);
					self.mortar_cannon.name = "MC";
					self.mortar_cannon.angles = angles;
					self.mortar_cannon hide();
					self.mortar_cannon.turredid = self.clientid;

				  
					self linkto(self.mortar_cannon);
					
					self.pers["mortar_cannon_planted"] = true;
					


					ammo = slotclipammo;


					// * Waffe auf Stand wechseln, damit keine mehr in den Händen sichtbar *
					self setWeaponSlotWeapon(currentslot, "mortar_cannon_stand_mp");					
					self setWeaponSlotAmmo(currentslot, slotammo);	
					self setWeaponSlotClipAmmo(currentslot, slotclipammo);		
					
					current = "mortar_cannon_stand_mp";
					old = current;					
					self switchToWeapon(current);

					wait 0.1;
				
					
					
					self disableWeapon();
								
					self notify("MortarCannonEnd");
					wait 3;					
					
					
					self playSound("stickybomb_plant");
					
					self maps\mp\gametypes\_admiral_mod_hud::showPlayerInfo("");
					if(isDefined(self.mortar_cannon)) self.mortar_cannon show();
										
					if(isDefined(self.mortar_cannon_indicator)) self.mortar_cannon_indicator destroy();
										
					
					self unlink();			
					self enableWeapon();					
					
					wait 0.5;
					
					maxwinkel = 30;
					
					
					old_angles = (0,0,0);
					
					
					links = angles[1] + maxwinkel;
					rechts = angles[1] - maxwinkel;
					
					if(links > 180) links = -360 + links;
					if(rechts < -180) 	rechts = 180 - rechts;
					
					
					// * Solange warten bis deplanting *
					while(isDefined(self.mortar_cannon) && distance(self.mortar_cannon.origin,self.origin) < 85 && ammo > 0) {
					
						current = self getcurrentweapon();						
						if(old != current) break;
						
						
						
						if(old_angles != self.angles) self.mortar_cannon.angles = self.angles;
						
						old_angles = self.angles;
						
						
						// * Wenn Player Attack-Knopf drückt *
						if(self attackButtonPressed() && ammo > 0) {
																			
							// * Feuer! *												
							
							
							ammo -= 1;								
							self setWeaponSlotClipAmmo(currentslot, ammo);			
							
							level thread ShotMortar(self.clientid, origin, self.mortar_cannon.angles, self.pers["team"]);							
							
							wait 1;
							
							if(ammo > 0) {
															
								self maps\mp\gametypes\_admiral_mod_hud::showPlayerInfo(reload_info);							
								wait 2; // * Reload *
								self maps\mp\gametypes\_admiral_mod_hud::showPlayerInfo("");
							}
						}
						
						if(ammo <= 0) {
																					
							break;
						}
																		
						
						wait 0.1;					
					}
					
					
					if(isDefined(self.mortar_cannon)) 	self.mortar_cannon playSound("weap_raise",self.origin);
					else								self playSound("weap_raise",self.origin);
					
					if(isDefined(self.mortar_cannon)) self.mortar_cannon hide();
					//wait 1.0;
					
					if(isDefined(self.mortar_cannon)) self.mortar_cannon delete();
						
										
					
					self.pers["mortar_cannon_planted"] = undefined;					
					
					
					// * Waffe wieder auf Walk wechseln, damit wieder sichtbar *
					weapon_a = self getweaponslotweapon("primary");
					weapon_b = self getweaponslotweapon("primaryb");
					
					
					if(ammo <= 0 && slotammo > 0) {
					
						slotammo--;
						ammo = 5;
					}
										
					if(weapon_a == "mortar_cannon_stand_mp") {
					
						self setWeaponSlotWeapon("primary", "mortar_cannon_walk_mp");					
						self setWeaponSlotAmmo(currentslot, slotammo);	
						self setWeaponSlotClipAmmo(currentslot, ammo);																
					}
					if(weapon_b == "mortar_cannon_stand_mp") {
					
						self setWeaponSlotWeapon("primaryb", "mortar_cannon_walk_mp");					
						self setWeaponSlotAmmo(currentslot, slotammo);	
						self setWeaponSlotClipAmmo(currentslot, ammo);	
					}					
					
										
					// * Wenn keine andere Waffe selectiert wurde... auf die MC schalten *	
					if(self getcurrentweapon() == "none") {
					
						wait 0.1;
						self switchToWeapon("mortar_cannon_walk_mp");				
					}
								
					break;
				}				
							
			}		
			
			self notify("MortarCannonEnd");																
			if(isDefined(self.mortar_cannon_indicator)) self.mortar_cannon_indicator destroy();
			
			if(isDefined(self.mortar_cannon)) self.mortar_cannon delete();	
	
			
			self maps\mp\gametypes\_admiral_mod_hud::showPlayerInfo("");
		}
		
	}
}




ShotMortar(client_id, origin, angles, team) {

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



	mortar = spawn("script_model", (0,0,0));
	mortar setModel("xmodel/prop_mortar_ammunition");
	mortar.origin = origin;	
	mortar.angles = angles + (-64,0,0);	
	mortar show();

	laden  = maps\mp\_utility::vectorScale(anglestoforward((mortar.angles)),45);
	imrohr = maps\mp\_utility::vectorScale(anglestoforward((mortar.angles)),20);
	
	mortar.origin = origin + laden;	


	
	mortar moveto(origin + imrohr, 0.5);   
	
	wait 0.2;
	mortar playsound("am_mortar_cannon_fire", origin);
	wait 0.55;


	forward = anglesToForward( mortar.angles );
	forward = maps\mp\_utility::vectorScale( forward, 30);
	position = mortar.origin + forward;

	speed = maps\mp\_utility::vectorScale(forward, 2.7);


	playfx(game["mortar_flash_fx"], origin + laden, forward);	
	earthquake(0.25, 0.2, origin, 500);
			
	 
	
	mortar maps\mp\gametypes\_admiral_mod_utils::ThrowObject(client_id, team, "mortar", speed, undefined);


    o = RandomInt(3);
    playfx(game["motar_explosions_fx"][o], mortar.origin);            
                    
    mortar playSound("am_mortar_explosion");    
    
    mortar hide();
           
           
    // * do the damage *
    points = game["am_points_for_artillerie_kill"];
    maps\mp\gametypes\_admiral_mod_player::DamageToPlayers(points, client_id, team, mortar.origin + (0,0,12), 1000, game["am_player_artillerie_damage_range"], game["am_player_artillerie_player_damage"], 1, 1, "MOD_EXPLOSIVE","mortar_cannon_walk_mp");
                 
	 
 	wait 3; // Sound ending   
   
    if(isDefined(mortar)) mortar delete();
	
}





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


	self notify("ShowMortarCannonTarget");
	self endon("ShowMortarCannonTarget");
	
	self endon("MortarCannonEnd");
	
	
	self endon("spawned");
	self endon("disconnect");
	self endon("killed_player");
	
		
	if(!isDefined(self.mortar_cannon_indicator)) {
		
		self.mortar_cannon_indicator = newClientHudElem(self);
		self.mortar_cannon_indicator.name = "mortar_cannonIndicator";
		self.mortar_cannon_indicator.x = -1000;
		self.mortar_cannon_indicator.y = -1000;
		self.mortar_cannon_indicator.z = -1000;
		self.mortar_cannon_indicator.alpha = 0.5;
		self.mortar_cannon_indicator.archived = true;
		self.mortar_cannon_indicator setShader("objpoint_star", 8, 8);
		self.mortar_cannon_indicator setwaypoint(false);				
	}



	while(1) {


		// * Wenn Mapdimension noch nicht berechnet nicht weiter *
		if(!isDefined(game["map_dimension_ready"])) {
		
			wait 1;
		
			continue;
		}

		
		if(isDefined(self.mortar_cannon_indicator)) {

		
			trace = self maps\mp\gametypes\_admiral_mod_player::GetEyeTrace();

			if ( trace["fraction"] == 1.0 || trace["surfacetype"] == "default" || distance(trace["position"],self.origin) > 70) {
			
				self.mortar_cannon_indicator.x = -1000;
				self.mortar_cannon_indicator.y = -1000;
				self.mortar_cannon_indicator.z = -1000;	
			
			}
			else {
			
				self.mortar_cannon_indicator.x = trace["position"][0];
				self.mortar_cannon_indicator.y = trace["position"][1];
				self.mortar_cannon_indicator.z = trace["position"][2] + 9;	
			}
							
					

			wait 0.2;
			
		}		
				
	}
}





SpysKnife() {

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


	self notify("SpysKnife");
	self endon("SpysKnife");

    self endon("disconnect");
	self endon("killed_player");

	level endon("am_end_map");



 	while(1) {
 	
 		wait 0.1;
 	
 		if(self meleeButtonPressed() && self.do_status == "none") {

			self.do_status = "throw_knife";			
			self ThrowKnife();			
			self.do_status = "none";
 			 	 		 	 		
 	 		while(self meleeButtonPressed()) wait 0.5; 	 		 	 		
 	 		
 	 		
 	 		wait 1; 	 		
 		} 	
 	}
}

ThrowKnife() {

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


    self endon("disconnect");


	startOrigin = self getEye() + self maps\mp\gametypes\_admiral_mod_player::getPlayerEyeOffset() + (0,0,10);
	forward = anglesToForward( self getplayerangles() );
	forward = maps\mp\_utility::vectorScale( forward, 30);
	position = startOrigin + forward;

	speed = maps\mp\_utility::vectorScale(forward, 2.7);


	self.knife = spawn("script_model", position);
	self.knife setModel("xmodel/am_messer");
	self.knife.angles = self.angles + (0,-90,0);	
	self.knife show();


	
	
	self playsound("am_throw_knife"); 
	
	self.knife maps\mp\gametypes\_admiral_mod_utils::ThrowObject(self.clientid, self.pers["team"], "messer", speed, "am_knife_bounce");
	
	self.knife delete();
}



KnifeDeath(client_id, client_team, trace) {

	self endon("disconnect");

	

  	eAttacker = maps\mp\gametypes\_admiral_mod_player::getPlayer(client_id, client_team);
  

	damage = 200;
	position = trace["position"];

	health = self.health - damage;
	
	
	
	points = 1;

	// * eAttacker bekommt Punkte für jeden getöteten Player, aber nur wenn eAttacker im Spiel *				      
	// * Punkte vergeben (-1 da auch bei finishPlayerDamage 1 Punkt vergeben wird = KilledPlayer) *
	if(isDefined(eAttacker)) {

		if(health <= 0 && eAttacker != self) {

			if(points > 1) {

				maps\mp\gametypes\_admiral_mod_player::givePlayerPoints(client_id,client_team,points-1);				
			}
		}
	}


	// * Wenn eattacker noch lebt *
	if(isDefined(eAttacker)) 	self finishPlayerDamage(self, eAttacker, damage, 1, "MOD_EXPLOSIVE", "knife_mp", position, (0,0,0), "none",0);
	else 						self finishPlayerDamage(self, self, damage, 1, "MOD_EXPLOSIVE", "knife_mp", position, (0,0,0), "none",0);
	
	maps\mp\gametypes\_admiral_mod_player::setPlayerHealth(self.clientid, self.health);
	

}







CreateWeaponSpecialFX(client_id, client_team, iDFlags, sMeansOfDeath, sWeapon, vPoint, vDir) {

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

	if(!isDefined(client_id) || !isDefined(client_team)) return;
	if(!isDefined(sWeapon)) return;

	
	// * Wenn schon erstellt wurde... nicht noch eins erstellen (all Player Damage) *
	if(isDefined(level.specialFX_vPoint) && isDefined(level.specialFX_vPoint[client_id]) && distance(level.specialFX_vPoint[client_id], vPoint) < 10) return;

	level.specialFX_vPoint[client_id] = vPoint;

	

	breite 	= 1;
	breite2	= 2;
	hoehe 	= 1;
	hoehe2 	= 2;
	zeit 	= 1;
	
	
	if(sWeapon == "gas_grenade_mp") {
		
		breite 	= 100;
		breite2 = 350;
		hoehe 	= 70;
		hoehe2 	= 250;
		zeit 	= 22;

		level thread playEffectGas(vPoint,zeit);
	}
	

	if(sWeapon == "fire_grenade_mp") {
	
		breite 	= 200;
		breite2 = 280;
		hoehe 	= 100;
		hoehe2 	= 200;
		zeit 	= 18;	
		
		level thread playEffectFire(vPoint,zeit);		
	}

	
	level thread ScanDamage(client_id, client_team, iDFlags, sMeansOfDeath, sWeapon, vPoint, vDir,breite,breite2,hoehe,hoehe2,zeit);
		
}

playEffectGas(vPoint,zeit) {

	sound = spawn("script_model", (vPoint));
	soundloop = spawn("script_model", (vPoint));


	sound playsound("smokegrenade_explode_default");


	playfx(game["gas_grenade_fx"],vPoint);
	wait 4;

	playfx(game["gas_grenade_fx2"],vPoint);
	wait 4;

	playfx(game["gas_grenade_fx3"],vPoint);
	wait 4;

	playfx(game["gas_grenade_fx4"],vPoint);
	wait 6;

	
	soundloop stoploopsound();
	wait 1;
	
	sound delete();
	soundloop delete();
}



playEffectFire(vPoint,zeit) {

	sound = spawn("script_model", (vPoint));
	soundloop = spawn("script_model", (vPoint));


	playfx(game["fire_grenade_explode_fx"],vPoint);		
	sound playsound("am_fire_grenade_glas");
	wait 0.1;

	soundloop playloopsound("am_fire_grenade_burn");
	playfx(game["fire_grenade_fx"],vPoint);


	wait 4;
	playfx(game["fire_grenade_fx2"],vPoint);		

	wait 4;
	playfx(game["fire_grenade_fx3"],vPoint);		

	wait 4;
	playfx(game["fire_grenade_fx4"],vPoint);		

	
	wait 6;


	soundloop stoploopsound();
	wait 1;
	
	sound delete();
	soundloop delete();
}




ScanDamage(client_id, client_team, iDFlags, sMeansOfDeath, sWeapon, vPoint, vDir,breite,breite2,hoehe,hoehe2,zeit) {

	zeit_timer = 0;

	while(zeit_timer < zeit) {
			
		wait 1;	
		zeit_timer++;		
		
		radius = breite + (((breite2-breite)/zeit) * zeit_timer);
		height = hoehe + (((hoehe2-hoehe)/zeit) * zeit_timer);
		
    	// * Damage *
    	points = game["am_points_for_tnt_kill"];
    	level maps\mp\gametypes\_admiral_mod_player::DamageToPlayers(1, client_id, client_team, vPoint, radius, height, 25, 15, 4, sMeansOfDeath, sWeapon);
	}
	
}