]> git.mxchange.org Git - flightgear.git/blob - src/ATC/transmissionlist.cxx
Enhancements to AI plane transmissions
[flightgear.git] / src / ATC / 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 - curt@flightgear.org
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., 675 Mass Ave, Cambridge, MA 02139, 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( 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 #ifdef __MWERKS__
74
75     char c = 0;
76     while ( in.get(c) && c != '\0' ) {
77         in.putback(c);
78         in >> a;
79         if ( a.get_type() != '[' ) {
80             transmissionlist_code[a.get_station()].push_back(a);
81         }
82         in >> skipcomment;
83     }
84
85 #else
86
87     double min = 100000;
88     double max = 0;
89
90     while ( ! in.eof() ) {
91         in >> a;
92         transmissionlist_station[a.get_station()].push_back(a);
93         
94         in >> skipcomment;
95
96         if ( a.get_station() < min ) {
97           min = a.get_station();
98         }
99         if ( a.get_station() > max ) {
100           max = a.get_station();
101         }
102         cout << a.get_station() << " " << a.get_code().c1 << " " << a.get_code().c2 << " "
103              << a.get_code().c3 << " " << a.get_transtext() 
104              << " " << a.get_menutext() << endl;
105     }
106     
107 #endif
108
109     // init ATC menu
110     fgSetBool("/sim/atc/menu",false);
111
112     return true;
113 }
114
115 // query the database for the specified station type; 
116 // for station see FlightGear/ATC/default.transmissions
117 bool FGTransmissionList::query_station( const int &station, FGTransmission *t,
118                                         int max_trans, int &num_trans ) 
119 {
120   transmission_list_type     tmissions = transmissionlist_station[station];
121   transmission_list_iterator current   = tmissions.begin();
122   transmission_list_iterator last      = tmissions.end();
123
124   for ( ; current != last ; ++current ) {
125     if (num_trans < max_trans) {
126       t[num_trans] = *current;
127       num_trans += 1;
128     }
129     else {
130       cout << "Transmissionlist error: Too many transmissions" << endl; 
131     }
132   }
133
134   if ( num_trans != 0 ) return true;
135   else {
136     cout << "No transmission with station " << station << "found." << endl;
137     string empty;
138     return false;
139   }
140 }
141
142 string FGTransmissionList::gen_text(const int &station, const TransCode code, 
143                                     const TransPar &tpars, const bool ttext )
144 {
145         const int cmax = 300;
146         string message;
147         char tag[4];
148         char crej = '@';
149         char mes[cmax];
150         char dum[cmax];
151         //char buf[10];
152         char *pos;
153         int len;
154         FGTransmission t;
155         
156         //  if (current_transmissionlist->query_station( station, &t ) ) { 
157         transmission_list_type     tmissions = transmissionlist_station[station];
158         transmission_list_iterator current   = tmissions.begin();
159         transmission_list_iterator last      = tmissions.end();
160         
161         for ( ; current != last ; ++current ) {
162                 if ( current->get_code().c1 == code.c1 &&  
163                         current->get_code().c2 == code.c2 &&
164                 current->get_code().c3 == code.c3 ) {
165                         
166                         if ( ttext ) message = current->get_transtext();
167                         else message = current->get_menutext();
168                         strcpy( &mes[0], message.c_str() ); 
169                         
170                         int check = 0;  // If mes gets overflowed the while loop can go infinite
171                         while ( strchr(&mes[0], crej) != NULL  ) {
172                                 pos = strchr( &mes[0], crej );
173                                 bcopy(pos, &tag[0], 3);
174                                 tag[3] = '\0';
175                                 int i;
176                                 len = 0;
177                                 for ( i=0; i<cmax; i++ ) {
178                                         if ( mes[i] == crej ) {
179                                                 len = i; 
180                                                 break;
181                                         }
182                                 }
183                                 strncpy( &dum[0], &mes[0], len );
184                                 dum[len] = '\0';
185                                 
186                                 if ( strcmp ( tag, "@ST" ) == 0 )
187                                         strcat( &dum[0], tpars.station.c_str() );
188                                 else if ( strcmp ( tag, "@AP" ) == 0 )
189                                         strcat( &dum[0], tpars.airport.c_str() );
190                                 else if ( strcmp ( tag, "@CS" ) == 0 ) 
191                                         strcat( &dum[0], tpars.callsign.c_str() );
192                                 else if ( strcmp ( tag, "@TD" ) == 0 ) {
193                                         if ( tpars.tdir == 1 ) {
194                                                 char buf[] = "left";
195                                                 strcat( &dum[0], &buf[0] );
196                                         }
197                                         else {
198                                                 char buf[] = "right";
199                                                 strcat( &dum[0], &buf[0] );
200                                         }
201                                 }
202                                 else if ( strcmp ( tag, "@HE" ) == 0 ) {
203                                         char buf[10];
204                                         sprintf( buf, "%i", (int)(tpars.heading) );
205                                         strcat( &dum[0], &buf[0] );
206                                 }
207                                 else if ( strcmp ( tag, "@VD" ) == 0 ) {
208                                         if ( tpars.VDir == 1 ) {
209                                                 char buf[] = "Descend and maintain";
210                                                 strcat( &dum[0], &buf[0] );
211                                         }
212                                         else if ( tpars.VDir == 2 ) {
213                                                 char buf[] = "Maintain";
214                                                 strcat( &dum[0], &buf[0] );
215                                         }
216                                         else if ( tpars.VDir == 3 ) {
217                                                 char buf[] = "Climb and maintain";
218                                                 strcat( &dum[0], &buf[0] );
219                                         }  
220                                 }
221                                 else if ( strcmp ( tag, "@AL" ) == 0 ) {
222                                         char buf[10];
223                                         sprintf( buf, "%i", (int)(tpars.alt) );
224                                         strcat( &dum[0], &buf[0] );
225                                 }
226                                 else if ( strcmp ( tag, "@MI" ) == 0 ) {
227                                         char buf[10];
228                                         sprintf( buf, "%3.1f", tpars.miles );
229                                         strcat( &dum[0], &buf[0] );
230                                 }
231                                 else if ( strcmp ( tag, "@FR" ) == 0 ) {
232                                         char buf[10];
233                                         sprintf( buf, "%6.2f", tpars.freq );
234                                         strcat( &dum[0], &buf[0] );
235                                 }
236                                 else if ( strcmp ( tag, "@RW" ) == 0 )
237                                         strcat( &dum[0], tpars.runway.c_str() );
238                                 else {
239                                         cout << "Tag " << tag << " not found" << endl;
240                                         break;
241                                 }
242                                 strcat( &dum[0], &mes[len+3] );
243                                 strcpy( &mes[0], &dum[0] );
244                                 
245                                 ++check;
246                                 if(check > 10) {
247                                         SG_LOG(SG_ATC, SG_WARN, "WARNING: Possibly endless loop terminated in FGTransmissionlist::gen_text(...)"); 
248                                         break;
249                                 }
250                         }
251                         
252                         //cout << mes  << endl;  
253                         break;
254                 }
255         }
256         if ( mes != "" ) return mes;
257         else return "No transmission found";
258 }
259
260