1 // ATCmgr.cxx - Implementation of FGATCMgr - a global Flightgear ATC manager.
3 // Written by David Luff, started February 2002.
5 // Copyright (C) 2002 David C Luff - david.luff@nottingham.ac.uk
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.
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.
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.
21 //#include <Time/event.hxx>
23 #include <simgear/misc/sg_path.hxx>
24 #include <simgear/debug/logstream.hxx>
25 #include <Airports/simple.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"
38 // periodic radio station search wrapper
39 static void fgATCSearch( void ) {
40 globals->get_ATC_mgr()->Search();
42 */ //This wouldn't compile - including Time/event.hxx breaks it :-(
44 AirportATC::AirportATC() :
56 set_by_comm_search(false) {
59 FGATCMgr::FGATCMgr() {
62 last_comm_ident[0] = "";
63 last_comm_ident[1] = "";
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;
72 FGATCMgr::~FGATCMgr() {
76 void FGATCMgr::bind() {
79 void FGATCMgr::unbind() {
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();
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.
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 );
100 #ifdef ENABLE_AUDIO_SUPPORT
101 // Load all available voices.
102 // For now we'll do one hardwired one
105 voiceOK = v1->LoadVoice("default");
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. */
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;
124 SG_LOG(SG_GENERAL, SG_INFO, " ATC Dialog System");
125 current_atcdialog = new FGATCDialog;
126 current_atcdialog->Init();
131 //current_atcdialog->add_entry( "EGNX", "Request vectoring for approach", "Request Vectors" );
132 //current_atcdialog->add_entry( "EGNX", "Mayday, Mayday", "Declare Emergency" );
135 bool ok = current_commlist->FindByCode("KEMT", test, TOWER);
137 cout << "KEMT tower frequency from test was " << test.freq << endl;
139 cout << "KEMT tower frequency not found :-(" << endl;
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();
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;
162 // Search the tuned frequencies every now and then - this should be done with the event scheduler
168 //cout << "About to search(1)" << endl;
172 //cout << "About to search(2)" << endl;
178 //cout << "comm1 type = " << comm_type[0] << '\n';
182 // Returns frequency in KHz - should I alter this to return in MHz?
183 unsigned short int FGATCMgr::GetFrequency(string ident, atc_type tp) {
185 bool ok = current_commlist->FindByCode(ident, test, tp);
186 return(ok ? test.freq : 0);
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;
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.
213 airport_atc_map[ident] = a;
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!
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) {
232 if(GetAirportATCDetails((string)id, &a)) {
235 a.set_by_comm_search = false;
236 airport_atc_map[(string)id] = a;
243 // Hack - need to implement this properly
244 RemoveFromList(id, tp);
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);
266 } // Note that that can upset where we are in the list but that doesn't really matter
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
284 // If we get here it's not in the list
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];
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()) {
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';
311 if(a->tower_active) {
312 // Get the pointer from the list
313 return(FindInList(icao.c_str(), type));
316 if(current_commlist->FindByFreq(a->lon, a->lat, a->elev, a->tower_freq, &data, TOWER)) {
317 FGTower* t = new FGTower;
319 atc_list.push_back(t);
320 a->tower_active = true;
321 airport_atc_map[icao] = a;
325 cout << "ERROR - tower that should exist in FGATCMgr::GetATCPointer for airport " << icao << " not found\n";
332 SG_LOG(SG_GENERAL, SG_ALERT, "ERROR - ATIS station should not be requested from FGATCMgr::GetATCPointer");
335 if(a->ground_active) {
336 // Get the pointer from the list
337 return(FindInList(icao.c_str(), type));
340 if(current_commlist->FindByFreq(a->lon, a->lat, a->elev, a->ground_freq, &data, GROUND)) {
341 FGGround* g = new FGGround;
343 atc_list.push_back(g);
344 a->ground_active = true;
345 airport_atc_map[icao] = a;
349 cout << "ERROR - ground control that should exist in FGATCMgr::GetATCPointer for airport " << icao << " not found\n";
361 SG_LOG(SG_GENERAL, SG_ALERT, "ERROR IN FGATCMgr - reached end of GetATCPointer");
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.
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!!
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
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
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;
411 // Query the data store and get the closest match if any
412 if(current_commlist->FindByFreq(lon, lat, elev, freq, &data)) {
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
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);
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;
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;
443 comm_atc_ptr[chan] = a;
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);
450 // The station is already in the ATC list
453 // Generate the station and put in the ATC list
454 FGTower* t = new FGTower;
456 comm_atc_ptr[chan] = t;
458 atc_list.push_back(t);
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);
464 // The station is already in the ATC list
465 app->AddPlane("Player");
467 comm_atc_ptr[chan] = app;
469 // Generate the station and put in the ATC list
470 FGApproach* a = new FGApproach;
472 a->AddPlane("Player");
473 atc_list.push_back(a);
474 comm_atc_ptr[chan] = a;
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);
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;
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;
499 lon = lon_node->getDoubleValue();
500 lat = lat_node->getDoubleValue();
501 elev = elev_node->getDoubleValue() * SG_FEET_TO_METER;
503 // search stations in range
504 int num_app = current_commlist->FindByPos(lon, lat, elev, &approaches, APPROACH);
506 //cout << num_app << " approaches found in radiostack search !!!!" << endl;
508 for(app_itr = approaches.begin(); app_itr != approaches.end(); app_itr++) {
510 FGATC* app = FindInList((app_itr->ident).c_str(), app_itr->type);
512 // The station is already in the ATC list
513 app->AddPlane("Player");
516 // Generate the station and put in the ATC list
517 FGApproach* a = new FGApproach;
518 a->SetData(&(*app_itr));
519 a->AddPlane("Player");
521 atc_list.push_back(a);
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
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