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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
25 #include <simgear/misc/sg_path.hxx>
26 #include <simgear/debug/logstream.hxx>
28 #include <Airports/simple.hxx>
31 #include "commlist.hxx"
32 #include "ATCDialog.hxx"
33 #include "ATCutils.hxx"
34 #include "transmissionlist.hxx"
39 // periodic radio station search wrapper
40 static void fgATCSearch( void ) {
41 globals->get_ATC_mgr()->Search();
43 */ //This wouldn't compile - including Time/event.hxx breaks it :-(
44 // Is this still true?? -EMH-
46 AirportATC::AirportATC() :
55 //airport_atc_map.clear();
59 FGATCMgr::FGATCMgr() :
61 atc_list(new atc_list_type),
66 FGATCMgr::~FGATCMgr() {
70 void FGATCMgr::bind() {
73 void FGATCMgr::unbind() {
76 void FGATCMgr::init() {
77 //cout << "ATCMgr::init called..." << endl;
79 lon_node = fgGetNode("/position/longitude-deg", true);
80 lat_node = fgGetNode("/position/latitude-deg", true);
81 elev_node = fgGetNode("/position/altitude-ft", true);
82 atc_list_itr = atc_list->begin();
84 // Search for connected ATC stations once per 0.8 seconds or so
85 // globals->get_event_mgr()->add( "fgATCSearch()", fgATCSearch,
86 // FGEvent::FG_EVENT_READY, 800);
88 // For some reason the above doesn't compile - including Time/event.hxx stops compilation.
89 // Is this still true after the reorganization of the event managar??
92 #ifdef ENABLE_AUDIO_SUPPORT
93 // Load all available voices.
94 // For now we'll do one hardwired one
98 voiceOK = v1->LoadVoice("default");
100 } catch ( sg_io_exception & ) {
107 /* I've loaded the voice even if /sim/sound/pause is true
108 * since I know no way of forcing load of the voice if the user
109 * subsequently switches /sim/sound/audible to true.
110 * (which is the right thing to do -- CLO) :-) */
115 // Initialise the ATC Dialogs
116 //cout << "Initing Transmissions..." << endl;
117 SG_LOG(SG_ATC, SG_INFO, " ATC Transmissions");
118 current_transmissionlist = new FGTransmissionList;
119 SGPath p_transmission( globals->get_fg_root() );
120 p_transmission.append( "ATC/default.transmissions" );
121 current_transmissionlist->init( p_transmission );
122 //cout << "Done Transmissions" << endl;
124 SG_LOG(SG_ATC, SG_INFO, " ATC Dialog System");
125 current_atcdialog = new FGATCDialog;
126 current_atcdialog->Init();
129 //cout << "ATCmgr::init done!" << endl;
132 void FGATCMgr::update(double dt) {
135 SG_LOG(SG_ATC, SG_WARN, "Warning - ATCMgr::update(...) called before ATCMgr::init()");
138 current_atcdialog->Update(dt);
140 //cout << "Entering update..." << endl;
141 //Traverse the list of active stations.
142 //Only update one class per update step to avoid the whole ATC system having to calculate between frames.
143 //Eventually we should only update every so many steps.
144 //cout << "In FGATCMgr::update - atc_list.size = " << atc_list->size() << endl;
145 if(atc_list->size()) {
146 if(atc_list_itr == atc_list->end()) {
147 atc_list_itr = atc_list->begin();
149 //cout << "Updating " << (*atc_list_itr)->get_ident() << ' ' << (*atc_list_itr)->GetType() << '\n';
150 //cout << "Freq = " << (*atc_list_itr)->get_freq() << '\n';
151 (*atc_list_itr).second->Update(dt * atc_list->size());
152 //cout << "Done ATC update..." << endl;
157 cout << "ATC_LIST: " << atc_list->size() << ' ';
158 for(atc_list_iterator it = atc_list->begin(); it != atc_list->end(); it++) {
159 cout << (*it)->get_ident() << ' ';
164 // Search the tuned frequencies every now and then - this should be done with the event scheduler
165 static int i = 0; // Very ugly - but there should only ever be one instance of FGATCMgr.
166 /*** Area search is defeated. Why?
168 //cout << "About to AreaSearch()" << endl;
173 //cout << "About to search navcomm1" << endl;
174 FreqSearch("comm", 0);
175 FreqSearch("nav", 0);
178 //cout << "About to search navcomm2" << endl;
179 FreqSearch("comm", 1);
180 FreqSearch("nav", 1);
185 //cout << "comm1 type = " << comm_type[0] << '\n';
186 //cout << "Leaving update..." << endl;
190 // Returns frequency in KHz - should I alter this to return in MHz?
191 unsigned short int FGATCMgr::GetFrequency(const string& ident, const atc_type& tp) {
193 bool ok = current_commlist->FindByCode(ident, test, tp);
194 return(ok ? test.freq : 0);
198 // Register the fact that the AI system wants to activate an airport
199 // Might need more sophistication in this in the future - eg registration by aircraft call-sign.
200 bool FGATCMgr::AIRegisterAirport(const string& ident) {
201 SG_LOG(SG_ATC, SG_BULK, "AI registered airport " << ident << " with the ATC system");
202 //cout << "AI registered airport " << ident << " with the ATC system" << '\n';
203 if(airport_atc_map.find(ident) != airport_atc_map.end()) {
204 airport_atc_map[ident]->set_by_AI = true;
205 airport_atc_map[ident]->numAI++;
208 const FGAirport *ap = fgFindAirportID(ident);
210 //cout << "ident = " << ident << '\n';
211 AirportATC *a = new AirportATC;
212 // I'm not entirely sure that this AirportATC structure business is actually needed - it just duplicates what we can find out anyway!
213 a->geod = ap->geod();
214 a->atis_freq = GetFrequency(ident, ATIS)
215 || GetFrequency(ident, AWOS);
216 //cout << "ATIS freq = " << a->atis_freq << '\n';
217 a->atis_active = false;
218 a->tower_freq = GetFrequency(ident, TOWER);
219 //cout << "Tower freq = " << a->tower_freq << '\n';
220 a->tower_active = false;
221 a->ground_freq = GetFrequency(ident, GROUND);
222 //cout << "Ground freq = " << a->ground_freq << '\n';
223 a->ground_active = false;
224 // TODO - some airports will have a tower/ground frequency but be inactive overnight.
227 airport_atc_map[ident] = a;
230 SG_LOG(SG_ATC, SG_ALERT, "ERROR - can't find airport " << ident << " in AIRegisterAirport(...)");
236 // Register the fact that the comm radio is tuned to an airport
237 // Channel is zero based
238 bool FGATCMgr::CommRegisterAirport(const string& ident, int chan, const atc_type& tp) {
239 SG_LOG(SG_ATC, SG_BULK, "Comm channel " << chan << " registered airport " << ident);
240 //cout << "Comm channel " << chan << " registered airport " << ident << ' ' << tp << '\n';
241 if(airport_atc_map.find(ident) != airport_atc_map.end()) {
242 //cout << "IN MAP - flagging set by comm..." << endl;
243 //xx airport_atc_map[ident]->set_by_comm[chan][tp] = true;
244 if(tp == ATIS || tp == AWOS) {
245 airport_atc_map[ident]->atis_active = true;
246 } else if(tp == TOWER) {
247 airport_atc_map[ident]->tower_active = true;
248 } else if(tp == GROUND) {
249 airport_atc_map[ident]->ground_active = true;
250 } else if(tp == APPROACH) {
251 //a->approach_active = true;
252 } // TODO - there *must* be a better way to do this!!!
255 //cout << "NOT IN MAP - creating new..." << endl;
256 const FGAirport *ap = fgFindAirportID(ident);
258 AirportATC *a = new AirportATC;
259 // I'm not entirely sure that this AirportATC structure business is actually needed - it just duplicates what we can find out anyway!
260 a->geod = ap->geod();
261 a->atis_freq = GetFrequency(ident, ATIS)
262 || GetFrequency(ident, AWOS);
263 a->atis_active = false;
264 a->tower_freq = GetFrequency(ident, TOWER);
265 a->tower_active = false;
266 a->ground_freq = GetFrequency(ident, GROUND);
267 a->ground_active = false;
268 if(tp == ATIS || tp == AWOS) {
269 a->atis_active = true;
270 } else if(tp == TOWER) {
271 a->tower_active = true;
272 } else if(tp == GROUND) {
273 a->ground_active = true;
274 } else if(tp == APPROACH) {
275 //a->approach_active = true;
276 } // TODO - there *must* be a better way to do this!!!
277 // TODO - some airports will have a tower/ground frequency but be inactive overnight.
278 a->set_by_AI = false;
280 //xx a->set_by_comm[chan][tp] = true;
281 airport_atc_map[ident] = a;
288 typedef map<string,int> MSI;
290 void FGATCMgr::ZapOtherService(const string ncunit, const string svc_name){
291 for (atc_list_iterator svc = atc_list->begin(); svc != atc_list->end(); svc++) {
292 //cout << "Zapping " << navcomm
293 // << "[" << unit << "]" << " otherthan: " << svc_name << endl;
294 if (svc->first != svc_name) {
295 MSI &actv = svc->second->active_on;
296 // OK, we have found some OTHER service;
297 // see if it is (was) active on our unit:
298 if (!actv.count(ncunit)) continue;
299 // cout << "Eradicating '" << svc->first << "' from: " << ncunit << endl;
302 //cout << "Eradicating service: '" << svc->first << "'" << endl;
303 svc->second->SetNoDisplay();
304 svc->second->Update(0); // one last update
306 atc_list->erase(svc);
307 // ALL pointers into the ATC list are now invalid,
308 // so let's reset them:
309 atc_list_itr = atc_list->begin();
311 break; // cannot be duplicates in the active list
317 // Find in list - return a currently active ATC pointer given ICAO code and type
318 // Return NULL if the given service is not in the list
319 // - *** THE CALLING FUNCTION MUST CHECK FOR THIS ***
320 FGATC* FGATCMgr::FindInList(const string& id, const atc_type& tp) {
321 string ndx = id + decimalNumeral(tp);
322 if (!atc_list->count(ndx)) return 0;
323 return (*atc_list)[ndx];
326 // Returns true if the airport is found in the map
327 bool FGATCMgr::GetAirportATCDetails(const string& icao, AirportATC* a) {
328 if(airport_atc_map.find(icao) != airport_atc_map.end()) {
329 *a = *airport_atc_map[icao];
337 // Return a pointer to a given sort of ATC at a given airport and activate if necessary
338 // Returns NULL if service doesn't exist - calling function should check for this.
339 // We really ought to make this private and call it from the CommRegisterAirport / AIRegisterAirport functions
340 // - at the moment all these GetATC... functions exposed are just too complicated.
341 FGATC* FGATCMgr::GetATCPointer(const string& icao, const atc_type& type) {
342 if(airport_atc_map.find(icao) == airport_atc_map.end()) {
343 //cout << "Unable to find " << icao << ' ' << type << " in the airport_atc_map" << endl;
346 //cout << "In GetATCPointer, found " << icao << ' ' << type << endl;
347 AirportATC *a = airport_atc_map[icao];
348 //cout << "a->lon = " << a->lon << '\n';
349 //cout << "a->elev = " << a->elev << '\n';
350 //cout << "a->tower_freq = " << a->tower_freq << '\n';
353 if(a->tower_active) {
354 // Get the pointer from the list
355 return(FindInList(icao, type));
358 if(current_commlist->FindByFreq(a->geod, a->tower_freq, &data, TOWER)) {
359 FGTower* t = new FGTower;
361 (*atc_list)[icao+decimalNumeral(type)] = t;
362 a->tower_active = true;
363 airport_atc_map[icao] = a;
364 //cout << "Initing tower " << icao << " in GetATCPointer()\n";
368 SG_LOG(SG_ATC, SG_ALERT, "ERROR - tower that should exist in FGATCMgr::GetATCPointer for airport " << icao << " not found");
374 case ATIS: case AWOS:
375 SG_LOG(SG_ATC, SG_ALERT, "ERROR - ATIS station should not be requested from FGATCMgr::GetATCPointer");
378 //cout << "IN CASE GROUND" << endl;
379 if(a->ground_active) {
380 // Get the pointer from the list
381 return(FindInList(icao, type));
384 if(current_commlist->FindByFreq(a->geod, a->ground_freq, &data, GROUND)) {
385 FGGround* g = new FGGround;
387 (*atc_list)[icao+decimalNumeral(type)] = g;
388 a->ground_active = true;
389 airport_atc_map[icao] = a;
393 SG_LOG(SG_ATC, SG_ALERT, "ERROR - ground control that should exist in FGATCMgr::GetATCPointer for airport " << icao << " not found");
405 SG_LOG(SG_ATC, SG_ALERT, "ERROR IN FGATCMgr - reached end of GetATCPointer");
406 //cout << "ERROR IN FGATCMgr - reached end of GetATCPointer" << endl;
411 // Return a pointer to an appropriate voice for a given type of ATC
412 // creating the voice if necessary - ie. make sure exactly one copy
413 // of every voice in use exists in memory.
415 // TODO - in the future this will get more complex and dole out country/airport
416 // specific voices, and possible make sure that the same voice doesn't get used
417 // at different airports in quick succession if a large enough selection are available.
418 FGATCVoice* FGATCMgr::GetVoicePointer(const atc_type& type) {
419 // TODO - implement me better - maintain a list of loaded voices and other voices!!
422 case ATIS: case AWOS:
441 // Search for ATC stations by frequency
442 void FGATCMgr::FreqSearch(const string navcomm, const int unit) {
445 string ncunit = navcomm + "[" + decimalNumeral(unit) + "]";
446 string commbase = "/instrumentation/" + ncunit;
447 string commfreq = commbase + "/frequencies/selected-mhz";
448 SGPropertyNode_ptr comm_node = fgGetNode(commfreq.c_str(), false);
450 //cout << "FreqSearch: " << ncunit
451 // << " node: " << comm_node << endl;
452 if (!comm_node) return; // no such radio unit
455 double freq = comm_node->getDoubleValue();
456 _aircraftPos = SGGeod::fromDegFt(lon_node->getDoubleValue(),
457 lat_node->getDoubleValue(), elev_node->getDoubleValue());
459 // Query the data store and get the closest match if any
460 //cout << "Will FindByFreq: " << lat << " " << lon << " " << elev
461 // << " freq: " << freq << endl;
462 if(current_commlist->FindByFreq(_aircraftPos, freq, &data)) {
463 //cout << "FoundByFreq: " << freq
464 // << " ident: " << data.ident
465 // << " type: " << data.type << " ***" << endl;
466 // We are in range of something.
469 // Get rid of any *other* service that was on this radio unit:
470 string svc_name = data.ident+decimalNumeral(data.type);
471 ZapOtherService(ncunit, svc_name);
472 // See if the service already exists, possibly connected to
473 // some other radio unit:
474 if (atc_list->count(svc_name)) {
475 // make sure the service knows it's tuned on this radio:
476 FGATC* svc = (*atc_list)[svc_name];
477 svc->active_on[ncunit] = 1;
479 if (data.type == APPROACH) svc->AddPlane("Player");
483 CommRegisterAirport(data.ident, unit, data.type);
485 // This was a switch-case statement but the compiler didn't like
486 // the new variable creation with it.
487 if (data.type == ATIS
488 || data.type == AWOS) (*atc_list)[svc_name] = new FGATIS;
489 else if (data.type == TOWER) (*atc_list)[svc_name] = new FGTower;
490 else if (data.type == GROUND) (*atc_list)[svc_name] = new FGGround;
491 else if (data.type == APPROACH) (*atc_list)[svc_name] = new FGApproach;
492 FGATC* svc = (*atc_list)[svc_name];
494 svc->active_on[ncunit] = 1;
497 if (data.type == APPROACH) svc->AddPlane("Player");
499 // No services in range. Zap any service on this unit.
500 ZapOtherService(ncunit, "x x x");
505 /* I don't think AreaSearch ever gets called */
506 // Search ATC stations by area in order that we appear 'on the radar'
507 void FGATCMgr::AreaSearch() {
508 const string AREA("AREA");
509 // Search for Approach stations
510 comm_list_type approaches;
511 comm_list_iterator app_itr;
513 lon = lon_node->getDoubleValue();
514 lat = lat_node->getDoubleValue();
515 elev = elev_node->getDoubleValue() * SG_FEET_TO_METER;
516 for (atc_list_iterator svc = atc_list->begin(); svc != atc_list->end(); svc++) {
517 MSI &actv = svc->second->active_on;
518 if (actv.count(AREA)) actv[AREA] = 0; // Mark all as maybe not in range
521 // search stations in range
522 int num_app = current_commlist->FindByPos(lon, lat, elev, 100.0, &approaches, APPROACH);
524 //cout << num_app << " approaches found in area search !!!!" << endl;
526 for(app_itr = approaches.begin(); app_itr != approaches.end(); app_itr++) {
527 FGATC* app = FindInList(app_itr->ident, app_itr->type);
528 string svc_name = app_itr->ident+decimalNumeral(app_itr->type);
530 // The station is already in the ATC list
531 app->AddPlane("Player");
533 // Generate the station and put in the ATC list
534 FGApproach* a = new FGApproach;
535 a->SetData(&(*app_itr));
536 a->AddPlane("Player");
537 (*atc_list)[svc_name] = a;
538 //cout << "New area service: " << svc_name << endl;
540 FGATC* svc = (*atc_list)[svc_name];
541 svc->active_on[AREA] = 1;
545 for (atc_list_iterator svc = atc_list->begin(); svc != atc_list->end(); svc++) {
546 MSI &actv = svc->second->active_on;
547 if (!actv.count(AREA)) continue;
548 if (!actv[AREA]) actv.erase(AREA);
549 if (!actv.size()) { // this service no longer active at all
550 cout << "Eradicating area service: " << svc->first << endl;
551 svc->second->SetNoDisplay();
552 svc->second->Update(0);
553 delete (svc->second);
554 atc_list->erase(svc);
555 // Reset the persistent iterator, since any erase() makes it invalid:
556 atc_list_itr = atc_list->begin();
557 // Hope we only move out of one approach-area;
558 // others will not be noticed until next update: