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