]> git.mxchange.org Git - flightgear.git/blob - src/ATC/transmissionlist.cxx
First draft of work by Alexander Kappes to add dynamically driven menu entries to...
[flightgear.git] / src / ATC / transmissionlist.cxx
1 // transmissionlist.cxx -- transmission management class\r
2 //\r
3 // Written by Alexander Kappes, started March 2002.\r
4 // Based on navlist.cxx by Curtis Olson, started April 2000.\r
5 //\r
6 // Copyright (C) 2000  Curtis L. Olson - curt@flightgear.org\r
7 //\r
8 // This program is free software; you can redistribute it and/or\r
9 // modify it under the terms of the GNU General Public License as\r
10 // published by the Free Software Foundation; either version 2 of the\r
11 // License, or (at your option) any later version.\r
12 //\r
13 // This program is distributed in the hope that it will be useful, but\r
14 // WITHOUT ANY WARRANTY; without even the implied warranty of\r
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU\r
16 // General Public License for more details.\r
17 //\r
18 // You should have received a copy of the GNU General Public License\r
19 // along with this program; if not, write to the Free Software\r
20 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.\r
21 //\r
22 // $Id$\r
23 \r
24 \r
25 #ifdef HAVE_CONFIG_H\r
26 #  include <config.h>\r
27 #endif\r
28 \r
29 #include <simgear/debug/logstream.hxx>\r
30 #include <simgear/misc/sgstream.hxx>\r
31 #include <simgear/math/sg_geodesy.hxx>\r
32 \r
33 #include "transmissionlist.hxx"\r
34 \r
35 #include <GUI/gui.h>\r
36 \r
37 static puDialogBox  *ATCMenuBox = 0;\r
38 static puFrame      *ATCMenuFrame = 0;\r
39 static puText       *ATCMenuBoxMessage = 0;\r
40 \r
41 FGTransmissionList *current_transmissionlist;\r
42 \r
43 \r
44 // Constructor\r
45 FGTransmissionList::FGTransmissionList( void ) {\r
46 }\r
47 \r
48 \r
49 // Destructor\r
50 FGTransmissionList::~FGTransmissionList( void ) {\r
51 }\r
52 \r
53 /*\r
54 // ============================================================================\r
55 // init menu window\r
56 // ============================================================================\r
57 void mkATCMenuInit (void)\r
58 {\r
59   int dx = 400;\r
60   int dy = 100;\r
61   int y = (fgGetInt("/sim/startup/ysize") - 10 - dy);\r
62   ATCMenuBox = new puDialogBox (10, y);\r
63   {\r
64     ATCMenuFrame = new puFrame (0,0,400,100);\r
65     ATCMenuBoxMessage  =  new puText         (10, 70);\r
66     ATCMenuBoxMessage  -> setLabel           ("");\r
67   }\r
68   fgSetBool("/sim/atc/menu",false);\r
69   fgSetBool("/sim/atc/opt1",false);\r
70   fgSetBool("/sim/atc/opt2",false);\r
71   fgSetBool("/sim/atc/opt3",false);\r
72   fgSetBool("/sim/atc/opt4",false);\r
73   fgSetBool("/sim/atc/opt5",false);\r
74   fgSetBool("/sim/atc/opt6",false);\r
75   fgSetBool("/sim/atc/opt7",false);\r
76   fgSetBool("/sim/atc/opt8",false);\r
77   fgSetBool("/sim/atc/opt9",false);\r
78   fgSetBool("/sim/atc/opt0",false);\r
79 }\r
80 \r
81 // ATC Menu Message Box\r
82 void mkATCMenu ( const char *txt )\r
83 {\r
84   ATCMenuBoxMessage  =  new puText   (10, 70);\r
85   ATCMenuBoxMessage->setLabel( txt );\r
86 \r
87   FG_PUSH_PUI_DIALOG( ATCMenuBox );\r
88 }\r
89 */\r
90 \r
91 // load default.transmissions\r
92 bool FGTransmissionList::init( SGPath path ) {\r
93     FGTransmission a;\r
94 \r
95     transmissionlist_station.erase( transmissionlist_station.begin(), transmissionlist_station.end() );\r
96 \r
97     sg_gzifstream in( path.str() );\r
98     if ( !in.is_open() ) {\r
99         SG_LOG( SG_GENERAL, SG_ALERT, "Cannot open file: " << path.str() );\r
100         exit(-1);\r
101     }\r
102 \r
103     // read in each line of the file\r
104 \r
105     // in >> skipeol;\r
106     // in >> skipcomment;\r
107 \r
108 #ifdef __MWERKS__\r
109 \r
110     char c = 0;\r
111     while ( in.get(c) && c != '\0' ) {\r
112         in.putback(c);\r
113         in >> a;\r
114         if ( a.get_type() != '[' ) {\r
115             transmissionlist_code[a.get_station()].push_back(a);\r
116         }\r
117         in >> skipcomment;\r
118     }\r
119 \r
120 #else\r
121 \r
122     double min = 100000;\r
123     double max = 0;\r
124 \r
125     while ( ! in.eof() ) {\r
126         in >> a;\r
127         transmissionlist_station[a.get_station()].push_back(a);\r
128         \r
129         in >> skipcomment;\r
130 \r
131         if ( a.get_station() < min ) {\r
132           min = a.get_station();\r
133         }\r
134         if ( a.get_station() > max ) {\r
135           max = a.get_station();\r
136         }\r
137         cout << a.get_station() << " " << a.get_code().c1 << " " << a.get_code().c2 << " "\r
138              << a.get_code().c3 << " " << a.get_transtext() \r
139              << " " << a.get_menutext() << endl;\r
140     }\r
141     \r
142 #endif\r
143 \r
144     // init ATC menu\r
145     fgSetBool("/sim/atc/menu",false);\r
146 \r
147     return true;\r
148 }\r
149 \r
150 // query the database for the specified station type; \r
151 // for station see FlightGear/ATC/default.transmissions\r
152 bool FGTransmissionList::query_station( const int &station, FGTransmission *t,\r
153                                         int max_trans, int &num_trans ) \r
154 {\r
155   transmission_list_type     tmissions = transmissionlist_station[station];\r
156   transmission_list_iterator current   = tmissions.begin();\r
157   transmission_list_iterator last      = tmissions.end();\r
158 \r
159   for ( ; current != last ; ++current ) {\r
160     if (num_trans < max_trans) {\r
161       t[num_trans] = *current;\r
162       num_trans += 1;\r
163     }\r
164     else {\r
165       cout << "Transmissionlist error: Too many transmissions" << endl; \r
166     }\r
167   }\r
168 \r
169   if ( num_trans != 0 ) return true;\r
170   else {\r
171     cout << "No transmission with station " << station << "found." << endl;\r
172     string empty;\r
173     return false;\r
174   }\r
175 }\r
176 \r
177 string FGTransmissionList::gen_text(const int &station, const TransCode code, \r
178                                     const TransPar &tpars, const bool ttext )\r
179 {\r
180   const int cmax = 100;\r
181   string message;\r
182   char tag[4];\r
183   char crej = '@';\r
184   char mes[cmax];\r
185   char dum[cmax];\r
186   char buf[10];\r
187   char *pos;\r
188   int len;\r
189   FGTransmission t;\r
190 \r
191   //  if (current_transmissionlist->query_station( station, &t ) ) {  \r
192   transmission_list_type     tmissions = transmissionlist_station[station];\r
193   transmission_list_iterator current   = tmissions.begin();\r
194   transmission_list_iterator last      = tmissions.end();\r
195   \r
196   for ( ; current != last ; ++current ) {\r
197     if ( current->get_code().c1 == code.c1 &&  \r
198          current->get_code().c2 == code.c2 &&\r
199          current->get_code().c3 == code.c3 ) {\r
200       \r
201       if ( ttext ) message = current->get_transtext();\r
202       else message = current->get_menutext();\r
203       strcpy( &mes[0], message.c_str() ); \r
204       \r
205       while ( strchr(&mes[0], crej) != NULL  ) {\r
206         pos = strchr( &mes[0], crej );\r
207         bcopy(pos, &tag, 3);\r
208         tag[3] = '\0';\r
209         int i;\r
210         len = 0;\r
211         for ( i=0; i<cmax; i++ ) {\r
212           if ( mes[i] == crej ) {\r
213             len = i; \r
214             break;\r
215           }\r
216         }\r
217         strncpy( &dum[0], &mes[0], len );\r
218         dum[len] = '\0';\r
219         \r
220         if ( strcmp ( tag, "@ST" ) == 0 )\r
221           strcat( &dum[0], tpars.station.c_str() );\r
222         else if ( strcmp ( tag, "@AP" ) == 0 )\r
223           strcat( &dum[0], tpars.airport.c_str() );\r
224         else if ( strcmp ( tag, "@CS" ) == 0 ) \r
225           strcat( &dum[0], tpars.callsign.c_str() );\r
226         else if ( strcmp ( tag, "@TD" ) == 0 ) {\r
227           if ( tpars.tdir == 1 ) {\r
228             char buf[] = "left";\r
229             strcat( &dum[0], &buf[0] );\r
230           }\r
231           else {\r
232             char buf[] = "right";\r
233             strcat( &dum[0], &buf[0] );\r
234           }\r
235         }\r
236         else if ( strcmp ( tag, "@HE" ) == 0 ) {\r
237           char buf[10];\r
238           sprintf( buf, "%i", (int)(tpars.heading) );\r
239           strcat( &dum[0], &buf[0] );\r
240         }\r
241         else if ( strcmp ( tag, "@VD" ) == 0 ) {\r
242           if ( tpars.VDir == 1 ) {\r
243             char buf[] = "Descent and maintain";\r
244             strcat( &dum[0], &buf[0] );\r
245           }\r
246           else if ( tpars.VDir == 2 ) {\r
247             char buf[] = "Maintain";\r
248             strcat( &dum[0], &buf[0] );\r
249           }\r
250           else if ( tpars.VDir == 3 ) {\r
251             char buf[] = "Climb and maintain";\r
252             strcat( &dum[0], &buf[0] );\r
253           }  \r
254         }\r
255         else if ( strcmp ( tag, "@AL" ) == 0 ) {\r
256           char buf[10];\r
257           sprintf( buf, "%i", (int)(tpars.alt) );\r
258           strcat( &dum[0], &buf[0] );\r
259         }\r
260         else if ( strcmp ( tag, "@MI" ) == 0 ) {\r
261           char buf[10];\r
262           sprintf( buf, "%3.1f", tpars.miles );\r
263           strcat( &dum[0], &buf[0] );\r
264         }\r
265         else if ( strcmp ( tag, "@FR" ) == 0 ) {\r
266           char buf[10];\r
267           sprintf( buf, "%6.2f", tpars.freq );\r
268           strcat( &dum[0], &buf[0] );\r
269         }\r
270         else if ( strcmp ( tag, "@RW" ) == 0 )\r
271           strcat( &dum[0], tpars.runway.c_str() );\r
272         else {\r
273           cout << "Tag " << tag << " not found" << endl;\r
274           break;\r
275         }\r
276         strcat( &dum[0], &mes[len+3] );\r
277         strcpy( &mes[0], &dum[0] );\r
278       }\r
279 \r
280       //cout << mes  << endl;  \r
281       break;\r
282     }\r
283   }\r
284   if ( mes != "" ) return mes;\r
285   else return "No transmission found";\r
286 }\r
287 \r
288 \r