]> git.mxchange.org Git - flightgear.git/blob - src/ATCDCL/transmissionlist.cxx
A generic, configurable autobrake system. Not fully features yet, but works quite...
[flightgear.git] / src / ATCDCL / transmissionlist.cxx
1 // transmissionlist.cxx -- transmission management class
2 //
3 // Written by Alexander Kappes, started March 2002.
4 // Based on navlist.cxx by Curtis Olson, started April 2000.
5 //
6 // Copyright (C) 2000  Curtis L. Olson - http://www.flightgear.org/~curt
7 //
8 // This program is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU General Public License as
10 // published by the Free Software Foundation; either version 2 of the
11 // License, or (at your option) any later version.
12 //
13 // This program is distributed in the hope that it will be useful, but
14 // WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16 // General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21 //
22 // $Id$
23
24
25 #ifdef HAVE_CONFIG_H
26 #  include <config.h>
27 #endif
28
29 #ifdef HAVE_STRINGS_H
30 #  include <strings.h>   // bcopy()
31 #else
32 #  include <string.h>    // MSVC doesn't have strings.h
33 #endif
34
35
36 #include <simgear/debug/logstream.hxx>
37 #include <simgear/misc/sgstream.hxx>
38 #include <simgear/math/sg_geodesy.hxx>
39
40 #include "transmissionlist.hxx"
41
42 #include <GUI/gui.h>
43
44
45 FGTransmissionList *current_transmissionlist;
46
47
48 FGTransmissionList::FGTransmissionList( void ) {
49 }
50
51
52 FGTransmissionList::~FGTransmissionList( void ) {
53 }
54
55
56 // load default.transmissions
57 bool FGTransmissionList::init( const SGPath& path ) {
58     FGTransmission a;
59
60     transmissionlist_station.erase( transmissionlist_station.begin(), transmissionlist_station.end() );
61
62     sg_gzifstream in( path.str() );
63     if ( !in.is_open() ) {
64         SG_LOG( SG_GENERAL, SG_ALERT, "Cannot open file: " << path.str() );
65         exit(-1);
66     }
67
68     // read in each line of the file
69
70     // in >> skipeol;
71     // in >> skipcomment;
72
73     double min = 100000;
74     double max = 0;
75
76     while ( ! in.eof() ) {
77         in >> a;
78         transmissionlist_station[a.get_station()].push_back(a);
79
80         in >> skipcomment;
81
82         if ( a.get_station() < min ) {
83             min = a.get_station();
84         }
85         if ( a.get_station() > max ) {
86             max = a.get_station();
87         }
88
89         /*
90         cout << a.get_station() << " " << a.get_code().c1 << " " << a.get_code().c2 << " "
91              << a.get_code().c3 << " " << a.get_transtext() 
92              << " " << a.get_menutext() << endl;
93         */
94     }
95  
96     // init ATC menu
97     fgSetBool("/sim/atc/menu",false);
98
99     return true;
100 }
101
102 // query the database for the specified station type; 
103 // for station see FlightGear/ATC/default.transmissions
104 bool FGTransmissionList::query_station( const atc_type &station, FGTransmission *t,
105                                         int max_trans, int &num_trans ) 
106 {
107   transmission_list_type     tmissions = transmissionlist_station[station];
108   transmission_list_iterator current   = tmissions.begin();
109   transmission_list_iterator last      = tmissions.end();
110
111   for ( ; current != last ; ++current ) {
112     if (num_trans < max_trans) {
113       t[num_trans] = *current;
114       num_trans += 1;
115     }
116     else {
117       SG_LOG(SG_GENERAL, SG_WARN, "Transmissionlist error: Too many transmissions"); 
118     }
119   }
120
121   if ( num_trans != 0 ) return true;
122   else {
123     SG_LOG(SG_GENERAL, SG_WARN, "No transmission with station " << station << "found.");
124     string empty;
125     return false;
126   }
127 }
128
129 string FGTransmissionList::gen_text(const atc_type &station, const TransCode code, 
130                                     const TransPar &tpars, const bool ttext )
131 {
132         const int cmax = 300;
133         string message;
134         char tag[4];
135         char crej = '@';
136         char mes[cmax];
137         char dum[cmax];
138         //char buf[10];
139         char *pos;
140         int len;
141         FGTransmission t;
142         
143         //  if (current_transmissionlist->query_station( station, &t ) ) { 
144         transmission_list_type     tmissions = transmissionlist_station[station];
145         transmission_list_iterator current   = tmissions.begin();
146         transmission_list_iterator last      = tmissions.end();
147         
148         for ( ; current != last ; ++current ) {
149                 if ( current->get_code().c1 == code.c1 &&  
150                         current->get_code().c2 == code.c2 &&
151                     current->get_code().c3 == code.c3 ) {
152                         
153                         if ( ttext ) message = current->get_transtext();
154                         else message = current->get_menutext();
155                         strcpy( &mes[0], message.c_str() ); 
156                         
157                         // Replace all the '@' parameters with the actual text.
158                         int check = 0;  // If mes gets overflowed the while loop can go infinite
159                         while ( strchr(&mes[0], crej) != NULL  ) {      // ie. loop until no more occurances of crej ('@') found
160                                 pos = strchr( &mes[0], crej );
161                                 memmove(&tag[0], pos, 3);
162                                 tag[3] = '\0';
163                                 int i;
164                                 len = 0;
165                                 for ( i=0; i<cmax; i++ ) {
166                                         if ( mes[i] == crej ) {
167                                                 len = i; 
168                                                 break;
169                                         }
170                                 }
171                                 strncpy( &dum[0], &mes[0], len );
172                                 dum[len] = '\0';
173                                 
174                                 if ( strcmp ( tag, "@ST" ) == 0 )
175                                         strcat( &dum[0], tpars.station.c_str() );
176                                 else if ( strcmp ( tag, "@AP" ) == 0 )
177                                         strcat( &dum[0], tpars.airport.c_str() );
178                                 else if ( strcmp ( tag, "@CS" ) == 0 ) 
179                                         strcat( &dum[0], tpars.callsign.c_str() );
180                                 else if ( strcmp ( tag, "@TD" ) == 0 ) {
181                                         if ( tpars.tdir == 1 ) {
182                                                 char buf[] = "left";
183                                                 strcat( &dum[0], &buf[0] );
184                                         }
185                                         else {
186                                                 char buf[] = "right";
187                                                 strcat( &dum[0], &buf[0] );
188                                         }
189                                 }
190                                 else if ( strcmp ( tag, "@HE" ) == 0 ) {
191                                         char buf[10];
192                                         sprintf( buf, "%i", (int)(tpars.heading) );
193                                         strcat( &dum[0], &buf[0] );
194                                 }
195                                 else if ( strcmp ( tag, "@VD" ) == 0 ) {
196                                         if ( tpars.VDir == 1 ) {
197                                                 char buf[] = "Descend and maintain";
198                                                 strcat( &dum[0], &buf[0] );
199                                         }
200                                         else if ( tpars.VDir == 2 ) {
201                                                 char buf[] = "Maintain";
202                                                 strcat( &dum[0], &buf[0] );
203                                         }
204                                         else if ( tpars.VDir == 3 ) {
205                                                 char buf[] = "Climb and maintain";
206                                                 strcat( &dum[0], &buf[0] );
207                                         }  
208                                 }
209                                 else if ( strcmp ( tag, "@AL" ) == 0 ) {
210                                         char buf[10];
211                                         sprintf( buf, "%i", (int)(tpars.alt) );
212                                         strcat( &dum[0], &buf[0] );
213                                 }
214                                 else if ( strcmp ( tag, "@MI" ) == 0 ) {
215                                         char buf[10];
216                                         sprintf( buf, "%3.1f", tpars.miles );
217                                         strcat( &dum[0], &buf[0] );
218                                 }
219                                 else if ( strcmp ( tag, "@FR" ) == 0 ) {
220                                         char buf[10];
221                                         sprintf( buf, "%6.2f", tpars.freq );
222                                         strcat( &dum[0], &buf[0] );
223                                 }
224                                 else if ( strcmp ( tag, "@RW" ) == 0 )
225                                         strcat( &dum[0], tpars.runway.c_str() );
226                                 else {
227                                         SG_LOG(SG_GENERAL, SG_WARN, "Tag " << tag << " not found");
228                                         break;
229                                 }
230                                 strcat( &dum[0], &mes[len+3] );
231                                 strcpy( &mes[0], &dum[0] );
232                                 
233                                 ++check;
234                                 if(check > 10) {
235                                         SG_LOG(SG_GENERAL, SG_WARN, "WARNING: Possibly endless loop terminated in FGTransmissionlist::gen_text(...)"); 
236                                         break;
237                                 }
238                         }
239                         
240                         //cout << mes  << endl;  
241                         break;
242                 }
243         }
244         return mes[0] ? mes : "No transmission found";
245 }
246
247