
MakeClassLimit() {

	self endon("disconnect");


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

	if(game["am_gametype"] == "noz") return;



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




	// * alle Klassen zaehlen und ggf. im Menü sperren *

	classcount["am_player_class_count_0"] 	= 0;
	classcount["am_player_class_count_1"] 	= 0;
	classcount["am_player_class_count_2"] 	= 0;
	classcount["am_player_class_count_3"] 	= 0;
	classcount["am_player_class_count_4"] 	= 0;
	classcount["am_player_class_count_5"] 	= 0;
	classcount["am_player_class_count_6"] 	= 0;
	classcount["am_player_class_count_7"] 	= 0;
	


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

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

		player = players[i];		
		
		if(!isDefined(player.pers["team"]) || !isDefined(self.pers["team"])) continue;
		if(player.pers["team"] != self.pers["team"]) continue;

		
		// * Nur wenn Spieler einen Team angehört und eine Class hat *
		if((player.pers["team"] == "allies" || player.pers["team"] == "axis") && player.pers["player_class"] > -1) {
	
			// * Klasse ermitteln *
			class = -1;	
			if(isDefined(player.pers["player_class"])) class = player.pers["player_class"];
	
			pre_class = -1;	
			if(isDefined(player.pers["preselected_player_class"])) pre_class = player.pers["preselected_player_class"];
			
			
			
			// * Wenn Klasse vorhanden, dann zählen *
			if(class > -1 && class < 8 && isDefined(classcount["am_player_class_count_" + class])) {
			
				classcount["am_player_class_count_" + class]++;
			}
			// * Wenn Klasse vorhanden, dann zählen *
			if(pre_class > -1 && pre_class < 8 && isDefined(classcount["am_player_class_count_" + pre_class])) {
			
				classcount["am_player_class_count_" + pre_class]++;
			}
			
		}
	}

   
    for(i=0;i<8;i++) {
	 	
       	if(classcount["am_player_class_count_" + i] >= game["am_player_class_max_" + i] && game["am_player_class_max_" + i] != -1) {
       	
       		self setClientCvar("am_ui_allow_player_class_" + i, "2");    
       		self.pers["am_ui_allow_player_class_" + i] = 2;
       	}
       	else {
       	
       		self setClientCvar("am_ui_allow_player_class_" + i, "1");
       		self.pers["am_ui_allow_player_class_" + i] = 2;
       	} 	
    }
}



CheckClassIsLimited() {

	self endon("disconnect");


	// * Wenn nicht aktiviert, dann keine Klassenbegrenzung notwendig *
	if(game["am_player_class_limit"] != 1) return false;


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


	// * Prüfen ob eine Klasse ausgewählt wurde *
	if(self.pers["remember_player_class"] < 0) return true;
	
	
	
	classToCheck = self.pers["remember_player_class"];


	class_count = 0;

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

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

		player = players[i];		
		

		if(!isDefined(player.pers["team"]) || !isDefined(self.pers["team"])) continue;
		if(player.pers["team"] != self.pers["team"]) continue;
		
		
		if((player.pers["team"] == "allies" || player.pers["team"] == "axis") && isDefined(player.pers["player_class"]) && player.pers["player_class"] > -1) {
	
									
			// * Pre-Klasse ermitteln (player != self weil die eingene Pre ausgewählte Klasse nicht mitgezählt werden darf) *
			if(player != self && isDefined(player.pers["remember_player_class"]) && player.pers["remember_player_class"] > -1) {
			
				if(player.pers["remember_player_class"] == classToCheck) class_count++;
			}
		}
	}


	//iprintln("ClassToCheck: " + classToCheck);
	//iprintln("ClassCount: " + class_count);

	// * Wenn genügend Spieler dieser Klasse bereits auf dem Spielfeld *
	if(isDefined(game["am_player_class_max_" + classToCheck]) && class_count >= game["am_player_class_max_" + classToCheck] && game["am_player_class_max_" + classToCheck] != -1) return true;
      	

	return false;
}



ChangeClassBySpawning() {

	
	self endon("disconnect");

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

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


	// * Prüfen ob eine Klasse ausgewählt wurde *
	if(self.pers["preselected_player_class"] < 0) return true;



	// * Wenn Spieler Tod und Class gewechselt, dann Wechsel brerits hier und alte Klasse freigeben *
	if(self.sessionstate == "playing" && self.pers["preselected_player_class"] > -1 && self.pers["player_class"] != self.pers["preselected_player_class"]) {

		// * Class jetzt kurz vor dem neu "Spawnen" übernemen *
		self.pers["player_class"] = self.pers["preselected_player_class"];

		// * Preselection wieder als Leer markieren *
		self.pers["preselected_player_class"] = -1;	
	}
	
	
}



ChangeClassByDying() {

	
	self endon("disconnect");

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

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


	// * Prüfen ob eine Klasse ausgewählt wurde *
	if(self.pers["preselected_player_class"] < 0) return true;


	
	// * Wenn Spieler Tod und Class gewechselt, dann Wechsel bereits hier und alte Klasse freigeben *
	if(self.sessionstate != "playing" && self.pers["preselected_player_class"] > -1 && self.pers["player_class"] != self.pers["preselected_player_class"]) {


		// * Class jetzt kurz vor dem neu "Spawnen" übernemen *
		self.pers["player_class"] = self.pers["preselected_player_class"];

		// * Preselection wieder als Leer markieren *
		self.pers["preselected_player_class"] = -1;	
	}
}




