]> git.mxchange.org Git - flightgear.git/blob - src/ATC/ATCmgr.cxx
Search ATC frequencies with both comm1 and 2 instead of just comm1 as previously
[flightgear.git] / src / ATC / ATCmgr.cxx
1 // ATCmgr.cxx - Implementation of FGATCMgr - a global Flightgear ATC manager.
2 //
3 // Written by David Luff, started February 2002.
4 //
5 // Copyright (C) 2002  David C Luff - david.luff@nottingham.ac.uk
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 <Time/event.hxx>
22
23 #include <simgear/misc/sg_path.hxx>
24 #include <simgear/misc/commands.hxx>
25 #include <simgear/debug/logstream.hxx>
26
27 #include "ATCmgr.hxx"
28 #include "atislist.hxx"
29 //#include "groundlist.hxx"
30 #include "towerlist.hxx"
31 #include "approachlist.hxx"
32 #include "ATCdisplay.hxx"
33
34 /*
35 // periodic radio station search wrapper
36 static void fgATCSearch( void ) {
37         globals->get_ATC_mgr()->Search();
38 }
39 */ //This wouldn't compile - including Time/event.hxx breaks it :-(
40
41 static char* t0 = "Request landing clearance";
42 static char* t1 = "Request departure clearance";
43 static char* t2 = "Report Runway vacated";
44 static char** towerOptions = new char*[4];
45 static char* a0 = "Request vectors";
46 static char** approachOptions = new char*[2];
47
48 // For the ATC dialog - copied from the Autopilot new heading dialog code!
49 static puDialogBox*             atcDialog;
50 static puFrame*                 atcDialogFrame;
51 static puText*                  atcDialogMessage;
52 //static puInput*                       atcDialogInput;
53 static puOneShot*               atcDialogOkButton;
54 static puOneShot*               atcDialogCancelButton;
55 static puButtonBox*             atcDialogCommunicationOptions;
56
57 static void ATCDialogCancel(puObject *)
58 {
59     //ATCDialogInput->rejectInput();
60     FG_POP_PUI_DIALOG( atcDialog );
61 }
62
63 static void ATCDialogOK (puObject *me)
64 {
65         // Note that currently the dialog is hardwired to comm1 only here.
66         switch(globals->get_ATC_mgr()->GetComm1ATCType()) {
67         case INVALID:
68                 break;
69         case ATIS:
70                 break;
71         case TOWER: {
72                 FGTower* twr = (FGTower*)globals->get_ATC_mgr()->GetComm1ATCPointer();
73                 switch(atcDialogCommunicationOptions->getValue()) {
74                 case 0:
75                         cout << "Option 0 chosen\n";
76                         twr->RequestLandingClearance("golf bravo echo");
77                         break;
78                 case 1:
79                         cout << "Option 1 chosen\n";
80                         twr->RequestDepartureClearance("golf bravo echo");
81                         break;
82                 case 2:
83                         cout << "Option 2 chosen\n";
84                         twr->ReportRunwayVacated("golf bravo echo");
85                         break;
86                 default:
87                         break;
88                 }
89                 break;
90         }
91         case GROUND:
92                 break;
93         case APPROACH:
94                 break;
95         default:
96                 break;
97         }
98
99     ATCDialogCancel(me);
100     //if(error) mkDialog(s.c_str());
101 }
102
103 static void ATCDialog(puObject *cb)
104 {
105     //ApHeadingDialogInput   ->    setValue ( heading );
106     //ApHeadingDialogInput    -> acceptInput();
107     FG_PUSH_PUI_DIALOG(atcDialog);
108 }
109
110 static void ATCDialogInit()
111 {
112         char defaultATCLabel[] = "Enter desired option to communicate with ATC:";
113         char *s;
114
115         // Option lists hardwired per ATC type  
116         towerOptions[0] = new char[strlen(t0)];
117         strcpy(towerOptions[0], t0);
118         towerOptions[1] = new char[strlen(t1)];
119         strcpy(towerOptions[1], t1);
120         towerOptions[2] = new char[strlen(t2)];
121         strcpy(towerOptions[2], t2);
122         towerOptions[3] = NULL;
123         
124         approachOptions[0] = new char[strlen(a0)];
125         strcpy(approachOptions[0], a0);
126         approachOptions[1] = NULL;
127
128         atcDialog = new puDialogBox (150, 50);
129         {
130                 atcDialogFrame   = new puFrame (0, 0, 500, 250);
131                 
132                 atcDialogMessage = new puText          (250, 220);
133                 atcDialogMessage    -> setDefaultValue (defaultATCLabel);
134                 atcDialogMessage    -> getDefaultValue (&s);
135                 atcDialogMessage    -> setLabel        (s);
136                 atcDialogMessage    -> setLabelPlace   (PUPLACE_TOP_CENTERED);
137
138                 atcDialogCommunicationOptions = new puButtonBox (50, 50, 450, 210, NULL, true);
139                 
140                 atcDialogOkButton     =  new puOneShot         (50, 10, 110, 50);
141                 atcDialogOkButton     ->     setLegend         (gui_msg_OK);
142                 atcDialogOkButton     ->     makeReturnDefault (TRUE);
143                 atcDialogOkButton     ->     setCallback       (ATCDialogOK);
144                 
145                 atcDialogCancelButton =  new puOneShot         (140, 10, 210, 50);
146                 atcDialogCancelButton ->     setLegend         (gui_msg_CANCEL);
147                 atcDialogCancelButton ->     setCallback       (ATCDialogCancel);
148                 
149         }
150         FG_FINALIZE_PUI_DIALOG(atcDialog);
151 }
152
153 // For the command manager - maybe eventually this should go in the built in command list
154 static bool do_ATC_dialog(const SGPropertyNode* arg) {
155         globals->get_ATC_mgr()->doStandardDialog();
156         return(true);
157 }
158
159
160 FGATCMgr::FGATCMgr() {
161         comm1_ident = "";
162         comm1_atis_ident = "";
163         comm1_tower_ident = "";
164         comm1_approach_ident = "";
165         last_comm1_ident = "";
166         last_comm1_atis_ident = "";
167         last_comm1_tower_ident = "";
168         last_comm1_approach_ident = "";
169         approach_ident = "";
170         last_in_range = false;
171         comm1_atis_valid = false;
172         comm1_tower_valid = false;
173         comm1_approach_valid = false;
174         comm1_type = INVALID;
175         comm1_atc_ptr = NULL;
176         comm2_ident = "";
177         comm2_atis_ident = "";
178         comm2_tower_ident = "";
179         comm2_approach_ident = "";
180         last_comm2_ident = "";
181         last_comm2_atis_ident = "";
182         last_comm2_tower_ident = "";
183         last_comm2_approach_ident = "";
184         comm2_atis_valid = false;
185         comm2_tower_valid = false;
186         comm2_approach_valid = false;
187         comm2_type = INVALID;
188         comm2_atc_ptr = NULL;
189 }
190
191 FGATCMgr::~FGATCMgr() {
192 }
193
194 void FGATCMgr::bind() {
195 }
196
197 void FGATCMgr::unbind() {
198 }
199
200 void FGATCMgr::init() {
201         comm1_node = fgGetNode("/radios/comm[0]/frequencies/selected-mhz", true);
202         comm2_node = fgGetNode("/radios/comm[1]/frequencies/selected-mhz", true);
203         lon_node = fgGetNode("/position/longitude-deg", true);
204         lat_node = fgGetNode("/position/latitude-deg", true);
205         elev_node = fgGetNode("/position/altitude-ft", true);
206         atc_list_itr = atc_list.begin();
207         // Search for connected ATC stations once per 0.8 seconds or so
208         // global_events.Register( "fgATCSearch()", fgATCSearch,
209         //                  fgEVENT::FG_EVENT_READY, 800);
210         // For some reason the above doesn't compile - including Time/event.hxx stops compilation.
211         
212         // Initialise the airport_atc_map - we'll cheat for now and just hardcode KEMT and any others that may be needed for development
213         AirportATC *a = new AirportATC;
214         a->lon = -118.034719;
215         a->lat = 34.086114;
216         a->elev = 296.0;
217         a->atis_freq = 118.75;
218         a->atis_active = false;
219         a->tower_freq = 121.2;
220         a->tower_active = false;
221         a->ground_freq = 125.9;
222         a->ground_active = false;
223         
224         //a->set_by_AI = true;
225         //a->set_by_comm_search = false;
226         
227         airport_atc_map[(string)"KEMT"] = a;
228
229 #ifdef ENABLE_AUDIO_SUPPORT     
230         // Load all available voices.
231         // For now we'll do one hardwired one
232         voiceOK = v1.LoadVoice("default");
233         /* I've loaded the voice even if /sim/sound/audible is false
234         *  since I know no way of forcing load of the voice if the user
235         *  subsequently switches /sim/sound/audible to true. */
236 #else
237         voice = false;
238 #endif
239
240         // Initialise the ATC Dialogs
241         ATCDialogInit();
242         
243         // Add ATC-dialog to the command list
244         globals->get_commands()->addCommand("ATC-dialog", do_ATC_dialog);
245 }
246
247 void FGATCMgr::update(double dt) {
248         //Traverse the list of active stations.
249         //Only update one class per update step to avoid the whole ATC system having to calculate between frames.
250         //Eventually we should only update every so many steps.
251         //cout << "In FGATCMgr::update - atc_list.size = " << atc_list.size() << '\n';
252         if(atc_list.size()) {
253                 if(atc_list_itr == atc_list.end()) {
254                         atc_list_itr = atc_list.begin();
255                 }
256                 (*atc_list_itr)->Update();
257                 ++atc_list_itr;
258         }
259         
260         // Search the tuned frequencies every now and then - this should be done with the event scheduler
261         static int i = 0;
262         if(i == 15) {
263                 Search(1);
264         }
265         if(i == 30) {
266                 Search(2);
267                 i = 0;
268         }
269         ++i;
270 }
271 /*
272 // Remove from list only if not needed by the AI system
273 void FGATCMgr::CommRemoveFromList(const char* id, atc_type tp) {
274         AirportATC a;
275         if(GetAirportATCDetails((string)id, &a)) {
276                 if(a.set_by_AI) {
277                         // Don't remove
278                         a.set_by_comm_search = false;
279                         airport_atc_map[(string)id] = a;
280                         return;
281                 } else {
282                         // remove
283                 }
284         }
285 }
286 */    
287
288 // Remove from list - should only be called from above or similar
289 void FGATCMgr::RemoveFromList(const char* id, atc_type tp) {
290         //cout << "Requested type = " << tp << '\n';
291         //cout << "id = " << id << '\n';
292         atc_list_itr = atc_list.begin();
293         while(atc_list_itr != atc_list.end()) {
294                 //cout << "type = " << (*atc_list_itr)->GetType() << '\n';
295                 //cout << "Ident = " << (*atc_list_itr)->GetIdent() << '\n';
296                 if( (!strcmp((*atc_list_itr)->GetIdent(), id))
297                         && ((*atc_list_itr)->GetType() == tp) ) {
298                                 //Before removing it stop it transmitting!!
299                                 //cout << "OBLITERATING FROM LIST!!!\n";
300                                 (*atc_list_itr)->SetNoDisplay();
301                                 (*atc_list_itr)->Update();
302                                 delete (*atc_list_itr);
303                                 atc_list_itr = atc_list.erase(atc_list_itr);
304                                 break;
305                         }  // Note that that can upset where we are in the list but that doesn't really matter
306                 ++atc_list_itr;
307         }
308 }
309
310
311 //DCL - this routine untested so far.
312 // Find in list - return a currently active ATC pointer given ICAO code and type
313 FGATC* FGATCMgr::FindInList(const char* id, atc_type tp) {
314         atc_list_itr = atc_list.begin();
315         while(atc_list_itr != atc_list.end()) {
316                 if( (!strcmp((*atc_list_itr)->GetIdent(), id))
317                 && ((*atc_list_itr)->GetType() == tp) ) {
318                         return(*atc_list_itr);
319                 }       // Note that that can upset where we are in the list but that shouldn't really matter
320                 ++atc_list_itr;
321         }
322         // We need a fallback position
323         SG_LOG(SG_GENERAL, SG_ALERT, "*** Failed to find FGATC* in FGATCMgr::FindInList - this should not happen!");
324         return(NULL);
325 }
326
327 // Returns true if the airport is found in the map
328 bool FGATCMgr::GetAirportATCDetails(string icao, AirportATC* a) {
329         if(airport_atc_map.find(icao) != airport_atc_map.end()) {
330                 *a = *airport_atc_map[icao];
331                 return(true);
332         } else {
333                 return(false);
334         }
335 }
336
337
338 // Return a pointer to a given sort of ATC at a given airport and activate if necessary
339 // ONLY CALL THIS FUNCTION AFTER FIRST CHECKING THE SERVICE EXISTS BY CALLING GetAirportATCDetails
340 // FIXME - we really ought to take out the necessity for two function calls by simply returning
341 // a NULL pointer if the service doesn't exist and requiring the caller to check for it (NULL).
342 FGATC* FGATCMgr::GetATCPointer(string icao, atc_type type) {
343         AirportATC *a = airport_atc_map[icao];
344         //cout << "a->lon = " << a->lon << '\n';
345         //cout << "a->elev = " << a->elev << '\n';
346         //cout << "a->tower_freq = " << a->tower_freq << '\n';
347         switch(type) {
348                 case TOWER:
349                 if(a->tower_active) {
350                         // Get the pointer from the list
351                         return(FindInList(icao.c_str(), type)); // DCL - this untested so far.
352                 } else {
353                         FGTower* t = new FGTower;
354                         if(current_towerlist->query(a->lon, a->lat, a->elev, a->tower_freq, &tower)) {
355                                 *t = tower;
356                                 atc_list.push_back(t);
357                                 a->tower_active = true;
358                                 airport_atc_map[icao] = a;
359                                 return(t);
360                         } else {
361                                 cout << "ERROR - tower that should exist in FGATCMgr::GetATCPointer for airport " << icao << " not found\n";
362                         }
363                 }
364                 break;
365                 // Lets add the rest to get rid of the compiler warnings even though we don't need them yet.
366                 case APPROACH:
367                 break;
368                 case ATIS:
369                 SG_LOG(SG_GENERAL, SG_ALERT, "ERROR - ATIS station should not be requested from FGATCMgr::GetATCPointer");
370                 break;
371                 case GROUND:
372                 break;
373                 case INVALID:
374                 break;
375                 case ENROUTE:
376                 break;
377                 case DEPARTURE:
378                 break;
379         }
380         
381         SG_LOG(SG_GENERAL, SG_ALERT, "ERROR IN FGATCMgr - reached end of GetATCPointer");
382         
383         return(NULL);
384 }
385
386
387 // Render a transmission
388 // Outputs the transmission either on screen or as audio depending on user preference
389 // The refname is a string to identify this sample to the sound manager
390 // The repeating flag indicates whether the message should be repeated continuously or played once.
391 void FGATCMgr::Render(string msg, string refname, bool repeating) {
392 #ifdef ENABLE_AUDIO_SUPPORT
393         voice = voiceOK && fgGetBool("/sim/sound/audible");
394         if(voice) {
395                 int len;
396                 unsigned char* buf = v1.WriteMessage((char*)msg.c_str(), len, voice);
397                 if(voice) {
398                         FGSimpleSound* simple = new FGSimpleSound(buf, len);
399                         simple->set_volume(2.0);
400                         globals->get_soundmgr()->add(simple, refname);
401                         if(repeating) {
402                                 globals->get_soundmgr()->play_looped(refname);
403                         } else {
404                                 globals->get_soundmgr()->play_once(refname);
405                         }
406                 }
407                 delete[] buf;
408         }
409 #endif  // ENABLE_AUDIO_SUPPORT
410         if(!voice) {
411                 // first rip the underscores and the pause hints out of the string - these are for the convienience of the voice parser
412                 for(unsigned int i = 0; i < msg.length(); ++i) {
413                         if((msg.substr(i,1) == "_") || (msg.substr(i,1) == "/")) {
414                                 msg[i] = ' ';
415                         }
416                 }
417                 globals->get_ATC_display()->RegisterRepeatingMessage(msg);
418         }
419         playing = true; 
420 }
421
422
423 // Cease rendering a transmission.
424 void FGATCMgr::NoRender(string refname) {
425         if(playing) {
426                 if(voice) {
427 #ifdef ENABLE_AUDIO_SUPPORT             
428                         globals->get_soundmgr()->stop(refname);
429                         globals->get_soundmgr()->remove(refname);
430 #endif
431                 } else {
432                         globals->get_ATC_display()->CancelRepeatingMessage();
433                 }
434                 playing = false;
435         }
436 }
437
438
439 // Display a dialog box with options relevant to the currently tuned ATC service.
440 void FGATCMgr::doStandardDialog() {
441         /* DCL 2002/12/06 - This function currently in development 
442            and dosen't display anything usefull to the end-user */
443         //cout << "FGATCMgr::doStandardDialog called..." << endl;
444         
445         // First - need to determine which ATC service (if any) the user is tuned to.
446         //cout << "comm1_type = " << comm1_type << endl;
447         
448         // Second - customise the dialog box
449         switch(comm1_type) {
450         case INVALID:
451                 atcDialogCommunicationOptions->newList(NULL);
452                 atcDialogMessage->setLabel("Not tuned in to any ATC service.");
453                 break;
454         case ATIS:
455                 atcDialogCommunicationOptions->newList(NULL);
456                 atcDialogMessage->setLabel("Tuned in to ATIS: no communication possible.");
457                 break;
458         case TOWER: 
459                 atcDialogCommunicationOptions->newList(towerOptions);
460                 atcDialogMessage->setLabel("Tuned in to Tower - select communication to transmit:");
461                 break;
462         case GROUND:
463                 atcDialogCommunicationOptions->newList(NULL);
464                 atcDialogMessage->setLabel("Tuned in to Ground - select communication to transmit:");
465                 break;
466         case APPROACH:
467                 atcDialogCommunicationOptions->newList(approachOptions);
468                 atcDialogMessage->setLabel("Tuned in to Approach - select communication to transmit:");
469                 break;
470         default:
471                 atcDialogCommunicationOptions->newList(NULL);
472                 atcDialogMessage->setLabel("Tuned in to unknown ATC service - enter transmission:");
473                 break;
474         }
475         
476         // Third - display the dialog without pausing sim.
477         ATCDialog(NULL);
478         
479         // Forth - need to direct input back from the dialog to the relevant ATC service.
480         // This is in ATCDialogOK()
481 }
482
483 ////////////////////////////////////////////////////////////////
484 //
485 // TODO - The whole ATC frequency storage and search is really
486 //        really ugly and needs reworking at some point.
487 //
488 ////////////////////////////////////////////////////////////////
489 // Search the specified comm channel (1 or 2)
490 void FGATCMgr::Search(int chan) {
491         
492         if(chan == 1) {
493                 ////////////////////////////////////////////////////////////////////////
494                 // Comm1.
495                 ////////////////////////////////////////////////////////////////////////
496                 //cout << "In FGATCMgr::Search() - atc_list.size = " << atc_list.size() << '\n';
497                 
498                 comm1_freq = comm1_node->getDoubleValue();
499                 //cout << "************* comm1_freq = " << comm1_freq << '\n';
500                 double lon = lon_node->getDoubleValue();
501                 double lat = lat_node->getDoubleValue();
502                 double elev = elev_node->getDoubleValue() * SG_FEET_TO_METER;
503                 
504                 // We must be able to generalise some of the repetetive searching below!
505                 
506                 //Search for ATIS first
507                 if(current_atislist->query(lon, lat, elev, comm1_freq, &atis)) {
508                         //cout << "atis found in radiostack search !!!!" << endl;
509                         //cout << "last_comm1_atis_ident = " << last_comm1_atis_ident << '\n';
510                         //cout << "comm1_type " << comm1_type << '\n';
511                         comm1_atis_ident = atis.GetIdent();
512                         comm1_atis_valid = true;
513                         if(last_comm1_atis_ident != comm1_atis_ident) {
514                                 if(last_comm1_atis_ident != "") {
515                                         RemoveFromList(last_comm1_atis_ident, ATIS);
516                                 }
517                                 last_comm1_atis_ident = comm1_atis_ident;
518                                 //cout << "last_comm1_atis_ident = " << last_comm1_atis_ident << '\n';
519                                 comm1_type = ATIS;
520                                 comm1_elev = atis.get_elev();
521                                 comm1_range = FG_ATIS_DEFAULT_RANGE;
522                                 comm1_effective_range = comm1_range;
523                                 comm1_x = atis.get_x();
524                                 comm1_y = atis.get_y();
525                                 comm1_z = atis.get_z();
526                                 FGATIS* a = new FGATIS;
527                                 *a = atis;
528                                 comm1_atc_ptr = a;
529                                 a->SetDisplay();
530                                 a->set_refname("atis1");
531                                 atc_list.push_back(a);
532                                 //cout << "Found a new atis station in range" << endl;
533                                 //cout << " id = " << atis.GetIdent() << endl;
534                                 return;  //This rather assumes that we never have more than one type of station in range.
535                         }
536                 } else {
537                         if(comm1_atis_valid) {
538                                 //cout << "Removing ATIS " << comm1_atis_ident << " from list\n";
539                                 RemoveFromList(comm1_atis_ident, ATIS);
540                                 comm1_atis_valid = false;
541                                 if(comm1_type == ATIS) {
542                                         comm1_type = INVALID;
543                                 }
544                                 comm1_atis_ident = "";
545                                 //comm1_trans_ident = "";
546                                 last_comm1_atis_ident = "";
547                                 comm1_atc_ptr = NULL;
548                         }
549                         //cout << "not picking up atis" << endl;
550                 }
551                 
552                 //Next search for tower
553                 //cout << "comm1_freq = " << comm1_freq << '\n';
554                 if(current_towerlist->query(lon, lat, elev, comm1_freq, &tower)) {
555                         //cout << "tower found in radiostack search !!!!" << endl;
556                         comm1_tower_ident = tower.GetIdent();
557                         //cout << "comm1_tower_ident = " << comm1_tower_ident << '\n';
558                         comm1_tower_valid = true;
559                         if(last_comm1_tower_ident != comm1_tower_ident) {
560                                 if(last_comm1_tower_ident != "") {
561                                         RemoveFromList(last_comm1_tower_ident, TOWER);
562                                 }
563                                 last_comm1_tower_ident = comm1_tower_ident;
564                                 comm1_type = TOWER;
565                                 comm1_elev = tower.get_elev();
566                                 comm1_range = FG_TOWER_DEFAULT_RANGE;
567                                 comm1_effective_range = comm1_range;
568                                 comm1_x = tower.get_x();
569                                 comm1_y = tower.get_y();
570                                 comm1_z = tower.get_z();
571                                 FGTower* t = new FGTower;
572                                 *t = tower;
573                                 comm1_atc_ptr = t;
574                                 t->SetDisplay();
575                                 atc_list.push_back(t);
576                                 //cout << "Found a new tower station in range" << endl;
577                                 //cout << " id = " << tower.GetIdent() << endl;
578                                 return;  //This rather assumes that we never have more than one type of station in range.
579                         }
580                 } else {
581                         if(comm1_tower_valid) {
582                                 //cout << "removing tower\n";
583                                 RemoveFromList(comm1_tower_ident, TOWER);
584                                 //comm1_valid = false;
585                                 if(comm1_type == TOWER) {
586                                         comm1_type = INVALID;   // Only invalidate if we haven't switched it to something else
587                                 }
588                                 comm1_tower_valid = false;
589                                 comm1_tower_ident = "";
590                                 last_comm1_tower_ident = "";
591                                 comm1_atc_ptr = NULL;
592                                 //comm1_ident = "";
593                                 //comm1_trans_ident = "";
594                                 //last_comm1_ident = "";
595                         }
596                         //cout << "not picking up tower" << endl;
597                 }
598                 /*
599                 //Next search for Ground control
600                 if(current_groundlist->query(lon, lat, elev, comm1_freq, &ground)) {
601                         //cout << "Ground Control found in radiostack search !!!!" << endl;
602                         comm1_ident = ground.GetIdent();
603                         comm1_valid = true;
604                         if((last_comm1_ident != comm1_ident) || (comm1_type != GROUND)) {
605                                 if(last_comm1_ident != "") {
606                                         RemoveFromList(last_comm1_ident, GROUND);
607                                 }
608                                 last_comm1_ident = comm1_ident;
609                                 comm1_type = GROUND;
610                                 comm1_elev = ground.get_elev();
611                                 comm1_range = FG_GROUND_DEFAULT_RANGE;
612                                 comm1_effective_range = comm1_range;
613                                 comm1_x = ground.get_x();
614                                 comm1_y = ground.get_y();
615                                 comm1_z = ground.get_z();
616                                 FGGround* g = new FGGround;
617                                 *g = ground;
618                                 g->SetDisplay();
619                                 atc_list.push_back(g);
620                                 // For now we will automatically make contact with ground when the radio is tuned.
621                                 // This rather assumes that the user tunes the radio at the appropriate place
622                                 // (ie. having just turned off the runway) and only uses ground control on arrival
623                                 // but its a start!
624                                 g->NewArrival(current_plane);
625                                 //cout << "Found a new ground station in range" << endl;
626                                 //cout << " id = " << ground.GetIdent() << endl;
627                                 return;  //This rather assumes that we never have more than one type of station in range.
628                         }
629                 } else {
630                         if((comm1_valid) && (comm1_type == GROUND)) {
631                                 RemoveFromList(comm1_ident, GROUND);
632                                 comm1_valid = false;
633                                 comm1_type = INVALID;
634                                 comm1_ident = "";
635                                 //comm1_trans_ident = "";
636                                 last_comm1_ident = "";
637                         }
638                         //cout << "not picking up ground control" << endl;
639                 }
640                 */
641                 // ================================================================================
642                 // Search for Approach stations
643                 // ================================================================================
644                 // init number of approach stations reachable by plane
645                 int  num_app = 0;
646                 
647                 // search stations in range
648                 current_approachlist->query_bck(lon, lat, elev, approaches, max_app, num_app);
649                 if (num_app != 0) {
650                         //cout << num_app << " approaches found in radiostack search !!!!" << endl;
651                         
652                         for ( int i=0; i<num_app; i++ ) {
653                                 bool new_app = true;
654                                 approach_ident = approaches[i].GetIdent();
655                                 
656                                 // check if station already exists on ATC stack
657                                 atc_list_itr = atc_list.begin();
658                                 while(atc_list_itr != atc_list.end()) {
659                                         //cout << "ATC list: " << (*atc_list_itr)->GetIdent() << endl;
660                                         if((!strcmp((*atc_list_itr)->GetIdent(), approach_ident))
661                                                 && ((*atc_list_itr)->GetType() == APPROACH) ) {
662                                                         new_app = false;
663                                                         string pid = "Player";
664                                                         (*atc_list_itr)->AddPlane(pid);
665                                                         (*atc_list_itr)->Update();
666                                                         break;
667                                                 }
668                                         ++atc_list_itr;
669                                 }
670                                 // generate new Approach on ATC stack
671                                 if (new_app) {
672                                         FGApproach* a = new FGApproach;
673                                         *a = approaches[i];
674                                         string pid = "Player";
675                                         a->AddPlane(pid);
676                                         a->Update();
677                                         a->SetDisplay();
678                                         comm1_atc_ptr = a;
679                                         atc_list.push_back(a);
680                                         //cout << "Found a new approach station in range: Id = " 
681                                         //     << approaches[i].GetIdent() << endl;
682                                 }
683                         }
684                 }
685                 
686                 // remove planes which are out of range
687                 atc_list_itr = atc_list.begin();
688                 while(atc_list_itr != atc_list.end()) {
689                         if((*atc_list_itr)->GetType() == APPROACH ) {
690                                 int np = (*atc_list_itr)->RemovePlane();
691                                 // if approach has no planes left remove it from ATC list
692                                 if ( np == 0) {
693                                         (*atc_list_itr)->SetNoDisplay();
694                                         (*atc_list_itr)->Update();
695                                         delete (*atc_list_itr);
696                                         atc_list_itr = atc_list.erase(atc_list_itr);
697                                         break;     // the other stations will be checked next time
698                                 }
699                         }
700                         ++atc_list_itr;
701                 }
702                 
703         } else {        // chan = 2
704                 
705                 ////////////////////////////////////////////////////////////////////////
706                 // Comm2.
707                 ////////////////////////////////////////////////////////////////////////
708                 //cout << "In FGATCMgr::Search() - atc_list.size = " << atc_list.size() << '\n';
709                 
710                 comm2_freq = comm2_node->getDoubleValue();
711                 //cout << "************* comm1_freq = " << comm1_freq << '\n';
712                 double lon = lon_node->getDoubleValue();
713                 double lat = lat_node->getDoubleValue();
714                 double elev = elev_node->getDoubleValue() * SG_FEET_TO_METER;
715                 
716                 if(current_atislist->query(lon, lat, elev, comm2_freq, &atis)) {
717                         comm2_atis_ident = atis.GetIdent();
718                         comm2_atis_valid = true;
719                         if(last_comm2_atis_ident != comm2_atis_ident) {
720                                 if(last_comm2_atis_ident != "") {
721                                         RemoveFromList(last_comm2_atis_ident, ATIS);
722                                 }
723                                 last_comm2_atis_ident = comm2_atis_ident;
724                                 comm2_type = ATIS;
725                                 comm2_elev = atis.get_elev();
726                                 comm2_range = FG_ATIS_DEFAULT_RANGE;
727                                 comm2_effective_range = comm2_range;
728                                 comm2_x = atis.get_x();
729                                 comm2_y = atis.get_y();
730                                 comm2_z = atis.get_z();
731                                 FGATIS* a = new FGATIS;
732                                 *a = atis;
733                                 comm2_atc_ptr = a;
734                                 a->SetDisplay();
735                                 a->set_refname("atis2");
736                                 atc_list.push_back(a);
737                                 //cout << "Found a new atis station in range" << endl;
738                                 //cout << " id = " << atis.GetIdent() << endl;
739                                 return;  //This rather assumes that we never have more than one type of station in range.
740                         }
741                 } else {
742                         if(comm2_atis_valid) {
743                                 RemoveFromList(comm2_atis_ident, ATIS);
744                                 comm2_atis_valid = false;
745                                 if(comm2_type == ATIS) {
746                                         comm2_type = INVALID;
747                                 }
748                                 comm2_atis_ident = "";
749                                 //comm2_trans_ident = "";
750                                 last_comm2_atis_ident = "";
751                                 comm2_atc_ptr = NULL;
752                         }
753                 }
754                 
755                 if(current_towerlist->query(lon, lat, elev, comm2_freq, &tower)) {
756                         //cout << "tower found in radiostack search !!!!" << endl;
757                         comm2_tower_ident = tower.GetIdent();
758                         comm2_tower_valid = true;
759                         if(last_comm2_tower_ident != comm2_tower_ident) {
760                                 if(last_comm2_tower_ident != "") {
761                                         RemoveFromList(last_comm2_tower_ident, TOWER);
762                                 }
763                                 last_comm2_tower_ident = comm2_tower_ident;
764                                 comm2_type = TOWER;
765                                 comm2_elev = tower.get_elev();
766                                 comm2_range = FG_TOWER_DEFAULT_RANGE;
767                                 comm2_effective_range = comm2_range;
768                                 comm2_x = tower.get_x();
769                                 comm2_y = tower.get_y();
770                                 comm2_z = tower.get_z();
771                                 FGTower* t = new FGTower;
772                                 *t = tower;
773                                 comm2_atc_ptr = t;
774                                 t->SetDisplay();
775                                 atc_list.push_back(t);
776                                 return;
777                         }
778                 } else {
779                         if(comm2_tower_valid) {
780                                 RemoveFromList(comm2_tower_ident, TOWER);
781                                 if(comm2_type == TOWER) {
782                                         comm2_type = INVALID;   // Only invalidate if we haven't switched it to something else
783                                 }
784                                 comm2_tower_valid = false;
785                                 comm2_tower_ident = "";
786                                 last_comm2_tower_ident = "";
787                                 comm2_atc_ptr = NULL;
788                         }
789                 }
790         }
791 }