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


weapon_init() {

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

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


	// * Wenn Spion, dann kein Weapon Arsenal *
	if(self.pers["player_class"] == 7) return;



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

	
	// * Wenn Daten nicht gelöscht wurden, dann sollen diese auch bleiben *
	if(isDefined(self.pers["weapons"])) return;

	
	self.pers["weapons"] 			 = undefined;
	self.pers["weapons_clipsize"] 	 = undefined;
	self.pers["weapons_reservesize"] = undefined;
	
	self.pers["weapon_count"] 		 = 0;
	self.pers["current_weapon_nr"] 	 = -1;
	
	
	self.pers["cur_weapon"] 			= self getcurrentweapon();
	self.pers["old_primary_weapon"] 	= self getweaponslotweapon("primary");
	self.pers["old_primaryb_weapon"] 	= self getweaponslotweapon("primaryb");

	
	self.pers["cur_slot"] = "primary";
	self addWeapon();
	self.pers["cur_slot"] = "primaryb";
	self addWeapon();


	self.pers["cur_slot"] = "primary";	
	
	self.pers["current_weapon_nr"] 	= 0;

}


weapon_reset() {

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

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


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



	self.pers["weapons"] 			 = undefined;
	self.pers["weapons_clipsize"] 	 = undefined;
	self.pers["weapons_reservesize"] = undefined;
}


checkWeaponSlots() {

	self notify("checkWeaponSlots");
	self endon("checkWeaponSlots");

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

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


	// * Wenn Spion, dann kein Weapon Arsenal *
	if(self.pers["player_class"] == 7) return;


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


	wait 0.5; // * Bei S&D ist eine verzögerung nötig, da MapReset! *

	
	for(;;) {
	
		wait 0.2;
		
		
		if(isDefined(self.pers["sprint"])) continue;
		
		if(isDefined(self.grenade_picked_up)) continue;
		
		if(isDefined(self.deny_weapon_arsenal)) continue;

		
		if(self checkSlotSwitch()) {
			
			// * Waffe rotieren *
			
			// * vorher Munitionsdaten der vorherigen Waffe speichern *
			if(self.pers["cur_slot"] == "primary") 	self.pers["weapons_clipsize"][self.pers["current_weapon_nr"]] 		= self getweaponslotclipammo("primaryb");
			else 									self.pers["weapons_clipsize"][self.pers["current_weapon_nr"]] 		= self getweaponslotclipammo("primary");
			if(self.pers["cur_slot"] == "primary") 	self.pers["weapons_reservesize"][self.pers["current_weapon_nr"]] 	= self getweaponslotammo("primaryb");
			else 									self.pers["weapons_reservesize"][self.pers["current_weapon_nr"]] 	= self getweaponslotammo("primary");
		
			
			self.pers["current_weapon_nr"]++;
			if(self.pers["current_weapon_nr"] >= self.pers["weapon_count"]) self.pers["current_weapon_nr"] = 0;
			
			self setNextArsenalWeapon(self.pers["cur_slot"],self.pers["current_weapon_nr"]);
						
			
			self.pers["old_primary_weapon"] 	= self getweaponslotweapon("primary");
			self.pers["old_primaryb_weapon"] 	= self getweaponslotweapon("primaryb");

			wait 0.05;
		}
		
		
		weapon = self checkWeaponSwitch();
		
		if(weapon != "none" && maps\mp\gametypes\_admiral_mod_utils::subStr(weapon,0,3) != "am_") {
		
			self addWeapon();
			
			self setNextArsenalWeapon(self.pers["cur_slot"], self.pers["current_weapon_nr"]);
			
			
			self.pers["old_primary_weapon"] 	= self getweaponslotweapon("primary");
			self.pers["old_primaryb_weapon"] 	= self getweaponslotweapon("primaryb");

			wait 0.05;
		}

	}
}


checkSlotSwitch() {

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


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


	currentslot = "";

	current = self getcurrentweapon();
	if(current != "none") {
	
		weapon1 = self getweaponslotweapon("primary");
		weapon2 = self getweaponslotweapon("primaryb");

		if(current == weapon1)
			currentslot = "primary";
		else {			
			currentslot = "primaryb";
		}
	}
	
	if(currentslot != "" && currentslot != self.pers["cur_slot"]) {
	
		//iprintln("SlotSwitch");
	
		self.pers["cur_slot"] = currentslot;
		return true;
	}
	
	return false;
}


checkWeaponSwitch() {

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


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


	weapon_slot1 = self getweaponslotweapon("primary");


	if(weapon_slot1 != "none" && self.pers["old_primary_weapon"] != weapon_slot1) {
	
		self.pers["old_primary_weapon"] 	= weapon_slot1;
		self.pers["old_primaryb_weapon"] 	= self getweaponslotweapon("primaryb");

		self.pers["cur_weapon"] = self.pers["old_primary_weapon"];
		self.pers["cur_slot"] 	= "primary";
		
		//iprintln("WeaponSwitch");
		return weapon_slot1;
	}
	
	weapon_slot2 = self getweaponslotweapon("primaryb");

	if(weapon_slot2 != "none" && self.pers["old_primaryb_weapon"] != weapon_slot2) {
	
		self.pers["old_primary_weapon"] 	= self getweaponslotweapon("primary");
		self.pers["old_primaryb_weapon"] 	= weapon_slot2;
		
		self.pers["cur_weapon"] = self.pers["old_primaryb_weapon"];
		self.pers["cur_slot"] 	= "primaryb";
		
		//iprintln("WeaponSwitch");
		return weapon_slot2;
	}	
	
	return "none";
}





addWeapon() {

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


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


	weapon 		= self getweaponslotweapon(self.pers["cur_slot"]);
	clipsize 	= self getweaponslotclipammo(self.pers["cur_slot"]);
	reservesize	= self getweaponslotammo(self.pers["cur_slot"]);

	
	if(weapon == "none" || maps\mp\gametypes\_admiral_mod_utils::subStr(weapon,0,3) == "am_") return;
	
		
	// * Prüfen ob Weapon schon im Arsenal *	
	arsenal_nr = self checkWeaponExists(weapon);

	if(arsenal_nr < 0) {


		// * Prüfen ob Arsenal schon voll *
 		if(self.pers["weapon_count"] < game["am_max_weapon_slot_count"]) { 

			arsenal_nr = self.pers["weapon_count"];

			// * Waffe ins Arsenal aufnehmen *
			self.pers["weapons"][arsenal_nr] 			 = weapon;
			self.pers["weapons_clipsize"][arsenal_nr] 	 = clipsize;
			self.pers["weapons_reservesize"][arsenal_nr] = reservesize;
		
			self.pers["weapon_count"]++;
			self.pers["current_weapon_nr"] = arsenal_nr;
		}
		else {
		
			arsenal_nr = self.pers["current_weapon_nr"];
		
			// * Waffe mit der aktuellen austauschen *
			self.pers["weapons"][arsenal_nr] 			 = weapon;
			self.pers["weapons_clipsize"][arsenal_nr] 	 = clipsize;
			self.pers["weapons_reservesize"][arsenal_nr] = reservesize;		
		}
	}
	else {
		
		// * Waffe schon vorhanden, nicht neu aufnehmen *
		
		self.pers["current_weapon_nr"] = arsenal_nr;	
	}	

}


checkWeaponExists(weapon) {

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


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



	for(i=0;i<self.pers["weapon_count"];i++) {
	
		if(self.pers["weapons"][i] == weapon) return i;	
	}
	
	return -1;
}