setClassMedic() {

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


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


	
 	// * Medic Helm aufsetzen *
	
	if(isDefined(self.pers["savedmodel"]) && isDefined(self.pers["savedmodel"]["attach"]) && isDefined(self.pers["savedmodel"]["attach"][1]) && isDefined(self.pers["savedmodel"]["attach"][1]["model"])) {
	
		if(self.pers["savedmodel"]["attach"][1]["model"] != "") self detach(self.pers["savedmodel"]["attach"][1]["model"],"");
	}
	
	self.pers["savedmodel"]["attach"][1]["model"] = "xmodel/helmet_us_wet_ranger_medic_wells";
	self attach("xmodel/helmet_us_wet_ranger_medic_wells","");   

	
	radius 		= 80;	// * Radius in dem der Teammate sein soll *
	maxhealth 	= 100;	// * Max Health das der Teammate haben soll *
        
	self.medic_ammo_count = game["am_class_medic_max_health_packs"];

	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", "0");
	self.pers["am_ui_show_artillery_weapon_ammo"] = 0;

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

	self setClientCvar("am_ui_medic_weapon_ammo", self.medic_ammo_count);
	self.pers["am_ui_medic_weapon_ammo"] = self.medic_ammo_count;


	self thread WaitForDropHealthpack();


	while(1) {
	
		// * Wenn keine Packs mehr vorhanden, dann Medic-Check beenden *
		if(self.medic_ammo_count < 1) break;

			
		wait 1;
		
		
		// * Wenn der Spieler gerade etwas anderes tut, dann kein Healing erlauben *
		if(self.do_status != "none") continue;
		
	
		// * nach zu heilenden Playern suchen *
		clientid = self maps\mp\gametypes\_admiral_mod_player::searchPlayerGetClientId(self.clientid,"playing",self.pers["team"],radius, maxhealth);
		
		// * Wenn Player gefunden *
		if(isDefined(clientid)) {
		
			self.do_status = "heal";
		 
		
			// * Healing Info anzeigen *
			self maps\mp\gametypes\_admiral_mod_hud::showPlayerInfo("@AM_MP_LNG_HEAL_BUTTON_INFO");
	
			healing_taste_getrueckt = undefined;
			
			while(1) {
			
				// * 0.5 Sekunden auf den Use-Button warten *
				for(i=0;i<5;i++) {
				
					wait 0.1;
				
					if(self useButtonPressed()) {
					
						healing_taste_getrueckt = true;
						break;
					}									
				}
				
				// * Wenn Use-Button gedrückt, dann raus hier *
				if(isDefined(healing_taste_getrueckt)) break;
				
				
				// * Nach 1 Sekunde abfragen ob sich der Medic noch im Radius des zu heilenden Spielers befindet *
				if(self maps\mp\gametypes\_admiral_mod_player::checkPlayerState(clientid,"playing",self.pers["team"], radius, maxhealth)) continue;
				else break;			
				
			}
									
			
			// * solange bis der Medic den Use-Button losgelassen hat *
			if(isDefined(healing_taste_getrueckt)) {
			
			
				// * dem zu heilenden Spieler anzeigen das das Healing beginnt *
				maps\mp\gametypes\_admiral_mod_player::setPlayerInfo(clientid,"@AM_MP_LNG_START_HEALING_INFO");
				
				// * zu heilenden Spieler festhalten *
				if(game["am_class_medic_hold_player"] == 1) {
								
					self.helper = spawn("script_model",self.origin);		
					self.helper maps\mp\gametypes\_admiral_mod_player::linkPlayer(clientid);
				}
			
			
				self disableWeapon();

				// * Healing starten *
				self maps\mp\gametypes\_admiral_mod_hud::showPlayerInfo("@AM_MP_LNG_START_HEALING_INFO");


				// * Healthbar anzeigen *
				self maps\mp\gametypes\_admiral_mod_hud::showHealthBar("healthbar_healing",258,320);

				newHealth = maps\mp\gametypes\_admiral_mod_player::getPlayerHealth(clientid);
				self maps\mp\gametypes\_admiral_mod_hud::updateHealthBar("healthbar_healing",newHealth);

				oldHealth = maps\mp\gametypes\_admiral_mod_player::getPlayerHealth(clientid);	
				
				
				
							
				// * Solange Player in Range und UseButton Pressed *			
				while(self useButtonPressed() && maps\mp\gametypes\_admiral_mod_player::changePlayerHealth(clientid,5) && self maps\mp\gametypes\_admiral_mod_player::checkPlayerState(clientid,"playing",self.pers["team"],radius,maxhealth)) {

					if(self.do_status != "heal") break;

					newHealth += 5;					
					if(newHealth > 98) newHealth = 98; // * Damit die Bar nicht über den Rand ragt *

					self maps\mp\gametypes\_admiral_mod_hud::updateHealthBar("healthbar_healing",newHealth);
										
					wait 0.5;
				}



				// * Wenn Medic Use-Button losläßt *


				// * Player-Info wieder löschen *
				maps\mp\gametypes\_admiral_mod_player::setPlayerInfo(clientid," ");


				// * Zum Schluss den letzten Health Stand anzeigen *
				newHealth = maps\mp\gametypes\_admiral_mod_player::getPlayerHealth(clientid);								
				self maps\mp\gametypes\_admiral_mod_hud::updateHealthBar("healthbar_healing",newHealth-2);
			
				
				if(newHealth >= maxhealth) {
					
					// * Wenn Gesundheitszustand wieder ok, dann Sprinten möglich *
					if(newHealth > game["am_deny_sprinting_health"]) {
						
						maps\mp\gametypes\_admiral_mod_player::allowSprinting(clientid);		
					}
					
					
					// * Wenn Herz-Icon angezeigt wurde, dann jetzt löschen *
					maps\mp\gametypes\_admiral_mod_player::deleteHeartIcon(clientid);
												

					healerID = self.clientid;
					healerName = self.name;
					healedID = clientid;
					healedName = maps\mp\gametypes\_admiral_mod_player::getPlayerName(clientid);

					logprint ("heal" + ";" + healerID + ";" + healerName + ";" + healedID + ";" + healedName + "\n");							

					
					
					// * Healing komplett *
					self maps\mp\gametypes\_admiral_mod_hud::showPlayerInfo("@AM_MP_LNG_COMPLETE_HEALING_INFO");
				
					wait 0.5;
					
					self.medic_ammo_count--;
					self setClientCvar("am_ui_medic_weapon_ammo", self.medic_ammo_count);
					self.pers["am_ui_medic_weapon_ammo"] = self.medic_ammo_count;
					
					// * Heilung komplett, also nicht zurücksetzen *
					oldHealth = 100;
					
					
					// * Player Punkte geben *					
					points = game["am_medic_heal_points"] + 1;  //  +1 wegen Arty +5 -1 (kill)
					maps\mp\gametypes\_admiral_mod_player::givePlayerPoints(self.clientid, self.pers["team"], points);
									
				}
				
				// * zu heilenden Spieler wieder loslassen *
				if(game["am_class_medic_hold_player"] == 1) {
				
					if(isDefined(self.helper)) 	self.helper maps\mp\gametypes\_admiral_mod_player::unlinkPlayer(clientid);
					if(isDefined(self.helper)) 	self.helper delete();					
				}

				wait 0.5;				


				// * Wenn vor Ende des Heilens abgebrochen wurde, dann Health des zu heilenden Players wieder zurücksetzten *
				if(oldHealth > 0 && oldHealth < 100) {

					maps\mp\gametypes\_admiral_mod_player::setPlayerHealth(clientid,oldHealth);
				}


				// * Healthbar löschen *
				self maps\mp\gametypes\_admiral_mod_hud::deletePlayerHudElementByName("healthbar_healing_health");
					

				self enableWeapon();
			}
			
			
			
			// * Wenn der Medic sich sich aus dem Radius bewegt, oder der zu heilende Spieler stirbt *
			self maps\mp\gametypes\_admiral_mod_hud::showPlayerInfo(" ");
				
			wait 0.2;
			if(self.do_status == "heal") self.do_status = "none";
			
			
			wait 1;
		}		
	} 
}

