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>
29 #include "commlist.hxx"
30 #include "ATCdisplay.hxx"
31 #include "ATCDialog.hxx"
32 #include "ATCutils.hxx"
35 // periodic radio station search wrapper
36 static void fgATCSearch( void ) {
37 globals->get_ATC_mgr()->Search();
39 */ //This wouldn't compile - including Time/event.hxx breaks it :-(
40 // Is this still true?? -EMH-
42 AirportATC::AirportATC() :
54 //airport_atc_map.clear();
56 for(int i=0; i<ATC_NUM_TYPES; ++i) {
57 set_by_comm[0][i] = false;
58 set_by_comm[1][i] = false;
62 FGATCMgr::FGATCMgr() {
65 //last_comm_ident[0] = "";
66 //last_comm_ident[1] = "";
67 //approach_ident = "";
68 last_in_range = false;
69 comm_type[0] = INVALID;
70 comm_type[1] = INVALID;
71 comm_atc_ptr[0] = NULL;
72 comm_atc_ptr[1] = NULL;
73 comm_valid[0] = false;
74 comm_valid[1] = false;
79 FGATCMgr::~FGATCMgr() {
83 void FGATCMgr::bind() {
86 void FGATCMgr::unbind() {
89 void FGATCMgr::init() {
90 //cout << "ATCMgr::init called..." << endl;
92 comm_node[0] = fgGetNode("/instrumentation/comm[0]/frequencies/selected-mhz", true);
93 comm_node[1] = fgGetNode("/instrumentation/comm[1]/frequencies/selected-mhz", true);
94 lon_node = fgGetNode("/position/longitude-deg", true);
95 lat_node = fgGetNode("/position/latitude-deg", true);
96 elev_node = fgGetNode("/position/altitude-ft", true);
97 atc_list_itr = atc_list.begin();
99 // Search for connected ATC stations once per 0.8 seconds or so
100 // globals->get_event_mgr()->add( "fgATCSearch()", fgATCSearch,
101 // FGEvent::FG_EVENT_READY, 800);
103 // For some reason the above doesn't compile - including Time/event.hxx stops compilation.
104 // Is this still true after the reorganization of the event managar??
107 // Initialise the frequency search map
108 current_commlist = new FGCommList;
109 SGPath p_comm( globals->get_fg_root() );
110 current_commlist->init( p_comm );
112 #ifdef ENABLE_AUDIO_SUPPORT
113 // Load all available voices.
114 // For now we'll do one hardwired one
118 voiceOK = v1->LoadVoice("default");
120 } catch ( sg_io_exception & ) {
127 /* I've loaded the voice even if /sim/sound/pause is true
128 * since I know no way of forcing load of the voice if the user
129 * subsequently switches /sim/sound/audible to true.
130 * (which is the right thing to do -- CLO) :-) */
135 // Initialise the ATC Dialogs
136 //cout << "Initing Transmissions..." << endl;
137 SG_LOG(SG_ATC, SG_INFO, " ATC Transmissions");
138 current_transmissionlist = new FGTransmissionList;
139 SGPath p_transmission( globals->get_fg_root() );
140 p_transmission.append( "ATC/default.transmissions" );
141 current_transmissionlist->init( p_transmission );
142 //cout << "Done Transmissions" << endl;
144 SG_LOG(SG_ATC, SG_INFO, " ATC Dialog System");
145 current_atcdialog = new FGATCDialog;
146 current_atcdialog->Init();
149 //cout << "ATCmgr::init done!" << endl;
152 void FGATCMgr::update(double dt) {
155 SG_LOG(SG_ATC, SG_WARN, "Warning - ATCMgr::update(...) called before ATCMgr::init()");
158 current_atcdialog->Update(dt);
160 //cout << "Entering update..." << endl;
161 //Traverse the list of active stations.
162 //Only update one class per update step to avoid the whole ATC system having to calculate between frames.
163 //Eventually we should only update every so many steps.
164 //cout << "In FGATCMgr::update - atc_list.size = " << atc_list.size() << endl;
165 if(atc_list.size()) {
166 if(atc_list_itr == atc_list.end()) {
167 atc_list_itr = atc_list.begin();
169 //cout << "Updating " << (*atc_list_itr)->get_ident() << ' ' << (*atc_list_itr)->GetType() << '\n';
170 //cout << "Freq = " << (*atc_list_itr)->get_freq() << '\n';
171 (*atc_list_itr)->Update(dt * atc_list.size());
172 //cout << "Done ATC update..." << endl;
177 cout << "ATC_LIST: " << atc_list.size() << ' ';
178 for(atc_list_iterator it = atc_list.begin(); it != atc_list.end(); it++) {
179 cout << (*it)->get_ident() << ' ';
184 // Search the tuned frequencies every now and then - this should be done with the event scheduler
185 static int i = 0; // Very ugly - but there should only ever be one instance of FGATCMgr.
188 //cout << "About to AreaSearch()" << endl;
193 //cout << "About to search(1)" << endl;
197 //cout << "About to search(2)" << endl;
203 //cout << "comm1 type = " << comm_type[0] << '\n';
204 //cout << "Leaving update..." << endl;
208 // Returns frequency in KHz - should I alter this to return in MHz?
209 unsigned short int FGATCMgr::GetFrequency(const string& ident, const atc_type& tp) {
211 bool ok = current_commlist->FindByCode(ident, test, tp);
212 return(ok ? test.freq : 0);
216 // Register the fact that the AI system wants to activate an airport
217 // Might need more sophistication in this in the future - eg registration by aircraft call-sign.
218 bool FGATCMgr::AIRegisterAirport(const string& ident) {
219 SG_LOG(SG_ATC, SG_BULK, "AI registered airport " << ident << " with the ATC system");
220 //cout << "AI registered airport " << ident << " with the ATC system" << '\n';
221 if(airport_atc_map.find(ident) != airport_atc_map.end()) {
222 airport_atc_map[ident]->set_by_AI = true;
223 airport_atc_map[ident]->numAI++;
226 const FGAirport *ap = fgFindAirportID(ident);
228 //cout << "ident = " << ident << '\n';
229 AirportATC *a = new AirportATC;
230 // I'm not entirely sure that this AirportATC structure business is actually needed - it just duplicates what we can find out anyway!
231 a->lon = ap->getLongitude();
232 a->lat = ap->getLatitude();
233 a->elev = ap->getElevation();
234 a->atis_freq = GetFrequency(ident, ATIS);
235 //cout << "ATIS freq = " << a->atis_freq << '\n';
236 a->atis_active = false;
237 a->tower_freq = GetFrequency(ident, TOWER);
238 //cout << "Tower freq = " << a->tower_freq << '\n';
239 a->tower_active = false;
240 a->ground_freq = GetFrequency(ident, GROUND);
241 //cout << "Ground freq = " << a->ground_freq << '\n';
242 a->ground_active = false;
243 // TODO - some airports will have a tower/ground frequency but be inactive overnight.
246 airport_atc_map[ident] = a;
249 SG_LOG(SG_ATC, SG_ALERT, "ERROR - can't find airport " << ident << " in AIRegisterAirport(...)");
256 // Register the fact that the comm radio is tuned to an airport
257 // Channel is zero based
258 bool FGATCMgr::CommRegisterAirport(const string& ident, int chan, const atc_type& tp) {
259 SG_LOG(SG_ATC, SG_BULK, "Comm channel " << chan << " registered airport " << ident);
260 //cout << "Comm channel " << chan << " registered airport " << ident << ' ' << tp << '\n';
261 if(airport_atc_map.find(ident) != airport_atc_map.end()) {
262 //cout << "IN MAP - flagging set by comm..." << endl;
263 airport_atc_map[ident]->set_by_comm[chan][tp] = true;
265 airport_atc_map[ident]->atis_active = true;
266 } else if(tp == TOWER) {
267 airport_atc_map[ident]->tower_active = true;
268 } else if(tp == GROUND) {
269 airport_atc_map[ident]->ground_active = true;
270 } else if(tp == APPROACH) {
271 //a->approach_active = true;
272 } // TODO - there *must* be a better way to do this!!!
275 //cout << "NOT IN MAP - creating new..." << endl;
276 const FGAirport *ap = fgFindAirportID(ident);
278 AirportATC *a = new AirportATC;
279 // I'm not entirely sure that this AirportATC structure business is actually needed - it just duplicates what we can find out anyway!
280 a->lon = ap->getLongitude();
281 a->lat = ap->getLatitude();
282 a->elev = ap->getElevation();
283 a->atis_freq = GetFrequency(ident, ATIS);
284 a->atis_active = false;
285 a->tower_freq = GetFrequency(ident, TOWER);
286 a->tower_active = false;
287 a->ground_freq = GetFrequency(ident, GROUND);
288 a->ground_active = false;
290 a->atis_active = true;
291 } else if(tp == TOWER) {
292 a->tower_active = true;
293 } else if(tp == GROUND) {
294 a->ground_active = true;
295 } else if(tp == APPROACH) {
296 //a->approach_active = true;
297 } // TODO - there *must* be a better way to do this!!!
298 // TODO - some airports will have a tower/ground frequency but be inactive overnight.
299 a->set_by_AI = false;
301 a->set_by_comm[chan][tp] = true;
302 airport_atc_map[ident] = a;
310 // Remove from list only if not needed by the AI system or the other comm channel
311 // Note that chan is zero based.
312 void FGATCMgr::CommRemoveFromList(const string& id, const atc_type& tp, int chan) {
313 SG_LOG(SG_ATC, SG_BULK, "CommRemoveFromList called for airport " << id << " " << tp << " by channel " << chan);
314 //cout << "CommRemoveFromList called for airport " << id << " " << tp << " by channel " << chan << '\n';
315 if(airport_atc_map.find(id) != airport_atc_map.end()) {
316 AirportATC* a = airport_atc_map[id];
317 //cout << "In CommRemoveFromList, a->ground_freq = " << a->ground_freq << endl;
318 if(a->set_by_AI && tp != ATIS) {
319 // Set by AI, so don't remove simply because user isn't tuned in any more - just stop displaying
320 SG_LOG(SG_ATC, SG_BULK, "In CommRemoveFromList, service was set by AI\n");
321 FGATC* aptr = GetATCPointer(id, tp);
324 //cout << "chan 1\n";
325 a->set_by_comm[0][tp] = false;
326 if(!a->set_by_comm[1][tp]) {
327 //cout << "not set by comm2\n";
329 //cout << "Got pointer\n";
330 aptr->SetNoDisplay();
331 //cout << "Setting no display...\n";
333 //cout << "Not got pointer\n";
338 a->set_by_comm[1][tp] = false;
339 if(!a->set_by_comm[0][tp]) {
341 aptr->SetNoDisplay();
342 //cout << "Setting no display...\n";
347 //airport_atc_map[id] = a;
352 a->set_by_comm[0][tp] = false;
353 // Remove only if not also set by the other comm channel
354 if(!a->set_by_comm[1][tp]) {
355 a->tower_active = false;
356 a->ground_active = false;
357 RemoveFromList(id, tp);
361 a->set_by_comm[1][tp] = false;
362 if(!a->set_by_comm[0][tp]) {
363 a->tower_active = false;
364 a->ground_active = false;
365 RemoveFromList(id, tp);
374 // Remove from list - should only be called from above or similar
375 // This function *will* remove it from the list regardless of who else might want it.
376 void FGATCMgr::RemoveFromList(const string& id, const atc_type& tp) {
377 //cout << "FGATCMgr::RemoveFromList called..." << endl;
378 //cout << "Requested type = " << tp << endl;
379 //cout << "id = " << id << endl;
380 atc_list_iterator it = atc_list.begin();
381 while(it != atc_list.end()) {
382 //cout << "type = " << (*it)->GetType() << '\n';
383 //cout << "Ident = " << (*it)->get_ident() << '\n';
384 if( ((*it)->get_ident() == id)
385 && ((*it)->GetType() == tp) ) {
386 //Before removing it stop it transmitting!!
387 //cout << "OBLITERATING FROM LIST!!!\n";
388 (*it)->SetNoDisplay();
389 (*it)->Update(0.00833);
392 atc_list_itr = atc_list.begin(); // Reset the persistent itr incase we've left it off the end.
400 // Find in list - return a currently active ATC pointer given ICAO code and type
401 // Return NULL if the given service is not in the list
402 // - *** THE CALLING FUNCTION MUST CHECK FOR THIS ***
403 FGATC* FGATCMgr::FindInList(const string& id, const atc_type& tp) {
404 //cout << "Entering FindInList for " << id << ' ' << tp << endl;
405 atc_list_iterator it = atc_list.begin();
406 while(it != atc_list.end()) {
407 if( ((*it)->get_ident() == id)
408 && ((*it)->GetType() == tp) ) {
413 // If we get here it's not in the list
414 //cout << "Couldn't find it in the list though :-(" << endl;
418 // Returns true if the airport is found in the map
419 bool FGATCMgr::GetAirportATCDetails(const string& icao, AirportATC* a) {
420 if(airport_atc_map.find(icao) != airport_atc_map.end()) {
421 *a = *airport_atc_map[icao];
429 // Return a pointer to a given sort of ATC at a given airport and activate if necessary
430 // Returns NULL if service doesn't exist - calling function should check for this.
431 // We really ought to make this private and call it from the CommRegisterAirport / AIRegisterAirport functions
432 // - at the moment all these GetATC... functions exposed are just too complicated.
433 FGATC* FGATCMgr::GetATCPointer(const string& icao, const atc_type& type) {
434 if(airport_atc_map.find(icao) == airport_atc_map.end()) {
435 //cout << "Unable to find " << icao << ' ' << type << " in the airport_atc_map" << endl;
438 //cout << "In GetATCPointer, found " << icao << ' ' << type << endl;
439 AirportATC *a = airport_atc_map[icao];
440 //cout << "a->lon = " << a->lon << '\n';
441 //cout << "a->elev = " << a->elev << '\n';
442 //cout << "a->tower_freq = " << a->tower_freq << '\n';
445 if(a->tower_active) {
446 // Get the pointer from the list
447 return(FindInList(icao, type));
450 if(current_commlist->FindByFreq(a->lon, a->lat, a->elev, a->tower_freq, &data, TOWER)) {
451 FGTower* t = new FGTower;
453 atc_list.push_back(t);
454 a->tower_active = true;
455 airport_atc_map[icao] = a;
456 //cout << "Initing tower " << icao << " in GetATCPointer()\n";
460 SG_LOG(SG_ATC, SG_ALERT, "ERROR - tower that should exist in FGATCMgr::GetATCPointer for airport " << icao << " not found");
467 SG_LOG(SG_ATC, SG_ALERT, "ERROR - ATIS station should not be requested from FGATCMgr::GetATCPointer");
470 //cout << "IN CASE GROUND" << endl;
471 if(a->ground_active) {
472 // Get the pointer from the list
473 return(FindInList(icao, type));
476 if(current_commlist->FindByFreq(a->lon, a->lat, a->elev, a->ground_freq, &data, GROUND)) {
477 FGGround* g = new FGGround;
479 atc_list.push_back(g);
480 a->ground_active = true;
481 airport_atc_map[icao] = a;
485 SG_LOG(SG_ATC, SG_ALERT, "ERROR - ground control that should exist in FGATCMgr::GetATCPointer for airport " << icao << " not found");
497 SG_LOG(SG_ATC, SG_ALERT, "ERROR IN FGATCMgr - reached end of GetATCPointer");
498 //cout << "ERROR IN FGATCMgr - reached end of GetATCPointer" << endl;
503 // Return a pointer to an appropriate voice for a given type of ATC
504 // creating the voice if necessary - ie. make sure exactly one copy
505 // of every voice in use exists in memory.
507 // TODO - in the future this will get more complex and dole out country/airport
508 // specific voices, and possible make sure that the same voice doesn't get used
509 // at different airports in quick succession if a large enough selection are available.
510 FGATCVoice* FGATCMgr::GetVoicePointer(const atc_type& type) {
511 // TODO - implement me better - maintain a list of loaded voices and other voices!!
533 // Search for ATC stations by frequency
534 void FGATCMgr::FreqSearch(int channel) {
535 int chan = channel - 1; // Convert to zero-based for the arrays
538 double freq = comm_node[chan]->getDoubleValue();
539 lon = lon_node->getDoubleValue();
540 lat = lat_node->getDoubleValue();
541 elev = elev_node->getDoubleValue() * SG_FEET_TO_METER;
543 // Query the data store and get the closest match if any
544 if(current_commlist->FindByFreq(lon, lat, elev, freq, &data)) {
547 // If this channel not previously valid then easy - add ATC to list
548 // If this channel was valid then - Have we tuned to a different service?
549 // If so - de-register one and add the other
550 if(comm_valid[chan]) {
551 if((comm_ident[chan] == data.ident) && (comm_type[chan] == data.type)) {
552 // Then we're still tuned into the same service so do nought and return
555 // Something's changed - either the location or the service type
556 // We need to feed the channel in so we're not removing it if we're also tuned in on the other channel
557 CommRemoveFromList(comm_ident[chan], comm_type[chan], chan);
560 // At this point we can assume that we need to add the service.
561 comm_ident[chan] = data.ident;
562 comm_type[chan] = data.type;
563 comm_x[chan] = (double)data.x;
564 comm_y[chan] = (double)data.y;
565 comm_z[chan] = (double)data.z;
566 comm_lon[chan] = (double)data.lon;
567 comm_lat[chan] = (double)data.lat;
568 comm_elev[chan] = (double)data.elev;
569 comm_valid[chan] = true;
571 // This was a switch-case statement but the compiler didn't like the new variable creation with it.
572 if(comm_type[chan] == ATIS) {
573 CommRegisterAirport(comm_ident[chan], chan, ATIS);
574 FGATC* app = FindInList(comm_ident[chan], ATIS);
576 // The station is already in the ATC list
577 //cout << "In list - flagging SetDisplay..." << endl;
578 comm_atc_ptr[chan] = app;
581 // Generate the station and put in the ATC list
582 //cout << "Not in list - generating..." << endl;
583 FGATIS* a = new FGATIS;
585 comm_atc_ptr[chan] = a;
588 atc_list.push_back(a);
590 } else if (comm_type[chan] == TOWER) {
591 //cout << "TOWER TOWER TOWER\n";
592 CommRegisterAirport(comm_ident[chan], chan, TOWER);
593 //cout << "Done (TOWER)" << endl;
594 FGATC* app = FindInList(comm_ident[chan], TOWER);
596 // The station is already in the ATC list
597 SG_LOG(SG_GENERAL, SG_DEBUG, comm_ident[chan] << " is in list - flagging SetDisplay...");
598 //cout << comm_ident[chan] << " is in list - flagging SetDisplay...\n";
599 comm_atc_ptr[chan] = app;
602 // Generate the station and put in the ATC list
603 SG_LOG(SG_GENERAL, SG_DEBUG, comm_ident[chan] << " is not in list - generating...");
604 //cout << comm_ident[chan] << " is not in list - generating...\n";
605 FGTower* t = new FGTower;
607 comm_atc_ptr[chan] = t;
608 //cout << "Initing tower in FreqSearch()\n";
611 atc_list.push_back(t);
613 } else if (comm_type[chan] == GROUND) {
614 CommRegisterAirport(comm_ident[chan], chan, GROUND);
615 //cout << "Done (GROUND)" << endl;
616 FGATC* app = FindInList(comm_ident[chan], GROUND);
618 // The station is already in the ATC list
619 comm_atc_ptr[chan] = app;
622 // Generate the station and put in the ATC list
623 FGGround* g = new FGGround;
625 comm_atc_ptr[chan] = g;
628 atc_list.push_back(g);
630 } else if (comm_type[chan] == APPROACH) {
631 // We have to be a bit more carefull here since approaches are also searched by area
632 CommRegisterAirport(comm_ident[chan], chan, APPROACH);
633 //cout << "Done (APPROACH)" << endl;
634 FGATC* app = FindInList(comm_ident[chan], APPROACH);
636 // The station is already in the ATC list
637 app->AddPlane("Player");
639 comm_atc_ptr[chan] = app;
641 // Generate the station and put in the ATC list
642 FGApproach* a = new FGApproach;
644 comm_atc_ptr[chan] = a;
647 a->AddPlane("Player");
648 atc_list.push_back(a);
652 if(comm_valid[chan]) {
653 if(comm_type[chan] != APPROACH) {
654 // Currently approaches are removed by Alexander's out-of-range mechanism
655 CommRemoveFromList(comm_ident[chan], comm_type[chan], chan);
657 // Note that we *don't* call SetNoDisplay() here because the other comm channel
658 // might be tuned into the same station - this is handled by CommRemoveFromList(...)
659 comm_type[chan] = INVALID;
660 comm_atc_ptr[chan] = NULL;
661 comm_valid[chan] = false;
666 // Search ATC stations by area in order that we appear 'on the radar'
667 void FGATCMgr::AreaSearch() {
668 // Search for Approach stations
669 comm_list_type approaches;
670 comm_list_iterator app_itr;
672 lon = lon_node->getDoubleValue();
673 lat = lat_node->getDoubleValue();
674 elev = elev_node->getDoubleValue() * SG_FEET_TO_METER;
676 // search stations in range
677 int num_app = current_commlist->FindByPos(lon, lat, elev, 100.0, &approaches, APPROACH);
679 //cout << num_app << " approaches found in radiostack search !!!!" << endl;
681 for(app_itr = approaches.begin(); app_itr != approaches.end(); app_itr++) {
683 FGATC* app = FindInList(app_itr->ident, app_itr->type);
685 // The station is already in the ATC list
686 //cout << "In list adding player\n";
687 app->AddPlane("Player");
690 // Generate the station and put in the ATC list
691 FGApproach* a = new FGApproach;
692 a->SetData(&(*app_itr));
693 //cout << "Adding player\n";
694 a->AddPlane("Player");
696 atc_list.push_back(a);
701 // remove planes which are out of range
702 // TODO - I'm not entirely sure that this belongs here.
703 atc_list_iterator it = atc_list.begin();
704 while(it != atc_list.end()) {
705 if((*it)->GetType() == APPROACH ) {
706 int np = (*it)->RemovePlane();
707 // if approach has no planes left remove it from ATC list
709 //cout << "REMOVING AN APPROACH STATION WITH NO PLANES..." << endl;
710 (*it)->SetNoDisplay();
711 (*it)->Update(0.00833);
714 atc_list_itr = atc_list.begin(); // Reset the persistent itr incase we've left it off the end.
715 break; // the other stations will be checked next time