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