WaitForDropHealthpack() {

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


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

	while(1) {
	
		// * Wenn keine Packs mehr vorhanden, dann Medic-Check beenden *
		if(self.medic_ammo_count < 1) break;
			
		wait 0.2;
		
		if(self meleeButtonPressed() && self useButtonPressed()) {

			while(isDefined(level.health_is_dropping)) wait 0.1;    
			level.health_is_dropping = true;



			// * Wenn in der Rotation in der akt. Reihenposition schon ein Health existiert, dann zum überschreiben löschen *
			if(isDefined(game["am_healthqueue"][game["am_healthqueuecurrent"]]))  {

				game["am_healthqueue"][game["am_healthqueuecurrent"]] delete();
			}
    
    
		
			startOrigin = self getEye() + self maps\mp\gametypes\_admiral_mod_player::getPlayerEyeOffset() + (0,0,10);
			forward = anglesToForward( self getplayerangles() );
			forward = maps\mp\_utility::vectorScale( forward, 40);
			position = startOrigin + forward;
			
			speed = maps\mp\_utility::vectorScale(forward, 1.1);


			item_health = spawn("script_model", position);	
			item_health setModel("xmodel/health_large");
			item_health.targetname = "item_health_" + game["am_healthqueuecurrent"];			
			item_health.angles = (0, randomint(360), 0);
			item_health show(); 


			game["am_healthqueue"][game["am_healthqueuecurrent"]] = item_health;   
 			game["am_healthqueuecurrent"]++;    
    
    		if(game["am_healthqueuecurrent"] >= 10)  game["am_healthqueuecurrent"] = 0;
    
			level.health_is_dropping = undefined;



			self.medic_ammo_count--;
			self setClientCvar("am_ui_medic_weapon_ammo", self.medic_ammo_count);
			self.pers["am_ui_medic_weapon_ammo"] = self.medic_ammo_count;
			
			
			item_health maps\mp\gametypes\_admiral_mod_utils::ThrowObject(self.clientid, self.pers["team"], "healthpack", speed, undefined);
			
	
			wait 1.2;					
		}
		
	}
}




