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 "ATCDialog.hxx"
32 #include "ATCutils.hxx"
33 #include "transmissionlist.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 :-(
43 // Is this still true?? -EMH-
45 AirportATC::AirportATC() :
57 //airport_atc_map.clear();
59 for(int i=0; i<ATC_NUM_TYPES; ++i) {
60 set_by_comm[0][i] = false;
61 set_by_comm[1][i] = false;
65 FGATCMgr::FGATCMgr() {
68 //last_comm_ident[0] = "";
69 //last_comm_ident[1] = "";
70 //approach_ident = "";
71 last_in_range = false;
72 comm_type[0] = INVALID;
73 comm_type[1] = INVALID;
74 comm_atc_ptr[0] = NULL;
75 comm_atc_ptr[1] = NULL;
76 comm_valid[0] = false;
77 comm_valid[1] = false;
82 FGATCMgr::~FGATCMgr() {
86 void FGATCMgr::bind() {
89 void FGATCMgr::unbind() {
92 void FGATCMgr::init() {
93 //cout << "ATCMgr::init called..." << endl;
95 comm_node[0] = fgGetNode("/instrumentation/comm[0]/frequencies/selected-mhz", true);
96 comm_node[1] = fgGetNode("/instrumentation/comm[1]/frequencies/selected-mhz", true);
97 lon_node = fgGetNode("/position/longitude-deg", true);
98 lat_node = fgGetNode("/position/latitude-deg", true);
99 elev_node = fgGetNode("/position/altitude-ft", true);
100 atc_list_itr = atc_list.begin();
102 // Search for connected ATC stations once per 0.8 seconds or so
103 // globals->get_event_mgr()->add( "fgATCSearch()", fgATCSearch,
104 // FGEvent::FG_EVENT_READY, 800);
106 // For some reason the above doesn't compile - including Time/event.hxx stops compilation.
107 // Is this still true after the reorganization of the event managar??
110 // Initialise the frequency search map
111 current_commlist = new FGCommList;
112 SGPath p_comm( globals->get_fg_root() );
113 current_commlist->init( p_comm );
115 #ifdef ENABLE_AUDIO_SUPPORT
116 // Load all available voices.
117 // For now we'll do one hardwired one
121 voiceOK = v1->LoadVoice("default");
123 } catch ( sg_io_exception & ) {
130 /* I've loaded the voice even if /sim/sound/pause is true
131 * since I know no way of forcing load of the voice if the user
132 * subsequently switches /sim/sound/audible to true.
133 * (which is the right thing to do -- CLO) :-) */
138 // Initialise the ATC Dialogs
139 //cout << "Initing Transmissions..." << endl;
140 SG_LOG(SG_ATC, SG_INFO, " ATC Transmissions");
141 current_transmissionlist = new FGTransmissionList;
142 SGPath p_transmission( globals->get_fg_root() );
143 p_transmission.append( "ATC/default.transmissions" );
144 current_transmissionlist->init( p_transmission );
145 //cout << "Done Transmissions" << endl;
147 SG_LOG(SG_ATC, SG_INFO, " ATC Dialog System");
148 current_atcdialog = new FGATCDialog;
149 current_atcdialog->Init();
152 //cout << "ATCmgr::init done!" << endl;
155 void FGATCMgr::update(double dt) {
158 SG_LOG(SG_ATC, SG_WARN, "Warning - ATCMgr::update(...) called before ATCMgr::init()");
161 current_atcdialog->Update(dt);
163 //cout << "Entering update..." << endl;
164 //Traverse the list of active stations.
165 //Only update one class per update step to avoid the whole ATC system having to calculate between frames.
166 //Eventually we should only update every so many steps.
167 //cout << "In FGATCMgr::update - atc_list.size = " << atc_list.size() << endl;
168 if(atc_list.size()) {
169 if(atc_list_itr == atc_list.end()) {
170 atc_list_itr = atc_list.begin();
172 //cout << "Updating " << (*atc_list_itr)->get_ident() << ' ' << (*atc_list_itr)->GetType() << '\n';
173 //cout << "Freq = " << (*atc_list_itr)->get_freq() << '\n';
174 (*atc_list_itr)->Update(dt * atc_list.size());
175 //cout << "Done ATC update..." << endl;
180 cout << "ATC_LIST: " << atc_list.size() << ' ';
181 for(atc_list_iterator it = atc_list.begin(); it != atc_list.end(); it++) {
182 cout << (*it)->get_ident() << ' ';
187 // Search the tuned frequencies every now and then - this should be done with the event scheduler
188 static int i = 0; // Very ugly - but there should only ever be one instance of FGATCMgr.
191 //cout << "About to AreaSearch()" << endl;
196 //cout << "About to search(1)" << endl;
200 //cout << "About to search(2)" << endl;
206 //cout << "comm1 type = " << comm_type[0] << '\n';
207 //cout << "Leaving update..." << endl;
211 // Returns frequency in KHz - should I alter this to return in MHz?
212 unsigned short int FGATCMgr::GetFrequency(const string& ident, const atc_type& tp) {
214 bool ok = current_commlist->FindByCode(ident, test, tp);
215 return(ok ? test.freq : 0);
219 // Register the fact that the AI system wants to activate an airport
220 // Might need more sophistication in this in the future - eg registration by aircraft call-sign.
221 bool FGATCMgr::AIRegisterAirport(const string& ident) {
222 SG_LOG(SG_ATC, SG_BULK, "AI registered airport " << ident << " with the ATC system");
223 //cout << "AI registered airport " << ident << " with the ATC system" << '\n';
224 if(airport_atc_map.find(ident) != airport_atc_map.end()) {
225 airport_atc_map[ident]->set_by_AI = true;
226 airport_atc_map[ident]->numAI++;
229 const FGAirport *ap = fgFindAirportID(ident);
231 //cout << "ident = " << ident << '\n';
232 AirportATC *a = new AirportATC;
233 // I'm not entirely sure that this AirportATC structure business is actually needed - it just duplicates what we can find out anyway!
234 a->lon = ap->getLongitude();
235 a->lat = ap->getLatitude();
236 a->elev = ap->getElevation();
237 a->atis_freq = GetFrequency(ident, ATIS);
238 //cout << "ATIS freq = " << a->atis_freq << '\n';
239 a->atis_active = false;
240 a->tower_freq = GetFrequency(ident, TOWER);
241 //cout << "Tower freq = " << a->tower_freq << '\n';
242 a->tower_active = false;
243 a->ground_freq = GetFrequency(ident, GROUND);
244 //cout << "Ground freq = " << a->ground_freq << '\n';
245 a->ground_active = false;
246 // TODO - some airports will have a tower/ground frequency but be inactive overnight.
249 airport_atc_map[ident] = a;
252 SG_LOG(SG_ATC, SG_ALERT, "ERROR - can't find airport " << ident << " in AIRegisterAirport(...)");
259 // Register the fact that the comm radio is tuned to an airport
260 // Channel is zero based
261 bool FGATCMgr::CommRegisterAirport(const string& ident, int chan, const atc_type& tp) {
262 SG_LOG(SG_ATC, SG_BULK, "Comm channel " << chan << " registered airport " << ident);
263 //cout << "Comm channel " << chan << " registered airport " << ident << ' ' << tp << '\n';
264 if(airport_atc_map.find(ident) != airport_atc_map.end()) {
265 //cout << "IN MAP - flagging set by comm..." << endl;
266 airport_atc_map[ident]->set_by_comm[chan][tp] = true;
268 airport_atc_map[ident]->atis_active = true;
269 } else if(tp == TOWER) {
270 airport_atc_map[ident]->tower_active = true;
271 } else if(tp == GROUND) {
272 airport_atc_map[ident]->ground_active = true;
273 } else if(tp == APPROACH) {
274 //a->approach_active = true;
275 } // TODO - there *must* be a better way to do this!!!
278 //cout << "NOT IN MAP - creating new..." << endl;
279 const FGAirport *ap = fgFindAirportID(ident);
281 AirportATC *a = new AirportATC;
282 // I'm not entirely sure that this AirportATC structure business is actually needed - it just duplicates what we can find out anyway!
283 a->lon = ap->getLongitude();
284 a->lat = ap->getLatitude();
285 a->elev = ap->getElevation();
286 a->atis_freq = GetFrequency(ident, ATIS);
287 a->atis_active = false;
288 a->tower_freq = GetFrequency(ident, TOWER);
289 a->tower_active = false;
290 a->ground_freq = GetFrequency(ident, GROUND);
291 a->ground_active = false;
293 a->atis_active = true;
294 } else if(tp == TOWER) {
295 a->tower_active = true;
296 } else if(tp == GROUND) {
297 a->ground_active = true;
298 } else if(tp == APPROACH) {
299 //a->approach_active = true;
300 } // TODO - there *must* be a better way to do this!!!
301 // TODO - some airports will have a tower/ground frequency but be inactive overnight.
302 a->set_by_AI = false;
304 a->set_by_comm[chan][tp] = true;
305 airport_atc_map[ident] = a;
313 // Remove from list only if not needed by the AI system or the other comm channel
314 // Note that chan is zero based.
315 void FGATCMgr::CommRemoveFromList(const string& id, const atc_type& tp, int chan) {
316 SG_LOG(SG_ATC, SG_BULK, "CommRemoveFromList called for airport " << id << " " << tp << " by channel " << chan);
317 //cout << "CommRemoveFromList called for airport " << id << " " << tp << " by channel " << chan << '\n';
318 if(airport_atc_map.find(id) != airport_atc_map.end()) {
319 AirportATC* a = airport_atc_map[id];
320 //cout << "In CommRemoveFromList, a->ground_freq = " << a->ground_freq << endl;
321 if(a->set_by_AI && tp != ATIS) {
322 // Set by AI, so don't remove simply because user isn't tuned in any more - just stop displaying
323 SG_LOG(SG_ATC, SG_BULK, "In CommRemoveFromList, service was set by AI\n");
324 FGATC* aptr = GetATCPointer(id, tp);
327 //cout << "chan 1\n";
328 a->set_by_comm[0][tp] = false;
329 if(!a->set_by_comm[1][tp]) {
330 //cout << "not set by comm2\n";
332 //cout << "Got pointer\n";
333 aptr->SetNoDisplay();
334 //cout << "Setting no display...\n";
336 //cout << "Not got pointer\n";
341 a->set_by_comm[1][tp] = false;
342 if(!a->set_by_comm[0][tp]) {
344 aptr->SetNoDisplay();
345 //cout << "Setting no display...\n";
350 //airport_atc_map[id] = a;
355 a->set_by_comm[0][tp] = false;
356 // Remove only if not also set by the other comm channel
357 if(!a->set_by_comm[1][tp]) {
358 a->tower_active = false;
359 a->ground_active = false;
360 RemoveFromList(id, tp);
364 a->set_by_comm[1][tp] = false;
365 if(!a->set_by_comm[0][tp]) {
366 a->tower_active = false;
367 a->ground_active = false;
368 RemoveFromList(id, tp);
377 // Remove from list - should only be called from above or similar
378 // This function *will* remove it from the list regardless of who else might want it.
379 void FGATCMgr::RemoveFromList(const string& id, const atc_type& tp) {
380 //cout << "FGATCMgr::RemoveFromList called..." << endl;
381 //cout << "Requested type = " << tp << endl;
382 //cout << "id = " << id << endl;
383 atc_list_iterator it = atc_list.begin();
384 while(it != atc_list.end()) {
385 //cout << "type = " << (*it)->GetType() << '\n';
386 //cout << "Ident = " << (*it)->get_ident() << '\n';
387 if( ((*it)->get_ident() == id)
388 && ((*it)->GetType() == tp) ) {
389 //Before removing it stop it transmitting!!
390 //cout << "OBLITERATING FROM LIST!!!\n";
391 (*it)->SetNoDisplay();
392 (*it)->Update(0.00833);
395 atc_list_itr = atc_list.begin(); // Reset the persistent itr incase we've left it off the end.
403 // Find in list - return a currently active ATC pointer given ICAO code and type
404 // Return NULL if the given service is not in the list
405 // - *** THE CALLING FUNCTION MUST CHECK FOR THIS ***
406 FGATC* FGATCMgr::FindInList(const string& id, const atc_type& tp) {
407 //cout << "Entering FindInList for " << id << ' ' << tp << endl;
408 atc_list_iterator it = atc_list.begin();
409 while(it != atc_list.end()) {
410 if( ((*it)->get_ident() == id)
411 && ((*it)->GetType() == tp) ) {
416 // If we get here it's not in the list
417 //cout << "Couldn't find it in the list though :-(" << endl;
421 // Returns true if the airport is found in the map
422 bool FGATCMgr::GetAirportATCDetails(const string& icao, AirportATC* a) {
423 if(airport_atc_map.find(icao) != airport_atc_map.end()) {
424 *a = *airport_atc_map[icao];
432 // Return a pointer to a given sort of ATC at a given airport and activate if necessary
433 // Returns NULL if service doesn't exist - calling function should check for this.
434 // We really ought to make this private and call it from the CommRegisterAirport / AIRegisterAirport functions
435 // - at the moment all these GetATC... functions exposed are just too complicated.
436 FGATC* FGATCMgr::GetATCPointer(const string& icao, const atc_type& type) {
437 if(airport_atc_map.find(icao) == airport_atc_map.end()) {
438 //cout << "Unable to find " << icao << ' ' << type << " in the airport_atc_map" << endl;
441 //cout << "In GetATCPointer, found " << icao << ' ' << type << endl;
442 AirportATC *a = airport_atc_map[icao];
443 //cout << "a->lon = " << a->lon << '\n';
444 //cout << "a->elev = " << a->elev << '\n';
445 //cout << "a->tower_freq = " << a->tower_freq << '\n';
448 if(a->tower_active) {
449 // Get the pointer from the list
450 return(FindInList(icao, type));
453 if(current_commlist->FindByFreq(a->lon, a->lat, a->elev, a->tower_freq, &data, TOWER)) {
454 FGTower* t = new FGTower;
456 atc_list.push_back(t);
457 a->tower_active = true;
458 airport_atc_map[icao] = a;
459 //cout << "Initing tower " << icao << " in GetATCPointer()\n";
463 SG_LOG(SG_ATC, SG_ALERT, "ERROR - tower that should exist in FGATCMgr::GetATCPointer for airport " << icao << " not found");
470 SG_LOG(SG_ATC, SG_ALERT, "ERROR - ATIS station should not be requested from FGATCMgr::GetATCPointer");
473 //cout << "IN CASE GROUND" << endl;
474 if(a->ground_active) {
475 // Get the pointer from the list
476 return(FindInList(icao, type));
479 if(current_commlist->FindByFreq(a->lon, a->lat, a->elev, a->ground_freq, &data, GROUND)) {
480 FGGround* g = new FGGround;
482 atc_list.push_back(g);
483 a->ground_active = true;
484 airport_atc_map[icao] = a;
488 SG_LOG(SG_ATC, SG_ALERT, "ERROR - ground control that should exist in FGATCMgr::GetATCPointer for airport " << icao << " not found");
500 SG_LOG(SG_ATC, SG_ALERT, "ERROR IN FGATCMgr - reached end of GetATCPointer");
501 //cout << "ERROR IN FGATCMgr - reached end of GetATCPointer" << endl;
506 // Return a pointer to an appropriate voice for a given type of ATC
507 // creating the voice if necessary - ie. make sure exactly one copy
508 // of every voice in use exists in memory.
510 // TODO - in the future this will get more complex and dole out country/airport
511 // specific voices, and possible make sure that the same voice doesn't get used
512 // at different airports in quick succession if a large enough selection are available.
513 FGATCVoice* FGATCMgr::GetVoicePointer(const atc_type& type) {
514 // TODO - implement me better - maintain a list of loaded voices and other voices!!
536 // Search for ATC stations by frequency
537 void FGATCMgr::FreqSearch(int channel) {
538 int chan = channel - 1; // Convert to zero-based for the arrays
541 double freq = comm_node[chan]->getDoubleValue();
542 lon = lon_node->getDoubleValue();
543 lat = lat_node->getDoubleValue();
544 elev = elev_node->getDoubleValue() * SG_FEET_TO_METER;
546 // Query the data store and get the closest match if any
547 if(current_commlist->FindByFreq(lon, lat, elev, freq, &data)) {
550 // If this channel not previously valid then easy - add ATC to list
551 // If this channel was valid then - Have we tuned to a different service?
552 // If so - de-register one and add the other
553 if(comm_valid[chan]) {
554 if((comm_ident[chan] == data.ident) && (comm_type[chan] == data.type)) {
555 // Then we're still tuned into the same service so do nought and return
558 // Something's changed - either the location or the service type
559 // We need to feed the channel in so we're not removing it if we're also tuned in on the other channel
560 CommRemoveFromList(comm_ident[chan], comm_type[chan], chan);
563 // At this point we can assume that we need to add the service.
564 comm_ident[chan] = data.ident;
565 comm_type[chan] = data.type;
566 comm_x[chan] = (double)data.x;
567 comm_y[chan] = (double)data.y;
568 comm_z[chan] = (double)data.z;
569 comm_lon[chan] = (double)data.lon;
570 comm_lat[chan] = (double)data.lat;
571 comm_elev[chan] = (double)data.elev;
572 comm_valid[chan] = true;
574 // This was a switch-case statement but the compiler didn't like the new variable creation with it.
575 if(comm_type[chan] == ATIS) {
576 CommRegisterAirport(comm_ident[chan], chan, ATIS);
577 FGATC* app = FindInList(comm_ident[chan], ATIS);
579 // The station is already in the ATC list
580 //cout << "In list - flagging SetDisplay..." << endl;
581 comm_atc_ptr[chan] = app;
584 // Generate the station and put in the ATC list
585 //cout << "Not in list - generating..." << endl;
586 FGATIS* a = new FGATIS;
588 comm_atc_ptr[chan] = a;
591 atc_list.push_back(a);
593 } else if (comm_type[chan] == TOWER) {
594 //cout << "TOWER TOWER TOWER\n";
595 CommRegisterAirport(comm_ident[chan], chan, TOWER);
596 //cout << "Done (TOWER)" << endl;
597 FGATC* app = FindInList(comm_ident[chan], TOWER);
599 // The station is already in the ATC list
600 SG_LOG(SG_GENERAL, SG_DEBUG, comm_ident[chan] << " is in list - flagging SetDisplay...");
601 //cout << comm_ident[chan] << " is in list - flagging SetDisplay...\n";
602 comm_atc_ptr[chan] = app;
605 // Generate the station and put in the ATC list
606 SG_LOG(SG_GENERAL, SG_DEBUG, comm_ident[chan] << " is not in list - generating...");
607 //cout << comm_ident[chan] << " is not in list - generating...\n";
608 FGTower* t = new FGTower;
610 comm_atc_ptr[chan] = t;
611 //cout << "Initing tower in FreqSearch()\n";
614 atc_list.push_back(t);
616 } else if (comm_type[chan] == GROUND) {
617 CommRegisterAirport(comm_ident[chan], chan, GROUND);
618 //cout << "Done (GROUND)" << endl;
619 FGATC* app = FindInList(comm_ident[chan], GROUND);
621 // The station is already in the ATC list
622 comm_atc_ptr[chan] = app;
625 // Generate the station and put in the ATC list
626 FGGround* g = new FGGround;
628 comm_atc_ptr[chan] = g;
631 atc_list.push_back(g);
633 } else if (comm_type[chan] == APPROACH) {
634 // We have to be a bit more carefull here since approaches are also searched by area
635 CommRegisterAirport(comm_ident[chan], chan, APPROACH);
636 //cout << "Done (APPROACH)" << endl;
637 FGATC* app = FindInList(comm_ident[chan], APPROACH);
639 // The station is already in the ATC list
640 app->AddPlane("Player");
642 comm_atc_ptr[chan] = app;
644 // Generate the station and put in the ATC list
645 FGApproach* a = new FGApproach;
647 comm_atc_ptr[chan] = a;
650 a->AddPlane("Player");
651 atc_list.push_back(a);
655 if(comm_valid[chan]) {
656 if(comm_type[chan] != APPROACH) {
657 // Currently approaches are removed by Alexander's out-of-range mechanism
658 CommRemoveFromList(comm_ident[chan], comm_type[chan], chan);
660 // Note that we *don't* call SetNoDisplay() here because the other comm channel
661 // might be tuned into the same station - this is handled by CommRemoveFromList(...)
662 comm_type[chan] = INVALID;
663 comm_atc_ptr[chan] = NULL;
664 comm_valid[chan] = false;
669 // Search ATC stations by area in order that we appear 'on the radar'
670 void FGATCMgr::AreaSearch() {
671 // Search for Approach stations
672 comm_list_type approaches;
673 comm_list_iterator app_itr;
675 lon = lon_node->getDoubleValue();
676 lat = lat_node->getDoubleValue();
677 elev = elev_node->getDoubleValue() * SG_FEET_TO_METER;
679 // search stations in range
680 int num_app = current_commlist->FindByPos(lon, lat, elev, 100.0, &approaches, APPROACH);
682 //cout << num_app << " approaches found in radiostack search !!!!" << endl;
684 for(app_itr = approaches.begin(); app_itr != approaches.end(); app_itr++) {
686 FGATC* app = FindInList(app_itr->ident, app_itr->type);
688 // The station is already in the ATC list
689 //cout << "In list adding player\n";
690 app->AddPlane("Player");
693 // Generate the station and put in the ATC list
694 FGApproach* a = new FGApproach;
695 a->SetData(&(*app_itr));
696 //cout << "Adding player\n";
697 a->AddPlane("Player");
699 atc_list.push_back(a);
704 // remove planes which are out of range
705 // TODO - I'm not entirely sure that this belongs here.
706 atc_list_iterator it = atc_list.begin();
707 while(it != atc_list.end()) {
708 if((*it)->GetType() == APPROACH ) {
709 int np = (*it)->RemovePlane();
710 // if approach has no planes left remove it from ATC list
712 //cout << "REMOVING AN APPROACH STATION WITH NO PLANES..." << endl;
713 (*it)->SetNoDisplay();
714 (*it)->Update(0.00833);
717 atc_list_itr = atc_list.begin(); // Reset the persistent itr incase we've left it off the end.
718 break; // the other stations will be checked next time