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 <simgear/misc/sg_path.hxx>
22 #include <simgear/debug/logstream.hxx>
23 #include <Airports/simple.hxx>
26 #include "commlist.hxx"
27 #include "ATCdisplay.hxx"
28 #include "ATCDialog.hxx"
29 #include "ATCutils.hxx"
32 // periodic radio station search wrapper
33 static void fgATCSearch( void ) {
34 globals->get_ATC_mgr()->Search();
36 */ //This wouldn't compile - including Time/event.hxx breaks it :-(
37 // Is this still true?? -EMH-
39 AirportATC::AirportATC() :
51 //airport_atc_map.clear();
53 for(int i=0; i<ATC_NUM_TYPES; ++i) {
54 set_by_comm[0][i] = false;
55 set_by_comm[1][i] = false;
59 FGATCMgr::FGATCMgr() {
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 comm_valid[0] = false;
71 comm_valid[1] = false;
76 FGATCMgr::~FGATCMgr() {
80 void FGATCMgr::bind() {
83 void FGATCMgr::unbind() {
86 void FGATCMgr::init() {
87 //cout << "ATCMgr::init called..." << endl;
89 comm_node[0] = fgGetNode("/radios/comm[0]/frequencies/selected-mhz", true);
90 comm_node[1] = fgGetNode("/radios/comm[1]/frequencies/selected-mhz", true);
91 lon_node = fgGetNode("/position/longitude-deg", true);
92 lat_node = fgGetNode("/position/latitude-deg", true);
93 elev_node = fgGetNode("/position/altitude-ft", true);
94 atc_list_itr = atc_list.begin();
96 // Search for connected ATC stations once per 0.8 seconds or so
97 // globals->get_event_mgr()->add( "fgATCSearch()", fgATCSearch,
98 // FGEvent::FG_EVENT_READY, 800);
100 // For some reason the above doesn't compile - including Time/event.hxx stops compilation.
101 // Is this still true after the reorganization of the event managar??
104 // Initialise the frequency search map
105 current_commlist = new FGCommList;
106 SGPath p_comm( globals->get_fg_root() );
107 current_commlist->init( p_comm );
109 // Set the user callsign - bit of a hack at the moment - eventually should be read from aircraft file and user-over-rideable
110 fgSetString("/sim/user/callsign", "Golf Foxtrot Sierra"); // C-FGFS
112 #ifdef ENABLE_AUDIO_SUPPORT
113 // Load all available voices.
114 // For now we'll do one hardwired one
117 voiceOK = v1->LoadVoice("default");
120 /* I've loaded the voice even if /sim/sound/audible is false
121 * since I know no way of forcing load of the voice if the user
122 * subsequently switches /sim/sound/audible to true. */
127 // Initialise the ATC Dialogs
128 //cout << "Initing Transmissions..." << endl;
129 SG_LOG(SG_ATC, SG_INFO, " ATC Transmissions");
130 current_transmissionlist = new FGTransmissionList;
131 SGPath p_transmission( globals->get_fg_root() );
132 p_transmission.append( "ATC/default.transmissions" );
133 current_transmissionlist->init( p_transmission );
134 //cout << "Done Transmissions" << endl;
136 SG_LOG(SG_ATC, SG_INFO, " ATC Dialog System");
137 current_atcdialog = new FGATCDialog;
138 current_atcdialog->Init();
141 //cout << "ATCmgr::init done!" << endl;
144 void FGATCMgr::update(double dt) {
147 SG_LOG(SG_ATC, SG_WARN, "Warning - ATCMgr::update(...) called before ATCMgr::init()");
150 current_atcdialog->Update(dt);
152 //cout << "Entering update..." << endl;
153 //Traverse the list of active stations.
154 //Only update one class per update step to avoid the whole ATC system having to calculate between frames.
155 //Eventually we should only update every so many steps.
156 //cout << "In FGATCMgr::update - atc_list.size = " << atc_list.size() << endl;
157 if(atc_list.size()) {
158 if(atc_list_itr == atc_list.end()) {
159 atc_list_itr = atc_list.begin();
161 //cout << "Updating " << (*atc_list_itr)->get_ident() << ' ' << (*atc_list_itr)->GetType() << '\n';
162 //cout << "Freq = " << (*atc_list_itr)->get_freq() << '\n';
163 (*atc_list_itr)->Update(dt * atc_list.size());
164 //cout << "Done ATC update..." << endl;
169 cout << "ATC_LIST: " << atc_list.size() << ' ';
170 for(atc_list_iterator it = atc_list.begin(); it != atc_list.end(); it++) {
171 cout << (*it)->get_ident() << ' ';
176 // Search the tuned frequencies every now and then - this should be done with the event scheduler
177 static int i = 0; // Very ugly - but there should only ever be one instance of FGATCMgr.
180 //cout << "About to AreaSearch()" << endl;
185 //cout << "About to search(1)" << endl;
189 //cout << "About to search(2)" << endl;
195 //cout << "comm1 type = " << comm_type[0] << '\n';
196 //cout << "Leaving update..." << endl;
200 // Returns frequency in KHz - should I alter this to return in MHz?
201 unsigned short int FGATCMgr::GetFrequency(string ident, atc_type tp) {
203 bool ok = current_commlist->FindByCode(ident, test, tp);
204 return(ok ? test.freq : 0);
208 // Register the fact that the AI system wants to activate an airport
209 // Might need more sophistication in this in the future - eg registration by aircraft call-sign.
210 bool FGATCMgr::AIRegisterAirport(string ident) {
211 SG_LOG(SG_ATC, SG_BULK, "AI registered airport " << ident << " with the ATC system");
212 //cout << "AI registered airport " << ident << " with the ATC system" << '\n';
213 if(airport_atc_map.find(ident) != airport_atc_map.end()) {
214 airport_atc_map[ident]->set_by_AI = true;
215 airport_atc_map[ident]->numAI++;
219 if(dclFindAirportID(ident, &ap)) {
220 //cout << "ident = " << ident << '\n';
221 AirportATC *a = new AirportATC;
222 // I'm not entirely sure that this AirportATC structure business is actually needed - it just duplicates what we can find out anyway!
223 a->lon = ap.longitude;
224 a->lat = ap.latitude;
225 a->elev = ap.elevation;
226 a->atis_freq = GetFrequency(ident, ATIS);
227 //cout << "ATIS freq = " << a->atis_freq << '\n';
228 a->atis_active = false;
229 a->tower_freq = GetFrequency(ident, TOWER);
230 //cout << "Tower freq = " << a->tower_freq << '\n';
231 a->tower_active = false;
232 a->ground_freq = GetFrequency(ident, GROUND);
233 //cout << "Ground freq = " << a->ground_freq << '\n';
234 a->ground_active = false;
235 // TODO - some airports will have a tower/ground frequency but be inactive overnight.
238 airport_atc_map[ident] = a;
241 SG_LOG(SG_ATC, SG_ALERT, "ERROR - can't find airport " << ident << " in AIRegisterAirport(...)");
248 // Register the fact that the comm radio is tuned to an airport
249 // Channel is zero based
250 bool FGATCMgr::CommRegisterAirport(string ident, int chan, atc_type tp) {
251 SG_LOG(SG_ATC, SG_BULK, "Comm channel " << chan << " registered airport " << ident);
252 //cout << "Comm channel " << chan << " registered airport " << ident << ' ' << tp << '\n';
253 if(airport_atc_map.find(ident) != airport_atc_map.end()) {
254 //cout << "IN MAP - flagging set by comm..." << endl;
255 airport_atc_map[ident]->set_by_comm[chan][tp] = true;
257 airport_atc_map[ident]->atis_active = true;
258 } else if(tp == TOWER) {
259 airport_atc_map[ident]->tower_active = true;
260 } else if(tp == GROUND) {
261 airport_atc_map[ident]->ground_active = true;
262 } else if(tp == APPROACH) {
263 //a->approach_active = true;
264 } // TODO - there *must* be a better way to do this!!!
267 //cout << "NOT IN MAP - creating new..." << endl;
269 if(dclFindAirportID(ident, &ap)) {
270 AirportATC *a = new AirportATC;
271 // I'm not entirely sure that this AirportATC structure business is actually needed - it just duplicates what we can find out anyway!
272 a->lon = ap.longitude;
273 a->lat = ap.latitude;
274 a->elev = ap.elevation;
275 a->atis_freq = GetFrequency(ident, ATIS);
276 a->atis_active = false;
277 a->tower_freq = GetFrequency(ident, TOWER);
278 a->tower_active = false;
279 a->ground_freq = GetFrequency(ident, GROUND);
280 a->ground_active = false;
282 a->atis_active = true;
283 } else if(tp == TOWER) {
284 a->tower_active = true;
285 } else if(tp == GROUND) {
286 a->ground_active = true;
287 } else if(tp == APPROACH) {
288 //a->approach_active = true;
289 } // TODO - there *must* be a better way to do this!!!
290 // TODO - some airports will have a tower/ground frequency but be inactive overnight.
291 a->set_by_AI = false;
293 a->set_by_comm[chan][tp] = true;
294 airport_atc_map[ident] = a;
302 // Remove from list only if not needed by the AI system or the other comm channel
303 // Note that chan is zero based.
304 void FGATCMgr::CommRemoveFromList(string id, atc_type tp, int chan) {
305 SG_LOG(SG_ATC, SG_BULK, "CommRemoveFromList called for airport " << id << " " << tp << " by channel " << chan);
306 //cout << "CommRemoveFromList called for airport " << id << " " << tp << " by channel " << chan << '\n';
307 if(airport_atc_map.find(id) != airport_atc_map.end()) {
308 AirportATC* a = airport_atc_map[id];
309 //cout << "In CommRemoveFromList, a->ground_freq = " << a->ground_freq << endl;
310 if(a->set_by_AI && tp != ATIS) {
311 // Set by AI, so don't remove simply because user isn't tuned in any more - just stop displaying
312 SG_LOG(SG_ATC, SG_BULK, "In CommRemoveFromList, service was set by AI\n");
313 FGATC* aptr = GetATCPointer(id, tp);
316 //cout << "chan 1\n";
317 a->set_by_comm[0][tp] = false;
318 if(!a->set_by_comm[1][tp]) {
319 //cout << "not set by comm2\n";
321 //cout << "Got pointer\n";
322 aptr->SetNoDisplay();
323 //cout << "Setting no display...\n";
325 //cout << "Not got pointer\n";
330 a->set_by_comm[1][tp] = false;
331 if(!a->set_by_comm[0][tp]) {
333 aptr->SetNoDisplay();
334 //cout << "Setting no display...\n";
339 //airport_atc_map[id] = a;
344 a->set_by_comm[0][tp] = false;
345 // Remove only if not also set by the other comm channel
346 if(!a->set_by_comm[1][tp]) {
347 a->tower_active = false;
348 a->ground_active = false;
349 RemoveFromList(id, tp);
353 a->set_by_comm[1][tp] = false;
354 if(!a->set_by_comm[0][tp]) {
355 a->tower_active = false;
356 a->ground_active = false;
357 RemoveFromList(id, tp);
366 // Remove from list - should only be called from above or similar
367 // This function *will* remove it from the list regardless of who else might want it.
368 void FGATCMgr::RemoveFromList(string id, atc_type tp) {
369 //cout << "FGATCMgr::RemoveFromList called..." << endl;
370 //cout << "Requested type = " << tp << endl;
371 //cout << "id = " << id << endl;
372 atc_list_iterator it = atc_list.begin();
373 while(it != atc_list.end()) {
374 //cout << "type = " << (*it)->GetType() << '\n';
375 //cout << "Ident = " << (*it)->get_ident() << '\n';
376 if( (!strcmp((*it)->get_ident(), id.c_str()))
377 && ((*it)->GetType() == tp) ) {
378 //Before removing it stop it transmitting!!
379 //cout << "OBLITERATING FROM LIST!!!\n";
380 (*it)->SetNoDisplay();
381 (*it)->Update(0.00833);
384 atc_list_itr = atc_list.begin(); // Reset the persistent itr incase we've left it off the end.
392 // Find in list - return a currently active ATC pointer given ICAO code and type
393 // Return NULL if the given service is not in the list
394 // - *** THE CALLING FUNCTION MUST CHECK FOR THIS ***
395 FGATC* FGATCMgr::FindInList(string id, atc_type tp) {
396 //cout << "Entering FindInList for " << id << ' ' << tp << endl;
397 atc_list_iterator it = atc_list.begin();
398 while(it != atc_list.end()) {
399 if( (!strcmp((*it)->get_ident(), id.c_str()))
400 && ((*it)->GetType() == tp) ) {
405 // If we get here it's not in the list
406 //cout << "Couldn't find it in the list though :-(" << endl;
410 // Returns true if the airport is found in the map
411 bool FGATCMgr::GetAirportATCDetails(string icao, AirportATC* a) {
412 if(airport_atc_map.find(icao) != airport_atc_map.end()) {
413 *a = *airport_atc_map[icao];
421 // Return a pointer to a given sort of ATC at a given airport and activate if necessary
422 // Returns NULL if service doesn't exist - calling function should check for this.
423 // We really ought to make this private and call it from the CommRegisterAirport / AIRegisterAirport functions
424 // - at the moment all these GetATC... functions exposed are just too complicated.
425 FGATC* FGATCMgr::GetATCPointer(string icao, atc_type type) {
426 if(airport_atc_map.find(icao) == airport_atc_map.end()) {
427 //cout << "Unable to find " << icao << ' ' << type << " in the airport_atc_map" << endl;
430 //cout << "In GetATCPointer, found " << icao << ' ' << type << endl;
431 AirportATC *a = airport_atc_map[icao];
432 //cout << "a->lon = " << a->lon << '\n';
433 //cout << "a->elev = " << a->elev << '\n';
434 //cout << "a->tower_freq = " << a->tower_freq << '\n';
437 if(a->tower_active) {
438 // Get the pointer from the list
439 return(FindInList(icao, type));
442 if(current_commlist->FindByFreq(a->lon, a->lat, a->elev, a->tower_freq, &data, TOWER)) {
443 FGTower* t = new FGTower;
445 atc_list.push_back(t);
446 a->tower_active = true;
447 airport_atc_map[icao] = a;
448 //cout << "Initing tower " << icao << " in GetATCPointer()\n";
452 SG_LOG(SG_ATC, SG_ALERT, "ERROR - tower that should exist in FGATCMgr::GetATCPointer for airport " << icao << " not found");
459 SG_LOG(SG_ATC, SG_ALERT, "ERROR - ATIS station should not be requested from FGATCMgr::GetATCPointer");
462 //cout << "IN CASE GROUND" << endl;
463 if(a->ground_active) {
464 // Get the pointer from the list
465 return(FindInList(icao, type));
468 if(current_commlist->FindByFreq(a->lon, a->lat, a->elev, a->ground_freq, &data, GROUND)) {
469 FGGround* g = new FGGround;
471 atc_list.push_back(g);
472 a->ground_active = true;
473 airport_atc_map[icao] = a;
477 SG_LOG(SG_ATC, SG_ALERT, "ERROR - ground control that should exist in FGATCMgr::GetATCPointer for airport " << icao << " not found");
489 SG_LOG(SG_ATC, SG_ALERT, "ERROR IN FGATCMgr - reached end of GetATCPointer");
490 //cout << "ERROR IN FGATCMgr - reached end of GetATCPointer" << endl;
495 // Return a pointer to an appropriate voice for a given type of ATC
496 // creating the voice if necessary - ie. make sure exactly one copy
497 // of every voice in use exists in memory.
499 // TODO - in the future this will get more complex and dole out country/airport
500 // specific voices, and possible make sure that the same voice doesn't get used
501 // at different airports in quick succession if a large enough selection are available.
502 FGATCVoice* FGATCMgr::GetVoicePointer(atc_type type) {
503 // TODO - implement me better - maintain a list of loaded voices and other voices!!
525 // Search for ATC stations by frequency
526 void FGATCMgr::FreqSearch(int channel) {
527 int chan = channel - 1; // Convert to zero-based for the arrays
530 double freq = comm_node[chan]->getDoubleValue();
531 lon = lon_node->getDoubleValue();
532 lat = lat_node->getDoubleValue();
533 elev = elev_node->getDoubleValue() * SG_FEET_TO_METER;
535 // Query the data store and get the closest match if any
536 if(current_commlist->FindByFreq(lon, lat, elev, freq, &data)) {
539 // If this channel not previously valid then easy - add ATC to list
540 // If this channel was valid then - Have we tuned to a different service?
541 // If so - de-register one and add the other
542 if(comm_valid[chan]) {
543 if((comm_ident[chan] == data.ident) && (comm_type[chan] == data.type)) {
544 // Then we're still tuned into the same service so do nought and return
547 // Something's changed - either the location or the service type
548 // We need to feed the channel in so we're not removing it if we're also tuned in on the other channel
549 CommRemoveFromList(comm_ident[chan], comm_type[chan], chan);
552 // At this point we can assume that we need to add the service.
553 comm_ident[chan] = data.ident;
554 comm_type[chan] = data.type;
555 comm_x[chan] = (double)data.x;
556 comm_y[chan] = (double)data.y;
557 comm_z[chan] = (double)data.z;
558 comm_lon[chan] = (double)data.lon;
559 comm_lat[chan] = (double)data.lat;
560 comm_elev[chan] = (double)data.elev;
561 comm_valid[chan] = true;
563 // This was a switch-case statement but the compiler didn't like the new variable creation with it.
564 if(comm_type[chan] == ATIS) {
565 CommRegisterAirport(comm_ident[chan], chan, ATIS);
566 FGATC* app = FindInList(comm_ident[chan], ATIS);
568 // The station is already in the ATC list
569 //cout << "In list - flagging SetDisplay..." << endl;
570 comm_atc_ptr[chan] = app;
573 // Generate the station and put in the ATC list
574 //cout << "Not in list - generating..." << endl;
575 FGATIS* a = new FGATIS;
577 comm_atc_ptr[chan] = a;
580 atc_list.push_back(a);
582 } else if (comm_type[chan] == TOWER) {
583 //cout << "TOWER TOWER TOWER\n";
584 CommRegisterAirport(comm_ident[chan], chan, TOWER);
585 //cout << "Done (TOWER)" << endl;
586 FGATC* app = FindInList(comm_ident[chan], TOWER);
588 // The station is already in the ATC list
589 SG_LOG(SG_GENERAL, SG_DEBUG, comm_ident[chan] << " is in list - flagging SetDisplay...");
590 //cout << comm_ident[chan] << " is in list - flagging SetDisplay...\n";
591 comm_atc_ptr[chan] = app;
594 // Generate the station and put in the ATC list
595 SG_LOG(SG_GENERAL, SG_DEBUG, comm_ident[chan] << " is not in list - generating...");
596 //cout << comm_ident[chan] << " is not in list - generating...\n";
597 FGTower* t = new FGTower;
599 comm_atc_ptr[chan] = t;
600 //cout << "Initing tower in FreqSearch()\n";
603 atc_list.push_back(t);
605 } else if (comm_type[chan] == GROUND) {
606 CommRegisterAirport(comm_ident[chan], chan, GROUND);
607 //cout << "Done (GROUND)" << endl;
608 FGATC* app = FindInList(comm_ident[chan], GROUND);
610 // The station is already in the ATC list
611 comm_atc_ptr[chan] = app;
614 // Generate the station and put in the ATC list
615 FGGround* g = new FGGround;
617 comm_atc_ptr[chan] = g;
620 atc_list.push_back(g);
622 } else if (comm_type[chan] == APPROACH) {
623 // We have to be a bit more carefull here since approaches are also searched by area
624 CommRegisterAirport(comm_ident[chan], chan, APPROACH);
625 //cout << "Done (APPROACH)" << endl;
626 FGATC* app = FindInList(comm_ident[chan], APPROACH);
628 // The station is already in the ATC list
629 app->AddPlane("Player");
631 comm_atc_ptr[chan] = app;
633 // Generate the station and put in the ATC list
634 FGApproach* a = new FGApproach;
636 comm_atc_ptr[chan] = a;
639 a->AddPlane("Player");
640 atc_list.push_back(a);
644 if(comm_valid[chan]) {
645 if(comm_type[chan] != APPROACH) {
646 // Currently approaches are removed by Alexander's out-of-range mechanism
647 CommRemoveFromList(comm_ident[chan], comm_type[chan], chan);
649 // Note that we *don't* call SetNoDisplay() here because the other comm channel
650 // might be tuned into the same station - this is handled by CommRemoveFromList(...)
651 comm_type[chan] = INVALID;
652 comm_atc_ptr[chan] = NULL;
653 comm_valid[chan] = false;
658 // Search ATC stations by area in order that we appear 'on the radar'
659 void FGATCMgr::AreaSearch() {
660 // Search for Approach stations
661 comm_list_type approaches;
662 comm_list_iterator app_itr;
664 lon = lon_node->getDoubleValue();
665 lat = lat_node->getDoubleValue();
666 elev = elev_node->getDoubleValue() * SG_FEET_TO_METER;
668 // search stations in range
669 int num_app = current_commlist->FindByPos(lon, lat, elev, 100.0, &approaches, APPROACH);
671 //cout << num_app << " approaches found in radiostack search !!!!" << endl;
673 for(app_itr = approaches.begin(); app_itr != approaches.end(); app_itr++) {
675 FGATC* app = FindInList(app_itr->ident, app_itr->type);
677 // The station is already in the ATC list
678 //cout << "In list adding player\n";
679 app->AddPlane("Player");
682 // Generate the station and put in the ATC list
683 FGApproach* a = new FGApproach;
684 a->SetData(&(*app_itr));
685 //cout << "Adding player\n";
686 a->AddPlane("Player");
688 atc_list.push_back(a);
693 // remove planes which are out of range
694 // TODO - I'm not entirely sure that this belongs here.
695 atc_list_iterator it = atc_list.begin();
696 while(it != atc_list.end()) {
697 if((*it)->GetType() == APPROACH ) {
698 int np = (*it)->RemovePlane();
699 // if approach has no planes left remove it from ATC list
701 //cout << "REMOVING AN APPROACH STATION WITH NO PLANES..." << endl;
702 (*it)->SetNoDisplay();
703 (*it)->Update(0.00833);
706 atc_list_itr = atc_list.begin(); // Reset the persistent itr incase we've left it off the end.
707 break; // the other stations will be checked next time