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