setNextArsenalWeapon(weapon_slot,arsenal_nr) {
	
	self endon("disconnect");
	self endon("killed_player");
	

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



	next = self.pers["current_weapon_nr"] +1;	
	if(next >= self.pers["weapon_count"]) next = 0;


	if(weapon_slot == "primary" ) {
	
		self setWeaponSlotWeapon("primaryb", self.pers["weapons"][next]);
		self setWeaponSlotClipAmmo("primaryb", self.pers["weapons_clipsize"][next]);
		self setWeaponSlotAmmo("primaryb", self.pers["weapons_reservesize"][next]);		
	}
	wait 0.05;
	if(weapon_slot == "primaryb") {
	
		self setWeaponSlotWeapon("primary", self.pers["weapons"][next]);
		self setWeaponSlotClipAmmo("primary", self.pers["weapons_clipsize"][next]);
		self setWeaponSlotAmmo("primary", self.pers["weapons_reservesize"][next]);		
	}
	wait 0.05;


}



GetSecondaryWeapon() {
  
  
	self endon("disconnect");
	self endon("killed_player");
 
 	self endon("secondary_weapon_not_changed");

 	
 	if(game["am_secondary_weapon"] != 1) return;
 	
 
	if(game["am_error_logfile"] == 1) logPrint("GetSecondaryWeapon\n");


	
	// * Wenn Spion, dann keine 2nd Weapon *
	if(self.pers["player_class"] == 7 || self.pers["preselected_player_class"] == 7) return;
 	

	wait 0.2; // * Bei S&D etwas warten, damit beim Map-Restart alles initialisiert ist *


	if(game["am_enable_weapon_thompson_for_axis"] == 0) self setClientCvar("ui_allow_thompson_german", "0");
	if(game["am_enable_weapon_thompson_for_axis"] == 1) self setClientCvar("ui_allow_thompson_german", "1");

	if(game["am_enable_weapon_enfield_for_american"] == 0) self setClientCvar("ui_allow_enfield_american", "0");
	if(game["am_enable_weapon_enfield_for_american"] == 1) self setClientCvar("ui_allow_enfield_american", "1");

 	
 	// * Wenn Weapon Arsenal aktiviert, dann muß für das WeaponLimit die 1. ausgewählte Waffe bereits im Arsenal sein *
 	// * Die wird nur zum jetzigen zählen verwendet, diese wird weiter unten dann wieder gelöscht bevor das Arsenal aktiv wird *
 	if(game["am_weapon_arsenal"] == 1) {
 	
		self.pers["weapon_count"] = 1;		
		self.pers["weapons"][0] = self getWeaponSlotWeapon("primary");
 	
 	
 		// * Waffen Limits Check und Limitation *
		//self maps\mp\gametypes\_admiral_mod_weapons::MakeWeaponLimit();	
		
		
		self.pers["weapon_count"] 	= 0;		
		self.pers["weapons"] 		= undefined;				
 	}
 	
  
 
 	if(isDefined(self.pers["team_old"]) && self.pers["team_old"] != self.pers["team"] || !isDefined(self.pers["team_old"])) {
 	
 		self.pers["team_old"] 					= self.pers["team"];
 		self.pers["old_secondary_weapon"]	 	= undefined;
 	}
 
 	if(!isDefined(self.pers["am_weapon_changed"])) {
 	
 		
 		// * Wenn Slot-B eine Primary-Weapon enthält, dann nicht mit Secondary Weapon auffüllen *
		if(maps\mp\gametypes\_weapons::isMainWeapon(self getweaponslotweapon("primaryb"))) return;
 	
 	
 		// * Wenn Primäre Waffe nicht gewechselt wurde, dann die schon mal als Secondary Weapon ausgewählte Waffe als 2. geben *
 	 	if(isDefined(self.pers["old_secondary_weapon"]) && ( maps\mp\gametypes\_weapons::isPistol(self.pers["old_secondary_weapon"]) || maps\mp\gametypes\_weapons::isMainWeapon(self.pers["old_secondary_weapon"]) ) ) {
 	
	 	    self setWeaponSlotWeapon("primaryb", self.pers["old_secondary_weapon"]);
		    self setWeaponSlotAmmo("primaryb", 999);
		    self setWeaponSlotClipAmmo("primaryb", 999);
		
			return;
		}
 		
 	}
 
 	self.pers["am_weapon_changed"] = undefined;



	if(game["am_give_players_thompson_second_weapon"] == 1) {
		
		weapon = "thompson_mp";
		
		// * 2nd Weapon *
    	self setWeaponSlotWeapon("primaryb", weapon);
    	self setWeaponSlotAmmo("primaryb", 999);
    	self setWeaponSlotClipAmmo("primaryb", 999);

		self.pers["old_secondary_weapon"] = weapon;
		
		return;
	}




 
 	// * für Vergleich * 
    primaryweapon   = self getWeaponSlotWeapon("primary");
 
    
        
    if(self.pers["team"] == "allies")
    {       
        self openMenu(game["menu_weapon_secondary_allies"]);      
    }
    else
    {
        self openMenu(game["menu_weapon_secondary_axis"]);        
    }
            
    
	self thread ShowWeaponSelectText();
	
	
	while(1) {

		self waittill("menuresponse", menu, response);      
      
        if(response == "open" || response == "close" || response == "changeweapon" || response == "changeteam" || response == "team")  return;   
      

        // * Wenn diese Waffe nicht für German, Russian, American, British existiert, diese Waffe nicht zulassen *
        if(!TeamWeaponCheck(response)) continue;

          
        weapon = self maps\mp\gametypes\_weapons::restrictWeaponByServerCvars(response);
        
        if(weapon == "restricted" || weapon == primaryweapon) {
        
            //self openMenu(menu);
            continue;           
        }
        else {
        
        	// * wenn alles ok *
        	break;                       
        }
        
    }

 	self notify("secondary_weapon_changed");

   
   
	self maps\mp\gametypes\_admiral_mod_hud::deletePlayerHudElementByName("secondary_weapon_top");    			
	self maps\mp\gametypes\_admiral_mod_hud::deletePlayerHudElementByName("secondary_weapon_bottom");    			
  
      
    self closeMenu();
    self closeMenu();
 	wait 0.05;
 	self closeMenu();
 


	if(isDefined(weapon) && weapon != "none" && ( maps\mp\gametypes\_weapons::isPistol(weapon) || maps\mp\gametypes\_weapons::isMainWeapon(weapon) ) ) {
     
       
    	// * Alte Waffe wieder herstellen *
    	self.pers["weapon"] = primaryweapon;
    	self setWeaponSlotWeapon("primary", primaryweapon);
    	self setWeaponSlotAmmo("primary", 999);
    	self setWeaponSlotClipAmmo("primary", 999);
    	self setSpawnWeapon(primaryweapon);
    
    	// * 2nd Weapon (Pistol) wird mit der neuen Waffe überschrieben *
    	self setWeaponSlotWeapon("primaryb", weapon);
    	self setWeaponSlotAmmo("primaryb", 999);
    	self setWeaponSlotClipAmmo("primaryb", 999);


    	self.pers["old_secondary_weapon"] = weapon;
	
		self.pers["am_weapon_changed"] = undefined;
	}


}



ShowWeaponSelectText() {
	
	self endon("disconnect");
	
	self notify("deleteSecondWeaponText");
	self endon("deleteSecondWeaponText");

 	self endon("secondary_weapon_changed");

		

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


	// * Existierende entfernen *
	self maps\mp\gametypes\_admiral_mod_hud::deletePlayerHudElementByName("secondary_weapon_top");    			
	self maps\mp\gametypes\_admiral_mod_hud::deletePlayerHudElementByName("secondary_weapon_bottom");    			
	self maps\mp\gametypes\_admiral_mod_hud::createPlayerTextHudElement("secondary_weapon_top", 440,80,"center","middle","fullscreen","fullscreen",true,&"AM_MP_SECOND_WEAPON",undefined,2.2,100,1,1,1,1);
	self maps\mp\gametypes\_admiral_mod_hud::createPlayerTextHudElement("secondary_weapon_bottom", 440,390,"center","middle","fullscreen","fullscreen",true,&"AM_MP_SECOND_WEAPON",undefined,2.2,100,1,1,1,1);

	wait game["am_secondary_weapon_selection_wait"];


	self maps\mp\gametypes\_admiral_mod_hud::deletePlayerHudElementByName("secondary_weapon_top");    			
	self maps\mp\gametypes\_admiral_mod_hud::deletePlayerHudElementByName("secondary_weapon_bottom");
	
	
    self closeMenu();
    self closeMenu();
 	wait 0.05;
 	self closeMenu();
 
 	self notify("secondary_weapon_not_changed");

 
}    


