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;
72 FGATCMgr::~FGATCMgr() {
76 void FGATCMgr::bind() {
79 void FGATCMgr::unbind() {
82 void FGATCMgr::init() {
83 comm_node[0] = fgGetNode("/radios/comm[0]/frequencies/selected-mhz", true);
84 comm_node[1] = fgGetNode("/radios/comm[1]/frequencies/selected-mhz", true);
85 lon_node = fgGetNode("/position/longitude-deg", true);
86 lat_node = fgGetNode("/position/latitude-deg", true);
87 elev_node = fgGetNode("/position/altitude-ft", true);
88 atc_list_itr = atc_list.begin();
90 // Search for connected ATC stations once per 0.8 seconds or so
91 // global_events.Register( "fgATCSearch()", fgATCSearch,
92 // fgEVENT::FG_EVENT_READY, 800);
93 // For some reason the above doesn't compile - including Time/event.hxx stops compilation.
95 // Initialise the frequency search map
96 current_commlist = new FGCommList;
97 SGPath p_comm( globals->get_fg_root() );
98 current_commlist->init( p_comm );
100 #ifdef ENABLE_AUDIO_SUPPORT
101 // Load all available voices.
102 // For now we'll do one hardwired one
105 voiceOK = v1->LoadVoice("default");
108 /* I've loaded the voice even if /sim/sound/audible is false
109 * since I know no way of forcing load of the voice if the user
110 * subsequently switches /sim/sound/audible to true. */
115 // Initialise the ATC Dialogs
116 //cout << "Initing Transmissions..." << endl;
117 SG_LOG(SG_ATC, SG_INFO, " ATC Transmissions");
118 current_transmissionlist = new FGTransmissionList;
119 SGPath p_transmission( globals->get_fg_root() );
120 p_transmission.append( "ATC/default.transmissions" );
121 current_transmissionlist->init( p_transmission );
122 //cout << "Done Transmissions" << endl;
124 SG_LOG(SG_ATC, SG_INFO, " ATC Dialog System");
125 current_atcdialog = new FGATCDialog;
126 current_atcdialog->Init();
131 //current_atcdialog->add_entry( "EGNX", "Request vectoring for approach", "Request Vectors" );
132 //current_atcdialog->add_entry( "EGNX", "Mayday, Mayday", "Declare Emergency" );
135 void FGATCMgr::update(double dt) {
136 //cout << "Entering update..." << endl;
137 //Traverse the list of active stations.
138 //Only update one class per update step to avoid the whole ATC system having to calculate between frames.
139 //Eventually we should only update every so many steps.
140 //cout << "In FGATCMgr::update - atc_list.size = " << atc_list.size() << '\n';
141 if(atc_list.size()) {
142 if(atc_list_itr == atc_list.end()) {
143 atc_list_itr = atc_list.begin();
145 //cout << "Updating " << (*atc_list_itr)->get_ident() << ' ' << (*atc_list_itr)->GetType() << '\n';
146 //cout << "Freq = " << (*atc_list_itr)->get_freq() << '\n';
147 (*atc_list_itr)->Update(dt * atc_list.size());
148 //cout << "Done ATC update..." << endl;
152 // Search the tuned frequencies every now and then - this should be done with the event scheduler
155 //cout << "About to AreaSearch()" << endl;
159 //cout << "About to search(1)" << endl;
163 //cout << "About to search(2)" << endl;
169 //cout << "comm1 type = " << comm_type[0] << '\n';
170 //cout << "Leaving update..." << endl;
174 // Returns frequency in KHz - should I alter this to return in MHz?
175 unsigned short int FGATCMgr::GetFrequency(string ident, atc_type tp) {
177 bool ok = current_commlist->FindByCode(ident, test, tp);
178 return(ok ? test.freq : 0);
182 // Register the fact that the AI system wants to activate an airport
183 // Might need more sophistication in this in the future - eg registration by aircraft call-sign.
184 bool FGATCMgr::AIRegisterAirport(string ident) {
185 SG_LOG(SG_ATC, SG_BULK, "AI registered airport " << ident << " with the ATC system");
186 if(airport_atc_map.find(ident) != airport_atc_map.end()) {
187 airport_atc_map[ident]->set_by_AI = true;
188 airport_atc_map[ident]->numAI++;
192 if(dclFindAirportID(ident, &ap)) {
193 //cout << "ident = " << ident << '\n';
194 AirportATC *a = new AirportATC;
195 // I'm not entirely sure that this AirportATC structure business is actually needed - it just duplicates what we can find out anyway!
196 a->lon = ap.longitude;
197 a->lat = ap.latitude;
198 a->elev = ap.elevation;
199 a->atis_freq = GetFrequency(ident, ATIS);
200 //cout << "ATIS freq = " << a->atis_freq << '\n';
201 a->atis_active = false;
202 a->tower_freq = GetFrequency(ident, TOWER);
203 //cout << "Tower freq = " << a->tower_freq << '\n';
204 a->tower_active = false;
205 a->ground_freq = GetFrequency(ident, GROUND);
206 //cout << "Ground freq = " << a->ground_freq << '\n';
207 a->ground_active = false;
208 // TODO - some airports will have a tower/ground frequency but be inactive overnight.
211 airport_atc_map[ident] = a;
219 // Register the fact that the comm radio is tuned to an airport
220 // Channel is zero based
221 bool FGATCMgr::CommRegisterAirport(string ident, int chan) {
222 SG_LOG(SG_ATC, SG_BULK, "Comm channel " << chan << " registered airport " << ident);
223 if(airport_atc_map.find(ident) != airport_atc_map.end()) {
224 //cout << "IN MAP - flagging set by comm..." << endl;
226 airport_atc_map[ident]->set_by_comm[0] = true;
227 } else if(chan == 1) {
228 airport_atc_map[ident]->set_by_comm[1] = true;
230 SG_LOG(SG_ATC, SG_ALERT, "COMM CHANNEL NOT 0 OR 1 IN FGATCMGR!");
231 // Just register both and accept the fact that this ATC will probably never get removed!
232 airport_atc_map[ident]->set_by_comm[0] = true;
233 airport_atc_map[ident]->set_by_comm[1] = true;
237 //cout << "NOT IN MAP - creating new..." << endl;
239 if(dclFindAirportID(ident, &ap)) {
240 AirportATC *a = new AirportATC;
241 // I'm not entirely sure that this AirportATC structure business is actually needed - it just duplicates what we can find out anyway!
242 a->lon = ap.longitude;
243 a->lat = ap.latitude;
244 a->elev = ap.elevation;
245 a->atis_freq = GetFrequency(ident, ATIS);
246 a->atis_active = false;
247 a->tower_freq = GetFrequency(ident, TOWER);
248 a->tower_active = false;
249 a->ground_freq = GetFrequency(ident, GROUND);
250 a->ground_active = false;
251 // TODO - some airports will have a tower/ground frequency but be inactive overnight.
252 a->set_by_AI = false;
255 a->set_by_comm[0] = true;
256 } else if(chan == 1) {
257 a->set_by_comm[1] = true;
259 SG_LOG(SG_ATC, SG_ALERT, "COMM CHANNEL NOT 1 OR 2 IN FGATCMGR!");
260 // Just register both and accept the fact that this ATC will probably never get removed!
261 a->set_by_comm[0] = true;
262 a->set_by_comm[1] = true;
264 airport_atc_map[ident] = a;
272 // Remove from list only if not needed by the AI system or the other comm channel
273 // Note that chan is zero based.
274 void FGATCMgr::CommRemoveFromList(const char* id, atc_type tp, int chan) {
275 SG_LOG(SG_ATC, SG_BULK, "CommRemoveFromList called for airport " << id << " by channel " << chan);
276 if(airport_atc_map.find((string)id) != airport_atc_map.end()) {
277 AirportATC* a = airport_atc_map[(string)id];
278 //cout << "In CommRemoveFromList, a->ground_freq = " << a->ground_freq << endl;
281 FGATC* aptr = GetATCPointer((string)id, tp);
284 //cout << "chan 1\n";
285 a->set_by_comm[0] = false;
286 if(!a->set_by_comm[1]) {
287 //cout << "not set by comm2\n";
289 //cout << "Got pointer\n";
290 aptr->SetNoDisplay();
291 //cout << "Setting no display...\n";
293 //cout << "Not got pointer\n";
298 a->set_by_comm[1] = false;
299 if(!a->set_by_comm[0]) {
301 aptr->SetNoDisplay();
302 //cout << "Setting no display...\n";
307 airport_atc_map[(string)id] = a;
312 a->set_by_comm[0] = false;
313 // Remove only if not also set by the other comm channel
314 if(!a->set_by_comm[1]) {
315 RemoveFromList(id, tp);
319 a->set_by_comm[1] = false;
320 if(!a->set_by_comm[0]) {
321 RemoveFromList(id, tp);
330 // Remove from list - should only be called from above or similar
331 // This function *will* remove it from the list regardless of who else might want it.
332 void FGATCMgr::RemoveFromList(const char* id, atc_type tp) {
333 //cout << "Requested type = " << tp << '\n';
334 //cout << "id = " << id << '\n';
335 atc_list_itr = atc_list.begin();
336 while(atc_list_itr != atc_list.end()) {
337 //cout << "type = " << (*atc_list_itr)->GetType() << '\n';
338 //cout << "Ident = " << (*atc_list_itr)->get_ident() << '\n';
339 if( (!strcmp((*atc_list_itr)->get_ident(), id))
340 && ((*atc_list_itr)->GetType() == tp) ) {
341 //Before removing it stop it transmitting!!
342 //cout << "OBLITERATING FROM LIST!!!\n";
343 (*atc_list_itr)->SetNoDisplay();
344 (*atc_list_itr)->Update(0.00833);
345 delete (*atc_list_itr);
346 atc_list_itr = atc_list.erase(atc_list_itr);
348 } // Note that that can upset where we are in the list but that doesn't really matter
354 // Find in list - return a currently active ATC pointer given ICAO code and type
355 // Return NULL if the given service is not in the list
356 // - *** THE CALLING FUNCTION MUST CHECK FOR THIS ***
357 FGATC* FGATCMgr::FindInList(const char* id, atc_type tp) {
358 atc_list_itr = atc_list.begin();
359 while(atc_list_itr != atc_list.end()) {
360 if( (!strcmp((*atc_list_itr)->get_ident(), id))
361 && ((*atc_list_itr)->GetType() == tp) ) {
362 return(*atc_list_itr);
363 } // Note that that can upset where we are in the list but that shouldn't really matter
366 // If we get here it's not in the list
370 // Returns true if the airport is found in the map
371 bool FGATCMgr::GetAirportATCDetails(string icao, AirportATC* a) {
372 if(airport_atc_map.find(icao) != airport_atc_map.end()) {
373 *a = *airport_atc_map[icao];
381 // Return a pointer to a given sort of ATC at a given airport and activate if necessary
382 // Returns NULL if service doesn't exist - calling function should check for this.
383 // We really ought to make this private and call it from the CommRegisterAirport / AIRegisterAirport functions
384 // - at the moment all these GetATC... functions exposed are just too complicated.
385 FGATC* FGATCMgr::GetATCPointer(string icao, atc_type type) {
386 if(airport_atc_map.find(icao) == airport_atc_map.end()) {
389 AirportATC *a = airport_atc_map[icao];
390 //cout << "a->lon = " << a->lon << '\n';
391 //cout << "a->elev = " << a->elev << '\n';
392 //cout << "a->tower_freq = " << a->tower_freq << '\n';
395 if(a->tower_active) {
396 // Get the pointer from the list
397 return(FindInList(icao.c_str(), type));
400 if(current_commlist->FindByFreq(a->lon, a->lat, a->elev, a->tower_freq, &data, TOWER)) {
401 FGTower* t = new FGTower;
403 atc_list.push_back(t);
404 a->tower_active = true;
405 airport_atc_map[icao] = a;
409 SG_LOG(SG_ATC, SG_ALERT, "ERROR - tower that should exist in FGATCMgr::GetATCPointer for airport " << icao << " not found");
416 SG_LOG(SG_ATC, SG_ALERT, "ERROR - ATIS station should not be requested from FGATCMgr::GetATCPointer");
419 //cout << "IN CASE GROUND" << endl;
420 if(a->ground_active) {
421 // Get the pointer from the list
422 return(FindInList(icao.c_str(), type));
425 if(current_commlist->FindByFreq(a->lon, a->lat, a->elev, a->ground_freq, &data, GROUND)) {
426 FGGround* g = new FGGround;
428 atc_list.push_back(g);
429 a->ground_active = true;
430 airport_atc_map[icao] = a;
434 SG_LOG(SG_ATC, SG_ALERT, "ERROR - ground control that should exist in FGATCMgr::GetATCPointer for airport " << icao << " not found");
446 SG_LOG(SG_ATC, SG_ALERT, "ERROR IN FGATCMgr - reached end of GetATCPointer");
451 // Return a pointer to an appropriate voice for a given type of ATC
452 // creating the voice if necessary - ie. make sure exactly one copy
453 // of every voice in use exists in memory.
455 // TODO - in the future this will get more complex and dole out country/airport
456 // specific voices, and possible make sure that the same voice doesn't get used
457 // at different airports in quick succession if a large enough selection are available.
458 FGATCVoice* FGATCMgr::GetVoicePointer(atc_type type) {
459 // TODO - implement me better - maintain a list of loaded voices and other voices!!
481 // Display a dialog box with options relevant to the currently tuned ATC service.
482 void FGATCMgr::doPopupDialog() {
483 ATCDoDialog(comm_type[0]); // FIXME - currently hardwired to comm1
486 // Search for ATC stations by frequency
487 void FGATCMgr::FreqSearch(int channel) {
488 int chan = channel - 1; // Convert to zero-based for the arrays
491 double freq = comm_node[chan]->getDoubleValue();
492 lon = lon_node->getDoubleValue();
493 lat = lat_node->getDoubleValue();
494 elev = elev_node->getDoubleValue() * SG_FEET_TO_METER;
496 // Query the data store and get the closest match if any
497 if(current_commlist->FindByFreq(lon, lat, elev, freq, &data)) {
500 // If this channel not previously valid then easy - add ATC to list
501 // If this channel was valid then - Have we tuned to a different service?
502 // If so - de-register one and add the other
503 if(comm_valid[chan]) {
504 if((comm_ident[chan] == data.ident) && (comm_type[chan] == data.type)) {
505 // Then we're still tuned into the same service so do nought and return
508 // Something's changed - either the location or the service type
509 // We need to feed the channel in so we're not removing it if we're also tuned in on the other channel
510 CommRemoveFromList(comm_ident[chan], comm_type[chan], chan);
513 // At this point we can assume that we need to add the service.
514 comm_ident[chan] = (data.ident).c_str();
515 comm_type[chan] = data.type;
516 comm_x[chan] = (double)data.x;
517 comm_y[chan] = (double)data.y;
518 comm_z[chan] = (double)data.z;
519 comm_lon[chan] = (double)data.lon;
520 comm_lat[chan] = (double)data.lat;
521 comm_elev[chan] = (double)data.elev;
522 comm_valid[chan] = true;
524 // This was a switch-case statement but the compiler didn't like the new variable creation with it.
525 if(comm_type[chan] == ATIS) {
526 CommRegisterAirport(comm_ident[chan], chan);
527 FGATC* app = FindInList(comm_ident[chan], TOWER);
529 // The station is already in the ATC list
530 //cout << "In list - flagging SetDisplay..." << endl;
533 // Generate the station and put in the ATC list
534 //cout << "Not in list - generating..." << endl;
535 FGATIS* a = new FGATIS;
537 comm_atc_ptr[chan] = a;
540 atc_list.push_back(a);
542 } else if (comm_type[chan] == TOWER) {
543 CommRegisterAirport(comm_ident[chan], chan);
544 //cout << "Done (TOWER)" << endl;
545 FGATC* app = FindInList(comm_ident[chan], TOWER);
547 // The station is already in the ATC list
548 //cout << "In list - flagging SetDisplay..." << endl;
551 // Generate the station and put in the ATC list
552 //cout << "Not in list - generating..." << endl;
553 FGTower* t = new FGTower;
555 comm_atc_ptr[chan] = t;
558 atc_list.push_back(t);
560 } else if (comm_type[chan] == GROUND) {
561 CommRegisterAirport(comm_ident[chan], chan);
562 //cout << "Done (GROUND)" << endl;
563 FGATC* app = FindInList(comm_ident[chan], GROUND);
565 // The station is already in the ATC list
568 // Generate the station and put in the ATC list
569 FGGround* g = new FGGround;
571 comm_atc_ptr[chan] = g;
574 atc_list.push_back(g);
576 } else if (comm_type[chan] == APPROACH) {
577 // We have to be a bit more carefull here since approaches are also searched by area
578 CommRegisterAirport(comm_ident[chan], chan);
579 //cout << "Done (APPROACH)" << endl;
580 FGATC* app = FindInList(comm_ident[chan], APPROACH);
582 // The station is already in the ATC list
583 app->AddPlane("Player");
585 comm_atc_ptr[chan] = app;
587 // Generate the station and put in the ATC list
588 FGApproach* a = new FGApproach;
590 a->AddPlane("Player");
591 atc_list.push_back(a);
592 comm_atc_ptr[chan] = 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;
611 // Search ATC stations by area in order that we appear 'on the radar'
612 void FGATCMgr::AreaSearch() {
613 // Search for Approach stations
614 comm_list_type approaches;
615 comm_list_iterator app_itr;
617 lon = lon_node->getDoubleValue();
618 lat = lat_node->getDoubleValue();
619 elev = elev_node->getDoubleValue() * SG_FEET_TO_METER;
621 // search stations in range
622 int num_app = current_commlist->FindByPos(lon, lat, elev, 100.0, &approaches, APPROACH);
624 //cout << num_app << " approaches found in radiostack search !!!!" << endl;
626 for(app_itr = approaches.begin(); app_itr != approaches.end(); app_itr++) {
628 FGATC* app = FindInList((app_itr->ident).c_str(), app_itr->type);
630 // The station is already in the ATC list
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 a->AddPlane("Player");
639 atc_list.push_back(a);
644 // remove planes which are out of range
645 // TODO - I'm not entirely sure that this belongs here.
646 atc_list_itr = atc_list.begin();
647 while(atc_list_itr != atc_list.end()) {
648 if((*atc_list_itr)->GetType() == APPROACH ) {
649 int np = (*atc_list_itr)->RemovePlane();
650 // if approach has no planes left remove it from ATC list
652 (*atc_list_itr)->SetNoDisplay();
653 (*atc_list_itr)->Update(0.00833);
654 delete (*atc_list_itr);
655 atc_list_itr = atc_list.erase(atc_list_itr);
656 break; // the other stations will be checked next time