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