MakeWeaponLimit() {

	self endon("disconnect");


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


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


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




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

	weaponcount["none"] 					= 0;

	weaponcount["mosin_nagant_sniper_mp"] 	= 0;
	weaponcount["enfield_scope_mp"]			= 0;
	weaponcount["kar98k_sniper_mp"]			= 0;
	weaponcount["springfield_mp"]			= 0;
	weaponcount["SVT40_mp"]					= 0;
	
	weaponcount["enfield_mp"]				= 0;
	weaponcount["kar98k_mp"]				= 0;
	weaponcount["m1carbine_mp"]				= 0;
	weaponcount["m1garand_mp"]				= 0;
	weaponcount["mosin_nagant_mp"]			= 0;
	weaponcount["g43_mp"]					= 0;

	weaponcount["bar_mp"]					= 0;
	weaponcount["bren_mp"]					= 0;
	weaponcount["mp44_mp"]					= 0;
	weaponcount["ppsh_mp"]					= 0;
		
	weaponcount["sten_mp"]					= 0;
	weaponcount["mp40_mp"]					= 0;
	weaponcount["thompson_mp"]				= 0;
	weaponcount["PPS42_mp"]					= 0;
	weaponcount["greasegun_mp"]				= 0;

	weaponcount["colt_mp"]					= 0;
	weaponcount["luger_mp"]					= 0;
	weaponcount["TT30_mp"]					= 0;
	weaponcount["webley_mp"]				= 0;

	weaponcount["shotgun_mp"]				= 0;
	
	weaponcount["smokegrenade"]				= 0;
	weaponcount["fraggrenade"]				= 0;
	

	weaponcount["panzerschreck_mp"]			= 0;
	weaponcount["30cal_walk_mp"]			= 0;
	weaponcount["mg42_bipod_walk_mp"]		= 0;
	weaponcount["mortar_cannon_walk_mp"]	= 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")) {

			granaten_anzahl 		= player maps\mp\gametypes\_weapons::getFragGrenadeCount();
			smokegranaten_anzahl 	= player maps\mp\gametypes\_weapons::getSmokeGrenadeCount();
			primary					= player getWeaponSlotWeapon("primary");	
			primaryb				= player getWeaponSlotWeapon("primaryb");	


			// * Weapon-Arsenal *
			if(game["am_weapon_arsenal"] == 1) {
			
				if(isDefined(player.pers["weapon_count"]) && isDefined(player.pers["weapons"])) {
			
					for(w=0;w<player.pers["weapon_count"];w++) {
				
						if(isDefined(weaponcount[player.pers["weapons"][w]])) weaponcount[player.pers["weapons"][w]]++;	
					}			
				}				
			}
			else {
			
				if(isDefined(weaponcount[primary])) 	weaponcount[primary]++;
				if(isDefined(weaponcount[primaryb])) 	weaponcount[primaryb]++;	
			}
			
			
			
			// * andere Waffe für die nächsten Runde ausgewählt? *			
			if(isDefined(player.pers["next_spawn_weapon"])) {
			
				next_spawn_weapon = player.pers["next_spawn_weapon"];
				
				if(isDefined(weaponcount[next_spawn_weapon])) 	weaponcount[next_spawn_weapon]++;	
			}
						
						
			
			// * Granaten *
			weaponcount["fraggrenade"] 	+= granaten_anzahl;
			weaponcount["smokegrenade"] += smokegranaten_anzahl;
		}
	}

    //self setWeaponSlotAmmo("grenade",granaten_anzahl);      
   
   
    for(i=0;i<level.weaponnames.size;i++) {
        	
    	if(!isDefined(weaponcount) || !isDefined(weaponcount[level.weaponnames[i]])) continue;
    	   
    	   
    	//iprintln(level.weaponnames[i] + " (" +game["am_weapon_max_count"][level.weaponnames[i]] + "): " + weaponcount[level.weaponnames[i]]);
 	
    	
    	if(weaponcount[level.weaponnames[i]] >= game["am_weapon_max_count"][level.weaponnames[i]] && game["am_weapon_max_count"][level.weaponnames[i]] != -1) {
		
			self setClientCvar(level.weapons[level.weaponnames[i]].client_allowcvar, 2); 
		}	
		else {
				
			self setClientCvar(level.weapons[level.weaponnames[i]].client_allowcvar, 1); 
		}
    }

}


CheckWeaponIsLimited(weapon) {

	self endon("disconnect");

	if(!isDefined(weapon)) return false;
	if(game["am_weapon_limit"] != 1) return false;
	

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




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

	weaponcount["none"] 					= 0;

	weaponcount["mosin_nagant_sniper_mp"] 	= 0;
	weaponcount["enfield_scope_mp"]			= 0;
	weaponcount["kar98k_sniper_mp"]			= 0;
	weaponcount["springfield_mp"]			= 0;
	weaponcount["SVT40_mp"]					= 0;
	
	weaponcount["enfield_mp"]				= 0;
	weaponcount["kar98k_mp"]				= 0;
	weaponcount["m1carbine_mp"]				= 0;
	weaponcount["m1garand_mp"]				= 0;
	weaponcount["mosin_nagant_mp"]			= 0;
	weaponcount["g43_mp"]					= 0;

	weaponcount["bar_mp"]					= 0;
	weaponcount["bren_mp"]					= 0;
	weaponcount["mp44_mp"]					= 0;
	weaponcount["ppsh_mp"]					= 0;
		
	weaponcount["sten_mp"]					= 0;
	weaponcount["mp40_mp"]					= 0;
	weaponcount["thompson_mp"]				= 0;
	weaponcount["PPS42_mp"]					= 0;
	weaponcount["greasegun_mp"]				= 0;

	weaponcount["colt_mp"]					= 0;
	weaponcount["luger_mp"]					= 0;
	weaponcount["TT30_mp"]					= 0;
	weaponcount["webley_mp"]				= 0;

	weaponcount["shotgun_mp"]				= 0;
	
	weaponcount["smokegrenade"]				= 0;
	weaponcount["fraggrenade"]				= 0;
	

	weaponcount["panzerschreck_mp"]			= 0;
	weaponcount["30cal_walk_mp"]			= 0;
	weaponcount["mg42_bipod_walk_mp"]		= 0;
	weaponcount["mortar_cannon_walk_mp"]	= 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")) {

			primary					= player getWeaponSlotWeapon("primary");	
			primaryb				= player getWeaponSlotWeapon("primaryb");	


			// * Weapon-Arsenal *
			if(game["am_weapon_arsenal"] == 1) {
			
				if(isDefined(player.pers["weapon_count"]) && isDefined(player.pers["weapons"])) {
			
					for(w=0;w<player.pers["weapon_count"];w++) {
				
						if(isDefined(weaponcount[player.pers["weapons"][w]])) weaponcount[player.pers["weapons"][w]]++;	
					}			
				}				
			}
			else {
			
				if(isDefined(weaponcount[primary])) 	weaponcount[primary]++;
				if(isDefined(weaponcount[primaryb])) 	weaponcount[primaryb]++;	
			}
			
			
			
			// * andere Waffe für die nächsten Runde ausgewählt? *			
			if(isDefined(player.pers["next_spawn_weapon"])) {
			
				next_spawn_weapon = player.pers["next_spawn_weapon"];
				
				if(isDefined(weaponcount[next_spawn_weapon])) 	weaponcount[next_spawn_weapon]++;	
			}
		}
	}

     
    if(weaponcount[weapon] >= game["am_weapon_max_count"][weapon] && game["am_weapon_max_count"][weapon] != -1) {
		
		return true;
	}	 
     
    
    return false;
    
}



