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>
27 #include "commlist.hxx"
28 //#include "atislist.hxx"
29 //#include "groundlist.hxx"
30 //#include "towerlist.hxx"
31 //#include "approachlist.hxx"
32 #include "ATCdisplay.hxx"
33 #include "ATCDialog.hxx"
36 // periodic radio station search wrapper
37 static void fgATCSearch( void ) {
38 globals->get_ATC_mgr()->Search();
40 */ //This wouldn't compile - including Time/event.hxx breaks it :-(
42 AirportATC::AirportATC() :
53 set_by_comm_search(false) {
56 FGATCMgr::FGATCMgr() {
59 last_comm_ident[0] = "";
60 last_comm_ident[1] = "";
62 last_in_range = false;
63 comm_type[0] = INVALID;
64 comm_type[1] = INVALID;
65 comm_atc_ptr[0] = NULL;
66 comm_atc_ptr[1] = NULL;
69 FGATCMgr::~FGATCMgr() {
73 void FGATCMgr::bind() {
76 void FGATCMgr::unbind() {
79 void FGATCMgr::init() {
80 comm_node[0] = fgGetNode("/radios/comm[0]/frequencies/selected-mhz", true);
81 comm_node[1] = fgGetNode("/radios/comm[1]/frequencies/selected-mhz", true);
82 lon_node = fgGetNode("/position/longitude-deg", true);
83 lat_node = fgGetNode("/position/latitude-deg", true);
84 elev_node = fgGetNode("/position/altitude-ft", true);
85 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 // Initialise the airport_atc_map - we'll cheat for now and just hardcode KEMT and any others that may be needed for development
97 AirportATC *a = new AirportATC;
101 a->atis_freq = 118.75;
102 a->atis_active = false;
103 a->tower_freq = 121.2;
104 a->tower_active = false;
105 a->ground_freq = 125.9;
106 a->ground_active = false;
108 //a->set_by_AI = true;
109 //a->set_by_comm_search = false;
111 airport_atc_map[(string)"KEMT"] = a;
113 #ifdef ENABLE_AUDIO_SUPPORT
114 // Load all available voices.
115 // For now we'll do one hardwired one
118 voiceOK = v1->LoadVoice("default");
121 /* I've loaded the voice even if /sim/sound/audible is false
122 * since I know no way of forcing load of the voice if the user
123 * subsequently switches /sim/sound/audible to true. */
128 // Initialise the ATC Dialogs
129 //cout << "Initing Transmissions..." << endl;
130 SG_LOG(SG_GENERAL, SG_INFO, " ATC Transmissions");
131 current_transmissionlist = new FGTransmissionList;
132 SGPath p_transmission( globals->get_fg_root() );
133 p_transmission.append( "ATC/default.transmissions" );
134 current_transmissionlist->init( p_transmission );
135 //cout << "Done Transmissions" << endl;
137 SG_LOG(SG_GENERAL, SG_INFO, " ATC Dialog System");
138 current_atcdialog = new FGATCDialog;
139 current_atcdialog->Init();
144 //current_atcdialog->add_entry( "EGNX", "Request vectoring for approach", "Request Vectors" );
145 //current_atcdialog->add_entry( "EGNX", "Mayday, Mayday", "Declare Emergency" );
148 bool ok = current_commlist->FindByCode("KEMT", test, TOWER);
150 cout << "KEMT tower frequency from test was " << test.freq << endl;
152 cout << "KEMT tower frequency not found :-(" << endl;
158 void FGATCMgr::update(double dt) {
159 //cout << "Entering update..." << endl;
160 //Traverse the list of active stations.
161 //Only update one class per update step to avoid the whole ATC system having to calculate between frames.
162 //Eventually we should only update every so many steps.
163 //cout << "In FGATCMgr::update - atc_list.size = " << atc_list.size() << '\n';
164 if(atc_list.size()) {
165 if(atc_list_itr == atc_list.end()) {
166 atc_list_itr = atc_list.begin();
168 //cout << "Updating " << (*atc_list_itr)->get_ident() << ' ' << (*atc_list_itr)->GetType() << '\n';
169 //cout << "Freq = " << (*atc_list_itr)->get_freq() << '\n';
170 (*atc_list_itr)->Update();
171 //cout << "Done ATC update..." << endl;
175 // Search the tuned frequencies every now and then - this should be done with the event scheduler
181 //cout << "About to search(1)" << endl;
185 //cout << "About to search(2)" << endl;
191 //cout << "comm1 type = " << comm_type[0] << '\n';
194 // Remove from list only if not needed by the AI system or the other comm channel
195 // TODO - implement me!!
196 void FGATCMgr::CommRemoveFromList(const char* id, atc_type tp, int chan) {
198 if(GetAirportATCDetails((string)id, &a)) {
201 a.set_by_comm_search = false;
202 airport_atc_map[(string)id] = a;
209 // Hack - need to implement this properly
210 RemoveFromList(id, tp);
214 // Remove from list - should only be called from above or similar
215 // This function *will* remove it from the list regardless of who else might want it.
216 void FGATCMgr::RemoveFromList(const char* id, atc_type tp) {
217 //cout << "Requested type = " << tp << '\n';
218 //cout << "id = " << id << '\n';
219 atc_list_itr = atc_list.begin();
220 while(atc_list_itr != atc_list.end()) {
221 //cout << "type = " << (*atc_list_itr)->GetType() << '\n';
222 //cout << "Ident = " << (*atc_list_itr)->get_ident() << '\n';
223 if( (!strcmp((*atc_list_itr)->get_ident(), id))
224 && ((*atc_list_itr)->GetType() == tp) ) {
225 //Before removing it stop it transmitting!!
226 //cout << "OBLITERATING FROM LIST!!!\n";
227 (*atc_list_itr)->SetNoDisplay();
228 (*atc_list_itr)->Update();
229 delete (*atc_list_itr);
230 atc_list_itr = atc_list.erase(atc_list_itr);
232 } // Note that that can upset where we are in the list but that doesn't really matter
238 // Find in list - return a currently active ATC pointer given ICAO code and type
239 // Return NULL if the given service is not in the list
240 // - *** THE CALLING FUNCTION MUST CHECK FOR THIS ***
241 FGATC* FGATCMgr::FindInList(const char* id, atc_type tp) {
242 atc_list_itr = atc_list.begin();
243 while(atc_list_itr != atc_list.end()) {
244 if( (!strcmp((*atc_list_itr)->get_ident(), id))
245 && ((*atc_list_itr)->GetType() == tp) ) {
246 return(*atc_list_itr);
247 } // Note that that can upset where we are in the list but that shouldn't really matter
250 // If we get here it's not in the list
254 // Returns true if the airport is found in the map
255 bool FGATCMgr::GetAirportATCDetails(string icao, AirportATC* a) {
256 if(airport_atc_map.find(icao) != airport_atc_map.end()) {
257 *a = *airport_atc_map[icao];
265 // Return a pointer to a given sort of ATC at a given airport and activate if necessary
266 // ONLY CALL THIS FUNCTION AFTER FIRST CHECKING THE SERVICE EXISTS BY CALLING GetAirportATCDetails
267 // FIXME - we really ought to take out the necessity for two function calls by simply returning
268 // a NULL pointer if the service doesn't exist and requiring the caller to check for it (NULL).
269 FGATC* FGATCMgr::GetATCPointer(string icao, atc_type type) {
270 AirportATC *a = airport_atc_map[icao];
271 //cout << "a->lon = " << a->lon << '\n';
272 //cout << "a->elev = " << a->elev << '\n';
273 //cout << "a->tower_freq = " << a->tower_freq << '\n';
276 if(a->tower_active) {
277 // Get the pointer from the list
278 return(FindInList(icao.c_str(), type)); // DCL - this untested so far.
280 FGTower* t = new FGTower;
282 if(current_commlist->FindByFreq(a->lon, a->lat, a->elev, a->tower_freq, &data, TOWER)) {
284 atc_list.push_back(t);
285 a->tower_active = true;
286 airport_atc_map[icao] = a;
289 cout << "ERROR - tower that should exist in FGATCMgr::GetATCPointer for airport " << icao << " not found\n";
293 // Lets add the rest to get rid of the compiler warnings even though we don't need them yet.
297 SG_LOG(SG_GENERAL, SG_ALERT, "ERROR - ATIS station should not be requested from FGATCMgr::GetATCPointer");
309 SG_LOG(SG_GENERAL, SG_ALERT, "ERROR IN FGATCMgr - reached end of GetATCPointer");
314 // Return a pointer to an appropriate voice for a given type of ATC
315 // creating the voice if necessary - ie. make sure exactly one copy
316 // of every voice in use exists in memory.
318 // TODO - in the future this will get more complex and dole out country/airport
319 // specific voices, and possible make sure that the same voice doesn't get used
320 // at different airports in quick succession if a large enough selection are available.
321 FGATCVoice* FGATCMgr::GetVoicePointer(atc_type type) {
322 // TODO - implement me better - maintain a list of loaded voices and other voices!!
344 // Display a dialog box with options relevant to the currently tuned ATC service.
345 void FGATCMgr::doPopupDialog() {
346 ATCDoDialog(comm_type[0]); // FIXME - currently hardwired to comm1
349 // Search for ATC stations by frequency
350 void FGATCMgr::FreqSearch(int channel) {
351 int chan = channel - 1; // Convert to zero-based for the arrays
354 double freq = comm_node[chan]->getDoubleValue();
355 lon = lon_node->getDoubleValue();
356 lat = lat_node->getDoubleValue();
357 elev = elev_node->getDoubleValue() * SG_FEET_TO_METER;
359 // Query the data store and get the closest match if any
360 if(current_commlist->FindByFreq(lon, lat, elev, freq, &data)) {
363 // If this channel not previously valid then easy - add ATC to list
364 // If this channel was valid then - Have we tuned to a different service?
365 // If so - de-register one and add the other
366 if(comm_valid[chan]) {
367 if((comm_ident[chan] == data.ident) && (comm_type[chan] == data.type)) {
368 // Then we're still tuned into the same service so do nought and return
371 // Something's changed - either the location or the service type
372 // We need to feed the channel in so we're not removing it if we're also tuned in on the other channel
373 CommRemoveFromList(comm_ident[chan], comm_type[chan], chan);
376 // At this point we can assume that we need to add the service.
377 comm_ident[chan] = (data.ident).c_str();
378 comm_type[chan] = data.type;
379 comm_x[chan] = (double)data.x;
380 comm_y[chan] = (double)data.y;
381 comm_z[chan] = (double)data.z;
382 comm_lon[chan] = (double)data.lon;
383 comm_lat[chan] = (double)data.lat;
384 comm_elev[chan] = (double)data.elev;
385 comm_valid[chan] = true;
387 // This was a switch-case statement but the compiler didn't like the new variable creation with it.
388 if(comm_type[chan] == ATIS) {
389 FGATIS* a = new FGATIS;
391 comm_atc_ptr[chan] = a;
393 //a->set_refname((chan) ? "atis2" : "atis1"); // FIXME - that line is limited to 2 channels
394 atc_list.push_back(a);
395 } else if (comm_type[chan] == TOWER) {
396 FGATC* app = FindInList(comm_ident[chan], TOWER);
398 // The station is already in the ATC list
401 // Generate the station and put in the ATC list
402 FGTower* t = new FGTower;
404 comm_atc_ptr[chan] = t;
406 atc_list.push_back(t);
408 } else if (comm_type[chan] == APPROACH) {
409 // We have to be a bit more carefull here since approaches are also searched by area
410 FGATC* app = FindInList(comm_ident[chan], APPROACH);
412 // The station is already in the ATC list
413 app->AddPlane("Player");
415 comm_atc_ptr[chan] = app;
417 // Generate the station and put in the ATC list
418 FGApproach* a = new FGApproach;
420 a->AddPlane("Player");
421 atc_list.push_back(a);
422 comm_atc_ptr[chan] = a;
426 if(comm_valid[chan]) {
427 if(comm_type[chan] != APPROACH) {
428 // Currently approaches are removed by Alexander's out-of-range mechanism
429 CommRemoveFromList(comm_ident[chan], comm_type[chan], chan);
431 // Note that we *don't* call SetNoDisplay() here because the other comm channel
432 // might be tuned into the same station - this is handled by CommRemoveFromList(...)
433 comm_type[chan] = INVALID;
434 comm_atc_ptr[chan] = NULL;
435 comm_valid[chan] = false;
441 // Search ATC stations by area in order that we appear 'on the radar'
442 void FGATCMgr::AreaSearch() {
443 // Search for Approach stations
444 comm_list_type approaches;
445 comm_list_iterator app_itr;
447 lon = lon_node->getDoubleValue();
448 lat = lat_node->getDoubleValue();
449 elev = elev_node->getDoubleValue() * SG_FEET_TO_METER;
451 // search stations in range
452 int num_app = current_commlist->FindByPos(lon, lat, elev, &approaches, APPROACH);
454 //cout << num_app << " approaches found in radiostack search !!!!" << endl;
456 for(app_itr = approaches.begin(); app_itr != approaches.end(); app_itr++) {
458 FGATC* app = FindInList((app_itr->ident).c_str(), app_itr->type);
460 // The station is already in the ATC list
461 app->AddPlane("Player");
464 // Generate the station and put in the ATC list
465 FGApproach* a = new FGApproach;
466 a->SetData(&(*app_itr));
467 a->AddPlane("Player");
469 atc_list.push_back(a);
474 // remove planes which are out of range
475 // TODO - I'm not entirely sure that this belongs here.
476 atc_list_itr = atc_list.begin();
477 while(atc_list_itr != atc_list.end()) {
478 if((*atc_list_itr)->GetType() == APPROACH ) {
479 int np = (*atc_list_itr)->RemovePlane();
480 // if approach has no planes left remove it from ATC list
482 (*atc_list_itr)->SetNoDisplay();
483 (*atc_list_itr)->Update();
484 delete (*atc_list_itr);
485 atc_list_itr = atc_list.erase(atc_list_itr);
486 break; // the other stations will be checked next time