setClassTNT() {

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

    self endon("disconnect");

	
	self thread maps\mp\gametypes\_admiral_mod_weapons_class::CheckTNT();
}

setClassArtillery() {

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

    self endon("disconnect");
    
     
    
	self thread maps\mp\gametypes\_admiral_mod_weapons_class::CheckArtillery();
}

setClassSpy() {

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


	self notify("setClassSpy");
	self endon("setClassSpy");

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

	level endon("am_end_map");


	sek 			= 0;
	send_pos 		= false;

	spy_wait_time 	= 20;
	spy_time		= 30;

	
	wait 0.5;


	if(game["am_battlechatter"] == 1 && game["am_battlechatter_comments_probability"] > 0) {
	
		// * Nicht jedes Mal talken *
		if(RandomInt(100) < game["am_battlechatter_comments_probability"]) {

			if(self.pers["team"] == "allies") 	self playsound("am_allies_spy");
			else								self playsound("am_axis_spy");
		}
	}


	self thread maps\mp\gametypes\_admiral_mod_weapons_class::SpysKnife();



	while(1) {
	
		wait 2;
		sek+=2;		

		
		if(send_pos == false && sek >= spy_wait_time) {
		
			send_pos = true;
			
			self thread ShowTransmittingInfo(spy_time);
		}
		if(sek >= spy_wait_time + spy_time) {
						
			send_pos = false;
			sek = 0;						
			
			if(isDefined(self.spy_radio_sound)) {
			
				self.spy_radio_sound stoploopsound();
				wait 1;
				if(isDefined(self.spy_radio_sound)) self.spy_radio_sound delete();				
			}
			
			self thread ShowEndOfTransmission();
		}
	
		// * Solange die Connection besteht, Positionen übermitteln *
		if(send_pos == true && isDefined(self.spy_radio_sound)) {

			
			// * Entfernt sich der Spion von der Übertragungseinheit? *
			if(isDefined(self.spy_radio_sound) && distance(self.origin,self.spy_radio_sound.origin) > 800) {
			
				send_pos = false;
				sek = 0;
				
				if(isDefined(self.spy_radio_sound)) {
				
					self.spy_radio_sound stoploopsound();
					wait 1;
					if(isDefined(self.spy_radio_sound)) self.spy_radio_sound delete();
				}					
				
				self thread ShowConnectionInterrupted();
			}



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

			for(i=0;i<players.size;i++) {
		
				if(!isDefined(players[i].spy_is_looking_at_you) && players[i].sessionstate == "playing" && players[i].pers["team"] != self.pers["team"]) {			

					
					trace = bulletTrace(self.origin + (0,0,50), players[i].origin + (0,0,50), false, undefined);	
				
					// * Wenn kein Gegenstand zwischen den Spielern (Sichtkontakt) *
					if(trace["fraction"] == 1) {
				
    					target_range = vectordot(anglestoforward(self.angles),anglestoforward(vectortoangles(vectornormalize((self.origin) - players[i].origin)))) * -1;
                   
 						if(target_range > 0.7) {
 					 					
							players[i] thread SpyIsLookingAtYou();
							level thread maps\mp\gametypes\_admiral_mod_compass::AddSpyPoint(players[i].origin, self.pers["team"], 15);
					 					
 						}
 					} 				
 				}
			}						
		}	
	}

}


SpyIsLookingAtYou() {

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


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

	level endon("am_end_map");
	
	// * Damit nicht jede Sekunde der selbe Spieler auf dem Compass neu angezeigt wird (= Sperre für 16 Sekunden) *

	self.spy_is_looking_at_you = true;
	
	wait 16;
	
	self.spy_is_looking_at_you = undefined;
}


