]> git.mxchange.org Git - flightgear.git/blob - src/AIModel/AIFlightPlanCreate.cxx
Ground network distance tracking code. AIAircraft taxiing at airports
[flightgear.git] / src / AIModel / AIFlightPlanCreate.cxx
1 /******************************************************************************
2  * AIFlightPlanCreate.cxx
3  * Written by Durk Talsma, started May, 2004.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License as
7  * published by the Free Software Foundation; either version 2 of the
8  * License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18  *
19  **************************************************************************/
20 #include "AIFlightPlan.hxx"
21 #include <simgear/math/sg_geodesy.hxx>
22 #include <Airports/runways.hxx>
23
24 #include <Environment/environment_mgr.hxx>
25 #include <Environment/environment.hxx>
26
27
28 /* FGAIFlightPlan::create()
29  * dynamically create a flight plan for AI traffic, based on data provided by the
30  * Traffic Manager, when reading a filed flightplan failes. (DT, 2004/07/10) 
31  *
32  * This is the top-level function, and the only one that is publicly available.
33  *
34  */ 
35
36
37 // Check lat/lon values during initialization;
38 void FGAIFlightPlan::create(FGAirport *dep, FGAirport *arr, int legNr, 
39                             double alt, double speed, double latitude, 
40                             double longitude, bool firstFlight,double radius, 
41                             const string& fltType, const string& aircraftType, 
42                             const string& airline)
43
44   int currWpt = wpt_iterator - waypoints.begin();
45   switch(legNr)
46     {
47       case 1:
48       createPushBack(firstFlight,dep, latitude, longitude, 
49                      radius, fltType, aircraftType, airline);
50       break;
51     case 2: 
52       createTaxi(firstFlight, 1, dep, latitude, longitude, 
53                  radius, fltType, aircraftType, airline);
54       break;
55     case 3: 
56       createTakeOff(firstFlight, dep, speed);
57       break;
58     case 4: 
59       createClimb(firstFlight, dep, speed, alt);
60       break;
61     case 5: 
62       createCruise(firstFlight, dep,arr, latitude, longitude, speed, alt);
63       break;
64     case 6: 
65       createDecent(arr);
66       break;
67     case 7: 
68       createLanding(arr);
69       break;
70     case 8: 
71       createTaxi(false, 2, arr, latitude, longitude, radius, 
72                  fltType, aircraftType, airline);
73       break;
74       case 9: 
75         createParking(arr, radius);
76       break;
77     default:
78       //exit(1);
79       SG_LOG(SG_INPUT, SG_ALERT, "AIFlightPlan::create() attempting to create unknown leg"
80              " this is probably an internal program error");
81     }
82   wpt_iterator = waypoints.begin()+currWpt;
83   leg++;
84 }
85
86 /*******************************************************************
87  * createPushBack
88  * initialize the Aircraft at the parking location
89  ******************************************************************/
90 void FGAIFlightPlan::createPushBack(bool firstFlight, FGAirport *dep, 
91                                     double latitude,
92                                     double longitude,
93                                     double radius,
94                                     const string& fltType,
95                                     const string& aircraftType,
96                                     const string& airline)
97 {
98   double heading;
99   double lat;
100   double lon;
101   double lat2;
102   double lon2;
103   double az2;
104   
105   //int currWpt = wpt_iterator - waypoints.begin();
106   // Erase all existing waypoints.
107   //resetWaypoints();
108   
109   // We only need to get a valid parking if this is the first leg. 
110   // Otherwise use the current aircraft position.
111   if (firstFlight)
112     {
113       if (!(dep->getDynamics()->getAvailableParking(&lat, &lon, 
114                                                     &heading, &gateId, 
115                                                     radius, fltType, 
116                                                     aircraftType, airline)))
117         {
118           SG_LOG(SG_INPUT, SG_WARN, "Could not find parking for a " << 
119                  aircraftType <<
120                  " of flight type " << fltType << 
121                  " of airline     " << airline <<
122                  " at airport     " << dep->getId());
123         }
124     }
125   else
126     {
127       dep->getDynamics()->getParking(gateId, &lat, &lon, &heading);
128     }
129   heading += 180.0;
130   if (heading > 360)
131     heading -= 360;
132   waypoint *wpt = new waypoint;
133   wpt->name      = "park";
134   wpt->latitude  = lat;
135   wpt->longitude = lon;
136   wpt->altitude  = dep->getElevation();
137   wpt->speed     = -10; 
138   wpt->crossat   = -10000;
139   wpt->gear_down = true;
140   wpt->flaps_down= true;
141   wpt->finished  = false;
142   wpt->on_ground = true;
143
144   waypoints.push_back(wpt); 
145   
146   geo_direct_wgs_84 ( 0, lat, lon, heading, 
147                       10, 
148                       &lat2, &lon2, &az2 );
149   wpt = new waypoint;
150   wpt->name      = "park2";
151   wpt->latitude  = lat2;
152   wpt->longitude = lon2;
153   wpt->altitude  = dep->getElevation();
154   wpt->speed     = -10; 
155   wpt->crossat   = -10000;
156   wpt->gear_down = true;
157   wpt->flaps_down= true;
158   wpt->finished  = false;
159   wpt->on_ground = true;
160   wpt->routeIndex = 0;
161   waypoints.push_back(wpt); 
162   geo_direct_wgs_84 ( 0, lat, lon, heading, 
163                       2.2*radius,           
164                       &lat2, &lon2, &az2 );
165   wpt = new waypoint;
166   wpt->name      = "taxiStart";
167   wpt->latitude  = lat2;
168   wpt->longitude = lon2;
169   wpt->altitude  = dep->getElevation();
170   wpt->speed     = 10; 
171   wpt->crossat   = -10000;
172   wpt->gear_down = true;
173   wpt->flaps_down= true;
174   wpt->finished  = false;
175   wpt->on_ground = true;
176   wpt->routeIndex = 0;
177   waypoints.push_back(wpt);  
178 }
179
180 /*******************************************************************
181  * createCreate Taxi. 
182  * initialize the Aircraft at the parking location
183  ******************************************************************/
184 void FGAIFlightPlan::createTaxi(bool firstFlight, int direction, 
185                                 FGAirport *apt, double latitude, double longitude, 
186                                 double radius, const string& fltType, 
187                                 const string& acType, const string& airline)
188 {
189   double heading;
190   double lat, lon;
191   double lat2, lon2, az2;
192   waypoint *wpt;
193
194   int nrWaypointsToSkip;
195
196    if (direction == 1)
197     {
198       // If this function is called during initialization,
199       // make sure we obtain a valid gate ID first
200       // and place the model at the location of the gate.
201       if (firstFlight)
202         {
203           if (!(apt->getDynamics()->getAvailableParking(&lat, &lon, 
204                                                         &heading, &gateId, 
205                                                         radius, fltType, 
206                                                         acType, airline)))
207             {
208               SG_LOG(SG_INPUT, SG_WARN, "Could not find parking for a " << 
209                      acType <<
210                      " of flight type " << fltType <<
211                      " of airline     " << airline <<
212                      " at airport     " << apt->getId());
213             }
214           //waypoint *wpt = new waypoint;
215           //wpt->name      = "park";
216           //wpt->latitude  = lat;
217           //wpt->longitude = lon;
218           //wpt->altitude  = apt->getElevation();
219           //wpt->speed     = -10; 
220           //wpt->crossat   = -10000;
221           //wpt->gear_down = true;
222           //wpt->flaps_down= true;
223           //wpt->finished  = false;
224           //wpt->on_ground = true;
225           //waypoints.push_back(wpt);  
226         }
227       // "NOTE: this is currently fixed to "com" for commercial traffic
228       // Should be changed to be used dynamically to allow "gen" and "mil"
229       // as well
230       apt->getDynamics()->getActiveRunway("com", 1, activeRunway);
231       if (!(globals->get_runways()->search(apt->getId(), 
232                                             activeRunway, 
233                                             &rwy)))
234         {
235            SG_LOG(SG_INPUT, SG_ALERT, "Failed to find runway " << 
236                   activeRunway << 
237                   " at airport     " << apt->getId());
238            exit(1);
239         } 
240
241       // Determine the beginning of he runway
242       heading = rwy._heading;
243       double azimuth = heading + 180.0;
244       while ( azimuth >= 360.0 ) { azimuth -= 360.0; }
245       geo_direct_wgs_84 ( 0, rwy._lat, rwy._lon, azimuth, 
246                           rwy._length * SG_FEET_TO_METER * 0.5 - 5.0,
247                           &lat2, &lon2, &az2 );
248
249       if (apt->getDynamics()->getGroundNetwork()->exists())
250         {
251           intVec ids;
252           int runwayId = apt->getDynamics()->getGroundNetwork()->findNearestNode(lat2, 
253                                                                                  lon2);
254
255           
256           // A negative gateId indicates an overflow parking, use a
257           // fallback mechanism for this. 
258           // Starting from gate 0 in this case is a bit of a hack
259           // which requires a more proper solution later on.
260           //FGTaxiRoute route;
261           if (taxiRoute)
262             delete taxiRoute;
263           taxiRoute = new FGTaxiRoute;
264           if (gateId >= 0)
265             *taxiRoute = apt->getDynamics()->getGroundNetwork()->findShortestRoute(gateId, 
266                                                                               runwayId);
267           else
268             *taxiRoute = apt->getDynamics()->getGroundNetwork()->findShortestRoute(0, runwayId);
269           intVecIterator i;
270          
271           if (taxiRoute->empty()) {
272             //Add the runway startpoint;
273             wpt = new waypoint;
274             wpt->name      = "Airport Center";
275             wpt->latitude  = latitude;
276             wpt->longitude = longitude;
277             wpt->altitude  = apt->getElevation();
278             wpt->speed     = 15; 
279             wpt->crossat   = -10000;
280             wpt->gear_down = true;
281             wpt->flaps_down= true;
282             wpt->finished  = false;
283             wpt->on_ground = true;
284             wpt->routeIndex = 0;
285             waypoints.push_back(wpt);
286             
287             //Add the runway startpoint;
288             wpt = new waypoint;
289             wpt->name      = "Runway Takeoff";
290             wpt->latitude  = lat2;
291             wpt->longitude = lon2;
292             wpt->altitude  = apt->getElevation();
293             wpt->speed     = 15; 
294             wpt->crossat   = -10000;
295             wpt->gear_down = true;
296             wpt->flaps_down= true;
297             wpt->finished  = false;
298             wpt->on_ground = true;
299             wpt->routeIndex = 0;
300             waypoints.push_back(wpt);   
301           } else {
302             int node;
303             taxiRoute->first();
304             bool isPushBackPoint = false;
305             if (firstFlight) {
306               // If this is called during initialization, randomly
307               // skip a number of waypoints to get a more realistic
308               // taxi situation.
309               isPushBackPoint = true;
310               int nrWaypoints = taxiRoute->size();
311               nrWaypointsToSkip = rand() % nrWaypoints;
312               // but make sure we always keep two active waypoints
313               // to prevent a segmentation fault
314               for (int i = 0; i < nrWaypointsToSkip-2; i++) {
315                 isPushBackPoint = false;
316                 taxiRoute->next(&node);
317               }
318             } else {
319               //chop off the first two waypoints, because
320               // those have already been created
321               // by create pushback
322               int size = taxiRoute->size();
323               if (size > 2) {
324                 taxiRoute->next(&node);
325                 taxiRoute->next(&node);
326               }
327             }
328             int route;
329             while(taxiRoute->next(&node, &route))
330               {
331                 //FGTaxiNode *tn = apt->getDynamics()->getGroundNetwork()->findSegment(node)->getEnd();
332                 char buffer[10];
333                 snprintf (buffer, 10, "%d", node);
334                 FGTaxiNode *tn = apt->getDynamics()->getGroundNetwork()->findNode(node);
335                 //ids.pop_back();  
336                 wpt = new waypoint;
337                 wpt->name      = string(buffer); // fixme: should be the name of the taxiway
338                 wpt->latitude  = tn->getLatitude();
339                 wpt->longitude = tn->getLongitude();
340                 // Elevation is currently disregarded when on_ground is true
341                 // because the AIModel obtains a periodic ground elevation estimate.
342                 wpt->altitude  = apt->getElevation();
343                 if (isPushBackPoint) {
344                   wpt->speed = -10;
345                   isPushBackPoint = false;
346                 }
347                 else {
348                   wpt->speed     = 15; 
349                 }
350                 wpt->crossat   = -10000;
351                 wpt->gear_down = true;
352                 wpt->flaps_down= true;
353                 wpt->finished  = false;
354                 wpt->on_ground = true;
355                 wpt->routeIndex = route;
356                 waypoints.push_back(wpt);
357               }
358             //cerr << endl;
359             // finally, rewind the taxiRoute object to the point where we started
360             // generating the Flightplan, for AI use.
361             // This is a bit tricky, because the 
362             taxiRoute->first();
363             if (firstFlight) { 
364               for (int i = 0; i < nrWaypointsToSkip-1; i++) {
365                 taxiRoute->next(&node);
366               }
367             } else {
368               int size = taxiRoute->size();
369               if (size > 2) {
370                 //taxiRoute->next(&node);
371                 //taxiRoute->next(&node);       
372                 //taxiRoute->next(&node);
373               }
374             }
375           } // taxiRoute not empty
376         }
377       else 
378         {
379           // This is the fallback mechanism, in case no ground network is available
380           //Add the runway startpoint;
381           wpt = new waypoint;
382           wpt->name      = "Airport Center";
383           wpt->latitude  = apt->getLatitude();
384           wpt->longitude = apt->getLongitude();
385           wpt->altitude  = apt->getElevation();
386           wpt->speed     = 15; 
387           wpt->crossat   = -10000;
388           wpt->gear_down = true;
389           wpt->flaps_down= true;
390           wpt->finished  = false;
391           wpt->on_ground = true;
392           wpt->routeIndex = 0;
393           waypoints.push_back(wpt);
394           
395           //Add the runway startpoint;
396           wpt = new waypoint;
397           wpt->name      = "Runway Takeoff";
398           wpt->latitude  = lat2;
399           wpt->longitude = lon2;
400           wpt->altitude  = apt->getElevation();
401           wpt->speed     = 15; 
402           wpt->crossat   = -10000;
403           wpt->gear_down = true;
404           wpt->flaps_down= true;
405           wpt->finished  = false;
406           wpt->on_ground = true;
407           wpt->routeIndex = 0;
408           waypoints.push_back(wpt);
409         }
410     }
411   else  // Landing taxi
412     {
413       apt->getDynamics()->getAvailableParking(&lat, &lon, &heading, 
414                                               &gateId, radius, fltType, 
415                                               acType, airline);
416      
417       double lat3 = (*(waypoints.end()-1))->latitude;
418       double lon3 = (*(waypoints.end()-1))->longitude;
419       //cerr << (*(waypoints.end()-1))->name << endl;
420       
421       // Find a route from runway end to parking/gate.
422       if (apt->getDynamics()->getGroundNetwork()->exists())
423         {
424           intVec ids;
425           int runwayId = apt->getDynamics()->getGroundNetwork()->findNearestNode(lat3, 
426                                                                                  lon3);
427           // A negative gateId indicates an overflow parking, use a
428           // fallback mechanism for this. 
429           // Starting from gate 0 is a bit of a hack...
430           //FGTaxiRoute route;
431           if (taxiRoute)
432             delete taxiRoute;
433           taxiRoute = new FGTaxiRoute;
434           if (gateId >= 0)
435             *taxiRoute = apt->getDynamics()->getGroundNetwork()->findShortestRoute(runwayId, 
436                                                                               gateId);
437           else
438             *taxiRoute = apt->getDynamics()->getGroundNetwork()->findShortestRoute(runwayId, 0);
439           intVecIterator i;
440          
441           // No route found: go from gate directly to runway
442           if (taxiRoute->empty()) {
443             //Add the runway startpoint;
444             wpt = new waypoint;
445             wpt->name      = "Airport Center";
446             wpt->latitude  = latitude;
447             wpt->longitude = longitude;
448             wpt->altitude  = apt->getElevation();
449             wpt->speed     = 15; 
450             wpt->crossat   = -10000;
451             wpt->gear_down = true;
452             wpt->flaps_down= true;
453             wpt->finished  = false;
454             wpt->on_ground = true;
455             wpt->routeIndex = 0;
456             waypoints.push_back(wpt);
457             
458             //Add the runway startpoint;
459             wpt = new waypoint;
460             wpt->name      = "Runway Takeoff";
461             wpt->latitude  = lat3;
462             wpt->longitude = lon3;
463             wpt->altitude  = apt->getElevation();
464             wpt->speed     = 15; 
465             wpt->crossat   = -10000;
466             wpt->gear_down = true;
467             wpt->flaps_down= true;
468             wpt->finished  = false;
469             wpt->on_ground = true;
470             wpt->routeIndex = 0;
471             waypoints.push_back(wpt);   
472           } else {
473             int node;
474             taxiRoute->first();
475             int size = taxiRoute->size();
476             // Omit the last two waypoints, as 
477             // those are created by createParking()
478             int route;
479             for (int i = 0; i < size-2; i++)
480               {
481                 taxiRoute->next(&node, &route);
482                 char buffer[10];
483                 snprintf (buffer, 10, "%d", node);
484                 //FGTaxiNode *tn = apt->getDynamics()->getGroundNetwork()->findNode(node);
485                 FGTaxiNode *tn = apt->getDynamics()->getGroundNetwork()->findNode(node);
486                 wpt = new waypoint;
487                 //wpt->name      = "taxiway"; // fixme: should be the name of the taxiway
488                 wpt->name      = string(buffer);// fixme: should be the name of the taxiway
489                 wpt->latitude  = tn->getLatitude();
490                 wpt->longitude = tn->getLongitude();
491                 wpt->altitude  = apt->getElevation();
492                 wpt->speed     = 15; 
493                 wpt->crossat   = -10000;
494                 wpt->gear_down = true;
495                 wpt->flaps_down= true;
496                 wpt->finished  = false;
497                 wpt->on_ground = true;
498                 wpt->routeIndex = route;
499                 waypoints.push_back(wpt);
500               }
501             //taxiRoute->first();
502             //taxiRoute->next(&node);
503           }
504         }
505       else
506         {
507           // Use a fallback mechanism in case no ground network is available
508           // obtain the location of the gate entrance point 
509           heading += 180.0;
510           if (heading > 360)
511             heading -= 360;
512           geo_direct_wgs_84 ( 0, lat, lon, heading, 
513                               100,
514                               &lat2, &lon2, &az2 );
515           wpt = new waypoint;
516           wpt->name      = "Airport Center";
517           wpt->latitude  = apt->getLatitude();
518           wpt->longitude = apt->getLongitude();
519           wpt->altitude  = apt->getElevation();
520           wpt->speed     = 15; 
521           wpt->crossat   = -10000;
522           wpt->gear_down = true;
523           wpt->flaps_down= true;
524           wpt->finished  = false;
525           wpt->on_ground = true;
526           wpt->routeIndex = 0;
527           waypoints.push_back(wpt);
528          
529           wpt = new waypoint;
530           wpt->name      = "Begin Parking"; //apt->getId(); //wpt_node->getStringValue("name", "END");
531           wpt->latitude  = lat2;
532           wpt->longitude = lon2;
533           wpt->altitude  = apt->getElevation();
534           wpt->speed     = 15; 
535           wpt->crossat   = -10000;
536           wpt->gear_down = true;
537           wpt->flaps_down= true;
538           wpt->finished  = false;
539           wpt->on_ground = true;
540           wpt->routeIndex = 0;
541           waypoints.push_back(wpt); 
542
543           //waypoint* wpt;
544           //double lat;
545           //double lon;
546           //double heading;
547           apt->getDynamics()->getParking(gateId, &lat, &lon, &heading);
548           heading += 180.0;
549           if (heading > 360)
550             heading -= 360; 
551           
552           wpt = new waypoint;
553           wpt->name      = "END"; //wpt_node->getStringValue("name", "END");
554           wpt->latitude  = lat;
555           wpt->longitude = lon;
556           wpt->altitude  = 19;
557           wpt->speed     = 15; 
558           wpt->crossat   = -10000;
559           wpt->gear_down = true;
560           wpt->flaps_down= true;
561           wpt->finished  = false;
562           wpt->on_ground = true;
563           wpt->routeIndex = 0;
564           waypoints.push_back(wpt);
565         }
566       
567     }
568 }
569
570 /*******************************************************************
571  * CreateTakeOff 
572  * initialize the Aircraft at the parking location
573  ******************************************************************/
574 void FGAIFlightPlan::createTakeOff(bool firstFlight, FGAirport *apt, double speed)
575 {
576   double heading;
577   double lat, lon, az;
578   double lat2, lon2, az2;
579   waypoint *wpt;
580   
581   // Get the current active runway, based on code from David Luff
582   // This should actually be unified and extended to include 
583   // Preferential runway use schema's 
584   if (firstFlight)
585     {
586       //string name;
587        // "NOTE: this is currently fixed to "com" for commercial traffic
588       // Should be changed to be used dynamically to allow "gen" and "mil"
589       // as well
590       apt->getDynamics()->getActiveRunway("com", 1, activeRunway);
591         if (!(globals->get_runways()->search(apt->getId(), 
592                                               activeRunway, 
593                                               &rwy)))
594           {
595             SG_LOG(SG_INPUT, SG_ALERT, "Failed to find runway " << 
596                    activeRunway << 
597                    " at airport     " << apt->getId());
598             exit(1);
599           }
600     }
601   heading = rwy._heading;
602   double azimuth = heading + 180.0;
603   while ( azimuth >= 360.0 ) { azimuth -= 360.0; }
604   geo_direct_wgs_84 ( 0, rwy._lat, rwy._lon, azimuth, 
605                       rwy._length * SG_FEET_TO_METER * 0.5 - 105.0,
606                       &lat2, &lon2, &az2 );
607   wpt = new waypoint; 
608   wpt->name      = "accel"; 
609   wpt->latitude  = lat2; 
610   wpt->longitude = lon2; 
611   wpt->altitude  = apt->getElevation();
612   wpt->speed     = speed;  
613   wpt->crossat   = -10000;
614   wpt->gear_down = true;
615   wpt->flaps_down= true;
616   wpt->finished  = false;
617   wpt->on_ground = true;
618   wpt->routeIndex = 0;
619   waypoints.push_back(wpt); 
620   
621   lat = lat2;
622   lon = lon2;
623   az  = az2;
624   
625   //Next: the Start of Climb
626   geo_direct_wgs_84 ( 0, lat, lon, heading, 
627   2560 * SG_FEET_TO_METER,
628   &lat2, &lon2, &az2 );
629   
630   wpt = new waypoint;
631   wpt->name      = "SOC";
632   wpt->latitude  = lat2;
633   wpt->longitude = lon2;
634   wpt->altitude  = apt->getElevation()+3000;
635   wpt->speed     = speed; 
636   wpt->crossat   = -10000;
637   wpt->gear_down = true;
638   wpt->flaps_down= true;
639   wpt->finished  = false;
640   wpt->on_ground = false;
641   wpt->routeIndex = 0;
642   waypoints.push_back(wpt);
643 }
644  
645 /*******************************************************************
646  * CreateClimb
647  * initialize the Aircraft at the parking location
648  ******************************************************************/
649 void FGAIFlightPlan::createClimb(bool firstFlight, FGAirport *apt, double speed, double alt)
650 {
651   double heading;
652   //FGRunway rwy;
653   double lat2, lon2, az2;
654   //int direction;
655   waypoint *wpt;
656
657  
658   if (firstFlight)
659     {
660       //string name;
661       // "NOTE: this is currently fixed to "com" for commercial traffic
662       // Should be changed to be used dynamically to allow "gen" and "mil"
663       // as well
664       apt->getDynamics()->getActiveRunway("com", 1, activeRunway);
665         if (!(globals->get_runways()->search(apt->getId(), 
666                                               activeRunway, 
667                                               &rwy)))
668           {
669             SG_LOG(SG_INPUT, SG_ALERT, "Failed to find runway " << 
670                    activeRunway << 
671                    " at airport     " << apt->getId());
672             exit(1);
673           }
674     }
675   
676   
677   heading = rwy._heading;
678   double azimuth = heading + 180.0;
679   while ( azimuth >= 360.0 ) { azimuth -= 360.0; }
680   //cerr << "Creating climb at : " << rwy._id << " " << rwy._rwy_no << endl;
681   geo_direct_wgs_84 ( 0, rwy._lat, rwy._lon, heading, 
682                       10*SG_NM_TO_METER,
683                       &lat2, &lon2, &az2 );
684   wpt = new waypoint;
685   wpt->name      = "10000ft climb";
686   wpt->latitude  = lat2;
687   wpt->longitude = lon2;
688   wpt->altitude  = 10000;
689   wpt->speed     = speed; 
690   wpt->crossat   = -10000;
691   wpt->gear_down = true;
692   wpt->flaps_down= true;
693   wpt->finished  = false;
694   wpt->on_ground = false;
695   wpt->routeIndex = 0;
696   waypoints.push_back(wpt); 
697   
698
699   geo_direct_wgs_84 ( 0, rwy._lat, rwy._lon, heading, 
700                       20*SG_NM_TO_METER,
701                       &lat2, &lon2, &az2 );
702   wpt = new waypoint;
703   wpt->name      = "18000ft climb";
704   wpt->latitude  = lat2;
705   wpt->longitude = lon2;
706   wpt->altitude  = 18000;
707   wpt->speed     = speed; 
708   wpt->crossat   = -10000;
709   wpt->gear_down = true;
710   wpt->flaps_down= true;
711   wpt->finished  = false;
712   wpt->on_ground = false;
713   wpt->routeIndex = 0;
714   waypoints.push_back(wpt); 
715 }
716
717
718 // /*******************************************************************
719 //  * CreateCruise
720 //  * initialize the Aircraft at the parking location
721 //  ******************************************************************/
722 // void FGAIFlightPlan::createCruise(bool firstFlight, FGAirport *dep, 
723 //                                FGAirport *arr, double latitude, 
724 //                                double longitude, double speed, 
725 //                                double alt)
726 // {
727 //   double wind_speed;
728 //   double wind_heading;
729 //   double heading;
730 //   double lat, lon, az;
731 //   double lat2, lon2, az2;
732 //   double azimuth;
733 //   waypoint *wpt;
734
735 //   wpt = new waypoint;
736 //   wpt->name      = "Cruise"; //wpt_node->getStringValue("name", "END");
737 //   wpt->latitude  = latitude;
738 //   wpt->longitude = longitude;
739 //   wpt->altitude  = alt;
740 //   wpt->speed     = speed; 
741 //   wpt->crossat   = -10000;
742 //   wpt->gear_down = false;
743 //   wpt->flaps_down= false;
744 //   wpt->finished  = false;
745 //   wpt->on_ground = false;
746 //   waypoints.push_back(wpt); 
747   
748  
749 //   // should be changed dynamically to allow "gen" and "mil"
750 //   arr->getDynamics()->getActiveRunway("com", 2, activeRunway);
751 //   if (!(globals->get_runways()->search(arr->getId(), 
752 //                                     activeRunway, 
753 //                                     &rwy)))
754 //     {
755 //       SG_LOG(SG_INPUT, SG_ALERT, "Failed to find runway " << 
756 //           activeRunway << 
757 //           " at airport     " << arr->getId());
758 //       exit(1);
759 //     }
760 //   heading = rwy._heading;
761 //   azimuth = heading + 180.0;
762 //   while ( azimuth >= 360.0 ) { azimuth -= 360.0; }
763   
764   
765 //   geo_direct_wgs_84 ( 0, rwy._lat, rwy._lon, azimuth, 
766 //                    110000,
767 //                    &lat2, &lon2, &az2 );
768 //   wpt = new waypoint;
769 //   wpt->name      = "BOD";
770 //   wpt->latitude  = lat2;
771 //   wpt->longitude = lon2;
772 //   wpt->altitude  = alt;
773 //   wpt->speed     = speed; 
774 //   wpt->crossat   = alt;
775 //   wpt->gear_down = false;
776 //   wpt->flaps_down= false;
777 //   wpt->finished  = false;
778 //   wpt->on_ground = false;
779 //   waypoints.push_back(wpt); 
780 // }
781
782 /*******************************************************************
783  * CreateDecent
784  * initialize the Aircraft at the parking location
785  ******************************************************************/
786 void FGAIFlightPlan::createDecent(FGAirport *apt)
787 {
788
789   // Ten thousand ft. Slowing down to 240 kts
790   double heading;
791   //FGRunway rwy;
792   double lat2, lon2, az2;
793   double azimuth;
794   //int direction;
795   waypoint *wpt;
796
797   //Beginning of Decent
798   //string name;
799   // allow "mil" and "gen" as well
800   apt->getDynamics()->getActiveRunway("com", 2, activeRunway);
801   if (!(globals->get_runways()->search(apt->getId(), 
802                                        activeRunway, 
803                                        &rwy)))
804     {
805       SG_LOG(SG_INPUT, SG_ALERT, "Failed to find runway " << 
806              activeRunway << 
807              " at airport     " << apt->getId());
808       exit(1);
809     }
810   
811   heading = rwy._heading;
812   azimuth = heading + 180.0;
813   while ( azimuth >= 360.0 ) { azimuth -= 360.0; }
814   geo_direct_wgs_84 ( 0, rwy._lat, rwy._lon, azimuth, 
815                       100000,
816                       &lat2, &lon2, &az2 );
817   
818   wpt = new waypoint;
819   wpt->name      = "Dec 10000ft"; //wpt_node->getStringValue("name", "END");
820   wpt->latitude  = lat2;
821   wpt->longitude = lon2;
822   wpt->altitude  = apt->getElevation();
823   wpt->speed     = 240; 
824   wpt->crossat   = 10000;
825   wpt->gear_down = false;
826   wpt->flaps_down= false;
827   wpt->finished  = false;
828   wpt->on_ground = false;
829   wpt->routeIndex = 0;
830   waypoints.push_back(wpt);  
831   
832   // Three thousand ft. Slowing down to 160 kts
833   geo_direct_wgs_84 ( 0, rwy._lat, rwy._lon, azimuth, 
834                       8*SG_NM_TO_METER,
835                       &lat2, &lon2, &az2 );
836   wpt = new waypoint;
837   wpt->name      = "DEC 3000ft"; //wpt_node->getStringValue("name", "END");
838   wpt->latitude  = lat2;
839   wpt->longitude = lon2;
840   wpt->altitude  = apt->getElevation();
841   wpt->speed     = 160; 
842   wpt->crossat   = 3000;
843   wpt->gear_down = true;
844   wpt->flaps_down= true;
845   wpt->finished  = false;
846   wpt->on_ground = false;
847   wpt->routeIndex = 0;
848   waypoints.push_back(wpt);
849 }
850 /*******************************************************************
851  * CreateLanding
852  * initialize the Aircraft at the parking location
853  ******************************************************************/
854 void FGAIFlightPlan::createLanding(FGAirport *apt)
855 {
856   // Ten thousand ft. Slowing down to 150 kts
857   double heading;
858   //FGRunway rwy;
859   double lat2, lon2, az2;
860   double azimuth;
861   //int direction;
862   waypoint *wpt;
863
864   
865   heading = rwy._heading;
866   azimuth = heading + 180.0;
867   while ( azimuth >= 360.0 ) { azimuth -= 360.0; }
868
869   //Runway Threshold
870  geo_direct_wgs_84 ( 0, rwy._lat, rwy._lon, azimuth, 
871                      rwy._length*0.45 * SG_FEET_TO_METER,
872                      &lat2, &lon2, &az2 );
873   wpt = new waypoint;
874   wpt->name      = "Threshold"; //wpt_node->getStringValue("name", "END");
875   wpt->latitude  = lat2;
876   wpt->longitude = lon2;
877   wpt->altitude  = apt->getElevation();
878   wpt->speed     = 150; 
879   wpt->crossat   = apt->getElevation();
880   wpt->gear_down = true;
881   wpt->flaps_down= true;
882   wpt->finished  = false;
883   wpt->on_ground = true;
884   wpt->routeIndex = 0;
885   waypoints.push_back(wpt); 
886
887  //Full stop at the runway centerpoint
888  geo_direct_wgs_84 ( 0, rwy._lat, rwy._lon, azimuth, 
889                      rwy._length*0.45,
890                       &lat2, &lon2, &az2 );
891   wpt = new waypoint;
892   wpt->name      = "Center"; //wpt_node->getStringValue("name", "END");
893   wpt->latitude  = rwy._lat;
894   wpt->longitude = rwy._lon;
895   wpt->altitude  = apt->getElevation();
896   wpt->speed     = 30; 
897   wpt->crossat   = -10000;
898   wpt->gear_down = true;
899   wpt->flaps_down= true;
900   wpt->finished  = false;
901   wpt->on_ground = true;
902   wpt->routeIndex = 0;
903   waypoints.push_back(wpt);
904
905  geo_direct_wgs_84 ( 0, rwy._lat, rwy._lon, heading, 
906                      rwy._length*0.45 * SG_FEET_TO_METER,
907                      &lat2, &lon2, &az2 );
908   wpt = new waypoint;
909   wpt->name      = "Threshold"; //wpt_node->getStringValue("name", "END");
910   wpt->latitude  = lat2;
911   wpt->longitude = lon2;
912   wpt->altitude  = apt->getElevation();
913   wpt->speed     = 15; 
914   wpt->crossat   = apt->getElevation();
915   wpt->gear_down = true;
916   wpt->flaps_down= true;
917   wpt->finished  = false;
918   wpt->on_ground = true;
919   wpt->routeIndex = 0;
920   waypoints.push_back(wpt); 
921 }
922
923 /*******************************************************************
924  * CreateParking
925  * initialize the Aircraft at the parking location
926  ******************************************************************/
927 void FGAIFlightPlan::createParking(FGAirport *apt, double radius)
928 {
929   waypoint* wpt;
930   double lat, lat2;
931   double lon, lon2;
932   double az2;
933   double heading;
934   apt->getDynamics()->getParking(gateId, &lat, &lon, &heading);
935   heading += 180.0;
936   if (heading > 360)
937     heading -= 360; 
938   geo_direct_wgs_84 ( 0, lat, lon, heading, 
939                       2.2*radius,           
940                       &lat2, &lon2, &az2 );
941   wpt = new waypoint;
942   wpt->name      = "taxiStart";
943   wpt->latitude  = lat2;
944   wpt->longitude = lon2;
945   wpt->altitude  = apt->getElevation();
946   wpt->speed     = 10; 
947   wpt->crossat   = -10000;
948   wpt->gear_down = true;
949   wpt->flaps_down= true;
950   wpt->finished  = false;
951   wpt->on_ground = true;
952   wpt->routeIndex = 0;
953   waypoints.push_back(wpt); 
954   geo_direct_wgs_84 ( 0, lat, lon, heading, 
955                       0.1 *radius,           
956                       &lat2, &lon2, &az2 );
957   wpt = new waypoint;
958   wpt->name      = "taxiStart";
959   wpt->latitude  = lat2;
960   wpt->longitude = lon2;
961   wpt->altitude  = apt->getElevation();
962   wpt->speed     = 10; 
963   wpt->crossat   = -10000;
964   wpt->gear_down = true;
965   wpt->flaps_down= true;
966   wpt->finished  = false;
967   wpt->on_ground = true;
968   wpt->routeIndex = 0;
969   waypoints.push_back(wpt);   
970
971   wpt = new waypoint;
972   wpt->name      = "END"; //wpt_node->getStringValue("name", "END");
973   wpt->latitude  = lat;
974   wpt->longitude = lon;
975   wpt->altitude  = apt->getElevation();
976   wpt->speed     = 15; 
977   wpt->crossat   = -10000;
978   wpt->gear_down = true;
979   wpt->flaps_down= true;
980   wpt->finished  = false;
981   wpt->on_ground = true;
982   wpt->routeIndex = 0;
983   waypoints.push_back(wpt);
984 }