]> git.mxchange.org Git - flightgear.git/blob - src/ATC/ATCDialog.cxx
This innocuous looking typo was crashing the sim whenever an AI plane was asked to...
[flightgear.git] / src / ATC / ATCDialog.cxx
1 // ATCDialog.cxx - Functions and classes to handle the pop-up ATC dialog
2 //
3 // Written by Alexander Kappes and David Luff, started February 2003.
4 //
5 // Copyright (C) 2003  Alexander Kappes and David Luff
6 //
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.
11 //
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.
16 //
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.
20
21 #ifdef HAVE_CONFIG_H
22 #  include "config.h"
23 #endif
24
25 #include <simgear/compiler.h>
26
27 #include <simgear/structure/commands.hxx>
28
29 #include <Main/globals.hxx>
30 #include <GUI/gui.h>            // mkDialog
31 #include <GUI/new_gui.hxx>
32
33 #include "ATCDialog.hxx"
34 #include "ATC.hxx"
35 #include "ATCmgr.hxx"
36 #include "ATCdisplay.hxx"
37 #include "commlist.hxx"
38 #include "ATCutils.hxx"
39 #include <Airports/simple.hxx>
40
41 #include <sstream>
42 #include <map>
43
44 SG_USING_STD(ostringstream);
45 SG_USING_STD(map);
46
47 FGATCDialog *current_atcdialog;
48
49 // For the command manager - maybe eventually this should go in the built in command list
50 static bool do_ATC_dialog(const SGPropertyNode* arg) {
51         current_atcdialog->PopupDialog();
52         return(true);
53 }
54
55 static bool do_ATC_freq_search(const SGPropertyNode* arg) {
56         current_atcdialog->FreqDialog();
57         return(true);
58 }
59
60 ATCMenuEntry::ATCMenuEntry() {
61   stationid    = "";
62   //stationfr    = 0;
63   transmission = "";
64   menuentry    = "";
65   callback_code = 0;
66 }
67
68 ATCMenuEntry::~ATCMenuEntry() {
69 }
70
71 static void atcUppercase(string &s) {
72         for(unsigned int i=0; i<s.size(); ++i) {
73                 s[i] = toupper(s[i]);
74         }
75 }
76
77 // find child whose <name>...</name> entry matches 'name'
78 static SGPropertyNode *getNamedNode(SGPropertyNode *prop, const char *name) {
79         SGPropertyNode* p;
80
81         for (int i = 0; i < prop->nChildren(); i++)
82                 if ((p = getNamedNode(prop->getChild(i), name)))
83                         return p;
84
85         if (!strcmp(prop->getStringValue("name"), name))
86                 return prop;
87
88         return 0;
89 }
90
91
92 FGATCDialog::FGATCDialog() {
93         _callbackPending = false;
94         _callbackTimer = 0.0;
95         _callbackWait = 0.0;
96         _callbackPtr = NULL;
97         _callbackCode = 0;
98         _gui = (NewGUI *)globals->get_subsystem("gui");
99 }
100
101 FGATCDialog::~FGATCDialog() {
102 }
103
104 void FGATCDialog::Init() {
105         // Add ATC-dialog to the command list
106         globals->get_commands()->addCommand("ATC-dialog", do_ATC_dialog);
107         // Add ATC-freq-search to the command list
108         globals->get_commands()->addCommand("ATC-freq-search", do_ATC_freq_search);
109
110         // initialize properties polled in Update()
111         globals->get_props()->setStringValue("/sim/atc/freq-airport", "");
112         globals->get_props()->setIntValue("/sim/atc/transmission-num", -1);
113 }
114
115 void FGATCDialog::Update(double dt) {
116         static SGPropertyNode_ptr airport = globals->get_props()->getNode("/sim/atc/freq-airport", true);
117         string s = airport->getStringValue();
118         if (!s.empty()) {
119                 airport->setStringValue("");
120                 FreqDisplay(s);
121         }
122
123         static SGPropertyNode_ptr trans_num = globals->get_props()->getNode("/sim/atc/transmission-num", true);
124         int n = trans_num->getIntValue();
125         if (n >= 0) {
126                 trans_num->setIntValue(-1);
127                 PopupCallback(n);
128         }
129
130         if(_callbackPending) {
131                 if(_callbackTimer > _callbackWait) {
132                         _callbackPtr->ReceiveUserCallback(_callbackCode);
133                         _callbackPtr->NotifyTransmissionFinished(fgGetString("/sim/user/callsign"));
134                         _callbackPending = false;
135                 } else {
136                         _callbackTimer += dt;
137                 }
138         }
139 }
140
141 // Add an entry
142 void FGATCDialog::add_entry(const string& station, const string& transmission, const string& menutext, atc_type type, int code) {
143
144   ATCMenuEntry a;
145
146   a.stationid = station;
147   a.transmission = transmission;
148   a.menuentry = menutext;
149   a.callback_code = code;
150
151   (available_dialog[type])[station.c_str()].push_back(a);
152
153 }
154
155 void FGATCDialog::remove_entry( const string &station, const string &trans, atc_type type ) {
156   atcmentry_vec_type* p = &((available_dialog[type])[station]);
157   atcmentry_vec_iterator current = p->begin();
158   while(current != p->end()) {
159     if(current->transmission == trans) current = p->erase(current);
160         else ++current;
161   }
162 }
163
164 void FGATCDialog::remove_entry( const string &station, int code, atc_type type ) {
165   atcmentry_vec_type* p = &((available_dialog[type])[station]);
166   atcmentry_vec_iterator current = p->begin();
167   while(current != p->end()) {
168     if(current->callback_code == code) current = p->erase(current);
169         else ++current;
170   }
171 }
172
173 // query the database whether the transmission is already registered;
174 bool FGATCDialog::trans_reg( const string &station, const string &trans, atc_type type ) {
175   atcmentry_vec_type* p = &((available_dialog[type])[station]);
176   atcmentry_vec_iterator current = p->begin();
177   for ( ; current != p->end() ; ++current ) {
178     if ( current->transmission == trans ) return true;
179   }
180   return false;
181 }
182
183 // query the database whether the transmission is already registered;
184 bool FGATCDialog::trans_reg( const string &station, int code, atc_type type ) {
185   atcmentry_vec_type* p = &((available_dialog[type])[station]);
186   atcmentry_vec_iterator current = p->begin();
187   for ( ; current != p->end() ; ++current ) {
188     if ( current->callback_code == code ) return true;
189   }
190   return false;
191 }
192
193 // Display the ATC popup dialog box with options relevant to the users current situation.
194 void FGATCDialog::PopupDialog() {
195         const char *dialog_name = "atc-dialog";
196         SGPropertyNode_ptr dlg = _gui->getDialog(dialog_name);
197         if (!dlg)
198                 return;
199
200         _gui->closeDialog(dialog_name);
201
202         SGPropertyNode_ptr button_group = getNamedNode(dlg, "transmission-choice");
203         // remove all transmission buttons
204         button_group->removeChildren("button", false);
205
206         string label;
207         FGATC* atcptr = globals->get_ATC_mgr()->GetComm1ATCPointer();   // Hardwired to comm1 at the moment
208
209         if (!atcptr) {
210                 label = "Not currently tuned to any ATC service";
211                 mkDialog(label.c_str());
212                 return;
213         }
214
215         if(atcptr->GetType() == ATIS) {
216                 label = "Tuned to ATIS - no communication possible";
217                 mkDialog(label.c_str());
218                 return;
219         }
220
221         atcmentry_vec_type atcmlist = (available_dialog[atcptr->GetType()])[atcptr->get_ident()];
222         atcmentry_vec_iterator current = atcmlist.begin();
223         atcmentry_vec_iterator last = atcmlist.end();
224         
225         if(!atcmlist.size()) {
226                 label = "No transmission available";
227                 mkDialog(label.c_str());
228                 return;
229         }
230
231         const int bufsize = 32;
232         char buf[bufsize];
233         // loop over all entries in atcmentrylist
234         for (int n = 0; n < 10; ++n) {
235                 snprintf(buf, bufsize, "/sim/atc/opt[%d]", n);
236                 fgSetBool(buf, false);
237
238                 if (current == last)
239                         continue;
240
241                 // add transmission button (modified copy of <button-template>)
242                 SGPropertyNode *entry = button_group->getNode("button", n, true);
243                 copyProperties(button_group->getNode("button-template", true), entry);
244                 entry->removeChildren("hide", false);
245                 entry->setStringValue("property", buf);
246                 entry->setIntValue("keynum", '1' + n);
247                 if (n == 0)
248                         entry->setBoolValue("default", true);
249
250                 snprintf(buf, bufsize, "%d", n + 1);
251                 string legend = string(buf) + ". " + current->menuentry;
252                 entry->setStringValue("legend", legend.c_str());
253                 entry->setIntValue("binding/value", n);
254                 current++;
255         }
256
257         _gui->showDialog(dialog_name);
258         return;
259 }
260
261 void FGATCDialog::PopupCallback(int num) {
262         FGATC* atcptr = globals->get_ATC_mgr()->GetComm1ATCPointer();   // FIXME - Hardwired to comm1 at the moment
263
264         if (!atcptr)
265                 return;
266
267         if (atcptr->GetType() == TOWER) {
268                 //cout << "TOWER " << endl;
269                 //cout << "ident is " << atcptr->get_ident() << endl;
270                 atcmentry_vec_type atcmlist = (available_dialog[TOWER])[atcptr->get_ident()];
271                 unsigned int size = atcmlist.size();
272                 if(size && num < size) {
273                         //cout << "Doing callback...\n";
274                         ATCMenuEntry a = atcmlist[num];
275                         atcptr->SetFreqInUse();
276                         string pilot = atcptr->GenText(a.transmission, a.callback_code);
277                         fgSetString("/sim/messages/pilot", pilot.c_str());
278                         // This is the user's speech getting displayed.
279                         globals->get_ATC_display()->RegisterSingleMessage(pilot);
280                         _callbackPending = true;
281                         _callbackTimer = 0.0;
282                         _callbackWait = 5.0;
283                         _callbackPtr = atcptr;
284                         _callbackCode = a.callback_code;
285                 } else {
286                         //cout << "No options available...\n";
287                 }
288                 //cout << "Donded" << endl;
289         }
290 }
291
292 // map() key data type (removes duplicates and sorts by distance)
293 struct atcdata {
294         atcdata() {}
295         atcdata(const string i, const string n, const double d) {
296                 id = i, name = n, distance = d;
297         }
298         bool operator<(const atcdata& a) const {
299                 return id != a.id && distance < a.distance;
300         }
301         bool operator==(const atcdata& a) const {
302                 return id == a.id && distance == a.distance;
303         }
304         string id;
305         string name;
306         double distance;
307 };
308
309 void FGATCDialog::FreqDialog() {
310         const char *dialog_name = "atc-freq-search";
311         SGPropertyNode_ptr dlg = _gui->getDialog(dialog_name);
312         if (!dlg)
313                 return;
314
315         _gui->closeDialog(dialog_name);
316
317         SGPropertyNode_ptr button_group = getNamedNode(dlg, "quick-buttons");
318         // remove all dynamic airport/ATC buttons
319         button_group->removeChildren("button", false);
320
321         // Find the ATC stations within a reasonable range (about 40 miles?)
322         comm_list_type atc_stations;
323         comm_list_iterator atc_stat_itr;
324         
325         double lon = fgGetDouble("/position/longitude-deg");
326         double lat = fgGetDouble("/position/latitude-deg");
327         double elev = fgGetDouble("/position/altitude-ft");
328         Point3D aircraft = sgGeodToCart(Point3D(lon * SGD_DEGREES_TO_RADIANS,
329                 lat * SGD_DEGREES_TO_RADIANS, elev));
330
331         // search stations in range
332         int num_stat = current_commlist->FindByPos(lon, lat, elev, 40.0, &atc_stations);
333         if (num_stat != 0) {
334                 map<atcdata, bool> uniq;
335                 // fill map (sorts by distance and removes duplicates)
336                 comm_list_iterator itr = atc_stations.begin();
337                 for (; itr != atc_stations.end(); ++itr) {
338                         Point3D station = Point3D(itr->x, itr->y, itr->z);
339                         double distance = aircraft.distance3Dsquared(station);
340                         uniq[atcdata(itr->ident, itr->name, distance)] = true;
341                 }
342                 // create button per map entry (modified copy of <button-template>)
343                 map<atcdata, bool>::iterator uit = uniq.begin();
344                 for (int n = 0; uit != uniq.end() && n < 6; ++uit, ++n) { // max 6 buttons
345                         SGPropertyNode *entry = button_group->getNode("button", n, true);
346                         copyProperties(button_group->getNode("button-template", true), entry);
347                         entry->removeChildren("hide", false);
348                         entry->setStringValue("legend", uit->first.id.c_str());
349                         entry->setStringValue("binding[0]/value", uit->first.id.c_str());
350                 }
351         }
352
353         // (un)hide message saying no things in range
354         SGPropertyNode_ptr range_error = getNamedNode(dlg, "no-atc-in-range");
355         range_error->setBoolValue("hide", num_stat);
356
357         _gui->showDialog(dialog_name);
358 }
359
360 void FGATCDialog::FreqDisplay(string& ident) {
361         const char *dialog_name = "atc-freq-display";
362         SGPropertyNode_ptr dlg = _gui->getDialog(dialog_name);
363         if (!dlg)
364                 return;
365
366         _gui->closeDialog(dialog_name);
367
368         SGPropertyNode_ptr freq_group = getNamedNode(dlg, "frequency-list");
369         // remove all frequency entries
370         freq_group->removeChildren("group", false);
371
372         atcUppercase(ident);
373         string label;
374
375         const FGAirport *a = fgFindAirportID(ident);
376         if (!a) {
377                 label = "Airport " + ident + " not found in database.";
378                 mkDialog(label.c_str());
379                 return;
380         }
381
382         // set title
383         label = ident + " Frequencies";
384         dlg->setStringValue("text/label", label.c_str());
385
386         int n = 0;      // Number of ATC frequencies at this airport
387
388         comm_list_type stations;
389         int found = current_commlist->FindByPos(a->getLongitude(), a->getLatitude(), a->getElevation(), 20.0, &stations);
390         if(found) {
391                 ostringstream ostr;
392                 comm_list_iterator itr = stations.begin();
393                 for (n = 0; itr != stations.end(); ++itr) {
394                         if(itr->ident != ident)
395                                 continue;
396
397                         if(itr->type == INVALID)
398                                 continue;
399
400                         // add frequency line (modified copy of <group-template>)
401                         SGPropertyNode *entry = freq_group->getNode("group", n, true);
402                         copyProperties(freq_group->getNode("group-template", true), entry);
403                         entry->removeChildren("hide", false);
404
405                         ostr << itr->type;
406                         entry->setStringValue("text[0]/label", ostr.str().c_str());
407
408                         char buf[8];
409                         snprintf(buf, 8, "%.2f", (itr->freq / 100.0));  // Convert from KHz to MHz
410                         if(buf[5] == '3') buf[5] = '2';
411                         if(buf[5] == '8') buf[5] = '7';
412                         buf[7] = '\0';
413
414                         entry->setStringValue("text[1]/label", buf);
415
416                         ostr.seekp(0);
417                         n++;
418                 }
419         }
420         if(n == 0) {
421                 label = "No frequencies found for airport " + ident;
422                 mkDialog(label.c_str());
423                 return;
424         }
425
426         _gui->showDialog(dialog_name);
427 }
428