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