MakeWeaponLimitSpectators() {


	wait 1; // * 1 Sekunde warten, da bei einem RoundStart (MapReset) bei S&D zuerst keine Spieler vorhanden sind *

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

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

		player = players[i];		
		
		if(player.sessionstate == "spectator") {
	
			player setClientCvar("ui_allow_weaponchange", "1");
	
			player MakeWeaponLimit();
		}
	}
}




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


	entitytypes = getentarray();
	for(c = 0; c < entitytypes.size; c++) {
	
	
		iprintln("Entitytype: ", entitytypes[c].classname);

		entity_list = [];
		entity_list = getentarray(entitytypes[c].classname,"classname");

		for(e = 0; e < entity_list.size; e++)	if(isDefined(entity_list[e].name)) iprintln("Name: ", entity_list[e].name);

	}	
}


LimitTurrets(maxTurrets) {


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


	ents1 	= getentarray ("misc_turret","classname");
	ents2 	= getentarray ("misc_mg42","classname");
	
	
	ents = [];
	
	
	for (i=0;i<ents1.size;i++) ents[ents.size] = ents1[i];
	for (i=0;i<ents2.size;i++) ents[ents.size] = ents2[i];
		
		
	for (i=0;i<ents.size - maxTurrets;i++) {
		
		ents[i] delete();
	}
	
}


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

	
	if(game["am_grenade_return_throw"] == 0 &&  game["am_grenade_warning"] == 0) return;


	level endon("am_end_map");


	grenade_counter = 0;


	while(1) {
	
		
		
		grenades = getentarray("grenade","classname");
		
		for(i=0;i<grenades.size;i++) {

			
			
			if(isDefined(grenades[i].origin) && !isDefined(grenades[i].running)) {
			
				//if(isDefined(grenades[i].targetname)) iprintln(grenades[i].targetname);
			
				grenade_counter++;
														
							
				grenades[i].running = true;
				grenades[i].type = "none";
				grenades[i].id = grenade_counter;
				
				grenades[i] thread WaitForGrenadeOrigin();	
				grenades[i] thread GrenadeWaitForPickup();	
				
			}
		}
		
		//for (i=0;i<grenades.size-200;i++) grenades[i] delete();
	
	
		wait 0.5;
	}
}





WaitForGrenadeOrigin() {

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


	time = 2.8;

	
	old_origin = self.origin;

	
	while(isDefined(self)) {
	
		wait 0.2;
		time -= 0.2;


		
		// * Rauchgranaten löschen sich nicht selber, also nach 5 Sekunden löschen *
		if(isDefined(self)) {
	
					
			if(time < -5) {
			
				if(isDefined(self)) self delete();
				return;
			}			
		}
	
	
	
		if(isDefined(self)) {
		
					
			
			// * solange warten bis Granate sich nicht mehr bewegt *
			if(self.origin == old_origin) {
					
				
				// * Warnung vor Handgranate *
				if(game["am_grenade_warning"] == 1) {

					// * Nur wenn Flugzeit der Granate erkennbar geworfen (und nicht bei einem Kill gedropped) *
					if(time <= 2.2)  {

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

							if(players[i].sessionstate == "playing" && distance(players[i].origin,old_origin) < 200) {			

								if(isDefined(self)) players[i] thread maps\mp\gametypes\_admiral_mod_sounds::TalkGrenadeWarning(old_origin,500);						
								break;								
							}
						}
					}					
				}


				// * Allen in der Nähe stehenden Spieler "Pickup" anzeigen *
				if(game["am_grenade_return_throw"] == 1) {

					players = getentarray("player", "classname");
					for(i = 0; i < players.size; i++) {             
			
						if(players[i].sessionstate == "playing" && distance(players[i].origin,old_origin) < 700) {			

							if(isDefined(self)) players[i] thread PlayerIndicateGrenadePickup(self,old_origin,time);						
						}
					}
				
					return;
				}
				
				return;
			}
		
			old_origin = self.origin;
		}
	}
}		

PlayerIndicateGrenadePickup(grenade,origin,time) {
	
	if(game["am_error_logfile"] == 1) logPrint("PlayerIndicateGrenadePickup\n");


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


	self endon("disconnect");
	

	while(1) {
	
		wait 0.2;
		time -= 0.2;
		
		if(isDefined(grenade)) {
				
			newobjpoint = newClientHudElem(self);
			newobjpoint.name = "grenade_pickup";
			newobjpoint.x = origin[0];
			newobjpoint.y = origin[1];
			newobjpoint.z = origin[2];
			newobjpoint.alpha = .61;
			newobjpoint.archived = false;
			newobjpoint setShader("gfx/custom/obj_pickup.tga", 7, 7);
			newobjpoint setwaypoint(true);	

			if(time > 0.1) {

				for(i=0;i<time;i+=0.1) {

					wait 0.1;

					if(!isDefined(grenade)) {

						if(isDefined(newobjpoint)) newobjpoint destroy();
						return;
					}
				}
			}

			if(isDefined(newobjpoint)) newobjpoint destroy();
			return;
				
		}
	}
}	



GrenadeWaitForPickup() {


	if(game["am_grenade_return_throw"] != 1) return;
	
	
	if(game["am_error_logfile"] == 1) logPrint("GrenadeWaitForPickup\n");


	time = 0;

	while(1) {


		if(!isDefined(self)) break;

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

		for(i = 0; i < players.size; i++) {             
			
			
			if(!isDefined(self)) break;
			

			player = players[i];

			if(isAlive(player) && player.sessionstate == "playing") {
						
							
				if(distance(player.origin,self.origin) < 70 && player useButtonPressed()) {
			
					offset = 18;
			
					if(players[i].pers["am_stance"] == 1) offset = 2;
					if(players[i].pers["am_stance"] == 0) offset = -27;

					// * Nur wenn der Spieler die Granate anguckt, ansonsten einfach übersprinten *								
					target_range = vectordot(anglestoforward(player getplayerangles()),anglestoforward(vectortoangles(vectornormalize((player getEye() + (0,0,offset)) - self.origin)))) * -1;


					if(target_range >= 0.97) {


						granaten_typ	= player getFragGrenadeType();
						granaten_anzahl = player getammocount(granaten_typ);


						player.grenade_picked_up = true;
						
						player.weaponbeforepickedup = player getcurrentweapon();


						player switchToWeapon(granaten_typ);					
						player playsound("grenade_pickup");					

						player setWeaponClipAmmo(granaten_typ, granaten_anzahl + 1);				
						
						if(time >= 3.5) time = 0.1;
						player thread AwaintingThrow(3.5 - time);

						wait 0.1;
						if(isDefined(self)) self delete();
						break;
					}
				}
			}
		}
		
		wait 0.2;
		time += 0.2;
		
		// * wenn > 5.5 und noch existiert, dann ist es eine Rauchgranate, und Pickup unterbrechen *
		if(time > 5.5) {
			
			if(isDefined(self)) self delete();
			break;
		}
	}
}


AwaintingThrow(time_to_throw) {

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


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

	
	self thread GrenadeThrowTimeHud(time_to_throw);
	
	
	while(self.sessionstate == "playing" && !self attackButtonPressed()) {
	
	
		time_to_throw -= 0.1;
		wait 0.1;
		
		if(time_to_throw <= 0) {
			
			self playsound("grenade_explode_dirt");
			playfx(game["grenade_explosion"],self.origin);
			
			self.grenade_picked_up 		= undefined;
			self.weaponbeforepickedup 	= undefined;

			self GrenadeThrowTimeHudDelete();
			self suicide();
			
			
			break;	
		}
	}
	
	
	wait 1;
	self GrenadeThrowTimeHudDelete();
	
	
	
	if(isDefined(self.weaponbeforepickedup) && self.weaponbeforepickedup != "none") {

		self switchToWeapon(self.weaponbeforepickedup);	
		
		wait 0.5;
	}
	
		
	self.weaponbeforepickedup 	= undefined;
	self.grenade_picked_up 		= undefined;
		
}


