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 FGATCMgr::FGATCMgr() {
45 last_comm_ident[0] = "";
46 last_comm_ident[1] = "";
48 last_in_range = false;
49 comm_type[0] = INVALID;
50 comm_type[1] = INVALID;
51 comm_atc_ptr[0] = NULL;
52 comm_atc_ptr[1] = NULL;
55 FGATCMgr::~FGATCMgr() {
58 void FGATCMgr::bind() {
61 void FGATCMgr::unbind() {
64 void FGATCMgr::init() {
65 comm_node[0] = fgGetNode("/radios/comm[0]/frequencies/selected-mhz", true);
66 comm_node[1] = fgGetNode("/radios/comm[1]/frequencies/selected-mhz", true);
67 lon_node = fgGetNode("/position/longitude-deg", true);
68 lat_node = fgGetNode("/position/latitude-deg", true);
69 elev_node = fgGetNode("/position/altitude-ft", true);
70 atc_list_itr = atc_list.begin();
71 // Search for connected ATC stations once per 0.8 seconds or so
72 // global_events.Register( "fgATCSearch()", fgATCSearch,
73 // fgEVENT::FG_EVENT_READY, 800);
74 // For some reason the above doesn't compile - including Time/event.hxx stops compilation.
76 // Initialise the frequency search map
77 current_commlist = new FGCommList;
78 SGPath p_comm( globals->get_fg_root() );
79 current_commlist->init( p_comm );
81 // Initialise the airport_atc_map - we'll cheat for now and just hardcode KEMT and any others that may be needed for development
82 AirportATC *a = new AirportATC;
86 a->atis_freq = 118.75;
87 a->atis_active = false;
88 a->tower_freq = 121.2;
89 a->tower_active = false;
90 a->ground_freq = 125.9;
91 a->ground_active = false;
93 //a->set_by_AI = true;
94 //a->set_by_comm_search = false;
96 airport_atc_map[(string)"KEMT"] = a;
98 #ifdef ENABLE_AUDIO_SUPPORT
99 // Load all available voices.
100 // For now we'll do one hardwired one
102 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 void FGATCMgr::update(double dt) {
132 //cout << "Entering update..." << endl;
133 //Traverse the list of active stations.
134 //Only update one class per update step to avoid the whole ATC system having to calculate between frames.
135 //Eventually we should only update every so many steps.
136 //cout << "In FGATCMgr::update - atc_list.size = " << atc_list.size() << '\n';
137 if(atc_list.size()) {
138 if(atc_list_itr == atc_list.end()) {
139 atc_list_itr = atc_list.begin();
141 //cout << "Updating " << (*atc_list_itr)->get_ident() << ' ' << (*atc_list_itr)->GetType() << '\n';
142 //cout << "Freq = " << (*atc_list_itr)->get_freq() << '\n';
143 (*atc_list_itr)->Update();
144 //cout << "Done ATC update..." << endl;
148 // Search the tuned frequencies every now and then - this should be done with the event scheduler
154 //cout << "About to search(1)" << endl;
158 //cout << "About to search(2)" << endl;
164 //cout << "comm1 type = " << comm_type[0] << '\n';
167 // Remove from list only if not needed by the AI system or the other comm channel
168 // TODO - implement me!!
169 void FGATCMgr::CommRemoveFromList(const char* id, atc_type tp, int chan) {
171 if(GetAirportATCDetails((string)id, &a)) {
174 a.set_by_comm_search = false;
175 airport_atc_map[(string)id] = a;
182 // Hack - need to implement this properly
183 RemoveFromList(id, tp);
187 // Remove from list - should only be called from above or similar
188 // This function *will* remove it from the list regardless of who else might want it.
189 void FGATCMgr::RemoveFromList(const char* id, atc_type tp) {
190 //cout << "Requested type = " << tp << '\n';
191 //cout << "id = " << id << '\n';
192 atc_list_itr = atc_list.begin();
193 while(atc_list_itr != atc_list.end()) {
194 //cout << "type = " << (*atc_list_itr)->GetType() << '\n';
195 //cout << "Ident = " << (*atc_list_itr)->get_ident() << '\n';
196 if( (!strcmp((*atc_list_itr)->get_ident(), id))
197 && ((*atc_list_itr)->GetType() == tp) ) {
198 //Before removing it stop it transmitting!!
199 //cout << "OBLITERATING FROM LIST!!!\n";
200 (*atc_list_itr)->SetNoDisplay();
201 (*atc_list_itr)->Update();
202 delete (*atc_list_itr);
203 atc_list_itr = atc_list.erase(atc_list_itr);
205 } // Note that that can upset where we are in the list but that doesn't really matter
211 // Find in list - return a currently active ATC pointer given ICAO code and type
212 // Return NULL if the given service is not in the list
213 // - *** THE CALLING FUNCTION MUST CHECK FOR THIS ***
214 FGATC* FGATCMgr::FindInList(const char* id, atc_type tp) {
215 atc_list_itr = atc_list.begin();
216 while(atc_list_itr != atc_list.end()) {
217 if( (!strcmp((*atc_list_itr)->get_ident(), id))
218 && ((*atc_list_itr)->GetType() == tp) ) {
219 return(*atc_list_itr);
220 } // Note that that can upset where we are in the list but that shouldn't really matter
223 // If we get here it's not in the list
227 // Returns true if the airport is found in the map
228 bool FGATCMgr::GetAirportATCDetails(string icao, AirportATC* a) {
229 if(airport_atc_map.find(icao) != airport_atc_map.end()) {
230 *a = *airport_atc_map[icao];
238 // Return a pointer to a given sort of ATC at a given airport and activate if necessary
239 // ONLY CALL THIS FUNCTION AFTER FIRST CHECKING THE SERVICE EXISTS BY CALLING GetAirportATCDetails
240 // FIXME - we really ought to take out the necessity for two function calls by simply returning
241 // a NULL pointer if the service doesn't exist and requiring the caller to check for it (NULL).
242 FGATC* FGATCMgr::GetATCPointer(string icao, atc_type type) {
243 AirportATC *a = airport_atc_map[icao];
244 //cout << "a->lon = " << a->lon << '\n';
245 //cout << "a->elev = " << a->elev << '\n';
246 //cout << "a->tower_freq = " << a->tower_freq << '\n';
249 if(a->tower_active) {
250 // Get the pointer from the list
251 return(FindInList(icao.c_str(), type)); // DCL - this untested so far.
253 FGTower* t = new FGTower;
255 if(current_commlist->FindByFreq(a->lon, a->lat, a->elev, a->tower_freq, &data, TOWER)) {
257 atc_list.push_back(t);
258 a->tower_active = true;
259 airport_atc_map[icao] = a;
262 cout << "ERROR - tower that should exist in FGATCMgr::GetATCPointer for airport " << icao << " not found\n";
266 // Lets add the rest to get rid of the compiler warnings even though we don't need them yet.
270 SG_LOG(SG_GENERAL, SG_ALERT, "ERROR - ATIS station should not be requested from FGATCMgr::GetATCPointer");
282 SG_LOG(SG_GENERAL, SG_ALERT, "ERROR IN FGATCMgr - reached end of GetATCPointer");
288 // Render a transmission
289 // Outputs the transmission either on screen or as audio depending on user preference
290 // The refname is a string to identify this sample to the sound manager
291 // The repeating flag indicates whether the message should be repeated continuously or played once.
292 void FGATCMgr::Render(string msg, string refname, bool repeating) {
293 #ifdef ENABLE_AUDIO_SUPPORT
294 voice = (voiceOK && fgGetBool("/sim/sound/audible")
295 && fgGetBool("/sim/sound/voice"));
298 unsigned char* buf = v1.WriteMessage((char*)msg.c_str(), len, voice);
300 FGSimpleSound* simple = new FGSimpleSound(buf, len);
301 // TODO - at the moment the volume is always set off comm1
302 // and can't be changed after the transmission has started.
303 simple->set_volume(5.0 * fgGetDouble("/radios/comm[0]/volume"));
304 globals->get_soundmgr()->add(simple, refname);
306 globals->get_soundmgr()->play_looped(refname);
308 globals->get_soundmgr()->play_once(refname);
313 #endif // ENABLE_AUDIO_SUPPORT
315 // first rip the underscores and the pause hints out of the string - these are for the convienience of the voice parser
316 for(unsigned int i = 0; i < msg.length(); ++i) {
317 if((msg.substr(i,1) == "_") || (msg.substr(i,1) == "/")) {
321 globals->get_ATC_display()->RegisterRepeatingMessage(msg);
327 // Cease rendering a transmission.
328 void FGATCMgr::NoRender(string refname) {
331 #ifdef ENABLE_AUDIO_SUPPORT
332 globals->get_soundmgr()->stop(refname);
333 globals->get_soundmgr()->remove(refname);
336 globals->get_ATC_display()->CancelRepeatingMessage();
343 // Display a dialog box with options relevant to the currently tuned ATC service.
344 void FGATCMgr::doPopupDialog() {
345 ATCDoDialog(comm_type[0]); // FIXME - currently hardwired to comm1
348 // Search for ATC stations by frequency
349 void FGATCMgr::FreqSearch(int channel) {
350 int chan = channel - 1; // Convert to zero-based for the arrays
353 double freq = comm_node[chan]->getDoubleValue();
354 lon = lon_node->getDoubleValue();
355 lat = lat_node->getDoubleValue();
356 elev = elev_node->getDoubleValue() * SG_FEET_TO_METER;
358 // Query the data store and get the closest match if any
359 if(current_commlist->FindByFreq(lon, lat, elev, freq, &data)) {
362 // If this channel not previously valid then easy - add ATC to list
363 // If this channel was valid then - Have we tuned to a different service?
364 // If so - de-register one and add the other
365 if(comm_valid[chan]) {
366 if((comm_ident[chan] == data.ident) && (comm_type[chan] == data.type)) {
367 // Then we're still tuned into the same service so do nought and return
370 // Something's changed - either the location or the service type
371 // We need to feed the channel in so we're not removing it if we're also tuned in on the other channel
372 CommRemoveFromList(comm_ident[chan], comm_type[chan], chan);
375 // At this point we can assume that we need to add the service.
376 comm_ident[chan] = (data.ident).c_str();
377 comm_type[chan] = data.type;
378 comm_x[chan] = (double)data.x;
379 comm_y[chan] = (double)data.y;
380 comm_z[chan] = (double)data.z;
381 comm_lon[chan] = (double)data.lon;
382 comm_lat[chan] = (double)data.lat;
383 comm_elev[chan] = (double)data.elev;
384 comm_valid[chan] = true;
386 // This was a switch-case statement but the compiler didn't like the new variable creation with it.
387 if(comm_type[chan] == ATIS) {
388 FGATIS* a = new FGATIS;
390 comm_atc_ptr[chan] = a;
392 //a->set_refname((chan) ? "atis2" : "atis1"); // FIXME - that line is limited to 2 channels
393 atc_list.push_back(a);
394 } else if (comm_type[chan] == TOWER) {
395 FGATC* app = FindInList(comm_ident[chan], TOWER);
397 // The station is already in the ATC list
400 // Generate the station and put in the ATC list
401 FGTower* t = new FGTower;
403 comm_atc_ptr[chan] = t;
405 atc_list.push_back(t);
407 } else if (comm_type[chan] == APPROACH) {
408 // We have to be a bit more carefull here since approaches are also searched by area
409 FGATC* app = FindInList(comm_ident[chan], APPROACH);
411 // The station is already in the ATC list
412 app->AddPlane("Player");
414 comm_atc_ptr[chan] = app;
416 // Generate the station and put in the ATC list
417 FGApproach* a = new FGApproach;
419 a->AddPlane("Player");
420 atc_list.push_back(a);
421 comm_atc_ptr[chan] = a;
425 if(comm_valid[chan]) {
426 if(comm_type[chan] != APPROACH) {
427 // Currently approaches are removed by Alexander's out-of-range mechanism
428 CommRemoveFromList(comm_ident[chan], comm_type[chan], chan);
430 // Note that we *don't* call SetNoDisplay() here because the other comm channel
431 // might be tuned into the same station - this is handled by CommRemoveFromList(...)
432 comm_type[chan] = INVALID;
433 comm_atc_ptr[chan] = NULL;
434 comm_valid[chan] = false;
440 // Search ATC stations by area in order that we appear 'on the radar'
441 void FGATCMgr::AreaSearch() {
442 // Search for Approach stations
443 comm_list_type approaches;
444 comm_list_iterator app_itr;
446 lon = lon_node->getDoubleValue();
447 lat = lat_node->getDoubleValue();
448 elev = elev_node->getDoubleValue() * SG_FEET_TO_METER;
450 // search stations in range
451 int num_app = current_commlist->FindByPos(lon, lat, elev, &approaches, APPROACH);
453 //cout << num_app << " approaches found in radiostack search !!!!" << endl;
455 for(app_itr = approaches.begin(); app_itr != approaches.end(); app_itr++) {
457 FGATC* app = FindInList((app_itr->ident).c_str(), app_itr->type);
459 // The station is already in the ATC list
460 app->AddPlane("Player");
463 // Generate the station and put in the ATC list
464 FGApproach* a = new FGApproach;
465 a->SetData(&(*app_itr));
466 a->AddPlane("Player");
468 atc_list.push_back(a);
473 // remove planes which are out of range
474 // TODO - I'm not entirely sure that this belongs here.
475 atc_list_itr = atc_list.begin();
476 while(atc_list_itr != atc_list.end()) {
477 if((*atc_list_itr)->GetType() == APPROACH ) {
478 int np = (*atc_list_itr)->RemovePlane();
479 // if approach has no planes left remove it from ATC list
481 (*atc_list_itr)->SetNoDisplay();
482 (*atc_list_itr)->Update();
483 delete (*atc_list_itr);
484 atc_list_itr = atc_list.erase(atc_list_itr);
485 break; // the other stations will be checked next time