]> git.mxchange.org Git - flightgear.git/blob - src/ATCDCL/ATCmgr.hxx
Merge branch 'jt/runway' into next
[flightgear.git] / src / ATCDCL / ATCmgr.hxx
1 // ATCMgr.hxx - definition of FGATCMgr 
2 // - a global management class for FlightGear generated ATC
3 //
4 // Written by David Luff, started February 2002.
5 //
6 // Copyright (C) 2002  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 #ifndef _FG_ATCMGR_HXX
23 #define _FG_ATCMGR_HXX
24
25 #include <simgear/structure/subsystem_mgr.hxx>
26
27 #include <Main/fg_props.hxx>
28 #include <GUI/gui.h>
29
30 #include <string>
31 #include <list>
32 #include <map>
33
34 #include "tower.hxx"
35 #include "approach.hxx"
36
37 using std::string;
38 using std::list;
39 using std::map;
40
41 // Structure for holding details of the ATC frequencies at a given airport, and whether they are in the active list or not.
42 // These can then be cross referenced with the commlists which are stored by frequency or bucket.
43 // Non-available services are denoted by a frequency of zero.
44 // These structures are only intended to be created for in-use airports, and removed when no longer needed. 
45 struct AirportATC {
46         AirportATC();
47         
48     float lon;
49     float lat;
50     float elev;
51     float atis_freq;
52     bool atis_active;
53     float tower_freq;
54     bool tower_active;
55     float ground_freq;
56     bool ground_active;
57     //float approach_freq;
58     //bool approach_active;
59     //float departure_freq;
60     //bool departure_active;
61         
62         // NOTE - the *_active flags determine whether the service is active in atc_list,
63         // *NOT* whether the tower etc is closed or not!!!!
64
65     // Flags to ensure the stations don't get wrongly deactivated
66     bool set_by_AI;     // true when the AI manager has activated this station
67         unsigned int numAI;     // Ref count of the number of AI planes registered
68     bool set_by_comm[2][ATC_NUM_TYPES]; // true when the relevant comm_freq has activated this station and type
69 };
70
71 class FGATCMgr : public SGSubsystem
72 {
73
74 private:
75
76         bool initDone;  // Hack - guard against update getting called before init
77
78     // A map of airport ID vs frequencies and ATC provision
79     typedef map < string, AirportATC* > airport_atc_map_type;
80     typedef airport_atc_map_type::iterator airport_atc_map_iterator;
81     typedef airport_atc_map_type::const_iterator airport_atc_map_const_iterator;
82
83     airport_atc_map_type airport_atc_map;
84     airport_atc_map_iterator airport_atc_map_itr;
85
86     // A list of pointers to all currently active ATC classes
87     typedef list <FGATC*> atc_list_type;
88     typedef atc_list_type::iterator atc_list_iterator;
89     typedef atc_list_type::const_iterator atc_list_const_iterator;
90
91     // Everything put in this list should be created dynamically
92     // on the heap and ***DELETED WHEN REMOVED!!!!!***
93     atc_list_type atc_list;
94     atc_list_iterator atc_list_itr;
95     // Any member function of FGATCMgr is permitted to leave this iterator pointing
96     // at any point in or at the end of the list.
97     // Hence any new access must explicitly first check for atc_list.end() before dereferencing.
98
99     // Position of the Users Aircraft
100     double lon;
101     double lat;
102     double elev;
103
104     // Type of ATC control that the user's radios are tuned to.
105     atc_type comm_type[2];
106         
107         // Pointer to the ATC station that the user is currently tuned into.
108         FGATC* comm_atc_ptr[2];
109
110     double comm_freq[2];
111
112     // Pointers to users current communication frequencies.
113     SGPropertyNode_ptr comm_node[2];
114
115     // Pointers to current users position
116     SGPropertyNode_ptr lon_node;
117     SGPropertyNode_ptr lat_node;
118     SGPropertyNode_ptr elev_node;
119
120     // Position of the ATC that the comm radios are tuned to in order to decide 
121         // whether transmission will be received.
122     double comm_x[2], comm_y[2], comm_z[2], comm_lon[2], comm_lat[2], comm_elev[2];
123
124     double comm_range[2], comm_effective_range[2];
125     bool comm_valid[2]; 
126     string comm_ident[2];
127     //string last_comm_ident[2];
128     //string approach_ident;
129     bool last_in_range;
130
131     //FGATIS atis;
132     //FGGround ground;
133     FGTower tower;
134     FGApproach approach;
135     //FGDeparture departure;
136         
137         // Voice related stuff
138         bool voice;                     // Flag - true if we are using voice
139 #ifdef ENABLE_AUDIO_SUPPORT
140         bool voiceOK;           // Flag - true if at least one voice has loaded OK
141         FGATCVoice* v1;
142 #endif
143
144 public:
145
146     FGATCMgr();
147     ~FGATCMgr();
148
149     void init();
150
151     void bind();
152
153     void unbind();
154
155     void update(double dt);
156
157     // Returns true if the airport is found in the map
158     bool GetAirportATCDetails(const string& icao, AirportATC* a);
159
160     // Return a pointer to a given sort of ATC at a given airport and activate if necessary
161         // Returns NULL if service doesn't exist - calling function should check for this.
162     FGATC* GetATCPointer(const string& icao, const atc_type& type);
163         
164         // Return a pointer to an appropriate voice for a given type of ATC
165         // creating the voice if necessary - ie. make sure exactly one copy
166         // of every voice in use exists in memory.
167         //
168         // TODO - in the future this will get more complex and dole out country/airport
169         // specific voices, and possible make sure that the same voice doesn't get used
170         // at different airports in quick succession if a large enough selection are available.
171         FGATCVoice* GetVoicePointer(const atc_type& type);
172         
173         atc_type GetComm1ATCType() { return(comm_type[0]); }
174         FGATC* GetComm1ATCPointer() { return(comm_atc_ptr[0]); }
175         atc_type GetComm2ATCType() { return(comm_type[1]); }
176         FGATC* GetComm2ATCPointer() { return(comm_atc_ptr[1]); }
177         
178         // Get the frequency of a given service at a given airport
179         // Returns zero if not found
180         unsigned short int GetFrequency(const string& ident, const atc_type& tp);
181         
182         // Register the fact that the AI system wants to activate an airport
183         bool AIRegisterAirport(const string& ident);
184         
185         // Register the fact that the comm radio is tuned to an airport
186         bool CommRegisterAirport(const string& ident, int chan, const atc_type& tp);
187         
188 private:
189
190     // Remove a class from the atc_list and delete it from memory
191         // *if* no other comm channel or AI plane is using it.
192     void CommRemoveFromList(const string& id, const atc_type& tp, int chan);
193
194     // Remove a class from the atc_list and delete it from memory
195         // Should be called from the above - not directly!!
196     void RemoveFromList(const string& id, const atc_type& tp);
197
198     // Return a pointer to a class in the list given ICAO code and type
199         // (external interface to this is through GetATCPointer) 
200         // Return NULL if the given service is not in the list
201         // - *** THE CALLING FUNCTION MUST CHECK FOR THIS ***
202     FGATC* FindInList(const string& id, const atc_type& tp);
203
204     // Search the specified channel for stations on the same frequency and in range.
205     void FreqSearch(int channel);
206         
207         // Search ATC stations by area in order that we appear 'on the radar'
208         void AreaSearch(); 
209
210 };
211
212 #endif  // _FG_ATCMGR_HXX