]> git.mxchange.org Git - flightgear.git/blob - src/ATC/approach.cxx
Add David Culp's AI model manager code which is derived from David Luff's AI/ATC...
[flightgear.git] / src / ATC / approach.cxx
1 // FGApproach - a class to provide approach control at larger airports.
2 //
3 // Written by Alexander Kappes, started March 2002.
4 //
5 // Copyright (C) 2002  Alexander Kappes
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 // General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20
21 #include "approach.hxx"
22 #include "transmission.hxx"
23 #include "transmissionlist.hxx"
24 #include "ATCdisplay.hxx"
25 #include "ATCDialog.hxx"
26
27 #include <Airports/runways.hxx>
28 #include <simgear/misc/sg_path.hxx>
29
30 #ifdef FG_WEATHERCM
31 # include <WeatherCM/FGLocalWeatherDatabase.h>
32 #else
33 # include <Environment/environment_mgr.hxx>
34 # include <Environment/environment.hxx>
35 #endif
36
37
38 #include <GUI/gui.h>
39
40 //Constructor
41 FGApproach::FGApproach(){
42   comm1_node = fgGetNode("/radios/comm[0]/frequencies/selected-mhz", true);
43   comm2_node = fgGetNode("/radios/comm[1]/frequencies/selected-mhz", true);
44
45   num_planes = 0;
46   lon_node   = fgGetNode("/position/longitude-deg", true);
47   lat_node   = fgGetNode("/position/latitude-deg", true);
48   elev_node  = fgGetNode("/position/altitude-ft", true);
49   hdg_node   = fgGetNode("/orientation/heading-deg", true);
50   speed_node = fgGetNode("/velocities/airspeed-kt", true);
51   etime_node = fgGetNode("/sim/time/elapsed-ms", true);
52
53   first = true;
54   active_runway = "";
55   int i;
56   for ( i=0; i<max_planes; i++) {
57     planes[i].contact   = 0;
58     planes[i].wpn       = 0;
59     planes[i].dnwp      = -999.;
60     planes[i].on_crs    = true;
61     planes[i].turn_rate = 10.0;
62     planes[i].desc_rate = 1000.0;
63     planes[i].clmb_rate = 500.0;
64     planes[i].tlm       = 0.0;
65     planes[i].lmc.c1    = 0;
66     planes[i].lmc.c2    = 0;
67     planes[i].lmc.c3    = -1;
68     planes[i].wp_change = false;
69   }
70 }
71
72 //Destructor
73 FGApproach::~FGApproach(){
74 }
75
76 void FGApproach::Init() {
77   display = false;
78 }
79
80
81
82 // ============================================================================
83 // the main update function
84 // ============================================================================
85 void FGApproach::Update(double dt) {
86         
87         const int max_trans = 20;
88         FGTransmission tmissions[max_trans];
89         int    wpn;
90         atc_type station = APPROACH;
91         TransCode code;
92         TransPar TPar;
93         int    i,j;
94         //double course, d, 
95         double adif, datp;
96         //char   buf[10];
97         string message;
98         //static string atcmsg1[10];
99         //static string atcmsg2[10];
100         string mentry;
101         string transm;
102         TransPar tpars;
103         //static bool TransDisplayed = false;
104         
105         update_plane_dat();
106         if ( active_runway == "" ) get_active_runway();
107         
108         double comm1_freq = comm1_node->getDoubleValue();
109         
110         //bool DisplayTransmissions = true;
111         
112         for (i=0; i<num_planes; i++) {
113                 if ( planes[i].ident == "Player") { 
114                         station = APPROACH;
115                         tpars.station = name;
116                         tpars.callsign = "Player";
117                         tpars.airport = ident;
118                         
119                         //cout << "ident = " << ident << " name = " << name << '\n';
120                         
121                         int num_trans = 0;
122                         // is the frequency of the station tuned in?
123                         if ( freq == (int)(comm1_freq*100.0 + 0.5) ) {
124                                 current_transmissionlist->query_station( station, tmissions, max_trans, num_trans );
125                                 // loop over all transmissions for station
126                                 for ( j=0; j<=num_trans-1; j++ ) {
127                                         code = tmissions[j].get_code();
128                                         //cout << "code is " << code.c1 << "  " << code.c2 << "  " << code.c3 << '\n';
129                                         // select proper transmissions
130                                         if(code.c3 != 2) {    // DCL - hack to prevent request crossing airspace being displayed since this isn't implemented yet.
131                                             if ( ( code.c2 == -1 && planes[i].lmc.c3 == 0 ) || 
132                                                     ( code.c1 == 0  && code.c2 == planes[i].lmc.c2 ) ) {
133                                                     mentry = current_transmissionlist->gen_text(station, code, tpars, false);
134                                                     transm = current_transmissionlist->gen_text(station, code, tpars, true);
135                                                     // is the transmission already registered?
136                                                     if (!current_atcdialog->trans_reg( ident, transm, APPROACH )) {
137                                                             current_atcdialog->add_entry( ident, transm, mentry, APPROACH, 0 );
138                                                     }
139                                             }
140                                         }
141                                 }
142                         }
143                 }
144         }
145         
146         for ( i=0; i<num_planes; i++ ) {
147                 //cout << "TPar.airport = " << TPar.airport << " TPar.station = " << TPar.station << " TPar.callsign = " << TPar.callsign << '\n';
148                 //if ( planes[i].ident == TPar.callsign && name == TPar.airport && TPar.station == "approach" ) {
149                         //if ( TPar.request && TPar.intention == "landing" && ident == TPar.intid) {
150                         if(planes[i].ident == "Player" && fgGetBool("/sim/atc/opt0")) {
151                                 //cout << "Landing requested\n";
152                                 fgSetBool("/sim/atc/opt0", false);
153                                 planes[i].wpn = 0; 
154                                 // ===========================
155                                 // === calculate waypoints ===
156                                 // ===========================
157                                 calc_wp( i );  
158                                 update_param( i );
159                                 wpn = planes[i].wpn-1;
160                                 planes[i].aalt = planes[i].wpts[wpn-1][2];
161                                 planes[i].ahdg = planes[i].wpts[wpn][4];
162                                 
163                                 // generate the message
164                                 code.c1 = 1;
165                                 code.c2 = 1;
166                                 code.c3 = 0;
167                                 adif = angle_diff_deg( planes[i].hdg, planes[i].ahdg );
168                                 tpars.station = name;
169                                 tpars.callsign = "Player";
170                                 if ( adif < 0 ) tpars.tdir = 1;
171                                 else            tpars.tdir = 2;
172                                 tpars.heading = planes[i].ahdg;
173                                 if      (planes[i].alt-planes[i].aalt > 100.0)  tpars.VDir = 1;
174                                 else if (planes[i].alt-planes[i].aalt < -100.0) tpars.VDir = 3;
175                                 else tpars.VDir = 2;
176                                 tpars.alt = planes[i].aalt;
177                                 message = current_transmissionlist->gen_text(station, code, tpars, true );
178                                 //cout << message << '\n';
179                                 globals->get_ATC_display()->RegisterSingleMessage( message, 0 );
180                                 planes[i].lmc = code;
181                                 planes[i].tlm = etime_node->getDoubleValue();
182                                 planes[i].on_crs = true;
183                                 planes[i].contact = 1;
184                         }
185                 //}
186                 
187                 //if(1) {
188                 if ( planes[i].contact == 1 ) {
189                         // =========================
190                         // === update parameters ===
191                         // =========================
192                         update_param( i );
193                         //cout << planes[i].brg << " " << planes[i].dist << " " << planes[i].wpts[wpn+1][0] 
194                         //<< " " << planes[i].wpts[wpn+1][1] << " " << planes[i].wpts[wpn+1][4] 
195                         //cout << wpn << " distance to current course = " << planes[i].dcc << endl;
196                         //cout << etime_node->getDoubleValue() << endl;
197                         
198                         // =========================
199                         // === reached waypoint? ===
200                         // =========================
201                         wpn = planes[i].wpn-2;
202                         adif = angle_diff_deg( planes[i].hdg, planes[i].wpts[wpn][4] ) 
203                         * SGD_DEGREES_TO_RADIANS;
204                         datp = 2*sin(fabs(adif)/2.0)*sin(fabs(adif)/2.0) *
205                                planes[i].spd/3600. * planes[i].turn_rate + 
206                                planes[i].spd/3600. * 3.0;
207                         //cout << adif/SGD_DEGREES_TO_RADIANS << " " 
208                         //     << datp << " " << planes[i].dnc << " " << planes[i].dcc <<endl;
209                         if ( fabs(planes[i].dnc) < datp ) {
210                         //if ( fabs(planes[i].dnc) < 0.3 && planes[i].dnwp < 1.0 ) {
211                                 //cout << "Reached next waypoint!\n";
212                                 planes[i].wpn -= 1;
213                                 wpn = planes[i].wpn-1;
214                                 planes[i].ahdg = planes[i].wpts[wpn][4];
215                                 planes[i].aalt = planes[i].wpts[wpn-1][2];
216                                 planes[i].wp_change = true;
217                                 
218                                 // generate the message
219                                 adif = angle_diff_deg( planes[i].hdg, planes[i].ahdg );
220                                 tpars.station = name;
221                                 tpars.callsign = "Player";
222                                 if ( adif < 0 ) tpars.tdir = 1;
223                                 else            tpars.tdir = 2;
224                                 tpars.heading = planes[i].ahdg;
225                                 
226                                 if ( wpn-1 != 0) { 
227                                         code.c1 = 1;
228                                         code.c2 = 1;
229                                         code.c3 = 0;
230                                         if      (planes[i].alt-planes[i].aalt > 100.0)  tpars.VDir = 1;
231                                         else if (planes[i].alt-planes[i].aalt < -100.0) tpars.VDir = 3;
232                                         else tpars.VDir = 2;
233                                         tpars.alt = planes[i].aalt;
234                                         message = current_transmissionlist->gen_text(station, code, tpars, true );
235                                         //cout << "Approach transmitting...\n";
236                                         //cout << message << endl;
237                                         globals->get_ATC_display()->RegisterSingleMessage( message, 0 );
238                                         
239                                 }
240                                 else {
241                                         code.c1 = 1;
242                                         code.c2 = 3;
243                                         code.c3 = 0;
244                                         tpars.runway = active_runway;
245                                         message = current_transmissionlist->gen_text(station, code, tpars, true);
246                                         //cout << "Approach transmitting 2 ...\n";
247                                         //cout << message << endl;
248                                         globals->get_ATC_display()->RegisterSingleMessage( message, 0 );
249                                 }
250                                 planes[i].lmc = code;
251                                 planes[i].tlm = etime_node->getDoubleValue();
252                                 planes[i].on_crs = true;
253                                 
254                                 update_param( i );
255                         }
256                         
257                         // =========================
258                         // === come off course ? ===
259                         // =========================
260                         if ( fabs(planes[i].dcc) > 1.0 && 
261                            ( !planes[i].wp_change || etime_node->getDoubleValue() - planes[i].tlm > tbm ) ) {
262                                 //cout << "Off course!\n";
263                                 if ( planes[i].on_crs ) {
264                                         if ( planes[i].dcc < 0) {
265                                                 planes[i].ahdg += 30.0;
266                                         }
267                                         else {
268                                                 planes[i].ahdg -= 30.0;
269                                         }
270                                         if (planes[i].ahdg > 360.0) planes[i].ahdg -= 360.0;
271                                         else if (planes[i].ahdg < 0.0) planes[i].ahdg += 360.0;
272                                 }
273                                 //cout << planes[i].on_crs << " " 
274                                 //     << angle_diff_deg( planes[i].hdg, planes[i].ahdg) << " "
275                                 //     << etime_node->getDoubleValue() << " "
276                                 //     << planes[i].tlm << endl;
277                                 // generate the message
278                                 if ( planes[i].on_crs || 
279                                    ( fabs(angle_diff_deg( planes[i].hdg, planes[i].ahdg )) >  30.0  && 
280                                     etime_node->getDoubleValue() - planes[i].tlm > tbm) ) {
281                                         // generate the message
282                                         code.c1 = 1;
283                                         code.c2 = 4;
284                                         code.c3 = 0;
285                                         adif = angle_diff_deg( planes[i].hdg, planes[i].ahdg );
286                                         tpars.station = name;
287                                         tpars.callsign = "Player";
288                                         tpars.miles   = fabs(planes[i].dcc);
289                                         if ( adif < 0 ) tpars.tdir = 1;
290                                         else            tpars.tdir = 2;
291                                         tpars.heading = planes[i].ahdg;
292                                         message = current_transmissionlist->gen_text(station, code, tpars, true);
293                                         //cout << "Approach transmitting 3 ...\n";
294                                         //cout << message << '\n';
295                                         globals->get_ATC_display()->RegisterSingleMessage( message, 0 );
296                                         planes[i].lmc = code;
297                                         planes[i].tlm = etime_node->getDoubleValue();
298                                 }
299                                 
300                                 planes[i].on_crs = false;
301                         }
302                         else if ( !planes[i].on_crs ) {
303                                 //cout << "Off course 2!\n";
304                                 wpn = planes[i].wpn-1;
305                                 adif = angle_diff_deg( planes[i].hdg, planes[i].wpts[wpn][4] ) 
306                                        * SGD_DEGREES_TO_RADIANS;
307                                 datp = 2*sin(fabs(adif)/2.0)*sin(fabs(adif)/2.0) *
308                                 planes[i].spd/3600. * planes[i].turn_rate + 
309                                 planes[i].spd/3600. * 3.0;
310                                 if ( fabs(planes[i].dcc) < datp ) { 
311                                         planes[i].ahdg = fabs(planes[i].wpts[wpn][4]);
312                                         
313                                         // generate the message
314                                         code.c1 = 1;
315                                         code.c2 = 2;
316                                         code.c3 = 0;
317                                         tpars.station = name;
318                                         tpars.callsign = "Player";
319                                         if ( adif < 0 ) tpars.tdir = 1;
320                                         else            tpars.tdir = 2;
321                                         tpars.heading = planes[i].ahdg;
322                                         message = current_transmissionlist->gen_text(station, code, tpars, true);
323                                         //cout << "Approach transmitting 4 ...\n";
324                                         //cout << message << '\n';
325                                         globals->get_ATC_display()->RegisterSingleMessage( message, 0 );
326                                         planes[i].lmc = code;
327                                         planes[i].tlm = etime_node->getDoubleValue();
328                                         
329                                         planes[i].on_crs = true;          
330                                 } 
331                         }
332                         else if ( planes[i].wp_change  ) {
333                                 planes[i].wp_change = false;
334                         }
335                         
336                         // ===================================================================
337                         // === Less than two minutes away from touchdown? -> Contact Tower ===
338                         // ===================================================================
339                         if ( planes[i].wpn == 2 && planes[i].dnwp < planes[i].spd/60.*2.0 ) {
340                                 
341                                 double freq = 121.95;   // Hardwired - FIXME
342                                 // generate message
343                                 code.c1 = 1;
344                                 code.c2 = 5;
345                                 code.c3 = 0;
346                                 tpars.station = name;
347                                 tpars.callsign = "Player";
348                                 tpars.freq    = freq;
349                                 message = current_transmissionlist->gen_text(station, code, tpars, true);
350                                 //cout << "Approach transmitting 5 ...\n";
351                                 //cout << message << '\n';
352                                 globals->get_ATC_display()->RegisterSingleMessage( message, 0 );
353                                 planes[i].lmc = code;
354                                 planes[i].tlm = etime_node->getDoubleValue();
355                                 
356                                 planes[i].contact = 2;
357                         }
358                 }
359         }
360 }
361
362
363 // ============================================================================
364 // update course parameters
365 // ============================================================================
366 void FGApproach::update_param( const int &i ) {
367   
368   double course, d;
369
370   int wpn = planes[i].wpn-1;            // this is the current waypoint
371
372   planes[i].dcc  = calc_psl_dist(planes[i].brg, planes[i].dist,
373                                  planes[i].wpts[wpn][0], planes[i].wpts[wpn][1],
374                                  planes[i].wpts[wpn][4]);
375   planes[i].dnc  = calc_psl_dist(planes[i].brg, planes[i].dist,
376                                  planes[i].wpts[wpn-1][0], planes[i].wpts[wpn-1][1],
377                                  planes[i].wpts[wpn-1][4]);
378   calc_hd_course_dist(planes[i].brg, planes[i].dist, 
379                       planes[i].wpts[wpn-1][0], planes[i].wpts[wpn-1][1],
380                       &course, &d);
381   planes[i].dnwp = d;
382
383 }
384
385 // ============================================================================
386 // smallest difference between two angles in degree
387 // difference is negative if a1 > a2 and positive if a2 > a1
388 // ===========================================================================
389 double FGApproach::angle_diff_deg( const double &a1, const double &a2) {
390   
391   double a3 = a2 - a1;
392   if (a3 < 180.0) a3 += 360.0;
393   if (a3 > 180.0) a3 -= 360.0;
394
395   return a3;
396 }
397
398 // ============================================================================
399 // calculate waypoints
400 // ============================================================================
401 void FGApproach::calc_wp( const int &i ) {
402         
403         int j;
404         double course, d, cd, a1;
405         
406         int wpn = planes[i].wpn;
407         // waypoint 0: Threshold of active runway
408         calc_gc_course_dist(Point3D(lon*SGD_DEGREES_TO_RADIANS, lat*SGD_DEGREES_TO_RADIANS, 0.0),
409                             Point3D(active_rw_lon*SGD_DEGREES_TO_RADIANS,active_rw_lat*SGD_DEGREES_TO_RADIANS, 0.0 ),
410                             &course, &d);
411         double d1 = active_rw_hdg+180.0;
412         if ( d1 > 360.0 ) d1 -=360.0;
413         calc_cd_head_dist(360.0-course*SGD_RADIANS_TO_DEGREES, d/SG_NM_TO_METER, 
414                           d1, active_rw_len/SG_NM_TO_METER/2.0, 
415                           &planes[i].wpts[wpn][0], &planes[i].wpts[wpn][1]);
416         planes[i].wpts[wpn][2] = elev;
417         planes[i].wpts[wpn][4] = 0.0;
418         planes[i].wpts[wpn][5] = 0.0;
419         wpn += 1;
420         
421         // ======================
422         // horizontal navigation
423         // ======================
424         // waypoint 1: point for turning onto final
425         calc_cd_head_dist(planes[i].wpts[wpn-1][0], planes[i].wpts[wpn-1][1], d1, lfl,
426                           &planes[i].wpts[wpn][0], &planes[i].wpts[wpn][1]);
427         calc_hd_course_dist(planes[i].wpts[wpn][0],   planes[i].wpts[wpn][1],
428                             planes[i].wpts[wpn-1][0], planes[i].wpts[wpn-1][1],
429                             &course, &d);
430         planes[i].wpts[wpn][4] = course;
431         planes[i].wpts[wpn][5] = d;
432         wpn += 1;
433         
434         // calculate course and distance from plane position to waypoint 1
435         calc_hd_course_dist(planes[i].brg, planes[i].dist, planes[i].wpts[1][0], 
436                             planes[i].wpts[1][1], &course, &d);
437         // check if airport is not between plane and waypoint 1 and
438         // DCA to airport on course to waypoint 1 is larger than 10 miles
439         double zero = 0.0;
440         if ( fabs(angle_diff_deg( planes[i].wpts[1][0], planes[i].brg  )) < 90.0 ||
441                 calc_psl_dist( zero, zero, planes[i].brg, planes[i].dist, course ) > 10.0 ) {
442                 // check if turning angle at waypoint 1 would be > max_ta
443                 if ( fabs(angle_diff_deg( planes[i].wpts[1][4], course )) > max_ta ) {
444                         cd = calc_psl_dist(planes[i].brg, planes[i].dist,
445                              planes[i].wpts[1][0], planes[i].wpts[1][1],
446                              planes[i].wpts[1][4]);
447                         a1 = atan2(cd,planes[i].wpts[1][1]);
448                         planes[i].wpts[wpn][0] = planes[i].wpts[1][0] - a1/SGD_DEGREES_TO_RADIANS;
449                         if ( planes[i].wpts[wpn][0] < 0.0)   planes[i].wpts[wpn][0] += 360.0;   
450                         if ( planes[i].wpts[wpn][0] > 360.0) planes[i].wpts[wpn][0] -= 360.0;   
451                         planes[i].wpts[wpn][1] = fabs(cd) / sin(fabs(a1));
452                         calc_hd_course_dist(planes[i].wpts[wpn][0],   planes[i].wpts[wpn][1],
453                                             planes[i].wpts[wpn-1][0], planes[i].wpts[wpn-1][1],
454                                             &course, &d);
455                         planes[i].wpts[wpn][4] = course;
456                         planes[i].wpts[wpn][5] = d;
457                         wpn += 1;
458                         
459                         calc_hd_course_dist(planes[i].brg, planes[i].dist, planes[i].wpts[wpn-1][0], 
460                                             planes[i].wpts[wpn-1][1], &course, &d);
461                 }
462         } else {
463                 double leg = 10.0;
464                 a1 = atan2(planes[i].wpts[1][1], leg );
465                 
466                 if ( angle_diff_deg(planes[i].brg,planes[i].wpts[1][0]) < 0 ) 
467                         planes[i].wpts[wpn][0] = planes[i].wpts[1][0] + a1/SGD_DEGREES_TO_RADIANS;
468                 else planes[i].wpts[wpn][0] = planes[i].wpts[1][0] - a1/SGD_DEGREES_TO_RADIANS;
469                 
470                 planes[i].wpts[wpn][1] = sqrt( planes[i].wpts[1][1]*planes[i].wpts[1][1] + leg*leg );
471                 calc_hd_course_dist(planes[i].wpts[wpn][0],   planes[i].wpts[wpn][1],
472                                     planes[i].wpts[wpn-1][0], planes[i].wpts[wpn-1][1],
473                                     &course, &d);
474                 planes[i].wpts[wpn][4] = course;
475                 planes[i].wpts[wpn][5] = d;
476                 wpn += 1;
477                 
478                 calc_hd_course_dist(planes[i].brg, planes[i].dist,
479                                     planes[i].wpts[wpn-1][0], planes[i].wpts[wpn-1][1],
480                                     &course, &d);
481         }
482         
483         planes[i].wpts[wpn][0] = planes[i].brg;
484         planes[i].wpts[wpn][1] = planes[i].dist;
485         planes[i].wpts[wpn][2] = planes[i].alt;
486         planes[i].wpts[wpn][4] = course;
487         planes[i].wpts[wpn][5] = d;
488         wpn += 1;
489         
490         planes[i].wpn = wpn;
491         
492         // Now check if legs are too short or if legs can be shortend
493         // legs must be at least 2 flight minutes long
494         double mdist = planes[i].spd / 60.0 * 2.0;
495         for ( j=2; j<wpn-1; ++j ) {
496                 if ( planes[i].wpts[j][1] < mdist) {
497                 }
498         }
499         
500         // ====================
501         // vertical navigation
502         // ====================
503         double alt = elev+3000.0;
504         planes[i].wpts[1][2] = round_alt( true, alt );
505         for ( j=2; j<wpn-1; ++j ) {
506                 double dalt = planes[i].alt - planes[i].wpts[j-1][2];
507                 if ( dalt > 0 ) {
508                         alt = planes[i].wpts[j-1][2] + 
509                               (planes[i].wpts[j][5] / planes[i].spd) * 60.0 * planes[i].desc_rate;
510                         planes[i].wpts[j][2] = round_alt( false, alt );
511                         if ( planes[i].wpts[j][2] > planes[i].alt ) 
512                                 planes[i].wpts[j][2] = round_alt( false, planes[i].alt );
513                 }
514                 else {
515                         planes[i].wpts[j][2] = planes[i].wpts[1][2];
516                 }
517         }
518         
519         cout << "Plane position: " << planes[i].brg << " " << planes[i].dist << endl;
520         for ( j=0; j<wpn; ++j ) {
521                 cout << "Waypoint " << j << endl;
522                 cout << "------------------" << endl;
523                 cout << planes[i].wpts[j][0] << "   " << planes[i].wpts[j][1]
524                      << "   " << planes[i].wpts[j][2] << "   " << planes[i].wpts[j][5]; 
525                 cout << endl << endl;
526         }
527         
528 }
529
530
531 // ============================================================================
532 // round altitude value to next highest/lowest 500 feet
533 // ============================================================================
534 double FGApproach::round_alt( const bool hl, double alt ) {
535
536   alt = alt/1000.0;
537   if ( hl ) {
538     if ( alt > (int)(alt)+0.5 ) alt = ((int)(alt)+1)*1000.0;
539     else alt = ((int)(alt)+0.5)*1000.0;
540   }
541   else {
542     if ( alt > (int)(alt)+0.5 ) alt = ((int)(alt)+0.5)*1000.0;
543     else alt = ((int)(alt))*1000.0;
544   }
545   
546   return alt;
547 }
548
549
550 // ============================================================================
551 // get active runway
552 // ============================================================================
553 void FGApproach::get_active_runway() {
554         //cout << "Entering FGApproach::get_active_runway()\n";
555
556 #ifdef FG_WEATHERCM
557   sgVec3 position = { lat, lon, elev };
558   FGPhysicalProperty stationweather = WeatherDatabase->get(position);
559 #else
560   FGEnvironment stationweather =
561       ((FGEnvironmentMgr *)globals->get_subsystem("environment"))
562         ->getEnvironment(lat, lon, elev);
563 #endif
564
565 #ifdef FG_WEATHERCM
566   //Set the heading to into the wind
567   double wind_x = stationweather.Wind[0];
568   double wind_y = stationweather.Wind[1];
569   
570   double speed = sqrt( wind_x*wind_x + wind_y*wind_y ) * SG_METER_TO_NM / (60.0*60.0);
571   double hdg;
572   
573   //If no wind use 270degrees
574   if(speed == 0) {
575     hdg = 270;
576   } else {
577     // //normalize the wind to get the direction
578     //wind_x /= speed; wind_y /= speed;
579     
580     hdg = - atan2 ( wind_x, wind_y ) * SG_RADIANS_TO_DEGREES ;
581     if (hdg < 0.0)
582       hdg += 360.0;
583   }
584 #else
585   double hdg = stationweather.get_wind_from_heading_deg();
586 #endif
587   
588   FGRunway runway;
589   if ( globals->get_runways()->search( ident, int(hdg), &runway) ) {
590     active_runway = runway.rwy_no;
591     active_rw_hdg = runway.heading;
592     active_rw_lon = runway.lon;
593     active_rw_lat = runway.lat;
594     active_rw_len = runway.length;
595     //cout << "Active runway is: " << active_runway << "  heading = " 
596     // << active_rw_hdg 
597     // << " lon = " << active_rw_lon 
598     // << " lat = " << active_rw_lat <<endl;
599   }
600   else cout << "FGRunways search failed\n";
601
602 }
603
604 // ========================================================================
605 // update infos about plane
606 // ========================================================================
607 void FGApproach::update_plane_dat() {
608   
609   //cout << "Update Approach " << ident << "   " << num_planes << " registered" << endl;
610   // update plane positions
611   int i;
612   for (i=0; i<num_planes; i++) {
613     planes[i].lon = lon_node->getDoubleValue();
614     planes[i].lat = lat_node->getDoubleValue();
615     planes[i].alt = elev_node->getDoubleValue();
616     planes[i].hdg = hdg_node->getDoubleValue();
617     planes[i].spd = speed_node->getDoubleValue();
618
619     /*Point3D aircraft = sgGeodToCart( Point3D(planes[i].lon*SGD_DEGREES_TO_RADIANS, 
620                                              planes[i].lat*SGD_DEGREES_TO_RADIANS, 
621                                              planes[i].alt*SG_FEET_TO_METER) );*/
622     double course, distance;
623     calc_gc_course_dist(Point3D(lon*SGD_DEGREES_TO_RADIANS, lat*SGD_DEGREES_TO_RADIANS, 0.0),
624                         Point3D(planes[i].lon*SGD_DEGREES_TO_RADIANS,planes[i].lat*SGD_DEGREES_TO_RADIANS, 0.0 ),
625                         &course, &distance);
626     planes[i].dist = distance/SG_NM_TO_METER;
627     planes[i].brg  = 360.0-course*SGD_RADIANS_TO_DEGREES;
628
629     //cout << "Plane Id: " << planes[i].ident << "  Distance to " << ident 
630     // << " is " << planes[i].dist << " miles   " << "Bearing " << planes[i].brg << endl;
631     
632   }   
633 }
634
635 // =======================================================================
636 // Add plane to Approach list
637 // =======================================================================
638 void FGApproach::AddPlane(string pid) {
639
640   int i;
641   for ( i=0; i<num_planes; i++) {
642     if ( planes[i].ident == pid) {
643       //cout << "Plane already registered: " << planes[i].ident << ' ' << ident << ' ' << num_planes << endl;
644       return;
645     }
646   }
647   planes[num_planes].ident = pid;
648   ++num_planes;
649   //cout << "Plane added to list: " << ident << " " << num_planes << endl;
650   return;
651 }
652
653 // ================================================================================
654 // closest distance between a point (h1,d1) and a straigt line (h2,d2,h3) in 2 dim.
655 // ================================================================================
656 double FGApproach::calc_psl_dist(const double &h1, const double &d1,
657                                  const double &h2, const double &d2,
658                                  const double &h3)
659 {
660   double a1 = h1 * SGD_DEGREES_TO_RADIANS;
661   double a2 = h2 * SGD_DEGREES_TO_RADIANS;
662   double a3 = h3 * SGD_DEGREES_TO_RADIANS;
663   double x1 = cos(a1) * d1;
664   double y1 = sin(a1) * d1;
665   double x2 = cos(a2) * d2;
666   double y2 = sin(a2) * d2;
667   double x3 = cos(a3);
668   double y3 = sin(a3);
669   
670   // formula: dis = sqrt( (v1-v2)**2 - ((v1-v2)*v3)**2 ); vi = (xi,yi)
671   double val1   = (x1-x2)*(x1-x2) + (y1-y2)*(y1-y2);
672   double val2   = ((x1-x2)*x3 + (y1-y2)*y3) * ((x1-x2)*x3 + (y1-y2)*y3);
673   double dis    = val1 - val2;
674   // now get sign for offset 
675   //cout << x1 << " " << x2 << " " << y1 << " " << y2 << " " 
676   //     << x3 << " " << y3 << " " 
677   //     << val1 << " " << val2 << " " << dis << endl;
678   x3 *= sqrt(val2);
679   y3 *= sqrt(val2);
680   double da = fabs(atan2(y3,x3) - atan2(y1-y2,x1-x2));
681   if ( da > SGD_PI ) da -= 2*SGD_PI;
682   if ( fabs(da) > SGD_PI/2.) {
683     //if ( x3*(x1-x2) < 0.0 && y3*(y1-y2) < 0.0) {
684     x3 *= -1.0;
685     y3 *= -1.0;
686   }
687   //cout << x3 << " " << y3 << endl;
688   double dis1   = x1-x2-x3;
689   double dis2   = y1-y2-y3;
690   dis = sqrt(dis);
691   da = atan2(dis2,dis1);
692   if ( da < 0.0 ) da  += 2*SGD_PI;
693   if ( da < a3 )  dis *= -1.0;
694   //cout << dis1 << " " << dis2 << " " << da*SGD_RADIANS_TO_DEGREES << " " << h3
695   //     << " " << sqrt(dis1*dis1 + dis2*dis2) << " " << dis << endl;
696   //cout << atan2(dis2,dis1)*SGD_RADIANS_TO_DEGREES << " " << dis << endl;
697
698   return dis;
699 }
700
701
702 // ========================================================================
703 // Calculate new bear/dist given starting bear/dis, and offset radial,
704 // and distance.
705 // ========================================================================
706 void FGApproach::calc_cd_head_dist(const double &h1, const double &d1, 
707                                    const double &course, const double &dist,
708                                    double *h2, double *d2)
709 {
710   double a1 = h1 * SGD_DEGREES_TO_RADIANS;
711   double a2 = course * SGD_DEGREES_TO_RADIANS;
712   double x1 = cos(a1) * d1;
713   double y1 = sin(a1) * d1;
714   double x2 = cos(a2) * dist;
715   double y2 = sin(a2) * dist;
716     
717   *d2 = sqrt((x1+x2)*(x1+x2) + (y1+y2)*(y1+y2));
718   *h2 = atan2( (y1+y2), (x1+x2) ) * SGD_RADIANS_TO_DEGREES;
719   if ( *h2 < 0 ) *h2 = *h2+360;
720 }
721
722
723
724 // ========================================================================
725 // get heading and distance between two points; point1 ---> point2
726 // ========================================================================
727 void FGApproach::calc_hd_course_dist(const double &h1, const double &d1, 
728                                      const double &h2, const double &d2,
729                                      double *course, double *dist)
730 {
731   double a1 = h1 * SGD_DEGREES_TO_RADIANS;
732   double a2 = h2 * SGD_DEGREES_TO_RADIANS;
733   double x1 = cos(a1) * d1;
734   double y1 = sin(a1) * d1;
735   double x2 = cos(a2) * d2;
736   double y2 = sin(a2) * d2;
737            
738   *dist   = sqrt( (y2-y1)*(y2-y1) + (x2-x1)*(x2-x1) );
739   *course = atan2( (y2-y1), (x2-x1) ) * SGD_RADIANS_TO_DEGREES;
740   if ( *course < 0 ) *course = *course+360;
741   //cout << x1 << " " << y1 << " " << x2 << " " << y2 << " " << *dist << " " << *course << endl;
742 }
743
744
745
746 int FGApproach::RemovePlane() {
747
748   // first check if anything has to be done
749   bool rmplane = false;
750   int i;
751
752   for (i=0; i<num_planes; i++) {
753     if (planes[i].dist > range*SG_NM_TO_METER) {
754       rmplane = true;
755       break;
756     }
757   }
758   if (!rmplane) return num_planes;
759
760   // now make a copy of the plane list
761   PlaneApp tmp[max_planes];
762   for (i=0; i<num_planes; i++) {
763     tmp[i] = planes[i];
764   }
765   
766   int np = 0;
767   // now check which planes are still in range
768   for (i=0; i<num_planes; i++) {
769     if (tmp[i].dist <= range*SG_NM_TO_METER) {
770       planes[np] = tmp[i];
771       np += 1;
772     }
773   }
774   num_planes = np;
775
776   return num_planes;
777 }