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


MapMortarAttacks() {


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

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



	attack_formation 		= game["am_mortar_attacks_formation"];
	attack_strength 		= game["am_mortar_attacks_strength"];
	attack_damage_range 	= game["am_mortar_attacks_damage_range"];
	attack_damage_players 	= game["am_mortar_attacks_player_damage"];
	attack_target 			= game["am_mortar_attacks_target"];
	


 	// * Start this AttackThread endless *
    while(1) {    
 
 		
 		players = getentarray("player", "classname");
		    
		if(players.size < 1) {
		
			wait 10;
			continue;
		}
 

        waittime = RandomInt(game["am_mortar_attacks_max_wait"]) + 5; 
        
        if(waittime < game["am_mortar_attacks_min_wait"]) waittime = game["am_mortar_attacks_min_wait"];        
        wait waittime;

		count = RandomInt(game["am_mortar_attacks_at_same_time"]) + 1;
		
		for(i=0;i<count;i++) {		
		
			thread MortarAttackFormation(attack_target,undefined,attack_formation,attack_strength,attack_damage_range,attack_damage_players);
		}
		
		wait 0.1; // min wait secure
	}
}


GetMortarAttackFormations() {

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


	formations[0] = "line";
	formations[1] = "circle";
	
	return formations;	

}

GetMortarAttackStrength(attack_strength) {

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


    switch(attack_strength) {
            
        case "light":
        	strength = 1;
        	break; 
        case "medium":
        	strength = 2;
        	break; 
        case "normal":
        	strength = 3;
        	break; 
        case "heavy":
        	strength = 4;
        	break; 
        case "hell":
        	strength = 5;
        	break; 
        case "random":
        	strength = RandomInt(5) + 1;
        	break;
		default:
			strength = 3;
			break;
	}
	
	return strength;
}


MortarAttackFormation(attack_target,attack_position,attack_formation,attack_strength,attack_damage_range,attack_damage) {


	if(game["am_error_logfile"] == 1) logPrint("MortarAttackFormation\n");
 
	
	target 				= "none";
	

	formations = GetMortarAttackFormations();	
	if(attack_formation == "random") attack_formation = formations[RandomInt(2)];


	strength = GetMortarAttackStrength(attack_strength);


	//iprintln("Attack Strength: " + strength);


	switch(attack_formation) {

		case "line":
			attack_formation_width 	= strength * 120;
			attack_formation_length = strength * 550;	
			break; 

		case "circle":
			attack_formation_width 	= strength * 450;
			attack_formation_length = strength * 450;	
			break; 

		default:
			attack_formation_width 	= strength * 450;
			attack_formation_length = strength * 450;	
			break; 
	}
    
   
    
    if(attack_formation == "circle") {   	       
        
        anzahl 					= RandomInt(strength*3) + strength*2;
		radius_attack 			= RandomInt(attack_formation_width) + 1;
		
		radius_seite			= (randomInt(800)-400);
		attack_seite  			= (radius_seite, radius_seite, game["map_dim_vMax"][2]);
	
		
		if(!isDefined(attack_position)) {
	
			attack_position_x 		= game["playfield_dim_vMin"][0] + randomInt(game["playfield_dim_breite"]);
			attack_position_y 		= game["playfield_dim_vMin"][1] + randomInt(game["playfield_dim_laenge"]);
		}
		else {
		
			attack_position_x 		= attack_position[0];
			attack_position_y 		= attack_position[1];		
		}
		
              
        for(i=0;i<anzahl;i++) {

			mortar_endpoint[0] = (attack_position_x - (radius_attack/2)) + RandomInt(radius_attack);
			mortar_endpoint[1] = (attack_position_y - (radius_attack/2)) + RandomInt(radius_attack);
			mortar_endpoint[2] = game["map_dim_vMax"][2];

			mortar_startpoint[0] = mortar_endpoint[0] + attack_seite[0];			
			mortar_startpoint[1] = mortar_endpoint[1] + attack_seite[1];			
			mortar_startpoint[2] = game["map_dim_vMax"][2];	
        
            thread SendAMortarToMap(mortar_startpoint, mortar_endpoint, attack_damage_range, attack_damage);
        
            wait randomFloat(6-strength) + 0.1;
        }  
    }
    
    if(attack_formation == "line") {   	       
        
        anzahl 					= randomInt(strength*3) + strength*2;
		attack_width			= attack_formation_width;
		attack_length			= attack_formation_length;
		
		radius_seite			= (randomInt(800)-400);
		attack_seite  			= (radius_seite, radius_seite, game["map_dim_vMax"][2]);


		if(!isDefined(attack_position)) {
	
			attack_position_x 		= game["playfield_dim_vMin"][0] + randomInt(game["playfield_dim_breite"]);
			attack_position_y 		= game["playfield_dim_vMin"][1] + randomInt(game["playfield_dim_laenge"]);
		}
		else {
		
			attack_position_x 		= attack_position[0];
			attack_position_y 		= attack_position[1];		
		}
		

		step 					= int(attack_length/anzahl);
		halflength 				= int(attack_length/2);
        
        winkel					= RandomInt(360);
        angles_forward 			= (0,winkel,0);
        
        angles_backward			= 0;
        if(winkel <= 180)		angles_backward = (0,winkel + 180,0);
        if(winkel > 180)		angles_backward = (0,winkel - 180,0);
        
        halfback 				= maps\mp\_utility::vectorScale(anglestoforward(angles_backward),halflength);
        
		attack_position_x		+= halfback[0];
		attack_position_y		+= halfback[1];
        
        
        for(i=0;i<anzahl;i++) {
		
			// * Start on the half way back, step forward, random backwards  * 
			forward 		= step - randomInt(int(step/2) + 1);		
		 	farpos 			= maps\mp\_utility::vectorScale(anglestoforward(angles_forward),forward);

			attack_position_x = attack_position_x + farpos[0];
			attack_position_y = attack_position_y + farpos[1];

			
			leftrightpos 	= maps\mp\_utility::vectorScale(anglestoforward(angles_forward + (90,0,0)),attack_width-RandomInt(attack_width*2));

			mortar_endpoint[0] = attack_position_x + leftrightpos[0];
			mortar_endpoint[1] = attack_position_y + leftrightpos[1];
			mortar_endpoint[2] = game["map_dim_vMax"][2];

			mortar_startpoint[0] = mortar_endpoint[0] + attack_seite[0];			
			mortar_startpoint[1] = mortar_endpoint[1] + attack_seite[1];			
			mortar_startpoint[2] = game["map_dim_vMax"][2];	
        
            thread SendAMortarToMap(mortar_startpoint, mortar_endpoint, attack_damage_range, attack_damage);
        
            wait randomFloat(6-strength) + 0.1;
        }  
    }     
}



SendAMortarToMap(startpoint, endpoint, attack_damage_range, attack_damage) {
 
 
 	if(game["am_error_logfile"] == 1) logPrint("SendAMortarToMap\n");

 
    mortar = spawn("script_model", (0,0,0));
    mortar setModel("xmodel/vehicle_halftrack_rockets_shell_d");
    mortar hide();
        
        
    trace = bulletTrace((endpoint[0],endpoint[1],endpoint[2] - 50),(endpoint[0],endpoint[1],endpoint[2] - 30000), false, undefined);                   
 
 	if(trace["fraction"] != 1)  endpoint[2] = trace["position"][2];
    else                        endpoint[2] = game["map_dim_vMin"][2];
 
 	mortar_startpoint 	= (startpoint[0],startpoint[1],startpoint[2]);
 	mortar_endpoint 	= (endpoint[0],endpoint[1],endpoint[2]);
 
 
    mortar.origin = mortar_startpoint;        
    mortar.angles = vectortoangles(vectornormalize((mortar_endpoint) - mortar_startpoint));    

       
    wait 0.25;  
    mortar playSound("am_mortar_incoming");
              
        
    falltime = maps\mp\gametypes\_admiral_mod_utils::ZeitSpeedBerechnung(900,mortar_startpoint,mortar_endpoint);
    
    // * Nur wenn Fallzeit < 3 Sekunden, ansonsten gibt es negative Werte in der WAIT-Zeit! *
    if(falltime < 2) {
    
        // * warten auf Einschlag *
        wait 2 - 0.05 - falltime;
    }
    
    
    mortar show();  
    mortar moveto(mortar_endpoint, falltime);    
    wait falltime;
    
    o = RandomInt(3);
    playfx(game["motar_explosions_fx"][o], mortar_endpoint);            
                    
    if(game["am_mortar_explosion_silent"] == 1)	mortar playSound("am_mortar_explosion_silent");    
    else										mortar playSound("am_mortar_explosion");    
    
    mortar hide();
           
           
    radiusDamage(mortar_endpoint + (0,0,12), attack_damage_range, attack_damage, 0);
            
    rs 			= randomInt(50) + 10;
    strength    = rs / 100;
    length      = 4 * strength;
    range       = 800 * strength;        
    earthquake(strength, length, mortar_endpoint, range); 
   
 
 	wait 3; // Sound ending   

   
    if(isDefined(mortar)) mortar delete();
  
}


