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


	
getNextMap() {	


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

	
	map_name 		= game["am_mapname"];
	first_map		= "";
	first_map_i		= 0;
	current_map 	= "";
	next_map_name 	= "";

	

	d_split = str_split(toLower(game["am_sv_mapRotation"])," ");
	
 	for(i=0;i<d_split.size;i++) {
     
        if(d_split[i] == "map") {

			if(current_map != "" && isDefined(d_split[i+1]) && d_split[i+1] != "") {
			
				next_map_name 	= d_split[i+1];
				break;
			}
             
            if(first_map == "" && isDefined(d_split[i+1])) {
            
            	first_map 		= d_split[i+1];
            	first_map_i		= i;
            }
            if(current_map == "" && isDefined(d_split[i+1]) && d_split[i+1] == game["am_mapname"]) 	current_map = d_split[i+1];           
        }
    }   
    
    // * Wenn die letzte Map, dann ist die nächste die erste Map *
    if(next_map_name == "" && first_map != "") {
    
    	next_map_name = first_map;
    	i = first_map_i;
    }
    
    
    return next_map_name;
}



getNextGametype() {
    
	if(game["am_error_logfile"] == 1) logPrint("getNextGametype\n");
    
    
    gametype = "";
    
		
	map_name 		= game["am_mapname"];
	first_map		= "";
	first_map_i		= 0;
	current_map 	= "";
	next_map_name 	= "";


	d_split = str_split(toLower(game["am_sv_mapRotation"])," ");
	
 	for(i=0;i<d_split.size;i++) {
     
        if(d_split[i] == "map") {

			if(current_map != "" && isDefined(d_split[i+1]) && d_split[i+1] != "") {
			
				next_map_name 	= d_split[i+1];
				break;
			}
             
            if(first_map == "" && isDefined(d_split[i+1])) {
            
            	first_map 		= d_split[i+1];
            	first_map_i		= i;
            }
            if(current_map == "" && isDefined(d_split[i+1]) && d_split[i+1] == game["am_mapname"]) 	current_map = d_split[i+1];           
        }
    }   
    
    // * Wenn die letzte Map, dann ist die nächste die erste Map *
    if(next_map_name == "" && first_map != "") {
    
    	next_map_name = first_map;
    	i = first_map_i;
    }

    if(d_split[0] == "map") {
    
    	if(isDefined(d_split[i+3]) && d_split[i+3] != "") gametype = d_split[i+3];    													
    }
    else {
    
    	if(isDefined(d_split[i-1]) && d_split[i-1] != "") gametype = d_split[i-1];    													    
    }
    
    return gametype;
   
}




ZeitSpeedBerechnung(speed,start_punkt,zielpunkt) {

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


    // * Negativen Differenten nicht zulassen *
    if(speed < 1) return 0.1;

    abstand = distance(start_punkt,zielpunkt);
    zeit = (abstand/speed)/4;
    
    
    // * Negative Werte nicht zulassen *
    if(zeit <= 0) zeit = 0.1;
          
    
    return zeit;
}


str_split(str, trennzeichen) {

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


    str_array[0]    = "";  
    z               = 0;  

    for(i=0;i<str.size;i++) {
    
        if(str[i] == trennzeichen) {
        
            z++;
            str_array[z] = "";
        }
        else {
        
            str_array[z] += str[i];
        }
    }
    
    
    return str_array;
}

find_first_string(str, zeichenfolge) {

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

    pos = -1;
    
    for(i=0;i<str.size - zeichenfolge.size+1;i++) {
    
		stueck = subStr(str, i, zeichenfolge.size);
        
    	if(stueck == zeichenfolge) {
    	
    		pos = i;        
    		break;
    	}
    }
     
    return pos;
}

find_last_string(str, zeichenfolge) {

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

    pos = -1;
    
    for(i=0;i<str.size - zeichenfolge.size+1;i++) {
    
		stueck = subStr(str, i, zeichenfolge.size);
    
    	if(stueck == zeichenfolge) {
    	
    		pos = i;        		
    	}
    }
    
    return pos;
}


subStr(str, startpos, zeichenanzahl) {

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

 
    if (!isdefined(str) ||  str == "")      return "";
    
    if(startpos < 0)                        startpos = 0;
    if(startpos > (str.size -1))            startpos = str.size - 1;
    
    if(zeichenanzahl < 1)                   return "";
    if(zeichenanzahl + startpos > str.size) zeichenanzahl = str.size;
    
    
    string_final = "";
    
    for(i=startpos;i < (startpos + zeichenanzahl);i++) {
    
    	if(i >= str.size) break;
    	string_final += str[i];
    }
    
    
    return string_final;
}


replace_string(str_to_find, string_replace, str) {


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

    pos = -1;

	
    for(i=0;i<str.size - str_to_find.size+1;i++) {
    
		stueck = subStr(str, i, str_to_find.size);
        
    	if(stueck == str_to_find) {
    	
    		pos = i;        

			anfang 	= subStr(str, 0, i);
			ende 	= subStr(str, i + str_to_find.size, str.size - (i + str_to_find.size));

			neu = anfang + string_replace + ende;
			
			return neu;
    	}
    }
     
    return str;
}



onlyNormalChars(str) {


    if (!isdefined(str) ||  str == "") return "";
     
 	if(game["am_error_logfile"] == 1) logPrint("onlyNormalChars\n");
    
     
    grosse_buchstaben = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    kleine_buchstaben = "abcdefghijklmnopqrstuvwxyz";
        
    string_final = "";
    
    // * Alle Zeichen durchgehen *
    for (i=0;i < str.size;i++) {
        
        zeichen = str[i]; // * naechstes Zeichen holen *

		zeichen_neu = "";

        // * Alle Buchstaben durchprobieren * 
        for (ii=0;ii < 26;ii++) {
        
            if(zeichen == kleine_buchstaben[ii]) {
        
                zeichen_neu = kleine_buchstaben[ii];
                break;
            }
            if(zeichen == grosse_buchstaben[ii]) {
        
                zeichen_neu = grosse_buchstaben[ii];
                break;
            } 
        }

		if(zeichen_neu == "")  zeichen_neu = "_";
            
        string_final += zeichen_neu;
    }

    return string_final;
}



toLower(str) {
    
    if (!isdefined(str) ||  str == "") return "";

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

        
    grosse_buchstaben = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    kleine_buchstaben = "abcdefghijklmnopqrstuvwxyz";
        
    string_final = "";
    
    // * Alle Zeichen durchgehen *
    for (i=0;i < str.size;i++) {
        
        zeichen = str[i]; // * naechstes Zeichen holen *

        // * Alle großen Buchstaben durchprobieren * 
        for (ii=0;ii < grosse_buchstaben.size;ii++) {
        
            if(zeichen == grosse_buchstaben[ii]) {
        
                zeichen = kleine_buchstaben[ii];
                break;
            }
        }

        string_final += zeichen;
    }

    return string_final;
}



toUpper(str) {


    if (!isdefined(str) ||  str == "") return "";
     
 	if(game["am_error_logfile"] == 1) logPrint("toUpper\n");
    
     
    grosse_buchstaben = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    kleine_buchstaben = "abcdefghijklmnopqrstuvwxyz";
        
    string_final = "";
    
    // * Alle Zeichen durchgehen *
    for (i=0;i < str.size;i++) {
        
        zeichen = str[i]; // * naechstes Zeichen holen *

        // * Alle großen Buchstaben durchprobieren * 
        for (ii=0;ii < kleine_buchstaben.size;ii++) {
        
            if(zeichen == kleine_buchstaben[ii]) {
        
                zeichen = grosse_buchstaben[ii];
                break;
            }
        }

        string_final += zeichen;
    }

    return string_final;
}


firstToUpper(str) {


    if (!isdefined(str) ||  str == "") return "";

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

        
    grosse_buchstaben = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    kleine_buchstaben = "abcdefghijklmnopqrstuvwxyz";
        
    string_final = "";
    
    // * Alle Zeichen durchgehen *
    for (i=0;i < 1;i++) {
        
        zeichen = str[i]; // * naechstes Zeichen holen *

        // * Alle großen Buchstaben durchprobieren * 
        for (ii=0;ii < kleine_buchstaben.size;ii++) {
        
            if(zeichen == kleine_buchstaben[ii]) {
        
                zeichen = grosse_buchstaben[ii];
                
                neu = zeichen + subStr(str,1,str.size-1);
                
                return neu;
            }
        }

        string_final += zeichen;
    }

    return string_final;
}



