]> git.mxchange.org Git - flightgear.git/blob - src/ATC/atis.cxx
Moved random ground cover object management code (userdata.[ch]xx) over
[flightgear.git] / src / ATC / atis.cxx
1 // atis.cxx - routines to generate the ATIS info string
2 // This is the implementation of the FGATIS class
3 //
4 // Written by David Luff, started October 2001.
5 //
6 // Copyright (C) 2001  David C Luff - david.luff@nottingham.ac.uk
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
23 #ifdef HAVE_CONFIG_H
24 #  include <config.h>
25 #endif
26
27 #include <simgear/compiler.h>
28
29 #include <stdlib.h>     // atoi()
30 #include <stdio.h>      // sprintf
31 #include <string>
32 SG_USING_STD(string);
33
34 #include STL_IOSTREAM
35 SG_USING_STD(cout);
36
37 //#include <simgear/debug/logstream.hxx>
38 //#include <simgear/misc/sgstream.hxx>
39 #include <simgear/misc/sg_path.hxx>
40
41 #ifdef FG_WEATHERCM
42 # include <WeatherCM/FGLocalWeatherDatabase.h>
43 #else
44 # include <Environment/environment_mgr.hxx>
45 # include <Environment/environment.hxx>
46 #endif
47
48 #include <Main/fg_props.hxx>
49 #include <Main/globals.hxx>
50 #include <Airports/runways.hxx>
51
52 #include "atis.hxx"
53 #include "commlist.hxx"
54 //#include "atislist.hxx"
55 #include "ATCdisplay.hxx"
56 #include "ATCutils.hxx"
57 #include "ATCmgr.hxx"
58
59 // Constructor
60 FGATIS::FGATIS() :
61 display(false),
62 displaying(false),
63 transmission(""),
64 trans_ident(""),
65 atis_failed(false),
66 refname("atis")
67 //type(ATIS)
68 {
69         vPtr = globals->get_ATC_mgr()->GetVoicePointer(ATIS);
70         voiceOK = (vPtr == NULL ? false : true);
71 }
72
73 // Destructor
74 FGATIS::~FGATIS() {
75 }
76
77 // Main update function - checks whether we are displaying or not the correct message.
78 void FGATIS::Update(double dt) {
79         if(display) {
80                 if(displaying) {
81                         // Check if we need to update the message
82                         // - basically every hour and if the weather changes significantly at the station
83                         //globals->get_ATC_display()->ChangeRepeatingMessage(transmission);
84                 } else {
85                         // We need to get and display the message
86                         UpdateTransmission();
87                         //cout << "ATIS.CXX - calling ATCMgr to render transmission..." << endl;
88                         Render(transmission, refname, true);
89                         displaying = true;
90                 }
91         } else {
92                 // We shouldn't be displaying
93                 if(displaying) {
94                         //cout << "ATIS.CXX - calling NoRender()..." << endl;
95                         NoRender(refname);
96                         displaying = false;
97                 }
98         }
99 }
100
101 // Sets the actual broadcast ATIS transmission.
102 void FGATIS::UpdateTransmission() {
103         double visibility;
104         char buf[10];
105         int phonetic_id;
106         string phonetic_id_string;
107         string time_str = fgGetString("sim/time/gmt-string");
108         int hours;
109         // int minutes;
110         
111         #ifdef FG_WEATHERCM
112         sgVec3 position = { lat, lon, elev };
113         FGPhysicalProperty stationweather = WeatherDatabase->get(position);
114         #else
115         FGEnvironment stationweather =
116         globals->get_environment_mgr()->getEnvironment(lat, lon, elev);
117         #endif
118         
119         transmission = "";
120         
121         // UK CAA radiotelephony manual indicated ATIS transmissions start with "This is"
122         // Not sure about rest of the world though.
123         transmission += "This_is ";
124         // transmitted station name.
125         transmission += name;
126         // Add "Information"
127         transmission += " information";
128         
129         //cout << "In atis.cxx, time_str = " << time_str << '\n';
130         // Add the recording identifier
131         // For now we will assume we only transmit every hour
132         hours = atoi((time_str.substr(1,2)).c_str());   //Warning - this is fragile if the 
133         //time string format changes
134         //cout << "In atis.cxx, hours = " << hours << endl;
135         phonetic_id = current_commlist->GetCallSign(ident, hours, 0);
136         phonetic_id_string = GetPhoneticIdent(phonetic_id);
137         transmission += " ";
138         transmission += phonetic_id_string;
139         
140         // Output the recording time. - we'll just output the last whole hour for now.
141         // FIXME - this only gets GMT time but that appears to be all the clock outputs for now
142         //cout << "in atis.cxx, time = " << time_str << endl;
143         transmission = transmission + " / Weather " + ConvertNumToSpokenDigits((time_str.substr(0,3) + "00")) + " hours zulu";
144         
145         // Get the temperature
146         int temp;
147         #ifdef FG_WEATHERCM
148         temp = int(stationweather.Temperature - 273.15);
149         #else
150         temp = (int)stationweather.get_temperature_degc();
151         #endif
152         
153         // HACK ALERT - at the moment the new environment subsystem returns bogus temperatures
154         // FIXME - take out this hack when this gets fixed upstream
155         if((temp < -50) || (temp > 60)) {
156                 temp = 15;
157         }
158
159         sprintf(buf, "%i", abs(temp));
160         transmission += " / Temperature ";
161         if(temp < 0) {
162                 transmission += "minus ";
163         }
164         string tempstr1 = buf;
165         string tempstr2;
166         transmission += ConvertNumToSpokenDigits(tempstr1);
167         transmission += " degrees_Celsius";
168         
169         // Get the visibility
170         #ifdef FG_WEATHERCM
171         visibility = fgGetDouble("/environment/visibility-m");
172         #else
173         visibility = stationweather.get_visibility_m();
174         #endif
175         sprintf(buf, "%i", int(visibility/1600));
176         transmission += " / Visibility ";
177         tempstr1 = buf;
178         transmission += ConvertNumToSpokenDigits(tempstr1);
179         transmission += " miles";
180         
181         // Get the cloudbase
182         // FIXME: kludge for now
183         if (strcmp(fgGetString("/environment/clouds/layer[0]/type"), "clear")) {
184                 double cloudbase =
185                 fgGetDouble("/environment/clouds/layer[0]/elevation-ft");
186                 // For some reason the altitude returned doesn't seem to correspond to the actual cloud altitude.
187                 char buf3[10];
188                 char buf4[10];
189                 // cout << "cloudbase = " << cloudbase << endl;
190                 sprintf(buf3, "%i", int(cloudbase)/1000);
191                 sprintf(buf4, "%i", ((int)cloudbase % 1000)/100);
192                 transmission += " / Cloudbase";
193                 if(int(cloudbase)/1000) {
194                         tempstr1 = buf3;
195                         transmission = transmission + " " + ConvertNumToSpokenDigits(tempstr1) + " thousand";
196                 }
197                 if(((int)cloudbase % 1000)/100) {
198                         tempstr1 = buf4;
199                         transmission = transmission + " " + ConvertNumToSpokenDigits(tempstr1) + " hundred";
200                 }                       
201                 transmission += " feet";
202         }
203         
204         // Get the pressure / altimeter
205         
206         #ifndef FG_WEATHERCM
207         double altimeter = stationweather.get_pressure_sea_level_inhg();
208         sprintf(buf, "%.2f", altimeter);
209         transmission += " / Altimeter ";
210         tempstr1 = buf;
211         transmission += ConvertNumToSpokenDigits(tempstr1);
212         #endif
213         
214         // Based on the airport-id and wind get the active runway
215         //FGRunway *r;
216         SGPath path( globals->get_fg_root() );
217         path.append( "Airports" );
218         path.append( "runways.mk4" );
219         FGRunways runways( path.c_str() );
220         
221         #ifdef FG_WEATHERCM
222         //Set the heading to into the wind
223         double wind_x = stationweather.Wind[0];
224         double wind_y = stationweather.Wind[1];
225         
226         double speed = sqrt( wind_x*wind_x + wind_y*wind_y ) * SG_METER_TO_NM / (60.0*60.0);
227         double hdg;
228         
229         //If no wind use 270degrees
230         if(speed == 0) {
231                 hdg = 270;
232                 transmission += " / Winds_light_and_variable";
233         } else {
234                 // //normalize the wind to get the direction
235                 //wind_x /= speed; wind_y /= speed;
236                 
237                 hdg = - atan2 ( wind_x, wind_y ) * SG_RADIANS_TO_DEGREES ;
238                 if (hdg < 0.0)
239                         hdg += 360.0;
240                 
241                 //add a description of the wind to the transmission
242                 char buf5[10];
243                 char buf6[10];
244                 sprintf(buf5, "%i", int(speed));
245                 sprintf(buf6, "%i", int(hdg));
246                 tempstr1 = buf5;
247                 tempstr2 = buf6;
248                 transmission = transmission + " / Winds " + ConvertNumToSpokenDigits(tempstr1) + " knots from "
249                                             + ConvertNumToSpokenDigits(tempstr2) + " degrees";
250         }
251         #else
252         double speed = stationweather.get_wind_speed_kt();
253         double hdg = stationweather.get_wind_from_heading_deg();
254         if (speed == 0) {
255                 hdg = 270;      // This forces West-facing rwys to be used in no-wind situations
256                                 // which is consistent with Flightgear's initial setup.
257                 transmission += " / Winds_light_and_variable";
258         } else {
259                 // FIXME: get gust factor in somehow
260                 char buf5[10];
261                 char buf6[10];
262                 sprintf(buf5, "%i", int(speed));
263                 sprintf(buf6, "%i", int(hdg));
264                 tempstr1 = buf5;
265                 tempstr2 = buf6;
266                 transmission = transmission + " / Winds " + ConvertNumToSpokenDigits(tempstr1) + " knots from "
267                                             + ConvertNumToSpokenDigits(tempstr2) + " degrees";
268         }
269         #endif
270         
271         string rwy_no = runways.search(ident, int(hdg));
272         if(rwy_no != (string)"NN") {
273                 transmission += " / Landing_and_departing_runway ";
274                 transmission += ConvertRwyNumToSpokenString(atoi(rwy_no.c_str()));
275                 //cout << "in atis.cxx, r.rwy_no = " << rwy_no << " r.id = " << r->id << " r.heading = " << r->heading << endl;
276         }
277         
278         // Anything else?
279         
280         transmission += " / Advise_controller_on_initial_contact_you_have ";
281         transmission += phonetic_id_string;
282         transmission += " /// ";
283 }