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