GrenadeThrowTimeHud(time_to_throw) {

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


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


   	self maps\mp\gametypes\_admiral_mod_hud::deletePlayerHudElementByName("GrenadeThrowTime");
 	self maps\mp\gametypes\_admiral_mod_hud::createPlayerHudElement("GrenadeThrowTime", 280,415,"left","top","fullscreen","fullscreen",false,"white",100,6,2,1,1,0,0);
	
	self maps\mp\gametypes\_admiral_mod_hud::scaleOverPlayerHudElement("GrenadeThrowTime",time_to_throw, 1, 6);    								

	self GrenadeThrowTimeHudDelete();
}

GrenadeThrowTimeHudDelete() {

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


	self endon("disconnect");
	self endon("killed_player");
   	
   	self maps\mp\gametypes\_admiral_mod_hud::deletePlayerHudElementByName("GrenadeThrowTime");
}



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


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


	if(self.pers["team"] == "allies") {
	
		switch(game["allies"]) {
		
		case "american":
			fraggrenadetype = "frag_grenade_american_mp";
			
			if(game["am_frag_grenades_type"] == "am_cooked") 		fraggrenadetype = "frag_grenade_american_mp"; 	
			if(game["am_frag_grenades_type"] == "am_cooked_slow") 	fraggrenadetype = "slow_frag_grenade_american_mp"; 		
			if(game["am_frag_grenades_type"] == "original") 		fraggrenadetype = "original_frag_grenade_american_mp";		
			if(game["am_frag_grenades_type"] == "original_slow") 	fraggrenadetype = "original_slow_frag_grenade_american_mp";		

			break;

		case "british":
			fraggrenadetype = "frag_grenade_british_mp";
			
			if(game["am_frag_grenades_type"] == "am_cooked") 		fraggrenadetype = "frag_grenade_british_mp"; 	
			if(game["am_frag_grenades_type"] == "am_cooked_slow") 	fraggrenadetype = "slow_frag_grenade_british_mp"; 		
			if(game["am_frag_grenades_type"] == "original") 		fraggrenadetype = "original_frag_grenade_british_mp";		
			if(game["am_frag_grenades_type"] == "original_slow") 	fraggrenadetype = "original_slow_frag_grenade_british_mp";		
			
			break;

		default:
			fraggrenadetype = "frag_grenade_russian_mp";
			
			if(game["am_frag_grenades_type"] == "am_cooked") 		fraggrenadetype = "frag_grenade_russian_mp"; 	
			if(game["am_frag_grenades_type"] == "am_cooked_slow") 	fraggrenadetype = "slow_frag_grenade_russian_mp"; 		
			if(game["am_frag_grenades_type"] == "original") 		fraggrenadetype = "original_frag_grenade_russian_mp";		
			if(game["am_frag_grenades_type"] == "original_slow") 	fraggrenadetype = "original_slow_frag_grenade_russian_mp";		
			
			break;
		}
	}
	else {		
	
		fraggrenadetype = "frag_grenade_german_mp";	
			
		if(game["am_frag_grenades_type"] == "am_cooked") 		fraggrenadetype = "frag_grenade_german_mp"; 	
		if(game["am_frag_grenades_type"] == "am_cooked_slow") 	fraggrenadetype = "slow_frag_grenade_german_mp"; 		
		if(game["am_frag_grenades_type"] == "original") 		fraggrenadetype = "original_frag_grenade_german_mp";		
		if(game["am_frag_grenades_type"] == "original_slow") 	fraggrenadetype = "original_slow_frag_grenade_german_mp";		
			
	}

	return fraggrenadetype;
}


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


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


	if(self.pers["team"] == "allies") {
	
		switch(game["allies"]) {
		
		case "american":
			smokegrenadetype = "smoke_grenade_american_mp";
			break;

		case "british":
			smokegrenadetype = "smoke_grenade_british_mp";
			break;

		default:
			smokegrenadetype = "smoke_grenade_russian_mp";
			break;
		}
	}
	else {		
	
		smokegrenadetype = "smoke_grenade_german_mp";		
	}


	// * Wenn farbige Rauchgranaten *	
	if(game["am_use_colored_smokegrenades"] == 1) {	
	
		f = randomint(4);
	
		if(f == 0) smokegrenadetype = "red_smoke_grenade_mp";	
		if(f == 1) smokegrenadetype = "blue_smoke_grenade_mp";	
		if(f == 2) smokegrenadetype = "green_smoke_grenade_mp";	
		if(f == 3) smokegrenadetype = "yellow_smoke_grenade_mp";	
	}



	return smokegrenadetype;
}



WeaponDrop(typ, weapon, ammosize, reservesize, origin, seconds) {

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


	// Typ: "offhand" oder "slot"


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


    // * Wenn in der Rotation in der akt. Reihenposition schon ein Weapon existiert, dann zum überschreiben löschen *
    if(isDefined(game["am_weaponqueue"][game["am_weaponqueuecurrent"]]))  {
    
    	game["am_weaponqueue"][game["am_weaponqueuecurrent"]] delete();
    }



	weapon = maps\mp\gametypes\_admiral_mod_utils::deleteAMextension(weapon);


	switch(weapon) {
	
		case "greasegun_mp":
			xmodel = "xmodel/weapon_greasegun";
			break;
		case "m1carbine_mp":
			xmodel = "xmodel/weapon_m1carbine";
			break;
		case "m1garand_mp":
			xmodel = "xmodel/weapon_m1garand";
			break;
		case "thompson_mp":
			xmodel = "xmodel/weapon_thompson";
			break;
		case "bar_mp":
			xmodel = "xmodel/weapon_bar";
			break;
		case "springfield_mp":
			xmodel = "xmodel/weapon_springfield";
			break;
		case "sten_mp":
			xmodel = "xmodel/weapon_sten";
			break;
		case "enfield_mp":
			xmodel = "xmodel/weapon_enfield";
			break;
		case "bren_mp":
			xmodel = "xmodel/weapon_bren";
			break;
		case "enfield_scope_mp":
			xmodel = "xmodel/weapon_enfield_scope";
			break;
		case "mosin_nagant_mp":
			xmodel = "xmodel/weapon_mosinnagant";
			break;
		case "SVT40_mp":
			xmodel = "xmodel/weapon_svt40";
			break;
		case "PPS42_mp":
			xmodel = "xmodel/weapon_pps43";
			break;
		case "ppsh_mp":
			xmodel = "xmodel/weapon_ppsh";
			break;
		case "mosin_nagant_sniper_mp":
			xmodel = "xmodel/weapon_mosinnagantscoped_cloth";
			break;
		case "kar98k_mp":
			xmodel = "xmodel/weapon_kar98";
			break;
		case "g43_mp":
			xmodel = "xmodel/weapon_g43";
			break;
		case "mp40_mp":
			xmodel = "xmodel/weapon_mp40";
			break;
		case "mp44_mp":
			xmodel = "xmodel/weapon_mp44";
			break;
		case "kar98k_sniper_mp":
			xmodel = "xmodel/weapon_kar98_scoped";
			break;
		case "shotgun_mp":
			xmodel = "xmodel/weapon_trenchgun";
			break;
		case "frag_grenade_american_mp":
		case "slow_frag_grenade_american_mp":
			xmodel = "xmodel/weapon_mk2fraggrenade";
			break;
		case "frag_grenade_british_mp":
		case "slow_frag_grenade_british_mp":
			xmodel = "xmodel/weapon_mk1grenade";
			break;
		case "frag_grenade_russian_mp":
		case "slow_frag_grenade_russian_mp":
			xmodel = "xmodel/weapon_russian_handgrenade";
			break;
		case "frag_grenade_german_mp":
		case "slow_frag_grenade_german_mp":
			xmodel = "xmodel/weapon_nebelhandgrenate";
			break;
		case "smoke_grenade_american_mp":
			xmodel = "xmodel/weapon_us_smoke_grenade";
			break;
		case "smoke_grenade_british_mp":
			xmodel = "xmodel/weapon_us_smoke_grenade";
			break;
		case "smoke_grenade_russian_mp":
			xmodel = "xmodel/weapon_us_smoke_grenade";
			break;
		case "smoke_grenade_german_mp":
			xmodel = "xmodel/weapon_us_smoke_grenade";
			break;


		case "red_smoke_grenade_mp":
			xmodel = "xmodel/weapon_us_smoke_grenade";
			break;
		case "blue_smoke_grenade_mp":
			xmodel = "xmodel/weapon_us_smoke_grenade";
			break;
		case "green_smoke_grenade_mp":
			xmodel = "xmodel/weapon_us_smoke_grenade";
			break;
		case "yellow_smoke_grenade_mp":
			xmodel = "xmodel/weapon_us_smoke_grenade";
			break;


		case "colt_mp":
			xmodel = "xmodel/weapon_colt45";
			break;
		case "webley_mp":
			xmodel = "xmodel/weapon_webley";
			break;
		case "TT30_mp":
			xmodel = "xmodel/weapon_tt30";
			break;
		case "luger_mp":
			xmodel = "xmodel/weapon_luger";
			break;		



		case "panzerschreck_mp":
			xmodel = "xmodel/weapon_panzerschreck";
			break;
		case "30cal_walk_mp":
			xmodel = "xmodel/weapon_30cal_carry";
			break;
		case "mg42_bipod_walk_mp":
			xmodel = "xmodel/weapon_mg42_carry";
			break;
		case "mortar_cannon_walk_mp":
			xmodel = "xmodel/prop_mortar";

	

		
		default:
			level.weapon_is_dropping = undefined;
			return;
	}			


	weapon_object = spawn("script_model", (0,0,0));	
	weapon_object setModel(xmodel);
	weapon_object.targetname = "weapon_drop";
	weapon_object hide(); 	

	trace = bulletTrace(origin, origin + (0,0,-1000),false,undefined);		
	weapon_object.origin = trace["position"] + (0, 0, 1);
	weapon_object.angles = (0, randomint(360), 90);
	weapon_object show(); 


    thread WeaponPickupThink(typ, weapon, ammosize, reservesize, weapon_object, weapon_object.origin, seconds);
 
  

	game["am_weaponqueue"][game["am_weaponqueuecurrent"]] = weapon_object;   
    game["am_weaponqueuecurrent"]++;    
    
    if(game["am_weaponqueuecurrent"] >= 25)  game["am_weaponqueuecurrent"] = 0;
    

	level.weapon_is_dropping = undefined;

}


