]> git.mxchange.org Git - flightgear.git/blob - src/AIModel/AIFlightPlan.cxx
Remove unused variables
[flightgear.git] / src / AIModel / AIFlightPlan.cxx
1 // FGAIFlightPlan - class for loading and storing  AI flight plans
2 // Written by David Culp, started May 2004
3 // - davidculp2@comcast.net
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 #ifdef HAVE_CONFIG_H
20 #  include <config.h>
21 #endif
22
23 #include <simgear/misc/sg_path.hxx>
24 #include <simgear/debug/logstream.hxx>
25 #include <simgear/route/waypoint.hxx>
26 #include <simgear/math/sg_geodesy.hxx>
27 #include <simgear/structure/exception.hxx>
28 #include <simgear/constants.h>
29 #ifdef __BORLANDC__
30 #  define exception c_exception
31 #endif
32 #include <simgear/props/props.hxx>
33 #include <Main/globals.hxx>
34 #include <Main/fg_props.hxx>
35 #include <Main/fg_init.hxx>
36 #include <Airports/simple.hxx>
37 #include <Airports/runways.hxx>
38
39
40 #include <Environment/environment_mgr.hxx>
41 #include <Environment/environment.hxx>
42
43 #include "AIFlightPlan.hxx"
44
45
46 FGAIFlightPlan::FGAIFlightPlan(const string& filename)
47 {
48   int i;
49   start_time = 0;
50   leg = 10;
51   gateId = 0;
52   taxiRoute = 0;
53   SGPath path( globals->get_fg_root() );
54   path.append( ("/AI/FlightPlans/" + filename).c_str() );
55   SGPropertyNode root;
56   repeat = false;
57
58   try {
59       readProperties(path.str(), &root);
60   } catch (const sg_exception &e) {
61       SG_LOG(SG_GENERAL, SG_ALERT,
62        "Error reading AI flight plan: " << path.str());
63        // cout << path.str() << endl;
64      return;
65   }
66
67   SGPropertyNode * node = root.getNode("flightplan");
68   for (i = 0; i < node->nChildren(); i++) { 
69      //cout << "Reading waypoint " << i << endl;        
70      waypoint* wpt = new waypoint;
71      SGPropertyNode * wpt_node = node->getChild(i);
72      wpt->name      = wpt_node->getStringValue("name", "END");
73      wpt->latitude  = wpt_node->getDoubleValue("lat", 0);
74      wpt->longitude = wpt_node->getDoubleValue("lon", 0);
75      wpt->altitude  = wpt_node->getDoubleValue("alt", 0);
76      wpt->speed     = wpt_node->getDoubleValue("ktas", 0);
77      wpt->crossat   = wpt_node->getDoubleValue("crossat", -10000);
78      wpt->gear_down = wpt_node->getBoolValue("gear-down", false);
79      wpt->flaps_down= wpt_node->getBoolValue("flaps-down", false);
80      wpt->on_ground = wpt_node->getBoolValue("on-ground", false);
81      wpt->time_sec   = wpt_node->getDoubleValue("time-sec", 0);
82      wpt->time       = wpt_node->getStringValue("time", "");
83
84      if (wpt->name == "END") wpt->finished = true;
85      else wpt->finished = false;
86
87      waypoints.push_back( wpt );
88    }
89
90   wpt_iterator = waypoints.begin();
91   //cout << waypoints.size() << " waypoints read." << endl;
92 }
93
94
95 // This is a modified version of the constructor,
96 // Which not only reads the waypoints from a 
97 // Flight plan file, but also adds the current
98 // Position computed by the traffic manager, as well
99 // as setting speeds and altitude computed by the
100 // traffic manager. 
101 FGAIFlightPlan::FGAIFlightPlan(const std::string& p,
102                                double course,
103                                time_t start,
104                                FGAirport *dep,
105                                FGAirport *arr,
106                                bool firstLeg,
107                                double radius,
108                                double alt,
109                                double lat,
110                                double lon,
111                                double speed,
112                                const string& fltType,
113                                const string& acType,
114                                const string& airline)
115 {
116   repeat = false;
117   leg = 10;
118   gateId=0;
119   taxiRoute = 0;
120   start_time = start;
121   bool useInitialWayPoint = true;
122   bool useCurrentWayPoint = false;
123   SGPath path( globals->get_fg_root() );
124   path.append( "/AI/FlightPlans" );
125   path.append( p );
126   
127   SGPropertyNode root;
128   
129   // This is a bit of a hack:
130   // Normally the value of course will be used to evaluate whether
131   // or not a waypoint will be used for midair initialization of 
132   // an AI aircraft. However, if a course value of 999 will be passed
133   // when an update request is received, which will by definition always be
134   // on the ground and should include all waypoints.
135   if (course == 999) 
136     {
137       useInitialWayPoint = false;
138       useCurrentWayPoint = true;
139     }
140
141   if (path.exists()) 
142     {
143       try 
144         {
145           readProperties(path.str(), &root);
146           
147           SGPropertyNode * node = root.getNode("flightplan");
148           
149           //waypoints.push_back( init_waypoint );
150           for (int i = 0; i < node->nChildren(); i++) { 
151             //cout << "Reading waypoint " << i << endl;
152             waypoint* wpt = new waypoint;
153             SGPropertyNode * wpt_node = node->getChild(i);
154             wpt->name      = wpt_node->getStringValue("name", "END");
155             wpt->latitude  = wpt_node->getDoubleValue("lat", 0);
156             wpt->longitude = wpt_node->getDoubleValue("lon", 0);
157             wpt->altitude  = wpt_node->getDoubleValue("alt", 0);
158             wpt->speed     = wpt_node->getDoubleValue("ktas", 0);
159             //wpt->speed     = speed;
160             wpt->crossat   = wpt_node->getDoubleValue("crossat", -10000);
161             wpt->gear_down = wpt_node->getBoolValue("gear-down", false);
162             wpt->flaps_down= wpt_node->getBoolValue("flaps-down", false);
163             
164             if (wpt->name == "END") wpt->finished = true;
165             else wpt->finished = false;
166             waypoints.push_back(wpt);
167           }
168         }
169       catch (const sg_exception &e) {
170         SG_LOG(SG_GENERAL, SG_WARN,
171                "Error reading AI flight plan: ");
172         cerr << "Errno = " << errno << endl;
173         if (errno == ENOENT)
174           {
175             SG_LOG(SG_GENERAL, SG_WARN, "Reason: No such file or directory");
176           }
177       }
178     }
179   else
180     {
181       // cout << path.str() << endl;
182       // cout << "Trying to create this plan dynamically" << endl;
183       // cout << "Route from " << dep->id << " to " << arr->id << endl;
184       time_t now = time(NULL) + fgGetLong("/sim/time/warp");
185       time_t timeDiff = now-start; 
186       leg = 1;
187       /*
188       if ((timeDiff > 300) && (timeDiff < 1200))
189         leg = 2;
190       else if ((timeDiff >= 1200) && (timeDiff < 1500))
191         leg = 3;
192       else if ((timeDiff >= 1500) && (timeDiff < 2000))
193         leg = 4;
194       else if (timeDiff >= 2000)
195         leg = 5;
196       */
197       if (timeDiff >= 2000)
198           leg = 5;
199
200       SG_LOG(SG_GENERAL, SG_INFO, "Route from " << dep->getId() << " to " << arr->getId() << ". Set leg to : " << leg);
201       wpt_iterator = waypoints.begin();
202       create(dep,arr, leg, alt, speed, lat, lon,
203              firstLeg, radius, fltType, acType, airline);
204       wpt_iterator = waypoints.begin();
205       //cerr << "after create: " << (*wpt_iterator)->name << endl;
206       //leg++;
207       // Now that we have dynamically created a flight plan,
208       // we need to add some code that pops any waypoints already past.
209       //return;
210     }
211   /*
212     waypoint* init_waypoint   = new waypoint;
213     init_waypoint->name       = string("initial position");
214     init_waypoint->latitude   = entity->latitude;
215     init_waypoint->longitude  = entity->longitude;
216     init_waypoint->altitude   = entity->altitude;
217     init_waypoint->speed      = entity->speed;
218     init_waypoint->crossat    = - 10000;
219     init_waypoint->gear_down  = false;
220     init_waypoint->flaps_down = false;
221     init_waypoint->finished   = false;
222     
223     wpt_vector_iterator i = waypoints.begin();
224     while (i != waypoints.end())
225     {
226       //cerr << "Checking status of each waypoint: " << (*i)->name << endl;
227        SGWayPoint first(init_waypoint->longitude, 
228                        init_waypoint->latitude, 
229                        init_waypoint->altitude);
230       SGWayPoint curr ((*i)->longitude, 
231                        (*i)->latitude, 
232                        (*i)->altitude);
233       double crse, crsDiff;
234       double dist;
235       curr.CourseAndDistance(first, &crse, &dist);
236       
237       dist *= SG_METER_TO_NM;
238       
239       // We're only interested in the absolute value of crsDiff
240       // wich should fall in the 0-180 deg range.
241       crsDiff = fabs(crse-course);
242       if (crsDiff > 180)
243         crsDiff = 360-crsDiff;
244       // These are the three conditions that we consider including
245       // in our flight plan:
246       // 1) current waypoint is less then 100 miles away OR
247       // 2) curren waypoint is ahead of us, at any distance
248      
249       if ((dist > 20.0) && (crsDiff > 90.0) && ((*i)->name != string ("EOF")))
250         {
251           //useWpt = false;
252           // Once we start including waypoints, we have to continue, even though
253           // one of the following way point would suffice. 
254           // so once is the useWpt flag is set to true, we cannot reset it to false.
255           //cerr << "Discarding waypoint: " << (*i)->name 
256           //   << ": Course difference = " << crsDiff
257           //  << "Course = " << course
258           // << "crse   = " << crse << endl;
259         }
260       else
261         useCurrentWayPoint = true;
262       
263       if (useCurrentWayPoint)
264         {
265           if ((dist > 100.0) && (useInitialWayPoint))
266             {
267               //waypoints.push_back(init_waypoint);;
268               waypoints.insert(i, init_waypoint);
269               //cerr << "Using waypoint : " << init_waypoint->name <<  endl;
270             }
271           //if (useInitialWayPoint)
272           // {
273           //    (*i)->speed = dist; // A hack
274           //  }
275           //waypoints.push_back( wpt );
276           //cerr << "Using waypoint : " << (*i)->name 
277           //  << ": course diff : " << crsDiff 
278           //   << "Course = " << course
279           //   << "crse   = " << crse << endl
280           //    << "distance      : " << dist << endl;
281           useInitialWayPoint = false;
282           i++;
283         }
284       else 
285         {
286           //delete wpt;
287           delete *(i);
288           i = waypoints.erase(i);
289           }
290           
291         }
292   */
293   //for (i = waypoints.begin(); i != waypoints.end(); i++)
294   //  cerr << "Using waypoint : " << (*i)->name << endl;
295   //wpt_iterator = waypoints.begin();
296   //cout << waypoints.size() << " waypoints read." << endl;
297 }
298
299
300
301
302 FGAIFlightPlan::~FGAIFlightPlan()
303 {
304   deleteWaypoints();
305   delete taxiRoute;
306 }
307
308
309 FGAIFlightPlan::waypoint* const
310 FGAIFlightPlan::getPreviousWaypoint( void ) const
311 {
312   if (wpt_iterator == waypoints.begin()) {
313     return 0;
314   } else {
315     wpt_vector_iterator prev = wpt_iterator;
316     return *(--prev);
317   }
318 }
319
320 FGAIFlightPlan::waypoint* const
321 FGAIFlightPlan::getCurrentWaypoint( void ) const
322 {
323   return *wpt_iterator;
324 }
325
326 FGAIFlightPlan::waypoint* const
327 FGAIFlightPlan::getNextWaypoint( void ) const
328 {
329   wpt_vector_iterator i = waypoints.end();
330   i--;  // end() points to one element after the last one. 
331   if (wpt_iterator == i) {
332     return 0;
333   } else {
334     wpt_vector_iterator next = wpt_iterator;
335     return *(++next);
336   }
337 }
338
339 void FGAIFlightPlan::IncrementWaypoint(bool eraseWaypoints )
340 {
341   if (eraseWaypoints)
342     {
343       if (wpt_iterator == waypoints.begin())
344         wpt_iterator++;
345       else
346         {
347           delete *(waypoints.begin());
348           waypoints.erase(waypoints.begin());
349           wpt_iterator = waypoints.begin();
350           wpt_iterator++;
351         }
352     }
353   else
354     wpt_iterator++;
355
356 }
357
358 // gives distance in feet from a position to a waypoint
359 double FGAIFlightPlan::getDistanceToGo(double lat, double lon, waypoint* wp) const{
360    double course, distance;
361    // get size of a degree2 at the present latitude
362    // this won't work over large distances
363    //double ft_per_deg_lat = 366468.96 - 3717.12 * cos(lat / SG_RADIANS_TO_DEGREES);
364    //double ft_per_deg_lon = 365228.16 * cos(lat / SG_RADIANS_TO_DEGREES);
365    //double lat_diff_ft = fabs(wp->latitude - lat) * ft_per_deg_lat;
366    //double lon_diff_ft = fabs(wp->longitude - lon) * ft_per_deg_lon;
367    //return sqrt((lat_diff_ft * lat_diff_ft) + (lon_diff_ft * lon_diff_ft));
368    SGWayPoint sgWp(wp->longitude,wp->latitude, wp->altitude, SGWayPoint::WGS84, string("temp"));
369    sgWp.CourseAndDistance(lon, lat, wp->altitude, &course, &distance);
370    return distance;
371 }
372
373 // sets distance in feet from a lead point to the current waypoint
374 void FGAIFlightPlan::setLeadDistance(double speed, double bearing, 
375                                      waypoint* current, waypoint* next){
376   double turn_radius;
377   // Handle Ground steering
378   // At a turn rate of 30 degrees per second, it takes 12 seconds to do a full 360 degree turn
379   // So, to get an estimate of the turn radius, calculate the cicumference of the circle
380   // we travel on. Get the turn radius by dividing by PI (*2).
381   if (speed < 25) {
382        turn_radius = ((360/30)*15) / (2*M_PI);
383   } else 
384       turn_radius = 0.1911 * speed * speed; // an estimate for 25 degrees bank
385
386   double inbound = bearing;
387   double outbound = getBearing(current, next);
388   leadInAngle = fabs(inbound - outbound);
389   if (leadInAngle > 180.0) 
390     leadInAngle = 360.0 - leadInAngle;
391   //if (leadInAngle < 30.0) // To prevent lead_dist from getting so small it is skipped 
392   //  leadInAngle = 30.0;
393   
394   //lead_distance = turn_radius * sin(leadInAngle * SG_DEGREES_TO_RADIANS); 
395   lead_distance = turn_radius * tan((leadInAngle * SG_DEGREES_TO_RADIANS)/2);
396   //  if ((errno == EDOM) || (errno == ERANGE) || lead_distance < 1.0)
397   //  {
398   //  }
399 }
400
401 void FGAIFlightPlan::setLeadDistance(double distance_ft){
402   lead_distance = distance_ft;
403 }
404
405
406 double FGAIFlightPlan::getBearing(waypoint* first, waypoint* second) const{
407   return getBearing(first->latitude, first->longitude, second);
408 }
409
410
411 double FGAIFlightPlan::getBearing(double lat, double lon, waypoint* wp) const{
412   double course, distance;
413  //  double latd = lat;
414 //   double lond = lon;
415 //   double latt = wp->latitude;
416 //   double lont = wp->longitude;
417 //   double ft_per_deg_lat = 366468.96 - 3717.12 * cos(lat/SG_RADIANS_TO_DEGREES);
418 //   double ft_per_deg_lon = 365228.16 * cos(lat/SG_RADIANS_TO_DEGREES);
419
420 //   if (lond < 0.0) {
421 //     lond+=360.0;
422 //     lont+=360;
423 //   }
424 //   if (lont < 0.0) {
425 //     lond+=360.0;
426 //     lont+=360.0;
427 //   }
428 //   latd+=90.0;
429 //   latt+=90.0;
430
431 //   double lat_diff = (latt - latd) * ft_per_deg_lat;
432 //   double lon_diff = (lont - lond) * ft_per_deg_lon;
433 //   double angle = atan(fabs(lat_diff / lon_diff)) * SG_RADIANS_TO_DEGREES;
434
435 //   bool southerly = true;
436 //   if (latt > latd) southerly = false;
437 //   bool easterly = false;
438 //   if (lont > lond) easterly = true;
439 //   if (southerly && easterly) return 90.0 + angle;
440 //   if (!southerly && easterly) return 90.0 - angle;
441 //   if (southerly && !easterly) return 270.0 - angle;
442 //   if (!southerly && !easterly) return 270.0 + angle; 
443   SGWayPoint sgWp(wp->longitude,wp->latitude, wp->altitude, SGWayPoint::WGS84, string("temp"));
444   sgWp.CourseAndDistance(lon, lat, wp->altitude, &course, &distance);
445   
446   return course;
447   // Omit a compiler warning. 
448   //if ((errno == EDOM) || (errno == ERANGE))
449   //  {
450   //    cerr << "Lon:  " << wp->longitude
451   //       << "Lat       = " << wp->latitude
452   //   << "Tgt Lon   = " <<  
453   //   << "TgT Lat   = " << speed << endl;
454   //  }
455   
456 }
457
458
459
460 void FGAIFlightPlan::deleteWaypoints()
461 {
462   for (wpt_vector_iterator i = waypoints.begin(); i != waypoints.end();i++)
463     delete (*i);
464   waypoints.clear();
465 }
466
467 // Delete all waypoints except the last, 
468 // which we will recycle as the first waypoint in the next leg;
469 void FGAIFlightPlan::resetWaypoints()
470 {
471   if (waypoints.begin() == waypoints.end())
472     return;
473   else
474     {
475       waypoint *wpt = new waypoint;
476       wpt_vector_iterator i = waypoints.end();
477       i--;
478       wpt->name      = (*i)->name;
479       wpt->latitude  = (*i)->latitude;
480       wpt->longitude =  (*i)->longitude;
481       wpt->altitude  =  (*i)->altitude;
482       wpt->speed     =  (*i)->speed;
483       wpt->crossat   =  (*i)->crossat;
484       wpt->gear_down =  (*i)->gear_down;
485       wpt->flaps_down=  (*i)->flaps_down;
486       wpt->finished  = false;
487       wpt->on_ground =  (*i)->on_ground;
488       //cerr << "Recycling waypoint " << wpt->name << endl;
489       deleteWaypoints();
490       waypoints.push_back(wpt);
491     }
492 }
493
494 // Start flightplan over from the beginning
495 void FGAIFlightPlan::restart()
496 {
497   wpt_iterator = waypoints.begin();
498 }
499
500
501 void FGAIFlightPlan::deleteTaxiRoute() 
502 {
503   delete taxiRoute;
504   taxiRoute = 0;
505 }
506
507
508 int FGAIFlightPlan::getRouteIndex(int i) {
509   if ((i > 0) && (i < waypoints.size())) {
510     return waypoints[i]->routeIndex;
511   }
512   else
513     return 0;
514 }