


InitFighters() {

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


	level.mod_max_fighters_on_map = 2;
	level.mod_fighter_rank_allow = 0;
	level.mod_fighter_flight_time = 30;

	CreateFighter(0);

}





GetNextFighterNumber() {

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


    if(!isDefined(level.fighter_in_game)) level.fighter_in_game = 0;
    
    
    if(level.fighter_in_game >= level.mod_max_fighters_on_map) return -1;
    
    level.fighter_in_game++;
    
    
    return level.fighter_in_game;
}




CreateFighter(fighter_nr) {

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


   
    // * Fighter Position ermitteln *
    pos_array = GetFighterMapPositionArray();

    // * Wenn auf aktueller Map kein Fighter sein soll *
    if(pos_array[1][0] == -2000 && pos_array[1][1] == -2000 && pos_array[1][2] == -2000) return;
    
	

    // * Wenn neuer Fighter, dann neue Nummer holen *
    if(fighter_nr == 0) {

        // * Neue Fighternummer holen *
        fighter_nr = GetNextFighterNumber();
    
        if(fighter_nr == -1) return;
    }


    
    // * Wenn für diesen Fighter keine Positon existiert, dann nicht erlauben *
    if(pos_array.size < fighter_nr) return;
           
    


    pos     = (pos_array[fighter_nr][0],pos_array[fighter_nr][1],pos_array[fighter_nr][2]);
    angles  = (pos_array[fighter_nr][3],pos_array[fighter_nr][4],pos_array[fighter_nr][5]);


    // * Flugzeug erzeugen *
    fightermodel = "xmodel/vehicle_stuka_flying";
        
    
    
    fighter = spawn("script_model", (0,0,0));
    fighter setModel(fightermodel);
    fighter.targetname = "fighters";
    fighter hide();    
    fighter.origin = (pos);
    fighter.angles = (angles);
    fighter show(); 


	
    // * Motorengeraeusch abspielen *
    fighter stoploopsound();
    fighter playloopsound("fighter_near");
    
    
    // * Flugzeug vom Radar entfernen *
    if(isDefined(level.fighter_objnum) && isDefined(level.fighter_objnum[fighter_nr])) {
    
        objective_delete(level.fighter_objnum[fighter_nr]);
        level.fighter_objnum[fighter_nr] = undefined;
    }
        
    // * Flugzeug auf dem Radar zeigen *  
    level.fighter_objnum[fighter_nr] = maps\mp\gametypes\_admiral_mod_compass::GetFreeObjective();
    objective_add(level.fighter_objnum[fighter_nr], "current", fighter.origin, level.flughafen_objective);
    objective_team(level.fighter_objnum[fighter_nr], "none");
            
    
   
    // * Variablen Reset *
    fighter.identity        = "fighter";
    fighter.number          = fighter_nr;
    fighter.do_status       = "none";    
    fighter.health_const    = 850; 
    fighter.health          = fighter.health_const; 
    
    fighter.rauch           = 0; 
    fighter.rauch_health    = 350;
        
    fighter.pilot_name      = "";
    fighter.pilot           = undefined;
    fighter.copilot_name    = "";
    fighter.copilot         = undefined;
    
    fighter.team            = "none";

    
 
    
    level.fighter_pilot_died[fighter.number]    = 0;
    level.fighter_copilot_died[fighter.number]  = 0;

    level.fighter_status[fighter.number]        = "nothing";


    // * Threads *
    //fighter thread WaitForDamage();       
    fighter thread WaitForStart();



    wait 0.1;

/*
    // * MG42 platzieren *

    // "mg42_bipod_stand_mp"
    // "mg42_bipod_duck_mp"
    // "mg42_bipod_prone_mp"


    position = fighter GetForwardPos(-85, 0, -2);
    
    
    fighter.mg42 = spawnTurret("misc_mg42", position, "mg42_bipod_duck_mp");
    fighter.mg42 setmodel ("xmodel/mg42_bipod");
    fighter.mg42.weaponinfo = "mg42_bipod_duck_mp";    
    fighter.mg42.angles = fighter.angles + (0,180,0);
    fighter.mg42 show();    
        
    fighter.mg42 linkto(fighter);
    
  
    fighter.mg42 SetTopArc(50);
    fighter.mg42 SetBottomArc(50);
    fighter.mg42 SetLeftArc(70);
    fighter.mg42 SetRightArc(70);
    
    fighter.mg42.fighter_nr = fighter_nr;

    fighter.mg42 thread maps\mp\_plat_mod_turret_gmi::turret_think();
*/    
    

   
    
    
}



WaitForDamage() {

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


    level endon("mod_boot");
    level endon("round_ended");
    self  endon("mod_plane_absturz");


             
     
    while(isDefined(self)) {
        
        
        // * Auf Treffer warten *
        self waittill ("damage", dmg, who, dir, point, mod);


        // * Wenn kein Spieler dann nicht Damage *
        if (!isPlayer(who)) continue;

        //who iprintln(who.name + " Sie haben getroffen !" + " Damage: " + dmg + " Dir: " + dir + " Point: " + point + " Mod: " + mod);
        
        // * Treffer  *
        if ( mod == "MOD_EXPLOSIVE" || mod == "MOD_PISTOL_BULLET" || mod == "MOD_RIFLE_BULLET" || mod == "MOD_PROJECTILE" || mod == "MOD_PROJECTILE_SPLASH" || mod == "MOD_GRENADE" || mod == "MOD_GRENADE_SPLASH"   || mod == "MOD_ARTILLERY" || mod == "MOD_ARTILLERY_SPLASH" || mod == "MOD_FLAME") {
            
            
            //iprintln("Damage: " + dmg);
            //iprintln("Mod: " + mod);
            
            // * Damage Werte verändern * 
            if(mod == "MOD_PISTOL_BULLET")      dmg = dmg / 4;
            if(mod == "MOD_RIFLE_BULLET")       dmg = dmg / 5;   

                                    
            self.health -= dmg;                        
        }
        
        // * Wenn Rauchgrenze erreicht, dann Rauch einschalten *
        if(self.health <= self.rauch_health && self.rauch == 0) self thread FighterRauch();
    
    
        // * Wenn Health < 1 dann Flugzeug abstuerzen lassen *
        if(self.health < 1) {
            
            self.do_status = "damage_die";
            
            self thread FighterGoingDown(who,0); 
            return;
        }
       
        
        wait 0.05;
    } 
}


FighterRauch() {

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


    level endon("mod_boot");
    level endon("round_ended");
    self  endon("mod_fighter_destroyed");

    self.rauch = 1;

    // * Solange Flugzeug nicht explodiert ist... Rauch erzeugen *
    while(1) {
    
       //playfx(level.plat_mod_stukkasmoke, self.origin); 
       wait 0.05;
   }
}



FighterRenew(old_fighter, delay_time) {

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


    self notify("mod_fighter_destroyed");
    
    level endon("mod_boot");


    if(delay_time < 0) delay_time = 0;



    fighter_nr = old_fighter.number;


    // * Wenn für diesen Fighter schon ein Renew läuft, dann nicht nochmal *
    if(level.fighter_status[fighter_nr] == "renew") return;
    level.fighter_status[fighter_nr] = "renew";



    // * Flugzeug vom Radar entfernen *
    if(isDefined(level.fighter_objnum) && isDefined(level.fighter_objnum[old_fighter.number])) {
    
        objective_delete(level.fighter_objnum[old_fighter.number]);
        level.fighter_objnum[old_fighter.number] = undefined;
    }


    // * Mögliche Steuerobjekte löschen *
    if(isDefined(old_fighter.org1))     old_fighter.org1 delete();
    if(isDefined(old_fighter.org2))     old_fighter.org2 delete();
    if(isDefined(old_fighter.augen))    old_fighter.augen delete();
   

    // * Flugzeug + MG42 loeschen *            
    //if(isDefined(old_fighter) && isDefined(old_fighter.mg42))   old_fighter.mg42 delete();
    if(isDefined(old_fighter))                                  old_fighter delete();     
    

    wait delay_time;


    
    level thread CreateFighter(fighter_nr);                 
} 



