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