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 <Time/event.hxx>
23 #include <simgear/misc/sg_path.hxx>
24 #include <simgear/debug/logstream.hxx>
25 #include <Airports/simple.hxx>
28 #include "commlist.hxx"
29 #include "ATCdisplay.hxx"
30 #include "ATCDialog.hxx"
31 #include "ATCutils.hxx"
34 // periodic radio station search wrapper
35 static void fgATCSearch( void ) {
36 globals->get_ATC_mgr()->Search();
38 */ //This wouldn't compile - including Time/event.hxx breaks it :-(
40 AirportATC::AirportATC() :
53 set_by_comm[0] = false;
54 set_by_comm[1] = false;
57 FGATCMgr::FGATCMgr() {
60 last_comm_ident[0] = "";
61 last_comm_ident[1] = "";
63 last_in_range = false;
64 comm_type[0] = INVALID;
65 comm_type[1] = INVALID;
66 comm_atc_ptr[0] = NULL;
67 comm_atc_ptr[1] = NULL;
68 comm_valid[0] = false;
69 comm_valid[1] = false;
74 FGATCMgr::~FGATCMgr() {
78 void FGATCMgr::bind() {
81 void FGATCMgr::unbind() {
84 void FGATCMgr::init() {
85 comm_node[0] = fgGetNode("/radios/comm[0]/frequencies/selected-mhz", true);
86 comm_node[1] = fgGetNode("/radios/comm[1]/frequencies/selected-mhz", true);
87 lon_node = fgGetNode("/position/longitude-deg", true);
88 lat_node = fgGetNode("/position/latitude-deg", true);
89 elev_node = fgGetNode("/position/altitude-ft", true);
90 atc_list_itr = atc_list.begin();
92 // Search for connected ATC stations once per 0.8 seconds or so
93 // global_events.Register( "fgATCSearch()", fgATCSearch,
94 // fgEVENT::FG_EVENT_READY, 800);
95 // For some reason the above doesn't compile - including Time/event.hxx stops compilation.
97 // Initialise the frequency search map
98 current_commlist = new FGCommList;
99 SGPath p_comm( globals->get_fg_root() );
100 current_commlist->init( p_comm );
102 #ifdef ENABLE_AUDIO_SUPPORT
103 // Load all available voices.
104 // For now we'll do one hardwired one
107 voiceOK = v1->LoadVoice("default");
110 /* I've loaded the voice even if /sim/sound/audible is false
111 * since I know no way of forcing load of the voice if the user
112 * subsequently switches /sim/sound/audible to true. */
117 // Initialise the ATC Dialogs
118 //cout << "Initing Transmissions..." << endl;
119 SG_LOG(SG_ATC, SG_INFO, " ATC Transmissions");
120 current_transmissionlist = new FGTransmissionList;
121 SGPath p_transmission( globals->get_fg_root() );
122 p_transmission.append( "ATC/default.transmissions" );
123 current_transmissionlist->init( p_transmission );
124 //cout << "Done Transmissions" << endl;
126 SG_LOG(SG_ATC, SG_INFO, " ATC Dialog System");
127 current_atcdialog = new FGATCDialog;
128 current_atcdialog->Init();
133 //current_atcdialog->add_entry( "EGNX", "Request vectoring for approach", "Request Vectors" );
134 //current_atcdialog->add_entry( "EGNX", "Mayday, Mayday", "Declare Emergency" );
139 void FGATCMgr::update(double dt) {
142 SG_LOG(SG_ATC, SG_WARN, "Warning - ATCMgr::update(...) called before ATCMgr::init()");
145 //cout << "Entering update..." << endl;
146 //Traverse the list of active stations.
147 //Only update one class per update step to avoid the whole ATC system having to calculate between frames.
148 //Eventually we should only update every so many steps.
149 //cout << "In FGATCMgr::update - atc_list.size = " << atc_list.size() << '\n';
150 if(atc_list.size()) {
151 if(atc_list_itr == atc_list.end()) {
152 atc_list_itr = atc_list.begin();
154 //cout << "Updating " << (*atc_list_itr)->get_ident() << ' ' << (*atc_list_itr)->GetType() << '\n';
155 //cout << "Freq = " << (*atc_list_itr)->get_freq() << '\n';
156 (*atc_list_itr)->Update(dt * atc_list.size());
157 //cout << "Done ATC update..." << endl;
161 // Search the tuned frequencies every now and then - this should be done with the event scheduler
164 //cout << "About to AreaSearch()" << endl;
168 //cout << "About to search(1)" << endl;
172 //cout << "About to search(2)" << endl;
178 //cout << "comm1 type = " << comm_type[0] << '\n';
179 //cout << "Leaving update..." << endl;
183 // Returns frequency in KHz - should I alter this to return in MHz?
184 unsigned short int FGATCMgr::GetFrequency(string ident, atc_type tp) {
186 bool ok = current_commlist->FindByCode(ident, test, tp);
187 return(ok ? test.freq : 0);
191 // Register the fact that the AI system wants to activate an airport
192 // Might need more sophistication in this in the future - eg registration by aircraft call-sign.
193 bool FGATCMgr::AIRegisterAirport(string ident) {
194 SG_LOG(SG_ATC, SG_BULK, "AI registered airport " << ident << " with the ATC system");
195 if(airport_atc_map.find(ident) != airport_atc_map.end()) {
196 airport_atc_map[ident]->set_by_AI = true;
197 airport_atc_map[ident]->numAI++;
201 if(dclFindAirportID(ident, &ap)) {
202 //cout << "ident = " << ident << '\n';
203 AirportATC *a = new AirportATC;
204 // I'm not entirely sure that this AirportATC structure business is actually needed - it just duplicates what we can find out anyway!
205 a->lon = ap.longitude;
206 a->lat = ap.latitude;
207 a->elev = ap.elevation;
208 a->atis_freq = GetFrequency(ident, ATIS);
209 //cout << "ATIS freq = " << a->atis_freq << '\n';
210 a->atis_active = false;
211 a->tower_freq = GetFrequency(ident, TOWER);
212 //cout << "Tower freq = " << a->tower_freq << '\n';
213 a->tower_active = false;
214 a->ground_freq = GetFrequency(ident, GROUND);
215 //cout << "Ground freq = " << a->ground_freq << '\n';
216 a->ground_active = false;
217 // TODO - some airports will have a tower/ground frequency but be inactive overnight.
220 airport_atc_map[ident] = a;
228 // Register the fact that the comm radio is tuned to an airport
229 // Channel is zero based
230 bool FGATCMgr::CommRegisterAirport(string ident, int chan) {
231 SG_LOG(SG_ATC, SG_BULK, "Comm channel " << chan << " registered airport " << ident);
232 if(airport_atc_map.find(ident) != airport_atc_map.end()) {
233 //cout << "IN MAP - flagging set by comm..." << endl;
235 airport_atc_map[ident]->set_by_comm[0] = true;
236 } else if(chan == 1) {
237 airport_atc_map[ident]->set_by_comm[1] = true;
239 SG_LOG(SG_ATC, SG_ALERT, "COMM CHANNEL NOT 0 OR 1 IN FGATCMGR!");
240 // Just register both and accept the fact that this ATC will probably never get removed!
241 airport_atc_map[ident]->set_by_comm[0] = true;
242 airport_atc_map[ident]->set_by_comm[1] = true;
246 //cout << "NOT IN MAP - creating new..." << endl;
248 if(dclFindAirportID(ident, &ap)) {
249 AirportATC *a = new AirportATC;
250 // I'm not entirely sure that this AirportATC structure business is actually needed - it just duplicates what we can find out anyway!
251 a->lon = ap.longitude;
252 a->lat = ap.latitude;
253 a->elev = ap.elevation;
254 a->atis_freq = GetFrequency(ident, ATIS);
255 a->atis_active = false;
256 a->tower_freq = GetFrequency(ident, TOWER);
257 a->tower_active = false;
258 a->ground_freq = GetFrequency(ident, GROUND);
259 a->ground_active = false;
260 // TODO - some airports will have a tower/ground frequency but be inactive overnight.
261 a->set_by_AI = false;
264 a->set_by_comm[0] = true;
265 } else if(chan == 1) {
266 a->set_by_comm[1] = true;
268 SG_LOG(SG_ATC, SG_ALERT, "COMM CHANNEL NOT 1 OR 2 IN FGATCMGR!");
269 // Just register both and accept the fact that this ATC will probably never get removed!
270 a->set_by_comm[0] = true;
271 a->set_by_comm[1] = true;
273 airport_atc_map[ident] = a;
281 // Remove from list only if not needed by the AI system or the other comm channel
282 // Note that chan is zero based.
283 void FGATCMgr::CommRemoveFromList(const char* id, atc_type tp, int chan) {
284 SG_LOG(SG_ATC, SG_BULK, "CommRemoveFromList called for airport " << id << " " << tp << " by channel " << chan);
285 if(airport_atc_map.find((string)id) != airport_atc_map.end()) {
286 AirportATC* a = airport_atc_map[(string)id];
287 //cout << "In CommRemoveFromList, a->ground_freq = " << a->ground_freq << endl;
288 if(a->set_by_AI && tp != ATIS) {
289 SG_LOG(SG_ATC, SG_BULK, "In CommRemoveFromList, service was set by AI\n");
291 FGATC* aptr = GetATCPointer((string)id, tp);
294 //cout << "chan 1\n";
295 a->set_by_comm[0] = false;
296 if(!a->set_by_comm[1]) {
297 //cout << "not set by comm2\n";
299 //cout << "Got pointer\n";
300 aptr->SetNoDisplay();
301 //cout << "Setting no display...\n";
303 //cout << "Not got pointer\n";
308 a->set_by_comm[1] = false;
309 if(!a->set_by_comm[0]) {
311 aptr->SetNoDisplay();
312 //cout << "Setting no display...\n";
317 airport_atc_map[(string)id] = a;
322 a->set_by_comm[0] = false;
323 // Remove only if not also set by the other comm channel
324 if(!a->set_by_comm[1]) {
325 RemoveFromList(id, tp);
329 a->set_by_comm[1] = false;
330 if(!a->set_by_comm[0]) {
331 RemoveFromList(id, tp);
340 // Remove from list - should only be called from above or similar
341 // This function *will* remove it from the list regardless of who else might want it.
342 void FGATCMgr::RemoveFromList(const char* id, atc_type tp) {
343 //cout << "Requested type = " << tp << '\n';
344 //cout << "id = " << id << '\n';
345 atc_list_itr = atc_list.begin();
346 while(atc_list_itr != atc_list.end()) {
347 //cout << "type = " << (*atc_list_itr)->GetType() << '\n';
348 //cout << "Ident = " << (*atc_list_itr)->get_ident() << '\n';
349 if( (!strcmp((*atc_list_itr)->get_ident(), id))
350 && ((*atc_list_itr)->GetType() == tp) ) {
351 //Before removing it stop it transmitting!!
352 //cout << "OBLITERATING FROM LIST!!!\n";
353 (*atc_list_itr)->SetNoDisplay();
354 (*atc_list_itr)->Update(0.00833);
355 delete (*atc_list_itr);
356 atc_list_itr = atc_list.erase(atc_list_itr);
358 } // Note that that can upset where we are in the list but that doesn't really matter
364 // Find in list - return a currently active ATC pointer given ICAO code and type
365 // Return NULL if the given service is not in the list
366 // - *** THE CALLING FUNCTION MUST CHECK FOR THIS ***
367 FGATC* FGATCMgr::FindInList(const char* id, atc_type tp) {
368 atc_list_itr = atc_list.begin();
369 while(atc_list_itr != atc_list.end()) {
370 if( (!strcmp((*atc_list_itr)->get_ident(), id))
371 && ((*atc_list_itr)->GetType() == tp) ) {
372 return(*atc_list_itr);
373 } // Note that that can upset where we are in the list but that shouldn't really matter
376 // If we get here it's not in the list
380 // Returns true if the airport is found in the map
381 bool FGATCMgr::GetAirportATCDetails(string icao, AirportATC* a) {
382 if(airport_atc_map.find(icao) != airport_atc_map.end()) {
383 *a = *airport_atc_map[icao];
391 // Return a pointer to a given sort of ATC at a given airport and activate if necessary
392 // Returns NULL if service doesn't exist - calling function should check for this.
393 // We really ought to make this private and call it from the CommRegisterAirport / AIRegisterAirport functions
394 // - at the moment all these GetATC... functions exposed are just too complicated.
395 FGATC* FGATCMgr::GetATCPointer(string icao, atc_type type) {
396 if(airport_atc_map.find(icao) == airport_atc_map.end()) {
399 AirportATC *a = airport_atc_map[icao];
400 //cout << "a->lon = " << a->lon << '\n';
401 //cout << "a->elev = " << a->elev << '\n';
402 //cout << "a->tower_freq = " << a->tower_freq << '\n';
405 if(a->tower_active) {
406 // Get the pointer from the list
407 return(FindInList(icao.c_str(), type));
410 if(current_commlist->FindByFreq(a->lon, a->lat, a->elev, a->tower_freq, &data, TOWER)) {
411 FGTower* t = new FGTower;
413 atc_list.push_back(t);
414 a->tower_active = true;
415 airport_atc_map[icao] = a;
419 SG_LOG(SG_ATC, SG_ALERT, "ERROR - tower that should exist in FGATCMgr::GetATCPointer for airport " << icao << " not found");
426 SG_LOG(SG_ATC, SG_ALERT, "ERROR - ATIS station should not be requested from FGATCMgr::GetATCPointer");
429 //cout << "IN CASE GROUND" << endl;
430 if(a->ground_active) {
431 // Get the pointer from the list
432 return(FindInList(icao.c_str(), type));
435 if(current_commlist->FindByFreq(a->lon, a->lat, a->elev, a->ground_freq, &data, GROUND)) {
436 FGGround* g = new FGGround;
438 atc_list.push_back(g);
439 a->ground_active = true;
440 airport_atc_map[icao] = a;
444 SG_LOG(SG_ATC, SG_ALERT, "ERROR - ground control that should exist in FGATCMgr::GetATCPointer for airport " << icao << " not found");
456 SG_LOG(SG_ATC, SG_ALERT, "ERROR IN FGATCMgr - reached end of GetATCPointer");
461 // Return a pointer to an appropriate voice for a given type of ATC
462 // creating the voice if necessary - ie. make sure exactly one copy
463 // of every voice in use exists in memory.
465 // TODO - in the future this will get more complex and dole out country/airport
466 // specific voices, and possible make sure that the same voice doesn't get used
467 // at different airports in quick succession if a large enough selection are available.
468 FGATCVoice* FGATCMgr::GetVoicePointer(atc_type type) {
469 // TODO - implement me better - maintain a list of loaded voices and other voices!!
491 // Display a dialog box with options relevant to the currently tuned ATC service.
492 void FGATCMgr::doPopupDialog() {
493 ATCDoDialog(comm_type[0]); // FIXME - currently hardwired to comm1
496 // Search for ATC stations by frequency
497 void FGATCMgr::FreqSearch(int channel) {
498 int chan = channel - 1; // Convert to zero-based for the arrays
501 double freq = comm_node[chan]->getDoubleValue();
502 lon = lon_node->getDoubleValue();
503 lat = lat_node->getDoubleValue();
504 elev = elev_node->getDoubleValue() * SG_FEET_TO_METER;
506 // Query the data store and get the closest match if any
507 if(current_commlist->FindByFreq(lon, lat, elev, freq, &data)) {
510 // If this channel not previously valid then easy - add ATC to list
511 // If this channel was valid then - Have we tuned to a different service?
512 // If so - de-register one and add the other
513 if(comm_valid[chan]) {
514 if((comm_ident[chan] == data.ident) && (comm_type[chan] == data.type)) {
515 // Then we're still tuned into the same service so do nought and return
518 // Something's changed - either the location or the service type
519 // We need to feed the channel in so we're not removing it if we're also tuned in on the other channel
520 CommRemoveFromList(comm_ident[chan], comm_type[chan], chan);
523 // At this point we can assume that we need to add the service.
524 comm_ident[chan] = (data.ident).c_str();
525 comm_type[chan] = data.type;
526 comm_x[chan] = (double)data.x;
527 comm_y[chan] = (double)data.y;
528 comm_z[chan] = (double)data.z;
529 comm_lon[chan] = (double)data.lon;
530 comm_lat[chan] = (double)data.lat;
531 comm_elev[chan] = (double)data.elev;
532 comm_valid[chan] = true;
534 // This was a switch-case statement but the compiler didn't like the new variable creation with it.
535 if(comm_type[chan] == ATIS) {
536 CommRegisterAirport(comm_ident[chan], chan);
537 FGATC* app = FindInList(comm_ident[chan], ATIS);
539 // The station is already in the ATC list
540 //cout << "In list - flagging SetDisplay..." << endl;
543 // Generate the station and put in the ATC list
544 //cout << "Not in list - generating..." << endl;
545 FGATIS* a = new FGATIS;
547 comm_atc_ptr[chan] = a;
550 atc_list.push_back(a);
552 } else if (comm_type[chan] == TOWER) {
553 CommRegisterAirport(comm_ident[chan], chan);
554 //cout << "Done (TOWER)" << endl;
555 FGATC* app = FindInList(comm_ident[chan], TOWER);
557 // The station is already in the ATC list
558 //cout << "In list - flagging SetDisplay..." << endl;
561 // Generate the station and put in the ATC list
562 //cout << "Not in list - generating..." << endl;
563 FGTower* t = new FGTower;
565 comm_atc_ptr[chan] = t;
568 atc_list.push_back(t);
570 } else if (comm_type[chan] == GROUND) {
571 CommRegisterAirport(comm_ident[chan], chan);
572 //cout << "Done (GROUND)" << endl;
573 FGATC* app = FindInList(comm_ident[chan], GROUND);
575 // The station is already in the ATC list
578 // Generate the station and put in the ATC list
579 FGGround* g = new FGGround;
581 comm_atc_ptr[chan] = g;
584 atc_list.push_back(g);
586 } else if (comm_type[chan] == APPROACH) {
587 // We have to be a bit more carefull here since approaches are also searched by area
588 CommRegisterAirport(comm_ident[chan], chan);
589 //cout << "Done (APPROACH)" << endl;
590 FGATC* app = FindInList(comm_ident[chan], APPROACH);
592 // The station is already in the ATC list
593 app->AddPlane("Player");
595 comm_atc_ptr[chan] = app;
597 // Generate the station and put in the ATC list
598 FGApproach* a = new FGApproach;
600 a->AddPlane("Player");
601 atc_list.push_back(a);
602 comm_atc_ptr[chan] = a;
606 if(comm_valid[chan]) {
607 if(comm_type[chan] != APPROACH) {
608 // Currently approaches are removed by Alexander's out-of-range mechanism
609 CommRemoveFromList(comm_ident[chan], comm_type[chan], chan);
611 // Note that we *don't* call SetNoDisplay() here because the other comm channel
612 // might be tuned into the same station - this is handled by CommRemoveFromList(...)
613 comm_type[chan] = INVALID;
614 comm_atc_ptr[chan] = NULL;
615 comm_valid[chan] = false;
621 // Search ATC stations by area in order that we appear 'on the radar'
622 void FGATCMgr::AreaSearch() {
623 // Search for Approach stations
624 comm_list_type approaches;
625 comm_list_iterator app_itr;
627 lon = lon_node->getDoubleValue();
628 lat = lat_node->getDoubleValue();
629 elev = elev_node->getDoubleValue() * SG_FEET_TO_METER;
631 // search stations in range
632 int num_app = current_commlist->FindByPos(lon, lat, elev, 100.0, &approaches, APPROACH);
634 //cout << num_app << " approaches found in radiostack search !!!!" << endl;
636 for(app_itr = approaches.begin(); app_itr != approaches.end(); app_itr++) {
638 FGATC* app = FindInList((app_itr->ident).c_str(), app_itr->type);
640 // The station is already in the ATC list
641 app->AddPlane("Player");
644 // Generate the station and put in the ATC list
645 FGApproach* a = new FGApproach;
646 a->SetData(&(*app_itr));
647 a->AddPlane("Player");
649 atc_list.push_back(a);
654 // remove planes which are out of range
655 // TODO - I'm not entirely sure that this belongs here.
656 atc_list_itr = atc_list.begin();
657 while(atc_list_itr != atc_list.end()) {
658 if((*atc_list_itr)->GetType() == APPROACH ) {
659 int np = (*atc_list_itr)->RemovePlane();
660 // if approach has no planes left remove it from ATC list
662 (*atc_list_itr)->SetNoDisplay();
663 (*atc_list_itr)->Update(0.00833);
664 delete (*atc_list_itr);
665 atc_list_itr = atc_list.erase(atc_list_itr);
666 break; // the other stations will be checked next time