1 // FGAIFlightPlan - class for loading and storing AI flight plans
2 // Written by David Culp, started May 2004
3 // - davidculp2@comcast.net
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.
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.
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., 675 Mass Ave, Cambridge, MA 02139, USA.
20 #include "AIFlightPlan.hxx"
21 #include <simgear/misc/sg_path.hxx>
22 #include <simgear/debug/logstream.hxx>
23 #include <simgear/structure/exception.hxx>
24 #include <simgear/constants.h>
26 # define exception c_exception
28 #include <simgear/props/props.hxx>
29 #include <Main/globals.hxx>
30 #include <Main/fg_props.hxx>
33 FGAIFlightPlan::FGAIFlightPlan(string filename)
36 SGPath path( globals->get_fg_root() );
37 path.append( ("/Data/AI/FlightPlans/" + filename).c_str() );
41 readProperties(path.str(), &root);
42 } catch (const sg_exception &e) {
43 SG_LOG(SG_GENERAL, SG_ALERT,
44 "Error reading AI flight plan: ");
45 cout << path.str() << endl;
49 SGPropertyNode * node = root.getNode("FLIGHTPLAN");
50 for (i = 0; i < node->nChildren(); i++) {
51 //cout << "Reading waypoint " << i << endl;
52 waypoint* wpt = new waypoint;
53 waypoints.push_back( wpt );
54 SGPropertyNode * wpt_node = node->getChild(i);
55 wpt->name = wpt_node->getStringValue("NAME", "END");
56 wpt->latitude = wpt_node->getDoubleValue("LAT", 0);
57 wpt->longitude = wpt_node->getDoubleValue("LON", 0);
58 wpt->altitude = wpt_node->getDoubleValue("ALT", 0);
59 wpt->speed = wpt_node->getDoubleValue("KTAS", 0);
60 wpt->crossat = wpt_node->getDoubleValue("CROSSAT", -10000);
61 wpt->gear_down = wpt_node->getBoolValue("GEAR-DOWN", false);
62 wpt->flaps_down= wpt_node->getBoolValue("FLAPS-DOWN", false);
65 wpt_iterator = waypoints.begin();
66 //cout << waypoints.size() << " waypoints read." << endl;
70 FGAIFlightPlan::~FGAIFlightPlan()
76 FGAIFlightPlan::waypoint*
77 FGAIFlightPlan::getPreviousWaypoint( void )
79 if (wpt_iterator == waypoints.begin()) {
82 wpt_vector_iterator prev = wpt_iterator;
87 FGAIFlightPlan::waypoint*
88 FGAIFlightPlan::getCurrentWaypoint( void )
93 FGAIFlightPlan::waypoint*
94 FGAIFlightPlan::getNextWaypoint( void )
96 if (wpt_iterator == waypoints.end()) {
99 wpt_vector_iterator next = wpt_iterator;
104 void FGAIFlightPlan::IncrementWaypoint( void )
109 // gives distance in feet from a position to a waypoint
110 double FGAIFlightPlan::getDistanceToGo(double lat, double lon, waypoint* wp){
111 // get size of a degree at the present latitude
112 // this won't work over large distances
113 double ft_per_deg_lat = 366468.96 - 3717.12 * cos(lat / SG_RADIANS_TO_DEGREES);
114 double ft_per_deg_lon = 365228.16 * cos(lat / SG_RADIANS_TO_DEGREES);
115 double lat_diff_ft = fabs(wp->latitude - lat) * ft_per_deg_lat;
116 double lon_diff_ft = fabs(wp->longitude - lon) * ft_per_deg_lon;
117 return sqrt((lat_diff_ft * lat_diff_ft) + (lon_diff_ft * lon_diff_ft));
120 // sets distance in feet from a lead point to the current waypoint
121 void FGAIFlightPlan::setLeadDistance(double speed, double bearing,
122 waypoint* current, waypoint* next){
123 double turn_radius = 0.1911 * speed * speed; // an estimate for 25 degrees bank
124 double inbound = bearing;
125 double outbound = getBearing(current, next);
126 double diff = fabs(inbound - outbound);
127 if (diff > 180.0) diff = 360.0 - diff;
128 lead_distance = turn_radius * sin(diff * SG_DEGREES_TO_RADIANS);
131 void FGAIFlightPlan::setLeadDistance(double distance_ft){
132 lead_distance = distance_ft;
136 double FGAIFlightPlan::getBearing(waypoint* first, waypoint* second){
137 return getBearing(first->latitude, first->longitude, second);
141 double FGAIFlightPlan::getBearing(double lat, double lon, waypoint* wp){
144 double latt = wp->latitude;
145 double lont = wp->longitude;
146 double ft_per_deg_lat = 366468.96 - 3717.12 * cos(lat/SG_RADIANS_TO_DEGREES);
147 double ft_per_deg_lon = 365228.16 * cos(lat/SG_RADIANS_TO_DEGREES);
149 if (lond < 0.0) lond+=360.0;
150 if (lont < 0.0) lont+=360.0;
154 double lat_diff = (latt - latd) * ft_per_deg_lat;
155 double lon_diff = (lont - lond) * ft_per_deg_lon;
156 double angle = atan(fabs(lat_diff / lon_diff)) * SG_RADIANS_TO_DEGREES;
158 bool southerly = true;
159 if (latt > latd) southerly = false;
160 bool easterly = false;
161 if (lont > lond) easterly = true;
162 if (southerly && easterly) return 90.0 + angle;
163 if (!southerly && easterly) return 90.0 - angle;
164 if (southerly && !easterly) return 270.0 - angle;
165 if (!southerly && !easterly) return 270.0 + angle;
167 /* prevent a compiler warning */