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