strToInt(str) {

    if (!isdefined(str) ||  str == "") return 0;
 
	if(game["am_error_logfile"] == 1) logPrint("strToInt\n");


        
    int_final   = 0;
    multi       = 1;
    
    for(i=(str.size-1);i >= 0 ;i--) {
    
        if(str[i] == "0") int_final += 0 * multi;
        if(str[i] == "1") int_final += 1 * multi;
        if(str[i] == "2") int_final += 2 * multi;
        if(str[i] == "3") int_final += 3 * multi;
        if(str[i] == "4") int_final += 4 * multi;
        if(str[i] == "5") int_final += 5 * multi;
        if(str[i] == "6") int_final += 6 * multi;
        if(str[i] == "7") int_final += 7 * multi;
        if(str[i] == "8") int_final += 8 * multi;
        if(str[i] == "9") int_final += 9 * multi;
        
        multi *= 10;    
    }
 
     
    return int_final;
}


getColor(old_color, str) {
	
	if(game["am_error_logfile"] == 1) logPrint("getColor\n");

	
	if(str == "^0") return (0,0,0);
	if(str == "^1") return (1,0,0);
	if(str == "^2") return (0,1,0);
	if(str == "^3") return (1,1,0);
	if(str == "^4") return (0,0,1);
	if(str == "^5") return (0,1,1);
	if(str == "^6") return (1,0,1);
	if(str == "^7") return (1,1,1);
	if(str == "^8") return (0.3,0.6,0.3);
	if(str == "^9") return (0.6,0.6,0.6);
	
	
	return old_color;
}