WaitForStart() {

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


    level endon("mod_boot");
    level endon("round_ended");
    self  endon("mod_plane_absturz");
        
 
    // * auf Pilot warten *
    self WaitForPilot();   
   
   
    // * Pilot Tod Check starten * 
    self thread CheckPilotTod();
    
    
    // * Flugzeug vom Radar entfernen *
    if(isDefined(level.fighter_objnum) && isDefined(level.fighter_objnum[self.number])) {
    
        objective_delete(level.fighter_objnum[self.number]);
        level.fighter_objnum[self.number] = undefined;
    }    
    
    
    // * Pilot reinsetzen und auf Copilot warten *    
    if(level.fighter_pilot_died[self.number] == 0) {
    
        // * Motorengeraeusch abspielen *   
        if(level.mod_planes_silent_mode == 1)   self playloopsound("fighter_near_sitin_silent");
        else                                    self playloopsound("fighter_near_sitin");
    
      
        // * auf Copilot warten *
        self thread WaitForCopilot();   

                
        // * 5 Sekunden auf Copilot warten * 
        for(i=0;i<5;i+=0.5) {
    
            // * Wenn Copilot eingestiegen dann los! *
            if(self.copilot_name != "") {
            
                if(isDefined(self.pilot.hud_copilot_wait_bgnd))   self.pilot.hud_copilot_wait_bgnd  destroy();
                if(isDefined(self.pilot.hud_copilot_wait))        self.pilot.hud_copilot_wait       destroy();
                if(isDefined(self.pilot.hud_copilot_wait_time))   self.pilot.hud_copilot_wait_time  destroy();
            
                break;
            }
            
            wait 0.5; 
        }
   
    
        // * Fighter starten *
        if(level.fighter_pilot_died[self.number] == 0)  self thread StartFighter();
        else {
        
            // * Fighter Reset, wenn Pilot zwischenzeitlich nicht mehr lebt *
            if(isDefined(self)) level thread FighterRenew(self,0);  
        }


    }
    else {
    
        // * Fighter Reset, wenn Pilot zwischenzeitlich nicht mehr lebt *
        if(isDefined(self)) level thread FighterRenew(self,0);      
    }
    
}



WaitForPilot() {

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


    level endon("mod_boot");
    level endon("round_ended");
    self  endon("mod_fighter_start");
    self  endon("mod_plane_absturz");

   

    while(isDefined(self)) {

        
        player = self GetPlayerTouch();

        if(isPlayer(player)) {
        
            self.team               = player.pers["team"];
        
            self.pilot_name         = player.name;
            self.pilot              = player;
            self.pilot.fighter_nr   = self.number;            
            
            self.pilot.do_status = "fighter_pilot";

            // * Pilot in Flugzeug setzen und in die Hocke *
            self.pilot.origin = self.origin + (0,0,0);             
            self.pilot linkto(self);
   
   
            // * Dem Pilot anzeigen das noch x Sek. auf den Copilot gewartet wird *
            //self thread maps\mp\gametypes\_plat_mod_hud_effects::ShowCopilotWaiting(self.pilot, 6);    

            //maps\mp\gametypes\_plat_mod_utils::setStance(self.pilot,"1");

            
            return;
        }        
    
        wait 0.5;
    } 
}

WaitForCopilot() {

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


    level endon("mod_boot");
    level endon("round_ended");
    self  endon("mod_fighter_start");
    self  endon("mod_plane_absturz");
    self  endon("mod_fighter_destroyed");
    
    // * Wenn Pilot schon tod, dann ende *
    if(level.fighter_pilot_died[self.number] == 1) return;

    self.pilot endon("mod_player_died");
        

    while(isDefined(self)) {

        player = self GetPlayerTouch();

        if(isPlayer(self.pilot) && isPlayer(player) && player != self.pilot) {
    
            self.copilot_name       = player.name;
            self.copilot            = player;
            self.copilot.fighter_nr = self.number;

            self.copilot.origin     = self.origin + (-50,0,-20); 
            self.copilot linkto(self);  
        
            self.copilot.do_status  = "fighter_copilot";
            
            return;
        }   
        
        wait 0.05;
    }
}


GetPlayerTouch() {

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


    level endon("mod_boot");
    level endon("round_ended");
    self  endon("mod_fighter_start");
    self  endon("mod_plane_absturz");    
   
  
    /*
    
    // * Bevor TouchCheck startet, prüfen ob jemand noch am MG42 hing, wenn ja davon von MG42 lösen *
    players = getentarray("player", "classname");  
    for(i = 0; i < players.size; i++) { 
        
        if(isDefined(players[i].fighter_mg42) && players[i].fighter_mg42 == true && isDefined(players[i].fighter_nr) && players[i].fighter_nr == self.number) {
                   
            players[i].fighter_mg42 = false;            
        }
    }
  	*/
  
  
    while(1) {
    
            
        player = "";
        players = getentarray("player", "classname");
                        
        for(i = 0; i < players.size; i++) {             
           
            player = players[i];
          
          
            // * Nur wenn der Player nichts anderes macht, darf er in das Flugzeug! *
            
            if(isDefined(player.do_status) && (player.do_status == "none" || player.do_status == "hand_on_fighter") && distance(player.origin, self.origin) < 200 && isAlive(player) && player.sessionstate == "playing" && isDefined(player.pers["am_stance"]) && player.pers["am_stance"]  != 0) {                        
                
                
                // * Wenn Pilot noch lebt, und auf Copilot gewartet wird, *
                // * dann die Hand nicht anzeigen wenn Player = Pilot *
                if(self.pilot_name != "" && player == self.pilot)  continue;
                                
                                
                // * Spieler muß mindestens Rang 3 haben und darf nicht liegen*
                if(player.pers["rank"] >= level.mod_fighter_rank_allow) {
                    
                    // * Player ist in Nähe des Fighters *
                    player.do_status  = "hand_on_fighter";
                    player.fighter_nr = self.number;
                    
                    
                    // * Hand anzeigen *
                    if(!isDefined(player.hand)) {
                            
                        player.hand = newClientHudElem(player);
                        player.hand.alignX = "center";
                        player.hand.alignY = "middle";
                        player.hand.x = 320;
                        player.hand.y = 285;
                        player.hand.alpha = 0.9;                        
                        player.hand setShader("gfx/icons/hint_usable.tga", 40, 40);                         
                    }
               
                    // * Wenn Player USE-Button drückt, oder am MG42 sitzt ... *
                    if(player useButtonPressed() || (isDefined(player.fighter_mg42) && player.fighter_mg42 == true) ) {                     
                    
                        // * Hand  *
                        if(isDefined(player.hand)) player.hand Destroy();
        
                        // * Spieler zurückgeben *
                        return player;  
                    }
                }
                else {
                    
                    // * Rank Hinweis anzeigen *
                    if(!isDefined(player.fighter_rank_txt)) {
                        
                        player.do_status    = "hand_on_fighter";
                        player.fighter_nr   = self.number;
                        
                        player.fighter_rank_bgnd = newClientHudElem(player);
                        player.fighter_rank_bgnd.alignX = "center";
                        player.fighter_rank_bgnd.alignY = "middle";                 
                        player.fighter_rank_bgnd.alpha = 0.8;
                        player.fighter_rank_bgnd.x = 0  + 320;
                        player.fighter_rank_bgnd.y = 377;
                        player.fighter_rank_bgnd.sort = -1;
                        player.fighter_rank_bgnd setShader("black", 260, 21);                           
                            
                        player.fighter_rank_txt = newClientHudElem(player);                     
                        player.fighter_rank_txt.alignX = "center";
                        player.fighter_rank_txt.alignY = "middle";                  
                        player.fighter_rank_txt.x = 320;
                        player.fighter_rank_txt.y = 376;    
                        player.fighter_rank_txt.alpha = 1;
                        player.fighter_rank_txt.fontScale = 1.0;    
                        player.fighter_rank_txt.color = (1,1,1);    
                        player.fighter_rank_txt setText(level.mod_text_fighter_rank);   
                    } 
                }
 
            }
            else {
                
                if(isDefined(player.fighter_nr) && player.fighter_nr == self.number) {
                
                    // * Wenn nicht mehr in Reichweite, dann Do-Status Reset *
                    if(isDefined(player.do_status) && player.do_status == "hand_on_fighter") {
                    
                        player.do_status = "none";
                
                        // * Hand und Rank Hinweis entfernen *
                        if(isDefined(player.hand))                  player.hand Destroy();
                        if(isDefined(player.fighter_rank_bgnd))     player.fighter_rank_bgnd Destroy();
                        if(isDefined(player.fighter_rank_txt))      player.fighter_rank_txt Destroy();                
                    
                        player.fighter_nr = 0;
                    }
                }
            }     
        }
        
        wait 0.2;
    } 
}




CheckPilotTod() {

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


    level endon("mod_boot");
    level endon("round_ended");
    self  endon("mod_fighter_pilot_parachuting");
    self  endon("mod_plane_landed");


    while(1) {

        if(level.fighter_pilot_died[self.number] == 1) {
    
            self.pilot_name = "";   // * Waiting for Copilot *
            self thread FighterGoingDown(undefined,0); 
            
            return;
        }
        
        wait 0.2;
    }
    
    return;
}


