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 "atislist.hxx"
29 //#include "groundlist.hxx"
30 #include "towerlist.hxx"
31 #include "approachlist.hxx"
32 #include "ATCdisplay.hxx"
35 // periodic radio station search wrapper
36 static void fgATCSearch( void ) {
37 globals->get_ATC_mgr()->Search();
39 */ //This wouldn't compile - including Time/event.hxx breaks it :-(
41 static char* t0 = "Request landing clearance";
42 static char* t1 = "Request departure clearance";
43 static char* t2 = "Report Runway vacated";
44 static char** towerOptions = new char*[4];
45 static char* a0 = "Request vectors";
46 static char** approachOptions = new char*[2];
48 // For the ATC dialog - copied from the Autopilot new heading dialog code!
49 static puDialogBox* atcDialog;
50 static puFrame* atcDialogFrame;
51 static puText* atcDialogMessage;
52 //static puInput* atcDialogInput;
53 static puOneShot* atcDialogOkButton;
54 static puOneShot* atcDialogCancelButton;
55 static puButtonBox* atcDialogCommunicationOptions;
57 static void ATCDialogCancel(puObject *)
59 //ATCDialogInput->rejectInput();
60 FG_POP_PUI_DIALOG( atcDialog );
63 static void ATCDialogOK (puObject *me)
65 switch(globals->get_ATC_mgr()->GetCurrentATCType()) {
71 FGTower* twr = (FGTower*)globals->get_ATC_mgr()->GetCurrentATCPointer();
72 switch(atcDialogCommunicationOptions->getValue()) {
74 cout << "Option 0 chosen\n";
75 twr->RequestLandingClearance("golf bravo echo");
78 cout << "Option 1 chosen\n";
79 twr->RequestDepartureClearance("golf bravo echo");
82 cout << "Option 2 chosen\n";
83 twr->ReportRunwayVacated("golf bravo echo");
99 //if(error) mkDialog(s.c_str());
102 static void ATCDialog(puObject *cb)
104 //ApHeadingDialogInput -> setValue ( heading );
105 //ApHeadingDialogInput -> acceptInput();
106 FG_PUSH_PUI_DIALOG(atcDialog);
109 static void ATCDialogInit()
111 char defaultATCLabel[] = "Enter desired option to communicate with ATC:";
114 // Option lists hardwired per ATC type
115 towerOptions[0] = new char[strlen(t0)];
116 strcpy(towerOptions[0], t0);
117 towerOptions[1] = new char[strlen(t1)];
118 strcpy(towerOptions[1], t1);
119 towerOptions[2] = new char[strlen(t2)];
120 strcpy(towerOptions[2], t2);
121 towerOptions[3] = NULL;
123 approachOptions[0] = new char[strlen(a0)];
124 strcpy(approachOptions[0], a0);
125 approachOptions[1] = NULL;
127 atcDialog = new puDialogBox (150, 50);
129 atcDialogFrame = new puFrame (0, 0, 500, 250);
131 atcDialogMessage = new puText (250, 220);
132 atcDialogMessage -> setDefaultValue (defaultATCLabel);
133 atcDialogMessage -> getDefaultValue (&s);
134 atcDialogMessage -> setLabel (s);
135 atcDialogMessage -> setLabelPlace (PUPLACE_TOP_CENTERED);
137 atcDialogCommunicationOptions = new puButtonBox (50, 50, 450, 210, NULL, true);
139 atcDialogOkButton = new puOneShot (50, 10, 110, 50);
140 atcDialogOkButton -> setLegend (gui_msg_OK);
141 atcDialogOkButton -> makeReturnDefault (TRUE);
142 atcDialogOkButton -> setCallback (ATCDialogOK);
144 atcDialogCancelButton = new puOneShot (140, 10, 210, 50);
145 atcDialogCancelButton -> setLegend (gui_msg_CANCEL);
146 atcDialogCancelButton -> setCallback (ATCDialogCancel);
149 FG_FINALIZE_PUI_DIALOG(atcDialog);
152 // For the command manager - maybe eventually this should go in the built in command list
153 static bool do_ATC_dialog(const SGPropertyNode* arg) {
154 globals->get_ATC_mgr()->doStandardDialog();
159 FGATCMgr::FGATCMgr() {
161 comm1_atis_ident = "";
162 comm1_tower_ident = "";
163 comm1_approach_ident = "";
164 last_comm1_ident = "";
165 last_comm1_atis_ident = "";
166 last_comm1_tower_ident = "";
167 last_comm1_approach_ident = "";
169 last_in_range = false;
170 comm1_atis_valid = false;
171 comm1_tower_valid = false;
172 comm1_approach_valid = false;
173 comm1_type = INVALID;
174 tuned_atc_ptr = NULL;
177 FGATCMgr::~FGATCMgr() {
180 void FGATCMgr::bind() {
183 void FGATCMgr::unbind() {
186 void FGATCMgr::init() {
187 comm1_node = fgGetNode("/radios/comm[0]/frequencies/selected-mhz", true);
188 comm2_node = fgGetNode("/radios/comm[1]/frequencies/selected-mhz", true);
189 lon_node = fgGetNode("/position/longitude-deg", true);
190 lat_node = fgGetNode("/position/latitude-deg", true);
191 elev_node = fgGetNode("/position/altitude-ft", true);
192 atc_list_itr = atc_list.begin();
193 // Search for connected ATC stations once per 0.8 seconds or so
194 // global_events.Register( "fgATCSearch()", fgATCSearch,
195 // fgEVENT::FG_EVENT_READY, 800);
196 // For some reason the above doesn't compile - including Time/event.hxx stops compilation.
198 // Initialise the airport_atc_map - we'll cheat for now and just hardcode KEMT and any others that may be needed for development
199 AirportATC *a = new AirportATC;
200 a->lon = -118.034719;
203 a->atis_freq = 118.75;
204 a->atis_active = false;
205 a->tower_freq = 121.2;
206 a->tower_active = false;
207 a->ground_freq = 125.9;
208 a->ground_active = false;
210 //a->set_by_AI = true;
211 //a->set_by_comm_search = false;
213 airport_atc_map[(string)"KEMT"] = a;
215 #ifdef ENABLE_AUDIO_SUPPORT
216 // Load all available voices.
217 // For now we'll do one hardwired one
218 voiceOK = v1.LoadVoice("default");
219 /* I've loaded the voice even if /sim/sound/audible is false
220 * since I know no way of forcing load of the voice if the user
221 * subsequently switches /sim/sound/audible to true. */
226 // Initialise the ATC Dialogs
229 // Add ATC-dialog to the command list
230 globals->get_commands()->addCommand("ATC-dialog", do_ATC_dialog);
233 void FGATCMgr::update(double dt) {
234 //Traverse the list of active stations.
235 //Only update one class per update step to avoid the whole ATC system having to calculate between frames.
236 //Eventually we should only update every so many steps.
237 //cout << "In FGATCMgr::update - atc_list.size = " << atc_list.size() << '\n';
238 if(atc_list.size()) {
239 if(atc_list_itr == atc_list.end()) {
240 atc_list_itr = atc_list.begin();
242 (*atc_list_itr)->Update();
246 // Search the tuned frequencies every now and then - this should be done with the event scheduler
255 // Remove from list only if not needed by the AI system
256 void FGATCMgr::CommRemoveFromList(const char* id, atc_type tp) {
258 if(GetAirportATCDetails((string)id, &a)) {
261 a.set_by_comm_search = false;
262 airport_atc_map[(string)id] = a;
271 // Remove from list - should only be called from above or similar
272 void FGATCMgr::RemoveFromList(const char* id, atc_type tp) {
273 //cout << "Requested type = " << tp << '\n';
274 //cout << "id = " << id << '\n';
275 atc_list_itr = atc_list.begin();
276 while(atc_list_itr != atc_list.end()) {
277 //cout << "type = " << (*atc_list_itr)->GetType() << '\n';
278 //cout << "Ident = " << (*atc_list_itr)->GetIdent() << '\n';
279 if( (!strcmp((*atc_list_itr)->GetIdent(), id))
280 && ((*atc_list_itr)->GetType() == tp) ) {
281 //Before removing it stop it transmitting!!
282 //cout << "OBLITERATING FROM LIST!!!\n";
283 (*atc_list_itr)->SetNoDisplay();
284 (*atc_list_itr)->Update();
285 delete (*atc_list_itr);
286 atc_list_itr = atc_list.erase(atc_list_itr);
288 } // Note that that can upset where we are in the list but that doesn't really matter
294 //DCL - this routine untested so far.
295 // Find in list - return a currently active ATC pointer given ICAO code and type
296 FGATC* FGATCMgr::FindInList(const char* id, atc_type tp) {
297 atc_list_itr = atc_list.begin();
298 while(atc_list_itr != atc_list.end()) {
299 if( (!strcmp((*atc_list_itr)->GetIdent(), id))
300 && ((*atc_list_itr)->GetType() == tp) ) {
301 return(*atc_list_itr);
302 } // Note that that can upset where we are in the list but that shouldn't really matter
305 // We need a fallback position
306 SG_LOG(SG_GENERAL, SG_ALERT, "*** Failed to find FGATC* in FGATCMgr::FindInList - this should not happen!");
310 // Returns true if the airport is found in the map
311 bool FGATCMgr::GetAirportATCDetails(string icao, AirportATC* a) {
312 if(airport_atc_map.find(icao) != airport_atc_map.end()) {
313 *a = *airport_atc_map[icao];
321 // Return a pointer to a given sort of ATC at a given airport and activate if necessary
322 // ONLY CALL THIS FUNCTION AFTER FIRST CHECKING THE SERVICE EXISTS BY CALLING GetAirportATCDetails
323 // FIXME - we really ought to take out the necessity for two function calls by simply returning
324 // a NULL pointer if the service doesn't exist and requiring the caller to check for it (NULL).
325 FGATC* FGATCMgr::GetATCPointer(string icao, atc_type type) {
326 AirportATC *a = airport_atc_map[icao];
327 //cout << "a->lon = " << a->lon << '\n';
328 //cout << "a->elev = " << a->elev << '\n';
329 //cout << "a->tower_freq = " << a->tower_freq << '\n';
332 if(a->tower_active) {
333 // Get the pointer from the list
334 return(FindInList(icao.c_str(), type)); // DCL - this untested so far.
336 FGTower* t = new FGTower;
337 if(current_towerlist->query(a->lon, a->lat, a->elev, a->tower_freq, &tower)) {
339 atc_list.push_back(t);
340 a->tower_active = true;
341 airport_atc_map[icao] = a;
344 cout << "ERROR - tower that should exist in FGATCMgr::GetATCPointer for airport " << icao << " not found\n";
348 // Lets add the rest to get rid of the compiler warnings even though we don't need them yet.
352 SG_LOG(SG_GENERAL, SG_ALERT, "ERROR - ATIS station should not be requested from FGATCMgr::GetATCPointer");
364 SG_LOG(SG_GENERAL, SG_ALERT, "ERROR IN FGATCMgr - reached end of GetATCPointer");
370 // Render a transmission
371 // Outputs the transmission either on screen or as audio depending on user preference
372 // The repeating flag indicates whether the message should be repeated continuously or played once.
373 void FGATCMgr::Render(string msg, bool repeating) {
374 #ifdef ENABLE_AUDIO_SUPPORT
375 voice = voiceOK && fgGetBool("/sim/sound/audible");
378 unsigned char* buf = v1.WriteMessage((char*)msg.c_str(), len, voice);
380 FGSimpleSound* simple = new FGSimpleSound(buf, len);
381 simple->set_volume(2.0);
382 globals->get_soundmgr()->add(simple, refname);
384 globals->get_soundmgr()->play_looped(refname);
386 globals->get_soundmgr()->play_once(refname);
391 #endif // ENABLE_AUDIO_SUPPORT
393 // first rip the underscores and the pause hints out of the string - these are for the convienience of the voice parser
394 for(unsigned int i = 0; i < msg.length(); ++i) {
395 if((msg.substr(i,1) == "_") || (msg.substr(i,1) == "/")) {
399 globals->get_ATC_display()->RegisterRepeatingMessage(msg);
405 // Cease rendering a transmission.
406 // At the moment this can handle one transmission active at a time only.
407 void FGATCMgr::NoRender() {
410 #ifdef ENABLE_AUDIO_SUPPORT
411 globals->get_soundmgr()->stop(refname);
412 globals->get_soundmgr()->remove(refname);
415 globals->get_ATC_display()->CancelRepeatingMessage();
422 // Display a dialog box with options relevant to the currently tuned ATC service.
423 void FGATCMgr::doStandardDialog() {
424 /* DCL 2002/12/06 - This function currently in development
425 and dosen't display anything usefull to the end-user */
426 //cout << "FGATCMgr::doStandardDialog called..." << endl;
428 // First - need to determine which ATC service (if any) the user is tuned to.
429 //cout << "comm1_type = " << comm1_type << endl;
431 // Second - customise the dialog box
434 atcDialogCommunicationOptions->newList(NULL);
435 atcDialogMessage->setLabel("Not tuned in to any ATC service.");
438 atcDialogCommunicationOptions->newList(NULL);
439 atcDialogMessage->setLabel("Tuned in to ATIS: no communication possible.");
442 atcDialogCommunicationOptions->newList(towerOptions);
443 atcDialogMessage->setLabel("Tuned in to Tower - select communication to transmit:");
446 atcDialogCommunicationOptions->newList(NULL);
447 atcDialogMessage->setLabel("Tuned in to Ground - select communication to transmit:");
450 atcDialogCommunicationOptions->newList(approachOptions);
451 atcDialogMessage->setLabel("Tuned in to Approach - select communication to transmit:");
454 atcDialogCommunicationOptions->newList(NULL);
455 atcDialogMessage->setLabel("Tuned in to unknown ATC service - enter transmission:");
459 // Third - display the dialog without pausing sim.
462 // Forth - need to direct input back from the dialog to the relevant ATC service.
463 // This is in ATCDialogOK()
466 void FGATCMgr::Search() {
468 ////////////////////////////////////////////////////////////////////////
470 ////////////////////////////////////////////////////////////////////////
471 //cout << "In FGATCMgr::Search() - atc_list.size = " << atc_list.size() << '\n';
473 comm1_freq = comm1_node->getDoubleValue();
474 //cout << "************* comm1_freq = " << comm1_freq << '\n';
475 double lon = lon_node->getDoubleValue();
476 double lat = lat_node->getDoubleValue();
477 double elev = elev_node->getDoubleValue() * SG_FEET_TO_METER;
479 // Store the comm1_type
480 //atc_type old_comm1_type = comm1_type;
482 // We must be able to generalise some of the repetetive searching below!
484 //Search for ATIS first
485 if(current_atislist->query(lon, lat, elev, comm1_freq, &atis)) {
486 //cout << "atis found in radiostack search !!!!" << endl;
487 //cout << "last_comm1_atis_ident = " << last_comm1_atis_ident << '\n';
488 //cout << "comm1_type " << comm1_type << '\n';
489 comm1_atis_ident = atis.GetIdent();
490 comm1_atis_valid = true;
491 if(last_comm1_atis_ident != comm1_atis_ident) {
492 if(last_comm1_atis_ident != "") {
493 RemoveFromList(last_comm1_atis_ident, ATIS);
495 last_comm1_atis_ident = comm1_atis_ident;
496 //cout << "last_comm1_atis_ident = " << last_comm1_atis_ident << '\n';
498 comm1_elev = atis.get_elev();
499 comm1_range = FG_ATIS_DEFAULT_RANGE;
500 comm1_effective_range = comm1_range;
501 comm1_x = atis.get_x();
502 comm1_y = atis.get_y();
503 comm1_z = atis.get_z();
504 FGATIS* a = new FGATIS;
508 atc_list.push_back(a);
509 //cout << "Found a new atis station in range" << endl;
510 //cout << " id = " << atis.GetIdent() << endl;
511 return; //This rather assumes that we never have more than one type of station in range.
514 if(comm1_atis_valid) {
515 //cout << "Removing ATIS " << comm1_atis_ident << " from list\n";
516 RemoveFromList(comm1_atis_ident, ATIS);
517 comm1_atis_valid = false;
518 if(comm1_type == ATIS) {
519 comm1_type = INVALID;
521 comm1_atis_ident = "";
522 //comm1_trans_ident = "";
523 last_comm1_atis_ident = "";
524 tuned_atc_ptr = NULL;
526 //cout << "not picking up atis" << endl;
529 //Next search for tower
530 //cout << "comm1_freq = " << comm1_freq << '\n';
531 if(current_towerlist->query(lon, lat, elev, comm1_freq, &tower)) {
532 //cout << "tower found in radiostack search !!!!" << endl;
533 comm1_tower_ident = tower.GetIdent();
534 //cout << "comm1_tower_ident = " << comm1_tower_ident << '\n';
535 comm1_tower_valid = true;
536 if(last_comm1_tower_ident != comm1_tower_ident) {
537 if(last_comm1_tower_ident != "") {
538 RemoveFromList(last_comm1_tower_ident, TOWER);
540 last_comm1_tower_ident = comm1_tower_ident;
542 comm1_elev = tower.get_elev();
543 comm1_range = FG_TOWER_DEFAULT_RANGE;
544 comm1_effective_range = comm1_range;
545 comm1_x = tower.get_x();
546 comm1_y = tower.get_y();
547 comm1_z = tower.get_z();
548 FGTower* t = new FGTower;
552 atc_list.push_back(t);
553 //cout << "Found a new tower station in range" << endl;
554 //cout << " id = " << tower.GetIdent() << endl;
555 return; //This rather assumes that we never have more than one type of station in range.
558 if(comm1_tower_valid) {
559 //cout << "removing tower\n";
560 RemoveFromList(comm1_tower_ident, TOWER);
561 //comm1_valid = false;
562 if(comm1_type == TOWER) {
563 comm1_type = INVALID; // Only invalidate if we haven't switched it to something else
565 comm1_tower_valid = false;
566 comm1_tower_ident = "";
567 last_comm1_tower_ident = "";
568 tuned_atc_ptr = NULL;
570 //comm1_trans_ident = "";
571 //last_comm1_ident = "";
573 //cout << "not picking up tower" << endl;
576 //Next search for Ground control
577 if(current_groundlist->query(lon, lat, elev, comm1_freq, &ground)) {
578 //cout << "Ground Control found in radiostack search !!!!" << endl;
579 comm1_ident = ground.GetIdent();
581 if((last_comm1_ident != comm1_ident) || (comm1_type != GROUND)) {
582 if(last_comm1_ident != "") {
583 RemoveFromList(last_comm1_ident, GROUND);
585 last_comm1_ident = comm1_ident;
587 comm1_elev = ground.get_elev();
588 comm1_range = FG_GROUND_DEFAULT_RANGE;
589 comm1_effective_range = comm1_range;
590 comm1_x = ground.get_x();
591 comm1_y = ground.get_y();
592 comm1_z = ground.get_z();
593 FGGround* g = new FGGround;
596 atc_list.push_back(g);
597 // For now we will automatically make contact with ground when the radio is tuned.
598 // This rather assumes that the user tunes the radio at the appropriate place
599 // (ie. having just turned off the runway) and only uses ground control on arrival
601 g->NewArrival(current_plane);
602 //cout << "Found a new ground station in range" << endl;
603 //cout << " id = " << ground.GetIdent() << endl;
604 return; //This rather assumes that we never have more than one type of station in range.
607 if((comm1_valid) && (comm1_type == GROUND)) {
608 RemoveFromList(comm1_ident, GROUND);
610 comm1_type = INVALID;
612 //comm1_trans_ident = "";
613 last_comm1_ident = "";
615 //cout << "not picking up ground control" << endl;
618 // ================================================================================
619 // Search for Approach stations
620 // ================================================================================
621 // init number of approach stations reachable by plane
624 // search stations in range
625 current_approachlist->query_bck(lon, lat, elev, approaches, max_app, num_app);
627 //cout << num_app << " approaches found in radiostack search !!!!" << endl;
629 for ( int i=0; i<num_app; i++ ) {
631 approach_ident = approaches[i].GetIdent();
633 // check if station already exists on ATC stack
634 atc_list_itr = atc_list.begin();
635 while(atc_list_itr != atc_list.end()) {
636 //cout << "ATC list: " << (*atc_list_itr)->GetIdent() << endl;
637 if((!strcmp((*atc_list_itr)->GetIdent(), approach_ident))
638 && ((*atc_list_itr)->GetType() == APPROACH) ) {
640 string pid = "Player";
641 (*atc_list_itr)->AddPlane(pid);
642 (*atc_list_itr)->Update();
647 // generate new Approach on ATC stack
649 FGApproach* a = new FGApproach;
651 string pid = "Player";
656 atc_list.push_back(a);
657 //cout << "Found a new approach station in range: Id = "
658 // << approaches[i].GetIdent() << endl;
663 // remove planes which are out of range
664 atc_list_itr = atc_list.begin();
665 while(atc_list_itr != atc_list.end()) {
666 if((*atc_list_itr)->GetType() == APPROACH ) {
667 int np = (*atc_list_itr)->RemovePlane();
668 // if approach has no planes left remove it from ATC list
670 (*atc_list_itr)->SetNoDisplay();
671 (*atc_list_itr)->Update();
672 delete (*atc_list_itr);
673 atc_list_itr = atc_list.erase(atc_list_itr);
674 break; // the other stations will be checked next time