GetPlayfieldDimension() {

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


	spawnpoints = [];


    spawnpoints_s1 = getentarray("mp_dm_spawn", "classname");
    for(i=0;i<spawnpoints_s1.size;i++) spawnpoints = maps\mp\gametypes\_spawnlogic::add_to_array(spawnpoints, spawnpoints_s1[i]);


	if(spawnpoints.size == 0) {
	
    	spawnpoints_s2 = getentarray("mp_tdm_spawn", "classname");
    	for(i=0;i<spawnpoints_s2.size;i++) spawnpoints = maps\mp\gametypes\_spawnlogic::add_to_array(spawnpoints, spawnpoints_s2[i]);
	}

	if(spawnpoints.size == 0) {
	
		spawnpoints_s3 = getentarray("mp_ctf_spawn_allied", "classname");
		spawnpoints_s4 = getentarray("mp_ctf_spawn_axis", "classname");
		for(i=0;i<spawnpoints_s3.size;i++) spawnpoints = maps\mp\gametypes\_spawnlogic::add_to_array(spawnpoints, spawnpoints_s3[i]);
		for(i=0;i<spawnpoints_s4.size;i++) spawnpoints = maps\mp\gametypes\_spawnlogic::add_to_array(spawnpoints, spawnpoints_s4[i]);
	}

	if(spawnpoints.size == 0) {
	
		spawnpoints_s5 = getentarray("mp_sd_spawn_attacker", "classname");
		spawnpoints_s6 = getentarray("mp_sd_spawn_defender", "classname");
		for(i=0;i<spawnpoints_s5.size;i++) spawnpoints = maps\mp\gametypes\_spawnlogic::add_to_array(spawnpoints, spawnpoints_s5[i]);
		for(i=0;i<spawnpoints_s6.size;i++) spawnpoints = maps\mp\gametypes\_spawnlogic::add_to_array(spawnpoints, spawnpoints_s6[i]);
	}


    // * 1. Objekt *
    xMax = spawnpoints[0].origin[0];
    xMin = spawnpoints[0].origin[0];
    
    yMax = spawnpoints[0].origin[1];
    yMin = spawnpoints[0].origin[1];
    
    zMax = spawnpoints[0].origin[2];
    zMin = spawnpoints[0].origin[2];
    
 
    
    for(i=1;i<spawnpoints.size;i++) {
            
        if (spawnpoints[i].origin[0] > xMax)     xMax = spawnpoints[i].origin[0];
        if (spawnpoints[i].origin[1] > yMax)     yMax = spawnpoints[i].origin[1];
        if (spawnpoints[i].origin[2] > zMax)     zMax = spawnpoints[i].origin[2];       
        if (spawnpoints[i].origin[0] < xMin)     xMin = spawnpoints[i].origin[0];
        if (spawnpoints[i].origin[1] < yMin)     yMin = spawnpoints[i].origin[1];
        if (spawnpoints[i].origin[2] < zMin)     zMin = spawnpoints[i].origin[2];
    }


    // * Mitte von der Map festlegen * 
    game["playfield_dim_CenterX"] = int(int(xMax + xMin)/2);
    game["playfield_dim_CenterY"] = int(int(yMax + yMin)/2);
    game["playfield_dim_CenterZ"] = int(int(zMax + zMin)/2);

    game["playfield_dim_vMin"]    = (xMin, yMin, zMin);
    game["playfield_dim_vMax"]    = (xMax, yMax, zMax);

    
    // * Breite und Länge *
    
    game["playfield_dim_breite"] = int(distance((xMin, yMin, 800),(xMax, yMin, 800))); 
    game["playfield_dim_laenge"] = int(distance((xMin, yMin, 800),(xMin, yMax, 800)));
         	
    // * Damit bei Random > 0 ist *     	
 	if(game["playfield_dim_breite"] < 1) game["playfield_dim_breite"] = 1;
 	if(game["playfield_dim_laenge"] < 1) game["playfield_dim_laenge"] = 1;
 	
 	
        

    // * Debug *

    
    /*
    plane1 = spawn("script_model", (0,0,0));
    plane1 setModel("xmodel/vehicle_stuka_flying");  
    plane1.origin = (level.playfield_dim_CenterX,level.playfield_dim_CenterY,800);


    plane2 = spawn("script_model", (0,0,0));
    plane2 setModel("xmodel/vehicle_stuka_flying");  
    plane2.origin = (xMax,yMax,800);
    
    plane3 = spawn("script_model", (0,0,0));
    plane3 setModel("xmodel/vehicle_stuka_flying");  
    plane3.origin = (xMin,yMin,800);
    
    plane4 = spawn("script_model", (0,0,0));
    plane4 setModel("xmodel/vehicle_stuka_flying");  
    plane4.origin = (xMax,yMin,800);
    
    plane5 = spawn("script_model", (0,0,0));
    plane5 setModel("xmodel/vehicle_stuka_flying");  
    plane5.origin = (xMin,yMax,800);
   	*/
    
    
}



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


    entitytypes = getentarray();
    


    // * 1. Objekt *
    xMax = game["playfield_dim_vMax"][0];
    xMin = game["playfield_dim_vMin"][0];
    
    yMax = game["playfield_dim_vMax"][1];
    yMin = game["playfield_dim_vMin"][1];
    
    zMax = game["playfield_dim_vMax"][2];
    zMin = game["playfield_dim_vMin"][2];
    

    xMin_e[0] = xMax;
    yMin_e[1] = yMax;
    zMin_e[2] = zMax;

    xMax_e[0] = xMin;
    yMax_e[1] = yMin;
    zMax_e[2] = zMin;       

    
    mitte = (game["playfield_dim_CenterX"],game["playfield_dim_CenterY"],800);
    

	trace                               = bulletTrace(mitte, mitte - (30000,0,0),false,undefined);           
	if(trace["fraction"] != 1)  xMin_e  = trace["position"];
	trace                               = bulletTrace(mitte, mitte + (30000,0,0),false,undefined);           
	if(trace["fraction"] != 1)  xMax_e  = trace["position"];
	trace                               = bulletTrace(mitte, mitte - (0,30000,0),false,undefined);           
	if(trace["fraction"] != 1)  yMin_e  = trace["position"];
	trace                               = bulletTrace(mitte, mitte + (0,30000,0),false,undefined);           
	if(trace["fraction"] != 1)  yMax_e  = trace["position"];
	trace                               = bulletTrace(mitte, mitte - (0,0,30000),false,undefined);           
	if(trace["fraction"] != 1)  zMin_e  = trace["position"];
	trace                               = bulletTrace(mitte, mitte + (0,0,30000),false,undefined);           
	if(trace["fraction"] != 1)  zMax_e  = trace["position"];


	if (xMin_e[0] < xMin)   xMin = xMin_e[0];
	if (yMin_e[1] < yMin)   yMin = yMin_e[1];
	if (zMin_e[2] < zMin)   zMin = zMin_e[2];

	if (xMax_e[0] > xMax)   xMax = xMax_e[0];
	if (yMax_e[1] > yMax)   yMax = yMax_e[1];
	if (zMax_e[2] > zMax)   zMax = zMax_e[2];       
    

    
    
    
    // * von jedem Objekt aus die World-Werte ermitteln * 
    
    /*
    for(i = 1; i < entitytypes.size; i++) {
        
      
        trace                               = bulletTrace(entitytypes[i].origin, entitytypes[i].origin - (30000,0,0),false,undefined);           
        if(trace["fraction"] != 1)  xMin_e  = trace["position"];
        trace                               = bulletTrace(entitytypes[i].origin, entitytypes[i].origin + (30000,0,0),false,undefined);           
        if(trace["fraction"] != 1)  xMax_e  = trace["position"];
        trace                               = bulletTrace(entitytypes[i].origin, entitytypes[i].origin - (0,30000,0),false,undefined);           
        if(trace["fraction"] != 1)  yMin_e  = trace["position"];
        trace                               = bulletTrace(entitytypes[i].origin, entitytypes[i].origin + (0,30000,0),false,undefined);           
        if(trace["fraction"] != 1)  yMax_e  = trace["position"];
        trace                               = bulletTrace(entitytypes[i].origin, entitytypes[i].origin - (0,0,30000),false,undefined);           
        if(trace["fraction"] != 1)  zMin_e  = trace["position"];
        trace                               = bulletTrace(entitytypes[i].origin, entitytypes[i].origin + (0,0,30000),false,undefined);           
        if(trace["fraction"] != 1)  zMax_e  = trace["position"];
      

        if (xMin_e[0] < xMin)   xMin = xMin_e[0];
        if (yMin_e[1] < yMin)   yMin = yMin_e[1];
        if (zMin_e[2] < zMin)   zMin = zMin_e[2];

        if (xMax_e[0] > xMax)   xMax = xMax_e[0];
        if (yMax_e[1] > yMax)   yMax = yMax_e[1];
        if (zMax_e[2] > zMax)   zMax = zMax_e[2];       
    
    
     	wait 0.1;  
    }
	*/
       
    // * Mitte der Map *
    game["map_dim_CenterX"] = int(xMax + xMin)/2;
    game["map_dim_CenterY"] = int(yMax + yMin)/2;
    game["map_dim_CenterZ"] = int(zMax + zMin)/2;
 
 
    game["map_dim_vMax"] = (xMax, yMax, zMax);
    game["map_dim_vMin"] = (xMin, yMin, zMin);



    // * Breite und Länge *
    
    game["map_dim_breite"] = distance((xMin,yMin,zMax),(xMax,yMin,zMax));   
    game["map_dim_laenge"] = distance((xMin,yMin,zMax),(xMin,yMax,zMax));


    // * Damit bei Random > 0 ist *     	
 	if(game["map_dim_breite"] < 1) game["map_dim_breite"] = 1;
 	if(game["map_dim_laenge"] < 1) game["map_dim_laenge"] = 1;




    entitytypes = [];
    entitytypes = undefined;
    


 
    // * Debug *
 
   
  	/*
     plane1 = spawn("script_model", (0,0,0));
     plane1 setModel("xmodel/vehicle_stuka_flying");  
     plane1.origin = ((int(xMax + xMin)/2),(int(yMax + yMin)/2),zMax);
 
 
     plane2 = spawn("script_model", (0,0,0));
     plane2 setModel("xmodel/vehicle_stuka_flying");  
     plane2.origin = (xMax,yMax,zMax);
     
     plane3 = spawn("script_model", (0,0,0));
     plane3 setModel("xmodel/vehicle_stuka_flying");  
     plane3.origin = (xMin,yMin,zMax);
     
     plane4 = spawn("script_model", (0,0,0));
     plane4 setModel("xmodel/vehicle_stuka_flying");  
     plane4.origin = (xMax,yMin,zMax);
     
     plane5 = spawn("script_model", (0,0,0));
     plane5 setModel("xmodel/vehicle_stuka_flying");  
     plane5.origin = (xMin,yMax,zMax);
   */
   
    
}




