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() :
52 set_by_comm[0] = false;
53 set_by_comm[1] = false;
56 FGATCMgr::FGATCMgr() {
59 last_comm_ident[0] = "";
60 last_comm_ident[1] = "";
62 last_in_range = false;
63 comm_type[0] = INVALID;
64 comm_type[1] = INVALID;
65 comm_atc_ptr[0] = NULL;
66 comm_atc_ptr[1] = NULL;
67 comm_valid[0] = false;
68 comm_valid[1] = false;
73 FGATCMgr::~FGATCMgr() {
77 void FGATCMgr::bind() {
80 void FGATCMgr::unbind() {
83 void FGATCMgr::init() {
84 comm_node[0] = fgGetNode("/radios/comm[0]/frequencies/selected-mhz", true);
85 comm_node[1] = fgGetNode("/radios/comm[1]/frequencies/selected-mhz", true);
86 lon_node = fgGetNode("/position/longitude-deg", true);
87 lat_node = fgGetNode("/position/latitude-deg", true);
88 elev_node = fgGetNode("/position/altitude-ft", true);
89 atc_list_itr = atc_list.begin();
91 // Search for connected ATC stations once per 0.8 seconds or so
92 // globals->get_event_mgr()->add( "fgATCSearch()", fgATCSearch,
93 // FGEvent::FG_EVENT_READY, 800);
95 // For some reason the above doesn't compile - including Time/event.hxx stops compilation.
96 // Is this still true after the reorganization of the event managar??
99 // Initialise the frequency search map
100 current_commlist = new FGCommList;
101 SGPath p_comm( globals->get_fg_root() );
102 current_commlist->init( p_comm );
104 #ifdef ENABLE_AUDIO_SUPPORT
105 // Load all available voices.
106 // For now we'll do one hardwired one
109 voiceOK = v1->LoadVoice("default");
112 /* I've loaded the voice even if /sim/sound/audible is false
113 * since I know no way of forcing load of the voice if the user
114 * subsequently switches /sim/sound/audible to true. */
119 // Initialise the ATC Dialogs
120 //cout << "Initing Transmissions..." << endl;
121 SG_LOG(SG_ATC, SG_INFO, " ATC Transmissions");
122 current_transmissionlist = new FGTransmissionList;
123 SGPath p_transmission( globals->get_fg_root() );
124 p_transmission.append( "ATC/default.transmissions" );
125 current_transmissionlist->init( p_transmission );
126 //cout << "Done Transmissions" << endl;
128 SG_LOG(SG_ATC, SG_INFO, " ATC Dialog System");
129 current_atcdialog = new FGATCDialog;
130 current_atcdialog->Init();
135 //current_atcdialog->add_entry( "EGNX", "Request vectoring for approach", "Request Vectors" );
136 //current_atcdialog->add_entry( "EGNX", "Mayday, Mayday", "Declare Emergency" );
141 void FGATCMgr::update(double dt) {
144 SG_LOG(SG_ATC, SG_WARN, "Warning - ATCMgr::update(...) called before ATCMgr::init()");
147 //cout << "Entering update..." << endl;
148 //Traverse the list of active stations.
149 //Only update one class per update step to avoid the whole ATC system having to calculate between frames.
150 //Eventually we should only update every so many steps.
151 //cout << "In FGATCMgr::update - atc_list.size = " << atc_list.size() << '\n';
152 if(atc_list.size()) {
153 if(atc_list_itr == atc_list.end()) {
154 atc_list_itr = atc_list.begin();
156 //cout << "Updating " << (*atc_list_itr)->get_ident() << ' ' << (*atc_list_itr)->GetType() << '\n';
157 //cout << "Freq = " << (*atc_list_itr)->get_freq() << '\n';
158 (*atc_list_itr)->Update(dt * atc_list.size());
159 //cout << "Done ATC update..." << endl;
163 // Search the tuned frequencies every now and then - this should be done with the event scheduler
166 //cout << "About to AreaSearch()" << endl;
170 //cout << "About to search(1)" << endl;
174 //cout << "About to search(2)" << endl;
180 //cout << "comm1 type = " << comm_type[0] << '\n';
181 //cout << "Leaving update..." << endl;
185 // Returns frequency in KHz - should I alter this to return in MHz?
186 unsigned short int FGATCMgr::GetFrequency(string ident, atc_type tp) {
188 bool ok = current_commlist->FindByCode(ident, test, tp);
189 return(ok ? test.freq : 0);
193 // Register the fact that the AI system wants to activate an airport
194 // Might need more sophistication in this in the future - eg registration by aircraft call-sign.
195 bool FGATCMgr::AIRegisterAirport(string ident) {
196 SG_LOG(SG_ATC, SG_BULK, "AI registered airport " << ident << " with the ATC system");
197 if(airport_atc_map.find(ident) != airport_atc_map.end()) {
198 airport_atc_map[ident]->set_by_AI = true;
199 airport_atc_map[ident]->numAI++;
203 if(dclFindAirportID(ident, &ap)) {
204 //cout << "ident = " << ident << '\n';
205 AirportATC *a = new AirportATC;
206 // I'm not entirely sure that this AirportATC structure business is actually needed - it just duplicates what we can find out anyway!
207 a->lon = ap.longitude;
208 a->lat = ap.latitude;
209 a->elev = ap.elevation;
210 a->atis_freq = GetFrequency(ident, ATIS);
211 //cout << "ATIS freq = " << a->atis_freq << '\n';
212 a->atis_active = false;
213 a->tower_freq = GetFrequency(ident, TOWER);
214 //cout << "Tower freq = " << a->tower_freq << '\n';
215 a->tower_active = false;
216 a->ground_freq = GetFrequency(ident, GROUND);
217 //cout << "Ground freq = " << a->ground_freq << '\n';
218 a->ground_active = false;
219 // TODO - some airports will have a tower/ground frequency but be inactive overnight.
222 airport_atc_map[ident] = a;
230 // Register the fact that the comm radio is tuned to an airport
231 // Channel is zero based
232 bool FGATCMgr::CommRegisterAirport(string ident, int chan) {
233 SG_LOG(SG_ATC, SG_BULK, "Comm channel " << chan << " registered airport " << ident);
234 if(airport_atc_map.find(ident) != airport_atc_map.end()) {
235 //cout << "IN MAP - flagging set by comm..." << endl;
237 airport_atc_map[ident]->set_by_comm[0] = true;
238 } else if(chan == 1) {
239 airport_atc_map[ident]->set_by_comm[1] = true;
241 SG_LOG(SG_ATC, SG_ALERT, "COMM CHANNEL NOT 0 OR 1 IN FGATCMGR!");
242 // Just register both and accept the fact that this ATC will probably never get removed!
243 airport_atc_map[ident]->set_by_comm[0] = true;
244 airport_atc_map[ident]->set_by_comm[1] = true;
248 //cout << "NOT IN MAP - creating new..." << endl;
250 if(dclFindAirportID(ident, &ap)) {
251 AirportATC *a = new AirportATC;
252 // I'm not entirely sure that this AirportATC structure business is actually needed - it just duplicates what we can find out anyway!
253 a->lon = ap.longitude;
254 a->lat = ap.latitude;
255 a->elev = ap.elevation;
256 a->atis_freq = GetFrequency(ident, ATIS);
257 a->atis_active = false;
258 a->tower_freq = GetFrequency(ident, TOWER);
259 a->tower_active = false;
260 a->ground_freq = GetFrequency(ident, GROUND);
261 a->ground_active = false;
262 // TODO - some airports will have a tower/ground frequency but be inactive overnight.
263 a->set_by_AI = false;
266 a->set_by_comm[0] = true;
267 } else if(chan == 1) {
268 a->set_by_comm[1] = true;
270 SG_LOG(SG_ATC, SG_ALERT, "COMM CHANNEL NOT 1 OR 2 IN FGATCMGR!");
271 // Just register both and accept the fact that this ATC will probably never get removed!
272 a->set_by_comm[0] = true;
273 a->set_by_comm[1] = true;
275 airport_atc_map[ident] = a;
283 // Remove from list only if not needed by the AI system or the other comm channel
284 // Note that chan is zero based.
285 void FGATCMgr::CommRemoveFromList(const char* id, atc_type tp, int chan) {
286 SG_LOG(SG_ATC, SG_BULK, "CommRemoveFromList called for airport " << id << " " << tp << " by channel " << chan);
287 if(airport_atc_map.find((string)id) != airport_atc_map.end()) {
288 AirportATC* a = airport_atc_map[(string)id];
289 //cout << "In CommRemoveFromList, a->ground_freq = " << a->ground_freq << endl;
290 if(a->set_by_AI && tp != ATIS) {
291 SG_LOG(SG_ATC, SG_BULK, "In CommRemoveFromList, service was set by AI\n");
293 FGATC* aptr = GetATCPointer((string)id, tp);
296 //cout << "chan 1\n";
297 a->set_by_comm[0] = false;
298 if(!a->set_by_comm[1]) {
299 //cout << "not set by comm2\n";
301 //cout << "Got pointer\n";
302 aptr->SetNoDisplay();
303 //cout << "Setting no display...\n";
305 //cout << "Not got pointer\n";
310 a->set_by_comm[1] = false;
311 if(!a->set_by_comm[0]) {
313 aptr->SetNoDisplay();
314 //cout << "Setting no display...\n";
319 airport_atc_map[(string)id] = a;
324 a->set_by_comm[0] = false;
325 // Remove only if not also set by the other comm channel
326 if(!a->set_by_comm[1]) {
327 RemoveFromList(id, tp);
331 a->set_by_comm[1] = false;
332 if(!a->set_by_comm[0]) {
333 RemoveFromList(id, tp);
342 // Remove from list - should only be called from above or similar
343 // This function *will* remove it from the list regardless of who else might want it.
344 void FGATCMgr::RemoveFromList(const char* id, atc_type tp) {
345 //cout << "Requested type = " << tp << '\n';
346 //cout << "id = " << id << '\n';
347 atc_list_itr = atc_list.begin();
348 while(atc_list_itr != atc_list.end()) {
349 //cout << "type = " << (*atc_list_itr)->GetType() << '\n';
350 //cout << "Ident = " << (*atc_list_itr)->get_ident() << '\n';
351 if( (!strcmp((*atc_list_itr)->get_ident(), id))
352 && ((*atc_list_itr)->GetType() == tp) ) {
353 //Before removing it stop it transmitting!!
354 //cout << "OBLITERATING FROM LIST!!!\n";
355 (*atc_list_itr)->SetNoDisplay();
356 (*atc_list_itr)->Update(0.00833);
357 delete (*atc_list_itr);
358 atc_list_itr = atc_list.erase(atc_list_itr);
360 } // Note that that can upset where we are in the list but that doesn't really matter
366 // Find in list - return a currently active ATC pointer given ICAO code and type
367 // Return NULL if the given service is not in the list
368 // - *** THE CALLING FUNCTION MUST CHECK FOR THIS ***
369 FGATC* FGATCMgr::FindInList(const char* id, atc_type tp) {
370 atc_list_itr = atc_list.begin();
371 while(atc_list_itr != atc_list.end()) {
372 if( (!strcmp((*atc_list_itr)->get_ident(), id))
373 && ((*atc_list_itr)->GetType() == tp) ) {
374 return(*atc_list_itr);
375 } // Note that that can upset where we are in the list but that shouldn't really matter
378 // If we get here it's not in the list
382 // Returns true if the airport is found in the map
383 bool FGATCMgr::GetAirportATCDetails(string icao, AirportATC* a) {
384 if(airport_atc_map.find(icao) != airport_atc_map.end()) {
385 *a = *airport_atc_map[icao];
393 // Return a pointer to a given sort of ATC at a given airport and activate if necessary
394 // Returns NULL if service doesn't exist - calling function should check for this.
395 // We really ought to make this private and call it from the CommRegisterAirport / AIRegisterAirport functions
396 // - at the moment all these GetATC... functions exposed are just too complicated.
397 FGATC* FGATCMgr::GetATCPointer(string icao, atc_type type) {
398 if(airport_atc_map.find(icao) == airport_atc_map.end()) {
401 AirportATC *a = airport_atc_map[icao];
402 //cout << "a->lon = " << a->lon << '\n';
403 //cout << "a->elev = " << a->elev << '\n';
404 //cout << "a->tower_freq = " << a->tower_freq << '\n';
407 if(a->tower_active) {
408 // Get the pointer from the list
409 return(FindInList(icao.c_str(), type));
412 if(current_commlist->FindByFreq(a->lon, a->lat, a->elev, a->tower_freq, &data, TOWER)) {
413 FGTower* t = new FGTower;
415 atc_list.push_back(t);
416 a->tower_active = true;
417 airport_atc_map[icao] = a;
421 SG_LOG(SG_ATC, SG_ALERT, "ERROR - tower that should exist in FGATCMgr::GetATCPointer for airport " << icao << " not found");
428 SG_LOG(SG_ATC, SG_ALERT, "ERROR - ATIS station should not be requested from FGATCMgr::GetATCPointer");
431 //cout << "IN CASE GROUND" << endl;
432 if(a->ground_active) {
433 // Get the pointer from the list
434 return(FindInList(icao.c_str(), type));
437 if(current_commlist->FindByFreq(a->lon, a->lat, a->elev, a->ground_freq, &data, GROUND)) {
438 FGGround* g = new FGGround;
440 atc_list.push_back(g);
441 a->ground_active = true;
442 airport_atc_map[icao] = a;
446 SG_LOG(SG_ATC, SG_ALERT, "ERROR - ground control that should exist in FGATCMgr::GetATCPointer for airport " << icao << " not found");
458 SG_LOG(SG_ATC, SG_ALERT, "ERROR IN FGATCMgr - reached end of GetATCPointer");
463 // Return a pointer to an appropriate voice for a given type of ATC
464 // creating the voice if necessary - ie. make sure exactly one copy
465 // of every voice in use exists in memory.
467 // TODO - in the future this will get more complex and dole out country/airport
468 // specific voices, and possible make sure that the same voice doesn't get used
469 // at different airports in quick succession if a large enough selection are available.
470 FGATCVoice* FGATCMgr::GetVoicePointer(atc_type type) {
471 // TODO - implement me better - maintain a list of loaded voices and other voices!!
493 // Display a dialog box with options relevant to the currently tuned ATC service.
494 void FGATCMgr::doPopupDialog() {
495 ATCDoDialog(comm_type[0]); // FIXME - currently hardwired to comm1
498 // Search for ATC stations by frequency
499 void FGATCMgr::FreqSearch(int channel) {
500 int chan = channel - 1; // Convert to zero-based for the arrays
503 double freq = comm_node[chan]->getDoubleValue();
504 lon = lon_node->getDoubleValue();
505 lat = lat_node->getDoubleValue();
506 elev = elev_node->getDoubleValue() * SG_FEET_TO_METER;
508 // Query the data store and get the closest match if any
509 if(current_commlist->FindByFreq(lon, lat, elev, freq, &data)) {
512 // If this channel not previously valid then easy - add ATC to list
513 // If this channel was valid then - Have we tuned to a different service?
514 // If so - de-register one and add the other
515 if(comm_valid[chan]) {
516 if((comm_ident[chan] == data.ident) && (comm_type[chan] == data.type)) {
517 // Then we're still tuned into the same service so do nought and return
520 // Something's changed - either the location or the service type
521 // We need to feed the channel in so we're not removing it if we're also tuned in on the other channel
522 CommRemoveFromList(comm_ident[chan], comm_type[chan], chan);
525 // At this point we can assume that we need to add the service.
526 comm_ident[chan] = (data.ident).c_str();
527 comm_type[chan] = data.type;
528 comm_x[chan] = (double)data.x;
529 comm_y[chan] = (double)data.y;
530 comm_z[chan] = (double)data.z;
531 comm_lon[chan] = (double)data.lon;
532 comm_lat[chan] = (double)data.lat;
533 comm_elev[chan] = (double)data.elev;
534 comm_valid[chan] = true;
536 // This was a switch-case statement but the compiler didn't like the new variable creation with it.
537 if(comm_type[chan] == ATIS) {
538 CommRegisterAirport(comm_ident[chan], chan);
539 FGATC* app = FindInList(comm_ident[chan], ATIS);
541 // The station is already in the ATC list
542 //cout << "In list - flagging SetDisplay..." << endl;
545 // Generate the station and put in the ATC list
546 //cout << "Not in list - generating..." << endl;
547 FGATIS* a = new FGATIS;
549 comm_atc_ptr[chan] = a;
552 atc_list.push_back(a);
554 } else if (comm_type[chan] == TOWER) {
555 CommRegisterAirport(comm_ident[chan], chan);
556 //cout << "Done (TOWER)" << endl;
557 FGATC* app = FindInList(comm_ident[chan], TOWER);
559 // The station is already in the ATC list
560 //cout << "In list - flagging SetDisplay..." << endl;
563 // Generate the station and put in the ATC list
564 //cout << "Not in list - generating..." << endl;
565 FGTower* t = new FGTower;
567 comm_atc_ptr[chan] = t;
570 atc_list.push_back(t);
572 } else if (comm_type[chan] == GROUND) {
573 CommRegisterAirport(comm_ident[chan], chan);
574 //cout << "Done (GROUND)" << endl;
575 FGATC* app = FindInList(comm_ident[chan], GROUND);
577 // The station is already in the ATC list
580 // Generate the station and put in the ATC list
581 FGGround* g = new FGGround;
583 comm_atc_ptr[chan] = g;
586 atc_list.push_back(g);
588 } else if (comm_type[chan] == APPROACH) {
589 // We have to be a bit more carefull here since approaches are also searched by area
590 CommRegisterAirport(comm_ident[chan], chan);
591 //cout << "Done (APPROACH)" << endl;
592 FGATC* app = FindInList(comm_ident[chan], APPROACH);
594 // The station is already in the ATC list
595 app->AddPlane("Player");
597 comm_atc_ptr[chan] = app;
599 // Generate the station and put in the ATC list
600 FGApproach* a = new FGApproach;
602 a->AddPlane("Player");
603 atc_list.push_back(a);
604 comm_atc_ptr[chan] = a;
608 if(comm_valid[chan]) {
609 if(comm_type[chan] != APPROACH) {
610 // Currently approaches are removed by Alexander's out-of-range mechanism
611 CommRemoveFromList(comm_ident[chan], comm_type[chan], chan);
613 // Note that we *don't* call SetNoDisplay() here because the other comm channel
614 // might be tuned into the same station - this is handled by CommRemoveFromList(...)
615 comm_type[chan] = INVALID;
616 comm_atc_ptr[chan] = NULL;
617 comm_valid[chan] = false;
623 // Search ATC stations by area in order that we appear 'on the radar'
624 void FGATCMgr::AreaSearch() {
625 // Search for Approach stations
626 comm_list_type approaches;
627 comm_list_iterator app_itr;
629 lon = lon_node->getDoubleValue();
630 lat = lat_node->getDoubleValue();
631 elev = elev_node->getDoubleValue() * SG_FEET_TO_METER;
633 // search stations in range
634 int num_app = current_commlist->FindByPos(lon, lat, elev, 100.0, &approaches, APPROACH);
636 //cout << num_app << " approaches found in radiostack search !!!!" << endl;
638 for(app_itr = approaches.begin(); app_itr != approaches.end(); app_itr++) {
640 FGATC* app = FindInList((app_itr->ident).c_str(), app_itr->type);
642 // The station is already in the ATC list
643 app->AddPlane("Player");
646 // Generate the station and put in the ATC list
647 FGApproach* a = new FGApproach;
648 a->SetData(&(*app_itr));
649 a->AddPlane("Player");
651 atc_list.push_back(a);
656 // remove planes which are out of range
657 // TODO - I'm not entirely sure that this belongs here.
658 atc_list_itr = atc_list.begin();
659 while(atc_list_itr != atc_list.end()) {
660 if((*atc_list_itr)->GetType() == APPROACH ) {
661 int np = (*atc_list_itr)->RemovePlane();
662 // if approach has no planes left remove it from ATC list
664 (*atc_list_itr)->SetNoDisplay();
665 (*atc_list_itr)->Update(0.00833);
666 delete (*atc_list_itr);
667 atc_list_itr = atc_list.erase(atc_list_itr);
668 break; // the other stations will be checked next time