ArtilleryAttack(client_id, client_team, attack_position, radius_attack, attack_damage_range, attack_damage) {


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

	
	wait 2;
	
	anzahl = randomInt(10) + 20;

	radius_seite			= (randomInt(800)-400);
	attack_seite  			= (radius_seite, radius_seite, game["map_dim_vMax"][2]);


	for(i=0;i<anzahl;i++) {
	
		mortar_endpoint[0] = (attack_position[0] - (radius_attack/2)) + RandomInt(radius_attack);
		mortar_endpoint[1] = (attack_position[1] - (radius_attack/2)) + RandomInt(radius_attack);
		mortar_endpoint[2] = game["map_dim_vMax"][2];

		mortar_startpoint[0] = mortar_endpoint[0] + attack_seite[0];			
		mortar_startpoint[1] = mortar_endpoint[1] + attack_seite[1];			
		mortar_startpoint[2] = game["map_dim_vMax"][2];	
        
        thread SendArtilleyToMap(client_id, client_team, mortar_startpoint, mortar_endpoint, attack_damage_range, attack_damage);
        
        wait randomFloat(1.5) + 0.2;
    }  
}


SendArtilleyToMap(client_id, client_team, startpoint, endpoint, attack_damage_range, attack_damage) {
 
 
 	if(game["am_error_logfile"] == 1) logPrint("SendAMortarToMap\n");

 
    mortar = spawn("script_model", (0,0,0));
    mortar setModel("xmodel/vehicle_halftrack_rockets_shell_d");
    mortar hide();
        
        
    trace = bulletTrace((endpoint[0],endpoint[1],endpoint[2] - 50),(endpoint[0],endpoint[1],endpoint[2] - 30000), false, undefined);                   
 
 	if(trace["fraction"] != 1)  endpoint[2] = trace["position"][2];
    else                        endpoint[2] = game["map_dim_vMin"][2];
 
 	mortar_startpoint 	= (startpoint[0],startpoint[1],startpoint[2]);
 	mortar_endpoint 	= (endpoint[0],endpoint[1],endpoint[2]);
 
 
    mortar.origin = mortar_startpoint;        
    mortar.angles = vectortoangles(vectornormalize((mortar_endpoint) - mortar_startpoint));    

       
    wait 0.25;  
    mortar playSound("am_mortar_incoming");
              
        
    falltime = maps\mp\gametypes\_admiral_mod_utils::ZeitSpeedBerechnung(900,mortar_startpoint,mortar_endpoint);
    
    // * Nur wenn Fallzeit < 3 Sekunden, ansonsten gibt es negative Werte in der WAIT-Zeit! *
    if(falltime < 2) {
    
        // * warten auf Einschlag *
        wait 2 - 0.05 - falltime;
    }
    
    
    mortar show();  
    mortar moveto(mortar_endpoint, falltime);    
    wait falltime;
    
    o = RandomInt(3);
    playfx(game["motar_explosions_fx"][o], mortar_endpoint);            
                    
    if(game["am_mortar_explosion_silent"] == 1)	mortar playSound("am_mortar_explosion_silent");    
    else										mortar playSound("am_mortar_explosion");    
    
    mortar hide();
           
           
    // * do the damage *
    points = game["am_points_for_artillerie_kill"];
    maps\mp\gametypes\_admiral_mod_player::DamageToPlayers(points, client_id, client_team, mortar_endpoint + (0,0,12), attack_damage_range, 500, attack_damage, 1, 1,"MOD_EXPLOSIVE","artillery_mp");
         
         
	 
 	wait 3; // Sound ending   

   
    if(isDefined(mortar)) mortar delete();
  
}




AmbientWarSounds() {

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


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


    ambient_point = spawn("script_model", (0,0,0));
    ambient_point hide();


	while(1) {

		wait 1;
		
		wait RandomInt(15);



		x = game["playfield_dim_vMin"][0];
   		y = game["playfield_dim_vMin"][1];    	


		seite = RandomInt(4);
		
		if(seite == 0) {
		
   			x = game["playfield_dim_vMin"][0] + randomInt(game["playfield_dim_breite"]);
    		y = game["playfield_dim_vMin"][1];    	
		}
		if(seite == 1) {
		
   			x = game["playfield_dim_vMin"][0] + randomInt(game["playfield_dim_breite"]);
    		y = game["playfield_dim_vMax"][1];    	
		}
		if(seite == 2) {
		
   			x = game["playfield_dim_vMin"][0];
    		y = game["playfield_dim_vMin"][1] + randomInt(game["playfield_dim_laenge"]);    	
		}
		if(seite == 3) {
		
   			x = game["playfield_dim_vMax"][0];
    		y = game["playfield_dim_vMin"][1] + randomInt(game["playfield_dim_laenge"]);    	
		}
				

		z = game["map_dim_vMax"][2]; // * Z-Daten von der Map, da vom Playfield zu gering! *
      	 
        
        m = RandomInt(8) + 1;
        
        for(i=0;i<m;i++) {

			if(isDefined(ambient_point)) {

				ambient_point.origin = (x - 500 + randomInt(1000),y - 500 + randomInt(1000),z);
								
				ambient_point playSound("am_distant_artillery");				
			}			
			
			wait RandomInt(3) + 0.7;
		}
	}
}




AmbientFlybySounds() {

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


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


    ambient_point = spawn("script_model", (0,0,0));
    ambient_point hide();


	while(1) {

		wait 1;
		
		wait RandomInt(50);



		x = game["playfield_dim_vMin"][0];
   		y = game["playfield_dim_vMin"][1];    	


		seite = RandomInt(4);
		
		if(seite == 0) {
		
   			x = game["playfield_dim_vMin"][0] + randomInt(game["playfield_dim_breite"]);
    		y = game["playfield_dim_vMin"][1];    	
		}
		if(seite == 1) {
		
   			x = game["playfield_dim_vMin"][0] + randomInt(game["playfield_dim_breite"]);
    		y = game["playfield_dim_vMax"][1];    	
		}
		if(seite == 2) {
		
   			x = game["playfield_dim_vMin"][0];
    		y = game["playfield_dim_vMin"][1] + randomInt(game["playfield_dim_laenge"]);    	
		}
		if(seite == 3) {
		
   			x = game["playfield_dim_vMax"][0];
    		y = game["playfield_dim_vMin"][1] + randomInt(game["playfield_dim_laenge"]);    	
		}
				

		z = game["map_dim_vMax"][2]; // * Z-Daten von der Map, da vom Playfield zu gering! *
      	 
        
        m = RandomInt(3) + 1;
        
        for(i=0;i<m;i++) {

			if(isDefined(ambient_point)) {

				ambient_point.origin = (x - 500 + randomInt(1000),y - 500 + randomInt(1000),z);
								
				ambient_point playSound("am_flybys");				
			}			
			
			wait RandomInt(3) + 0.7;
		}
		
		wait 8;
	}
}


AmbientDogs() {

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


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


	


    ambient_point = spawn("script_model", (0,0,0));
    ambient_point hide();


	while(1) {

		wait 1;
		
		wait RandomInt(30);



		x = game["playfield_dim_vMin"][0];
   		y = game["playfield_dim_vMin"][1];    	


		seite = RandomInt(4);
		
		if(seite == 0) {
		
   			x = game["playfield_dim_vMin"][0] + randomInt(game["playfield_dim_breite"]);
    		y = game["playfield_dim_vMin"][1];    	
		}
		if(seite == 1) {
		
   			x = game["playfield_dim_vMin"][0] + randomInt(game["playfield_dim_breite"]);
    		y = game["playfield_dim_vMax"][1];    	
		}
		if(seite == 2) {
		
   			x = game["playfield_dim_vMin"][0];
    		y = game["playfield_dim_vMin"][1] + randomInt(game["playfield_dim_laenge"]);    	
		}
		if(seite == 3) {
		
   			x = game["playfield_dim_vMax"][0];
    		y = game["playfield_dim_vMin"][1] + randomInt(game["playfield_dim_laenge"]);    	
		}
				

		z = game["map_dim_vMin"][2]; // * Z-Daten von der Map, da vom Playfield zu gering! *
      	 
        
        m = RandomInt(3) + 1;
        
        for(i=0;i<m;i++) {

			if(isDefined(ambient_point)) {

				ambient_point.origin = (x - 500 + randomInt(1000),y - 500 + randomInt(1000),z);
								
				ambient_point playSound("am_dogs");				
			}			
			
			wait RandomInt(3) + 0.7;
		}
		
		wait 8;
	}
}


