]> git.mxchange.org Git - flightgear.git/blob - src/ATC/ATCmgr.cxx
Changes in support of Alexander's ATC menu/transmission work. In particular all...
[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 }
57
58 void FGATCMgr::bind() {
59 }
60
61 void FGATCMgr::unbind() {
62 }
63
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.
75         
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 );
80         
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;
83         a->lon = -118.034719;
84         a->lat = 34.086114;
85         a->elev = 296.0;
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;
92         
93         //a->set_by_AI = true;
94         //a->set_by_comm_search = false;
95         
96         airport_atc_map[(string)"KEMT"] = a;
97
98 #ifdef ENABLE_AUDIO_SUPPORT     
99         // Load all available voices.
100         // For now we'll do one hardwired one
101         
102         voiceOK = v1.LoadVoice("default");
103         
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. */
107 #else
108         voice = false;
109 #endif
110
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;
119
120     SG_LOG(SG_GENERAL, SG_INFO, "  ATC Dialog System");
121     current_atcdialog = new FGATCDialog;
122     current_atcdialog->Init();
123
124         ATCDialogInit();
125         
126         // DCL - testing
127         //current_atcdialog->add_entry( "EGNX", "Request vectoring for approach", "Request Vectors" );
128         //current_atcdialog->add_entry( "EGNX", "Mayday, Mayday", "Declare Emergency" );
129 }
130
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();
140                 }
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;
145                 ++atc_list_itr;
146         }
147         
148         // Search the tuned frequencies every now and then - this should be done with the event scheduler
149         static int i = 0;
150         if(i == 7) {
151                 AreaSearch();
152         }
153         if(i == 15) {
154                 //cout << "About to search(1)" << endl;
155                 FreqSearch(1);
156         }
157         if(i == 30) {
158                 //cout << "About to search(2)" << endl;
159                 FreqSearch(2);
160                 i = 0;
161         }
162         ++i;
163         
164         //cout << "comm1 type = " << comm_type[0] << '\n';
165 }
166
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) {
170         /*AirportATC a;
171         if(GetAirportATCDetails((string)id, &a)) {
172                 if(a.set_by_AI) {
173                         // Don't remove
174                         a.set_by_comm_search = false;
175                         airport_atc_map[(string)id] = a;
176                         return;
177                 } else {
178                         // remove
179                 }
180         }*/
181         
182         // Hack - need to implement this properly
183         RemoveFromList(id, tp);
184 }
185     
186
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);
204                                 break;
205                         }  // Note that that can upset where we are in the list but that doesn't really matter
206                 ++atc_list_itr;
207         }
208 }
209
210
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
221                 ++atc_list_itr;
222         }
223         // If we get here it's not in the list
224         return(NULL);
225 }
226
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];
231                 return(true);
232         } else {
233                 return(false);
234         }
235 }
236
237
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';
247         switch(type) {
248                 case TOWER:
249                 if(a->tower_active) {
250                         // Get the pointer from the list
251                         return(FindInList(icao.c_str(), type)); // DCL - this untested so far.
252                 } else {
253                         FGTower* t = new FGTower;
254                         ATCData data;
255                         if(current_commlist->FindByFreq(a->lon, a->lat, a->elev, a->tower_freq, &data, TOWER)) {
256                                 t->SetData(&data);
257                                 atc_list.push_back(t);
258                                 a->tower_active = true;
259                                 airport_atc_map[icao] = a;
260                                 return(t);
261                         } else {
262                                 cout << "ERROR - tower that should exist in FGATCMgr::GetATCPointer for airport " << icao << " not found\n";
263                         }
264                 }
265                 break;
266                 // Lets add the rest to get rid of the compiler warnings even though we don't need them yet.
267                 case APPROACH:
268                 break;
269                 case ATIS:
270                 SG_LOG(SG_GENERAL, SG_ALERT, "ERROR - ATIS station should not be requested from FGATCMgr::GetATCPointer");
271                 break;
272                 case GROUND:
273                 break;
274                 case INVALID:
275                 break;
276                 case ENROUTE:
277                 break;
278                 case DEPARTURE:
279                 break;
280         }
281         
282         SG_LOG(SG_GENERAL, SG_ALERT, "ERROR IN FGATCMgr - reached end of GetATCPointer");
283         
284         return(NULL);
285 }
286
287
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"));
296         if(voice) {
297                 int len;
298                 unsigned char* buf = v1.WriteMessage((char*)msg.c_str(), len, voice);
299                 if(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);
305                         if(repeating) {
306                                 globals->get_soundmgr()->play_looped(refname);
307                         } else {
308                                 globals->get_soundmgr()->play_once(refname);
309                         }
310                 }
311                 delete[] buf;
312         }
313 #endif  // ENABLE_AUDIO_SUPPORT
314         if(!voice) {
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) == "/")) {
318                                 msg[i] = ' ';
319                         }
320                 }
321                 globals->get_ATC_display()->RegisterRepeatingMessage(msg);
322         }
323         playing = true; 
324 }
325
326
327 // Cease rendering a transmission.
328 void FGATCMgr::NoRender(string refname) {
329         if(playing) {
330                 if(voice) {
331 #ifdef ENABLE_AUDIO_SUPPORT             
332                         globals->get_soundmgr()->stop(refname);
333                         globals->get_soundmgr()->remove(refname);
334 #endif
335                 } else {
336                         globals->get_ATC_display()->CancelRepeatingMessage();
337                 }
338                 playing = false;
339         }
340 }
341
342
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
346 }
347
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
351
352         ATCData data;   
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;
357         
358         // Query the data store and get the closest match if any
359         if(current_commlist->FindByFreq(lon, lat, elev, freq, &data)) {
360                 // We have a match
361                 // What's the logic?
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
368                                 return;
369                         } else {
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);
373                         }
374                 }
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;
385                 
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;
389                         a->SetData(&data);
390                         comm_atc_ptr[chan] = a;
391                         a->SetDisplay();
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);
396                         if(app != NULL) {
397                                 // The station is already in the ATC list
398                                 app->SetDisplay();
399                         } else {
400                                 // Generate the station and put in the ATC list
401                                 FGTower* t = new FGTower;
402                                 t->SetData(&data);
403                                 comm_atc_ptr[chan] = t;
404                                 t->SetDisplay();
405                                 atc_list.push_back(t);
406                         }
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);
410                         if(app != NULL) {
411                                 // The station is already in the ATC list
412                                 app->AddPlane("Player");
413                                 app->SetDisplay();
414                                 comm_atc_ptr[chan] = app;
415                         } else {
416                                 // Generate the station and put in the ATC list
417                                 FGApproach* a = new FGApproach;
418                                 a->SetData(&data);
419                                 a->AddPlane("Player");
420                                 atc_list.push_back(a);
421                                 comm_atc_ptr[chan] = a;
422                         }                       
423                 }
424         } else {
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);
429                         }
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;
435                 }
436         }
437 }
438
439
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;
445         
446         lon = lon_node->getDoubleValue();
447         lat = lat_node->getDoubleValue();
448         elev = elev_node->getDoubleValue() * SG_FEET_TO_METER;
449         
450         // search stations in range
451         int num_app = current_commlist->FindByPos(lon, lat, elev, &approaches, APPROACH);
452         if (num_app != 0) {
453                 //cout << num_app << " approaches found in radiostack search !!!!" << endl;
454                 
455                 for(app_itr = approaches.begin(); app_itr != approaches.end(); app_itr++) {
456                         
457                         FGATC* app = FindInList((app_itr->ident).c_str(), app_itr->type);
458                         if(app != NULL) {
459                                 // The station is already in the ATC list
460                                 app->AddPlane("Player");
461                                 //app->Update();
462                         } else {
463                                 // Generate the station and put in the ATC list
464                                 FGApproach* a = new FGApproach;
465                                 a->SetData(&(*app_itr));
466                                 a->AddPlane("Player");
467                                 //a->Update();
468                                 atc_list.push_back(a);
469                         }
470                 }
471         }
472         
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
480                         if ( np == 0) {
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
486                         }
487                 }
488                 ++atc_list_itr;
489         }
490 }