WeaponPickupThink(typ, weapon, ammosize, reservesize, weapon_object, origin, seconds) {

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

	
	range = 80;


	level endon("am_end_map");


	delete_weapon = false;


	
	trigger = spawn("trigger_radius", origin, 0, range, 70);	

	level thread WeaponWaitForDelete(weapon_object, seconds);
			
			
	while(isDefined(weapon_object)) {
	
		wait 0.1;			
		trigger waittill("trigger", other);	


		// * Sonst fehler bei neuen Spielern! * 
		if(!isPlayer(other) || !isDefined(other.do_status)) continue;

		// * Wenn Spieler grad was andere tut, dann kein Weapon Pickup *
		if(other.do_status != "none" || other.do_status == "defuse" || other.do_status == "plant") continue;				



		if(other.sessionstate == "playing" && !isDefined(other.pers["sprint"]) && !isDefined(self.grenade_picked_up) && distance(other.origin, origin) < range) {

			if(!isDefined(weapon_object)) break;


			// * Wenn Spieler Spion, dann keine andere Waffe aufnehmbar *
			if(other.pers["player_class"] == 7) {

				if(!isDefined(other.weapon_pickup_warning)) {									

					other.weapon_pickup_warning = true;
					other thread WeaponPickupWarning();
				}

				continue;
			}


			if(typ != "offhand" && !isDefined(other.weapon_pickup_thread_started)) {

				// * Warten auf USE_Button starten *
				other.do_status = "weapon_pickup_wait_for_button";
				other.weapon_pickup_thread_started = true;				
				other thread WeaponPickupThinkWaitForButton(typ, weapon, ammosize, reservesize, weapon_object, origin, range);				
				other setClientCvar("am_ui_show_pickup_weapon", maps\mp\gametypes\_weapons::getweapon_nr(weapon));
				other.pers["last_pickup_weapon_shown"] = weapon;
				continue;
				
			}
			else if(typ == "offhand" && !isDefined(other.offhand_pickup_thread_started)) {

				// * Automatische Aufnahme von Offhand Weapons *										
				other.do_status = "weapon_pickup_wait_for_button";
				other.offhand_pickup_thread_started = true;
				other thread WeaponPickupThinkWaitForButton(typ, weapon, ammosize, reservesize, weapon_object, origin, range);				
				break;
			}				
		}		

	}
	
	
	if(isDefined(trigger)) trigger delete();
}


WeaponWaitForDelete(weapon_object, seconds) {
	
	while(seconds > 0) {
	
		wait 1;
		seconds -= 1;
	}	
	
	if(isDefined(weapon_object)) weapon_object delete();	
}




WeaponPickupWarning() {

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

	self maps\mp\gametypes\_admiral_mod_hud::showWeaponPickupInfo("@AM_MP_LNG_PICKUP_WEAPON_WARNING");
	wait 2;
	self maps\mp\gametypes\_admiral_mod_hud::deleteWeaponPickupInfo();


	if(isDefined(self.weapon_pickup_warning)) self.weapon_pickup_warning = undefined;
}



