]> git.mxchange.org Git - flightgear.git/blob - src/ATCDCL/ATISmgr.cxx
Merge branch 'next' of gitorious.org:fg/flightgear into next
[flightgear.git] / src / ATCDCL / ATISmgr.cxx
1 // ATISmgr.cxx - Implementation of FGATISMgr - a global Flightgear ATIS manager.
2 //
3 // Written by David Luff, started February 2002.
4 //
5 // Copyright (C) 2002  David C Luff - david.luff@nottingham.ac.uk
6 // Copyright (C) 2012  Thorsten Brehm
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 #ifdef HAVE_CONFIG_H
23 #  include <config.h>
24 #endif
25
26 #include <simgear/misc/sg_path.hxx>
27 #include <simgear/debug/logstream.hxx>
28 #include <simgear/structure/exception.hxx>
29
30 #include <Airports/simple.hxx>
31 #include <ATC/CommStation.hxx>
32 #include <Main/fg_props.hxx>
33
34 #include "ATISmgr.hxx"
35 #include "ATCutils.hxx"
36 #include "atis.hxx"
37
38 using flightgear::CommStation;
39
40 FGATISMgr::FGATISMgr() :
41     _currentUnit(0),
42     _maxCommRadios(2),
43 #ifdef ENABLE_AUDIO_SUPPORT
44     voice(true),
45     voice1(0)
46 #else
47     voice(false)
48 #endif
49 {
50     globals->set_ATIS_mgr(this);
51 }
52
53 FGATISMgr::~FGATISMgr()
54 {
55     globals->set_ATIS_mgr(NULL);
56
57     for (unsigned int unit = 0;unit < _maxCommRadios; ++unit)
58     {
59         delete radios[unit].station;
60         radios[unit].station = NULL;
61     }
62
63 #ifdef ENABLE_AUDIO_SUPPORT
64     delete voice1;
65 #endif
66 }
67
68 void FGATISMgr::bind()
69 {
70 }
71
72 void FGATISMgr::unbind()
73 {
74 }
75
76 void FGATISMgr::init()
77 {
78     lon_node  = fgGetNode("/position/longitude-deg", true);
79     lat_node  = fgGetNode("/position/latitude-deg",  true);
80     elev_node = fgGetNode("/position/altitude-ft",   true);
81
82     for (unsigned int unit = 0;unit < _maxCommRadios; ++unit)
83     {
84         CommRadioData data;
85         string ncunit = "comm[" + decimalNumeral(unit) + "]";
86         string commbase = "/instrumentation/" + ncunit;
87         string commfreq = commbase + "/frequencies/selected-mhz";
88
89         data.freq = fgGetNode(commfreq.c_str(), true);
90         data.station = new FGATIS(commbase);
91         radios.push_back(data);
92     }
93 }
94
95 void FGATISMgr::update(double dt)
96 {
97     // update only runs every now and then (1-2 per second)
98     if (++_currentUnit >= _maxCommRadios)
99         _currentUnit = 0;
100
101     FGATC* pStation = radios[_currentUnit].station;
102     if (pStation)
103         pStation->Update(dt * _maxCommRadios);
104
105     // Search the tuned frequencies
106     FreqSearch(_currentUnit);
107 }
108
109 // Return a pointer to an appropriate voice for a given type of ATC
110 // creating the voice if necessary - i.e. make sure exactly one copy
111 // of every voice in use exists in memory.
112 //
113 // TODO - in the future this will get more complex and dole out country/airport
114 // specific voices, and possible make sure that the same voice doesn't get used
115 // at different airports in quick succession if a large enough selection are available.
116 FGATCVoice* FGATISMgr::GetVoicePointer(const atc_type& type)
117 {
118     // TODO - implement me better - maintain a list of loaded voices and other voices!!
119     if(voice)
120     {
121         switch(type)
122         {
123         case ATIS: case AWOS:
124 #ifdef ENABLE_AUDIO_SUPPORT
125             // Delayed loading for all available voices, needed because the
126             // sound manager might not be initialized (at all) at this point.
127             // For now we'll do one hard-wired one
128
129             /* I've loaded the voice even if /sim/sound/pause is true
130              *  since I know no way of forcing load of the voice if the user
131              *  subsequently switches /sim/sound/audible to true.
132              *  (which is the right thing to do -- CLO) :-)
133              */
134             if (!voice1 && fgGetBool("/sim/sound/working")) {
135                 voice1 = new FGATCVoice;
136                 try {
137                     voice = voice1->LoadVoice("default");
138                 } catch ( sg_io_exception & e) {
139                     SG_LOG(SG_ATC, SG_ALERT, "Unable to load default voice : "
140                                             << e.getFormattedMessage().c_str());
141                     voice = false;
142                     delete voice1;
143                     voice1 = 0;
144                 }
145             }
146             if (voice)
147                 return voice1;
148 #endif
149             return NULL;
150         case TOWER:
151             return NULL;
152         case APPROACH:
153             return NULL;
154         case GROUND:
155             return NULL;
156         default:
157             return NULL;
158         }
159     }
160
161     return NULL;
162 }
163
164 class RangeFilter : public CommStation::Filter
165 {
166 public:
167     RangeFilter( const SGGeod & pos ) :
168       CommStation::Filter(),
169       _cart(SGVec3d::fromGeod(pos)),
170       _pos(pos)
171     {
172     }
173
174     virtual bool pass(FGPositioned* aPos) const
175     {
176         flightgear::CommStation * stn = dynamic_cast<flightgear::CommStation*>(aPos);
177         if( NULL == stn )
178             return false;
179
180         // do the range check in cartesian space, since the distances are potentially
181         // large enough that the geodetic functions become unstable
182         // (eg, station on opposite side of the planet)
183         double rangeM = SGMiscd::max( stn->rangeNm(), 10.0 ) * SG_NM_TO_METER;
184         double d2 = distSqr( aPos->cart(), _cart);
185
186         return d2 <= (rangeM * rangeM);
187     }
188 private:
189     SGVec3d _cart;
190     SGGeod _pos;
191 };
192
193 // Search for ATC stations by frequency
194 void FGATISMgr::FreqSearch(const unsigned int unit)
195 {
196     double frequency = radios[unit].freq->getDoubleValue();
197
198     // Note:  122.375 must be rounded DOWN to 12237 
199     // in order to be consistent with apt.dat et cetera.
200     int freqKhz = static_cast<int>(frequency * 100.0 + 0.25);
201
202     _aircraftPos = SGGeod::fromDegFt(lon_node->getDoubleValue(),
203         lat_node->getDoubleValue(), elev_node->getDoubleValue());
204
205     RangeFilter rangeFilter(_aircraftPos );
206     CommStation* sta = CommStation::findByFreq(freqKhz, _aircraftPos, &rangeFilter );
207     radios[unit].station->SetStation(sta);
208 }