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