StartFighter() {

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


    self notify("mod_fighter_start");

    level endon("mod_boot");
    level endon("round_ended");
    self  endon("mod_plane_absturz");
    
    
    // * Wenn Pilot schon tod, dann ende *
    if(level.fighter_pilot_died[self.number] == 1) return;

    self.pilot endon("mod_player_died");    
    
    
    self.do_status = "flying";
    

    wait 1;


    // * Start-Hoehe merken (für den Absprung) *
    self.start_hoehe = self.origin[2];
    

    // * Absprung für Pilot und Copilot ermöglichen * 
    //if(self.pilot_name != ""    && level.fighter_pilot_died[self.number]     == 0)  self thread WaitForParachutePilot();
    //if(self.copilot_name != ""  && level.fighter_copilot_died[self.number]   == 0)  self thread WaitForParachuteCopilot();
    
    
    
    // * Alle Flugzeug-HUD Anzeigen entfernen *                                        
    self DestroyFighterHud();
    
    
    // * Motorengeraeusch abspielen *   
    if(level.mod_planes_silent_mode == 1)   self playloopsound("fighter_near_start01_silent");
    else                                    self playloopsound("fighter_near_start01");
        
    
    ziel = self GetForwardPos(200,0,0); 
    
    self.landepunkt_1 = ziel;
    self moveto(ziel,1);
    
    wait 1; 
        
    
    ziel = self GetForwardPos(300,0,0);   
    
    self.landepunkt_2 = ziel;
    self moveto(ziel,1);
    
    wait 1; 
    

    ziel = self GetForwardPos(400,10,40);   
    
    self.landepunkt_3 = ziel;
    self moveto(ziel,1);
        
    wait 1;
    

    // * Motorengeraeusch abspielen *   
    if(level.mod_planes_silent_mode == 1)   self playloopsound("fighter_near_start02_silent");
    else                                    self playloopsound("fighter_near_start02");


    
    ziel = self GetForwardPos(400,20,200);   
    
    self.landepunkt_4 = ziel;
    self moveto(ziel,0.7);
        
    wait 0.7;
    

    // * Einflugspunkt merken * 
    self.einflug_punkt = self.landepunkt_4;
    
    
    // * Nur wenn Pilot noch an Bord *
    if(self.pilot.do_status == "fighter_pilot") {
    
        // * Pilot höher setzen, damit er beim Lenken sehen kann *
        self.pilot unlink();
        self.pilot.origin = self.origin + (0,0,20); 
        self.pilot linkto(self);
        
        self.pilot disableWeapon(); 

    }
    

    // * Motorengeraeusch abspielen *   
    if(level.mod_planes_silent_mode == 1)   self playloopsound("fighter_near_combat_silent");
    else                                    self playloopsound("fighter_near_combat");


    weite   = randomint(1500) + 2000;    
    ziel    = self GetForwardPos(weite,0,200);    
    
    
    // * Landbahn-Ende Merken *
    self.landebahn_ende = ziel;
    
    
    
    // * Flug  *
        
    zeit = maps\mp\gametypes\_admiral_mod_utils::ZeitSpeedBerechnung(280, self.origin, ziel);
    self moveto(ziel,zeit);     

    wait zeit-1;
    
        

    // * Luftalarm-Sirene abspielen, wenn Zufallszahl > 50 *
    
    luftalarm_ausloesen = randomInt(100);   
    if(luftalarm_ausloesen > 40)    {
    
        level.mod_luftalarm_model playloopsound("luftalarm");
        
        self.luftalarm_is_on = true;
    }
    else  self.luftalarm_is_on = false;


 

    // * Pilot übernimmt jetzt das Flugzeug *
    self thread FlightFighter();

    
    
    // * Flugzeug Timer anzeigen *    
    //self thread maps\mp\gametypes\_plat_mod_hud_effects::ShowFlightTime(self.pilot,level.mod_fighter_flight_time);
        
    
    
    // ***  FLUGZEUG FLIEGT EINE DEFINIERTE ZEIT  ***
    
    for(i=0;i<level.mod_fighter_flight_time;i++) wait 1;
    
   
    
        
    self thread LandingFighter();

}


DestroyFighterHud() {

    players = [];
    players = getentarray("player", "classname");            
    
    
    // * bei allen Spielern die Flugzeug-HUD Elemente entfernen *
    for(i = 0; i < players.size; i++) {
    
        player = players[i];
        
        
        // * Nur die HUD-Elemente der Spieler löschen die in dem Flugzeug sitzen *
        if(isDefined(player.fighter_nr) && isDefined(self.number) && player.fighter_nr == self.number) {
    
            if(isDefined(player.hand))                              player.hand                       Destroy();
            if(isDefined(player.fighter_rank_bgnd))                 player.fighter_rank_bgnd          Destroy();
            if(isDefined(player.fighter_rank_txt))                  player.fighter_rank_txt           Destroy();                
    
            if(isDefined(player.hud_copilot_wait_bgnd))             player.hud_copilot_wait_bgnd      Destroy();
            if(isDefined(player.hud_copilot_wait))                  player.hud_copilot_wait           Destroy();
            if(isDefined(player.hud_copilot_wait_time))             player.hud_copilot_wait_time      Destroy();
    
            if(isDefined(player.hud_flight_time_bgnd))              player.hud_flight_time_bgnd       Destroy();
            if(isDefined(player.hud_flight_time))                   player.hud_flight_time            Destroy();
            if(isDefined(player.hud_flight_time_time))              player.hud_flight_time_time       Destroy();
    
            if(isDefined(player.hud_fighter_health_bgnd))           player.hud_fighter_health_bgnd    Destroy();
            if(isDefined(player.hud_fighter_health))                player.hud_fighter_health         Destroy();
    
            if(isDefined(player.hud_bombs_bgnd))                    player.hud_bombs_bgnd             Destroy();
            if(isDefined(player.hud_bombs))                         player.hud_bombs                  Destroy();
            if(isDefined(player.hud_bombs_counter))                 player.hud_bombs_counter          Destroy();
            
            if(isDefined(player.hud_rockets_bgnd))                  player.hud_rockets_bgnd           Destroy();
            if(isDefined(player.hud_rockets))                       player.hud_rockets                Destroy();
            if(isDefined(player.hud_rockets_counter))               player.hud_rockets_counter        Destroy();

            if(isDefined(player.hud_target_left))                   player.hud_target_left            Destroy();
            if(isDefined(player.hud_target_right))                  player.hud_target_right           Destroy();
            if(isDefined(player.hud_target_top))                    player.hud_target_top             Destroy();
            if(isDefined(player.hud_target_bottom))                 player.hud_target_bottom          Destroy();


        }
    }
}


WaitForParachutePilot() {

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


    level endon("mod_boot");
    level endon("round_ended");
    self  endon("mod_fighter_destroyed");
    self  endon("mod_plane_landed");

       
    self.pilot endon("mod_player_died");
    self.pilot endon("mod_player_spawn");

    while(1) {
             
        // * Pilot-Absprung *
        
        if(isPlayer(self.pilot) && isAlive(self.pilot) && self.pilot.sessionstate == "playing") {

        
            if(self.pilot meleeButtonPressed()) {                 
         
                self DestroyFighterHud();
                
                
                self.pilot enableWeapon(); 

            
                // * Pilot vom Flugzeug lösen *
                if(isPlayer(self.pilot)) self.pilot unlink();                
                
                
                // * Pilot abspringen lassen *
                if(isPlayer(self.pilot) && isAlive(self.pilot)) {
                                        
                    // * Nur bei einer groesseren Hoehe *
                    if(isDefined(self) && self.pilot.origin[2] - self.start_hoehe > 250) {
                    
                        //self.pilot thread maps\mp\gametypes\_plat_mod_player::PlayerToEarth((0,0,0));           
                    }
                    else {
                    
                        self.pilot.do_status = "none";
                    }
                   
                    self.pilot.fighter_mg42 = false;
                }

                // * Flugzeug mitteilen das Pilot abgesprungen ist * 
                self notify("mod_fighter_pilot_parachuting");

                wait 0.5;
                
                self thread FighterGoingDown(undefined,0.5);    
            
                return;            
            }
        }
    
        wait 0.2;
    }
}

WaitForParachuteCopilot() {

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


    level endon("mod_boot");
    level endon("round_ended");
    self  endon("mod_fighter_destroyed");
    self  endon("mod_plane_landed");

       
    self.copilot endon("mod_player_died");
    self.copilot endon("mod_player_spawn");


    while(1) {
             
        // * Co-Pilot-Absprung *
        
        if(isPlayer(self.copilot) && isAlive(self.copilot) && self.copilot.sessionstate == "playing") {
        
            if(self.copilot meleeButtonPressed()) {                 
         
         
                // * CoPilot vom Flugzeug lösen *
                if(isPlayer(self.copilot)) self.copilot unlink();
                
                // * CoPilot Abspringen lassen *               
                if(isPlayer(self.copilot) && isAlive(self.copilot)) {
                                    
                    // * Nur bei einer groesseren Hoehe *
                    if(isDefined(self) && self.copilot.origin[2] - self.start_hoehe > 250) {
                    
                        //self.copilot thread maps\mp\gametypes\_plat_mod_player::PlayerToEarth((0,0,0));           
                    }                                        
                    else {
                                       
                        self.copilot.do_status = "none";
                    }
                    
                    self.copilot.fighter_mg42   = false;
                }
                
                return;
            }
            
            
        }
    
        wait 0.2;
    }
}