WeaponPickupThinkWaitForButton(typ, weapon, ammosize, reservesize, weapon_object, origin, range) {

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


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



	if(typ != "offhand") {
	
		if(game["am_weapon_pickup_key"] == 0 && game["am_weapon_pickup_player_must_look_at"] == 0) self maps\mp\gametypes\_admiral_mod_hud::showWeaponPickupInfo("@AM_MP_LNG_PICKUP_WEAPON_INFO_MELEE");
	    if(game["am_weapon_pickup_key"] == 1 && game["am_weapon_pickup_player_must_look_at"] == 0) self maps\mp\gametypes\_admiral_mod_hud::showWeaponPickupInfo("@AM_MP_LNG_PICKUP_WEAPON_INFO_USE");
		if(game["am_weapon_pickup_key"] == 0 && game["am_weapon_pickup_player_must_look_at"] == 1) self maps\mp\gametypes\_admiral_mod_hud::showWeaponPickupInfo("@AM_MP_LNG_LOOK_PICKUP_WEAPON_INFO_MELEE");
	    if(game["am_weapon_pickup_key"] == 1 && game["am_weapon_pickup_player_must_look_at"] == 1) self maps\mp\gametypes\_admiral_mod_hud::showWeaponPickupInfo("@AM_MP_LNG_LOOK_PICKUP_WEAPON_INFO_USE");
	}

	while(isDefined(weapon_object) && self.sessionstate == "playing" && distance(self.origin, origin) < range && self.do_status == "weapon_pickup_wait_for_button") {
	
	
		wait 0.1;
	
	
		pickupKeyPressed = false;
		if(game["am_weapon_pickup_key"] == 1 && self useButtonPressed()) 	pickupKeyPressed = true;
		if(game["am_weapon_pickup_key"] == 0 && self meleeButtonPressed()) 	pickupKeyPressed = true;
		
		
		if(pickupKeyPressed && typ != "offhand") {	

		
			if(game["am_weapon_pickup_player_must_look_at"] == 1) {
			
				offset = 18;

				if(self.pers["am_stance"] == 1) offset = 2;
				if(self.pers["am_stance"] == 0) offset = -27;

				// * Nur wenn der Spieler die Waffe anguckt *								
				target_range = vectordot(anglestoforward(self getplayerangles()),anglestoforward(vectortoangles(vectornormalize((self getEye() + (0,0,offset)) - origin)))) * -1;
					
				//self iprintln(target_range);	
					
				if(target_range < 0.965) continue;
			}


			current = self getcurrentweapon();
			weapon1 = self getweaponslotweapon("primary");
			weapon2 = self getweaponslotweapon("primaryb");

			if(current == weapon1)	currentslot = "primary";
			else					currentslot = "primaryb";

			if(currentslot == "none") continue;
			

			slotclipammo 	= self getweaponslotclipammo(currentslot);
			slotammo		= self getweaponslotammo(currentslot);



			// * Wenn Player diese Waffe noch nicht hat, dann aufnehmen * 
			if(weapon != weapon1 && weapon != weapon2) {

				self.deny_weapon_arsenal = true; 
				
				// * Nur Wait wenn Weapon Arsenal benutzt wird, sonst Pickup zu sehr verzögert *
				if(game["am_weapon_arsenal"] == 1) wait 0.3;
				
				
				level thread WeaponDrop("slot", current, slotclipammo, slotammo, origin, game["am_delete_dropped_weapon_time"]);


				//if(isDefined(weapon_object)) 	weapon_object playsound("weap_ammo_pickup",origin);	
				if(isDefined(weapon_object)) 	weapon_object playsound("weap_pickup",origin);
				if(isDefined(weapon_object)) 	weapon_object delete();

				self setWeaponSlotWeapon(currentslot, weapon);			
				self setWeaponSlotClipAmmo(currentslot, ammosize);
				self setWeaponSlotAmmo(currentslot, reservesize);
				wait 0.1;
				self switchToWeapon(weapon);
				wait 0.1;	
								
				self.deny_weapon_arsenal = undefined;
				
				
				break;			
			}
			else {

				// * Falls Weapon Hinweis noch angezeigt wird, dann löschen *
				if(isDefined(self.pers["last_pickup_weapon_shown"])) self setClientCvar("am_ui_show_pickup_weapon" , "0");

			
				// * Wenn der Spieler diese Waffe schon hat, dann Hinweis *
				if(typ != "offhand") {

			
					// * Waffen-Munition aufnehmen und Waffe löschen, wenn eigene Waffe nicht Full Ammo *
					wslot = "primaryb";
					if(weapon == weapon1) 	wslot = "primary";					
										
					vorher_clipammo = self getweaponslotclipammo(wslot);
					vorher_ammo		= self getweaponslotammo(wslot);
					
					self setWeaponSlotClipAmmo(wslot, 999);
					self setWeaponSlotAmmo(wslot, 999);
					
					nachher_clipammo = self getweaponslotclipammo(wslot);
					nachher_ammo	 = self getweaponslotammo(wslot);

					// * Wenn Muni aufgenommen, dann Waffe löschen *
					if(vorher_clipammo != nachher_clipammo || vorher_ammo != nachher_ammo) {


						self playsound("weap_ammo_pickup",origin);	
						if(isDefined(weapon_object)) 	weapon_object delete();
						
						break;	// * beenden * 			
					}
					

					// * Ansonsten "Waffe schon vorhanden" *
					self maps\mp\gametypes\_admiral_mod_hud::showWeaponPickupInfo("@AM_MP_LNG_PICKUP_WEAPON_HAS_SAME_WEAPON");
					wait 2;
					self maps\mp\gametypes\_admiral_mod_hud::deleteWeaponPickupInfo();
					
					// * Wenn Waffe, dann Info wieder anzeigen *
					if(isDefined(self.pers["last_pickup_weapon_shown"])) {
					
						if(game["am_weapon_pickup_key"] == 0 && game["am_weapon_pickup_player_must_look_at"] == 0) self maps\mp\gametypes\_admiral_mod_hud::showWeaponPickupInfo("@AM_MP_LNG_PICKUP_WEAPON_INFO_MELEE");
						if(game["am_weapon_pickup_key"] == 1 && game["am_weapon_pickup_player_must_look_at"] == 0) self maps\mp\gametypes\_admiral_mod_hud::showWeaponPickupInfo("@AM_MP_LNG_PICKUP_WEAPON_INFO_USE");
						if(game["am_weapon_pickup_key"] == 0 && game["am_weapon_pickup_player_must_look_at"] == 1) self maps\mp\gametypes\_admiral_mod_hud::showWeaponPickupInfo("@AM_MP_LNG_LOOK_PICKUP_WEAPON_INFO_MELEE");
						if(game["am_weapon_pickup_key"] == 1 && game["am_weapon_pickup_player_must_look_at"] == 1) self maps\mp\gametypes\_admiral_mod_hud::showWeaponPickupInfo("@AM_MP_LNG_LOOK_PICKUP_WEAPON_INFO_USE");
						
						self setClientCvar("am_ui_show_pickup_weapon" , maps\mp\gametypes\_weapons::getweapon_nr(self.pers["last_pickup_weapon_shown"]));
					}
				}
			
			}
		}	
		
		if(typ == "offhand") {


			wait 0.2;

			if(maps\mp\gametypes\_admiral_mod_utils::find_first_string(weapon, "frag_grenade") > -1) {

				anzahl 	= self maps\mp\gametypes\_weapons::getFragGrenadeCount();
				type 	= self getFragGrenadeType();								
					
					
				// * Wenn maximale Anzahl erreicht und keine weiteren aufnehmen *
				if(anzahl >= game["am_pickup_max_frag_grenades"] || (game["am_frag_grenade_count"] > -1 && anzahl >= game["am_frag_grenade_count"])) break;			

				if(anzahl + ammosize >= 10) stueck = 10;
				else 						stueck = anzahl + ammosize;

				if(game["am_frag_grenade_count"] > -1 && anzahl >= game["am_frag_grenade_count"]) stueck = game["am_frag_grenade_count"];
			}
			else {

				anzahl 	= self maps\mp\gametypes\_weapons::getSmokeGrenadeCount();
				type 	= self getSmokeGrenadeType();	


				// * Wenn maximale Anzahl erreicht und keine weiteren aufnehmen *
				if(anzahl >= game["am_pickup_max_smoke_grenades"] || (game["am_smoke_grenade_count"] > -1 && anzahl >= game["am_smoke_grenade_count"])) break;

				if(anzahl + ammosize >= 10)	stueck = 10;
				else 						stueck = anzahl + ammosize;

				if(game["am_smoke_grenade_count"] > -1 && anzahl >= game["am_smoke_grenade_count"]) stueck = game["am_smoke_grenade_count"];
			}

					

			if(isDefined(weapon_object)) weapon_object playsound("weap_ammo_pickup",origin);	
			if(isDefined(weapon_object)) weapon_object delete();
						
			
			self setWeaponClipAmmo(type, stueck);
			
						
			break;
		}			
	}	

	if(typ != "offhand") {
		
		self maps\mp\gametypes\_admiral_mod_hud::deleteWeaponPickupInfo();
		
		if(isDefined(self.pers["last_pickup_weapon_shown"])) self setClientCvar("am_ui_show_pickup_weapon" , "0");
	}
	
	if(typ == "offhand") 	self.offhand_pickup_thread_started 	= undefined;
	else 					self.weapon_pickup_thread_started 	= undefined;	
	
	
	if(isDefined(self.do_status) && self.do_status == "weapon_pickup_wait_for_button") self.do_status = "none";
}




isSniper(sWeapon) {

	if(!isDefined(sWeapon)) return false;
	

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

	
	if(sWeapon == "mosin_nagant_sniper_mp") return true;
	if(sWeapon == "enfield_scope_mp") 		return true;
	if(sWeapon == "kar98k_sniper_mp") 		return true;
	if(sWeapon == "springfield_mp") 		return true;

	return false;
}






