]> git.mxchange.org Git - flightgear.git/blob - src/ATC/ground.hxx
Read KEMT ground network from file instead of hardwiring it into the code.
[flightgear.git] / src / ATC / ground.hxx
1 // FGGround - a class to provide ground control at larger airports.
2 //
3 // Written by David Luff, started March 2002.
4 //
5 // Copyright (C) 2002  David C. Luff - david.luff@nottingham.ac.uk
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 // General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20
21 #ifndef _FG_GROUND_HXX
22 #define _FG_GROUND_HXX
23
24 #include STL_IOSTREAM
25 #include STL_STRING
26
27 SG_USING_STD(string);
28 #ifndef SG_HAVE_NATIVE_SGI_COMPILERS
29 SG_USING_STD(ios);
30 #endif
31
32 #include <map>
33 #include <vector>
34 #include <list>
35 #include <simgear/math/point3d.hxx>
36 #include <simgear/misc/sgstream.hxx>
37 #include <simgear/math/sg_geodesy.hxx>
38
39 #include "ATC.hxx"
40 #include "ATCProjection.hxx"
41
42 SG_USING_STD(map);
43 SG_USING_STD(vector);
44 SG_USING_STD(list);
45
46 //////////////////////////////////////////////////////
47 // Types for the logical network data structure
48 typedef enum arc_type {
49         RUNWAY,
50         TAXIWAY
51 };
52
53 typedef enum node_type {
54         GATE,
55         APRON,
56         HOLD,
57         JUNCTION,
58         TJUNCTION
59 };
60
61 enum GateType {
62         TRANSPORT_PASSENGER,
63         TRANSPORT_PASSENGER_NARROWBODY,
64         TRANSPORT_PASSENGER_WIDEBODY,
65         TRANSPORT_CARGO,
66         GA_LOCAL,
67         GA_LOCAL_SINGLE,
68         GA_LOCAL_TWIN,
69         GA_TRANSIENT,
70         GA_TRANSIENT_SINGLE,
71         GA_TRANSIENT_TWIN,
72         OTHER   // ie. anything goes!!
73 };
74
75 typedef enum network_element_type {
76         NODE,
77         ARC
78 };
79
80 struct ground_network_element {
81         network_element_type struct_type;
82 };
83
84 struct arc : public ground_network_element {
85         int distance;
86         string name;
87         arc_type type;
88         bool directed;  //false if 2-way, true if 1-way.  
89         //This is a can of worms since arcs might be one way in different directions under different circumstances
90         unsigned int n1;        // The nodeID of the first node
91         unsigned int n2;        // The nodeID of the second node
92         // If the arc is directed then flow is normally from n1 to n2.  See the above can of worms comment though.
93 };
94
95 typedef vector <arc*> arc_array_type;   // This was and may become again a list instead of vector
96 typedef arc_array_type::iterator arc_array_iterator;
97 typedef arc_array_type::const_iterator arc_array_const_iterator; 
98
99 struct node : public ground_network_element {
100         unsigned int nodeID;    //each node in an airport needs a unique ID number - this is ZERO-BASED to match array position
101         Point3D pos;
102         Point3D orthoPos;
103         char* name;
104         node_type type;
105         arc_array_type arcs;
106         double max_turn_radius;
107 };
108
109 typedef vector <node*> node_array_type;
110 typedef node_array_type::iterator node_array_iterator;
111 typedef node_array_type::const_iterator node_array_const_iterator;
112
113 struct Gate : public node {
114         GateType gateType;
115         int max_weight; //units??
116         //airline_code airline; //For the future - we don't have any airline codes ATM
117         int id; // The gate number in the logical scheme of things
118         string sid;     // The real-world gate letter/number
119         //node* pNode;
120         bool used;
121         double heading; // The direction the parked-up plane should point in degrees
122 };
123
124 typedef vector < Gate > gate_vec_type;
125 typedef gate_vec_type::iterator gate_vec_iterator;
126 typedef gate_vec_type::const_iterator gate_vec_const_iterator;
127
128 // A map of gate vs. the logical (internal FGFS) gate ID
129 typedef map < int, Gate > gate_map_type;
130 typedef gate_map_type::iterator gate_map_iterator;
131 typedef gate_map_type::const_iterator gate_map_const_iterator;
132
133 // Runways - all the runway stuff is likely to change in the future
134 typedef struct Rwy {
135         int id; //note this is a very simplified scheme for now - R & L are not differentiated
136         //It should work for simple one rwy airports
137         node_array_type exits;  //Array of available exits from runway
138         // should probably add an FGRunway structure here as well eventually
139         // Eventually we will also want some encoding of real-life preferred runways
140         // This will get us up and running for single runway airports though.
141 };
142 typedef vector < Rwy > runway_array_type;
143 typedef runway_array_type::iterator runway_array_iterator;
144 typedef runway_array_type::const_iterator runway_array_const_iterator;
145
146 // end logical network types
147 ///////////////////////////////////////////////////////
148
149 ///////////////////////////////////////////////////////
150 // Structures to use the network
151
152 // A path through the network 
153 typedef vector < ground_network_element* > ground_network_path_type;
154 typedef ground_network_path_type::iterator ground_network_path_iterator;
155 typedef ground_network_path_type::const_iterator ground_network_path_const_iterator;
156
157 //////////////////////////////////////////////////////////////////////////////////////////
158
159 // Planes active within the ground network.
160 // somewhere in the ATC/AI system we are going to have defined something like
161 // typedef struct plane_rec
162 // list <PlaneRec> plane_rec_list_type
163 /*
164 // A more specialist plane rec to include ground information
165 typedef struct ground_rec {
166     plane_rec plane;
167     point current_pos;
168     node destination;
169     node last_clearance;
170     bool cleared;  // set true when the plane has been cleared to somewhere
171     bool incoming; //true for arrivals, false for departures
172     // status?
173     // Almost certainly need to add more here
174 };
175
176 typedef list<ground_rec*> ground_rec_list;
177 typedef ground_rec_list::iterator ground_rec_list_itr;
178 typedef ground_rec_list::const_iterator ground_rec_list_const_itr;
179 */
180 //////////////////////////////////////////////////////////////////////////////////////////
181
182 ///////////////////////////////////////////////////////////////////////////////
183 //
184 // FGGround
185 //
186 ///////////////////////////////////////////////////////////////////////////////
187 class FGGround : public FGATC {
188
189 public:
190         FGGround();
191         ~FGGround();
192     void Init();
193
194     void Update();
195         
196         inline char get_type() const { return type; }
197         inline double get_lon() const { return lon; }
198         inline double get_lat() const { return lat; }
199         inline double get_elev() const { return elev; }
200         inline double get_x() const { return x; }
201         inline double get_y() const { return y; }
202         inline double get_z() const { return z; }
203         inline int get_freq() const { return freq; }
204         inline int get_range() const { return range; }
205         inline const char* GetIdent() { return ident.c_str(); }
206         inline string get_trans_ident() { return trans_ident; }
207         inline string get_name() { return name; }
208         inline atc_type GetType() { return GROUND; }
209
210     inline void SetDisplay() {display = true;}
211     inline void SetNoDisplay() {display = false;}
212
213     // Its possible that NewArrival and NewDeparture should simply be rolled into Request.
214
215     // Contact ground control on arrival, assumed to request any gate
216     //void NewArrival(plane_rec plane);
217
218     // Contact ground control on departure, assumed to request currently active runway.
219     //void NewDeparture(plane_rec plane);
220
221     // Contact ground control when the calling routine doesn't know if arrival
222     // or departure is appropriate.
223     //void NewContact(plane_rec plane);
224
225     // Make a request of ground control.
226     //void Request(ground_request request);
227         
228         // Randomly fill some of the available gates and GA parking spots with planes
229         void PopulateGates();
230         
231         // Return a suitable gate (maybe this should be a list of suitable gates so the plane or controller can choose the closest one)
232         void ReturnGate(Gate &gate, GateType type);
233         
234         //The following two functions have been made public for now but may go private with a higher level accessor at some point
235         // Return the internal ID of a random, suitable, unused gate
236         // For now we are simply implementing as any random unused gate
237         int GetRandomGateID();
238         // Return a pointer to a node based on the gate ID
239         Gate* GetGateNode(int gateID);
240         
241         // Runway stuff - this might change in the future.
242         // Get a list of exits from a given runway
243         // It is up to the calling function to check for non-zero size of returned array before use
244         node_array_type GetExits(int rwyID);
245         
246         // Get a path from one node to another
247         ground_network_path_type GetPath(node* A, node* B); 
248
249 private:
250
251     // Need a data structure to hold details of the various active planes
252     // Need a data structure to hold details of the logical network
253     // including which gates are filled - or possibly another data structure
254     // with the positions of the inactive planes.
255     // Need a data structure to hold outstanding communications from aircraft.
256     // Possibly need a data structure to hold outstanding communications to aircraft.
257
258         // The logical network
259         // NODES WILL BE STORED IN THE NETWORK IN ORDER OF nodeID NUMBER
260         // ie. NODE 5 WILL BE AT network[5]
261     node_array_type network;
262         
263         // A map of all the gates indexed against internal (FGFS) ID
264         gate_map_type gates;
265         gate_map_iterator gatesItr;
266         
267         // Runway stuff - this might change in the future.
268         //runway_array_type runways;    // STL way
269         Rwy runways[36];        // quick hack!
270
271         FGATCAlignedProjection ortho;
272         
273         // Planes currently active
274     //ground_rec_list ground_traffic;
275
276     // Find the shortest route through the logical network between two points.
277     //FindShortestRoute(point a, point b);
278
279     // Project a point in WGS84 lat/lon onto the local gnomonic.
280     //ConvertWGS84ToXY(sgVec3 wgs84, point xy);
281
282     // Assign a gate or parking location to a new arrival
283     //AssignGate(ground_rec &g);
284
285     // Generate the next clearance for an airplane
286     //NextClearance(ground_rec &g);
287         
288         char type;
289         double lon, lat;
290         double elev;
291         double x, y, z;
292         int freq;
293         int range;
294         bool display;           // Flag to indicate whether we should be outputting to the ATC display.
295         bool displaying;                // Flag to indicate whether we are outputting to the ATC display.
296         string ident;           // Code of the airport its at.
297         string name;            // Name generally used in transmissions.
298         // for failure modeling
299         string trans_ident;             // transmitted ident
300         bool ground_failed;             // ground failed?
301         bool networkLoadOK;             // Indicates whether LoadNetwork returned true or false at last attempt
302         
303         friend istream& operator>> ( istream&, FGGround& );
304         
305         // Load the logical ground network for this airport from file.
306         // Return true if successfull.
307         bool LoadNetwork();
308         
309         // Parse a runway exit string and push the supplied node pointer onto the runway exit list
310         void ParseRwyExits(node* np, char* es);
311 };
312
313 inline istream&
314 operator >> ( istream& in, FGGround& g )
315 {
316         double f;
317         char ch;
318         
319         in >> g.type;
320         
321         if ( g.type == '[' )
322                 return in >> skipeol;
323         
324         in >> g.lat >> g.lon >> g.elev >> f >> g.range 
325         >> g.ident;
326         
327         g.name = "";
328         in >> ch;
329         g.name += ch;
330         while(1) {
331                 //in >> noskipws
332                 in.unsetf(ios::skipws);
333                 in >> ch;
334                 g.name += ch;
335                 if((ch == '"') || (ch == 0x0A)) {
336                         break;
337                 }   // we shouldn't need the 0x0A but it makes a nice safely in case someone leaves off the "
338         }
339         in.setf(ios::skipws);
340         //cout << "tower.name = " << t.name << '\n';
341         
342         g.freq = (int)(f*100.0 + 0.5);
343         
344         // cout << g.ident << endl;
345         
346         // generate cartesian coordinates
347         Point3D geod( g.lon * SGD_DEGREES_TO_RADIANS, g.lat * SGD_DEGREES_TO_RADIANS, g.elev );
348         Point3D cart = sgGeodToCart( geod );
349         g.x = cart.x();
350         g.y = cart.y();
351         g.z = cart.z();
352         
353         g.trans_ident = g.ident;
354         g.ground_failed = false;
355         
356         return in >> skipeol;
357 }
358
359 #endif  // _FG_GROUND_HXX
360