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>
25 #include <Airports/simple.hxx>
28 #include "commlist.hxx"
29 #include "ATCdisplay.hxx"
30 #include "ATCDialog.hxx"
31 #include "ATCutils.hxx"
34 // periodic radio station search wrapper
35 static void fgATCSearch( void ) {
36 globals->get_ATC_mgr()->Search();
38 */ //This wouldn't compile - including Time/event.hxx breaks it :-(
40 AirportATC::AirportATC() :
53 set_by_comm[0] = false;
54 set_by_comm[1] = false;
57 FGATCMgr::FGATCMgr() {
60 last_comm_ident[0] = "";
61 last_comm_ident[1] = "";
63 last_in_range = false;
64 comm_type[0] = INVALID;
65 comm_type[1] = INVALID;
66 comm_atc_ptr[0] = NULL;
67 comm_atc_ptr[1] = NULL;
68 comm_valid[0] = false;
69 comm_valid[1] = false;
74 FGATCMgr::~FGATCMgr() {
78 void FGATCMgr::bind() {
81 void FGATCMgr::unbind() {
84 void FGATCMgr::init() {
85 comm_node[0] = fgGetNode("/radios/comm[0]/frequencies/selected-mhz", true);
86 comm_node[1] = fgGetNode("/radios/comm[1]/frequencies/selected-mhz", true);
87 lon_node = fgGetNode("/position/longitude-deg", true);
88 lat_node = fgGetNode("/position/latitude-deg", true);
89 elev_node = fgGetNode("/position/altitude-ft", true);
90 atc_list_itr = atc_list.begin();
92 // Search for connected ATC stations once per 0.8 seconds or so
93 // global_events.Register( "fgATCSearch()", fgATCSearch,
94 // fgEVENT::FG_EVENT_READY, 800);
95 // For some reason the above doesn't compile - including Time/event.hxx stops compilation.
97 // Initialise the frequency search map
98 current_commlist = new FGCommList;
99 SGPath p_comm( globals->get_fg_root() );
100 current_commlist->init( p_comm );
102 #ifdef ENABLE_AUDIO_SUPPORT
103 // Load all available voices.
104 // For now we'll do one hardwired one
107 voiceOK = v1->LoadVoice("default");
110 /* I've loaded the voice even if /sim/sound/audible is false
111 * since I know no way of forcing load of the voice if the user
112 * subsequently switches /sim/sound/audible to true. */
117 // Initialise the ATC Dialogs
118 //cout << "Initing Transmissions..." << endl;
119 SG_LOG(SG_ATC, SG_INFO, " ATC Transmissions");
120 current_transmissionlist = new FGTransmissionList;
121 SGPath p_transmission( globals->get_fg_root() );
122 p_transmission.append( "ATC/default.transmissions" );
123 current_transmissionlist->init( p_transmission );
124 //cout << "Done Transmissions" << endl;
126 SG_LOG(SG_ATC, SG_INFO, " ATC Dialog System");
127 current_atcdialog = new FGATCDialog;
128 current_atcdialog->Init();
133 //current_atcdialog->add_entry( "EGNX", "Request vectoring for approach", "Request Vectors" );
134 //current_atcdialog->add_entry( "EGNX", "Mayday, Mayday", "Declare Emergency" );
139 void FGATCMgr::update(double dt) {
142 SG_LOG(SG_ATC, SG_WARN, "Warning - ATCMgr::update(...) called before ATCMgr::init()");
145 //cout << "Entering update..." << endl;
146 //Traverse the list of active stations.
147 //Only update one class per update step to avoid the whole ATC system having to calculate between frames.
148 //Eventually we should only update every so many steps.
149 //cout << "In FGATCMgr::update - atc_list.size = " << atc_list.size() << '\n';
150 if(atc_list.size()) {
151 if(atc_list_itr == atc_list.end()) {
152 atc_list_itr = atc_list.begin();
154 //cout << "Updating " << (*atc_list_itr)->get_ident() << ' ' << (*atc_list_itr)->GetType() << '\n';
155 //cout << "Freq = " << (*atc_list_itr)->get_freq() << '\n';
156 (*atc_list_itr)->Update(dt * atc_list.size());
157 //cout << "Done ATC update..." << endl;
161 // Search the tuned frequencies every now and then - this should be done with the event scheduler
164 //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) {
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;
235 airport_atc_map[ident]->set_by_comm[0] = true;
236 } else if(chan == 1) {
237 airport_atc_map[ident]->set_by_comm[1] = true;
239 SG_LOG(SG_ATC, SG_ALERT, "COMM CHANNEL NOT 0 OR 1 IN FGATCMGR!");
240 // Just register both and accept the fact that this ATC will probably never get removed!
241 airport_atc_map[ident]->set_by_comm[0] = true;
242 airport_atc_map[ident]->set_by_comm[1] = true;
246 //cout << "NOT IN MAP - creating new..." << endl;
248 if(dclFindAirportID(ident, &ap)) {
249 AirportATC *a = new AirportATC;
250 // I'm not entirely sure that this AirportATC structure business is actually needed - it just duplicates what we can find out anyway!
251 a->lon = ap.longitude;
252 a->lat = ap.latitude;
253 a->elev = ap.elevation;
254 a->atis_freq = GetFrequency(ident, ATIS);
255 a->atis_active = false;
256 a->tower_freq = GetFrequency(ident, TOWER);
257 a->tower_active = false;
258 a->ground_freq = GetFrequency(ident, GROUND);
259 a->ground_active = false;
260 // TODO - some airports will have a tower/ground frequency but be inactive overnight.
261 a->set_by_AI = false;
264 a->set_by_comm[0] = true;
265 } else if(chan == 1) {
266 a->set_by_comm[1] = true;
268 SG_LOG(SG_ATC, SG_ALERT, "COMM CHANNEL NOT 1 OR 2 IN FGATCMGR!");
269 // Just register both and accept the fact that this ATC will probably never get removed!
270 a->set_by_comm[0] = true;
271 a->set_by_comm[1] = true;
273 airport_atc_map[ident] = a;
281 // Remove from list only if not needed by the AI system or the other comm channel
282 // Note that chan is zero based.
283 void FGATCMgr::CommRemoveFromList(const char* id, atc_type tp, int chan) {
284 SG_LOG(SG_ATC, SG_BULK, "CommRemoveFromList called for airport " << id << " by channel " << chan);
285 if(airport_atc_map.find((string)id) != airport_atc_map.end()) {
286 AirportATC* a = airport_atc_map[(string)id];
287 //cout << "In CommRemoveFromList, a->ground_freq = " << a->ground_freq << endl;
290 FGATC* aptr = GetATCPointer((string)id, tp);
293 //cout << "chan 1\n";
294 a->set_by_comm[0] = false;
295 if(!a->set_by_comm[1]) {
296 //cout << "not set by comm2\n";
298 //cout << "Got pointer\n";
299 aptr->SetNoDisplay();
300 //cout << "Setting no display...\n";
302 //cout << "Not got pointer\n";
307 a->set_by_comm[1] = false;
308 if(!a->set_by_comm[0]) {
310 aptr->SetNoDisplay();
311 //cout << "Setting no display...\n";
316 airport_atc_map[(string)id] = a;
321 a->set_by_comm[0] = false;
322 // Remove only if not also set by the other comm channel
323 if(!a->set_by_comm[1]) {
324 RemoveFromList(id, tp);
328 a->set_by_comm[1] = false;
329 if(!a->set_by_comm[0]) {
330 RemoveFromList(id, tp);
339 // Remove from list - should only be called from above or similar
340 // This function *will* remove it from the list regardless of who else might want it.
341 void FGATCMgr::RemoveFromList(const char* id, atc_type tp) {
342 //cout << "Requested type = " << tp << '\n';
343 //cout << "id = " << id << '\n';
344 atc_list_itr = atc_list.begin();
345 while(atc_list_itr != atc_list.end()) {
346 //cout << "type = " << (*atc_list_itr)->GetType() << '\n';
347 //cout << "Ident = " << (*atc_list_itr)->get_ident() << '\n';
348 if( (!strcmp((*atc_list_itr)->get_ident(), id))
349 && ((*atc_list_itr)->GetType() == tp) ) {
350 //Before removing it stop it transmitting!!
351 //cout << "OBLITERATING FROM LIST!!!\n";
352 (*atc_list_itr)->SetNoDisplay();
353 (*atc_list_itr)->Update(0.00833);
354 delete (*atc_list_itr);
355 atc_list_itr = atc_list.erase(atc_list_itr);
357 } // Note that that can upset where we are in the list but that doesn't really matter
363 // Find in list - return a currently active ATC pointer given ICAO code and type
364 // Return NULL if the given service is not in the list
365 // - *** THE CALLING FUNCTION MUST CHECK FOR THIS ***
366 FGATC* FGATCMgr::FindInList(const char* id, atc_type tp) {
367 atc_list_itr = atc_list.begin();
368 while(atc_list_itr != atc_list.end()) {
369 if( (!strcmp((*atc_list_itr)->get_ident(), id))
370 && ((*atc_list_itr)->GetType() == tp) ) {
371 return(*atc_list_itr);
372 } // Note that that can upset where we are in the list but that shouldn't really matter
375 // If we get here it's not in the list
379 // Returns true if the airport is found in the map
380 bool FGATCMgr::GetAirportATCDetails(string icao, AirportATC* a) {
381 if(airport_atc_map.find(icao) != airport_atc_map.end()) {
382 *a = *airport_atc_map[icao];
390 // Return a pointer to a given sort of ATC at a given airport and activate if necessary
391 // Returns NULL if service doesn't exist - calling function should check for this.
392 // We really ought to make this private and call it from the CommRegisterAirport / AIRegisterAirport functions
393 // - at the moment all these GetATC... functions exposed are just too complicated.
394 FGATC* FGATCMgr::GetATCPointer(string icao, atc_type type) {
395 if(airport_atc_map.find(icao) == airport_atc_map.end()) {
398 AirportATC *a = airport_atc_map[icao];
399 //cout << "a->lon = " << a->lon << '\n';
400 //cout << "a->elev = " << a->elev << '\n';
401 //cout << "a->tower_freq = " << a->tower_freq << '\n';
404 if(a->tower_active) {
405 // Get the pointer from the list
406 return(FindInList(icao.c_str(), type));
409 if(current_commlist->FindByFreq(a->lon, a->lat, a->elev, a->tower_freq, &data, TOWER)) {
410 FGTower* t = new FGTower;
412 atc_list.push_back(t);
413 a->tower_active = true;
414 airport_atc_map[icao] = a;
418 SG_LOG(SG_ATC, SG_ALERT, "ERROR - tower that should exist in FGATCMgr::GetATCPointer for airport " << icao << " not found");
425 SG_LOG(SG_ATC, SG_ALERT, "ERROR - ATIS station should not be requested from FGATCMgr::GetATCPointer");
428 //cout << "IN CASE GROUND" << endl;
429 if(a->ground_active) {
430 // Get the pointer from the list
431 return(FindInList(icao.c_str(), type));
434 if(current_commlist->FindByFreq(a->lon, a->lat, a->elev, a->ground_freq, &data, GROUND)) {
435 FGGround* g = new FGGround;
437 atc_list.push_back(g);
438 a->ground_active = true;
439 airport_atc_map[icao] = a;
443 SG_LOG(SG_ATC, SG_ALERT, "ERROR - ground control that should exist in FGATCMgr::GetATCPointer for airport " << icao << " not found");
455 SG_LOG(SG_ATC, SG_ALERT, "ERROR IN FGATCMgr - reached end of GetATCPointer");
460 // Return a pointer to an appropriate voice for a given type of ATC
461 // creating the voice if necessary - ie. make sure exactly one copy
462 // of every voice in use exists in memory.
464 // TODO - in the future this will get more complex and dole out country/airport
465 // specific voices, and possible make sure that the same voice doesn't get used
466 // at different airports in quick succession if a large enough selection are available.
467 FGATCVoice* FGATCMgr::GetVoicePointer(atc_type type) {
468 // TODO - implement me better - maintain a list of loaded voices and other voices!!
490 // Display a dialog box with options relevant to the currently tuned ATC service.
491 void FGATCMgr::doPopupDialog() {
492 ATCDoDialog(comm_type[0]); // FIXME - currently hardwired to comm1
495 // Search for ATC stations by frequency
496 void FGATCMgr::FreqSearch(int channel) {
497 int chan = channel - 1; // Convert to zero-based for the arrays
500 double freq = comm_node[chan]->getDoubleValue();
501 lon = lon_node->getDoubleValue();
502 lat = lat_node->getDoubleValue();
503 elev = elev_node->getDoubleValue() * SG_FEET_TO_METER;
505 // Query the data store and get the closest match if any
506 if(current_commlist->FindByFreq(lon, lat, elev, freq, &data)) {
509 // If this channel not previously valid then easy - add ATC to list
510 // If this channel was valid then - Have we tuned to a different service?
511 // If so - de-register one and add the other
512 if(comm_valid[chan]) {
513 if((comm_ident[chan] == data.ident) && (comm_type[chan] == data.type)) {
514 // Then we're still tuned into the same service so do nought and return
517 // Something's changed - either the location or the service type
518 // We need to feed the channel in so we're not removing it if we're also tuned in on the other channel
519 CommRemoveFromList(comm_ident[chan], comm_type[chan], chan);
522 // At this point we can assume that we need to add the service.
523 comm_ident[chan] = (data.ident).c_str();
524 comm_type[chan] = data.type;
525 comm_x[chan] = (double)data.x;
526 comm_y[chan] = (double)data.y;
527 comm_z[chan] = (double)data.z;
528 comm_lon[chan] = (double)data.lon;
529 comm_lat[chan] = (double)data.lat;
530 comm_elev[chan] = (double)data.elev;
531 comm_valid[chan] = true;
533 // This was a switch-case statement but the compiler didn't like the new variable creation with it.
534 if(comm_type[chan] == ATIS) {
535 CommRegisterAirport(comm_ident[chan], chan);
536 FGATC* app = FindInList(comm_ident[chan], TOWER);
538 // The station is already in the ATC list
539 //cout << "In list - flagging SetDisplay..." << endl;
542 // Generate the station and put in the ATC list
543 //cout << "Not in list - generating..." << endl;
544 FGATIS* a = new FGATIS;
546 comm_atc_ptr[chan] = a;
549 atc_list.push_back(a);
551 } else if (comm_type[chan] == TOWER) {
552 CommRegisterAirport(comm_ident[chan], chan);
553 //cout << "Done (TOWER)" << endl;
554 FGATC* app = FindInList(comm_ident[chan], TOWER);
556 // The station is already in the ATC list
557 //cout << "In list - flagging SetDisplay..." << endl;
560 // Generate the station and put in the ATC list
561 //cout << "Not in list - generating..." << endl;
562 FGTower* t = new FGTower;
564 comm_atc_ptr[chan] = t;
567 atc_list.push_back(t);
569 } else if (comm_type[chan] == GROUND) {
570 CommRegisterAirport(comm_ident[chan], chan);
571 //cout << "Done (GROUND)" << endl;
572 FGATC* app = FindInList(comm_ident[chan], GROUND);
574 // The station is already in the ATC list
577 // Generate the station and put in the ATC list
578 FGGround* g = new FGGround;
580 comm_atc_ptr[chan] = g;
583 atc_list.push_back(g);
585 } else if (comm_type[chan] == APPROACH) {
586 // We have to be a bit more carefull here since approaches are also searched by area
587 CommRegisterAirport(comm_ident[chan], chan);
588 //cout << "Done (APPROACH)" << endl;
589 FGATC* app = FindInList(comm_ident[chan], APPROACH);
591 // The station is already in the ATC list
592 app->AddPlane("Player");
594 comm_atc_ptr[chan] = app;
596 // Generate the station and put in the ATC list
597 FGApproach* a = new FGApproach;
599 a->AddPlane("Player");
600 atc_list.push_back(a);
601 comm_atc_ptr[chan] = a;
605 if(comm_valid[chan]) {
606 if(comm_type[chan] != APPROACH) {
607 // Currently approaches are removed by Alexander's out-of-range mechanism
608 CommRemoveFromList(comm_ident[chan], comm_type[chan], chan);
610 // Note that we *don't* call SetNoDisplay() here because the other comm channel
611 // might be tuned into the same station - this is handled by CommRemoveFromList(...)
612 comm_type[chan] = INVALID;
613 comm_atc_ptr[chan] = NULL;
614 comm_valid[chan] = false;
620 // Search ATC stations by area in order that we appear 'on the radar'
621 void FGATCMgr::AreaSearch() {
622 // Search for Approach stations
623 comm_list_type approaches;
624 comm_list_iterator app_itr;
626 lon = lon_node->getDoubleValue();
627 lat = lat_node->getDoubleValue();
628 elev = elev_node->getDoubleValue() * SG_FEET_TO_METER;
630 // search stations in range
631 int num_app = current_commlist->FindByPos(lon, lat, elev, 100.0, &approaches, APPROACH);
633 //cout << num_app << " approaches found in radiostack search !!!!" << endl;
635 for(app_itr = approaches.begin(); app_itr != approaches.end(); app_itr++) {
637 FGATC* app = FindInList((app_itr->ident).c_str(), app_itr->type);
639 // The station is already in the ATC list
640 app->AddPlane("Player");
643 // Generate the station and put in the ATC list
644 FGApproach* a = new FGApproach;
645 a->SetData(&(*app_itr));
646 a->AddPlane("Player");
648 atc_list.push_back(a);
653 // remove planes which are out of range
654 // TODO - I'm not entirely sure that this belongs here.
655 atc_list_itr = atc_list.begin();
656 while(atc_list_itr != atc_list.end()) {
657 if((*atc_list_itr)->GetType() == APPROACH ) {
658 int np = (*atc_list_itr)->RemovePlane();
659 // if approach has no planes left remove it from ATC list
661 (*atc_list_itr)->SetNoDisplay();
662 (*atc_list_itr)->Update(0.00833);
663 delete (*atc_list_itr);
664 atc_list_itr = atc_list.erase(atc_list_itr);
665 break; // the other stations will be checked next time