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() :
52 set_by_comm_search(false) {
55 FGATCMgr::FGATCMgr() {
58 last_comm_ident[0] = "";
59 last_comm_ident[1] = "";
61 last_in_range = false;
62 comm_type[0] = INVALID;
63 comm_type[1] = INVALID;
64 comm_atc_ptr[0] = NULL;
65 comm_atc_ptr[1] = NULL;
68 FGATCMgr::~FGATCMgr() {
72 void FGATCMgr::bind() {
75 void FGATCMgr::unbind() {
78 void FGATCMgr::init() {
79 comm_node[0] = fgGetNode("/radios/comm[0]/frequencies/selected-mhz", true);
80 comm_node[1] = fgGetNode("/radios/comm[1]/frequencies/selected-mhz", true);
81 lon_node = fgGetNode("/position/longitude-deg", true);
82 lat_node = fgGetNode("/position/latitude-deg", true);
83 elev_node = fgGetNode("/position/altitude-ft", true);
84 atc_list_itr = atc_list.begin();
86 // Search for connected ATC stations once per 0.8 seconds or so
87 // global_events.Register( "fgATCSearch()", fgATCSearch,
88 // fgEVENT::FG_EVENT_READY, 800);
89 // For some reason the above doesn't compile - including Time/event.hxx stops compilation.
91 // Initialise the frequency search map
92 current_commlist = new FGCommList;
93 SGPath p_comm( globals->get_fg_root() );
94 current_commlist->init( p_comm );
96 #ifdef ENABLE_AUDIO_SUPPORT
97 // Load all available voices.
98 // For now we'll do one hardwired one
101 voiceOK = v1->LoadVoice("default");
104 /* I've loaded the voice even if /sim/sound/audible is false
105 * since I know no way of forcing load of the voice if the user
106 * subsequently switches /sim/sound/audible to true. */
111 // Initialise the ATC Dialogs
112 //cout << "Initing Transmissions..." << endl;
113 SG_LOG(SG_GENERAL, SG_INFO, " ATC Transmissions");
114 current_transmissionlist = new FGTransmissionList;
115 SGPath p_transmission( globals->get_fg_root() );
116 p_transmission.append( "ATC/default.transmissions" );
117 current_transmissionlist->init( p_transmission );
118 //cout << "Done Transmissions" << endl;
120 SG_LOG(SG_GENERAL, SG_INFO, " ATC Dialog System");
121 current_atcdialog = new FGATCDialog;
122 current_atcdialog->Init();
127 //current_atcdialog->add_entry( "EGNX", "Request vectoring for approach", "Request Vectors" );
128 //current_atcdialog->add_entry( "EGNX", "Mayday, Mayday", "Declare Emergency" );
131 bool ok = current_commlist->FindByCode("KEMT", test, TOWER);
133 cout << "KEMT tower frequency from test was " << test.freq << endl;
135 cout << "KEMT tower frequency not found :-(" << endl;
141 void FGATCMgr::update(double dt) {
142 //cout << "Entering update..." << endl;
143 //Traverse the list of active stations.
144 //Only update one class per update step to avoid the whole ATC system having to calculate between frames.
145 //Eventually we should only update every so many steps.
146 //cout << "In FGATCMgr::update - atc_list.size = " << atc_list.size() << '\n';
147 if(atc_list.size()) {
148 if(atc_list_itr == atc_list.end()) {
149 atc_list_itr = atc_list.begin();
151 //cout << "Updating " << (*atc_list_itr)->get_ident() << ' ' << (*atc_list_itr)->GetType() << '\n';
152 //cout << "Freq = " << (*atc_list_itr)->get_freq() << '\n';
153 (*atc_list_itr)->Update();
154 //cout << "Done ATC update..." << endl;
158 // Search the tuned frequencies every now and then - this should be done with the event scheduler
164 //cout << "About to search(1)" << endl;
168 //cout << "About to search(2)" << endl;
174 //cout << "comm1 type = " << comm_type[0] << '\n';
178 // Returns frequency in KHz - should I alter this to return in MHz?
179 unsigned short int FGATCMgr::GetFrequency(string ident, atc_type tp) {
181 bool ok = current_commlist->FindByCode(ident, test, tp);
182 return(ok ? test.freq : 0);
186 // Register the fact that the AI system wants to activate an airport
187 // Might need more sophistication in this in the future - eg registration by aircraft call-sign.
188 bool FGATCMgr::AIRegisterAirport(string ident) {
189 if(airport_atc_map.find(ident) != airport_atc_map.end()) {
190 airport_atc_map[ident]->set_by_AI = true;
194 if(dclFindAirportID(ident, &ap)) {
195 AirportATC *a = new AirportATC;
196 // I'm not entirely sure that this AirportATC structure business is actually needed - it just duplicates what we can find out anyway!
197 a->lon = ap.longitude;
198 a->lat = ap.latitude;
199 a->elev = ap.elevation;
200 a->atis_freq = GetFrequency(ident, ATIS);
201 a->atis_active = false;
202 a->tower_freq = GetFrequency(ident, TOWER);
203 a->tower_active = false;
204 a->ground_freq = GetFrequency(ident, GROUND);
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 bool FGATCMgr::CommRegisterAirport(string ident) { // Later we'll differentiate between comm 1 and comm2
219 // TODO - implement me!
224 // Remove from list only if not needed by the AI system or the other comm channel
225 // TODO - implement me!!
226 void FGATCMgr::CommRemoveFromList(const char* id, atc_type tp, int chan) {
228 if(GetAirportATCDetails((string)id, &a)) {
231 a.set_by_comm_search = false;
232 airport_atc_map[(string)id] = a;
239 // Hack - need to implement this properly
240 RemoveFromList(id, tp);
244 // Remove from list - should only be called from above or similar
245 // This function *will* remove it from the list regardless of who else might want it.
246 void FGATCMgr::RemoveFromList(const char* id, atc_type tp) {
247 //cout << "Requested type = " << tp << '\n';
248 //cout << "id = " << id << '\n';
249 atc_list_itr = atc_list.begin();
250 while(atc_list_itr != atc_list.end()) {
251 //cout << "type = " << (*atc_list_itr)->GetType() << '\n';
252 //cout << "Ident = " << (*atc_list_itr)->get_ident() << '\n';
253 if( (!strcmp((*atc_list_itr)->get_ident(), id))
254 && ((*atc_list_itr)->GetType() == tp) ) {
255 //Before removing it stop it transmitting!!
256 //cout << "OBLITERATING FROM LIST!!!\n";
257 (*atc_list_itr)->SetNoDisplay();
258 (*atc_list_itr)->Update();
259 delete (*atc_list_itr);
260 atc_list_itr = atc_list.erase(atc_list_itr);
262 } // Note that that can upset where we are in the list but that doesn't really matter
268 // Find in list - return a currently active ATC pointer given ICAO code and type
269 // Return NULL if the given service is not in the list
270 // - *** THE CALLING FUNCTION MUST CHECK FOR THIS ***
271 FGATC* FGATCMgr::FindInList(const char* id, atc_type tp) {
272 atc_list_itr = atc_list.begin();
273 while(atc_list_itr != atc_list.end()) {
274 if( (!strcmp((*atc_list_itr)->get_ident(), id))
275 && ((*atc_list_itr)->GetType() == tp) ) {
276 return(*atc_list_itr);
277 } // Note that that can upset where we are in the list but that shouldn't really matter
280 // If we get here it's not in the list
284 // Returns true if the airport is found in the map
285 bool FGATCMgr::GetAirportATCDetails(string icao, AirportATC* a) {
286 if(airport_atc_map.find(icao) != airport_atc_map.end()) {
287 *a = *airport_atc_map[icao];
295 // Return a pointer to a given sort of ATC at a given airport and activate if necessary
296 // Returns NULL if service doesn't exist - calling function should check for this.
297 FGATC* FGATCMgr::GetATCPointer(string icao, atc_type type) {
298 if(airport_atc_map.find(icao) == airport_atc_map.end()) {
301 AirportATC *a = airport_atc_map[icao];
302 //cout << "a->lon = " << a->lon << '\n';
303 //cout << "a->elev = " << a->elev << '\n';
304 //cout << "a->tower_freq = " << a->tower_freq << '\n';
307 if(a->tower_active) {
308 // Get the pointer from the list
309 return(FindInList(icao.c_str(), type));
312 if(current_commlist->FindByFreq(a->lon, a->lat, a->elev, a->tower_freq, &data, TOWER)) {
313 FGTower* t = new FGTower;
315 atc_list.push_back(t);
316 a->tower_active = true;
317 airport_atc_map[icao] = a;
321 cout << "ERROR - tower that should exist in FGATCMgr::GetATCPointer for airport " << icao << " not found\n";
328 SG_LOG(SG_GENERAL, SG_ALERT, "ERROR - ATIS station should not be requested from FGATCMgr::GetATCPointer");
331 if(a->ground_active) {
332 // Get the pointer from the list
333 return(FindInList(icao.c_str(), type));
336 if(current_commlist->FindByFreq(a->lon, a->lat, a->elev, a->ground_freq, &data, GROUND)) {
337 FGGround* g = new FGGround;
339 atc_list.push_back(g);
340 a->ground_active = true;
341 airport_atc_map[icao] = a;
345 cout << "ERROR - ground control that should exist in FGATCMgr::GetATCPointer for airport " << icao << " not found\n";
357 SG_LOG(SG_GENERAL, SG_ALERT, "ERROR IN FGATCMgr - reached end of GetATCPointer");
362 // Return a pointer to an appropriate voice for a given type of ATC
363 // creating the voice if necessary - ie. make sure exactly one copy
364 // of every voice in use exists in memory.
366 // TODO - in the future this will get more complex and dole out country/airport
367 // specific voices, and possible make sure that the same voice doesn't get used
368 // at different airports in quick succession if a large enough selection are available.
369 FGATCVoice* FGATCMgr::GetVoicePointer(atc_type type) {
370 // TODO - implement me better - maintain a list of loaded voices and other voices!!
392 // Display a dialog box with options relevant to the currently tuned ATC service.
393 void FGATCMgr::doPopupDialog() {
394 ATCDoDialog(comm_type[0]); // FIXME - currently hardwired to comm1
397 // Search for ATC stations by frequency
398 void FGATCMgr::FreqSearch(int channel) {
399 int chan = channel - 1; // Convert to zero-based for the arrays
402 double freq = comm_node[chan]->getDoubleValue();
403 lon = lon_node->getDoubleValue();
404 lat = lat_node->getDoubleValue();
405 elev = elev_node->getDoubleValue() * SG_FEET_TO_METER;
407 // Query the data store and get the closest match if any
408 if(current_commlist->FindByFreq(lon, lat, elev, freq, &data)) {
411 // If this channel not previously valid then easy - add ATC to list
412 // If this channel was valid then - Have we tuned to a different service?
413 // If so - de-register one and add the other
414 if(comm_valid[chan]) {
415 if((comm_ident[chan] == data.ident) && (comm_type[chan] == data.type)) {
416 // Then we're still tuned into the same service so do nought and return
419 // Something's changed - either the location or the service type
420 // We need to feed the channel in so we're not removing it if we're also tuned in on the other channel
421 CommRemoveFromList(comm_ident[chan], comm_type[chan], chan);
424 // At this point we can assume that we need to add the service.
425 comm_ident[chan] = (data.ident).c_str();
426 comm_type[chan] = data.type;
427 comm_x[chan] = (double)data.x;
428 comm_y[chan] = (double)data.y;
429 comm_z[chan] = (double)data.z;
430 comm_lon[chan] = (double)data.lon;
431 comm_lat[chan] = (double)data.lat;
432 comm_elev[chan] = (double)data.elev;
433 comm_valid[chan] = true;
435 // This was a switch-case statement but the compiler didn't like the new variable creation with it.
436 if(comm_type[chan] == ATIS) {
437 FGATIS* a = new FGATIS;
439 comm_atc_ptr[chan] = a;
441 //a->set_refname((chan) ? "atis2" : "atis1"); // FIXME - that line is limited to 2 channels
442 atc_list.push_back(a);
443 } else if (comm_type[chan] == TOWER) {
444 FGATC* app = FindInList(comm_ident[chan], TOWER);
446 // The station is already in the ATC list
449 // Generate the station and put in the ATC list
450 FGTower* t = new FGTower;
452 comm_atc_ptr[chan] = t;
454 atc_list.push_back(t);
456 } else if (comm_type[chan] == APPROACH) {
457 // We have to be a bit more carefull here since approaches are also searched by area
458 FGATC* app = FindInList(comm_ident[chan], APPROACH);
460 // The station is already in the ATC list
461 app->AddPlane("Player");
463 comm_atc_ptr[chan] = app;
465 // Generate the station and put in the ATC list
466 FGApproach* a = new FGApproach;
468 a->AddPlane("Player");
469 atc_list.push_back(a);
470 comm_atc_ptr[chan] = a;
474 if(comm_valid[chan]) {
475 if(comm_type[chan] != APPROACH) {
476 // Currently approaches are removed by Alexander's out-of-range mechanism
477 CommRemoveFromList(comm_ident[chan], comm_type[chan], chan);
479 // Note that we *don't* call SetNoDisplay() here because the other comm channel
480 // might be tuned into the same station - this is handled by CommRemoveFromList(...)
481 comm_type[chan] = INVALID;
482 comm_atc_ptr[chan] = NULL;
483 comm_valid[chan] = false;
489 // Search ATC stations by area in order that we appear 'on the radar'
490 void FGATCMgr::AreaSearch() {
491 // Search for Approach stations
492 comm_list_type approaches;
493 comm_list_iterator app_itr;
495 lon = lon_node->getDoubleValue();
496 lat = lat_node->getDoubleValue();
497 elev = elev_node->getDoubleValue() * SG_FEET_TO_METER;
499 // search stations in range
500 int num_app = current_commlist->FindByPos(lon, lat, elev, 100.0, &approaches, APPROACH);
502 //cout << num_app << " approaches found in radiostack search !!!!" << endl;
504 for(app_itr = approaches.begin(); app_itr != approaches.end(); app_itr++) {
506 FGATC* app = FindInList((app_itr->ident).c_str(), app_itr->type);
508 // The station is already in the ATC list
509 app->AddPlane("Player");
512 // Generate the station and put in the ATC list
513 FGApproach* a = new FGApproach;
514 a->SetData(&(*app_itr));
515 a->AddPlane("Player");
517 atc_list.push_back(a);
522 // remove planes which are out of range
523 // TODO - I'm not entirely sure that this belongs here.
524 atc_list_itr = atc_list.begin();
525 while(atc_list_itr != atc_list.end()) {
526 if((*atc_list_itr)->GetType() == APPROACH ) {
527 int np = (*atc_list_itr)->RemovePlane();
528 // if approach has no planes left remove it from ATC list
530 (*atc_list_itr)->SetNoDisplay();
531 (*atc_list_itr)->Update();
532 delete (*atc_list_itr);
533 atc_list_itr = atc_list.erase(atc_list_itr);
534 break; // the other stations will be checked next time