TeamWeaponCheck(response) {


	// * Wenn leer dann nicht weiter beachten *
	if(!isDefined(response)) return true;
	
	
	if(game["am_only_rifles"] == 1)  return true;	
	if(game["am_only_snipers"] == 1)  return true;	

	switch(response) {
	
		case "luger_mp":
			return true;		
		case "kar98k_mp":
			return true;			
		case "g43_mp":
			return true;	
		case "mp40_mp":
			return true;	
		case "mp44_mp":
			return true;	
		case "kar98k_sniper_mp":
			return true;	
		case "shotgun_mp":
			return true;
		case "thompson_mp":
			return true;
		case "panzerschreck_mp":
			return true;
		case "mg42_bipod_walk_mp":
			return true;
		case "mortar_cannon_walk_mp":
			return true;	
		
	}


	if(game["allies"] == "american") {
	
		switch(response) {
		
			case "colt_mp":
				return true;	
			case "springfield_mp":
				return true;	
			case "greasegun_mp":
				return true;	
			case "thompson_mp":
				return true;		
			case "m1carbine_mp":
				return true;		
			case "m1garand_mp":
				return true;	
			case "bar_mp":
				return true;
			case "shotgun_mp":
				return true;	
			case "enfield_mp":
				return true;	
			case "30cal_walk_mp":
				return true;	
			case "mortar_cannon_walk_mp":
				return true;	
				
				
		}
		
		return false;
	}


	if(game["allies"] == "british") {
	
	
		switch(response) {
		
			case "webley_mp":
				return true;
			case "sten_mp":
				return true;	
			case "thompson_mp":
				return true;				
			case "m1garand_mp":
				return true;	
			case "enfield_mp":
				return true;	
			case "enfield_scope_mp":
				return true;	
			case "bren_mp":
				return true;	
			case "shotgun_mp":
				return true;	
			case "30cal_walk_mp":
				return true;	
			case "mortar_cannon_walk_mp":
				return true;	
		}
		
		return false;
	}

	if(game["allies"] == "russian") {
	
	
		switch(response) {
	
			case "TT30_mp":
				return true;
			case "mosin_nagant_mp":
				return true;	
			case "SVT40_mp":
				return true;	
			case "PPS42_mp":
				return true;	
			case "ppsh_mp":
				return true;	
			case "mosin_nagant_sniper_mp":
				return true;			
			case "shotgun_mp":
				return true;	
			case "30cal_walk_mp":
				return true;	
			case "bar_mp":
				return true;
			case "mortar_cannon_walk_mp":
				return true;	
		}	
		
		return false;
	}


	return false;
	
}



GetWeaponName(weapon) {


	switch(weapon) {
	
		case "greasegun_mp":
			w = "Greasegun";
			break;
		case "m1carbine_mp":
			w = "MCarbine";
			break;
		case "m1garand_mp":
			w = "M1Garand";
			break;
		case "thompson_mp":
			w = "Thompson";
			break;
		case "bar_mp":
			w = "Bar";
			break;
		case "springfield_mp":
			w = "Springfield";
			break;
		case "sten_mp":
			w = "Sten";
			break;
		case "enfield_mp":
			w = "Enfield";
			break;
		case "bren_mp":
			w = "Bren";
			break;
		case "enfield_scope_mp":
			w = "Enfield Scope";
			break;
		case "mosin_nagant_mp":
			w = "Mosinnagant";
			break;
		case "SVT40_mp":
			w = "SVT40";
			break;
		case "PPS42_mp":
			w = "PPS43";
			break;
		case "ppsh_mp":
			w = "PPSH";
			break;
		case "mosin_nagant_sniper_mp":
			w = "Mosinnagant Scoped";
			break;
		case "kar98k_mp":
			w = "Kar98";
			break;
		case "g43_mp":
			w = "G43";
			break;
		case "mp40_mp":
			w = "MP40";
			break;
		case "mp44_mp":
			w = "MP44";
			break;
		case "kar98k_sniper_mp":
			w = "Kar98 Scoped";
			break;
		case "shotgun_mp":
			w = "Trenchgun";
			break;
		case "frag_grenade_american_mp":
			w = "Grenade";
			break;
		case "frag_grenade_british_mp":
			w = "Grenade";
			break;
		case "frag_grenade_russian_mp":
			w = "Grenade";
			break;
		case "frag_grenade_german_mp":
			w = "Grenade";
			break;
			
		case "original_frag_grenade_american_mp":
			w = "Grenade";
			break;
		case "original_frag_grenade_british_mp":
			w = "Grenade";
			break;
		case "original_frag_grenade_russian_mp":
			w = "Grenade";
			break;
		case "original_frag_grenade_german_mp":
			w = "Grenade";
			break;			
			
		case "slow_frag_grenade_american_mp":
			w = "Grenade";
			break;
		case "slow_frag_grenade_british_mp":
			w = "Grenade";
			break;
		case "slow_frag_grenade_russian_mp":
			w = "Grenade";
			break;
		case "slow_frag_grenade_german_mp":
			w = "Grenade";
			break;			
			
		case "original_slow_frag_grenade_american_mp":
			w = "Grenade";
			break;
		case "original_slow_frag_grenade_british_mp":
			w = "Grenade";
			break;
		case "original_slow_frag_grenade_russian_mp":
			w = "Grenade";
			break;
		case "original_slow_frag_grenade_german_mp":
			w = "Grenade";
			break;			

		case "smoke_grenade_american_mp":
			w = "Smokegrenade";
			break;
		case "smoke_grenade_british_mp":
			w = "Smokegrenade";
			break;
		case "smoke_grenade_russian_mp":
			w = "Smokegrenade";
			break;
		case "smoke_grenade_german_mp":
			w = "Smokegrenade";
			break;

		case "red_smoke_grenade_mp":
			w = "Smokegrenade";
			break;
		case "blue_smoke_grenade_mp":
			w = "Smokegrenade";
			break;
		case "green_smoke_grenade_mp":
			w = "Smokegrenade";
			break;
		case "yellow_smoke_grenade_mp":
			w = "Smokegrenade";
			break;


		case "colt_mp":
			w = "Colt45";
			break;
		case "webley_mp":
			w = "Webley";
			break;
		case "TT30_mp":
			w = "TT30";
			break;
		case "luger_mp":
			w = "Luger";
			break;
		
		
		
		case "panzerschreck_mp":
			w = "Panzerschreck";
			break;


		case "30cal_walk_mp":
			w = "30cal";
			break;
		case "30cal_stand_mp":
			w = "30cal";
			break;
		case "30cal_duck_mp":
			w = "30cal";
			break;
		case "30cal_prone_mp":
			w = "30cal";
			break;

		case "mg42_bipod_walk_mp":
			w = "MG42";
			break;
		case "mg42_bipod_stand_mp":
			w = "MG42";
			break;
		case "mg42_bipod_duck_mp":
			w = "MG42";
			break;
		case "mg42_bipod_prone_mp":
			w = "MG42";
			break;

		case "mortar_cannon_walk_mp":
			w = "Mortar Cannon";
			break;


	
		case "gas_grenade_mp":
			w = "Gas Grenade";
			break;

		case "fire_grenade_mp":
			w = "Fire Grenade";
			break;

		case "knife_mp":
			w = "Knife";
			break;

		
		default:
			w = weapon;
			break;		
	}			
	
						
	return w;	

}


ModifyWeaponDamage(iDamage, sWeapon) {

	
	if(isDefined(game["am_weapon_damage"][sWeapon]) && game["am_weapon_damage"][sWeapon] > 0 && game["am_weapon_damage"][sWeapon] < 5) {
			
		iDamage = int(iDamage * game["am_weapon_damage"][sWeapon]);
	}	

		
	return iDamage;

}