isMapNameExists(mapname) {

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

	switch(mapname) {

		case "mp_breakout":
			return true;			
		case "mp_brecourt":
			return true;	
		case "mp_burgundy":
			return true;	
		case "mp_carentan":
			return true;	
		case "mp_dawnville":
			return true;	
		case "mp_decoy":
			return true;	
		case "mp_downtown":
			return true;	
		case "mp_farmhouse":
			return true;	
		case "mp_firestation":
			return true;	
		case "mp_leningrad":
			return true;	
		case "mp_matmata":
			return true;	
		case "mp_railyard":
			return true;	
		case "mp_toujane":
			return true;	
		case "mp_trainstation":
			return true;	
		case "mp_rhine":
			return true;	
		case "mp_harbor":
			return true;	
	}
	
	
	return false;
}


isGametypeNameExists(gametype) {

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

	switch(gametype) {

		case "dm":
			return true;	
		case "tdm":
			return true;	
		case "hq":
			return true;	
		case "hq2":
			return true;	
		case "ctf":
			return true;	
		case "ctfb":
			return true;	
		case "ctb":
			return true;	
		case "sd":
			return true;	
		case "sdr":
			return true;
		case "csd":
			return true;
		case "mm":
			return true;		
		case "dom":
			return true;		
		case "noz":
			return true;		
		case "ein":
			return true;		
	}   	
	
	return false;
}


CheckImRaum() {

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


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

	
	if(!isDefined(level.map_dim_vMax)) return false;
	
	
	trace = bulletTrace(self.origin + (0,0,50),(self.origin[0],self.origin[1],level.map_dim_vMax[2] - 50), false, undefined);		
	
	if(trace["fraction"] < 1) 	return true;
	
	return false;
}


GetStockwerk(origin) {

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


	position = origin;


	stockwerk_hoehen = [];
	
	
	
	spawnpoints = getentarray("mp_dm_spawn", "classname");	
		
	for(i=0;i<spawnpoints.size;i++) {
	
		if(distance(spawnpoints[i].origin, origin) < 1200) {
		
						
			existiert = false;			
			// * existiert diese Stockwerk-Hoehe schon? *
			for(s=0;s<stockwerk_hoehen.size;s++) {
			
				if(spawnpoints[i].origin[2] == stockwerk_hoehen[s]) {
				
					existiert = true;	
					break;
				}
			}
			
			if(existiert == false) stockwerk_hoehen[stockwerk_hoehen.size] = spawnpoints[i].origin[2];		
		}
	}


	// * Sortieren *
	for(s1=0;s1<stockwerk_hoehen.size-1;s1++) {
			
		for(s2=s1+1;s2<stockwerk_hoehen.size;s2++) {
	
			if(stockwerk_hoehen[s1] > stockwerk_hoehen[s2]) {
				
				h = stockwerk_hoehen[s1];
				stockwerk_hoehen[s1] = stockwerk_hoehen[s2];
				stockwerk_hoehen[s2] = h;
			}
		}
	}

	// * Wenn keine Stockwerke, dann Stockwerk 0 zurückgeben *
	if(!isDefined(stockwerk_hoehen)) return 0;
	

	stockwerke = [];
	s = 0;
	stockwerke[s] = stockwerk_hoehen[0];
	letzte_hoehe = stockwerk_hoehen[0];
	
	for(i=0;i<stockwerk_hoehen.size-1;i++) {			

		if(distance((0,0,letzte_hoehe), (0,0,stockwerk_hoehen[i+1])) >= 100) {
			
			s++;
			stockwerke[s] = stockwerk_hoehen[i+1];		
			letzte_hoehe = stockwerk_hoehen[i+1];
		}
	}


 	stockwerk = 0;
	for(i=0;i<stockwerke.size-1;i++) {			

		if(origin[2] + 30 > stockwerke[i] && origin[2] + 30 < stockwerke[i+1]) {		
			
			stockwerk = i + 1;	
			break;
		}
	}

	
	if(origin[2] > -70 && stockwerke[0] < - 70) stockwerk--;	
	
	if(origin[2] < -90) stockwerk = 0;
	
	
	return stockwerk;
}


CheckMatesNaehe(origin,range) {

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


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


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

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

		player = players[i];

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

			mates++;
		}
	}
		
	return mates;
}

CheckEnemyNaehe(origin,range) {

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


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


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

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

		player = players[i];

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

			enemys++;
		}
	}
		
	return enemys;
}


isWinter() {

	if(game["am_error_logfile"] == 1) logPrint("isWinter\n");
	
	if(game["am_mapname"] == "mp_leningrad") 		return true;
	if(game["am_mapname"] == "mp_downtown") 		return true;
	if(game["am_mapname"] == "mp_railyard") 		return true;
	
	return false;

}


getGametypeName(gametype) {

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


	switch(gametype) {

		case "dm":
			return "Deathmatch";	
		case "tdm":
			return "Team-Deathmatch";
		case "hq":
			return "Headquarters";	
		case "hq2":
			return "Headquarters2";	
		case "ctf":
			return "Capture The Flag";
		case "ctfb":
			return "Capture The Flag Back";
		case "ctb":
			return "Capture The Bottle";
		case "sd":
			return "Search & Destroy";	
		case "sdr":
			return "Search & Destroy Respawn";
		case "csd":
			return "Counterstrike Search & Destroy";
		case "mm":
			return "Multiplayer Mission";		
		case "dom":
			return "Domination";		
		case "noz":
			return "Night Of Zombies";		
	}   	
	
	return gametype;
	
}