ShowTransmittingInfo(spy_time) {

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


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

	level endon("am_end_map");

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

	self.spy_radio_sound = spawn("script_model", self.origin);
	if(isDefined(self.spy_radio_sound)) self.spy_radio_sound playloopsound("am_spy_radio");


	wait spy_time - 5;
	self setClientCvar("am_ui_player_class_info_text", " ");
	self.pers["am_ui_player_class_info_text"] = " ";
}

ShowConnectionInterrupted() {

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


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

	level endon("am_end_map");


	self setClientCvar("am_ui_player_class_info_text", "@AM_MP_LNG_CLASS_SPY_CONNECTION_INTERRUPTED");
	self.pers["am_ui_player_class_info_text"] = "AM_MP_LNG_CLASS_SPY_CONNECTION_INTERRUPTED";
	wait 5;
	self setClientCvar("am_ui_player_class_info_text", " ");
	self.pers["am_ui_player_class_info_text"] = " ";
}

ShowEndOfTransmission() {

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


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

	level endon("am_end_map");


	self setClientCvar("am_ui_player_class_info_text", "@AM_MP_LNG_CLASS_SPY_END_OF_TRANSMISSION");
	self.pers["am_ui_player_class_info_text"] = "AM_MP_LNG_CLASS_SPY_END_OF_TRANSMISSION";
	wait 5;
	self setClientCvar("am_ui_player_class_info_text", " ");
	self.pers["am_ui_player_class_info_text"] = " ";
}






DropAmmoCrate(client_id, team, position, normal) {


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


	if(!isDefined(client_id) || !isDefined(team) || !isDefined(position) || !isDefined(normal)) return;
	

	// * alte Team-Crate entfernen... und neu einsegeln lassen *
	if(isDefined(game["crate_" + team])) {
			
		// * Wenn die Landung noch läuft, dann keine neue Crate erzeugen *
		if(isDefined(game["crate_" + team].isLanding)) return;
		
		game["crate_" + team] delete();		
	}


	// * Crate auf Kompass *
	level thread maps\mp\gametypes\_admiral_mod_compass::ShowCrate(team, position);

	
	speed = 25; // * Langsam zu Boden *
	

 	start_position = position + (0,0,game["map_dim_vMax"][2] - 250);
 	 
 
    fallschirm = spawn("script_model", (0,0,0));
    fallschirm setModel("xmodel/am_fallschirm");    
   	fallschirm.origin = start_position + (0,0,-30);
  	fallschirm.angles = fallschirm.angles + (0,0,90);
    fallschirm hide();


	if(!isDefined(level.crate_id)) level.crate_id = 0;
	else level.crate_id++;



	crate = spawn("script_model", (0,0,0));
    
    if(team == "allies") 	crate setModel("xmodel/crate01");   
    else 					crate setModel("xmodel/prop_crate_wehrmacht2a"); 
    
   	crate.origin = start_position;
  	crate hide();
  	crate.id = level.crate_id;
  	crate.isLanding = true;
    
    game["crate_" + team] = crate;
    
  
    fahrstuhl = spawn("script_model", (0,0,0));
   	fahrstuhl.origin = crate.origin;
  	    
    fallschirm linkto (fahrstuhl);
    crate linkto (fahrstuhl);
    


	fahrstuhl.origin = start_position;
    fallschirm show();
    crate show();


	fahrstuhl playloopsound("am_parachuting");

    ziel_position = position;
        
   
    zeit  = maps\mp\gametypes\_admiral_mod_utils::ZeitSpeedBerechnung(speed, start_position, ziel_position);
            
    fahrstuhl moveto(ziel_position, zeit);
    
    wait 0.1 + zeit;


	fahrstuhl stoploopsound();
	wait 0.1;    
    crate playsound("am_kiste_drop");
    
    
    crate.angles = vectortoangles(vectornormalize(normal)) + (90,0,0);
  	crate.isLanding = undefined;
  
 
    if(isDefined(fallschirm))  	fallschirm delete();
    if(isDefined(fahrstuhl))  	fahrstuhl delete();
 
 
    crate thread CrateThink(client_id, team);
    
     
   return;  
}