FlightFighter() {

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


    level endon("mod_boot");
    level endon("round_ended");
    level endon("intermission");
    self  endon("mod_fighter_pilot_parachuting");
    self  endon("mod_plane_absturz");
    self  endon("mod_fighter_landing");

    
    if(level.fighter_pilot_died[self.number] == 1) return;
    
    self.pilot endon("mod_player_died");    


       
    
    // * Pilot kann Bomben abwerfen *
    //self thread BombThink(self.pilot);
    
    // * Pilot kann Raketen abschiessen *
    //self thread FireThink(self.pilot);
    
                        
    self.org1 = spawn("script_model", (self.pilot.origin));   
    self.org2 = spawn("script_model", (self.pilot.origin));
            
    self.org1 hide();
    self.org2 hide();
    

    self.augen = spawn("script_model", self.pilot.origin);
    self.augen linkto (self.pilot,"tag_weapon_left",(0,200,0),(0,0,0));
    self.augen hide();


    diff_old = 0.0;

    while(isPlayer(self.pilot)) {
      
      
        stance = self.pilot.pers["am_stance"];

		if(!isDefined(stance)) stance = 2;
		
                
        if(stance != 2) {
            
            while(stance != 2) {
            
                stance = self.pilot.pers["am_stance"];
            	if(!isDefined(stance)) stance = 2;
		
                //maps\mp\gametypes\_plat_mod_utils::setStance(self.pilot,"0");
                wait 0.1;
            }
        }
       
        if(stance == 2)        augenwinkel = (self.augen.angles[2]) * 2;
        else                   augenwinkel = 0;
		
		
        iprintln(self.augen.angles[2]);
        
        
            
        // * Wen max. Hoehe erreicht, dann nicht weiter steigen *   
        if(self.origin[2] >= (game["map_dim_vMax"][2] - 100)) augenwinkel = 10;
                
        
        blickwinkel_rakete  = vectortoangles(vectornormalize((self.org2.origin + (0,0,0)) - self.org1.origin)) + (90,0,0);
        blickwinkel_fighter = vectortoangles(vectornormalize((self.org1.origin + (0,0,0)) - self.origin));

        diff = self.angles[1] - self.org1.angles[1];

        if(diff-diff_old > 90 || diff-diff_old < -90) diff = diff_old;

        if(diff < -70) diff = -70;
        if(diff > 70)  diff = 70;
        
        
        diff_old = diff;
        
        
        self.org1.angles    = blickwinkel_rakete;
        self.angles         = blickwinkel_fighter + (0,0,diff);
                
        
        forwardvector   = anglestoforward(self.pilot.angles + (augenwinkel,0,0));                
        ziel_pos        = self.pilot.origin + (VectorMulti(forwardvector,1200)) +(0,0,0);
        
        self.org2.origin     = ziel_pos;
            
        objekt_verfolgen(self.org1,self.org2,0.6,0);       
        objekt_verfolgen(self,self.org2,1.4,0);
         
        
              
        // * Pilot irgendwo gegen geflogen ? *    
        forwardvector   = anglestoforward(self.angles);              
        ziel_pos        = self.origin + (VectorMulti(forwardvector,50));

        trace=bulletTrace(self.origin,ziel_pos,false, undefined); 
        if(trace["fraction"] != 1)  {
            
            if(level.mapname != "mp_railyard") {
            
                if(isDefined(self)) self thread FighterGoingDown(undefined,0);
                if(isPlayer(self.pilot) && isPlayer(self.pilot) && self.pilot.sessionstate == "playing") self.pilot suicide();            
            }
        }
       

        
        wait 0.05;      
    } 
}



FighterGoingDown(who,waittime) {

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



    // * Bei allen Spielern die Hud-Elemente entfernen *
    self DestroyFighterHud();


    wait waittime; // Wenn ein Spieler abspringt, das Flugzeug nicht sofort am Spieler explodiert!


    if(isDefined(self) && isDefined(self.mg42)) self.mg42 hide();


    // * Nur wenn Flugzeug schon gestartet oder von einem anderen Spieler zerstört, dann abstürzen lassen *
    if(self.do_status == "flying" || self.do_status == "damage_die") {
    
        // * Flugzeug abstürzen lassen *
        self FlugzeugAbsturz(who, 230); 
    }

                            
    self notify("mod_fighter_destroyed");
    wait 1;
    
    // * Luftalarm abstellen *
    level.mod_luftalarm_model stoploopsound();
                       
    
    // * Fighter Wartezeit *     
    if(self.do_status == "flying" || self.do_status == "damage_die")    level.mod_fighter_wait = level.mod_waittime_next_fighter;         
    else                                                                level.mod_fighter_wait = 0;
   
   
    // * Renew, loeschen und erneuern *
    if(isDefined(self)) level thread FighterRenew(self,level.mod_fighter_wait);  
}




BombThink(pilot) {

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


    level endon("mod_boot");
    level endon("round_ended");
    self  endon("mod_plane_absturz");   
    self  endon("mod_fighter_landing");


    if(level.fighter_pilot_died[self.number] == 1) return; 
    
    pilot endon("mod_player_died");



    bomben = level.mod_fighter_bombs;
    
    
    if(isDefined(pilot.hud_bombs_bgnd))             pilot.hud_bombs_bgnd            destroy();
    if(isDefined(pilot.hud_bombs))                  pilot.hud_bombs                 destroy();
    if(isDefined(pilot.hud_bombs_counter))          pilot.hud_bombs_counter         destroy();
    if(isDefined(pilot.hud_fighter_health_bgnd))    pilot.hud_fighter_health_bgnd   destroy();
    if(isDefined(pilot.hud_fighter_health))         pilot.hud_fighter_health        destroy();
        
        
    pilot.hud_bombs_bgnd = newClientHudElem(pilot);
    pilot.hud_bombs_bgnd.alpha = 0.8;
    pilot.hud_bombs_bgnd.x = 0;
    pilot.hud_bombs_bgnd.y = 110;
    pilot.hud_bombs_bgnd.sort = -1;
    pilot.hud_bombs_bgnd setShader("black", 150, 17);
            
    pilot.hud_bombs = newClientHudElem(pilot);  
    pilot.hud_bombs.x = 18;
    pilot.hud_bombs.y = 112; 
    pilot.hud_bombs.alpha = 1;
    pilot.hud_bombs.fontScale = 1;  
    pilot.hud_bombs.color = (1,1,1);    
    pilot.hud_bombs setText(level.mod_text_bomber_pilot);   
                
    pilot.hud_bombs_counter = newClientHudElem(pilot);  
    pilot.hud_bombs_counter.x = 122;
    pilot.hud_bombs_counter.y = 111;
    pilot.hud_bombs_counter.alpha = 1;
    pilot.hud_bombs_counter.fontScale = 1;  
    pilot.hud_bombs_counter.color = (1, 0.2, 0.2);  
    pilot.hud_bombs_counter setValue(bomben);       


    pilot.hud_fighter_health_bgnd = newClientHudElem(pilot);
    pilot.hud_fighter_health_bgnd.alpha = 0.8;
    pilot.hud_fighter_health_bgnd.x = 0;
    pilot.hud_fighter_health_bgnd.y = 92;
    pilot.hud_fighter_health_bgnd.sort = -1;
    pilot.hud_fighter_health_bgnd setShader("black", 150, 8);

    pilot.hud_fighter_health = newClientHudElem(pilot);             
    pilot.hud_fighter_health.alignX = "left";
    pilot.hud_fighter_health.alignY = "middle";
    pilot.hud_fighter_health.x = 18;
    pilot.hud_fighter_health.y = 96;
    pilot.hud_fighter_health.alpha = 1.0;
    pilot.hud_fighter_health setShader("white", 110,4);
    pilot.hud_fighter_health.color = (0.2, 1, 0.2);             
    
    old_health  = 0;
        
    while(1) {

               
        if(pilot useButtonPressed() && bomben > 0) {
    
            self thread BombIsGoing(pilot);
            bomben--;
            
            if(isDefined(pilot.hud_bombs_counter)) pilot.hud_bombs_counter setValue(bomben);
            
            wait 0.2 + randomFloat(0.4);
        }   
    
        if(old_health != self.health && self.health > 0) {
        
            laenge  = self.health * 0.15;
            
            if(laenge < 0) laenge = 0;
            
            if(self.health <= self.rauch_health) if(isDefined(pilot.hud_fighter_health)) pilot.hud_fighter_health.color = (1, 0.0, 0.0);  
            
            
            if(isDefined(pilot.hud_fighter_health)) pilot.hud_fighter_health scaleOverTime(0.2, laenge, 4);
            
            old_health = self.health;
        }
        
        
        if(bomben < 1) return;
        
        wait 0.2;               
    }
    

}



