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 for(int i=0; i<ATC_NUM_TYPES; ++i) {
53 set_by_comm[0][i] = false;
54 set_by_comm[1][i] = false;
58 FGATCMgr::FGATCMgr() {
61 last_comm_ident[0] = "";
62 last_comm_ident[1] = "";
64 last_in_range = false;
65 comm_type[0] = INVALID;
66 comm_type[1] = INVALID;
67 comm_atc_ptr[0] = NULL;
68 comm_atc_ptr[1] = NULL;
69 comm_valid[0] = false;
70 comm_valid[1] = false;
75 FGATCMgr::~FGATCMgr() {
79 void FGATCMgr::bind() {
82 void FGATCMgr::unbind() {
85 void FGATCMgr::init() {
86 comm_node[0] = fgGetNode("/radios/comm[0]/frequencies/selected-mhz", true);
87 comm_node[1] = fgGetNode("/radios/comm[1]/frequencies/selected-mhz", true);
88 lon_node = fgGetNode("/position/longitude-deg", true);
89 lat_node = fgGetNode("/position/latitude-deg", true);
90 elev_node = fgGetNode("/position/altitude-ft", true);
91 atc_list_itr = atc_list.begin();
93 // Search for connected ATC stations once per 0.8 seconds or so
94 // globals->get_event_mgr()->add( "fgATCSearch()", fgATCSearch,
95 // FGEvent::FG_EVENT_READY, 800);
97 // For some reason the above doesn't compile - including Time/event.hxx stops compilation.
98 // Is this still true after the reorganization of the event managar??
101 // Initialise the frequency search map
102 current_commlist = new FGCommList;
103 SGPath p_comm( globals->get_fg_root() );
104 current_commlist->init( p_comm );
106 #ifdef ENABLE_AUDIO_SUPPORT
107 // Load all available voices.
108 // For now we'll do one hardwired one
111 voiceOK = v1->LoadVoice("default");
114 /* I've loaded the voice even if /sim/sound/audible is false
115 * since I know no way of forcing load of the voice if the user
116 * subsequently switches /sim/sound/audible to true. */
121 // Initialise the ATC Dialogs
122 //cout << "Initing Transmissions..." << endl;
123 SG_LOG(SG_ATC, SG_INFO, " ATC Transmissions");
124 current_transmissionlist = new FGTransmissionList;
125 SGPath p_transmission( globals->get_fg_root() );
126 p_transmission.append( "ATC/default.transmissions" );
127 current_transmissionlist->init( p_transmission );
128 //cout << "Done Transmissions" << endl;
130 SG_LOG(SG_ATC, SG_INFO, " ATC Dialog System");
131 current_atcdialog = new FGATCDialog;
132 current_atcdialog->Init();
137 //current_atcdialog->add_entry( "EGNX", "Request vectoring for approach", "Request Vectors" );
138 //current_atcdialog->add_entry( "EGNX", "Mayday, Mayday", "Declare Emergency" );
143 void FGATCMgr::update(double dt) {
146 SG_LOG(SG_ATC, SG_WARN, "Warning - ATCMgr::update(...) called before ATCMgr::init()");
149 //cout << "Entering update..." << endl;
150 //Traverse the list of active stations.
151 //Only update one class per update step to avoid the whole ATC system having to calculate between frames.
152 //Eventually we should only update every so many steps.
153 //cout << "In FGATCMgr::update - atc_list.size = " << atc_list.size() << '\n';
154 if(atc_list.size()) {
155 if(atc_list_itr == atc_list.end()) {
156 atc_list_itr = atc_list.begin();
158 //cout << "Updating " << (*atc_list_itr)->get_ident() << ' ' << (*atc_list_itr)->GetType() << '\n';
159 //cout << "Freq = " << (*atc_list_itr)->get_freq() << '\n';
160 (*atc_list_itr)->Update(dt * atc_list.size());
161 //cout << "Done ATC update..." << endl;
165 // Search the tuned frequencies every now and then - this should be done with the event scheduler
166 static int i = 0; // Very ugly - but there should only ever be one instance of FGATCMgr.
169 //cout << "About to AreaSearch()" << endl;
174 //cout << "About to search(1)" << endl;
178 //cout << "About to search(2)" << endl;
184 //cout << "comm1 type = " << comm_type[0] << '\n';
185 //cout << "Leaving update..." << endl;
189 // Returns frequency in KHz - should I alter this to return in MHz?
190 unsigned short int FGATCMgr::GetFrequency(string ident, atc_type tp) {
192 bool ok = current_commlist->FindByCode(ident, test, tp);
193 return(ok ? test.freq : 0);
197 // Register the fact that the AI system wants to activate an airport
198 // Might need more sophistication in this in the future - eg registration by aircraft call-sign.
199 bool FGATCMgr::AIRegisterAirport(string ident) {
200 SG_LOG(SG_ATC, SG_BULK, "AI registered airport " << ident << " with the ATC system");
201 if(airport_atc_map.find(ident) != airport_atc_map.end()) {
202 airport_atc_map[ident]->set_by_AI = true;
203 airport_atc_map[ident]->numAI++;
207 if(dclFindAirportID(ident, &ap)) {
208 //cout << "ident = " << ident << '\n';
209 AirportATC *a = new AirportATC;
210 // I'm not entirely sure that this AirportATC structure business is actually needed - it just duplicates what we can find out anyway!
211 a->lon = ap.longitude;
212 a->lat = ap.latitude;
213 a->elev = ap.elevation;
214 a->atis_freq = GetFrequency(ident, ATIS);
215 //cout << "ATIS freq = " << a->atis_freq << '\n';
216 a->atis_active = false;
217 a->tower_freq = GetFrequency(ident, TOWER);
218 //cout << "Tower freq = " << a->tower_freq << '\n';
219 a->tower_active = false;
220 a->ground_freq = GetFrequency(ident, GROUND);
221 //cout << "Ground freq = " << a->ground_freq << '\n';
222 a->ground_active = false;
223 // TODO - some airports will have a tower/ground frequency but be inactive overnight.
226 airport_atc_map[ident] = a;
234 // Register the fact that the comm radio is tuned to an airport
235 // Channel is zero based
236 bool FGATCMgr::CommRegisterAirport(string ident, int chan, atc_type tp) {
237 SG_LOG(SG_ATC, SG_BULK, "Comm channel " << chan << " registered airport " << ident);
238 if(airport_atc_map.find(ident) != airport_atc_map.end()) {
239 //cout << "IN MAP - flagging set by comm..." << endl;
240 airport_atc_map[ident]->set_by_comm[chan][tp] = true;
243 //cout << "NOT IN MAP - creating new..." << endl;
245 if(dclFindAirportID(ident, &ap)) {
246 AirportATC *a = new AirportATC;
247 // I'm not entirely sure that this AirportATC structure business is actually needed - it just duplicates what we can find out anyway!
248 a->lon = ap.longitude;
249 a->lat = ap.latitude;
250 a->elev = ap.elevation;
251 a->atis_freq = GetFrequency(ident, ATIS);
252 a->atis_active = false;
253 a->tower_freq = GetFrequency(ident, TOWER);
254 a->tower_active = false;
255 a->ground_freq = GetFrequency(ident, GROUND);
256 a->ground_active = false;
257 // TODO - some airports will have a tower/ground frequency but be inactive overnight.
258 a->set_by_AI = false;
260 a->set_by_comm[chan][tp] = true;
261 airport_atc_map[ident] = a;
269 // Remove from list only if not needed by the AI system or the other comm channel
270 // Note that chan is zero based.
271 void FGATCMgr::CommRemoveFromList(const char* id, atc_type tp, int chan) {
272 SG_LOG(SG_ATC, SG_BULK, "CommRemoveFromList called for airport " << id << " " << tp << " by channel " << chan);
273 if(airport_atc_map.find(id) != airport_atc_map.end()) {
274 AirportATC* a = airport_atc_map[id];
275 //cout << "In CommRemoveFromList, a->ground_freq = " << a->ground_freq << endl;
276 if(a->set_by_AI && tp != ATIS) {
277 // Set by AI, so don't remove simply because user isn't tuned in any more - just stop displaying
278 SG_LOG(SG_ATC, SG_BULK, "In CommRemoveFromList, service was set by AI\n");
279 FGATC* aptr = GetATCPointer(id, tp);
282 //cout << "chan 1\n";
283 a->set_by_comm[0][tp] = false;
284 if(!a->set_by_comm[1][tp]) {
285 //cout << "not set by comm2\n";
287 //cout << "Got pointer\n";
288 aptr->SetNoDisplay();
289 //cout << "Setting no display...\n";
291 //cout << "Not got pointer\n";
296 a->set_by_comm[1][tp] = false;
297 if(!a->set_by_comm[0][tp]) {
299 aptr->SetNoDisplay();
300 //cout << "Setting no display...\n";
305 airport_atc_map[id] = a;
310 a->set_by_comm[0][tp] = false;
311 // Remove only if not also set by the other comm channel
312 if(!a->set_by_comm[1][tp]) {
313 RemoveFromList(id, tp);
317 a->set_by_comm[1][tp] = false;
318 if(!a->set_by_comm[0][tp]) {
319 RemoveFromList(id, tp);
328 // Remove from list - should only be called from above or similar
329 // This function *will* remove it from the list regardless of who else might want it.
330 void FGATCMgr::RemoveFromList(const char* id, atc_type tp) {
331 //cout << "Requested type = " << tp << '\n';
332 //cout << "id = " << id << '\n';
333 atc_list_itr = atc_list.begin();
334 while(atc_list_itr != atc_list.end()) {
335 //cout << "type = " << (*atc_list_itr)->GetType() << '\n';
336 //cout << "Ident = " << (*atc_list_itr)->get_ident() << '\n';
337 if( (!strcmp((*atc_list_itr)->get_ident(), id))
338 && ((*atc_list_itr)->GetType() == tp) ) {
339 //Before removing it stop it transmitting!!
340 //cout << "OBLITERATING FROM LIST!!!\n";
341 (*atc_list_itr)->SetNoDisplay();
342 (*atc_list_itr)->Update(0.00833);
343 delete (*atc_list_itr);
344 atc_list_itr = atc_list.erase(atc_list_itr);
346 } // Note that that can upset where we are in the list but that doesn't really matter
352 // Find in list - return a currently active ATC pointer given ICAO code and type
353 // Return NULL if the given service is not in the list
354 // - *** THE CALLING FUNCTION MUST CHECK FOR THIS ***
355 FGATC* FGATCMgr::FindInList(const char* id, atc_type tp) {
356 atc_list_itr = atc_list.begin();
357 while(atc_list_itr != atc_list.end()) {
358 if( (!strcmp((*atc_list_itr)->get_ident(), id))
359 && ((*atc_list_itr)->GetType() == tp) ) {
360 return(*atc_list_itr);
361 } // Note that that can upset where we are in the list but that shouldn't really matter
364 // If we get here it's not in the list
368 // Returns true if the airport is found in the map
369 bool FGATCMgr::GetAirportATCDetails(string icao, AirportATC* a) {
370 if(airport_atc_map.find(icao) != airport_atc_map.end()) {
371 *a = *airport_atc_map[icao];
379 // Return a pointer to a given sort of ATC at a given airport and activate if necessary
380 // Returns NULL if service doesn't exist - calling function should check for this.
381 // We really ought to make this private and call it from the CommRegisterAirport / AIRegisterAirport functions
382 // - at the moment all these GetATC... functions exposed are just too complicated.
383 FGATC* FGATCMgr::GetATCPointer(string icao, atc_type type) {
384 if(airport_atc_map.find(icao) == airport_atc_map.end()) {
387 AirportATC *a = airport_atc_map[icao];
388 //cout << "a->lon = " << a->lon << '\n';
389 //cout << "a->elev = " << a->elev << '\n';
390 //cout << "a->tower_freq = " << a->tower_freq << '\n';
393 if(a->tower_active) {
394 // Get the pointer from the list
395 return(FindInList(icao.c_str(), type));
398 if(current_commlist->FindByFreq(a->lon, a->lat, a->elev, a->tower_freq, &data, TOWER)) {
399 FGTower* t = new FGTower;
401 atc_list.push_back(t);
402 a->tower_active = true;
403 airport_atc_map[icao] = a;
407 SG_LOG(SG_ATC, SG_ALERT, "ERROR - tower that should exist in FGATCMgr::GetATCPointer for airport " << icao << " not found");
414 SG_LOG(SG_ATC, SG_ALERT, "ERROR - ATIS station should not be requested from FGATCMgr::GetATCPointer");
417 //cout << "IN CASE GROUND" << endl;
418 if(a->ground_active) {
419 // Get the pointer from the list
420 return(FindInList(icao.c_str(), type));
423 if(current_commlist->FindByFreq(a->lon, a->lat, a->elev, a->ground_freq, &data, GROUND)) {
424 FGGround* g = new FGGround;
426 atc_list.push_back(g);
427 a->ground_active = true;
428 airport_atc_map[icao] = a;
432 SG_LOG(SG_ATC, SG_ALERT, "ERROR - ground control that should exist in FGATCMgr::GetATCPointer for airport " << icao << " not found");
444 SG_LOG(SG_ATC, SG_ALERT, "ERROR IN FGATCMgr - reached end of GetATCPointer");
449 // Return a pointer to an appropriate voice for a given type of ATC
450 // creating the voice if necessary - ie. make sure exactly one copy
451 // of every voice in use exists in memory.
453 // TODO - in the future this will get more complex and dole out country/airport
454 // specific voices, and possible make sure that the same voice doesn't get used
455 // at different airports in quick succession if a large enough selection are available.
456 FGATCVoice* FGATCMgr::GetVoicePointer(atc_type type) {
457 // TODO - implement me better - maintain a list of loaded voices and other voices!!
479 // Display a dialog box with options relevant to the currently tuned ATC service.
480 void FGATCMgr::doPopupDialog() {
481 ATCDoDialog(comm_type[0]); // FIXME - currently hardwired to comm1
484 // Search for ATC stations by frequency
485 void FGATCMgr::FreqSearch(int channel) {
486 int chan = channel - 1; // Convert to zero-based for the arrays
489 double freq = comm_node[chan]->getDoubleValue();
490 lon = lon_node->getDoubleValue();
491 lat = lat_node->getDoubleValue();
492 elev = elev_node->getDoubleValue() * SG_FEET_TO_METER;
494 // Query the data store and get the closest match if any
495 if(current_commlist->FindByFreq(lon, lat, elev, freq, &data)) {
498 // If this channel not previously valid then easy - add ATC to list
499 // If this channel was valid then - Have we tuned to a different service?
500 // If so - de-register one and add the other
501 if(comm_valid[chan]) {
502 if((comm_ident[chan] == data.ident) && (comm_type[chan] == data.type)) {
503 // Then we're still tuned into the same service so do nought and return
506 // Something's changed - either the location or the service type
507 // We need to feed the channel in so we're not removing it if we're also tuned in on the other channel
508 CommRemoveFromList(comm_ident[chan], comm_type[chan], chan);
511 // At this point we can assume that we need to add the service.
512 comm_ident[chan] = (data.ident).c_str();
513 comm_type[chan] = data.type;
514 comm_x[chan] = (double)data.x;
515 comm_y[chan] = (double)data.y;
516 comm_z[chan] = (double)data.z;
517 comm_lon[chan] = (double)data.lon;
518 comm_lat[chan] = (double)data.lat;
519 comm_elev[chan] = (double)data.elev;
520 comm_valid[chan] = true;
522 // This was a switch-case statement but the compiler didn't like the new variable creation with it.
523 if(comm_type[chan] == ATIS) {
524 CommRegisterAirport(comm_ident[chan], chan, ATIS);
525 FGATC* app = FindInList(comm_ident[chan], ATIS);
527 // The station is already in the ATC list
528 //cout << "In list - flagging SetDisplay..." << endl;
531 // Generate the station and put in the ATC list
532 //cout << "Not in list - generating..." << endl;
533 FGATIS* a = new FGATIS;
535 comm_atc_ptr[chan] = a;
538 atc_list.push_back(a);
540 } else if (comm_type[chan] == TOWER) {
541 CommRegisterAirport(comm_ident[chan], chan, TOWER);
542 //cout << "Done (TOWER)" << endl;
543 FGATC* app = FindInList(comm_ident[chan], TOWER);
545 // The station is already in the ATC list
546 //cout << "In list - flagging SetDisplay..." << endl;
549 // Generate the station and put in the ATC list
550 //cout << "Not in list - generating..." << endl;
551 FGTower* t = new FGTower;
553 comm_atc_ptr[chan] = t;
556 atc_list.push_back(t);
558 } else if (comm_type[chan] == GROUND) {
559 CommRegisterAirport(comm_ident[chan], chan, GROUND);
560 //cout << "Done (GROUND)" << endl;
561 FGATC* app = FindInList(comm_ident[chan], GROUND);
563 // The station is already in the ATC list
566 // Generate the station and put in the ATC list
567 FGGround* g = new FGGround;
569 comm_atc_ptr[chan] = g;
572 atc_list.push_back(g);
574 } else if (comm_type[chan] == APPROACH) {
575 // We have to be a bit more carefull here since approaches are also searched by area
576 CommRegisterAirport(comm_ident[chan], chan, APPROACH);
577 //cout << "Done (APPROACH)" << endl;
578 FGATC* app = FindInList(comm_ident[chan], APPROACH);
580 // The station is already in the ATC list
581 app->AddPlane("Player");
583 comm_atc_ptr[chan] = app;
585 // Generate the station and put in the ATC list
586 FGApproach* a = new FGApproach;
588 comm_atc_ptr[chan] = a;
591 a->AddPlane("Player");
592 atc_list.push_back(a);
596 if(comm_valid[chan]) {
597 if(comm_type[chan] != APPROACH) {
598 // Currently approaches are removed by Alexander's out-of-range mechanism
599 CommRemoveFromList(comm_ident[chan], comm_type[chan], chan);
601 // Note that we *don't* call SetNoDisplay() here because the other comm channel
602 // might be tuned into the same station - this is handled by CommRemoveFromList(...)
603 comm_type[chan] = INVALID;
604 comm_atc_ptr[chan] = NULL;
605 comm_valid[chan] = false;
610 // Search ATC stations by area in order that we appear 'on the radar'
611 void FGATCMgr::AreaSearch() {
612 // Search for Approach stations
613 comm_list_type approaches;
614 comm_list_iterator app_itr;
616 lon = lon_node->getDoubleValue();
617 lat = lat_node->getDoubleValue();
618 elev = elev_node->getDoubleValue() * SG_FEET_TO_METER;
620 // search stations in range
621 int num_app = current_commlist->FindByPos(lon, lat, elev, 100.0, &approaches, APPROACH);
623 //cout << num_app << " approaches found in radiostack search !!!!" << endl;
625 for(app_itr = approaches.begin(); app_itr != approaches.end(); app_itr++) {
627 FGATC* app = FindInList((app_itr->ident).c_str(), app_itr->type);
629 // The station is already in the ATC list
630 //cout << "In list adding player\n";
631 app->AddPlane("Player");
634 // Generate the station and put in the ATC list
635 FGApproach* a = new FGApproach;
636 a->SetData(&(*app_itr));
637 //cout << "Adding player\n";
638 a->AddPlane("Player");
640 atc_list.push_back(a);
645 // remove planes which are out of range
646 // TODO - I'm not entirely sure that this belongs here.
647 atc_list_itr = atc_list.begin();
648 while(atc_list_itr != atc_list.end()) {
649 if((*atc_list_itr)->GetType() == APPROACH ) {
650 int np = (*atc_list_itr)->RemovePlane();
651 // if approach has no planes left remove it from ATC list
653 (*atc_list_itr)->SetNoDisplay();
654 (*atc_list_itr)->Update(0.00833);
655 delete (*atc_list_itr);
656 atc_list_itr = atc_list.erase(atc_list_itr);
657 break; // the other stations will be checked next time