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() {
59 void FGATCMgr::bind() {
62 void FGATCMgr::unbind() {
65 void FGATCMgr::init() {
66 comm_node[0] = fgGetNode("/radios/comm[0]/frequencies/selected-mhz", true);
67 comm_node[1] = fgGetNode("/radios/comm[1]/frequencies/selected-mhz", true);
68 lon_node = fgGetNode("/position/longitude-deg", true);
69 lat_node = fgGetNode("/position/latitude-deg", true);
70 elev_node = fgGetNode("/position/altitude-ft", true);
71 atc_list_itr = atc_list.begin();
72 // Search for connected ATC stations once per 0.8 seconds or so
73 // global_events.Register( "fgATCSearch()", fgATCSearch,
74 // fgEVENT::FG_EVENT_READY, 800);
75 // For some reason the above doesn't compile - including Time/event.hxx stops compilation.
77 // Initialise the frequency search map
78 current_commlist = new FGCommList;
79 SGPath p_comm( globals->get_fg_root() );
80 current_commlist->init( p_comm );
82 // Initialise the airport_atc_map - we'll cheat for now and just hardcode KEMT and any others that may be needed for development
83 AirportATC *a = new AirportATC;
87 a->atis_freq = 118.75;
88 a->atis_active = false;
89 a->tower_freq = 121.2;
90 a->tower_active = false;
91 a->ground_freq = 125.9;
92 a->ground_active = false;
94 //a->set_by_AI = true;
95 //a->set_by_comm_search = false;
97 airport_atc_map[(string)"KEMT"] = a;
99 #ifdef ENABLE_AUDIO_SUPPORT
100 // Load all available voices.
101 // For now we'll do one hardwired one
104 voiceOK = v1->LoadVoice("default");
107 /* I've loaded the voice even if /sim/sound/audible is false
108 * since I know no way of forcing load of the voice if the user
109 * subsequently switches /sim/sound/audible to true. */
114 // Initialise the ATC Dialogs
115 //cout << "Initing Transmissions..." << endl;
116 SG_LOG(SG_GENERAL, SG_INFO, " ATC Transmissions");
117 current_transmissionlist = new FGTransmissionList;
118 SGPath p_transmission( globals->get_fg_root() );
119 p_transmission.append( "ATC/default.transmissions" );
120 current_transmissionlist->init( p_transmission );
121 //cout << "Done Transmissions" << endl;
123 SG_LOG(SG_GENERAL, SG_INFO, " ATC Dialog System");
124 current_atcdialog = new FGATCDialog;
125 current_atcdialog->Init();
130 //current_atcdialog->add_entry( "EGNX", "Request vectoring for approach", "Request Vectors" );
131 //current_atcdialog->add_entry( "EGNX", "Mayday, Mayday", "Declare Emergency" );
134 void FGATCMgr::update(double dt) {
135 //cout << "Entering update..." << endl;
136 //Traverse the list of active stations.
137 //Only update one class per update step to avoid the whole ATC system having to calculate between frames.
138 //Eventually we should only update every so many steps.
139 //cout << "In FGATCMgr::update - atc_list.size = " << atc_list.size() << '\n';
140 if(atc_list.size()) {
141 if(atc_list_itr == atc_list.end()) {
142 atc_list_itr = atc_list.begin();
144 //cout << "Updating " << (*atc_list_itr)->get_ident() << ' ' << (*atc_list_itr)->GetType() << '\n';
145 //cout << "Freq = " << (*atc_list_itr)->get_freq() << '\n';
146 (*atc_list_itr)->Update();
147 //cout << "Done ATC update..." << endl;
151 // Search the tuned frequencies every now and then - this should be done with the event scheduler
157 //cout << "About to search(1)" << endl;
161 //cout << "About to search(2)" << endl;
167 //cout << "comm1 type = " << comm_type[0] << '\n';
170 // Remove from list only if not needed by the AI system or the other comm channel
171 // TODO - implement me!!
172 void FGATCMgr::CommRemoveFromList(const char* id, atc_type tp, int chan) {
174 if(GetAirportATCDetails((string)id, &a)) {
177 a.set_by_comm_search = false;
178 airport_atc_map[(string)id] = a;
185 // Hack - need to implement this properly
186 RemoveFromList(id, tp);
190 // Remove from list - should only be called from above or similar
191 // This function *will* remove it from the list regardless of who else might want it.
192 void FGATCMgr::RemoveFromList(const char* id, atc_type tp) {
193 //cout << "Requested type = " << tp << '\n';
194 //cout << "id = " << id << '\n';
195 atc_list_itr = atc_list.begin();
196 while(atc_list_itr != atc_list.end()) {
197 //cout << "type = " << (*atc_list_itr)->GetType() << '\n';
198 //cout << "Ident = " << (*atc_list_itr)->get_ident() << '\n';
199 if( (!strcmp((*atc_list_itr)->get_ident(), id))
200 && ((*atc_list_itr)->GetType() == tp) ) {
201 //Before removing it stop it transmitting!!
202 //cout << "OBLITERATING FROM LIST!!!\n";
203 (*atc_list_itr)->SetNoDisplay();
204 (*atc_list_itr)->Update();
205 delete (*atc_list_itr);
206 atc_list_itr = atc_list.erase(atc_list_itr);
208 } // Note that that can upset where we are in the list but that doesn't really matter
214 // Find in list - return a currently active ATC pointer given ICAO code and type
215 // Return NULL if the given service is not in the list
216 // - *** THE CALLING FUNCTION MUST CHECK FOR THIS ***
217 FGATC* FGATCMgr::FindInList(const char* id, atc_type tp) {
218 atc_list_itr = atc_list.begin();
219 while(atc_list_itr != atc_list.end()) {
220 if( (!strcmp((*atc_list_itr)->get_ident(), id))
221 && ((*atc_list_itr)->GetType() == tp) ) {
222 return(*atc_list_itr);
223 } // Note that that can upset where we are in the list but that shouldn't really matter
226 // If we get here it's not in the list
230 // Returns true if the airport is found in the map
231 bool FGATCMgr::GetAirportATCDetails(string icao, AirportATC* a) {
232 if(airport_atc_map.find(icao) != airport_atc_map.end()) {
233 *a = *airport_atc_map[icao];
241 // Return a pointer to a given sort of ATC at a given airport and activate if necessary
242 // ONLY CALL THIS FUNCTION AFTER FIRST CHECKING THE SERVICE EXISTS BY CALLING GetAirportATCDetails
243 // FIXME - we really ought to take out the necessity for two function calls by simply returning
244 // a NULL pointer if the service doesn't exist and requiring the caller to check for it (NULL).
245 FGATC* FGATCMgr::GetATCPointer(string icao, atc_type type) {
246 AirportATC *a = airport_atc_map[icao];
247 //cout << "a->lon = " << a->lon << '\n';
248 //cout << "a->elev = " << a->elev << '\n';
249 //cout << "a->tower_freq = " << a->tower_freq << '\n';
252 if(a->tower_active) {
253 // Get the pointer from the list
254 return(FindInList(icao.c_str(), type)); // DCL - this untested so far.
256 FGTower* t = new FGTower;
258 if(current_commlist->FindByFreq(a->lon, a->lat, a->elev, a->tower_freq, &data, TOWER)) {
260 atc_list.push_back(t);
261 a->tower_active = true;
262 airport_atc_map[icao] = a;
265 cout << "ERROR - tower that should exist in FGATCMgr::GetATCPointer for airport " << icao << " not found\n";
269 // Lets add the rest to get rid of the compiler warnings even though we don't need them yet.
273 SG_LOG(SG_GENERAL, SG_ALERT, "ERROR - ATIS station should not be requested from FGATCMgr::GetATCPointer");
285 SG_LOG(SG_GENERAL, SG_ALERT, "ERROR IN FGATCMgr - reached end of GetATCPointer");
290 // Return a pointer to an appropriate voice for a given type of ATC
291 // creating the voice if necessary - ie. make sure exactly one copy
292 // of every voice in use exists in memory.
294 // TODO - in the future this will get more complex and dole out country/airport
295 // specific voices, and possible make sure that the same voice doesn't get used
296 // at different airports in quick succession if a large enough selection are available.
297 FGATCVoice* FGATCMgr::GetVoicePointer(atc_type type) {
298 // TODO - implement me better - maintain a list of loaded voices and other voices!!
320 // Display a dialog box with options relevant to the currently tuned ATC service.
321 void FGATCMgr::doPopupDialog() {
322 ATCDoDialog(comm_type[0]); // FIXME - currently hardwired to comm1
325 // Search for ATC stations by frequency
326 void FGATCMgr::FreqSearch(int channel) {
327 int chan = channel - 1; // Convert to zero-based for the arrays
330 double freq = comm_node[chan]->getDoubleValue();
331 lon = lon_node->getDoubleValue();
332 lat = lat_node->getDoubleValue();
333 elev = elev_node->getDoubleValue() * SG_FEET_TO_METER;
335 // Query the data store and get the closest match if any
336 if(current_commlist->FindByFreq(lon, lat, elev, freq, &data)) {
339 // If this channel not previously valid then easy - add ATC to list
340 // If this channel was valid then - Have we tuned to a different service?
341 // If so - de-register one and add the other
342 if(comm_valid[chan]) {
343 if((comm_ident[chan] == data.ident) && (comm_type[chan] == data.type)) {
344 // Then we're still tuned into the same service so do nought and return
347 // Something's changed - either the location or the service type
348 // We need to feed the channel in so we're not removing it if we're also tuned in on the other channel
349 CommRemoveFromList(comm_ident[chan], comm_type[chan], chan);
352 // At this point we can assume that we need to add the service.
353 comm_ident[chan] = (data.ident).c_str();
354 comm_type[chan] = data.type;
355 comm_x[chan] = (double)data.x;
356 comm_y[chan] = (double)data.y;
357 comm_z[chan] = (double)data.z;
358 comm_lon[chan] = (double)data.lon;
359 comm_lat[chan] = (double)data.lat;
360 comm_elev[chan] = (double)data.elev;
361 comm_valid[chan] = true;
363 // This was a switch-case statement but the compiler didn't like the new variable creation with it.
364 if(comm_type[chan] == ATIS) {
365 FGATIS* a = new FGATIS;
367 comm_atc_ptr[chan] = a;
369 //a->set_refname((chan) ? "atis2" : "atis1"); // FIXME - that line is limited to 2 channels
370 atc_list.push_back(a);
371 } else if (comm_type[chan] == TOWER) {
372 FGATC* app = FindInList(comm_ident[chan], TOWER);
374 // The station is already in the ATC list
377 // Generate the station and put in the ATC list
378 FGTower* t = new FGTower;
380 comm_atc_ptr[chan] = t;
382 atc_list.push_back(t);
384 } else if (comm_type[chan] == APPROACH) {
385 // We have to be a bit more carefull here since approaches are also searched by area
386 FGATC* app = FindInList(comm_ident[chan], APPROACH);
388 // The station is already in the ATC list
389 app->AddPlane("Player");
391 comm_atc_ptr[chan] = app;
393 // Generate the station and put in the ATC list
394 FGApproach* a = new FGApproach;
396 a->AddPlane("Player");
397 atc_list.push_back(a);
398 comm_atc_ptr[chan] = a;
402 if(comm_valid[chan]) {
403 if(comm_type[chan] != APPROACH) {
404 // Currently approaches are removed by Alexander's out-of-range mechanism
405 CommRemoveFromList(comm_ident[chan], comm_type[chan], chan);
407 // Note that we *don't* call SetNoDisplay() here because the other comm channel
408 // might be tuned into the same station - this is handled by CommRemoveFromList(...)
409 comm_type[chan] = INVALID;
410 comm_atc_ptr[chan] = NULL;
411 comm_valid[chan] = false;
417 // Search ATC stations by area in order that we appear 'on the radar'
418 void FGATCMgr::AreaSearch() {
419 // Search for Approach stations
420 comm_list_type approaches;
421 comm_list_iterator app_itr;
423 lon = lon_node->getDoubleValue();
424 lat = lat_node->getDoubleValue();
425 elev = elev_node->getDoubleValue() * SG_FEET_TO_METER;
427 // search stations in range
428 int num_app = current_commlist->FindByPos(lon, lat, elev, &approaches, APPROACH);
430 //cout << num_app << " approaches found in radiostack search !!!!" << endl;
432 for(app_itr = approaches.begin(); app_itr != approaches.end(); app_itr++) {
434 FGATC* app = FindInList((app_itr->ident).c_str(), app_itr->type);
436 // The station is already in the ATC list
437 app->AddPlane("Player");
440 // Generate the station and put in the ATC list
441 FGApproach* a = new FGApproach;
442 a->SetData(&(*app_itr));
443 a->AddPlane("Player");
445 atc_list.push_back(a);
450 // remove planes which are out of range
451 // TODO - I'm not entirely sure that this belongs here.
452 atc_list_itr = atc_list.begin();
453 while(atc_list_itr != atc_list.end()) {
454 if((*atc_list_itr)->GetType() == APPROACH ) {
455 int np = (*atc_list_itr)->RemovePlane();
456 // if approach has no planes left remove it from ATC list
458 (*atc_list_itr)->SetNoDisplay();
459 (*atc_list_itr)->Update();
460 delete (*atc_list_itr);
461 atc_list_itr = atc_list.erase(atc_list_itr);
462 break; // the other stations will be checked next time