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/misc/commands.hxx>
25 #include <simgear/debug/logstream.hxx>
28 #include "commlist.hxx"
29 //#include "atislist.hxx"
30 //#include "groundlist.hxx"
31 //#include "towerlist.hxx"
32 //#include "approachlist.hxx"
33 #include "ATCdisplay.hxx"
36 // periodic radio station search wrapper
37 static void fgATCSearch( void ) {
38 globals->get_ATC_mgr()->Search();
40 */ //This wouldn't compile - including Time/event.hxx breaks it :-(
42 static char* t0 = "Request landing clearance";
43 static char* t1 = "Request departure clearance";
44 static char* t2 = "Report Runway vacated";
45 static char** towerOptions = new char*[4];
46 static char* a0 = "Request vectors";
47 static char** approachOptions = new char*[2];
49 // For the ATC dialog - copied from the Autopilot new heading dialog code!
50 static puDialogBox* atcDialog;
51 static puFrame* atcDialogFrame;
52 static puText* atcDialogMessage;
53 //static puInput* atcDialogInput;
54 static puOneShot* atcDialogOkButton;
55 static puOneShot* atcDialogCancelButton;
56 static puButtonBox* atcDialogCommunicationOptions;
58 static void ATCDialogCancel(puObject *)
60 //ATCDialogInput->rejectInput();
61 FG_POP_PUI_DIALOG( atcDialog );
64 static void ATCDialogOK (puObject *me)
66 // Note that currently the dialog is hardwired to comm1 only here.
67 switch(globals->get_ATC_mgr()->GetComm1ATCType()) {
73 FGTower* twr = (FGTower*)globals->get_ATC_mgr()->GetComm1ATCPointer();
74 switch(atcDialogCommunicationOptions->getValue()) {
76 //cout << "Option 0 chosen\n";
77 twr->RequestLandingClearance("golf bravo echo");
80 //cout << "Option 1 chosen\n";
81 twr->RequestDepartureClearance("golf bravo echo");
84 //cout << "Option 2 chosen\n";
85 twr->ReportRunwayVacated("golf bravo echo");
101 //if(error) mkDialog(s.c_str());
104 static void ATCDialog(puObject *cb)
106 //ApHeadingDialogInput -> setValue ( heading );
107 //ApHeadingDialogInput -> acceptInput();
108 FG_PUSH_PUI_DIALOG(atcDialog);
111 static void ATCDialogInit()
113 char defaultATCLabel[] = "Enter desired option to communicate with ATC:";
116 // Option lists hardwired per ATC type
117 towerOptions[0] = new char[strlen(t0)+1];
118 strcpy(towerOptions[0], t0);
119 towerOptions[1] = new char[strlen(t1)+1];
120 strcpy(towerOptions[1], t1);
121 towerOptions[2] = new char[strlen(t2)+1];
122 strcpy(towerOptions[2], t2);
123 towerOptions[3] = NULL;
125 approachOptions[0] = new char[strlen(a0)+1];
126 strcpy(approachOptions[0], a0);
127 approachOptions[1] = NULL;
129 atcDialog = new puDialogBox (150, 50);
131 atcDialogFrame = new puFrame (0, 0, 500, 250);
133 atcDialogMessage = new puText (250, 220);
134 atcDialogMessage -> setDefaultValue (defaultATCLabel);
135 atcDialogMessage -> getDefaultValue (&s);
136 atcDialogMessage -> setLabel (s);
137 atcDialogMessage -> setLabelPlace (PUPLACE_TOP_CENTERED);
139 atcDialogCommunicationOptions = new puButtonBox (50, 50, 450, 210, NULL, true);
141 atcDialogOkButton = new puOneShot (50, 10, 110, 50);
142 atcDialogOkButton -> setLegend (gui_msg_OK);
143 atcDialogOkButton -> makeReturnDefault (TRUE);
144 atcDialogOkButton -> setCallback (ATCDialogOK);
146 atcDialogCancelButton = new puOneShot (140, 10, 210, 50);
147 atcDialogCancelButton -> setLegend (gui_msg_CANCEL);
148 atcDialogCancelButton -> setCallback (ATCDialogCancel);
151 FG_FINALIZE_PUI_DIALOG(atcDialog);
154 // For the command manager - maybe eventually this should go in the built in command list
155 static bool do_ATC_dialog(const SGPropertyNode* arg) {
156 globals->get_ATC_mgr()->doStandardDialog();
161 FGATCMgr::FGATCMgr() {
164 last_comm_ident[0] = "";
165 last_comm_ident[1] = "";
167 last_in_range = false;
168 comm_type[0] = INVALID;
169 comm_type[1] = INVALID;
170 comm_atc_ptr[0] = NULL;
171 comm_atc_ptr[1] = NULL;
174 FGATCMgr::~FGATCMgr() {
177 void FGATCMgr::bind() {
180 void FGATCMgr::unbind() {
183 void FGATCMgr::init() {
184 comm_node[0] = fgGetNode("/radios/comm[0]/frequencies/selected-mhz", true);
185 comm_node[1] = fgGetNode("/radios/comm[1]/frequencies/selected-mhz", true);
186 lon_node = fgGetNode("/position/longitude-deg", true);
187 lat_node = fgGetNode("/position/latitude-deg", true);
188 elev_node = fgGetNode("/position/altitude-ft", true);
189 atc_list_itr = atc_list.begin();
190 // Search for connected ATC stations once per 0.8 seconds or so
191 // global_events.Register( "fgATCSearch()", fgATCSearch,
192 // fgEVENT::FG_EVENT_READY, 800);
193 // For some reason the above doesn't compile - including Time/event.hxx stops compilation.
195 // Initialise the frequency search map
196 current_commlist = new FGCommList;
197 SGPath p_comm( globals->get_fg_root() );
198 current_commlist->init( p_comm );
200 // Initialise the airport_atc_map - we'll cheat for now and just hardcode KEMT and any others that may be needed for development
201 AirportATC *a = new AirportATC;
202 a->lon = -118.034719;
205 a->atis_freq = 118.75;
206 a->atis_active = false;
207 a->tower_freq = 121.2;
208 a->tower_active = false;
209 a->ground_freq = 125.9;
210 a->ground_active = false;
212 //a->set_by_AI = true;
213 //a->set_by_comm_search = false;
215 airport_atc_map[(string)"KEMT"] = a;
217 #ifdef ENABLE_AUDIO_SUPPORT
218 // Load all available voices.
219 // For now we'll do one hardwired one
220 voiceOK = v1.LoadVoice("default");
221 /* I've loaded the voice even if /sim/sound/audible is false
222 * since I know no way of forcing load of the voice if the user
223 * subsequently switches /sim/sound/audible to true. */
228 // Initialise the ATC Dialogs
231 // Add ATC-dialog to the command list
232 globals->get_commands()->addCommand("ATC-dialog", do_ATC_dialog);
235 void FGATCMgr::update(double dt) {
236 //cout << "Entering update..." << endl;
237 //Traverse the list of active stations.
238 //Only update one class per update step to avoid the whole ATC system having to calculate between frames.
239 //Eventually we should only update every so many steps.
240 //cout << "In FGATCMgr::update - atc_list.size = " << atc_list.size() << '\n';
241 if(atc_list.size()) {
242 if(atc_list_itr == atc_list.end()) {
243 atc_list_itr = atc_list.begin();
245 //cout << "Updating " << (*atc_list_itr)->get_ident() << ' ' << (*atc_list_itr)->GetType() << '\n';
246 //cout << "Freq = " << (*atc_list_itr)->get_freq() << '\n';
247 (*atc_list_itr)->Update();
248 //cout << "Done ATC update..." << endl;
252 // Search the tuned frequencies every now and then - this should be done with the event scheduler
258 //cout << "About to search(1)" << endl;
262 //cout << "About to search(2)" << endl;
269 // Remove from list only if not needed by the AI system or the other comm channel
270 // TODO - implement me!!
271 void FGATCMgr::CommRemoveFromList(const char* id, atc_type tp, int chan) {
273 if(GetAirportATCDetails((string)id, &a)) {
276 a.set_by_comm_search = false;
277 airport_atc_map[(string)id] = a;
284 // Hack - need to implement this properly
285 RemoveFromList(id, tp);
289 // Remove from list - should only be called from above or similar
290 // This function *will* remove it from the list regardless of who else might want it.
291 void FGATCMgr::RemoveFromList(const char* id, atc_type tp) {
292 //cout << "Requested type = " << tp << '\n';
293 //cout << "id = " << id << '\n';
294 atc_list_itr = atc_list.begin();
295 while(atc_list_itr != atc_list.end()) {
296 //cout << "type = " << (*atc_list_itr)->GetType() << '\n';
297 //cout << "Ident = " << (*atc_list_itr)->get_ident() << '\n';
298 if( (!strcmp((*atc_list_itr)->get_ident(), id))
299 && ((*atc_list_itr)->GetType() == tp) ) {
300 //Before removing it stop it transmitting!!
301 //cout << "OBLITERATING FROM LIST!!!\n";
302 (*atc_list_itr)->SetNoDisplay();
303 (*atc_list_itr)->Update();
304 delete (*atc_list_itr);
305 atc_list_itr = atc_list.erase(atc_list_itr);
307 } // Note that that can upset where we are in the list but that doesn't really matter
313 // Find in list - return a currently active ATC pointer given ICAO code and type
314 // Return NULL if the given service is not in the list
315 // - *** THE CALLING FUNCTION MUST CHECK FOR THIS ***
316 FGATC* FGATCMgr::FindInList(const char* id, atc_type tp) {
317 atc_list_itr = atc_list.begin();
318 while(atc_list_itr != atc_list.end()) {
319 if( (!strcmp((*atc_list_itr)->get_ident(), id))
320 && ((*atc_list_itr)->GetType() == tp) ) {
321 return(*atc_list_itr);
322 } // Note that that can upset where we are in the list but that shouldn't really matter
325 // If we get here it's not in the list
329 // Returns true if the airport is found in the map
330 bool FGATCMgr::GetAirportATCDetails(string icao, AirportATC* a) {
331 if(airport_atc_map.find(icao) != airport_atc_map.end()) {
332 *a = *airport_atc_map[icao];
340 // Return a pointer to a given sort of ATC at a given airport and activate if necessary
341 // ONLY CALL THIS FUNCTION AFTER FIRST CHECKING THE SERVICE EXISTS BY CALLING GetAirportATCDetails
342 // FIXME - we really ought to take out the necessity for two function calls by simply returning
343 // a NULL pointer if the service doesn't exist and requiring the caller to check for it (NULL).
344 FGATC* FGATCMgr::GetATCPointer(string icao, atc_type type) {
345 AirportATC *a = airport_atc_map[icao];
346 //cout << "a->lon = " << a->lon << '\n';
347 //cout << "a->elev = " << a->elev << '\n';
348 //cout << "a->tower_freq = " << a->tower_freq << '\n';
351 if(a->tower_active) {
352 // Get the pointer from the list
353 return(FindInList(icao.c_str(), type)); // DCL - this untested so far.
355 FGTower* t = new FGTower;
357 if(current_commlist->FindByFreq(a->lon, a->lat, a->elev, a->tower_freq, &data, TOWER)) {
359 atc_list.push_back(t);
360 a->tower_active = true;
361 airport_atc_map[icao] = a;
364 cout << "ERROR - tower that should exist in FGATCMgr::GetATCPointer for airport " << icao << " not found\n";
368 // Lets add the rest to get rid of the compiler warnings even though we don't need them yet.
372 SG_LOG(SG_GENERAL, SG_ALERT, "ERROR - ATIS station should not be requested from FGATCMgr::GetATCPointer");
384 SG_LOG(SG_GENERAL, SG_ALERT, "ERROR IN FGATCMgr - reached end of GetATCPointer");
390 // Render a transmission
391 // Outputs the transmission either on screen or as audio depending on user preference
392 // The refname is a string to identify this sample to the sound manager
393 // The repeating flag indicates whether the message should be repeated continuously or played once.
394 void FGATCMgr::Render(string msg, string refname, bool repeating) {
395 #ifdef ENABLE_AUDIO_SUPPORT
396 voice = voiceOK && fgGetBool("/sim/sound/audible");
399 unsigned char* buf = v1.WriteMessage((char*)msg.c_str(), len, voice);
401 FGSimpleSound* simple = new FGSimpleSound(buf, len);
402 // TODO - at the moment the volume is always set off comm1
403 // and can't be changed after the transmission has started.
404 simple->set_volume(5.0 * fgGetDouble("/radios/comm[0]/volume"));
405 globals->get_soundmgr()->add(simple, refname);
407 globals->get_soundmgr()->play_looped(refname);
409 globals->get_soundmgr()->play_once(refname);
414 #endif // ENABLE_AUDIO_SUPPORT
416 // first rip the underscores and the pause hints out of the string - these are for the convienience of the voice parser
417 for(unsigned int i = 0; i < msg.length(); ++i) {
418 if((msg.substr(i,1) == "_") || (msg.substr(i,1) == "/")) {
422 globals->get_ATC_display()->RegisterRepeatingMessage(msg);
428 // Cease rendering a transmission.
429 void FGATCMgr::NoRender(string refname) {
432 #ifdef ENABLE_AUDIO_SUPPORT
433 globals->get_soundmgr()->stop(refname);
434 globals->get_soundmgr()->remove(refname);
437 globals->get_ATC_display()->CancelRepeatingMessage();
444 // Display a dialog box with options relevant to the currently tuned ATC service.
445 void FGATCMgr::doStandardDialog() {
446 /* DCL 2002/12/06 - This function currently in development
447 and dosen't display anything usefull to the end-user */
448 //cout << "FGATCMgr::doStandardDialog called..." << endl;
450 // First - need to determine which ATC service (if any) the user is tuned to.
451 //cout << "comm1_type = " << comm1_type << endl;
453 // Second - customise the dialog box
454 switch(comm_type[0]) {
456 atcDialogCommunicationOptions->newList(NULL);
457 atcDialogMessage->setLabel("Not tuned in to any ATC service.");
460 atcDialogCommunicationOptions->newList(NULL);
461 atcDialogMessage->setLabel("Tuned in to ATIS: no communication possible.");
464 atcDialogCommunicationOptions->newList(towerOptions);
465 atcDialogMessage->setLabel("Tuned in to Tower - select communication to transmit:");
468 atcDialogCommunicationOptions->newList(NULL);
469 atcDialogMessage->setLabel("Tuned in to Ground - select communication to transmit:");
472 atcDialogCommunicationOptions->newList(approachOptions);
473 atcDialogMessage->setLabel("Tuned in to Approach - select communication to transmit:");
476 atcDialogCommunicationOptions->newList(NULL);
477 atcDialogMessage->setLabel("Tuned in to unknown ATC service - enter transmission:");
481 // Third - display the dialog without pausing sim.
484 // Forth - need to direct input back from the dialog to the relevant ATC service.
485 // This is in ATCDialogOK()
488 // Search for ATC stations by frequency
489 void FGATCMgr::FreqSearch(int channel) {
490 int chan = channel - 1; // Convert to zero-based for the arrays
493 double freq = comm_node[chan]->getDoubleValue();
494 lon = lon_node->getDoubleValue();
495 lat = lat_node->getDoubleValue();
496 elev = elev_node->getDoubleValue() * SG_FEET_TO_METER;
498 // Query the data store and get the closest match if any
499 if(current_commlist->FindByFreq(lon, lat, elev, freq, &data)) {
502 // If this channel not previously valid then easy - add ATC to list
503 // If this channel was valid then - Have we tuned to a different service?
504 // If so - de-register one and add the other
505 if(comm_valid[chan]) {
506 if((comm_ident[chan] == data.ident) && (comm_type[chan] == data.type)) {
507 // Then we're still tuned into the same service so do nought and return
510 // Something's changed - either the location or the service type
511 // We need to feed the channel in so we're not removing it if we're also tuned in on the other channel
512 CommRemoveFromList(comm_ident[chan], comm_type[chan], chan);
515 // At this point we can assume that we need to add the service.
516 comm_ident[chan] = (data.ident).c_str();
517 comm_type[chan] = data.type;
518 comm_x[chan] = (double)data.x;
519 comm_y[chan] = (double)data.y;
520 comm_z[chan] = (double)data.z;
521 comm_lon[chan] = (double)data.lon;
522 comm_lat[chan] = (double)data.lat;
523 comm_elev[chan] = (double)data.elev;
524 comm_valid[chan] = true;
526 // This was a switch-case statement but the compiler didn't like the new variable creation with it.
527 if(comm_type[chan] == ATIS) {
528 FGATIS* a = new FGATIS;
530 comm_atc_ptr[chan] = a;
532 //a->set_refname((chan) ? "atis2" : "atis1"); // FIXME - that line is limited to 2 channels
533 atc_list.push_back(a);
534 } else if (comm_type[chan] == TOWER) {
535 FGATC* app = FindInList(comm_ident[chan], TOWER);
537 // The station is already in the ATC list
540 // Generate the station and put in the ATC list
541 FGTower* t = new FGTower;
543 comm_atc_ptr[chan] = t;
545 atc_list.push_back(t);
547 } /*else if (comm_type[chan] == APPROACH) {
548 // We have to be a bit more carefull here since approaches are also searched by area
549 FGATC* app = FindInList(comm_ident[chan], APPROACH);
551 // The station is already in the ATC list
552 app->AddPlane("Player");
555 // Generate the station and put in the ATC list
556 FGApproach* a = new FGApproach;
558 a->AddPlane("Player");
559 atc_list.push_back(a);
563 if(comm_valid[chan]) {
564 if(comm_type[chan] != APPROACH) {
565 // Currently approaches are removed by Alexander's out-of-range mechanism
566 CommRemoveFromList(comm_ident[chan], comm_type[chan], chan);
568 // Note that we *don't* call SetNoDisplay() here because the other comm channel
569 // might be tuned into the same station - this is handled by CommRemoveFromList(...)
570 comm_type[chan] = INVALID;
571 comm_atc_ptr[chan] = NULL;
572 comm_valid[chan] = false;
578 // Search ATC stations by area in order that we appear 'on the radar'
579 void FGATCMgr::AreaSearch() {
580 // Search for Approach stations
581 comm_list_type approaches;
582 comm_list_iterator app_itr;
584 lon = lon_node->getDoubleValue();
585 lat = lat_node->getDoubleValue();
586 elev = elev_node->getDoubleValue() * SG_FEET_TO_METER;
588 // search stations in range
589 int num_app = current_commlist->FindByPos(lon, lat, elev, &approaches, APPROACH);
591 //cout << num_app << " approaches found in radiostack search !!!!" << endl;
593 for(app_itr = approaches.begin(); app_itr != approaches.end(); app_itr++) {
595 FGATC* app = FindInList((app_itr->ident).c_str(), app_itr->type);
597 // The station is already in the ATC list
598 app->AddPlane("Player");
601 // Generate the station and put in the ATC list
602 FGApproach* a = new FGApproach;
603 a->SetData(&(*app_itr));
604 a->AddPlane("Player");
606 atc_list.push_back(a);
611 // remove planes which are out of range
612 // TODO - I'm not entirely sure that this belongs here.
613 atc_list_itr = atc_list.begin();
614 while(atc_list_itr != atc_list.end()) {
615 if((*atc_list_itr)->GetType() == APPROACH ) {
616 int np = (*atc_list_itr)->RemovePlane();
617 // if approach has no planes left remove it from ATC list
619 (*atc_list_itr)->SetNoDisplay();
620 (*atc_list_itr)->Update();
621 delete (*atc_list_itr);
622 atc_list_itr = atc_list.erase(atc_list_itr);
623 break; // the other stations will be checked next time