FireThink(pilot) {

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


    level endon("mod_boot");
    level endon("round_ended");
    self  endon("mod_plane_absturz");   
    self  endon("mod_fighter_landing");


    if(level.fighter_pilot_died[self.number] == 1) return; 
    
    pilot endon("mod_player_died");



    raketen = level.mod_fighter_rockets;
    
    
    if(isDefined(pilot.hud_rockets_bgnd))             pilot.hud_rockets_bgnd            destroy();
    if(isDefined(pilot.hud_rockets))                  pilot.hud_rockets                 destroy();
        
        
    pilot.hud_rockets_bgnd = newClientHudElem(pilot);
    pilot.hud_rockets_bgnd.alpha = 0.8;
    pilot.hud_rockets_bgnd.x = 0;
    pilot.hud_rockets_bgnd.y = 136;
    pilot.hud_rockets_bgnd.sort = -1;
    pilot.hud_rockets_bgnd setShader("black", 150, 17);
            
    pilot.hud_rockets = newClientHudElem(pilot);  
    pilot.hud_rockets.x = 18;
    pilot.hud_rockets.y = 138; 
    pilot.hud_rockets.alpha = 1;
    pilot.hud_rockets.fontScale = 1;  
    pilot.hud_rockets.color = (1,1,1);    
    pilot.hud_rockets setText(level.mod_text_rocket_pilot);   
                
    pilot.hud_rockets_counter = newClientHudElem(pilot);  
    pilot.hud_rockets_counter.x = 122;
    pilot.hud_rockets_counter.y = 137;
    pilot.hud_rockets_counter.alpha = 1;
    pilot.hud_rockets_counter.fontScale = 1;  
    pilot.hud_rockets_counter.color = (1, 0.2, 0.2);  
    pilot.hud_rockets_counter setValue(raketen);       
  
  
    pilot.hud_target_left = newClientHudElem(pilot);  
    pilot.hud_target_left.x = 220;
    pilot.hud_target_left.y = 240;
    pilot.hud_target_left.alpha = 0.1;     
    pilot.hud_target_left.color = (1, 1, 1);  
    pilot.hud_target_left setShader("white", 5, 5);     

    pilot.hud_target_right = newClientHudElem(pilot);  
    pilot.hud_target_right.x = 420;
    pilot.hud_target_right.y = 240;
    pilot.hud_target_right.alpha = 0.1;     
    pilot.hud_target_right.color = (1, 1, 1);  
    pilot.hud_target_right setShader("white", 5, 5);     

    
    if(level.gametype != "bas") {
    
        pilot.hud_target_top = newClientHudElem(pilot);  
        pilot.hud_target_top.x = 320;
        pilot.hud_target_top.y = 140;
        pilot.hud_target_top.alpha = 0.1;     
        pilot.hud_target_top.color = (1, 1, 1);  
        pilot.hud_target_top setShader("white", 5, 5);     

        pilot.hud_target_bottom = newClientHudElem(pilot);  
        pilot.hud_target_bottom.x = 320;
        pilot.hud_target_bottom.y = 340;
        pilot.hud_target_bottom.alpha = 0.1;     
        pilot.hud_target_bottom.color = (1, 1, 1);  
        pilot.hud_target_bottom setShader("white", 5, 5);     
    }
    
        
    while(1) {

        
        // * Target-Check nach Priorität, Reihenfolge: Fighters, Player, Planes * 
        target_objekt = self TargetsCheck("fighters");
        
        if(target_objekt["targetname"] == "") target_objekt = self TargetsCheck("planes");
        if(target_objekt["targetname"] == "") target_objekt = self TargetsCheck("script_vehicle");
        if(target_objekt["targetname"] == "") target_objekt = self TargetsCheck("player");
        
        
        
        if(target_objekt["targetname"] != "") {
        
            if(isDefined(pilot.hud_target_left))    {
            
                pilot.hud_target_left.x = (320 - 110)   + target_objekt["range"] * 100;
                pilot.hud_target_left.alpha = 0.2 + target_objekt["range"];
                pilot.hud_target_left.color = (1, 1 - target_objekt["range"], 1 - target_objekt["range"]);
            }
            if(isDefined(pilot.hud_target_right))   {
            
                pilot.hud_target_right.x = (320 + 110)  - target_objekt["range"] * 100;
                pilot.hud_target_right.alpha = 0.2 + target_objekt["range"];
                pilot.hud_target_right.color = (1, 1 - target_objekt["range"], 1 - target_objekt["range"]);
            }
            if(isDefined(pilot.hud_target_top))     {
            
                pilot.hud_target_top.y = (240 - 110)    + target_objekt["range"] * 100;
                pilot.hud_target_top.alpha = 0.2 + target_objekt["range"];
                pilot.hud_target_top.color = (1, 1 - target_objekt["range"], 1 - target_objekt["range"]);
            }
            if(isDefined(pilot.hud_target_bottom))  {
            
                pilot.hud_target_bottom.y = (240 + 110) - target_objekt["range"] * 100;
                pilot.hud_target_bottom.alpha = 0.2 + target_objekt["range"];
                pilot.hud_target_bottom.color = (1, 1 - target_objekt["range"], 1 - target_objekt["range"]);
                
            }
            
        }
        else {
        
            if(isDefined(pilot.hud_target_left))    {
                
                pilot.hud_target_left.x = 220;
                pilot.hud_target_left.alpha = 0.1;
                pilot.hud_target_left.color = (1, 1, 1);
            }
            if(isDefined(pilot.hud_target_right))   { 
                
                pilot.hud_target_right.x = 420;
                pilot.hud_target_right.alpha = 0.1;
                pilot.hud_target_right.color = (1, 1, 1);
            }
            if(isDefined(pilot.hud_target_top))     { 
            
                pilot.hud_target_top.y = 140;
                pilot.hud_target_top.alpha = 0.1;
                pilot.hud_target_top.color = (1, 1, 1);
            }
            if(isDefined(pilot.hud_target_bottom))  {
            
                pilot.hud_target_bottom.y = 340;
                pilot.hud_target_bottom.alpha = 0.1;
                pilot.hud_target_bottom.color = (1, 1, 1);
            }
        }
        
        
        if(pilot attackButtonPressed() && raketen > 0) {
                  
    
            self thread RocketIsGoing(pilot,target_objekt);
            raketen--;
            
            if(isDefined(pilot.hud_rockets_counter)) pilot.hud_rockets_counter setValue(raketen);
            
            wait 1;
        }   
    
        
        if(raketen < 1) {
        
            if(isDefined(pilot.hud_target_left))    pilot.hud_target_left.alpha = 0.0;
            if(isDefined(pilot.hud_target_right))   pilot.hud_target_right.alpha = 0.0;
            if(isDefined(pilot.hud_target_top))     pilot.hud_target_top.alpha = 0.0;
            if(isDefined(pilot.hud_target_bottom))  pilot.hud_target_bottom.alpha = 0.0;
            
            return;
            
        }
        
        wait 0.1;               
    }
    

}









LandingFighter() {

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


    self  notify("mod_fighter_landing");

    level endon("mod_boot");
    level endon("round_ended");
    self  endon("mod_plane_absturz");


    if(level.fighter_pilot_died[self.number] == 1) return;   

    self.pilot endon("mod_player_died");    
 
  
  
     
    
    // * Nur wenn Pilot noch an Bord *
    if(self.pilot.do_status == "fighter_pilot") {

        // * Pilot wieder runter setzen, da er nicht mehr lenken muß *
    
        self.pilot unlink();
        self.pilot.origin = self.origin + (0,0,0); 
        self.pilot linkto(self);

        self DestroyFighterHud();    

    }
    
        
    // * und wieder zurück zum Ausgangspunkt (Landebahn Ende) *     
    
    self.angles = vectortoangles(vectornormalize(self.landebahn_ende - self.origin)) + (0,0,0);        
    zeit = maps\mp\gametypes\_admiral_mod_utils::ZeitSpeedBerechnung(280,self.origin,self.landebahn_ende);
    self moveto(self.landebahn_ende,zeit);


    self.pilot enableWeapon();

    wait zeit;  

    
    // * die letzte Luftalarm-Sirene abspielen *
    if(self.luftalarm_is_on == true) level.mod_luftalarm_model playloopsound("luftalarm");


       
    self.angles = vectortoangles(vectornormalize(self.einflug_punkt - self.origin));        
    zeit = maps\mp\gametypes\_admiral_mod_utils::ZeitSpeedBerechnung(280,self.origin,self.einflug_punkt);
    self moveto(self.einflug_punkt,zeit); 

    wait zeit;


    // * Motorengeraeusch abspielen *   
    if(level.mod_planes_silent_mode == 1)   self playloopsound("fighter_near_start02_silent");
    else                                    self playloopsound("fighter_near_start02");

 

    // * Luftalarm abstellen *
    level.mod_luftalarm_model stoploopsound();

    level.bombing_allowed = undefined;


        
    ziel = self.landepunkt_3;    
    self moveto(ziel,0.7);        
    
    wait 0.7;




    // * Motorengeraeusch abspielen *   
    if(level.mod_planes_silent_mode == 1)   self playloopsound("fighter_near_start01_silent");
    else                                    self playloopsound("fighter_near_start01");

    
    ziel = self.landepunkt_2;    
    self moveto(ziel,1);    

    wait 1; 



    
    ziel = self.landepunkt_1;    
    self moveto(ziel,1);    

    wait 1; 

   

    // * Motorengeraeusch abspielen *   
    if(level.mod_planes_silent_mode == 1)   self playloopsound("fighter_near_silent");
    else                                    self playloopsound("fighter_near");



    // * Pilot und Copilot aussteigen lassen *
    if(level.fighter_pilot_died[self.number] == 0 && isPlayer(self.pilot)) {
    
        self.pilot unlink();
        self.pilot.do_status    = "none";
        self.pilot.fighter_mg42 = false;
        
    }
    if(level.fighter_copilot_died[self.number] == 0 && isPlayer(self.copilot)) {
    
        self.copilot unlink();
        self.copilot.do_status      = "none";
        self.copilot.fighter_mg42   = false;
    }

        
    //maps\mp\gametypes\_plat_mod_utils::setStance(self.pilot,"0");

  
        
    // * Flugzeug wieder richtig positionieren *
    self.angles = vectortoangles(vectornormalize(self.origin) + (0,0,0));   
    
    
    // * Damit kein Pilot-Die mehr passieren kann und das Flugzeug nicht erneuert wird *
    self  endon("mod_plane_landed");
    
    
    wait 5;
    
    
    // * Fighter Reset, wenn Pilot zwischenzeitlich nicht mehr lebt *
        
    level thread FighterRenew(self,0);  
    
    

}




FlugzeugAbsturz(who, speed) {


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


    self  notify("mod_plane_absturz");
    
    level endon("mod_boot");    
    


    // * Feuer im Flugzeug wenn von Player abgeschossen *
    self.feuer = 1;

    // * Wenn nicht Player, dann kein Feuer *
    if(!isDefined(who)) self.feuer = 0;



    rnd_feuer = randomint(100);    
    
    // * Feuer per Zufall *
    if(rnd_feuer > 50) self.feuer = 1;
        
 


    
    // * Absturzwinkel einstellen *
    winkel = self.angles;
            
    neigung_vorne   = randomInt(50)  + 20;
    neigung_seite   = randomInt(50)  - 25;
    neigung_drehung = 200 - randomInt(400);
            
    winkel_start    = winkel + (neigung_vorne,neigung_seite,0);



    // * Einschlagspunkt berechnen *
                
    forwardvector   = anglestoforward(winkel_start);              
    ziel_pos        = self.origin + (VectorMulti(forwardvector,30000));
        
    // * Wenn Punkt gefunden, dann nehmen, wenn nicht dann Flugzeug sofort abstürzen lassen *    
    trace=bulletTrace(self.origin,ziel_pos,false, undefined); 
    if(trace["fraction"] != 1 && trace["position"][2] > game["map_dim_vMin"][2] - 500)    ziel_pos = trace["position"];
    else                                                                                ziel_pos = self.origin + (VectorMulti(forwardvector,100));
 
  	if(game["am_error_logfile"] == 1) logPrint("Exploding Dist: "+ distance(self.origin,ziel_pos)+ " \n");
 
  
    zeit = maps\mp\gametypes\_admiral_mod_utils::ZeitSpeedBerechnung(speed,self.origin,ziel_pos);
    
       
    
    // * Nur wenn Einschlagpunkt nicht so nah, dann fliegen *
    if(zeit > 1) {

        
        drehung_start = 0;
        
        // * Flugzeug zur Absturzposition fliegen * 
        self moveto(ziel_pos,zeit);
    
                
        neigungs_zeit = 0.5;
        
        nv = (neigung_vorne/(neigungs_zeit/0.05));
        ns = (neigung_seite/(neigungs_zeit/0.05));
        nr = (neigung_drehung/(zeit/0.05));
        
        
        // * Das muß hier drinbleiben (für die Zeit und die Planes) *
        for(i=0;i<zeit;i+=0.05) {
                    
            if(i <= neigungs_zeit) {
                
                self.angles += (nv,0,0);
                self.angles += (0,ns,0);
                self.angles += (0,0,nr);
            }
            else {
                
                if(drehung_start == 0) {
                                    
                    //self.angles   = winkel_start;
                    drehung_start   = 1;
                }
                
                self.angles += (0,0,nr);
            }
        
            // * Rauch *
            playfx(level.plat_mod_stukkasmoke, self.origin);
            
            // * Feuer *
            if(self.feuer == 1) playfx(level.plat_mod_stukkafire, self.origin);
            
            wait 0.05;
        }
    }       
        
  
    
    wait 0.05;
            
    
    self hide();
    
    self stoploopsound();
              
    
    // * Plane nicht mehr da *
    self notify("mod_plane_die");
    
    
    
    // * Wenn Plane Motor-Sound aktiv, dann löschen *
    if(isDefined(self.plane_sound)) {
                
        self.plane_sound stopLoopSound();
        self.plane_sound delete();
    }
    
    // * Wenn Dive Sound aktiv, dann löschen *
    if(isDefined(self.plane_dive_sound)) {
                
        self.plane_dive_sound stopLoopSound();
        self.plane_dive_sound delete();
    }
    
    
    
    
    // * 1. Explosion *
    
    // Play sound if defined
    self playSound("mortar_explode_default");
    
    playfx(level.plat_mod_planefx, self.origin);
            
                        
    // * do the damage *
    //self DamageToPlayers(who,self.origin,700,90,1,"MOD_EXPLOSIVE","none");
                                
                            
    // * Earthquake *       
    earthquake(0.8, 1.5, self.origin, 750);     
    
    
    wait 0.3;
    wait(randomFloat(1));
    
    
    
    // * 2. Explosion *
    
    // Play sound if defined
    self playSound("mortar_explode_default");

    playfx(level.plat_mod_planefx, self.origin);
            
                            
    // * do the damage *
    //self DamageToPlayers(who,self.origin,700,90,1,"MOD_EXPLOSIVE","none");
                                    
                                
    // * Earthquake *       
    earthquake(0.8, 1.5, self.origin, 750);     
    
    
    wait 0.5;
    wait(randomFloat(1.7));
    
    
    // * 3. Explosion *
    
    // Play sound if defined
    self playSound("mortar_explode_default");
                
    playfx(level.plat_mod_planefx, self.origin);
            
                            
    // * do the damage *    
    //self DamageToPlayers(who,self.origin,700,90,1,"MOD_EXPLOSIVE","none");
                                    
                                
    // * Earthquake *       
    earthquake(0.8, 1.5, self.origin, 750);     
    
    
    
    // * Punkte vergeben *  
    if(!isDefined(who)) {
    
        // * Keine Punkte vergeben *
    }
    else {
        
        if(isPlayer(who)) {
        
            //who maps\mp\gametypes\_plat_mod_player::SetPlayerPoints(3);            

            iprintln("^8" + who.name + "  destroyed a plane!"); 
        }
    }
    
    return;    
}




BombIsGoing(who) {
    

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

    bombmodel = "xmodel/o_ge_prp_stukabomb";
    bombfx    = level.plat_mod_mortarfx_default;


    bomb = spawn("script_model", (0,0,0));
    bomb setModel(bombmodel);
    bomb hide();
    
   
      
                
    x = self.origin[0];
    y = self.origin[1];                
    z = self.origin[2];
        
      
       
    startpoint  = (x,y,z);
    bomb.origin = startpoint;
        
        
    // * Impact Point *
        
    trace = bulletTrace((x,y,z - 200),(x,y,z - 30000), false, undefined);                   
    if(trace["fraction"] != 1)  z = trace["position"][2];
    else                        z = game["map_dim_vMin"][2];
        
        
    // * Blickrichtung *
    bomb.angles = self.angles + (-180 + randomint(50),0,0);
    
    forwardvector   = anglestoforward(self.angles);                
    ziel_pos        = (x,y,z) + (VectorMulti(forwardvector,500));

    
    
    endorigin = ziel_pos;
                
   
       
    wait .05;
    
    
            
    // * Bomb Sound *
    bomb playSound("mortar_incoming5");
    
    
    // * Fallzeit ermitteln *
    falltime = maps\mp\gametypes\_admiral_mod_utils::ZeitSpeedBerechnung(500,startpoint,endorigin);
    
  
    
    bomb show();
    wait .05;
    
    // * Bombe bewegen *
    bomb moveto(endorigin, falltime);
    
    // * Fallzeit *
    wait falltime;
    
    
    
    
    // * Einschlag Effect anzeigen *
    
    if(level.isWinter == 1) {
        
        bombfx = level.plat_mod_mortarfx_snow;
        soundtyp = "snow";
    }
    else {
    
        e = randomInt(4);
        
        soundtyp = "default";
        
        switch(e) {
        
            case 0:
                bombfx = level.plat_mod_mortarfx_default;
                soundtyp = "default";
                break;
            case 1:
                bombfx = level.plat_mod_mortarfx_grass;
                soundtyp = "grass";
                break;
            case 2:
                bombfx = level.plat_mod_mortarfx_dirt;
                soundtyp = "dirt";
                break;
            case 3:
                bombfx = level.plat_mod_mortarfx_default;
                soundtyp = "default";
                break;            
        } 
    }
    
    playfx(bombfx, endorigin);
            
    wait .05;
        
                    
    // * Explosion abspielen *      
    bomb playSound("mortar_explode_" + soundtyp);

    bomb hide();    


    // * Zusatzexplosionen anzeigen *
    if(randomint(100) > 30) {
    
        playfx(level.plat_mod_bomben_explosion, endorigin);    
        playfx(level.plat_mod_bomben_explosion2, endorigin);
    }
           
   
   
   
    // * Damage *
    //self DamageToPlayers(who,endorigin,800,90,1,"MOD_EXPLOSIVE","binoculars_artillery_mp");
                    
    // * Damage Tanks *
    //self DamageToTanks(who,endorigin,800,3000,1,"MOD_EXPLOSIVE","binoculars_artillery_mp");                

    // * Damage Tanks *
    //self DamageToFlaks(who,endorigin,800,10000,1,"MOD_EXPLOSIVE","binoculars_artillery_mp");                

    // * Damage Bases *
    //self DamageToBases(who,endorigin,800,7000,1,"MOD_EXPLOSIVE","binoculars_artillery_mp");                
                
    // * Earthquake *       
    earthquake(0.8, 1.5, endorigin, 750);       
    
    
    
    wait 3;
    
    // * Bombe loeschen *
    bomb delete();
   
    return;
}




