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 <simgear/misc/sg_path.hxx>
22 #include <simgear/debug/logstream.hxx>
23 #include <Airports/simple.hxx>
26 #include "commlist.hxx"
27 #include "ATCdisplay.hxx"
28 #include "ATCDialog.hxx"
29 #include "ATCutils.hxx"
32 // periodic radio station search wrapper
33 static void fgATCSearch( void ) {
34 globals->get_ATC_mgr()->Search();
36 */ //This wouldn't compile - including Time/event.hxx breaks it :-(
37 // Is this still true?? -EMH-
39 AirportATC::AirportATC() :
52 for(int i=0; i<ATC_NUM_TYPES; ++i) {
53 set_by_comm[0][i] = false;
54 set_by_comm[1][i] = false;
58 FGATCMgr::FGATCMgr() {
61 last_comm_ident[0] = "";
62 last_comm_ident[1] = "";
64 last_in_range = false;
65 comm_type[0] = INVALID;
66 comm_type[1] = INVALID;
67 comm_atc_ptr[0] = NULL;
68 comm_atc_ptr[1] = NULL;
69 comm_valid[0] = false;
70 comm_valid[1] = false;
75 FGATCMgr::~FGATCMgr() {
79 void FGATCMgr::bind() {
82 void FGATCMgr::unbind() {
85 void FGATCMgr::init() {
86 comm_node[0] = fgGetNode("/radios/comm[0]/frequencies/selected-mhz", true);
87 comm_node[1] = fgGetNode("/radios/comm[1]/frequencies/selected-mhz", true);
88 lon_node = fgGetNode("/position/longitude-deg", true);
89 lat_node = fgGetNode("/position/latitude-deg", true);
90 elev_node = fgGetNode("/position/altitude-ft", true);
91 atc_list_itr = atc_list.begin();
93 // Search for connected ATC stations once per 0.8 seconds or so
94 // globals->get_event_mgr()->add( "fgATCSearch()", fgATCSearch,
95 // FGEvent::FG_EVENT_READY, 800);
97 // For some reason the above doesn't compile - including Time/event.hxx stops compilation.
98 // Is this still true after the reorganization of the event managar??
101 // Initialise the frequency search map
102 current_commlist = new FGCommList;
103 SGPath p_comm( globals->get_fg_root() );
104 current_commlist->init( p_comm );
106 #ifdef ENABLE_AUDIO_SUPPORT
107 // Load all available voices.
108 // For now we'll do one hardwired one
111 voiceOK = v1->LoadVoice("default");
114 /* I've loaded the voice even if /sim/sound/audible is false
115 * since I know no way of forcing load of the voice if the user
116 * subsequently switches /sim/sound/audible to true. */
121 // Initialise the ATC Dialogs
122 //cout << "Initing Transmissions..." << endl;
123 SG_LOG(SG_ATC, SG_INFO, " ATC Transmissions");
124 current_transmissionlist = new FGTransmissionList;
125 SGPath p_transmission( globals->get_fg_root() );
126 p_transmission.append( "ATC/default.transmissions" );
127 current_transmissionlist->init( p_transmission );
128 //cout << "Done Transmissions" << endl;
130 SG_LOG(SG_ATC, SG_INFO, " ATC Dialog System");
131 current_atcdialog = new FGATCDialog;
132 current_atcdialog->Init();
137 void FGATCMgr::update(double dt) {
140 SG_LOG(SG_ATC, SG_WARN, "Warning - ATCMgr::update(...) called before ATCMgr::init()");
143 //cout << "Entering update..." << endl;
144 //Traverse the list of active stations.
145 //Only update one class per update step to avoid the whole ATC system having to calculate between frames.
146 //Eventually we should only update every so many steps.
147 //cout << "In FGATCMgr::update - atc_list.size = " << atc_list.size() << '\n';
148 if(atc_list.size()) {
149 if(atc_list_itr == atc_list.end()) {
150 atc_list_itr = atc_list.begin();
152 //cout << "Updating " << (*atc_list_itr)->get_ident() << ' ' << (*atc_list_itr)->GetType() << '\n';
153 //cout << "Freq = " << (*atc_list_itr)->get_freq() << '\n';
154 (*atc_list_itr)->Update(dt * atc_list.size());
155 //cout << "Done ATC update..." << endl;
159 // Search the tuned frequencies every now and then - this should be done with the event scheduler
160 static int i = 0; // Very ugly - but there should only ever be one instance of FGATCMgr.
163 //cout << "About to AreaSearch()" << endl;
168 //cout << "About to search(1)" << endl;
172 //cout << "About to search(2)" << endl;
178 //cout << "comm1 type = " << comm_type[0] << '\n';
179 //cout << "Leaving update..." << endl;
183 // Returns frequency in KHz - should I alter this to return in MHz?
184 unsigned short int FGATCMgr::GetFrequency(string ident, atc_type tp) {
186 bool ok = current_commlist->FindByCode(ident, test, tp);
187 return(ok ? test.freq : 0);
191 // Register the fact that the AI system wants to activate an airport
192 // Might need more sophistication in this in the future - eg registration by aircraft call-sign.
193 bool FGATCMgr::AIRegisterAirport(string ident) {
194 SG_LOG(SG_ATC, SG_BULK, "AI registered airport " << ident << " with the ATC system");
195 if(airport_atc_map.find(ident) != airport_atc_map.end()) {
196 airport_atc_map[ident]->set_by_AI = true;
197 airport_atc_map[ident]->numAI++;
201 if(dclFindAirportID(ident, &ap)) {
202 //cout << "ident = " << ident << '\n';
203 AirportATC *a = new AirportATC;
204 // I'm not entirely sure that this AirportATC structure business is actually needed - it just duplicates what we can find out anyway!
205 a->lon = ap.longitude;
206 a->lat = ap.latitude;
207 a->elev = ap.elevation;
208 a->atis_freq = GetFrequency(ident, ATIS);
209 //cout << "ATIS freq = " << a->atis_freq << '\n';
210 a->atis_active = false;
211 a->tower_freq = GetFrequency(ident, TOWER);
212 //cout << "Tower freq = " << a->tower_freq << '\n';
213 a->tower_active = false;
214 a->ground_freq = GetFrequency(ident, GROUND);
215 //cout << "Ground freq = " << a->ground_freq << '\n';
216 a->ground_active = false;
217 // TODO - some airports will have a tower/ground frequency but be inactive overnight.
220 airport_atc_map[ident] = a;
228 // Register the fact that the comm radio is tuned to an airport
229 // Channel is zero based
230 bool FGATCMgr::CommRegisterAirport(string ident, int chan, atc_type tp) {
231 SG_LOG(SG_ATC, SG_BULK, "Comm channel " << chan << " registered airport " << ident);
232 if(airport_atc_map.find(ident) != airport_atc_map.end()) {
233 //cout << "IN MAP - flagging set by comm..." << endl;
234 airport_atc_map[ident]->set_by_comm[chan][tp] = true;
237 //cout << "NOT IN MAP - creating new..." << endl;
239 if(dclFindAirportID(ident, &ap)) {
240 AirportATC *a = new AirportATC;
241 // I'm not entirely sure that this AirportATC structure business is actually needed - it just duplicates what we can find out anyway!
242 a->lon = ap.longitude;
243 a->lat = ap.latitude;
244 a->elev = ap.elevation;
245 a->atis_freq = GetFrequency(ident, ATIS);
246 a->atis_active = false;
247 a->tower_freq = GetFrequency(ident, TOWER);
248 a->tower_active = false;
249 a->ground_freq = GetFrequency(ident, GROUND);
250 a->ground_active = false;
251 // TODO - some airports will have a tower/ground frequency but be inactive overnight.
252 a->set_by_AI = false;
254 a->set_by_comm[chan][tp] = true;
255 airport_atc_map[ident] = a;
263 // Remove from list only if not needed by the AI system or the other comm channel
264 // Note that chan is zero based.
265 void FGATCMgr::CommRemoveFromList(const char* id, atc_type tp, int chan) {
266 SG_LOG(SG_ATC, SG_BULK, "CommRemoveFromList called for airport " << id << " " << tp << " by channel " << chan);
267 if(airport_atc_map.find(id) != airport_atc_map.end()) {
268 AirportATC* a = airport_atc_map[id];
269 //cout << "In CommRemoveFromList, a->ground_freq = " << a->ground_freq << endl;
270 if(a->set_by_AI && tp != ATIS) {
271 // Set by AI, so don't remove simply because user isn't tuned in any more - just stop displaying
272 SG_LOG(SG_ATC, SG_BULK, "In CommRemoveFromList, service was set by AI\n");
273 FGATC* aptr = GetATCPointer(id, tp);
276 //cout << "chan 1\n";
277 a->set_by_comm[0][tp] = false;
278 if(!a->set_by_comm[1][tp]) {
279 //cout << "not set by comm2\n";
281 //cout << "Got pointer\n";
282 aptr->SetNoDisplay();
283 //cout << "Setting no display...\n";
285 //cout << "Not got pointer\n";
290 a->set_by_comm[1][tp] = false;
291 if(!a->set_by_comm[0][tp]) {
293 aptr->SetNoDisplay();
294 //cout << "Setting no display...\n";
299 airport_atc_map[id] = a;
304 a->set_by_comm[0][tp] = false;
305 // Remove only if not also set by the other comm channel
306 if(!a->set_by_comm[1][tp]) {
307 RemoveFromList(id, tp);
311 a->set_by_comm[1][tp] = false;
312 if(!a->set_by_comm[0][tp]) {
313 RemoveFromList(id, tp);
322 // Remove from list - should only be called from above or similar
323 // This function *will* remove it from the list regardless of who else might want it.
324 void FGATCMgr::RemoveFromList(const char* id, atc_type tp) {
325 //cout << "Requested type = " << tp << '\n';
326 //cout << "id = " << id << '\n';
327 atc_list_itr = atc_list.begin();
328 while(atc_list_itr != atc_list.end()) {
329 //cout << "type = " << (*atc_list_itr)->GetType() << '\n';
330 //cout << "Ident = " << (*atc_list_itr)->get_ident() << '\n';
331 if( (!strcmp((*atc_list_itr)->get_ident(), id))
332 && ((*atc_list_itr)->GetType() == tp) ) {
333 //Before removing it stop it transmitting!!
334 //cout << "OBLITERATING FROM LIST!!!\n";
335 (*atc_list_itr)->SetNoDisplay();
336 (*atc_list_itr)->Update(0.00833);
337 delete (*atc_list_itr);
338 atc_list_itr = atc_list.erase(atc_list_itr);
340 } // Note that that can upset where we are in the list but that doesn't really matter
346 // Find in list - return a currently active ATC pointer given ICAO code and type
347 // Return NULL if the given service is not in the list
348 // - *** THE CALLING FUNCTION MUST CHECK FOR THIS ***
349 FGATC* FGATCMgr::FindInList(const char* id, atc_type tp) {
350 atc_list_itr = atc_list.begin();
351 while(atc_list_itr != atc_list.end()) {
352 if( (!strcmp((*atc_list_itr)->get_ident(), id))
353 && ((*atc_list_itr)->GetType() == tp) ) {
354 return(*atc_list_itr);
355 } // Note that that can upset where we are in the list but that shouldn't really matter
358 // If we get here it's not in the list
362 // Returns true if the airport is found in the map
363 bool FGATCMgr::GetAirportATCDetails(string icao, AirportATC* a) {
364 if(airport_atc_map.find(icao) != airport_atc_map.end()) {
365 *a = *airport_atc_map[icao];
373 // Return a pointer to a given sort of ATC at a given airport and activate if necessary
374 // Returns NULL if service doesn't exist - calling function should check for this.
375 // We really ought to make this private and call it from the CommRegisterAirport / AIRegisterAirport functions
376 // - at the moment all these GetATC... functions exposed are just too complicated.
377 FGATC* FGATCMgr::GetATCPointer(string icao, atc_type type) {
378 if(airport_atc_map.find(icao) == airport_atc_map.end()) {
381 AirportATC *a = airport_atc_map[icao];
382 //cout << "a->lon = " << a->lon << '\n';
383 //cout << "a->elev = " << a->elev << '\n';
384 //cout << "a->tower_freq = " << a->tower_freq << '\n';
387 if(a->tower_active) {
388 // Get the pointer from the list
389 return(FindInList(icao.c_str(), type));
392 if(current_commlist->FindByFreq(a->lon, a->lat, a->elev, a->tower_freq, &data, TOWER)) {
393 FGTower* t = new FGTower;
395 atc_list.push_back(t);
396 a->tower_active = true;
397 airport_atc_map[icao] = a;
401 SG_LOG(SG_ATC, SG_ALERT, "ERROR - tower that should exist in FGATCMgr::GetATCPointer for airport " << icao << " not found");
408 SG_LOG(SG_ATC, SG_ALERT, "ERROR - ATIS station should not be requested from FGATCMgr::GetATCPointer");
411 //cout << "IN CASE GROUND" << endl;
412 if(a->ground_active) {
413 // Get the pointer from the list
414 return(FindInList(icao.c_str(), type));
417 if(current_commlist->FindByFreq(a->lon, a->lat, a->elev, a->ground_freq, &data, GROUND)) {
418 FGGround* g = new FGGround;
420 atc_list.push_back(g);
421 a->ground_active = true;
422 airport_atc_map[icao] = a;
426 SG_LOG(SG_ATC, SG_ALERT, "ERROR - ground control that should exist in FGATCMgr::GetATCPointer for airport " << icao << " not found");
438 SG_LOG(SG_ATC, SG_ALERT, "ERROR IN FGATCMgr - reached end of GetATCPointer");
443 // Return a pointer to an appropriate voice for a given type of ATC
444 // creating the voice if necessary - ie. make sure exactly one copy
445 // of every voice in use exists in memory.
447 // TODO - in the future this will get more complex and dole out country/airport
448 // specific voices, and possible make sure that the same voice doesn't get used
449 // at different airports in quick succession if a large enough selection are available.
450 FGATCVoice* FGATCMgr::GetVoicePointer(atc_type type) {
451 // TODO - implement me better - maintain a list of loaded voices and other voices!!
473 // Search for ATC stations by frequency
474 void FGATCMgr::FreqSearch(int channel) {
475 int chan = channel - 1; // Convert to zero-based for the arrays
478 double freq = comm_node[chan]->getDoubleValue();
479 lon = lon_node->getDoubleValue();
480 lat = lat_node->getDoubleValue();
481 elev = elev_node->getDoubleValue() * SG_FEET_TO_METER;
483 // Query the data store and get the closest match if any
484 if(current_commlist->FindByFreq(lon, lat, elev, freq, &data)) {
487 // If this channel not previously valid then easy - add ATC to list
488 // If this channel was valid then - Have we tuned to a different service?
489 // If so - de-register one and add the other
490 if(comm_valid[chan]) {
491 if((comm_ident[chan] == data.ident) && (comm_type[chan] == data.type)) {
492 // Then we're still tuned into the same service so do nought and return
495 // Something's changed - either the location or the service type
496 // We need to feed the channel in so we're not removing it if we're also tuned in on the other channel
497 CommRemoveFromList(comm_ident[chan], comm_type[chan], chan);
500 // At this point we can assume that we need to add the service.
501 comm_ident[chan] = (data.ident).c_str();
502 comm_type[chan] = data.type;
503 comm_x[chan] = (double)data.x;
504 comm_y[chan] = (double)data.y;
505 comm_z[chan] = (double)data.z;
506 comm_lon[chan] = (double)data.lon;
507 comm_lat[chan] = (double)data.lat;
508 comm_elev[chan] = (double)data.elev;
509 comm_valid[chan] = true;
511 // This was a switch-case statement but the compiler didn't like the new variable creation with it.
512 if(comm_type[chan] == ATIS) {
513 CommRegisterAirport(comm_ident[chan], chan, ATIS);
514 FGATC* app = FindInList(comm_ident[chan], ATIS);
516 // The station is already in the ATC list
517 //cout << "In list - flagging SetDisplay..." << endl;
520 // Generate the station and put in the ATC list
521 //cout << "Not in list - generating..." << endl;
522 FGATIS* a = new FGATIS;
524 comm_atc_ptr[chan] = a;
527 atc_list.push_back(a);
529 } else if (comm_type[chan] == TOWER) {
530 CommRegisterAirport(comm_ident[chan], chan, TOWER);
531 //cout << "Done (TOWER)" << endl;
532 FGATC* app = FindInList(comm_ident[chan], TOWER);
534 // The station is already in the ATC list
535 //cout << "In list - flagging SetDisplay..." << endl;
538 // Generate the station and put in the ATC list
539 //cout << "Not in list - generating..." << endl;
540 FGTower* t = new FGTower;
542 comm_atc_ptr[chan] = t;
545 atc_list.push_back(t);
547 } else if (comm_type[chan] == GROUND) {
548 CommRegisterAirport(comm_ident[chan], chan, GROUND);
549 //cout << "Done (GROUND)" << endl;
550 FGATC* app = FindInList(comm_ident[chan], GROUND);
552 // The station is already in the ATC list
555 // Generate the station and put in the ATC list
556 FGGround* g = new FGGround;
558 comm_atc_ptr[chan] = g;
561 atc_list.push_back(g);
563 } else if (comm_type[chan] == APPROACH) {
564 // We have to be a bit more carefull here since approaches are also searched by area
565 CommRegisterAirport(comm_ident[chan], chan, APPROACH);
566 //cout << "Done (APPROACH)" << endl;
567 FGATC* app = FindInList(comm_ident[chan], APPROACH);
569 // The station is already in the ATC list
570 app->AddPlane("Player");
572 comm_atc_ptr[chan] = app;
574 // Generate the station and put in the ATC list
575 FGApproach* a = new FGApproach;
577 comm_atc_ptr[chan] = a;
580 a->AddPlane("Player");
581 atc_list.push_back(a);
585 if(comm_valid[chan]) {
586 if(comm_type[chan] != APPROACH) {
587 // Currently approaches are removed by Alexander's out-of-range mechanism
588 CommRemoveFromList(comm_ident[chan], comm_type[chan], chan);
590 // Note that we *don't* call SetNoDisplay() here because the other comm channel
591 // might be tuned into the same station - this is handled by CommRemoveFromList(...)
592 comm_type[chan] = INVALID;
593 comm_atc_ptr[chan] = NULL;
594 comm_valid[chan] = false;
599 // Search ATC stations by area in order that we appear 'on the radar'
600 void FGATCMgr::AreaSearch() {
601 // Search for Approach stations
602 comm_list_type approaches;
603 comm_list_iterator app_itr;
605 lon = lon_node->getDoubleValue();
606 lat = lat_node->getDoubleValue();
607 elev = elev_node->getDoubleValue() * SG_FEET_TO_METER;
609 // search stations in range
610 int num_app = current_commlist->FindByPos(lon, lat, elev, 100.0, &approaches, APPROACH);
612 //cout << num_app << " approaches found in radiostack search !!!!" << endl;
614 for(app_itr = approaches.begin(); app_itr != approaches.end(); app_itr++) {
616 FGATC* app = FindInList((app_itr->ident).c_str(), app_itr->type);
618 // The station is already in the ATC list
619 //cout << "In list adding player\n";
620 app->AddPlane("Player");
623 // Generate the station and put in the ATC list
624 FGApproach* a = new FGApproach;
625 a->SetData(&(*app_itr));
626 //cout << "Adding player\n";
627 a->AddPlane("Player");
629 atc_list.push_back(a);
634 // remove planes which are out of range
635 // TODO - I'm not entirely sure that this belongs here.
636 atc_list_itr = atc_list.begin();
637 while(atc_list_itr != atc_list.end()) {
638 if((*atc_list_itr)->GetType() == APPROACH ) {
639 int np = (*atc_list_itr)->RemovePlane();
640 // if approach has no planes left remove it from ATC list
642 (*atc_list_itr)->SetNoDisplay();
643 (*atc_list_itr)->Update(0.00833);
644 delete (*atc_list_itr);
645 atc_list_itr = atc_list.erase(atc_list_itr);
646 break; // the other stations will be checked next time