AmbientMetall() {

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


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


	


    ambient_point = spawn("script_model", (0,0,0));
    ambient_point hide();


	while(1) {

		wait 1;
		
		wait RandomInt(20) + 2;



		x = game["playfield_dim_vMin"][0];
   		y = game["playfield_dim_vMin"][1];    	


		seite = RandomInt(4);
		
		if(seite == 0) {
		
   			x = game["playfield_dim_vMin"][0] + randomInt(game["playfield_dim_breite"]);
    		y = game["playfield_dim_vMin"][1];    	
		}
		if(seite == 1) {
		
   			x = game["playfield_dim_vMin"][0] + randomInt(game["playfield_dim_breite"]);
    		y = game["playfield_dim_vMax"][1];    	
		}
		if(seite == 2) {
		
   			x = game["playfield_dim_vMin"][0];
    		y = game["playfield_dim_vMin"][1] + randomInt(game["playfield_dim_laenge"]);    	
		}
		if(seite == 3) {
		
   			x = game["playfield_dim_vMax"][0];
    		y = game["playfield_dim_vMin"][1] + randomInt(game["playfield_dim_laenge"]);    	
		}
				

		z = game["map_dim_vMin"][2]; // * Z-Daten von der Map, da vom Playfield zu gering! *
      	 
        
      

		if(isDefined(ambient_point)) {

			ambient_point.origin = (x - 500 + randomInt(1000),y - 500 + randomInt(1000),z);
			
			ambient_point playSound("am_metall");				
		}			
		
		
		wait 4;
	}
}




fireArtilleryMarker(origin) {


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


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



	// * Abschusspunkt finden *
	trace = bulletTrace((origin[0],origin[1],game["map_dim_vMax"][2] - 50),(origin[0],origin[1],origin[2]), false, undefined);		



	start_origin 	= trace["position"] + (0,0,10);

	start_hoehe		= start_origin[2];
	max_hoehe		= game["map_dim_vMax"][2] - 80;
	

	pos_x			= start_origin[0];
	pos_y			= start_origin[1];
	
	wind_x			= 3 - RandomInt(6);
	wind_y			= 3 - RandomInt(6);
	hoehe			= start_origin[2] + 50;
	

	gravitation 	= -0.5;
	geschoss_speed  = 0;
	berechn_hoehe   = 0;

	diff_hoehe = max_hoehe - start_hoehe;


	// * Wenn Abschuss-Start schon an Mapgrenzen-Hoehe, dann keine Leuchtkugel *
	if(diff_hoehe < 120) return;


	while(berechn_hoehe <= diff_hoehe) {
	
		geschoss_speed += gravitation*-1;
	
		berechn_hoehe += geschoss_speed;
	}

	

	
	while(1) {
	
		wait 0.07;

		geschoss_speed  += gravitation;
		
		pos_x			+= wind_x;
		pos_y			+= wind_y;		
		hoehe 			+= geschoss_speed;
	
		target_origin = (pos_x,pos_y,hoehe);
	
		playfx(game["artillery_signal_start"],target_origin);
	
	
		if(geschoss_speed < 0 && hoehe < (max_hoehe-start_hoehe)/3) break;
	}
	
	

}




WinterBreath() {

	self notify("WinterBreath");
	self endon("WinterBreath");

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

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



	if(maps\mp\gametypes\_admiral_mod_utils::isWinter()) {

	
		wait 5;

		
		while(1) {
					
			wait RandomFloat(4) + 2; 		

			// * Wenn gesprintet, dann nicht noch zusätzlich atmen *					
			if(isDefined(self.pers["sprint_wait"]) && self.pers["sprint_wait"] > 0) continue;		
					
			playfxOnTag ( game["cold_breath"], self, "TAG_EYE");
		}
	}
}



AmbientPlayOff() {

	wait 1;
	ambientPlay("ambient_silence");
}


