]> git.mxchange.org Git - flightgear.git/blob - src/ATC/ATCmgr.cxx
Need to call Init() when initialising tower, otherwise bad things happen...
[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/debug/logstream.hxx>
25 #include <Airports/simple.hxx>
26
27 #include "ATCmgr.hxx"
28 #include "commlist.hxx"
29 #include "ATCdisplay.hxx"
30 #include "ATCDialog.hxx"
31 #include "ATCutils.hxx"
32
33 /*
34 // periodic radio station search wrapper
35 static void fgATCSearch( void ) {
36         globals->get_ATC_mgr()->Search();
37 }
38 */ //This wouldn't compile - including Time/event.hxx breaks it :-(
39
40 AirportATC::AirportATC() :
41     lon(0.0),
42     lat(0.0),
43     elev(0.0),
44     atis_freq(0.0),
45     atis_active(false),
46     tower_freq(0.0),
47     tower_active(false),
48     ground_freq(0.0),
49     ground_active(false),
50     set_by_AI(false),
51         numAI(0),
52     set_by_comm_search(false) {
53 }
54
55 FGATCMgr::FGATCMgr() {
56         comm_ident[0] = "";
57         comm_ident[1] = "";
58         last_comm_ident[0] = "";
59         last_comm_ident[1] = "";
60         approach_ident = "";
61         last_in_range = false;
62         comm_type[0] = INVALID;
63         comm_type[1] = INVALID;
64         comm_atc_ptr[0] = NULL;
65         comm_atc_ptr[1] = NULL;
66 }
67
68 FGATCMgr::~FGATCMgr() {
69         delete v1;
70 }
71
72 void FGATCMgr::bind() {
73 }
74
75 void FGATCMgr::unbind() {
76 }
77
78 void FGATCMgr::init() {
79         comm_node[0] = fgGetNode("/radios/comm[0]/frequencies/selected-mhz", true);
80         comm_node[1] = fgGetNode("/radios/comm[1]/frequencies/selected-mhz", true);
81         lon_node = fgGetNode("/position/longitude-deg", true);
82         lat_node = fgGetNode("/position/latitude-deg", true);
83         elev_node = fgGetNode("/position/altitude-ft", true);
84         atc_list_itr = atc_list.begin();
85         
86         // Search for connected ATC stations once per 0.8 seconds or so
87         // global_events.Register( "fgATCSearch()", fgATCSearch,
88         //                  fgEVENT::FG_EVENT_READY, 800);
89         // For some reason the above doesn't compile - including Time/event.hxx stops compilation.
90         
91         // Initialise the frequency search map
92     current_commlist = new FGCommList;
93     SGPath p_comm( globals->get_fg_root() );
94     current_commlist->init( p_comm );
95
96 #ifdef ENABLE_AUDIO_SUPPORT     
97         // Load all available voices.
98         // For now we'll do one hardwired one
99         
100         v1 = new FGATCVoice;
101         voiceOK = v1->LoadVoice("default");
102         voice = true;
103         
104         /* I've loaded the voice even if /sim/sound/audible is false
105         *  since I know no way of forcing load of the voice if the user
106         *  subsequently switches /sim/sound/audible to true. */
107 #else
108         voice = false;
109 #endif
110
111         // Initialise the ATC Dialogs
112         //cout << "Initing Transmissions..." << endl;
113     SG_LOG(SG_GENERAL, SG_INFO, "  ATC Transmissions");
114     current_transmissionlist = new FGTransmissionList;
115     SGPath p_transmission( globals->get_fg_root() );
116     p_transmission.append( "ATC/default.transmissions" );
117     current_transmissionlist->init( p_transmission );
118         //cout << "Done Transmissions" << endl;
119
120     SG_LOG(SG_GENERAL, SG_INFO, "  ATC Dialog System");
121     current_atcdialog = new FGATCDialog;
122     current_atcdialog->Init();
123
124         ATCDialogInit();
125         
126         // DCL - testing
127         //current_atcdialog->add_entry( "EGNX", "Request vectoring for approach", "Request Vectors" );
128         //current_atcdialog->add_entry( "EGNX", "Mayday, Mayday", "Declare Emergency" );
129         /*
130         ATCData test;
131         bool ok = current_commlist->FindByCode("KEMT", test, TOWER);
132         if(ok) {
133                 cout << "KEMT tower frequency from test was " << test.freq << endl;
134         } else {
135                 cout << "KEMT tower frequency not found :-(" << endl;
136         }
137         exit(-1);
138         */
139 }
140
141 void FGATCMgr::update(double dt) {
142         //cout << "Entering update..." << endl;
143         //Traverse the list of active stations.
144         //Only update one class per update step to avoid the whole ATC system having to calculate between frames.
145         //Eventually we should only update every so many steps.
146         //cout << "In FGATCMgr::update - atc_list.size = " << atc_list.size() << '\n';
147         if(atc_list.size()) {
148                 if(atc_list_itr == atc_list.end()) {
149                         atc_list_itr = atc_list.begin();
150                 }
151                 //cout << "Updating " << (*atc_list_itr)->get_ident() << ' ' << (*atc_list_itr)->GetType() << '\n';
152                 //cout << "Freq = " << (*atc_list_itr)->get_freq() << '\n';
153                 (*atc_list_itr)->Update();
154                 //cout << "Done ATC update..." << endl;
155                 ++atc_list_itr;
156         }
157         
158         // Search the tuned frequencies every now and then - this should be done with the event scheduler
159         static int i = 0;
160         if(i == 7) {
161                 AreaSearch();
162         }
163         if(i == 15) {
164                 //cout << "About to search(1)" << endl;
165                 FreqSearch(1);
166         }
167         if(i == 30) {
168                 //cout << "About to search(2)" << endl;
169                 FreqSearch(2);
170                 i = 0;
171         }
172         ++i;
173         
174         //cout << "comm1 type = " << comm_type[0] << '\n';
175 }
176
177
178 // Returns frequency in KHz - should I alter this to return in MHz?
179 unsigned short int FGATCMgr::GetFrequency(string ident, atc_type tp) {
180         ATCData test;
181         bool ok = current_commlist->FindByCode(ident, test, tp);
182         return(ok ? test.freq : 0);
183 }       
184
185
186 // Register the fact that the AI system wants to activate an airport
187 // Might need more sophistication in this in the future - eg registration by aircraft call-sign.
188 bool FGATCMgr::AIRegisterAirport(string ident) {
189         if(airport_atc_map.find(ident) != airport_atc_map.end()) {
190                 airport_atc_map[ident]->set_by_AI = true;
191                 return(true);
192         } else {
193                 FGAirport ap;
194                 if(dclFindAirportID(ident, &ap)) {
195                         AirportATC *a = new AirportATC;
196                         // I'm not entirely sure that this AirportATC structure business is actually needed - it just duplicates what we can find out anyway!
197                         a->lon = ap.longitude;
198                         a->lat = ap.latitude;
199                         a->elev = ap.elevation;
200                         a->atis_freq = GetFrequency(ident, ATIS);
201                         a->atis_active = false;
202                         a->tower_freq = GetFrequency(ident, TOWER);
203                         a->tower_active = false;
204                         a->ground_freq = GetFrequency(ident, GROUND);
205                         a->ground_active = false;
206                         // TODO - some airports will have a tower/ground frequency but be inactive overnight.
207                         a->set_by_AI = true;
208                         a->numAI++;
209                         airport_atc_map[ident] = a;
210                         return(true);
211                 }
212         }
213         return(false);
214 }
215
216
217 // Register the fact that the comm radio is tuned to an airport
218 bool FGATCMgr::CommRegisterAirport(string ident) {      // Later we'll differentiate between comm 1 and comm2
219         // TODO - implement me!
220         return(false);
221 }
222
223
224 // Remove from list only if not needed by the AI system or the other comm channel
225 // TODO - implement me!!
226 void FGATCMgr::CommRemoveFromList(const char* id, atc_type tp, int chan) {
227         /*AirportATC a;
228         if(GetAirportATCDetails((string)id, &a)) {
229                 if(a.set_by_AI) {
230                         // Don't remove
231                         a.set_by_comm_search = false;
232                         airport_atc_map[(string)id] = a;
233                         return;
234                 } else {
235                         // remove
236                 }
237         }*/
238         
239         // Hack - need to implement this properly
240         RemoveFromList(id, tp);
241 }
242     
243
244 // Remove from list - should only be called from above or similar
245 // This function *will* remove it from the list regardless of who else might want it.
246 void FGATCMgr::RemoveFromList(const char* id, atc_type tp) {
247         //cout << "Requested type = " << tp << '\n';
248         //cout << "id = " << id << '\n';
249         atc_list_itr = atc_list.begin();
250         while(atc_list_itr != atc_list.end()) {
251                 //cout << "type = " << (*atc_list_itr)->GetType() << '\n';
252                 //cout << "Ident = " << (*atc_list_itr)->get_ident() << '\n';
253                 if( (!strcmp((*atc_list_itr)->get_ident(), id))
254                         && ((*atc_list_itr)->GetType() == tp) ) {
255                                 //Before removing it stop it transmitting!!
256                                 //cout << "OBLITERATING FROM LIST!!!\n";
257                                 (*atc_list_itr)->SetNoDisplay();
258                                 (*atc_list_itr)->Update();
259                                 delete (*atc_list_itr);
260                                 atc_list_itr = atc_list.erase(atc_list_itr);
261                                 break;
262                         }  // Note that that can upset where we are in the list but that doesn't really matter
263                 ++atc_list_itr;
264         }
265 }
266
267
268 // Find in list - return a currently active ATC pointer given ICAO code and type
269 // Return NULL if the given service is not in the list
270 // - *** THE CALLING FUNCTION MUST CHECK FOR THIS ***
271 FGATC* FGATCMgr::FindInList(const char* id, atc_type tp) {
272         atc_list_itr = atc_list.begin();
273         while(atc_list_itr != atc_list.end()) {
274                 if( (!strcmp((*atc_list_itr)->get_ident(), id))
275                 && ((*atc_list_itr)->GetType() == tp) ) {
276                         return(*atc_list_itr);
277                 }       // Note that that can upset where we are in the list but that shouldn't really matter
278                 ++atc_list_itr;
279         }
280         // If we get here it's not in the list
281         return(NULL);
282 }
283
284 // Returns true if the airport is found in the map
285 bool FGATCMgr::GetAirportATCDetails(string icao, AirportATC* a) {
286         if(airport_atc_map.find(icao) != airport_atc_map.end()) {
287                 *a = *airport_atc_map[icao];
288                 return(true);
289         } else {
290                 return(false);
291         }
292 }
293
294
295 // Return a pointer to a given sort of ATC at a given airport and activate if necessary
296 // Returns NULL if service doesn't exist - calling function should check for this.
297 FGATC* FGATCMgr::GetATCPointer(string icao, atc_type type) {
298         if(airport_atc_map.find(icao) == airport_atc_map.end()) {
299                 return NULL;
300         }
301         AirportATC *a = airport_atc_map[icao];
302         //cout << "a->lon = " << a->lon << '\n';
303         //cout << "a->elev = " << a->elev << '\n';
304         //cout << "a->tower_freq = " << a->tower_freq << '\n';
305         switch(type) {
306                 case TOWER:
307                 if(a->tower_active) {
308                         // Get the pointer from the list
309                         return(FindInList(icao.c_str(), type));
310                 } else {
311                         ATCData data;
312                         if(current_commlist->FindByFreq(a->lon, a->lat, a->elev, a->tower_freq, &data, TOWER)) {
313                                 FGTower* t = new FGTower;
314                                 t->SetData(&data);
315                                 atc_list.push_back(t);
316                                 a->tower_active = true;
317                                 airport_atc_map[icao] = a;
318                                 t->Init();
319                                 return(t);
320                         } else {
321                                 cout << "ERROR - tower that should exist in FGATCMgr::GetATCPointer for airport " << icao << " not found\n";
322                         }
323                 }
324                 break;
325                 case APPROACH:
326                 break;
327                 case ATIS:
328                 SG_LOG(SG_GENERAL, SG_ALERT, "ERROR - ATIS station should not be requested from FGATCMgr::GetATCPointer");
329                 break;
330                 case GROUND:
331                 if(a->ground_active) {
332                         // Get the pointer from the list
333                         return(FindInList(icao.c_str(), type));
334                 } else {
335                         ATCData data;
336                         if(current_commlist->FindByFreq(a->lon, a->lat, a->elev, a->ground_freq, &data, GROUND)) {
337                                 FGGround* g = new FGGround;
338                                 g->SetData(&data);
339                                 atc_list.push_back(g);
340                                 a->ground_active = true;
341                                 airport_atc_map[icao] = a;
342                                 g->Init();
343                                 return(g);
344                         } else {
345                                 cout << "ERROR - ground control that should exist in FGATCMgr::GetATCPointer for airport " << icao << " not found\n";
346                         }
347                 }
348                 break;
349                 case INVALID:
350                 break;
351                 case ENROUTE:
352                 break;
353                 case DEPARTURE:
354                 break;
355         }
356         
357         SG_LOG(SG_GENERAL, SG_ALERT, "ERROR IN FGATCMgr - reached end of GetATCPointer");
358         
359         return(NULL);
360 }
361
362 // Return a pointer to an appropriate voice for a given type of ATC
363 // creating the voice if necessary - ie. make sure exactly one copy
364 // of every voice in use exists in memory.
365 //
366 // TODO - in the future this will get more complex and dole out country/airport
367 // specific voices, and possible make sure that the same voice doesn't get used
368 // at different airports in quick succession if a large enough selection are available.
369 FGATCVoice* FGATCMgr::GetVoicePointer(atc_type type) {
370         // TODO - implement me better - maintain a list of loaded voices and other voices!!
371         if(voice) {
372                 switch(type) {
373                 case ATIS:
374                         if(voiceOK) {
375                                 return(v1);
376                         }
377                 case TOWER:
378                         return(NULL);
379                 case APPROACH:
380                         return(NULL);
381                 case GROUND:
382                         return(NULL);
383                 default:
384                         return(NULL);
385                 }
386                 return(NULL);
387         } else {
388                 return(NULL);
389         }
390 }
391
392 // Display a dialog box with options relevant to the currently tuned ATC service.
393 void FGATCMgr::doPopupDialog() {
394         ATCDoDialog(comm_type[0]);      // FIXME - currently hardwired to comm1
395 }
396
397 // Search for ATC stations by frequency
398 void FGATCMgr::FreqSearch(int channel) {
399         int chan = channel - 1;         // Convert to zero-based for the arrays
400
401         ATCData data;   
402         double freq = comm_node[chan]->getDoubleValue();
403         lon = lon_node->getDoubleValue();
404         lat = lat_node->getDoubleValue();
405         elev = elev_node->getDoubleValue() * SG_FEET_TO_METER;
406         
407         // Query the data store and get the closest match if any
408         if(current_commlist->FindByFreq(lon, lat, elev, freq, &data)) {
409                 // We have a match
410                 // What's the logic?
411                 // If this channel not previously valid then easy - add ATC to list
412                 // If this channel was valid then - Have we tuned to a different service?
413                 // If so - de-register one and add the other
414                 if(comm_valid[chan]) {
415                         if((comm_ident[chan] == data.ident) && (comm_type[chan] == data.type)) {
416                                 // Then we're still tuned into the same service so do nought and return
417                                 return;
418                         } else {
419                                 // Something's changed - either the location or the service type
420                                 // We need to feed the channel in so we're not removing it if we're also tuned in on the other channel
421                                 CommRemoveFromList(comm_ident[chan], comm_type[chan], chan);
422                         }
423                 }
424                 // At this point we can assume that we need to add the service.
425                 comm_ident[chan] = (data.ident).c_str();
426                 comm_type[chan] = data.type;
427                 comm_x[chan] = (double)data.x;
428                 comm_y[chan] = (double)data.y;
429                 comm_z[chan] = (double)data.z;
430                 comm_lon[chan] = (double)data.lon;
431                 comm_lat[chan] = (double)data.lat;
432                 comm_elev[chan] = (double)data.elev;
433                 comm_valid[chan] = true;
434                 
435                 // This was a switch-case statement but the compiler didn't like the new variable creation with it. 
436                 if(comm_type[chan] == ATIS) {
437                         FGATIS* a = new FGATIS;
438                         a->SetData(&data);
439                         comm_atc_ptr[chan] = a;
440                         a->SetDisplay();
441                         //a->set_refname((chan) ? "atis2" : "atis1");           // FIXME - that line is limited to 2 channels
442                         atc_list.push_back(a);
443                 } else if (comm_type[chan] == TOWER) {
444                         FGATC* app = FindInList(comm_ident[chan], TOWER);
445                         if(app != NULL) {
446                                 // The station is already in the ATC list
447                                 app->SetDisplay();
448                         } else {
449                                 // Generate the station and put in the ATC list
450                                 FGTower* t = new FGTower;
451                                 t->SetData(&data);
452                                 comm_atc_ptr[chan] = t;
453                                 t->SetDisplay();
454                                 t->Init();
455                                 atc_list.push_back(t);
456                         }
457                 } else if (comm_type[chan] == APPROACH) {
458                         // We have to be a bit more carefull here since approaches are also searched by area
459                         FGATC* app = FindInList(comm_ident[chan], APPROACH);
460                         if(app != NULL) {
461                                 // The station is already in the ATC list
462                                 app->AddPlane("Player");
463                                 app->SetDisplay();
464                                 comm_atc_ptr[chan] = app;
465                         } else {
466                                 // Generate the station and put in the ATC list
467                                 FGApproach* a = new FGApproach;
468                                 a->SetData(&data);
469                                 a->AddPlane("Player");
470                                 atc_list.push_back(a);
471                                 comm_atc_ptr[chan] = a;
472                         }                       
473                 }
474         } else {
475                 if(comm_valid[chan]) {
476                         if(comm_type[chan] != APPROACH) {
477                                 // Currently approaches are removed by Alexander's out-of-range mechanism
478                                 CommRemoveFromList(comm_ident[chan], comm_type[chan], chan);
479                         }
480                         // Note that we *don't* call SetNoDisplay() here because the other comm channel
481                         // might be tuned into the same station - this is handled by CommRemoveFromList(...)
482                         comm_type[chan] = INVALID;
483                         comm_atc_ptr[chan] = NULL;
484                         comm_valid[chan] = false;
485                 }
486         }
487 }
488
489
490 // Search ATC stations by area in order that we appear 'on the radar'
491 void FGATCMgr::AreaSearch() {
492         // Search for Approach stations
493         comm_list_type approaches;
494         comm_list_iterator app_itr;
495         
496         lon = lon_node->getDoubleValue();
497         lat = lat_node->getDoubleValue();
498         elev = elev_node->getDoubleValue() * SG_FEET_TO_METER;
499         
500         // search stations in range
501         int num_app = current_commlist->FindByPos(lon, lat, elev, 100.0, &approaches, APPROACH);
502         if (num_app != 0) {
503                 //cout << num_app << " approaches found in radiostack search !!!!" << endl;
504                 
505                 for(app_itr = approaches.begin(); app_itr != approaches.end(); app_itr++) {
506                         
507                         FGATC* app = FindInList((app_itr->ident).c_str(), app_itr->type);
508                         if(app != NULL) {
509                                 // The station is already in the ATC list
510                                 app->AddPlane("Player");
511                                 //app->Update();
512                         } else {
513                                 // Generate the station and put in the ATC list
514                                 FGApproach* a = new FGApproach;
515                                 a->SetData(&(*app_itr));
516                                 a->AddPlane("Player");
517                                 //a->Update();
518                                 atc_list.push_back(a);
519                         }
520                 }
521         }
522         
523         // remove planes which are out of range
524         // TODO - I'm not entirely sure that this belongs here.
525         atc_list_itr = atc_list.begin();
526         while(atc_list_itr != atc_list.end()) {
527                 if((*atc_list_itr)->GetType() == APPROACH ) {
528                         int np = (*atc_list_itr)->RemovePlane();
529                         // if approach has no planes left remove it from ATC list
530                         if ( np == 0) {
531                                 (*atc_list_itr)->SetNoDisplay();
532                                 (*atc_list_itr)->Update();
533                                 delete (*atc_list_itr);
534                                 atc_list_itr = atc_list.erase(atc_list_itr);
535                                 break;     // the other stations will be checked next time
536                         }
537                 }
538                 ++atc_list_itr;
539         }
540 }