RocketIsGoing(who, target_objekt) {
    
  

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

    rocketmodel = "xmodel/w_projectile_tank_shell";
    rocketfx    = level.plat_mod_bazooka_smoke;


    rocket = spawn("script_model", (0,0,0));
    rocket setModel(rocketmodel);
    rocket hide();
    
   
      
    // * Position vom feuernden Objekt *                
    x = self.origin[0];
    y = self.origin[1];                
    z = self.origin[2];
                     
    startpoint  = (x,y,z);
    rocket.origin = startpoint;

	endorigin = startpoint;


    // * Blickrichtung der Rakete *
    rocket.angles = self.angles + (0,0,0);

    
      
    
    rocket show();
    wait .05;


    // * Rakete Sound *
    rocket playSound("weap_bazooka_fire");
    

    lenkung_aus = false;

	ziel_objekt = undefined;


    // * Wenn kein Target, dann  fliegt Rakete nur so in eine Richtung *
    if(target_objekt["targetname"] != "") {
        
        if(target_objekt["targetname"] == "player" || target_objekt["targetname"] == "script_vehicle")  objekt_entitys = getentarray(target_objekt["targetname"], "classname");
        else                                                                                            objekt_entitys = getentarray(target_objekt["targetname"], "targetname");
 
        for(i=0;i<objekt_entitys.size;i++) {
             
            // * Wenn ein Script-Vehicle, aber kein Panzer, dann nächstes Objekt * 
            if(target_objekt["targetname"] == "script_vehicle" && !isDefined(objekt_entitys[i].number)) continue;
             
            if(objekt_entitys[i] != self && objekt_entitys[i].number == target_objekt["number"]) {
    
                ziel_objekt = objekt_entitys[i];
                              
                break;
            }
        }
    }
    else {
    
        lenkung_aus = true;
    }



    rauch_zaehler = 0;
    winkelveraenderung_gesamt = (0,0,0);


	treffer = undefined;

    while(!isDefined(treffer)) {


        // * Rauch anzeigen *
        rauch_zaehler++;
        if(rauch_zaehler == 2) {
        
            rauch_zaehler = 0;
            playfx(level.plat_mod_bazooka_smoke, rocket.origin);
        }



        forwardvector   = anglestoforward(rocket.angles);              
        vor_pos         = rocket.origin + (VectorMulti(forwardvector,200));


        // * Flugzeit ermitteln *
        rocket_speed = 700;
        
        if(target_objekt["targetname"] == "fighters")       rocket_speed = 700;
        if(target_objekt["targetname"] == "planes")         rocket_speed = 1100;
        if(target_objekt["targetname"] == "script_vehicle") rocket_speed = 700;
        if(target_objekt["targetname"] == "player")         rocket_speed = 600;
       
        flugzeit = maps\mp\gametypes\_admiral_mod_utils::ZeitSpeedBerechnung(rocket_speed,rocket.origin,vor_pos);

        //iprintln(lenkung_aus);


        // * Rakete bewegen *
        rocket moveto(vor_pos, flugzeit);

        for(i=0;i<flugzeit;i+=0.05) { 


            // * Treffer-Check nur wenn die Rakete vom eigenem Flugzeug weit genug weg ist *

            forwardvector   = anglestoforward(rocket.angles);              
            vor_pos         = rocket.origin + (VectorMulti(forwardvector,200));

            
            trace = bulletTrace(rocket.origin, vor_pos, false, undefined); 
            if((trace["fraction"] != 1 && distance(rocket.origin, self.origin) > 500 && rocket.origin[2] < game["map_dim_vMax"][2] -20) || (target_objekt["targetname"] != "player" && isDefined(ziel_objekt) && distance(rocket.origin,ziel_objekt.origin) < 100) )  {


                // * Einschlagsspunkt *
                endorigin = rocket.origin;

                treffer = true;
                break;                    
            }

            
            // * Wenn Zielobjekt (kein Player) nicht mehr vorhanden, dann Lenkung aus *
            if(target_objekt["targetname"] != "player" && !isDefined(ziel_objekt)) lenkung_aus = true;


            // * Wenn Zielobjekt (Player) nicht mehr vorhanden, dann Lenkung aus *
            if(target_objekt["targetname"] == "player" && !isDefined(ziel_objekt)) lenkung_aus = true;



            // * Raketen-Lenkung erst ab einem bestimmten Abstand und nur wenn Lenkung noch aktiv *
            if(distance(self.origin,rocket.origin) > 700 && lenkung_aus == false ) {

                
                //iprintln(distance(rocket.origin,ziel_objekt.origin));


                // * Winkel-Annäherung der Rakete an das Zielobjekt *

                raketenWinkel   = vectortoangles(anglestoforward(rocket.angles));                     
                zielWinkel      = vectortoangles(vectornormalize((ziel_objekt.origin + (0,0,50) ) - rocket.origin));

				
				xdiff = 0;
				ydiff = 0;
				zdiff = 0;


                if(zielWinkel[0] - raketenWinkel[0] >= 0 && zielWinkel[0] - raketenWinkel[0] <= 180)         xdiff = zielWinkel[0] - raketenWinkel[0];
                else if(zielWinkel[0] - raketenWinkel[0] > 180)                                              xdiff = (360 - zielWinkel[0] + raketenWinkel[0]) * -1;
                else if(zielWinkel[0] - raketenWinkel[0] < 0 && zielWinkel[0] - raketenWinkel[0] >= -180)    xdiff = zielWinkel[0] - raketenWinkel[0];
                else if(zielWinkel[0] - raketenWinkel[0] < -180)                                             xdiff = (zielWinkel[0] - raketenWinkel[0]) + 360;

                if(zielWinkel[1] - raketenWinkel[1] >= 0 && zielWinkel[1] - raketenWinkel[1] <= 180)         ydiff = zielWinkel[1] - raketenWinkel[1];
                else if(zielWinkel[1] - raketenWinkel[1] > 180)                                              ydiff = (360 - zielWinkel[1] + raketenWinkel[1]) * -1;
                else if(zielWinkel[1] - raketenWinkel[1] < 0 && zielWinkel[1] - raketenWinkel[1] >= -180)    ydiff = zielWinkel[1] - raketenWinkel[1];
                else if(zielWinkel[1] - raketenWinkel[1] < -180)                                             ydiff = (zielWinkel[0] - raketenWinkel[0]) + 360;

                if(zielWinkel[2] - raketenWinkel[2] >= 0 && zielWinkel[2] - raketenWinkel[2] <= 180)         zdiff = zielWinkel[2] - raketenWinkel[2];
                else if(zielWinkel[2] - raketenWinkel[2] > 180)                                              zdiff = (360 - zielWinkel[2] + raketenWinkel[2]) * -1;
                else if(zielWinkel[2] - raketenWinkel[2] < 0 && zielWinkel[2] - raketenWinkel[2] >= -180)    zdiff = zielWinkel[2] - raketenWinkel[2];
                else if(zielWinkel[2] - raketenWinkel[2] < -180)                                             zdiff = (zielWinkel[0] - raketenWinkel[0]) + 360;

                diff = (xdiff,ydiff,zdiff);


                winkelveraenderung_gesamt += diff;

                // * Rakete nicht erlauben eine Kurve mit mehr als 90° zu fliegen *
                if(winkelveraenderung_gesamt[0] < -160 || winkelveraenderung_gesamt[0] > 160) lenkung_aus = true;
                if(winkelveraenderung_gesamt[1] < -160 || winkelveraenderung_gesamt[1] > 160) lenkung_aus = true;
                if(winkelveraenderung_gesamt[2] < -160 || winkelveraenderung_gesamt[2] > 160) lenkung_aus = true;



                faktor          = target_objekt["range"] * 0.25;  // (0.1 ungenau, 0.2 genau, 0.3 sehr genau)
                
                // * Bei Planes Treffergenauigkeit etwas erhöhen *
                if(target_objekt["targetname"] == "planes") faktor += 0.1;
                
                
                diff_teil       = (diff[0] * faktor, diff[1] * faktor, diff[2] * faktor);                                   

                rocket.angles   = rocket.angles + diff_teil;



            }


            wait 0.05;
        }

    }


    


    // * Einschlag Effect anzeigen *

    if(level.isWinter == 1) {

        rocketfx = level.plat_mod_mortarfx_snow;
        soundtyp = "snow";
    }
    else {

        e = randomInt(4);

		soundtyp = "default";

        switch(e) {

            case 0:
                rocketfx = level.plat_mod_mortarfx_default;
                soundtyp = "default";
                break;
            case 1:
                rocketfx = level.plat_mod_mortarfx_grass;
                soundtyp = "grass";
                break;
            case 2:
                rocketfx = level.plat_mod_mortarfx_dirt;
                soundtyp = "dirt";
                break;
            case 3:
                rocketfx = level.plat_mod_mortarfx_default;
                soundtyp = "default";
                break;            
        } 
    }

    playfx(rocketfx, endorigin);

    wait .05;


    // * Explosion abspielen *      
    rocket playSound("mortar_explode_" + soundtyp);
   
   
    rocket hide();    


     
    playfx(level.plat_mod_bomben_explosion, endorigin);    
    playfx(level.plat_mod_bomben_explosion2, endorigin);
         
   
   
   
    // * Damage Players *
    //self DamageToPlayers(who,endorigin,800,90,1,"MOD_EXPLOSIVE","binoculars_artillery_mp");
    
    // * Damage Fighters *
    //self DamageToFighters(who,endorigin,200,1000,1,"MOD_EXPLOSIVE","binoculars_artillery_mp");                
 
    // * Damage Planes *
    //self DamageToPlanes(who,endorigin,1000,1000,1,"MOD_EXPLOSIVE","binoculars_artillery_mp");                
 
    // * Damage Tanks *
    //self DamageToTanks(who,endorigin,800,(3000 + randomInt(2000)),1,"MOD_EXPLOSIVE","binoculars_artillery_mp");                

 
    // * Earthquake *       
    earthquake(0.8, 1.5, endorigin, 750);       
    
    
    
    wait 3;
    
    // * Rocket loeschen *
    rocket delete();
   
    return;
}