CrateThink(client_id, team) {


	// * alten Team-Crate-Trigger entfernen... *
	if(isDefined(game["crate_" + team + "_trigger"])) {
					
		game["crate_" + team + "_trigger"] delete();		
	}


	trigger = spawn("trigger_radius", self.origin, 0, 60, 50);	
	game["crate_" + team + "_trigger"] = trigger;


	crate_id = self.id;


	while(isDefined(self)) {
		
		// * Auf einen Spieler warten *		
		trigger waittill("trigger", other);	
		
		// * Wenn Crate gerade gelöscht, dann raus *
		if(!isDefined(self)) break;
		
	
		if(isPlayer(other) && other.sessionstate == "playing" && other.pers["team"] == team) {
			
			if(!isDefined(other.ammo_reloaded_count)) other.ammo_reloaded_count = 0;				
			
			if(other.ammo_reloaded_count < game["am_ammo_crate_allow_max_ammo_reloads"] || (isDefined(other.ammo_reloaded_crate_id) && other.ammo_reloaded_crate_id != crate_id)) {

				// * Wenn Team zwischendurch gewechselt *
				if(isDefined(other.ammo_reloaded_crate_id) && other.ammo_reloaded_crate_id != crate_id) {
				
					other.ammo_reloaded_count = 0;
				}
				
				other.ammo_reloaded_crate_id = crate_id;
				other.ammo_reloaded_count++;
				other thread PlayerAmmoUpload(client_id, team);
			}
			else {

				other thread ShowCrateUploadWarning();						
			}			
		}		
		
		wait 5;
	}
	
	if(isDefined(trigger)) trigger delete();

	
}


ShowCrateUploadWarning() {

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


	if(isDefined(self.player_info_text_is_showing)) return;
	self.player_info_text_is_showing = true;


		
	self setClientCvar("am_ui_player_info_text", "@AM_MP_LNG_AMMO_CRATE_UPLOAD_WARNING");
	self.pers["am_ui_player_info_text"] = "AM_MP_LNG_AMMO_CRATE_UPLOAD_WARNING";
	wait 4;
	self setClientCvar("am_ui_player_info_text", " ");
	self.pers["am_ui_player_info_text"] = " ";


	self.player_info_text_is_showing = undefined;

}


PlayerAmmoUpload(client_id, team) {


	self endon("disconnect");
	self endon("killed_player");
	
	
	if(isDefined(self.player_info_text_is_showing)) return;
	self.player_info_text_is_showing = true;
		
		
		
	self giveMaxAmmo(self maps\mp\gametypes\_weapons::getPistolType());
		
	self setWeaponSlotAmmo("primary", 999);
	self setWeaponSlotAmmo("primaryb", 999);
	
	self maps\mp\gametypes\_weapons::giveGrenades();
	
	self playsound("weap_pickup",self.origin);
	wait 0.5;
	self playsound("weap_ammo_pickup",self.origin);
	
	
	self setClientCvar("am_ui_player_info_text", "@AM_MP_LNG_AMMO_CRATE_UPLOAD");
	self.pers["am_ui_player_info_text"] = " ";
	wait 4;
	self setClientCvar("am_ui_player_info_text", " ");
	self.pers["am_ui_player_info_text"] = " ";
		
	self.player_info_text_is_showing = undefined;
		
}



SetClassStatusIcon() {

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

	if(game["am_use_player_class_system"] == 0 || game["am_show_rank_icons_in_highscore"] == 1) return;
	

	// * Player-Class *	
		
	if(self.pers["player_class"] == 4) self.statusicon = "gfx/custom/hud@medic.tga";
	if(self.pers["player_class"] == 5) self.statusicon = "gfx/custom/tnt_bomb.tga";
	if(self.pers["player_class"] == 6) self.statusicon = "gfx/custom/hud@artillery.tga";
	if(self.pers["player_class"] == 7) self.statusicon = "gfx/custom/hud@spy.tga";
 
}

