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;
70 FGATCMgr::~FGATCMgr() {
74 void FGATCMgr::bind() {
77 void FGATCMgr::unbind() {
80 void FGATCMgr::init() {
81 comm_node[0] = fgGetNode("/radios/comm[0]/frequencies/selected-mhz", true);
82 comm_node[1] = fgGetNode("/radios/comm[1]/frequencies/selected-mhz", true);
83 lon_node = fgGetNode("/position/longitude-deg", true);
84 lat_node = fgGetNode("/position/latitude-deg", true);
85 elev_node = fgGetNode("/position/altitude-ft", true);
86 atc_list_itr = atc_list.begin();
88 // Search for connected ATC stations once per 0.8 seconds or so
89 // global_events.Register( "fgATCSearch()", fgATCSearch,
90 // fgEVENT::FG_EVENT_READY, 800);
91 // For some reason the above doesn't compile - including Time/event.hxx stops compilation.
93 // Initialise the frequency search map
94 current_commlist = new FGCommList;
95 SGPath p_comm( globals->get_fg_root() );
96 current_commlist->init( p_comm );
98 #ifdef ENABLE_AUDIO_SUPPORT
99 // Load all available voices.
100 // For now we'll do one hardwired one
103 voiceOK = v1->LoadVoice("default");
106 /* I've loaded the voice even if /sim/sound/audible is false
107 * since I know no way of forcing load of the voice if the user
108 * subsequently switches /sim/sound/audible to true. */
113 // Initialise the ATC Dialogs
114 //cout << "Initing Transmissions..." << endl;
115 SG_LOG(SG_ATC, SG_INFO, " ATC Transmissions");
116 current_transmissionlist = new FGTransmissionList;
117 SGPath p_transmission( globals->get_fg_root() );
118 p_transmission.append( "ATC/default.transmissions" );
119 current_transmissionlist->init( p_transmission );
120 //cout << "Done Transmissions" << endl;
122 SG_LOG(SG_ATC, SG_INFO, " ATC Dialog System");
123 current_atcdialog = new FGATCDialog;
124 current_atcdialog->Init();
129 //current_atcdialog->add_entry( "EGNX", "Request vectoring for approach", "Request Vectors" );
130 //current_atcdialog->add_entry( "EGNX", "Mayday, Mayday", "Declare Emergency" );
133 void FGATCMgr::update(double dt) {
134 //cout << "Entering update..." << endl;
135 //Traverse the list of active stations.
136 //Only update one class per update step to avoid the whole ATC system having to calculate between frames.
137 //Eventually we should only update every so many steps.
138 //cout << "In FGATCMgr::update - atc_list.size = " << atc_list.size() << '\n';
139 if(atc_list.size()) {
140 if(atc_list_itr == atc_list.end()) {
141 atc_list_itr = atc_list.begin();
143 //cout << "Updating " << (*atc_list_itr)->get_ident() << ' ' << (*atc_list_itr)->GetType() << '\n';
144 //cout << "Freq = " << (*atc_list_itr)->get_freq() << '\n';
145 (*atc_list_itr)->Update(dt * atc_list.size());
146 //cout << "Done ATC update..." << endl;
150 // Search the tuned frequencies every now and then - this should be done with the event scheduler
153 //cout << "About to AreaSearch()" << endl;
157 //cout << "About to search(1)" << endl;
161 //cout << "About to search(2)" << endl;
167 //cout << "comm1 type = " << comm_type[0] << '\n';
168 //cout << "Leaving update..." << endl;
172 // Returns frequency in KHz - should I alter this to return in MHz?
173 unsigned short int FGATCMgr::GetFrequency(string ident, atc_type tp) {
175 bool ok = current_commlist->FindByCode(ident, test, tp);
176 return(ok ? test.freq : 0);
180 // Register the fact that the AI system wants to activate an airport
181 // Might need more sophistication in this in the future - eg registration by aircraft call-sign.
182 bool FGATCMgr::AIRegisterAirport(string ident) {
183 SG_LOG(SG_ATC, SG_BULK, "AI registered airport " << ident << " with the ATC system");
184 if(airport_atc_map.find(ident) != airport_atc_map.end()) {
185 airport_atc_map[ident]->set_by_AI = true;
186 airport_atc_map[ident]->numAI++;
190 if(dclFindAirportID(ident, &ap)) {
191 //cout << "ident = " << ident << '\n';
192 AirportATC *a = new AirportATC;
193 // I'm not entirely sure that this AirportATC structure business is actually needed - it just duplicates what we can find out anyway!
194 a->lon = ap.longitude;
195 a->lat = ap.latitude;
196 a->elev = ap.elevation;
197 a->atis_freq = GetFrequency(ident, ATIS);
198 //cout << "ATIS freq = " << a->atis_freq << '\n';
199 a->atis_active = false;
200 a->tower_freq = GetFrequency(ident, TOWER);
201 //cout << "Tower freq = " << a->tower_freq << '\n';
202 a->tower_active = false;
203 a->ground_freq = GetFrequency(ident, GROUND);
204 //cout << "Ground freq = " << a->ground_freq << '\n';
205 a->ground_active = false;
206 // TODO - some airports will have a tower/ground frequency but be inactive overnight.
209 airport_atc_map[ident] = a;
217 // Register the fact that the comm radio is tuned to an airport
218 // Channel is zero based
219 bool FGATCMgr::CommRegisterAirport(string ident, int chan) {
220 SG_LOG(SG_ATC, SG_BULK, "Comm channel " << chan << " registered airport " << ident);
221 if(airport_atc_map.find(ident) != airport_atc_map.end()) {
222 //cout << "IN MAP - flagging set by comm..." << endl;
224 airport_atc_map[ident]->set_by_comm[0] = true;
225 } else if(chan == 1) {
226 airport_atc_map[ident]->set_by_comm[1] = true;
228 SG_LOG(SG_ATC, SG_ALERT, "COMM CHANNEL NOT 0 OR 1 IN FGATCMGR!");
229 // Just register both and accept the fact that this ATC will probably never get removed!
230 airport_atc_map[ident]->set_by_comm[0] = true;
231 airport_atc_map[ident]->set_by_comm[1] = true;
235 //cout << "NOT IN MAP - creating new..." << endl;
237 if(dclFindAirportID(ident, &ap)) {
238 AirportATC *a = new AirportATC;
239 // I'm not entirely sure that this AirportATC structure business is actually needed - it just duplicates what we can find out anyway!
240 a->lon = ap.longitude;
241 a->lat = ap.latitude;
242 a->elev = ap.elevation;
243 a->atis_freq = GetFrequency(ident, ATIS);
244 a->atis_active = false;
245 a->tower_freq = GetFrequency(ident, TOWER);
246 a->tower_active = false;
247 a->ground_freq = GetFrequency(ident, GROUND);
248 a->ground_active = false;
249 // TODO - some airports will have a tower/ground frequency but be inactive overnight.
250 a->set_by_AI = false;
253 a->set_by_comm[0] = true;
254 } else if(chan == 1) {
255 a->set_by_comm[1] = true;
257 SG_LOG(SG_ATC, SG_ALERT, "COMM CHANNEL NOT 1 OR 2 IN FGATCMGR!");
258 // Just register both and accept the fact that this ATC will probably never get removed!
259 a->set_by_comm[0] = true;
260 a->set_by_comm[1] = true;
262 airport_atc_map[ident] = a;
270 // Remove from list only if not needed by the AI system or the other comm channel
271 // Note that chan is zero based.
272 void FGATCMgr::CommRemoveFromList(const char* id, atc_type tp, int chan) {
273 SG_LOG(SG_ATC, SG_BULK, "CommRemoveFromList called for airport " << id << " by channel " << chan);
274 if(airport_atc_map.find((string)id) != airport_atc_map.end()) {
275 AirportATC* a = airport_atc_map[(string)id];
276 //cout << "In CommRemoveFromList, a->ground_freq = " << a->ground_freq << endl;
279 FGATC* aptr = GetATCPointer((string)id, tp);
282 //cout << "chan 1\n";
283 a->set_by_comm[0] = false;
284 if(!a->set_by_comm[1]) {
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] = false;
297 if(!a->set_by_comm[0]) {
299 aptr->SetNoDisplay();
300 //cout << "Setting no display...\n";
305 airport_atc_map[(string)id] = a;
310 a->set_by_comm[0] = false;
311 // Remove only if not also set by the other comm channel
312 if(!a->set_by_comm[1]) {
313 RemoveFromList(id, tp);
317 a->set_by_comm[1] = false;
318 if(!a->set_by_comm[0]) {
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);
525 FGATC* app = FindInList(comm_ident[chan], TOWER);
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);
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);
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);
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 a->AddPlane("Player");
589 atc_list.push_back(a);
590 comm_atc_ptr[chan] = a;
594 if(comm_valid[chan]) {
595 if(comm_type[chan] != APPROACH) {
596 // Currently approaches are removed by Alexander's out-of-range mechanism
597 CommRemoveFromList(comm_ident[chan], comm_type[chan], chan);
599 // Note that we *don't* call SetNoDisplay() here because the other comm channel
600 // might be tuned into the same station - this is handled by CommRemoveFromList(...)
601 comm_type[chan] = INVALID;
602 comm_atc_ptr[chan] = NULL;
603 comm_valid[chan] = false;
609 // Search ATC stations by area in order that we appear 'on the radar'
610 void FGATCMgr::AreaSearch() {
611 // Search for Approach stations
612 comm_list_type approaches;
613 comm_list_iterator app_itr;
615 lon = lon_node->getDoubleValue();
616 lat = lat_node->getDoubleValue();
617 elev = elev_node->getDoubleValue() * SG_FEET_TO_METER;
619 // search stations in range
620 int num_app = current_commlist->FindByPos(lon, lat, elev, 100.0, &approaches, APPROACH);
622 //cout << num_app << " approaches found in radiostack search !!!!" << endl;
624 for(app_itr = approaches.begin(); app_itr != approaches.end(); app_itr++) {
626 FGATC* app = FindInList((app_itr->ident).c_str(), app_itr->type);
628 // The station is already in the ATC list
629 app->AddPlane("Player");
632 // Generate the station and put in the ATC list
633 FGApproach* a = new FGApproach;
634 a->SetData(&(*app_itr));
635 a->AddPlane("Player");
637 atc_list.push_back(a);
642 // remove planes which are out of range
643 // TODO - I'm not entirely sure that this belongs here.
644 atc_list_itr = atc_list.begin();
645 while(atc_list_itr != atc_list.end()) {
646 if((*atc_list_itr)->GetType() == APPROACH ) {
647 int np = (*atc_list_itr)->RemovePlane();
648 // if approach has no planes left remove it from ATC list
650 (*atc_list_itr)->SetNoDisplay();
651 (*atc_list_itr)->Update(0.00833);
652 delete (*atc_list_itr);
653 atc_list_itr = atc_list.erase(atc_list_itr);
654 break; // the other stations will be checked next time