]> git.mxchange.org Git - flightgear.git/blob - src/ATC/transmissionlist.cxx
Moved some of the low level scene graph construction code over to simgear.
[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 // Constructor
49 FGTransmissionList::FGTransmissionList( void ) {
50 }
51
52
53 // Destructor
54 FGTransmissionList::~FGTransmissionList( void ) {
55 }
56
57 /*
58 // ============================================================================
59 // init menu window
60 // ============================================================================
61 void mkATCMenuInit (void)
62 {
63   int dx = 400;
64   int dy = 100;
65   int y = (fgGetInt("/sim/startup/ysize") - 10 - dy);
66   ATCMenuBox = new puDialogBox (10, y);
67   {
68     ATCMenuFrame = new puFrame (0,0,400,100);
69     ATCMenuBoxMessage  =  new puText         (10, 70);
70     ATCMenuBoxMessage  -> setLabel           ("");
71   }
72   fgSetBool("/sim/atc/menu",false);
73   fgSetBool("/sim/atc/opt1",false);
74   fgSetBool("/sim/atc/opt2",false);
75   fgSetBool("/sim/atc/opt3",false);
76   fgSetBool("/sim/atc/opt4",false);
77   fgSetBool("/sim/atc/opt5",false);
78   fgSetBool("/sim/atc/opt6",false);
79   fgSetBool("/sim/atc/opt7",false);
80   fgSetBool("/sim/atc/opt8",false);
81   fgSetBool("/sim/atc/opt9",false);
82   fgSetBool("/sim/atc/opt0",false);
83 }
84
85 // ATC Menu Message Box
86 void mkATCMenu ( const char *txt )
87 {
88   ATCMenuBoxMessage  =  new puText   (10, 70);
89   ATCMenuBoxMessage->setLabel( txt );
90
91   FG_PUSH_PUI_DIALOG( ATCMenuBox );
92 }
93 */
94
95 // load default.transmissions
96 bool FGTransmissionList::init( SGPath path ) {
97     FGTransmission a;
98
99     transmissionlist_station.erase( transmissionlist_station.begin(), transmissionlist_station.end() );
100
101     sg_gzifstream in( path.str() );
102     if ( !in.is_open() ) {
103         SG_LOG( SG_GENERAL, SG_ALERT, "Cannot open file: " << path.str() );
104         exit(-1);
105     }
106
107     // read in each line of the file
108
109     // in >> skipeol;
110     // in >> skipcomment;
111
112 #ifdef __MWERKS__
113
114     char c = 0;
115     while ( in.get(c) && c != '\0' ) {
116         in.putback(c);
117         in >> a;
118         if ( a.get_type() != '[' ) {
119             transmissionlist_code[a.get_station()].push_back(a);
120         }
121         in >> skipcomment;
122     }
123
124 #else
125
126     double min = 100000;
127     double max = 0;
128
129     while ( ! in.eof() ) {
130         in >> a;
131         transmissionlist_station[a.get_station()].push_back(a);
132         
133         in >> skipcomment;
134
135         if ( a.get_station() < min ) {
136           min = a.get_station();
137         }
138         if ( a.get_station() > max ) {
139           max = a.get_station();
140         }
141         cout << a.get_station() << " " << a.get_code().c1 << " " << a.get_code().c2 << " "
142              << a.get_code().c3 << " " << a.get_transtext() 
143              << " " << a.get_menutext() << endl;
144     }
145     
146 #endif
147
148     // init ATC menu
149     fgSetBool("/sim/atc/menu",false);
150
151     return true;
152 }
153
154 // query the database for the specified station type; 
155 // for station see FlightGear/ATC/default.transmissions
156 bool FGTransmissionList::query_station( const int &station, FGTransmission *t,
157                                         int max_trans, int &num_trans ) 
158 {
159   transmission_list_type     tmissions = transmissionlist_station[station];
160   transmission_list_iterator current   = tmissions.begin();
161   transmission_list_iterator last      = tmissions.end();
162
163   for ( ; current != last ; ++current ) {
164     if (num_trans < max_trans) {
165       t[num_trans] = *current;
166       num_trans += 1;
167     }
168     else {
169       cout << "Transmissionlist error: Too many transmissions" << endl; 
170     }
171   }
172
173   if ( num_trans != 0 ) return true;
174   else {
175     cout << "No transmission with station " << station << "found." << endl;
176     string empty;
177     return false;
178   }
179 }
180
181 string FGTransmissionList::gen_text(const int &station, const TransCode code, 
182                                     const TransPar &tpars, const bool ttext )
183 {
184   const int cmax = 100;
185   string message;
186   char tag[4];
187   char crej = '@';
188   char mes[cmax];
189   char dum[cmax];
190   //char buf[10];
191   char *pos;
192   int len;
193   FGTransmission t;
194
195   //  if (current_transmissionlist->query_station( station, &t ) ) {  
196   transmission_list_type     tmissions = transmissionlist_station[station];
197   transmission_list_iterator current   = tmissions.begin();
198   transmission_list_iterator last      = tmissions.end();
199   
200   for ( ; current != last ; ++current ) {
201     if ( current->get_code().c1 == code.c1 &&  
202          current->get_code().c2 == code.c2 &&
203          current->get_code().c3 == code.c3 ) {
204       
205       if ( ttext ) message = current->get_transtext();
206       else message = current->get_menutext();
207       strcpy( &mes[0], message.c_str() ); 
208       
209       while ( strchr(&mes[0], crej) != NULL  ) {
210         pos = strchr( &mes[0], crej );
211         bcopy(pos, &tag[0], 3);
212         tag[3] = '\0';
213         int i;
214         len = 0;
215         for ( i=0; i<cmax; i++ ) {
216           if ( mes[i] == crej ) {
217             len = i; 
218             break;
219           }
220         }
221         strncpy( &dum[0], &mes[0], len );
222         dum[len] = '\0';
223         
224         if ( strcmp ( tag, "@ST" ) == 0 )
225           strcat( &dum[0], tpars.station.c_str() );
226         else if ( strcmp ( tag, "@AP" ) == 0 )
227           strcat( &dum[0], tpars.airport.c_str() );
228         else if ( strcmp ( tag, "@CS" ) == 0 ) 
229           strcat( &dum[0], tpars.callsign.c_str() );
230         else if ( strcmp ( tag, "@TD" ) == 0 ) {
231           if ( tpars.tdir == 1 ) {
232             char buf[] = "left";
233             strcat( &dum[0], &buf[0] );
234           }
235           else {
236             char buf[] = "right";
237             strcat( &dum[0], &buf[0] );
238           }
239         }
240         else if ( strcmp ( tag, "@HE" ) == 0 ) {
241           char buf[10];
242           sprintf( buf, "%i", (int)(tpars.heading) );
243           strcat( &dum[0], &buf[0] );
244         }
245         else if ( strcmp ( tag, "@VD" ) == 0 ) {
246           if ( tpars.VDir == 1 ) {
247             char buf[] = "Descent and maintain";
248             strcat( &dum[0], &buf[0] );
249           }
250           else if ( tpars.VDir == 2 ) {
251             char buf[] = "Maintain";
252             strcat( &dum[0], &buf[0] );
253           }
254           else if ( tpars.VDir == 3 ) {
255             char buf[] = "Climb and maintain";
256             strcat( &dum[0], &buf[0] );
257           }  
258         }
259         else if ( strcmp ( tag, "@AL" ) == 0 ) {
260           char buf[10];
261           sprintf( buf, "%i", (int)(tpars.alt) );
262           strcat( &dum[0], &buf[0] );
263         }
264         else if ( strcmp ( tag, "@MI" ) == 0 ) {
265           char buf[10];
266           sprintf( buf, "%3.1f", tpars.miles );
267           strcat( &dum[0], &buf[0] );
268         }
269         else if ( strcmp ( tag, "@FR" ) == 0 ) {
270           char buf[10];
271           sprintf( buf, "%6.2f", tpars.freq );
272           strcat( &dum[0], &buf[0] );
273         }
274         else if ( strcmp ( tag, "@RW" ) == 0 )
275           strcat( &dum[0], tpars.runway.c_str() );
276         else {
277           cout << "Tag " << tag << " not found" << endl;
278           break;
279         }
280         strcat( &dum[0], &mes[len+3] );
281         strcpy( &mes[0], &dum[0] );
282       }
283
284       //cout << mes  << endl;  
285       break;
286     }
287   }
288   if ( mes != "" ) return mes;
289   else return "No transmission found";
290 }
291
292