TargetsCheck(targetname) {

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


    // * Mögliche Targetnamen:  "fighters", "player", "planes", "script_vehicle"

        
        
    self_forwardvector   = anglestoforward(self.angles);   


    // * Objektdaten leeren *
    objekt_daten["targetname"]  = "";
    objekt_daten["number"]      = 0;
    objekt_daten["range"]       = 0;

    


    // * zum ermitteln des nahesten Objektes (wenn mehrere im Visier) *
    naheste_objektentfernung = 40000;
    
    // * Kein Objekt anvisiert *
    nahestes_objekt = 0;
    
    // * Objekt - Range *
    naheste_objekt_range = 0;



    if(targetname == "player" || targetname == "script_vehicle")    objekt_entitys = getentarray(targetname, "classname");
    else                                                            objekt_entitys = getentarray(targetname, "targetname");


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

        
        objekt = objekt_entitys[i];


        if (targetname == "script_vehicle") {
               
            if( (objekt.vehicletype != "t34_mp") && (objekt.vehicletype != "shermantank_mp") && (objekt.vehicletype != "elefant_mp") && (objekt.vehicletype != "su152_mp") && (objekt.vehicletype != "panzeriv_mp") && (objekt.vehicletype != "willyjeep_mp") && (objekt.vehicletype != "horch_mp") && (objekt.vehicletype != "gaz67b_mp") ) continue;
        
            objekt.number = objekt.tank_num;
        }
       

        // * Nicht sich selbst anvisieren *
        if(self == objekt) continue;
        
        // * Nicht das eigene Team anvisieren (vehicles) *
        if(isDefined(self.team) && isDefined(objekt.team) && objekt.team == self.team) continue;
        
        
        
        // * keine Piloten und Copiloten anvisieren *
        if(targetname == "player" && isPlayer(objekt) && isAlive(objekt) && objekt.sessionstate == "playing" && (objekt.do_status == "fighter_pilot" || objekt.do_status == "fighter_copilot") ) continue;
        
        
        
        // * Winkel zum Ziel (0 = Ziel an der Seite, 1 = direkt vor dem Ziel) *
        blickwinkel             = vectortoangles(vectornormalize((self.origin) - objekt.origin));        
        objekt_forwardvector    = anglestoforward(blickwinkel);        
        target_range            = vectordot(self_forwardvector,objekt_forwardvector) * -1;
              
       
        // * Nur wenn Ziel direkt vor einem *
        if(target_range > 0.95) {
                    
            entfernung = distance(self.origin,objekt.origin);
            
            if(entfernung < naheste_objektentfernung) {
                
                // * aus 0.2 auf 1 verbeitern *
                target_range = (target_range - 0.95) * 20;
                
                // * nahestes Objekt speichern *
                naheste_objektentfernung    = entfernung;           
                nahestes_objekt             = objekt.number;
                naheste_objekt_range        = target_range;
                
        
                //iprintln("Target in Range: " + target_range);
            }
        }
    }
    
    
    // * Nur wenn Objekt gefunden *
    if(naheste_objektentfernung != 40000) {
    
        objekt_daten["targetname"]    = targetname;
        objekt_daten["number"]        = nahestes_objekt;
        objekt_daten["range"]         = naheste_objekt_range;        
    }
    
    return objekt_daten;
}





objekt_verfolgen(verfolger,zielobjekt,geschwindigkeit,z_offset) {

    dist = distance(verfolger.origin,zielobjekt.origin);
    
    if(dist) {
                            
        if(dist > 200) {
            
            fraction    = (dist - 20) / dist;
                                                
            delta       = (zielobjekt.origin + (0,0,z_offset)) - verfolger.origin;
            delta2      = VectorMulti(delta,fraction);
            neworigin   = verfolger.origin + delta2;
                        
            verfolger moveto(neworigin,geschwindigkeit);     
        }
    }   
    
    return;
}



blickwinkel_auf_zielvector(quellposition,zielposition) {


    blickwinkel = vectortoangles(vectornormalize((zielposition) - quellposition)) + (90,0,0);
     
    return blickwinkel;
}







GetForwardPos(weite, breite, hoehe) {

    forwardvector   = anglestoforward(self.angles);                
    ziel_pos        = self.origin + (VectorMulti(forwardvector,weite)) + (0,breite,hoehe);
        
    return ziel_pos;
}


VectorMulti(vec, dif) {

    vec = (vec[0] * dif, vec[1] * dif, vec[2] * dif);
    
    return vec;   
}




GetFighterMapPositionArray() {


    pos_array[1][0] = -2000;
    pos_array[1][1] = -2000;
    pos_array[1][2] = -2000;      
    pos_array[1][3] = 0;
    pos_array[1][4] = 0;
    pos_array[1][5] = 0;        




    if(game["am_mapname"] == "mp_island_assault") {
        
        pos_array[1][0] = -4586;
        pos_array[1][1] = -1104;
        pos_array[1][2] = 185;        
        pos_array[1][3] = -8;
        pos_array[1][4] = 123;
        pos_array[1][5] = 0;        
                         
    }
    
    if(game["am_mapname"] == "mp_brecourt") {
        
        pos_array[1][0] = 1718;
        pos_array[1][1] = -3280;
        pos_array[1][2] = 109;        
        pos_array[1][3] = 0;
        pos_array[1][4] = 0;
        pos_array[1][5] = 0;        
        
        pos_array[2][0] = -356;
        pos_array[2][1] = -1309;
        pos_array[2][2] = 77;        
        pos_array[2][3] = 0;
        pos_array[2][4] = -88;
        pos_array[2][5] = 0;        

        pos_array[3][0] = 6;
        pos_array[3][1] = -3319;
        pos_array[3][2] = 107;        
        pos_array[3][3] = 0;
        pos_array[3][4] = -175.25;
        pos_array[3][5] = 0;        
        
        pos_array[4][0] = 2280;
        pos_array[4][1] = -1322;
        pos_array[4][2] = 118;        
        pos_array[4][3] = -3;
        pos_array[4][4] = -123;
        pos_array[4][5] = 0;        
        
     }
  
    
    return pos_array;

}