deleteAMextension(name) {

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


	if(isDefined(name)) {

		if(subStr(name,0,3) == "am_")  {

			name = subStr(name,3,name.size);
		}
	}

	return name;
}


getGround(origin) {

	
	trace = bulletTrace(origin + (0,0,8), origin + (0,0,-50),false,undefined);		

	if(trace["fraction"] < 1) 	return trace["position"];


	return origin;	
	
}


check_teams_exist() {

	players = getentarray("player", "classname");
	
	alliesexist = false;
	axisexist = false;
	
	for(i = 0; i < players.size; i++) {
	
		if(!isdefined(players[i].pers["team"]) || players[i].pers["team"] == "spectator") continue;
		
		if(players[i].pers["team"] == "allies")		alliesexist = true;
		else if(players[i].pers["team"] == "axis")	axisexist = true;

		if(alliesexist && axisexist) return true;
	}
	
	return false;
}


check_teams_exist_plus() {

	players = getentarray("player", "classname");
	
	alliesexist = false;
	axisexist = false;
	
	for(i = 0; i < players.size; i++) {
	
		if(!isdefined(players[i].pers["team"]) || !isdefined(players[i].sessionstate) || players[i].sessionstate != "playing") continue;
		
		if(players[i].pers["team"] == "allies")		alliesexist = true;
		else if(players[i].pers["team"] == "axis")	axisexist = true;

		if(alliesexist && axisexist) return true;
	}
	
	return false;
}




