]> git.mxchange.org Git - flightgear.git/blob - src/ATC/ATCmgr.cxx
Added constructor to ATCData struct
[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 AirportATC::AirportATC() :
43     lon(0.0),
44     lat(0.0),
45     elev(0.0),
46     atis_freq(0.0),
47     atis_active(false),
48     tower_freq(0.0),
49     tower_active(false),
50     ground_freq(0.0),
51     ground_active(false),
52     set_by_AI(false),
53     set_by_comm_search(false) {
54 }
55
56 FGATCMgr::FGATCMgr() {
57         comm_ident[0] = "";
58         comm_ident[1] = "";
59         last_comm_ident[0] = "";
60         last_comm_ident[1] = "";
61         approach_ident = "";
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;
67 }
68
69 FGATCMgr::~FGATCMgr() {
70         delete v1;
71 }
72
73 void FGATCMgr::bind() {
74 }
75
76 void FGATCMgr::unbind() {
77 }
78
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.
90         
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 );
95         
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;
98         a->lon = -118.034719;
99         a->lat = 34.086114;
100         a->elev = 296.0;
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;
107         
108         //a->set_by_AI = true;
109         //a->set_by_comm_search = false;
110         
111         airport_atc_map[(string)"KEMT"] = a;
112
113 #ifdef ENABLE_AUDIO_SUPPORT     
114         // Load all available voices.
115         // For now we'll do one hardwired one
116         
117         v1 = new FGATCVoice;
118         voiceOK = v1->LoadVoice("default");
119         voice = true;
120         
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. */
124 #else
125         voice = false;
126 #endif
127
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;
136
137     SG_LOG(SG_GENERAL, SG_INFO, "  ATC Dialog System");
138     current_atcdialog = new FGATCDialog;
139     current_atcdialog->Init();
140
141         ATCDialogInit();
142         
143         // DCL - testing
144         //current_atcdialog->add_entry( "EGNX", "Request vectoring for approach", "Request Vectors" );
145         //current_atcdialog->add_entry( "EGNX", "Mayday, Mayday", "Declare Emergency" );
146         /*
147         ATCData test;
148         bool ok = current_commlist->FindByCode("KEMT", test, TOWER);
149         if(ok) {
150                 cout << "KEMT tower frequency from test was " << test.freq << endl;
151         } else {
152                 cout << "KEMT tower frequency not found :-(" << endl;
153         }
154         exit(-1);
155         */
156 }
157
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();
167                 }
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;
172                 ++atc_list_itr;
173         }
174         
175         // Search the tuned frequencies every now and then - this should be done with the event scheduler
176         static int i = 0;
177         if(i == 7) {
178                 AreaSearch();
179         }
180         if(i == 15) {
181                 //cout << "About to search(1)" << endl;
182                 FreqSearch(1);
183         }
184         if(i == 30) {
185                 //cout << "About to search(2)" << endl;
186                 FreqSearch(2);
187                 i = 0;
188         }
189         ++i;
190         
191         //cout << "comm1 type = " << comm_type[0] << '\n';
192 }
193
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) {
197         /*AirportATC a;
198         if(GetAirportATCDetails((string)id, &a)) {
199                 if(a.set_by_AI) {
200                         // Don't remove
201                         a.set_by_comm_search = false;
202                         airport_atc_map[(string)id] = a;
203                         return;
204                 } else {
205                         // remove
206                 }
207         }*/
208         
209         // Hack - need to implement this properly
210         RemoveFromList(id, tp);
211 }
212     
213
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);
231                                 break;
232                         }  // Note that that can upset where we are in the list but that doesn't really matter
233                 ++atc_list_itr;
234         }
235 }
236
237
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
248                 ++atc_list_itr;
249         }
250         // If we get here it's not in the list
251         return(NULL);
252 }
253
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];
258                 return(true);
259         } else {
260                 return(false);
261         }
262 }
263
264
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';
274         switch(type) {
275                 case TOWER:
276                 if(a->tower_active) {
277                         // Get the pointer from the list
278                         return(FindInList(icao.c_str(), type)); // DCL - this untested so far.
279                 } else {
280                         FGTower* t = new FGTower;
281                         ATCData data;
282                         if(current_commlist->FindByFreq(a->lon, a->lat, a->elev, a->tower_freq, &data, TOWER)) {
283                                 t->SetData(&data);
284                                 atc_list.push_back(t);
285                                 a->tower_active = true;
286                                 airport_atc_map[icao] = a;
287                                 return(t);
288                         } else {
289                                 cout << "ERROR - tower that should exist in FGATCMgr::GetATCPointer for airport " << icao << " not found\n";
290                         }
291                 }
292                 break;
293                 // Lets add the rest to get rid of the compiler warnings even though we don't need them yet.
294                 case APPROACH:
295                 break;
296                 case ATIS:
297                 SG_LOG(SG_GENERAL, SG_ALERT, "ERROR - ATIS station should not be requested from FGATCMgr::GetATCPointer");
298                 break;
299                 case GROUND:
300                 break;
301                 case INVALID:
302                 break;
303                 case ENROUTE:
304                 break;
305                 case DEPARTURE:
306                 break;
307         }
308         
309         SG_LOG(SG_GENERAL, SG_ALERT, "ERROR IN FGATCMgr - reached end of GetATCPointer");
310         
311         return(NULL);
312 }
313
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.
317 //
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!!
323         if(voice) {
324                 switch(type) {
325                 case ATIS:
326                         if(voiceOK) {
327                                 return(v1);
328                         }
329                 case TOWER:
330                         return(NULL);
331                 case APPROACH:
332                         return(NULL);
333                 case GROUND:
334                         return(NULL);
335                 default:
336                         return(NULL);
337                 }
338                 return(NULL);
339         } else {
340                 return(NULL);
341         }
342 }
343
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
347 }
348
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
352
353         ATCData data;   
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;
358         
359         // Query the data store and get the closest match if any
360         if(current_commlist->FindByFreq(lon, lat, elev, freq, &data)) {
361                 // We have a match
362                 // What's the logic?
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
369                                 return;
370                         } else {
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);
374                         }
375                 }
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;
386                 
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;
390                         a->SetData(&data);
391                         comm_atc_ptr[chan] = a;
392                         a->SetDisplay();
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);
397                         if(app != NULL) {
398                                 // The station is already in the ATC list
399                                 app->SetDisplay();
400                         } else {
401                                 // Generate the station and put in the ATC list
402                                 FGTower* t = new FGTower;
403                                 t->SetData(&data);
404                                 comm_atc_ptr[chan] = t;
405                                 t->SetDisplay();
406                                 atc_list.push_back(t);
407                         }
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);
411                         if(app != NULL) {
412                                 // The station is already in the ATC list
413                                 app->AddPlane("Player");
414                                 app->SetDisplay();
415                                 comm_atc_ptr[chan] = app;
416                         } else {
417                                 // Generate the station and put in the ATC list
418                                 FGApproach* a = new FGApproach;
419                                 a->SetData(&data);
420                                 a->AddPlane("Player");
421                                 atc_list.push_back(a);
422                                 comm_atc_ptr[chan] = a;
423                         }                       
424                 }
425         } else {
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);
430                         }
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;
436                 }
437         }
438 }
439
440
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;
446         
447         lon = lon_node->getDoubleValue();
448         lat = lat_node->getDoubleValue();
449         elev = elev_node->getDoubleValue() * SG_FEET_TO_METER;
450         
451         // search stations in range
452         int num_app = current_commlist->FindByPos(lon, lat, elev, &approaches, APPROACH);
453         if (num_app != 0) {
454                 //cout << num_app << " approaches found in radiostack search !!!!" << endl;
455                 
456                 for(app_itr = approaches.begin(); app_itr != approaches.end(); app_itr++) {
457                         
458                         FGATC* app = FindInList((app_itr->ident).c_str(), app_itr->type);
459                         if(app != NULL) {
460                                 // The station is already in the ATC list
461                                 app->AddPlane("Player");
462                                 //app->Update();
463                         } else {
464                                 // Generate the station and put in the ATC list
465                                 FGApproach* a = new FGApproach;
466                                 a->SetData(&(*app_itr));
467                                 a->AddPlane("Player");
468                                 //a->Update();
469                                 atc_list.push_back(a);
470                         }
471                 }
472         }
473         
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
481                         if ( np == 0) {
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
487                         }
488                 }
489                 ++atc_list_itr;
490         }
491 }