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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
25 #include <simgear/misc/sg_path.hxx>
26 #include <simgear/debug/logstream.hxx>
27 #include <Airports/simple.hxx>
30 #include "commlist.hxx"
31 #include "ATCdisplay.hxx"
32 #include "ATCDialog.hxx"
33 #include "ATCutils.hxx"
34 #include "transmissionlist.hxx"
39 // periodic radio station search wrapper
40 static void fgATCSearch( void ) {
41 globals->get_ATC_mgr()->Search();
43 */ //This wouldn't compile - including Time/event.hxx breaks it :-(
44 // Is this still true?? -EMH-
46 AirportATC::AirportATC() :
58 //airport_atc_map.clear();
60 for(int i=0; i<ATC_NUM_TYPES; ++i) {
61 set_by_comm[0][i] = false;
62 set_by_comm[1][i] = false;
66 FGATCMgr::FGATCMgr() {
69 //last_comm_ident[0] = "";
70 //last_comm_ident[1] = "";
71 //approach_ident = "";
72 last_in_range = false;
73 comm_type[0] = INVALID;
74 comm_type[1] = INVALID;
75 comm_atc_ptr[0] = NULL;
76 comm_atc_ptr[1] = NULL;
77 comm_valid[0] = false;
78 comm_valid[1] = false;
83 FGATCMgr::~FGATCMgr() {
87 void FGATCMgr::bind() {
90 void FGATCMgr::unbind() {
93 void FGATCMgr::init() {
94 //cout << "ATCMgr::init called..." << endl;
96 comm_node[0] = fgGetNode("/instrumentation/comm[0]/frequencies/selected-mhz", true);
97 comm_node[1] = fgGetNode("/instrumentation/comm[1]/frequencies/selected-mhz", true);
98 lon_node = fgGetNode("/position/longitude-deg", true);
99 lat_node = fgGetNode("/position/latitude-deg", true);
100 elev_node = fgGetNode("/position/altitude-ft", true);
101 atc_list_itr = atc_list.begin();
103 // Search for connected ATC stations once per 0.8 seconds or so
104 // globals->get_event_mgr()->add( "fgATCSearch()", fgATCSearch,
105 // FGEvent::FG_EVENT_READY, 800);
107 // For some reason the above doesn't compile - including Time/event.hxx stops compilation.
108 // Is this still true after the reorganization of the event managar??
111 // Initialise the frequency search map
112 current_commlist = new FGCommList;
113 SGPath p_comm( globals->get_fg_root() );
114 current_commlist->init( p_comm );
116 #ifdef ENABLE_AUDIO_SUPPORT
117 // Load all available voices.
118 // For now we'll do one hardwired one
122 voiceOK = v1->LoadVoice("default");
124 } catch ( sg_io_exception & ) {
131 /* I've loaded the voice even if /sim/sound/pause is true
132 * since I know no way of forcing load of the voice if the user
133 * subsequently switches /sim/sound/audible to true.
134 * (which is the right thing to do -- CLO) :-) */
139 // Initialise the ATC Dialogs
140 //cout << "Initing Transmissions..." << endl;
141 SG_LOG(SG_ATC, SG_INFO, " ATC Transmissions");
142 current_transmissionlist = new FGTransmissionList;
143 SGPath p_transmission( globals->get_fg_root() );
144 p_transmission.append( "ATC/default.transmissions" );
145 current_transmissionlist->init( p_transmission );
146 //cout << "Done Transmissions" << endl;
148 SG_LOG(SG_ATC, SG_INFO, " ATC Dialog System");
149 current_atcdialog = new FGATCDialog;
150 current_atcdialog->Init();
153 //cout << "ATCmgr::init done!" << endl;
156 void FGATCMgr::update(double dt) {
159 SG_LOG(SG_ATC, SG_WARN, "Warning - ATCMgr::update(...) called before ATCMgr::init()");
162 current_atcdialog->Update(dt);
164 //cout << "Entering update..." << endl;
165 //Traverse the list of active stations.
166 //Only update one class per update step to avoid the whole ATC system having to calculate between frames.
167 //Eventually we should only update every so many steps.
168 //cout << "In FGATCMgr::update - atc_list.size = " << atc_list.size() << endl;
169 if(atc_list.size()) {
170 if(atc_list_itr == atc_list.end()) {
171 atc_list_itr = atc_list.begin();
173 //cout << "Updating " << (*atc_list_itr)->get_ident() << ' ' << (*atc_list_itr)->GetType() << '\n';
174 //cout << "Freq = " << (*atc_list_itr)->get_freq() << '\n';
175 (*atc_list_itr)->Update(dt * atc_list.size());
176 //cout << "Done ATC update..." << endl;
181 cout << "ATC_LIST: " << atc_list.size() << ' ';
182 for(atc_list_iterator it = atc_list.begin(); it != atc_list.end(); it++) {
183 cout << (*it)->get_ident() << ' ';
188 // Search the tuned frequencies every now and then - this should be done with the event scheduler
189 static int i = 0; // Very ugly - but there should only ever be one instance of FGATCMgr.
192 //cout << "About to AreaSearch()" << endl;
197 //cout << "About to search(1)" << endl;
201 //cout << "About to search(2)" << endl;
207 //cout << "comm1 type = " << comm_type[0] << '\n';
208 //cout << "Leaving update..." << endl;
212 // Returns frequency in KHz - should I alter this to return in MHz?
213 unsigned short int FGATCMgr::GetFrequency(const string& ident, const atc_type& tp) {
215 bool ok = current_commlist->FindByCode(ident, test, tp);
216 return(ok ? test.freq : 0);
220 // Register the fact that the AI system wants to activate an airport
221 // Might need more sophistication in this in the future - eg registration by aircraft call-sign.
222 bool FGATCMgr::AIRegisterAirport(const string& ident) {
223 SG_LOG(SG_ATC, SG_BULK, "AI registered airport " << ident << " with the ATC system");
224 //cout << "AI registered airport " << ident << " with the ATC system" << '\n';
225 if(airport_atc_map.find(ident) != airport_atc_map.end()) {
226 airport_atc_map[ident]->set_by_AI = true;
227 airport_atc_map[ident]->numAI++;
230 const FGAirport *ap = fgFindAirportID(ident);
232 //cout << "ident = " << ident << '\n';
233 AirportATC *a = new AirportATC;
234 // I'm not entirely sure that this AirportATC structure business is actually needed - it just duplicates what we can find out anyway!
235 a->lon = ap->getLongitude();
236 a->lat = ap->getLatitude();
237 a->elev = ap->getElevation();
238 a->atis_freq = GetFrequency(ident, ATIS);
239 //cout << "ATIS freq = " << a->atis_freq << '\n';
240 a->atis_active = false;
241 a->tower_freq = GetFrequency(ident, TOWER);
242 //cout << "Tower freq = " << a->tower_freq << '\n';
243 a->tower_active = false;
244 a->ground_freq = GetFrequency(ident, GROUND);
245 //cout << "Ground freq = " << a->ground_freq << '\n';
246 a->ground_active = false;
247 // TODO - some airports will have a tower/ground frequency but be inactive overnight.
250 airport_atc_map[ident] = a;
253 SG_LOG(SG_ATC, SG_ALERT, "ERROR - can't find airport " << ident << " in AIRegisterAirport(...)");
260 // Register the fact that the comm radio is tuned to an airport
261 // Channel is zero based
262 bool FGATCMgr::CommRegisterAirport(const string& ident, int chan, const atc_type& tp) {
263 SG_LOG(SG_ATC, SG_BULK, "Comm channel " << chan << " registered airport " << ident);
264 //cout << "Comm channel " << chan << " registered airport " << ident << ' ' << tp << '\n';
265 if(airport_atc_map.find(ident) != airport_atc_map.end()) {
266 //cout << "IN MAP - flagging set by comm..." << endl;
267 airport_atc_map[ident]->set_by_comm[chan][tp] = true;
269 airport_atc_map[ident]->atis_active = true;
270 } else if(tp == TOWER) {
271 airport_atc_map[ident]->tower_active = true;
272 } else if(tp == GROUND) {
273 airport_atc_map[ident]->ground_active = true;
274 } else if(tp == APPROACH) {
275 //a->approach_active = true;
276 } // TODO - there *must* be a better way to do this!!!
279 //cout << "NOT IN MAP - creating new..." << endl;
280 const FGAirport *ap = fgFindAirportID(ident);
282 AirportATC *a = new AirportATC;
283 // I'm not entirely sure that this AirportATC structure business is actually needed - it just duplicates what we can find out anyway!
284 a->lon = ap->getLongitude();
285 a->lat = ap->getLatitude();
286 a->elev = ap->getElevation();
287 a->atis_freq = GetFrequency(ident, ATIS);
288 a->atis_active = false;
289 a->tower_freq = GetFrequency(ident, TOWER);
290 a->tower_active = false;
291 a->ground_freq = GetFrequency(ident, GROUND);
292 a->ground_active = false;
294 a->atis_active = true;
295 } else if(tp == TOWER) {
296 a->tower_active = true;
297 } else if(tp == GROUND) {
298 a->ground_active = true;
299 } else if(tp == APPROACH) {
300 //a->approach_active = true;
301 } // TODO - there *must* be a better way to do this!!!
302 // TODO - some airports will have a tower/ground frequency but be inactive overnight.
303 a->set_by_AI = false;
305 a->set_by_comm[chan][tp] = true;
306 airport_atc_map[ident] = a;
314 // Remove from list only if not needed by the AI system or the other comm channel
315 // Note that chan is zero based.
316 void FGATCMgr::CommRemoveFromList(const string& id, const atc_type& tp, int chan) {
317 SG_LOG(SG_ATC, SG_BULK, "CommRemoveFromList called for airport " << id << " " << tp << " by channel " << chan);
318 //cout << "CommRemoveFromList called for airport " << id << " " << tp << " by channel " << chan << '\n';
319 if(airport_atc_map.find(id) != airport_atc_map.end()) {
320 AirportATC* a = airport_atc_map[id];
321 //cout << "In CommRemoveFromList, a->ground_freq = " << a->ground_freq << endl;
322 if(a->set_by_AI && tp != ATIS) {
323 // Set by AI, so don't remove simply because user isn't tuned in any more - just stop displaying
324 SG_LOG(SG_ATC, SG_BULK, "In CommRemoveFromList, service was set by AI\n");
325 FGATC* aptr = GetATCPointer(id, tp);
328 //cout << "chan 1\n";
329 a->set_by_comm[0][tp] = false;
330 if(!a->set_by_comm[1][tp]) {
331 //cout << "not set by comm2\n";
333 //cout << "Got pointer\n";
334 aptr->SetNoDisplay();
335 //cout << "Setting no display...\n";
337 //cout << "Not got pointer\n";
342 a->set_by_comm[1][tp] = false;
343 if(!a->set_by_comm[0][tp]) {
345 aptr->SetNoDisplay();
346 //cout << "Setting no display...\n";
351 //airport_atc_map[id] = a;
356 a->set_by_comm[0][tp] = false;
357 // Remove only if not also set by the other comm channel
358 if(!a->set_by_comm[1][tp]) {
359 a->tower_active = false;
360 a->ground_active = false;
361 RemoveFromList(id, tp);
365 a->set_by_comm[1][tp] = false;
366 if(!a->set_by_comm[0][tp]) {
367 a->tower_active = false;
368 a->ground_active = false;
369 RemoveFromList(id, tp);
378 // Remove from list - should only be called from above or similar
379 // This function *will* remove it from the list regardless of who else might want it.
380 void FGATCMgr::RemoveFromList(const string& id, const atc_type& tp) {
381 //cout << "FGATCMgr::RemoveFromList called..." << endl;
382 //cout << "Requested type = " << tp << endl;
383 //cout << "id = " << id << endl;
384 atc_list_iterator it = atc_list.begin();
385 while(it != atc_list.end()) {
386 //cout << "type = " << (*it)->GetType() << '\n';
387 //cout << "Ident = " << (*it)->get_ident() << '\n';
388 if( ((*it)->get_ident() == id)
389 && ((*it)->GetType() == tp) ) {
390 //Before removing it stop it transmitting!!
391 //cout << "OBLITERATING FROM LIST!!!\n";
392 (*it)->SetNoDisplay();
393 (*it)->Update(0.00833);
396 atc_list_itr = atc_list.begin(); // Reset the persistent itr incase we've left it off the end.
404 // Find in list - return a currently active ATC pointer given ICAO code and type
405 // Return NULL if the given service is not in the list
406 // - *** THE CALLING FUNCTION MUST CHECK FOR THIS ***
407 FGATC* FGATCMgr::FindInList(const string& id, const atc_type& tp) {
408 //cout << "Entering FindInList for " << id << ' ' << tp << endl;
409 atc_list_iterator it = atc_list.begin();
410 while(it != atc_list.end()) {
411 if( ((*it)->get_ident() == id)
412 && ((*it)->GetType() == tp) ) {
417 // If we get here it's not in the list
418 //cout << "Couldn't find it in the list though :-(" << endl;
422 // Returns true if the airport is found in the map
423 bool FGATCMgr::GetAirportATCDetails(const string& icao, AirportATC* a) {
424 if(airport_atc_map.find(icao) != airport_atc_map.end()) {
425 *a = *airport_atc_map[icao];
433 // Return a pointer to a given sort of ATC at a given airport and activate if necessary
434 // Returns NULL if service doesn't exist - calling function should check for this.
435 // We really ought to make this private and call it from the CommRegisterAirport / AIRegisterAirport functions
436 // - at the moment all these GetATC... functions exposed are just too complicated.
437 FGATC* FGATCMgr::GetATCPointer(const string& icao, const atc_type& type) {
438 if(airport_atc_map.find(icao) == airport_atc_map.end()) {
439 //cout << "Unable to find " << icao << ' ' << type << " in the airport_atc_map" << endl;
442 //cout << "In GetATCPointer, found " << icao << ' ' << type << endl;
443 AirportATC *a = airport_atc_map[icao];
444 //cout << "a->lon = " << a->lon << '\n';
445 //cout << "a->elev = " << a->elev << '\n';
446 //cout << "a->tower_freq = " << a->tower_freq << '\n';
449 if(a->tower_active) {
450 // Get the pointer from the list
451 return(FindInList(icao, type));
454 if(current_commlist->FindByFreq(a->lon, a->lat, a->elev, a->tower_freq, &data, TOWER)) {
455 FGTower* t = new FGTower;
457 atc_list.push_back(t);
458 a->tower_active = true;
459 airport_atc_map[icao] = a;
460 //cout << "Initing tower " << icao << " in GetATCPointer()\n";
464 SG_LOG(SG_ATC, SG_ALERT, "ERROR - tower that should exist in FGATCMgr::GetATCPointer for airport " << icao << " not found");
471 SG_LOG(SG_ATC, SG_ALERT, "ERROR - ATIS station should not be requested from FGATCMgr::GetATCPointer");
474 //cout << "IN CASE GROUND" << endl;
475 if(a->ground_active) {
476 // Get the pointer from the list
477 return(FindInList(icao, type));
480 if(current_commlist->FindByFreq(a->lon, a->lat, a->elev, a->ground_freq, &data, GROUND)) {
481 FGGround* g = new FGGround;
483 atc_list.push_back(g);
484 a->ground_active = true;
485 airport_atc_map[icao] = a;
489 SG_LOG(SG_ATC, SG_ALERT, "ERROR - ground control that should exist in FGATCMgr::GetATCPointer for airport " << icao << " not found");
501 SG_LOG(SG_ATC, SG_ALERT, "ERROR IN FGATCMgr - reached end of GetATCPointer");
502 //cout << "ERROR IN FGATCMgr - reached end of GetATCPointer" << endl;
507 // Return a pointer to an appropriate voice for a given type of ATC
508 // creating the voice if necessary - ie. make sure exactly one copy
509 // of every voice in use exists in memory.
511 // TODO - in the future this will get more complex and dole out country/airport
512 // specific voices, and possible make sure that the same voice doesn't get used
513 // at different airports in quick succession if a large enough selection are available.
514 FGATCVoice* FGATCMgr::GetVoicePointer(const atc_type& type) {
515 // TODO - implement me better - maintain a list of loaded voices and other voices!!
537 // Search for ATC stations by frequency
538 void FGATCMgr::FreqSearch(int channel) {
539 int chan = channel - 1; // Convert to zero-based for the arrays
542 double freq = comm_node[chan]->getDoubleValue();
543 lon = lon_node->getDoubleValue();
544 lat = lat_node->getDoubleValue();
545 elev = elev_node->getDoubleValue() * SG_FEET_TO_METER;
547 // Query the data store and get the closest match if any
548 if(current_commlist->FindByFreq(lon, lat, elev, freq, &data)) {
551 // If this channel not previously valid then easy - add ATC to list
552 // If this channel was valid then - Have we tuned to a different service?
553 // If so - de-register one and add the other
554 if(comm_valid[chan]) {
555 if((comm_ident[chan] == data.ident) && (comm_type[chan] == data.type)) {
556 // Then we're still tuned into the same service so do nought and return
559 // Something's changed - either the location or the service type
560 // We need to feed the channel in so we're not removing it if we're also tuned in on the other channel
561 CommRemoveFromList(comm_ident[chan], comm_type[chan], chan);
564 // At this point we can assume that we need to add the service.
565 comm_ident[chan] = data.ident;
566 comm_type[chan] = data.type;
567 comm_x[chan] = (double)data.x;
568 comm_y[chan] = (double)data.y;
569 comm_z[chan] = (double)data.z;
570 comm_lon[chan] = (double)data.lon;
571 comm_lat[chan] = (double)data.lat;
572 comm_elev[chan] = (double)data.elev;
573 comm_valid[chan] = true;
575 // This was a switch-case statement but the compiler didn't like the new variable creation with it.
576 if(comm_type[chan] == ATIS) {
577 CommRegisterAirport(comm_ident[chan], chan, ATIS);
578 FGATC* app = FindInList(comm_ident[chan], ATIS);
580 // The station is already in the ATC list
581 //cout << "In list - flagging SetDisplay..." << endl;
582 comm_atc_ptr[chan] = app;
585 // Generate the station and put in the ATC list
586 //cout << "Not in list - generating..." << endl;
587 FGATIS* a = new FGATIS;
589 comm_atc_ptr[chan] = a;
592 atc_list.push_back(a);
594 } else if (comm_type[chan] == TOWER) {
595 //cout << "TOWER TOWER TOWER\n";
596 CommRegisterAirport(comm_ident[chan], chan, TOWER);
597 //cout << "Done (TOWER)" << endl;
598 FGATC* app = FindInList(comm_ident[chan], TOWER);
600 // The station is already in the ATC list
601 SG_LOG(SG_GENERAL, SG_DEBUG, comm_ident[chan] << " is in list - flagging SetDisplay...");
602 //cout << comm_ident[chan] << " is in list - flagging SetDisplay...\n";
603 comm_atc_ptr[chan] = app;
606 // Generate the station and put in the ATC list
607 SG_LOG(SG_GENERAL, SG_DEBUG, comm_ident[chan] << " is not in list - generating...");
608 //cout << comm_ident[chan] << " is not in list - generating...\n";
609 FGTower* t = new FGTower;
611 comm_atc_ptr[chan] = t;
612 //cout << "Initing tower in FreqSearch()\n";
615 atc_list.push_back(t);
617 } else if (comm_type[chan] == GROUND) {
618 CommRegisterAirport(comm_ident[chan], chan, GROUND);
619 //cout << "Done (GROUND)" << endl;
620 FGATC* app = FindInList(comm_ident[chan], GROUND);
622 // The station is already in the ATC list
623 comm_atc_ptr[chan] = app;
626 // Generate the station and put in the ATC list
627 FGGround* g = new FGGround;
629 comm_atc_ptr[chan] = g;
632 atc_list.push_back(g);
634 } else if (comm_type[chan] == APPROACH) {
635 // We have to be a bit more carefull here since approaches are also searched by area
636 CommRegisterAirport(comm_ident[chan], chan, APPROACH);
637 //cout << "Done (APPROACH)" << endl;
638 FGATC* app = FindInList(comm_ident[chan], APPROACH);
640 // The station is already in the ATC list
641 app->AddPlane("Player");
643 comm_atc_ptr[chan] = app;
645 // Generate the station and put in the ATC list
646 FGApproach* a = new FGApproach;
648 comm_atc_ptr[chan] = a;
651 a->AddPlane("Player");
652 atc_list.push_back(a);
656 if(comm_valid[chan]) {
657 if(comm_type[chan] != APPROACH) {
658 // Currently approaches are removed by Alexander's out-of-range mechanism
659 CommRemoveFromList(comm_ident[chan], comm_type[chan], chan);
661 // Note that we *don't* call SetNoDisplay() here because the other comm channel
662 // might be tuned into the same station - this is handled by CommRemoveFromList(...)
663 comm_type[chan] = INVALID;
664 comm_atc_ptr[chan] = NULL;
665 comm_valid[chan] = false;
670 // Search ATC stations by area in order that we appear 'on the radar'
671 void FGATCMgr::AreaSearch() {
672 // Search for Approach stations
673 comm_list_type approaches;
674 comm_list_iterator app_itr;
676 lon = lon_node->getDoubleValue();
677 lat = lat_node->getDoubleValue();
678 elev = elev_node->getDoubleValue() * SG_FEET_TO_METER;
680 // search stations in range
681 int num_app = current_commlist->FindByPos(lon, lat, elev, 100.0, &approaches, APPROACH);
683 //cout << num_app << " approaches found in radiostack search !!!!" << endl;
685 for(app_itr = approaches.begin(); app_itr != approaches.end(); app_itr++) {
687 FGATC* app = FindInList(app_itr->ident, app_itr->type);
689 // The station is already in the ATC list
690 //cout << "In list adding player\n";
691 app->AddPlane("Player");
694 // Generate the station and put in the ATC list
695 FGApproach* a = new FGApproach;
696 a->SetData(&(*app_itr));
697 //cout << "Adding player\n";
698 a->AddPlane("Player");
700 atc_list.push_back(a);
705 // remove planes which are out of range
706 // TODO - I'm not entirely sure that this belongs here.
707 atc_list_iterator it = atc_list.begin();
708 while(it != atc_list.end()) {
709 if((*it)->GetType() == APPROACH ) {
710 int np = (*it)->RemovePlane();
711 // if approach has no planes left remove it from ATC list
713 //cout << "REMOVING AN APPROACH STATION WITH NO PLANES..." << endl;
714 (*it)->SetNoDisplay();
715 (*it)->Update(0.00833);
718 atc_list_itr = atc_list.begin(); // Reset the persistent itr incase we've left it off the end.
719 break; // the other stations will be checked next time