ThrowObject(client_id, client_team, objekt_typ, vRichtung, aufprallsound) {

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


    self hide();

	Gravitation = 100;

    // * Gravitationsstärke *
    if(objekt_typ == "helm")  		Gravitation = 100;
    if(objekt_typ == "messer")  	Gravitation = 50;
    if(objekt_typ == "healthpack")  Gravitation = 80;
    if(objekt_typ == "mortar")  	Gravitation = 100;
   

    // * Z - Vector Gravitation *
    zGravitation = (0,0,-0.02 * Gravitation);


    GeschwAbnahme  = 0.5;      // * 1 = keine Abnahme, 0 = große Abnahme *
    MoveStep       = 5;        // * Objekt-Bewegungs-Steps in Pixel *

    
    aufprall_anzahl         = 0;    
    aufprall_anzahl_max     = 4;

    if(objekt_typ == "bird") 		aufprall_anzahl_max = 1;
    if(objekt_typ == "healthpack") 	aufprall_anzahl_max = 4;
    if(objekt_typ == "mortar") 		aufprall_anzahl_max = 1;


    rx = randomInt(20);
    ry = randomInt(20);
    rz = randomInt(20);
  


    // * Hilfsobjekt *
    self.hilfsObjekt = spawn("script_model", self.origin );
    self.hilfsObjekt.angles = self.angles;
    
    if(objekt_typ == "helm") 			self linkto(self.hilfsObjekt,"",(0,0,-5),(-70,0,-90));   
    else if(objekt_typ == "messer") 	self linkto(self.hilfsObjekt); 
    else 								self linkto(self.hilfsObjekt);
    
    self show();


	old_origin = self.origin;

	if(objekt_typ == "mortar" && game["am_mortar_cannon_training_player_fly"] == 1) {
	
		player = maps\mp\gametypes\_admiral_mod_player::getPlayer(client_id,client_team);
		if(isDefined(player)){ 		
			
			old_origin = player.origin;
			player linkto(self.hilfsObjekt);
		}
	}
	
	

	zeit = 0;
	incomming_on = undefined;

	fx_zaehler = 0;

    while(1) {
    
    	if(objekt_typ == "mortar" && !isDefined(incomming_on)) {
    	
    		zeit += 0.05;
    		if(zeit > 0.6 && isDefined(self.hilfsObjekt)) {
    		
    			self.hilfsObjekt playloopsound("am_mortar_cannon_incoming");
    			incomming_on = true;
    		}
    	}
    
    
       
    
    
        // * zunehmende Geschwindigkeit *
        vRichtung += zGravitation;  // - Z Vector Gravitation

        // * Punkt-zu-Punkt Pfad ... solange bis etwas getroffen wird *
        NeuePosition = self.hilfsObjekt.origin + vRichtung;
      
              
    
        // * Prüfen ob eine Berührung stattfindet *     
        trace=bulletTrace(self.hilfsObjekt.origin, NeuePosition, true, self.hilfsObjekt);         
        
        
        // * Wenn Motar die Mapdecke überfliegt, dann keine Explosion *
        if(objekt_typ == "mortar" && trace["fraction"] != 1 && trace["position"][2] > game["map_dim_vMax"][2] - 50) trace["fraction"] = 1;
        
        
        if(trace["fraction"] != 1) {  // * Wenn etwas berühert *
        
        	// * Wenn das Messer aus dem Flug heraus einen Spieler trifft *
        	if(objekt_typ == "messer" && aufprall_anzahl == 0 && isPlayer(trace["entity"]) && trace["entity"].clientid != client_id) {
        	        		
        		self playsound("Land_plr_snow");
        		self playsound("am_knife_death");
        		        	
        		trace["entity"] thread maps\mp\gametypes\_admiral_mod_weapons_class::KnifeDeath(client_id, client_team, trace);
        		return;
        	}
        
           
           	if(isDefined(self) && isDefined(aufprallsound)) self playsound(aufprallsound);
           
            NeuePosition = trace["position"] + (0,0,4);      
            
            
            // * Sonst liegt das Healthpack am Ende zu hoch auf dem Boden *
            if(objekt_typ == "healthpack") NeuePosition = trace["position"] + (0,0,1);  
			    
    	      
            
        
            // * Boden berühert ? *
            aufprall_anzahl++;             
            if(aufprall_anzahl >= aufprall_anzahl_max) {
            
            	if(objekt_typ == "mortar") break;
            
            
            	wait 0.25;
            
            	// * Check ob auch wirklich am Boden *
            	trace = bulletTrace(NeuePosition, NeuePosition + (0,0,-1000), true, self.hilfsObjekt);		
				if(distance(trace["position"], NeuePosition) > 10) {
				
					aufprall_anzahl = 1;
				}
				else break;
            }
        
        
            // * bei jeder Berührung Geschwindigkeit verringern *
            vGeschwindigkeit    = length(vRichtung) * GeschwAbnahme;


            
            // * nach der Berührung abprallen *
            VectorNormale   = trace["normal"];            
            VectorRichtung  = maps\mp\_utility::vectorScale(vectorNormalize(vRichtung),-1);
                        
            Blickwinkel     = vectortoangles(VectorNormale - VectorRichtung);
            vRichtungNeu    = anglestoforward(Blickwinkel);
          
           
         
            // * Neuen Zielpunkt ermitteln *
            vRichtung  = maps\mp\_utility::vectorScale(vRichtungNeu, vGeschwindigkeit);
      
        }
        

        if(objekt_typ == "mortar")  self.hilfsObjekt.angles = vectortoangles(vectornormalize((NeuePosition + (0,0,0)) - self.hilfsObjekt.origin));

        
        
        // * Hilfsobjekt zum Zielpunkt bewegen *
        self.hilfsObjekt.origin = NeuePosition;
        
   
        
        if(objekt_typ == "helm") self.hilfsObjekt.angles = self.hilfsObjekt.angles + (rx,ry,rz);
        
        
        if(objekt_typ == "messer") {
        
        	if(aufprall_anzahl > 0) self.hilfsObjekt.angles = self.hilfsObjekt.angles + (rx,ry,rz);
       		else 					self.hilfsObjekt.angles = self.hilfsObjekt.angles + (0,0,-30);
		}
		
		
        wait 0.05;
    }



    if(isDefined(self) && isDefined(self.hilfsObjekt)) {
    
    	self.hilfsObjekt stoploopsound();
    	wait 0.1;
    }
 
    
    if(objekt_typ == "mortar" && game["am_mortar_cannon_training_player_fly"] == 1) {
	
		player = maps\mp\gametypes\_admiral_mod_player::getPlayer(client_id,client_team);
		if(isDefined(player)){ 
		
			self unlink();				
			self.hilfsObjekt moveto(old_origin,0.1);
			wait 0.15;
			player unlink();			
		}
	}

    
    if(isDefined(self) && isDefined(self.hilfsObjekt)) self.hilfsObjekt delete();
  
    
    // * 15 Sekunden warten, dann Helm entfernen (damit nicht so viele Objekte auf der Map liegen, Gamestate) *
    if(objekt_typ == "helm")  wait 15; 
    
        
    // * Helm löschen *
    if(objekt_typ == "helm" && isDefined(self)) self delete();
    
    
    
    if(objekt_typ == "healthpack") {    
    	
    	self thread maps\mp\gametypes\_admiral_mod_player::healthThink(2);
    }
		
 
    
    return;

}


