]> git.mxchange.org Git - flightgear.git/blob - src/ATC/ATCmgr.cxx
Added basic support for using more than one voice. The render function is moved...
[flightgear.git] / src / ATC / ATCmgr.cxx
1 // ATCmgr.cxx - Implementation of FGATCMgr - a global Flightgear ATC manager.
2 //
3 // Written by David Luff, started February 2002.
4 //
5 // Copyright (C) 2002  David C Luff - david.luff@nottingham.ac.uk
6 //
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.
11 //
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.
16 //
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.
20
21 //#include <Time/event.hxx>
22
23 #include <simgear/misc/sg_path.hxx>
24 #include <simgear/debug/logstream.hxx>
25
26 #include "ATCmgr.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"
34
35 /*
36 // periodic radio station search wrapper
37 static void fgATCSearch( void ) {
38         globals->get_ATC_mgr()->Search();
39 }
40 */ //This wouldn't compile - including Time/event.hxx breaks it :-(
41
42 FGATCMgr::FGATCMgr() {
43         comm_ident[0] = "";
44         comm_ident[1] = "";
45         last_comm_ident[0] = "";
46         last_comm_ident[1] = "";
47         approach_ident = "";
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;
53 }
54
55 FGATCMgr::~FGATCMgr() {
56         delete v1;
57 }
58
59 void FGATCMgr::bind() {
60 }
61
62 void FGATCMgr::unbind() {
63 }
64
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.
76         
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 );
81         
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;
84         a->lon = -118.034719;
85         a->lat = 34.086114;
86         a->elev = 296.0;
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;
93         
94         //a->set_by_AI = true;
95         //a->set_by_comm_search = false;
96         
97         airport_atc_map[(string)"KEMT"] = a;
98
99 #ifdef ENABLE_AUDIO_SUPPORT     
100         // Load all available voices.
101         // For now we'll do one hardwired one
102         
103         v1 = new FGATCVoice;
104         voiceOK = v1->LoadVoice("default");
105         voice = true;
106         
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. */
110 #else
111         voice = false;
112 #endif
113
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;
122
123     SG_LOG(SG_GENERAL, SG_INFO, "  ATC Dialog System");
124     current_atcdialog = new FGATCDialog;
125     current_atcdialog->Init();
126
127         ATCDialogInit();
128         
129         // DCL - testing
130         //current_atcdialog->add_entry( "EGNX", "Request vectoring for approach", "Request Vectors" );
131         //current_atcdialog->add_entry( "EGNX", "Mayday, Mayday", "Declare Emergency" );
132 }
133
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();
143                 }
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;
148                 ++atc_list_itr;
149         }
150         
151         // Search the tuned frequencies every now and then - this should be done with the event scheduler
152         static int i = 0;
153         if(i == 7) {
154                 AreaSearch();
155         }
156         if(i == 15) {
157                 //cout << "About to search(1)" << endl;
158                 FreqSearch(1);
159         }
160         if(i == 30) {
161                 //cout << "About to search(2)" << endl;
162                 FreqSearch(2);
163                 i = 0;
164         }
165         ++i;
166         
167         //cout << "comm1 type = " << comm_type[0] << '\n';
168 }
169
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) {
173         /*AirportATC a;
174         if(GetAirportATCDetails((string)id, &a)) {
175                 if(a.set_by_AI) {
176                         // Don't remove
177                         a.set_by_comm_search = false;
178                         airport_atc_map[(string)id] = a;
179                         return;
180                 } else {
181                         // remove
182                 }
183         }*/
184         
185         // Hack - need to implement this properly
186         RemoveFromList(id, tp);
187 }
188     
189
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);
207                                 break;
208                         }  // Note that that can upset where we are in the list but that doesn't really matter
209                 ++atc_list_itr;
210         }
211 }
212
213
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
224                 ++atc_list_itr;
225         }
226         // If we get here it's not in the list
227         return(NULL);
228 }
229
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];
234                 return(true);
235         } else {
236                 return(false);
237         }
238 }
239
240
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';
250         switch(type) {
251                 case TOWER:
252                 if(a->tower_active) {
253                         // Get the pointer from the list
254                         return(FindInList(icao.c_str(), type)); // DCL - this untested so far.
255                 } else {
256                         FGTower* t = new FGTower;
257                         ATCData data;
258                         if(current_commlist->FindByFreq(a->lon, a->lat, a->elev, a->tower_freq, &data, TOWER)) {
259                                 t->SetData(&data);
260                                 atc_list.push_back(t);
261                                 a->tower_active = true;
262                                 airport_atc_map[icao] = a;
263                                 return(t);
264                         } else {
265                                 cout << "ERROR - tower that should exist in FGATCMgr::GetATCPointer for airport " << icao << " not found\n";
266                         }
267                 }
268                 break;
269                 // Lets add the rest to get rid of the compiler warnings even though we don't need them yet.
270                 case APPROACH:
271                 break;
272                 case ATIS:
273                 SG_LOG(SG_GENERAL, SG_ALERT, "ERROR - ATIS station should not be requested from FGATCMgr::GetATCPointer");
274                 break;
275                 case GROUND:
276                 break;
277                 case INVALID:
278                 break;
279                 case ENROUTE:
280                 break;
281                 case DEPARTURE:
282                 break;
283         }
284         
285         SG_LOG(SG_GENERAL, SG_ALERT, "ERROR IN FGATCMgr - reached end of GetATCPointer");
286         
287         return(NULL);
288 }
289
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.
293 //
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!!
299         if(voice) {
300                 switch(type) {
301                 case ATIS:
302                         if(voiceOK) {
303                                 return(v1);
304                         }
305                 case TOWER:
306                         return(NULL);
307                 case APPROACH:
308                         return(NULL);
309                 case GROUND:
310                         return(NULL);
311                 default:
312                         return(NULL);
313                 }
314                 return(NULL);
315         } else {
316                 return(NULL);
317         }
318 }
319
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
323 }
324
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
328
329         ATCData data;   
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;
334         
335         // Query the data store and get the closest match if any
336         if(current_commlist->FindByFreq(lon, lat, elev, freq, &data)) {
337                 // We have a match
338                 // What's the logic?
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
345                                 return;
346                         } else {
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);
350                         }
351                 }
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;
362                 
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;
366                         a->SetData(&data);
367                         comm_atc_ptr[chan] = a;
368                         a->SetDisplay();
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);
373                         if(app != NULL) {
374                                 // The station is already in the ATC list
375                                 app->SetDisplay();
376                         } else {
377                                 // Generate the station and put in the ATC list
378                                 FGTower* t = new FGTower;
379                                 t->SetData(&data);
380                                 comm_atc_ptr[chan] = t;
381                                 t->SetDisplay();
382                                 atc_list.push_back(t);
383                         }
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);
387                         if(app != NULL) {
388                                 // The station is already in the ATC list
389                                 app->AddPlane("Player");
390                                 app->SetDisplay();
391                                 comm_atc_ptr[chan] = app;
392                         } else {
393                                 // Generate the station and put in the ATC list
394                                 FGApproach* a = new FGApproach;
395                                 a->SetData(&data);
396                                 a->AddPlane("Player");
397                                 atc_list.push_back(a);
398                                 comm_atc_ptr[chan] = a;
399                         }                       
400                 }
401         } else {
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);
406                         }
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;
412                 }
413         }
414 }
415
416
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;
422         
423         lon = lon_node->getDoubleValue();
424         lat = lat_node->getDoubleValue();
425         elev = elev_node->getDoubleValue() * SG_FEET_TO_METER;
426         
427         // search stations in range
428         int num_app = current_commlist->FindByPos(lon, lat, elev, &approaches, APPROACH);
429         if (num_app != 0) {
430                 //cout << num_app << " approaches found in radiostack search !!!!" << endl;
431                 
432                 for(app_itr = approaches.begin(); app_itr != approaches.end(); app_itr++) {
433                         
434                         FGATC* app = FindInList((app_itr->ident).c_str(), app_itr->type);
435                         if(app != NULL) {
436                                 // The station is already in the ATC list
437                                 app->AddPlane("Player");
438                                 //app->Update();
439                         } else {
440                                 // Generate the station and put in the ATC list
441                                 FGApproach* a = new FGApproach;
442                                 a->SetData(&(*app_itr));
443                                 a->AddPlane("Player");
444                                 //a->Update();
445                                 atc_list.push_back(a);
446                         }
447                 }
448         }
449         
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
457                         if ( np == 0) {
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
463                         }
464                 }
465                 ++atc_list_itr;
466         }
467 }